Thu Mar 3 05:49:08 2022 UTC ()
usbnet: Don't issue a detach event if we never issued an attach one.


(riastradh)
diff -r1.61 -r1.62 src/sys/dev/usb/usbnet.c

cvs diff -r1.61 -r1.62 src/sys/dev/usb/usbnet.c (switch to unified diff)

--- src/sys/dev/usb/usbnet.c 2022/03/03 05:49:00 1.61
+++ src/sys/dev/usb/usbnet.c 2022/03/03 05:49:07 1.62
@@ -1,1734 +1,1741 @@ @@ -1,1734 +1,1741 @@
1/* $NetBSD: usbnet.c,v 1.61 2022/03/03 05:49:00 riastradh Exp $ */ 1/* $NetBSD: usbnet.c,v 1.62 2022/03/03 05:49:07 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2019 Matthew R. Green 4 * Copyright (c) 2019 Matthew R. Green
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * Common code shared between USB network drivers. 30 * Common code shared between USB network drivers.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.61 2022/03/03 05:49:00 riastradh Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.62 2022/03/03 05:49:07 riastradh Exp $");
35 35
36#include <sys/param.h> 36#include <sys/param.h>
37#include <sys/kernel.h> 37#include <sys/kernel.h>
38#include <sys/kmem.h> 38#include <sys/kmem.h>
39#include <sys/module.h> 39#include <sys/module.h>
40#include <sys/atomic.h> 40#include <sys/atomic.h>
41 41
42#include <dev/usb/usbnet.h> 42#include <dev/usb/usbnet.h>
43#include <dev/usb/usbhist.h> 43#include <dev/usb/usbhist.h>
44 44
45struct usbnet_cdata { 45struct usbnet_cdata {
46 struct usbnet_chain *uncd_tx_chain; 46 struct usbnet_chain *uncd_tx_chain;
47 struct usbnet_chain *uncd_rx_chain; 47 struct usbnet_chain *uncd_rx_chain;
48 48
49 int uncd_tx_prod; 49 int uncd_tx_prod;
50 int uncd_tx_cnt; 50 int uncd_tx_cnt;
51}; 51};
52 52
53struct usbnet_private { 53struct usbnet_private {
54 /* 54 /*
55 * - unp_core_lock protects most of this structure, the public one, 55 * - unp_core_lock protects most of this structure, the public one,
56 * and the MII / media data. 56 * and the MII / media data.
57 * - unp_rxlock protects the rx path and its data 57 * - unp_rxlock protects the rx path and its data
58 * - unp_txlock protects the tx path and its data 58 * - unp_txlock protects the tx path and its data
59 * - unp_detachcv handles detach vs open references 59 * - unp_detachcv handles detach vs open references
60 * 60 *
61 * the lock ordering is: 61 * the lock ordering is:
62 * ifnet lock -> unp_core_lock -> unp_rxlock -> unp_txlock 62 * ifnet lock -> unp_core_lock -> unp_rxlock -> unp_txlock
63 * - ifnet lock is not needed for unp_core_lock, but if ifnet lock is 63 * - ifnet lock is not needed for unp_core_lock, but if ifnet lock is
64 * involved, it must be taken first 64 * involved, it must be taken first
65 */ 65 */
66 kmutex_t unp_core_lock; 66 kmutex_t unp_core_lock;
67 kmutex_t unp_rxlock; 67 kmutex_t unp_rxlock;
68 kmutex_t unp_txlock; 68 kmutex_t unp_txlock;
69 kcondvar_t unp_detachcv; 69 kcondvar_t unp_detachcv;
70 70
71 struct usbnet_cdata unp_cdata; 71 struct usbnet_cdata unp_cdata;
72 72
73 struct ethercom unp_ec; 73 struct ethercom unp_ec;
74 struct mii_data unp_mii; 74 struct mii_data unp_mii;
75 struct usb_task unp_mcasttask; 75 struct usb_task unp_mcasttask;
76 struct usb_task unp_ticktask; 76 struct usb_task unp_ticktask;
77 struct callout unp_stat_ch; 77 struct callout unp_stat_ch;
78 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX]; 78 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX];
79 79
80 bool unp_dying; 80 bool unp_dying;
81 bool unp_stopping; 81 bool unp_stopping;
82 bool unp_attached; 82 bool unp_attached;
83 bool unp_ifp_attached; 83 bool unp_ifp_attached;
84 bool unp_link; 84 bool unp_link;
85 85
86 int unp_refcnt; 86 int unp_refcnt;
87 int unp_timer; 87 int unp_timer;
88 unsigned short unp_if_flags; 88 unsigned short unp_if_flags;
89 unsigned unp_number; 89 unsigned unp_number;
90 90
91 krndsource_t unp_rndsrc; 91 krndsource_t unp_rndsrc;
92 92
93 struct timeval unp_rx_notice; 93 struct timeval unp_rx_notice;
94 struct timeval unp_tx_notice; 94 struct timeval unp_tx_notice;
95 struct timeval unp_intr_notice; 95 struct timeval unp_intr_notice;
96}; 96};
97 97
98#define un_cdata(un) (&(un)->un_pri->unp_cdata) 98#define un_cdata(un) (&(un)->un_pri->unp_cdata)
99 99
100volatile unsigned usbnet_number; 100volatile unsigned usbnet_number;
101 101
102static int usbnet_modcmd(modcmd_t, void *); 102static int usbnet_modcmd(modcmd_t, void *);
103 103
104#ifdef USB_DEBUG 104#ifdef USB_DEBUG
105#ifndef USBNET_DEBUG 105#ifndef USBNET_DEBUG
106#define usbnetdebug 0 106#define usbnetdebug 0
107#else 107#else
108static int usbnetdebug = 0; 108static int usbnetdebug = 0;
109 109
110SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup") 110SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup")
111{ 111{
112 int err; 112 int err;
113 const struct sysctlnode *rnode; 113 const struct sysctlnode *rnode;
114 const struct sysctlnode *cnode; 114 const struct sysctlnode *cnode;
115 115
116 err = sysctl_createv(clog, 0, NULL, &rnode, 116 err = sysctl_createv(clog, 0, NULL, &rnode,
117 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet", 117 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet",
118 SYSCTL_DESCR("usbnet global controls"), 118 SYSCTL_DESCR("usbnet global controls"),
119 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 119 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
120 120
121 if (err) 121 if (err)
122 goto fail; 122 goto fail;
123 123
124 /* control debugging printfs */ 124 /* control debugging printfs */
125 err = sysctl_createv(clog, 0, &rnode, &cnode, 125 err = sysctl_createv(clog, 0, &rnode, &cnode,
126 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 126 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
127 "debug", SYSCTL_DESCR("Enable debugging output"), 127 "debug", SYSCTL_DESCR("Enable debugging output"),
128 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL); 128 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL);
129 if (err) 129 if (err)
130 goto fail; 130 goto fail;
131 131
132 return; 132 return;
133fail: 133fail:
134 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 134 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
135} 135}
136 136
137#endif /* USBNET_DEBUG */ 137#endif /* USBNET_DEBUG */
138#endif /* USB_DEBUG */ 138#endif /* USB_DEBUG */
139 139
140#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D) 140#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D)
141#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D) 141#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D)
142#define USBNETHIST_FUNC() USBHIST_FUNC() 142#define USBNETHIST_FUNC() USBHIST_FUNC()
143#define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) 143#define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug)
144#define USBNETHIST_CALLARGS(FMT,A,B,C,D) \ 144#define USBNETHIST_CALLARGS(FMT,A,B,C,D) \
145 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D) 145 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D)
146#define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \ 146#define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \
147 USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D) 147 USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D)
148 148
149/* Callback vectors. */ 149/* Callback vectors. */
150 150
151static void 151static void
152uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) 152uno_stop(struct usbnet *un, struct ifnet *ifp, int disable)
153{ 153{
154 KASSERTMSG(!un->un_pri->unp_ifp_attached || IFNET_LOCKED(ifp), 154 KASSERTMSG(!un->un_pri->unp_ifp_attached || IFNET_LOCKED(ifp),
155 "%s", ifp->if_xname); 155 "%s", ifp->if_xname);
156 usbnet_isowned_core(un); 156 usbnet_isowned_core(un);
157 if (un->un_ops->uno_stop) 157 if (un->un_ops->uno_stop)
158 (*un->un_ops->uno_stop)(ifp, disable); 158 (*un->un_ops->uno_stop)(ifp, disable);
159} 159}
160 160
161static int 161static int
162uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 162uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
163{ 163{
164 /* 164 /*
165 * There are cases where IFNET_LOCK will not be held when we 165 * There are cases where IFNET_LOCK will not be held when we
166 * are called (e.g. add/delete multicast address), so we can't 166 * are called (e.g. add/delete multicast address), so we can't
167 * assert it. 167 * assert it.
168 */ 168 */
169 if (un->un_ops->uno_ioctl) 169 if (un->un_ops->uno_ioctl)
170 return (*un->un_ops->uno_ioctl)(ifp, cmd, data); 170 return (*un->un_ops->uno_ioctl)(ifp, cmd, data);
171 return 0; 171 return 0;
172} 172}
173 173
174static int 174static int
175uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 175uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
176{ 176{
177 /* See above. */ 177 /* See above. */
178 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); 178 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data);
179} 179}
180 180
181static int 181static int
182uno_init(struct usbnet *un, struct ifnet *ifp) 182uno_init(struct usbnet *un, struct ifnet *ifp)
183{ 183{
184 KASSERT(IFNET_LOCKED(ifp)); 184 KASSERT(IFNET_LOCKED(ifp));
185 return (*un->un_ops->uno_init)(ifp); 185 return (*un->un_ops->uno_init)(ifp);
186} 186}
187 187
188static int 188static int
189uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) 189uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
190{ 190{
191 usbnet_isowned_core(un); 191 usbnet_isowned_core(un);
192 return (*un->un_ops->uno_read_reg)(un, phy, reg, val); 192 return (*un->un_ops->uno_read_reg)(un, phy, reg, val);
193} 193}
194 194
195static int 195static int
196uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) 196uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
197{ 197{
198 usbnet_isowned_core(un); 198 usbnet_isowned_core(un);
199 return (*un->un_ops->uno_write_reg)(un, phy, reg, val); 199 return (*un->un_ops->uno_write_reg)(un, phy, reg, val);
200} 200}
201 201
202static void 202static void
203uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) 203uno_mii_statchg(struct usbnet *un, struct ifnet *ifp)
204{ 204{
205 usbnet_isowned_core(un); 205 usbnet_isowned_core(un);
206 (*un->un_ops->uno_statchg)(ifp); 206 (*un->un_ops->uno_statchg)(ifp);
207} 207}
208 208
209static unsigned 209static unsigned
210uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 210uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
211{ 211{
212 usbnet_isowned_tx(un); 212 usbnet_isowned_tx(un);
213 return (*un->un_ops->uno_tx_prepare)(un, m, c); 213 return (*un->un_ops->uno_tx_prepare)(un, m, c);
214} 214}
215 215
216static void 216static void
217uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) 217uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
218{ 218{
219 usbnet_isowned_rx(un); 219 usbnet_isowned_rx(un);
220 (*un->un_ops->uno_rx_loop)(un, c, total_len); 220 (*un->un_ops->uno_rx_loop)(un, c, total_len);
221} 221}
222 222
223static void 223static void
224uno_tick(struct usbnet *un) 224uno_tick(struct usbnet *un)
225{ 225{
226 if (un->un_ops->uno_tick) 226 if (un->un_ops->uno_tick)
227 (*un->un_ops->uno_tick)(un); 227 (*un->un_ops->uno_tick)(un);
228} 228}
229 229
230static void 230static void
231uno_intr(struct usbnet *un, usbd_status status) 231uno_intr(struct usbnet *un, usbd_status status)
232{ 232{
233 if (un->un_ops->uno_intr) 233 if (un->un_ops->uno_intr)
234 (*un->un_ops->uno_intr)(un, status); 234 (*un->un_ops->uno_intr)(un, status);
235} 235}
236 236
237/* Interrupt handling. */ 237/* Interrupt handling. */
238 238
239static struct mbuf * 239static struct mbuf *
240usbnet_newbuf(size_t buflen) 240usbnet_newbuf(size_t buflen)
241{ 241{
242 struct mbuf *m; 242 struct mbuf *m;
243 243
244 if (buflen > MCLBYTES) 244 if (buflen > MCLBYTES)
245 return NULL; 245 return NULL;
246 246
247 MGETHDR(m, M_DONTWAIT, MT_DATA); 247 MGETHDR(m, M_DONTWAIT, MT_DATA);
248 if (m == NULL) 248 if (m == NULL)
249 return NULL; 249 return NULL;
250 250
251 if (buflen > MHLEN - ETHER_ALIGN) { 251 if (buflen > MHLEN - ETHER_ALIGN) {
252 MCLGET(m, M_DONTWAIT); 252 MCLGET(m, M_DONTWAIT);
253 if (!(m->m_flags & M_EXT)) { 253 if (!(m->m_flags & M_EXT)) {
254 m_freem(m); 254 m_freem(m);
255 return NULL; 255 return NULL;
256 } 256 }
257 } 257 }
258 258
259 m_adj(m, ETHER_ALIGN); 259 m_adj(m, ETHER_ALIGN);
260 m->m_len = m->m_pkthdr.len = buflen; 260 m->m_len = m->m_pkthdr.len = buflen;
261 261
262 return m; 262 return m;
263} 263}
264 264
265/* 265/*
266 * usbnet_rxeof() is designed to be the done callback for rx completion. 266 * usbnet_rxeof() is designed to be the done callback for rx completion.
267 * it provides generic setup and finalisation, calls a different usbnet 267 * it provides generic setup and finalisation, calls a different usbnet
268 * rx_loop callback in the middle, which can use usbnet_enqueue() to 268 * rx_loop callback in the middle, which can use usbnet_enqueue() to
269 * enqueue a packet for higher levels (or usbnet_input() if previously 269 * enqueue a packet for higher levels (or usbnet_input() if previously
270 * using if_input() path.) 270 * using if_input() path.)
271 */ 271 */
272void 272void
273usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen, 273usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen,
274 int csum_flags, uint32_t csum_data, int mbuf_flags) 274 int csum_flags, uint32_t csum_data, int mbuf_flags)
275{ 275{
276 USBNETHIST_FUNC(); 276 USBNETHIST_FUNC();
277 struct ifnet * const ifp = usbnet_ifp(un); 277 struct ifnet * const ifp = usbnet_ifp(un);
278 struct usbnet_private * const unp __unused = un->un_pri; 278 struct usbnet_private * const unp __unused = un->un_pri;
279 struct mbuf *m; 279 struct mbuf *m;
280 280
281 USBNETHIST_CALLARGSN(5, "%jd: enter: len=%ju csf %#jx mbf %#jx", 281 USBNETHIST_CALLARGSN(5, "%jd: enter: len=%ju csf %#jx mbf %#jx",
282 unp->unp_number, buflen, csum_flags, mbuf_flags); 282 unp->unp_number, buflen, csum_flags, mbuf_flags);
283 283
284 usbnet_isowned_rx(un); 284 usbnet_isowned_rx(un);
285 285
286 m = usbnet_newbuf(buflen); 286 m = usbnet_newbuf(buflen);
287 if (m == NULL) { 287 if (m == NULL) {
288 DPRINTF("%jd: no memory", unp->unp_number, 0, 0, 0); 288 DPRINTF("%jd: no memory", unp->unp_number, 0, 0, 0);
289 if_statinc(ifp, if_ierrors); 289 if_statinc(ifp, if_ierrors);
290 return; 290 return;
291 } 291 }
292 292
293 m_set_rcvif(m, ifp); 293 m_set_rcvif(m, ifp);
294 m->m_pkthdr.csum_flags = csum_flags; 294 m->m_pkthdr.csum_flags = csum_flags;
295 m->m_pkthdr.csum_data = csum_data; 295 m->m_pkthdr.csum_data = csum_data;
296 m->m_flags |= mbuf_flags; 296 m->m_flags |= mbuf_flags;
297 memcpy(mtod(m, uint8_t *), buf, buflen); 297 memcpy(mtod(m, uint8_t *), buf, buflen);
298 298
299 /* push the packet up */ 299 /* push the packet up */
300 if_percpuq_enqueue(ifp->if_percpuq, m); 300 if_percpuq_enqueue(ifp->if_percpuq, m);
301} 301}
302 302
303void 303void
304usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen) 304usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen)
305{ 305{
306 USBNETHIST_FUNC(); 306 USBNETHIST_FUNC();
307 struct ifnet * const ifp = usbnet_ifp(un); 307 struct ifnet * const ifp = usbnet_ifp(un);
308 struct usbnet_private * const unp __unused = un->un_pri; 308 struct usbnet_private * const unp __unused = un->un_pri;
309 struct mbuf *m; 309 struct mbuf *m;
310 310
311 USBNETHIST_CALLARGSN(5, "%jd: enter: buf %#jx len %ju", 311 USBNETHIST_CALLARGSN(5, "%jd: enter: buf %#jx len %ju",
312 unp->unp_number, (uintptr_t)buf, buflen, 0); 312 unp->unp_number, (uintptr_t)buf, buflen, 0);
313 313
314 usbnet_isowned_rx(un); 314 usbnet_isowned_rx(un);
315 315
316 m = usbnet_newbuf(buflen); 316 m = usbnet_newbuf(buflen);
317 if (m == NULL) { 317 if (m == NULL) {
318 if_statinc(ifp, if_ierrors); 318 if_statinc(ifp, if_ierrors);
319 return; 319 return;
320 } 320 }
321 321
322 m_set_rcvif(m, ifp); 322 m_set_rcvif(m, ifp);
323 memcpy(mtod(m, char *), buf, buflen); 323 memcpy(mtod(m, char *), buf, buflen);
324 324
325 /* push the packet up */ 325 /* push the packet up */
326 if_input(ifp, m); 326 if_input(ifp, m);
327} 327}
328 328
329/* 329/*
330 * A frame has been uploaded: pass the resulting mbuf chain up to 330 * A frame has been uploaded: pass the resulting mbuf chain up to
331 * the higher level protocols. 331 * the higher level protocols.
332 */ 332 */
333static void 333static void
334usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 334usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
335{ 335{
336 USBNETHIST_FUNC(); 336 USBNETHIST_FUNC();
337 struct usbnet_chain * const c = priv; 337 struct usbnet_chain * const c = priv;
338 struct usbnet * const un = c->unc_un; 338 struct usbnet * const un = c->unc_un;
339 struct usbnet_private * const unp = un->un_pri; 339 struct usbnet_private * const unp = un->un_pri;
340 uint32_t total_len; 340 uint32_t total_len;
341 341
342 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", 342 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx",
343 unp->unp_number, status, (uintptr_t)xfer, 0); 343 unp->unp_number, status, (uintptr_t)xfer, 0);
344 344
345 mutex_enter(&unp->unp_rxlock); 345 mutex_enter(&unp->unp_rxlock);
346 346
347 if (unp->unp_dying || unp->unp_stopping || 347 if (unp->unp_dying || unp->unp_stopping ||
348 status == USBD_INVAL || status == USBD_NOT_STARTED || 348 status == USBD_INVAL || status == USBD_NOT_STARTED ||
349 status == USBD_CANCELLED) 349 status == USBD_CANCELLED)
350 goto out; 350 goto out;
351 351
352 if (status != USBD_NORMAL_COMPLETION) { 352 if (status != USBD_NORMAL_COMPLETION) {
353 if (usbd_ratecheck(&unp->unp_rx_notice)) 353 if (usbd_ratecheck(&unp->unp_rx_notice))
354 device_printf(un->un_dev, "usb errors on rx: %s\n", 354 device_printf(un->un_dev, "usb errors on rx: %s\n",
355 usbd_errstr(status)); 355 usbd_errstr(status));
356 if (status == USBD_STALLED) 356 if (status == USBD_STALLED)
357 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]); 357 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]);
358 goto done; 358 goto done;
359 } 359 }
360 360
361 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 361 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
362 362
363 if (total_len > un->un_rx_bufsz) { 363 if (total_len > un->un_rx_bufsz) {
364 aprint_error_dev(un->un_dev, 364 aprint_error_dev(un->un_dev,
365 "rxeof: too large transfer (%u > %u)\n", 365 "rxeof: too large transfer (%u > %u)\n",
366 total_len, un->un_rx_bufsz); 366 total_len, un->un_rx_bufsz);
367 goto done; 367 goto done;
368 } 368 }
369 369
370 uno_rx_loop(un, c, total_len); 370 uno_rx_loop(un, c, total_len);
371 usbnet_isowned_rx(un); 371 usbnet_isowned_rx(un);
372 372
373done: 373done:
374 if (unp->unp_dying || unp->unp_stopping) 374 if (unp->unp_dying || unp->unp_stopping)
375 goto out; 375 goto out;
376 376
377 mutex_exit(&unp->unp_rxlock); 377 mutex_exit(&unp->unp_rxlock);
378 378
379 /* Setup new transfer. */ 379 /* Setup new transfer. */
380 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz, 380 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz,
381 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 381 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
382 usbd_transfer(xfer); 382 usbd_transfer(xfer);
383 return; 383 return;
384 384
385out: 385out:
386 mutex_exit(&unp->unp_rxlock); 386 mutex_exit(&unp->unp_rxlock);
387} 387}
388 388
389static void 389static void
390usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 390usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
391{ 391{
392 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 392 USBNETHIST_FUNC(); USBNETHIST_CALLED();
393 struct usbnet_chain * const c = priv; 393 struct usbnet_chain * const c = priv;
394 struct usbnet * const un = c->unc_un; 394 struct usbnet * const un = c->unc_un;
395 struct usbnet_cdata * const cd = un_cdata(un); 395 struct usbnet_cdata * const cd = un_cdata(un);
396 struct usbnet_private * const unp = un->un_pri; 396 struct usbnet_private * const unp = un->un_pri;
397 struct ifnet * const ifp = usbnet_ifp(un); 397 struct ifnet * const ifp = usbnet_ifp(un);
398 398
399 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", 399 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx",
400 unp->unp_number, status, (uintptr_t)xfer, 0); 400 unp->unp_number, status, (uintptr_t)xfer, 0);
401 401
402 mutex_enter(&unp->unp_txlock); 402 mutex_enter(&unp->unp_txlock);
403 if (unp->unp_stopping || unp->unp_dying) { 403 if (unp->unp_stopping || unp->unp_dying) {
404 mutex_exit(&unp->unp_txlock); 404 mutex_exit(&unp->unp_txlock);
405 return; 405 return;
406 } 406 }
407 407
408 KASSERT(cd->uncd_tx_cnt > 0); 408 KASSERT(cd->uncd_tx_cnt > 0);
409 cd->uncd_tx_cnt--; 409 cd->uncd_tx_cnt--;
410 410
411 unp->unp_timer = 0; 411 unp->unp_timer = 0;
412 412
413 switch (status) { 413 switch (status) {
414 case USBD_NOT_STARTED: 414 case USBD_NOT_STARTED:
415 case USBD_CANCELLED: 415 case USBD_CANCELLED:
416 break; 416 break;
417 417
418 case USBD_NORMAL_COMPLETION: 418 case USBD_NORMAL_COMPLETION:
419 if_statinc(ifp, if_opackets); 419 if_statinc(ifp, if_opackets);
420 break; 420 break;
421 421
422 default: 422 default:
423 423
424 if_statinc(ifp, if_oerrors); 424 if_statinc(ifp, if_oerrors);
425 if (usbd_ratecheck(&unp->unp_tx_notice)) 425 if (usbd_ratecheck(&unp->unp_tx_notice))
426 device_printf(un->un_dev, "usb error on tx: %s\n", 426 device_printf(un->un_dev, "usb error on tx: %s\n",
427 usbd_errstr(status)); 427 usbd_errstr(status));
428 if (status == USBD_STALLED) 428 if (status == USBD_STALLED)
429 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]); 429 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]);
430 break; 430 break;
431 } 431 }
432 432
433 mutex_exit(&unp->unp_txlock); 433 mutex_exit(&unp->unp_txlock);
434 434
435 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd)) 435 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
436 (*ifp->if_start)(ifp); 436 (*ifp->if_start)(ifp);
437} 437}
438 438
439static void 439static void
440usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 440usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
441{ 441{
442 USBNETHIST_FUNC(); 442 USBNETHIST_FUNC();
443 struct usbnet * const un = priv; 443 struct usbnet * const un = priv;
444 struct usbnet_private * const unp = un->un_pri; 444 struct usbnet_private * const unp = un->un_pri;
445 struct usbnet_intr * const uni = un->un_intr; 445 struct usbnet_intr * const uni = un->un_intr;
446 446
447 if (uni == NULL || unp->unp_dying || unp->unp_stopping || 447 if (uni == NULL || unp->unp_dying || unp->unp_stopping ||
448 status == USBD_INVAL || status == USBD_NOT_STARTED || 448 status == USBD_INVAL || status == USBD_NOT_STARTED ||
449 status == USBD_CANCELLED) { 449 status == USBD_CANCELLED) {
450 USBNETHIST_CALLARGS("%jd: uni %#jx d/s %#jx status %#jx", 450 USBNETHIST_CALLARGS("%jd: uni %#jx d/s %#jx status %#jx",
451 unp->unp_number, (uintptr_t)uni, 451 unp->unp_number, (uintptr_t)uni,
452 (unp->unp_dying << 8) | unp->unp_stopping, status); 452 (unp->unp_dying << 8) | unp->unp_stopping, status);
453 return; 453 return;
454 } 454 }
455 455
456 if (status != USBD_NORMAL_COMPLETION) { 456 if (status != USBD_NORMAL_COMPLETION) {
457 if (usbd_ratecheck(&unp->unp_intr_notice)) { 457 if (usbd_ratecheck(&unp->unp_intr_notice)) {
458 aprint_error_dev(un->un_dev, "usb error on intr: %s\n", 458 aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
459 usbd_errstr(status)); 459 usbd_errstr(status));
460 } 460 }
461 if (status == USBD_STALLED) 461 if (status == USBD_STALLED)
462 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]); 462 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]);
463 USBNETHIST_CALLARGS("%jd: not normal status %#jx", 463 USBNETHIST_CALLARGS("%jd: not normal status %#jx",
464 unp->unp_number, status, 0, 0); 464 unp->unp_number, status, 0, 0);
465 return; 465 return;
466 } 466 }
467 467
468 uno_intr(un, status); 468 uno_intr(un, status);
469} 469}
470 470
471static void 471static void
472usbnet_start_locked(struct ifnet *ifp) 472usbnet_start_locked(struct ifnet *ifp)
473{ 473{
474 USBNETHIST_FUNC(); 474 USBNETHIST_FUNC();
475 struct usbnet * const un = ifp->if_softc; 475 struct usbnet * const un = ifp->if_softc;
476 struct usbnet_cdata * const cd = un_cdata(un); 476 struct usbnet_cdata * const cd = un_cdata(un);
477 struct usbnet_private * const unp = un->un_pri; 477 struct usbnet_private * const unp = un->un_pri;
478 struct mbuf *m; 478 struct mbuf *m;
479 unsigned length; 479 unsigned length;
480 bool done_transmit = false; 480 bool done_transmit = false;
481 int idx, count; 481 int idx, count;
482 482
483 USBNETHIST_CALLARGS("%jd: tx_cnt %jd list_cnt %jd link %jd", 483 USBNETHIST_CALLARGS("%jd: tx_cnt %jd list_cnt %jd link %jd",
484 unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt, 484 unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt,
485 unp->unp_link); 485 unp->unp_link);
486 486
487 usbnet_isowned_tx(un); 487 usbnet_isowned_tx(un);
488 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt); 488 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt);
489 489
490 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) { 490 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) {
491 DPRINTF("start called no link (%jx) or running (flags %jx)", 491 DPRINTF("start called no link (%jx) or running (flags %jx)",
492 unp->unp_link, ifp->if_flags, 0, 0); 492 unp->unp_link, ifp->if_flags, 0, 0);
493 return; 493 return;
494 } 494 }
495 495
496 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) { 496 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) {
497 DPRINTF("start called, tx busy (%#jx == %#jx)", 497 DPRINTF("start called, tx busy (%#jx == %#jx)",
498 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0); 498 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0);
499 return; 499 return;
500 } 500 }
501 501
502 idx = cd->uncd_tx_prod; 502 idx = cd->uncd_tx_prod;
503 count = 0; 503 count = 0;
504 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) { 504 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) {
505 IFQ_POLL(&ifp->if_snd, m); 505 IFQ_POLL(&ifp->if_snd, m);
506 if (m == NULL) { 506 if (m == NULL) {
507 DPRINTF("start called, queue empty", 0, 0, 0, 0); 507 DPRINTF("start called, queue empty", 0, 0, 0, 0);
508 break; 508 break;
509 } 509 }
510 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz); 510 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz);
511 511
512 struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; 512 struct usbnet_chain *c = &cd->uncd_tx_chain[idx];
513 513
514 length = uno_tx_prepare(un, m, c); 514 length = uno_tx_prepare(un, m, c);
515 if (length == 0) { 515 if (length == 0) {
516 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0); 516 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0);
517 if_statinc(ifp, if_oerrors); 517 if_statinc(ifp, if_oerrors);
518 break; 518 break;
519 } 519 }
520 520
521 if (__predict_false(c->unc_xfer == NULL)) { 521 if (__predict_false(c->unc_xfer == NULL)) {
522 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0); 522 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0);
523 if_statinc(ifp, if_oerrors); 523 if_statinc(ifp, if_oerrors);
524 break; 524 break;
525 } 525 }
526 526
527 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, 527 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
528 un->un_tx_xfer_flags, 10000, usbnet_txeof); 528 un->un_tx_xfer_flags, 10000, usbnet_txeof);
529 529
530 /* Transmit */ 530 /* Transmit */
531 usbd_status err = usbd_transfer(c->unc_xfer); 531 usbd_status err = usbd_transfer(c->unc_xfer);
532 if (err != USBD_IN_PROGRESS) { 532 if (err != USBD_IN_PROGRESS) {
533 DPRINTF("usbd_transfer on %#jx for %ju bytes: %jd", 533 DPRINTF("usbd_transfer on %#jx for %ju bytes: %jd",
534 (uintptr_t)c->unc_buf, length, err, 0); 534 (uintptr_t)c->unc_buf, length, err, 0);
535 if_statinc(ifp, if_oerrors); 535 if_statinc(ifp, if_oerrors);
536 break; 536 break;
537 } 537 }
538 done_transmit = true; 538 done_transmit = true;
539 539
540 IFQ_DEQUEUE(&ifp->if_snd, m); 540 IFQ_DEQUEUE(&ifp->if_snd, m);
541 541
542 /* 542 /*
543 * If there's a BPF listener, bounce a copy of this frame 543 * If there's a BPF listener, bounce a copy of this frame
544 * to him. 544 * to him.
545 */ 545 */
546 bpf_mtap(ifp, m, BPF_D_OUT); 546 bpf_mtap(ifp, m, BPF_D_OUT);
547 m_freem(m); 547 m_freem(m);
548 548
549 idx = (idx + 1) % un->un_tx_list_cnt; 549 idx = (idx + 1) % un->un_tx_list_cnt;
550 cd->uncd_tx_cnt++; 550 cd->uncd_tx_cnt++;
551 count++; 551 count++;
552 } 552 }
553 cd->uncd_tx_prod = idx; 553 cd->uncd_tx_prod = idx;
554 554
555 DPRINTF("finished with start; tx_cnt %jd list_cnt %jd link %jd", 555 DPRINTF("finished with start; tx_cnt %jd list_cnt %jd link %jd",
556 cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0); 556 cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0);
557 557
558 /* 558 /*
559 * Set a timeout in case the chip goes out to lunch. 559 * Set a timeout in case the chip goes out to lunch.
560 */ 560 */
561 if (done_transmit) 561 if (done_transmit)
562 unp->unp_timer = 5; 562 unp->unp_timer = 5;
563 563
564 if (count != 0) 564 if (count != 0)
565 rnd_add_uint32(&unp->unp_rndsrc, count); 565 rnd_add_uint32(&unp->unp_rndsrc, count);
566} 566}
567 567
568static void 568static void
569usbnet_if_start(struct ifnet *ifp) 569usbnet_if_start(struct ifnet *ifp)
570{ 570{
571 struct usbnet * const un = ifp->if_softc; 571 struct usbnet * const un = ifp->if_softc;
572 struct usbnet_private * const unp = un->un_pri; 572 struct usbnet_private * const unp = un->un_pri;
573 573
574 USBNETHIST_FUNC(); 574 USBNETHIST_FUNC();
575 USBNETHIST_CALLARGS("%jd: stopping %jd", 575 USBNETHIST_CALLARGS("%jd: stopping %jd",
576 unp->unp_number, unp->unp_stopping, 0, 0); 576 unp->unp_number, unp->unp_stopping, 0, 0);
577 577
578 mutex_enter(&unp->unp_txlock); 578 mutex_enter(&unp->unp_txlock);
579 if (!unp->unp_stopping) 579 if (!unp->unp_stopping)
580 usbnet_start_locked(ifp); 580 usbnet_start_locked(ifp);
581 mutex_exit(&unp->unp_txlock); 581 mutex_exit(&unp->unp_txlock);
582} 582}
583 583
584/* 584/*
585 * Chain management. 585 * Chain management.
586 * 586 *
587 * RX and TX are identical. Keep them that way. 587 * RX and TX are identical. Keep them that way.
588 */ 588 */
589 589
590/* Start of common RX functions */ 590/* Start of common RX functions */
591 591
592static size_t 592static size_t
593usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 593usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
594{ 594{
595 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt; 595 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt;
596} 596}
597 597
598static void 598static void
599usbnet_rx_list_alloc(struct usbnet * const un) 599usbnet_rx_list_alloc(struct usbnet * const un)
600{ 600{
601 struct usbnet_cdata * const cd = un_cdata(un); 601 struct usbnet_cdata * const cd = un_cdata(un);
602 602
603 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP); 603 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP);
604} 604}
605 605
606static void 606static void
607usbnet_rx_list_free(struct usbnet * const un) 607usbnet_rx_list_free(struct usbnet * const un)
608{ 608{
609 struct usbnet_cdata * const cd = un_cdata(un); 609 struct usbnet_cdata * const cd = un_cdata(un);
610 610
611 if (cd->uncd_rx_chain) { 611 if (cd->uncd_rx_chain) {
612 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un)); 612 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un));
613 cd->uncd_rx_chain = NULL; 613 cd->uncd_rx_chain = NULL;
614 } 614 }
615} 615}
616 616
617static int 617static int
618usbnet_rx_list_init(struct usbnet * const un) 618usbnet_rx_list_init(struct usbnet * const un)
619{ 619{
620 struct usbnet_cdata * const cd = un_cdata(un); 620 struct usbnet_cdata * const cd = un_cdata(un);
621 struct usbnet_private * const unp = un->un_pri; 621 struct usbnet_private * const unp = un->un_pri;
622 622
623 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 623 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
624 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 624 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
625 625
626 c->unc_un = un; 626 c->unc_un = un;
627 if (c->unc_xfer == NULL) { 627 if (c->unc_xfer == NULL) {
628 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX], 628 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX],
629 un->un_rx_bufsz, un->un_rx_xfer_flags, 0, 629 un->un_rx_bufsz, un->un_rx_xfer_flags, 0,
630 &c->unc_xfer); 630 &c->unc_xfer);
631 if (err) 631 if (err)
632 return err; 632 return err;
633 c->unc_buf = usbd_get_buffer(c->unc_xfer); 633 c->unc_buf = usbd_get_buffer(c->unc_xfer);
634 } 634 }
635 } 635 }
636 636
637 return 0; 637 return 0;
638} 638}
639 639
640static void 640static void
641usbnet_rx_list_fini(struct usbnet * const un) 641usbnet_rx_list_fini(struct usbnet * const un)
642{ 642{
643 struct usbnet_cdata * const cd = un_cdata(un); 643 struct usbnet_cdata * const cd = un_cdata(un);
644 644
645 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 645 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
646 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 646 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
647 647
648 if (c->unc_xfer != NULL) { 648 if (c->unc_xfer != NULL) {
649 usbd_destroy_xfer(c->unc_xfer); 649 usbd_destroy_xfer(c->unc_xfer);
650 c->unc_xfer = NULL; 650 c->unc_xfer = NULL;
651 c->unc_buf = NULL; 651 c->unc_buf = NULL;
652 } 652 }
653 } 653 }
654} 654}
655 655
656/* End of common RX functions */ 656/* End of common RX functions */
657 657
658static void 658static void
659usbnet_rx_start_pipes(struct usbnet * const un) 659usbnet_rx_start_pipes(struct usbnet * const un)
660{ 660{
661 struct usbnet_cdata * const cd = un_cdata(un); 661 struct usbnet_cdata * const cd = un_cdata(un);
662 struct usbnet_private * const unp = un->un_pri; 662 struct usbnet_private * const unp = un->un_pri;
663 663
664 mutex_enter(&unp->unp_rxlock); 664 mutex_enter(&unp->unp_rxlock);
665 mutex_enter(&unp->unp_txlock); 665 mutex_enter(&unp->unp_txlock);
666 unp->unp_stopping = false; 666 unp->unp_stopping = false;
667 667
668 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 668 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
669 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 669 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
670 670
671 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz, 671 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz,
672 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 672 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
673 usbd_transfer(c->unc_xfer); 673 usbd_transfer(c->unc_xfer);
674 } 674 }
675 675
676 mutex_exit(&unp->unp_txlock); 676 mutex_exit(&unp->unp_txlock);
677 mutex_exit(&unp->unp_rxlock); 677 mutex_exit(&unp->unp_rxlock);
678} 678}
679 679
680/* Start of common TX functions */ 680/* Start of common TX functions */
681 681
682static size_t 682static size_t
683usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 683usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
684{ 684{
685 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt; 685 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt;
686} 686}
687 687
688static void 688static void
689usbnet_tx_list_alloc(struct usbnet * const un) 689usbnet_tx_list_alloc(struct usbnet * const un)
690{ 690{
691 struct usbnet_cdata * const cd = un_cdata(un); 691 struct usbnet_cdata * const cd = un_cdata(un);
692 692
693 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP); 693 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP);
694} 694}
695 695
696static void 696static void
697usbnet_tx_list_free(struct usbnet * const un) 697usbnet_tx_list_free(struct usbnet * const un)
698{ 698{
699 struct usbnet_cdata * const cd = un_cdata(un); 699 struct usbnet_cdata * const cd = un_cdata(un);
700 700
701 if (cd->uncd_tx_chain) { 701 if (cd->uncd_tx_chain) {
702 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un)); 702 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un));
703 cd->uncd_tx_chain = NULL; 703 cd->uncd_tx_chain = NULL;
704 } 704 }
705} 705}
706 706
707static int 707static int
708usbnet_tx_list_init(struct usbnet * const un) 708usbnet_tx_list_init(struct usbnet * const un)
709{ 709{
710 struct usbnet_cdata * const cd = un_cdata(un); 710 struct usbnet_cdata * const cd = un_cdata(un);
711 struct usbnet_private * const unp = un->un_pri; 711 struct usbnet_private * const unp = un->un_pri;
712 712
713 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 713 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
714 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 714 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
715 715
716 c->unc_un = un; 716 c->unc_un = un;
717 if (c->unc_xfer == NULL) { 717 if (c->unc_xfer == NULL) {
718 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX], 718 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX],
719 un->un_tx_bufsz, un->un_tx_xfer_flags, 0, 719 un->un_tx_bufsz, un->un_tx_xfer_flags, 0,
720 &c->unc_xfer); 720 &c->unc_xfer);
721 if (err) 721 if (err)
722 return err; 722 return err;
723 c->unc_buf = usbd_get_buffer(c->unc_xfer); 723 c->unc_buf = usbd_get_buffer(c->unc_xfer);
724 } 724 }
725 } 725 }
726 726
727 return 0; 727 return 0;
728} 728}
729 729
730static void 730static void
731usbnet_tx_list_fini(struct usbnet * const un) 731usbnet_tx_list_fini(struct usbnet * const un)
732{ 732{
733 struct usbnet_cdata * const cd = un_cdata(un); 733 struct usbnet_cdata * const cd = un_cdata(un);
734 734
735 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 735 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
736 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 736 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
737 737
738 if (c->unc_xfer != NULL) { 738 if (c->unc_xfer != NULL) {
739 usbd_destroy_xfer(c->unc_xfer); 739 usbd_destroy_xfer(c->unc_xfer);
740 c->unc_xfer = NULL; 740 c->unc_xfer = NULL;
741 c->unc_buf = NULL; 741 c->unc_buf = NULL;
742 } 742 }
743 } 743 }
744 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0; 744 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0;
745} 745}
746 746
747/* End of common TX functions */ 747/* End of common TX functions */
748 748
749/* Endpoint pipe management. */ 749/* Endpoint pipe management. */
750 750
751static void 751static void
752usbnet_ep_close_pipes(struct usbnet * const un) 752usbnet_ep_close_pipes(struct usbnet * const un)
753{ 753{
754 struct usbnet_private * const unp = un->un_pri; 754 struct usbnet_private * const unp = un->un_pri;
755 755
756 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 756 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
757 if (unp->unp_ep[i] == NULL) 757 if (unp->unp_ep[i] == NULL)
758 continue; 758 continue;
759 usbd_status err = usbd_close_pipe(unp->unp_ep[i]); 759 usbd_status err = usbd_close_pipe(unp->unp_ep[i]);
760 if (err) 760 if (err)
761 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i, 761 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
762 usbd_errstr(err)); 762 usbd_errstr(err));
763 unp->unp_ep[i] = NULL; 763 unp->unp_ep[i] = NULL;
764 } 764 }
765} 765}
766 766
767static usbd_status 767static usbd_status
768usbnet_ep_open_pipes(struct usbnet * const un) 768usbnet_ep_open_pipes(struct usbnet * const un)
769{ 769{
770 struct usbnet_intr * const uni = un->un_intr; 770 struct usbnet_intr * const uni = un->un_intr;
771 struct usbnet_private * const unp = un->un_pri; 771 struct usbnet_private * const unp = un->un_pri;
772 772
773 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 773 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
774 usbd_status err; 774 usbd_status err;
775 775
776 if (un->un_ed[i] == 0) 776 if (un->un_ed[i] == 0)
777 continue; 777 continue;
778 778
779 if (i == USBNET_ENDPT_INTR && uni) { 779 if (i == USBNET_ENDPT_INTR && uni) {
780 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i], 780 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
781 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un, 781 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un,
782 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr, 782 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr,
783 uni->uni_interval); 783 uni->uni_interval);
784 } else { 784 } else {
785 err = usbd_open_pipe(un->un_iface, un->un_ed[i], 785 err = usbd_open_pipe(un->un_iface, un->un_ed[i],
786 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]); 786 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]);
787 } 787 }
788 if (err) { 788 if (err) {
789 usbnet_ep_close_pipes(un); 789 usbnet_ep_close_pipes(un);
790 return err; 790 return err;
791 } 791 }
792 } 792 }
793 793
794 return USBD_NORMAL_COMPLETION; 794 return USBD_NORMAL_COMPLETION;
795} 795}
796 796
797static usbd_status 797static usbd_status
798usbnet_ep_stop_pipes(struct usbnet * const un) 798usbnet_ep_stop_pipes(struct usbnet * const un)
799{ 799{
800 struct usbnet_private * const unp = un->un_pri; 800 struct usbnet_private * const unp = un->un_pri;
801 usbd_status err = USBD_NORMAL_COMPLETION; 801 usbd_status err = USBD_NORMAL_COMPLETION;
802 802
803 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 803 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
804 if (unp->unp_ep[i] == NULL) 804 if (unp->unp_ep[i] == NULL)
805 continue; 805 continue;
806 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]); 806 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]);
807 if (err == USBD_NORMAL_COMPLETION && err2) 807 if (err == USBD_NORMAL_COMPLETION && err2)
808 err = err2; 808 err = err2;
809 } 809 }
810 810
811 return err; 811 return err;
812} 812}
813 813
814int 814int
815usbnet_init_rx_tx(struct usbnet * const un) 815usbnet_init_rx_tx(struct usbnet * const un)
816{ 816{
817 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 817 USBNETHIST_FUNC(); USBNETHIST_CALLED();
818 struct usbnet_private * const unp = un->un_pri; 818 struct usbnet_private * const unp = un->un_pri;
819 struct ifnet * const ifp = usbnet_ifp(un); 819 struct ifnet * const ifp = usbnet_ifp(un);
820 usbd_status err; 820 usbd_status err;
821 int error = 0; 821 int error = 0;
822 822
823 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), 823 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp),
824 "%s", ifp->if_xname); 824 "%s", ifp->if_xname);
825 825
826 usbnet_isowned_core(un); 826 usbnet_isowned_core(un);
827 827
828 if (unp->unp_dying) { 828 if (unp->unp_dying) {
829 return EIO; 829 return EIO;
830 } 830 }
831 831
832 usbnet_busy(un); 832 usbnet_busy(un);
833 833
834 /* Open RX and TX pipes. */ 834 /* Open RX and TX pipes. */
835 err = usbnet_ep_open_pipes(un); 835 err = usbnet_ep_open_pipes(un);
836 if (err) { 836 if (err) {
837 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n", 837 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
838 usbd_errstr(err)); 838 usbd_errstr(err));
839 error = EIO; 839 error = EIO;
840 goto out; 840 goto out;
841 } 841 }
842 842
843 /* Init RX ring. */ 843 /* Init RX ring. */
844 if (usbnet_rx_list_init(un)) { 844 if (usbnet_rx_list_init(un)) {
845 aprint_error_dev(un->un_dev, "rx list init failed\n"); 845 aprint_error_dev(un->un_dev, "rx list init failed\n");
846 error = ENOBUFS; 846 error = ENOBUFS;
847 goto out; 847 goto out;
848 } 848 }
849 849
850 /* Init TX ring. */ 850 /* Init TX ring. */
851 if (usbnet_tx_list_init(un)) { 851 if (usbnet_tx_list_init(un)) {
852 aprint_error_dev(un->un_dev, "tx list init failed\n"); 852 aprint_error_dev(un->un_dev, "tx list init failed\n");
853 error = ENOBUFS; 853 error = ENOBUFS;
854 goto out; 854 goto out;
855 } 855 }
856 856
857 /* Indicate we are up and running. */ 857 /* Indicate we are up and running. */
858#if 0 858#if 0
859 /* XXX if_mcast_op() can call this without ifnet locked */ 859 /* XXX if_mcast_op() can call this without ifnet locked */
860 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp)); 860 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
861#endif 861#endif
862 ifp->if_flags |= IFF_RUNNING; 862 ifp->if_flags |= IFF_RUNNING;
863 863
864 /* Start up the receive pipe(s). */ 864 /* Start up the receive pipe(s). */
865 usbnet_rx_start_pipes(un); 865 usbnet_rx_start_pipes(un);
866 866
867 callout_schedule(&unp->unp_stat_ch, hz); 867 callout_schedule(&unp->unp_stat_ch, hz);
868 868
869out: 869out:
870 if (error) { 870 if (error) {
871 usbnet_rx_list_fini(un); 871 usbnet_rx_list_fini(un);
872 usbnet_tx_list_fini(un); 872 usbnet_tx_list_fini(un);
873 usbnet_ep_close_pipes(un); 873 usbnet_ep_close_pipes(un);
874 } 874 }
875 usbnet_unbusy(un); 875 usbnet_unbusy(un);
876 876
877 usbnet_isowned_core(un); 877 usbnet_isowned_core(un);
878 878
879 return error; 879 return error;
880} 880}
881 881
882void 882void
883usbnet_busy(struct usbnet *un) 883usbnet_busy(struct usbnet *un)
884{ 884{
885 struct usbnet_private * const unp = un->un_pri; 885 struct usbnet_private * const unp = un->un_pri;
886 886
887 usbnet_isowned_core(un); 887 usbnet_isowned_core(un);
888 888
889 unp->unp_refcnt++; 889 unp->unp_refcnt++;
890} 890}
891 891
892void 892void
893usbnet_unbusy(struct usbnet *un) 893usbnet_unbusy(struct usbnet *un)
894{ 894{
895 struct usbnet_private * const unp = un->un_pri; 895 struct usbnet_private * const unp = un->un_pri;
896 896
897 usbnet_isowned_core(un); 897 usbnet_isowned_core(un);
898 898
899 if (--unp->unp_refcnt < 0) 899 if (--unp->unp_refcnt < 0)
900 cv_broadcast(&unp->unp_detachcv); 900 cv_broadcast(&unp->unp_detachcv);
901} 901}
902 902
903/* MII management. */ 903/* MII management. */
904 904
905int 905int
906usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) 906usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
907{ 907{
908 USBNETHIST_FUNC(); 908 USBNETHIST_FUNC();
909 struct usbnet * const un = device_private(dev); 909 struct usbnet * const un = device_private(dev);
910 struct usbnet_private * const unp = un->un_pri; 910 struct usbnet_private * const unp = un->un_pri;
911 int err; 911 int err;
912 912
913 /* MII layer ensures core_lock is held. */ 913 /* MII layer ensures core_lock is held. */
914 usbnet_isowned_core(un); 914 usbnet_isowned_core(un);
915 915
916 if (unp->unp_dying) { 916 if (unp->unp_dying) {
917 return EIO; 917 return EIO;
918 } 918 }
919 919
920 usbnet_busy(un); 920 usbnet_busy(un);
921 err = uno_read_reg(un, phy, reg, val); 921 err = uno_read_reg(un, phy, reg, val);
922 usbnet_unbusy(un); 922 usbnet_unbusy(un);
923 923
924 if (err) { 924 if (err) {
925 USBNETHIST_CALLARGS("%jd: read PHY failed: %jd", 925 USBNETHIST_CALLARGS("%jd: read PHY failed: %jd",
926 unp->unp_number, err, 0, 0); 926 unp->unp_number, err, 0, 0);
927 return err; 927 return err;
928 } 928 }
929 929
930 return 0; 930 return 0;
931} 931}
932 932
933int 933int
934usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) 934usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
935{ 935{
936 USBNETHIST_FUNC(); 936 USBNETHIST_FUNC();
937 struct usbnet * const un = device_private(dev); 937 struct usbnet * const un = device_private(dev);
938 struct usbnet_private * const unp = un->un_pri; 938 struct usbnet_private * const unp = un->un_pri;
939 int err; 939 int err;
940 940
941 /* MII layer ensures core_lock is held. */ 941 /* MII layer ensures core_lock is held. */
942 usbnet_isowned_core(un); 942 usbnet_isowned_core(un);
943 943
944 if (unp->unp_dying) { 944 if (unp->unp_dying) {
945 return EIO; 945 return EIO;
946 } 946 }
947 947
948 usbnet_busy(un); 948 usbnet_busy(un);
949 err = uno_write_reg(un, phy, reg, val); 949 err = uno_write_reg(un, phy, reg, val);
950 usbnet_unbusy(un); 950 usbnet_unbusy(un);
951 951
952 if (err) { 952 if (err) {
953 USBNETHIST_CALLARGS("%jd: write PHY failed: %jd", 953 USBNETHIST_CALLARGS("%jd: write PHY failed: %jd",
954 unp->unp_number, err, 0, 0); 954 unp->unp_number, err, 0, 0);
955 return err; 955 return err;
956 } 956 }
957 957
958 return 0; 958 return 0;
959} 959}
960 960
961void 961void
962usbnet_mii_statchg(struct ifnet *ifp) 962usbnet_mii_statchg(struct ifnet *ifp)
963{ 963{
964 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 964 USBNETHIST_FUNC(); USBNETHIST_CALLED();
965 struct usbnet * const un = ifp->if_softc; 965 struct usbnet * const un = ifp->if_softc;
966 966
967 /* MII layer ensures core_lock is held. */ 967 /* MII layer ensures core_lock is held. */
968 usbnet_isowned_core(un); 968 usbnet_isowned_core(un);
969 969
970 usbnet_busy(un); 970 usbnet_busy(un);
971 uno_mii_statchg(un, ifp); 971 uno_mii_statchg(un, ifp);
972 usbnet_unbusy(un); 972 usbnet_unbusy(un);
973} 973}
974 974
975static int 975static int
976usbnet_media_upd(struct ifnet *ifp) 976usbnet_media_upd(struct ifnet *ifp)
977{ 977{
978 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 978 USBNETHIST_FUNC(); USBNETHIST_CALLED();
979 struct usbnet * const un = ifp->if_softc; 979 struct usbnet * const un = ifp->if_softc;
980 struct usbnet_private * const unp = un->un_pri; 980 struct usbnet_private * const unp = un->un_pri;
981 struct mii_data * const mii = usbnet_mii(un); 981 struct mii_data * const mii = usbnet_mii(un);
982 982
983 /* ifmedia layer ensures core_lock is held. */ 983 /* ifmedia layer ensures core_lock is held. */
984 usbnet_isowned_core(un); 984 usbnet_isowned_core(un);
985 985
986 /* ifmedia changes only with IFNET_LOCK held. */ 986 /* ifmedia changes only with IFNET_LOCK held. */
987 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 987 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
988 988
989 if (unp->unp_dying) 989 if (unp->unp_dying)
990 return EIO; 990 return EIO;
991 991
992 unp->unp_link = false; 992 unp->unp_link = false;
993 993
994 if (mii->mii_instance) { 994 if (mii->mii_instance) {
995 struct mii_softc *miisc; 995 struct mii_softc *miisc;
996 996
997 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 997 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
998 mii_phy_reset(miisc); 998 mii_phy_reset(miisc);
999 } 999 }
1000 1000
1001 return ether_mediachange(ifp); 1001 return ether_mediachange(ifp);
1002} 1002}
1003 1003
1004/* ioctl */ 1004/* ioctl */
1005 1005
1006static int 1006static int
1007usbnet_ifflags_cb(struct ethercom *ec) 1007usbnet_ifflags_cb(struct ethercom *ec)
1008{ 1008{
1009 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1009 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1010 struct ifnet *ifp = &ec->ec_if; 1010 struct ifnet *ifp = &ec->ec_if;
1011 struct usbnet *un = ifp->if_softc; 1011 struct usbnet *un = ifp->if_softc;
1012 struct usbnet_private * const unp = un->un_pri; 1012 struct usbnet_private * const unp = un->un_pri;
1013 int rv = 0; 1013 int rv = 0;
1014 1014
1015 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1015 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1016 1016
1017 mutex_enter(&unp->unp_core_lock); 1017 mutex_enter(&unp->unp_core_lock);
1018 1018
1019 const u_short changed = ifp->if_flags ^ unp->unp_if_flags; 1019 const u_short changed = ifp->if_flags ^ unp->unp_if_flags;
1020 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) { 1020 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
1021 unp->unp_if_flags = ifp->if_flags; 1021 unp->unp_if_flags = ifp->if_flags;
1022 if ((changed & IFF_PROMISC) != 0) 1022 if ((changed & IFF_PROMISC) != 0)
1023 rv = ENETRESET; 1023 rv = ENETRESET;
1024 } else { 1024 } else {
1025 rv = ENETRESET; 1025 rv = ENETRESET;
1026 } 1026 }
1027 1027
1028 mutex_exit(&unp->unp_core_lock); 1028 mutex_exit(&unp->unp_core_lock);
1029 1029
1030 return rv; 1030 return rv;
1031} 1031}
1032 1032
1033static int 1033static int
1034usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1034usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1035{ 1035{
1036 USBNETHIST_FUNC(); 1036 USBNETHIST_FUNC();
1037 struct usbnet * const un = ifp->if_softc; 1037 struct usbnet * const un = ifp->if_softc;
1038 struct usbnet_private * const unp __unused = un->un_pri; 1038 struct usbnet_private * const unp __unused = un->un_pri;
1039 int error; 1039 int error;
1040 1040
1041 USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx", 1041 USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx",
1042 unp->unp_number, cmd, (uintptr_t)data, 0); 1042 unp->unp_number, cmd, (uintptr_t)data, 0);
1043 1043
1044 if (un->un_ops->uno_override_ioctl) 1044 if (un->un_ops->uno_override_ioctl)
1045 return uno_override_ioctl(un, ifp, cmd, data); 1045 return uno_override_ioctl(un, ifp, cmd, data);
1046 1046
1047 error = ether_ioctl(ifp, cmd, data); 1047 error = ether_ioctl(ifp, cmd, data);
1048 if (error == ENETRESET) { 1048 if (error == ENETRESET) {
1049 switch (cmd) { 1049 switch (cmd) {
1050 case SIOCADDMULTI: 1050 case SIOCADDMULTI:
1051 case SIOCDELMULTI: 1051 case SIOCDELMULTI:
1052 usb_add_task(un->un_udev, &unp->unp_mcasttask, 1052 usb_add_task(un->un_udev, &unp->unp_mcasttask,
1053 USB_TASKQ_DRIVER); 1053 USB_TASKQ_DRIVER);
1054 error = 0; 1054 error = 0;
1055 break; 1055 break;
1056 default: 1056 default:
1057 error = uno_ioctl(un, ifp, cmd, data); 1057 error = uno_ioctl(un, ifp, cmd, data);
1058 } 1058 }
1059 } 1059 }
1060 1060
1061 return error; 1061 return error;
1062} 1062}
1063 1063
1064static void 1064static void
1065usbnet_mcast_task(void *arg) 1065usbnet_mcast_task(void *arg)
1066{ 1066{
1067 USBNETHIST_FUNC(); 1067 USBNETHIST_FUNC();
1068 struct usbnet * const un = arg; 1068 struct usbnet * const un = arg;
1069 struct usbnet_private * const unp = un->un_pri; 1069 struct usbnet_private * const unp = un->un_pri;
1070 struct ifnet * const ifp = usbnet_ifp(un); 1070 struct ifnet * const ifp = usbnet_ifp(un);
1071 bool dying; 1071 bool dying;
1072 struct ifreq ifr; 1072 struct ifreq ifr;
1073 1073
1074 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); 1074 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0);
1075 1075
1076 /* 1076 /*
1077 * If we're detaching, we must check unp_dying _before_ 1077 * If we're detaching, we must check unp_dying _before_
1078 * touching IFNET_LOCK -- the ifnet may have been detached by 1078 * touching IFNET_LOCK -- the ifnet may have been detached by
1079 * the time this task runs. This is racy -- unp_dying may be 1079 * the time this task runs. This is racy -- unp_dying may be
1080 * set immediately after we test it -- but nevertheless safe, 1080 * set immediately after we test it -- but nevertheless safe,
1081 * because usbnet_detach waits for the task to complete before 1081 * because usbnet_detach waits for the task to complete before
1082 * issuing if_detach, and necessary, so that we don't touch 1082 * issuing if_detach, and necessary, so that we don't touch
1083 * IFNET_LOCK after if_detach. See usbnet_detach for details. 1083 * IFNET_LOCK after if_detach. See usbnet_detach for details.
1084 */ 1084 */
1085 mutex_enter(&unp->unp_core_lock); 1085 mutex_enter(&unp->unp_core_lock);
1086 dying = unp->unp_dying; 1086 dying = unp->unp_dying;
1087 mutex_exit(&unp->unp_core_lock); 1087 mutex_exit(&unp->unp_core_lock);
1088 if (dying) 1088 if (dying)
1089 return; 1089 return;
1090 1090
1091 /* 1091 /*
1092 * Pass a bogus ifr with SIOCDELMULTI -- the goal is to just 1092 * Pass a bogus ifr with SIOCDELMULTI -- the goal is to just
1093 * notify the driver to reprogram any hardware multicast 1093 * notify the driver to reprogram any hardware multicast
1094 * filter, according to what's already stored in the ethercom. 1094 * filter, according to what's already stored in the ethercom.
1095 * None of the drivers actually examine this argument, so it 1095 * None of the drivers actually examine this argument, so it
1096 * doesn't change the ABI as far as they can tell. 1096 * doesn't change the ABI as far as they can tell.
1097 */ 1097 */
1098 IFNET_LOCK(ifp); 1098 IFNET_LOCK(ifp);
1099 if (ifp->if_flags & IFF_RUNNING) { 1099 if (ifp->if_flags & IFF_RUNNING) {
1100 memset(&ifr, 0, sizeof(ifr)); 1100 memset(&ifr, 0, sizeof(ifr));
1101 (void)uno_ioctl(un, ifp, SIOCDELMULTI, &ifr); 1101 (void)uno_ioctl(un, ifp, SIOCDELMULTI, &ifr);
1102 } 1102 }
1103 IFNET_UNLOCK(ifp); 1103 IFNET_UNLOCK(ifp);
1104} 1104}
1105 1105
1106/* 1106/*
1107 * Generic stop network function: 1107 * Generic stop network function:
1108 * - mark as stopping 1108 * - mark as stopping
1109 * - call DD routine to stop the device 1109 * - call DD routine to stop the device
1110 * - turn off running, timer, statchg callout, link 1110 * - turn off running, timer, statchg callout, link
1111 * - stop transfers 1111 * - stop transfers
1112 * - free RX and TX resources 1112 * - free RX and TX resources
1113 * - close pipes 1113 * - close pipes
1114 * 1114 *
1115 * usbnet_stop() is exported for drivers to use, expects lock held. 1115 * usbnet_stop() is exported for drivers to use, expects lock held.
1116 * 1116 *
1117 * usbnet_if_stop() is for the if_stop handler. 1117 * usbnet_if_stop() is for the if_stop handler.
1118 */ 1118 */
1119void 1119void
1120usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) 1120usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
1121{ 1121{
1122 struct usbnet_private * const unp = un->un_pri; 1122 struct usbnet_private * const unp = un->un_pri;
1123 1123
1124 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1124 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1125 1125
1126 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), 1126 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp),
1127 "%s", ifp->if_xname); 1127 "%s", ifp->if_xname);
1128 usbnet_isowned_core(un); 1128 usbnet_isowned_core(un);
1129 1129
1130 usbnet_busy(un); 1130 usbnet_busy(un);
1131 1131
1132 mutex_enter(&unp->unp_rxlock); 1132 mutex_enter(&unp->unp_rxlock);
1133 mutex_enter(&unp->unp_txlock); 1133 mutex_enter(&unp->unp_txlock);
1134 unp->unp_stopping = true; 1134 unp->unp_stopping = true;
1135 mutex_exit(&unp->unp_txlock); 1135 mutex_exit(&unp->unp_txlock);
1136 mutex_exit(&unp->unp_rxlock); 1136 mutex_exit(&unp->unp_rxlock);
1137 1137
1138 /* 1138 /*
1139 * Stop the timer first, then the task -- if the timer was 1139 * Stop the timer first, then the task -- if the timer was
1140 * already firing, we stop the task or wait for it complete 1140 * already firing, we stop the task or wait for it complete
1141 * only after if last fired. Setting unp_stopping prevents the 1141 * only after if last fired. Setting unp_stopping prevents the
1142 * timer task from being scheduled again. 1142 * timer task from being scheduled again.
1143 */ 1143 */
1144 callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock); 1144 callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock);
1145 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1145 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
1146 &unp->unp_core_lock); 1146 &unp->unp_core_lock);
1147 1147
1148 /* 1148 /*
1149 * Now that the software is quiescent, ask the driver to stop 1149 * Now that the software is quiescent, ask the driver to stop
1150 * the hardware. The driver's uno_stop routine now has 1150 * the hardware. The driver's uno_stop routine now has
1151 * exclusive access to any registers that might previously have 1151 * exclusive access to any registers that might previously have
1152 * been used by to ifmedia, mii, or ioctl callbacks. 1152 * been used by to ifmedia, mii, or ioctl callbacks.
1153 */ 1153 */
1154 uno_stop(un, ifp, disable); 1154 uno_stop(un, ifp, disable);
1155 1155
1156 /* Clear the watchdog timer. */ 1156 /* Clear the watchdog timer. */
1157 mutex_enter(&unp->unp_txlock); 1157 mutex_enter(&unp->unp_txlock);
1158 unp->unp_timer = 0; 1158 unp->unp_timer = 0;
1159 mutex_exit(&unp->unp_txlock); 1159 mutex_exit(&unp->unp_txlock);
1160 1160
1161 /* Stop transfers. */ 1161 /* Stop transfers. */
1162 usbnet_ep_stop_pipes(un); 1162 usbnet_ep_stop_pipes(un);
1163 1163
1164 /* Free RX/TX resources. */ 1164 /* Free RX/TX resources. */
1165 usbnet_rx_list_fini(un); 1165 usbnet_rx_list_fini(un);
1166 usbnet_tx_list_fini(un); 1166 usbnet_tx_list_fini(un);
1167 1167
1168 /* Close pipes. */ 1168 /* Close pipes. */
1169 usbnet_ep_close_pipes(un); 1169 usbnet_ep_close_pipes(un);
1170 1170
1171 /* Everything is quesced now. */ 1171 /* Everything is quesced now. */
1172 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), 1172 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp),
1173 "%s", ifp->if_xname); 1173 "%s", ifp->if_xname);
1174 ifp->if_flags &= ~IFF_RUNNING; 1174 ifp->if_flags &= ~IFF_RUNNING;
1175 1175
1176 usbnet_unbusy(un); 1176 usbnet_unbusy(un);
1177} 1177}
1178 1178
1179static void 1179static void
1180usbnet_if_stop(struct ifnet *ifp, int disable) 1180usbnet_if_stop(struct ifnet *ifp, int disable)
1181{ 1181{
1182 struct usbnet * const un = ifp->if_softc; 1182 struct usbnet * const un = ifp->if_softc;
1183 struct usbnet_private * const unp = un->un_pri; 1183 struct usbnet_private * const unp = un->un_pri;
1184 1184
1185 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1185 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1186 1186
1187 mutex_enter(&unp->unp_core_lock); 1187 mutex_enter(&unp->unp_core_lock);
1188 usbnet_stop(un, ifp, disable); 1188 usbnet_stop(un, ifp, disable);
1189 mutex_exit(&unp->unp_core_lock); 1189 mutex_exit(&unp->unp_core_lock);
1190} 1190}
1191 1191
1192/* 1192/*
1193 * Generic tick task function. 1193 * Generic tick task function.
1194 * 1194 *
1195 * usbnet_tick() is triggered from a callout, and triggers a call to 1195 * usbnet_tick() is triggered from a callout, and triggers a call to
1196 * usbnet_tick_task() from the usb_task subsystem. 1196 * usbnet_tick_task() from the usb_task subsystem.
1197 */ 1197 */
1198static void 1198static void
1199usbnet_tick(void *arg) 1199usbnet_tick(void *arg)
1200{ 1200{
1201 USBNETHIST_FUNC(); 1201 USBNETHIST_FUNC();
1202 struct usbnet * const un = arg; 1202 struct usbnet * const un = arg;
1203 struct usbnet_private * const unp = un->un_pri; 1203 struct usbnet_private * const unp = un->un_pri;
1204 1204
1205 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); 1205 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0);
1206 1206
1207 if (unp != NULL && !unp->unp_stopping && !unp->unp_dying) { 1207 if (unp != NULL && !unp->unp_stopping && !unp->unp_dying) {
1208 /* Perform periodic stuff in process context */ 1208 /* Perform periodic stuff in process context */
1209 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); 1209 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER);
1210 } 1210 }
1211} 1211}
1212 1212
1213static void 1213static void
1214usbnet_watchdog(struct ifnet *ifp) 1214usbnet_watchdog(struct ifnet *ifp)
1215{ 1215{
1216 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1216 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1217 struct usbnet * const un = ifp->if_softc; 1217 struct usbnet * const un = ifp->if_softc;
1218 struct usbnet_private * const unp = un->un_pri; 1218 struct usbnet_private * const unp = un->un_pri;
1219 struct usbnet_cdata * const cd = un_cdata(un); 1219 struct usbnet_cdata * const cd = un_cdata(un);
1220 usbd_status err; 1220 usbd_status err;
1221 1221
1222 if_statinc(ifp, if_oerrors); 1222 if_statinc(ifp, if_oerrors);
1223 device_printf(un->un_dev, "watchdog timeout\n"); 1223 device_printf(un->un_dev, "watchdog timeout\n");
1224 1224
1225 if (cd->uncd_tx_cnt > 0) { 1225 if (cd->uncd_tx_cnt > 0) {
1226 DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0); 1226 DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0);
1227 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); 1227 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]);
1228 if (err) 1228 if (err)
1229 device_printf(un->un_dev, "pipe abort failed: %s\n", 1229 device_printf(un->un_dev, "pipe abort failed: %s\n",
1230 usbd_errstr(err)); 1230 usbd_errstr(err));
1231 if (cd->uncd_tx_cnt != 0) 1231 if (cd->uncd_tx_cnt != 0)
1232 DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0); 1232 DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0);
1233 } 1233 }
1234 1234
1235 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1235 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1236 (*ifp->if_start)(ifp); 1236 (*ifp->if_start)(ifp);
1237} 1237}
1238 1238
1239static void 1239static void
1240usbnet_tick_task(void *arg) 1240usbnet_tick_task(void *arg)
1241{ 1241{
1242 USBNETHIST_FUNC(); 1242 USBNETHIST_FUNC();
1243 struct usbnet * const un = arg; 1243 struct usbnet * const un = arg;
1244 struct usbnet_private * const unp = un->un_pri; 1244 struct usbnet_private * const unp = un->un_pri;
1245 1245
1246 USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0); 1246 USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0);
1247 1247
1248 mutex_enter(&unp->unp_core_lock); 1248 mutex_enter(&unp->unp_core_lock);
1249 if (unp->unp_stopping || unp->unp_dying) { 1249 if (unp->unp_stopping || unp->unp_dying) {
1250 mutex_exit(&unp->unp_core_lock); 1250 mutex_exit(&unp->unp_core_lock);
1251 return; 1251 return;
1252 } 1252 }
1253 1253
1254 struct ifnet * const ifp = usbnet_ifp(un); 1254 struct ifnet * const ifp = usbnet_ifp(un);
1255 struct mii_data * const mii = usbnet_mii(un); 1255 struct mii_data * const mii = usbnet_mii(un);
1256 1256
1257 KASSERT(ifp != NULL); /* embedded member */ 1257 KASSERT(ifp != NULL); /* embedded member */
1258 1258
1259 usbnet_busy(un); 1259 usbnet_busy(un);
1260 mutex_exit(&unp->unp_core_lock); 1260 mutex_exit(&unp->unp_core_lock);
1261 1261
1262 mutex_enter(&unp->unp_txlock); 1262 mutex_enter(&unp->unp_txlock);
1263 const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0; 1263 const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0;
1264 mutex_exit(&unp->unp_txlock); 1264 mutex_exit(&unp->unp_txlock);
1265 if (timeout) 1265 if (timeout)
1266 usbnet_watchdog(ifp); 1266 usbnet_watchdog(ifp);
1267 1267
1268 DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0); 1268 DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0);
1269 if (mii) { 1269 if (mii) {
1270 mutex_enter(&unp->unp_core_lock); 1270 mutex_enter(&unp->unp_core_lock);
1271 mii_tick(mii); 1271 mii_tick(mii);
1272 if (!unp->unp_link) 1272 if (!unp->unp_link)
1273 (*mii->mii_statchg)(ifp); 1273 (*mii->mii_statchg)(ifp);
1274 mutex_exit(&unp->unp_core_lock); 1274 mutex_exit(&unp->unp_core_lock);
1275 } 1275 }
1276 1276
1277 /* Call driver if requested. */ 1277 /* Call driver if requested. */
1278 uno_tick(un); 1278 uno_tick(un);
1279 1279
1280 mutex_enter(&unp->unp_core_lock); 1280 mutex_enter(&unp->unp_core_lock);
1281 usbnet_unbusy(un); 1281 usbnet_unbusy(un);
1282 if (!unp->unp_stopping && !unp->unp_dying) 1282 if (!unp->unp_stopping && !unp->unp_dying)
1283 callout_schedule(&unp->unp_stat_ch, hz); 1283 callout_schedule(&unp->unp_stat_ch, hz);
1284 mutex_exit(&unp->unp_core_lock); 1284 mutex_exit(&unp->unp_core_lock);
1285} 1285}
1286 1286
1287static int 1287static int
1288usbnet_if_init(struct ifnet *ifp) 1288usbnet_if_init(struct ifnet *ifp)
1289{ 1289{
1290 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1290 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1291 struct usbnet * const un = ifp->if_softc; 1291 struct usbnet * const un = ifp->if_softc;
1292 bool dying; 1292 bool dying;
1293 1293
1294 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1294 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1295 1295
1296 /* 1296 /*
1297 * Prevent anyone from bringing the interface back up once 1297 * Prevent anyone from bringing the interface back up once
1298 * we're detaching. 1298 * we're detaching.
1299 */ 1299 */
1300 mutex_enter(&un->un_pri->unp_core_lock); 1300 mutex_enter(&un->un_pri->unp_core_lock);
1301 dying = un->un_pri->unp_dying; 1301 dying = un->un_pri->unp_dying;
1302 mutex_exit(&un->un_pri->unp_core_lock); 1302 mutex_exit(&un->un_pri->unp_core_lock);
1303 if (dying) 1303 if (dying)
1304 return EIO; 1304 return EIO;
1305 1305
1306 return uno_init(un, ifp); 1306 return uno_init(un, ifp);
1307} 1307}
1308 1308
1309 1309
1310/* Various accessors. */ 1310/* Various accessors. */
1311 1311
1312void 1312void
1313usbnet_set_link(struct usbnet *un, bool link) 1313usbnet_set_link(struct usbnet *un, bool link)
1314{ 1314{
1315 un->un_pri->unp_link = link; 1315 un->un_pri->unp_link = link;
1316} 1316}
1317 1317
1318struct ifnet * 1318struct ifnet *
1319usbnet_ifp(struct usbnet *un) 1319usbnet_ifp(struct usbnet *un)
1320{ 1320{
1321 return &un->un_pri->unp_ec.ec_if; 1321 return &un->un_pri->unp_ec.ec_if;
1322} 1322}
1323 1323
1324struct ethercom * 1324struct ethercom *
1325usbnet_ec(struct usbnet *un) 1325usbnet_ec(struct usbnet *un)
1326{ 1326{
1327 return &un->un_pri->unp_ec; 1327 return &un->un_pri->unp_ec;
1328} 1328}
1329 1329
1330struct mii_data * 1330struct mii_data *
1331usbnet_mii(struct usbnet *un) 1331usbnet_mii(struct usbnet *un)
1332{ 1332{
1333 return un->un_pri->unp_ec.ec_mii; 1333 return un->un_pri->unp_ec.ec_mii;
1334} 1334}
1335 1335
1336krndsource_t * 1336krndsource_t *
1337usbnet_rndsrc(struct usbnet *un) 1337usbnet_rndsrc(struct usbnet *un)
1338{ 1338{
1339 return &un->un_pri->unp_rndsrc; 1339 return &un->un_pri->unp_rndsrc;
1340} 1340}
1341 1341
1342void * 1342void *
1343usbnet_softc(struct usbnet *un) 1343usbnet_softc(struct usbnet *un)
1344{ 1344{
1345 return un->un_sc; 1345 return un->un_sc;
1346} 1346}
1347 1347
1348bool 1348bool
1349usbnet_havelink(struct usbnet *un) 1349usbnet_havelink(struct usbnet *un)
1350{ 1350{
1351 return un->un_pri->unp_link; 1351 return un->un_pri->unp_link;
1352} 1352}
1353 1353
1354bool 1354bool
1355usbnet_isdying(struct usbnet *un) 1355usbnet_isdying(struct usbnet *un)
1356{ 1356{
1357 return un->un_pri->unp_dying; 1357 return un->un_pri->unp_dying;
1358} 1358}
1359 1359
1360 1360
1361/* Locking. */ 1361/* Locking. */
1362 1362
1363void 1363void
1364usbnet_lock_core(struct usbnet *un) 1364usbnet_lock_core(struct usbnet *un)
1365{ 1365{
1366 mutex_enter(&un->un_pri->unp_core_lock); 1366 mutex_enter(&un->un_pri->unp_core_lock);
1367} 1367}
1368 1368
1369void 1369void
1370usbnet_unlock_core(struct usbnet *un) 1370usbnet_unlock_core(struct usbnet *un)
1371{ 1371{
1372 mutex_exit(&un->un_pri->unp_core_lock); 1372 mutex_exit(&un->un_pri->unp_core_lock);
1373} 1373}
1374 1374
1375kmutex_t * 1375kmutex_t *
1376usbnet_mutex_core(struct usbnet *un) 1376usbnet_mutex_core(struct usbnet *un)
1377{ 1377{
1378 return &un->un_pri->unp_core_lock; 1378 return &un->un_pri->unp_core_lock;
1379} 1379}
1380 1380
1381void 1381void
1382usbnet_lock_rx(struct usbnet *un) 1382usbnet_lock_rx(struct usbnet *un)
1383{ 1383{
1384 mutex_enter(&un->un_pri->unp_rxlock); 1384 mutex_enter(&un->un_pri->unp_rxlock);
1385} 1385}
1386 1386
1387void 1387void
1388usbnet_unlock_rx(struct usbnet *un) 1388usbnet_unlock_rx(struct usbnet *un)
1389{ 1389{
1390 mutex_exit(&un->un_pri->unp_rxlock); 1390 mutex_exit(&un->un_pri->unp_rxlock);
1391} 1391}
1392 1392
1393kmutex_t * 1393kmutex_t *
1394usbnet_mutex_rx(struct usbnet *un) 1394usbnet_mutex_rx(struct usbnet *un)
1395{ 1395{
1396 return &un->un_pri->unp_rxlock; 1396 return &un->un_pri->unp_rxlock;
1397} 1397}
1398 1398
1399void 1399void
1400usbnet_lock_tx(struct usbnet *un) 1400usbnet_lock_tx(struct usbnet *un)
1401{ 1401{
1402 mutex_enter(&un->un_pri->unp_txlock); 1402 mutex_enter(&un->un_pri->unp_txlock);
1403} 1403}
1404 1404
1405void 1405void
1406usbnet_unlock_tx(struct usbnet *un) 1406usbnet_unlock_tx(struct usbnet *un)
1407{ 1407{
1408 mutex_exit(&un->un_pri->unp_txlock); 1408 mutex_exit(&un->un_pri->unp_txlock);
1409} 1409}
1410 1410
1411kmutex_t * 1411kmutex_t *
1412usbnet_mutex_tx(struct usbnet *un) 1412usbnet_mutex_tx(struct usbnet *un)
1413{ 1413{
1414 return &un->un_pri->unp_txlock; 1414 return &un->un_pri->unp_txlock;
1415} 1415}
1416 1416
1417/* Autoconf management. */ 1417/* Autoconf management. */
1418 1418
1419static bool 1419static bool
1420usbnet_empty_eaddr(struct usbnet * const un) 1420usbnet_empty_eaddr(struct usbnet * const un)
1421{ 1421{
1422 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 && 1422 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
1423 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 && 1423 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
1424 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0); 1424 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
1425} 1425}
1426 1426
1427/* 1427/*
1428 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant 1428 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
1429 * 'usbnet'. The first is enough to enable device access (eg, endpoints 1429 * 'usbnet'. The first is enough to enable device access (eg, endpoints
1430 * are connected and commands can be sent), and the second connects the 1430 * are connected and commands can be sent), and the second connects the
1431 * device to the system networking. 1431 * device to the system networking.
1432 * 1432 *
1433 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped. 1433 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
1434 * Also usable as driver detach directly. 1434 * Also usable as driver detach directly.
1435 * 1435 *
1436 * To skip ethernet configuration (eg, point-to-point), make sure that 1436 * To skip ethernet configuration (eg, point-to-point), make sure that
1437 * the un_eaddr[] is fully zero. 1437 * the un_eaddr[] is fully zero.
1438 */ 1438 */
1439 1439
1440void 1440void
1441usbnet_attach(struct usbnet *un, 1441usbnet_attach(struct usbnet *un,
1442 const char *detname) /* detach cv name */ 1442 const char *detname) /* detach cv name */
1443{ 1443{
1444 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1444 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1445 1445
1446 /* Required inputs. */ 1446 /* Required inputs. */
1447 KASSERT(un->un_ops->uno_tx_prepare); 1447 KASSERT(un->un_ops->uno_tx_prepare);
1448 KASSERT(un->un_ops->uno_rx_loop); 1448 KASSERT(un->un_ops->uno_rx_loop);
1449 KASSERT(un->un_ops->uno_init); 1449 KASSERT(un->un_ops->uno_init);
1450 KASSERT(un->un_rx_bufsz); 1450 KASSERT(un->un_rx_bufsz);
1451 KASSERT(un->un_tx_bufsz); 1451 KASSERT(un->un_tx_bufsz);
1452 KASSERT(un->un_rx_list_cnt); 1452 KASSERT(un->un_rx_list_cnt);
1453 KASSERT(un->un_tx_list_cnt); 1453 KASSERT(un->un_tx_list_cnt);
1454 1454
1455 /* Unfortunate fact. */ 1455 /* Unfortunate fact. */
1456 KASSERT(un == device_private(un->un_dev)); 1456 KASSERT(un == device_private(un->un_dev));
1457 1457
1458 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP); 1458 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP);
1459 struct usbnet_private * const unp = un->un_pri; 1459 struct usbnet_private * const unp = un->un_pri;
1460 1460
1461 usb_init_task(&unp->unp_mcasttask, usbnet_mcast_task, un, 1461 usb_init_task(&unp->unp_mcasttask, usbnet_mcast_task, un,
1462 USB_TASKQ_MPSAFE); 1462 USB_TASKQ_MPSAFE);
1463 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, 1463 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un,
1464 USB_TASKQ_MPSAFE); 1464 USB_TASKQ_MPSAFE);
1465 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE); 1465 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE);
1466 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un); 1466 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un);
1467 1467
1468 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1468 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1469 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1469 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1470 mutex_init(&unp->unp_core_lock, MUTEX_DEFAULT, IPL_NONE); 1470 mutex_init(&unp->unp_core_lock, MUTEX_DEFAULT, IPL_NONE);
1471 cv_init(&unp->unp_detachcv, detname); 1471 cv_init(&unp->unp_detachcv, detname);
1472 1472
1473 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev), 1473 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev),
1474 RND_TYPE_NET, RND_FLAG_DEFAULT); 1474 RND_TYPE_NET, RND_FLAG_DEFAULT);
1475 1475
1476 usbnet_rx_list_alloc(un); 1476 usbnet_rx_list_alloc(un);
1477 usbnet_tx_list_alloc(un); 1477 usbnet_tx_list_alloc(un);
1478 1478
1479 unp->unp_number = atomic_inc_uint_nv(&usbnet_number); 1479 unp->unp_number = atomic_inc_uint_nv(&usbnet_number);
1480 1480
1481 unp->unp_attached = true; 1481 unp->unp_attached = true;
1482} 1482}
1483 1483
1484static void 1484static void
1485usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm) 1485usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm)
1486{ 1486{
1487 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1487 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1488 struct usbnet_private * const unp = un->un_pri; 1488 struct usbnet_private * const unp = un->un_pri;
1489 struct mii_data * const mii = &unp->unp_mii; 1489 struct mii_data * const mii = &unp->unp_mii;
1490 struct ifnet * const ifp = usbnet_ifp(un); 1490 struct ifnet * const ifp = usbnet_ifp(un);
1491 1491
1492 KASSERT(un->un_ops->uno_read_reg); 1492 KASSERT(un->un_ops->uno_read_reg);
1493 KASSERT(un->un_ops->uno_write_reg); 1493 KASSERT(un->un_ops->uno_write_reg);
1494 KASSERT(un->un_ops->uno_statchg); 1494 KASSERT(un->un_ops->uno_statchg);
1495 1495
1496 mii->mii_ifp = ifp; 1496 mii->mii_ifp = ifp;
1497 mii->mii_readreg = usbnet_mii_readreg; 1497 mii->mii_readreg = usbnet_mii_readreg;
1498 mii->mii_writereg = usbnet_mii_writereg; 1498 mii->mii_writereg = usbnet_mii_writereg;
1499 mii->mii_statchg = usbnet_mii_statchg; 1499 mii->mii_statchg = usbnet_mii_statchg;
1500 mii->mii_flags = MIIF_AUTOTSLEEP; 1500 mii->mii_flags = MIIF_AUTOTSLEEP;
1501 1501
1502 usbnet_ec(un)->ec_mii = mii; 1502 usbnet_ec(un)->ec_mii = mii;
1503 ifmedia_init_with_lock(&mii->mii_media, 0, 1503 ifmedia_init_with_lock(&mii->mii_media, 0,
1504 usbnet_media_upd, ether_mediastatus, usbnet_mutex_core(un)); 1504 usbnet_media_upd, ether_mediastatus, usbnet_mutex_core(un));
1505 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc, 1505 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc,
1506 unm->un_mii_offset, unm->un_mii_flags); 1506 unm->un_mii_offset, unm->un_mii_flags);
1507 1507
1508 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1508 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1509 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1509 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1510 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1510 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1511 } else 1511 } else
1512 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1512 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1513} 1513}
1514 1514
1515void 1515void
1516usbnet_attach_ifp(struct usbnet *un, 1516usbnet_attach_ifp(struct usbnet *un,
1517 unsigned if_flags, /* additional if_flags */ 1517 unsigned if_flags, /* additional if_flags */
1518 unsigned if_extflags, /* additional if_extflags */ 1518 unsigned if_extflags, /* additional if_extflags */
1519 const struct usbnet_mii *unm) /* additional mii_attach flags */ 1519 const struct usbnet_mii *unm) /* additional mii_attach flags */
1520{ 1520{
1521 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1521 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1522 struct usbnet_private * const unp = un->un_pri; 1522 struct usbnet_private * const unp = un->un_pri;
1523 struct ifnet * const ifp = usbnet_ifp(un); 1523 struct ifnet * const ifp = usbnet_ifp(un);
1524 1524
1525 KASSERT(unp->unp_attached); 1525 KASSERT(unp->unp_attached);
1526 KASSERT(!unp->unp_ifp_attached); 1526 KASSERT(!unp->unp_ifp_attached);
1527 1527
1528 ifp->if_softc = un; 1528 ifp->if_softc = un;
1529 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ); 1529 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
1530 ifp->if_flags = if_flags; 1530 ifp->if_flags = if_flags;
1531 ifp->if_extflags = IFEF_MPSAFE | if_extflags; 1531 ifp->if_extflags = IFEF_MPSAFE | if_extflags;
1532 ifp->if_ioctl = usbnet_if_ioctl; 1532 ifp->if_ioctl = usbnet_if_ioctl;
1533 ifp->if_start = usbnet_if_start; 1533 ifp->if_start = usbnet_if_start;
1534 ifp->if_init = usbnet_if_init; 1534 ifp->if_init = usbnet_if_init;
1535 ifp->if_stop = usbnet_if_stop; 1535 ifp->if_stop = usbnet_if_stop;
1536 1536
1537 if (unm) 1537 if (unm)
1538 usbnet_attach_mii(un, unm); 1538 usbnet_attach_mii(un, unm);
1539 else 1539 else
1540 unp->unp_link = true; 1540 unp->unp_link = true;
1541 1541
1542 /* Attach the interface. */ 1542 /* Attach the interface. */
1543 if_initialize(ifp); 1543 if_initialize(ifp);
1544 if (ifp->_if_input == NULL) 1544 if (ifp->_if_input == NULL)
1545 ifp->if_percpuq = if_percpuq_create(ifp); 1545 ifp->if_percpuq = if_percpuq_create(ifp);
1546 if_register(ifp); 1546 if_register(ifp);
1547 unp->unp_ifp_attached = true; 1547 unp->unp_ifp_attached = true;
1548 1548
1549 /* 1549 /*
1550 * If ethernet address is all zero, skip ether_ifattach() and 1550 * If ethernet address is all zero, skip ether_ifattach() and
1551 * instead attach bpf here.. 1551 * instead attach bpf here..
1552 */ 1552 */
1553 if (!usbnet_empty_eaddr(un)) { 1553 if (!usbnet_empty_eaddr(un)) {
1554 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb); 1554 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb);
1555 aprint_normal_dev(un->un_dev, "Ethernet address %s\n", 1555 aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
1556 ether_sprintf(un->un_eaddr)); 1556 ether_sprintf(un->un_eaddr));
1557 ether_ifattach(ifp, un->un_eaddr); 1557 ether_ifattach(ifp, un->un_eaddr);
1558 } else { 1558 } else {
1559 if_alloc_sadl(ifp); 1559 if_alloc_sadl(ifp);
1560 bpf_attach(ifp, DLT_RAW, 0); 1560 bpf_attach(ifp, DLT_RAW, 0);
1561 } 1561 }
1562 1562
1563 /* Now ready, and attached. */ 1563 /* Now ready, and attached. */
1564 IFQ_SET_READY(&ifp->if_snd); 1564 IFQ_SET_READY(&ifp->if_snd);
1565 1565
1566 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev); 1566 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
1567 1567
1568 if (!pmf_device_register(un->un_dev, NULL, NULL)) 1568 if (!pmf_device_register(un->un_dev, NULL, NULL))
1569 aprint_error_dev(un->un_dev, "couldn't establish power handler\n"); 1569 aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
1570} 1570}
1571 1571
1572int 1572int
1573usbnet_detach(device_t self, int flags) 1573usbnet_detach(device_t self, int flags)
1574{ 1574{
1575 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1575 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1576 struct usbnet * const un = device_private(self); 1576 struct usbnet * const un = device_private(self);
1577 struct usbnet_private * const unp = un->un_pri; 1577 struct usbnet_private * const unp = un->un_pri;
1578 1578
1579 /* Detached before attached finished, so just bail out. */ 1579 /* Detached before attached finished, so just bail out. */
1580 if (unp == NULL || !unp->unp_attached) 1580 if (unp == NULL || !unp->unp_attached)
1581 return 0; 1581 return 0;
1582 1582
1583 struct ifnet * const ifp = usbnet_ifp(un); 1583 struct ifnet * const ifp = usbnet_ifp(un);
1584 struct mii_data * const mii = usbnet_mii(un); 1584 struct mii_data * const mii = usbnet_mii(un);
1585 1585
1586 /* 1586 /*
1587 * Prevent new activity. After we stop the interface, it 1587 * Prevent new activity. After we stop the interface, it
1588 * cannot be brought back up. 1588 * cannot be brought back up.
1589 */ 1589 */
1590 mutex_enter(&unp->unp_core_lock); 1590 mutex_enter(&unp->unp_core_lock);
1591 unp->unp_dying = true; 1591 unp->unp_dying = true;
1592 mutex_exit(&unp->unp_core_lock); 1592 mutex_exit(&unp->unp_core_lock);
1593 1593
1594 /* 1594 /*
1595 * If we're still running on the network, stop and wait for all 1595 * If we're still running on the network, stop and wait for all
1596 * asynchronous activity to finish. 1596 * asynchronous activity to finish.
1597 */ 1597 */
1598 IFNET_LOCK(ifp); 1598 IFNET_LOCK(ifp);
1599 if (ifp->if_flags & IFF_RUNNING) { 1599 if (ifp->if_flags & IFF_RUNNING) {
1600 usbnet_if_stop(ifp, 1); 1600 usbnet_if_stop(ifp, 1);
1601 } 1601 }
1602 IFNET_UNLOCK(ifp); 1602 IFNET_UNLOCK(ifp);
1603 1603
1604 /* 1604 /*
1605 * The callout and tick task can't be scheduled anew at this 1605 * The callout and tick task can't be scheduled anew at this
1606 * point, and usbnet_if_stop has waited for them to complete. 1606 * point, and usbnet_if_stop has waited for them to complete.
1607 */ 1607 */
1608 KASSERT(!callout_pending(&unp->unp_stat_ch)); 1608 KASSERT(!callout_pending(&unp->unp_stat_ch));
1609 KASSERT(!usb_task_pending(un->un_udev, &unp->unp_ticktask)); 1609 KASSERT(!usb_task_pending(un->un_udev, &unp->unp_ticktask));
1610 1610
1611 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, 1611 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER,
1612 NULL); 1612 NULL);
1613 1613
1614 if (mii) { 1614 if (mii) {
1615 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY); 1615 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
1616 ifmedia_fini(&mii->mii_media); 1616 ifmedia_fini(&mii->mii_media);
1617 } 1617 }
1618 if (unp->unp_ifp_attached) { 1618 if (unp->unp_ifp_attached) {
1619 if (!usbnet_empty_eaddr(un)) 1619 if (!usbnet_empty_eaddr(un))
1620 ether_ifdetach(ifp); 1620 ether_ifdetach(ifp);
1621 else 1621 else
1622 bpf_detach(ifp); 1622 bpf_detach(ifp);
1623 if_detach(ifp); 1623 if_detach(ifp);
1624 } 1624 }
1625 usbnet_ec(un)->ec_mii = NULL; 1625 usbnet_ec(un)->ec_mii = NULL;
1626 1626
1627 /* 1627 /*
1628 * We have already waited for the multicast task to complete. 1628 * We have already waited for the multicast task to complete.
1629 * Unfortunately, until if_detach, nothing has prevented it 1629 * Unfortunately, until if_detach, nothing has prevented it
1630 * from running again -- another thread might issue if_mcast_op 1630 * from running again -- another thread might issue if_mcast_op
1631 * between the time of our first usb_rem_task_wait and the time 1631 * between the time of our first usb_rem_task_wait and the time
1632 * we actually get around to if_detach. 1632 * we actually get around to if_detach.
1633 * 1633 *
1634 * Fortunately, the first usb_rem_task_wait ensures that if the 1634 * Fortunately, the first usb_rem_task_wait ensures that if the
1635 * task is scheduled again, it will witness our setting of 1635 * task is scheduled again, it will witness our setting of
1636 * unp_dying to true[*]. So after that point, if the task is 1636 * unp_dying to true[*]. So after that point, if the task is
1637 * scheduled again, it will decline to touch IFNET_LOCK and do 1637 * scheduled again, it will decline to touch IFNET_LOCK and do
1638 * nothing. But we still need to wait for it to complete. 1638 * nothing. But we still need to wait for it to complete.
1639 * 1639 *
1640 * It would be nice if we could write 1640 * It would be nice if we could write
1641 * 1641 *
1642 * if_pleasestopissuingmcastopsthanks(ifp); 1642 * if_pleasestopissuingmcastopsthanks(ifp);
1643 * usb_rem_task_wait(..., &unp->unp_mcasttask, ...); 1643 * usb_rem_task_wait(..., &unp->unp_mcasttask, ...);
1644 * if_detach(ifp); 1644 * if_detach(ifp);
1645 * 1645 *
1646 * and then we would need only one usb_rem_task_wait. 1646 * and then we would need only one usb_rem_task_wait.
1647 * 1647 *
1648 * Unfortunately, there is no such operation available in 1648 * Unfortunately, there is no such operation available in
1649 * sys/net at the moment, and it would require a bit of 1649 * sys/net at the moment, and it would require a bit of
1650 * coordination with if_mcast_op and doifioctl probably under a 1650 * coordination with if_mcast_op and doifioctl probably under a
1651 * new lock. So we'll use this kludge until that mechanism is 1651 * new lock. So we'll use this kludge until that mechanism is
1652 * invented. 1652 * invented.
1653 * 1653 *
1654 * [*] This is not exactly a documented property of the API, 1654 * [*] This is not exactly a documented property of the API,
1655 * but it is implied by the single lock in the task queue 1655 * but it is implied by the single lock in the task queue
1656 * serializing changes to the task state. 1656 * serializing changes to the task state.
1657 */ 1657 */
1658 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, 1658 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER,
1659 NULL); 1659 NULL);
1660 1660
1661 mutex_enter(&unp->unp_core_lock); 1661 mutex_enter(&unp->unp_core_lock);
1662 unp->unp_refcnt--; 1662 unp->unp_refcnt--;
1663 while (unp->unp_refcnt >= 0) { 1663 while (unp->unp_refcnt >= 0) {
1664 /* Wait for processes to go away */ 1664 /* Wait for processes to go away */
1665 cv_wait(&unp->unp_detachcv, &unp->unp_core_lock); 1665 cv_wait(&unp->unp_detachcv, &unp->unp_core_lock);
1666 } 1666 }
1667 mutex_exit(&unp->unp_core_lock); 1667 mutex_exit(&unp->unp_core_lock);
1668 1668
1669 usbnet_rx_list_free(un); 1669 usbnet_rx_list_free(un);
1670 usbnet_tx_list_free(un); 1670 usbnet_tx_list_free(un);
1671 1671
1672 rnd_detach_source(&unp->unp_rndsrc); 1672 rnd_detach_source(&unp->unp_rndsrc);
1673 1673
1674 cv_destroy(&unp->unp_detachcv); 1674 cv_destroy(&unp->unp_detachcv);
1675 mutex_destroy(&unp->unp_core_lock); 1675 mutex_destroy(&unp->unp_core_lock);
1676 mutex_destroy(&unp->unp_rxlock); 1676 mutex_destroy(&unp->unp_rxlock);
1677 mutex_destroy(&unp->unp_txlock); 1677 mutex_destroy(&unp->unp_txlock);
1678 1678
1679 callout_destroy(&unp->unp_stat_ch); 1679 callout_destroy(&unp->unp_stat_ch);
1680 1680
1681 pmf_device_deregister(un->un_dev); 1681 pmf_device_deregister(un->un_dev);
1682 1682
1683 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev); 1683 /*
 1684 * Notify userland that we're going away, if we arrived in the
 1685 * first place.
 1686 */
 1687 if (unp->unp_ifp_attached) {
 1688 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev,
 1689 un->un_dev);
 1690 }
