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