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