1684 1691
1685 kmem_free(unp, sizeof(*unp)); 1692 kmem_free(unp, sizeof(*unp));
1686 un->un_pri = NULL; 1693 un->un_pri = NULL;
1687 1694
1688 return 0; 1695 return 0;
1689} 1696}
1690 1697
1691int 1698int
1692usbnet_activate(device_t self, devact_t act) 1699usbnet_activate(device_t self, devact_t act)
1693{ 1700{
1694 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1701 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1695 struct usbnet * const un = device_private(self); 1702 struct usbnet * const un = device_private(self);
1696 struct usbnet_private * const unp = un->un_pri; 1703 struct usbnet_private * const unp = un->un_pri;
1697 struct ifnet * const ifp = usbnet_ifp(un); 1704 struct ifnet * const ifp = usbnet_ifp(un);
1698 1705
1699 switch (act) { 1706 switch (act) {
1700 case DVACT_DEACTIVATE: 1707 case DVACT_DEACTIVATE:
1701 if_deactivate(ifp); 1708 if_deactivate(ifp);
1702 1709
1703 mutex_enter(&unp->unp_core_lock); 1710 mutex_enter(&unp->unp_core_lock);
1704 unp->unp_dying = true; 1711 unp->unp_dying = true;
1705 mutex_exit(&unp->unp_core_lock); 1712 mutex_exit(&unp->unp_core_lock);
1706 1713
1707 mutex_enter(&unp->unp_rxlock); 1714 mutex_enter(&unp->unp_rxlock);
1708 mutex_enter(&unp->unp_txlock); 1715 mutex_enter(&unp->unp_txlock);
1709 unp->unp_stopping = true; 1716 unp->unp_stopping = true;
1710 mutex_exit(&unp->unp_txlock); 1717 mutex_exit(&unp->unp_txlock);
1711 mutex_exit(&unp->unp_rxlock); 1718 mutex_exit(&unp->unp_rxlock);
1712 1719
1713 return 0; 1720 return 0;
1714 default: 1721 default:
1715 return EOPNOTSUPP; 1722 return EOPNOTSUPP;
1716 } 1723 }
1717} 1724}
1718 1725
1719MODULE(MODULE_CLASS_MISC, usbnet, NULL); 1726MODULE(MODULE_CLASS_MISC, usbnet, NULL);
1720 1727
1721static int 1728static int
1722usbnet_modcmd(modcmd_t cmd, void *arg) 1729usbnet_modcmd(modcmd_t cmd, void *arg)
1723{ 1730{
1724 switch (cmd) { 1731 switch (cmd) {
1725 case MODULE_CMD_INIT: 1732 case MODULE_CMD_INIT:
1726 return 0; 1733 return 0;
1727 case MODULE_CMD_FINI: 1734 case MODULE_CMD_FINI:
1728 return 0; 1735 return 0;
1729 case MODULE_CMD_STAT: 1736 case MODULE_CMD_STAT:
1730 case MODULE_CMD_AUTOUNLOAD: 1737 case MODULE_CMD_AUTOUNLOAD:
1731 default: 1738 default:
1732 return ENOTTY; 1739 return ENOTTY;
1733 } 1740 }
1734} 1741}