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