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