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