Home | History | Annotate | Line # | Download | only in usb
usbnet.c revision 1.16
      1 /*	$NetBSD: usbnet.c,v 1.16 2019/08/16 08:38:21 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.16 2019/08/16 08:38:21 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 
    278 	m = usbnet_newbuf(buflen);
    279 	if (m == NULL) {
    280 		ifp->if_ierrors++;
    281 		return;
    282 	}
    283 
    284 	m_set_rcvif(m, ifp);
    285 	memcpy(mtod(m, char *), buf, buflen);
    286 
    287 	/* push the packet up */
    288 	if_input(ifp, m);
    289 }
    290 
    291 /*
    292  * A frame has been uploaded: pass the resulting mbuf chain up to
    293  * the higher level protocols.
    294  */
    295 static void
    296 usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    297 {
    298 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    299 	struct usbnet_chain * const c = priv;
    300 	struct usbnet * const un = c->unc_un;
    301 	struct usbnet_private * const unp = un->un_pri;
    302 	struct ifnet * const ifp = usbnet_ifp(un);
    303 	uint32_t total_len;
    304 
    305 	mutex_enter(&unp->unp_rxlock);
    306 
    307 	if (unp->unp_dying || unp->unp_stopping ||
    308 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
    309 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
    310 		goto out;
    311 
    312 	if (status != USBD_NORMAL_COMPLETION) {
    313 		if (usbd_ratecheck(&unp->unp_rx_notice))
    314 			aprint_error_dev(un->un_dev, "usb errors on rx: %s\n",
    315 			    usbd_errstr(status));
    316 		if (status == USBD_STALLED)
    317 			usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]);
    318 		goto done;
    319 	}
    320 
    321 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    322 
    323 	if (total_len > un->un_rx_bufsz) {
    324 		aprint_error_dev(un->un_dev,
    325 		    "rxeof: too large transfer (%u > %u)\n",
    326 		    total_len, un->un_rx_bufsz);
    327 		goto done;
    328 	}
    329 
    330 	uno_rx_loop(un, c, total_len);
    331 	usbnet_isowned_rx(un);
    332 
    333 done:
    334 	if (unp->unp_dying || unp->unp_stopping)
    335 		goto out;
    336 
    337 	mutex_exit(&unp->unp_rxlock);
    338 
    339 	/* Setup new transfer. */
    340 	usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz,
    341 	    un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
    342 	usbd_transfer(xfer);
    343 	return;
    344 
    345 out:
    346 	mutex_exit(&unp->unp_rxlock);
    347 }
    348 
    349 static void
    350 usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    351 {
    352 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    353 	struct usbnet_chain * const c = priv;
    354 	struct usbnet * const un = c->unc_un;
    355 	struct usbnet_cdata * const cd = un_cdata(un);
    356 	struct usbnet_private * const unp = un->un_pri;
    357 	struct ifnet * const ifp = usbnet_ifp(un);
    358 
    359 	mutex_enter(&unp->unp_txlock);
    360 	if (unp->unp_stopping || unp->unp_dying) {
    361 		mutex_exit(&unp->unp_txlock);
    362 		return;
    363 	}
    364 
    365 	KASSERT(cd->uncd_tx_cnt > 0);
    366 	cd->uncd_tx_cnt--;
    367 
    368 	unp->unp_timer = 0;
    369 
    370 	switch (status) {
    371 	case USBD_NOT_STARTED:
    372 	case USBD_CANCELLED:
    373 		break;
    374 
    375 	case USBD_NORMAL_COMPLETION:
    376 		ifp->if_opackets++;
    377 		break;
    378 
    379 	default:
    380 
    381 		ifp->if_oerrors++;
    382 		if (usbd_ratecheck(&unp->unp_tx_notice))
    383 			aprint_error_dev(un->un_dev, "usb error on tx: %s\n",
    384 			    usbd_errstr(status));
    385 		if (status == USBD_STALLED)
    386 			usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]);
    387 		break;
    388 	}
    389 
    390 	mutex_exit(&unp->unp_txlock);
    391 
    392 	if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
    393 		(*ifp->if_start)(ifp);
    394 }
    395 
    396 static void
    397 usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    398 {
    399 	struct usbnet * const un = priv;
    400 	struct usbnet_private * const unp = un->un_pri;
    401 	struct usbnet_intr * const uni = un->un_intr;
    402 	struct ifnet * const ifp = usbnet_ifp(un);
    403 
    404 	if (uni == NULL || unp->unp_dying || unp->unp_stopping ||
    405 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
    406 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
    407 		return;
    408 
    409 	if (status != USBD_NORMAL_COMPLETION) {
    410 		if (usbd_ratecheck(&unp->unp_intr_notice)) {
    411 			aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
    412 			    usbd_errstr(status));
    413 		}
    414 		if (status == USBD_STALLED)
    415 			usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]);
    416 		return;
    417 	}
    418 
    419 	uno_intr(un, status);
    420 }
    421 
    422 static void
    423 usbnet_start_locked(struct ifnet *ifp)
    424 {
    425 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    426 	struct usbnet * const un = ifp->if_softc;
    427 	struct usbnet_cdata * const cd = un_cdata(un);
    428 	struct usbnet_private * const unp = un->un_pri;
    429 	struct mbuf *m;
    430 	unsigned length;
    431 	int idx;
    432 
    433 	usbnet_isowned_tx(un);
    434 	KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt);
    435 
    436 	if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) {
    437 		DPRINTF("start called no link (%x) or running (flags %x)",
    438 		    unp->unp_link, ifp->if_flags, 0, 0);
    439 		return;
    440 	}
    441 
    442 	idx = cd->uncd_tx_prod;
    443 	while (cd->uncd_tx_cnt < un->un_tx_list_cnt) {
    444 		IFQ_POLL(&ifp->if_snd, m);
    445 		if (m == NULL)
    446 			break;
    447 		KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz);
    448 
    449 		struct usbnet_chain *c = &cd->uncd_tx_chain[idx];
    450 
    451 		length = uno_tx_prepare(un, m, c);
    452 		if (length == 0) {
    453 			ifp->if_oerrors++;
    454 			break;
    455 		}
    456 
    457 		if (__predict_false(c->unc_xfer == NULL)) {
    458 			ifp->if_oerrors++;
    459 			break;
    460 		}
    461 
    462 		usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
    463 		    un->un_tx_xfer_flags, 10000, usbnet_txeof);
    464 
    465 		/* Transmit */
    466 		usbd_status err = usbd_transfer(c->unc_xfer);
    467 		if (err != USBD_IN_PROGRESS) {
    468 			ifp->if_oerrors++;
    469 			break;
    470 		}
    471 
    472 		IFQ_DEQUEUE(&ifp->if_snd, m);
    473 
    474 		/*
    475 		 * If there's a BPF listener, bounce a copy of this frame
    476 		 * to him.
    477 		 */
    478 		bpf_mtap(ifp, m, BPF_D_OUT);
    479 		m_freem(m);
    480 
    481 		idx = (idx + 1) % un->un_tx_list_cnt;
    482 		cd->uncd_tx_cnt++;
    483 	}
    484 	cd->uncd_tx_prod = idx;
    485 
    486 	/*
    487 	 * Set a timeout in case the chip goes out to lunch.
    488 	 */
    489 	unp->unp_timer = 5;
    490 }
    491 
    492 static void
    493 usbnet_start(struct ifnet *ifp)
    494 {
    495 	struct usbnet * const un = ifp->if_softc;
    496 	struct usbnet_private * const unp = un->un_pri;
    497 
    498 	mutex_enter(&unp->unp_txlock);
    499 	if (!unp->unp_stopping)
    500 		usbnet_start_locked(ifp);
    501 	mutex_exit(&unp->unp_txlock);
    502 }
    503 
    504 /*
    505  * Chain management.
    506  *
    507  * RX and TX are identical. Keep them that way.
    508  */
    509 
    510 /* Start of common RX functions */
    511 
    512 static size_t
    513 usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
    514 {
    515 	return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt;
    516 }
    517 
    518 static void
    519 usbnet_rx_list_alloc(struct usbnet * const un)
    520 {
    521 	struct usbnet_cdata * const cd = un_cdata(un);
    522 
    523 	cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP);
    524 }
    525 
    526 static void
    527 usbnet_rx_list_free(struct usbnet * const un)
    528 {
    529 	struct usbnet_cdata * const cd = un_cdata(un);
    530 
    531 	if (cd->uncd_rx_chain) {
    532 		kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un));
    533 		cd->uncd_rx_chain = NULL;
    534 	}
    535 }
    536 
    537 static int
    538 usbnet_rx_list_init(struct usbnet * const un)
    539 {
    540 	struct usbnet_cdata * const cd = un_cdata(un);
    541 	struct usbnet_private * const unp = un->un_pri;
    542 
    543 	for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
    544 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
    545 
    546 		c->unc_un = un;
    547 		if (c->unc_xfer == NULL) {
    548 			int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX],
    549 			    un->un_rx_bufsz, un->un_rx_xfer_flags, 0,
    550 			    &c->unc_xfer);
    551 			if (err)
    552 				return err;
    553 			c->unc_buf = usbd_get_buffer(c->unc_xfer);
    554 		}
    555 	}
    556 
    557 	return 0;
    558 }
    559 
    560 static void
    561 usbnet_rx_list_fini(struct usbnet * const un)
    562 {
    563 	struct usbnet_cdata * const cd = un_cdata(un);
    564 
    565 	for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
    566 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
    567 
    568 		if (c->unc_xfer != NULL) {
    569 			usbd_destroy_xfer(c->unc_xfer);
    570 			c->unc_xfer = NULL;
    571 			c->unc_buf = NULL;
    572 		}
    573 	}
    574 }
    575 
    576 /* End of common RX functions */
    577 
    578 static void
    579 usbnet_rx_start_pipes(struct usbnet * const un)
    580 {
    581 	struct usbnet_cdata * const cd = un_cdata(un);
    582 	struct usbnet_private * const unp = un->un_pri;
    583 
    584 	mutex_enter(&unp->unp_rxlock);
    585 	mutex_enter(&unp->unp_txlock);
    586 	unp->unp_stopping = false;
    587 
    588 	for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
    589 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
    590 
    591 		usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz,
    592 		    un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
    593 		usbd_transfer(c->unc_xfer);
    594 	}
    595 
    596 	mutex_exit(&unp->unp_txlock);
    597 	mutex_exit(&unp->unp_rxlock);
    598 }
    599 
    600 /* Start of common TX functions */
    601 
    602 static size_t
    603 usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
    604 {
    605 	return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt;
    606 }
    607 
    608 static void
    609 usbnet_tx_list_alloc(struct usbnet * const un)
    610 {
    611 	struct usbnet_cdata * const cd = un_cdata(un);
    612 
    613 	cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP);
    614 }
    615 
    616 static void
    617 usbnet_tx_list_free(struct usbnet * const un)
    618 {
    619 	struct usbnet_cdata * const cd = un_cdata(un);
    620 
    621 	if (cd->uncd_tx_chain) {
    622 		kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un));
    623 		cd->uncd_tx_chain = NULL;
    624 	}
    625 }
    626 
    627 static int
    628 usbnet_tx_list_init(struct usbnet * const un)
    629 {
    630 	struct usbnet_cdata * const cd = un_cdata(un);
    631 	struct usbnet_private * const unp = un->un_pri;
    632 
    633 	for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
    634 		struct usbnet_chain *c = &cd->uncd_tx_chain[i];
    635 
    636 		c->unc_un = un;
    637 		if (c->unc_xfer == NULL) {
    638 			int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX],
    639 			    un->un_tx_bufsz, un->un_tx_xfer_flags, 0,
    640 			    &c->unc_xfer);
    641 			if (err)
    642 				return err;
    643 			c->unc_buf = usbd_get_buffer(c->unc_xfer);
    644 		}
    645 	}
    646 
    647 	return 0;
    648 }
    649 
    650 static void
    651 usbnet_tx_list_fini(struct usbnet * const un)
    652 {
    653 	struct usbnet_cdata * const cd = un_cdata(un);
    654 
    655 	for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
    656 		struct usbnet_chain *c = &cd->uncd_tx_chain[i];
    657 
    658 		if (c->unc_xfer != NULL) {
    659 			usbd_destroy_xfer(c->unc_xfer);
    660 			c->unc_xfer = NULL;
    661 			c->unc_buf = NULL;
    662 		}
    663 	}
    664 }
    665 
    666 /* End of common TX functions */
    667 
    668 /* Endpoint pipe management. */
    669 
    670 static void
    671 usbnet_ep_close_pipes(struct usbnet * const un)
    672 {
    673 	struct usbnet_private * const unp = un->un_pri;
    674 
    675 	for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
    676 		if (unp->unp_ep[i] == NULL)
    677 			continue;
    678 		usbd_status err = usbd_close_pipe(unp->unp_ep[i]);
    679 		if (err)
    680 			aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
    681 			    usbd_errstr(err));
    682 		unp->unp_ep[i] = NULL;
    683 	}
    684 }
    685 
    686 static usbd_status
    687 usbnet_ep_open_pipes(struct usbnet * const un)
    688 {
    689 	struct usbnet_intr * const uni = un->un_intr;
    690 	struct usbnet_private * const unp = un->un_pri;
    691 
    692 	for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
    693 		usbd_status err;
    694 
    695 		if (un->un_ed[i] == 0)
    696 			continue;
    697 
    698 		if (i == USBNET_ENDPT_INTR && uni) {
    699 			err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
    700 			    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un,
    701 			    uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr,
    702 			    uni->uni_interval);
    703 		} else {
    704 			err = usbd_open_pipe(un->un_iface, un->un_ed[i],
    705 			    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]);
    706 		}
    707 		if (err) {
    708 			usbnet_ep_close_pipes(un);
    709 			return err;
    710 		}
    711 	}
    712 
    713 	return USBD_NORMAL_COMPLETION;
    714 }
    715 
    716 static usbd_status
    717 usbnet_ep_stop_pipes(struct usbnet * const un)
    718 {
    719 	struct usbnet_private * const unp = un->un_pri;
    720 
    721 	for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
    722 		if (unp->unp_ep[i] == NULL)
    723 			continue;
    724 		usbd_status err = usbd_abort_pipe(unp->unp_ep[i]);
    725 		if (err)
    726 			return err;
    727 	}
    728 
    729 	return USBD_NORMAL_COMPLETION;
    730 }
    731 
    732 int
    733 usbnet_init_rx_tx(struct usbnet * const un)
    734 {
    735 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    736 	struct usbnet_private * const unp = un->un_pri;
    737 	struct ifnet * const ifp = usbnet_ifp(un);
    738 	usbd_status err;
    739 	int error = 0;
    740 
    741 	usbnet_isowned(un);
    742 
    743 	if (unp->unp_dying) {
    744 		return EIO;
    745 	}
    746 	unp->unp_refcnt++;
    747 
    748 	/* Open RX and TX pipes. */
    749 	err = usbnet_ep_open_pipes(un);
    750 	if (err) {
    751 		aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
    752 		    usbd_errstr(err));
    753 		error = EIO;
    754 		goto out;
    755 	}
    756 
    757 	/* Init RX ring. */
    758 	if (usbnet_rx_list_init(un)) {
    759 		aprint_error_dev(un->un_dev, "rx list init failed\n");
    760 		error = ENOBUFS;
    761 		goto out;
    762 	}
    763 
    764 	/* Init TX ring. */
    765 	if (usbnet_tx_list_init(un)) {
    766 		aprint_error_dev(un->un_dev, "tx list init failed\n");
    767 		error = ENOBUFS;
    768 		goto out;
    769 	}
    770 
    771 	/* Start up the receive pipe(s). */
    772 	usbnet_rx_start_pipes(un);
    773 
    774 	/* Indicate we are up and running. */
    775 	KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
    776 	ifp->if_flags |= IFF_RUNNING;
    777 
    778 	callout_schedule(&unp->unp_stat_ch, hz);
    779 
    780 out:
    781 	if (error) {
    782 		usbnet_rx_list_fini(un);
    783 		usbnet_tx_list_fini(un);
    784 		usbnet_ep_close_pipes(un);
    785 	}
    786 	if (--unp->unp_refcnt < 0)
    787 		cv_broadcast(&unp->unp_detachcv);
    788 
    789 	usbnet_isowned(un);
    790 
    791 	return error;
    792 }
    793 
    794 /* MII management. */
    795 
    796 /*
    797  * Access functions for MII.  Take the MII lock to call access MII regs.
    798  * Two forms: usbnet (softc) lock currently held or not.
    799  */
    800 void
    801 usbnet_lock_mii(struct usbnet *un)
    802 {
    803 	struct usbnet_private * const unp = un->un_pri;
    804 
    805 	mutex_enter(&unp->unp_lock);
    806 	unp->unp_refcnt++;
    807 	mutex_exit(&unp->unp_lock);
    808 
    809 	mutex_enter(&unp->unp_miilock);
    810 }
    811 
    812 void
    813 usbnet_lock_mii_un_locked(struct usbnet *un)
    814 {
    815 	struct usbnet_private * const unp = un->un_pri;
    816 
    817 	usbnet_isowned(un);
    818 
    819 	unp->unp_refcnt++;
    820 	mutex_enter(&unp->unp_miilock);
    821 }
    822 
    823 void
    824 usbnet_unlock_mii(struct usbnet *un)
    825 {
    826 	struct usbnet_private * const unp = un->un_pri;
    827 
    828 	mutex_exit(&unp->unp_miilock);
    829 	mutex_enter(&unp->unp_lock);
    830 	if (--unp->unp_refcnt < 0)
    831 		cv_broadcast(&unp->unp_detachcv);
    832 	mutex_exit(&unp->unp_lock);
    833 }
    834 
    835 void
    836 usbnet_unlock_mii_un_locked(struct usbnet *un)
    837 {
    838 	struct usbnet_private * const unp = un->un_pri;
    839 
    840 	usbnet_isowned(un);
    841 
    842 	mutex_exit(&unp->unp_miilock);
    843 	if (--unp->unp_refcnt < 0)
    844 		cv_broadcast(&unp->unp_detachcv);
    845 }
    846 
    847 kmutex_t *
    848 usbnet_mutex_mii(struct usbnet *un)
    849 {
    850 	struct usbnet_private * const unp = un->un_pri;
    851 
    852 	return &unp->unp_miilock;
    853 }
    854 
    855 int
    856 usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
    857 {
    858 	struct usbnet * const un = device_private(dev);
    859 	struct usbnet_private * const unp = un->un_pri;
    860 	usbd_status err;
    861 
    862 	mutex_enter(&unp->unp_lock);
    863 	if (unp->unp_dying || un->un_phyno != phy) {
    864 		mutex_exit(&unp->unp_lock);
    865 		return EIO;
    866 	}
    867 	mutex_exit(&unp->unp_lock);
    868 
    869 	usbnet_lock_mii(un);
    870 	err = uno_read_reg(un, phy, reg, val);
    871 	usbnet_unlock_mii(un);
    872 
    873 	if (err) {
    874 		aprint_error_dev(un->un_dev, "read PHY failed: %d\n", err);
    875 		return EIO;
    876 	}
    877 
    878 	return 0;
    879 }
    880 
    881 int
    882 usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
    883 {
    884 	struct usbnet * const un = device_private(dev);
    885 	struct usbnet_private * const unp = un->un_pri;
    886 	usbd_status err;
    887 
    888 	mutex_enter(&unp->unp_lock);
    889 	if (unp->unp_dying || un->un_phyno != phy) {
    890 		mutex_exit(&unp->unp_lock);
    891 		return EIO;
    892 	}
    893 	mutex_exit(&unp->unp_lock);
    894 
    895 	usbnet_lock_mii(un);
    896 	err = uno_write_reg(un, phy, reg, val);
    897 	usbnet_unlock_mii(un);
    898 
    899 	if (err) {
    900 		aprint_error_dev(un->un_dev, "write PHY failed: %d\n", err);
    901 		return EIO;
    902 	}
    903 
    904 	return 0;
    905 }
    906 
    907 void
    908 usbnet_mii_statchg(struct ifnet *ifp)
    909 {
    910 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    911 	struct usbnet * const un = ifp->if_softc;
    912 
    913 	uno_mii_statchg(un, ifp);
    914 }
    915 
    916 static int
    917 usbnet_media_upd(struct ifnet *ifp)
    918 {
    919 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    920 	struct usbnet * const un = ifp->if_softc;
    921 	struct usbnet_private * const unp = un->un_pri;
    922 	struct mii_data * const mii = usbnet_mii(un);
    923 
    924 	if (unp->unp_dying)
    925 		return EIO;
    926 
    927 	unp->unp_link = false;
    928 
    929 	if (mii->mii_instance) {
    930 		struct mii_softc *miisc;
    931 
    932 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    933 			mii_phy_reset(miisc);
    934 	}
    935 
    936 	return ether_mediachange(ifp);
    937 }
    938 
    939 /* ioctl */
    940 
    941 static int
    942 usbnet_ifflags_cb(struct ethercom *ec)
    943 {
    944 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    945 	struct ifnet *ifp = &ec->ec_if;
    946 	struct usbnet *un = ifp->if_softc;
    947 	struct usbnet_private * const unp = un->un_pri;
    948 	int rv = 0;
    949 
    950 	mutex_enter(&unp->unp_lock);
    951 
    952 	const int changed = ifp->if_flags ^ unp->unp_if_flags;
    953 	if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
    954 		unp->unp_if_flags = ifp->if_flags;
    955 		if ((changed & IFF_PROMISC) != 0)
    956 			rv = ENETRESET;
    957 	} else {
    958 		rv = ENETRESET;
    959 	}
    960 
    961 	mutex_exit(&unp->unp_lock);
    962 
    963 	return rv;
    964 }
    965 
    966 static int
    967 usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    968 {
    969 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    970 	struct usbnet * const un = ifp->if_softc;
    971 	int error;
    972 
    973 	if (un->un_ops->uno_override_ioctl)
    974 		return uno_override_ioctl(un, ifp, cmd, data);
    975 
    976 	error = ether_ioctl(ifp, cmd, data);
    977 	if (error == ENETRESET)
    978 		error = uno_ioctl(un, ifp, cmd, data);
    979 
    980 	return error;
    981 }
    982 
    983 /*
    984  * Generic stop network function:
    985  *	- mark as stopping
    986  *	- call DD routine to stop the device
    987  *	- turn off running, timer, statchg callout, link
    988  *	- stop transfers
    989  *	- free RX and TX resources
    990  *	- close pipes
    991  *
    992  * usbnet_stop() is exported for drivers to use, expects lock held.
    993  *
    994  * usbnet_stop_ifp() is for the if_stop handler.
    995  */
    996 void
    997 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
    998 {
    999 	struct usbnet_private * const unp = un->un_pri;
   1000 
   1001 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1002 
   1003 	usbnet_isowned(un);
   1004 
   1005 	mutex_enter(&unp->unp_rxlock);
   1006 	mutex_enter(&unp->unp_txlock);
   1007 	unp->unp_stopping = true;
   1008 	mutex_exit(&unp->unp_txlock);
   1009 	mutex_exit(&unp->unp_rxlock);
   1010 
   1011 	uno_stop(un, ifp, disable);
   1012 
   1013 	/*
   1014 	 * XXXSMP Would like to
   1015 	 *	KASSERT(IFNET_LOCKED(ifp))
   1016 	 * here but the locking order is:
   1017 	 *	ifnet -> unlock -> rxlock -> txlock
   1018 	 * and unlock is already held.
   1019 	 */
   1020 	ifp->if_flags &= ~IFF_RUNNING;
   1021 	unp->unp_timer = 0;
   1022 
   1023 	callout_stop(&unp->unp_stat_ch);
   1024 
   1025 	/* Stop transfers. */
   1026 	usbnet_ep_stop_pipes(un);
   1027 
   1028 	/* Free RX/TX resources. */
   1029 	usbnet_rx_list_fini(un);
   1030 	usbnet_tx_list_fini(un);
   1031 
   1032 	/* Close pipes. */
   1033 	usbnet_ep_close_pipes(un);
   1034 }
   1035 
   1036 static void
   1037 usbnet_stop_ifp(struct ifnet *ifp, int disable)
   1038 {
   1039 	struct usbnet * const un = ifp->if_softc;
   1040 	struct usbnet_private * const unp = un->un_pri;
   1041 
   1042 	mutex_enter(&unp->unp_lock);
   1043 	usbnet_stop(un, ifp, disable);
   1044 	mutex_exit(&unp->unp_lock);
   1045 }
   1046 
   1047 /*
   1048  * Generic tick task function.
   1049  *
   1050  * usbnet_tick() is triggered from a callout, and triggers a call to
   1051  * usbnet_tick_task() from the usb_task subsystem.
   1052  */
   1053 static void
   1054 usbnet_tick(void *arg)
   1055 {
   1056 	struct usbnet * const un = arg;
   1057 	struct usbnet_private * const unp = un->un_pri;
   1058 
   1059 	mutex_enter(&unp->unp_lock);
   1060 	if (!unp->unp_stopping && !unp->unp_dying) {
   1061 		/* Perform periodic stuff in process context */
   1062 		usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER);
   1063 	}
   1064 	mutex_exit(&unp->unp_lock);
   1065 }
   1066 
   1067 static void
   1068 usbnet_watchdog(struct ifnet *ifp)
   1069 {
   1070 	struct usbnet * const un = ifp->if_softc;
   1071 	struct usbnet_cdata * const cd = un_cdata(un);
   1072 	usbd_status stat;
   1073 
   1074 	ifp->if_oerrors++;
   1075 	aprint_error_dev(un->un_dev, "watchdog timeout\n");
   1076 
   1077 	if (cd->uncd_tx_cnt > 0) {
   1078 		/*
   1079 		 * XXX index 0
   1080 		 */
   1081 		struct usbnet_chain *c = &un_cdata(un)->uncd_tx_chain[0];
   1082 		usbd_get_xfer_status(c->unc_xfer, NULL, NULL, NULL, &stat);
   1083 		usbnet_txeof(c->unc_xfer, c, stat);
   1084 	}
   1085 
   1086 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1087 		(*ifp->if_start)(ifp);
   1088 }
   1089 
   1090 static void
   1091 usbnet_tick_task(void *arg)
   1092 {
   1093 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1094 	struct usbnet * const un = arg;
   1095 	struct usbnet_private * const unp = un->un_pri;
   1096 
   1097 	mutex_enter(&unp->unp_lock);
   1098 	if (unp->unp_stopping || unp->unp_dying) {
   1099 		mutex_exit(&unp->unp_lock);
   1100 		return;
   1101 	}
   1102 
   1103 	struct ifnet * const ifp = usbnet_ifp(un);
   1104 	struct mii_data * const mii = usbnet_mii(un);
   1105 
   1106 	unp->unp_refcnt++;
   1107 	mutex_exit(&unp->unp_lock);
   1108 
   1109 	if (ifp && unp->unp_timer != 0 && --unp->unp_timer == 0)
   1110 		usbnet_watchdog(ifp);
   1111 
   1112 	if (mii && ifp) {
   1113 		mii_tick(mii);
   1114 
   1115 		if (!unp->unp_link)
   1116 			(*mii->mii_statchg)(ifp);
   1117 	}
   1118 
   1119 	/* Call driver if requested. */
   1120 	uno_tick(un);
   1121 
   1122 	mutex_enter(&unp->unp_lock);
   1123 	if (--unp->unp_refcnt < 0)
   1124 		cv_broadcast(&unp->unp_detachcv);
   1125 	if (!unp->unp_stopping && !unp->unp_dying)
   1126 		callout_schedule(&unp->unp_stat_ch, hz);
   1127 	mutex_exit(&unp->unp_lock);
   1128 }
   1129 
   1130 static int
   1131 usbnet_init(struct ifnet *ifp)
   1132 {
   1133 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1134 	struct usbnet * const un = ifp->if_softc;
   1135 
   1136 	return uno_init(un, ifp);
   1137 }
   1138 
   1139 
   1140 /* Various accessors. */
   1141 
   1142 void
   1143 usbnet_set_link(struct usbnet *un, bool link)
   1144 {
   1145 	un->un_pri->unp_link = link;
   1146 }
   1147 
   1148 void
   1149 usbnet_set_dying(struct usbnet *un, bool link)
   1150 {
   1151 	un->un_pri->unp_dying = link;
   1152 }
   1153 
   1154 struct ifnet *
   1155 usbnet_ifp(struct usbnet *un)
   1156 {
   1157 	return &un->un_pri->unp_ec.ec_if;
   1158 }
   1159 
   1160 struct ethercom *
   1161 usbnet_ec(struct usbnet *un)
   1162 {
   1163 	return &un->un_pri->unp_ec;
   1164 }
   1165 
   1166 struct mii_data *
   1167 usbnet_mii(struct usbnet *un)
   1168 {
   1169 	return un->un_pri->unp_ec.ec_mii;
   1170 }
   1171 
   1172 krndsource_t *
   1173 usbnet_rndsrc(struct usbnet *un)
   1174 {
   1175 	return &un->un_pri->unp_rndsrc;
   1176 }
   1177 
   1178 void *
   1179 usbnet_softc(struct usbnet *un)
   1180 {
   1181 	//return un->un_pri->unp_sc;
   1182 	return un->un_sc;
   1183 }
   1184 
   1185 bool
   1186 usbnet_havelink(struct usbnet *un)
   1187 {
   1188 	return un->un_pri->unp_link;
   1189 }
   1190 
   1191 bool
   1192 usbnet_isdying(struct usbnet *un)
   1193 {
   1194 	return un->un_pri->unp_dying;
   1195 }
   1196 
   1197 
   1198 /* Locking. */
   1199 
   1200 void
   1201 usbnet_lock(struct usbnet *un)
   1202 {
   1203 	mutex_enter(&un->un_pri->unp_lock);
   1204 }
   1205 
   1206 void
   1207 usbnet_unlock(struct usbnet *un)
   1208 {
   1209 	mutex_exit(&un->un_pri->unp_lock);
   1210 }
   1211 
   1212 kmutex_t *
   1213 usbnet_mutex(struct usbnet *un)
   1214 {
   1215 	return &un->un_pri->unp_lock;
   1216 }
   1217 
   1218 void
   1219 usbnet_lock_rx(struct usbnet *un)
   1220 {
   1221 	mutex_enter(&un->un_pri->unp_rxlock);
   1222 }
   1223 
   1224 void
   1225 usbnet_unlock_rx(struct usbnet *un)
   1226 {
   1227 	mutex_exit(&un->un_pri->unp_rxlock);
   1228 }
   1229 
   1230 kmutex_t *
   1231 usbnet_mutex_rx(struct usbnet *un)
   1232 {
   1233 	return &un->un_pri->unp_rxlock;
   1234 }
   1235 
   1236 void
   1237 usbnet_lock_tx(struct usbnet *un)
   1238 {
   1239 	mutex_enter(&un->un_pri->unp_txlock);
   1240 }
   1241 
   1242 void
   1243 usbnet_unlock_tx(struct usbnet *un)
   1244 {
   1245 	mutex_exit(&un->un_pri->unp_txlock);
   1246 }
   1247 
   1248 kmutex_t *
   1249 usbnet_mutex_tx(struct usbnet *un)
   1250 {
   1251 	return &un->un_pri->unp_txlock;
   1252 }
   1253 
   1254 /* Autoconf management. */
   1255 
   1256 static bool
   1257 usbnet_empty_eaddr(struct usbnet * const un)
   1258 {
   1259 	return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
   1260 		un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
   1261 		un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
   1262 }
   1263 
   1264 /*
   1265  * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
   1266  * 'usbnet'.  The first is enough to enable device access (eg, endpoints
   1267  * are connected and commands can be sent), and the second connects the
   1268  * device to the system networking.
   1269  *
   1270  * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
   1271  * Also usable as driver detach directly.
   1272  *
   1273  * To skip ethernet configuration (eg, point-to-point), make sure that
   1274  * the un_eaddr[] is fully zero.
   1275  */
   1276 
   1277 void
   1278 usbnet_attach(struct usbnet *un,
   1279 	      const char *detname)	/* detach cv name */
   1280 {
   1281 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1282 
   1283 	/* Required inputs.  */
   1284 	KASSERT(un->un_ops->uno_tx_prepare);
   1285 	KASSERT(un->un_ops->uno_rx_loop);
   1286 	KASSERT(un->un_ops->uno_init);
   1287 	KASSERT(un->un_rx_bufsz);
   1288 	KASSERT(un->un_tx_bufsz);
   1289 	KASSERT(un->un_rx_list_cnt);
   1290 	KASSERT(un->un_tx_list_cnt);
   1291 
   1292 	/* Unfortunate fact.  */
   1293 	KASSERT(un == device_private(un->un_dev));
   1294 
   1295 	un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP);
   1296 	struct usbnet_private * const unp = un->un_pri;
   1297 
   1298 	usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE);
   1299 	callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE);
   1300 	callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un);
   1301 
   1302 	mutex_init(&unp->unp_miilock, MUTEX_DEFAULT, IPL_NONE);
   1303 	mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1304 	mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1305 	mutex_init(&unp->unp_lock, MUTEX_DEFAULT, IPL_NONE);
   1306 	cv_init(&unp->unp_detachcv, detname);
   1307 
   1308 	rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev),
   1309 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
   1310 
   1311 	usbnet_rx_list_alloc(un);
   1312 	usbnet_tx_list_alloc(un);
   1313 
   1314 	unp->unp_attached = true;
   1315 }
   1316 
   1317 static void
   1318 usbnet_attach_mii(struct usbnet *un, int mii_flags)
   1319 {
   1320 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1321 	struct usbnet_private * const unp = un->un_pri;
   1322 	struct mii_data * const mii = &unp->unp_mii;
   1323 	struct ifnet * const ifp = usbnet_ifp(un);
   1324 
   1325 	KASSERT(un->un_ops->uno_read_reg);
   1326 	KASSERT(un->un_ops->uno_write_reg);
   1327 	KASSERT(un->un_ops->uno_statchg);
   1328 
   1329 	mii->mii_ifp = ifp;
   1330 	mii->mii_readreg = usbnet_mii_readreg;
   1331 	mii->mii_writereg = usbnet_mii_writereg;
   1332 	mii->mii_statchg = usbnet_mii_statchg;
   1333 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1334 
   1335 	usbnet_ec(un)->ec_mii = mii;
   1336 	ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus);
   1337 	mii_attach(un->un_dev, mii, 0xffffffff, MII_PHY_ANY,
   1338 		   MII_OFFSET_ANY, mii_flags);
   1339 
   1340 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1341 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1342 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1343 	} else
   1344 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1345 }
   1346 
   1347 void
   1348 usbnet_attach_ifp(struct usbnet *un,
   1349 		  bool have_mii,		/* setup MII */
   1350 		  unsigned if_flags,		/* additional if_flags */
   1351 		  unsigned if_extflags,		/* additional if_extflags */
   1352 		  int mii_flags)		/* additional mii_attach flags */
   1353 {
   1354 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1355 	struct usbnet_private * const unp = un->un_pri;
   1356 	struct ifnet * const ifp = usbnet_ifp(un);
   1357 
   1358 	KASSERT(unp->unp_attached);
   1359 
   1360 	IFQ_SET_READY(&ifp->if_snd);
   1361 
   1362 	ifp->if_softc = un;
   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 	if_attach(ifp);
   1380 
   1381 	/*
   1382 	 * If ethernet address is all zero, skip ether_ifattach() and
   1383 	 * instead attach bpf here..
   1384 	 */
   1385 	if (!usbnet_empty_eaddr(un)) {
   1386 		ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb);
   1387 		aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
   1388 		    ether_sprintf(un->un_eaddr));
   1389 		ether_ifattach(ifp, un->un_eaddr);
   1390 	} else {
   1391 		if_alloc_sadl(ifp);
   1392 		bpf_attach(ifp, DLT_RAW, 0);
   1393 	}
   1394 
   1395 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
   1396 
   1397 	if (!pmf_device_register(un->un_dev, NULL, NULL))
   1398 		aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
   1399 }
   1400 
   1401 int
   1402 usbnet_detach(device_t self, int flags)
   1403 {
   1404 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1405 	struct usbnet * const un = device_private(self);
   1406 	struct ifnet * const ifp = usbnet_ifp(un);
   1407 	struct mii_data * const mii = usbnet_mii(un);
   1408 	struct usbnet_private * const unp = un->un_pri;
   1409 
   1410 	mutex_enter(&unp->unp_lock);
   1411 	unp->unp_dying = true;
   1412 	mutex_exit(&unp->unp_lock);
   1413 
   1414 	/* Detached before attached finished, so just bail out. */
   1415 	if (!unp->unp_attached)
   1416 		return 0;
   1417 
   1418 	callout_halt(&unp->unp_stat_ch, NULL);
   1419 	usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, NULL);
   1420 
   1421 	if (ifp->if_flags & IFF_RUNNING) {
   1422 		IFNET_LOCK(ifp);
   1423 		usbnet_stop_ifp(ifp, 1);
   1424 		IFNET_UNLOCK(ifp);
   1425 	}
   1426 
   1427 	mutex_enter(&unp->unp_lock);
   1428 	unp->unp_refcnt--;
   1429 	while (unp->unp_refcnt > 0) {
   1430 		/* Wait for processes to go away */
   1431 		cv_wait(&unp->unp_detachcv, &unp->unp_lock);
   1432 	}
   1433 	mutex_exit(&unp->unp_lock);
   1434 
   1435 	usbnet_rx_list_free(un);
   1436 	usbnet_tx_list_free(un);
   1437 
   1438 	callout_destroy(&unp->unp_stat_ch);
   1439 	rnd_detach_source(&unp->unp_rndsrc);
   1440 
   1441 	if (mii) {
   1442 		mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1443 		ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY);
   1444 	}
   1445 	if (ifp->if_softc) {
   1446 		if (!usbnet_empty_eaddr(un))
   1447 			ether_ifdetach(ifp);
   1448 		else
   1449 			bpf_detach(ifp);
   1450 		if_detach(ifp);
   1451 	}
   1452 
   1453 	cv_destroy(&unp->unp_detachcv);
   1454 	mutex_destroy(&unp->unp_lock);
   1455 	mutex_destroy(&unp->unp_rxlock);
   1456 	mutex_destroy(&unp->unp_txlock);
   1457 	mutex_destroy(&unp->unp_miilock);
   1458 
   1459 	pmf_device_deregister(un->un_dev);
   1460 
   1461 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
   1462 
   1463 	kmem_free(unp, sizeof(*unp));
   1464 
   1465 	return 0;
   1466 }
   1467 
   1468 int
   1469 usbnet_activate(device_t self, devact_t act)
   1470 {
   1471 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1472 	struct usbnet * const un = device_private(self);
   1473 	struct usbnet_private * const unp = un->un_pri;
   1474 	struct ifnet * const ifp = usbnet_ifp(un);
   1475 
   1476 	switch (act) {
   1477 	case DVACT_DEACTIVATE:
   1478 		if_deactivate(ifp);
   1479 
   1480 		mutex_enter(&unp->unp_lock);
   1481 		unp->unp_dying = true;
   1482 		mutex_exit(&unp->unp_lock);
   1483 
   1484 		mutex_enter(&unp->unp_rxlock);
   1485 		mutex_enter(&unp->unp_txlock);
   1486 		unp->unp_stopping = true;
   1487 		mutex_exit(&unp->unp_txlock);
   1488 		mutex_exit(&unp->unp_rxlock);
   1489 
   1490 		return 0;
   1491 	default:
   1492 		return EOPNOTSUPP;
   1493 	}
   1494 }
   1495 
   1496 MODULE(MODULE_CLASS_MISC, usbnet, NULL);
   1497 
   1498 static int
   1499 usbnet_modcmd(modcmd_t cmd, void *arg)
   1500 {
   1501 	switch (cmd) {
   1502 	case MODULE_CMD_INIT:
   1503 		return 0;
   1504 	case MODULE_CMD_FINI:
   1505 		return 0;
   1506 	case MODULE_CMD_STAT:
   1507 	case MODULE_CMD_AUTOUNLOAD:
   1508 	default:
   1509 		return ENOTTY;
   1510 	}
   1511 }
   1512