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