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