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