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