xref: /freebsd-13-stable/sys/dev/usb/net/usb_ethernet.c (revision f500e5c6c99bd4520daa4524113462e3cf68f032)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/condvar.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/sysctl.h>
42 #include <sys/sx.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/ethernet.h>
47 #include <net/if_types.h>
48 #include <net/if_media.h>
49 #include <net/if_vlan_var.h>
50 
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/net/usb_ethernet.h>
59 
60 static SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
61     "USB Ethernet parameters");
62 
63 #define	UE_LOCK(_ue)		mtx_lock((_ue)->ue_mtx)
64 #define	UE_UNLOCK(_ue)		mtx_unlock((_ue)->ue_mtx)
65 #define	UE_LOCK_ASSERT(_ue, t)	mtx_assert((_ue)->ue_mtx, t)
66 
67 MODULE_DEPEND(uether, usb, 1, 1, 1);
68 MODULE_DEPEND(uether, miibus, 1, 1, 1);
69 
70 static struct unrhdr *ueunit;
71 
72 static usb_proc_callback_t ue_attach_post_task;
73 static usb_proc_callback_t ue_promisc_task;
74 static usb_proc_callback_t ue_setmulti_task;
75 static usb_proc_callback_t ue_ifmedia_task;
76 static usb_proc_callback_t ue_tick_task;
77 static usb_proc_callback_t ue_start_task;
78 static usb_proc_callback_t ue_stop_task;
79 
80 static void	ue_init(void *);
81 static void	ue_start(struct ifnet *);
82 static int	ue_ifmedia_upd(struct ifnet *);
83 static void	ue_watchdog(void *);
84 
85 /*
86  * Return values:
87  *    0: success
88  * Else: device has been detached
89  */
90 uint8_t
uether_pause(struct usb_ether * ue,unsigned int _ticks)91 uether_pause(struct usb_ether *ue, unsigned int _ticks)
92 {
93 	if (usb_proc_is_gone(&ue->ue_tq)) {
94 		/* nothing to do */
95 		return (1);
96 	}
97 	usb_pause_mtx(ue->ue_mtx, _ticks);
98 	return (0);
99 }
100 
101 static void
ue_queue_command(struct usb_ether * ue,usb_proc_callback_t * fn,struct usb_proc_msg * t0,struct usb_proc_msg * t1)102 ue_queue_command(struct usb_ether *ue,
103     usb_proc_callback_t *fn,
104     struct usb_proc_msg *t0, struct usb_proc_msg *t1)
105 {
106 	struct usb_ether_cfg_task *task;
107 
108 	UE_LOCK_ASSERT(ue, MA_OWNED);
109 
110 	if (usb_proc_is_gone(&ue->ue_tq)) {
111 		return;         /* nothing to do */
112 	}
113 	/*
114 	 * NOTE: The task cannot get executed before we drop the
115 	 * "sc_mtx" mutex. It is safe to update fields in the message
116 	 * structure after that the message got queued.
117 	 */
118 	task = (struct usb_ether_cfg_task *)
119 	  usb_proc_msignal(&ue->ue_tq, t0, t1);
120 
121 	/* Setup callback and self pointers */
122 	task->hdr.pm_callback = fn;
123 	task->ue = ue;
124 
125 	/*
126 	 * Start and stop must be synchronous!
127 	 */
128 	if ((fn == ue_start_task) || (fn == ue_stop_task))
129 		usb_proc_mwait(&ue->ue_tq, t0, t1);
130 }
131 
132 struct ifnet *
uether_getifp(struct usb_ether * ue)133 uether_getifp(struct usb_ether *ue)
134 {
135 	return (ue->ue_ifp);
136 }
137 
138 struct mii_data *
uether_getmii(struct usb_ether * ue)139 uether_getmii(struct usb_ether *ue)
140 {
141 	return (device_get_softc(ue->ue_miibus));
142 }
143 
144 void *
uether_getsc(struct usb_ether * ue)145 uether_getsc(struct usb_ether *ue)
146 {
147 	return (ue->ue_sc);
148 }
149 
150 static int
ue_sysctl_parent(SYSCTL_HANDLER_ARGS)151 ue_sysctl_parent(SYSCTL_HANDLER_ARGS)
152 {
153 	struct usb_ether *ue = arg1;
154 	const char *name;
155 
156 	name = device_get_nameunit(ue->ue_dev);
157 	return SYSCTL_OUT_STR(req, name);
158 }
159 
160 int
uether_ifattach(struct usb_ether * ue)161 uether_ifattach(struct usb_ether *ue)
162 {
163 	int error;
164 
165 	/* check some critical parameters */
166 	if ((ue->ue_dev == NULL) ||
167 	    (ue->ue_udev == NULL) ||
168 	    (ue->ue_mtx == NULL) ||
169 	    (ue->ue_methods == NULL))
170 		return (EINVAL);
171 
172 	error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
173 	    device_get_nameunit(ue->ue_dev), USB_PRI_MED);
174 	if (error) {
175 		device_printf(ue->ue_dev, "could not setup taskqueue\n");
176 		goto error;
177 	}
178 
179 	/* fork rest of the attach code */
180 	UE_LOCK(ue);
181 	ue_queue_command(ue, ue_attach_post_task,
182 	    &ue->ue_sync_task[0].hdr,
183 	    &ue->ue_sync_task[1].hdr);
184 	UE_UNLOCK(ue);
185 
186 error:
187 	return (error);
188 }
189 
190 void
uether_ifattach_wait(struct usb_ether * ue)191 uether_ifattach_wait(struct usb_ether *ue)
192 {
193 
194 	UE_LOCK(ue);
195 	usb_proc_mwait(&ue->ue_tq,
196 	    &ue->ue_sync_task[0].hdr,
197 	    &ue->ue_sync_task[1].hdr);
198 	UE_UNLOCK(ue);
199 }
200 
201 static void
ue_attach_post_task(struct usb_proc_msg * _task)202 ue_attach_post_task(struct usb_proc_msg *_task)
203 {
204 	struct usb_ether_cfg_task *task =
205 	    (struct usb_ether_cfg_task *)_task;
206 	struct usb_ether *ue = task->ue;
207 	struct ifnet *ifp;
208 	int error;
209 	char num[14];			/* sufficient for 32 bits */
210 
211 	/* first call driver's post attach routine */
212 	ue->ue_methods->ue_attach_post(ue);
213 
214 	UE_UNLOCK(ue);
215 
216 	ue->ue_unit = alloc_unr(ueunit);
217 	usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
218 	sysctl_ctx_init(&ue->ue_sysctl_ctx);
219 	mbufq_init(&ue->ue_rxq, 0 /* unlimited length */);
220 
221 	error = 0;
222 	CURVNET_SET_QUIET(vnet0);
223 	ifp = if_alloc(IFT_ETHER);
224 	ifp->if_softc = ue;
225 	if_initname(ifp, "ue", ue->ue_unit);
226 	if (ue->ue_methods->ue_attach_post_sub != NULL) {
227 		ue->ue_ifp = ifp;
228 		error = ue->ue_methods->ue_attach_post_sub(ue);
229 	} else {
230 		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
231 		if (ue->ue_methods->ue_ioctl != NULL)
232 			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
233 		else
234 			ifp->if_ioctl = uether_ioctl;
235 		ifp->if_start = ue_start;
236 		ifp->if_init = ue_init;
237 		IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
238 		ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
239 		IFQ_SET_READY(&ifp->if_snd);
240 		ue->ue_ifp = ifp;
241 
242 		if (ue->ue_methods->ue_mii_upd != NULL &&
243 		    ue->ue_methods->ue_mii_sts != NULL) {
244 			bus_topo_lock();
245 			error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
246 			    ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
247 			    BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
248 			bus_topo_unlock();
249 		}
250 	}
251 
252 	if (error) {
253 		device_printf(ue->ue_dev, "attaching PHYs failed\n");
254 		goto fail;
255 	}
256 
257 	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
258 	ether_ifattach(ifp, ue->ue_eaddr);
259 	/* Tell upper layer we support VLAN oversized frames. */
260 	if (ifp->if_capabilities & IFCAP_VLAN_MTU)
261 		ifp->if_hdrlen = sizeof(struct ether_vlan_header);
262 
263 	CURVNET_RESTORE();
264 
265 	snprintf(num, sizeof(num), "%u", ue->ue_unit);
266 	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
267 	    &SYSCTL_NODE_CHILDREN(_net, ue),
268 	    OID_AUTO, num, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
269 	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
270 	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO, "%parent",
271 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ue, 0,
272 	    ue_sysctl_parent, "A", "parent device");
273 
274 	UE_LOCK(ue);
275 	return;
276 
277 fail:
278 	CURVNET_RESTORE();
279 
280 	/* drain mbuf queue */
281 	mbufq_drain(&ue->ue_rxq);
282 
283 	/* free unit */
284 	free_unr(ueunit, ue->ue_unit);
285 	if (ue->ue_ifp != NULL) {
286 		if_free(ue->ue_ifp);
287 		ue->ue_ifp = NULL;
288 	}
289 	UE_LOCK(ue);
290 	return;
291 }
292 
293 void
uether_ifdetach(struct usb_ether * ue)294 uether_ifdetach(struct usb_ether *ue)
295 {
296 	struct ifnet *ifp;
297 
298 	/* wait for any post attach or other command to complete */
299 	usb_proc_drain(&ue->ue_tq);
300 
301 	/* read "ifnet" pointer after taskqueue drain */
302 	ifp = ue->ue_ifp;
303 
304 	if (ifp != NULL) {
305 		/* we are not running any more */
306 		UE_LOCK(ue);
307 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
308 		UE_UNLOCK(ue);
309 
310 		/* drain any callouts */
311 		usb_callout_drain(&ue->ue_watchdog);
312 
313 		/*
314 		 * Detach ethernet first to stop miibus calls from
315 		 * user-space:
316 		 */
317 		ether_ifdetach(ifp);
318 
319 		/* detach miibus */
320 		if (ue->ue_miibus != NULL) {
321 			bus_topo_lock();
322 			device_delete_child(ue->ue_dev, ue->ue_miibus);
323 			bus_topo_unlock();
324 		}
325 
326 		/* free interface instance */
327 		if_free(ifp);
328 
329 		/* free sysctl */
330 		sysctl_ctx_free(&ue->ue_sysctl_ctx);
331 
332 		/* drain mbuf queue */
333 		mbufq_drain(&ue->ue_rxq);
334 
335 		/* free unit */
336 		free_unr(ueunit, ue->ue_unit);
337 	}
338 
339 	/* free taskqueue, if any */
340 	usb_proc_free(&ue->ue_tq);
341 }
342 
343 uint8_t
uether_is_gone(struct usb_ether * ue)344 uether_is_gone(struct usb_ether *ue)
345 {
346 	return (usb_proc_is_gone(&ue->ue_tq));
347 }
348 
349 void
uether_init(void * arg)350 uether_init(void *arg)
351 {
352 
353 	ue_init(arg);
354 }
355 
356 static void
ue_init(void * arg)357 ue_init(void *arg)
358 {
359 	struct usb_ether *ue = arg;
360 
361 	UE_LOCK(ue);
362 	ue_queue_command(ue, ue_start_task,
363 	    &ue->ue_sync_task[0].hdr,
364 	    &ue->ue_sync_task[1].hdr);
365 	UE_UNLOCK(ue);
366 }
367 
368 static void
ue_start_task(struct usb_proc_msg * _task)369 ue_start_task(struct usb_proc_msg *_task)
370 {
371 	struct usb_ether_cfg_task *task =
372 	    (struct usb_ether_cfg_task *)_task;
373 	struct usb_ether *ue = task->ue;
374 	struct ifnet *ifp = ue->ue_ifp;
375 
376 	UE_LOCK_ASSERT(ue, MA_OWNED);
377 
378 	ue->ue_methods->ue_init(ue);
379 
380 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
381 		return;
382 
383 	if (ue->ue_methods->ue_tick != NULL)
384 		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
385 }
386 
387 static void
ue_stop_task(struct usb_proc_msg * _task)388 ue_stop_task(struct usb_proc_msg *_task)
389 {
390 	struct usb_ether_cfg_task *task =
391 	    (struct usb_ether_cfg_task *)_task;
392 	struct usb_ether *ue = task->ue;
393 
394 	UE_LOCK_ASSERT(ue, MA_OWNED);
395 
396 	usb_callout_stop(&ue->ue_watchdog);
397 
398 	ue->ue_methods->ue_stop(ue);
399 }
400 
401 void
uether_start(struct ifnet * ifp)402 uether_start(struct ifnet *ifp)
403 {
404 
405 	ue_start(ifp);
406 }
407 
408 static void
ue_start(struct ifnet * ifp)409 ue_start(struct ifnet *ifp)
410 {
411 	struct usb_ether *ue = ifp->if_softc;
412 
413 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
414 		return;
415 
416 	UE_LOCK(ue);
417 	ue->ue_methods->ue_start(ue);
418 	UE_UNLOCK(ue);
419 }
420 
421 static void
ue_promisc_task(struct usb_proc_msg * _task)422 ue_promisc_task(struct usb_proc_msg *_task)
423 {
424 	struct usb_ether_cfg_task *task =
425 	    (struct usb_ether_cfg_task *)_task;
426 	struct usb_ether *ue = task->ue;
427 
428 	ue->ue_methods->ue_setpromisc(ue);
429 }
430 
431 static void
ue_setmulti_task(struct usb_proc_msg * _task)432 ue_setmulti_task(struct usb_proc_msg *_task)
433 {
434 	struct usb_ether_cfg_task *task =
435 	    (struct usb_ether_cfg_task *)_task;
436 	struct usb_ether *ue = task->ue;
437 
438 	ue->ue_methods->ue_setmulti(ue);
439 }
440 
441 int
uether_ifmedia_upd(struct ifnet * ifp)442 uether_ifmedia_upd(struct ifnet *ifp)
443 {
444 
445 	return (ue_ifmedia_upd(ifp));
446 }
447 
448 static int
ue_ifmedia_upd(struct ifnet * ifp)449 ue_ifmedia_upd(struct ifnet *ifp)
450 {
451 	struct usb_ether *ue = ifp->if_softc;
452 
453 	/* Defer to process context */
454 	UE_LOCK(ue);
455 	ue_queue_command(ue, ue_ifmedia_task,
456 	    &ue->ue_media_task[0].hdr,
457 	    &ue->ue_media_task[1].hdr);
458 	UE_UNLOCK(ue);
459 
460 	return (0);
461 }
462 
463 static void
ue_ifmedia_task(struct usb_proc_msg * _task)464 ue_ifmedia_task(struct usb_proc_msg *_task)
465 {
466 	struct usb_ether_cfg_task *task =
467 	    (struct usb_ether_cfg_task *)_task;
468 	struct usb_ether *ue = task->ue;
469 	struct ifnet *ifp = ue->ue_ifp;
470 
471 	ue->ue_methods->ue_mii_upd(ifp);
472 }
473 
474 static void
ue_watchdog(void * arg)475 ue_watchdog(void *arg)
476 {
477 	struct usb_ether *ue = arg;
478 	struct ifnet *ifp = ue->ue_ifp;
479 
480 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
481 		return;
482 
483 	ue_queue_command(ue, ue_tick_task,
484 	    &ue->ue_tick_task[0].hdr,
485 	    &ue->ue_tick_task[1].hdr);
486 
487 	usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
488 }
489 
490 static void
ue_tick_task(struct usb_proc_msg * _task)491 ue_tick_task(struct usb_proc_msg *_task)
492 {
493 	struct usb_ether_cfg_task *task =
494 	    (struct usb_ether_cfg_task *)_task;
495 	struct usb_ether *ue = task->ue;
496 	struct ifnet *ifp = ue->ue_ifp;
497 
498 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
499 		return;
500 
501 	ue->ue_methods->ue_tick(ue);
502 }
503 
504 int
uether_ioctl(struct ifnet * ifp,u_long command,caddr_t data)505 uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
506 {
507 	struct usb_ether *ue = ifp->if_softc;
508 	struct ifreq *ifr = (struct ifreq *)data;
509 	struct mii_data *mii;
510 	int error = 0;
511 
512 	switch (command) {
513 	case SIOCSIFFLAGS:
514 		UE_LOCK(ue);
515 		if (ifp->if_flags & IFF_UP) {
516 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
517 				ue_queue_command(ue, ue_promisc_task,
518 				    &ue->ue_promisc_task[0].hdr,
519 				    &ue->ue_promisc_task[1].hdr);
520 			else
521 				ue_queue_command(ue, ue_start_task,
522 				    &ue->ue_sync_task[0].hdr,
523 				    &ue->ue_sync_task[1].hdr);
524 		} else {
525 			ue_queue_command(ue, ue_stop_task,
526 			    &ue->ue_sync_task[0].hdr,
527 			    &ue->ue_sync_task[1].hdr);
528 		}
529 		UE_UNLOCK(ue);
530 		break;
531 	case SIOCADDMULTI:
532 	case SIOCDELMULTI:
533 		UE_LOCK(ue);
534 		ue_queue_command(ue, ue_setmulti_task,
535 		    &ue->ue_multi_task[0].hdr,
536 		    &ue->ue_multi_task[1].hdr);
537 		UE_UNLOCK(ue);
538 		break;
539 	case SIOCGIFMEDIA:
540 	case SIOCSIFMEDIA:
541 		if (ue->ue_miibus != NULL) {
542 			mii = device_get_softc(ue->ue_miibus);
543 			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
544 		} else
545 			error = ether_ioctl(ifp, command, data);
546 		break;
547 	default:
548 		error = ether_ioctl(ifp, command, data);
549 		break;
550 	}
551 	return (error);
552 }
553 
554 static int
uether_modevent(module_t mod,int type,void * data)555 uether_modevent(module_t mod, int type, void *data)
556 {
557 
558 	switch (type) {
559 	case MOD_LOAD:
560 		ueunit = new_unrhdr(0, INT_MAX, NULL);
561 		break;
562 	case MOD_UNLOAD:
563 		break;
564 	default:
565 		return (EOPNOTSUPP);
566 	}
567 	return (0);
568 }
569 static moduledata_t uether_mod = {
570 	"uether",
571 	uether_modevent,
572 	0
573 };
574 
575 struct mbuf *
uether_newbuf(void)576 uether_newbuf(void)
577 {
578 	struct mbuf *m_new;
579 
580 	m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
581 	if (m_new == NULL)
582 		return (NULL);
583 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
584 
585 	m_adj(m_new, ETHER_ALIGN);
586 	return (m_new);
587 }
588 
589 int
uether_rxmbuf(struct usb_ether * ue,struct mbuf * m,unsigned int len)590 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
591     unsigned int len)
592 {
593 	struct ifnet *ifp = ue->ue_ifp;
594 
595 	UE_LOCK_ASSERT(ue, MA_OWNED);
596 
597 	/* finalize mbuf */
598 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
599 	m->m_pkthdr.rcvif = ifp;
600 	m->m_pkthdr.len = m->m_len = len;
601 
602 	/* enqueue for later when the lock can be released */
603 	(void)mbufq_enqueue(&ue->ue_rxq, m);
604 	return (0);
605 }
606 
607 int
uether_rxbuf(struct usb_ether * ue,struct usb_page_cache * pc,unsigned int offset,unsigned int len)608 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
609     unsigned int offset, unsigned int len)
610 {
611 	struct ifnet *ifp = ue->ue_ifp;
612 	struct mbuf *m;
613 
614 	UE_LOCK_ASSERT(ue, MA_OWNED);
615 
616 	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
617 		return (1);
618 
619 	m = uether_newbuf();
620 	if (m == NULL) {
621 		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
622 		return (ENOMEM);
623 	}
624 
625 	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
626 
627 	/* finalize mbuf */
628 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
629 	m->m_pkthdr.rcvif = ifp;
630 	m->m_pkthdr.len = m->m_len = len;
631 
632 	/* enqueue for later when the lock can be released */
633 	(void)mbufq_enqueue(&ue->ue_rxq, m);
634 	return (0);
635 }
636 
637 void
uether_rxflush(struct usb_ether * ue)638 uether_rxflush(struct usb_ether *ue)
639 {
640 	struct ifnet *ifp = ue->ue_ifp;
641 	struct epoch_tracker et;
642 	struct mbuf *m, *n;
643 
644 	UE_LOCK_ASSERT(ue, MA_OWNED);
645 
646 	n = mbufq_flush(&ue->ue_rxq);
647 	UE_UNLOCK(ue);
648 	NET_EPOCH_ENTER(et);
649 	while ((m = n) != NULL) {
650 		n = STAILQ_NEXT(m, m_stailqpkt);
651 		m->m_nextpkt = NULL;
652 		ifp->if_input(ifp, m);
653 	}
654 	NET_EPOCH_EXIT(et);
655 	UE_LOCK(ue);
656 }
657 
658 /*
659  * USB net drivers are run by DRIVER_MODULE() thus SI_SUB_DRIVERS,
660  * SI_ORDER_MIDDLE.  Run uether after that.
661  */
662 DECLARE_MODULE(uether, uether_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
663 MODULE_VERSION(uether, 1);
664