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