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