Home | History | Annotate | Line # | Download | only in usb
usbnet.c revision 1.7
      1 /*	$NetBSD: usbnet.c,v 1.7 2019/08/07 00:38:02 pgoyette 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 ethernet drivers.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.7 2019/08/07 00:38:02 pgoyette 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 static int usbnet_modcmd(modcmd_t, void *);
     47 
     48 #ifdef USB_DEBUG
     49 #ifndef USBNET_DEBUG
     50 #define usbnetdebug 0
     51 #else
     52 static int usbnetdebug = 1;
     53 
     54 int     sysctl_hw_usbnet_setup(SYSCTLFN_PROTO);
     55 
     56 SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup")
     57 {
     58 	int err;
     59 	const struct sysctlnode *rnode;
     60 	const struct sysctlnode *cnode;
     61 
     62 	err = sysctl_createv(clog, 0, NULL, &rnode,
     63 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet",
     64 	    SYSCTL_DESCR("usbnet global controls"),
     65 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     66 
     67 	if (err)
     68 		goto fail;
     69 
     70 	/* control debugging printfs */
     71 	err = sysctl_createv(clog, 0, &rnode, &cnode,
     72 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
     73 	    "debug", SYSCTL_DESCR("Enable debugging output"),
     74 	    NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL);
     75 	if (err)
     76 		goto fail;
     77 
     78 	return;
     79 fail:
     80 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
     81 }
     82 
     83 #endif /* USBNET_DEBUG */
     84 #endif /* USB_DEBUG */
     85 
     86 #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D)
     87 #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D)
     88 #define USBNETHIST_FUNC()	USBHIST_FUNC()
     89 #define USBNETHIST_CALLED(name)	USBHIST_CALLED(usbnetdebug)
     90 
     91 /* Interrupt handling. */
     92 
     93 static struct mbuf *
     94 usbnet_newbuf(void)
     95 {
     96 	struct mbuf *m;
     97 
     98 	MGETHDR(m, M_DONTWAIT, MT_DATA);
     99 	if (m == NULL)
    100 		return NULL;
    101 
    102 	MCLGET(m, M_DONTWAIT);
    103 	if (!(m->m_flags & M_EXT)) {
    104 		m_freem(m);
    105 		return NULL;
    106 	}
    107 
    108 	m->m_len = m->m_pkthdr.len = MCLBYTES;
    109 	m_adj(m, ETHER_ALIGN);
    110 
    111 	return m;
    112 }
    113 
    114 /*
    115  * usbnet_rxeof() is designed to be the done callback for rx completion.
    116  * it provides generic setup and finalisation, calls a different usbnet
    117  * rx_loop callback in the middle, which can use usbnet_enqueue() to
    118  * enqueue a packet for higher levels (or usbnet_input() if previously
    119  * using if_input() path.)
    120  */
    121 void
    122 usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen,
    123 	       int csum_flags, uint32_t csum_data, int mbuf_flags)
    124 {
    125 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    126 	struct ifnet *ifp = &un->un_ec.ec_if;
    127 	struct mbuf *m;
    128 
    129 	KASSERT(mutex_owned(&un->un_rxlock));
    130 
    131 	m = usbnet_newbuf();
    132 	if (m == NULL) {
    133 		ifp->if_ierrors++;
    134 		return;
    135 	}
    136 
    137 	m_set_rcvif(m, ifp);
    138 	m->m_pkthdr.len = m->m_len = buflen;
    139 	m->m_pkthdr.csum_flags = csum_flags;
    140 	m->m_pkthdr.csum_data = csum_data;
    141 	m->m_flags |= mbuf_flags;
    142 	memcpy(mtod(m, char *), buf, buflen);
    143 
    144 	/* push the packet up */
    145 	if_percpuq_enqueue(ifp->if_percpuq, m);
    146 }
    147 
    148 void
    149 usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen)
    150 {
    151 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    152 	struct ifnet * const ifp = usbnet_ifp(un);
    153 	struct mbuf *m;
    154 
    155 	KASSERT(mutex_owned(&un->un_rxlock));
    156 
    157 	m = usbnet_newbuf();
    158 	if (m == NULL) {
    159 		ifp->if_ierrors++;
    160 		return;
    161 	}
    162 
    163 	m_set_rcvif(m, ifp);
    164 	m->m_pkthdr.len = m->m_len = buflen;
    165 	memcpy(mtod(m, char *), buf, buflen);
    166 
    167 	/* push the packet up */
    168 	if_input(ifp, m);
    169 }
    170 
    171 /*
    172  * A frame has been uploaded: pass the resulting mbuf chain up to
    173  * the higher level protocols.
    174  */
    175 static void
    176 usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    177 {
    178 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    179 	struct usbnet_chain *c = priv;
    180 	struct usbnet * const un = c->unc_un;
    181 	struct ifnet * const ifp = usbnet_ifp(un);
    182 	uint32_t total_len;
    183 
    184 	mutex_enter(&un->un_rxlock);
    185 
    186 	if (un->un_dying || un->un_stopping ||
    187 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
    188 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
    189 		goto out;
    190 
    191 	if (status != USBD_NORMAL_COMPLETION) {
    192 		if (usbd_ratecheck(&un->un_rx_notice))
    193 			aprint_error_dev(un->un_dev, "usb errors on rx: %s\n",
    194 			    usbd_errstr(status));
    195 		if (status == USBD_STALLED)
    196 			usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_RX]);
    197 		goto done;
    198 	}
    199 
    200 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    201 
    202 	if (total_len > un->un_cdata.uncd_rx_bufsz) {
    203 		aprint_error_dev(un->un_dev,
    204 		    "rxeof: too large transfer (%u > %u)\n",
    205 		    total_len, un->un_cdata.uncd_rx_bufsz);
    206 		goto done;
    207 	}
    208 
    209 	(*un->un_rx_loop_cb)(un, xfer, c, total_len);
    210 	KASSERT(mutex_owned(&un->un_rxlock));
    211 
    212 done:
    213 	if (un->un_dying || un->un_stopping)
    214 		goto out;
    215 
    216 	mutex_exit(&un->un_rxlock);
    217 
    218 	/* Setup new transfer. */
    219 	usbd_setup_xfer(xfer, c, c->unc_buf, un->un_cdata.uncd_rx_bufsz,
    220 	    un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
    221 	usbd_transfer(xfer);
    222 	return;
    223 
    224 out:
    225 	mutex_exit(&un->un_rxlock);
    226 }
    227 
    228 static void
    229 usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    230 {
    231 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    232 	struct usbnet_chain *c = priv;
    233 	struct usbnet * const un = c->unc_un;
    234 	struct usbnet_cdata *cd = &un->un_cdata;
    235 	struct ifnet * const ifp = usbnet_ifp(un);
    236 
    237 	mutex_enter(&un->un_txlock);
    238 	if (un->un_stopping || un->un_dying) {
    239 		mutex_exit(&un->un_txlock);
    240 		return;
    241 	}
    242 
    243 	KASSERT(cd->uncd_tx_cnt > 0);
    244 	cd->uncd_tx_cnt--;
    245 
    246 	un->un_timer = 0;
    247 
    248 	switch (status) {
    249 	case USBD_NOT_STARTED:
    250 	case USBD_CANCELLED:
    251 		break;
    252 
    253 	case USBD_NORMAL_COMPLETION:
    254 		ifp->if_opackets++;
    255 		break;
    256 
    257 	default:
    258 
    259 		ifp->if_oerrors++;
    260 		if (usbd_ratecheck(&un->un_tx_notice))
    261 			aprint_error_dev(un->un_dev, "usb error on tx: %s\n",
    262 			    usbd_errstr(status));
    263 		if (status == USBD_STALLED)
    264 			usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_TX]);
    265 		break;
    266 	}
    267 
    268 	mutex_exit(&un->un_txlock);
    269 
    270 	if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
    271 		(*ifp->if_start)(ifp);
    272 }
    273 
    274 static void
    275 usbnet_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    276 {
    277 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    278 	struct usbnet		*un = priv;
    279 	struct ifnet		*ifp = usbnet_ifp(un);
    280 
    281 	if (un->un_dying || un->un_stopping ||
    282 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
    283 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
    284 		return;
    285 
    286 	if (status != USBD_NORMAL_COMPLETION) {
    287 		if (usbd_ratecheck(&un->un_intr_notice)) {
    288 			aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
    289 			    usbd_errstr(status));
    290 		}
    291 		if (status == USBD_STALLED)
    292 			usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_INTR]);
    293 		return;
    294 	}
    295 
    296 	if (un->un_intr_cb)
    297 		(*un->un_intr_cb)(un, status);
    298 }
    299 
    300 static void
    301 usbnet_start_locked(struct ifnet *ifp)
    302 {
    303 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    304 	struct usbnet * const un = ifp->if_softc;
    305 	struct usbnet_cdata *cd = &un->un_cdata;
    306 	struct mbuf *m;
    307 	unsigned length;
    308 	int idx;
    309 
    310 	KASSERT(mutex_owned(&un->un_txlock));
    311 	KASSERT(cd->uncd_tx_cnt <= cd->uncd_tx_list_cnt);
    312 
    313 	if (!un->un_link || (ifp->if_flags & IFF_RUNNING) == 0)
    314 		return;
    315 
    316 	idx = cd->uncd_tx_prod;
    317 	while (cd->uncd_tx_cnt < cd->uncd_tx_list_cnt) {
    318 		IFQ_POLL(&ifp->if_snd, m);
    319 		if (m == NULL)
    320 			break;
    321 
    322 		struct usbnet_chain *c = &un->un_cdata.uncd_tx_chain[idx];
    323 
    324 		length = (*un->un_tx_prepare_cb)(un, m, c);
    325 		if (length == 0) {
    326 			ifp->if_oerrors++;
    327 			break;
    328 		}
    329 
    330 		if (__predict_false(c->unc_xfer == NULL)) {
    331 			ifp->if_oerrors++;
    332 			break;
    333 		}
    334 
    335 		usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
    336 		    un->un_tx_xfer_flags, 10000, usbnet_txeof);
    337 
    338 		/* Transmit */
    339 		usbd_status err = usbd_transfer(c->unc_xfer);
    340 		if (err != USBD_IN_PROGRESS) {
    341 			ifp->if_oerrors++;
    342 			break;
    343 		}
    344 
    345 		IFQ_DEQUEUE(&ifp->if_snd, m);
    346 
    347 		/*
    348 		 * If there's a BPF listener, bounce a copy of this frame
    349 		 * to him.
    350 		 */
    351 		bpf_mtap(ifp, m, BPF_D_OUT);
    352 		m_freem(m);
    353 
    354 		idx = (idx + 1) % cd->uncd_tx_list_cnt;
    355 		cd->uncd_tx_cnt++;
    356 	}
    357 	cd->uncd_tx_prod = idx;
    358 
    359 	/*
    360 	 * Set a timeout in case the chip goes out to lunch.
    361 	 */
    362 	un->un_timer = 5;
    363 }
    364 
    365 static void
    366 usbnet_start(struct ifnet *ifp)
    367 {
    368 	struct usbnet * const un = ifp->if_softc;
    369 
    370 	mutex_enter(&un->un_txlock);
    371 	if (!un->un_stopping)
    372 		usbnet_start_locked(ifp);
    373 	mutex_exit(&un->un_txlock);
    374 }
    375 
    376 /*
    377  * Chain management.
    378  *
    379  * RX and TX are identical. Keep them that way.
    380  */
    381 
    382 /* Start of common RX functions */
    383 
    384 static size_t
    385 usbnet_rx_list_size(struct usbnet_cdata *cd)
    386 {
    387 	return sizeof(*cd->uncd_rx_chain) * cd->uncd_rx_list_cnt;
    388 }
    389 
    390 static void
    391 usbnet_rx_list_alloc(struct usbnet *un, unsigned cnt)
    392 {
    393 	struct usbnet_cdata *cd = &un->un_cdata;
    394 
    395 	cd->uncd_rx_list_cnt = cnt;
    396 	cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd), KM_SLEEP);
    397 }
    398 
    399 static void
    400 usbnet_rx_list_free(struct usbnet *un)
    401 {
    402 	struct usbnet_cdata *cd = &un->un_cdata;
    403 
    404 	if (cd->uncd_rx_chain) {
    405 		kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd));
    406 		cd->uncd_rx_chain = NULL;
    407 	}
    408 }
    409 
    410 static int
    411 usbnet_rx_list_init(struct usbnet *un, unsigned xfer_flags)
    412 {
    413 	struct usbnet_cdata *cd = &un->un_cdata;
    414 
    415 	for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
    416 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
    417 
    418 		c->unc_un = un;
    419 		if (c->unc_xfer == NULL) {
    420 			int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_RX],
    421 			    cd->uncd_rx_bufsz, xfer_flags, 0, &c->unc_xfer);
    422 			if (err)
    423 				return err;
    424 			c->unc_buf = usbd_get_buffer(c->unc_xfer);
    425 		}
    426 	}
    427 
    428 	return 0;
    429 }
    430 
    431 static void
    432 usbnet_rx_list_fini(struct usbnet *un)
    433 {
    434 	struct usbnet_cdata *cd = &un->un_cdata;
    435 
    436 	for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
    437 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
    438 
    439 		if (c->unc_xfer != NULL) {
    440 			usbd_destroy_xfer(c->unc_xfer);
    441 			c->unc_xfer = NULL;
    442 			c->unc_buf = NULL;
    443 		}
    444 	}
    445 }
    446 
    447 /* End of common RX functions */
    448 
    449 static void
    450 usbnet_rx_start_pipes(struct usbnet *un, usbd_callback cb)
    451 {
    452 	struct usbnet_cdata *cd = &un->un_cdata;
    453 
    454 	mutex_enter(&un->un_rxlock);
    455 	mutex_enter(&un->un_txlock);
    456 	un->un_stopping = false;
    457 
    458 	for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
    459 		struct usbnet_chain *c = &cd->uncd_rx_chain[i];
    460 
    461 		usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, cd->uncd_rx_bufsz,
    462 		    un->un_rx_xfer_flags, USBD_NO_TIMEOUT, cb);
    463 		usbd_transfer(c->unc_xfer);
    464 	}
    465 
    466 	mutex_exit(&un->un_txlock);
    467 	mutex_exit(&un->un_rxlock);
    468 }
    469 
    470 /* Start of common TX functions */
    471 
    472 static size_t
    473 usbnet_tx_list_size(struct usbnet_cdata *cd)
    474 {
    475 	return sizeof(*cd->uncd_tx_chain) * cd->uncd_tx_list_cnt;
    476 }
    477 
    478 static void
    479 usbnet_tx_list_alloc(struct usbnet *un, unsigned cnt)
    480 {
    481 	struct usbnet_cdata *cd = &un->un_cdata;
    482 
    483 	cd->uncd_tx_list_cnt = cnt;
    484 	cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd), KM_SLEEP);
    485 }
    486 
    487 static void
    488 usbnet_tx_list_free(struct usbnet *un)
    489 {
    490 	struct usbnet_cdata *cd = &un->un_cdata;
    491 
    492 	if (cd->uncd_tx_chain) {
    493 		kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd));
    494 		cd->uncd_tx_chain = NULL;
    495 	}
    496 }
    497 
    498 static int
    499 usbnet_tx_list_init(struct usbnet *un, unsigned xfer_flags)
    500 {
    501 	struct usbnet_cdata *cd = &un->un_cdata;
    502 
    503 	for (size_t i = 0; i < cd->uncd_tx_list_cnt; i++) {
    504 		struct usbnet_chain *c = &cd->uncd_tx_chain[i];
    505 
    506 		c->unc_un = un;
    507 		if (c->unc_xfer == NULL) {
    508 			int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_TX],
    509 			    cd->uncd_tx_bufsz, xfer_flags, 0, &c->unc_xfer);
    510 			if (err)
    511 				return err;
    512 			c->unc_buf = usbd_get_buffer(c->unc_xfer);
    513 		}
    514 	}
    515 
    516 	return 0;
    517 }
    518 
    519 static void
    520 usbnet_tx_list_fini(struct usbnet *un)
    521 {
    522 	struct usbnet_cdata *cd = &un->un_cdata;
    523 
    524 	for (size_t i = 0; i < cd->uncd_tx_list_cnt; i++) {
    525 		struct usbnet_chain *c = &cd->uncd_tx_chain[i];
    526 
    527 		if (c->unc_xfer != NULL) {
    528 			usbd_destroy_xfer(c->unc_xfer);
    529 			c->unc_xfer = NULL;
    530 			c->unc_buf = NULL;
    531 		}
    532 	}
    533 }
    534 
    535 /* End of common TX functions */
    536 
    537 /* Endpoint pipe management. */
    538 
    539 static void
    540 usbnet_ep_close_pipes(struct usbnet *un)
    541 {
    542 	for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
    543 		if (un->un_ep[i] == NULL)
    544 			continue;
    545 		usbd_status err = usbd_close_pipe(un->un_ep[i]);
    546 		if (err)
    547 			aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
    548 			    usbd_errstr(err));
    549 		un->un_ep[i] = NULL;
    550 	}
    551 }
    552 
    553 static usbd_status
    554 usbnet_ep_open_pipes(struct usbnet *un)
    555 {
    556 	for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
    557 		usbd_status err;
    558 
    559 		if (un->un_ed[i] == 0)
    560 			continue;
    561 
    562 		if (i == USBNET_ENDPT_INTR && un->un_intr_buf) {
    563 			err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
    564 			    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &un->un_ep[i], un,
    565 			    un->un_intr_buf, un->un_intr_bufsz, usbnet_intr,
    566 			    un->un_intr_interval);
    567 		} else {
    568 			err = usbd_open_pipe(un->un_iface, un->un_ed[i],
    569 			    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &un->un_ep[i]);
    570 		}
    571 		if (err) {
    572 			usbnet_ep_close_pipes(un);
    573 			return err;
    574 		}
    575 	}
    576 
    577 	return USBD_NORMAL_COMPLETION;
    578 }
    579 
    580 static usbd_status
    581 usbnet_ep_stop_pipes(struct usbnet *un)
    582 {
    583 	for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
    584 		if (un->un_ep[i] == NULL)
    585 			continue;
    586 		usbd_status err = usbd_abort_pipe(un->un_ep[i]);
    587 		if (err)
    588 			return err;
    589 	}
    590 
    591 	return USBD_NORMAL_COMPLETION;
    592 }
    593 
    594 int
    595 usbnet_init_rx_tx(struct usbnet * const un, unsigned rxflags, unsigned txflags)
    596 {
    597 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    598 	struct ifnet * const ifp = usbnet_ifp(un);
    599 	usbd_status err;
    600 	int error = 0;
    601 
    602 	usbnet_isowned(un);
    603 
    604 	if (un->un_dying) {
    605 		return EIO;
    606 	}
    607 	un->un_refcnt++;
    608 
    609 	/* Open RX and TX pipes. */
    610 	err = usbnet_ep_open_pipes(un);
    611 	if (err) {
    612 		aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
    613 		    usbd_errstr(err));
    614 		error = EIO;
    615 		goto out;
    616 	}
    617 
    618 	/* Init RX ring. */
    619 	if (usbnet_rx_list_init(un, rxflags)) {
    620 		aprint_error_dev(un->un_dev, "rx list init failed\n");
    621 		error = ENOBUFS;
    622 		goto out;
    623 	}
    624 
    625 	/* Init TX ring. */
    626 	if (usbnet_tx_list_init(un, txflags)) {
    627 		aprint_error_dev(un->un_dev, "tx list init failed\n");
    628 		error = ENOBUFS;
    629 		goto out;
    630 	}
    631 
    632 	/* Start up the receive pipe(s). */
    633 	usbnet_rx_start_pipes(un, usbnet_rxeof);
    634 
    635 	/* Indicate we are up and running. */
    636 	KASSERT(IFNET_LOCKED(ifp));
    637 	ifp->if_flags |= IFF_RUNNING;
    638 
    639 	callout_schedule(&un->un_stat_ch, hz);
    640 
    641 out:
    642 	if (error) {
    643 		usbnet_rx_list_fini(un);
    644 		usbnet_tx_list_fini(un);
    645 		usbnet_ep_close_pipes(un);
    646 	}
    647 	if (--un->un_refcnt < 0)
    648 		cv_broadcast(&un->un_detachcv);
    649 
    650 	usbnet_isowned(un);
    651 
    652 	return error;
    653 }
    654 
    655 /* MII management. */
    656 
    657 /*
    658  * Access functions for MII.  Take the MII lock to call access MII regs.
    659  * Two forms: usbnet (softc) lock currently held or not.
    660  */
    661 void
    662 usbnet_lock_mii(struct usbnet *un)
    663 {
    664 
    665 	mutex_enter(&un->un_lock);
    666 	un->un_refcnt++;
    667 	mutex_exit(&un->un_lock);
    668 
    669 	mutex_enter(&un->un_miilock);
    670 }
    671 
    672 void
    673 usbnet_lock_mii_un_locked(struct usbnet *un)
    674 {
    675 	KASSERT(mutex_owned(&un->un_lock));
    676 
    677 	un->un_refcnt++;
    678 	mutex_enter(&un->un_miilock);
    679 }
    680 
    681 void
    682 usbnet_unlock_mii(struct usbnet *un)
    683 {
    684 
    685 	mutex_exit(&un->un_miilock);
    686 	mutex_enter(&un->un_lock);
    687 	if (--un->un_refcnt < 0)
    688 		cv_broadcast(&un->un_detachcv);
    689 	mutex_exit(&un->un_lock);
    690 }
    691 
    692 void
    693 usbnet_unlock_mii_un_locked(struct usbnet *un)
    694 {
    695 	KASSERT(mutex_owned(&un->un_lock));
    696 
    697 	mutex_exit(&un->un_miilock);
    698 	if (--un->un_refcnt < 0)
    699 		cv_broadcast(&un->un_detachcv);
    700 }
    701 
    702 int
    703 usbnet_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    704 {
    705 	struct usbnet * const un = device_private(dev);
    706 	usbd_status err;
    707 
    708 	mutex_enter(&un->un_lock);
    709 	if (un->un_dying || un->un_phyno != phy) {
    710 		mutex_exit(&un->un_lock);
    711 		return EIO;
    712 	}
    713 	mutex_exit(&un->un_lock);
    714 
    715 	usbnet_lock_mii(un);
    716 	err = (*un->un_read_reg_cb)(un, phy, reg, val);
    717 	usbnet_unlock_mii(un);
    718 
    719 	if (err) {
    720 		aprint_error_dev(un->un_dev, "read PHY failed: %d\n", err);
    721 		return EIO;
    722 	}
    723 
    724 	return 0;
    725 }
    726 
    727 int
    728 usbnet_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    729 {
    730 	struct usbnet * const un = device_private(dev);
    731 	usbd_status err;
    732 
    733 	mutex_enter(&un->un_lock);
    734 	if (un->un_dying || un->un_phyno != phy) {
    735 		mutex_exit(&un->un_lock);
    736 		return EIO;
    737 	}
    738 	mutex_exit(&un->un_lock);
    739 
    740 	usbnet_lock_mii(un);
    741 	err = (*un->un_write_reg_cb)(un, phy, reg, val);
    742 	usbnet_unlock_mii(un);
    743 
    744 	if (err) {
    745 		aprint_error_dev(un->un_dev, "write PHY failed: %d\n", err);
    746 		return EIO;
    747 	}
    748 
    749 	return 0;
    750 }
    751 
    752 void
    753 usbnet_miibus_statchg(struct ifnet *ifp)
    754 {
    755 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    756 	struct usbnet * const un = ifp->if_softc;
    757 
    758 	(*un->un_statchg_cb)(ifp);
    759 }
    760 
    761 static int
    762 usbnet_media_upd(struct ifnet *ifp)
    763 {
    764 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    765 	struct usbnet * const un = ifp->if_softc;
    766 	struct mii_data * const mii = usbnet_mii(un);
    767 
    768 	if (un->un_dying)
    769 		return EIO;
    770 
    771 	un->un_link = false;
    772 
    773 	if (mii->mii_instance) {
    774 		struct mii_softc *miisc;
    775 
    776 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    777 			mii_phy_reset(miisc);
    778 	}
    779 
    780 	return ether_mediachange(ifp);
    781 }
    782 
    783 /* ioctl */
    784 
    785 static int
    786 usbnet_ifflags_cb(struct ethercom *ec)
    787 {
    788 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    789 	struct ifnet *ifp = &ec->ec_if;
    790 	struct usbnet *un = ifp->if_softc;
    791 	int rv = 0;
    792 
    793 	mutex_enter(&un->un_lock);
    794 
    795 	const int changed = ifp->if_flags ^ un->un_if_flags;
    796 	if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
    797 		un->un_if_flags = ifp->if_flags;
    798 		if ((changed & IFF_PROMISC) != 0)
    799 			rv = ENETRESET;
    800 	} else {
    801 		rv = ENETRESET;
    802 	}
    803 
    804 	mutex_exit(&un->un_lock);
    805 
    806 	return rv;
    807 }
    808 
    809 static int
    810 usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    811 {
    812 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    813 	struct usbnet * const un = ifp->if_softc;
    814 	int error;
    815 
    816 	if (un->un_override_ioctl_cb)
    817 		return (*un->un_override_ioctl_cb)(ifp, cmd, data);
    818 
    819 	error = ether_ioctl(ifp, cmd, data);
    820 	if (error == ENETRESET && un->un_ioctl_cb)
    821 		error = (*un->un_ioctl_cb)(ifp, cmd, data);
    822 
    823 	return error;
    824 }
    825 
    826 /*
    827  * Generic stop network function:
    828  *	- mark as stopping
    829  *	- call DD routine to stop the device
    830  *	- turn off running, timer, statchg callout, link
    831  *	- stop transfers
    832  *	- free RX and TX resources
    833  *	- close pipes
    834  *
    835  * usbnet_stop() is exported for drivers to use, expects lock held.
    836  *
    837  * usbnet_stop_ifp() is for the if_stop handler.
    838  */
    839 void
    840 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
    841 {
    842 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    843 
    844 	KASSERT(mutex_owned(&un->un_lock));
    845 
    846 	mutex_enter(&un->un_rxlock);
    847 	mutex_enter(&un->un_txlock);
    848 	un->un_stopping = true;
    849 	mutex_exit(&un->un_txlock);
    850 	mutex_exit(&un->un_rxlock);
    851 
    852 	if (un->un_stop_cb)
    853 		(*un->un_stop_cb)(ifp, disable);
    854 
    855 	/*
    856 	 * XXXSMP Would like to
    857 	 *	KASSERT(IFNET_LOCKED(ifp))
    858 	 * here but the locking order is:
    859 	 *	ifnet -> unlock -> rxlock -> txlock
    860 	 * and unlock is already held.
    861 	 */
    862 	ifp->if_flags &= ~IFF_RUNNING;
    863 	un->un_timer = 0;
    864 
    865 	callout_stop(&un->un_stat_ch);
    866 	un->un_link = false;
    867 
    868 	/* Stop transfers. */
    869 	usbnet_ep_stop_pipes(un);
    870 
    871 	/* Free RX/TX resources. */
    872 	usbnet_rx_list_fini(un);
    873 	usbnet_tx_list_fini(un);
    874 
    875 	/* Close pipes. */
    876 	usbnet_ep_close_pipes(un);
    877 }
    878 
    879 static void
    880 usbnet_stop_ifp(struct ifnet *ifp, int disable)
    881 {
    882 	struct usbnet * const un = ifp->if_softc;
    883 
    884 	mutex_enter(&un->un_lock);
    885 	usbnet_stop(un, ifp, disable);
    886 	mutex_exit(&un->un_lock);
    887 }
    888 
    889 /*
    890  * Generic tick task function.
    891  *
    892  * usbnet_tick() is triggered from a callout, and triggers a call to
    893  * usbnet_tick_task() from the usb_task subsystem.
    894  */
    895 static void
    896 usbnet_tick(void *arg)
    897 {
    898 	struct usbnet * const un = arg;
    899 
    900 	mutex_enter(&un->un_lock);
    901 	if (!un->un_stopping && !un->un_dying) {
    902 		/* Perform periodic stuff in process context */
    903 		usb_add_task(un->un_udev, &un->un_ticktask, USB_TASKQ_DRIVER);
    904 	}
    905 	mutex_exit(&un->un_lock);
    906 }
    907 
    908 static void
    909 usbnet_watchdog(struct ifnet *ifp)
    910 {
    911 	struct usbnet * const un = ifp->if_softc;
    912 	struct usbnet_cdata *cd = &un->un_cdata;
    913 	usbd_status stat;
    914 
    915 	ifp->if_oerrors++;
    916 	aprint_error_dev(un->un_dev, "watchdog timeout\n");
    917 
    918 	if (cd->uncd_tx_cnt > 0) {
    919 		/*
    920 		 * XXX index 0
    921 		 */
    922 		struct usbnet_chain *c = &un->un_cdata.uncd_tx_chain[0];
    923 		usbd_get_xfer_status(c->unc_xfer, NULL, NULL, NULL, &stat);
    924 		usbnet_txeof(c->unc_xfer, c, stat);
    925 	}
    926 
    927 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
    928 		(*ifp->if_start)(ifp);
    929 }
    930 
    931 static void
    932 usbnet_tick_task(void *arg)
    933 {
    934 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    935 	struct usbnet * const un = arg;
    936 
    937 	mutex_enter(&un->un_lock);
    938 	if (un->un_stopping || un->un_dying) {
    939 		mutex_exit(&un->un_lock);
    940 		return;
    941 	}
    942 
    943 	struct ifnet * const ifp = usbnet_ifp(un);
    944 	struct mii_data * const mii = usbnet_mii(un);
    945 
    946 	un->un_refcnt++;
    947 	mutex_exit(&un->un_lock);
    948 
    949 	if (ifp && un->un_timer != 0 && --un->un_timer == 0)
    950 		usbnet_watchdog(ifp);
    951 
    952 	if (mii && ifp) {
    953 		mii_tick(mii);
    954 
    955 		if (!un->un_link)
    956 			(*mii->mii_statchg)(ifp);
    957 	}
    958 
    959 	mutex_enter(&un->un_lock);
    960 	if (--un->un_refcnt < 0)
    961 		cv_broadcast(&un->un_detachcv);
    962 	if (!un->un_stopping && !un->un_dying)
    963 		callout_schedule(&un->un_stat_ch, hz);
    964 	mutex_exit(&un->un_lock);
    965 }
    966 
    967 static int
    968 usbnet_init(struct ifnet *ifp)
    969 {
    970 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
    971 	struct usbnet * const un = ifp->if_softc;
    972 
    973 	return (*un->un_init_cb)(ifp);
    974 }
    975 
    976 /* Autoconf management. */
    977 
    978 static bool
    979 usbnet_empty_eaddr(struct usbnet *un)
    980 {
    981 	return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
    982 		un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
    983 		un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
    984 }
    985 
    986 /*
    987  * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
    988  * 'usbnet'.  The first is enough to enable device access (eg, endpoints
    989  * are connected and commands can be sent), and the second connects the
    990  * device to the system networking.
    991  *
    992  * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
    993  * Also usable as driver detach directly.
    994  *
    995  * To skip ethernet configuration (eg, point-to-point), make sure that
    996  * the un_eaddr[] is fully zero.
    997  */
    998 void
    999 usbnet_attach(struct usbnet *un,
   1000 	      const char *detname,	/* detach cv name */
   1001 	      unsigned rx_list_cnt,	/* size of rx chain list */
   1002 	      unsigned tx_list_cnt)	/* size of tx chain list */
   1003 {
   1004 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1005 
   1006 	KASSERT(un->un_tx_prepare_cb);
   1007 	KASSERT(un->un_rx_loop_cb);
   1008 	KASSERT(un->un_init_cb);
   1009 	KASSERT(un->un_cdata.uncd_rx_bufsz);
   1010 	KASSERT(un->un_cdata.uncd_tx_bufsz);
   1011 	KASSERT(rx_list_cnt);
   1012 	KASSERT(tx_list_cnt);
   1013 
   1014 	ether_set_ifflags_cb(&un->un_ec, usbnet_ifflags_cb);
   1015 
   1016 	usb_init_task(&un->un_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE);
   1017 	callout_init(&un->un_stat_ch, CALLOUT_MPSAFE);
   1018 	callout_setfunc(&un->un_stat_ch, usbnet_tick, un);
   1019 
   1020 	mutex_init(&un->un_miilock, MUTEX_DEFAULT, IPL_NONE);
   1021 	mutex_init(&un->un_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1022 	mutex_init(&un->un_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1023 	mutex_init(&un->un_lock, MUTEX_DEFAULT, IPL_NONE);
   1024 	cv_init(&un->un_detachcv, detname);
   1025 
   1026 	rnd_attach_source(&un->un_rndsrc, device_xname(un->un_dev),
   1027 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
   1028 
   1029 	usbnet_rx_list_alloc(un, rx_list_cnt);
   1030 	usbnet_tx_list_alloc(un, tx_list_cnt);
   1031 
   1032 	un->un_attached = true;
   1033 }
   1034 
   1035 static void
   1036 usbnet_attach_mii(struct usbnet *un, int mii_flags)
   1037 {
   1038 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1039 	struct mii_data * const mii = &un->un_mii;
   1040 	struct ifnet *ifp = usbnet_ifp(un);
   1041 
   1042 	mii->mii_ifp = ifp;
   1043 	mii->mii_readreg = usbnet_miibus_readreg;
   1044 	mii->mii_writereg = usbnet_miibus_writereg;
   1045 	mii->mii_statchg = usbnet_miibus_statchg;
   1046 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1047 
   1048 	un->un_ec.ec_mii = mii;
   1049 	ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus);
   1050 	mii_attach(un->un_dev, mii, 0xffffffff, MII_PHY_ANY,
   1051 		   MII_OFFSET_ANY, mii_flags);
   1052 
   1053 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1054 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1055 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1056 	} else
   1057 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1058 
   1059 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
   1060 
   1061 	if (!pmf_device_register(un->un_dev, NULL, NULL))
   1062 		aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
   1063 }
   1064 
   1065 void
   1066 usbnet_attach_ifp(struct usbnet *un,
   1067 		  bool have_mii,		/* setup MII */
   1068 		  unsigned if_flags,		/* additional if_flags */
   1069 		  unsigned if_extflags,		/* additional if_extflags */
   1070 		  int mii_flags)		/* additional mii_attach flags */
   1071 {
   1072 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1073 	struct ifnet *ifp = usbnet_ifp(un);
   1074 
   1075 	KASSERT(un->un_attached);
   1076 
   1077 	IFQ_SET_READY(&ifp->if_snd);
   1078 
   1079 	ifp->if_softc = un;
   1080 	strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
   1081 	ifp->if_flags = if_flags;
   1082 	ifp->if_extflags = IFEF_MPSAFE | if_extflags;
   1083 	ifp->if_ioctl = usbnet_ioctl;
   1084 	ifp->if_start = usbnet_start;
   1085 	ifp->if_init = usbnet_init;
   1086 	ifp->if_stop = usbnet_stop_ifp;
   1087 
   1088 	IFQ_SET_READY(&ifp->if_snd);
   1089 
   1090 	if (have_mii)
   1091 		usbnet_attach_mii(un, mii_flags);
   1092 	else
   1093 		un->un_link = true;
   1094 
   1095 	/* Attach the interface. */
   1096 	if_attach(ifp);
   1097 
   1098 	/*
   1099 	 * If ethernet address is all zero, skip ether_ifattach() and
   1100 	 * instead attach bpf here..
   1101 	 */
   1102 	if (!usbnet_empty_eaddr(un)) {
   1103 		aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
   1104 		    ether_sprintf(un->un_eaddr));
   1105 		ether_ifattach(ifp, un->un_eaddr);
   1106 	} else {
   1107 		if_alloc_sadl(ifp);
   1108 		bpf_attach(ifp, DLT_RAW, 0);
   1109 	}
   1110 }
   1111 
   1112 int
   1113 usbnet_detach(device_t self, int flags)
   1114 {
   1115 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1116 	struct usbnet * const un = device_private(self);
   1117 	struct ifnet *ifp = usbnet_ifp(un);
   1118 	struct mii_data *mii = usbnet_mii(un);
   1119 
   1120 	mutex_enter(&un->un_lock);
   1121 	un->un_dying = true;
   1122 	mutex_exit(&un->un_lock);
   1123 
   1124 	/* Detached before attached finished, so just bail out. */
   1125 	if (!un->un_attached)
   1126 		return 0;
   1127 
   1128 	callout_halt(&un->un_stat_ch, NULL);
   1129 	usb_rem_task_wait(un->un_udev, &un->un_ticktask, USB_TASKQ_DRIVER, NULL);
   1130 
   1131 	if (ifp->if_flags & IFF_RUNNING) {
   1132 		IFNET_LOCK(ifp);
   1133 		usbnet_stop_ifp(ifp, 1);
   1134 		IFNET_UNLOCK(ifp);
   1135 	}
   1136 
   1137 	mutex_enter(&un->un_lock);
   1138 	un->un_refcnt--;
   1139 	while (un->un_refcnt > 0) {
   1140 		/* Wait for processes to go away */
   1141 		cv_wait(&un->un_detachcv, &un->un_lock);
   1142 	}
   1143 	mutex_exit(&un->un_lock);
   1144 
   1145 	usbnet_rx_list_free(un);
   1146 	usbnet_tx_list_free(un);
   1147 
   1148 	callout_destroy(&un->un_stat_ch);
   1149 	rnd_detach_source(&un->un_rndsrc);
   1150 
   1151 	if (mii) {
   1152 		mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1153 		ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY);
   1154 	}
   1155 	if (ifp->if_softc) {
   1156 		if (!usbnet_empty_eaddr(un))
   1157 			ether_ifdetach(ifp);
   1158 		else
   1159 			bpf_detach(ifp);
   1160 		if_detach(ifp);
   1161 	}
   1162 
   1163 	cv_destroy(&un->un_detachcv);
   1164 	mutex_destroy(&un->un_lock);
   1165 	mutex_destroy(&un->un_rxlock);
   1166 	mutex_destroy(&un->un_txlock);
   1167 	mutex_destroy(&un->un_miilock);
   1168 
   1169 	pmf_device_deregister(un->un_dev);
   1170 
   1171 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
   1172 
   1173 	return 0;
   1174 }
   1175 
   1176 int
   1177 usbnet_activate(device_t self, devact_t act)
   1178 {
   1179 	USBNETHIST_FUNC(); USBNETHIST_CALLED();
   1180 	struct usbnet * const un = device_private(self);
   1181 	struct ifnet * const ifp = usbnet_ifp(un);
   1182 
   1183 	switch (act) {
   1184 	case DVACT_DEACTIVATE:
   1185 		if_deactivate(ifp);
   1186 
   1187 		mutex_enter(&un->un_lock);
   1188 		un->un_dying = true;
   1189 		mutex_exit(&un->un_lock);
   1190 
   1191 		mutex_enter(&un->un_rxlock);
   1192 		mutex_enter(&un->un_txlock);
   1193 		un->un_stopping = true;
   1194 		mutex_exit(&un->un_txlock);
   1195 		mutex_exit(&un->un_rxlock);
   1196 
   1197 		return 0;
   1198 	default:
   1199 		return EOPNOTSUPP;
   1200 	}
   1201 }
   1202 
   1203 MODULE(MODULE_CLASS_MISC, usbnet, NULL);
   1204 
   1205 static int
   1206 usbnet_modcmd(modcmd_t cmd, void *arg)
   1207 {
   1208 	switch (cmd) {
   1209 	case MODULE_CMD_INIT:
   1210 		return 0;
   1211 	case MODULE_CMD_FINI:
   1212 		return 0;
   1213 	case MODULE_CMD_STAT:
   1214 	case MODULE_CMD_AUTOUNLOAD:
   1215 	default:
   1216 		return ENOTTY;
   1217 	}
   1218 }
   1219