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