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