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