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