xref: /netbsd-src/sys/dev/usb/usbnet.c (revision 2d8e86c2f207da6fbbd50f11b6f33765ebdfa0e9)
1 /*	$NetBSD: usbnet.c,v 1.9 2019/08/07 10:01:05 maya 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 ethernet drivers.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.9 2019/08/07 10:01:05 maya Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/kmem.h>
41 #include <sys/module.h>
42 
43 #include <dev/usb/usbnet.h>
44 #include <dev/usb/usbhist.h>
45 
46 static int usbnet_modcmd(modcmd_t, void *);
47 
48 #ifdef USB_DEBUG
49 #ifndef USBNET_DEBUG
50 #define usbnetdebug 0
51 #else
52 static int usbnetdebug = 1;
53 
54 int     sysctl_hw_usbnet_setup(SYSCTLFN_PROTO);
55 
56 SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup")
57 {
58 	int err;
59 	const struct sysctlnode *rnode;
60 	const struct sysctlnode *cnode;
61 
62 	err = sysctl_createv(clog, 0, NULL, &rnode,
63 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet",
64 	    SYSCTL_DESCR("usbnet global controls"),
65 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
66 
67 	if (err)
68 		goto fail;
69 
70 	/* control debugging printfs */
71 	err = sysctl_createv(clog, 0, &rnode, &cnode,
72 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
73 	    "debug", SYSCTL_DESCR("Enable debugging output"),
74 	    NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL);
75 	if (err)
76 		goto fail;
77 
78 	return;
79 fail:
80 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
81 }
82 
83 #endif /* USBNET_DEBUG */
84 #endif /* USB_DEBUG */
85 
86 #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D)
87 #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D)
88 #define USBNETHIST_FUNC()	USBHIST_FUNC()
89 #define USBNETHIST_CALLED(name)	USBHIST_CALLED(usbnetdebug)
90 
91 /* Interrupt handling. */
92 
93 static struct mbuf *
94 usbnet_newbuf(void)
95 {
96 	struct mbuf *m;
97 
98 	MGETHDR(m, M_DONTWAIT, MT_DATA);
99 	if (m == NULL)
100 		return NULL;
101 
102 	MCLGET(m, M_DONTWAIT);
103 	if (!(m->m_flags & M_EXT)) {
104 		m_freem(m);
105 		return NULL;
106 	}
107 
108 	m->m_len = m->m_pkthdr.len = MCLBYTES;
109 	m_adj(m, ETHER_ALIGN);
110 
111 	return m;
112 }
113 
114 /*
115  * usbnet_rxeof() is designed to be the done callback for rx completion.
116  * it provides generic setup and finalisation, calls a different usbnet
117  * rx_loop callback in the middle, which can use usbnet_enqueue() to
118  * enqueue a packet for higher levels (or usbnet_input() if previously
119  * using if_input() path.)
120  */
121 void
122 usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen,
123 	       int csum_flags, uint32_t csum_data, int mbuf_flags)
124 {
125 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
126 	struct ifnet *ifp = &un->un_ec.ec_if;
127 	struct mbuf *m;
128 
129 	KASSERT(mutex_owned(&un->un_rxlock));
130 
131 	m = usbnet_newbuf();
132 	if (m == NULL) {
133 		ifp->if_ierrors++;
134 		return;
135 	}
136 
137 	m_set_rcvif(m, ifp);
138 	m->m_pkthdr.len = m->m_len = buflen;
139 	m->m_pkthdr.csum_flags = csum_flags;
140 	m->m_pkthdr.csum_data = csum_data;
141 	m->m_flags |= mbuf_flags;
142 	memcpy(mtod(m, char *), buf, buflen);
143 
144 	/* push the packet up */
145 	if_percpuq_enqueue(ifp->if_percpuq, m);
146 }
147 
148 void
149 usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen)
150 {
151 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
152 	struct ifnet * const ifp = usbnet_ifp(un);
153 	struct mbuf *m;
154 
155 	KASSERT(mutex_owned(&un->un_rxlock));
156 
157 	m = usbnet_newbuf();
158 	if (m == NULL) {
159 		ifp->if_ierrors++;
160 		return;
161 	}
162 
163 	m_set_rcvif(m, ifp);
164 	m->m_pkthdr.len = m->m_len = buflen;
165 	memcpy(mtod(m, char *), buf, buflen);
166 
167 	/* push the packet up */
168 	if_input(ifp, m);
169 }
170 
171 /*
172  * A frame has been uploaded: pass the resulting mbuf chain up to
173  * the higher level protocols.
174  */
175 static void
176 usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
177 {
178 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
179 	struct usbnet_chain *c = priv;
180 	struct usbnet * const un = c->unc_un;
181 	struct ifnet * const ifp = usbnet_ifp(un);
182 	uint32_t total_len;
183 
184 	mutex_enter(&un->un_rxlock);
185 
186 	if (un->un_dying || un->un_stopping ||
187 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
188 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
189 		goto out;
190 
191 	if (status != USBD_NORMAL_COMPLETION) {
192 		if (usbd_ratecheck(&un->un_rx_notice))
193 			aprint_error_dev(un->un_dev, "usb errors on rx: %s\n",
194 			    usbd_errstr(status));
195 		if (status == USBD_STALLED)
196 			usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_RX]);
197 		goto done;
198 	}
199 
200 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
201 
202 	if (total_len > un->un_cdata.uncd_rx_bufsz) {
203 		aprint_error_dev(un->un_dev,
204 		    "rxeof: too large transfer (%u > %u)\n",
205 		    total_len, un->un_cdata.uncd_rx_bufsz);
206 		goto done;
207 	}
208 
209 	(*un->un_rx_loop_cb)(un, xfer, c, total_len);
210 	KASSERT(mutex_owned(&un->un_rxlock));
211 
212 done:
213 	if (un->un_dying || un->un_stopping)
214 		goto out;
215 
216 	mutex_exit(&un->un_rxlock);
217 
218 	/* Setup new transfer. */
219 	usbd_setup_xfer(xfer, c, c->unc_buf, un->un_cdata.uncd_rx_bufsz,
220 	    un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
221 	usbd_transfer(xfer);
222 	return;
223 
224 out:
225 	mutex_exit(&un->un_rxlock);
226 }
227 
228 static void
229 usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
230 {
231 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
232 	struct usbnet_chain *c = priv;
233 	struct usbnet * const un = c->unc_un;
234 	struct usbnet_cdata *cd = &un->un_cdata;
235 	struct ifnet * const ifp = usbnet_ifp(un);
236 
237 	mutex_enter(&un->un_txlock);
238 	if (un->un_stopping || un->un_dying) {
239 		mutex_exit(&un->un_txlock);
240 		return;
241 	}
242 
243 	KASSERT(cd->uncd_tx_cnt > 0);
244 	cd->uncd_tx_cnt--;
245 
246 	un->un_timer = 0;
247 
248 	switch (status) {
249 	case USBD_NOT_STARTED:
250 	case USBD_CANCELLED:
251 		break;
252 
253 	case USBD_NORMAL_COMPLETION:
254 		ifp->if_opackets++;
255 		break;
256 
257 	default:
258 
259 		ifp->if_oerrors++;
260 		if (usbd_ratecheck(&un->un_tx_notice))
261 			aprint_error_dev(un->un_dev, "usb error on tx: %s\n",
262 			    usbd_errstr(status));
263 		if (status == USBD_STALLED)
264 			usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_TX]);
265 		break;
266 	}
267 
268 	mutex_exit(&un->un_txlock);
269 
270 	if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
271 		(*ifp->if_start)(ifp);
272 }
273 
274 static void
275 usbnet_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
276 {
277 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
278 	struct usbnet		*un = priv;
279 	struct ifnet		*ifp = usbnet_ifp(un);
280 
281 	if (un->un_dying || un->un_stopping ||
282 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
283 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
284 		return;
285 
286 	if (status != USBD_NORMAL_COMPLETION) {
287 		if (usbd_ratecheck(&un->un_intr_notice)) {
288 			aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
289 			    usbd_errstr(status));
290 		}
291 		if (status == USBD_STALLED)
292 			usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_INTR]);
293 		return;
294 	}
295 
296 	if (un->un_intr_cb)
297 		(*un->un_intr_cb)(un, status);
298 }
299 
300 static void
301 usbnet_start_locked(struct ifnet *ifp)
302 {
303 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
304 	struct usbnet * const un = ifp->if_softc;
305 	struct usbnet_cdata *cd = &un->un_cdata;
306 	struct mbuf *m;
307 	unsigned length;
308 	int idx;
309 
310 	KASSERT(mutex_owned(&un->un_txlock));
311 	KASSERT(cd->uncd_tx_cnt <= cd->uncd_tx_list_cnt);
312 
313 	if (!un->un_link || (ifp->if_flags & IFF_RUNNING) == 0)
314 		return;
315 
316 	idx = cd->uncd_tx_prod;
317 	while (cd->uncd_tx_cnt < cd->uncd_tx_list_cnt) {
318 		IFQ_POLL(&ifp->if_snd, m);
319 		if (m == NULL)
320 			break;
321 
322 		struct usbnet_chain *c = &un->un_cdata.uncd_tx_chain[idx];
323 
324 		length = (*un->un_tx_prepare_cb)(un, m, c);
325 		if (length == 0) {
326 			ifp->if_oerrors++;
327 			break;
328 		}
329 
330 		if (__predict_false(c->unc_xfer == NULL)) {
331 			ifp->if_oerrors++;
332 			break;
333 		}
334 
335 		usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
336 		    un->un_tx_xfer_flags, 10000, usbnet_txeof);
337 
338 		/* Transmit */
339 		usbd_status err = usbd_transfer(c->unc_xfer);
340 		if (err != USBD_IN_PROGRESS) {
341 			ifp->if_oerrors++;
342 			break;
343 		}
344 
345 		IFQ_DEQUEUE(&ifp->if_snd, m);
346 
347 		/*
348 		 * If there's a BPF listener, bounce a copy of this frame
349 		 * to him.
350 		 */
351 		bpf_mtap(ifp, m, BPF_D_OUT);
352 		m_freem(m);
353 
354 		idx = (idx + 1) % cd->uncd_tx_list_cnt;
355 		cd->uncd_tx_cnt++;
356 	}
357 	cd->uncd_tx_prod = idx;
358 
359 	/*
360 	 * Set a timeout in case the chip goes out to lunch.
361 	 */
362 	un->un_timer = 5;
363 }
364 
365 static void
366 usbnet_start(struct ifnet *ifp)
367 {
368 	struct usbnet * const un = ifp->if_softc;
369 
370 	mutex_enter(&un->un_txlock);
371 	if (!un->un_stopping)
372 		usbnet_start_locked(ifp);
373 	mutex_exit(&un->un_txlock);
374 }
375 
376 /*
377  * Chain management.
378  *
379  * RX and TX are identical. Keep them that way.
380  */
381 
382 /* Start of common RX functions */
383 
384 static size_t
385 usbnet_rx_list_size(struct usbnet_cdata *cd)
386 {
387 	return sizeof(*cd->uncd_rx_chain) * cd->uncd_rx_list_cnt;
388 }
389 
390 static void
391 usbnet_rx_list_alloc(struct usbnet *un, unsigned cnt)
392 {
393 	struct usbnet_cdata *cd = &un->un_cdata;
394 
395 	cd->uncd_rx_list_cnt = cnt;
396 	cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd), KM_SLEEP);
397 }
398 
399 static void
400 usbnet_rx_list_free(struct usbnet *un)
401 {
402 	struct usbnet_cdata *cd = &un->un_cdata;
403 
404 	if (cd->uncd_rx_chain) {
405 		kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd));
406 		cd->uncd_rx_chain = NULL;
407 	}
408 }
409 
410 static int
411 usbnet_rx_list_init(struct usbnet *un, unsigned xfer_flags)
412 {
413 	struct usbnet_cdata *cd = &un->un_cdata;
414 
415 	for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
416 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
417 
418 		c->unc_un = un;
419 		if (c->unc_xfer == NULL) {
420 			int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_RX],
421 			    cd->uncd_rx_bufsz, xfer_flags, 0, &c->unc_xfer);
422 			if (err)
423 				return err;
424 			c->unc_buf = usbd_get_buffer(c->unc_xfer);
425 		}
426 	}
427 
428 	return 0;
429 }
430 
431 static void
432 usbnet_rx_list_fini(struct usbnet *un)
433 {
434 	struct usbnet_cdata *cd = &un->un_cdata;
435 
436 	for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
437 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
438 
439 		if (c->unc_xfer != NULL) {
440 			usbd_destroy_xfer(c->unc_xfer);
441 			c->unc_xfer = NULL;
442 			c->unc_buf = NULL;
443 		}
444 	}
445 }
446 
447 /* End of common RX functions */
448 
449 static void
450 usbnet_rx_start_pipes(struct usbnet *un, usbd_callback cb)
451 {
452 	struct usbnet_cdata *cd = &un->un_cdata;
453 
454 	mutex_enter(&un->un_rxlock);
455 	mutex_enter(&un->un_txlock);
456 	un->un_stopping = false;
457 
458 	for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
459 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
460 
461 		usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, cd->uncd_rx_bufsz,
462 		    un->un_rx_xfer_flags, USBD_NO_TIMEOUT, cb);
463 		usbd_transfer(c->unc_xfer);
464 	}
465 
466 	mutex_exit(&un->un_txlock);
467 	mutex_exit(&un->un_rxlock);
468 }
469 
470 /* Start of common TX functions */
471 
472 static size_t
473 usbnet_tx_list_size(struct usbnet_cdata *cd)
474 {
475 	return sizeof(*cd->uncd_tx_chain) * cd->uncd_tx_list_cnt;
476 }
477 
478 static void
479 usbnet_tx_list_alloc(struct usbnet *un, unsigned cnt)
480 {
481 	struct usbnet_cdata *cd = &un->un_cdata;
482 
483 	cd->uncd_tx_list_cnt = cnt;
484 	cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd), KM_SLEEP);
485 }
486 
487 static void
488 usbnet_tx_list_free(struct usbnet *un)
489 {
490 	struct usbnet_cdata *cd = &un->un_cdata;
491 
492 	if (cd->uncd_tx_chain) {
493 		kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd));
494 		cd->uncd_tx_chain = NULL;
495 	}
496 }
497 
498 static int
499 usbnet_tx_list_init(struct usbnet *un, unsigned xfer_flags)
500 {
501 	struct usbnet_cdata *cd = &un->un_cdata;
502 
503 	for (size_t i = 0; i < cd->uncd_tx_list_cnt; i++) {
504 		struct usbnet_chain *c = &cd->uncd_tx_chain[i];
505 
506 		c->unc_un = un;
507 		if (c->unc_xfer == NULL) {
508 			int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_TX],
509 			    cd->uncd_tx_bufsz, xfer_flags, 0, &c->unc_xfer);
510 			if (err)
511 				return err;
512 			c->unc_buf = usbd_get_buffer(c->unc_xfer);
513 		}
514 	}
515 
516 	return 0;
517 }
518 
519 static void
520 usbnet_tx_list_fini(struct usbnet *un)
521 {
522 	struct usbnet_cdata *cd = &un->un_cdata;
523 
524 	for (size_t i = 0; i < cd->uncd_tx_list_cnt; i++) {
525 		struct usbnet_chain *c = &cd->uncd_tx_chain[i];
526 
527 		if (c->unc_xfer != NULL) {
528 			usbd_destroy_xfer(c->unc_xfer);
529 			c->unc_xfer = NULL;
530 			c->unc_buf = NULL;
531 		}
532 	}
533 }
534 
535 /* End of common TX functions */
536 
537 /* Endpoint pipe management. */
538 
539 static void
540 usbnet_ep_close_pipes(struct usbnet *un)
541 {
542 	for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
543 		if (un->un_ep[i] == NULL)
544 			continue;
545 		usbd_status err = usbd_close_pipe(un->un_ep[i]);
546 		if (err)
547 			aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
548 			    usbd_errstr(err));
549 		un->un_ep[i] = NULL;
550 	}
551 }
552 
553 static usbd_status
554 usbnet_ep_open_pipes(struct usbnet *un)
555 {
556 	for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
557 		usbd_status err;
558 
559 		if (un->un_ed[i] == 0)
560 			continue;
561 
562 		if (i == USBNET_ENDPT_INTR && un->un_intr_buf) {
563 			err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
564 			    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &un->un_ep[i], un,
565 			    un->un_intr_buf, un->un_intr_bufsz, usbnet_intr,
566 			    un->un_intr_interval);
567 		} else {
568 			err = usbd_open_pipe(un->un_iface, un->un_ed[i],
569 			    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &un->un_ep[i]);
570 		}
571 		if (err) {
572 			usbnet_ep_close_pipes(un);
573 			return err;
574 		}
575 	}
576 
577 	return USBD_NORMAL_COMPLETION;
578 }
579 
580 static usbd_status
581 usbnet_ep_stop_pipes(struct usbnet *un)
582 {
583 	for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
584 		if (un->un_ep[i] == NULL)
585 			continue;
586 		usbd_status err = usbd_abort_pipe(un->un_ep[i]);
587 		if (err)
588 			return err;
589 	}
590 
591 	return USBD_NORMAL_COMPLETION;
592 }
593 
594 int
595 usbnet_init_rx_tx(struct usbnet * const un, unsigned rxflags, unsigned txflags)
596 {
597 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
598 	struct ifnet * const ifp = usbnet_ifp(un);
599 	usbd_status err;
600 	int error = 0;
601 
602 	usbnet_isowned(un);
603 
604 	if (un->un_dying) {
605 		return EIO;
606 	}
607 	un->un_refcnt++;
608 
609 	/* Open RX and TX pipes. */
610 	err = usbnet_ep_open_pipes(un);
611 	if (err) {
612 		aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
613 		    usbd_errstr(err));
614 		error = EIO;
615 		goto out;
616 	}
617 
618 	/* Init RX ring. */
619 	if (usbnet_rx_list_init(un, rxflags)) {
620 		aprint_error_dev(un->un_dev, "rx list init failed\n");
621 		error = ENOBUFS;
622 		goto out;
623 	}
624 
625 	/* Init TX ring. */
626 	if (usbnet_tx_list_init(un, txflags)) {
627 		aprint_error_dev(un->un_dev, "tx list init failed\n");
628 		error = ENOBUFS;
629 		goto out;
630 	}
631 
632 	/* Start up the receive pipe(s). */
633 	usbnet_rx_start_pipes(un, usbnet_rxeof);
634 
635 	/* Indicate we are up and running. */
636 	KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
637 	ifp->if_flags |= IFF_RUNNING;
638 
639 	callout_schedule(&un->un_stat_ch, hz);
640 
641 out:
642 	if (error) {
643 		usbnet_rx_list_fini(un);
644 		usbnet_tx_list_fini(un);
645 		usbnet_ep_close_pipes(un);
646 	}
647 	if (--un->un_refcnt < 0)
648 		cv_broadcast(&un->un_detachcv);
649 
650 	usbnet_isowned(un);
651 
652 	return error;
653 }
654 
655 /* MII management. */
656 
657 /*
658  * Access functions for MII.  Take the MII lock to call access MII regs.
659  * Two forms: usbnet (softc) lock currently held or not.
660  */
661 void
662 usbnet_lock_mii(struct usbnet *un)
663 {
664 
665 	mutex_enter(&un->un_lock);
666 	un->un_refcnt++;
667 	mutex_exit(&un->un_lock);
668 
669 	mutex_enter(&un->un_miilock);
670 }
671 
672 void
673 usbnet_lock_mii_un_locked(struct usbnet *un)
674 {
675 	KASSERT(mutex_owned(&un->un_lock));
676 
677 	un->un_refcnt++;
678 	mutex_enter(&un->un_miilock);
679 }
680 
681 void
682 usbnet_unlock_mii(struct usbnet *un)
683 {
684 
685 	mutex_exit(&un->un_miilock);
686 	mutex_enter(&un->un_lock);
687 	if (--un->un_refcnt < 0)
688 		cv_broadcast(&un->un_detachcv);
689 	mutex_exit(&un->un_lock);
690 }
691 
692 void
693 usbnet_unlock_mii_un_locked(struct usbnet *un)
694 {
695 	KASSERT(mutex_owned(&un->un_lock));
696 
697 	mutex_exit(&un->un_miilock);
698 	if (--un->un_refcnt < 0)
699 		cv_broadcast(&un->un_detachcv);
700 }
701 
702 int
703 usbnet_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
704 {
705 	struct usbnet * const un = device_private(dev);
706 	usbd_status err;
707 
708 	mutex_enter(&un->un_lock);
709 	if (un->un_dying || un->un_phyno != phy) {
710 		mutex_exit(&un->un_lock);
711 		return EIO;
712 	}
713 	mutex_exit(&un->un_lock);
714 
715 	usbnet_lock_mii(un);
716 	err = (*un->un_read_reg_cb)(un, phy, reg, val);
717 	usbnet_unlock_mii(un);
718 
719 	if (err) {
720 		aprint_error_dev(un->un_dev, "read PHY failed: %d\n", err);
721 		return EIO;
722 	}
723 
724 	return 0;
725 }
726 
727 int
728 usbnet_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
729 {
730 	struct usbnet * const un = device_private(dev);
731 	usbd_status err;
732 
733 	mutex_enter(&un->un_lock);
734 	if (un->un_dying || un->un_phyno != phy) {
735 		mutex_exit(&un->un_lock);
736 		return EIO;
737 	}
738 	mutex_exit(&un->un_lock);
739 
740 	usbnet_lock_mii(un);
741 	err = (*un->un_write_reg_cb)(un, phy, reg, val);
742 	usbnet_unlock_mii(un);
743 
744 	if (err) {
745 		aprint_error_dev(un->un_dev, "write PHY failed: %d\n", err);
746 		return EIO;
747 	}
748 
749 	return 0;
750 }
751 
752 void
753 usbnet_miibus_statchg(struct ifnet *ifp)
754 {
755 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
756 	struct usbnet * const un = ifp->if_softc;
757 
758 	(*un->un_statchg_cb)(ifp);
759 }
760 
761 static int
762 usbnet_media_upd(struct ifnet *ifp)
763 {
764 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
765 	struct usbnet * const un = ifp->if_softc;
766 	struct mii_data * const mii = usbnet_mii(un);
767 
768 	if (un->un_dying)
769 		return EIO;
770 
771 	un->un_link = false;
772 
773 	if (mii->mii_instance) {
774 		struct mii_softc *miisc;
775 
776 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
777 			mii_phy_reset(miisc);
778 	}
779 
780 	return ether_mediachange(ifp);
781 }
782 
783 /* ioctl */
784 
785 static int
786 usbnet_ifflags_cb(struct ethercom *ec)
787 {
788 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
789 	struct ifnet *ifp = &ec->ec_if;
790 	struct usbnet *un = ifp->if_softc;
791 	int rv = 0;
792 
793 	mutex_enter(&un->un_lock);
794 
795 	const int changed = ifp->if_flags ^ un->un_if_flags;
796 	if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
797 		un->un_if_flags = ifp->if_flags;
798 		if ((changed & IFF_PROMISC) != 0)
799 			rv = ENETRESET;
800 	} else {
801 		rv = ENETRESET;
802 	}
803 
804 	mutex_exit(&un->un_lock);
805 
806 	return rv;
807 }
808 
809 static int
810 usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
811 {
812 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
813 	struct usbnet * const un = ifp->if_softc;
814 	int error;
815 
816 	if (un->un_override_ioctl_cb)
817 		return (*un->un_override_ioctl_cb)(ifp, cmd, data);
818 
819 	error = ether_ioctl(ifp, cmd, data);
820 	if (error == ENETRESET) {
821 		if (un->un_ioctl_cb)
822 			error = (*un->un_ioctl_cb)(ifp, cmd, data);
823 		else
824 			error = 0;
825 	}
826 
827 	return error;
828 }
829 
830 /*
831  * Generic stop network function:
832  *	- mark as stopping
833  *	- call DD routine to stop the device
834  *	- turn off running, timer, statchg callout, link
835  *	- stop transfers
836  *	- free RX and TX resources
837  *	- close pipes
838  *
839  * usbnet_stop() is exported for drivers to use, expects lock held.
840  *
841  * usbnet_stop_ifp() is for the if_stop handler.
842  */
843 void
844 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
845 {
846 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
847 
848 	KASSERT(mutex_owned(&un->un_lock));
849 
850 	mutex_enter(&un->un_rxlock);
851 	mutex_enter(&un->un_txlock);
852 	un->un_stopping = true;
853 	mutex_exit(&un->un_txlock);
854 	mutex_exit(&un->un_rxlock);
855 
856 	if (un->un_stop_cb)
857 		(*un->un_stop_cb)(ifp, disable);
858 
859 	/*
860 	 * XXXSMP Would like to
861 	 *	KASSERT(IFNET_LOCKED(ifp))
862 	 * here but the locking order is:
863 	 *	ifnet -> unlock -> rxlock -> txlock
864 	 * and unlock is already held.
865 	 */
866 	ifp->if_flags &= ~IFF_RUNNING;
867 	un->un_timer = 0;
868 
869 	callout_stop(&un->un_stat_ch);
870 	un->un_link = false;
871 
872 	/* Stop transfers. */
873 	usbnet_ep_stop_pipes(un);
874 
875 	/* Free RX/TX resources. */
876 	usbnet_rx_list_fini(un);
877 	usbnet_tx_list_fini(un);
878 
879 	/* Close pipes. */
880 	usbnet_ep_close_pipes(un);
881 }
882 
883 static void
884 usbnet_stop_ifp(struct ifnet *ifp, int disable)
885 {
886 	struct usbnet * const un = ifp->if_softc;
887 
888 	mutex_enter(&un->un_lock);
889 	usbnet_stop(un, ifp, disable);
890 	mutex_exit(&un->un_lock);
891 }
892 
893 /*
894  * Generic tick task function.
895  *
896  * usbnet_tick() is triggered from a callout, and triggers a call to
897  * usbnet_tick_task() from the usb_task subsystem.
898  */
899 static void
900 usbnet_tick(void *arg)
901 {
902 	struct usbnet * const un = arg;
903 
904 	mutex_enter(&un->un_lock);
905 	if (!un->un_stopping && !un->un_dying) {
906 		/* Perform periodic stuff in process context */
907 		usb_add_task(un->un_udev, &un->un_ticktask, USB_TASKQ_DRIVER);
908 	}
909 	mutex_exit(&un->un_lock);
910 }
911 
912 static void
913 usbnet_watchdog(struct ifnet *ifp)
914 {
915 	struct usbnet * const un = ifp->if_softc;
916 	struct usbnet_cdata *cd = &un->un_cdata;
917 	usbd_status stat;
918 
919 	ifp->if_oerrors++;
920 	aprint_error_dev(un->un_dev, "watchdog timeout\n");
921 
922 	if (cd->uncd_tx_cnt > 0) {
923 		/*
924 		 * XXX index 0
925 		 */
926 		struct usbnet_chain *c = &un->un_cdata.uncd_tx_chain[0];
927 		usbd_get_xfer_status(c->unc_xfer, NULL, NULL, NULL, &stat);
928 		usbnet_txeof(c->unc_xfer, c, stat);
929 	}
930 
931 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
932 		(*ifp->if_start)(ifp);
933 }
934 
935 static void
936 usbnet_tick_task(void *arg)
937 {
938 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
939 	struct usbnet * const un = arg;
940 
941 	mutex_enter(&un->un_lock);
942 	if (un->un_stopping || un->un_dying) {
943 		mutex_exit(&un->un_lock);
944 		return;
945 	}
946 
947 	struct ifnet * const ifp = usbnet_ifp(un);
948 	struct mii_data * const mii = usbnet_mii(un);
949 
950 	un->un_refcnt++;
951 	mutex_exit(&un->un_lock);
952 
953 	if (ifp && un->un_timer != 0 && --un->un_timer == 0)
954 		usbnet_watchdog(ifp);
955 
956 	if (mii && ifp) {
957 		mii_tick(mii);
958 
959 		if (!un->un_link)
960 			(*mii->mii_statchg)(ifp);
961 	}
962 
963 	mutex_enter(&un->un_lock);
964 	if (--un->un_refcnt < 0)
965 		cv_broadcast(&un->un_detachcv);
966 	if (!un->un_stopping && !un->un_dying)
967 		callout_schedule(&un->un_stat_ch, hz);
968 	mutex_exit(&un->un_lock);
969 }
970 
971 static int
972 usbnet_init(struct ifnet *ifp)
973 {
974 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
975 	struct usbnet * const un = ifp->if_softc;
976 
977 	return (*un->un_init_cb)(ifp);
978 }
979 
980 /* Autoconf management. */
981 
982 static bool
983 usbnet_empty_eaddr(struct usbnet *un)
984 {
985 	return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
986 		un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
987 		un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
988 }
989 
990 /*
991  * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
992  * 'usbnet'.  The first is enough to enable device access (eg, endpoints
993  * are connected and commands can be sent), and the second connects the
994  * device to the system networking.
995  *
996  * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
997  * Also usable as driver detach directly.
998  *
999  * To skip ethernet configuration (eg, point-to-point), make sure that
1000  * the un_eaddr[] is fully zero.
1001  */
1002 void
1003 usbnet_attach(struct usbnet *un,
1004 	      const char *detname,	/* detach cv name */
1005 	      unsigned rx_list_cnt,	/* size of rx chain list */
1006 	      unsigned tx_list_cnt)	/* size of tx chain list */
1007 {
1008 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
1009 
1010 	KASSERT(un->un_tx_prepare_cb);
1011 	KASSERT(un->un_rx_loop_cb);
1012 	KASSERT(un->un_init_cb);
1013 	KASSERT(un->un_cdata.uncd_rx_bufsz);
1014 	KASSERT(un->un_cdata.uncd_tx_bufsz);
1015 	KASSERT(rx_list_cnt);
1016 	KASSERT(tx_list_cnt);
1017 
1018 	ether_set_ifflags_cb(&un->un_ec, usbnet_ifflags_cb);
1019 
1020 	usb_init_task(&un->un_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE);
1021 	callout_init(&un->un_stat_ch, CALLOUT_MPSAFE);
1022 	callout_setfunc(&un->un_stat_ch, usbnet_tick, un);
1023 
1024 	mutex_init(&un->un_miilock, MUTEX_DEFAULT, IPL_NONE);
1025 	mutex_init(&un->un_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1026 	mutex_init(&un->un_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1027 	mutex_init(&un->un_lock, MUTEX_DEFAULT, IPL_NONE);
1028 	cv_init(&un->un_detachcv, detname);
1029 
1030 	rnd_attach_source(&un->un_rndsrc, device_xname(un->un_dev),
1031 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
1032 
1033 	usbnet_rx_list_alloc(un, rx_list_cnt);
1034 	usbnet_tx_list_alloc(un, tx_list_cnt);
1035 
1036 	un->un_attached = true;
1037 }
1038 
1039 static void
1040 usbnet_attach_mii(struct usbnet *un, int mii_flags)
1041 {
1042 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
1043 	struct mii_data * const mii = &un->un_mii;
1044 	struct ifnet *ifp = usbnet_ifp(un);
1045 
1046 	mii->mii_ifp = ifp;
1047 	mii->mii_readreg = usbnet_miibus_readreg;
1048 	mii->mii_writereg = usbnet_miibus_writereg;
1049 	mii->mii_statchg = usbnet_miibus_statchg;
1050 	mii->mii_flags = MIIF_AUTOTSLEEP;
1051 
1052 	un->un_ec.ec_mii = mii;
1053 	ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus);
1054 	mii_attach(un->un_dev, mii, 0xffffffff, MII_PHY_ANY,
1055 		   MII_OFFSET_ANY, mii_flags);
1056 
1057 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
1058 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1059 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1060 	} else
1061 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1062 }
1063 
1064 void
1065 usbnet_attach_ifp(struct usbnet *un,
1066 		  bool have_mii,		/* setup MII */
1067 		  unsigned if_flags,		/* additional if_flags */
1068 		  unsigned if_extflags,		/* additional if_extflags */
1069 		  int mii_flags)		/* additional mii_attach flags */
1070 {
1071 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
1072 	struct ifnet *ifp = usbnet_ifp(un);
1073 
1074 	KASSERT(un->un_attached);
1075 
1076 	IFQ_SET_READY(&ifp->if_snd);
1077 
1078 	ifp->if_softc = un;
1079 	strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
1080 	ifp->if_flags = if_flags;
1081 	ifp->if_extflags = IFEF_MPSAFE | if_extflags;
1082 	ifp->if_ioctl = usbnet_ioctl;
1083 	ifp->if_start = usbnet_start;
1084 	ifp->if_init = usbnet_init;
1085 	ifp->if_stop = usbnet_stop_ifp;
1086 
1087 	IFQ_SET_READY(&ifp->if_snd);
1088 
1089 	if (have_mii)
1090 		usbnet_attach_mii(un, mii_flags);
1091 	else
1092 		un->un_link = true;
1093 
1094 	/* Attach the interface. */
1095 	if_attach(ifp);
1096 
1097 	/*
1098 	 * If ethernet address is all zero, skip ether_ifattach() and
1099 	 * instead attach bpf here..
1100 	 */
1101 	if (!usbnet_empty_eaddr(un)) {
1102 		aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
1103 		    ether_sprintf(un->un_eaddr));
1104 		ether_ifattach(ifp, un->un_eaddr);
1105 	} else {
1106 		if_alloc_sadl(ifp);
1107 		bpf_attach(ifp, DLT_RAW, 0);
1108 	}
1109 
1110 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
1111 
1112 	if (!pmf_device_register(un->un_dev, NULL, NULL))
1113 		aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
1114 }
1115 
1116 int
1117 usbnet_detach(device_t self, int flags)
1118 {
1119 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
1120 	struct usbnet * const un = device_private(self);
1121 	struct ifnet *ifp = usbnet_ifp(un);
1122 	struct mii_data *mii = usbnet_mii(un);
1123 
1124 	mutex_enter(&un->un_lock);
1125 	un->un_dying = true;
1126 	mutex_exit(&un->un_lock);
1127 
1128 	/* Detached before attached finished, so just bail out. */
1129 	if (!un->un_attached)
1130 		return 0;
1131 
1132 	callout_halt(&un->un_stat_ch, NULL);
1133 	usb_rem_task_wait(un->un_udev, &un->un_ticktask, USB_TASKQ_DRIVER, NULL);
1134 
1135 	if (ifp->if_flags & IFF_RUNNING) {
1136 		IFNET_LOCK(ifp);
1137 		usbnet_stop_ifp(ifp, 1);
1138 		IFNET_UNLOCK(ifp);
1139 	}
1140 
1141 	mutex_enter(&un->un_lock);
1142 	un->un_refcnt--;
1143 	while (un->un_refcnt > 0) {
1144 		/* Wait for processes to go away */
1145 		cv_wait(&un->un_detachcv, &un->un_lock);
1146 	}
1147 	mutex_exit(&un->un_lock);
1148 
1149 	usbnet_rx_list_free(un);
1150 	usbnet_tx_list_free(un);
1151 
1152 	callout_destroy(&un->un_stat_ch);
1153 	rnd_detach_source(&un->un_rndsrc);
1154 
1155 	if (mii) {
1156 		mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
1157 		ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY);
1158 	}
1159 	if (ifp->if_softc) {
1160 		if (!usbnet_empty_eaddr(un))
1161 			ether_ifdetach(ifp);
1162 		else
1163 			bpf_detach(ifp);
1164 		if_detach(ifp);
1165 	}
1166 
1167 	cv_destroy(&un->un_detachcv);
1168 	mutex_destroy(&un->un_lock);
1169 	mutex_destroy(&un->un_rxlock);
1170 	mutex_destroy(&un->un_txlock);
1171 	mutex_destroy(&un->un_miilock);
1172 
1173 	pmf_device_deregister(un->un_dev);
1174 
1175 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
1176 
1177 	return 0;
1178 }
1179 
1180 int
1181 usbnet_activate(device_t self, devact_t act)
1182 {
1183 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
1184 	struct usbnet * const un = device_private(self);
1185 	struct ifnet * const ifp = usbnet_ifp(un);
1186 
1187 	switch (act) {
1188 	case DVACT_DEACTIVATE:
1189 		if_deactivate(ifp);
1190 
1191 		mutex_enter(&un->un_lock);
1192 		un->un_dying = true;
1193 		mutex_exit(&un->un_lock);
1194 
1195 		mutex_enter(&un->un_rxlock);
1196 		mutex_enter(&un->un_txlock);
1197 		un->un_stopping = true;
1198 		mutex_exit(&un->un_txlock);
1199 		mutex_exit(&un->un_rxlock);
1200 
1201 		return 0;
1202 	default:
1203 		return EOPNOTSUPP;
1204 	}
1205 }
1206 
1207 MODULE(MODULE_CLASS_MISC, usbnet, NULL);
1208 
1209 static int
1210 usbnet_modcmd(modcmd_t cmd, void *arg)
1211 {
1212 	switch (cmd) {
1213 	case MODULE_CMD_INIT:
1214 		return 0;
1215 	case MODULE_CMD_FINI:
1216 		return 0;
1217 	case MODULE_CMD_STAT:
1218 	case MODULE_CMD_AUTOUNLOAD:
1219 	default:
1220 		return ENOTTY;
1221 	}
1222 }
1223