Index: usb/TODO =================================================================== RCS file: /cvsroot/src/sys/dev/usb/TODO,v retrieving revision 1.46 diff -p -u -u -r1.46 TODO --- usb/TODO 29 Jun 2019 07:47:41 -0000 1.46 +++ usb/TODO 29 Aug 2019 06:22:05 -0000 @@ -11,20 +11,6 @@ Use some encapsulation in the upl driver Get the uezcom driver (with Doug Ambrisko's firmware) running. -Bug reports: ------------- - -Bernd Ernesti's problem: host controller halted with umodem - -umass problem with Ricoh flash reader - -wskbd for X - -mel kravitz's problem, lockups with disk and printer - -Aaron's problem: Test aue with ohci. - - General: -------- @@ -47,24 +33,16 @@ Rotate the QHs for bulk transport to get Change HC drivers to queue multiple request for an endpoint to get better performance. -Do memory deallocation when HC driver is detached. - uaudio problems: mixer names are awful, use some heuristics. implement selector units debug input - test with more devices Stylistic changes: use usb_ and usbd_ consistently rearrange the contents and names of some files (Nick) -Change what's done at watchdog timeout inb if_{a,c,k}ue.c; what we have -now doesn't work because it's done in an interrupt context. - -Factor out the common code from the Ethernet drivers and reuse that. - -Get rid of hcpriv. +Use usbnet in if_umb(4). Figure out how to use it for wifi. Keyspan serial driver @@ -74,8 +52,8 @@ Clean up umass driver Documentation: -------------- -Document device driver API. +Complete documentation of device driver API. -Document HC driver API. +Complete documentation of HC driver API. Update ugen(4) Index: usb/TODO.usbmp =================================================================== RCS file: /cvsroot/src/sys/dev/usb/TODO.usbmp,v retrieving revision 1.13 diff -p -u -u -r1.13 TODO.usbmp --- usb/TODO.usbmp 30 Jun 2019 22:19:55 -0000 1.13 +++ usb/TODO.usbmp 29 Aug 2019 06:22:05 -0000 @@ -21,9 +21,6 @@ KERNEL_LOCK usb_detach_{waitold,wakeup} to usb_detach_{wait,broadcast} conversion: - drivers: if_aue.c - if_axe.c - if_udav.c - if_url.c stuirda.c ubt.c ucom.c @@ -60,30 +57,33 @@ convert uhidev users to MPSAFE: uyurex(4) - sysmon -- hm? -splusb drivers: +splusb drivers to convert: - auvitek_dtv.c - auvitek_video.c - emdtv_dtv.c - if_athn_usb.c - - if_aue.c - - if_axe.c - - if_cue.c - - if_kue.c - if_otus.c - if_rum.c - if_run.c - - if_smsc.c - - if_udav.c - - if_upl.c + - if_umb.c - if_ural.c - - if_url.c - - if_urndis.c - if_urtw.c - if_urtwn.c - if_zyd.c - ubt.c - ucycom.c - udsir.c + - uhso.c + - uirda.c + - ukbd.c + - ulpt.c + - urio.c + - usbdi.h + - uscanner.c + - usscanner.c + - ustir.c + - utoppy.c + - uvideo.c wakeup/tsleep drivers: - if_otus.c @@ -114,17 +114,11 @@ missing D_MPSAFE drivers: - utoppy missing CALLOUT_MPSAFE drivers: - - if_aue.c - - if_axe.c - - if_cue.c - if_otus.c - if_rum.c - if_run.c - - if_smsc.c - - if_udav.c - if_upgt.c - if_ural.c - - if_url.c - if_urtw.c - if_urtwn.c - if_zyd.c @@ -155,11 +149,11 @@ driver testing: STATUS - axe working - axen working - cdce working - - cue - - kue + - cue working + - kue working - udav - - url - - urndis + - url working + - urndis working - atu - otus - ral @@ -170,7 +164,7 @@ driver testing: STATUS - urtwn working - upgt - zyd - - upl + - upl working - uberry - uipad - urio Index: usb/files.usb =================================================================== RCS file: /cvsroot/src/sys/dev/usb/files.usb,v retrieving revision 1.156 diff -p -u -u -r1.156 files.usb --- usb/files.usb 8 May 2019 13:40:19 -0000 1.156 +++ usb/files.usb 29 Aug 2019 06:22:05 -0000 @@ -75,6 +75,7 @@ defflag opt_usb.h URTWN_DEBUG: USB_DEBUG defflag opt_usb.h URTW_DEBUG: USB_DEBUG defflag opt_usb.h USCANNER_DEBUG: USB_DEBUG defflag opt_usb.h USLSA_DEBUG: USB_DEBUG +defflag opt_usb.h USMSC_DEBUG: USB_DEBUG defflag opt_usb.h USSCANNER_DEBUG: USB_DEBUG defflag opt_usb.h USTIR_DEBUG: USB_DEBUG defflag opt_usb.h UTHUM_DEBUG: USB_DEBUG @@ -103,6 +104,9 @@ file dev/usb/usb_quirks.c usb define usb_dma: usb file dev/usb/usb_mem.c usb_dma needs-flag +define usbnet: usb +file dev/usb/usbnet.c usbnet + file dev/usb/usb_verbose.c usbverbose & usb # Hub driver @@ -330,52 +334,52 @@ file dev/usb/uthum.c uthum # Ethernet adapters # ADMtek AN986 Pegasus -device aue: arp, ether, ifnet, mii, mii_phy +device aue: arp, ether, ifnet, mii, mii_phy, usbnet attach aue at usbdevif file dev/usb/if_aue.c aue # CDC Ethernet -device cdce: arp, ether, ifnet +device cdce: arp, ether, ifnet, usbnet attach cdce at usbifif file dev/usb/if_cdce.c cdce # CATC USB-EL1201A -device cue: arp, ether, ifnet +device cue: arp, ether, ifnet, usbnet attach cue at usbdevif file dev/usb/if_cue.c cue # Kawasaki LSI KL5KUSB101B -device kue: arp, ether, ifnet +device kue: arp, ether, ifnet, usbnet attach kue at usbdevif file dev/usb/if_kue.c kue # Prolific PL2302 host-host -device upl: ifnet +device upl: ifnet, usbnet attach upl at usbdevif file dev/usb/if_upl.c upl # Realtek RTL8150L(M) -device url: arp, ether, ifnet, mii +device url: arp, ether, ifnet, mii, usbnet attach url at usbdevif file dev/usb/if_url.c url # ASIX AX88172 -device axe: arp, ether, ifnet, mii, mii_phy +device axe: arp, ether, ifnet, mii, mii_phy, usbnet attach axe at usbdevif file dev/usb/if_axe.c axe # ASIX AX88178a and AX88179 -device axen: arp, ether, ifnet, mii +device axen: arp, ether, ifnet, mii, usbnet attach axen at usbdevif file dev/usb/if_axen.c axen # Microchip LAN750x and LAN85xx -device mue: arp, ether, ifnet, mii, mii_phy +device mue: arp, ether, ifnet, mii, mii_phy, usbnet attach mue at usbdevif file dev/usb/if_mue.c mue # DAVICOM DM9601 -device udav: arp, ether, ifnet, mii, mii_phy +device udav: arp, ether, ifnet, mii, mii_phy, usbnet attach udav at usbdevif file dev/usb/if_udav.c udav @@ -390,7 +394,7 @@ attach umb at usbifif file dev/usb/if_umb.c umb # RealTek RTL8152 and RTL8153 -device ure: arp, ether, ifnet, mii, mii_phy +device ure: arp, ether, ifnet, mii, mii_phy, usbnet attach ure at usbdevif file dev/usb/if_ure.c ure @@ -486,7 +490,7 @@ attach upgt at usbdevif file dev/usb/if_upgt.c upgt # SMSC LAN95xx -device usmsc: arp, ether, ifnet, mii, mii_phy +device usmsc: arp, ether, ifnet, mii, mii_phy, usbnet attach usmsc at usbdevif file dev/usb/if_smsc.c usmsc @@ -525,7 +529,7 @@ attach uyurex at uhidbus file dev/usb/uyurex.c uyurex # Microsoft RNDIS -device urndis: arp, ether, ifnet +device urndis: arp, ether, ifnet, usbnet attach urndis at usbifif file dev/usb/if_urndis.c urndis Index: usb/if_auereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_auereg.h,v retrieving revision 1.29 diff -p -u -u -r1.29 if_auereg.h --- usb/if_auereg.h 21 Jul 2019 10:27:56 -0000 1.29 +++ usb/if_auereg.h 29 Aug 2019 06:22:05 -0000 @@ -1,4 +1,5 @@ /* $NetBSD: if_auereg.h,v 1.29 2019/07/21 10:27:56 mrg Exp $ */ + /* * Copyright (c) 1997, 1998, 1999 * Bill Paul . All rights reserved. @@ -66,10 +67,6 @@ * we consider the RX data endpoint to be index 0 * and work up from there. */ -#define AUE_ENDPT_RX 0x0 -#define AUE_ENDPT_TX 0x1 -#define AUE_ENDPT_INTR 0x2 -#define AUE_ENDPT_MAX 0x3 #define AUE_CTL0 0x00 #define AUE_CTL1 0x01 @@ -198,74 +195,3 @@ struct aue_rxpkt { #define AUE_RXSTAT_CRCERR 0x08 #define AUE_RXSTAT_DRIBBLE 0x10 #define AUE_RXSTAT_MASK 0x1E - - -/*************** The rest belongs in if_auevar.h *************/ - -#define AUE_TX_LIST_CNT 1 -#define AUE_RX_LIST_CNT 1 - -struct aue_softc; - -struct aue_chain { - struct aue_softc *aue_sc; - struct usbd_xfer *aue_xfer; - char *aue_buf; - struct mbuf *aue_mbuf; - int aue_idx; -}; - -struct aue_cdata { - struct aue_chain aue_tx_chain[AUE_TX_LIST_CNT]; - struct aue_chain aue_rx_chain[AUE_RX_LIST_CNT]; - struct aue_intrpkt aue_ibuf; - int aue_tx_prod; - int aue_tx_cnt; -}; - -struct aue_softc { - device_t aue_dev; - - struct ethercom aue_ec; - struct mii_data aue_mii; - krndsource_t rnd_source; - struct lwp *aue_thread; - int aue_closing; - kcondvar_t aue_domc; - kcondvar_t aue_closemc; - kmutex_t aue_mcmtx; -#define GET_IFP(sc) (&(sc)->aue_ec.ec_if) -#define GET_MII(sc) (&(sc)->aue_mii) - - struct callout aue_stat_ch; - - struct usbd_device *aue_udev; - struct usbd_interface *aue_iface; - uint16_t aue_vendor; - uint16_t aue_product; - int aue_ed[AUE_ENDPT_MAX]; - struct usbd_pipe *aue_ep[AUE_ENDPT_MAX]; - uint8_t aue_link; - int aue_if_flags; - struct aue_cdata aue_cdata; - - uint16_t aue_flags; - - int aue_refcnt; - char aue_dying; - char aue_attached; - u_int aue_rx_errs; - u_int aue_intr_errs; - struct timeval aue_rx_notice; - - struct usb_task aue_tick_task; - struct usb_task aue_stop_task; - - kmutex_t aue_mii_lock; -}; - -#define AUE_TIMEOUT 1000 -#define AUE_BUFSZ 1536 -#define AUE_MIN_FRAMELEN 60 -#define AUE_TX_TIMEOUT 10000 /* ms */ -#define AUE_INTR_INTERVAL 100 /* ms */ Index: usb/if_axe.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_axe.c,v retrieving revision 1.102 diff -p -u -u -r1.102 if_axe.c --- usb/if_axe.c 21 Jul 2019 10:27:56 -0000 1.102 +++ usb/if_axe.c 29 Aug 2019 06:22:05 -0000 @@ -90,41 +90,14 @@ __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.102 2019/07/21 10:27:56 mrg Exp $"); #ifdef _KERNEL_OPT -#include "opt_inet.h" #include "opt_usb.h" #include "opt_net_mpsafe.h" #endif #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include +#include #include -#include -#include -#include -#include - #include struct axe_type { @@ -132,33 +105,10 @@ struct axe_type { uint16_t axe_flags; }; -struct axe_softc; - -struct axe_chain { - struct axe_softc *axe_sc; - struct usbd_xfer *axe_xfer; - uint8_t *axe_buf; -}; - -struct axe_cdata { - struct axe_chain axe_tx_chain[AXE_TX_LIST_CNT]; - struct axe_chain axe_rx_chain[AXE_RX_LIST_CNT]; - int axe_tx_prod; - int axe_tx_cnt; -}; - struct axe_softc { - device_t axe_dev; - struct ethercom axe_ec; - struct mii_data axe_mii; - krndsource_t rnd_source; - struct usbd_device * axe_udev; - struct usbd_interface * axe_iface; - - uint16_t axe_vendor; - uint16_t axe_product; - uint16_t axe_timer; - uint32_t axe_flags; /* copied from axe_type */ + struct usbnet axe_un; + + /* usbnet:un_flags values */ #define AX178 __BIT(0) /* AX88178 */ #define AX772 __BIT(1) /* AX88772 */ #define AX772A __BIT(2) /* AX88772A */ @@ -166,47 +116,18 @@ struct axe_softc { #define AXSTD_FRAME __BIT(12) #define AXCSUM_FRAME __BIT(13) - int axe_ed[AXE_ENDPT_MAX]; - struct usbd_pipe * axe_ep[AXE_ENDPT_MAX]; - int axe_if_flags; - int axe_phyno; - struct axe_cdata axe_cdata; - struct callout axe_stat_ch; - - uint8_t axe_enaddr[ETHER_ADDR_LEN]; - - int axe_refcnt; - bool axe_dying; - bool axe_stopping; - bool axe_attached; - - struct usb_task axe_tick_task; - - kmutex_t axe_lock; - kmutex_t axe_mii_lock; - kmutex_t axe_rxlock; - kmutex_t axe_txlock; - kcondvar_t axe_detachcv; - - int axe_link; - uint8_t axe_ipgs[3]; uint8_t axe_phyaddrs[2]; uint16_t sc_pwrcfg; uint16_t sc_lenmask; - struct timeval axe_rx_notice; - struct timeval axe_tx_notice; - int axe_bufsz; - -#define sc_if axe_ec.ec_if }; -#define AXE_IS_178_FAMILY(sc) \ - ((sc)->axe_flags & (AX772 | AX772A | AX772B | AX178)) +#define AXE_IS_178_FAMILY(un) \ + ((un)->un_flags & (AX772 | AX772A | AX772B | AX178)) -#define AXE_IS_772(sc) \ - ((sc)->axe_flags & (AX772 | AX772A | AX772B)) +#define AXE_IS_772(un) \ + ((un)->un_flags & (AX772 | AX772A | AX772B)) #define AX_RXCSUM \ (IFCAP_CSUM_IPv4_Rx | \ @@ -238,7 +159,7 @@ struct axe_softc { #ifndef AXE_DEBUG #define axedebug 0 #else -static int axedebug = 20; +static int axedebug = 0; SYSCTL_SETUP(sysctl_hw_axe_setup, "sysctl hw.axe setup") { @@ -331,97 +252,47 @@ static const struct ax88772b_mfb ax88772 int axe_match(device_t, cfdata_t, void *); void axe_attach(device_t, device_t, void *); -int axe_detach(device_t, int); -int axe_activate(device_t, devact_t); CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc), - axe_match, axe_attach, axe_detach, axe_activate); + axe_match, axe_attach, usbnet_detach, usbnet_activate); -static int axe_tx_list_init(struct axe_softc *); -static int axe_rx_list_init(struct axe_softc *); -static int axe_encap(struct axe_softc *, struct mbuf *, int); -static void axe_rxeof(struct usbd_xfer *, void *, usbd_status); -static void axe_txeof(struct usbd_xfer *, void *, usbd_status); -static void axe_tick(void *); -static void axe_tick_task(void *); -static void axe_start(struct ifnet *); -static void axe_start_locked(struct ifnet *); +static void axe_stop(struct ifnet *, int); static int axe_ioctl(struct ifnet *, u_long, void *); static int axe_init(struct ifnet *); -static int axe_init_locked(struct ifnet *); -static void axe_stop(struct ifnet *, int); -static void axe_stop_locked(struct ifnet *, int); -static void axe_watchdog(struct ifnet *); -static int axe_miibus_readreg(device_t, int, int, uint16_t *); -static int axe_miibus_readreg_locked(device_t, int, int, uint16_t *); -static int axe_miibus_writereg(device_t, int, int, uint16_t); -static int axe_miibus_writereg_locked(device_t, int, int, uint16_t); -static void axe_miibus_statchg(struct ifnet *); -static int axe_cmd(struct axe_softc *, int, int, int, void *); -static void axe_reset(struct axe_softc *); - -static void axe_setmulti(struct axe_softc *); -static void axe_setmulti_locked(struct axe_softc *); -static void axe_lock_mii(struct axe_softc *); -static void axe_unlock_mii(struct axe_softc *); +static int axe_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static int axe_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void axe_mii_statchg(struct ifnet *); +static void axe_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned axe_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); static void axe_ax88178_init(struct axe_softc *); static void axe_ax88772_init(struct axe_softc *); static void axe_ax88772a_init(struct axe_softc *); static void axe_ax88772b_init(struct axe_softc *); -/* Get exclusive access to the MII registers */ -static void -axe_lock_mii(struct axe_softc *sc) -{ - - mutex_enter(&sc->axe_lock); - sc->axe_refcnt++; - mutex_exit(&sc->axe_lock); - - mutex_enter(&sc->axe_mii_lock); -} - -static void -axe_lock_mii_sc_locked(struct axe_softc *sc) -{ - KASSERT(mutex_owned(&sc->axe_lock)); - - sc->axe_refcnt++; - mutex_enter(&sc->axe_mii_lock); -} - -static void -axe_unlock_mii(struct axe_softc *sc) -{ - - mutex_exit(&sc->axe_mii_lock); - mutex_enter(&sc->axe_lock); - if (--sc->axe_refcnt < 0) - cv_broadcast(&sc->axe_detachcv); - mutex_exit(&sc->axe_lock); -} - -static void -axe_unlock_mii_sc_locked(struct axe_softc *sc) -{ - KASSERT(mutex_owned(&sc->axe_lock)); - - mutex_exit(&sc->axe_mii_lock); - if (--sc->axe_refcnt < 0) - cv_broadcast(&sc->axe_detachcv); -} +static struct usbnet_ops axe_ops = { + .uno_stop = axe_stop, + .uno_ioctl = axe_ioctl, + .uno_read_reg = axe_mii_read_reg, + .uno_write_reg = axe_mii_write_reg, + .uno_statchg = axe_mii_statchg, + .uno_tx_prepare = axe_tx_prepare, + .uno_rx_loop = axe_rx_loop, + .uno_init = axe_init, +}; -static int +static usbd_status axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf) { AXEHIST_FUNC(); AXEHIST_CALLED(); + struct usbnet * const un = &sc->axe_un; usb_device_request_t req; usbd_status err; - KASSERT(mutex_owned(&sc->axe_mii_lock)); + usbnet_isowned_mii(un); - if (sc->axe_dying) + if (usbnet_isdying(un)) return -1; DPRINTFN(20, "cmd %#jx index %#jx val %#jx", cmd, index, val, 0); @@ -435,44 +306,38 @@ axe_cmd(struct axe_softc *sc, int cmd, i USETW(req.wIndex, index); USETW(req.wLength, AXE_CMD_LEN(cmd)); - err = usbd_do_request(sc->axe_udev, &req, buf); - - if (err) { + err = usbd_do_request(un->un_udev, &req, buf); + if (err) DPRINTF("cmd %jd err %jd", cmd, err, 0, 0); - return -1; - } - return 0; + + return err; } static int -axe_miibus_readreg_locked(device_t dev, int phy, int reg, uint16_t *val) +axe_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_softc *sc = device_private(dev); + struct axe_softc * const sc = usbnet_softc(un); usbd_status err; uint16_t data; - mutex_enter(&sc->axe_lock); - if (sc->axe_dying || sc->axe_phyno != phy) { - mutex_exit(&sc->axe_lock); - return -1; - } - mutex_exit(&sc->axe_lock); - DPRINTFN(30, "phy 0x%jx reg 0x%jx\n", phy, reg, 0, 0); + if (un->un_phyno != phy) + return EINVAL; + axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL); err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &data); axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL); if (err) { - aprint_error_dev(sc->axe_dev, "read PHY failed\n"); - return err; + aprint_error_dev(un->un_dev, "read PHY failed\n"); + return EIO; } *val = le16toh(data); - if (AXE_IS_772(sc) && reg == MII_BMSR) { + if (AXE_IS_772(un) && reg == MII_BMSR) { /* * BMSR of AX88772 indicates that it supports extended * capability but the extended status register is @@ -488,82 +353,43 @@ axe_miibus_readreg_locked(device_t dev, } static int -axe_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) -{ - struct axe_softc *sc = device_private(dev); - int rv; - - mutex_enter(&sc->axe_lock); - if (sc->axe_dying || sc->axe_phyno != phy) { - mutex_exit(&sc->axe_lock); - return -1; - } - mutex_exit(&sc->axe_lock); - - axe_lock_mii(sc); - rv = axe_miibus_readreg_locked(dev, phy, reg, val); - axe_unlock_mii(sc); - - return rv; -} - -static int -axe_miibus_writereg_locked(device_t dev, int phy, int reg, uint16_t aval) +axe_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct axe_softc *sc = device_private(dev); + struct axe_softc * const sc = usbnet_softc(un); usbd_status err; - uint16_t val; + uint16_t aval; - val = htole16(aval); + if (un->un_phyno != phy) + return EINVAL; + + aval = htole16(val); axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL); - err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val); + err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &aval); axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL); - if (err) { - aprint_error_dev(sc->axe_dev, "write PHY failed\n"); - return err; - } - + if (err) + return EIO; return 0; } -static int -axe_miibus_writereg(device_t dev, int phy, int reg, uint16_t aval) -{ - struct axe_softc *sc = device_private(dev); - int rv; - - mutex_enter(&sc->axe_lock); - if (sc->axe_dying || sc->axe_phyno != phy) { - mutex_exit(&sc->axe_lock); - return -1; - } - mutex_exit(&sc->axe_lock); - - axe_lock_mii(sc); - rv = axe_miibus_writereg_locked(dev, phy, reg, aval); - axe_unlock_mii(sc); - - return rv; -} - static void -axe_miibus_statchg(struct ifnet *ifp) +axe_mii_statchg(struct ifnet *ifp) { AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_softc * const sc = ifp->if_softc; - struct mii_data *mii = &sc->axe_mii; + struct usbnet * const un = ifp->if_softc; + struct axe_softc * const sc = usbnet_softc(un); + struct mii_data *mii = usbnet_mii(un); int val, err; - if (sc->axe_dying) + if (usbnet_isdying(un)) return; val = 0; if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { val |= AXE_MEDIA_FULL_DUPLEX; - if (AXE_IS_178_FAMILY(sc)) { + if (AXE_IS_178_FAMILY(un)) { if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0) val |= AXE_178_MEDIA_TXFLOW_CONTROL_EN; @@ -572,52 +398,53 @@ axe_miibus_statchg(struct ifnet *ifp) val |= AXE_178_MEDIA_RXFLOW_CONTROL_EN; } } - if (AXE_IS_178_FAMILY(sc)) { + if (AXE_IS_178_FAMILY(un)) { val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC; - if (sc->axe_flags & AX178) + if (un->un_flags & AX178) val |= AXE_178_MEDIA_ENCK; switch (IFM_SUBTYPE(mii->mii_media_active)) { case IFM_1000_T: val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK; + usbnet_set_link(un, true); break; case IFM_100_TX: val |= AXE_178_MEDIA_100TX; + usbnet_set_link(un, true); break; case IFM_10_T: - /* doesn't need to be handled */ + usbnet_set_link(un, true); break; } } DPRINTF("val=0x%jx", val, 0, 0, 0); - axe_lock_mii(sc); + usbnet_lock_mii(un); err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL); - axe_unlock_mii(sc); - if (err) { - aprint_error_dev(sc->axe_dev, "media change failed\n"); - return; - } + usbnet_unlock_mii(un); + if (err) + aprint_error_dev(un->un_dev, "media change failed\n"); } static void -axe_setmulti_locked(struct axe_softc *sc) +axe_setiff_locked(struct usbnet *un) { AXEHIST_FUNC(); AXEHIST_CALLED(); - struct ethercom *ec = &sc->axe_ec; - struct ifnet *ifp = &sc->sc_if; + struct axe_softc * const sc = usbnet_softc(un); + struct ifnet * const ifp = usbnet_ifp(un); + struct ethercom *ec = usbnet_ec(un); struct ether_multi *enm; struct ether_multistep step; uint32_t h = 0; uint16_t rxmode; uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; - KASSERT(mutex_owned(&sc->axe_mii_lock)); + usbnet_isowned_mii(un); - if (sc->axe_dying) + if (usbnet_isdying(un)) return; if (axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode)) { - aprint_error_dev(sc->axe_dev, "can't read rxmode"); + aprint_error_dev(un->un_dev, "can't read rxmode"); return; } rxmode = le16toh(rxmode); @@ -664,43 +491,47 @@ axe_setmulti_locked(struct axe_softc *sc } static void -axe_setmulti(struct axe_softc *sc) +axe_setiff(struct usbnet *un) { - axe_lock_mii(sc); - axe_setmulti_locked(sc); - axe_unlock_mii(sc); + usbnet_lock_mii(un); + axe_setiff_locked(un); + usbnet_unlock_mii(un); } static void -axe_ax_init(struct axe_softc *sc) +axe_ax_init(struct usbnet *un) { + struct axe_softc * const sc = usbnet_softc(un); + int cmd = AXE_178_CMD_READ_NODEID; - if (sc->axe_flags & AX178) { + if (un->un_flags & AX178) { axe_ax88178_init(sc); - } else if (sc->axe_flags & AX772) { + } else if (un->un_flags & AX772) { axe_ax88772_init(sc); - } else if (sc->axe_flags & AX772A) { + } else if (un->un_flags & AX772A) { axe_ax88772a_init(sc); - } else if (sc->axe_flags & AX772B) { + } else if (un->un_flags & AX772B) { axe_ax88772b_init(sc); return; } else { cmd = AXE_172_CMD_READ_NODEID; } - if (axe_cmd(sc, cmd, 0, 0, sc->axe_enaddr)) { - aprint_error_dev(sc->axe_dev, + if (axe_cmd(sc, cmd, 0, 0, un->un_eaddr)) { + aprint_error_dev(un->un_dev, "failed to read ethernet address\n"); } } static void -axe_reset(struct axe_softc *sc) +axe_reset(struct usbnet *un) { - if (sc->axe_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return; /* @@ -708,7 +539,7 @@ axe_reset(struct axe_softc *sc) * if_addr_init -> if_init. This doesn't mix well with the * usbd_delay_ms calls in the init routines as things like nd6_slowtimo * can fire during the wait and attempt to take softnet_lock and then - * block the softclk thread meaing the wait never ends. + * block the softclk thread meaning the wait never ends. */ #ifndef NET_MPSAFE /* XXX What to reset? */ @@ -716,11 +547,7 @@ axe_reset(struct axe_softc *sc) /* Wait a little while for the chip to get its brains in order. */ DELAY(1000); #else - axe_lock_mii(sc); - - axe_ax_init(sc); - - axe_unlock_mii(sc); + axe_ax_init(un); #endif } @@ -751,13 +578,14 @@ axe_get_phyno(struct axe_softc *sc, int #define AXE_GPIO_WRITE(x, y) do { \ axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL); \ - usbd_delay_ms(sc->axe_udev, hztoms(y)); \ + usbd_delay_ms(sc->axe_un.un_udev, hztoms(y)); \ } while (0) static void axe_ax88178_init(struct axe_softc *sc) { AXEHIST_FUNC(); AXEHIST_CALLED(); + struct usbnet * const un = &sc->axe_un; int gpio0, ledmode, phymode; uint16_t eeprom, val; @@ -841,16 +669,11 @@ axe_ax88178_init(struct axe_softc *sc) AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4); AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32); if (phymode == AXE_PHY_MODE_REALTEK_8211CL) { - axe_miibus_writereg_locked(sc->axe_dev, - sc->axe_phyno, 0x1F, 0x0005); - axe_miibus_writereg_locked(sc->axe_dev, - sc->axe_phyno, 0x0C, 0x0000); - axe_miibus_readreg_locked(sc->axe_dev, - sc->axe_phyno, 0x0001, &val); - axe_miibus_writereg_locked(sc->axe_dev, - sc->axe_phyno, 0x01, val | 0x0080); - axe_miibus_writereg_locked(sc->axe_dev, - sc->axe_phyno, 0x1F, 0x0000); + axe_mii_write_reg(un, un->un_phyno, 0x1F, 0x0005); + axe_mii_write_reg(un, un->un_phyno, 0x0C, 0x0000); + axe_mii_read_reg(un, un->un_phyno, 0x0001, &val); + axe_mii_write_reg(un, un->un_phyno, 0x01, val | 0x0080); + axe_mii_write_reg(un, un->un_phyno, 0x1F, 0x0000); } break; default: @@ -860,13 +683,13 @@ axe_ax88178_init(struct axe_softc *sc) /* soft reset */ axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL); - usbd_delay_ms(sc->axe_udev, 150); + usbd_delay_ms(un->un_udev, 150); axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL); - usbd_delay_ms(sc->axe_udev, 150); + usbd_delay_ms(un->un_udev, 150); /* Enable MII/GMII/RGMII interface to work with external PHY. */ axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL); - usbd_delay_ms(sc->axe_udev, 10); + usbd_delay_ms(un->un_udev, 10); axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); } @@ -874,24 +697,25 @@ static void axe_ax88772_init(struct axe_softc *sc) { AXEHIST_FUNC(); AXEHIST_CALLED(); + struct usbnet * const un = &sc->axe_un; axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL); - usbd_delay_ms(sc->axe_udev, 40); + usbd_delay_ms(un->un_udev, 40); - if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) { + if (un->un_phyno == AXE_772_PHY_NO_EPHY) { /* ask for the embedded PHY */ axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_EMBEDDED, NULL); - usbd_delay_ms(sc->axe_udev, 10); + usbd_delay_ms(un->un_udev, 10); /* power down and reset state, pin reset state */ axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL); - usbd_delay_ms(sc->axe_udev, 60); + usbd_delay_ms(un->un_udev, 60); /* power down/reset state, pin operating state */ axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL); - usbd_delay_ms(sc->axe_udev, 150); + usbd_delay_ms(un->un_udev, 150); /* power up, reset */ axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL); @@ -903,14 +727,14 @@ axe_ax88772_init(struct axe_softc *sc) /* ask for external PHY */ axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_EXT, NULL); - usbd_delay_ms(sc->axe_udev, 10); + usbd_delay_ms(un->un_udev, 10); /* power down internal PHY */ axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL); } - usbd_delay_ms(sc->axe_udev, 150); + usbd_delay_ms(un->un_udev, 150); axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); } @@ -918,12 +742,13 @@ static void axe_ax88772_phywake(struct axe_softc *sc) { AXEHIST_FUNC(); AXEHIST_CALLED(); + struct usbnet * const un = &sc->axe_un; - if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) { + if (un->un_phyno == AXE_772_PHY_NO_EPHY) { /* Manually select internal(embedded) PHY - MAC mode. */ axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_EMBEDDED, NULL); - usbd_delay_ms(sc->axe_udev, hztoms(hz / 32)); + usbd_delay_ms(un->un_udev, hztoms(hz / 32)); } else { /* * Manually select external PHY - MAC mode. @@ -931,31 +756,31 @@ axe_ax88772_phywake(struct axe_softc *sc */ axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB | AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL); - usbd_delay_ms(sc->axe_udev, hztoms(hz / 32)); + usbd_delay_ms(un->un_udev, hztoms(hz / 32)); } axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD | AXE_SW_RESET_IPRL, NULL); /* T1 = min 500ns everywhere */ - usbd_delay_ms(sc->axe_udev, 150); + usbd_delay_ms(un->un_udev, 150); /* Take PHY out of power down. */ - if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) { + if (un->un_phyno == AXE_772_PHY_NO_EPHY) { axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL); } else { axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRTE, NULL); } /* 772 T2 is 60ms. 772A T2 is 160ms, 772B T2 is 600ms */ - usbd_delay_ms(sc->axe_udev, 600); + usbd_delay_ms(un->un_udev, 600); axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL); /* T3 = 500ns everywhere */ - usbd_delay_ms(sc->axe_udev, hztoms(hz / 32)); + usbd_delay_ms(un->un_udev, hztoms(hz / 32)); axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL); - usbd_delay_ms(sc->axe_udev, hztoms(hz / 32)); + usbd_delay_ms(un->un_udev, hztoms(hz / 32)); } static void @@ -974,6 +799,7 @@ static void axe_ax88772b_init(struct axe_softc *sc) { AXEHIST_FUNC(); AXEHIST_CALLED(); + struct usbnet * const un = &sc->axe_un; uint16_t eeprom; int i; @@ -986,7 +812,7 @@ axe_ax88772b_init(struct axe_softc *sc) */ if (axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG, &eeprom)) { - aprint_error_dev(sc->axe_dev, "failed to read eeprom\n"); + aprint_error_dev(un->un_dev, "failed to read eeprom\n"); return; } @@ -997,11 +823,11 @@ axe_ax88772b_init(struct axe_softc *sc) * 00:00:00:00:00:00 such that an explicit access to EEPROM * is required to get real station address. */ - uint8_t *eaddr = sc->axe_enaddr; + uint8_t *eaddr = un->un_eaddr; for (i = 0; i < ETHER_ADDR_LEN / 2; i++) { if (axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_NODE_ID + i, &eeprom)) { - aprint_error_dev(sc->axe_dev, + aprint_error_dev(un->un_dev, "failed to read eeprom\n"); eeprom = 0; } @@ -1037,28 +863,35 @@ void axe_attach(device_t parent, device_t self, void *aux) { AXEHIST_FUNC(); AXEHIST_CALLED(); + USBNET_MII_DECL_DEFAULT(unm); struct axe_softc *sc = device_private(self); + struct usbnet * const un = &sc->axe_un; struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; usbd_status err; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; - struct mii_data *mii; char *devinfop; - const char *devname = device_xname(self); - struct ifnet *ifp; + unsigned bufsz; int i; + KASSERT((void *)sc == un); + aprint_naive("\n"); aprint_normal("\n"); - - sc->axe_dev = self; - sc->axe_udev = dev; - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &axe_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = AXE_RX_LIST_CNT; + un->un_tx_list_cnt = AXE_TX_LIST_CNT; + err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -1066,35 +899,31 @@ axe_attach(device_t parent, device_t sel return; } - sc->axe_flags = axe_lookup(uaa->uaa_vendor, uaa->uaa_product)->axe_flags; - - usb_init_task(&sc->axe_tick_task, axe_tick_task, sc, USB_TASKQ_MPSAFE); + un->un_flags = axe_lookup(uaa->uaa_vendor, uaa->uaa_product)->axe_flags; - err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface); + err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->axe_product = uaa->uaa_product; - sc->axe_vendor = uaa->uaa_vendor; - - id = usbd_get_interface_descriptor(sc->axe_iface); + id = usbd_get_interface_descriptor(un->un_iface); /* decide on what our bufsize will be */ - if (AXE_IS_178_FAMILY(sc)) - sc->axe_bufsz = (sc->axe_udev->ud_speed == USB_SPEED_HIGH) ? + if (AXE_IS_178_FAMILY(un)) + bufsz = (un->un_udev->ud_speed == USB_SPEED_HIGH) ? AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ; else - sc->axe_bufsz = AXE_172_BUFSZ; + bufsz = AXE_172_BUFSZ; + un->un_rx_bufsz = un->un_tx_bufsz = bufsz; - sc->axe_ed[AXE_ENDPT_RX] = -1; - sc->axe_ed[AXE_ENDPT_TX] = -1; - sc->axe_ed[AXE_ENDPT_INTR] = -1; + un->un_ed[USBNET_ENDPT_RX] = 0; + un->un_ed[USBNET_ENDPT_TX] = 0; + un->un_ed[USBNET_ENDPT_INTR] = 0; /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; @@ -1103,56 +932,46 @@ axe_attach(device_t parent, device_t sel const uint8_t dir = UE_GET_DIR(ed->bEndpointAddress); if (dir == UE_DIR_IN && xt == UE_BULK && - sc->axe_ed[AXE_ENDPT_RX] == -1) { - sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] == 0) { + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (dir == UE_DIR_OUT && xt == UE_BULK && - sc->axe_ed[AXE_ENDPT_TX] == -1) { - sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] == 0) { + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } else if (dir == UE_DIR_IN && xt == UE_INTERRUPT) { - sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } } /* Set these up now for axe_cmd(). */ - mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE); - mutex_init(&sc->axe_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->axe_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->axe_lock, MUTEX_DEFAULT, IPL_NONE); - cv_init(&sc->axe_detachcv, "axedet"); + usbnet_attach(un, "axedet"); /* We need the PHYID for init dance in some cases */ - axe_lock_mii(sc); + usbnet_lock_mii(un); if (axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, &sc->axe_phyaddrs)) { aprint_error_dev(self, "failed to read phyaddrs\n"); - cv_destroy(&sc->axe_detachcv); - mutex_destroy(&sc->axe_lock); - mutex_destroy(&sc->axe_rxlock); - mutex_destroy(&sc->axe_txlock); - mutex_destroy(&sc->axe_mii_lock); - return; } DPRINTF(" phyaddrs[0]: %jx phyaddrs[1]: %jx", sc->axe_phyaddrs[0], sc->axe_phyaddrs[1], 0, 0); - sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI); - if (sc->axe_phyno == -1) - sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC); - if (sc->axe_phyno == -1) { + un->un_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI); + if (un->un_phyno == -1) + un->un_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC); + if (un->un_phyno == -1) { DPRINTF(" no valid PHY address found, assuming PHY address 0", 0, 0, 0, 0); - sc->axe_phyno = 0; + un->un_phyno = 0; } /* Initialize controller and get station address. */ - axe_ax_init(sc); + axe_ax_init(un); /* * Fetch IPG values. */ - if (sc->axe_flags & (AX772A | AX772B)) { + if (un->un_flags & (AX772A | AX772B)) { /* Set IPG values. */ sc->axe_ipgs[0] = AXE_IPG0_DEFAULT; sc->axe_ipgs[1] = AXE_IPG1_DEFAULT; @@ -1160,42 +979,18 @@ axe_attach(device_t parent, device_t sel } else { if (axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->axe_ipgs)) { aprint_error_dev(self, "failed to read ipg\n"); - - cv_destroy(&sc->axe_detachcv); - mutex_destroy(&sc->axe_lock); - mutex_destroy(&sc->axe_rxlock); - mutex_destroy(&sc->axe_txlock); - mutex_destroy(&sc->axe_mii_lock); - + usbnet_unlock_mii(un); return; } } - axe_unlock_mii(sc); + usbnet_unlock_mii(un); - /* - * An ASIX chip was detected. Inform the world. - */ - aprint_normal_dev(self, "Ethernet address %s\n", - ether_sprintf(sc->axe_enaddr)); + if (AXE_IS_178_FAMILY(un)) + usbnet_ec(un)->ec_capabilities = ETHERCAP_VLAN_MTU; + if (un->un_flags & AX772B) { + struct ifnet *ifp = usbnet_ifp(un); - /* Initialize interface info.*/ - ifp = &sc->sc_if; - ifp->if_softc = sc; - strlcpy(ifp->if_xname, devname, IFNAMSIZ); - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_extflags = IFEF_MPSAFE; - ifp->if_ioctl = axe_ioctl; - ifp->if_start = axe_start; - ifp->if_init = axe_init; - ifp->if_stop = axe_stop; - ifp->if_watchdog = axe_watchdog; - - IFQ_SET_READY(&ifp->if_snd); - - if (AXE_IS_178_FAMILY(sc)) - sc->axe_ec.ec_capabilities = ETHERCAP_VLAN_MTU; - if (sc->axe_flags & AX772B) { ifp->if_capabilities = IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx | @@ -1211,236 +1006,36 @@ axe_attach(device_t parent, device_t sel */ } u_int adv_pause; - if (sc->axe_flags & (AX772A | AX772B | AX178)) + if (un->un_flags & (AX772A | AX772B | AX178)) adv_pause = MIIF_DOPAUSE; else adv_pause = 0; adv_pause = 0; - /* Initialize MII/media info. */ - mii = &sc->axe_mii; - mii->mii_ifp = ifp; - mii->mii_readreg = axe_miibus_readreg; - mii->mii_writereg = axe_miibus_writereg; - mii->mii_statchg = axe_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - - sc->axe_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus); - - mii_attach(sc->axe_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, - adv_pause); - - if (LIST_EMPTY(&mii->mii_phys)) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, sc->axe_enaddr); - rnd_attach_source(&sc->rnd_source, device_xname(sc->axe_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&sc->axe_stat_ch, CALLOUT_MPSAFE); - callout_setfunc(&sc->axe_stat_ch, axe_tick, sc); - - sc->axe_attached = true; - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev, sc->axe_dev); - - if (!pmf_device_register(self, NULL, NULL)) - aprint_error_dev(self, "couldn't establish power handler\n"); -} - -int -axe_detach(device_t self, int flags) -{ - AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_softc *sc = device_private(self); - struct ifnet *ifp = &sc->sc_if; - - mutex_enter(&sc->axe_lock); - sc->axe_dying = true; - mutex_exit(&sc->axe_lock); - - /* Detached before attached finished, so just bail out. */ - if (!sc->axe_attached) - return 0; - - pmf_device_deregister(self); - - callout_halt(&sc->axe_stat_ch, NULL); - usb_rem_task_wait(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER, - NULL); - - if (ifp->if_flags & IFF_RUNNING) { - IFNET_LOCK(ifp); - axe_stop(ifp, 1); - IFNET_UNLOCK(ifp); - } - - mutex_enter(&sc->axe_lock); - sc->axe_refcnt--; - while (sc->axe_refcnt > 0) { - /* Wait for processes to go away */ - cv_wait(&sc->axe_detachcv, &sc->axe_lock); - } - -#ifdef DIAGNOSTIC - if (sc->axe_ep[AXE_ENDPT_TX] != NULL || - sc->axe_ep[AXE_ENDPT_RX] != NULL || - sc->axe_ep[AXE_ENDPT_INTR] != NULL) - aprint_debug_dev(self, "detach has active endpoints\n"); -#endif - - mutex_exit(&sc->axe_lock); - - callout_destroy(&sc->axe_stat_ch); - rnd_detach_source(&sc->rnd_source); - mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY); - ether_ifdetach(ifp); - if_detach(ifp); - - sc->axe_attached = false; - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev, sc->axe_dev); - - cv_destroy(&sc->axe_detachcv); - mutex_destroy(&sc->axe_lock); - mutex_destroy(&sc->axe_rxlock); - mutex_destroy(&sc->axe_txlock); - mutex_destroy(&sc->axe_mii_lock); - - return 0; -} - -int -axe_activate(device_t self, devact_t act) -{ - AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_softc *sc = device_private(self); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(&sc->axe_ec.ec_if); - - mutex_enter(&sc->axe_lock); - sc->axe_dying = true; - mutex_exit(&sc->axe_lock); - - mutex_enter(&sc->axe_rxlock); - mutex_enter(&sc->axe_txlock); - sc->axe_stopping = true; - mutex_exit(&sc->axe_txlock); - mutex_exit(&sc->axe_rxlock); - - return 0; - default: - return EOPNOTSUPP; - } -} - -static int -axe_rx_list_init(struct axe_softc *sc) -{ - AXEHIST_FUNC(); AXEHIST_CALLED(); - - struct axe_cdata *cd; - struct axe_chain *c; - int i; - - cd = &sc->axe_cdata; - for (i = 0; i < AXE_RX_LIST_CNT; i++) { - c = &cd->axe_rx_chain[i]; - c->axe_sc = sc; - if (c->axe_xfer == NULL) { - int err = usbd_create_xfer(sc->axe_ep[AXE_ENDPT_RX], - sc->axe_bufsz, 0, 0, &c->axe_xfer); - if (err) - return err; - c->axe_buf = usbd_get_buffer(c->axe_xfer); - } - } - - return 0; -} - -static int -axe_tx_list_init(struct axe_softc *sc) -{ - AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_cdata *cd; - struct axe_chain *c; - int i; - - cd = &sc->axe_cdata; - for (i = 0; i < AXE_TX_LIST_CNT; i++) { - c = &cd->axe_tx_chain[i]; - c->axe_sc = sc; - if (c->axe_xfer == NULL) { - int err = usbd_create_xfer(sc->axe_ep[AXE_ENDPT_TX], - sc->axe_bufsz, USBD_FORCE_SHORT_XFER, 0, - &c->axe_xfer); - if (err) - return err; - c->axe_buf = usbd_get_buffer(c->axe_xfer); - } - } - - cd->axe_tx_cnt = 0; - - return 0; + unm.un_mii_flags = adv_pause; + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, &unm); } -/* - * A frame has been uploaded: pass the resulting mbuf chain up to - * the higher level protocols. - */ static void -axe_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status) +axe_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len) { AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_chain *c = (struct axe_chain *)priv; - struct axe_softc * const sc = c->axe_sc; - struct ifnet *ifp = &sc->sc_if; - uint8_t *buf = c->axe_buf; - uint32_t total_len; - struct mbuf *m; - - mutex_enter(&sc->axe_rxlock); - - if (sc->axe_dying || sc->axe_stopping || - status == USBD_INVAL || status == USBD_NOT_STARTED || - status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) { - mutex_exit(&sc->axe_rxlock); - return; - } - - if (status != USBD_NORMAL_COMPLETION) { - if (usbd_ratecheck(&sc->axe_rx_notice)) { - aprint_error_dev(sc->axe_dev, "usb errors on rx: %s\n", - usbd_errstr(status)); - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + struct axe_softc * const sc = usbnet_softc(un); + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; do { u_int pktlen = 0; u_int rxlen = 0; int flags = 0; - if ((sc->axe_flags & AXSTD_FRAME) != 0) { + + if ((un->un_flags & AXSTD_FRAME) != 0) { struct axe_sframe_hdr hdr; if (total_len < sizeof(hdr)) { ifp->if_ierrors++; - goto done; + break; } #if !defined(__NO_STRICT_ALIGNMENT) && __GNUC_PREREQ__(6, 1) @@ -1468,7 +1063,7 @@ axe_rxeof(struct usbd_xfer *xfer, void * (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK)) != AXE_RH1M_RXLEN_MASK) { ifp->if_ierrors++; - goto done; + break; } rxlen = le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK; @@ -1481,12 +1076,12 @@ axe_rxeof(struct usbd_xfer *xfer, void * total_len -= rxlen; } - } else if ((sc->axe_flags & AXCSUM_FRAME) != 0) { + } else if ((un->un_flags & AXCSUM_FRAME) != 0) { struct axe_csum_hdr csum_hdr; - if (total_len < sizeof(csum_hdr)) { + if (total_len < sizeof(csum_hdr)) { ifp->if_ierrors++; - goto done; + break; } memcpy(&csum_hdr, buf, sizeof(csum_hdr)); @@ -1509,7 +1104,7 @@ axe_rxeof(struct usbd_xfer *xfer, void * AXE_CSUM_RXBYTES(csum_hdr.len), AXE_CSUM_RXBYTES(csum_hdr.ilen), sc->sc_lenmask, 0); - goto done; + break; } /* * Get total transferred frame length including @@ -1524,7 +1119,7 @@ axe_rxeof(struct usbd_xfer *xfer, void * total_len, len, 0, 0); /* invalid length */ ifp->if_ierrors++; - goto done; + break; } buf += sizeof(csum_hdr); @@ -1558,244 +1153,67 @@ axe_rxeof(struct usbd_xfer *xfer, void * total_len = 0; } - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (m == NULL) { - ifp->if_ierrors++; - goto done; - } - - if (pktlen > MHLEN - ETHER_ALIGN) { - MCLGET(m, M_DONTWAIT); - if ((m->m_flags & M_EXT) == 0) { - m_freem(m); - ifp->if_ierrors++; - goto done; - } - } - m->m_data += ETHER_ALIGN; - - m_set_rcvif(m, ifp); - m->m_pkthdr.len = m->m_len = pktlen; - m->m_pkthdr.csum_flags = flags; - - memcpy(mtod(m, uint8_t *), buf, pktlen); + usbnet_enqueue(un, buf, pktlen, flags, 0, 0); buf += rxlen; - DPRINTFN(10, "deliver %jd (%#jx)", m->m_len, m->m_len, 0, 0); - - mutex_exit(&sc->axe_rxlock); - - if_percpuq_enqueue((ifp)->if_percpuq, (m)); - - mutex_enter(&sc->axe_rxlock); - if (sc->axe_dying || sc->axe_stopping) { - mutex_exit(&sc->axe_rxlock); - return; - } - } while (total_len > 0); - done: - - if (sc->axe_dying || sc->axe_stopping) { - mutex_exit(&sc->axe_rxlock); - return; - } - - mutex_exit(&sc->axe_rxlock); - - /* Setup new transfer. */ - usbd_setup_xfer(xfer, c, c->axe_buf, sc->axe_bufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof); - usbd_transfer(xfer); - DPRINTFN(10, "start rx", 0, 0, 0, 0); } -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ - -static void -axe_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status) -{ - AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_chain *c = priv; - struct axe_softc *sc = c->axe_sc; - struct axe_cdata *cd = &sc->axe_cdata; - struct ifnet *ifp = &sc->sc_if; - - mutex_enter(&sc->axe_txlock); - if (sc->axe_stopping || sc->axe_dying) { - mutex_exit(&sc->axe_txlock); - return; - } - - KASSERT(cd->axe_tx_cnt == 1); - cd->axe_tx_cnt--; - - sc->axe_timer = 0; - - switch (status) { - case USBD_NOT_STARTED: - case USBD_CANCELLED: - break; - - case USBD_NORMAL_COMPLETION: - ifp->if_opackets++; - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - axe_start_locked(ifp); - break; - - default: - - ifp->if_oerrors++; - if (usbd_ratecheck(&sc->axe_tx_notice)) - aprint_error_dev(sc->axe_dev, "usb error on tx: %s\n", - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]); - break; - } - - mutex_exit(&sc->axe_txlock); -} - -static void -axe_tick(void *xsc) +static unsigned +axe_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_softc *sc = xsc; - - if (sc == NULL) - return; - - mutex_enter(&sc->axe_lock); - if (!sc->axe_stopping && !sc->axe_dying) { - /* Perform periodic stuff in process context */ - usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER); - } - mutex_exit(&sc->axe_lock); - -} - -static void -axe_tick_task(void *xsc) -{ - AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_softc *sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - - if (sc == NULL) - return; - - mutex_enter(&sc->axe_lock); - if (sc->axe_stopping || sc->axe_dying) { - mutex_exit(&sc->axe_lock); - return; - } - - ifp = &sc->sc_if; - mii = &sc->axe_mii; - - if (mii == NULL) { - mutex_exit(&sc->axe_lock); - return; - } - - sc->axe_refcnt++; - mutex_exit(&sc->axe_lock); - - if (sc->axe_timer != 0 && --sc->axe_timer == 0) - axe_watchdog(ifp); - - mii_tick(mii); - - if (sc->axe_link == 0 && - (mii->mii_media_status & IFM_ACTIVE) != 0 && - IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { - DPRINTF("got link", 0, 0, 0, 0); - sc->axe_link++; - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - axe_start(ifp); - } - - - mutex_enter(&sc->axe_lock); - if (--sc->axe_refcnt < 0) - cv_broadcast(&sc->axe_detachcv); - if (!sc->axe_stopping && !sc->axe_dying) - callout_schedule(&sc->axe_stat_ch, hz); - mutex_exit(&sc->axe_lock); -} - -static int -axe_encap(struct axe_softc *sc, struct mbuf *m, int idx) -{ - struct ifnet *ifp = &sc->sc_if; - struct axe_chain *c; - usbd_status err; + struct axe_sframe_hdr hdr, tlr; + size_t hdr_len = 0, tlr_len = 0; int length, boundary; - KASSERT(mutex_owned(&sc->axe_txlock)); - - c = &sc->axe_cdata.axe_tx_chain[idx]; - - /* - * Copy the mbuf data into a contiguous buffer, leaving two - * bytes at the beginning to hold the frame length. - */ - if (AXE_IS_178_FAMILY(sc)) { - struct axe_sframe_hdr hdr; + usbnet_isowned_tx(un); - boundary = (sc->axe_udev->ud_speed == USB_SPEED_HIGH) ? 512 : 64; + if (AXE_IS_178_FAMILY(un)) { + /* + * Copy the mbuf data into a contiguous buffer, leaving two + * bytes at the beginning to hold the frame length. + */ + boundary = (un->un_udev->ud_speed == USB_SPEED_HIGH) ? 512 : 64; hdr.len = htole16(m->m_pkthdr.len); hdr.ilen = ~hdr.len; + hdr_len = sizeof(hdr); - memcpy(c->axe_buf, &hdr, sizeof(hdr)); - length = sizeof(hdr); - - m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length); - length += m->m_pkthdr.len; + length = hdr_len + m->m_pkthdr.len; if ((length % boundary) == 0) { - hdr.len = 0x0000; - hdr.ilen = 0xffff; - memcpy(c->axe_buf + length, &hdr, sizeof(hdr)); - length += sizeof(hdr); + tlr.len = 0x0000; + tlr.ilen = 0xffff; + tlr_len = sizeof(tlr); } - } else { - m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf); - length = m->m_pkthdr.len; + DPRINTFN(20, "length %jx m_pkthdr.len %jx hdrsize %#jx", + length, m->m_pkthdr.len, sizeof(hdr), 0); } - usbd_setup_xfer(c->axe_xfer, c, c->axe_buf, length, - USBD_FORCE_SHORT_XFER, 10000, axe_txeof); - - /* Transmit */ - err = usbd_transfer(c->axe_xfer); - if (err != USBD_IN_PROGRESS) { - /* XXXSMP IFNET_LOCK */ - axe_stop(ifp, 0); - return EIO; - } + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - hdr_len - tlr_len) + return 0; + length = hdr_len + m->m_pkthdr.len + tlr_len; - sc->axe_cdata.axe_tx_cnt++; + if (hdr_len) + memcpy(c->unc_buf, &hdr, hdr_len); + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + hdr_len); + if (tlr_len) + memcpy(c->unc_buf + length - tlr_len, &tlr, tlr_len); - return 0; + return length; } - static void axe_csum_cfg(struct axe_softc *sc) { - struct ifnet *ifp = &sc->sc_if; + struct usbnet * const un = &sc->axe_un; + struct ifnet * const ifp = usbnet_ifp(un); uint16_t csum1, csum2; - if ((sc->axe_flags & AX772B) != 0) { + if ((un->un_flags & AX772B) != 0) { csum1 = 0; csum2 = 0; if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) != 0) @@ -1826,111 +1244,58 @@ axe_csum_cfg(struct axe_softc *sc) } } -static void -axe_start_locked(struct ifnet *ifp) -{ - struct axe_softc *sc = ifp->if_softc; - struct mbuf *m; - struct axe_cdata *cd = &sc->axe_cdata; - - KASSERT(mutex_owned(&sc->axe_txlock)); - - if (cd->axe_tx_cnt != 0) - return; - - if (sc->axe_link == 0 || (ifp->if_flags & IFF_RUNNING) == 0) - return; - - IFQ_POLL(&ifp->if_snd, m); - if (m == NULL) { - return; - } - - if (axe_encap(sc, m, 0)) { - return; - } - IFQ_DEQUEUE(&ifp->if_snd, m); - - /* - * If there's a BPF listener, bounce a copy of this frame - * to him. - */ - bpf_mtap(ifp, m, BPF_D_OUT); - m_freem(m); - - /* - * Set a timeout in case the chip goes out to lunch. - */ - sc->axe_timer = 5; - - return; -} - -static void -axe_start(struct ifnet *ifp) -{ - struct axe_softc * const sc = ifp->if_softc; - - mutex_enter(&sc->axe_txlock); - if (!sc->axe_stopping) - axe_start_locked(ifp); - mutex_exit(&sc->axe_txlock); -} - static int axe_init_locked(struct ifnet *ifp) { AXEHIST_FUNC(); AXEHIST_CALLED(); - struct axe_softc *sc = ifp->if_softc; - struct axe_chain *c; - usbd_status err; + struct usbnet * const un = ifp->if_softc; + struct axe_softc * const sc = usbnet_softc(un); int rxmode; - int i; - KASSERT(mutex_owned(&sc->axe_lock)); + usbnet_isowned(un); - if (sc->axe_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O */ - axe_stop_locked(ifp, 0); + usbnet_stop(un, ifp, 1); - /* Reset the ethernet interface. */ - axe_reset(sc); + usbnet_lock_mii_un_locked(un); - axe_lock_mii_sc_locked(sc); + /* Reset the ethernet interface. */ + axe_reset(un); #if 0 ret = asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5, in_pm); #endif /* Set MAC address and transmitter IPG values. */ - if (AXE_IS_178_FAMILY(sc)) { - axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, sc->axe_enaddr); + if (AXE_IS_178_FAMILY(un)) { + axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, un->un_eaddr); axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2], (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL); } else { - axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, sc->axe_enaddr); + axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, un->un_eaddr); axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL); axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL); axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL); } - if (AXE_IS_178_FAMILY(sc)) { - sc->axe_flags &= ~(AXSTD_FRAME | AXCSUM_FRAME); - if ((sc->axe_flags & AX772B) != 0 && + if (AXE_IS_178_FAMILY(un)) { + un->un_flags &= ~(AXSTD_FRAME | AXCSUM_FRAME); + if ((un->un_flags & AX772B) != 0 && (ifp->if_capenable & AX_RXCSUM) != 0) { sc->sc_lenmask = AXE_CSUM_HDR_LEN_MASK; - sc->axe_flags |= AXCSUM_FRAME; + un->un_flags |= AXCSUM_FRAME; } else { sc->sc_lenmask = AXE_HDR_LEN_MASK; - sc->axe_flags |= AXSTD_FRAME; + un->un_flags |= AXSTD_FRAME; } } /* Configure TX/RX checksum offloading. */ axe_csum_cfg(sc); - if (sc->axe_flags & AX772B) { + if (un->un_flags & AX772B) { /* AX88772B uses different maximum frame burst configuration. */ axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG, ax88772b_mfb_table[AX88772B_MFB_16K].threshold, @@ -1938,8 +1303,8 @@ axe_init_locked(struct ifnet *ifp) } /* Enable receiver, set RX mode */ rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE); - if (AXE_IS_178_FAMILY(sc)) { - if (sc->axe_flags & AX772B) { + if (AXE_IS_178_FAMILY(un)) { + if (un->un_flags & AX772B) { /* * Select RX header format type 1. Aligning IP * header on 4 byte boundary is not needed when @@ -1951,7 +1316,7 @@ axe_init_locked(struct ifnet *ifp) * header size. */ rxmode |= AXE_772B_RXCMD_HDR_TYPE_1; - if (sc->axe_flags & AXCSUM_FRAME) + if (un->un_flags & AXCSUM_FRAME) rxmode |= AXE_772B_RXCMD_IPHDR_ALIGN; } else { /* @@ -1959,7 +1324,7 @@ axe_init_locked(struct ifnet *ifp) * maximum performance. */ #if 0 - if (sc->axe_udev->ud_speed == USB_SPEED_HIGH) { + if (un->un_udev->ud_speed == USB_SPEED_HIGH) { /* Largest possible USB buffer size for AX88178 */ } #endif @@ -1982,70 +1347,21 @@ axe_init_locked(struct ifnet *ifp) axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); /* Load the multicast filter. */ - axe_setmulti_locked(sc); - - axe_unlock_mii_sc_locked(sc); - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axe_ep[AXE_ENDPT_RX]); - if (err) { - aprint_error_dev(sc->axe_dev, "open rx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } + axe_setiff_locked(un); - err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axe_ep[AXE_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->axe_dev, "open tx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } + usbnet_unlock_mii_un_locked(un); - /* Init RX ring. */ - if (axe_rx_list_init(sc) != 0) { - aprint_error_dev(sc->axe_dev, "rx list init failed\n"); - return ENOBUFS; - } - - /* Init TX ring. */ - if (axe_tx_list_init(sc) != 0) { - aprint_error_dev(sc->axe_dev, "tx list init failed\n"); - return ENOBUFS; - } - - mutex_enter(&sc->axe_rxlock); - mutex_enter(&sc->axe_txlock); - sc->axe_stopping = false; - - /* Start up the receive pipe. */ - for (i = 0; i < AXE_RX_LIST_CNT; i++) { - c = &sc->axe_cdata.axe_rx_chain[i]; - usbd_setup_xfer(c->axe_xfer, c, c->axe_buf, sc->axe_bufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof); - usbd_transfer(c->axe_xfer); - } - - mutex_exit(&sc->axe_txlock); - mutex_exit(&sc->axe_rxlock); - - /* Indicate we are up and running. */ - KASSERT(IFNET_LOCKED(ifp)); - ifp->if_flags |= IFF_RUNNING; - - callout_schedule(&sc->axe_stat_ch, hz); - return 0; + return usbnet_init_rx_tx(un); } static int axe_init(struct ifnet *ifp) { - struct axe_softc * const sc = ifp->if_softc; + struct usbnet * const un = ifp->if_softc; - mutex_enter(&sc->axe_lock); + usbnet_lock(un); int ret = axe_init_locked(ifp); - mutex_exit(&sc->axe_lock); + usbnet_unlock(un); return ret; } @@ -2053,206 +1369,32 @@ axe_init(struct ifnet *ifp) static int axe_ioctl(struct ifnet *ifp, u_long cmd, void *data) { - struct axe_softc *sc = ifp->if_softc; - int error = 0; + struct usbnet * const un = ifp->if_softc; switch (cmd) { - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, cmd, data)) != 0) - break; - - switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { - case IFF_RUNNING: - axe_stop(ifp, 1); - break; - case IFF_UP: - axe_init(ifp); - break; - case IFF_UP | IFF_RUNNING: - if ((ifp->if_flags ^ sc->axe_if_flags) == IFF_PROMISC) - axe_setmulti(sc); - else - axe_init(ifp); - break; - } - mutex_enter(&sc->axe_rxlock); - mutex_enter(&sc->axe_txlock); - sc->axe_if_flags = ifp->if_flags; - mutex_exit(&sc->axe_txlock); - mutex_exit(&sc->axe_rxlock); + case SIOCADDMULTI: + case SIOCDELMULTI: + axe_setiff(un); break; - default: - if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) - break; - - error = 0; - - if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) - axe_setmulti(sc); - - } - - return error; -} - -static void -axe_watchdog(struct ifnet *ifp) -{ - struct axe_softc * const sc = ifp->if_softc; - struct axe_chain *c; - usbd_status stat; - - ifp->if_oerrors++; - aprint_error_dev(sc->axe_dev, "watchdog timeout\n"); - - c = &sc->axe_cdata.axe_tx_chain[0]; - usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat); - axe_txeof(c->axe_xfer, c, stat); - - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - axe_start(ifp); -} - -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -static void -axe_stop_locked(struct ifnet *ifp, int disable) -{ - struct axe_softc * const sc = ifp->if_softc; - usbd_status err; - int i; - - KASSERT(mutex_owned(&sc->axe_lock)); - - mutex_enter(&sc->axe_rxlock); - mutex_enter(&sc->axe_txlock); - sc->axe_stopping = true; - mutex_exit(&sc->axe_txlock); - mutex_exit(&sc->axe_rxlock); - - /* - * XXXSMP Would like to - * KASSERT(IFNET_LOCKED(ifp)) - * here but the locking order is: - * ifnet -> sc lock -> rxlock -> txlock - * and sc lock is already held. - */ - ifp->if_flags &= ~IFF_RUNNING; - sc->axe_timer = 0; - - callout_stop(&sc->axe_stat_ch); - sc->axe_link = 0; - - /* Stop transfers. */ - if (sc->axe_ep[AXE_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]); - if (err) { - aprint_error_dev(sc->axe_dev, - "abort rx pipe failed: %s\n", usbd_errstr(err)); - } - } - - if (sc->axe_ep[AXE_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->axe_dev, - "abort tx pipe failed: %s\n", usbd_errstr(err)); - } - } - - if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]); - if (err) { - aprint_error_dev(sc->axe_dev, - "abort intr pipe failed: %s\n", usbd_errstr(err)); - } - } - - axe_reset(sc); - - /* Free RX resources. */ - for (i = 0; i < AXE_RX_LIST_CNT; i++) { - if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) { - usbd_destroy_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer); - sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < AXE_TX_LIST_CNT; i++) { - if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) { - usbd_destroy_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer); - sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL; - } - } - - /* Close pipes. */ - if (sc->axe_ep[AXE_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]); - if (err) { - aprint_error_dev(sc->axe_dev, - "close rx pipe failed: %s\n", usbd_errstr(err)); - } - sc->axe_ep[AXE_ENDPT_RX] = NULL; - } - - if (sc->axe_ep[AXE_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->axe_dev, - "close tx pipe failed: %s\n", usbd_errstr(err)); - } - sc->axe_ep[AXE_ENDPT_TX] = NULL; + break; } - if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]); - if (err) { - aprint_error_dev(sc->axe_dev, - "close intr pipe failed: %s\n", usbd_errstr(err)); - } - sc->axe_ep[AXE_ENDPT_INTR] = NULL; - } + return 0; } static void axe_stop(struct ifnet *ifp, int disable) { - struct axe_softc * const sc = ifp->if_softc; + struct usbnet * const un = ifp->if_softc; - mutex_enter(&sc->axe_lock); - axe_stop_locked(ifp, disable); - mutex_exit(&sc->axe_lock); + usbnet_lock_mii_un_locked(un); + axe_reset(un); + usbnet_unlock_mii_un_locked(un); } -MODULE(MODULE_CLASS_DRIVER, if_axe, NULL); - #ifdef _MODULE #include "ioconf.c" #endif -static int -if_axe_modcmd(modcmd_t cmd, void *aux) -{ - int error = 0; - - switch (cmd) { - case MODULE_CMD_INIT: -#ifdef _MODULE - error = config_init_component(cfdriver_ioconf_axe, - cfattach_ioconf_axe, cfdata_ioconf_axe); -#endif - return error; - case MODULE_CMD_FINI: -#ifdef _MODULE - error = config_fini_component(cfdriver_ioconf_axe, - cfattach_ioconf_axe, cfdata_ioconf_axe); -#endif - return error; - default: - return ENOTTY; - } -} +USBNET_MODULE(axe) Index: usb/if_axen.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_axen.c,v retrieving revision 1.50 diff -p -u -u -r1.50 if_axen.c --- usb/if_axen.c 15 Jul 2019 03:14:22 -0000 1.50 +++ usb/if_axen.c 29 Aug 2019 06:22:05 -0000 @@ -26,40 +26,15 @@ __KERNEL_RCSID(0, "$NetBSD: if_axen.c,v 1.50 2019/07/15 03:14:22 mrg Exp $"); #ifdef _KERNEL_OPT -#include "opt_inet.h" #include "opt_usb.h" #endif #include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -#include #include /* XXX for netinet/ip.h */ #include /* XXX for IP_MAXPACKET */ -#include -#include - -#include -#include -#include -#include -#include +#include #include @@ -72,70 +47,8 @@ int axendebug = 0; #define DPRINTFN(n, x) #endif -struct axen_softc; - -struct axen_chain { - struct axen_softc *axen_sc; - struct usbd_xfer *axen_xfer; - uint8_t *axen_buf; -}; - -struct axen_cdata { - struct axen_chain axen_tx_chain[AXEN_TX_LIST_CNT]; - struct axen_chain axen_rx_chain[AXEN_RX_LIST_CNT]; - int axen_tx_prod; - int axen_tx_cnt; -}; - -struct axen_softc { - device_t axen_dev; - struct ethercom axen_ec; - struct mii_data axen_mii; - krndsource_t rnd_source; - struct usbd_device * axen_udev; - struct usbd_interface * axen_iface; - - uint16_t axen_vendor; - uint16_t axen_product; - uint16_t axen_flags; - uint16_t axen_timer; - - int axen_ed[AXEN_ENDPT_MAX]; - struct usbd_pipe *axen_ep[AXEN_ENDPT_MAX]; - int axen_if_flags; - struct axen_cdata axen_cdata; - struct callout axen_stat_ch; - - int axen_refcnt; - bool axen_dying; - bool axen_stopping; - bool axen_attached; - - struct usb_task axen_tick_task; - - kmutex_t axen_lock; - kmutex_t axen_mii_lock; - kmutex_t axen_rxlock; - kmutex_t axen_txlock; - kcondvar_t axen_detachcv; - - int axen_link; - - int axen_phyno; - struct timeval axen_rx_notice; - struct timeval axen_tx_notice; - u_int axen_rx_bufsz; - u_int axen_tx_bufsz; - int axen_rev; - -#define sc_if axen_ec.ec_if -}; - -#define GET_MII(sc) (&(sc)->axen_mii) -#define GET_IFP(sc) (&(sc)->sc_if) - struct axen_type { - struct usb_devno axen_dev; + struct usb_devno axen_devno; uint16_t axen_flags; #define AX178A 0x0001 /* AX88178a */ #define AX179 0x0002 /* AX88179 */ @@ -156,94 +69,45 @@ static const struct axen_type axen_devs[ static int axen_match(device_t, cfdata_t, void *); static void axen_attach(device_t, device_t, void *); -static int axen_detach(device_t, int); -static int axen_activate(device_t, devact_t); -CFATTACH_DECL_NEW(axen, sizeof(struct axen_softc), - axen_match, axen_attach, axen_detach, axen_activate); +CFATTACH_DECL_NEW(axen, sizeof(struct usbnet), + axen_match, axen_attach, usbnet_detach, usbnet_activate); -static int axen_tx_list_init(struct axen_softc *); -static int axen_rx_list_init(struct axen_softc *); -static struct mbuf *axen_newbuf(void); -static int axen_encap(struct axen_softc *, struct mbuf *, int); -static void axen_rxeof(struct usbd_xfer *, void *, usbd_status); -static int axen_csum_flags_rx(struct ifnet *, uint32_t); -static void axen_txeof(struct usbd_xfer *, void *, usbd_status); -static void axen_tick(void *); -static void axen_tick_task(void *); -static void axen_start(struct ifnet *); -static void axen_start_locked(struct ifnet *); -static int axen_ioctl(struct ifnet *, u_long, void *); +static int axen_cmd(struct usbnet *, int, int, int, void *); +static void axen_reset(struct usbnet *); +static int axen_get_eaddr(struct usbnet *, void *); +static void axen_ax88179_init(struct usbnet *); + +static void axen_stop_cb(struct ifnet *, int); +static int axen_ioctl_cb(struct ifnet *, u_long, void *); +static int axen_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static int axen_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void axen_mii_statchg(struct ifnet *); +static void axen_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned axen_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); static int axen_init(struct ifnet *); -static void axen_stop(struct ifnet *, int); -static void axen_stop_locked(struct ifnet *, int); -static void axen_watchdog(struct ifnet *); -static int axen_miibus_readreg(device_t, int, int, uint16_t *); -static int axen_miibus_writereg(device_t, int, int, uint16_t); -static void axen_miibus_statchg(struct ifnet *); -static int axen_cmd(struct axen_softc *, int, int, int, void *); -static int axen_ifmedia_upd(struct ifnet *); -static void axen_ifmedia_sts(struct ifnet *, struct ifmediareq *); -static void axen_reset(struct axen_softc *); -static int axen_get_eaddr(struct axen_softc *, void *); -static void axen_iff(struct axen_softc *); -static void axen_ax88179_init(struct axen_softc *); -static void axen_setcoe(struct axen_softc *); -/* - * Access functions for MII. Take the MII lock to call axen_cmd(). - * Two forms: softc lock currently held or not. - */ -static void -axen_lock_mii(struct axen_softc *sc) -{ - - mutex_enter(&sc->axen_lock); - sc->axen_refcnt++; - mutex_exit(&sc->axen_lock); - - mutex_enter(&sc->axen_mii_lock); -} - -static void -axen_lock_mii_sc_locked(struct axen_softc *sc) -{ - KASSERT(mutex_owned(&sc->axen_lock)); - - sc->axen_refcnt++; - mutex_enter(&sc->axen_mii_lock); -} - -static void -axen_unlock_mii(struct axen_softc *sc) -{ - - mutex_exit(&sc->axen_mii_lock); - mutex_enter(&sc->axen_lock); - if (--sc->axen_refcnt < 0) - cv_broadcast(&sc->axen_detachcv); - mutex_exit(&sc->axen_lock); -} - -static void -axen_unlock_mii_sc_locked(struct axen_softc *sc) -{ - KASSERT(mutex_owned(&sc->axen_lock)); - - mutex_exit(&sc->axen_mii_lock); - if (--sc->axen_refcnt < 0) - cv_broadcast(&sc->axen_detachcv); -} +static struct usbnet_ops axen_ops = { + .uno_stop = axen_stop_cb, + .uno_ioctl = axen_ioctl_cb, + .uno_read_reg = axen_mii_read_reg, + .uno_write_reg = axen_mii_write_reg, + .uno_statchg = axen_mii_statchg, + .uno_tx_prepare = axen_tx_prepare, + .uno_rx_loop = axen_rx_loop, + .uno_init = axen_init, +}; static int -axen_cmd(struct axen_softc *sc, int cmd, int index, int val, void *buf) +axen_cmd(struct usbnet *un, int cmd, int index, int val, void *buf) { usb_device_request_t req; usbd_status err; - KASSERT(mutex_owned(&sc->axen_mii_lock)); + usbnet_isowned_mii(un); - if (sc->axen_dying) + if (usbnet_isdying(un)) return 0; if (AXEN_CMD_DIR(cmd)) @@ -255,7 +119,7 @@ axen_cmd(struct axen_softc *sc, int cmd, USETW(req.wIndex, index); USETW(req.wLength, AXEN_CMD_LEN(cmd)); - err = usbd_do_request(sc->axen_udev, &req, buf); + err = usbd_do_request(un->un_udev, &req, buf); DPRINTFN(5, ("axen_cmd: cmd 0x%04x val 0x%04x len %d\n", cmd, val, AXEN_CMD_LEN(cmd))); @@ -268,92 +132,60 @@ axen_cmd(struct axen_softc *sc, int cmd, } static int -axen_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) +axen_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - struct axen_softc * const sc = device_private(dev); - usbd_status err; uint16_t data; - mutex_enter(&sc->axen_lock); - if (sc->axen_dying || sc->axen_phyno != phy) { - mutex_exit(&sc->axen_lock); - return -1; - } - mutex_exit(&sc->axen_lock); - - axen_lock_mii(sc); - err = axen_cmd(sc, AXEN_CMD_MII_READ_REG, reg, phy, &data); - axen_unlock_mii(sc); + if (un->un_phyno != phy) + return EINVAL; - if (err) { - aprint_error_dev(sc->axen_dev, "read PHY failed: %d\n", err); - return err; - } + usbd_status err = axen_cmd(un, AXEN_CMD_MII_READ_REG, reg, phy, &data); + if (err) + return EIO; *val = le16toh(data); - DPRINTFN(2,("axen_miibus_readreg: phy 0x%x reg 0x%x val 0x%hx\n", - phy, reg, *val)); - - if (reg == MII_BMSR) { + if (reg == MII_BMSR) *val &= ~BMSR_EXTCAP; - } return 0; } static int -axen_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +axen_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct axen_softc * const sc = device_private(dev); - usbd_status err; - uint16_t uval; - - mutex_enter(&sc->axen_lock); - if (sc->axen_dying || sc->axen_phyno != phy) { - mutex_exit(&sc->axen_lock); - return -1; - } - mutex_exit(&sc->axen_lock); - - uval = htole16(val); + uint16_t uval = htole16(val); - axen_lock_mii(sc); - err = axen_cmd(sc, AXEN_CMD_MII_WRITE_REG, reg, phy, &uval); - axen_unlock_mii(sc); - - DPRINTFN(2, ("axen_miibus_writereg: phy 0x%x reg 0x%x val 0x%04hx\n", - phy, reg, val)); + if (un->un_phyno != phy) + return EINVAL; - if (err) { - aprint_error_dev(sc->axen_dev, "write PHY failed: %d\n", err); - return err; - } + usbd_status err = axen_cmd(un, AXEN_CMD_MII_WRITE_REG, reg, phy, &uval); + if (err) + return EIO; return 0; } static void -axen_miibus_statchg(struct ifnet *ifp) +axen_mii_statchg(struct ifnet *ifp) { - struct axen_softc * const sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); + struct usbnet * const un = ifp->if_softc; + struct mii_data * const mii = usbnet_mii(un); int err; uint16_t val; uint16_t wval; - if (sc->axen_dying) + if (usbnet_isdying(un)) return; - sc->axen_link = 0; if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == (IFM_ACTIVE | IFM_AVALID)) { switch (IFM_SUBTYPE(mii->mii_media_active)) { case IFM_10_T: case IFM_100_TX: - sc->axen_link++; + usbnet_set_link(un, true); break; case IFM_1000_T: - sc->axen_link++; + usbnet_set_link(un, true); break; default: break; @@ -361,7 +193,7 @@ axen_miibus_statchg(struct ifnet *ifp) } /* Lost link, do nothing. */ - if (sc->axen_link == 0) + if (!usbnet_havelink(un)) return; val = 0; @@ -384,58 +216,18 @@ axen_miibus_statchg(struct ifnet *ifp) DPRINTF(("%s: val=0x%x\n", __func__, val)); wval = htole16(val); - axen_lock_mii(sc); - err = axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MEDIUM_STATUS, &wval); - axen_unlock_mii(sc); - if (err) { - aprint_error_dev(sc->axen_dev, "media change failed\n"); - return; - } -} - -/* - * Set media options. - */ -static int -axen_ifmedia_upd(struct ifnet *ifp) -{ - struct axen_softc * const sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); - int rc; - - sc->axen_link = 0; - - if (mii->mii_instance) { - struct mii_softc *miisc; - - LIST_FOREACH(miisc, &mii->mii_phys, mii_list) - mii_phy_reset(miisc); - } - - if ((rc = mii_mediachg(mii)) == ENXIO) - return 0; - return rc; -} - -/* - * Report current media status. - */ -static void -axen_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) -{ - struct axen_softc * const sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); - - mii_pollstat(mii); - ifmr->ifm_active = mii->mii_media_active; - ifmr->ifm_status = mii->mii_media_status; + usbnet_lock_mii(un); + err = axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_MEDIUM_STATUS, &wval); + usbnet_unlock_mii(un); + if (err) + aprint_error_dev(un->un_dev, "media change failed\n"); } static void -axen_iff_locked(struct axen_softc *sc) +axen_setiff_locked(struct usbnet *un) { - struct ifnet *ifp = GET_IFP(sc); - struct ethercom *ec = &sc->axen_ec; + struct ifnet * const ifp = usbnet_ifp(un); + struct ethercom *ec = usbnet_ec(un); struct ether_multi *enm; struct ether_multistep step; uint32_t h = 0; @@ -443,21 +235,21 @@ axen_iff_locked(struct axen_softc *sc) uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; uint16_t wval; - if (sc->axen_dying) + if (usbnet_isdying(un)) return; - KASSERT(mutex_owned(&sc->axen_mii_lock)); + usbnet_isowned_mii(un); rxmode = 0; /* Enable receiver, set RX mode */ - axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval); + axen_cmd(un, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval); rxmode = le16toh(wval); rxmode &= ~(AXEN_RXCTL_ACPT_ALL_MCAST | AXEN_RXCTL_PROMISC | AXEN_RXCTL_ACPT_MCAST); if (ifp->if_flags & IFF_PROMISC) { - DPRINTF(("%s: promisc\n", device_xname(sc->axen_dev))); + DPRINTF(("%s: promisc\n", device_xname(un->un_dev))); rxmode |= AXEN_RXCTL_PROMISC; allmulti: ETHER_LOCK(ec); @@ -468,7 +260,7 @@ allmulti: } else { /* now program new ones */ DPRINTF(("%s: initializing hash table\n", - device_xname(sc->axen_dev))); + device_xname(un->un_dev))); ETHER_LOCK(ec); ec->ec_flags &= ~ETHER_F_ALLMULTI; @@ -477,7 +269,7 @@ allmulti: if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { DPRINTF(("%s: allmulti\n", - device_xname(sc->axen_dev))); + device_xname(un->un_dev))); memset(hashtbl, 0, sizeof(hashtbl)); ETHER_UNLOCK(ec); goto allmulti; @@ -486,7 +278,7 @@ allmulti: ETHER_ADDR_LEN) >> 26; hashtbl[h / 8] |= 1 << (h % 8); DPRINTF(("%s: %s added\n", - device_xname(sc->axen_dev), + device_xname(un->un_dev), ether_sprintf(enm->enm_addrlo))); ETHER_NEXT_MULTI(step, enm); } @@ -494,26 +286,24 @@ allmulti: rxmode |= AXEN_RXCTL_ACPT_MCAST; } - axen_cmd(sc, AXEN_CMD_MAC_WRITE_FILTER, 8, AXEN_FILTER_MULTI, hashtbl); + axen_cmd(un, AXEN_CMD_MAC_WRITE_FILTER, 8, AXEN_FILTER_MULTI, hashtbl); wval = htole16(rxmode); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); } static void -axen_iff(struct axen_softc *sc) +axen_setiff(struct usbnet *un) { - - axen_lock_mii(sc); - axen_iff_locked(sc); - axen_unlock_mii(sc); + usbnet_lock_mii(un); + axen_setiff_locked(un); + usbnet_unlock_mii(un); } static void -axen_reset(struct axen_softc *sc) +axen_reset(struct usbnet *un) { - - KASSERT(mutex_owned(&sc->axen_lock)); - if (sc->axen_dying) + usbnet_isowned(un); + if (usbnet_isdying(un)) return; /* XXX What to reset? */ @@ -521,16 +311,11 @@ axen_reset(struct axen_softc *sc) DELAY(1000); } -#define AXEN_GPIO_WRITE(x, y) do { \ - axen_cmd(sc, AXEN_CMD_WRITE_GPIO, 0, (x), NULL); \ - usbd_delay_ms(sc->axen_udev, (y)); \ -} while (/*CONSTCOND*/0) - static int -axen_get_eaddr(struct axen_softc *sc, void *addr) +axen_get_eaddr(struct usbnet *un, void *addr) { #if 1 - return axen_cmd(sc, AXEN_CMD_MAC_READ_ETHER, 6, AXEN_CMD_MAC_NODE_ID, + return axen_cmd(un, AXEN_CMD_MAC_READ_ETHER, 6, AXEN_CMD_MAC_NODE_ID, addr); #else int i, retry; @@ -541,18 +326,18 @@ axen_get_eaddr(struct axen_softc *sc, vo for (i = 0; i < 6; i++) { /* set eeprom address */ buf = htole16(i); - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_ADDR, &buf); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_ADDR, &buf); /* set eeprom command */ buf = htole16(AXEN_EEPROM_READ); - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_CMD, &buf); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_CMD, &buf); /* check the value is ready */ retry = 3; do { buf = htole16(AXEN_EEPROM_READ); - usbd_delay_ms(sc->axen_udev, 10); - axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_MAC_EEPROM_CMD, + usbd_delay_ms(un->un_udev, 10); + axen_cmd(un, AXEN_CMD_MAC_READ, 1, AXEN_MAC_EEPROM_CMD, &buf); retry--; if (retry < 0) @@ -560,7 +345,7 @@ axen_get_eaddr(struct axen_softc *sc, vo } while ((le16toh(buf) & 0xff) & AXEN_EEPROM_BUSY); /* read data */ - axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_EEPROM_READ, + axen_cmd(un, AXEN_CMD_MAC_READ2, 2, AXEN_EEPROM_READ, &eeprom[i * 2]); /* sanity check */ @@ -572,7 +357,7 @@ axen_get_eaddr(struct axen_softc *sc, vo csum = eeprom[6] + eeprom[7] + eeprom[8] + eeprom[9]; csum = (csum >> 8) + (csum & 0xff) + eeprom[10]; if (csum != 0xff) { - printf("eeprom checksum mismatchi(0x%02x)\n", csum); + printf("eeprom checksum mismatch(0x%02x)\n", csum); return EINVAL; } @@ -582,71 +367,69 @@ axen_get_eaddr(struct axen_softc *sc, vo } static void -axen_ax88179_init(struct axen_softc *sc) +axen_ax88179_init(struct usbnet *un) { struct axen_qctrl qctrl; uint16_t ctl, temp; uint16_t wval; uint8_t val; - axen_lock_mii(sc); + usbnet_lock_mii(un); /* XXX: ? */ - axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_UNK_05, &val); + axen_cmd(un, AXEN_CMD_MAC_READ, 1, AXEN_UNK_05, &val); DPRINTFN(5, ("AXEN_CMD_MAC_READ(0x05): 0x%02x\n", val)); /* check AX88179 version, UA1 / UA2 */ - axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_GENERAL_STATUS, &val); + axen_cmd(un, AXEN_CMD_MAC_READ, 1, AXEN_GENERAL_STATUS, &val); /* UA1 */ if (!(val & AXEN_GENERAL_STATUS_MASK)) { - sc->axen_rev = AXEN_REV_UA1; DPRINTF(("AX88179 ver. UA1\n")); } else { - sc->axen_rev = AXEN_REV_UA2; DPRINTF(("AX88179 ver. UA2\n")); } /* power up ethernet PHY */ wval = htole16(0); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval); + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval); wval = htole16(AXEN_PHYPWR_RSTCTL_IPRL); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval); - usbd_delay_ms(sc->axen_udev, 200); + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval); + usbd_delay_ms(un->un_udev, 200); /* set clock mode */ val = AXEN_PHYCLK_ACS | AXEN_PHYCLK_BCS; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val); - usbd_delay_ms(sc->axen_udev, 100); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val); + usbd_delay_ms(un->un_udev, 100); /* set monitor mode (disable) */ val = AXEN_MONITOR_NONE; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val); /* enable auto detach */ - axen_cmd(sc, AXEN_CMD_EEPROM_READ, 2, AXEN_EEPROM_STAT, &wval); + axen_cmd(un, AXEN_CMD_EEPROM_READ, 2, AXEN_EEPROM_STAT, &wval); temp = le16toh(wval); DPRINTFN(2,("EEPROM0x43 = 0x%04x\n", temp)); if (!(temp == 0xffff) && !(temp & 0x0100)) { /* Enable auto detach bit */ val = 0; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val); val = AXEN_PHYCLK_ULR; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val); - usbd_delay_ms(sc->axen_udev, 100); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val); + usbd_delay_ms(un->un_udev, 100); - axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_PHYPWR_RSTCTL, &wval); + axen_cmd(un, AXEN_CMD_MAC_READ2, 2, AXEN_PHYPWR_RSTCTL, &wval); ctl = le16toh(wval); ctl |= AXEN_PHYPWR_RSTCTL_AUTODETACH; wval = htole16(ctl); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval); - usbd_delay_ms(sc->axen_udev, 200); - aprint_error_dev(sc->axen_dev, "enable auto detach (0x%04x)\n", + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval); + usbd_delay_ms(un->un_udev, 200); + aprint_error_dev(un->un_dev, "enable auto detach (0x%04x)\n", ctl); } /* bulkin queue setting */ - axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_USB_UPLINK, &val); + axen_cmd(un, AXEN_CMD_MAC_READ, 1, AXEN_USB_UPLINK, &val); switch (val) { case AXEN_USB_FS: DPRINTF(("uplink: USB1.1\n")); @@ -673,12 +456,12 @@ axen_ax88179_init(struct axen_softc *sc) qctrl.ifg = 0xff; break; default: - aprint_error_dev(sc->axen_dev, "unknown uplink bus:0x%02x\n", + aprint_error_dev(un->un_dev, "unknown uplink bus:0x%02x\n", val); - axen_unlock_mii(sc); + usbnet_unlock_mii(un); return; } - axen_cmd(sc, AXEN_CMD_MAC_SET_RXSR, 5, AXEN_RX_BULKIN_QCTRL, &qctrl); + axen_cmd(un, AXEN_CMD_MAC_SET_RXSR, 5, AXEN_RX_BULKIN_QCTRL, &qctrl); /* * set buffer high/low watermark to pause/resume. @@ -687,20 +470,20 @@ axen_ax88179_init(struct axen_softc *sc) * watermark parameters. */ val = 0x34; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_LOW_WATERMARK, &val); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_LOW_WATERMARK, &val); val = 0x52; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_HIGH_WATERMARK, &val); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_HIGH_WATERMARK, &val); /* Set RX/TX configuration. */ /* Set RX control register */ ctl = AXEN_RXCTL_IPE | AXEN_RXCTL_DROPCRCERR | AXEN_RXCTL_AUTOB; wval = htole16(ctl); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); /* set monitor mode (enable) */ val = AXEN_MONITOR_PMETYPE | AXEN_MONITOR_PMEPOL | AXEN_MONITOR_RWMP; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val); - axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_MONITOR_MODE, &val); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val); + axen_cmd(un, AXEN_CMD_MAC_READ, 1, AXEN_MONITOR_MODE, &val); DPRINTF(("axen: Monitor mode = 0x%02x\n", val)); /* set medium type */ @@ -709,43 +492,42 @@ axen_ax88179_init(struct axen_softc *sc) AXEN_MEDIUM_RECV_EN; wval = htole16(ctl); DPRINTF(("axen: set to medium mode: 0x%04x\n", ctl)); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MEDIUM_STATUS, &wval); - usbd_delay_ms(sc->axen_udev, 100); + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_MEDIUM_STATUS, &wval); + usbd_delay_ms(un->un_udev, 100); - axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MEDIUM_STATUS, &wval); + axen_cmd(un, AXEN_CMD_MAC_READ2, 2, AXEN_MEDIUM_STATUS, &wval); DPRINTF(("axen: current medium mode: 0x%04x\n", le16toh(wval))); - axen_unlock_mii(sc); + usbnet_unlock_mii(un); #if 0 /* XXX: TBD.... */ #define GMII_LED_ACTIVE 0x1a #define GMII_PHY_PAGE_SEL 0x1e #define GMII_PHY_PAGE_SEL 0x1f #define GMII_PAGE_EXT 0x0007 - axen_miibus_writereg(&sc->axen_dev, sc->axen_phyno, GMII_PHY_PAGE_SEL, + usbnet_mii_writereg(un->un_dev, un->un_phyno, GMII_PHY_PAGE_SEL, GMII_PAGE_EXT); - axen_miibus_writereg(&sc->axen_dev, sc->axen_phyno, GMII_PHY_PAGE, + usbnet_mii_writereg(un->un_dev, un->un_phyno, GMII_PHY_PAGE, 0x002c); #endif #if 1 /* XXX: phy hack ? */ - axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x1F, 0x0005); - axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x0C, 0x0000); - axen_miibus_readreg(sc->axen_dev, sc->axen_phyno, 0x0001, &wval); - axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x01, - wval | 0x0080); - axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x1F, 0x0000); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x1F, 0x0005); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x0C, 0x0000); + usbnet_mii_readreg(un->un_dev, un->un_phyno, 0x0001, &wval); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x01, wval | 0x0080); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x1F, 0x0000); #endif } static void -axen_setcoe(struct axen_softc *sc) +axen_setoe_locked(struct usbnet *un) { - struct ifnet *ifp = GET_IFP(sc); + struct ifnet * const ifp = usbnet_ifp(un); uint64_t enabled = ifp->if_capenable; uint8_t val; - KASSERT(mutex_owned(&sc->axen_mii_lock)); + usbnet_isowned_mii(un); val = AXEN_RXCOE_OFF; if (enabled & IFCAP_CSUM_IPv4_Rx) @@ -758,7 +540,7 @@ axen_setcoe(struct axen_softc *sc) val |= AXEN_RXCOE_TCPv6; if (enabled & IFCAP_CSUM_UDPv6_Rx) val |= AXEN_RXCOE_UDPv6; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_RX_COE, &val); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_RX_COE, &val); val = AXEN_TXCOE_OFF; if (enabled & IFCAP_CSUM_IPv4_Tx) @@ -771,7 +553,38 @@ axen_setcoe(struct axen_softc *sc) val |= AXEN_TXCOE_TCPv6; if (enabled & IFCAP_CSUM_UDPv6_Tx) val |= AXEN_TXCOE_UDPv6; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_TX_COE, &val); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_TX_COE, &val); +} + +static void +axen_setoe(struct usbnet *un) +{ + + usbnet_lock_mii(un); + axen_setoe_locked(un); + usbnet_unlock_mii(un); +} + +static int +axen_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) +{ + struct usbnet * const un = ifp->if_softc; + + switch (cmd) { + case SIOCSIFFLAGS: + case SIOCSETHERCAP: + case SIOCADDMULTI: + case SIOCDELMULTI: + axen_setiff(un); + break; + case SIOCSIFCAP: + axen_setoe(un); + break; + default: + break; + } + + return 0; } static int @@ -786,29 +599,32 @@ axen_match(device_t parent, cfdata_t mat static void axen_attach(device_t parent, device_t self, void *aux) { - struct axen_softc * const sc = device_private(self); + USBNET_MII_DECL_DEFAULT(unm); + struct usbnet * const un = device_private(self); struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; usbd_status err; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; - struct mii_data *mii; - uint8_t eaddr[ETHER_ADDR_LEN]; char *devinfop; - const char *devname = device_xname(self); - struct ifnet *ifp; + uint16_t axen_flags; int i; aprint_naive("\n"); aprint_normal("\n"); - - sc->axen_dev = self; - sc->axen_udev = dev; - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = un; + un->un_ops = &axen_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = AXEN_RX_LIST_CNT; + un->un_tx_list_cnt = AXEN_TX_LIST_CNT; + err = usbd_set_config_no(dev, AXEN_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -816,109 +632,81 @@ axen_attach(device_t parent, device_t se return; } - sc->axen_flags = axen_lookup(uaa->uaa_vendor, uaa->uaa_product)->axen_flags; + axen_flags = axen_lookup(uaa->uaa_vendor, uaa->uaa_product)->axen_flags; - usb_init_task(&sc->axen_tick_task, axen_tick_task, sc, USB_TASKQ_MPSAFE); - - err = usbd_device2interface_handle(dev, AXEN_IFACE_IDX,&sc->axen_iface); + err = usbd_device2interface_handle(dev, AXEN_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->axen_product = uaa->uaa_product; - sc->axen_vendor = uaa->uaa_vendor; - - id = usbd_get_interface_descriptor(sc->axen_iface); - /* decide on what our bufsize will be */ - switch (sc->axen_udev->ud_speed) { + switch (dev->ud_speed) { case USB_SPEED_SUPER: - sc->axen_rx_bufsz = AXEN_BUFSZ_SS * 1024; + un->un_rx_bufsz = AXEN_BUFSZ_SS * 1024; break; case USB_SPEED_HIGH: - sc->axen_rx_bufsz = AXEN_BUFSZ_HS * 1024; + un->un_rx_bufsz = AXEN_BUFSZ_HS * 1024; break; default: - sc->axen_rx_bufsz = AXEN_BUFSZ_LS * 1024; + un->un_rx_bufsz = AXEN_BUFSZ_LS * 1024; break; } - - sc->axen_tx_bufsz = IP_MAXPACKET + - ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN + - sizeof(struct axen_sframe_hdr); + un->un_tx_bufsz = IP_MAXPACKET + ETHER_HDR_LEN + ETHER_CRC_LEN + + ETHER_VLAN_ENCAP_LEN + sizeof(struct axen_sframe_hdr); /* Find endpoints. */ + id = usbd_get_interface_descriptor(un->un_iface); for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->axen_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (!ed) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->axen_ed[AXEN_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->axen_ed[AXEN_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; +#if 0 /* not used yet */ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->axen_ed[AXEN_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; +#endif } } /* Set these up now for axen_cmd(). */ - mutex_init(&sc->axen_mii_lock, MUTEX_DEFAULT, IPL_NONE); - mutex_init(&sc->axen_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->axen_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->axen_lock, MUTEX_DEFAULT, IPL_NONE); - cv_init(&sc->axen_detachcv, "axendet"); + usbnet_attach(un, "axendet"); - sc->axen_phyno = AXEN_PHY_ID; - DPRINTF(("%s: phyno %d\n", device_xname(self), sc->axen_phyno)); + un->un_phyno = AXEN_PHY_ID; + DPRINTF(("%s: phyno %d\n", device_xname(self), un->un_phyno)); /* Get station address. */ - axen_lock_mii(sc); - if (axen_get_eaddr(sc, &eaddr)) { - axen_unlock_mii(sc); + usbnet_lock_mii(un); + if (axen_get_eaddr(un, &un->un_eaddr)) { + usbnet_unlock_mii(un); printf("EEPROM checksum error\n"); - cv_destroy(&sc->axen_detachcv); - mutex_destroy(&sc->axen_lock); - mutex_destroy(&sc->axen_rxlock); - mutex_destroy(&sc->axen_txlock); - mutex_destroy(&sc->axen_mii_lock); return; } - axen_unlock_mii(sc); + usbnet_unlock_mii(un); - axen_ax88179_init(sc); + axen_ax88179_init(un); - /* - * An ASIX chip was detected. Inform the world. - */ - if (sc->axen_flags & AX178A) + /* An ASIX chip was detected. Inform the world. */ + if (axen_flags & AX178A) aprint_normal_dev(self, "AX88178a\n"); - else if (sc->axen_flags & AX179) + else if (axen_flags & AX179) aprint_normal_dev(self, "AX88179\n"); - aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); - - /* Initialize interface info. */ - - ifp = &sc->sc_if; - ifp->if_softc = sc; - strlcpy(ifp->if_xname, devname, IFNAMSIZ); - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_extflags = IFEF_MPSAFE; - ifp->if_ioctl = axen_ioctl; - ifp->if_start = axen_start; - ifp->if_init = axen_init; - ifp->if_stop = axen_stop; - - IFQ_SET_READY(&ifp->if_snd); + else + aprint_normal_dev(self, "(unknown)\n"); - sc->axen_ec.ec_capabilities = ETHERCAP_VLAN_MTU; + struct ethercom *ec = usbnet_ec(un); + ec->ec_capabilities = ETHERCAP_VLAN_MTU; /* Adapter does not support TSOv6 (They call it LSOv2). */ + struct ifnet *ifp = usbnet_ifp(un); ifp->if_capabilities |= IFCAP_TSOv4 | IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_TCPv4_Tx | @@ -926,252 +714,72 @@ axen_attach(device_t parent, device_t se IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Rx | IFCAP_CSUM_UDPv6_Tx; - /* Initialize MII/media info. */ - mii = &sc->axen_mii; - mii->mii_ifp = ifp; - mii->mii_readreg = axen_miibus_readreg; - mii->mii_writereg = axen_miibus_writereg; - mii->mii_statchg = axen_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - - sc->axen_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, axen_ifmedia_upd, axen_ifmedia_sts); - mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); - - if (LIST_FIRST(&mii->mii_phys) == NULL) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, eaddr); - rnd_attach_source(&sc->rnd_source, device_xname(sc->axen_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&sc->axen_stat_ch, CALLOUT_MPSAFE); - callout_setfunc(&sc->axen_stat_ch, axen_tick, sc); - - sc->axen_attached = true; - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axen_udev,sc->axen_dev); - - if (!pmf_device_register(self, NULL, NULL)) - aprint_error_dev(self, "couldn't establish power handler\n"); + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, &unm); } static int -axen_detach(device_t self, int flags) +axen_csum_flags_rx(struct ifnet *ifp, uint32_t pkt_hdr) { - struct axen_softc * const sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - - mutex_enter(&sc->axen_lock); - sc->axen_dying = true; - mutex_exit(&sc->axen_lock); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__)); + int enabled_flags = ifp->if_csum_flags_rx; + int csum_flags = 0; + int l3_type, l4_type; - /* Detached before attached finished, so just bail out. */ - if (!sc->axen_attached) + if (enabled_flags == 0) return 0; - pmf_device_deregister(self); - - callout_halt(&sc->axen_stat_ch, NULL); - usb_rem_task_wait(sc->axen_udev, &sc->axen_tick_task, - USB_TASKQ_DRIVER, NULL); - - if (ifp->if_flags & IFF_RUNNING) { - IFNET_LOCK(ifp); - axen_stop(ifp, 1); - IFNET_UNLOCK(ifp); - } - - mutex_enter(&sc->axen_lock); - sc->axen_refcnt--; - while (sc->axen_refcnt > 0) { - /* Wait for processes to go away */ - cv_wait(&sc->axen_detachcv, &sc->axen_lock); - } - -#ifdef DIAGNOSTIC - if (sc->axen_ep[AXEN_ENDPT_TX] != NULL || - sc->axen_ep[AXEN_ENDPT_RX] != NULL || - sc->axen_ep[AXEN_ENDPT_INTR] != NULL) - aprint_debug_dev(self, "detach has active endpoints\n"); -#endif - - mutex_exit(&sc->axen_lock); - - callout_destroy(&sc->axen_stat_ch); - rnd_detach_source(&sc->rnd_source); - mii_detach(&sc->axen_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->axen_mii.mii_media, IFM_INST_ANY); - ether_ifdetach(ifp); - if_detach(ifp); - - sc->axen_attached = false; - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axen_udev,sc->axen_dev); - - cv_destroy(&sc->axen_detachcv); - mutex_destroy(&sc->axen_lock); - mutex_destroy(&sc->axen_rxlock); - mutex_destroy(&sc->axen_txlock); - mutex_destroy(&sc->axen_mii_lock); - - return 0; -} - -static int -axen_activate(device_t self, devact_t act) -{ - struct axen_softc * const sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); + l3_type = (pkt_hdr & AXEN_RXHDR_L3_TYPE_MASK) >> + AXEN_RXHDR_L3_TYPE_OFFSET; - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__)); + if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4) + csum_flags |= M_CSUM_IPv4; - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(ifp); - - mutex_enter(&sc->axen_lock); - sc->axen_dying = true; - mutex_exit(&sc->axen_lock); - - mutex_enter(&sc->axen_rxlock); - mutex_enter(&sc->axen_txlock); - sc->axen_stopping = true; - mutex_exit(&sc->axen_txlock); - mutex_exit(&sc->axen_rxlock); + l4_type = (pkt_hdr & AXEN_RXHDR_L4_TYPE_MASK) >> + AXEN_RXHDR_L4_TYPE_OFFSET; - return 0; + switch (l4_type) { + case AXEN_RXHDR_L4_TYPE_TCP: + if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4) + csum_flags |= M_CSUM_TCPv4; + else + csum_flags |= M_CSUM_TCPv6; + break; + case AXEN_RXHDR_L4_TYPE_UDP: + if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4) + csum_flags |= M_CSUM_UDPv4; + else + csum_flags |= M_CSUM_UDPv6; + break; default: - return EOPNOTSUPP; - } -} - -static struct mbuf * -axen_newbuf(void) -{ - struct mbuf *m; - - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (m == NULL) - return NULL; - - MCLGET(m, M_DONTWAIT); - if (!(m->m_flags & M_EXT)) { - m_freem(m); - return NULL; - } - - m->m_len = m->m_pkthdr.len = MCLBYTES; - m_adj(m, ETHER_ALIGN); - - return m; -} - -static int -axen_rx_list_init(struct axen_softc *sc) -{ - struct axen_cdata *cd; - struct axen_chain *c; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->axen_dev), __func__)); - - cd = &sc->axen_cdata; - for (i = 0; i < AXEN_RX_LIST_CNT; i++) { - c = &cd->axen_rx_chain[i]; - c->axen_sc = sc; - if (c->axen_xfer == NULL) { - int err = usbd_create_xfer(sc->axen_ep[AXEN_ENDPT_RX], - sc->axen_rx_bufsz, 0, 0, &c->axen_xfer); - if (err) - return err; - c->axen_buf = usbd_get_buffer(c->axen_xfer); - } - } - - return 0; -} - -static int -axen_tx_list_init(struct axen_softc *sc) -{ - struct axen_cdata *cd; - struct axen_chain *c; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->axen_dev), __func__)); - - cd = &sc->axen_cdata; - for (i = 0; i < AXEN_TX_LIST_CNT; i++) { - c = &cd->axen_tx_chain[i]; - c->axen_sc = sc; - if (c->axen_xfer == NULL) { - int err = usbd_create_xfer(sc->axen_ep[AXEN_ENDPT_TX], - sc->axen_tx_bufsz, USBD_FORCE_SHORT_XFER, 0, - &c->axen_xfer); - if (err) - return err; - c->axen_buf = usbd_get_buffer(c->axen_xfer); - } + break; } - cd->axen_tx_prod = cd->axen_tx_cnt = 0; + csum_flags &= enabled_flags; + if ((csum_flags & M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L3CSUM_ERR)) + csum_flags |= M_CSUM_IPv4_BAD; + if ((csum_flags & ~M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L4CSUM_ERR)) + csum_flags |= M_CSUM_TCP_UDP_BAD; - return 0; + return csum_flags; } -/* - * A frame has been uploaded: pass the resulting mbuf chain up to - * the higher level protocols. - */ static void -axen_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status) +axen_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct axen_chain *c = (struct axen_chain *)priv; - struct axen_softc * const sc = c->axen_sc; - struct ifnet *ifp = GET_IFP(sc); - uint8_t *buf = c->axen_buf; - struct mbuf *m; - uint32_t total_len; + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; uint32_t rx_hdr, pkt_hdr; uint32_t *hdr_p; uint16_t hdr_offset, pkt_count; size_t pkt_len; size_t temp; - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__)); - - mutex_enter(&sc->axen_rxlock); - - if (sc->axen_dying || sc->axen_stopping || - status == USBD_INVAL || status == USBD_NOT_STARTED || - status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) { - mutex_exit(&sc->axen_rxlock); - return; - } - - if (status != USBD_NORMAL_COMPLETION) { - if (usbd_ratecheck(&sc->axen_rx_notice)) - aprint_error_dev(sc->axen_dev, "usb errors on rx: %s\n", - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->axen_ep[AXEN_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + usbnet_isowned_rx(un); if (total_len < sizeof(pkt_hdr)) { - aprint_error_dev(sc->axen_dev, "rxeof: too short transfer\n"); + aprint_error_dev(un->un_dev, "rxeof: too short transfer\n"); ifp->if_ierrors++; - goto done; + return; } /* @@ -1184,21 +792,14 @@ axen_rxeof(struct usbd_xfer *xfer, void hdr_offset = (uint16_t)(rx_hdr >> 16); pkt_count = (uint16_t)(rx_hdr & 0xffff); - if (total_len > sc->axen_rx_bufsz) { - aprint_error_dev(sc->axen_dev, - "rxeof: too large transfer (%u > %u)\n", - total_len, sc->axen_rx_bufsz); - goto done; - } - /* sanity check */ if (hdr_offset > total_len) { - aprint_error_dev(sc->axen_dev, + aprint_error_dev(un->un_dev, "rxeof: invalid hdr offset (%u > %u)\n", hdr_offset, total_len); ifp->if_ierrors++; - usbd_delay_ms(sc->axen_udev, 100); - goto done; + usbd_delay_ms(un->un_udev, 100); + return; } /* point first packet header */ @@ -1213,60 +814,40 @@ axen_rxeof(struct usbd_xfer *xfer, void #define AXEN_MAX_PACKED_PACKET 200 if (pkt_count > AXEN_MAX_PACKED_PACKET) { DPRINTF(("%s: Too many packets (%d) in a transaction, discard.\n", - device_xname(sc->axen_dev), pkt_count)); - goto done; + device_xname(un->un_dev), pkt_count)); + return; } #endif + if (pkt_count) + rnd_add_uint32(usbnet_rndsrc(un), pkt_count); + do { if ((buf[0] != 0xee) || (buf[1] != 0xee)) { - aprint_error_dev(sc->axen_dev, + aprint_error_dev(un->un_dev, "invalid buffer(pkt#%d), continue\n", pkt_count); ifp->if_ierrors += pkt_count; - goto done; + return; } pkt_hdr = le32toh(*hdr_p); pkt_len = (pkt_hdr >> 16) & 0x1fff; DPRINTFN(10, ("%s: rxeof: packet#%d, pkt_hdr 0x%08x, pkt_len %zu\n", - device_xname(sc->axen_dev), pkt_count, pkt_hdr, pkt_len)); + device_xname(un->un_dev), pkt_count, pkt_hdr, pkt_len)); if (pkt_hdr & (AXEN_RXHDR_CRC_ERR | AXEN_RXHDR_DROP_ERR)) { ifp->if_ierrors++; /* move to next pkt header */ DPRINTF(("%s: %s err (pkt#%d)\n", - device_xname(sc->axen_dev), + device_xname(un->un_dev), (pkt_hdr & AXEN_RXHDR_CRC_ERR) ? "crc" : "drop", pkt_count)); goto nextpkt; } - /* process each packet */ - /* allocate mbuf */ - m = axen_newbuf(); - if (m == NULL) { - ifp->if_ierrors++; - goto nextpkt; - } - - /* skip pseudo header (2byte) */ - m_set_rcvif(m, ifp); - m->m_pkthdr.len = m->m_len = pkt_len - 6; - - m->m_pkthdr.csum_flags = axen_csum_flags_rx(ifp, pkt_hdr); - memcpy(mtod(m, char *), buf + 2, pkt_len - 6); - - mutex_exit(&sc->axen_rxlock); - - /* push the packet up */ - if_percpuq_enqueue((ifp)->if_percpuq, (m)); - - mutex_enter(&sc->axen_rxlock); - if (sc->axen_dying || sc->axen_stopping) { - mutex_exit(&sc->axen_rxlock); - return; - } + usbnet_enqueue(un, buf + 2, pkt_len - 6, + axen_csum_flags_rx(ifp, pkt_hdr), 0, 0); nextpkt: /* @@ -1279,191 +860,22 @@ nextpkt: hdr_p++; pkt_count--; } while (pkt_count > 0); - -done: - if (sc->axen_dying || sc->axen_stopping) { - mutex_exit(&sc->axen_rxlock); - return; - } - - mutex_exit(&sc->axen_rxlock); - - /* Setup new transfer. */ - usbd_setup_xfer(xfer, c, c->axen_buf, sc->axen_rx_bufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axen_rxeof); - usbd_transfer(xfer); - - DPRINTFN(10,("%s: %s: start rx\n",device_xname(sc->axen_dev),__func__)); } -static int -axen_csum_flags_rx(struct ifnet *ifp, uint32_t pkt_hdr) +static unsigned +axen_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - int enabled_flags = ifp->if_csum_flags_rx; - int csum_flags = 0; - int l3_type, l4_type; - - if (enabled_flags == 0) - return 0; - - l3_type = (pkt_hdr & AXEN_RXHDR_L3_TYPE_MASK) >> - AXEN_RXHDR_L3_TYPE_OFFSET; - - if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4) - csum_flags |= M_CSUM_IPv4; - - l4_type = (pkt_hdr & AXEN_RXHDR_L4_TYPE_MASK) >> - AXEN_RXHDR_L4_TYPE_OFFSET; - - switch (l4_type) { - case AXEN_RXHDR_L4_TYPE_TCP: - if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4) - csum_flags |= M_CSUM_TCPv4; - else - csum_flags |= M_CSUM_TCPv6; - break; - case AXEN_RXHDR_L4_TYPE_UDP: - if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4) - csum_flags |= M_CSUM_UDPv4; - else - csum_flags |= M_CSUM_UDPv6; - break; - default: - break; - } - - csum_flags &= enabled_flags; - if ((csum_flags & M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L3CSUM_ERR)) - csum_flags |= M_CSUM_IPv4_BAD; - if ((csum_flags & ~M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L4CSUM_ERR)) - csum_flags |= M_CSUM_TCP_UDP_BAD; - - return csum_flags; -} - -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ -static void -axen_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status) -{ - struct axen_chain *c = (struct axen_chain *)priv; - struct axen_softc * const sc = c->axen_sc; - struct axen_cdata *cd = &sc->axen_cdata; - struct ifnet *ifp = GET_IFP(sc); - - mutex_enter(&sc->axen_txlock); - if (sc->axen_stopping || sc->axen_dying) { - mutex_exit(&sc->axen_txlock); - return; - } - - KASSERT(cd->axen_tx_cnt > 0); - cd->axen_tx_cnt--; - - sc->axen_timer = 0; - - switch (status) { - case USBD_NOT_STARTED: - case USBD_CANCELLED: - break; - - case USBD_NORMAL_COMPLETION: - ifp->if_opackets++; - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - axen_start_locked(ifp); - break; - - default: - - ifp->if_oerrors++; - if (usbd_ratecheck(&sc->axen_tx_notice)) - aprint_error_dev(sc->axen_dev, "usb error on tx: %s\n", - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->axen_ep[AXEN_ENDPT_TX]); - break; - } - - mutex_exit(&sc->axen_txlock); -} - -static void -axen_tick(void *xsc) -{ - struct axen_softc * const sc = xsc; - - if (sc == NULL) - return; - - DPRINTFN(0xff,("%s: %s: enter\n", device_xname(sc->axen_dev),__func__)); - - mutex_enter(&sc->axen_lock); - if (!sc->axen_stopping && !sc->axen_dying) { - /* Perform periodic stuff in process context */ - usb_add_task(sc->axen_udev, &sc->axen_tick_task, USB_TASKQ_DRIVER); - } - mutex_exit(&sc->axen_lock); -} - -static void -axen_tick_task(void *xsc) -{ - struct axen_softc * const sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - - if (sc == NULL) - return; - - mutex_enter(&sc->axen_lock); - if (sc->axen_stopping || sc->axen_dying) { - mutex_exit(&sc->axen_lock); - return; - } - - ifp = GET_IFP(sc); - mii = GET_MII(sc); - if (mii == NULL) { - mutex_exit(&sc->axen_lock); - return; - } - - sc->axen_refcnt++; - mutex_exit(&sc->axen_lock); - - if (sc->axen_timer != 0 && --sc->axen_timer == 0) - axen_watchdog(ifp); - - mii_tick(mii); - - if (sc->axen_link == 0) - axen_miibus_statchg(ifp); - - mutex_enter(&sc->axen_lock); - if (--sc->axen_refcnt < 0) - cv_broadcast(&sc->axen_detachcv); - if (!sc->axen_stopping && !sc->axen_dying) - callout_schedule(&sc->axen_stat_ch, hz); - mutex_exit(&sc->axen_lock); -} - -static int -axen_encap(struct axen_softc *sc, struct mbuf *m, int idx) -{ - struct ifnet *ifp = GET_IFP(sc); - struct axen_chain *c; - usbd_status err; struct axen_sframe_hdr hdr; u_int length, boundary; - KASSERT(mutex_owned(&sc->axen_txlock)); + usbnet_isowned_tx(un); - c = &sc->axen_cdata.axen_tx_chain[idx]; + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - sizeof(hdr)) + return 0; + length = m->m_pkthdr.len + sizeof(hdr); /* XXX Is this needed? wMaxPacketSize? */ - switch (sc->axen_udev->ud_speed) { + switch (un->un_udev->ud_speed) { case USB_SPEED_SUPER: boundary = 4096; break; @@ -1475,428 +887,97 @@ axen_encap(struct axen_softc *sc, struct break; } - length = m->m_pkthdr.len + sizeof(hdr); - KASSERT(length <= sc->axen_tx_bufsz); - hdr.plen = htole32(m->m_pkthdr.len); hdr.gso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) ? m->m_pkthdr.segsz : 0; if ((length % boundary) == 0) { - DPRINTF(("%s: boundary hit\n", device_xname(sc->axen_dev))); + DPRINTF(("%s: boundary hit\n", device_xname(un->un_dev))); hdr.gso |= 0x80008000; /* XXX enable padding */ } hdr.gso = htole32(hdr.gso); - memcpy(c->axen_buf, &hdr, sizeof(hdr)); - m_copydata(m, 0, m->m_pkthdr.len, c->axen_buf + sizeof(hdr)); - - if (__predict_false(c->axen_xfer == NULL)) - return EIO; /* XXX plugged out or down */ - - usbd_setup_xfer(c->axen_xfer, c, c->axen_buf, length, - USBD_FORCE_SHORT_XFER, 10000, axen_txeof); - - /* Transmit */ - err = usbd_transfer(c->axen_xfer); - if (err != USBD_IN_PROGRESS) { - /* XXXSMP IFNET_LOCK */ - axen_stop(ifp, 0); - return EIO; - } - - return 0; -} - -static void -axen_start_locked(struct ifnet *ifp) -{ - struct axen_softc * const sc = ifp->if_softc; - struct mbuf *m; - struct axen_cdata *cd = &sc->axen_cdata; - int idx; - - KASSERT(mutex_owned(&sc->axen_txlock)); - KASSERT(cd->axen_tx_cnt <= AXEN_TX_LIST_CNT); - - if (sc->axen_link == 0 || (ifp->if_flags & IFF_RUNNING) == 0) - return; - - idx = cd->axen_tx_prod; - while (cd->axen_tx_cnt < AXEN_TX_LIST_CNT) { - IFQ_POLL(&ifp->if_snd, m); - if (m == NULL) - break; - - if (axen_encap(sc, m, idx)) { - ifp->if_oerrors++; - break; - } - IFQ_DEQUEUE(&ifp->if_snd, m); - - /* - * If there's a BPF listener, bounce a copy of this frame - * to him. - */ - bpf_mtap(ifp, m, BPF_D_OUT); - m_freem(m); - - idx = (idx + 1) % AXEN_TX_LIST_CNT; - cd->axen_tx_cnt++; - } - cd->axen_tx_prod = idx; + memcpy(c->unc_buf, &hdr, sizeof(hdr)); + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + sizeof(hdr)); - /* - * Set a timeout in case the chip goes out to lunch. - */ - sc->axen_timer = 5; -} - -static void -axen_start(struct ifnet *ifp) -{ - struct axen_softc * const sc = ifp->if_softc; - - mutex_enter(&sc->axen_txlock); - if (!sc->axen_stopping) - axen_start_locked(ifp); - mutex_exit(&sc->axen_txlock); + return length; } static int axen_init_locked(struct ifnet *ifp) { - struct axen_softc * const sc = ifp->if_softc; - struct axen_chain *c; - usbd_status err; - int i; + struct usbnet * const un = ifp->if_softc; uint16_t rxmode; uint16_t wval; uint8_t bval; - KASSERT(mutex_owned(&sc->axen_lock)); + usbnet_isowned(un); - if (sc->axen_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O */ - axen_stop_locked(ifp, 1); + usbnet_stop(un, ifp, 1); /* Reset the ethernet interface. */ - axen_reset(sc); + axen_reset(un); - axen_lock_mii_sc_locked(sc); + usbnet_lock_mii_un_locked(un); /* XXX: ? */ bval = 0x01; - axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_UNK_28, &bval); + axen_cmd(un, AXEN_CMD_MAC_WRITE, 1, AXEN_UNK_28, &bval); /* Configure offloading engine. */ - axen_setcoe(sc); + axen_setoe_locked(un); /* Program promiscuous mode and multicast filters. */ - axen_iff_locked(sc); + axen_setiff_locked(un); /* Enable receiver, set RX mode */ - axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval); + axen_cmd(un, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval); rxmode = le16toh(wval); rxmode |= AXEN_RXCTL_START; wval = htole16(rxmode); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); - - axen_unlock_mii_sc_locked(sc); - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->axen_iface, sc->axen_ed[AXEN_ENDPT_RX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axen_ep[AXEN_ENDPT_RX]); - if (err) { - aprint_error_dev(sc->axen_dev, "open rx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } - - err = usbd_open_pipe(sc->axen_iface, sc->axen_ed[AXEN_ENDPT_TX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axen_ep[AXEN_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->axen_dev, "open tx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } - - /* Init RX ring. */ - if (axen_rx_list_init(sc)) { - aprint_error_dev(sc->axen_dev, "rx list init failed\n"); - return ENOBUFS; - } - - /* Init TX ring. */ - if (axen_tx_list_init(sc)) { - aprint_error_dev(sc->axen_dev, "tx list init failed\n"); - return ENOBUFS; - } - - mutex_enter(&sc->axen_rxlock); - mutex_enter(&sc->axen_txlock); - sc->axen_stopping = false; - - /* Start up the receive pipe. */ - for (i = 0; i < AXEN_RX_LIST_CNT; i++) { - c = &sc->axen_cdata.axen_rx_chain[i]; - - usbd_setup_xfer(c->axen_xfer, c, c->axen_buf, sc->axen_rx_bufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axen_rxeof); - usbd_transfer(c->axen_xfer); - } + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); - mutex_exit(&sc->axen_txlock); - mutex_exit(&sc->axen_rxlock); + usbnet_unlock_mii_un_locked(un); - /* Indicate we are up and running. */ - KASSERT(IFNET_LOCKED(ifp)); - ifp->if_flags |= IFF_RUNNING; - - callout_schedule(&sc->axen_stat_ch, hz); - return 0; + return usbnet_init_rx_tx(un); } static int axen_init(struct ifnet *ifp) { - struct axen_softc * const sc = ifp->if_softc; + struct usbnet * const un = ifp->if_softc; - mutex_enter(&sc->axen_lock); + usbnet_lock(un); int ret = axen_init_locked(ifp); - mutex_exit(&sc->axen_lock); + usbnet_unlock(un); return ret; } -static int -axen_ioctl(struct ifnet *ifp, u_long cmd, void *data) -{ - struct axen_softc * const sc = ifp->if_softc; - int error = 0; - - switch (cmd) { - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, cmd, data)) != 0) - break; - - switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { - case IFF_RUNNING: - axen_stop(ifp, 1); - break; - case IFF_UP: - axen_init(ifp); - break; - case IFF_UP | IFF_RUNNING: - if ((ifp->if_flags ^ sc->axen_if_flags) == IFF_PROMISC) { - axen_iff(sc); - } else - axen_init(ifp); - break; - } - - mutex_enter(&sc->axen_rxlock); - mutex_enter(&sc->axen_txlock); - sc->axen_if_flags = ifp->if_flags; - mutex_exit(&sc->axen_txlock); - mutex_exit(&sc->axen_rxlock); - break; - - default: - if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) - break; - - error = 0; - switch (cmd) { - case SIOCADDMULTI: - case SIOCDELMULTI: - axen_iff(sc); - break; - case SIOCSIFCAP: - mutex_enter(&sc->axen_lock); - axen_setcoe(sc); - mutex_exit(&sc->axen_lock); - break; - default: - break; - } - break; - } - - return error; -} - -static void -axen_watchdog(struct ifnet *ifp) -{ - struct axen_softc * const sc = ifp->if_softc; - struct axen_chain *c; - usbd_status stat; - - ifp->if_oerrors++; - aprint_error_dev(sc->axen_dev, "watchdog timeout\n"); - - c = &sc->axen_cdata.axen_tx_chain[0]; - usbd_get_xfer_status(c->axen_xfer, NULL, NULL, NULL, &stat); - axen_txeof(c->axen_xfer, c, stat); - - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - axen_start(ifp); -} - -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ static void -axen_stop_locked(struct ifnet *ifp, int disable) +axen_stop_cb(struct ifnet *ifp, int disable) { - struct axen_softc * const sc = ifp->if_softc; - struct axen_chain *c; - usbd_status err; - int i; + struct usbnet * const un = ifp->if_softc; uint16_t rxmode, wval; - KASSERT(mutex_owned(&sc->axen_lock)); - mutex_enter(&sc->axen_rxlock); - mutex_enter(&sc->axen_txlock); - sc->axen_stopping = true; - mutex_exit(&sc->axen_txlock); - mutex_exit(&sc->axen_rxlock); - - axen_reset(sc); + axen_reset(un); /* Disable receiver, set RX mode */ - axen_lock_mii_sc_locked(sc); - axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval); + usbnet_lock_mii_un_locked(un); + axen_cmd(un, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval); rxmode = le16toh(wval); rxmode &= ~AXEN_RXCTL_START; wval = htole16(rxmode); - axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); - axen_unlock_mii_sc_locked(sc); - - /* - * XXXSMP Would like to - * KASSERT(IFNET_LOCKED(ifp)) - * here but the locking order is: - * ifnet -> sc lock -> rxlock -> txlock - * and sc lock is already held. - */ - ifp->if_flags &= ~IFF_RUNNING; - sc->axen_timer = 0; - - callout_stop(&sc->axen_stat_ch); - sc->axen_link = 0; - - /* Stop transfers. */ - if (sc->axen_ep[AXEN_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_RX]); - if (err) { - aprint_error_dev(sc->axen_dev, - "abort rx pipe failed: %s\n", usbd_errstr(err)); - } - } - - if (sc->axen_ep[AXEN_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->axen_dev, - "abort tx pipe failed: %s\n", usbd_errstr(err)); - } - } - - if (sc->axen_ep[AXEN_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_INTR]); - if (err) { - aprint_error_dev(sc->axen_dev, - "abort intr pipe failed: %s\n", usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < AXEN_RX_LIST_CNT; i++) { - c = &sc->axen_cdata.axen_rx_chain[i]; - if (c->axen_xfer != NULL) { - usbd_destroy_xfer(c->axen_xfer); - c->axen_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < AXEN_TX_LIST_CNT; i++) { - c = &sc->axen_cdata.axen_tx_chain[i]; - if (c->axen_xfer != NULL) { - usbd_destroy_xfer(c->axen_xfer); - c->axen_xfer = NULL; - } - } - - /* Close pipes. */ - if (sc->axen_ep[AXEN_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_RX]); - if (err) { - aprint_error_dev(sc->axen_dev, - "close rx pipe failed: %s\n", usbd_errstr(err)); - } - sc->axen_ep[AXEN_ENDPT_RX] = NULL; - } - - if (sc->axen_ep[AXEN_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->axen_dev, - "close tx pipe failed: %s\n", usbd_errstr(err)); - } - sc->axen_ep[AXEN_ENDPT_TX] = NULL; - } - - if (sc->axen_ep[AXEN_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_INTR]); - if (err) { - aprint_error_dev(sc->axen_dev, - "close intr pipe failed: %s\n", usbd_errstr(err)); - } - sc->axen_ep[AXEN_ENDPT_INTR] = NULL; - } -} - -static void -axen_stop(struct ifnet *ifp, int disable) -{ - struct axen_softc * const sc = ifp->if_softc; - - mutex_enter(&sc->axen_lock); - axen_stop_locked(ifp, disable); - mutex_exit(&sc->axen_lock); + axen_cmd(un, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval); + usbnet_unlock_mii_un_locked(un); } -MODULE(MODULE_CLASS_DRIVER, if_axen, NULL); - #ifdef _MODULE #include "ioconf.c" #endif -static int -if_axen_modcmd(modcmd_t cmd, void *aux) -{ - int error = 0; - - switch (cmd) { - case MODULE_CMD_INIT: -#ifdef _MODULE - error = config_init_component(cfdriver_ioconf_axen, - cfattach_ioconf_axen, cfdata_ioconf_axen); -#endif - return error; - case MODULE_CMD_FINI: -#ifdef _MODULE - error = config_fini_component(cfdriver_ioconf_axen, - cfattach_ioconf_axen, cfdata_ioconf_axen); -#endif - return error; - default: - return ENOTTY; - } -} +USBNET_MODULE(axen) Index: usb/if_axenreg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_axenreg.h,v retrieving revision 1.14 diff -p -u -u -r1.14 if_axenreg.h --- usb/if_axenreg.h 18 Jun 2019 09:34:57 -0000 1.14 +++ usb/if_axenreg.h 29 Aug 2019 06:22:05 -0000 @@ -221,15 +221,6 @@ #define AXEN_CONFIG_NO 1 #define AXEN_IFACE_IDX 0 -/* - * The interrupt endpoint is currently unused - * by the ASIX part. - */ -#define AXEN_ENDPT_RX 0x0 -#define AXEN_ENDPT_TX 0x1 -#define AXEN_ENDPT_INTR 0x2 -#define AXEN_ENDPT_MAX 0x3 - struct axen_qctrl { uint8_t ctrl; uint8_t timer_low; Index: usb/if_cdce.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_cdce.c,v retrieving revision 1.53 diff -p -u -u -r1.53 if_cdce.c --- usb/if_cdce.c 21 Jul 2019 10:27:56 -0000 1.53 +++ usb/if_cdce.c 29 Aug 2019 06:22:05 -0000 @@ -37,43 +37,14 @@ /* * USB Communication Device Class (Ethernet Networking Control Model) * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf - * */ #include __KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.53 2019/07/21 10:27:56 mrg Exp $"); -#ifdef _KERNEL_OPT -#include "opt_inet.h" -#endif - #include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#ifdef INET -#include -#include -#endif -#include -#include -#include -#include -#include +#include #include #include @@ -85,69 +56,6 @@ struct cdce_type { #define CDCE_NO_UNION 2 }; -struct cdce_softc; - -struct cdce_chain { - struct cdce_softc *cdce_sc; - struct usbd_xfer *cdce_xfer; - char *cdce_buf; - struct mbuf *cdce_mbuf; - int cdce_accum; - int cdce_idx; -}; - -struct cdce_cdata { - struct cdce_chain cdce_rx_chain[CDCE_RX_LIST_CNT]; - struct cdce_chain cdce_tx_chain[CDCE_TX_LIST_CNT]; - int cdce_tx_prod; - int cdce_tx_cnt; -}; - -struct cdce_softc { - device_t cdce_dev; - struct ethercom cdce_ec; - krndsource_t rnd_source; -#define GET_IFP(sc) (&(sc)->cdce_ec.ec_if) - struct usbd_device * cdce_udev; - struct usbd_interface * cdce_ctl_iface; - struct usbd_interface * cdce_data_iface; - int cdce_bulkin_no; - struct usbd_pipe * cdce_bulkin_pipe; - int cdce_bulkout_no; - struct usbd_pipe * cdce_bulkout_pipe; - int cdce_unit; - struct cdce_cdata cdce_cdata; - int cdce_rxeof_errors; - uint16_t cdce_flags; - uint16_t cdce_timer; - - bool cdce_attached; - bool cdce_dying; - bool cdce_stopping; - - struct usb_task cdce_tick_task; - struct callout cdce_stat_ch; - - kmutex_t cdce_lock; - kmutex_t cdce_mii_lock; - kmutex_t cdce_rxlock; - kmutex_t cdce_txlock; -}; - -static int cdce_tx_list_init(struct cdce_softc *); -static int cdce_rx_list_init(struct cdce_softc *); -static int cdce_newbuf(struct cdce_softc *, struct cdce_chain *, - struct mbuf *); -static int cdce_encap(struct cdce_softc *, struct mbuf *, int); -static void cdce_rxeof(struct usbd_xfer *, void *, usbd_status); -static void cdce_txeof(struct usbd_xfer *, void *, usbd_status); -static void cdce_start(struct ifnet *); -static int cdce_ioctl(struct ifnet *, u_long, void *); -static void cdce_init(void *); -static void cdce_stop(struct cdce_softc *); -static void cdce_tick(void *); -static void cdce_tick_task(void *); - static const struct cdce_type cdce_devs[] = { {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION }, {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION }, @@ -164,15 +72,24 @@ static const struct cdce_type cdce_devs[ #define cdce_lookup(v, p) \ ((const struct cdce_type *)usb_lookup(cdce_devs, v, p)) -int cdce_match(device_t, cfdata_t, void *); -void cdce_attach(device_t, device_t, void *); -int cdce_detach(device_t, int); -int cdce_activate(device_t, enum devact); +static int cdce_match(device_t, cfdata_t, void *); +static void cdce_attach(device_t, device_t, void *); + +CFATTACH_DECL_NEW(cdce, sizeof(struct usbnet), cdce_match, cdce_attach, + usbnet_detach, usbnet_activate); -CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach, - cdce_detach, cdce_activate); +static void cdce_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned cdce_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static int cdce_init(struct ifnet *); + +static struct usbnet_ops cdce_ops = { + .uno_tx_prepare = cdce_tx_prepare, + .uno_rx_loop = cdce_rx_loop, + .uno_init = cdce_init, +}; -int +static int cdce_match(device_t parent, cfdata_t match, void *aux) { struct usbif_attach_arg *uiaa = aux; @@ -187,13 +104,12 @@ cdce_match(device_t parent, cfdata_t mat return UMATCH_NONE; } -void +static void cdce_attach(device_t parent, device_t self, void *aux) { - struct cdce_softc *sc = device_private(self); - struct usbif_attach_arg *uiaa = aux; + struct usbnet * const un = device_private(self); + struct usbif_attach_arg *uiaa = aux; char *devinfop; - struct ifnet *ifp; struct usbd_device *dev = uiaa->uiaa_device; const struct cdce_type *t; usb_interface_descriptor_t *id; @@ -202,30 +118,34 @@ cdce_attach(device_t parent, device_t se usb_config_descriptor_t *cd; int data_ifcno; int i, j, numalts; - u_char eaddr[ETHER_ADDR_LEN]; const usb_cdc_ethernet_descriptor_t *ue; char eaddr_str[USB_MAX_ENCODED_STRING_LEN]; - sc->cdce_dev = self; - aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); - sc->cdce_udev = uiaa->uiaa_device; - sc->cdce_ctl_iface = uiaa->uiaa_iface; + un->un_dev = self; + un->un_udev = dev; + un->un_sc = un; + un->un_ops = &cdce_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = CDCE_RX_LIST_CNT; + un->un_tx_list_cnt = CDCE_TX_LIST_CNT; + un->un_rx_bufsz = CDCE_BUFSZ; + un->un_tx_bufsz = CDCE_BUFSZ; t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product); if (t) - sc->cdce_flags = t->cdce_flags; + un->un_flags = t->cdce_flags; - if (sc->cdce_flags & CDCE_NO_UNION) - sc->cdce_data_iface = sc->cdce_ctl_iface; + if (un->un_flags & CDCE_NO_UNION) + un->un_iface = uiaa->uiaa_iface; else { - ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(sc->cdce_udev, + ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(un->un_udev, UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION); if (ud == NULL) { aprint_error_dev(self, "no union descriptor\n"); @@ -239,14 +159,13 @@ cdce_attach(device_t parent, device_t se uiaa->uiaa_ifaces[i]); if (id != NULL && id->bInterfaceNumber == data_ifcno) { - sc->cdce_data_iface = uiaa->uiaa_ifaces[i]; + un->un_iface = uiaa->uiaa_ifaces[i]; uiaa->uiaa_ifaces[i] = NULL; } } } } - - if (sc->cdce_data_iface == NULL) { + if (un->un_iface == NULL) { aprint_error_dev(self, "no data interface\n"); return; } @@ -269,21 +188,21 @@ cdce_attach(device_t parent, device_t se * available interface settings looking for one with both IN and OUT * endpoints. */ - id = usbd_get_interface_descriptor(sc->cdce_data_iface); - cd = usbd_get_config_descriptor(sc->cdce_udev); + id = usbd_get_interface_descriptor(un->un_iface); + cd = usbd_get_config_descriptor(un->un_udev); numalts = usbd_get_no_alts(cd, id->bInterfaceNumber); for (j = 0; j < numalts; j++) { - if (usbd_set_interface(sc->cdce_data_iface, j)) { - aprint_error_dev(sc->cdce_dev, + if (usbd_set_interface(un->un_iface, j)) { + aprint_error_dev(un->un_dev, "setting alternate interface failed\n"); return; } /* Find endpoints. */ - id = usbd_get_interface_descriptor(sc->cdce_data_iface); - sc->cdce_bulkin_no = sc->cdce_bulkout_no = -1; + id = usbd_get_interface_descriptor(un->un_iface); + un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 0; for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->cdce_data_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (!ed) { aprint_error_dev(self, "could not read endpoint descriptor\n"); @@ -291,10 +210,10 @@ cdce_attach(device_t parent, device_t se } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->cdce_bulkin_no = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->cdce_bulkout_no = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { /* XXX: CDC spec defines an interrupt pipe, but it is not @@ -304,15 +223,15 @@ cdce_attach(device_t parent, device_t se } } /* If we found something, try and use it... */ - if ((sc->cdce_bulkin_no != -1) && (sc->cdce_bulkout_no != -1)) + if (un->un_ed[USBNET_ENDPT_RX] != 0 && un->un_ed[USBNET_ENDPT_TX] != 0) break; } - if (sc->cdce_bulkin_no == -1) { + if (un->un_ed[USBNET_ENDPT_RX] == 0) { aprint_error_dev(self, "could not find data bulk in\n"); return; } - if (sc->cdce_bulkout_no == -1 ) { + if (un->un_ed[USBNET_ENDPT_TX] == 0) { aprint_error_dev(self, "could not find data bulk out\n"); return; } @@ -320,681 +239,84 @@ cdce_attach(device_t parent, device_t se ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF); if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) || - ether_aton_r(eaddr, sizeof(eaddr), eaddr_str)) { + ether_aton_r(un->un_eaddr, sizeof(un->un_eaddr), eaddr_str)) { aprint_normal_dev(self, "faking address\n"); - eaddr[0]= 0x2a; - memcpy(&eaddr[1], &hardclock_ticks, sizeof(uint32_t)); - eaddr[5] = (uint8_t)(device_unit(sc->cdce_dev)); - } - - mutex_init(&sc->cdce_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->cdce_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->cdce_lock, MUTEX_DEFAULT, IPL_NONE); - - usb_init_task(&sc->cdce_tick_task, cdce_tick_task, sc, USB_TASKQ_MPSAFE); - - aprint_normal_dev(self, "address %s\n", ether_sprintf(eaddr)); - - ifp = GET_IFP(sc); - ifp->if_softc = sc; - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_extflags = IFEF_MPSAFE; - ifp->if_ioctl = cdce_ioctl; - ifp->if_start = cdce_start; - strlcpy(ifp->if_xname, device_xname(sc->cdce_dev), IFNAMSIZ); - - IFQ_SET_READY(&ifp->if_snd); - - if_attach(ifp); - ether_ifattach(ifp, eaddr); - - callout_init(&sc->cdce_stat_ch, CALLOUT_MPSAFE); - callout_setfunc(&sc->cdce_stat_ch, cdce_tick, sc); - - sc->cdce_attached = true; - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cdce_udev, - sc->cdce_dev); - - if (!pmf_device_register(self, NULL, NULL)) - aprint_error_dev(self, "couldn't establish power handler\n"); - - return; -} - -int -cdce_detach(device_t self, int flags) -{ - struct cdce_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - - mutex_enter(&sc->cdce_lock); - sc->cdce_dying = true; - mutex_exit(&sc->cdce_lock); - - if (!sc->cdce_attached) - return 0; - - pmf_device_deregister(self); - - callout_halt(&sc->cdce_stat_ch, NULL); - usb_rem_task_wait(sc->cdce_udev, &sc->cdce_tick_task, - USB_TASKQ_DRIVER, NULL); - - if (ifp->if_flags & IFF_RUNNING) { - IFNET_LOCK(ifp); - cdce_stop(sc); - IFNET_UNLOCK(ifp); - } - - callout_destroy(&sc->cdce_stat_ch); - ether_ifdetach(ifp); - if_detach(ifp); - sc->cdce_attached = false; - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cdce_udev,sc->cdce_dev); - - mutex_destroy(&sc->cdce_lock); - mutex_destroy(&sc->cdce_rxlock); - mutex_destroy(&sc->cdce_txlock); - - return 0; -} - -static void -cdce_start_locked(struct ifnet *ifp) -{ - struct cdce_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - KASSERT(mutex_owned(&sc->cdce_txlock)); - - if (sc->cdce_dying || sc->cdce_cdata.cdce_tx_cnt == CDCE_TX_LIST_CNT) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (cdce_encap(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } - - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - bpf_mtap(ifp, m_head, BPF_D_OUT); - - /* - * Set a timeout in case the chip goes out to lunch. - */ - sc->cdce_timer = 6; -} - -static void -cdce_start(struct ifnet *ifp) -{ - struct cdce_softc * const sc = ifp->if_softc; - - mutex_enter(&sc->cdce_txlock); - cdce_start_locked(ifp); - mutex_exit(&sc->cdce_txlock); -} - -static int -cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx) -{ - struct cdce_chain *c; - usbd_status err; - int extra = 0; - - KASSERT(mutex_owned(&sc->cdce_txlock)); - - c = &sc->cdce_cdata.cdce_tx_chain[idx]; - - m_copydata(m, 0, m->m_pkthdr.len, c->cdce_buf); - if (sc->cdce_flags & CDCE_ZAURUS) { - /* Zaurus wants a 32-bit CRC appended to every frame */ - uint32_t crc; - - crc = htole32(~ether_crc32_le(c->cdce_buf, m->m_pkthdr.len)); - memcpy(c->cdce_buf + m->m_pkthdr.len, &crc, sizeof(crc)); - extra = sizeof(crc); - } - c->cdce_mbuf = m; - - usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, m->m_pkthdr.len + extra, - USBD_FORCE_SHORT_XFER, 10000, cdce_txeof); - err = usbd_transfer(c->cdce_xfer); - if (err != USBD_IN_PROGRESS) { - /* XXXSMP IFNET_LOCK */ - cdce_stop(sc); - return EIO; - } - - sc->cdce_cdata.cdce_tx_cnt++; - KASSERT(sc->cdce_cdata.cdce_tx_cnt <= CDCE_TX_LIST_CNT); - - return 0; -} - -static void -cdce_stop_locked(struct cdce_softc *sc) -{ - usbd_status err; - struct ifnet *ifp = GET_IFP(sc); - int i; - - /* XXXSMP can't KASSERT(IFNET_LOCKED(ifp)); */ - KASSERT(mutex_owned(&sc->cdce_lock)); - - mutex_enter(&sc->cdce_rxlock); - mutex_enter(&sc->cdce_txlock); - sc->cdce_stopping = true; - mutex_exit(&sc->cdce_txlock); - mutex_exit(&sc->cdce_rxlock); - - /* - * XXXSMP Would like to - * KASSERT(IFNET_LOCKED(ifp)) - * here but the locking order is: - * ifnet -> sc lock -> rxlock -> txlock - * and sc lock is already held. - */ - ifp->if_flags &= ~IFF_RUNNING; - sc->cdce_timer = 0; - - callout_stop(&sc->cdce_stat_ch); - - if (sc->cdce_bulkin_pipe != NULL) { - err = usbd_abort_pipe(sc->cdce_bulkin_pipe); - if (err) - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->cdce_dev), usbd_errstr(err)); - } - - if (sc->cdce_bulkout_pipe != NULL) { - err = usbd_abort_pipe(sc->cdce_bulkout_pipe); - if (err) - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->cdce_dev), usbd_errstr(err)); - } - - for (i = 0; i < CDCE_RX_LIST_CNT; i++) { - if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) { - m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf); - sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL; - } - if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) { - usbd_destroy_xfer - (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer); - sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL; - } - } - - for (i = 0; i < CDCE_TX_LIST_CNT; i++) { - if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) { - m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf); - sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL; - } - if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) { - usbd_destroy_xfer( - sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer); - sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL; - } - } - - if (sc->cdce_bulkin_pipe != NULL) { - err = usbd_close_pipe(sc->cdce_bulkin_pipe); - if (err) - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->cdce_dev), usbd_errstr(err)); - sc->cdce_bulkin_pipe = NULL; - } - - if (sc->cdce_bulkout_pipe != NULL) { - err = usbd_close_pipe(sc->cdce_bulkout_pipe); - if (err) - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->cdce_dev), usbd_errstr(err)); - sc->cdce_bulkout_pipe = NULL; - } -} - -static void -cdce_stop(struct cdce_softc *sc) -{ - - mutex_enter(&sc->cdce_lock); - cdce_stop_locked(sc); - mutex_exit(&sc->cdce_lock); -} - -static int -cdce_ioctl(struct ifnet *ifp, u_long command, void *data) -{ - struct cdce_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int error = 0; - - if (sc->cdce_dying) - return EIO; - - switch(command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - cdce_init(sc); - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - arp_ifinit(ifp, ifa); - break; -#endif /* INET */ - } - break; - - case SIOCSIFMTU: - if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) - error = EINVAL; - else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) - error = 0; - break; - - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - /* XXX re-use ether_ioctl() */ - switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { - case IFF_UP: - cdce_init(sc); - break; - case IFF_RUNNING: - cdce_stop(sc); - break; - default: - break; - } - break; - - default: - error = ether_ioctl(ifp, command, data); - break; + un->un_eaddr[0] = 0x2a; + memcpy(&un->un_eaddr[1], &hardclock_ticks, sizeof(uint32_t)); + un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev)); } - if (error == ENETRESET) - error = 0; - - return error; -} - -static void -cdce_watchdog(struct ifnet *ifp) -{ - struct cdce_softc *sc = ifp->if_softc; - struct cdce_chain *c; - usbd_status stat; - - KASSERT(mutex_owned(&sc->cdce_lock)); - - if (sc->cdce_dying) - return; - - ifp->if_oerrors++; - aprint_error_dev(sc->cdce_dev, "watchdog timeout\n"); - - mutex_enter(&sc->cdce_txlock); - - c = &sc->cdce_cdata.cdce_rx_chain[0]; - usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &stat); - cdce_txeof(c->cdce_xfer, c, stat); - - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - cdce_start_locked(ifp); - - mutex_exit(&sc->cdce_txlock); -} - -static void -cdce_init(void *xsc) -{ - struct cdce_softc *sc = xsc; - struct ifnet *ifp = GET_IFP(sc); - struct cdce_chain *c; - usbd_status err; - int i; - - mutex_enter(&sc->cdce_lock); - if (ifp->if_flags & IFF_RUNNING) - goto out; - - /* Maybe set multicast / broadcast here??? */ - - err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no, - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->cdce_bulkin_pipe); - if (err) { - printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev), - usbd_errstr(err)); - goto out; - } - - err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no, - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->cdce_bulkout_pipe); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->cdce_dev), usbd_errstr(err)); - goto out; - } - - if (cdce_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->cdce_dev)); - goto out; - } - - if (cdce_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->cdce_dev)); - goto out; - } - - mutex_enter(&sc->cdce_rxlock); - mutex_enter(&sc->cdce_txlock); - sc->cdce_stopping = false; - - for (i = 0; i < CDCE_RX_LIST_CNT; i++) { - c = &sc->cdce_cdata.cdce_rx_chain[i]; - - usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof); - usbd_transfer(c->cdce_xfer); - } - - mutex_exit(&sc->cdce_txlock); - mutex_exit(&sc->cdce_rxlock); - - ifp->if_flags |= IFF_RUNNING; - - callout_schedule(&sc->cdce_stat_ch, hz); - -out: - mutex_exit(&sc->cdce_lock); + usbnet_attach(un, "cdcedet"); + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, NULL); } static int -cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m) +cdce_init(struct ifnet *ifp) { - struct mbuf *m_new = NULL; - - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->cdce_dev)); - return ENOBUFS; - } - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->cdce_dev)); - m_freem(m_new); - return ENOBUFS; - } - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - } else { - m_new = m; - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - m_new->m_data = m_new->m_ext.ext_buf; - } - c->cdce_mbuf = m_new; - return 0; -} + struct usbnet *un = ifp->if_softc; + int rv; -static int -cdce_rx_list_init(struct cdce_softc *sc) -{ - struct cdce_cdata *cd; - struct cdce_chain *c; - int i; - - cd = &sc->cdce_cdata; - for (i = 0; i < CDCE_RX_LIST_CNT; i++) { - c = &cd->cdce_rx_chain[i]; - c->cdce_sc = sc; - c->cdce_idx = i; - if (cdce_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->cdce_xfer == NULL) { - int err = usbd_create_xfer(sc->cdce_bulkin_pipe, - CDCE_BUFSZ, 0, 0, &c->cdce_xfer); - if (err) - return err; - c->cdce_buf = usbd_get_buffer(c->cdce_xfer); - } - } - - return 0; -} - -static int -cdce_tx_list_init(struct cdce_softc *sc) -{ - struct cdce_cdata *cd; - struct cdce_chain *c; - int i; - - cd = &sc->cdce_cdata; - for (i = 0; i < CDCE_TX_LIST_CNT; i++) { - c = &cd->cdce_tx_chain[i]; - c->cdce_sc = sc; - c->cdce_idx = i; - c->cdce_mbuf = NULL; - if (c->cdce_xfer == NULL) { - int err = usbd_create_xfer(sc->cdce_bulkout_pipe, - CDCE_BUFSZ, USBD_FORCE_SHORT_XFER, 0, - &c->cdce_xfer); - if (err) - return err; - c->cdce_buf = usbd_get_buffer(c->cdce_xfer); - } + usbnet_lock(un); + if (usbnet_isdying(un)) + rv = EIO; + else { + usbnet_stop(un, ifp, 1); + rv = usbnet_init_rx_tx(un); + usbnet_set_link(un, rv == 0); } + usbnet_unlock(un); - return 0; + return rv; } static void -cdce_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +cdce_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len) { - struct cdce_chain *c = priv; - struct cdce_softc *sc = c->cdce_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - int total_len = 0; - - mutex_enter(&sc->cdce_rxlock); - - if (sc->cdce_dying || sc->cdce_stopping || - status == USBD_INVAL || status == USBD_NOT_STARTED || - status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) { - mutex_exit(&sc->cdce_rxlock); - return; - } + struct ifnet *ifp = usbnet_ifp(un); - if (status != USBD_NORMAL_COMPLETION) { - if (sc->cdce_rxeof_errors == 0) - printf("%s: usb error on rx: %s\n", - device_xname(sc->cdce_dev), usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe); - usbd_delay_ms(sc->cdce_udev, 10); - sc->cdce_rxeof_errors++; - goto done; - } - - sc->cdce_rxeof_errors = 0; + usbnet_isowned_rx(un); - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - if (sc->cdce_flags & CDCE_ZAURUS) - total_len -= 4; /* Strip off CRC added by Zaurus */ - if (total_len <= 1) { - ifp->if_ierrors++; - goto done; - } - - m = c->cdce_mbuf; - memcpy(mtod(m, char *), c->cdce_buf, total_len); + /* Strip off CRC added by Zaurus, if present */ + if (un->un_flags & CDCE_ZAURUS && total_len > 4) + total_len -= 4; if (total_len < sizeof(struct ether_header)) { ifp->if_ierrors++; - goto done; - } - - m->m_pkthdr.len = m->m_len = total_len; - m_set_rcvif(m, ifp); - - if (cdce_newbuf(sc, c, NULL) == ENOBUFS) { - ifp->if_ierrors++; - goto done; - } - - mutex_exit(&sc->cdce_rxlock); - if_percpuq_enqueue(ifp->if_percpuq, m); - mutex_enter(&sc->cdce_rxlock); - -done: - if (sc->cdce_stopping || sc->cdce_dying) { - mutex_exit(&sc->cdce_rxlock); return; } - mutex_exit(&sc->cdce_rxlock); - - /* Setup new transfer. */ - usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof); - usbd_transfer(c->cdce_xfer); -} - -static void -cdce_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) -{ - struct cdce_chain *c = priv; - struct cdce_softc *sc = c->cdce_sc; - struct ifnet *ifp = GET_IFP(sc); - usbd_status err; - - mutex_enter(&sc->cdce_txlock); - - if (sc->cdce_dying) - goto out; - - KASSERT(sc->cdce_cdata.cdce_tx_cnt > 0); - sc->cdce_cdata.cdce_tx_cnt--; - - sc->cdce_timer = 0; - - switch (status) { - case USBD_NOT_STARTED: - case USBD_CANCELLED: - goto out; - - case USBD_NORMAL_COMPLETION: - break; - - default: - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe); - goto out; - } - - usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err); - - if (c->cdce_mbuf != NULL) { - m_freem(c->cdce_mbuf); - c->cdce_mbuf = NULL; - } - - if (err) - ifp->if_oerrors++; - else - ifp->if_opackets++; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - cdce_start_locked(ifp); - -out: - mutex_exit(&sc->cdce_txlock); + usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0); } -static void -cdce_tick(void *xsc) +static unsigned +cdce_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - struct cdce_softc * const sc = xsc; - - if (sc == NULL) - return; - - mutex_enter(&sc->cdce_lock); + /* Zaurus wants a 32-bit CRC appended to every frame */ + uint32_t crc; + unsigned extra = 0; + unsigned length; - if (sc->cdce_stopping || sc->cdce_dying) { - mutex_exit(&sc->cdce_lock); - return; - } + usbnet_isowned_tx(un); - /* Perform periodic stuff in process context */ - usb_add_task(sc->cdce_udev, &sc->cdce_tick_task, USB_TASKQ_DRIVER); - - mutex_exit(&sc->cdce_lock); -} - -static void -cdce_tick_task(void *xsc) -{ - struct cdce_softc * const sc = xsc; - struct ifnet *ifp; - - if (sc == NULL) - return; + if (un->un_flags & CDCE_ZAURUS) + extra = sizeof(crc); - mutex_enter(&sc->cdce_lock); + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - extra) + return 0; + length = m->m_pkthdr.len + extra; - if (sc->cdce_stopping || sc->cdce_dying) { - mutex_exit(&sc->cdce_lock); - return; + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf); + if (un->un_flags & CDCE_ZAURUS) { + crc = htole32(~ether_crc32_le(c->unc_buf, m->m_pkthdr.len)); + memcpy(c->unc_buf + m->m_pkthdr.len, &crc, sizeof(crc)); } - ifp = GET_IFP(sc); - - if (sc->cdce_timer != 0 && --sc->cdce_timer == 0) - cdce_watchdog(ifp); - if (!sc->cdce_stopping && !sc->cdce_dying) - callout_schedule(&sc->cdce_stat_ch, hz); - - mutex_exit(&sc->cdce_lock); + return length; } -int -cdce_activate(device_t self, enum devact act) -{ - struct cdce_softc *sc = device_private(self); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(GET_IFP(sc)); - - mutex_enter(&sc->cdce_lock); - sc->cdce_dying = true; - mutex_exit(&sc->cdce_lock); - - mutex_enter(&sc->cdce_rxlock); - mutex_enter(&sc->cdce_txlock); - sc->cdce_stopping = true; - mutex_exit(&sc->cdce_txlock); - mutex_exit(&sc->cdce_rxlock); +#ifdef _MODULE +#include "ioconf.c" +#endif - return 0; - default: - return EOPNOTSUPP; - } -} +USBNET_MODULE(cdce) Index: usb/if_cue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_cue.c,v retrieving revision 1.84 diff -p -u -u -r1.84 if_cue.c --- usb/if_cue.c 28 May 2019 07:41:50 -0000 1.84 +++ usb/if_cue.c 29 Aug 2019 06:22:05 -0000 @@ -1,4 +1,5 @@ /* $NetBSD: if_cue.c,v 1.84 2019/05/28 07:41:50 msaitoh Exp $ */ + /* * Copyright (c) 1997, 1998, 1999, 2000 * Bill Paul . All rights reserved. @@ -64,34 +65,15 @@ __KERNEL_RCSID(0, "$NetBSD: if_cue.c,v 1 #endif #include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include + +#include +#include #ifdef INET #include #include #endif -#include -#include -#include -#include -#include - -#include - #ifdef CUE_DEBUG #define DPRINTF(x) if (cuedebug) printf x #define DPRINTFN(n, x) if (cuedebug >= (n)) printf x @@ -101,10 +83,48 @@ int cuedebug = 0; #define DPRINTFN(n, x) #endif +#define CUE_BUFSZ 1536 +#define CUE_MIN_FRAMELEN 60 +#define CUE_RX_FRAMES 1 +#define CUE_TX_FRAMES 1 + +#define CUE_CONFIG_NO 1 +#define CUE_IFACE_IDX 0 + +#define CUE_RX_LIST_CNT 1 +#define CUE_TX_LIST_CNT 1 + +struct cue_type { + uint16_t cue_vid; + uint16_t cue_did; +}; + +struct cue_softc; + +struct cue_chain { + struct cue_softc *cue_sc; + struct usbd_xfer *cue_xfer; + char *cue_buf; + struct mbuf *cue_mbuf; + int cue_idx; +}; + +struct cue_cdata { + struct cue_chain cue_tx_chain[CUE_TX_LIST_CNT]; + struct cue_chain cue_rx_chain[CUE_RX_LIST_CNT]; + int cue_tx_prod; + int cue_tx_cnt; +}; + +struct cue_softc { + struct usbnet cue_un; + uint8_t cue_mctab[CUE_MCAST_TABLE_LEN]; +}; + /* * Various supported device vendors/products. */ -Static struct usb_devno cue_devs[] = { +static struct usb_devno cue_devs[] = { { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE }, { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 }, { USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK }, @@ -114,54 +134,36 @@ Static struct usb_devno cue_devs[] = { int cue_match(device_t, cfdata_t, void *); void cue_attach(device_t, device_t, void *); -int cue_detach(device_t, int); -int cue_activate(device_t, enum devact); CFATTACH_DECL_NEW(cue, sizeof(struct cue_softc), cue_match, cue_attach, - cue_detach, cue_activate); + usbnet_detach, usbnet_activate); -Static int cue_open_pipes(struct cue_softc *); -Static int cue_tx_list_init(struct cue_softc *); -Static int cue_rx_list_init(struct cue_softc *); -Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *); -Static int cue_send(struct cue_softc *, struct mbuf *, int); -Static void cue_rxeof(struct usbd_xfer *, void *, usbd_status); -Static void cue_txeof(struct usbd_xfer *, void *, usbd_status); -Static void cue_tick(void *); -Static void cue_tick_task(void *); -Static void cue_start(struct ifnet *); -Static int cue_ioctl(struct ifnet *, u_long, void *); -Static void cue_init(void *); -Static void cue_stop(struct cue_softc *); -Static void cue_watchdog(struct ifnet *); - -Static void cue_setmulti(struct cue_softc *); -Static uint32_t cue_crc(const char *); -Static void cue_reset(struct cue_softc *); - -Static int cue_csr_read_1(struct cue_softc *, int); -Static int cue_csr_write_1(struct cue_softc *, int, int); -Static int cue_csr_read_2(struct cue_softc *, int); -#if 0 -Static int cue_csr_write_2(struct cue_softc *, int, int); -#endif -Static int cue_mem(struct cue_softc *, int, int, void *, int); -Static int cue_getmac(struct cue_softc *, void *); - -#define CUE_SETBIT(sc, reg, x) \ - cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x)) - -#define CUE_CLRBIT(sc, reg, x) \ - cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x)) +static unsigned cue_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void cue_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static int cue_ioctl_cb(struct ifnet *, u_long, void *); +static void cue_stop_cb(struct ifnet *, int); +static int cue_init(struct ifnet *); +static void cue_tick(struct usbnet *); + +static struct usbnet_ops cue_ops = { + .uno_stop = cue_stop_cb, + .uno_ioctl = cue_ioctl_cb, + .uno_tx_prepare = cue_tx_prepare, + .uno_rx_loop = cue_rx_loop, + .uno_init = cue_init, + .uno_tick = cue_tick, +}; -Static int -cue_csr_read_1(struct cue_softc *sc, int reg) +#ifdef CUE_DEBUG +static int +cue_csr_read_1(struct usbnet *un, int reg) { usb_device_request_t req; usbd_status err; uint8_t val = 0; - if (sc->cue_dying) + if (usbnet_isdying(un)) return 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -170,28 +172,29 @@ cue_csr_read_1(struct cue_softc *sc, int USETW(req.wIndex, reg); USETW(req.wLength, 1); - err = usbd_do_request(sc->cue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n", - device_xname(sc->cue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return 0; } DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n", - device_xname(sc->cue_dev), reg, val)); + device_xname(un->un_dev), reg, val)); return val; } +#endif -Static int -cue_csr_read_2(struct cue_softc *sc, int reg) +static int +cue_csr_read_2(struct usbnet *un, int reg) { usb_device_request_t req; usbd_status err; uWord val; - if (sc->cue_dying) + if (usbnet_isdying(un)) return 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -200,31 +203,31 @@ cue_csr_read_2(struct cue_softc *sc, int USETW(req.wIndex, reg); USETW(req.wLength, 2); - err = usbd_do_request(sc->cue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n", - device_xname(sc->cue_dev), reg, UGETW(val))); + device_xname(un->un_dev), reg, UGETW(val))); if (err) { DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n", - device_xname(sc->cue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return 0; } return UGETW(val); } -Static int -cue_csr_write_1(struct cue_softc *sc, int reg, int val) +static int +cue_csr_write_1(struct usbnet *un, int reg, int val) { usb_device_request_t req; usbd_status err; - if (sc->cue_dying) + if (usbnet_isdying(un)) return 0; DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n", - device_xname(sc->cue_dev), reg, val)); + device_xname(un->un_dev), reg, val)); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = CUE_CMD_WRITEREG; @@ -232,34 +235,34 @@ cue_csr_write_1(struct cue_softc *sc, in USETW(req.wIndex, reg); USETW(req.wLength, 0); - err = usbd_do_request(sc->cue_udev, &req, NULL); + err = usbd_do_request(un->un_udev, &req, NULL); if (err) { DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n", - device_xname(sc->cue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return -1; } DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n", - device_xname(sc->cue_dev), reg, cue_csr_read_1(sc, reg))); + device_xname(un->un_dev), reg, cue_csr_read_1(un, reg))); return 0; } #if 0 -Static int -cue_csr_write_2(struct cue_softc *sc, int reg, int aval) +static int +cue_csr_write_2(struct usbnet *un, int reg, int aval) { usb_device_request_t req; usbd_status err; uWord val; int s; - if (sc->cue_dying) + if (usbnet_isdying(un)) return 0; DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n", - device_xname(sc->cue_dev), reg, aval)); + device_xname(un->un_dev), reg, aval)); USETW(val, aval); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; @@ -268,11 +271,11 @@ cue_csr_write_2(struct cue_softc *sc, in USETW(req.wIndex, reg); USETW(req.wLength, 0); - err = usbd_do_request(sc->cue_udev, &req, NULL); + err = usbd_do_request(un->un_udev, &req, NULL); if (err) { DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n", - device_xname(sc->cue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return -1; } @@ -280,14 +283,14 @@ cue_csr_write_2(struct cue_softc *sc, in } #endif -Static int -cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len) +static int +cue_mem(struct usbnet *un, int cmd, int addr, void *buf, int len) { usb_device_request_t req; usbd_status err; DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n", - device_xname(sc->cue_dev), cmd, addr, len)); + device_xname(un->un_dev), cmd, addr, len)); if (cmd == CUE_CMD_READSRAM) req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -298,24 +301,24 @@ cue_mem(struct cue_softc *sc, int cmd, i USETW(req.wIndex, addr); USETW(req.wLength, len); - err = usbd_do_request(sc->cue_udev, &req, buf); + err = usbd_do_request(un->un_udev, &req, buf); if (err) { DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n", - device_xname(sc->cue_dev), addr, usbd_errstr(err))); + device_xname(un->un_dev), addr, usbd_errstr(err))); return -1; } return 0; } -Static int -cue_getmac(struct cue_softc *sc, void *buf) +static int +cue_getmac(struct usbnet *un) { usb_device_request_t req; usbd_status err; - DPRINTFN(10,("%s: cue_getmac\n", device_xname(sc->cue_dev))); + DPRINTFN(10,("%s: cue_getmac\n", device_xname(un->un_dev))); req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = CUE_CMD_GET_MACADDR; @@ -323,11 +326,11 @@ cue_getmac(struct cue_softc *sc, void *b USETW(req.wIndex, 0); USETW(req.wLength, ETHER_ADDR_LEN); - err = usbd_do_request(sc->cue_udev, &req, buf); + err = usbd_do_request(un->un_udev, &req, un->un_eaddr); if (err) { printf("%s: read MAC address failed\n", - device_xname(sc->cue_dev)); + device_xname(un->un_dev)); return -1; } @@ -337,7 +340,7 @@ cue_getmac(struct cue_softc *sc, void *b #define CUE_POLY 0xEDB88320 #define CUE_BITS 9 -Static uint32_t +static uint32_t cue_crc(const char *addr) { uint32_t idx, bit, data, crc; @@ -353,26 +356,25 @@ cue_crc(const char *addr) return crc & ((1 << CUE_BITS) - 1); } -Static void -cue_setmulti(struct cue_softc *sc) +static void +cue_setiff(struct usbnet *un) { - struct ethercom *ec = &sc->cue_ec; - struct ifnet *ifp; + struct cue_softc *sc = usbnet_softc(un); + struct ethercom *ec = usbnet_ec(un); + struct ifnet *ifp = usbnet_ifp(un); struct ether_multi *enm; struct ether_multistep step; uint32_t h, i; - ifp = GET_IFP(sc); - - DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n", - device_xname(sc->cue_dev), ifp->if_flags)); + DPRINTFN(2,("%s: cue_setiff if_flags=0x%x\n", + device_xname(un->un_dev), ifp->if_flags)); if (ifp->if_flags & IFF_PROMISC) { allmulti: ifp->if_flags |= IFF_ALLMULTI; for (i = 0; i < CUE_MCAST_TABLE_LEN; i++) sc->cue_mctab[i] = 0xFF; - cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, + cue_mem(un, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &sc->cue_mctab, CUE_MCAST_TABLE_LEN); return; } @@ -408,19 +410,19 @@ allmulti: sc->cue_mctab[h >> 3] |= 1 << (h & 0x7); } - cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, + cue_mem(un, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &sc->cue_mctab, CUE_MCAST_TABLE_LEN); } -Static void -cue_reset(struct cue_softc *sc) +static void +cue_reset(struct usbnet *un) { usb_device_request_t req; usbd_status err; - DPRINTFN(2,("%s: cue_reset\n", device_xname(sc->cue_dev))); + DPRINTFN(2,("%s: cue_reset\n", device_xname(un->un_dev))); - if (sc->cue_dying) + if (usbnet_isdying(un)) return; req.bmRequestType = UT_WRITE_VENDOR_DEVICE; @@ -429,13 +431,13 @@ cue_reset(struct cue_softc *sc) USETW(req.wIndex, 0); USETW(req.wLength, 0); - err = usbd_do_request(sc->cue_udev, &req, NULL); + err = usbd_do_request(un->un_udev, &req, NULL); if (err) - printf("%s: reset failed\n", device_xname(sc->cue_dev)); + printf("%s: reset failed\n", device_xname(un->un_dev)); /* Wait a little while for the chip to get its brains in order. */ - usbd_delay_ms(sc->cue_udev, 1); + usbd_delay_ms(un->un_udev, 1); } /* @@ -458,25 +460,21 @@ void cue_attach(device_t parent, device_t self, void *aux) { struct cue_softc *sc = device_private(self); + struct usbnet * const un = &sc->cue_un; struct usb_attach_arg *uaa = aux; char *devinfop; - int s; - u_char eaddr[ETHER_ADDR_LEN]; struct usbd_device * dev = uaa->uaa_device; - struct usbd_interface * iface; usbd_status err; - struct ifnet *ifp; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev)); + KASSERT((void *)sc == un); - sc->cue_dev = self; + DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev)); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); @@ -488,809 +486,222 @@ cue_attach(device_t parent, device_t sel return; } - sc->cue_udev = dev; - sc->cue_product = uaa->uaa_product; - sc->cue_vendor = uaa->uaa_vendor; + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &cue_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = CUE_RX_LIST_CNT; + un->un_tx_list_cnt = CUE_TX_LIST_CNT; + un->un_rx_bufsz = CUE_BUFSZ; + un->un_tx_bufsz = CUE_BUFSZ; - usb_init_task(&sc->cue_tick_task, cue_tick_task, sc, 0); - usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc, 0); - - err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface); + err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->cue_iface = iface; - id = usbd_get_interface_descriptor(iface); + id = usbd_get_interface_descriptor(un->un_iface); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } } + /* First level attach. */ + usbnet_attach(un, "cuedet"); + #if 0 /* Reset the adapter. */ - cue_reset(sc); + cue_reset(un); #endif /* * Get station address. */ - cue_getmac(sc, &eaddr); - - s = splnet(); - - /* - * A CATC chip was detected. Inform the world. - */ - aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); + cue_getmac(un); - /* Initialize interface info.*/ - ifp = GET_IFP(sc); - ifp->if_softc = sc; - ifp->if_mtu = ETHERMTU; - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_ioctl = cue_ioctl; - ifp->if_start = cue_start; - ifp->if_watchdog = cue_watchdog; - strlcpy(ifp->if_xname, device_xname(sc->cue_dev), IFNAMSIZ); - - IFQ_SET_READY(&ifp->if_snd); - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, eaddr); - rnd_attach_source(&sc->rnd_source, device_xname(sc->cue_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&(sc->cue_stat_ch), 0); - - sc->cue_attached = 1; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev, sc->cue_dev); - - return; + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, NULL); } -int -cue_detach(device_t self, int flags) +static void +cue_tick(struct usbnet *un) { - struct cue_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - int s; - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__)); - - /* - * XXX Halting callout guarantees no more tick tasks. What - * guarantees no more stop tasks? What guarantees no more - * calls to cue_send? Don't we need to wait for if_detach or - * something? Should we set sc->cue_dying here? Is device - * deactivation guaranteed to have already happened? - */ - callout_halt(&sc->cue_stat_ch, NULL); - usb_rem_task_wait(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER, - NULL); - usb_rem_task_wait(sc->cue_udev, &sc->cue_stop_task, USB_TASKQ_DRIVER, - NULL); - - if (!sc->cue_attached) { - /* Detached before attached finished, so just bail out. */ - return 0; - } - - s = splusb(); - - if (ifp->if_flags & IFF_RUNNING) - cue_stop(sc); - - rnd_detach_source(&sc->rnd_source); - ether_ifdetach(ifp); + struct ifnet *ifp = usbnet_ifp(un); - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->cue_ep[CUE_ENDPT_TX] != NULL || - sc->cue_ep[CUE_ENDPT_RX] != NULL || - sc->cue_ep[CUE_ENDPT_INTR] != NULL) - aprint_debug_dev(self, "detach has active endpoints\n"); -#endif - - sc->cue_attached = 0; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, sc->cue_dev); - - return 0; -} - -int -cue_activate(device_t self, enum devact act) -{ - struct cue_softc *sc = device_private(self); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__)); - - switch (act) { - case DVACT_DEACTIVATE: - /* Deactivate the interface. */ - if_deactivate(&sc->cue_ec.ec_if); - sc->cue_dying = 1; - return 0; - default: - return EOPNOTSUPP; - } -} - -/* - * Initialize an RX descriptor and attach an MBUF cluster. - */ -Static int -cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m) -{ - struct mbuf *m_new = NULL; - - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->cue_dev)); - return ENOBUFS; - } - - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->cue_dev)); - m_freem(m_new); - return ENOBUFS; - } - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - } else { - m_new = m; - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - m_new->m_data = m_new->m_ext.ext_buf; - } - - m_adj(m_new, ETHER_ALIGN); - c->cue_mbuf = m_new; - - return 0; -} - -Static int -cue_rx_list_init(struct cue_softc *sc) -{ - struct cue_cdata *cd; - struct cue_chain *c; - int i; - - cd = &sc->cue_cdata; - for (i = 0; i < CUE_RX_LIST_CNT; i++) { - c = &cd->cue_rx_chain[i]; - c->cue_sc = sc; - c->cue_idx = i; - if (cue_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->cue_xfer == NULL) { - int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_RX], - CUE_BUFSZ, 0, 0, &c->cue_xfer); - if (error) - return error; - c->cue_buf = usbd_get_buffer(c->cue_xfer); - } - } - - return 0; -} - -Static int -cue_tx_list_init(struct cue_softc *sc) -{ - struct cue_cdata *cd; - struct cue_chain *c; - int i; - - cd = &sc->cue_cdata; - for (i = 0; i < CUE_TX_LIST_CNT; i++) { - c = &cd->cue_tx_chain[i]; - c->cue_sc = sc; - c->cue_idx = i; - c->cue_mbuf = NULL; - if (c->cue_xfer == NULL) { - int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_TX], - CUE_BUFSZ, 0, 0, &c->cue_xfer); - if (error) - return error; - c->cue_buf = usbd_get_buffer(c->cue_xfer); - } - } + if (cue_csr_read_2(un, CUE_RX_FRAMEERR)) + ifp->if_ierrors++; - return 0; + ifp->if_collisions += cue_csr_read_2(un, CUE_TX_SINGLECOLL); + ifp->if_collisions += cue_csr_read_2(un, CUE_TX_MULTICOLL); + ifp->if_collisions += cue_csr_read_2(un, CUE_TX_EXCESSCOLL); } -/* - * A frame has been uploaded: pass the resulting mbuf chain up to - * the higher level protocols. - */ -Static void -cue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +static void +cue_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct cue_chain *c = priv; - struct cue_softc *sc = c->cue_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - int total_len = 0; + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; uint16_t len; - int s; - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev), - __func__, status)); - - if (sc->cue_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->cue_rx_errs++; - if (usbd_ratecheck(&sc->cue_rx_notice)) { - printf("%s: %u usb errors on rx: %s\n", - device_xname(sc->cue_dev), sc->cue_rx_errs, - usbd_errstr(status)); - sc->cue_rx_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - - memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len); - - m = c->cue_mbuf; - len = UGETW(mtod(m, uint8_t *)); - - /* No errors; receive the packet. */ - total_len = len; - - if (len < sizeof(struct ether_header)) { - ifp->if_ierrors++; - goto done; - } - - m_adj(m, sizeof(uint16_t)); - m->m_pkthdr.len = m->m_len = total_len; - - m_set_rcvif(m, ifp); - s = splnet(); - - /* XXX ugly */ - if (cue_newbuf(sc, c, NULL) == ENOBUFS) { + DPRINTFN(5,("%s: %s: total_len=%d len=%d\n", + device_xname(un->un_dev), __func__, + total_len, le16dec(buf))); + + len = UGETW(buf); + if (total_len < 2 || + len > total_len - 2 || + len < sizeof(struct ether_header)) { ifp->if_ierrors++; - goto done1; - } - - DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->cue_dev), - __func__, m->m_len)); - if_percpuq_enqueue(ifp->if_percpuq, m); - done1: - splx(s); - -done: - - /* Setup new transfer. */ - usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof); - usbd_transfer(c->cue_xfer); - - DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->cue_dev), - __func__)); -} - -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ -Static void -cue_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct cue_chain *c = priv; - struct cue_softc *sc = c->cue_sc; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (sc->cue_dying) - return; - - s = splnet(); - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev), - __func__, status)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->cue_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]); - splx(s); return; } - ifp->if_opackets++; - - m_freem(c->cue_mbuf); - c->cue_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - cue_start(ifp); - - splx(s); -} - -Static void -cue_tick(void *xsc) -{ - struct cue_softc *sc = xsc; - - if (sc == NULL) - return; - - if (sc->cue_dying) - return; - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__)); - - /* Perform statistics update in process context. */ - usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER); + /* No errors; receive the packet. */ + usbnet_enqueue(un, buf + 2, len, 0, 0, 0); } -Static void -cue_tick_task(void *xsc) +static unsigned +cue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - struct cue_softc *sc = xsc; - struct ifnet *ifp; - - if (sc->cue_dying) - return; - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__)); - - ifp = GET_IFP(sc); - - ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL); - ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL); - ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL); - - if (cue_csr_read_2(sc, CUE_RX_FRAMEERR)) - ifp->if_ierrors++; -} + unsigned total_len; -Static int -cue_send(struct cue_softc *sc, struct mbuf *m, int idx) -{ - int total_len; - struct cue_chain *c; - usbd_status err; + DPRINTFN(5,("%s: %s: mbuf len=%d\n", + device_xname(un->un_dev), __func__, + m->m_pkthdr.len)); - c = &sc->cue_cdata.cue_tx_chain[idx]; + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2) + return 0; /* * Copy the mbuf data into a contiguous buffer, leaving two * bytes at the beginning to hold the frame length. */ - m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2); - c->cue_mbuf = m; + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + 2); total_len = m->m_pkthdr.len + 2; - DPRINTFN(10,("%s: %s: total_len=%d\n", - device_xname(sc->cue_dev), __func__, total_len)); - /* The first two bytes are the frame length */ - c->cue_buf[0] = (uint8_t)m->m_pkthdr.len; - c->cue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); - - /* XXX 10000 */ - usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, total_len, 0, 10000, - cue_txeof); - - /* Transmit */ - err = usbd_transfer(c->cue_xfer); - if (err != USBD_IN_PROGRESS) { - printf("%s: cue_send error=%s\n", device_xname(sc->cue_dev), - usbd_errstr(err)); - /* Stop the interface from process context. */ - usb_add_task(sc->cue_udev, &sc->cue_stop_task, - USB_TASKQ_DRIVER); - return EIO; - } + c->unc_buf[0] = (uint8_t)m->m_pkthdr.len; + c->unc_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); - sc->cue_cdata.cue_tx_cnt++; - - return 0; + return total_len; } -Static void -cue_start(struct ifnet *ifp) +static int +cue_init_locked(struct ifnet *ifp) { - struct cue_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - if (sc->cue_dying) - return; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__)); - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (cue_send(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } - - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - /* - * If there's a BPF listener, bounce a copy of this frame - * to him. - */ - bpf_mtap(ifp, m_head, BPF_D_OUT); - - ifp->if_flags |= IFF_OACTIVE; - - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 5; -} - -Static void -cue_init(void *xsc) -{ - struct cue_softc *sc = xsc; - struct ifnet *ifp = GET_IFP(sc); - int i, s, ctl; + struct usbnet * const un = ifp->if_softc; + int i, ctl; const u_char *eaddr; - if (sc->cue_dying) - return; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__)); + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); - if (ifp->if_flags & IFF_RUNNING) - return; + if (usbnet_isdying(un)) + return -1; - s = splnet(); + /* Cancel pending I/O */ + usbnet_stop(un, ifp, 1); - /* - * Cancel pending I/O and free all RX/TX buffers. - */ + /* Reset the interface. */ #if 1 - cue_reset(sc); + cue_reset(un); #endif /* Set advanced operation modes. */ - cue_csr_write_1(sc, CUE_ADVANCED_OPMODES, + cue_csr_write_1(un, CUE_ADVANCED_OPMODES, CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */ eaddr = CLLADDR(ifp->if_sadl); /* Set MAC address */ for (i = 0; i < ETHER_ADDR_LEN; i++) - cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]); + cue_csr_write_1(un, CUE_PAR0 - i, eaddr[i]); /* Enable RX logic. */ ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON; if (ifp->if_flags & IFF_PROMISC) ctl |= CUE_ETHCTL_PROMISC; - cue_csr_write_1(sc, CUE_ETHCTL, ctl); + cue_csr_write_1(un, CUE_ETHCTL, ctl); /* Load the multicast filter. */ - cue_setmulti(sc); + cue_setiff(un); /* * Set the number of RX and TX buffers that we want * to reserve inside the ASIC. */ - cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES); - cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES); + cue_csr_write_1(un, CUE_RX_BUFPKTS, CUE_RX_FRAMES); + cue_csr_write_1(un, CUE_TX_BUFPKTS, CUE_TX_FRAMES); /* Set advanced operation modes. */ - cue_csr_write_1(sc, CUE_ADVANCED_OPMODES, + cue_csr_write_1(un, CUE_ADVANCED_OPMODES, CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */ /* Program the LED operation. */ - cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK); + cue_csr_write_1(un, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK); - if (sc->cue_ep[CUE_ENDPT_RX] == NULL) { - if (cue_open_pipes(sc)) { - splx(s); - return; - } - } - /* Init TX ring. */ - if (cue_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->cue_dev)); - splx(s); - return; - } - - /* Init RX ring. */ - if (cue_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->cue_dev)); - splx(s); - return; - } - - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); - - callout_reset(&(sc->cue_stat_ch), (hz), (cue_tick), (sc)); + return usbnet_init_rx_tx(un); } -Static int -cue_open_pipes(struct cue_softc *sc) +static int +cue_init(struct ifnet *ifp) { - struct cue_chain *c; - usbd_status err; - int i; - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX], - USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]); - if (err) { - printf("%s: open rx pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX], - USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - return EIO; - } + struct usbnet * const un = ifp->if_softc; + int rv; - /* Start up the receive pipe. */ - for (i = 0; i < CUE_RX_LIST_CNT; i++) { - c = &sc->cue_cdata.cue_rx_chain[i]; - - usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof); - usbd_transfer(c->cue_xfer); - } + usbnet_lock(un); + rv = cue_init_locked(ifp); + usbnet_unlock(un); - return 0; + return rv; } -Static int -cue_ioctl(struct ifnet *ifp, u_long command, void *data) +static int +cue_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct cue_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; - - if (sc->cue_dying) - return EIO; - - s = splnet(); - - switch (command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - cue_init(sc); + struct usbnet * const un = ifp->if_softc; - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - arp_ifinit(ifp, ifa); - break; -#endif /* INET */ - } - break; - - case SIOCSIFMTU: - if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) - error = EINVAL; - else if ((error = ifioctl_common(ifp, command, data)) - == ENETRESET) - error = 0; - break; - - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - if (ifp->if_flags & IFF_UP) { - if (ifp->if_flags & IFF_RUNNING && - ifp->if_flags & IFF_PROMISC && - !(sc->cue_if_flags & IFF_PROMISC)) { - CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); - cue_setmulti(sc); - } else if (ifp->if_flags & IFF_RUNNING && - !(ifp->if_flags & IFF_PROMISC) && - sc->cue_if_flags & IFF_PROMISC) { - CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); - cue_setmulti(sc); - } else if (!(ifp->if_flags & IFF_RUNNING)) - cue_init(sc); - } else { - if (ifp->if_flags & IFF_RUNNING) - cue_stop(sc); - } - sc->cue_if_flags = ifp->if_flags; - error = 0; - break; + switch (cmd) { case SIOCADDMULTI: case SIOCDELMULTI: - cue_setmulti(sc); - error = 0; + cue_setiff(un); break; default: - error = ether_ioctl(ifp, command, data); break; } - splx(s); - - return error; + return 0; } -Static void -cue_watchdog(struct ifnet *ifp) +/* Stop and reset the adapter. */ +static void +cue_stop_cb(struct ifnet *ifp, int disable) { - struct cue_softc *sc = ifp->if_softc; - struct cue_chain *c; - usbd_status stat; - int s; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__)); + struct usbnet * const un = ifp->if_softc; - if (sc->cue_dying) - return; - - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->cue_dev)); + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - s = splusb(); - c = &sc->cue_cdata.cue_tx_chain[0]; - usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat); - cue_txeof(c->cue_xfer, c, stat); - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - cue_start(ifp); - splx(s); + cue_csr_write_1(un, CUE_ETHCTL, 0); + cue_reset(un); } -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -Static void -cue_stop(struct cue_softc *sc) -{ - usbd_status err; - struct ifnet *ifp; - int i; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__)); - - ifp = GET_IFP(sc); - ifp->if_timer = 0; - - cue_csr_write_1(sc, CUE_ETHCTL, 0); - cue_reset(sc); - callout_stop(&sc->cue_stat_ch); - - /* Stop transfers. */ - if (sc->cue_ep[CUE_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]); - if (err) { - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - } - } - - if (sc->cue_ep[CUE_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]); - if (err) { - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - } - } - - if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < CUE_RX_LIST_CNT; i++) { - if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) { - usbd_destroy_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer); - sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < CUE_TX_LIST_CNT; i++) { - if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) { - m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf); - sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL; - } - if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) { - usbd_destroy_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer); - sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL; - } - } - - /* Stop transfers. */ - if (sc->cue_ep[CUE_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - } - sc->cue_ep[CUE_ENDPT_RX] = NULL; - } - - if (sc->cue_ep[CUE_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - } - sc->cue_ep[CUE_ENDPT_TX] = NULL; - } - - if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->cue_dev), usbd_errstr(err)); - } - sc->cue_ep[CUE_ENDPT_INTR] = NULL; - } +#ifdef _MODULE +#include "ioconf.c" +#endif - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); -} +USBNET_MODULE(cue) Index: usb/if_cuereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_cuereg.h,v retrieving revision 1.22 diff -p -u -u -r1.22 if_cuereg.h --- usb/if_cuereg.h 21 Jul 2019 10:27:56 -0000 1.22 +++ usb/if_cuereg.h 29 Aug 2019 06:22:05 -0000 @@ -117,76 +117,5 @@ #define CUE_MCAST_TABLE_ADDR 0xFA80 #define CUE_MCAST_TABLE_LEN 64 -#define CUE_TIMEOUT 1000 -#define CUE_BUFSZ 1536 -#define CUE_MIN_FRAMELEN 60 -#define CUE_RX_FRAMES 1 -#define CUE_TX_FRAMES 1 - -#define CUE_RX_LIST_CNT 1 -#define CUE_TX_LIST_CNT 1 - #define CUE_CTL_READ 0x01 #define CUE_CTL_WRITE 0x02 - -#define CUE_CONFIG_NO 1 -#define CUE_IFACE_IDX 0 - -/* - * The interrupt endpoint is currently unused by the CATC part. - */ -#define CUE_ENDPT_RX 0x0 -#define CUE_ENDPT_TX 0x1 -#define CUE_ENDPT_INTR 0x2 -#define CUE_ENDPT_MAX 0x3 - -struct cue_type { - uint16_t cue_vid; - uint16_t cue_did; -}; - -struct cue_softc; - -struct cue_chain { - struct cue_softc *cue_sc; - struct usbd_xfer *cue_xfer; - char *cue_buf; - struct mbuf *cue_mbuf; - int cue_idx; -}; - -struct cue_cdata { - struct cue_chain cue_tx_chain[CUE_TX_LIST_CNT]; - struct cue_chain cue_rx_chain[CUE_RX_LIST_CNT]; - int cue_tx_prod; - int cue_tx_cnt; -}; - -struct cue_softc { - device_t cue_dev; - - struct ethercom cue_ec; - krndsource_t rnd_source; -#define GET_IFP(sc) (&(sc)->cue_ec.ec_if) - - struct callout cue_stat_ch; - - struct usbd_device * cue_udev; - struct usbd_interface * cue_iface; - uint16_t cue_vendor; - uint16_t cue_product; - int cue_ed[CUE_ENDPT_MAX]; - struct usbd_pipe * cue_ep[CUE_ENDPT_MAX]; - uint8_t cue_mctab[CUE_MCAST_TABLE_LEN]; - int cue_if_flags; - uint16_t cue_rxfilt; - struct cue_cdata cue_cdata; - - char cue_dying; - char cue_attached; - u_int cue_rx_errs; - struct timeval cue_rx_notice; - - struct usb_task cue_tick_task; - struct usb_task cue_stop_task; -}; Index: usb/if_kue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_kue.c,v retrieving revision 1.96 diff -p -u -u -r1.96 if_kue.c --- usb/if_kue.c 28 May 2019 07:41:50 -0000 1.96 +++ usb/if_kue.c 29 Aug 2019 06:22:05 -0000 @@ -79,33 +79,15 @@ __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1 #endif #include -#include -#include -#include #include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include + +#include #ifdef INET #include #include #endif -#include -#include -#include -#include -#include - #include #include @@ -118,6 +100,28 @@ int kuedebug = 0; #define DPRINTFN(n, x) #endif +struct kue_type { + uint16_t kue_vid; + uint16_t kue_did; +}; + +struct kue_softc { + struct usbnet kue_un; + + struct kue_ether_desc kue_desc; + uint16_t kue_rxfilt; + uint8_t *kue_mcfilters; +}; + +#define KUE_MCFILT(x, y) \ + (uint8_t *)&(sc->kue_mcfilters[y * ETHER_ADDR_LEN]) + +#define KUE_BUFSZ 1536 +#define KUE_MIN_FRAMELEN 60 + +#define KUE_RX_LIST_CNT 1 +#define KUE_TX_LIST_CNT 1 + /* * Various supported device vendors/products. */ @@ -127,6 +131,8 @@ static const struct usb_devno kue_devs[] { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 }, { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT }, { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX }, + { USB_VENDOR_ACTIONTEC, USB_PRODUCT_ACTIONTEC_KL5KUSB101 }, + { USB_VENDOR_ALLIEDTELESYN, USB_PRODUCT_ALLIEDTELESYN_AT_USB10 }, { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 }, { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA }, { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T }, @@ -153,6 +159,7 @@ static const struct usb_devno kue_devs[] { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA }, { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA }, { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E }, + { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_GPE }, { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB }, }; #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p)) @@ -160,37 +167,36 @@ static const struct usb_devno kue_devs[] int kue_match(device_t, cfdata_t, void *); void kue_attach(device_t, device_t, void *); int kue_detach(device_t, int); -int kue_activate(device_t, enum devact); CFATTACH_DECL_NEW(kue, sizeof(struct kue_softc), kue_match, kue_attach, - kue_detach, kue_activate); + kue_detach, usbnet_activate); -static int kue_tx_list_init(struct kue_softc *); -static int kue_rx_list_init(struct kue_softc *); -static int kue_send(struct kue_softc *, struct mbuf *, int); -static int kue_open_pipes(struct kue_softc *); -static void kue_rxeof(struct usbd_xfer *, void *, usbd_status); -static void kue_txeof(struct usbd_xfer *, void *, usbd_status); -static void kue_start(struct ifnet *); -static int kue_ioctl(struct ifnet *, u_long, void *); -static void kue_init(void *); -static void kue_stop(struct kue_softc *); -static void kue_watchdog(struct ifnet *); +static void kue_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned kue_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static int kue_ioctl_cb(struct ifnet *, u_long, void *); +static int kue_init(struct ifnet *); + +static struct usbnet_ops kue_ops = { + .uno_ioctl = kue_ioctl_cb, + .uno_tx_prepare = kue_tx_prepare, + .uno_rx_loop = kue_rx_loop, + .uno_init = kue_init, +}; -static void kue_setmulti(struct kue_softc *); -static void kue_reset(struct kue_softc *); +static void kue_setiff(struct usbnet *); +static void kue_reset(struct usbnet *); -static usbd_status kue_ctl(struct kue_softc *, int, uint8_t, +static usbd_status kue_ctl(struct usbnet *, int, uint8_t, uint16_t, void *, uint32_t); -static usbd_status kue_setword(struct kue_softc *, uint8_t, uint16_t); -static int kue_load_fw(struct kue_softc *); +static int kue_load_fw(struct usbnet *); static usbd_status -kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word) +kue_setword(struct usbnet *un, uint8_t breq, uint16_t word) { usb_device_request_t req; - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = breq; @@ -198,16 +204,16 @@ kue_setword(struct kue_softc *sc, uint8_ USETW(req.wIndex, 0); USETW(req.wLength, 0); - return usbd_do_request(sc->kue_udev, &req, NULL); + return usbd_do_request(un->un_udev, &req, NULL); } static usbd_status -kue_ctl(struct kue_softc *sc, int rw, uint8_t breq, uint16_t val, +kue_ctl(struct usbnet *un, int rw, uint8_t breq, uint16_t val, void *data, uint32_t len) { usb_device_request_t req; - DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(sc->kue_dev), + DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(un->un_dev), __func__, len)); if (rw == KUE_CTL_WRITE) @@ -220,16 +226,16 @@ kue_ctl(struct kue_softc *sc, int rw, ui USETW(req.wIndex, 0); USETW(req.wLength, len); - return usbd_do_request(sc->kue_udev, &req, data); + return usbd_do_request(un->un_udev, &req, data); } static int -kue_load_fw(struct kue_softc *sc) +kue_load_fw(struct usbnet *un) { usb_device_descriptor_t dd; usbd_status err; - DPRINTFN(1,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(1,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); /* * First, check if we even need to load the firmware. @@ -245,54 +251,54 @@ kue_load_fw(struct kue_softc *sc) * it's probed while the firmware is still loaded and * running. */ - if (usbd_get_device_desc(sc->kue_udev, &dd)) + if (usbd_get_device_desc(un->un_udev, &dd)) return EIO; if (UGETW(dd.bcdDevice) == KUE_WARM_REV) { printf("%s: warm boot, no firmware download\n", - device_xname(sc->kue_dev)); + device_xname(un->un_dev)); return 0; } printf("%s: cold boot, downloading firmware\n", - device_xname(sc->kue_dev)); + device_xname(un->un_dev)); /* Load code segment */ DPRINTFN(1,("%s: kue_load_fw: download code_seg\n", - device_xname(sc->kue_dev))); + device_xname(un->un_dev))); /*XXXUNCONST*/ - err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, + err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, __UNCONST(kue_code_seg), sizeof(kue_code_seg)); if (err) { printf("%s: failed to load code segment: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); + device_xname(un->un_dev), usbd_errstr(err)); return EIO; } /* Load fixup segment */ DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n", - device_xname(sc->kue_dev))); + device_xname(un->un_dev))); /*XXXUNCONST*/ - err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, + err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, __UNCONST(kue_fix_seg), sizeof(kue_fix_seg)); if (err) { printf("%s: failed to load fixup segment: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); + device_xname(un->un_dev), usbd_errstr(err)); return EIO; } /* Send trigger command. */ DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n", - device_xname(sc->kue_dev))); + device_xname(un->un_dev))); /*XXXUNCONST*/ - err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, + err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, __UNCONST(kue_trig_seg), sizeof(kue_trig_seg)); if (err) { printf("%s: failed to load trigger segment: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); + device_xname(un->un_dev), usbd_errstr(err)); return EIO; } - usbd_delay_ms(sc->kue_udev, 10); + usbd_delay_ms(un->un_udev, 10); /* * Reload device descriptor. @@ -302,37 +308,44 @@ kue_load_fw(struct kue_softc *sc) * code. This confuses the quirk mechanism, which is * dependent on the revision data. */ - (void)usbd_reload_device_desc(sc->kue_udev); + (void)usbd_reload_device_desc(un->un_udev); - DPRINTFN(1,("%s: %s: done\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(1,("%s: %s: done\n", device_xname(un->un_dev), __func__)); /* Reset the adapter. */ - kue_reset(sc); + kue_reset(un); return 0; } static void -kue_setmulti(struct kue_softc *sc) +kue_setiff(struct usbnet *un) { - struct ethercom *ec = &sc->kue_ec; - struct ifnet *ifp = GET_IFP(sc); + struct ethercom * ec = usbnet_ec(un); + struct kue_softc * sc = usbnet_softc(un); + struct ifnet * const ifp = usbnet_ifp(un); struct ether_multi *enm; struct ether_multistep step; int i; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); + + /* If we want promiscuous mode, set the allframes bit. */ + if (ifp->if_flags & IFF_PROMISC) + sc->kue_rxfilt |= KUE_RXFILT_PROMISC; + else + sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC; if (ifp->if_flags & IFF_PROMISC) { allmulti: ifp->if_flags |= IFF_ALLMULTI; - sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI; + sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI|KUE_RXFILT_PROMISC; sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST; - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); + kue_setword(un, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); return; } - sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI; + sc->kue_rxfilt &= ~(KUE_RXFILT_ALLMULTI|KUE_RXFILT_PROMISC); i = 0; ETHER_LOCK(ec); @@ -354,10 +367,10 @@ allmulti: ifp->if_flags &= ~IFF_ALLMULTI; sc->kue_rxfilt |= KUE_RXFILT_MULTICAST; - kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS, + kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS, i, sc->kue_mcfilters, i * ETHER_ADDR_LEN); - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); + kue_setword(un, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); } /* @@ -366,17 +379,17 @@ allmulti: * bring it into proper operation. */ static void -kue_reset(struct kue_softc *sc) +kue_reset(struct usbnet *un) { - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) || - usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX, - &sc->kue_iface)) - printf("%s: reset failed\n", device_xname(sc->kue_dev)); + if (usbd_set_config_no(un->un_udev, KUE_CONFIG_NO, 1) || + usbd_device2interface_handle(un->un_udev, KUE_IFACE_IDX, + &un->un_iface)) + printf("%s: reset failed\n", device_xname(un->un_dev)); /* Wait a little while for the chip to get its brains in order. */ - usbd_delay_ms(sc->kue_udev, 10); + usbd_delay_ms(un->un_udev, 10); } /* @@ -401,28 +414,36 @@ void kue_attach(device_t parent, device_t self, void *aux) { struct kue_softc *sc = device_private(self); + struct usbnet * const un = &sc->kue_un; struct usb_attach_arg *uaa = aux; char *devinfop; - int s; - struct ifnet *ifp; struct usbd_device * dev = uaa->uaa_device; - struct usbd_interface * iface; usbd_status err; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev)); + KASSERT((void *)sc == un); - sc->kue_dev = self; + DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev)); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &kue_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = 0; + un->un_rx_list_cnt = KUE_RX_LIST_CNT; + un->un_tx_list_cnt = KUE_TX_LIST_CNT; + un->un_rx_bufsz = KUE_BUFSZ; + un->un_tx_bufsz = KUE_BUFSZ; + err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -430,102 +451,72 @@ kue_attach(device_t parent, device_t sel return; } - sc->kue_udev = dev; - sc->kue_product = uaa->uaa_product; - sc->kue_vendor = uaa->uaa_vendor; - /* Load the firmware into the NIC. */ - if (kue_load_fw(sc)) { + if (kue_load_fw(un)) { aprint_error_dev(self, "loading firmware failed\n"); return; } - err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface); + err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->kue_iface = iface; - id = usbd_get_interface_descriptor(iface); + id = usbd_get_interface_descriptor(un->un_iface); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress; + /* + * The interrupt endpoint is currently unused by the + * KLSI part. + */ + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } } - if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) { + if (un->un_ed[USBNET_ENDPT_RX] == 0 || + un->un_ed[USBNET_ENDPT_TX] == 0) { aprint_error_dev(self, "missing endpoint\n"); return; } + /* First level attach, so kue_ctl() works. */ + usbnet_attach(un, "kuedet"); + /* Read ethernet descriptor */ - err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, + err = kue_ctl(un, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, 0, &sc->kue_desc, sizeof(sc->kue_desc)); if (err) { aprint_error_dev(self, "could not read Ethernet descriptor\n"); return; } + memcpy(un->un_eaddr, sc->kue_desc.kue_macaddr, sizeof un->un_eaddr); sc->kue_mcfilters = kmem_alloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN, KM_SLEEP); - s = splnet(); - - /* - * A KLSI chip was detected. Inform the world. - */ - aprint_normal_dev(self, "Ethernet address %s\n", - ether_sprintf(sc->kue_desc.kue_macaddr)); - - /* Initialize interface info.*/ - ifp = GET_IFP(sc); - ifp->if_softc = sc; - ifp->if_mtu = ETHERMTU; - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_ioctl = kue_ioctl; - ifp->if_start = kue_start; - ifp->if_watchdog = kue_watchdog; - strlcpy(ifp->if_xname, device_xname(sc->kue_dev), IFNAMSIZ); - - IFQ_SET_READY(&ifp->if_snd); - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, sc->kue_desc.kue_macaddr); - rnd_attach_source(&sc->rnd_source, device_xname(sc->kue_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - sc->kue_attached = true; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev, sc->kue_dev); - - return; + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, NULL); } int kue_detach(device_t self, int flags) { struct kue_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - int s; - - s = splusb(); /* XXX why? */ if (sc->kue_mcfilters != NULL) { kmem_free(sc->kue_mcfilters, @@ -533,101 +524,7 @@ kue_detach(device_t self, int flags) sc->kue_mcfilters = NULL; } - if (!sc->kue_attached) { - /* Detached before attached finished, so just bail out. */ - splx(s); - return 0; - } - - if (ifp->if_flags & IFF_RUNNING) - kue_stop(sc); - - rnd_detach_source(&sc->rnd_source); - ether_ifdetach(ifp); - - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->kue_ep[KUE_ENDPT_TX] != NULL || - sc->kue_ep[KUE_ENDPT_RX] != NULL || - sc->kue_ep[KUE_ENDPT_INTR] != NULL) - aprint_debug_dev(self, "detach has active endpoints\n"); -#endif - - sc->kue_attached = false; - splx(s); - - return 0; -} - -int -kue_activate(device_t self, enum devact act) -{ - struct kue_softc *sc = device_private(self); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); - - switch (act) { - case DVACT_DEACTIVATE: - /* Deactivate the interface. */ - if_deactivate(&sc->kue_ec.ec_if); - sc->kue_dying = true; - return 0; - default: - return EOPNOTSUPP; - } -} - -static int -kue_rx_list_init(struct kue_softc *sc) -{ - struct kue_cdata *cd; - struct kue_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); - - cd = &sc->kue_cdata; - for (i = 0; i < KUE_RX_LIST_CNT; i++) { - c = &cd->kue_rx_chain[i]; - c->kue_sc = sc; - c->kue_idx = i; - if (c->kue_xfer == NULL) { - int error = usbd_create_xfer(sc->kue_ep[KUE_ENDPT_RX], - KUE_BUFSZ, 0, 0, &c->kue_xfer); - if (error) - return error; - c->kue_buf = usbd_get_buffer(c->kue_xfer); - } - } - - return 0; -} - -static int -kue_tx_list_init(struct kue_softc *sc) -{ - struct kue_cdata *cd; - struct kue_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); - - cd = &sc->kue_cdata; - for (i = 0; i < KUE_TX_LIST_CNT; i++) { - c = &cd->kue_tx_chain[i]; - c->kue_sc = sc; - c->kue_idx = i; - if (c->kue_xfer == NULL) { - int error = usbd_create_xfer(sc->kue_ep[KUE_ENDPT_TX], - KUE_BUFSZ, 0, 0, &c->kue_xfer); - if (error) - return error; - c->kue_buf = usbd_get_buffer(c->kue_xfer); - } - } - - return 0; + return usbnet_detach(self, flags); } /* @@ -635,519 +532,135 @@ kue_tx_list_init(struct kue_softc *sc) * the higher level protocols. */ static void -kue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +kue_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct kue_chain *c = priv; - struct kue_softc *sc = c->kue_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - int total_len, pktlen; - int s; - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev), - __func__, status)); - - if (sc->kue_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->kue_rx_errs++; - if (usbd_ratecheck(&sc->kue_rx_notice)) { - printf("%s: %u usb errors on rx: %s\n", - device_xname(sc->kue_dev), sc->kue_rx_errs, - usbd_errstr(status)); - sc->kue_rx_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - - DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", device_xname(sc->kue_dev), - __func__, total_len, - le16dec(c->kue_buf))); + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; + unsigned pktlen; if (total_len <= 1) - goto done; + return; - pktlen = le16dec(c->kue_buf); - if (pktlen > total_len - 2) - pktlen = total_len - 2; + DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", + device_xname(un->un_dev), __func__, + total_len, le16dec(buf))); + + pktlen = le16dec(buf); + if (pktlen > total_len - ETHER_ALIGN) + pktlen = total_len - ETHER_ALIGN; if (pktlen < ETHER_MIN_LEN - ETHER_CRC_LEN || pktlen > MCLBYTES - ETHER_ALIGN) { ifp->if_ierrors++; - goto done; - } - - /* No errors; receive the packet. */ - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (m == NULL) { - ifp->if_ierrors++; - goto done; - } - if (pktlen > MHLEN - ETHER_ALIGN) { - MCLGET(m, M_DONTWAIT); - if ((m->m_flags & M_EXT) == 0) { - m_freem(m); - ifp->if_ierrors++; - goto done; - } - } - m->m_data += ETHER_ALIGN; - - /* copy data to mbuf */ - memcpy(mtod(m, uint8_t *), c->kue_buf + 2, pktlen); - - m->m_pkthdr.len = m->m_len = pktlen; - m_set_rcvif(m, ifp); - - s = splnet(); - - DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->kue_dev), - __func__, m->m_len)); - if_percpuq_enqueue(ifp->if_percpuq, m); - - splx(s); - - done: - - /* Setup new transfer. */ - usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, KUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof); - usbd_transfer(c->kue_xfer); - - DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->kue_dev), - __func__)); -} - -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ - -static void -kue_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct kue_chain *c = priv; - struct kue_softc *sc = c->kue_sc; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (sc->kue_dying) - return; - - s = splnet(); - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev), - __func__, status)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->kue_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]); - splx(s); return; } - ifp->if_opackets++; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - kue_start(ifp); - - splx(s); + DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(un->un_dev), + __func__, pktlen)); + usbnet_enqueue(un, buf + 2, pktlen, 0, 0, 0); } -static int -kue_send(struct kue_softc *sc, struct mbuf *m, int idx) +static unsigned +kue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - int total_len; - struct kue_chain *c; - usbd_status err; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); + unsigned total_len, pkt_len; - c = &sc->kue_cdata.kue_tx_chain[idx]; + pkt_len = m->m_pkthdr.len + 2; + total_len = roundup2(pkt_len, 64); - /* Frame length is specified in the first 2 bytes of the buffer. */ - le16enc(c->kue_buf, (uint16_t)m->m_pkthdr.len); - - /* - * Copy the mbuf data into a contiguous buffer, leaving two - * bytes at the beginning to hold the frame length. - */ - m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2); - - total_len = 2 + m->m_pkthdr.len; - total_len = roundup2(total_len, 64); - - usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, total_len, 0, - USBD_DEFAULT_TIMEOUT, kue_txeof); - - /* Transmit */ - err = usbd_transfer(c->kue_xfer); - if (err != USBD_IN_PROGRESS) { - printf("%s: kue_send error=%s\n", device_xname(sc->kue_dev), - usbd_errstr(err)); - kue_stop(sc); - return EIO; - } - - sc->kue_cdata.kue_tx_cnt++; - - return 0; -} - -static void -kue_start(struct ifnet *ifp) -{ - struct kue_softc *sc = ifp->if_softc; - struct mbuf *m; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - if (sc->kue_dying) - return; - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m); - if (m == NULL) - return; - - if (kue_send(sc, m, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; + if ((unsigned)total_len > un->un_tx_bufsz) { + DPRINTFN(10,("%s: %s: too big pktlen %u total %u\n", + device_xname(un->un_dev), __func__, pkt_len, total_len)); + return 0; } - IFQ_DEQUEUE(&ifp->if_snd, m); + /* Frame length is specified in the first 2 bytes of the buffer. */ + le16enc(c->unc_buf, (uint16_t)m->m_pkthdr.len); /* - * If there's a BPF listener, bounce a copy of this frame - * to him. + * Copy the mbuf data into a contiguous buffer after the frame length, + * possibly zeroing the rest of the buffer. */ - bpf_mtap(ifp, m, BPF_D_OUT); - m_freem(m); + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + 2); + if (total_len - pkt_len > 0) + memset(c->unc_buf + pkt_len, 0, total_len - pkt_len); - ifp->if_flags |= IFF_OACTIVE; + DPRINTFN(10,("%s: %s: enter pktlen %u total %u\n", + device_xname(un->un_dev), __func__, pkt_len, total_len)); - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 6; + return total_len; } -static void -kue_init(void *xsc) +static int +kue_init_locked(struct ifnet *ifp) { - struct kue_softc *sc = xsc; - struct ifnet *ifp = GET_IFP(sc); - int s; + struct usbnet * const un = ifp->if_softc; + struct kue_softc *sc = usbnet_softc(un); uint8_t eaddr[ETHER_ADDR_LEN]; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); - if (ifp->if_flags & IFF_RUNNING) - return; + if (usbnet_isdying(un)) + return EIO; - s = splnet(); + /* Cancel pending I/O */ + usbnet_stop(un, ifp, 1); memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr)); /* Set MAC address */ - kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN); + kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN); sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST; - /* If we want promiscuous mode, set the allframes bit. */ - if (ifp->if_flags & IFF_PROMISC) - sc->kue_rxfilt |= KUE_RXFILT_PROMISC; - - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); - /* I'm not sure how to tune these. */ #if 0 /* * Leave this one alone for now; setting it * wrong causes lockups on some machines/controllers. */ - kue_setword(sc, KUE_CMD_SET_SOFS, 1); + kue_setword(un, KUE_CMD_SET_SOFS, 1); #endif - kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64); + kue_setword(un, KUE_CMD_SET_URB_SIZE, 64); /* Load the multicast filter. */ - kue_setmulti(sc); + kue_setiff(un); - if (sc->kue_ep[KUE_ENDPT_RX] == NULL) { - if (kue_open_pipes(sc)) { - splx(s); - return; - } - } - /* Init TX ring. */ - if (kue_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->kue_dev)); - splx(s); - return; - } - - /* Init RX ring. */ - if (kue_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->kue_dev)); - splx(s); - return; - } - - /* Start up the receive pipe. */ - for (size_t i = 0; i < KUE_RX_LIST_CNT; i++) { - struct kue_chain *c = &sc->kue_cdata.kue_rx_chain[i]; - usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, KUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof); - DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->kue_dev), - __func__)); - usbd_transfer(c->kue_xfer); - } - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); + return usbnet_init_rx_tx(un); } static int -kue_open_pipes(struct kue_softc *sc) +kue_init(struct ifnet *ifp) { - usbd_status err; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX], - USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]); - if (err) { - printf("%s: open rx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - return EIO; - } + struct usbnet * const un = ifp->if_softc; + int rv; - err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX], - USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - return EIO; - } + usbnet_lock(un); + rv = kue_init_locked(ifp); + usbnet_unlock(un); - return 0; + return rv; } static int -kue_ioctl(struct ifnet *ifp, u_long command, void *data) +kue_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct kue_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; + struct usbnet * const un = ifp->if_softc; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - if (sc->kue_dying) - return EIO; - - s = splnet(); - - switch (command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - kue_init(sc); - - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - arp_ifinit(ifp, ifa); - break; -#endif /* INET */ - } - break; - - case SIOCSIFMTU: - if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) - error = EINVAL; - else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) - error = 0; - break; - - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - if (ifp->if_flags & IFF_UP) { - if (ifp->if_flags & IFF_RUNNING && - ifp->if_flags & IFF_PROMISC && - !(sc->kue_if_flags & IFF_PROMISC)) { - sc->kue_rxfilt |= KUE_RXFILT_PROMISC; - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, - sc->kue_rxfilt); - } else if (ifp->if_flags & IFF_RUNNING && - !(ifp->if_flags & IFF_PROMISC) && - sc->kue_if_flags & IFF_PROMISC) { - sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC; - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, - sc->kue_rxfilt); - } else if (!(ifp->if_flags & IFF_RUNNING)) - kue_init(sc); - } else { - if (ifp->if_flags & IFF_RUNNING) - kue_stop(sc); - } - sc->kue_if_flags = ifp->if_flags; - error = 0; - break; + switch (cmd) { case SIOCADDMULTI: case SIOCDELMULTI: - error = ether_ioctl(ifp, command, data); - if (error == ENETRESET) { - if (ifp->if_flags & IFF_RUNNING) - kue_setmulti(sc); - error = 0; - } + //kue_init(ifp); + kue_setiff(un); break; default: - error = ether_ioctl(ifp, command, data); break; } - splx(s); - - return error; -} - -static void -kue_watchdog(struct ifnet *ifp) -{ - struct kue_softc *sc = ifp->if_softc; - struct kue_chain *c; - usbd_status stat; - int s; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - if (sc->kue_dying) - return; - - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->kue_dev)); - - s = splusb(); - c = &sc->kue_cdata.kue_tx_chain[0]; - usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat); - kue_txeof(c->kue_xfer, c, stat); - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - kue_start(ifp); - splx(s); + return 0; } -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -static void -kue_stop(struct kue_softc *sc) -{ - usbd_status err; - struct ifnet *ifp; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - ifp = GET_IFP(sc); - ifp->if_timer = 0; - - /* Stop transfers. */ - if (sc->kue_ep[KUE_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]); - if (err) { - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - } - - if (sc->kue_ep[KUE_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]); - if (err) { - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - } - - if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < KUE_RX_LIST_CNT; i++) { - if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) { - usbd_destroy_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer); - sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < KUE_TX_LIST_CNT; i++) { - if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) { - usbd_destroy_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer); - sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL; - } - } - - /* Close pipes. */ - if (sc->kue_ep[KUE_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - sc->kue_ep[KUE_ENDPT_RX] = NULL; - } - - if (sc->kue_ep[KUE_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - sc->kue_ep[KUE_ENDPT_TX] = NULL; - } - - if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - sc->kue_ep[KUE_ENDPT_INTR] = NULL; - } +#ifdef _MODULE +#include "ioconf.c" +#endif - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); -} +USBNET_MODULE(kue) Index: usb/if_kuereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_kuereg.h,v retrieving revision 1.22 diff -p -u -u -r1.22 if_kuereg.h --- usb/if_kuereg.h 21 Jul 2019 10:27:56 -0000 1.22 +++ usb/if_kuereg.h 29 Aug 2019 06:22:05 -0000 @@ -1,4 +1,5 @@ /* $NetBSD: if_kuereg.h,v 1.22 2019/07/21 10:27:56 mrg Exp $ */ + /* * Copyright (c) 1997, 1998, 1999, 2000 * Bill Paul . All rights reserved. @@ -76,8 +77,6 @@ struct kue_ether_desc { le16dec((x)->kue_desc.kue_maxseg) #define KUE_MCFILTCNT(x) \ (le16dec((x)->kue_desc.kue_mcastfilt) & 0x7FFF) -#define KUE_MCFILT(x, y) \ - (uint8_t *)&(sc->kue_mcfilters[y * ETHER_ADDR_LEN]) #define KUE_STAT_TX_OK 0x00000001 #define KUE_STAT_RX_OK 0x00000002 @@ -115,69 +114,7 @@ struct kue_ether_desc { #define KUE_RXFILT_BROADCAST 0x0008 #define KUE_RXFILT_MULTICAST 0x0010 -#define KUE_TIMEOUT 1000 -#define KUE_BUFSZ 1536 -#define KUE_MIN_FRAMELEN 60 - -#define KUE_RX_LIST_CNT 1 -#define KUE_TX_LIST_CNT 1 - #define KUE_CTL_READ 0x01 #define KUE_CTL_WRITE 0x02 #define KUE_WARM_REV 0x0202 - -/* - * The interrupt endpoint is currently unused - * by the KLSI part. - */ -#define KUE_ENDPT_RX 0x0 -#define KUE_ENDPT_TX 0x1 -#define KUE_ENDPT_INTR 0x2 -#define KUE_ENDPT_MAX 0x3 - -struct kue_type { - uint16_t kue_vid; - uint16_t kue_did; -}; - -struct kue_softc; - -struct kue_chain { - struct kue_softc *kue_sc; - struct usbd_xfer *kue_xfer; - uint8_t *kue_buf; - int kue_idx; -}; - -struct kue_cdata { - struct kue_chain kue_tx_chain[KUE_TX_LIST_CNT]; - struct kue_chain kue_rx_chain[KUE_RX_LIST_CNT]; - int kue_tx_prod; - int kue_tx_cnt; -}; - -struct kue_softc { - device_t kue_dev; - - struct ethercom kue_ec; - krndsource_t rnd_source; -#define GET_IFP(sc) (&(sc)->kue_ec.ec_if) - - struct usbd_device * kue_udev; - struct usbd_interface * kue_iface; - uint16_t kue_vendor; - uint16_t kue_product; - struct kue_ether_desc kue_desc; - int kue_ed[KUE_ENDPT_MAX]; - struct usbd_pipe * kue_ep[KUE_ENDPT_MAX]; - int kue_if_flags; - uint16_t kue_rxfilt; - uint8_t *kue_mcfilters; - struct kue_cdata kue_cdata; - - bool kue_dying; - bool kue_attached; - u_int kue_rx_errs; - struct timeval kue_rx_notice; -}; Index: usb/if_mue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_mue.c,v retrieving revision 1.50 diff -p -u -u -r1.50 if_mue.c --- usb/if_mue.c 29 May 2019 09:04:01 -0000 1.50 +++ usb/if_mue.c 29 Aug 2019 06:22:05 -0000 @@ -28,55 +28,24 @@ __KERNEL_RCSID(0, "$NetBSD: if_mue.c,v 1 #endif #include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include -#include -#include -#include - -#include - -#include -#include -#include /* XXX for struct ip */ -#include /* XXX for struct ip6_hdr */ - -#include -#include - -#include -#include -#include -#include -#include + +#include #include #include -#define MUE_PRINTF(sc, fmt, args...) \ - device_printf((sc)->mue_dev, "%s: " fmt, __func__, ##args); +#define MUE_PRINTF(un, fmt, args...) \ + device_printf((un)->un_dev, "%s: " fmt, __func__, ##args); #ifdef USB_DEBUG int muedebug = 0; -#define DPRINTF(sc, fmt, args...) \ +#define DPRINTF(un, fmt, args...) \ do { \ if (muedebug) \ - MUE_PRINTF(sc, fmt, ##args); \ + MUE_PRINTF(un, fmt, ##args); \ } while (0 /* CONSTCOND */) #else -#define DPRINTF(sc, fmt, args...) __nothing +#define DPRINTF(un, fmt, args...) __nothing #endif /* @@ -86,14 +55,17 @@ struct mue_type { struct usb_devno mue_dev; uint16_t mue_flags; #define LAN7500 0x0001 /* LAN7500 */ +#define LAN7800 0x0002 /* LAN7800 */ +#define LAN7801 0x0004 /* LAN7801 */ +#define LAN7850 0x0008 /* LAN7850 */ }; const struct mue_type mue_devs[] = { { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7500 }, LAN7500 }, { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7505 }, LAN7500 }, - { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7800 }, 0 }, - { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7801 }, 0 }, - { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7850 }, 0 } + { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7800 }, LAN7800 }, + { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7801 }, LAN7801 }, + { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7850 }, LAN7850 } }; #define MUE_LOOKUP(uaa) ((const struct mue_type *)usb_lookup(mue_devs, \ @@ -106,77 +78,57 @@ const struct mue_type mue_devs[] = { static int mue_match(device_t, cfdata_t, void *); static void mue_attach(device_t, device_t, void *); -static int mue_detach(device_t, int); -static int mue_activate(device_t, enum devact); -static uint32_t mue_csr_read(struct mue_softc *, uint32_t); -static int mue_csr_write(struct mue_softc *, uint32_t, uint32_t); -static int mue_wait_for_bits(struct mue_softc *sc, uint32_t, uint32_t, +static uint32_t mue_csr_read(struct usbnet *, uint32_t); +static int mue_csr_write(struct usbnet *, uint32_t, uint32_t); +static int mue_wait_for_bits(struct usbnet *, uint32_t, uint32_t, uint32_t, uint32_t); - -static void mue_lock_mii(struct mue_softc *); -static void mue_unlock_mii(struct mue_softc *); - -static int mue_miibus_readreg(device_t, int, int, uint16_t *); -static int mue_miibus_writereg(device_t, int, int, uint16_t); -static void mue_miibus_statchg(struct ifnet *); -static int mue_ifmedia_upd(struct ifnet *); -static void mue_ifmedia_sts(struct ifnet *, struct ifmediareq *); - -static uint8_t mue_eeprom_getbyte(struct mue_softc *, int, uint8_t *); -static int mue_read_eeprom(struct mue_softc *, uint8_t *, int, int); -static bool mue_eeprom_present(struct mue_softc *sc); - -static int mue_read_otp_raw(struct mue_softc *, uint8_t *, int, int); -static int mue_read_otp(struct mue_softc *, uint8_t *, int, int); - -static void mue_dataport_write(struct mue_softc *, uint32_t, uint32_t, +static uint8_t mue_eeprom_getbyte(struct usbnet *, int, uint8_t *); +static bool mue_eeprom_present(struct usbnet *); +static void mue_dataport_write(struct usbnet *, uint32_t, uint32_t, uint32_t, uint32_t *); +static void mue_init_ltm(struct usbnet *); +static int mue_chip_init(struct usbnet *); +static void mue_set_macaddr(struct usbnet *); +static int mue_get_macaddr(struct usbnet *, prop_dictionary_t); +static int mue_prepare_tso(struct usbnet *, struct mbuf *); +static void mue_setiff(struct usbnet *); +static void mue_sethwcsum(struct usbnet *); +static void mue_setmtu(struct usbnet *); +static void mue_reset(struct usbnet *); + +static void mue_stop_cb(struct ifnet *, int); +static int mue_ioctl_cb(struct ifnet *, u_long, void *); +static int mue_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static int mue_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void mue_mii_statchg(struct ifnet *); +static void mue_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned mue_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static int mue_init(struct ifnet *); -static void mue_init_ltm(struct mue_softc *); - -static int mue_chip_init(struct mue_softc *); - -static void mue_set_macaddr(struct mue_softc *); -static int mue_get_macaddr(struct mue_softc *, prop_dictionary_t); - -static int mue_rx_list_init(struct mue_softc *); -static int mue_tx_list_init(struct mue_softc *); -static int mue_open_pipes(struct mue_softc *); -static void mue_startup_rx_pipes(struct mue_softc *); - -static int mue_encap(struct mue_softc *, struct mbuf *, int); -static int mue_prepare_tso(struct mue_softc *, struct mbuf *); - -static void mue_setmulti(struct mue_softc *); -static void mue_sethwcsum(struct mue_softc *); -static void mue_setmtu(struct mue_softc *); +static struct usbnet_ops mue_ops = { + .uno_stop = mue_stop_cb, + .uno_ioctl = mue_ioctl_cb, + .uno_read_reg = mue_mii_read_reg, + .uno_write_reg = mue_mii_write_reg, + .uno_statchg = mue_mii_statchg, + .uno_tx_prepare = mue_tx_prepare, + .uno_rx_loop = mue_rx_loop, + .uno_init = mue_init, +}; -static void mue_rxeof(struct usbd_xfer *, void *, usbd_status); -static void mue_txeof(struct usbd_xfer *, void *, usbd_status); +#define MUE_SETBIT(un, reg, x) \ + mue_csr_write(un, reg, mue_csr_read(un, reg) | (x)) -static int mue_init(struct ifnet *); -static int mue_ioctl(struct ifnet *, u_long, void *); -static void mue_watchdog(struct ifnet *); -static void mue_reset(struct mue_softc *); -static void mue_start(struct ifnet *); -static void mue_stop(struct ifnet *, int); -static void mue_tick(void *); -static void mue_tick_task(void *); - -static struct mbuf *mue_newbuf(void); - -#define MUE_SETBIT(sc, reg, x) \ - mue_csr_write(sc, reg, mue_csr_read(sc, reg) | (x)) +#define MUE_CLRBIT(un, reg, x) \ + mue_csr_write(un, reg, mue_csr_read(un, reg) & ~(x)) -#define MUE_CLRBIT(sc, reg, x) \ - mue_csr_write(sc, reg, mue_csr_read(sc, reg) & ~(x)) +#define MUE_WAIT_SET(un, reg, set, fail) \ + mue_wait_for_bits(un, reg, set, ~0, fail) -#define MUE_WAIT_SET(sc, reg, set, fail) \ - mue_wait_for_bits(sc, reg, set, ~0, fail) - -#define MUE_WAIT_CLR(sc, reg, clear, fail) \ - mue_wait_for_bits(sc, reg, 0, clear, fail) +#define MUE_WAIT_CLR(un, reg, clear, fail) \ + mue_wait_for_bits(un, reg, 0, clear, fail) #define ETHER_IS_VALID(addr) \ (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr)) @@ -184,17 +136,17 @@ static struct mbuf *mue_newbuf(void); #define ETHER_IS_ZERO(addr) \ (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) -CFATTACH_DECL_NEW(mue, sizeof(struct mue_softc), mue_match, mue_attach, - mue_detach, mue_activate); +CFATTACH_DECL_NEW(mue, sizeof(struct usbnet), mue_match, mue_attach, + usbnet_detach, usbnet_activate); static uint32_t -mue_csr_read(struct mue_softc *sc, uint32_t reg) +mue_csr_read(struct usbnet *un, uint32_t reg) { usb_device_request_t req; usbd_status err; uDWord val; - if (sc->mue_dying) + if (usbnet_isdying(un)) return 0; USETDW(val, 0); @@ -204,9 +156,9 @@ mue_csr_read(struct mue_softc *sc, uint3 USETW(req.wIndex, reg); USETW(req.wLength, 4); - err = usbd_do_request(sc->mue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { - MUE_PRINTF(sc, "reg = 0x%x: %s\n", reg, usbd_errstr(err)); + MUE_PRINTF(un, "reg = 0x%x: %s\n", reg, usbd_errstr(err)); return 0; } @@ -214,13 +166,13 @@ mue_csr_read(struct mue_softc *sc, uint3 } static int -mue_csr_write(struct mue_softc *sc, uint32_t reg, uint32_t aval) +mue_csr_write(struct usbnet *un, uint32_t reg, uint32_t aval) { usb_device_request_t req; usbd_status err; uDWord val; - if (sc->mue_dying) + if (usbnet_isdying(un)) return 0; USETDW(val, aval); @@ -230,9 +182,9 @@ mue_csr_write(struct mue_softc *sc, uint USETW(req.wIndex, reg); USETW(req.wLength, 4); - err = usbd_do_request(sc->mue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { - MUE_PRINTF(sc, "reg = 0x%x: %s\n", reg, usbd_errstr(err)); + MUE_PRINTF(un, "reg = 0x%x: %s\n", reg, usbd_errstr(err)); return -1; } @@ -240,153 +192,97 @@ mue_csr_write(struct mue_softc *sc, uint } static int -mue_wait_for_bits(struct mue_softc *sc, uint32_t reg, +mue_wait_for_bits(struct usbnet *un, uint32_t reg, uint32_t set, uint32_t clear, uint32_t fail) { uint32_t val; int ntries; for (ntries = 0; ntries < 1000; ntries++) { - val = mue_csr_read(sc, reg); + val = mue_csr_read(un, reg); if ((val & set) || !(val & clear)) return 0; if (val & fail) return 1; - usbd_delay_ms(sc->mue_udev, 1); + usbd_delay_ms(un->un_udev, 1); } return 1; } -/* - * Get exclusive access to the MII registers. - */ -static void -mue_lock_mii(struct mue_softc *sc) -{ - sc->mue_refcnt++; - mutex_enter(&sc->mue_mii_lock); -} - -static void -mue_unlock_mii(struct mue_softc *sc) -{ - mutex_exit(&sc->mue_mii_lock); - if (--sc->mue_refcnt < 0) - usb_detach_wakeupold(sc->mue_dev); -} - static int -mue_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) +mue_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - struct mue_softc *sc = device_private(dev); uint32_t data; - int rv = 0; - if (sc->mue_dying) { - DPRINTF(sc, "dying\n"); - return -1; - } + usbnet_isowned_mii(un); - if (sc->mue_phyno != phy) - return -1; + if (un->un_phyno != phy) + return EINVAL; - mue_lock_mii(sc); - if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { - mue_unlock_mii(sc); - MUE_PRINTF(sc, "not ready\n"); - return -1; + if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { + MUE_PRINTF(un, "not ready\n"); + return EBUSY; } - mue_csr_write(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_READ | + mue_csr_write(un, MUE_MII_ACCESS, MUE_MII_ACCESS_READ | MUE_MII_ACCESS_BUSY | MUE_MII_ACCESS_REGADDR(reg) | MUE_MII_ACCESS_PHYADDR(phy)); - if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { - MUE_PRINTF(sc, "timed out\n"); - rv = ETIMEDOUT; - goto out; + if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { + MUE_PRINTF(un, "timed out\n"); + return ETIMEDOUT; } - data = mue_csr_read(sc, MUE_MII_DATA); + data = mue_csr_read(un, MUE_MII_DATA); *val = data & 0xffff; -out: - mue_unlock_mii(sc); - return rv; + return 0; } static int -mue_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +mue_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct mue_softc *sc = device_private(dev); - int rv = 0; + usbnet_isowned_mii(un); - if (sc->mue_dying) { - DPRINTF(sc, "dying\n"); - return -1; - } - - if (sc->mue_phyno != phy) { - DPRINTF(sc, "sc->mue_phyno (%d) != phy (%d)\n", - sc->mue_phyno, phy); - return -1; - } + if (un->un_phyno != phy) + return EINVAL; - mue_lock_mii(sc); - if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { - MUE_PRINTF(sc, "not ready\n"); - rv = EBUSY; - goto out; + if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { + MUE_PRINTF(un, "not ready\n"); + return EBUSY; } - mue_csr_write(sc, MUE_MII_DATA, val); - mue_csr_write(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_WRITE | + mue_csr_write(un, MUE_MII_DATA, val); + mue_csr_write(un, MUE_MII_ACCESS, MUE_MII_ACCESS_WRITE | MUE_MII_ACCESS_BUSY | MUE_MII_ACCESS_REGADDR(reg) | MUE_MII_ACCESS_PHYADDR(phy)); - if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { - MUE_PRINTF(sc, "timed out\n"); - rv = ETIMEDOUT; + if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { + MUE_PRINTF(un, "timed out\n"); + return ETIMEDOUT; } -out: - mue_unlock_mii(sc); - return rv; + + return 0; } static void -mue_miibus_statchg(struct ifnet *ifp) +mue_mii_statchg(struct ifnet *ifp) { - struct mue_softc *sc; - struct mii_data *mii; + struct usbnet * const un = ifp->if_softc; + struct mii_data * const mii = usbnet_mii(un); uint32_t flow, threshold; - if (ifp == NULL) { - printf("%s: ifp not ready\n", __func__); - return; - } - - sc = ifp->if_softc; - mii = GET_MII(sc); - - if ((ifp->if_flags & IFF_RUNNING) == 0) { - DPRINTF(sc, "not running\n"); + if (usbnet_isdying(un)) return; - } - if (mii == NULL) { - DPRINTF(sc, "mii not ready\n"); - return; - } - - sc->mue_link = 0; if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == (IFM_ACTIVE | IFM_AVALID)) { switch (IFM_SUBTYPE(mii->mii_media_active)) { case IFM_10_T: case IFM_100_TX: case IFM_1000_T: - sc->mue_link++; + usbnet_set_link(un, true); break; default: break; @@ -394,22 +290,22 @@ mue_miibus_statchg(struct ifnet *ifp) } /* Lost link, do nothing. */ - if (sc->mue_link == 0) { - DPRINTF(sc, "mii_media_status = 0x%x\n", mii->mii_media_status); + if (!usbnet_havelink(un)) { + DPRINTF(un, "mii_media_status = 0x%x\n", mii->mii_media_status); return; } - if (!(sc->mue_flags & LAN7500)) { - if (sc->mue_udev->ud_speed == USB_SPEED_SUPER) { + if (!(un->un_flags & LAN7500)) { + if (un->un_udev->ud_speed == USB_SPEED_SUPER) { if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) { /* Disable U2 and enable U1. */ - MUE_CLRBIT(sc, MUE_USB_CFG1, + MUE_CLRBIT(un, MUE_USB_CFG1, MUE_USB_CFG1_DEV_U2_INIT_EN); - MUE_SETBIT(sc, MUE_USB_CFG1, + MUE_SETBIT(un, MUE_USB_CFG1, MUE_USB_CFG1_DEV_U1_INIT_EN); } else { /* Enable U1 and U2. */ - MUE_SETBIT(sc, MUE_USB_CFG1, + MUE_SETBIT(un, MUE_USB_CFG1, MUE_USB_CFG1_DEV_U1_INIT_EN | MUE_USB_CFG1_DEV_U2_INIT_EN); } @@ -426,10 +322,10 @@ mue_miibus_statchg(struct ifnet *ifp) } /* XXX Magic numbers taken from Linux driver. */ - if (sc->mue_flags & LAN7500) + if (un->un_flags & LAN7500) threshold = 0x820; else - switch (sc->mue_udev->ud_speed) { + switch (un->un_udev->ud_speed) { case USB_SPEED_SUPER: threshold = 0x817; break; @@ -442,74 +338,41 @@ mue_miibus_statchg(struct ifnet *ifp) } /* Threshold value should be set before enabling flow. */ - mue_csr_write(sc, (sc->mue_flags & LAN7500) ? + mue_csr_write(un, (un->un_flags & LAN7500) ? MUE_7500_FCT_FLOW : MUE_7800_FCT_FLOW, threshold); - mue_csr_write(sc, MUE_FLOW, flow); - - DPRINTF(sc, "done\n"); -} - -/* - * Set media options. - */ -static int -mue_ifmedia_upd(struct ifnet *ifp) -{ - struct mue_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); + mue_csr_write(un, MUE_FLOW, flow); - sc->mue_link = 0; /* XXX */ - - if (mii->mii_instance) { - struct mii_softc *miisc; - LIST_FOREACH(miisc, &mii->mii_phys, mii_list) - mii_phy_reset(miisc); - } - return mii_mediachg(mii); -} - -/* - * Report current media status. - */ -static void -mue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) -{ - struct mue_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); - - mii_pollstat(mii); - ifmr->ifm_active = mii->mii_media_active; - ifmr->ifm_status = mii->mii_media_status; + DPRINTF(un, "done\n"); } static uint8_t -mue_eeprom_getbyte(struct mue_softc *sc, int off, uint8_t *dest) +mue_eeprom_getbyte(struct usbnet *un, int off, uint8_t *dest) { uint32_t val; - if (MUE_WAIT_CLR(sc, MUE_E2P_CMD, MUE_E2P_CMD_BUSY, 0)) { - MUE_PRINTF(sc, "not ready\n"); + if (MUE_WAIT_CLR(un, MUE_E2P_CMD, MUE_E2P_CMD_BUSY, 0)) { + MUE_PRINTF(un, "not ready\n"); return ETIMEDOUT; } KASSERT((off & ~MUE_E2P_CMD_ADDR_MASK) == 0); - mue_csr_write(sc, MUE_E2P_CMD, MUE_E2P_CMD_READ | MUE_E2P_CMD_BUSY | + mue_csr_write(un, MUE_E2P_CMD, MUE_E2P_CMD_READ | MUE_E2P_CMD_BUSY | off); - if (MUE_WAIT_CLR(sc, MUE_E2P_CMD, MUE_E2P_CMD_BUSY, + if (MUE_WAIT_CLR(un, MUE_E2P_CMD, MUE_E2P_CMD_BUSY, MUE_E2P_CMD_TIMEOUT)) { - MUE_PRINTF(sc, "timed out\n"); + MUE_PRINTF(un, "timed out\n"); return ETIMEDOUT; } - val = mue_csr_read(sc, MUE_E2P_DATA); + val = mue_csr_read(un, MUE_E2P_DATA); *dest = val & 0xff; return 0; } static int -mue_read_eeprom(struct mue_softc *sc, uint8_t *dest, int off, int cnt) +mue_read_eeprom(struct usbnet *un, uint8_t *dest, int off, int cnt) { uint32_t val = 0; /* XXX gcc */ uint8_t byte; @@ -518,76 +381,76 @@ mue_read_eeprom(struct mue_softc *sc, ui /* * EEPROM pins are muxed with the LED function on LAN7800 device. */ - if (sc->mue_product == USB_PRODUCT_SMSC_LAN7800) { - val = mue_csr_read(sc, MUE_HW_CFG); - mue_csr_write(sc, MUE_HW_CFG, + if (un->un_flags & LAN7800) { + val = mue_csr_read(un, MUE_HW_CFG); + mue_csr_write(un, MUE_HW_CFG, val & ~(MUE_HW_CFG_LED0_EN | MUE_HW_CFG_LED1_EN)); } for (i = 0; i < cnt; i++) { - err = mue_eeprom_getbyte(sc, off + i, &byte); + err = mue_eeprom_getbyte(un, off + i, &byte); if (err) break; *(dest + i) = byte; } - if (sc->mue_product == USB_PRODUCT_SMSC_LAN7800) - mue_csr_write(sc, MUE_HW_CFG, val); + if (un->un_flags & LAN7800) + mue_csr_write(un, MUE_HW_CFG, val); return err ? 1 : 0; } static bool -mue_eeprom_present(struct mue_softc *sc) +mue_eeprom_present(struct usbnet *un) { uint32_t val; uint8_t sig; int ret; - if (sc->mue_flags & LAN7500) { - val = mue_csr_read(sc, MUE_E2P_CMD); + if (un->un_flags & LAN7500) { + val = mue_csr_read(un, MUE_E2P_CMD); return val & MUE_E2P_CMD_LOADED; } else { - ret = mue_read_eeprom(sc, &sig, MUE_E2P_IND_OFFSET, 1); + ret = mue_read_eeprom(un, &sig, MUE_E2P_IND_OFFSET, 1); return (ret == 0) && (sig == MUE_E2P_IND); } } static int -mue_read_otp_raw(struct mue_softc *sc, uint8_t *dest, int off, int cnt) +mue_read_otp_raw(struct usbnet *un, uint8_t *dest, int off, int cnt) { uint32_t val; int i, err; - val = mue_csr_read(sc, MUE_OTP_PWR_DN); + val = mue_csr_read(un, MUE_OTP_PWR_DN); /* Checking if bit is set. */ if (val & MUE_OTP_PWR_DN_PWRDN_N) { /* Clear it, then wait for it to be cleared. */ - mue_csr_write(sc, MUE_OTP_PWR_DN, 0); - err = MUE_WAIT_CLR(sc, MUE_OTP_PWR_DN, MUE_OTP_PWR_DN_PWRDN_N, + mue_csr_write(un, MUE_OTP_PWR_DN, 0); + err = MUE_WAIT_CLR(un, MUE_OTP_PWR_DN, MUE_OTP_PWR_DN_PWRDN_N, 0); if (err) { - MUE_PRINTF(sc, "not ready\n"); + MUE_PRINTF(un, "not ready\n"); return 1; } } /* Start reading the bytes, one at a time. */ for (i = 0; i < cnt; i++) { - mue_csr_write(sc, MUE_OTP_ADDR1, + mue_csr_write(un, MUE_OTP_ADDR1, ((off + i) >> 8) & MUE_OTP_ADDR1_MASK); - mue_csr_write(sc, MUE_OTP_ADDR2, + mue_csr_write(un, MUE_OTP_ADDR2, ((off + i) & MUE_OTP_ADDR2_MASK)); - mue_csr_write(sc, MUE_OTP_FUNC_CMD, MUE_OTP_FUNC_CMD_READ); - mue_csr_write(sc, MUE_OTP_CMD_GO, MUE_OTP_CMD_GO_GO); + mue_csr_write(un, MUE_OTP_FUNC_CMD, MUE_OTP_FUNC_CMD_READ); + mue_csr_write(un, MUE_OTP_CMD_GO, MUE_OTP_CMD_GO_GO); - err = MUE_WAIT_CLR(sc, MUE_OTP_STATUS, MUE_OTP_STATUS_BUSY, 0); + err = MUE_WAIT_CLR(un, MUE_OTP_STATUS, MUE_OTP_STATUS_BUSY, 0); if (err) { - MUE_PRINTF(sc, "timed out\n"); + MUE_PRINTF(un, "timed out\n"); return 1; } - val = mue_csr_read(sc, MUE_OTP_RD_DATA); + val = mue_csr_read(un, MUE_OTP_RD_DATA); *(dest + i) = (uint8_t)(val & 0xff); } @@ -595,15 +458,15 @@ mue_read_otp_raw(struct mue_softc *sc, u } static int -mue_read_otp(struct mue_softc *sc, uint8_t *dest, int off, int cnt) +mue_read_otp(struct usbnet *un, uint8_t *dest, int off, int cnt) { uint8_t sig; int err; - if (sc->mue_flags & LAN7500) + if (un->un_flags & LAN7500) return 1; - err = mue_read_otp_raw(sc, &sig, MUE_OTP_IND_OFFSET, 1); + err = mue_read_otp_raw(un, &sig, MUE_OTP_IND_OFFSET, 1); if (err) return 1; switch (sig) { @@ -613,125 +476,125 @@ mue_read_otp(struct mue_softc *sc, uint8 off += 0x100; break; default: - DPRINTF(sc, "OTP not found\n"); + DPRINTF(un, "OTP not found\n"); return 1; } - err = mue_read_otp_raw(sc, dest, off, cnt); + err = mue_read_otp_raw(un, dest, off, cnt); return err; } static void -mue_dataport_write(struct mue_softc *sc, uint32_t sel, uint32_t addr, +mue_dataport_write(struct usbnet *un, uint32_t sel, uint32_t addr, uint32_t cnt, uint32_t *data) { uint32_t i; - if (MUE_WAIT_SET(sc, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) { - MUE_PRINTF(sc, "not ready\n"); + if (MUE_WAIT_SET(un, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) { + MUE_PRINTF(un, "not ready\n"); return; } - mue_csr_write(sc, MUE_DP_SEL, - (mue_csr_read(sc, MUE_DP_SEL) & ~MUE_DP_SEL_RSEL_MASK) | sel); + mue_csr_write(un, MUE_DP_SEL, + (mue_csr_read(un, MUE_DP_SEL) & ~MUE_DP_SEL_RSEL_MASK) | sel); for (i = 0; i < cnt; i++) { - mue_csr_write(sc, MUE_DP_ADDR, addr + i); - mue_csr_write(sc, MUE_DP_DATA, data[i]); - mue_csr_write(sc, MUE_DP_CMD, MUE_DP_CMD_WRITE); - if (MUE_WAIT_SET(sc, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) { - MUE_PRINTF(sc, "timed out\n"); + mue_csr_write(un, MUE_DP_ADDR, addr + i); + mue_csr_write(un, MUE_DP_DATA, data[i]); + mue_csr_write(un, MUE_DP_CMD, MUE_DP_CMD_WRITE); + if (MUE_WAIT_SET(un, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) { + MUE_PRINTF(un, "timed out\n"); return; } } } static void -mue_init_ltm(struct mue_softc *sc) +mue_init_ltm(struct usbnet *un) { uint32_t idx[MUE_NUM_LTM_INDEX] = { 0, 0, 0, 0, 0, 0 }; uint8_t temp[2]; size_t i; - if (mue_csr_read(sc, MUE_USB_CFG1) & MUE_USB_CFG1_LTM_ENABLE) { - if (mue_eeprom_present(sc) && - (mue_read_eeprom(sc, temp, MUE_E2P_LTM_OFFSET, 2) == 0)) { + if (mue_csr_read(un, MUE_USB_CFG1) & MUE_USB_CFG1_LTM_ENABLE) { + if (mue_eeprom_present(un) && + (mue_read_eeprom(un, temp, MUE_E2P_LTM_OFFSET, 2) == 0)) { if (temp[0] != sizeof(idx)) { - DPRINTF(sc, "EEPROM: unexpected size\n"); + DPRINTF(un, "EEPROM: unexpected size\n"); goto done; } - if (mue_read_eeprom(sc, (uint8_t *)idx, temp[1] << 1, + if (mue_read_eeprom(un, (uint8_t *)idx, temp[1] << 1, sizeof(idx))) { - DPRINTF(sc, "EEPROM: failed to read\n"); + DPRINTF(un, "EEPROM: failed to read\n"); goto done; } - DPRINTF(sc, "success\n"); - } else if (mue_read_otp(sc, temp, MUE_E2P_LTM_OFFSET, 2) == 0) { + DPRINTF(un, "success\n"); + } else if (mue_read_otp(un, temp, MUE_E2P_LTM_OFFSET, 2) == 0) { if (temp[0] != sizeof(idx)) { - DPRINTF(sc, "OTP: unexpected size\n"); + DPRINTF(un, "OTP: unexpected size\n"); goto done; } - if (mue_read_otp(sc, (uint8_t *)idx, temp[1] << 1, + if (mue_read_otp(un, (uint8_t *)idx, temp[1] << 1, sizeof(idx))) { - DPRINTF(sc, "OTP: failed to read\n"); + DPRINTF(un, "OTP: failed to read\n"); goto done; } - DPRINTF(sc, "success\n"); + DPRINTF(un, "success\n"); } else - DPRINTF(sc, "nothing to do\n"); + DPRINTF(un, "nothing to do\n"); } else - DPRINTF(sc, "nothing to do\n"); + DPRINTF(un, "nothing to do\n"); done: for (i = 0; i < __arraycount(idx); i++) - mue_csr_write(sc, MUE_LTM_INDEX(i), idx[i]); + mue_csr_write(un, MUE_LTM_INDEX(i), idx[i]); } static int -mue_chip_init(struct mue_softc *sc) +mue_chip_init(struct usbnet *un) { uint32_t val; - if ((sc->mue_flags & LAN7500) && - MUE_WAIT_SET(sc, MUE_PMT_CTL, MUE_PMT_CTL_READY, 0)) { - MUE_PRINTF(sc, "not ready\n"); + if ((un->un_flags & LAN7500) && + MUE_WAIT_SET(un, MUE_PMT_CTL, MUE_PMT_CTL_READY, 0)) { + MUE_PRINTF(un, "not ready\n"); return ETIMEDOUT; } - MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_LRST); - if (MUE_WAIT_CLR(sc, MUE_HW_CFG, MUE_HW_CFG_LRST, 0)) { - MUE_PRINTF(sc, "timed out\n"); + MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_LRST); + if (MUE_WAIT_CLR(un, MUE_HW_CFG, MUE_HW_CFG_LRST, 0)) { + MUE_PRINTF(un, "timed out\n"); return ETIMEDOUT; } /* Respond to the IN token with a NAK. */ - if (sc->mue_flags & LAN7500) - MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_BIR); + if (un->un_flags & LAN7500) + MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_BIR); else - MUE_SETBIT(sc, MUE_USB_CFG0, MUE_USB_CFG0_BIR); + MUE_SETBIT(un, MUE_USB_CFG0, MUE_USB_CFG0_BIR); - if (sc->mue_flags & LAN7500) { - if (sc->mue_udev->ud_speed == USB_SPEED_HIGH) + if (un->un_flags & LAN7500) { + if (un->un_udev->ud_speed == USB_SPEED_HIGH) val = MUE_7500_HS_RX_BUFSIZE / MUE_HS_USB_PKT_SIZE; else val = MUE_7500_FS_RX_BUFSIZE / MUE_FS_USB_PKT_SIZE; - mue_csr_write(sc, MUE_7500_BURST_CAP, val); - mue_csr_write(sc, MUE_7500_BULKIN_DELAY, + mue_csr_write(un, MUE_7500_BURST_CAP, val); + mue_csr_write(un, MUE_7500_BULKIN_DELAY, MUE_7500_DEFAULT_BULKIN_DELAY); - MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_BCE | MUE_HW_CFG_MEF); + MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_BCE | MUE_HW_CFG_MEF); /* Set FIFO sizes. */ val = (MUE_7500_MAX_RX_FIFO_SIZE - 512) / 512; - mue_csr_write(sc, MUE_7500_FCT_RX_FIFO_END, val); + mue_csr_write(un, MUE_7500_FCT_RX_FIFO_END, val); val = (MUE_7500_MAX_TX_FIFO_SIZE - 512) / 512; - mue_csr_write(sc, MUE_7500_FCT_TX_FIFO_END, val); + mue_csr_write(un, MUE_7500_FCT_TX_FIFO_END, val); } else { /* Init LTM. */ - mue_init_ltm(sc); + mue_init_ltm(un); val = MUE_7800_RX_BUFSIZE; - switch (sc->mue_udev->ud_speed) { + switch (un->un_udev->ud_speed) { case USB_SPEED_SUPER: val /= MUE_SS_USB_PKT_SIZE; break; @@ -742,12 +605,12 @@ mue_chip_init(struct mue_softc *sc) val /= MUE_FS_USB_PKT_SIZE; break; } - mue_csr_write(sc, MUE_7800_BURST_CAP, val); - mue_csr_write(sc, MUE_7800_BULKIN_DELAY, + mue_csr_write(un, MUE_7800_BURST_CAP, val); + mue_csr_write(un, MUE_7800_BULKIN_DELAY, MUE_7800_DEFAULT_BULKIN_DELAY); - MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_MEF); - MUE_SETBIT(sc, MUE_USB_CFG0, MUE_USB_CFG0_BCE); + MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_MEF); + MUE_SETBIT(un, MUE_USB_CFG0, MUE_USB_CFG0_BCE); /* * Set FCL's RX and TX FIFO sizes: according to data sheet this @@ -755,112 +618,111 @@ mue_chip_init(struct mue_softc *sc) * same value anyways, as that's what the Linux driver does. */ val = (MUE_7800_MAX_RX_FIFO_SIZE - 512) / 512; - mue_csr_write(sc, MUE_7800_FCT_RX_FIFO_END, val); + mue_csr_write(un, MUE_7800_FCT_RX_FIFO_END, val); val = (MUE_7800_MAX_TX_FIFO_SIZE - 512) / 512; - mue_csr_write(sc, MUE_7800_FCT_TX_FIFO_END, val); + mue_csr_write(un, MUE_7800_FCT_TX_FIFO_END, val); } /* Enabling interrupts. */ - mue_csr_write(sc, MUE_INT_STATUS, ~0); + mue_csr_write(un, MUE_INT_STATUS, ~0); - mue_csr_write(sc, (sc->mue_flags & LAN7500) ? + mue_csr_write(un, (un->un_flags & LAN7500) ? MUE_7500_FCT_FLOW : MUE_7800_FCT_FLOW, 0); - mue_csr_write(sc, MUE_FLOW, 0); + mue_csr_write(un, MUE_FLOW, 0); /* Reset PHY. */ - MUE_SETBIT(sc, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST); - if (MUE_WAIT_CLR(sc, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST, 0)) { - MUE_PRINTF(sc, "PHY not ready\n"); + MUE_SETBIT(un, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST); + if (MUE_WAIT_CLR(un, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST, 0)) { + MUE_PRINTF(un, "PHY not ready\n"); return ETIMEDOUT; } /* LAN7801 only has RGMII mode. */ - if (sc->mue_product == USB_PRODUCT_SMSC_LAN7801) - MUE_CLRBIT(sc, MUE_MAC_CR, MUE_MAC_CR_GMII_EN); + if (un->un_flags & LAN7801) + MUE_CLRBIT(un, MUE_MAC_CR, MUE_MAC_CR_GMII_EN); - if ((sc->mue_flags & LAN7500) || - (sc->mue_product == USB_PRODUCT_SMSC_LAN7800 && - !mue_eeprom_present(sc))) { + if ((un->un_flags & (LAN7500 | LAN7800)) || + !mue_eeprom_present(un)) { /* Allow MAC to detect speed and duplex from PHY. */ - MUE_SETBIT(sc, MUE_MAC_CR, MUE_MAC_CR_AUTO_SPEED | + MUE_SETBIT(un, MUE_MAC_CR, MUE_MAC_CR_AUTO_SPEED | MUE_MAC_CR_AUTO_DUPLEX); } - MUE_SETBIT(sc, MUE_MAC_TX, MUE_MAC_TX_TXEN); - MUE_SETBIT(sc, (sc->mue_flags & LAN7500) ? + MUE_SETBIT(un, MUE_MAC_TX, MUE_MAC_TX_TXEN); + MUE_SETBIT(un, (un->un_flags & LAN7500) ? MUE_7500_FCT_TX_CTL : MUE_7800_FCT_TX_CTL, MUE_FCT_TX_CTL_EN); - MUE_SETBIT(sc, (sc->mue_flags & LAN7500) ? + MUE_SETBIT(un, (un->un_flags & LAN7500) ? MUE_7500_FCT_RX_CTL : MUE_7800_FCT_RX_CTL, MUE_FCT_RX_CTL_EN); /* Set default GPIO/LED settings only if no EEPROM is detected. */ - if ((sc->mue_flags & LAN7500) && !mue_eeprom_present(sc)) { - MUE_CLRBIT(sc, MUE_LED_CFG, MUE_LED_CFG_LED10_FUN_SEL); - MUE_SETBIT(sc, MUE_LED_CFG, + if ((un->un_flags & LAN7500) && !mue_eeprom_present(un)) { + MUE_CLRBIT(un, MUE_LED_CFG, MUE_LED_CFG_LED10_FUN_SEL); + MUE_SETBIT(un, MUE_LED_CFG, MUE_LED_CFG_LEDGPIO_EN | MUE_LED_CFG_LED2_FUN_SEL); } /* XXX We assume two LEDs at least when EEPROM is missing. */ - if (sc->mue_product == USB_PRODUCT_SMSC_LAN7800 && - !mue_eeprom_present(sc)) - MUE_SETBIT(sc, MUE_HW_CFG, + if (un->un_flags & LAN7800 && + !mue_eeprom_present(un)) + MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_LED0_EN | MUE_HW_CFG_LED1_EN); return 0; } static void -mue_set_macaddr(struct mue_softc *sc) +mue_set_macaddr(struct usbnet *un) { - struct ifnet *ifp = GET_IFP(sc); + struct ifnet * const ifp = usbnet_ifp(un); const uint8_t *enaddr = CLLADDR(ifp->if_sadl); uint32_t lo, hi; lo = MUE_ENADDR_LO(enaddr); hi = MUE_ENADDR_HI(enaddr); - mue_csr_write(sc, MUE_RX_ADDRL, lo); - mue_csr_write(sc, MUE_RX_ADDRH, hi); + mue_csr_write(un, MUE_RX_ADDRL, lo); + mue_csr_write(un, MUE_RX_ADDRH, hi); } static int -mue_get_macaddr(struct mue_softc *sc, prop_dictionary_t dict) +mue_get_macaddr(struct usbnet *un, prop_dictionary_t dict) { prop_data_t eaprop; uint32_t low, high; - if (!(sc->mue_flags & LAN7500)) { - low = mue_csr_read(sc, MUE_RX_ADDRL); - high = mue_csr_read(sc, MUE_RX_ADDRH); - sc->mue_enaddr[5] = (uint8_t)((high >> 8) & 0xff); - sc->mue_enaddr[4] = (uint8_t)((high) & 0xff); - sc->mue_enaddr[3] = (uint8_t)((low >> 24) & 0xff); - sc->mue_enaddr[2] = (uint8_t)((low >> 16) & 0xff); - sc->mue_enaddr[1] = (uint8_t)((low >> 8) & 0xff); - sc->mue_enaddr[0] = (uint8_t)((low) & 0xff); - if (ETHER_IS_VALID(sc->mue_enaddr)) + if (!(un->un_flags & LAN7500)) { + low = mue_csr_read(un, MUE_RX_ADDRL); + high = mue_csr_read(un, MUE_RX_ADDRH); + un->un_eaddr[5] = (uint8_t)((high >> 8) & 0xff); + un->un_eaddr[4] = (uint8_t)((high) & 0xff); + un->un_eaddr[3] = (uint8_t)((low >> 24) & 0xff); + un->un_eaddr[2] = (uint8_t)((low >> 16) & 0xff); + un->un_eaddr[1] = (uint8_t)((low >> 8) & 0xff); + un->un_eaddr[0] = (uint8_t)((low) & 0xff); + if (ETHER_IS_VALID(un->un_eaddr)) return 0; else - DPRINTF(sc, "registers: %s\n", - ether_sprintf(sc->mue_enaddr)); + DPRINTF(un, "registers: %s\n", + ether_sprintf(un->un_eaddr)); } - if (mue_eeprom_present(sc) && !mue_read_eeprom(sc, sc->mue_enaddr, + if (mue_eeprom_present(un) && !mue_read_eeprom(un, un->un_eaddr, MUE_E2P_MAC_OFFSET, ETHER_ADDR_LEN)) { - if (ETHER_IS_VALID(sc->mue_enaddr)) + if (ETHER_IS_VALID(un->un_eaddr)) return 0; else - DPRINTF(sc, "EEPROM: %s\n", - ether_sprintf(sc->mue_enaddr)); + DPRINTF(un, "EEPROM: %s\n", + ether_sprintf(un->un_eaddr)); } - if (mue_read_otp(sc, sc->mue_enaddr, MUE_OTP_MAC_OFFSET, + if (mue_read_otp(un, un->un_eaddr, MUE_OTP_MAC_OFFSET, ETHER_ADDR_LEN) == 0) { - if (ETHER_IS_VALID(sc->mue_enaddr)) + if (ETHER_IS_VALID(un->un_eaddr)) return 0; else - DPRINTF(sc, "OTP: %s\n", - ether_sprintf(sc->mue_enaddr)); + DPRINTF(un, "OTP: %s\n", + ether_sprintf(un->un_eaddr)); } /* @@ -872,13 +734,13 @@ mue_get_macaddr(struct mue_softc *sc, pr if (eaprop != NULL) { KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA); KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN); - memcpy(sc->mue_enaddr, prop_data_data_nocopy(eaprop), + memcpy(un->un_eaddr, prop_data_data_nocopy(eaprop), ETHER_ADDR_LEN); - if (ETHER_IS_VALID(sc->mue_enaddr)) + if (ETHER_IS_VALID(un->un_eaddr)) return 0; else - DPRINTF(sc, "prop_dictionary_get: %s\n", - ether_sprintf(sc->mue_enaddr)); + DPRINTF(un, "prop_dictionary_get: %s\n", + ether_sprintf(un->un_eaddr)); } return 1; @@ -899,32 +761,33 @@ mue_match(device_t parent, cfdata_t matc static void mue_attach(device_t parent, device_t self, void *aux) { - struct mue_softc *sc = device_private(self); + USBNET_MII_DECL_DEFAULT(unm); + struct usbnet * const un = device_private(self); prop_dictionary_t dict = device_properties(self); struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; char *devinfop; - struct mii_data *mii; - struct ifnet *ifp; usbd_status err; const char *descr; + uint32_t id_rev; uint8_t i; - int s; + unsigned rx_list_cnt, tx_list_cnt; + unsigned rx_bufsz; aprint_naive("\n"); aprint_normal("\n"); - - sc->mue_dev = self; - sc->mue_udev = dev; - - devinfop = usbd_devinfo_alloc(sc->mue_udev, 0); + devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); - mutex_init(&sc->mue_mii_lock, MUTEX_DEFAULT, IPL_NONE); - mutex_init(&sc->mue_usb_lock, MUTEX_DEFAULT, IPL_SOFTUSB); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = un; + un->un_ops = &mue_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; #define MUE_CONFIG_NO 1 err = usbd_set_config_no(dev, MUE_CONFIG_NO, 1); @@ -934,95 +797,82 @@ mue_attach(device_t parent, device_t sel return; } - usb_init_task(&sc->mue_tick_task, mue_tick_task, sc, 0); - #define MUE_IFACE_IDX 0 - err = usbd_device2interface_handle(dev, MUE_IFACE_IDX, &sc->mue_iface); + err = usbd_device2interface_handle(dev, MUE_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "failed to get interface handle: %s\n", usbd_errstr(err)); return; } - sc->mue_product = uaa->uaa_product; - sc->mue_flags = MUE_LOOKUP(uaa)->mue_flags; - - sc->mue_id_rev = mue_csr_read(sc, MUE_ID_REV); + un->un_flags = MUE_LOOKUP(uaa)->mue_flags; /* Decide on what our bufsize will be. */ - if (sc->mue_flags & LAN7500) { - sc->mue_rxbufsz = (sc->mue_udev->ud_speed == USB_SPEED_HIGH) ? + if (un->un_flags & LAN7500) { + rx_bufsz = (un->un_udev->ud_speed == USB_SPEED_HIGH) ? MUE_7500_HS_RX_BUFSIZE : MUE_7500_FS_RX_BUFSIZE; - sc->mue_rx_list_cnt = 1; - sc->mue_tx_list_cnt = 1; + rx_list_cnt = 1; + tx_list_cnt = 1; } else { - sc->mue_rxbufsz = MUE_7800_RX_BUFSIZE; - sc->mue_rx_list_cnt = MUE_RX_LIST_CNT; - sc->mue_tx_list_cnt = MUE_TX_LIST_CNT; + rx_bufsz = MUE_7800_RX_BUFSIZE; + rx_list_cnt = MUE_RX_LIST_CNT; + tx_list_cnt = MUE_TX_LIST_CNT; } - sc->mue_txbufsz = MUE_TX_BUFSIZE; + + un->un_rx_list_cnt = rx_list_cnt; + un->un_tx_list_cnt = tx_list_cnt; + un->un_rx_bufsz = rx_bufsz; + un->un_tx_bufsz = MUE_TX_BUFSIZE; /* Find endpoints. */ - id = usbd_get_interface_descriptor(sc->mue_iface); + id = usbd_get_interface_descriptor(un->un_iface); for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->mue_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "failed to get ep %hhd\n", i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->mue_ed[MUE_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->mue_ed[MUE_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->mue_ed[MUE_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } } - KASSERT(sc->mue_ed[MUE_ENDPT_RX] != 0); - KASSERT(sc->mue_ed[MUE_ENDPT_TX] != 0); - KASSERT(sc->mue_ed[MUE_ENDPT_INTR] != 0); + if (un->un_ed[USBNET_ENDPT_RX] == 0 || + un->un_ed[USBNET_ENDPT_TX] == 0 || + un->un_ed[USBNET_ENDPT_INTR] == 0) { + aprint_error_dev(self, "failed to find endpoints\n"); + return; + } - s = splnet(); + /* Set these up now for mue_cmd(). */ + usbnet_attach(un, "muedet"); - sc->mue_phyno = 1; + un->un_phyno = 1; - if (mue_chip_init(sc)) { + if (mue_chip_init(un)) { aprint_error_dev(self, "failed to initialize chip\n"); - splx(s); return; } /* A Microchip chip was detected. Inform the world. */ - descr = (sc->mue_flags & LAN7500) ? "LAN7500" : "LAN7800"; + id_rev = mue_csr_read(un, MUE_ID_REV); + descr = (un->un_flags & LAN7500) ? "LAN7500" : "LAN7800"; aprint_normal_dev(self, "%s id 0x%x rev 0x%x\n", descr, - (unsigned)__SHIFTOUT(sc->mue_id_rev, MUE_ID_REV_ID), - (unsigned)__SHIFTOUT(sc->mue_id_rev, MUE_ID_REV_REV)); + (unsigned)__SHIFTOUT(id_rev, MUE_ID_REV_ID), + (unsigned)__SHIFTOUT(id_rev, MUE_ID_REV_REV)); - if (mue_get_macaddr(sc, dict)) { + if (mue_get_macaddr(un, dict)) { aprint_error_dev(self, "failed to read MAC address\n"); - splx(s); return; } - aprint_normal_dev(self, "Ethernet address %s\n", - ether_sprintf(sc->mue_enaddr)); - - /* Initialize interface info.*/ - ifp = GET_IFP(sc); - ifp->if_softc = sc; - strlcpy(ifp->if_xname, device_xname(sc->mue_dev), IFNAMSIZ); - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_init = mue_init; - ifp->if_ioctl = mue_ioctl; - ifp->if_start = mue_start; - ifp->if_stop = mue_stop; - ifp->if_watchdog = mue_watchdog; - - IFQ_SET_READY(&ifp->if_snd); - + struct ifnet *ifp = usbnet_ifp(un); ifp->if_capabilities = IFCAP_TSOv4 | IFCAP_TSOv6 | IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx | @@ -1030,210 +880,30 @@ mue_attach(device_t parent, device_t sel IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx; - sc->mue_ec.ec_capabilities = ETHERCAP_VLAN_MTU; + struct ethercom *ec = usbnet_ec(un); + ec->ec_capabilities = ETHERCAP_VLAN_MTU; #if 0 /* XXX not yet */ - sc->mue_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU; + ec->ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU; #endif - /* Initialize MII/media info. */ - mii = GET_MII(sc); - mii->mii_ifp = ifp; - mii->mii_readreg = mue_miibus_readreg; - mii->mii_writereg = mue_miibus_writereg; - mii->mii_statchg = mue_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - - sc->mue_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, mue_ifmedia_upd, mue_ifmedia_sts); - mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); - - if (LIST_FIRST(&mii->mii_phys) == NULL) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, sc->mue_enaddr); - - rnd_attach_source(&sc->mue_rnd_source, device_xname(sc->mue_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&sc->mue_stat_ch, 0); - - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->mue_udev, sc->mue_dev); -} - -static int -mue_detach(device_t self, int flags) -{ - struct mue_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - size_t i; - int s; - - sc->mue_dying = true; - - callout_halt(&sc->mue_stat_ch, NULL); - - for (i = 0; i < __arraycount(sc->mue_ep); i++) - if (sc->mue_ep[i] != NULL) - usbd_abort_pipe(sc->mue_ep[i]); - - /* - * Remove any pending tasks. They cannot be executing because they run - * in the same thread as detach. - */ - usb_rem_task_wait(sc->mue_udev, &sc->mue_tick_task, USB_TASKQ_DRIVER, - NULL); - - s = splusb(); - - if (ifp->if_flags & IFF_RUNNING) - mue_stop(ifp, 1); - - callout_destroy(&sc->mue_stat_ch); - rnd_detach_source(&sc->mue_rnd_source); - mii_detach(&sc->mue_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->mue_mii.mii_media, IFM_INST_ANY); - if (ifp->if_softc != NULL) { - ether_ifdetach(ifp); - if_detach(ifp); - } - - if (--sc->mue_refcnt >= 0) { - /* Wait for processes to go away. */ - usb_detach_waitold(sc->mue_dev); - } - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->mue_udev, sc->mue_dev); - - mutex_destroy(&sc->mue_mii_lock); - mutex_destroy(&sc->mue_usb_lock); - - return 0; -} - -static int -mue_activate(device_t self, enum devact act) -{ - struct mue_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(ifp); - sc->mue_dying = true; - return 0; - default: - return EOPNOTSUPP; - } - return 0; -} - -static int -mue_rx_list_init(struct mue_softc *sc) -{ - struct mue_cdata *cd; - struct mue_chain *c; - size_t i; - int err; - - cd = &sc->mue_cdata; - for (i = 0; i < sc->mue_rx_list_cnt; i++) { - c = &cd->mue_rx_chain[i]; - c->mue_sc = sc; - if (c->mue_xfer == NULL) { - err = usbd_create_xfer(sc->mue_ep[MUE_ENDPT_RX], - sc->mue_rxbufsz, 0, 0, &c->mue_xfer); - if (err) - return err; - c->mue_buf = usbd_get_buffer(c->mue_xfer); - } - } - - return 0; -} - -static int -mue_tx_list_init(struct mue_softc *sc) -{ - struct mue_cdata *cd; - struct mue_chain *c; - size_t i; - int err; - - cd = &sc->mue_cdata; - for (i = 0; i < sc->mue_tx_list_cnt; i++) { - c = &cd->mue_tx_chain[i]; - c->mue_sc = sc; - if (c->mue_xfer == NULL) { - err = usbd_create_xfer(sc->mue_ep[MUE_ENDPT_TX], - sc->mue_txbufsz, USBD_FORCE_SHORT_XFER, 0, - &c->mue_xfer); - if (err) - return err; - c->mue_buf = usbd_get_buffer(c->mue_xfer); - } - } - - cd->mue_tx_prod = 0; - cd->mue_tx_cnt = 0; - - return 0; -} - -static int -mue_open_pipes(struct mue_softc *sc) -{ - usbd_status err; - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->mue_iface, sc->mue_ed[MUE_ENDPT_RX], - USBD_EXCLUSIVE_USE, &sc->mue_ep[MUE_ENDPT_RX]); - if (err) { - MUE_PRINTF(sc, "rx pipe: %s\n", usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe(sc->mue_iface, sc->mue_ed[MUE_ENDPT_TX], - USBD_EXCLUSIVE_USE, &sc->mue_ep[MUE_ENDPT_TX]); - if (err) { - MUE_PRINTF(sc, "tx pipe: %s\n", usbd_errstr(err)); - return EIO; - } - return 0; -} - -static void -mue_startup_rx_pipes(struct mue_softc *sc) -{ - struct mue_chain *c; - size_t i; - - /* Start up the receive pipe. */ - for (i = 0; i < sc->mue_rx_list_cnt; i++) { - c = &sc->mue_cdata.mue_rx_chain[i]; - usbd_setup_xfer(c->mue_xfer, c, c->mue_buf, sc->mue_rxbufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, mue_rxeof); - usbd_transfer(c->mue_xfer); - } + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, &unm); } -static int -mue_encap(struct mue_softc *sc, struct mbuf *m, int idx) +static unsigned +mue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - struct ifnet *ifp = GET_IFP(sc); - struct mue_chain *c; - usbd_status err; + struct ifnet * const ifp = usbnet_ifp(un); struct mue_txbuf_hdr hdr; uint32_t tx_cmd_a, tx_cmd_b; int csum, len, rv; bool tso, ipe, tpe; + usbnet_isowned_tx(un); + + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - sizeof(hdr)) + return 0; + csum = m->m_pkthdr.csum_flags; tso = csum & (M_CSUM_TSOv4 | M_CSUM_TSOv6); ipe = csum & M_CSUM_IPv4; @@ -1243,12 +913,10 @@ mue_encap(struct mue_softc *sc, struct m len = m->m_pkthdr.len; if (__predict_false((!tso && len > (int)MUE_FRAME_LEN(ifp->if_mtu)) || ( tso && len > MUE_TSO_FRAME_LEN))) { - MUE_PRINTF(sc, "packet length %d\n too long", len); - return EINVAL; + MUE_PRINTF(un, "packet length %d\n too long", len); + return 0; } - c = &sc->mue_cdata.mue_tx_chain[idx]; - KASSERT((len & ~MUE_TX_CMD_A_LEN_MASK) == 0); tx_cmd_a = len | MUE_TX_CMD_A_FCS; @@ -1260,9 +928,9 @@ mue_encap(struct mue_softc *sc, struct m tx_cmd_b = MUE_TX_MSS_MIN; tx_cmd_b <<= MUE_TX_CMD_B_MSS_SHIFT; KASSERT((tx_cmd_b & ~MUE_TX_CMD_B_MSS_MASK) == 0); - rv = mue_prepare_tso(sc, m); + rv = mue_prepare_tso(un, m); if (__predict_false(rv)) - return rv; + return 0; } else { if (ipe) tx_cmd_a |= MUE_TX_CMD_A_IPE; @@ -1274,31 +942,17 @@ mue_encap(struct mue_softc *sc, struct m hdr.tx_cmd_a = htole32(tx_cmd_a); hdr.tx_cmd_b = htole32(tx_cmd_b); - memcpy(c->mue_buf, &hdr, sizeof(hdr)); - m_copydata(m, 0, len, c->mue_buf + sizeof(hdr)); - - if (__predict_false(c->mue_xfer == NULL)) - return EIO; /* XXX plugged out or down */ - - usbd_setup_xfer(c->mue_xfer, c, c->mue_buf, len + sizeof(hdr), - USBD_FORCE_SHORT_XFER, 10000, mue_txeof); - - /* Transmit */ - err = usbd_transfer(c->mue_xfer); - if (__predict_false(err != USBD_IN_PROGRESS)) { - MUE_PRINTF(sc, "%s\n", usbd_errstr(err)); - mue_stop(ifp, 0); - return EIO; - } + memcpy(c->unc_buf, &hdr, sizeof(hdr)); + m_copydata(m, 0, len, c->unc_buf + sizeof(hdr)); - return 0; + return len + sizeof(hdr); } /* * L3 length field should be cleared. */ static int -mue_prepare_tso(struct mue_softc *sc, struct mbuf *m) +mue_prepare_tso(struct usbnet *un, struct mbuf *m) { struct ether_header *eh; struct ip *ip; @@ -1321,10 +975,6 @@ mue_prepare_tso(struct mue_softc *sc, st off = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; break; default: - if (usbd_ratecheck(&sc->mue_tx_notice)) - MUE_PRINTF(sc, "dropping invalid frame " - "type 0x%04hx csum_flags 0x%08x\n", - type, m->m_pkthdr.csum_flags); return EINVAL; } @@ -1347,10 +997,10 @@ mue_prepare_tso(struct mue_softc *sc, st } static void -mue_setmulti(struct mue_softc *sc) +mue_setiff(struct usbnet *un) { - struct ethercom *ec = &sc->mue_ec; - struct ifnet *ifp = GET_IFP(sc); + struct ethercom *ec = usbnet_ec(un); + struct ifnet * const ifp = usbnet_ifp(un); const uint8_t *enaddr = CLLADDR(ifp->if_sadl); struct ether_multi *enm; struct ether_multistep step; @@ -1359,15 +1009,15 @@ mue_setmulti(struct mue_softc *sc) uint32_t reg, rxfilt, h, hireg, loreg; size_t i; - if (sc->mue_dying) + if (usbnet_isdying(un)) return; /* Clear perfect filter and hash tables. */ memset(pfiltbl, 0, sizeof(pfiltbl)); memset(hashtbl, 0, sizeof(hashtbl)); - reg = (sc->mue_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL; - rxfilt = mue_csr_read(sc, reg); + reg = (un->un_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL; + rxfilt = mue_csr_read(un, reg); rxfilt &= ~(MUE_RFE_CTL_PERFECT | MUE_RFE_CTL_MULTICAST_HASH | MUE_RFE_CTL_UNICAST | MUE_RFE_CTL_MULTICAST); @@ -1379,9 +1029,9 @@ mue_setmulti(struct mue_softc *sc) allmulti: rxfilt |= MUE_RFE_CTL_MULTICAST; ifp->if_flags |= IFF_ALLMULTI; if (ifp->if_flags & IFF_PROMISC) - DPRINTF(sc, "promisc\n"); + DPRINTF(un, "promisc\n"); else - DPRINTF(sc, "allmulti\n"); + DPRINTF(un, "allmulti\n"); } else { /* Now program new ones. */ pfiltbl[0][0] = MUE_ENADDR_HI(enaddr) | MUE_ADDR_FILTX_VALID; @@ -1417,117 +1067,94 @@ allmulti: rxfilt |= MUE_RFE_CTL_MULTICAS rxfilt |= MUE_RFE_CTL_PERFECT; ifp->if_flags &= ~IFF_ALLMULTI; if (rxfilt & MUE_RFE_CTL_MULTICAST_HASH) - DPRINTF(sc, "perfect filter and hash tables\n"); + DPRINTF(un, "perfect filter and hash tables\n"); else - DPRINTF(sc, "perfect filter\n"); + DPRINTF(un, "perfect filter\n"); } for (i = 0; i < MUE_NUM_ADDR_FILTX; i++) { - hireg = (sc->mue_flags & LAN7500) ? + hireg = (un->un_flags & LAN7500) ? MUE_7500_ADDR_FILTX(i) : MUE_7800_ADDR_FILTX(i); loreg = hireg + 4; - mue_csr_write(sc, hireg, 0); - mue_csr_write(sc, loreg, pfiltbl[i][1]); - mue_csr_write(sc, hireg, pfiltbl[i][0]); + mue_csr_write(un, hireg, 0); + mue_csr_write(un, loreg, pfiltbl[i][1]); + mue_csr_write(un, hireg, pfiltbl[i][0]); } - mue_dataport_write(sc, MUE_DP_SEL_VHF, MUE_DP_SEL_VHF_VLAN_LEN, + mue_dataport_write(un, MUE_DP_SEL_VHF, MUE_DP_SEL_VHF_VLAN_LEN, MUE_DP_SEL_VHF_HASH_LEN, hashtbl); - mue_csr_write(sc, reg, rxfilt); + mue_csr_write(un, reg, rxfilt); } static void -mue_sethwcsum(struct mue_softc *sc) +mue_sethwcsum(struct usbnet *un) { - struct ifnet *ifp = GET_IFP(sc); + struct ifnet * const ifp = usbnet_ifp(un); uint32_t reg, val; - reg = (sc->mue_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL; - val = mue_csr_read(sc, reg); + reg = (un->un_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL; + val = mue_csr_read(un, reg); if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) { - DPRINTF(sc, "RX IPv4 hwcsum enabled\n"); + DPRINTF(un, "RX IPv4 hwcsum enabled\n"); val |= MUE_RFE_CTL_IP_COE; } else { - DPRINTF(sc, "RX IPv4 hwcsum disabled\n"); + DPRINTF(un, "RX IPv4 hwcsum disabled\n"); val &= ~MUE_RFE_CTL_IP_COE; } if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx | IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx)) { - DPRINTF(sc, "RX L4 hwcsum enabled\n"); + DPRINTF(un, "RX L4 hwcsum enabled\n"); val |= MUE_RFE_CTL_TCPUDP_COE; } else { - DPRINTF(sc, "RX L4 hwcsum disabled\n"); + DPRINTF(un, "RX L4 hwcsum disabled\n"); val &= ~MUE_RFE_CTL_TCPUDP_COE; } val &= ~MUE_RFE_CTL_VLAN_FILTER; - mue_csr_write(sc, reg, val); + mue_csr_write(un, reg, val); } static void -mue_setmtu(struct mue_softc *sc) +mue_setmtu(struct usbnet *un) { - struct ifnet *ifp = GET_IFP(sc); + struct ifnet * const ifp = usbnet_ifp(un); uint32_t val; /* Set the maximum frame size. */ - MUE_CLRBIT(sc, MUE_MAC_RX, MUE_MAC_RX_RXEN); - val = mue_csr_read(sc, MUE_MAC_RX); + MUE_CLRBIT(un, MUE_MAC_RX, MUE_MAC_RX_RXEN); + val = mue_csr_read(un, MUE_MAC_RX); val &= ~MUE_MAC_RX_MAX_SIZE_MASK; val |= MUE_MAC_RX_MAX_LEN(MUE_FRAME_LEN(ifp->if_mtu)); - mue_csr_write(sc, MUE_MAC_RX, val); - MUE_SETBIT(sc, MUE_MAC_RX, MUE_MAC_RX_RXEN); + mue_csr_write(un, MUE_MAC_RX, val); + MUE_SETBIT(un, MUE_MAC_RX, MUE_MAC_RX_RXEN); } static void -mue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +mue_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct mue_chain *c = (struct mue_chain *)priv; - struct mue_softc *sc = c->mue_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; + struct ifnet * const ifp = usbnet_ifp(un); struct mue_rxbuf_hdr *hdrp; - uint32_t rx_cmd_a, totlen; + uint32_t rx_cmd_a; uint16_t pktlen; - int s; int csum; - char *buf = c->mue_buf; + uint8_t *buf = c->unc_buf; bool v6; - if (__predict_false(sc->mue_dying)) { - DPRINTF(sc, "dying\n"); - return; - } - - if (__predict_false(status != USBD_NORMAL_COMPLETION)) { - DPRINTF(sc, "%s\n", usbd_errstr(status)); - if (status == USBD_INVAL) - return; /* XXX plugged out or down */ - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - if (usbd_ratecheck(&sc->mue_rx_notice)) - MUE_PRINTF(sc, "%s\n", usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async( - sc->mue_ep[MUE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &totlen, NULL); + usbnet_isowned_rx(un); - KASSERTMSG(totlen <= sc->mue_rxbufsz, "%u vs %u", - totlen, sc->mue_rxbufsz); + KASSERTMSG(total_len <= un->un_rx_bufsz, "%u vs %u", + total_len, un->un_rx_bufsz); do { - if (__predict_false(totlen < sizeof(*hdrp))) { - MUE_PRINTF(sc, "packet length %u too short\n", totlen); + if (__predict_false(total_len < sizeof(*hdrp))) { + MUE_PRINTF(un, "packet length %u too short\n", total_len); ifp->if_ierrors++; - goto done; + return; } hdrp = (struct mue_rxbuf_hdr *)buf; @@ -1539,34 +1166,23 @@ mue_rxeof(struct usbd_xfer *xfer, void * * it is turned on in the cases of L3/L4 * checksum errors which we handle below. */ - MUE_PRINTF(sc, "rx_cmd_a: 0x%x\n", rx_cmd_a); + MUE_PRINTF(un, "rx_cmd_a: 0x%x\n", rx_cmd_a); ifp->if_ierrors++; - goto done; + return; } pktlen = (uint16_t)(rx_cmd_a & MUE_RX_CMD_A_LEN_MASK); - if (sc->mue_flags & LAN7500) + if (un->un_flags & LAN7500) pktlen -= 2; if (__predict_false(pktlen < ETHER_HDR_LEN + ETHER_CRC_LEN || pktlen > MCLBYTES - ETHER_ALIGN || /* XXX */ - pktlen + sizeof(*hdrp) > totlen)) { - MUE_PRINTF(sc, "invalid packet length %d\n", pktlen); + pktlen + sizeof(*hdrp) > total_len)) { + MUE_PRINTF(un, "invalid packet length %d\n", pktlen); ifp->if_ierrors++; - goto done; - } - - m = mue_newbuf(); - if (__predict_false(m == NULL)) { - MUE_PRINTF(sc, "failed to allocate mbuf\n"); - ifp->if_ierrors++; - goto done; + return; } - m_set_rcvif(m, ifp); - m->m_pkthdr.len = m->m_len = pktlen; - m->m_flags |= M_HASFCS; - if (__predict_false(rx_cmd_a & MUE_RX_CMD_A_ICSM)) { csum = 0; } else { @@ -1595,380 +1211,110 @@ mue_rxeof(struct usbd_xfer *xfer, void * (rx_cmd_a & MUE_RX_CMD_A_TCE))) csum |= M_CSUM_TCP_UDP_BAD; } - m->m_pkthdr.csum_flags = csum; - memcpy(mtod(m, char *), buf + sizeof(*hdrp), pktlen); + + usbnet_enqueue(un, buf + sizeof(*hdrp), pktlen, csum, + 0, M_HASFCS); /* Attention: sizeof(hdr) = 10 */ pktlen = roundup(pktlen + sizeof(*hdrp), 4); - if (pktlen > totlen) - pktlen = totlen; - totlen -= pktlen; + if (pktlen > total_len) + pktlen = total_len; + total_len -= pktlen; buf += pktlen; - - s = splnet(); - if_percpuq_enqueue(ifp->if_percpuq, m); - splx(s); - } while (totlen > 0); - -done: - /* Setup new transfer. */ - usbd_setup_xfer(xfer, c, c->mue_buf, sc->mue_rxbufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, mue_rxeof); - usbd_transfer(xfer); -} - -static void -mue_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) -{ - struct mue_chain *c = priv; - struct mue_softc *sc = c->mue_sc; - struct mue_cdata *cd = &sc->mue_cdata; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (__predict_false(sc->mue_dying)) - return; - - s = splnet(); - KASSERT(cd->mue_tx_cnt > 0); - cd->mue_tx_cnt--; - ifp->if_timer = 0; - if (__predict_false(status != USBD_NORMAL_COMPLETION)) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - if (usbd_ratecheck(&sc->mue_tx_notice)) - MUE_PRINTF(sc, "%s\n", usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async( - sc->mue_ep[MUE_ENDPT_TX]); - splx(s); - ifp->if_flags &= ~IFF_OACTIVE; - return; - } - - ifp->if_flags &= ~IFF_OACTIVE; - - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - mue_start(ifp); - - ifp->if_opackets++; - splx(s); + } while (total_len > 0); } static int -mue_init(struct ifnet *ifp) +mue_init_locked(struct ifnet *ifp) { - struct mue_softc *sc = ifp->if_softc; - int s; + struct usbnet * const un = ifp->if_softc; - if (sc->mue_dying) { - DPRINTF(sc, "dying\n"); + if (usbnet_isdying(un)) { + DPRINTF(un, "dying\n"); return EIO; } - s = splnet(); - /* Cancel pending I/O and free all TX/RX buffers. */ if (ifp->if_flags & IFF_RUNNING) - mue_stop(ifp, 1); + usbnet_stop(un, ifp, 1); - mue_reset(sc); + mue_reset(un); /* Set MAC address. */ - mue_set_macaddr(sc); + mue_set_macaddr(un); /* Load the multicast filter. */ - mue_setmulti(sc); + mue_setiff(un); /* TCP/UDP checksum offload engines. */ - mue_sethwcsum(sc); + mue_sethwcsum(un); /* Set MTU. */ - mue_setmtu(sc); - - if (mue_open_pipes(sc)) { - splx(s); - return EIO; - } - - /* Init RX ring. */ - if (mue_rx_list_init(sc)) { - MUE_PRINTF(sc, "failed to init rx list\n"); - splx(s); - return ENOBUFS; - } - - /* Init TX ring. */ - if (mue_tx_list_init(sc)) { - MUE_PRINTF(sc, "failed to init tx list\n"); - splx(s); - return ENOBUFS; - } + mue_setmtu(un); - mue_startup_rx_pipes(sc); - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; + return usbnet_init_rx_tx(un); +} - splx(s); +static int +mue_init(struct ifnet *ifp) +{ + struct usbnet * const un = ifp->if_softc; + int rv; - callout_reset(&sc->mue_stat_ch, hz, mue_tick, sc); + usbnet_lock(un); + rv = mue_init_locked(ifp); + usbnet_unlock(un); - return 0; + return rv; } static int -mue_ioctl(struct ifnet *ifp, u_long cmd, void *data) +mue_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct mue_softc *sc = ifp->if_softc; - int s, error = 0; - - s = splnet(); + struct usbnet * const un = ifp->if_softc; switch (cmd) { case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, cmd, data)) != 0) - break; - - switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { - case IFF_RUNNING: - mue_stop(ifp, 1); - break; - case IFF_UP: - mue_init(ifp); - break; - case IFF_UP | IFF_RUNNING: - if ((ifp->if_flags ^ sc->mue_if_flags) == IFF_PROMISC) - mue_setmulti(sc); - else - mue_init(ifp); - break; - } - sc->mue_if_flags = ifp->if_flags; + case SIOCSETHERCAP: + case SIOCADDMULTI: + case SIOCDELMULTI: + mue_setiff(un); + break; + case SIOCSIFCAP: + mue_sethwcsum(un); + break; + case SIOCSIFMTU: + mue_setmtu(un); break; default: - if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) - break; - error = 0; - switch (cmd) { - case SIOCADDMULTI: - case SIOCDELMULTI: - mue_setmulti(sc); - break; - case SIOCSIFCAP: - mue_sethwcsum(sc); - break; - case SIOCSIFMTU: - mue_setmtu(sc); - break; - default: - break; - } break; } - splx(s); - return error; -} - -static void -mue_watchdog(struct ifnet *ifp) -{ - struct mue_softc *sc = ifp->if_softc; - struct mue_chain *c; - usbd_status stat; - int s; - - ifp->if_oerrors++; - MUE_PRINTF(sc, "timed out\n"); - - s = splusb(); - c = &sc->mue_cdata.mue_tx_chain[0]; - usbd_get_xfer_status(c->mue_xfer, NULL, NULL, NULL, &stat); - mue_txeof(c->mue_xfer, c, stat); - - if (!IFQ_IS_EMPTY(&ifp->if_snd)) - mue_start(ifp); - splx(s); + return 0; } static void -mue_reset(struct mue_softc *sc) +mue_reset(struct usbnet *un) { - if (sc->mue_dying) + if (usbnet_isdying(un)) return; /* Wait a little while for the chip to get its brains in order. */ - usbd_delay_ms(sc->mue_udev, 1); + usbd_delay_ms(un->un_udev, 1); -// mue_chip_init(sc); /* XXX */ +// mue_chip_init(un); /* XXX */ } static void -mue_start(struct ifnet *ifp) +mue_stop_cb(struct ifnet *ifp, int disable) { - struct mue_softc *sc = ifp->if_softc; - struct mbuf *m; - struct mue_cdata *cd = &sc->mue_cdata; - int idx; - - if (__predict_false(!sc->mue_link)) { - DPRINTF(sc, "no link\n"); - return; - } - - if (__predict_false((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) - != IFF_RUNNING)) { - DPRINTF(sc, "not ready\n"); - return; - } - - mutex_enter(&sc->mue_usb_lock); + struct usbnet * const un = ifp->if_softc; - idx = cd->mue_tx_prod; - while (cd->mue_tx_cnt < (int)sc->mue_tx_list_cnt) { - IFQ_POLL(&ifp->if_snd, m); - if (m == NULL) - break; - - if (__predict_false(mue_encap(sc, m, idx))) { - ifp->if_oerrors++; - break; - } - IFQ_DEQUEUE(&ifp->if_snd, m); - - bpf_mtap(ifp, m, BPF_D_OUT); - m_freem(m); - - cd->mue_tx_cnt++; - idx = (idx + 1) % sc->mue_tx_list_cnt; - } - cd->mue_tx_prod = idx; - - if (cd->mue_tx_cnt >= (int)sc->mue_tx_list_cnt) - ifp->if_flags |= IFF_OACTIVE; - - mutex_exit(&sc->mue_usb_lock); - - /* Set a timeout in case the chip goes out to lunch. */ - ifp->if_timer = 5; + mue_reset(un); } -static void -mue_stop(struct ifnet *ifp, int disable __unused) -{ - struct mue_softc *sc = ifp->if_softc; - struct mue_chain *c; - usbd_status err; - size_t i; - - mue_reset(sc); - - ifp->if_timer = 0; - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); - - callout_stop(&sc->mue_stat_ch); - sc->mue_link = 0; - - /* Stop transfers. */ - for (i = 0; i < __arraycount(sc->mue_ep); i++) - if (sc->mue_ep[i] != NULL) { - err = usbd_abort_pipe(sc->mue_ep[i]); - if (err) - MUE_PRINTF(sc, "abort pipe %zu: %s\n", - i, usbd_errstr(err)); - } - - /* Free RX resources. */ - for (i = 0; i < sc->mue_rx_list_cnt; i++) { - c = &sc->mue_cdata.mue_rx_chain[i]; - if (c->mue_xfer != NULL) { - usbd_destroy_xfer(c->mue_xfer); - c->mue_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < sc->mue_tx_list_cnt; i++) { - c = &sc->mue_cdata.mue_tx_chain[i]; - if (c->mue_xfer != NULL) { - usbd_destroy_xfer(c->mue_xfer); - c->mue_xfer = NULL; - } - } - - /* Close pipes */ - for (i = 0; i < __arraycount(sc->mue_ep); i++) - if (sc->mue_ep[i] != NULL) { - err = usbd_close_pipe(sc->mue_ep[i]); - if (err) - MUE_PRINTF(sc, "close pipe %zu: %s\n", - i, usbd_errstr(err)); - sc->mue_ep[i] = NULL; - } - - DPRINTF(sc, "done\n"); -} - -static void -mue_tick(void *xsc) -{ - struct mue_softc *sc = xsc; - - if (sc == NULL) - return; - - if (sc->mue_dying) - return; - - /* Perform periodic stuff in process context. */ - usb_add_task(sc->mue_udev, &sc->mue_tick_task, USB_TASKQ_DRIVER); -} - -static void -mue_tick_task(void *xsc) -{ - struct mue_softc *sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - int s; - - if (sc == NULL) - return; - - if (sc->mue_dying) - return; - - ifp = GET_IFP(sc); - mii = GET_MII(sc); - - s = splnet(); - mii_tick(mii); - if (sc->mue_link == 0) - mue_miibus_statchg(ifp); - callout_reset(&sc->mue_stat_ch, hz, mue_tick, sc); - splx(s); -} - -static struct mbuf * -mue_newbuf(void) -{ - struct mbuf *m; - - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (__predict_false(m == NULL)) - return NULL; - - MCLGET(m, M_DONTWAIT); - if (__predict_false(!(m->m_flags & M_EXT))) { - m_freem(m); - return NULL; - } - - m_adj(m, ETHER_ALIGN); +#ifdef _MODULE +#include "ioconf.c" +#endif - return m; -} +USBNET_MODULE(mue) Index: usb/if_muereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_muereg.h,v retrieving revision 1.5 diff -p -u -u -r1.5 if_muereg.h --- usb/if_muereg.h 3 Feb 2019 13:11:07 -0000 1.5 +++ usb/if_muereg.h 29 Aug 2019 06:22:05 -0000 @@ -24,8 +24,10 @@ #include /* XXX for IP_MAXPACKET */ +#include #include - +/* XXX for struct ip6_hdr */ +#include /* XXX for struct mue_txbuf_hdr */ #include Index: usb/if_muevar.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_muevar.h,v retrieving revision 1.8 diff -p -u -u -r1.8 if_muevar.h --- usb/if_muevar.h 26 May 2019 04:52:07 -0000 1.8 +++ usb/if_muevar.h 29 Aug 2019 06:22:05 -0000 @@ -20,26 +20,12 @@ #ifndef _IF_MUEVAR_H_ #define _IF_MUEVAR_H_ -#include - -struct mue_chain { - struct mue_softc *mue_sc; - struct usbd_xfer *mue_xfer; - char *mue_buf; -}; - -struct mue_cdata { #ifndef MUE_TX_LIST_CNT #define MUE_TX_LIST_CNT 4 #endif - struct mue_chain mue_tx_chain[MUE_TX_LIST_CNT]; #ifndef MUE_RX_LIST_CNT #define MUE_RX_LIST_CNT 4 #endif - struct mue_chain mue_rx_chain[MUE_RX_LIST_CNT]; - int mue_tx_prod; - int mue_tx_cnt; -}; struct mue_rxbuf_hdr { uint32_t rx_cmd_a; @@ -74,55 +60,4 @@ struct mue_txbuf_hdr { } __packed; -struct mue_softc { - device_t mue_dev; - bool mue_dying; - - uint8_t mue_enaddr[ETHER_ADDR_LEN]; - struct ethercom mue_ec; - struct mii_data mue_mii; -#define GET_MII(sc) (&(sc)->mue_mii) -#define GET_IFP(sc) (&(sc)->mue_ec.ec_if) - -/* The interrupt endpoint is currently unused by the Moschip part. */ -#define MUE_ENDPT_RX 0 -#define MUE_ENDPT_TX 1 -#define MUE_ENDPT_INTR 2 -#define MUE_ENDPT_MAX 3 - int mue_ed[MUE_ENDPT_MAX]; - struct usbd_pipe *mue_ep[MUE_ENDPT_MAX]; - - struct mue_cdata mue_cdata; - callout_t mue_stat_ch; - - struct usbd_device *mue_udev; - struct usbd_interface *mue_iface; - - struct usb_task mue_tick_task; - - kmutex_t mue_mii_lock; - - struct timeval mue_rx_notice; - struct timeval mue_tx_notice; - - uint16_t mue_product; - uint16_t mue_flags; - uint32_t mue_id_rev; - - int mue_if_flags; - int mue_refcnt; - - krndsource_t mue_rnd_source; - - int mue_phyno; - uint32_t mue_rxbufsz; - uint32_t mue_txbufsz; - int mue_link; - - unsigned mue_rx_list_cnt; - unsigned mue_tx_list_cnt; - - kmutex_t mue_usb_lock; -}; - #endif /* _IF_MUEVAR_H_ */ Index: usb/if_smsc.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_smsc.c,v retrieving revision 1.45 diff -p -u -u -r1.45 if_smsc.c --- usb/if_smsc.c 23 May 2019 13:10:52 -0000 1.45 +++ usb/if_smsc.c 29 Aug 2019 06:22:05 -0000 @@ -65,50 +65,32 @@ __KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v #ifdef _KERNEL_OPT #include "opt_usb.h" -#include "opt_inet.h" #endif #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#ifdef INET -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include +#include +#include #include -#include #include "ioconf.h" -#ifdef USB_DEBUG -int smsc_debug = 0; -#endif +struct smsc_softc { + struct usbnet smsc_un; + + /* + * The following stores the settings in the mac control (MAC_CSR) + * register + */ + uint32_t sc_mac_csr; + uint32_t sc_rev_id; + + uint32_t sc_coe_ctrl; +}; + +#define SMSC_MIN_BUFSZ 2048 +#define SMSC_MAX_BUFSZ 18944 /* * Various supported device vendors/products. @@ -135,85 +117,113 @@ static const struct usb_devno smsc_devs[ }; #ifdef USB_DEBUG -#define smsc_dbg_printf(sc, fmt, args...) \ - do { \ - if (smsc_debug > 0) \ - printf("debug: " fmt, ##args); \ - } while(0) +#ifndef USMSC_DEBUG +#define usmscdebug 0 #else -#define smsc_dbg_printf(sc, fmt, args...) -#endif +static int usmscdebug = 1; + +SYSCTL_SETUP(sysctl_hw_smsc_setup, "sysctl hw.usmsc setup") +{ + int err; + const struct sysctlnode *rnode; + const struct sysctlnode *cnode; -#define smsc_warn_printf(sc, fmt, args...) \ - printf("%s: warning: " fmt, device_xname((sc)->sc_dev), ##args) + err = sysctl_createv(clog, 0, NULL, &rnode, + CTLFLAG_PERMANENT, CTLTYPE_NODE, "usmsc", + SYSCTL_DESCR("usmsc global controls"), + NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); -#define smsc_err_printf(sc, fmt, args...) \ - printf("%s: error: " fmt, device_xname((sc)->sc_dev), ##args) + if (err) + goto fail; -/* Function declarations */ -int smsc_chip_init(struct smsc_softc *); -void smsc_setmulti(struct smsc_softc *); -int smsc_setmacaddress(struct smsc_softc *, const uint8_t *); + /* control debugging printfs */ + err = sysctl_createv(clog, 0, &rnode, &cnode, + CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, + "debug", SYSCTL_DESCR("Enable debugging output"), + NULL, 0, &usmscdebug, sizeof(usmscdebug), CTL_CREATE, CTL_EOL); + if (err) + goto fail; + + return; +fail: + aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); +} + +#endif /* SMSC_DEBUG */ +#endif /* USB_DEBUG */ +#define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(usmscdebug,FMT,A,B,C,D) +#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usmscdebug,N,FMT,A,B,C,D) +#define USMSCHIST_FUNC() USBHIST_FUNC() +#define USMSCHIST_CALLED() USBHIST_CALLED(usmscdebug) + +#define smsc_warn_printf(un, fmt, args...) \ + printf("%s: warning: " fmt, device_xname((un)->un_dev), ##args) + +#define smsc_err_printf(un, fmt, args...) \ + printf("%s: error: " fmt, device_xname((un)->un_dev), ##args) + +/* Function declarations */ int smsc_match(device_t, cfdata_t, void *); void smsc_attach(device_t, device_t, void *); -int smsc_detach(device_t, int); -int smsc_activate(device_t, enum devact); + +CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), + smsc_match, smsc_attach, usbnet_detach, usbnet_activate); + +int smsc_chip_init(struct usbnet *); +int smsc_setmacaddress(struct usbnet *, const uint8_t *); int smsc_init(struct ifnet *); int smsc_init_locked(struct ifnet *); -void smsc_start(struct ifnet *); -void smsc_start_locked(struct ifnet *); int smsc_ioctl(struct ifnet *, u_long, void *); -void smsc_stop(struct ifnet *, int); -void smsc_stop_locked(struct ifnet *, int); +void smsc_stop_cb(struct ifnet *, int); void smsc_reset(struct smsc_softc *); -struct mbuf *smsc_newbuf(void); -void smsc_tick(void *); -void smsc_tick_task(void *); -void smsc_miibus_statchg(struct ifnet *); -void smsc_miibus_statchg_locked(struct ifnet *); -int smsc_miibus_readreg(device_t, int, int, uint16_t *); -int smsc_miibus_writereg(device_t, int, int, uint16_t); -int smsc_ifmedia_upd(struct ifnet *); -void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *); -void smsc_lock_mii(struct smsc_softc *); -void smsc_unlock_mii(struct smsc_softc *); - -int smsc_tx_list_init(struct smsc_softc *); -void smsc_tx_list_free(struct smsc_softc *); -int smsc_rx_list_init(struct smsc_softc *); -void smsc_rx_list_free(struct smsc_softc *); -int smsc_encap(struct smsc_softc *, struct mbuf *, int); -void smsc_rxeof(struct usbd_xfer *, void *, usbd_status); -void smsc_txeof(struct usbd_xfer *, void *, usbd_status); - -int smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *); -int smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t); -int smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t); -int smsc_sethwcsum(struct smsc_softc *); - -CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach, - smsc_detach, smsc_activate); +static void smsc_miibus_statchg(struct ifnet *); +int smsc_readreg(struct usbnet *, uint32_t, uint32_t *); +int smsc_writereg(struct usbnet *, uint32_t, uint32_t); +int smsc_wait_for_bits(struct usbnet *, uint32_t, uint32_t); +static int smsc_miibus_readreg(struct usbnet *, int, int, uint16_t *); +static int smsc_miibus_writereg(struct usbnet *, int, int, uint16_t); + +static int smsc_ioctl_cb(struct ifnet *, u_long, void *); +static unsigned smsc_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void smsc_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); + +static struct usbnet_ops smsc_ops = { + .uno_stop = smsc_stop_cb, + .uno_ioctl = smsc_ioctl_cb, + .uno_read_reg = smsc_miibus_readreg, + .uno_write_reg = smsc_miibus_writereg, + .uno_statchg = smsc_miibus_statchg, + .uno_tx_prepare = smsc_tx_prepare, + .uno_rx_loop = smsc_rx_loop, + .uno_init = smsc_init, +}; int -smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data) +smsc_readreg(struct usbnet *un, uint32_t off, uint32_t *data) { usb_device_request_t req; uint32_t buf; usbd_status err; + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) + return 0; + req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = SMSC_UR_READ_REG; USETW(req.wValue, 0); USETW(req.wIndex, off); USETW(req.wLength, 4); - err = usbd_do_request(sc->sc_udev, &req, &buf); + err = usbd_do_request(un->un_udev, &req, &buf); if (err != 0) - smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off); + smsc_warn_printf(un, "Failed to read register 0x%0x\n", off); *data = le32toh(buf); @@ -221,12 +231,17 @@ smsc_read_reg(struct smsc_softc *sc, uin } int -smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data) +smsc_writereg(struct usbnet *un, uint32_t off, uint32_t data) { usb_device_request_t req; uint32_t buf; usbd_status err; + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) + return 0; + buf = htole32(data); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; @@ -235,21 +250,21 @@ smsc_write_reg(struct smsc_softc *sc, ui USETW(req.wIndex, off); USETW(req.wLength, 4); - err = usbd_do_request(sc->sc_udev, &req, &buf); + err = usbd_do_request(un->un_udev, &req, &buf); if (err != 0) - smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off); + smsc_warn_printf(un, "Failed to write register 0x%0x\n", off); return err; } int -smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits) +smsc_wait_for_bits(struct usbnet *un, uint32_t reg, uint32_t bits) { uint32_t val; int err, i; for (i = 0; i < 100; i++) { - if ((err = smsc_read_reg(sc, reg, &val)) != 0) + if ((err = smsc_readreg(un, reg, &val)) != 0) return err; if (!(val & bits)) return 0; @@ -259,62 +274,58 @@ smsc_wait_for_bits(struct smsc_softc *sc return 1; } -int -smsc_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) +static int +smsc_miibus_readreg(struct usbnet *un, int phy, int reg, uint16_t *val) { - struct smsc_softc * const sc = device_private(dev); uint32_t addr; uint32_t data = 0; - int rv = 0; - smsc_lock_mii(sc); - if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { - smsc_warn_printf(sc, "MII is busy\n"); - rv = -1; - goto done; + usbnet_isowned_mii(un); + + if (un->un_phyno != phy) + return EINVAL; + + if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { + smsc_warn_printf(un, "MII is busy\n"); + return ETIMEDOUT; } addr = (phy << 11) | (reg << 6) | SMSC_MII_READ; - smsc_write_reg(sc, SMSC_MII_ADDR, addr); + smsc_writereg(un, SMSC_MII_ADDR, addr); - if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { - smsc_warn_printf(sc, "MII read timeout\n"); - rv = ETIMEDOUT; + if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { + smsc_warn_printf(un, "MII read timeout\n"); + return ETIMEDOUT; } - smsc_read_reg(sc, SMSC_MII_DATA, &data); - -done: - smsc_unlock_mii(sc); + smsc_readreg(un, SMSC_MII_DATA, &data); *val = data & 0xffff; - return rv; + return 0; } -int -smsc_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +static int +smsc_miibus_writereg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct smsc_softc * const sc = device_private(dev); uint32_t addr; - if (sc->sc_phyno != phy) - return -1; + usbnet_isowned_mii(un); + + if (un->un_phyno != phy) + return EINVAL; - smsc_lock_mii(sc); - if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { - smsc_warn_printf(sc, "MII is busy\n"); - smsc_unlock_mii(sc); - return -1; + if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { + smsc_warn_printf(un, "MII is busy\n"); + return ETIMEDOUT; } - smsc_write_reg(sc, SMSC_MII_DATA, val); + smsc_writereg(un, SMSC_MII_DATA, val); addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE; - smsc_write_reg(sc, SMSC_MII_ADDR, addr); - smsc_unlock_mii(sc); + smsc_writereg(un, SMSC_MII_ADDR, addr); - if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { - smsc_warn_printf(sc, "MII write timeout\n"); + if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { + smsc_warn_printf(un, "MII write timeout\n"); return ETIMEDOUT; } @@ -324,46 +335,23 @@ smsc_miibus_writereg(device_t dev, int p void smsc_miibus_statchg(struct ifnet *ifp) { - if (ifp == NULL) - return; - - struct smsc_softc * const sc = ifp->if_softc; + USMSCHIST_FUNC(); USMSCHIST_CALLED(); + struct usbnet * const un = ifp->if_softc; - mutex_enter(&sc->sc_lock); - if (sc->sc_dying) { - mutex_exit(&sc->sc_lock); + if (usbnet_isdying(un)) return; - } - smsc_miibus_statchg_locked(ifp); - - mutex_exit(&sc->sc_lock); -} - -void -smsc_miibus_statchg_locked(struct ifnet *ifp) -{ - struct smsc_softc * const sc = ifp->if_softc; - struct mii_data * const mii = &sc->sc_mii; - int err; + struct smsc_softc * const sc = usbnet_softc(un); + struct mii_data * const mii = usbnet_mii(un); uint32_t flow; uint32_t afc_cfg; - KASSERT(mutex_owned(&sc->sc_lock)); - - if ((ifp->if_flags & IFF_RUNNING) == 0) { - smsc_dbg_printf(sc, "%s: not running\n", __func__); - return; - } - - /* Use the MII status to determine link status */ - sc->sc_flags &= ~SMSC_FLAG_LINK; if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == (IFM_ACTIVE | IFM_AVALID)) { switch (IFM_SUBTYPE(mii->mii_media_active)) { case IFM_10_T: case IFM_100_TX: - sc->sc_flags |= SMSC_FLAG_LINK; + usbnet_set_link(un, true); break; case IFM_1000_T: /* Gigabit ethernet not supported by chipset */ @@ -374,21 +362,21 @@ smsc_miibus_statchg_locked(struct ifnet } /* Lost link, do nothing. */ - if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { - smsc_dbg_printf(sc, "link flag not set\n"); + if (!usbnet_havelink(un)) return; - } - err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg); + usbnet_lock_mii(un); + int err = smsc_readreg(un, SMSC_AFC_CFG, &afc_cfg); + usbnet_unlock_mii(un); if (err) { - smsc_warn_printf(sc, "failed to read initial AFC_CFG, " + smsc_warn_printf(un, "failed to read initial AFC_CFG, " "error %d\n", err); return; } /* Enable/disable full duplex operation and TX/RX pause */ if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { - smsc_dbg_printf(sc, "full duplex operation\n"); + DPRINTF("full duplex operation", 0, 0, 0, 0); sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN; sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX; @@ -402,7 +390,7 @@ smsc_miibus_statchg_locked(struct ifnet else afc_cfg &= ~0xf; } else { - smsc_dbg_printf(sc, "half duplex operation\n"); + DPRINTF("half duplex operation", 0, 0, 0, 0); sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX; sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN; @@ -410,44 +398,14 @@ smsc_miibus_statchg_locked(struct ifnet afc_cfg |= 0xf; } - err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); - err += smsc_write_reg(sc, SMSC_FLOW, flow); - err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg); - if (err) - smsc_warn_printf(sc, "media change failed, error %d\n", err); -} - -int -smsc_ifmedia_upd(struct ifnet *ifp) -{ - struct smsc_softc * const sc = ifp->if_softc; - struct mii_data * const mii = &sc->sc_mii; - int err; + usbnet_lock_mii(un); + err = smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr); + err += smsc_writereg(un, SMSC_FLOW, flow); + err += smsc_writereg(un, SMSC_AFC_CFG, afc_cfg); + usbnet_unlock_mii(un); - if (mii->mii_instance) { - struct mii_softc *miisc; - - LIST_FOREACH(miisc, &mii->mii_phys, mii_list) - mii_phy_reset(miisc); - } - err = mii_mediachg(mii); - return err; -} - -void -smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) -{ - struct smsc_softc * const sc = ifp->if_softc; - struct mii_data * const mii = &sc->sc_mii; - - /* SMSC_LOCK */ - - mii_pollstat(mii); - - ifmr->ifm_active = mii->mii_media_active; - ifmr->ifm_status = mii->mii_media_status; - - /* SMSC_UNLOCK */ + if (err) + smsc_warn_printf(un, "media change failed, error %d\n", err); } static inline uint32_t @@ -457,27 +415,29 @@ smsc_hash(uint8_t addr[ETHER_ADDR_LEN]) return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f; } -void -smsc_setmulti(struct smsc_softc *sc) +static void +smsc_setiff_locked(struct usbnet *un) { - struct ethercom *ec = &sc->sc_ec; - struct ifnet * const ifp = &ec->ec_if; + USMSCHIST_FUNC(); USMSCHIST_CALLED(); + struct smsc_softc * const sc = usbnet_softc(un); + struct ifnet * const ifp = usbnet_ifp(un); + struct ethercom *ec = usbnet_ec(un); struct ether_multi *enm; struct ether_multistep step; uint32_t hashtbl[2] = { 0, 0 }; uint32_t hash; - KASSERT(mutex_owned(&sc->sc_lock)); + usbnet_isowned_mii(un); - if (sc->sc_dying) + if (usbnet_isdying(un)) return; if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { allmulti: - smsc_dbg_printf(sc, "receive all multicast enabled\n"); + DPRINTF("receive all multicast enabled", 0, 0, 0, 0); sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS; sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT; - smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); + smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr); return; } else { sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT; @@ -500,28 +460,41 @@ allmulti: /* Debug */ if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) { - smsc_dbg_printf(sc, "receive select group of macs\n"); + DPRINTF("receive select group of macs", 0, 0, 0, 0); } else { - smsc_dbg_printf(sc, "receive own packets only\n"); + DPRINTF("receive own packets only", 0, 0, 0, 0); } /* Write the hash table and mac control registers */ + + //XXX should we be doing this? ifp->if_flags &= ~IFF_ALLMULTI; - smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]); - smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]); - smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); + smsc_writereg(un, SMSC_HASHH, hashtbl[1]); + smsc_writereg(un, SMSC_HASHL, hashtbl[0]); + smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr); } -int -smsc_sethwcsum(struct smsc_softc *sc) +static void +smsc_setiff(struct usbnet *un) { - struct ifnet * const ifp = &sc->sc_ec.ec_if; + usbnet_lock_mii(un); + smsc_setiff_locked(un); + usbnet_unlock_mii(un); +} + +static int +smsc_setoe_locked(struct usbnet *un) +{ + struct smsc_softc * const sc = usbnet_softc(un); + struct ifnet * const ifp = usbnet_ifp(un); uint32_t val; int err; - err = smsc_read_reg(sc, SMSC_COE_CTRL, &val); + usbnet_isowned_mii(un); + + err = smsc_readreg(un, SMSC_COE_CTRL, &val); if (err != 0) { - smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", + smsc_warn_printf(un, "failed to read SMSC_COE_CTRL (err=%d)\n", err); return err; } @@ -540,9 +513,9 @@ smsc_sethwcsum(struct smsc_softc *sc) sc->sc_coe_ctrl = val; - err = smsc_write_reg(sc, SMSC_COE_CTRL, val); + err = smsc_writereg(un, SMSC_COE_CTRL, val); if (err != 0) { - smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", + smsc_warn_printf(un, "failed to write SMSC_COE_CTRL (err=%d)\n", err); return err; } @@ -550,22 +523,34 @@ smsc_sethwcsum(struct smsc_softc *sc) return 0; } +static void +smsc_setoe(struct usbnet *un) +{ + + usbnet_lock_mii(un); + smsc_setoe_locked(un); + usbnet_unlock_mii(un); +} + + int -smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr) +smsc_setmacaddress(struct usbnet *un, const uint8_t *addr) { + USMSCHIST_FUNC(); USMSCHIST_CALLED(); int err; uint32_t val; - smsc_dbg_printf(sc, "setting mac address to " - "%02x:%02x:%02x:%02x:%02x:%02x\n", - addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); + DPRINTF("setting mac address to %02jx:%02jx:%02jx:...", addr[0], addr[1], + addr[2], 0); + + DPRINTF("... %02jx:%0j2x:%02jx", addr[3], addr[4], addr[5], 0); val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; - if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0) + if ((err = smsc_writereg(un, SMSC_MAC_ADDRL, val)) != 0) goto done; val = (addr[5] << 8) | addr[4]; - err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val); + err = smsc_writereg(un, SMSC_MAC_ADDRH, val); done: return err; @@ -574,25 +559,27 @@ done: void smsc_reset(struct smsc_softc *sc) { - KASSERT(mutex_owned(&sc->sc_lock)); - if (sc->sc_dying) + struct usbnet * const un = &sc->smsc_un; + + usbnet_isowned(un); + if (usbnet_isdying(un)) return; /* Wait a little while for the chip to get its brains in order. */ DELAY(1000); /* Reinitialize controller to achieve full reset. */ - smsc_chip_init(sc); + smsc_chip_init(un); } int smsc_init(struct ifnet *ifp) { - struct smsc_softc * const sc = ifp->if_softc; + struct usbnet * const un = ifp->if_softc; - mutex_enter(&sc->sc_lock); + usbnet_lock(un); int ret = smsc_init_locked(ifp); - mutex_exit(&sc->sc_lock); + usbnet_unlock(un); return ret; } @@ -600,295 +587,77 @@ smsc_init(struct ifnet *ifp) int smsc_init_locked(struct ifnet *ifp) { - struct smsc_softc * const sc = ifp->if_softc; - usbd_status err; + struct usbnet * const un = ifp->if_softc; + struct smsc_softc * const sc = usbnet_softc(un); - if (sc->sc_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O */ - smsc_stop_locked(ifp, 1); + usbnet_stop(un, ifp, 1); /* Reset the ethernet interface. */ smsc_reset(sc); + usbnet_lock_mii_un_locked(un); + /* Load the multicast filter. */ - smsc_setmulti(sc); + smsc_setiff_locked(un); /* TCP/UDP checksum offload engines. */ - smsc_sethwcsum(sc); - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_RX]); - if (err) { - printf("%s: open rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - goto fail; - } - - err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_TX]); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - goto fail1; - } - - /* Init RX ring. */ - if (smsc_rx_list_init(sc)) { - aprint_error_dev(sc->sc_dev, "rx list init failed\n"); - goto fail2; - } - - /* Init TX ring. */ - if (smsc_tx_list_init(sc)) { - aprint_error_dev(sc->sc_dev, "tx list init failed\n"); - goto fail3; - } - - mutex_enter(&sc->sc_rxlock); - mutex_enter(&sc->sc_txlock); - sc->sc_stopping = false; - - /* Start up the receive pipe. */ - for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) { - struct smsc_chain * const c = &sc->sc_cdata.rx_chain[i]; - usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, sc->sc_bufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, smsc_rxeof); - usbd_transfer(c->sc_xfer); - } - - mutex_exit(&sc->sc_txlock); - mutex_exit(&sc->sc_rxlock); - - /* Indicate we are up and running. */ - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); - - return 0; - -fail3: - smsc_rx_list_free(sc); -fail2: - usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); -fail1: - usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); -fail: - return EIO; -} - -void -smsc_start(struct ifnet *ifp) -{ - struct smsc_softc * const sc = ifp->if_softc; - KASSERT(ifp->if_extflags & IFEF_MPSAFE); - - mutex_enter(&sc->sc_txlock); - if (!sc->sc_stopping) - smsc_start_locked(ifp); - mutex_exit(&sc->sc_txlock); -} - -void -smsc_start_locked(struct ifnet *ifp) -{ - struct smsc_softc * const sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - KASSERT(mutex_owned(&sc->sc_txlock)); - - /* Don't send anything if there is no link or controller is busy. */ - if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { - smsc_dbg_printf(sc, "%s: no link\n", __func__); - return; - } - - /* Any free USB transfers? */ - if (sc->sc_cdata.tx_free == 0) { - smsc_dbg_printf(sc, "%s: all USB transfers in use\n", __func__); - return; - } - - if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) { - smsc_dbg_printf(sc, "%s: not running\n", __func__); - return; - } - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - sc->sc_cdata.tx_free--; - - IFQ_DEQUEUE(&ifp->if_snd, m_head); - if (smsc_encap(sc, m_head, sc->sc_cdata.tx_next)) { - m_free(m_head); - sc->sc_cdata.tx_free++; - return; - } - - sc->sc_cdata.tx_next = (sc->sc_cdata.tx_next + 1) % SMSC_TX_LIST_CNT; - - bpf_mtap(ifp, m_head, BPF_D_OUT); - - if (sc->sc_cdata.tx_free == 0) - ifp->if_flags |= IFF_OACTIVE; - - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 5; -} + smsc_setoe_locked(un); -void -smsc_tick(void *xsc) -{ - struct smsc_softc * const sc = xsc; - - if (sc == NULL) - return; - - mutex_enter(&sc->sc_lock); - - if (sc->sc_dying) { - mutex_exit(&sc->sc_lock); - return; - } - - if (!sc->sc_ttpending) { - sc->sc_ttpending = true; - usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER); - } + usbnet_unlock_mii_un_locked(un); - mutex_exit(&sc->sc_lock); + return usbnet_init_rx_tx(un); } void -smsc_stop(struct ifnet *ifp, int disable) +smsc_stop_cb(struct ifnet *ifp, int disable) { - struct smsc_softc * const sc = ifp->if_softc; - - mutex_enter(&sc->sc_lock); - smsc_stop_locked(ifp, disable); - mutex_exit(&sc->sc_lock); -} - -void -smsc_stop_locked(struct ifnet *ifp, int disable) -{ - struct smsc_softc * const sc = ifp->if_softc; - usbd_status err; - - KASSERT(mutex_owned(&sc->sc_lock)); - mutex_enter(&sc->sc_rxlock); - mutex_enter(&sc->sc_txlock); - sc->sc_stopping = true; - mutex_exit(&sc->sc_txlock); - mutex_exit(&sc->sc_rxlock); - - callout_stop(&sc->sc_stat_ch); - - /* Stop transfers. */ - if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); - if (err) { - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); - if (err) { - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - smsc_rx_list_free(sc); - - smsc_tx_list_free(sc); - - /* Close pipes */ - if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[SMSC_ENDPT_RX] = NULL; - } - - if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[SMSC_ENDPT_TX] = NULL; - } - - if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[SMSC_ENDPT_INTR] = NULL; - } - - ifp->if_timer = 0; - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + struct usbnet * const un = ifp->if_softc; + struct smsc_softc * const sc = usbnet_softc(un); - if (disable) { - /* drain */ - } + // XXXNH didn't do this before + smsc_reset(sc); } int -smsc_chip_init(struct smsc_softc *sc) +smsc_chip_init(struct usbnet *un) { - int err; + struct smsc_softc * const sc = usbnet_softc(un); uint32_t reg_val; int burst_cap; + int err; + + usbnet_lock_mii_un_locked(un); /* Enter H/W config mode */ - smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST); + smsc_writereg(un, SMSC_HW_CFG, SMSC_HW_CFG_LRST); - if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, + if ((err = smsc_wait_for_bits(un, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) { - smsc_warn_printf(sc, "timed-out waiting for reset to " + smsc_warn_printf(un, "timed-out waiting for reset to " "complete\n"); goto init_failed; } /* Reset the PHY */ - smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); + smsc_writereg(un, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); - if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, + if ((err = smsc_wait_for_bits(un, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST)) != 0) { - smsc_warn_printf(sc, "timed-out waiting for phy reset to " + smsc_warn_printf(un, "timed-out waiting for phy reset to " "complete\n"); goto init_failed; } - usbd_delay_ms(sc->sc_udev, 40); + usbd_delay_ms(un->un_udev, 40); /* Set the mac address */ - struct ifnet * const ifp = &sc->sc_ec.ec_if; + struct ifnet * const ifp = usbnet_ifp(un); const char *eaddr = CLLADDR(ifp->if_sadl); - if ((err = smsc_setmacaddress(sc, eaddr)) != 0) { - smsc_warn_printf(sc, "failed to set the MAC address\n"); + if ((err = smsc_setmacaddress(un, eaddr)) != 0) { + smsc_warn_printf(un, "failed to set the MAC address\n"); goto init_failed; } @@ -896,12 +665,12 @@ smsc_chip_init(struct smsc_softc *sc) * Don't know what the HW_CFG_BIR bit is, but following the reset * sequence as used in the Linux driver. */ - if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) { - smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err); + if ((err = smsc_readreg(un, SMSC_HW_CFG, ®_val)) != 0) { + smsc_warn_printf(un, "failed to read HW_CFG: %d\n", err); goto init_failed; } reg_val |= SMSC_HW_CFG_BIR; - smsc_write_reg(sc, SMSC_HW_CFG, reg_val); + smsc_writereg(un, SMSC_HW_CFG, reg_val); /* * There is a so called 'turbo mode' that the linux driver supports, it @@ -918,21 +687,21 @@ smsc_chip_init(struct smsc_softc *sc) * data/ethernet frames. */ - if (sc->sc_udev->ud_speed == USB_SPEED_HIGH) + if (un->un_udev->ud_speed == USB_SPEED_HIGH) burst_cap = 37; else burst_cap = 128; - smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap); + smsc_writereg(un, SMSC_BURST_CAP, burst_cap); /* Set the default bulk in delay (magic value from Linux driver) */ - smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000); + smsc_writereg(un, SMSC_BULK_IN_DLY, 0x00002000); /* * Initialise the RX interface */ - if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) { - smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", + if ((err = smsc_readreg(un, SMSC_HW_CFG, ®_val)) < 0) { + smsc_warn_printf(un, "failed to read HW_CFG: (err = %d)\n", err); goto init_failed; } @@ -949,32 +718,32 @@ smsc_chip_init(struct smsc_softc *sc) */ reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT; - smsc_write_reg(sc, SMSC_HW_CFG, reg_val); + smsc_writereg(un, SMSC_HW_CFG, reg_val); /* Clear the status register ? */ - smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff); + smsc_writereg(un, SMSC_INTR_STATUS, 0xffffffff); /* Read and display the revision register */ - if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { - smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err); + if ((err = smsc_readreg(un, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { + smsc_warn_printf(un, "failed to read ID_REV (err = %d)\n", err); goto init_failed; } /* GPIO/LED setup */ reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED | SMSC_LED_GPIO_CFG_FDX_LED; - smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val); + smsc_writereg(un, SMSC_LED_GPIO_CFG, reg_val); /* * Initialise the TX interface */ - smsc_write_reg(sc, SMSC_FLOW, 0); + smsc_writereg(un, SMSC_FLOW, 0); - smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT); + smsc_writereg(un, SMSC_AFC_CFG, AFC_CFG_DEFAULT); /* Read the current MAC configuration */ - if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { - smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err); + if ((err = smsc_readreg(un, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { + smsc_warn_printf(un, "failed to read MAC_CSR (err=%d)\n", err); goto init_failed; } @@ -982,90 +751,52 @@ smsc_chip_init(struct smsc_softc *sc) sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR; /* Vlan */ - smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); + smsc_writereg(un, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); /* * Start TX */ sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN; - smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); - smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON); + smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr); + smsc_writereg(un, SMSC_TX_CFG, SMSC_TX_CFG_ON); /* * Start RX */ sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN; - smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); + smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr); + usbnet_unlock_mii_un_locked(un); return 0; init_failed: - smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err); + usbnet_unlock_mii_un_locked(un); + smsc_err_printf(un, "smsc_chip_init failed (err=%d)\n", err); return err; } static int -smsc_ifflags_cb(struct ethercom *ec) +smsc_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct ifnet *ifp = &ec->ec_if; - struct smsc_softc *sc = ifp->if_softc; - - mutex_enter(&sc->sc_lock); - - const int change = ifp->if_flags ^ sc->sc_if_flags; - if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) { - mutex_exit(&sc->sc_lock); - return ENETRESET; - } - - smsc_dbg_printf(sc, "%s: change %x\n", __func__, change); + struct usbnet * const un = ifp->if_softc; - if ((change & IFF_PROMISC) != 0) { - if (ifp->if_flags & IFF_PROMISC) { - sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS; - smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); - } else if (!(ifp->if_flags & IFF_PROMISC)) { - sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS; - smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); - } - smsc_setmulti(sc); + switch (cmd) { + case SIOCSIFFLAGS: + case SIOCSETHERCAP: + case SIOCADDMULTI: + case SIOCDELMULTI: + smsc_setiff(un); + break; + case SIOCSIFCAP: + smsc_setoe(un); + break; + default: + break; } - mutex_exit(&sc->sc_lock); - return 0; } - -int -smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data) -{ - struct smsc_softc * const sc = ifp->if_softc; - - smsc_dbg_printf(sc, "%s: cmd %0lx data %p\n", __func__, cmd, data); - - int error = ether_ioctl(ifp, cmd, data); - - if (error == ENETRESET) { - error = 0; - if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) { - if (ifp->if_flags & IFF_RUNNING) { - mutex_enter(&sc->sc_lock); - smsc_setmulti(sc); - mutex_exit(&sc->sc_lock); - } - } - } - - mutex_enter(&sc->sc_rxlock); - mutex_enter(&sc->sc_txlock); - sc->sc_if_flags = ifp->if_flags; - mutex_exit(&sc->sc_txlock); - mutex_exit(&sc->sc_rxlock); - - return error; -} - int smsc_match(device_t parent, cfdata_t match, void *aux) { @@ -1078,27 +809,33 @@ smsc_match(device_t parent, cfdata_t mat void smsc_attach(device_t parent, device_t self, void *aux) { - struct smsc_softc *sc = device_private(self); + USBNET_MII_DECL_DEFAULT(unm); + struct smsc_softc * const sc = device_private(self); + struct usbnet * const un = &sc->smsc_un; struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; char *devinfop; - struct mii_data *mii; - struct ifnet *ifp; + unsigned bufsz; int err, i; uint32_t mac_h, mac_l; - sc->sc_dev = self; - sc->sc_udev = dev; - sc->sc_dying = false; - sc->sc_stopping = false; - sc->sc_ttpending = false; + KASSERT((void *)sc == un); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &smsc_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = SMSC_RX_LIST_CNT; + un->un_tx_list_cnt = SMSC_TX_LIST_CNT; + + devinfop = usbd_devinfo_alloc(un->un_udev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); @@ -1110,76 +847,68 @@ smsc_attach(device_t parent, device_t se } /* Setup the endpoints for the SMSC LAN95xx device(s) */ - err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface); + err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - id = usbd_get_interface_descriptor(sc->sc_iface); + id = usbd_get_interface_descriptor(un->un_iface); - if (sc->sc_udev->ud_speed >= USB_SPEED_HIGH) - sc->sc_bufsz = SMSC_MAX_BUFSZ; - else - sc->sc_bufsz = SMSC_MIN_BUFSZ; + if (dev->ud_speed >= USB_SPEED_HIGH) { + bufsz = SMSC_MAX_BUFSZ; + } else { + bufsz = SMSC_MIN_BUFSZ; + } + un->un_rx_bufsz = bufsz; + un->un_tx_bufsz = bufsz; /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (!ed) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; +#if 0 /* not used yet */ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; +#endif } } - usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, USB_TASKQ_MPSAFE); - - mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); - mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->sc_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE); - cv_init(&sc->sc_detachcv, "smsc_det"); - - ifp = &sc->sc_ec.ec_if; - ifp->if_softc = sc; - strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_extflags = IFEF_MPSAFE; - ifp->if_init = smsc_init; - ifp->if_ioctl = smsc_ioctl; - ifp->if_start = smsc_start; - ifp->if_stop = smsc_stop; + usbnet_attach(un, "smscdet"); #ifdef notyet /* * We can do TCPv4, and UDPv4 checksums in hardware. */ + struct ifnet *ifp = usbnet_ifp(un); + ifp->if_capabilities |= /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx | /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx; #endif - - sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU; + struct ethercom *ec = usbnet_ec(un); + ec->ec_capabilities = ETHERCAP_VLAN_MTU; /* Setup some of the basics */ - sc->sc_phyno = 1; + un->un_phyno = 1; + usbnet_lock_mii(un); /* * Attempt to get the mac address, if an EEPROM is not attached this * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC * address based on urandom. */ - memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN); + memset(un->un_eaddr, 0xff, ETHER_ADDR_LEN); prop_dictionary_t dict = device_properties(self); prop_data_t eaprop = prop_dictionary_get(dict, "mac-address"); @@ -1187,249 +916,44 @@ smsc_attach(device_t parent, device_t se if (eaprop != NULL) { KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA); KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN); - memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop), + memcpy(un->un_eaddr, prop_data_data_nocopy(eaprop), ETHER_ADDR_LEN); } else { /* Check if there is already a MAC address in the register */ - if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) && - (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) { - sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); - sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff); - sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); - sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); - sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); - sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff); + if ((smsc_readreg(un, SMSC_MAC_ADDRL, &mac_l) == 0) && + (smsc_readreg(un, SMSC_MAC_ADDRH, &mac_h) == 0)) { + un->un_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); + un->un_eaddr[4] = (uint8_t)((mac_h) & 0xff); + un->un_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); + un->un_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); + un->un_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); + un->un_eaddr[0] = (uint8_t)((mac_l) & 0xff); } } + usbnet_unlock_mii(un); - aprint_normal_dev(self, "Ethernet address %s\n", - ether_sprintf(sc->sc_enaddr)); - - IFQ_SET_READY(&ifp->if_snd); - - /* Initialize MII/media info. */ - mii = &sc->sc_mii; - mii->mii_ifp = ifp; - mii->mii_readreg = smsc_miibus_readreg; - mii->mii_writereg = smsc_miibus_writereg; - mii->mii_statchg = smsc_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - sc->sc_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts); - mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); - - if (LIST_FIRST(&mii->mii_phys) == NULL) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - - callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE); - - if_initialize(ifp); - sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if); - ether_ifattach(ifp, sc->sc_enaddr); - ether_set_ifflags_cb(&sc->sc_ec, smsc_ifflags_cb); - if_register(ifp); - - rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); -} - -int -smsc_detach(device_t self, int flags) -{ - struct smsc_softc *sc = device_private(self); - struct ifnet *ifp = &sc->sc_ec.ec_if; - - mutex_enter(&sc->sc_lock); - sc->sc_dying = true; - mutex_exit(&sc->sc_lock); - - callout_halt(&sc->sc_stat_ch, NULL); - - if (ifp->if_flags & IFF_RUNNING) - smsc_stop_locked(ifp, 1); - - /* - * Remove any pending tasks. They cannot be executing because they run - * in the same thread as detach. - */ - usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER, - NULL); - - mutex_enter(&sc->sc_lock); - sc->sc_refcnt--; - while (sc->sc_refcnt > 0) { - /* Wait for processes to go away */ - cv_wait(&sc->sc_detachcv, &sc->sc_lock); - } - -#ifdef DIAGNOSTIC - if (sc->sc_ep[SMSC_ENDPT_TX] != NULL || - sc->sc_ep[SMSC_ENDPT_RX] != NULL || - sc->sc_ep[SMSC_ENDPT_INTR] != NULL) - printf("%s: detach has active endpoints\n", - device_xname(sc->sc_dev)); -#endif - - mutex_exit(&sc->sc_lock); - - rnd_detach_source(&sc->sc_rnd_source); - mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); - if (ifp->if_softc != NULL) { - ether_ifdetach(ifp); - if_detach(ifp); - } - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); - - cv_destroy(&sc->sc_detachcv); - mutex_destroy(&sc->sc_mii_lock); - mutex_destroy(&sc->sc_rxlock); - mutex_destroy(&sc->sc_txlock); - mutex_destroy(&sc->sc_lock); - - return 0; + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, &unm); } void -smsc_tick_task(void *xsc) +smsc_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len) { - struct smsc_softc * const sc = xsc; - - if (sc == NULL) - return; - - mutex_enter(&sc->sc_lock); - - if (sc->sc_dying) { - mutex_exit(&sc->sc_lock); - return; - } - - struct ifnet * const ifp = &sc->sc_ec.ec_if; - struct mii_data * const mii = &sc->sc_mii; - - sc->sc_refcnt++; - mutex_exit(&sc->sc_lock); - - mii_tick(mii); - if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) - smsc_miibus_statchg(ifp); - - mutex_enter(&sc->sc_lock); - sc->sc_ttpending = false; - - if (--sc->sc_refcnt < 0) - cv_broadcast(&sc->sc_detachcv); + USMSCHIST_FUNC(); USMSCHIST_CALLED(); + struct smsc_softc * const sc = usbnet_softc(un); + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; - if (sc->sc_dying) { - mutex_exit(&sc->sc_lock); - return; - } - callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); - - mutex_exit(&sc->sc_lock); -} - -int -smsc_activate(device_t self, enum devact act) -{ - struct smsc_softc *sc = device_private(self); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(&sc->sc_ec.ec_if); - - mutex_enter(&sc->sc_lock); - sc->sc_dying = true; - - mutex_enter(&sc->sc_rxlock); - mutex_enter(&sc->sc_txlock); - sc->sc_stopping = true; - mutex_exit(&sc->sc_txlock); - mutex_exit(&sc->sc_rxlock); - - mutex_exit(&sc->sc_lock); - return 0; - default: - return EOPNOTSUPP; - } - return 0; -} - -void -smsc_lock_mii(struct smsc_softc *sc) -{ - - mutex_enter(&sc->sc_lock); - sc->sc_refcnt++; - mutex_exit(&sc->sc_lock); - - mutex_enter(&sc->sc_mii_lock); -} - -void -smsc_unlock_mii(struct smsc_softc *sc) -{ - - mutex_exit(&sc->sc_mii_lock); - mutex_enter(&sc->sc_lock); - if (--sc->sc_refcnt < 0) - cv_broadcast(&sc->sc_detachcv); - mutex_exit(&sc->sc_lock); -} - -void -smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) -{ - struct smsc_chain * const c = (struct smsc_chain *)priv; - struct smsc_softc * const sc = c->sc_sc; - struct ifnet * const ifp = &sc->sc_ec.ec_if; - u_char *buf = c->sc_buf; - uint32_t total_len; - - mutex_enter(&sc->sc_rxlock); - if (sc->sc_stopping) { - smsc_dbg_printf(sc, "%s: stopping\n", __func__); - mutex_exit(&sc->sc_rxlock); - return; - } - - if (!(sc->sc_if_flags & IFF_RUNNING)) { - smsc_dbg_printf(sc, "%s: not running\n", __func__); - mutex_exit(&sc->sc_rxlock); - return; - } - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - mutex_exit(&sc->sc_rxlock); - return; - } - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: usb errors on rx: %s\n", - device_xname(sc->sc_dev), usbd_errstr(status)); - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len); + usbnet_isowned_rx(un); + DPRINTF("total_len %jd/0x%jx", total_len, total_len, 0, 0); while (total_len != 0) { uint32_t rxhdr; if (total_len < sizeof(rxhdr)) { - smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n", - total_len, sizeof(rxhdr)); + DPRINTF("total_len %jd < sizeof(rxhdr) %jd", + total_len, sizeof(rxhdr), 0, 0); ifp->if_ierrors++; - goto done; + return; } memcpy(&rxhdr, buf, sizeof(rxhdr)); @@ -1443,62 +967,54 @@ smsc_rxeof(struct usbd_xfer *xfer, void if (rxhdr & (SMSC_RX_STAT_ERROR | SMSC_RX_STAT_LENGTH_ERROR | SMSC_RX_STAT_MII_ERROR)) { - smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr); + DPRINTF("rx error (hdr 0x%08jx)", rxhdr, 0, 0, 0); ifp->if_ierrors++; - goto done; + return; } uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr); - smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr " - "0x%08x\n", total_len, pktlen, rxhdr); + DPRINTF("total_len %jd pktlen %jd rxhdr 0x%08jx", total_len, + pktlen, rxhdr, 0); if (pktlen < ETHER_HDR_LEN) { - smsc_dbg_printf(sc, "pktlen %d < ETHER_HDR_LEN %d\n", - pktlen, ETHER_HDR_LEN); + DPRINTF("pktlen %jd < ETHER_HDR_LEN %jd", pktlen, + ETHER_HDR_LEN, 0, 0); ifp->if_ierrors++; - goto done; + return; } pktlen += ETHER_ALIGN; if (pktlen > MCLBYTES) { - smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n", - pktlen, MCLBYTES); + DPRINTF("pktlen %jd > MCLBYTES %jd", pktlen, MCLBYTES, 0, + 0); ifp->if_ierrors++; - goto done; + return; } if (pktlen > total_len) { - smsc_dbg_printf(sc, "pktlen %d > total_len %d\n", - pktlen, total_len); - ifp->if_ierrors++; - goto done; - } - - struct mbuf *m = smsc_newbuf(); - if (m == NULL) { - smsc_dbg_printf(sc, "smc_newbuf returned NULL\n"); + DPRINTF("pktlen %jd > total_len %jd", pktlen, total_len, + 0, 0); ifp->if_ierrors++; - goto done; + return; } - m_set_rcvif(m, ifp); - m->m_pkthdr.len = m->m_len = pktlen; - m->m_flags |= M_HASFCS; - m_adj(m, ETHER_ALIGN); + uint8_t *pktbuf = buf + ETHER_ALIGN; + size_t buflen = pktlen - ETHER_ALIGN; + int mbuf_flags = M_HASFCS; + int csum_flags = 0; + uint16_t csum_data = 0; - KASSERT(m->m_len < MCLBYTES); - memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len); + KASSERT(pktlen < MCLBYTES); /* Check if RX TCP/UDP checksumming is being offloaded */ if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) { - smsc_dbg_printf(sc,"RX checksum offload checking\n"); - struct ether_header *eh; - - eh = mtod(m, struct ether_header *); + DPRINTF("RX checksum offload checking", 0, 0, 0, 0); + struct ether_header *eh = (struct ether_header *)pktbuf; + const size_t cssz = sizeof(csum_data); /* Remove the extra 2 bytes of the csum */ - m_adj(m, -2); + buflen -= cssz; /* * The checksum appears to be simplistically calculated @@ -1511,12 +1027,12 @@ smsc_rxeof(struct usbd_xfer *xfer, void * * Ignore H/W csum for non-IPv4 packets. */ - smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n", - be16toh(eh->ether_type), pktlen); + DPRINTF("Ethertype %02jx pktlen %02jx", + be16toh(eh->ether_type), pktlen, 0, 0); if (be16toh(eh->ether_type) == ETHERTYPE_IP && pktlen > ETHER_MIN_LEN) { - m->m_pkthdr.csum_flags |= + csum_flags |= (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA); /* @@ -1524,18 +1040,16 @@ smsc_rxeof(struct usbd_xfer *xfer, void * bytes of the transfer and put in the * csum_data field. */ - memcpy(&m->m_pkthdr.csum_data, - buf + pktlen - 2, 2); + memcpy(&csum_data, buf + pktlen - cssz, cssz); + /* * The data is copied in network order, but the * csum algorithm in the kernel expects it to be * in host network order. */ - m->m_pkthdr.csum_data = - ntohs(m->m_pkthdr.csum_data); - smsc_dbg_printf(sc, - "RX checksum offloaded (0x%04x)\n", - m->m_pkthdr.csum_data); + csum_data = ntohs(csum_data); + DPRINTF("RX checksum offloaded (0x%04jx)", + csum_data, 0, 0, 0); } } @@ -1549,180 +1063,24 @@ smsc_rxeof(struct usbd_xfer *xfer, void buf += pktlen; total_len -= pktlen; - mutex_exit(&sc->sc_rxlock); - /* push the packet up */ - if_percpuq_enqueue(sc->sc_ipq, m); - - mutex_enter(&sc->sc_rxlock); - if (sc->sc_stopping) { - smsc_dbg_printf(sc, "%s: stopping\n", __func__); - mutex_exit(&sc->sc_rxlock); - return; - } - } - -done: - mutex_exit(&sc->sc_rxlock); - - /* Setup new transfer. */ - usbd_setup_xfer(xfer, c, c->sc_buf, sc->sc_bufsz, USBD_SHORT_XFER_OK, - USBD_NO_TIMEOUT, smsc_rxeof); - usbd_transfer(xfer); - - return; -} - -void -smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) -{ - struct smsc_chain *c = priv; - struct smsc_softc *sc = c->sc_sc; - struct ifnet *ifp = &sc->sc_ec.ec_if; - - mutex_enter(&sc->sc_txlock); - if (sc->sc_stopping) { - smsc_dbg_printf(sc, "%s: stopping\n", __func__); - mutex_exit(&sc->sc_txlock); - return; - } - - sc->sc_cdata.tx_free++; - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - mutex_exit(&sc->sc_txlock); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]); - mutex_exit(&sc->sc_txlock); - return; - } - ifp->if_opackets++; - - m_freem(c->sc_mbuf); - c->sc_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - smsc_start_locked(ifp); - - mutex_exit(&sc->sc_txlock); -} - -int -smsc_tx_list_init(struct smsc_softc *sc) -{ - struct smsc_cdata *cd = &sc->sc_cdata; - struct smsc_chain *c; - int i; - - for (i = 0; i < SMSC_TX_LIST_CNT; i++) { - c = &cd->tx_chain[i]; - c->sc_sc = sc; - c->sc_idx = i; - c->sc_mbuf = NULL; - if (c->sc_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_TX], - sc->sc_bufsz, USBD_FORCE_SHORT_XFER, 0, - &c->sc_xfer); - if (error) - return EIO; - c->sc_buf = usbd_get_buffer(c->sc_xfer); - } - } - - cd->tx_free = SMSC_TX_LIST_CNT; - cd->tx_next = 0; - - return 0; -} - -void -smsc_tx_list_free(struct smsc_softc *sc) -{ - /* Free TX resources. */ - for (size_t i = 0; i < SMSC_TX_LIST_CNT; i++) { - if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) { - m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf); - sc->sc_cdata.tx_chain[i].sc_mbuf = NULL; - } - if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.tx_chain[i].sc_xfer); - sc->sc_cdata.tx_chain[i].sc_xfer = NULL; - } - } -} - -int -smsc_rx_list_init(struct smsc_softc *sc) -{ - struct smsc_cdata *cd = &sc->sc_cdata; - struct smsc_chain *c; - int i; - - for (i = 0; i < SMSC_RX_LIST_CNT; i++) { - c = &cd->rx_chain[i]; - c->sc_sc = sc; - c->sc_idx = i; - c->sc_mbuf = NULL; - if (c->sc_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_RX], - sc->sc_bufsz, USBD_SHORT_XFER_OK, 0, &c->sc_xfer); - if (error) - return error; - c->sc_buf = usbd_get_buffer(c->sc_xfer); - } + usbnet_enqueue(un, pktbuf, buflen, csum_flags, csum_data, + mbuf_flags); } - - return 0; } -void -smsc_rx_list_free(struct smsc_softc *sc) +static unsigned +smsc_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - /* Free RX resources. */ - for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) { - if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) { - m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf); - sc->sc_cdata.rx_chain[i].sc_mbuf = NULL; - } - if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.rx_chain[i].sc_xfer); - sc->sc_cdata.rx_chain[i].sc_xfer = NULL; - } - } -} - -struct mbuf * -smsc_newbuf(void) -{ - struct mbuf *m; - - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (m == NULL) - return NULL; + uint32_t txhdr; + uint32_t frm_len = 0; - MCLGET(m, M_DONTWAIT); - if (!(m->m_flags & M_EXT)) { - m_freem(m); - return NULL; - } + usbnet_isowned_tx(un); - return m; -} + const size_t hdrsz = sizeof(txhdr) * 2; -int -smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx) -{ - struct smsc_chain * const c = &sc->sc_cdata.tx_chain[idx]; - uint32_t txhdr; - uint32_t frm_len = 0; + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - hdrsz) + return 0; /* * Each frame is prefixed with two 32-bit values describing the @@ -1731,27 +1089,23 @@ smsc_encap(struct smsc_softc *sc, struct txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) | SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG; txhdr = htole32(txhdr); - memcpy(c->sc_buf, &txhdr, sizeof(txhdr)); + memcpy(c->unc_buf, &txhdr, sizeof(txhdr)); txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len); txhdr = htole32(txhdr); - memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr)); + memcpy(c->unc_buf + sizeof(txhdr), &txhdr, sizeof(txhdr)); - frm_len += 8; + frm_len += hdrsz; /* Next copy in the actual packet */ - m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len); + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + frm_len); frm_len += m->m_pkthdr.len; - c->sc_mbuf = m; - - usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, frm_len, - USBD_FORCE_SHORT_XFER, 10000, smsc_txeof); + return frm_len; +} - usbd_status err = usbd_transfer(c->sc_xfer); - if (err != USBD_IN_PROGRESS) { - return EIO; - } +#ifdef _MODULE +#include "ioconf.c" +#endif - return 0; -} +USBNET_MODULE(smsc) Index: usb/if_smscreg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_smscreg.h,v retrieving revision 1.5 diff -p -u -u -r1.5 if_smscreg.h --- usb/if_smscreg.h 21 Aug 2014 14:02:10 -0000 1.5 +++ usb/if_smscreg.h 29 Aug 2019 06:22:05 -0000 @@ -52,8 +52,8 @@ * * Where the headers have the following fields: * - * TX_CTRL_0 <31:18> Reserved - * TX_CTRL_0 <17:16> Data offset (alignment padding 0-3) + * TX_CTRL_0 <31:21> Reserved + * TX_CTRL_0 <20:16> Data offset (alignment padding 0-3) * TX_CTRL_0 <13> First segment of frame indicator * TX_CTRL_0 <12> Last segment of frame indicator * TX_CTRL_0 <10:0> Buffer size (payload size) @@ -64,7 +64,7 @@ * TX_CTRL_1 <10:0> Packet byte length * */ -#define SMSC_TX_CTRL_0_OFFSET(x) (((x) & 0x3UL) << 16) +#define SMSC_TX_CTRL_0_OFFSET(x) (((x) & 0x1FUL) << 16) #define SMSC_TX_CTRL_0_FIRST_SEG (0x1UL << 13) #define SMSC_TX_CTRL_0_LAST_SEG (0x1UL << 12) #define SMSC_TX_CTRL_0_BUF_SIZE(x) ((x) & 0x000007FFUL) Index: usb/if_smscvar.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/Attic/if_smscvar.h,v retrieving revision 1.7 diff -p -u -u -r1.7 if_smscvar.h --- usb/if_smscvar.h 30 Jul 2019 06:35:03 -0000 1.7 +++ usb/if_smscvar.h 29 Aug 2019 06:22:05 -0000 @@ -1,106 +0,0 @@ -/* $NetBSD: if_smscvar.h,v 1.7 2019/07/30 06:35:03 skrll Exp $ */ - -/* $OpenBSD: if_smscreg.h,v 1.2 2012/09/27 12:38:11 jsg Exp $ */ -/*- - * Copyright (c) 2012 - * Ben Gray . - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD: src/sys/dev/usb/net/if_smscreg.h,v 1.1 2012/08/15 04:03:55 gonzo Exp $ - */ -#ifndef _IF_SMSCVAR_H_ -#define _IF_SMSCVAR_H_ - -#include - -struct smsc_chain { - struct smsc_softc *sc_sc; - struct usbd_xfer *sc_xfer; - char *sc_buf; - struct mbuf *sc_mbuf; - int sc_accum; - int sc_idx; -}; - -struct smsc_cdata { - struct smsc_chain tx_chain[SMSC_TX_LIST_CNT]; - struct smsc_chain rx_chain[SMSC_RX_LIST_CNT]; - int tx_free; - int tx_next; - int rx_next; -}; - -struct smsc_softc { - device_t sc_dev; - struct usbd_device * sc_udev; - bool sc_dying; - bool sc_stopping; - bool sc_ttpending; - - uint8_t sc_enaddr[ETHER_ADDR_LEN]; - struct ethercom sc_ec; - struct mii_data sc_mii; - krndsource_t sc_rnd_source; - int sc_phyno; - struct usbd_interface * sc_iface; - - /* - * The following stores the settings in the mac control (MAC_CSR) - * register - */ - uint32_t sc_mac_csr; - uint32_t sc_rev_id; - - uint32_t sc_coe_ctrl; - - int sc_if_flags; - int sc_refcnt; - - struct usb_task sc_tick_task; - - int sc_ed[SMSC_ENDPT_MAX]; - struct usbd_pipe * sc_ep[SMSC_ENDPT_MAX]; - - kmutex_t sc_lock; - kmutex_t sc_txlock; - kmutex_t sc_rxlock; - kmutex_t sc_mii_lock; - kcondvar_t sc_detachcv; - - struct smsc_cdata sc_cdata; - callout_t sc_stat_ch; - - struct timeval sc_rx_notice; - u_int sc_bufsz; - - uint32_t sc_flags; -#define SMSC_FLAG_LINK 0x0001 - - struct if_percpuq *sc_ipq; /* softint-based input queues */ -}; - -#define SMSC_MIN_BUFSZ 2048 -#define SMSC_MAX_BUFSZ 18944 - -#endif /* _IF_SMSCVAR_H_ */ Index: usb/if_udav.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_udav.c,v retrieving revision 1.59 diff -p -u -u -r1.59 if_udav.c --- usb/if_udav.c 28 May 2019 07:41:50 -0000 1.59 +++ usb/if_udav.c 29 Aug 2019 06:22:05 -0000 @@ -48,86 +48,45 @@ __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.59 2019/05/28 07:41:50 msaitoh Exp $"); #ifdef _KERNEL_OPT -#include "opt_inet.h" #include "opt_usb.h" #endif #include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#ifdef INET -#include -#include -#endif - -#include -#include - -#include -#include -#include -#include +#include #include - /* Function declarations */ int udav_match(device_t, cfdata_t, void *); void udav_attach(device_t, device_t, void *); -int udav_detach(device_t, int); -int udav_activate(device_t, enum devact); -CFATTACH_DECL_NEW(udav, sizeof(struct udav_softc), udav_match, udav_attach, - udav_detach, udav_activate); +CFATTACH_DECL_NEW(udav, sizeof(struct usbnet), udav_match, udav_attach, + usbnet_detach, usbnet_activate); + +static void udav_chip_init(struct usbnet *); -Static int udav_openpipes(struct udav_softc *); -Static int udav_rx_list_init(struct udav_softc *); -Static int udav_tx_list_init(struct udav_softc *); -Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *); -Static void udav_start(struct ifnet *); -Static int udav_send(struct udav_softc *, struct mbuf *, int); -Static void udav_txeof(struct usbd_xfer *, void *, usbd_status); -Static void udav_rxeof(struct usbd_xfer *, void *, usbd_status); -Static void udav_tick(void *); -Static void udav_tick_task(void *); -Static int udav_ioctl(struct ifnet *, u_long, void *); -Static void udav_stop_task(struct udav_softc *); -Static void udav_stop(struct ifnet *, int); -Static void udav_watchdog(struct ifnet *); -Static int udav_ifmedia_change(struct ifnet *); -Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *); -Static void udav_lock_mii(struct udav_softc *); -Static void udav_unlock_mii(struct udav_softc *); -Static int udav_miibus_readreg(device_t, int, int, uint16_t *); -Static int udav_miibus_writereg(device_t, int, int, uint16_t); -Static void udav_miibus_statchg(struct ifnet *); -Static int udav_init(struct ifnet *); -Static void udav_setmulti(struct udav_softc *); -Static void udav_reset(struct udav_softc *); - -Static int udav_csr_read(struct udav_softc *, int, void *, int); -Static int udav_csr_write(struct udav_softc *, int, void *, int); -Static int udav_csr_read1(struct udav_softc *, int); -Static int udav_csr_write1(struct udav_softc *, int, unsigned char); +static unsigned udav_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void udav_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static void udav_stop_cb(struct ifnet *, int); +static int udav_ioctl_cb(struct ifnet *, u_long, void *); +static int udav_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static int udav_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void udav_mii_statchg(struct ifnet *); +static int udav_init(struct ifnet *); +static void udav_setiff(struct usbnet *); +static void udav_setiff_locked(struct usbnet *); +static void udav_reset(struct usbnet *); + +static int udav_csr_read(struct usbnet *, int, void *, int); +static int udav_csr_write(struct usbnet *, int, void *, int); +static int udav_csr_read1(struct usbnet *, int); +static int udav_csr_write1(struct usbnet *, int, unsigned char); #if 0 -Static int udav_mem_read(struct udav_softc *, int, void *, int); -Static int udav_mem_write(struct udav_softc *, int, void *, int); -Static int udav_mem_write1(struct udav_softc *, int, unsigned char); +static int udav_mem_read(struct usbnet *, int, void *, int); +static int udav_mem_write(struct usbnet *, int, void *, int); +static int udav_mem_write1(struct usbnet *, int, unsigned char); #endif /* Macros */ @@ -140,11 +99,11 @@ int udavdebug = 0; #define DPRINTFN(n, x) #endif -#define UDAV_SETBIT(sc, reg, x) \ - udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x)) +#define UDAV_SETBIT(un, reg, x) \ + udav_csr_write1(un, reg, udav_csr_read1(un, reg) | (x)) -#define UDAV_CLRBIT(sc, reg, x) \ - udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x)) +#define UDAV_CLRBIT(un, reg, x) \ + udav_csr_write1(un, reg, udav_csr_read1(un, reg) & ~(x)) static const struct udav_type { struct usb_devno udav_dev; @@ -172,6 +131,16 @@ static const struct udav_type { }; #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p)) +static struct usbnet_ops udav_ops = { + .uno_stop = udav_stop_cb, + .uno_ioctl = udav_ioctl_cb, + .uno_read_reg = udav_mii_read_reg, + .uno_write_reg = udav_mii_write_reg, + .uno_statchg = udav_mii_statchg, + .uno_tx_prepare = udav_tx_prepare, + .uno_rx_loop = udav_rx_loop, + .uno_init = udav_init, +}; /* Probe */ int @@ -187,7 +156,9 @@ udav_match(device_t parent, cfdata_t mat void udav_attach(device_t parent, device_t self, void *aux) { - struct udav_softc *sc = device_private(self); + USBNET_MII_DECL_DEFAULT(unm); + struct usbnet_mii *unmp; + struct usbnet * const un = device_private(self); struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; struct usbd_interface *iface; @@ -195,220 +166,116 @@ udav_attach(device_t parent, device_t se usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; char *devinfop; - struct ifnet *ifp; - struct mii_data *mii; - u_char eaddr[ETHER_ADDR_LEN]; - int i, s; - - sc->sc_dev = self; + int i; aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = un; + un->un_ops = &udav_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = UDAV_RX_LIST_CNT; + un->un_tx_list_cnt = UDAV_TX_LIST_CNT; + un->un_rx_bufsz = UDAV_BUFSZ; + un->un_tx_bufsz = UDAV_BUFSZ; + /* Move the device into the configured state. */ err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */ if (err) { aprint_error_dev(self, "failed to set configuration" ", err=%s\n", usbd_errstr(err)); - goto bad; + return; } - usb_init_task(&sc->sc_tick_task, udav_tick_task, sc, 0); - mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE); - usb_init_task(&sc->sc_stop_task, (void (*)(void *))udav_stop_task, sc, - 0); - /* get control interface */ err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface); if (err) { aprint_error_dev(self, "failed to get interface, err=%s\n", usbd_errstr(err)); - goto bad; + return; } - sc->sc_udev = dev; - sc->sc_ctl_iface = iface; - sc->sc_flags = udav_lookup(uaa->uaa_vendor, + un->un_iface = iface; + un->un_flags = udav_lookup(uaa->uaa_vendor, uaa->uaa_product)->udav_flags; /* get interface descriptor */ - id = usbd_get_interface_descriptor(sc->sc_ctl_iface); + id = usbd_get_interface_descriptor(un->un_iface); /* find endpoints */ - sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1; + un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = + un->un_ed[USBNET_ENDPT_INTR] = -1; for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get endpoint %d\n", i); - goto bad; + return; } if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) - sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */ + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) - sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */ + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) - sc->sc_intrin_no = ed->bEndpointAddress; /* Status */ + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } - if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 || - sc->sc_intrin_no == -1) { + if (un->un_ed[USBNET_ENDPT_RX] == 0 || + un->un_ed[USBNET_ENDPT_TX] == 0 || + un->un_ed[USBNET_ENDPT_INTR] == 0) { aprint_error_dev(self, "missing endpoint\n"); - goto bad; + return; } - s = splnet(); + /* Not supported yet. */ + un->un_ed[USBNET_ENDPT_INTR] = 0; - /* reset the adapter */ - udav_reset(sc); +// /* reset the adapter */ +// udav_reset(un); + + usbnet_attach(un, "udavdet"); /* Get Ethernet Address */ - err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN); + usbnet_lock_mii(un); + err = udav_csr_read(un, UDAV_PAR, un->un_eaddr, ETHER_ADDR_LEN); + usbnet_unlock_mii(un); if (err) { aprint_error_dev(self, "read MAC address failed\n"); - splx(s); - goto bad; + return; } - /* Print Ethernet Address */ - aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); + if (ISSET(un->un_flags, UDAV_NO_PHY)) + unmp = NULL; + else + unmp = &unm; /* initialize interface information */ - ifp = GET_IFP(sc); - ifp->if_softc = sc; - ifp->if_mtu = ETHERMTU; - strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_start = udav_start; - ifp->if_ioctl = udav_ioctl; - ifp->if_watchdog = udav_watchdog; - ifp->if_init = udav_init; - ifp->if_stop = udav_stop; - - IFQ_SET_READY(&ifp->if_snd); - - if (ISSET(sc->sc_flags, UDAV_NO_PHY)) { - sc->sc_link = 1; - goto skipmii; - } - - /* - * Do ifmedia setup. - */ - mii = &sc->sc_mii; - mii->mii_ifp = ifp; - mii->mii_readreg = udav_miibus_readreg; - mii->mii_writereg = udav_miibus_writereg; - mii->mii_statchg = udav_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - sc->sc_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, - udav_ifmedia_change, udav_ifmedia_status); - mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); - if (LIST_FIRST(&mii->mii_phys) == NULL) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - -skipmii: - /* attach the interface */ - if_attach(ifp); - ether_ifattach(ifp, eaddr); - - rnd_attach_source(&sc->rnd_source, device_xname(self), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&sc->sc_stat_ch, 0); - sc->sc_attached = 1; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev); - - return; + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, unmp); - bad: - sc->sc_dying = 1; return; } -/* detach */ -int -udav_detach(device_t self, int flags) -{ - struct udav_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - int s; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - /* Detached before attached finished */ - if (!sc->sc_attached) - return 0; - - callout_halt(&sc->sc_stat_ch, NULL); - - /* Remove any pending tasks */ - usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER, - NULL); - usb_rem_task_wait(sc->sc_udev, &sc->sc_stop_task, USB_TASKQ_DRIVER, - NULL); - - s = splusb(); - - if (--sc->sc_refcnt >= 0) { - /* Wait for processes to go away */ - usb_detach_waitold(sc->sc_dev); - } - if (ifp->if_flags & IFF_RUNNING) - udav_stop(GET_IFP(sc), 1); - - rnd_detach_source(&sc->rnd_source); - mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); - ether_ifdetach(ifp); - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->sc_pipe_tx != NULL) - aprint_debug_dev(self, "detach has active tx endpoint.\n"); - if (sc->sc_pipe_rx != NULL) - aprint_debug_dev(self, "detach has active rx endpoint.\n"); - if (sc->sc_pipe_intr != NULL) - aprint_debug_dev(self, "detach has active intr endpoint.\n"); -#endif - sc->sc_attached = 0; - - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); - - mutex_destroy(&sc->sc_mii_lock); - - return 0; -} - #if 0 /* read memory */ -Static int -udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len) +static int +udav_mem_read(struct usbnet *un, int offset, void *buf, int len) { usb_device_request_t req; usbd_status err; - if (sc == NULL) - return 0; - DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) + if (usbnet_isdying(un)) return 0; offset &= 0xffff; @@ -420,32 +287,26 @@ udav_mem_read(struct udav_softc *sc, int USETW(req.wIndex, offset); USETW(req.wLength, len); - sc->sc_refcnt++; - err = usbd_do_request(sc->sc_udev, &req, buf); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + err = usbd_do_request(un->un_udev, &req, buf); if (err) { DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n", - device_xname(sc->sc_dev), __func__, offset, err)); + device_xname(un->un_dev), __func__, offset, err)); } return err; } /* write memory */ -Static int -udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len) +static int +udav_mem_write(struct usbnet *un, int offset, void *buf, int len) { usb_device_request_t req; usbd_status err; - if (sc == NULL) - return 0; - DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) + if (usbnet_isdying(un)) return 0; offset &= 0xffff; @@ -457,32 +318,26 @@ udav_mem_write(struct udav_softc *sc, in USETW(req.wIndex, offset); USETW(req.wLength, len); - sc->sc_refcnt++; - err = usbd_do_request(sc->sc_udev, &req, buf); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + err = usbd_do_request(un->un_udev, &req, buf); if (err) { DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", - device_xname(sc->sc_dev), __func__, offset, err)); + device_xname(un->un_dev), __func__, offset, err)); } return err; } /* write memory */ -Static int -udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch) +static int +udav_mem_write1(struct usbnet *un, int offset, unsigned char ch) { usb_device_request_t req; usbd_status err; - if (sc == NULL) - return 0; - DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) + if (usbnet_isdying(un)) return 0; offset &= 0xffff; @@ -493,13 +348,10 @@ udav_mem_write1(struct udav_softc *sc, i USETW(req.wIndex, offset); USETW(req.wLength, 0x0000); - sc->sc_refcnt++; - err = usbd_do_request(sc->sc_udev, &req, NULL); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + err = usbd_do_request(un->un_udev, &req, NULL); if (err) { DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", - device_xname(sc->sc_dev), __func__, offset, err)); + device_xname(un->un_dev), __func__, offset, err)); } return err; @@ -507,20 +359,17 @@ udav_mem_write1(struct udav_softc *sc, i #endif /* read register(s) */ -Static int -udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len) +static int +udav_csr_read(struct usbnet *un, int offset, void *buf, int len) { usb_device_request_t req; usbd_status err; - if (sc == NULL) - return 0; + usbnet_isowned_mii(un); + KASSERT(!usbnet_isdying(un)); DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); offset &= 0xff; len &= 0xff; @@ -531,33 +380,27 @@ udav_csr_read(struct udav_softc *sc, int USETW(req.wIndex, offset); USETW(req.wLength, len); - sc->sc_refcnt++; - err = usbd_do_request(sc->sc_udev, &req, buf); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + err = usbd_do_request(un->un_udev, &req, buf); if (err) { DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n", - device_xname(sc->sc_dev), __func__, offset, err)); + device_xname(un->un_dev), __func__, offset, err)); } return err; } /* write register(s) */ -Static int -udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len) +static int +udav_csr_write(struct usbnet *un, int offset, void *buf, int len) { usb_device_request_t req; usbd_status err; - if (sc == NULL) - return 0; + usbnet_isowned_mii(un); + KASSERT(!usbnet_isdying(un)); DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); offset &= 0xff; len &= 0xff; @@ -568,50 +411,43 @@ udav_csr_write(struct udav_softc *sc, in USETW(req.wIndex, offset); USETW(req.wLength, len); - sc->sc_refcnt++; - err = usbd_do_request(sc->sc_udev, &req, buf); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + err = usbd_do_request(un->un_udev, &req, buf); if (err) { DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", - device_xname(sc->sc_dev), __func__, offset, err)); + device_xname(un->un_dev), __func__, offset, err)); } return err; } -Static int -udav_csr_read1(struct udav_softc *sc, int offset) +static int +udav_csr_read1(struct usbnet *un, int offset) { uint8_t val = 0; - if (sc == NULL) - return 0; + usbnet_isowned_mii(un); DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) + if (usbnet_isdying(un)) return 0; - return udav_csr_read(sc, offset, &val, 1) ? 0 : val; + return udav_csr_read(un, offset, &val, 1) ? 0 : val; } /* write a register */ -Static int -udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch) +static int +udav_csr_write1(struct usbnet *un, int offset, unsigned char ch) { usb_device_request_t req; usbd_status err; - if (sc == NULL) - return 0; + usbnet_isowned_mii(un); + KASSERT(!usbnet_isdying(un)); DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); offset &= 0xff; @@ -621,117 +457,100 @@ udav_csr_write1(struct udav_softc *sc, i USETW(req.wIndex, offset); USETW(req.wLength, 0x0000); - sc->sc_refcnt++; - err = usbd_do_request(sc->sc_udev, &req, NULL); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + err = usbd_do_request(un->un_udev, &req, NULL); if (err) { DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", - device_xname(sc->sc_dev), __func__, offset, err)); + device_xname(un->un_dev), __func__, offset, err)); } return err; } -Static int +static int udav_init(struct ifnet *ifp) { - struct udav_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); + struct usbnet * const un = ifp->if_softc; + struct mii_data * const mii = usbnet_mii(un); uint8_t eaddr[ETHER_ADDR_LEN]; - int rc, s; + int rc = 0; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) - return EIO; + usbnet_lock(un); - s = splnet(); + if (usbnet_isdying(un)) { + usbnet_unlock(un); + return EIO; + } /* Cancel pending I/O and free all TX/RX buffers */ - udav_stop(ifp, 1); + if (ifp->if_flags & IFF_RUNNING) + usbnet_stop(un, ifp, 1); + + usbnet_lock_mii_un_locked(un); memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr)); - udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN); + udav_csr_write(un, UDAV_PAR, eaddr, ETHER_ADDR_LEN); /* Initialize network control register */ /* Disable loopback */ - UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1); + UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1); /* Initialize RX control register */ - UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC); + UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC); /* If we want promiscuous mode, accept all physical frames. */ if (ifp->if_flags & IFF_PROMISC) - UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); + UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); else - UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); + UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); /* Load the multicast filter */ - udav_setmulti(sc); + udav_setiff_locked(un); /* Enable RX */ - UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN); + UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_RXEN); /* clear POWER_DOWN state of internal PHY */ - UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0); - UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0); + UDAV_SETBIT(un, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0); + UDAV_CLRBIT(un, UDAV_GPR, UDAV_GPR_GEPIO0); + + usbnet_unlock_mii_un_locked(un); + usbnet_unlock(un); - if ((rc = mii_mediachg(mii)) == ENXIO) + if (mii && (rc = mii_mediachg(mii)) == ENXIO) rc = 0; - else if (rc != 0) - goto out; - if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) { - if (udav_openpipes(sc)) { - splx(s); - return EIO; - } - } + if (rc != 0) + return rc; - /* Initialize transmit ring */ - if (udav_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return EIO; - } + usbnet_lock(un); + if (usbnet_isdying(un)) + rc = EIO; + else + rc = usbnet_init_rx_tx(un); + usbnet_unlock(un); - /* Initialize receive ring */ - if (udav_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return EIO; - } + return rc; +} - /* Start up the receive pipe. */ - for (size_t i = 0; i < UDAV_RX_LIST_CNT; i++) { - struct udav_chain *c = &sc->sc_cdata.udav_rx_chain[i]; - usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, UDAV_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, udav_rxeof); - (void)usbd_transfer(c->udav_xfer); - DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev), - __func__)); - } +static void +udav_reset(struct usbnet *un) +{ + usbnet_isowned(un); - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; + if (usbnet_isdying(un)) + return; - callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); -out: - splx(s); - return rc; + udav_chip_init(un); } -Static void -udav_reset(struct udav_softc *sc) +static void +udav_chip_init(struct usbnet *un) { - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return; + usbnet_lock_mii_un_locked(un); /* Select PHY */ #if 1 @@ -739,40 +558,25 @@ udav_reset(struct udav_softc *sc) * XXX: force select internal phy. * external phy routines are not tested. */ - UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); + UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY); #else - if (sc->sc_flags & UDAV_EXT_PHY) { - UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); + if (un->un_flags & UDAV_EXT_PHY) { + UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY); } else { - UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); + UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY); } #endif - UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST); + UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_RST); - for (i = 0; i < UDAV_TX_TIMEOUT; i++) { - if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST)) + for (int i = 0; i < UDAV_TX_TIMEOUT; i++) { + if (!(udav_csr_read1(un, UDAV_NCR) & UDAV_NCR_RST)) break; delay(10); /* XXX */ } delay(10000); /* XXX */ -} -int -udav_activate(device_t self, enum devact act) -{ - struct udav_softc *sc = device_private(self); - - DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev), - __func__, act)); - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(&sc->sc_ec.ec_if); - sc->sc_dying = 1; - return 0; - default: - return EOPNOTSUPP; - } + usbnet_unlock_mii_un_locked(un); } #define UDAV_BITS 6 @@ -780,44 +584,44 @@ udav_activate(device_t self, enum devact #define UDAV_CALCHASH(addr) \ (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1)) -Static void -udav_setmulti(struct udav_softc *sc) +static void +udav_setiff_locked(struct usbnet *un) { - struct ethercom *ec = &sc->sc_ec; - struct ifnet *ifp; + struct ethercom *ec = usbnet_ec(un); + struct ifnet * const ifp = usbnet_ifp(un); struct ether_multi *enm; struct ether_multistep step; uint8_t hashes[8]; int h = 0; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) - return; + usbnet_isowned_mii(un); - ifp = GET_IFP(sc); + if (usbnet_isdying(un)) + return; - if (ISSET(sc->sc_flags, UDAV_NO_PHY)) { - UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL); - UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC); + if (ISSET(un->un_flags, UDAV_NO_PHY)) { + UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL); + UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_PRMSC); return; } if (ifp->if_flags & IFF_PROMISC) { - UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); + UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); return; } else if (ifp->if_flags & IFF_ALLMULTI) { allmulti: ifp->if_flags |= IFF_ALLMULTI; - UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL); - UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC); + UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL); + UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_PRMSC); return; } /* first, zot all the existing hash bits */ memset(hashes, 0x00, sizeof(hashes)); hashes[7] |= 0x80; /* broadcast address */ - udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes)); + udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes)); /* now program new ones */ ETHER_LOCK(ec); @@ -837,775 +641,239 @@ allmulti: /* disable all multicast */ ifp->if_flags &= ~IFF_ALLMULTI; - UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL); + UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL); /* write hash value to the register */ - udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes)); + udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes)); } -Static int -udav_openpipes(struct udav_softc *sc) +static void +udav_setiff(struct usbnet *un) { - usbd_status err; - int error = 0; - - if (sc->sc_dying) - return EIO; - - sc->sc_refcnt++; - - /* Open RX pipe */ - err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no, - USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx); - if (err) { - printf("%s: open rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - error = EIO; - goto done; - } - - /* Open TX pipe */ - err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no, - USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - error = EIO; - goto done; - } - -#if 0 - /* XXX: interrupt endpoint is not yet supported */ - /* Open Interrupt pipe */ - err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no, - USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc, - &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN, - udav_intr, USBD_DEFAULT_INTERVAL); - if (err) { - printf("%s: open intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - error = EIO; - goto done; - } -#endif - done: - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - - return error; + usbnet_lock_mii(un); + udav_setiff_locked(un); + usbnet_unlock_mii(un); } -Static int -udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m) -{ - struct mbuf *m_new = NULL; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - return ENOBUFS; - } - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - m_freem(m_new); - return ENOBUFS; - } - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - } else { - m_new = m; - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - m_new->m_data = m_new->m_ext.ext_buf; - } - - m_adj(m_new, ETHER_ALIGN); - c->udav_mbuf = m_new; - - return 0; -} - - -Static int -udav_rx_list_init(struct udav_softc *sc) -{ - struct udav_cdata *cd; - struct udav_chain *c; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < UDAV_RX_LIST_CNT; i++) { - c = &cd->udav_rx_chain[i]; - c->udav_sc = sc; - c->udav_idx = i; - if (udav_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->udav_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_pipe_rx, - UDAV_BUFSZ, 0, 0, &c->udav_xfer); - if (error) - return error; - c->udav_buf = usbd_get_buffer(c->udav_xfer); - } - } - - return 0; -} - -Static int -udav_tx_list_init(struct udav_softc *sc) -{ - struct udav_cdata *cd; - struct udav_chain *c; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < UDAV_TX_LIST_CNT; i++) { - c = &cd->udav_tx_chain[i]; - c->udav_sc = sc; - c->udav_idx = i; - c->udav_mbuf = NULL; - if (c->udav_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_pipe_tx, UDAV_BUFSZ, - USBD_FORCE_SHORT_XFER, 0, &c->udav_xfer); - if (error) - return error; - c->udav_buf = usbd_get_buffer(c->udav_xfer); - } - } - - return 0; -} - -Static void -udav_start(struct ifnet *ifp) -{ - struct udav_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev), - __func__, sc->sc_link)); - - if (sc->sc_dying) - return; - - if (!sc->sc_link) - return; - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (udav_send(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } - - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - bpf_mtap(ifp, m_head, BPF_D_OUT); - - ifp->if_flags |= IFF_OACTIVE; - /* Set a timeout in case the chip goes out to lunch. */ - ifp->if_timer = 5; -} - -Static int -udav_send(struct udav_softc *sc, struct mbuf *m, int idx) +static unsigned +udav_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { int total_len; - struct udav_chain *c; - usbd_status err; + uint8_t *buf = c->unc_buf; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); + usbnet_isowned_tx(un); - c = &sc->sc_cdata.udav_tx_chain[idx]; + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); + + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2) + return 0; /* Copy the mbuf data into a contiguous buffer */ - /* first 2 bytes are packet length */ - m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2); - c->udav_mbuf = m; + m_copydata(m, 0, m->m_pkthdr.len, buf + 2); total_len = m->m_pkthdr.len; if (total_len < UDAV_MIN_FRAME_LEN) { - memset(c->udav_buf + 2 + total_len, 0, + memset(buf + 2 + total_len, 0, UDAV_MIN_FRAME_LEN - total_len); total_len = UDAV_MIN_FRAME_LEN; } /* Frame length is specified in the first 2bytes of the buffer */ - c->udav_buf[0] = (uint8_t)total_len; - c->udav_buf[1] = (uint8_t)(total_len >> 8); + buf[0] = (uint8_t)total_len; + buf[1] = (uint8_t)(total_len >> 8); total_len += 2; - usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, total_len, - USBD_FORCE_SHORT_XFER, UDAV_TX_TIMEOUT, udav_txeof); - - /* Transmit */ - sc->sc_refcnt++; - err = usbd_transfer(c->udav_xfer); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - if (err != USBD_IN_PROGRESS) { - printf("%s: udav_send error=%s\n", device_xname(sc->sc_dev), - usbd_errstr(err)); - /* Stop the interface */ - usb_add_task(sc->sc_udev, &sc->sc_stop_task, - USB_TASKQ_DRIVER); - return EIO; - } - - DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev), - __func__, total_len)); - - sc->sc_cdata.udav_tx_cnt++; - - return 0; -} - -Static void -udav_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct udav_chain *c = priv; - struct udav_softc *sc = c->udav_sc; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (sc->sc_dying) - return; - - s = splnet(); - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; + DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev), + __func__, total_len)); - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) { - sc->sc_refcnt++; - usbd_clear_endpoint_stall_async(sc->sc_pipe_tx); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - } - splx(s); - return; - } - - ifp->if_opackets++; - - m_freem(c->udav_mbuf); - c->udav_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - udav_start(ifp); - - splx(s); + return total_len; } -Static void -udav_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +static void +udav_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct udav_chain *c = priv; - struct udav_softc *sc = c->udav_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - uint32_t total_len; - uint8_t *pktstat; - int s; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; + uint16_t pkt_len; + uint8_t pktstat; - if (sc->sc_dying) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->sc_rx_errs++; - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: %u usb errors on rx: %s\n", - device_xname(sc->sc_dev), sc->sc_rx_errs, - usbd_errstr(status)); - sc->sc_rx_errs = 0; - } - if (status == USBD_STALLED) { - sc->sc_refcnt++; - usbd_clear_endpoint_stall_async(sc->sc_pipe_rx); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - } - goto done; - } + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + /* first byte in received data */ + pktstat = *buf; + total_len -= sizeof(pktstat); + buf += sizeof(pktstat); - /* copy data to mbuf */ - m = c->udav_mbuf; - memcpy(mtod(m, char *), c->udav_buf, total_len); + DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(un->un_dev), pktstat)); - /* first byte in received data */ - pktstat = mtod(m, uint8_t *); - m_adj(m, sizeof(uint8_t)); - DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(sc->sc_dev), - *pktstat)); + pkt_len = UGETW(buf); + total_len -= sizeof(pkt_len); + buf += sizeof(pkt_len); - total_len = UGETW(mtod(m, uint8_t *)); - m_adj(m, sizeof(uint16_t)); + DPRINTF(("%s: RX Length: 0x%02x\n", device_xname(un->un_dev), pkt_len)); - if (*pktstat & UDAV_RSR_LCS) { + if (pktstat & UDAV_RSR_LCS) { ifp->if_collisions++; - goto done; - } - - if (total_len < sizeof(struct ether_header) || - *pktstat & UDAV_RSR_ERR) { - ifp->if_ierrors++; - goto done; + return; } - total_len -= ETHER_CRC_LEN; - - m->m_pkthdr.len = m->m_len = total_len; - m_set_rcvif(m, ifp); - - s = splnet(); - - if (udav_newbuf(sc, c, NULL) == ENOBUFS) { + if (pkt_len < sizeof(struct ether_header) || + pkt_len > total_len || + (pktstat & UDAV_RSR_ERR)) { ifp->if_ierrors++; - goto done1; + return; } - DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev), - __func__, m->m_len)); - if_percpuq_enqueue((ifp)->if_percpuq, (m)); - - done1: - splx(s); - - done: - /* Setup new transfer */ - usbd_setup_xfer(xfer, c, c->udav_buf, UDAV_BUFSZ, USBD_SHORT_XFER_OK, - USBD_NO_TIMEOUT, udav_rxeof); - sc->sc_refcnt++; - usbd_transfer(xfer); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + pkt_len -= ETHER_CRC_LEN; - DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__)); -} + DPRINTF(("%s: Rx deliver: 0x%02x\n", device_xname(un->un_dev), pkt_len)); -#if 0 -Static void udav_intr(void) -{ + usbnet_enqueue(un, buf, pkt_len, 0, 0, 0); } -#endif -Static int -udav_ioctl(struct ifnet *ifp, u_long cmd, void *data) +static int +udav_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct udav_softc *sc = ifp->if_softc; - int s, error = 0; + struct usbnet * const un = ifp->if_softc; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return EIO; - - s = splnet(); - - error = ether_ioctl(ifp, cmd, data); - if (error == ENETRESET) { - if (ifp->if_flags & IFF_RUNNING) - udav_setmulti(sc); - error = 0; + switch (cmd) { + case SIOCADDMULTI: + case SIOCDELMULTI: + udav_setiff(un); + break; + default: + break; } - splx(s); - - return error; -} - -Static void -udav_watchdog(struct ifnet *ifp) -{ - struct udav_softc *sc = ifp->if_softc; - struct udav_chain *c; - usbd_status stat; - int s; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->sc_dev)); - - s = splusb(); - c = &sc->sc_cdata.udav_tx_chain[0]; - usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat); - udav_txeof(c->udav_xfer, c, stat); - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - udav_start(ifp); - splx(s); -} - -Static void -udav_stop_task(struct udav_softc *sc) -{ - udav_stop(GET_IFP(sc), 1); + return 0; } /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */ -Static void -udav_stop(struct ifnet *ifp, int disable) -{ - struct udav_softc *sc = ifp->if_softc; - usbd_status err; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - ifp->if_timer = 0; - - udav_reset(sc); - - callout_stop(&sc->sc_stat_ch); - - /* Stop transfers */ - /* RX endpoint */ - if (sc->sc_pipe_rx != NULL) { - err = usbd_abort_pipe(sc->sc_pipe_rx); - if (err) - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - - /* TX endpoint */ - if (sc->sc_pipe_tx != NULL) { - err = usbd_abort_pipe(sc->sc_pipe_tx); - if (err) - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - -#if 0 - /* XXX: Interrupt endpoint is not yet supported!! */ - /* Interrupt endpoint */ - if (sc->sc_pipe_intr != NULL) { - err = usbd_abort_pipe(sc->sc_pipe_intr); - if (err) - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - err = usbd_close_pipe(sc->sc_pipe_intr); - if (err) - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - sc->sc_pipe_intr = NULL; - } -#endif - - /* Free RX resources. */ - for (i = 0; i < UDAV_RX_LIST_CNT; i++) { - if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) { - m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf); - sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL; - } - if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer); - sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < UDAV_TX_LIST_CNT; i++) { - if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) { - m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf); - sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL; - } - if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer); - sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL; - } - } - - /* Close pipes */ - /* RX endpoint */ - if (sc->sc_pipe_rx != NULL) { - err = usbd_close_pipe(sc->sc_pipe_rx); - if (err) - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - sc->sc_pipe_rx = NULL; - } - - /* TX endpoint */ - if (sc->sc_pipe_tx != NULL) { - err = usbd_close_pipe(sc->sc_pipe_tx); - if (err) - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - sc->sc_pipe_tx = NULL; - } - - if (!ISSET(sc->sc_flags, UDAV_NO_PHY)) - sc->sc_link = 0; - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); -} - -/* Set media options */ -Static int -udav_ifmedia_change(struct ifnet *ifp) -{ - struct udav_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); - int rc; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; - - sc->sc_link = 0; - if ((rc = mii_mediachg(mii)) == ENXIO) - return 0; - return rc; -} - -/* Report current media status. */ -Static void -udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) -{ - struct udav_softc *sc = ifp->if_softc; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return; - - ether_mediastatus(ifp, ifmr); -} - -Static void -udav_tick(void *xsc) +static void +udav_stop_cb(struct ifnet *ifp, int disable) { - struct udav_softc *sc = xsc; - - if (sc == NULL) - return; + struct usbnet * const un = ifp->if_softc; - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) - return; - - /* Perform periodic stuff in process context */ - usb_add_task(sc->sc_udev, &sc->sc_tick_task, - USB_TASKQ_DRIVER); + udav_reset(un); } -Static void -udav_tick_task(void *xsc) +static int +udav_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - struct udav_softc *sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - int s; - - if (sc == NULL) - return; - - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); - - if (sc->sc_dying) - return; - - ifp = GET_IFP(sc); - mii = GET_MII(sc); - - if (mii == NULL) - return; - - s = splnet(); - - mii_tick(mii); - if (!sc->sc_link) { - mii_pollstat(mii); - if (mii->mii_media_status & IFM_ACTIVE && - IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { - DPRINTF(("%s: %s: got link\n", - device_xname(sc->sc_dev), __func__)); - sc->sc_link++; - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - udav_start(ifp); - } - } - - callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc); - - splx(s); -} - -/* Get exclusive access to the MII registers */ -Static void -udav_lock_mii(struct udav_softc *sc) -{ - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); - - sc->sc_refcnt++; - mutex_enter(&sc->sc_mii_lock); -} - -Static void -udav_unlock_mii(struct udav_softc *sc) -{ - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); - - mutex_exit(&sc->sc_mii_lock); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); -} - -Static int -udav_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) -{ - struct udav_softc *sc; uint8_t data[2]; - if (dev == NULL) - return -1; - - sc = device_private(dev); + usbnet_isowned_mii(un); DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", - device_xname(sc->sc_dev), __func__, phy, reg)); + device_xname(un->un_dev), __func__, phy, reg)); - if (sc->sc_dying) { + if (usbnet_isdying(un)) { #ifdef DIAGNOSTIC - printf("%s: %s: dying\n", device_xname(sc->sc_dev), + printf("%s: %s: dying\n", device_xname(un->un_dev), __func__); #endif - return -1; + return EINVAL; } /* XXX: one PHY only for the internal PHY */ if (phy != 0) { DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", - device_xname(sc->sc_dev), __func__, phy)); - return -1; + device_xname(un->un_dev), __func__, phy)); + return EINVAL; } - udav_lock_mii(sc); - /* select internal PHY and set PHY register address */ - udav_csr_write1(sc, UDAV_EPAR, + udav_csr_write1(un, UDAV_EPAR, UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); /* select PHY operation and start read command */ - udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR); + udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR); /* XXX: should be wait? */ /* end read command */ - UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR); + UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRR); /* retrieve the result from data registers */ - udav_csr_read(sc, UDAV_EPDRL, data, 2); - - udav_unlock_mii(sc); + udav_csr_read(un, UDAV_EPDRL, data, 2); *val = data[0] | (data[1] << 8); DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n", - device_xname(sc->sc_dev), __func__, phy, reg, *val)); + device_xname(un->un_dev), __func__, phy, reg, *val)); return 0; } -Static int -udav_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +static int +udav_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct udav_softc *sc; uint8_t data[2]; - if (dev == NULL) - return -1; - - sc = device_private(dev); + usbnet_isowned_mii(un); DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n", - device_xname(sc->sc_dev), __func__, phy, reg, val)); + device_xname(un->un_dev), __func__, phy, reg, val)); - if (sc->sc_dying) { + if (usbnet_isdying(un)) { #ifdef DIAGNOSTIC - printf("%s: %s: dying\n", device_xname(sc->sc_dev), + printf("%s: %s: dying\n", device_xname(un->un_dev), __func__); #endif - return -1; + return EIO; } /* XXX: one PHY only for the internal PHY */ if (phy != 0) { DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", - device_xname(sc->sc_dev), __func__, phy)); - return -1; + device_xname(un->un_dev), __func__, phy)); + return EIO; } - udav_lock_mii(sc); - /* select internal PHY and set PHY register address */ - udav_csr_write1(sc, UDAV_EPAR, + udav_csr_write1(un, UDAV_EPAR, UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); /* put the value to the data registers */ data[0] = val & 0xff; data[1] = (val >> 8) & 0xff; - udav_csr_write(sc, UDAV_EPDRL, data, 2); + udav_csr_write(un, UDAV_EPDRL, data, 2); /* select PHY operation and start write command */ - udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW); + udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW); /* XXX: should be wait? */ /* end write command */ - UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW); - - udav_unlock_mii(sc); + UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRW); return 0; } -Static void -udav_miibus_statchg(struct ifnet *ifp) +static void +udav_mii_statchg(struct ifnet *ifp) { -#ifdef UDAV_DEBUG + struct usbnet * const un = ifp->if_softc; + struct mii_data * const mii = usbnet_mii(un); + + DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__)); - if (ifp == NULL) + if (usbnet_isdying(un)) return; - DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__)); -#endif - /* Nothing to do */ + if (mii->mii_media_status & IFM_ACTIVE && + IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { + DPRINTF(("%s: %s: got link\n", + device_xname(un->un_dev), __func__)); + usbnet_set_link(un, true); + } } + +#ifdef _MODULE +#include "ioconf.c" +#endif + +USBNET_MODULE(udav) Index: usb/if_udavreg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_udavreg.h,v retrieving revision 1.13 diff -p -u -u -r1.13 if_udavreg.h --- usb/if_udavreg.h 21 Jul 2019 10:27:56 -0000 1.13 +++ usb/if_udavreg.h 29 Aug 2019 06:22:05 -0000 @@ -1,5 +1,6 @@ /* $NetBSD: if_udavreg.h,v 1.13 2019/07/21 10:27:56 mrg Exp $ */ /* $nabe: if_udavreg.h,v 1.2 2003/08/21 16:26:40 nabe Exp $ */ + /* * Copyright (c) 2003 * Shingo WATANABE . All rights reserved. @@ -140,62 +141,3 @@ #define UDAV_GPR_GEPIO2 (1<<2) /* General purpose 2 */ #define UDAV_GPR_GEPIO1 (1<<1) /* General purpose 1 */ #define UDAV_GPR_GEPIO0 (1<<0) /* General purpose 0 */ - -#define GET_IFP(sc) (&(sc)->sc_ec.ec_if) -#define GET_MII(sc) (&(sc)->sc_mii) - -struct udav_chain { - struct udav_softc *udav_sc; - struct usbd_xfer *udav_xfer; - char *udav_buf; - struct mbuf *udav_mbuf; - int udav_idx; -}; - -struct udav_cdata { - struct udav_chain udav_tx_chain[UDAV_TX_LIST_CNT]; - struct udav_chain udav_rx_chain[UDAV_TX_LIST_CNT]; -#if 0 - /* XXX: Interrupt Endpoint is not yet supported! */ - struct udav_intrpkg udav_ibuf; -#endif - int udav_tx_prod; - int udav_tx_cnt; -}; - -struct udav_softc { - device_t sc_dev; /* base device */ - struct usbd_device * sc_udev; - - /* USB */ - struct usbd_interface * sc_ctl_iface; - /* int sc_ctl_iface_no; */ - int sc_bulkin_no; /* bulk in endpoint */ - int sc_bulkout_no; /* bulk out endpoint */ - int sc_intrin_no; /* intr in endpoint */ - struct usbd_pipe * sc_pipe_rx; - struct usbd_pipe * sc_pipe_tx; - struct usbd_pipe * sc_pipe_intr; - struct callout sc_stat_ch; - u_int sc_rx_errs; - /* u_int sc_intr_errs; */ - struct timeval sc_rx_notice; - - /* Ethernet */ - struct ethercom sc_ec; /* ethernet common */ - struct mii_data sc_mii; - kmutex_t sc_mii_lock; - int sc_link; -#define sc_media udav_mii.mii_media - krndsource_t rnd_source; - struct udav_cdata sc_cdata; - - int sc_attached; - int sc_dying; - int sc_refcnt; - - struct usb_task sc_tick_task; - struct usb_task sc_stop_task; - - uint16_t sc_flags; -}; Index: usb/if_upl.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_upl.c,v retrieving revision 1.64 diff -p -u -u -r1.64 if_upl.c --- usb/if_upl.c 21 Jul 2019 10:27:56 -0000 1.64 +++ usb/if_upl.c 29 Aug 2019 06:22:05 -0000 @@ -1,4 +1,5 @@ /* $NetBSD: if_upl.c,v 1.64 2019/07/21 10:27:56 mrg Exp $ */ + /* * Copyright (c) 2000 The NetBSD Foundation, Inc. * All rights reserved. @@ -42,22 +43,10 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #endif #include -#include -#include -#include -#include -#include -#include -#include -#include +#include -#include #include -#include -#include - -#include #ifdef INET #include @@ -65,12 +54,6 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #include #endif - -#include -#include -#include -#include - /* * 7 6 5 4 3 2 1 0 * tx rx 1 0 @@ -83,8 +66,6 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #define UPL_RXDATA 0x40 #define UPL_TXOK 0x80 -#define UPL_INTR_PKTLEN 1 - #define UPL_CONFIG_NO 1 #define UPL_IFACE_IDX 0 @@ -94,63 +75,9 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #define UPL_BUFSZ 1024 -#define UPL_RX_FRAMES 1 -#define UPL_TX_FRAMES 1 - #define UPL_RX_LIST_CNT 1 #define UPL_TX_LIST_CNT 1 -#define UPL_ENDPT_RX 0x0 -#define UPL_ENDPT_TX 0x1 -#define UPL_ENDPT_INTR 0x2 -#define UPL_ENDPT_MAX 0x3 - -struct upl_type { - uint16_t upl_vid; - uint16_t upl_did; -}; - -struct upl_softc; - -struct upl_chain { - struct upl_softc *upl_sc; - struct usbd_xfer *upl_xfer; - char *upl_buf; - struct mbuf *upl_mbuf; -}; - -struct upl_cdata { - struct upl_chain upl_tx_chain[UPL_TX_LIST_CNT]; - struct upl_chain upl_rx_chain[UPL_RX_LIST_CNT]; - int upl_tx_prod; - int upl_tx_cnt; -}; - -struct upl_softc { - device_t sc_dev; - - struct ifnet sc_if; - krndsource_t sc_rnd_source; - - struct callout sc_stat_ch; - - struct usbd_device * sc_udev; - struct usbd_interface * sc_iface; - uint16_t sc_vendor; - uint16_t sc_product; - int sc_ed[UPL_ENDPT_MAX]; - struct usbd_pipe * sc_ep[UPL_ENDPT_MAX]; - struct upl_cdata sc_cdata; - - uByte sc_ibuf; - - char sc_dying; - char sc_attached; - u_int sc_rx_errs; - struct timeval sc_rx_notice; - u_int sc_intr_errs; -}; - #ifdef UPL_DEBUG #define DPRINTF(x) if (upldebug) printf x #define DPRINTFN(n,x) if (upldebug >= (n)) printf x @@ -163,37 +90,43 @@ int upldebug = 0; /* * Various supported device vendors/products. */ -Static struct upl_type sc_devs[] = { +static struct usb_devno sc_devs[] = { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 }, { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 }, + { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL25A1 }, + { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U258 }, + { USB_VENDOR_NI, USB_PRODUCT_NI_HTOH_7825 }, { 0, 0 } }; int upl_match(device_t, cfdata_t, void *); void upl_attach(device_t, device_t, void *); -int upl_detach(device_t, int); -int upl_activate(device_t, enum devact); -CFATTACH_DECL_NEW(upl, sizeof(struct upl_softc), upl_match, upl_attach, - upl_detach, upl_activate); +CFATTACH_DECL_NEW(upl, sizeof(struct usbnet), upl_match, upl_attach, + usbnet_detach, usbnet_activate); -Static int upl_openpipes(struct upl_softc *); -Static int upl_tx_list_init(struct upl_softc *); -Static int upl_rx_list_init(struct upl_softc *); -Static int upl_newbuf(struct upl_softc *, struct upl_chain *, struct mbuf *); -Static int upl_send(struct upl_softc *, struct mbuf *, int); -Static void upl_intr(struct usbd_xfer *, void *, usbd_status); -Static void upl_rxeof(struct usbd_xfer *, void *, usbd_status); -Static void upl_txeof(struct usbd_xfer *, void *, usbd_status); -Static void upl_start(struct ifnet *); -Static int upl_ioctl(struct ifnet *, u_long, void *); -Static void upl_init(void *); -Static void upl_stop(struct upl_softc *); -Static void upl_watchdog(struct ifnet *); +#if 0 +static void upl_intr_cb(struct usbnet *, usbd_status); +#endif +static void upl_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned upl_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static int upl_ioctl_cb(struct ifnet *, u_long, void *); +static int upl_init(struct ifnet *); + +static struct usbnet_ops upl_ops = { + .uno_init = upl_init, + .uno_tx_prepare = upl_tx_prepare, + .uno_rx_loop = upl_rx_loop, + .uno_ioctl = upl_ioctl_cb, +#if 0 + .uno_intr = upl_intr_cb, +#endif +}; -Static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *, +static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *, const struct rtentry *); -Static void upl_input(struct ifnet *, struct mbuf *); +static void upl_input(struct ifnet *, struct mbuf *); /* * Probe for a Prolific chip. @@ -202,42 +135,42 @@ int upl_match(device_t parent, cfdata_t match, void *aux) { struct usb_attach_arg *uaa = aux; - struct upl_type *t; - for (t = sc_devs; t->upl_vid != 0; t++) - if (uaa->uaa_vendor == t->upl_vid && uaa->uaa_product == t->upl_did) - return UMATCH_VENDOR_PRODUCT; - - return UMATCH_NONE; + return usb_lookup(sc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL ? + UMATCH_VENDOR_PRODUCT : UMATCH_NONE; } void upl_attach(device_t parent, device_t self, void *aux) { - struct upl_softc *sc = device_private(self); - struct usb_attach_arg *uaa = aux; + struct usbnet * const un = device_private(self); + struct usb_attach_arg *uaa = aux; char *devinfop; - int s; struct usbd_device * dev = uaa->uaa_device; - struct usbd_interface * iface; usbd_status err; - struct ifnet *ifp; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - int rv; - DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev)); - - sc->sc_dev = self; + DPRINTFN(5,(" : upl_attach: un=%p, dev=%p", un, dev)); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = un; + un->un_ops = &upl_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = UPL_RX_LIST_CNT; + un->un_tx_list_cnt = UPL_TX_LIST_CNT; + un->un_rx_bufsz = UPL_BUFSZ; + un->un_tx_bufsz = UPL_BUFSZ; + err = usbd_set_config_no(dev, UPL_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -245,56 +178,41 @@ upl_attach(device_t parent, device_t sel return; } - sc->sc_udev = dev; - sc->sc_product = uaa->uaa_product; - sc->sc_vendor = uaa->uaa_vendor; - - err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &iface); + err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->sc_iface = iface; - id = usbd_get_interface_descriptor(iface); + id = usbd_get_interface_descriptor(un->un_iface); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_ed[UPL_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_ed[UPL_ENDPT_TX] = ed->bEndpointAddress; - } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && - UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } } - if (sc->sc_ed[UPL_ENDPT_RX] == 0 || sc->sc_ed[UPL_ENDPT_TX] == 0 || - sc->sc_ed[UPL_ENDPT_INTR] == 0) { + if (un->un_ed[USBNET_ENDPT_RX] == 0 || un->un_ed[USBNET_ENDPT_TX] == 0 /*|| + un->un_ed[USBNET_ENDPT_INTR] == 0*/) { aprint_error_dev(self, "missing endpoint\n"); return; } - s = splnet(); + usbnet_attach(un, "upldet"); /* Initialize interface info.*/ - ifp = &sc->sc_if; - ifp->if_softc = sc; + struct ifnet *ifp = usbnet_ifp(un); ifp->if_mtu = UPL_BUFSZ; - ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX; - ifp->if_ioctl = upl_ioctl; - ifp->if_start = upl_start; - ifp->if_watchdog = upl_watchdog; - strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); - ifp->if_type = IFT_OTHER; ifp->if_addrlen = 0; ifp->if_hdrlen = 0; @@ -302,705 +220,86 @@ upl_attach(device_t parent, device_t sel ifp->_if_input = upl_input; ifp->if_baudrate = 12000000; ifp->if_dlt = DLT_RAW; - IFQ_SET_READY(&ifp->if_snd); - - /* Attach the interface. */ - rv = if_initialize(ifp); - if (rv != 0) { - aprint_error_dev(self, "if_initialize failed(%d)\n", rv); - splx(s); - return; - } - if_register(ifp); - if_alloc_sadl(ifp); - - bpf_attach(ifp, DLT_RAW, 0); - rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - sc->sc_attached = 1; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); - - return; -} - -int -upl_detach(device_t self, int flags) -{ - struct upl_softc *sc = device_private(self); - struct ifnet *ifp = &sc->sc_if; - int s; - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - s = splusb(); - - if (!sc->sc_attached) { - /* Detached before attached finished, so just bail out. */ - splx(s); - return 0; - } - - if (ifp->if_flags & IFF_RUNNING) - upl_stop(sc); - - rnd_detach_source(&sc->sc_rnd_source); - bpf_detach(ifp); - - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->sc_ep[UPL_ENDPT_TX] != NULL || - sc->sc_ep[UPL_ENDPT_RX] != NULL || - sc->sc_ep[UPL_ENDPT_INTR] != NULL) - aprint_debug_dev(self, "detach has active endpoints\n"); -#endif - - sc->sc_attached = 0; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); - - return 0; -} - -int -upl_activate(device_t self, enum devact act) -{ - struct upl_softc *sc = device_private(self); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - switch (act) { - case DVACT_DEACTIVATE: - /* Deactivate the interface. */ - if_deactivate(&sc->sc_if); - sc->sc_dying = 1; - return 0; - default: - return EOPNOTSUPP; - } -} - -/* - * Initialize an RX descriptor and attach an MBUF cluster. - */ -Static int -upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m) -{ - struct mbuf *m_new = NULL; - - DPRINTFN(8,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - return ENOBUFS; - } - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - m_freem(m_new); - return ENOBUFS; - } - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - } else { - m_new = m; - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - m_new->m_data = m_new->m_ext.ext_buf; - } - - c->upl_mbuf = m_new; - - return 0; -} - -Static int -upl_rx_list_init(struct upl_softc *sc) -{ - struct upl_cdata *cd; - struct upl_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < UPL_RX_LIST_CNT; i++) { - c = &cd->upl_rx_chain[i]; - c->upl_sc = sc; - if (upl_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->upl_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_RX], - UPL_BUFSZ, 0, 0, &c->upl_xfer); - if (error) - return error; - c->upl_buf = usbd_get_buffer(c->upl_xfer); - } - } - - return 0; -} - -Static int -upl_tx_list_init(struct upl_softc *sc) -{ - struct upl_cdata *cd; - struct upl_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < UPL_TX_LIST_CNT; i++) { - c = &cd->upl_tx_chain[i]; - c->upl_sc = sc; - c->upl_mbuf = NULL; - if (c->upl_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_TX], - UPL_BUFSZ, 0, 0, &c->upl_xfer); - if (error) - return error; - c->upl_buf = usbd_get_buffer(c->upl_xfer); - } - } - - return 0; + usbnet_attach_ifp(un, IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX, + 0, NULL); } -/* - * A frame has been uploaded: pass the resulting mbuf chain up to - * the higher level protocols. - */ -Static void -upl_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +static void +upl_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len) { - struct upl_chain *c = priv; - struct upl_softc *sc = c->upl_sc; - struct ifnet *ifp = &sc->sc_if; - struct mbuf *m; - int total_len = 0; - int s; - - if (sc->sc_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->sc_rx_errs++; - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: %u usb errors on rx: %s\n", - device_xname(sc->sc_dev), sc->sc_rx_errs, - usbd_errstr(status)); - sc->sc_rx_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + usbnet_isowned_rx(un); DPRINTFN(9,("%s: %s: enter status=%d length=%d\n", - device_xname(sc->sc_dev), __func__, status, total_len)); - - m = c->upl_mbuf; - memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len); - - m->m_pkthdr.len = m->m_len = total_len; - - m_set_rcvif(m, ifp); - - s = splnet(); - - /* XXX ugly */ - if (upl_newbuf(sc, c, NULL) == ENOBUFS) { - ifp->if_ierrors++; - goto done1; - } - - DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->sc_dev), - __func__, m->m_len)); - - if_input((ifp), (m)); - - done1: - splx(s); - - done: -#if 1 - /* Setup new transfer. */ - usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upl_rxeof); - usbd_transfer(c->upl_xfer); - - DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->sc_dev), - __func__)); -#endif -} - -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ -Static void -upl_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct upl_chain *c = priv; - struct upl_softc *sc = c->upl_sc; - struct ifnet *ifp = &sc->sc_if; - int s; - - if (sc->sc_dying) - return; - - s = splnet(); - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->sc_dev), - __func__, status)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]); - splx(s); - return; - } - - ifp->if_opackets++; + device_xname(un->un_dev), __func__, status, total_len)); - m_freem(c->upl_mbuf); - c->upl_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - upl_start(ifp); - - splx(s); + usbnet_input(un, c->unc_buf, total_len); } -Static int -upl_send(struct upl_softc *sc, struct mbuf *m, int idx) +static unsigned +upl_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - int total_len; - struct upl_chain *c; - usbd_status err; + int total_len; - c = &sc->sc_cdata.upl_tx_chain[idx]; - - /* - * Copy the mbuf data into a contiguous buffer, leaving two - * bytes at the beginning to hold the frame length. - */ - m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf); - c->upl_mbuf = m; + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz) + return 0; + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf); total_len = m->m_pkthdr.len; DPRINTFN(10,("%s: %s: total_len=%d\n", - device_xname(sc->sc_dev), __func__, total_len)); - - usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, total_len, 0, - USBD_DEFAULT_TIMEOUT, upl_txeof); - - /* Transmit */ - err = usbd_transfer(c->upl_xfer); - if (err != USBD_IN_PROGRESS) { - printf("%s: upl_send error=%s\n", device_xname(sc->sc_dev), - usbd_errstr(err)); - upl_stop(sc); - return EIO; - } - - sc->sc_cdata.upl_tx_cnt++; - - return 0; -} - -Static void -upl_start(struct ifnet *ifp) -{ - struct upl_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - if (sc->sc_dying) - return; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (upl_send(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } - - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - /* - * If there's a BPF listener, bounce a copy of this frame - * to him. - */ - bpf_mtap(ifp, m_head, BPF_D_OUT); - - ifp->if_flags |= IFF_OACTIVE; - - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 5; -} - -Static void -upl_init(void *xsc) -{ - struct upl_softc *sc = xsc; - struct ifnet *ifp = &sc->sc_if; - int s; - - if (sc->sc_dying) - return; + device_xname(un->un_dev), __func__, total_len)); - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (ifp->if_flags & IFF_RUNNING) - return; - - s = splnet(); - - if (sc->sc_ep[UPL_ENDPT_RX] == NULL) { - if (upl_openpipes(sc)) { - splx(s); - return; - } - } - /* Init TX ring. */ - if (upl_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return; - } - - /* Init RX ring. */ - if (upl_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return; - } - - /* Start up the receive pipe. */ - for (int i = 0; i < UPL_RX_LIST_CNT; i++) { - struct upl_chain *c = &sc->sc_cdata.upl_rx_chain[i]; - usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, - upl_rxeof); - usbd_transfer(c->upl_xfer); - } - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); + return total_len; } -Static int -upl_openpipes(struct upl_softc *sc) +static int +upl_init(struct ifnet *ifp) { - usbd_status err; - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX], - USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]); - if (err) { - printf("%s: open rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX], - USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR], - USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc, - &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr, - UPL_INTR_INTERVAL); - if (err) { - printf("%s: open intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - return EIO; - } - - return 0; -} - -Static void -upl_intr(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct upl_softc *sc = priv; - struct ifnet *ifp = &sc->sc_if; - uByte stat; - - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (sc->sc_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; + struct usbnet * const un = ifp->if_softc; + int rv; - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - return; - } - sc->sc_intr_errs++; - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: %u usb errors on intr: %s\n", - device_xname(sc->sc_dev), sc->sc_rx_errs, - usbd_errstr(status)); - sc->sc_intr_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]); - return; - } - - stat = sc->sc_ibuf; - - if (stat == 0) - return; - - DPRINTFN(10,("%s: %s: stat=0x%02x\n", device_xname(sc->sc_dev), - __func__, stat)); + usbnet_lock(un); + if (usbnet_isdying(un)) + rv = EIO; + else + rv = usbnet_init_rx_tx(un); + usbnet_unlock(un); + return rv; } -Static int -upl_ioctl(struct ifnet *ifp, u_long command, void *data) +static int +upl_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct upl_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; + if (cmd == SIOCSIFMTU) { + struct ifreq *ifr = data; - if (sc->sc_dying) - return EIO; - - DPRINTFN(5,("%s: %s: cmd=0x%08lx\n", - device_xname(sc->sc_dev), __func__, command)); - - s = splnet(); - - switch(command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - upl_init(sc); - - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - break; -#endif /* INET */ - } - break; - - case SIOCSIFMTU: if (ifr->ifr_mtu > UPL_BUFSZ) - error = EINVAL; - else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) - error = 0; - break; - - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - /* XXX re-use ether_ioctl() */ - switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { - case IFF_UP: - upl_init(sc); - break; - case IFF_RUNNING: - upl_stop(sc); - break; - default: - break; - } - break; - default: - error = ifioctl_common(ifp, command, data); - break; - } - - splx(s); - - return error; -} - -Static void -upl_watchdog(struct ifnet *ifp) -{ - struct upl_softc *sc = ifp->if_softc; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (sc->sc_dying) - return; - - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->sc_dev)); - - upl_stop(sc); - upl_init(sc); - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - upl_start(ifp); -} - -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -Static void -upl_stop(struct upl_softc *sc) -{ - usbd_status err; - struct ifnet *ifp; - int i; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - ifp = &sc->sc_if; - ifp->if_timer = 0; - - /* Stop transfers. */ - if (sc->sc_ep[UPL_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]); - if (err) { - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - if (sc->sc_ep[UPL_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]); - if (err) { - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < UPL_RX_LIST_CNT; i++) { - if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) { - m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf); - sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < UPL_TX_LIST_CNT; i++) { - if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) { - m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf); - sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL; - } - if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer); - sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL; - } - } - - /* Close pipes */ - if (sc->sc_ep[UPL_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[UPL_ENDPT_RX] = NULL; - } - - if (sc->sc_ep[UPL_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[UPL_ENDPT_TX] = NULL; - } - - if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[UPL_ENDPT_INTR] = NULL; + return EINVAL; } - - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + return 0; } -Static int +static int upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, const struct rtentry *rt0) { - int error; + struct usbnet * const un __unused = ifp->if_softc; - DPRINTFN(10,("%s: %s: enter\n", - device_xname(((struct upl_softc *)ifp->if_softc)->sc_dev), - __func__)); + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - /* - * if the queueing discipline needs packet classification, - * do it now. - */ + /* If the queueing discipline needs packet classification, do it now. */ IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family); /* * Queue message on interface, and start output if interface * not yet active. */ - error = if_transmit_lock(ifp, m); - - return error; + return if_transmit_lock(ifp, m); } -Static void +static void upl_input(struct ifnet *ifp, struct mbuf *m) { #ifdef INET @@ -1018,3 +317,9 @@ upl_input(struct ifnet *ifp, struct mbuf splx(s); #endif } + +#ifdef _MODULE +#include "ioconf.c" +#endif + +USBNET_MODULE(upl) Index: usb/if_ure.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_ure.c,v retrieving revision 1.14 diff -p -u -u -r1.14 if_ure.c --- usb/if_ure.c 19 Jul 2019 04:17:34 -0000 1.14 +++ usb/if_ure.c 29 Aug 2019 06:22:05 -0000 @@ -1,6 +1,6 @@ /* $NetBSD: if_ure.c,v 1.14 2019/07/19 04:17:34 mrg Exp $ */ - /* $OpenBSD: if_ure.c,v 1.10 2018/11/02 21:32:30 jcs Exp $ */ + /*- * Copyright (c) 2015-2016 Kevin Lo * All rights reserved. @@ -38,52 +38,29 @@ __KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1 #endif #include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include +#include -#include +#include #include /* XXX for in_undefer_cksum() */ #ifdef INET6 +#include #include /* XXX for in6_undefer_cksum() */ #endif -#include -#include - -#include -#include -#include -#include -#include - #include /* XXX for RTK_GMEDIASTAT */ #include #include -#define URE_PRINTF(sc, fmt, args...) \ - device_printf((sc)->ure_dev, "%s: " fmt, __func__, ##args); +#define URE_PRINTF(un, fmt, args...) \ + device_printf((un)->un_dev, "%s: " fmt, __func__, ##args); #define URE_DEBUG #ifdef URE_DEBUG #define DPRINTF(x) do { if (uredebug) printf x; } while (0) #define DPRINTFN(n, x) do { if (uredebug >= (n)) printf x; } while (0) -int uredebug = 1; +int uredebug = 0; #else #define DPRINTF(x) #define DPRINTFN(n, x) @@ -94,67 +71,51 @@ static const struct usb_devno ure_devs[] { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8153 } }; -static int ure_match(device_t, cfdata_t, void *); -static void ure_attach(device_t, device_t, void *); -static int ure_detach(device_t, int); -static int ure_activate(device_t, enum devact); - -static int ure_ctl(struct ure_softc *, uint8_t, uint16_t, uint16_t, - void *, int); -static int ure_read_mem(struct ure_softc *, uint16_t, uint16_t, void *, - int); -static int ure_write_mem(struct ure_softc *, uint16_t, uint16_t, void *, - int); -static uint8_t ure_read_1(struct ure_softc *, uint16_t, uint16_t); -static uint16_t ure_read_2(struct ure_softc *, uint16_t, uint16_t); -static uint32_t ure_read_4(struct ure_softc *, uint16_t, uint16_t); -static int ure_write_1(struct ure_softc *, uint16_t, uint16_t, uint32_t); -static int ure_write_2(struct ure_softc *, uint16_t, uint16_t, uint32_t); -static int ure_write_4(struct ure_softc *, uint16_t, uint16_t, uint32_t); -static uint16_t ure_ocp_reg_read(struct ure_softc *, uint16_t); -static void ure_ocp_reg_write(struct ure_softc *, uint16_t, uint16_t); +#define URE_BUFSZ (16 * 1024) -static int ure_init(struct ifnet *); -static void ure_stop(struct ifnet *, int); -static void ure_stop_locked(struct ifnet *, int); -static void ure_start(struct ifnet *); -static void ure_reset(struct ure_softc *); -static void ure_miibus_statchg(struct ifnet *); -static int ure_miibus_readreg(device_t, int, int, uint16_t *); -static int ure_miibus_writereg(device_t, int, int, uint16_t); -static void ure_lock_mii(struct ure_softc *); -static void ure_unlock_mii(struct ure_softc *); - -static int ure_encap(struct ure_softc *, struct mbuf *, int); +static void ure_reset(struct usbnet *); static uint32_t ure_txcsum(struct mbuf *); -static void ure_rxeof(struct usbd_xfer *, void *, usbd_status); static int ure_rxcsum(struct ifnet *, struct ure_rxpkt *); -static void ure_txeof(struct usbd_xfer *, void *, usbd_status); -static int ure_rx_list_init(struct ure_softc *); -static int ure_tx_list_init(struct ure_softc *); - -static void ure_tick_task(void *); -static void ure_tick(void *); - -static int ure_ifmedia_upd(struct ifnet *); -static void ure_ifmedia_sts(struct ifnet *, struct ifmediareq *); -static int ure_ioctl(struct ifnet *, u_long, void *); -static void ure_rtl8152_init(struct ure_softc *); -static void ure_rtl8153_init(struct ure_softc *); -static void ure_disable_teredo(struct ure_softc *); -static void ure_init_fifo(struct ure_softc *); +static void ure_rtl8152_init(struct usbnet *); +static void ure_rtl8153_init(struct usbnet *); +static void ure_disable_teredo(struct usbnet *); +static void ure_init_fifo(struct usbnet *); + +static void ure_stop_cb(struct ifnet *, int); +static int ure_ioctl_cb(struct ifnet *, u_long, void *); +static int ure_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static int ure_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void ure_miibus_statchg(struct ifnet *); +static unsigned ure_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void ure_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static int ure_init(struct ifnet *); + +static int ure_match(device_t, cfdata_t, void *); +static void ure_attach(device_t, device_t, void *); -CFATTACH_DECL_NEW(ure, sizeof(struct ure_softc), ure_match, ure_attach, - ure_detach, ure_activate); +CFATTACH_DECL_NEW(ure, sizeof(struct usbnet), ure_match, ure_attach, + usbnet_detach, usbnet_activate); + +static struct usbnet_ops ure_ops = { + .uno_stop = ure_stop_cb, + .uno_ioctl = ure_ioctl_cb, + .uno_read_reg = ure_mii_read_reg, + .uno_write_reg = ure_mii_write_reg, + .uno_statchg = ure_miibus_statchg, + .uno_tx_prepare = ure_tx_prepare, + .uno_rx_loop = ure_rx_loop, + .uno_init = ure_init, +}; static int -ure_ctl(struct ure_softc *sc, uint8_t rw, uint16_t val, uint16_t index, +ure_ctl(struct usbnet *un, uint8_t rw, uint16_t val, uint16_t index, void *buf, int len) { usb_device_request_t req; usbd_status err; - if (sc->ure_dying) + if (usbnet_isdying(un)) return 0; if (rw == URE_CTL_WRITE) @@ -168,7 +129,7 @@ ure_ctl(struct ure_softc *sc, uint8_t rw DPRINTFN(5, ("ure_ctl: rw %d, val 0x%04hu, index 0x%04hu, len %d\n", rw, val, index, len)); - err = usbd_do_request(sc->ure_udev, &req, buf); + err = usbd_do_request(un->un_udev, &req, buf); if (err) { DPRINTF(("ure_ctl: error %d\n", err)); return -1; @@ -178,23 +139,21 @@ ure_ctl(struct ure_softc *sc, uint8_t rw } static int -ure_read_mem(struct ure_softc *sc, uint16_t addr, uint16_t index, +ure_read_mem(struct usbnet *un, uint16_t addr, uint16_t index, void *buf, int len) { - - return ure_ctl(sc, URE_CTL_READ, addr, index, buf, len); + return ure_ctl(un, URE_CTL_READ, addr, index, buf, len); } static int -ure_write_mem(struct ure_softc *sc, uint16_t addr, uint16_t index, +ure_write_mem(struct usbnet *un, uint16_t addr, uint16_t index, void *buf, int len) { - - return ure_ctl(sc, URE_CTL_WRITE, addr, index, buf, len); + return ure_ctl(un, URE_CTL_WRITE, addr, index, buf, len); } static uint8_t -ure_read_1(struct ure_softc *sc, uint16_t reg, uint16_t index) +ure_read_1(struct usbnet *un, uint16_t reg, uint16_t index) { uint32_t val; uint8_t temp[4]; @@ -203,7 +162,7 @@ ure_read_1(struct ure_softc *sc, uint16_ shift = (reg & 3) << 3; reg &= ~3; - ure_read_mem(sc, reg, index, &temp, 4); + ure_read_mem(un, reg, index, &temp, 4); val = UGETDW(temp); val >>= shift; @@ -211,7 +170,7 @@ ure_read_1(struct ure_softc *sc, uint16_ } static uint16_t -ure_read_2(struct ure_softc *sc, uint16_t reg, uint16_t index) +ure_read_2(struct usbnet *un, uint16_t reg, uint16_t index) { uint32_t val; uint8_t temp[4]; @@ -220,7 +179,7 @@ ure_read_2(struct ure_softc *sc, uint16_ shift = (reg & 2) << 3; reg &= ~3; - ure_read_mem(sc, reg, index, &temp, 4); + ure_read_mem(un, reg, index, &temp, 4); val = UGETDW(temp); val >>= shift; @@ -228,16 +187,16 @@ ure_read_2(struct ure_softc *sc, uint16_ } static uint32_t -ure_read_4(struct ure_softc *sc, uint16_t reg, uint16_t index) +ure_read_4(struct usbnet *un, uint16_t reg, uint16_t index) { uint8_t temp[4]; - ure_read_mem(sc, reg, index, &temp, 4); + ure_read_mem(un, reg, index, &temp, 4); return UGETDW(temp); } static int -ure_write_1(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val) +ure_write_1(struct usbnet *un, uint16_t reg, uint16_t index, uint32_t val) { uint16_t byen; uint8_t temp[4]; @@ -254,11 +213,11 @@ ure_write_1(struct ure_softc *sc, uint16 } USETDW(temp, val); - return ure_write_mem(sc, reg, index | byen, &temp, 4); + return ure_write_mem(un, reg, index | byen, &temp, 4); } static int -ure_write_2(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val) +ure_write_2(struct usbnet *un, uint16_t reg, uint16_t index, uint32_t val) { uint16_t byen; uint8_t temp[4]; @@ -275,80 +234,68 @@ ure_write_2(struct ure_softc *sc, uint16 } USETDW(temp, val); - return ure_write_mem(sc, reg, index | byen, &temp, 4); + return ure_write_mem(un, reg, index | byen, &temp, 4); } static int -ure_write_4(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val) +ure_write_4(struct usbnet *un, uint16_t reg, uint16_t index, uint32_t val) { uint8_t temp[4]; USETDW(temp, val); - return ure_write_mem(sc, reg, index | URE_BYTE_EN_DWORD, &temp, 4); + return ure_write_mem(un, reg, index | URE_BYTE_EN_DWORD, &temp, 4); } static uint16_t -ure_ocp_reg_read(struct ure_softc *sc, uint16_t addr) +ure_ocp_reg_read(struct usbnet *un, uint16_t addr) { uint16_t reg; - ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000); + ure_write_2(un, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000); reg = (addr & 0x0fff) | 0xb000; - return ure_read_2(sc, reg, URE_MCU_TYPE_PLA); + return ure_read_2(un, reg, URE_MCU_TYPE_PLA); } static void -ure_ocp_reg_write(struct ure_softc *sc, uint16_t addr, uint16_t data) +ure_ocp_reg_write(struct usbnet *un, uint16_t addr, uint16_t data) { uint16_t reg; - ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000); + ure_write_2(un, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000); reg = (addr & 0x0fff) | 0xb000; - ure_write_2(sc, reg, URE_MCU_TYPE_PLA, data); + ure_write_2(un, reg, URE_MCU_TYPE_PLA, data); } static int -ure_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) +ure_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - struct ure_softc *sc = device_private(dev); + usbnet_isowned_mii(un); - mutex_enter(&sc->ure_lock); - if (sc->ure_dying || sc->ure_phyno != phy) { - mutex_exit(&sc->ure_lock); - return -1; - } - mutex_exit(&sc->ure_lock); + if (un->un_phyno != phy) + return EINVAL; /* Let the rgephy driver read the URE_PLA_PHYSTATUS register. */ if (reg == RTK_GMEDIASTAT) { - *val = ure_read_1(sc, URE_PLA_PHYSTATUS, URE_MCU_TYPE_PLA); - return 0; + *val = ure_read_1(un, URE_PLA_PHYSTATUS, URE_MCU_TYPE_PLA); + return USBD_NORMAL_COMPLETION; } - ure_lock_mii(sc); - *val = ure_ocp_reg_read(sc, URE_OCP_BASE_MII + reg * 2); - ure_unlock_mii(sc); + *val = ure_ocp_reg_read(un, URE_OCP_BASE_MII + reg * 2); return 0; } static int -ure_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +ure_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct ure_softc *sc = device_private(dev); + usbnet_isowned_mii(un); - mutex_enter(&sc->ure_lock); - if (sc->ure_dying || sc->ure_phyno != phy) { - mutex_exit(&sc->ure_lock); - return -1; - } - mutex_exit(&sc->ure_lock); + if (un->un_phyno != phy) + return EINVAL; - ure_lock_mii(sc); - ure_ocp_reg_write(sc, URE_OCP_BASE_MII + reg * 2, val); - ure_unlock_mii(sc); + ure_ocp_reg_write(un, URE_OCP_BASE_MII + reg * 2, val); return 0; } @@ -356,30 +303,23 @@ ure_miibus_writereg(device_t dev, int ph static void ure_miibus_statchg(struct ifnet *ifp) { - struct ure_softc *sc; - struct mii_data *mii; - - if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) - return; - - sc = ifp->if_softc; - mii = GET_MII(sc); + struct usbnet * const un = ifp->if_softc; + struct mii_data * const mii = usbnet_mii(un); - if (mii == NULL) + if (usbnet_isdying(un)) return; - sc->ure_flags &= ~URE_FLAG_LINK; if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == (IFM_ACTIVE | IFM_AVALID)) { switch (IFM_SUBTYPE(mii->mii_media_active)) { case IFM_10_T: case IFM_100_TX: - sc->ure_flags |= URE_FLAG_LINK; + usbnet_set_link(un, true); break; case IFM_1000_T: - if ((sc->ure_flags & URE_FLAG_8152) != 0) + if ((un->un_flags & URE_FLAG_8152) != 0) break; - sc->ure_flags |= URE_FLAG_LINK; + usbnet_set_link(un, true); break; default: break; @@ -387,55 +327,23 @@ ure_miibus_statchg(struct ifnet *ifp) } } -static int -ure_ifmedia_upd(struct ifnet *ifp) -{ - struct ure_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); - int err; - - sc->ure_flags &= ~URE_FLAG_LINK; - if (mii->mii_instance) { - struct mii_softc *miisc; - LIST_FOREACH(miisc, &mii->mii_phys, mii_list) - mii_phy_reset(miisc); - } - - err = mii_mediachg(mii); - if (err == ENXIO) - return 0; /* XXX */ - else - return err; -} - static void -ure_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) +ure_setiff_locked(struct usbnet *un) { - struct ure_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); - - mii_pollstat(mii); - ifmr->ifm_active = mii->mii_media_active; - ifmr->ifm_status = mii->mii_media_status; -} - -static void -ure_iff_locked(struct ure_softc *sc) -{ - struct ethercom *ec = &sc->ure_ec; - struct ifnet *ifp = GET_IFP(sc); + struct ethercom *ec = usbnet_ec(un); + struct ifnet *ifp = usbnet_ifp(un); struct ether_multi *enm; struct ether_multistep step; uint32_t hashes[2] = { 0, 0 }; uint32_t hash; uint32_t rxmode; - KASSERT(mutex_owned(&sc->ure_lock)); + usbnet_isowned(un); - if (sc->ure_dying) + if (usbnet_isdying(un)) return; - rxmode = ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA); + rxmode = ure_read_4(un, URE_PLA_RCR, URE_MCU_TYPE_PLA); rxmode &= ~URE_RCR_ACPT_ALL; /* @@ -482,659 +390,440 @@ allmulti: hashes[1] = hash; } - ure_write_4(sc, URE_PLA_MAR0, URE_MCU_TYPE_PLA, hashes[0]); - ure_write_4(sc, URE_PLA_MAR4, URE_MCU_TYPE_PLA, hashes[1]); - ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode); + ure_write_4(un, URE_PLA_MAR0, URE_MCU_TYPE_PLA, hashes[0]); + ure_write_4(un, URE_PLA_MAR4, URE_MCU_TYPE_PLA, hashes[1]); + ure_write_4(un, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode); } static void -ure_iff(struct ure_softc *sc) +ure_setiff(struct usbnet *un) { - mutex_enter(&sc->ure_lock); - ure_iff_locked(sc); - mutex_exit(&sc->ure_lock); + usbnet_lock(un); + ure_setiff_locked(un); + usbnet_unlock(un); } static void -ure_reset(struct ure_softc *sc) +ure_reset(struct usbnet *un) { int i; - KASSERT(mutex_owned(&sc->ure_lock)); + usbnet_isowned(un); - ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, URE_CR_RST); + ure_write_1(un, URE_PLA_CR, URE_MCU_TYPE_PLA, URE_CR_RST); for (i = 0; i < URE_TIMEOUT; i++) { - if (!(ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) & + if (!(ure_read_1(un, URE_PLA_CR, URE_MCU_TYPE_PLA) & URE_CR_RST)) break; - usbd_delay_ms(sc->ure_udev, 10); + usbd_delay_ms(un->un_udev, 10); } if (i == URE_TIMEOUT) - URE_PRINTF(sc, "reset never completed\n"); + URE_PRINTF(un, "reset never completed\n"); } static int ure_init_locked(struct ifnet *ifp) { - struct ure_softc * const sc = ifp->if_softc; - struct ure_chain *c; - usbd_status err; - int i; + struct usbnet * const un = ifp->if_softc; uint8_t eaddr[8]; - KASSERT(mutex_owned(&sc->ure_lock)); + usbnet_isowned(un); - if (sc->ure_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O. */ if (ifp->if_flags & IFF_RUNNING) - ure_stop_locked(ifp, 1); + usbnet_stop(un, ifp, 1); /* Set MAC address. */ memset(eaddr, 0, sizeof(eaddr)); memcpy(eaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN); - ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_CONFIG); - ure_write_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA | URE_BYTE_EN_SIX_BYTES, + ure_write_1(un, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_CONFIG); + ure_write_mem(un, URE_PLA_IDR, URE_MCU_TYPE_PLA | URE_BYTE_EN_SIX_BYTES, eaddr, 8); - ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML); + ure_write_1(un, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML); /* Reset the packet filter. */ - ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) & + ure_write_2(un, URE_PLA_FMC, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_FMC, URE_MCU_TYPE_PLA) & ~URE_FMC_FCR_MCU_EN); - ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) | + ure_write_2(un, URE_PLA_FMC, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_FMC, URE_MCU_TYPE_PLA) | URE_FMC_FCR_MCU_EN); /* Enable transmit and receive. */ - ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, - ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) | URE_CR_RE | + ure_write_1(un, URE_PLA_CR, URE_MCU_TYPE_PLA, + ure_read_1(un, URE_PLA_CR, URE_MCU_TYPE_PLA) | URE_CR_RE | URE_CR_TE); - ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) & + ure_write_2(un, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) & ~URE_RXDY_GATED_EN); /* Load the multicast filter. */ - ure_iff_locked(sc); + ure_setiff_locked(un); - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_RX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->ure_ep[URE_ENDPT_RX]); - if (err) { - URE_PRINTF(sc, "open rx pipe failed: %s\n", usbd_errstr(err)); - return EIO; - } - - err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_TX], - USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->ure_ep[URE_ENDPT_TX]); - if (err) { - URE_PRINTF(sc, "open tx pipe failed: %s\n", usbd_errstr(err)); - return EIO; - } - - if (ure_rx_list_init(sc)) { - URE_PRINTF(sc, "rx list init failed\n"); - return ENOBUFS; - } - - if (ure_tx_list_init(sc)) { - URE_PRINTF(sc, "tx list init failed\n"); - return ENOBUFS; - } - - mutex_enter(&sc->ure_rxlock); - mutex_enter(&sc->ure_txlock); - sc->ure_stopping = false; - - /* Start up the receive pipe. */ - for (i = 0; i < URE_RX_LIST_CNT; i++) { - c = &sc->ure_cdata.rx_chain[i]; - usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, sc->ure_bufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof); - usbd_transfer(c->uc_xfer); - } - - mutex_exit(&sc->ure_txlock); - mutex_exit(&sc->ure_rxlock); - - /* Indicate we are up and running. */ - KASSERT(IFNET_LOCKED(ifp)); - ifp->if_flags |= IFF_RUNNING; - - callout_reset(&sc->ure_stat_ch, hz, ure_tick, sc); - - return 0; + return usbnet_init_rx_tx(un); } static int ure_init(struct ifnet *ifp) { - struct ure_softc * const sc = ifp->if_softc; + struct usbnet * const un = ifp->if_softc; - mutex_enter(&sc->ure_lock); + usbnet_lock(un); int ret = ure_init_locked(ifp); - mutex_exit(&sc->ure_lock); + usbnet_unlock(un); return ret; } static void -ure_start_locked(struct ifnet *ifp) -{ - struct ure_softc *sc = ifp->if_softc; - struct mbuf *m; - struct ure_cdata *cd = &sc->ure_cdata; - int idx; - - KASSERT(cd->tx_cnt <= URE_TX_LIST_CNT); - - if (sc->ure_dying || sc->ure_stopping || - (sc->ure_flags & URE_FLAG_LINK) == 0 || - (ifp->if_flags & IFF_RUNNING) == 0 || - cd->tx_cnt == URE_TX_LIST_CNT) { - return; - } - - idx = cd->tx_prod; - while (cd->tx_cnt < URE_TX_LIST_CNT) { - IFQ_POLL(&ifp->if_snd, m); - if (m == NULL) - break; - - if (ure_encap(sc, m, idx)) { - ifp->if_oerrors++; - break; - } - IFQ_DEQUEUE(&ifp->if_snd, m); - - bpf_mtap(ifp, m, BPF_D_OUT); - m_freem(m); - - idx = (idx + 1) % URE_TX_LIST_CNT; - cd->tx_cnt++; - } - cd->tx_prod = idx; -} - -static void -ure_start(struct ifnet *ifp) -{ - struct ure_softc * const sc = ifp->if_softc; - - mutex_enter(&sc->ure_txlock); - ure_start_locked(ifp); - mutex_exit(&sc->ure_txlock); -} - -static void -ure_tick(void *xsc) -{ - struct ure_softc *sc = xsc; - - if (sc == NULL) - return; - - mutex_enter(&sc->ure_lock); - if (!sc->ure_stopping && !sc->ure_dying) { - /* Perform periodic stuff in process context */ - usb_add_task(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER); - } - mutex_exit(&sc->ure_lock); -} - -static void -ure_stop_locked(struct ifnet *ifp, int disable __unused) -{ - struct ure_softc *sc = ifp->if_softc; - struct ure_chain *c; - usbd_status err; - int i; - - KASSERT(mutex_owned(&sc->ure_lock)); - mutex_enter(&sc->ure_rxlock); - mutex_enter(&sc->ure_txlock); - sc->ure_stopping = true; - mutex_exit(&sc->ure_txlock); - mutex_exit(&sc->ure_rxlock); - - ure_reset(sc); - - /* - * XXXSMP Would like to - * KASSERT(IFNET_LOCKED(ifp)) - * here but the locking order is: - * ifnet -> sc lock -> rxlock -> txlock - * and sc lock is already held. - */ - ifp->if_flags &= ~IFF_RUNNING; - - callout_stop(&sc->ure_stat_ch); - - sc->ure_flags &= ~URE_FLAG_LINK; /* XXX */ - - if (sc->ure_ep[URE_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]); - if (err) - URE_PRINTF(sc, "abort rx pipe failed: %s\n", - usbd_errstr(err)); - } - - if (sc->ure_ep[URE_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]); - if (err) - URE_PRINTF(sc, "abort tx pipe failed: %s\n", - usbd_errstr(err)); - } - - for (i = 0; i < URE_RX_LIST_CNT; i++) { - c = &sc->ure_cdata.rx_chain[i]; - if (c->uc_xfer != NULL) { - usbd_destroy_xfer(c->uc_xfer); - c->uc_xfer = NULL; - } - } - - for (i = 0; i < URE_TX_LIST_CNT; i++) { - c = &sc->ure_cdata.tx_chain[i]; - if (c->uc_xfer != NULL) { - usbd_destroy_xfer(c->uc_xfer); - c->uc_xfer = NULL; - } - } - - if (sc->ure_ep[URE_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_RX]); - if (err) - URE_PRINTF(sc, "close rx pipe failed: %s\n", - usbd_errstr(err)); - sc->ure_ep[URE_ENDPT_RX] = NULL; - } - - if (sc->ure_ep[URE_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_TX]); - if (err) - URE_PRINTF(sc, "close tx pipe failed: %s\n", - usbd_errstr(err)); - sc->ure_ep[URE_ENDPT_TX] = NULL; - } -} - -static void -ure_stop(struct ifnet *ifp, int disable __unused) +ure_stop_cb(struct ifnet *ifp, int disable __unused) { - struct ure_softc * const sc = ifp->if_softc; + struct usbnet * const un = ifp->if_softc; - mutex_enter(&sc->ure_lock); - ure_stop_locked(ifp, disable); - mutex_exit(&sc->ure_lock); + ure_reset(un); } static void -ure_rtl8152_init(struct ure_softc *sc) +ure_rtl8152_init(struct usbnet *un) { uint32_t pwrctrl; /* Disable ALDPS. */ - ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA | + ure_ocp_reg_write(un, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA | URE_DIS_SDSAVE); - usbd_delay_ms(sc->ure_udev, 20); + usbd_delay_ms(un->un_udev, 20); - if (sc->ure_chip & URE_CHIP_VER_4C00) { - ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) & + if (un->un_flags & URE_FLAG_VER_4C00) { + ure_write_2(un, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) & ~URE_LED_MODE_MASK); } - ure_write_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB) & ~URE_POWER_CUT); - ure_write_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB) & ~URE_RESUME_INDICATE); - ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) | + ure_write_2(un, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) | URE_TX_10M_IDLE_EN | URE_PFM_PWM_SWITCH); - pwrctrl = ure_read_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA); + pwrctrl = ure_read_4(un, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA); pwrctrl &= ~URE_MCU_CLK_RATIO_MASK; pwrctrl |= URE_MCU_CLK_RATIO | URE_D3_CLK_GATED_EN; - ure_write_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, pwrctrl); - ure_write_2(sc, URE_PLA_GPHY_INTR_IMR, URE_MCU_TYPE_PLA, + ure_write_4(un, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, pwrctrl); + ure_write_2(un, URE_PLA_GPHY_INTR_IMR, URE_MCU_TYPE_PLA, URE_GPHY_STS_MSK | URE_SPEED_DOWN_MSK | URE_SPDWN_RXDV_MSK | URE_SPDWN_LINKCHG_MSK); /* Enable Rx aggregation. */ - ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_USB_CTRL, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) & ~URE_RX_AGG_DISABLE); /* Disable ALDPS. */ - ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA | + ure_ocp_reg_write(un, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA | URE_DIS_SDSAVE); - usbd_delay_ms(sc->ure_udev, 20); + usbd_delay_ms(un->un_udev, 20); - ure_init_fifo(sc); + ure_init_fifo(un); - ure_write_1(sc, URE_USB_TX_AGG, URE_MCU_TYPE_USB, + ure_write_1(un, URE_USB_TX_AGG, URE_MCU_TYPE_USB, URE_TX_AGG_MAX_THRESHOLD); - ure_write_4(sc, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB, URE_RX_THR_HIGH); - ure_write_4(sc, URE_USB_TX_DMA, URE_MCU_TYPE_USB, + ure_write_4(un, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB, URE_RX_THR_HIGH); + ure_write_4(un, URE_USB_TX_DMA, URE_MCU_TYPE_USB, URE_TEST_MODE_DISABLE | URE_TX_SIZE_ADJUST1); } static void -ure_rtl8153_init(struct ure_softc *sc) +ure_rtl8153_init(struct usbnet *un) { uint16_t val; uint8_t u1u2[8]; int i; /* Disable ALDPS. */ - ure_ocp_reg_write(sc, URE_OCP_POWER_CFG, - ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS); - usbd_delay_ms(sc->ure_udev, 20); + ure_ocp_reg_write(un, URE_OCP_POWER_CFG, + ure_ocp_reg_read(un, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS); + usbd_delay_ms(un->un_udev, 20); memset(u1u2, 0x00, sizeof(u1u2)); - ure_write_mem(sc, URE_USB_TOLERANCE, + ure_write_mem(un, URE_USB_TOLERANCE, URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2)); for (i = 0; i < URE_TIMEOUT; i++) { - if (ure_read_2(sc, URE_PLA_BOOT_CTRL, URE_MCU_TYPE_PLA) & + if (ure_read_2(un, URE_PLA_BOOT_CTRL, URE_MCU_TYPE_PLA) & URE_AUTOLOAD_DONE) break; - usbd_delay_ms(sc->ure_udev, 10); + usbd_delay_ms(un->un_udev, 10); } if (i == URE_TIMEOUT) - URE_PRINTF(sc, "timeout waiting for chip autoload\n"); + URE_PRINTF(un, "timeout waiting for chip autoload\n"); for (i = 0; i < URE_TIMEOUT; i++) { - val = ure_ocp_reg_read(sc, URE_OCP_PHY_STATUS) & + val = ure_ocp_reg_read(un, URE_OCP_PHY_STATUS) & URE_PHY_STAT_MASK; if (val == URE_PHY_STAT_LAN_ON || val == URE_PHY_STAT_PWRDN) break; - usbd_delay_ms(sc->ure_udev, 10); + usbd_delay_ms(un->un_udev, 10); } if (i == URE_TIMEOUT) - URE_PRINTF(sc, "timeout waiting for phy to stabilize\n"); + URE_PRINTF(un, "timeout waiting for phy to stabilize\n"); - ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB) & ~URE_U2P3_ENABLE); - if (sc->ure_chip & URE_CHIP_VER_5C10) { - val = ure_read_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB); + if (un->un_flags & URE_FLAG_VER_5C10) { + val = ure_read_2(un, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB); val &= ~URE_PWD_DN_SCALE_MASK; val |= URE_PWD_DN_SCALE(96); - ure_write_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB, val); + ure_write_2(un, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB, val); - ure_write_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB, - ure_read_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB) | + ure_write_1(un, URE_USB_USB2PHY, URE_MCU_TYPE_USB, + ure_read_1(un, URE_USB_USB2PHY, URE_MCU_TYPE_USB) | URE_USB2PHY_L1 | URE_USB2PHY_SUSPEND); - } else if (sc->ure_chip & URE_CHIP_VER_5C20) { - ure_write_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA, - ure_read_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA) & + } else if (un->un_flags & URE_FLAG_VER_5C20) { + ure_write_1(un, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA, + ure_read_1(un, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA) & ~URE_ECM_ALDPS); } - if (sc->ure_chip & (URE_CHIP_VER_5C20 | URE_CHIP_VER_5C30)) { - val = ure_read_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB); - if (ure_read_2(sc, URE_USB_BURST_SIZE, URE_MCU_TYPE_USB) == + if (un->un_flags & (URE_FLAG_VER_5C20 | URE_FLAG_VER_5C30)) { + val = ure_read_1(un, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB); + if (ure_read_2(un, URE_USB_BURST_SIZE, URE_MCU_TYPE_USB) == 0) val &= ~URE_DYNAMIC_BURST; else val |= URE_DYNAMIC_BURST; - ure_write_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB, val); + ure_write_1(un, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB, val); } - ure_write_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB, - ure_read_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB) | + ure_write_1(un, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB, + ure_read_1(un, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB) | URE_EP4_FULL_FC); - ure_write_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB) & ~URE_TIMER11_EN); - ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) & + ure_write_2(un, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) & ~URE_LED_MODE_MASK); - if ((sc->ure_chip & URE_CHIP_VER_5C10) && - sc->ure_udev->ud_speed != USB_SPEED_SUPER) + if ((un->un_flags & URE_FLAG_VER_5C10) && + un->un_udev->ud_speed != USB_SPEED_SUPER) val = URE_LPM_TIMER_500MS; else val = URE_LPM_TIMER_500US; - ure_write_1(sc, URE_USB_LPM_CTRL, URE_MCU_TYPE_USB, + ure_write_1(un, URE_USB_LPM_CTRL, URE_MCU_TYPE_USB, val | URE_FIFO_EMPTY_1FB | URE_ROK_EXIT_LPM); - val = ure_read_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB); + val = ure_read_2(un, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB); val &= ~URE_SEN_VAL_MASK; val |= URE_SEN_VAL_NORMAL | URE_SEL_RXIDLE; - ure_write_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB, val); + ure_write_2(un, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB, val); - ure_write_2(sc, URE_USB_CONNECT_TIMER, URE_MCU_TYPE_USB, 0x0001); + ure_write_2(un, URE_USB_CONNECT_TIMER, URE_MCU_TYPE_USB, 0x0001); - ure_write_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_POWER_CUT, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_POWER_CUT, URE_MCU_TYPE_USB) & ~(URE_PWR_EN | URE_PHASE2_EN)); - ure_write_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_MISC_0, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_MISC_0, URE_MCU_TYPE_USB) & ~URE_PCUT_STATUS); memset(u1u2, 0xff, sizeof(u1u2)); - ure_write_mem(sc, URE_USB_TOLERANCE, + ure_write_mem(un, URE_USB_TOLERANCE, URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2)); - ure_write_2(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, + ure_write_2(un, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, URE_ALDPS_SPDWN_RATIO); - ure_write_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA, + ure_write_2(un, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA, URE_EEE_SPDWN_RATIO); - ure_write_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA, + ure_write_2(un, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA, URE_PKT_AVAIL_SPDWN_EN | URE_SUSPEND_SPDWN_EN | URE_U1U2_SPDWN_EN | URE_L1_SPDWN_EN); - ure_write_2(sc, URE_PLA_MAC_PWR_CTRL4, URE_MCU_TYPE_PLA, + ure_write_2(un, URE_PLA_MAC_PWR_CTRL4, URE_MCU_TYPE_PLA, URE_PWRSAVE_SPDWN_EN | URE_RXDV_SPDWN_EN | URE_TX10MIDLE_EN | URE_TP100_SPDWN_EN | URE_TP500_SPDWN_EN | URE_TP1000_SPDWN_EN | URE_EEE_SPDWN_EN); - val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB); - if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10))) + val = ure_read_2(un, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB); + if (!(un->un_flags & (URE_FLAG_VER_5C00 | URE_FLAG_VER_5C10))) val |= URE_U2P3_ENABLE; else val &= ~URE_U2P3_ENABLE; - ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val); + ure_write_2(un, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val); memset(u1u2, 0x00, sizeof(u1u2)); - ure_write_mem(sc, URE_USB_TOLERANCE, + ure_write_mem(un, URE_USB_TOLERANCE, URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2)); /* Disable ALDPS. */ - ure_ocp_reg_write(sc, URE_OCP_POWER_CFG, - ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS); - usbd_delay_ms(sc->ure_udev, 20); + ure_ocp_reg_write(un, URE_OCP_POWER_CFG, + ure_ocp_reg_read(un, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS); + usbd_delay_ms(un->un_udev, 20); - ure_init_fifo(sc); + ure_init_fifo(un); /* Enable Rx aggregation. */ - ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB, - ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) & + ure_write_2(un, URE_USB_USB_CTRL, URE_MCU_TYPE_USB, + ure_read_2(un, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) & ~URE_RX_AGG_DISABLE); - val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB); - if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10))) + val = ure_read_2(un, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB); + if (!(un->un_flags & (URE_FLAG_VER_5C00 | URE_FLAG_VER_5C10))) val |= URE_U2P3_ENABLE; else val &= ~URE_U2P3_ENABLE; - ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val); + ure_write_2(un, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val); memset(u1u2, 0xff, sizeof(u1u2)); - ure_write_mem(sc, URE_USB_TOLERANCE, + ure_write_mem(un, URE_USB_TOLERANCE, URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2)); } static void -ure_disable_teredo(struct ure_softc *sc) +ure_disable_teredo(struct usbnet *un) { - - ure_write_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA, - ure_read_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA) & + ure_write_4(un, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA, + ure_read_4(un, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA) & ~(URE_TEREDO_SEL | URE_TEREDO_RS_EVENT_MASK | URE_OOB_TEREDO_EN)); - ure_write_2(sc, URE_PLA_WDT6_CTRL, URE_MCU_TYPE_PLA, + ure_write_2(un, URE_PLA_WDT6_CTRL, URE_MCU_TYPE_PLA, URE_WDT6_SET_MODE); - ure_write_2(sc, URE_PLA_REALWOW_TIMER, URE_MCU_TYPE_PLA, 0); - ure_write_4(sc, URE_PLA_TEREDO_TIMER, URE_MCU_TYPE_PLA, 0); + ure_write_2(un, URE_PLA_REALWOW_TIMER, URE_MCU_TYPE_PLA, 0); + ure_write_4(un, URE_PLA_TEREDO_TIMER, URE_MCU_TYPE_PLA, 0); } static void -ure_init_fifo(struct ure_softc *sc) +ure_init_fifo(struct usbnet *un) { uint32_t rx_fifo1, rx_fifo2; int i; - ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) | + ure_write_2(un, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) | URE_RXDY_GATED_EN); - ure_disable_teredo(sc); + ure_disable_teredo(un); - ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, - ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA) & + ure_write_4(un, URE_PLA_RCR, URE_MCU_TYPE_PLA, + ure_read_4(un, URE_PLA_RCR, URE_MCU_TYPE_PLA) & ~URE_RCR_ACPT_ALL); - if (!(sc->ure_flags & URE_FLAG_8152)) { - if (sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10 | - URE_CHIP_VER_5C20)) - ure_ocp_reg_write(sc, URE_OCP_ADC_CFG, + if (!(un->un_flags & URE_FLAG_8152)) { + if (un->un_flags & (URE_FLAG_VER_5C00 | URE_FLAG_VER_5C10 | + URE_FLAG_VER_5C20)) + ure_ocp_reg_write(un, URE_OCP_ADC_CFG, URE_CKADSEL_L | URE_ADC_EN | URE_EN_EMI_L); - if (sc->ure_chip & URE_CHIP_VER_5C00) - ure_ocp_reg_write(sc, URE_OCP_EEE_CFG, - ure_ocp_reg_read(sc, URE_OCP_EEE_CFG) & + if (un->un_flags & URE_FLAG_VER_5C00) + ure_ocp_reg_write(un, URE_OCP_EEE_CFG, + ure_ocp_reg_read(un, URE_OCP_EEE_CFG) & ~URE_CTAP_SHORT_EN); - ure_ocp_reg_write(sc, URE_OCP_POWER_CFG, - ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) | + ure_ocp_reg_write(un, URE_OCP_POWER_CFG, + ure_ocp_reg_read(un, URE_OCP_POWER_CFG) | URE_EEE_CLKDIV_EN); - ure_ocp_reg_write(sc, URE_OCP_DOWN_SPEED, - ure_ocp_reg_read(sc, URE_OCP_DOWN_SPEED) | + ure_ocp_reg_write(un, URE_OCP_DOWN_SPEED, + ure_ocp_reg_read(un, URE_OCP_DOWN_SPEED) | URE_EN_10M_BGOFF); - ure_ocp_reg_write(sc, URE_OCP_POWER_CFG, - ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) | + ure_ocp_reg_write(un, URE_OCP_POWER_CFG, + ure_ocp_reg_read(un, URE_OCP_POWER_CFG) | URE_EN_10M_PLLOFF); - ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_IMPEDANCE); - ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0b13); - ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) | + ure_ocp_reg_write(un, URE_OCP_SRAM_ADDR, URE_SRAM_IMPEDANCE); + ure_ocp_reg_write(un, URE_OCP_SRAM_DATA, 0x0b13); + ure_write_2(un, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) | URE_PFM_PWM_SWITCH); /* Enable LPF corner auto tune. */ - ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_LPF_CFG); - ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0xf70f); + ure_ocp_reg_write(un, URE_OCP_SRAM_ADDR, URE_SRAM_LPF_CFG); + ure_ocp_reg_write(un, URE_OCP_SRAM_DATA, 0xf70f); /* Adjust 10M amplitude. */ - ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP1); - ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x00af); - ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP2); - ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0208); + ure_ocp_reg_write(un, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP1); + ure_ocp_reg_write(un, URE_OCP_SRAM_DATA, 0x00af); + ure_ocp_reg_write(un, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP2); + ure_ocp_reg_write(un, URE_OCP_SRAM_DATA, 0x0208); } - ure_reset(sc); + ure_reset(un); - ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, 0); + ure_write_1(un, URE_PLA_CR, URE_MCU_TYPE_PLA, 0); - ure_write_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA, - ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) & + ure_write_1(un, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA, + ure_read_1(un, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) & ~URE_NOW_IS_OOB); - ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) & + ure_write_2(un, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) & ~URE_MCU_BORW_EN); for (i = 0; i < URE_TIMEOUT; i++) { - if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) & + if (ure_read_1(un, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) & URE_LINK_LIST_READY) break; - usbd_delay_ms(sc->ure_udev, 10); + usbd_delay_ms(un->un_udev, 10); } if (i == URE_TIMEOUT) - URE_PRINTF(sc, "timeout waiting for OOB control\n"); - ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) | + URE_PRINTF(un, "timeout waiting for OOB control\n"); + ure_write_2(un, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) | URE_RE_INIT_LL); for (i = 0; i < URE_TIMEOUT; i++) { - if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) & + if (ure_read_1(un, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) & URE_LINK_LIST_READY) break; - usbd_delay_ms(sc->ure_udev, 10); + usbd_delay_ms(un->un_udev, 10); } if (i == URE_TIMEOUT) - URE_PRINTF(sc, "timeout waiting for OOB control\n"); + URE_PRINTF(un, "timeout waiting for OOB control\n"); - ure_write_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA) & + ure_write_2(un, URE_PLA_CPCR, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_CPCR, URE_MCU_TYPE_PLA) & ~URE_CPCR_RX_VLAN); - ure_write_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA, - ure_read_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA) | + ure_write_2(un, URE_PLA_TCR0, URE_MCU_TYPE_PLA, + ure_read_2(un, URE_PLA_TCR0, URE_MCU_TYPE_PLA) | URE_TCR0_AUTO_FIFO); /* Configure Rx FIFO threshold and coalescing. */ - ure_write_4(sc, URE_PLA_RXFIFO_CTRL0, URE_MCU_TYPE_PLA, + ure_write_4(un, URE_PLA_RXFIFO_CTRL0, URE_MCU_TYPE_PLA, URE_RXFIFO_THR1_NORMAL); - if (sc->ure_udev->ud_speed == USB_SPEED_FULL) { + if (un->un_udev->ud_speed == USB_SPEED_FULL) { rx_fifo1 = URE_RXFIFO_THR2_FULL; rx_fifo2 = URE_RXFIFO_THR3_FULL; } else { rx_fifo1 = URE_RXFIFO_THR2_HIGH; rx_fifo2 = URE_RXFIFO_THR3_HIGH; } - ure_write_4(sc, URE_PLA_RXFIFO_CTRL1, URE_MCU_TYPE_PLA, rx_fifo1); - ure_write_4(sc, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA, rx_fifo2); + ure_write_4(un, URE_PLA_RXFIFO_CTRL1, URE_MCU_TYPE_PLA, rx_fifo1); + ure_write_4(un, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA, rx_fifo2); /* Configure Tx FIFO threshold. */ - ure_write_4(sc, URE_PLA_TXFIFO_CTRL, URE_MCU_TYPE_PLA, + ure_write_4(un, URE_PLA_TXFIFO_CTRL, URE_MCU_TYPE_PLA, URE_TXFIFO_THR_NORMAL); } -int -ure_ioctl(struct ifnet *ifp, u_long cmd, void *data) +static int +ure_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct ure_softc *sc = ifp->if_softc; - int error = 0, oflags = ifp->if_flags; + struct usbnet * const un = ifp->if_softc; switch (cmd) { - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, cmd, data)) != 0) - break; - switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { - case IFF_RUNNING: - ure_stop(ifp, 1); - break; - case IFF_UP: - ure_init(ifp); - break; - case IFF_UP | IFF_RUNNING: - if ((ifp->if_flags ^ oflags) == IFF_PROMISC) - ure_iff(sc); - else - ure_init(ifp); - } + case SIOCADDMULTI: + case SIOCDELMULTI: + ure_setiff(un); break; default: - if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) - break; - error = 0; - if ((ifp->if_flags & IFF_RUNNING) == 0) - break; - switch (cmd) { - case SIOCADDMULTI: - case SIOCDELMULTI: - ure_iff(sc); - break; - default: - break; - } + break; } - return error; + return 0; } static int @@ -1149,13 +838,12 @@ ure_match(device_t parent, cfdata_t matc static void ure_attach(device_t parent, device_t self, void *aux) { - struct ure_softc *sc = device_private(self); + USBNET_MII_DECL_DEFAULT(unm); + struct usbnet * const un = device_private(self); struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; - struct ifnet *ifp; - struct mii_data *mii; int error, i; uint16_t ver; uint8_t eaddr[8]; /* 2byte padded */ @@ -1163,28 +851,20 @@ ure_attach(device_t parent, device_t sel aprint_naive("\n"); aprint_normal("\n"); - - sc->ure_dev = self; - sc->ure_udev = dev; - - devinfop = usbd_devinfo_alloc(sc->ure_udev, 0); + devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); - callout_init(&sc->ure_stat_ch, CALLOUT_MPSAFE); - usb_init_task(&sc->ure_tick_task, ure_tick_task, sc, USB_TASKQ_MPSAFE); - mutex_init(&sc->ure_mii_lock, MUTEX_DEFAULT, IPL_NONE); - mutex_init(&sc->ure_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->ure_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); - mutex_init(&sc->ure_lock, MUTEX_DEFAULT, IPL_NONE); - cv_init(&sc->ure_detachcv, "uredet"); - - /* - * ure_phyno is set to 0 below when configuration has succeeded. - * if it is still -1 in detach, then ifmedia/mii/etc was not - * setup and should not be torn down. - */ - sc->ure_phyno = -1; + un->un_dev = self; + un->un_udev = dev; + un->un_sc = un; + un->un_ops = &ure_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = URE_RX_LIST_CNT; + un->un_tx_list_cnt = URE_TX_LIST_CNT; + un->un_rx_bufsz = URE_BUFSZ; + un->un_tx_bufsz = URE_BUFSZ; #define URE_CONFIG_NO 1 /* XXX */ error = usbd_set_config_no(dev, URE_CONFIG_NO, 1); @@ -1195,90 +875,82 @@ ure_attach(device_t parent, device_t sel } if (uaa->uaa_product == USB_PRODUCT_REALTEK_RTL8152) - sc->ure_flags |= URE_FLAG_8152; + un->un_flags |= URE_FLAG_8152; #define URE_IFACE_IDX 0 /* XXX */ - error = usbd_device2interface_handle(dev, URE_IFACE_IDX, &sc->ure_iface); + error = usbd_device2interface_handle(dev, URE_IFACE_IDX, &un->un_iface); if (error) { aprint_error_dev(self, "failed to get interface handle: %s\n", usbd_errstr(error)); return; /* XXX */ } - sc->ure_bufsz = 16 * 1024; - - id = usbd_get_interface_descriptor(sc->ure_iface); + id = usbd_get_interface_descriptor(un->un_iface); for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->ure_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; /* XXX */ } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->ure_ed[URE_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->ure_ed[URE_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } } - sc->ure_phyno = 0; + /* Set these up now for ure_ctl(). */ + usbnet_attach(un, "uredet"); - ver = ure_read_2(sc, URE_PLA_TCR1, URE_MCU_TYPE_PLA) & URE_VERSION_MASK; + un->un_phyno = 0; + + ver = ure_read_2(un, URE_PLA_TCR1, URE_MCU_TYPE_PLA) & URE_VERSION_MASK; switch (ver) { case 0x4c00: - sc->ure_chip |= URE_CHIP_VER_4C00; + un->un_flags |= URE_FLAG_VER_4C00; break; case 0x4c10: - sc->ure_chip |= URE_CHIP_VER_4C10; + un->un_flags |= URE_FLAG_VER_4C10; break; case 0x5c00: - sc->ure_chip |= URE_CHIP_VER_5C00; + un->un_flags |= URE_FLAG_VER_5C00; break; case 0x5c10: - sc->ure_chip |= URE_CHIP_VER_5C10; + un->un_flags |= URE_FLAG_VER_5C10; break; case 0x5c20: - sc->ure_chip |= URE_CHIP_VER_5C20; + un->un_flags |= URE_FLAG_VER_5C20; break; case 0x5c30: - sc->ure_chip |= URE_CHIP_VER_5C30; + un->un_flags |= URE_FLAG_VER_5C30; break; default: /* fake addr? or just fail? */ break; } aprint_normal_dev(self, "RTL%d %sver %04x\n", - (sc->ure_flags & URE_FLAG_8152) ? 8152 : 8153, - (sc->ure_chip != 0) ? "" : "unknown ", + (un->un_flags & URE_FLAG_8152) ? 8152 : 8153, + (un->un_flags != 0) ? "" : "unknown ", ver); - mutex_enter(&sc->ure_lock); - if (sc->ure_flags & URE_FLAG_8152) - ure_rtl8152_init(sc); + usbnet_lock(un); + if (un->un_flags & URE_FLAG_8152) + ure_rtl8152_init(un); else - ure_rtl8153_init(sc); + ure_rtl8153_init(un); - if (sc->ure_chip & URE_CHIP_VER_4C00) - ure_read_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA, eaddr, + if (un->un_flags & URE_FLAG_VER_4C00) + ure_read_mem(un, URE_PLA_IDR, URE_MCU_TYPE_PLA, eaddr, sizeof(eaddr)); else - ure_read_mem(sc, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr, + ure_read_mem(un, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr, sizeof(eaddr)); - mutex_exit(&sc->ure_lock); - - aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); + usbnet_unlock(un); + memcpy(un->un_eaddr, eaddr, sizeof un->un_eaddr); - ifp = GET_IFP(sc); - ifp->if_softc = sc; - strlcpy(ifp->if_xname, device_xname(sc->ure_dev), IFNAMSIZ); - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_extflags = IFEF_MPSAFE; - ifp->if_init = ure_init; - ifp->if_ioctl = ure_ioctl; - ifp->if_start = ure_start; - ifp->if_stop = ure_stop; + struct ifnet *ifp = usbnet_ifp(un); /* * We don't support TSOv4 and v6 for now, that are required to @@ -1289,285 +961,65 @@ ure_attach(device_t parent, device_t sel #ifdef INET6 ifp->if_capabilities |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx; #endif - if (sc->ure_chip & ~URE_CHIP_VER_4C00) { + if (un->un_flags & ~URE_FLAG_VER_4C00) { ifp->if_capabilities |= IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx | IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx; } - sc->ure_ec.ec_capabilities = ETHERCAP_VLAN_MTU; + struct ethercom *ec = usbnet_ec(un); + ec->ec_capabilities = ETHERCAP_VLAN_MTU; #ifdef notyet - sc->ure_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU; + ec->ec_capabilities |= ETHERCAP_JUMBO_MTU; #endif - IFQ_SET_READY(&ifp->if_snd); - - mii = GET_MII(sc); - mii->mii_ifp = ifp; - mii->mii_readreg = ure_miibus_readreg; - mii->mii_writereg = ure_miibus_writereg; - mii->mii_statchg = ure_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - - sc->ure_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, ure_ifmedia_upd, ure_ifmedia_sts); - mii_attach(self, mii, 0xffffffff, sc->ure_phyno, MII_OFFSET_ANY, 0); - - if (LIST_FIRST(&mii->mii_phys) == NULL) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - - if_attach(ifp); - ether_ifattach(ifp, eaddr); - - rnd_attach_source(&sc->ure_rnd_source, device_xname(sc->ure_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->ure_udev, sc->ure_dev); - - if (!pmf_device_register(self, NULL, NULL)) - aprint_error_dev(self, "couldn't establish power handler\n"); -} - -static int -ure_detach(device_t self, int flags) -{ - struct ure_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - - pmf_device_deregister(self); - - mutex_enter(&sc->ure_lock); - sc->ure_dying = true; - mutex_exit(&sc->ure_lock); - - callout_halt(&sc->ure_stat_ch, NULL); - - usb_rem_task_wait(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER, - NULL); - - if (sc->ure_ep[URE_ENDPT_TX] != NULL) - usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]); - if (sc->ure_ep[URE_ENDPT_RX] != NULL) - usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]); - - mutex_enter(&sc->ure_lock); - sc->ure_refcnt--; - while (sc->ure_refcnt > 0) { - /* Wait for processes to go away */ - cv_wait(&sc->ure_detachcv, &sc->ure_lock); - } - mutex_exit(&sc->ure_lock); - - /* partial-attach, below items weren't configured. */ - if (sc->ure_phyno != -1) { - if (ifp->if_flags & IFF_RUNNING) { - IFNET_LOCK(ifp); - ure_stop(ifp, 1); - IFNET_UNLOCK(ifp); - } - - rnd_detach_source(&sc->ure_rnd_source); - mii_detach(&sc->ure_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->ure_mii.mii_media, IFM_INST_ANY); - if (ifp->if_softc != NULL) { - ether_ifdetach(ifp); - if_detach(ifp); - } - } - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->ure_udev, sc->ure_dev); - - callout_destroy(&sc->ure_stat_ch); - cv_destroy(&sc->ure_detachcv); - mutex_destroy(&sc->ure_lock); - mutex_destroy(&sc->ure_rxlock); - mutex_destroy(&sc->ure_txlock); - mutex_destroy(&sc->ure_mii_lock); - - return 0; -} - -static int -ure_activate(device_t self, enum devact act) -{ - struct ure_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(ifp); - - mutex_enter(&sc->ure_lock); - sc->ure_dying = true; - mutex_exit(&sc->ure_lock); - - mutex_enter(&sc->ure_rxlock); - mutex_enter(&sc->ure_txlock); - sc->ure_stopping = true; - mutex_exit(&sc->ure_txlock); - mutex_exit(&sc->ure_rxlock); - - return 0; - default: - return EOPNOTSUPP; - } - return 0; -} - -static void -ure_tick_task(void *xsc) -{ - struct ure_softc *sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - - if (sc == NULL) - return; - - mutex_enter(&sc->ure_lock); - if (sc->ure_stopping || sc->ure_dying) { - mutex_exit(&sc->ure_lock); - return; - } - - ifp = GET_IFP(sc); - mii = GET_MII(sc); - if (mii == NULL) { - mutex_exit(&sc->ure_lock); - return; - } - - sc->ure_refcnt++; - mutex_exit(&sc->ure_lock); - - mii_tick(mii); - - if ((sc->ure_flags & URE_FLAG_LINK) == 0) - ure_miibus_statchg(ifp); - - mutex_enter(&sc->ure_lock); - if (--sc->ure_refcnt < 0) - cv_broadcast(&sc->ure_detachcv); - if (!sc->ure_stopping && !sc->ure_dying) - callout_schedule(&sc->ure_stat_ch, hz); - mutex_exit(&sc->ure_lock); -} - -static void -ure_lock_mii(struct ure_softc *sc) -{ - - mutex_enter(&sc->ure_lock); - sc->ure_refcnt++; - mutex_exit(&sc->ure_lock); - - mutex_enter(&sc->ure_mii_lock); -} - -static void -ure_unlock_mii(struct ure_softc *sc) -{ - - mutex_exit(&sc->ure_mii_lock); - mutex_enter(&sc->ure_lock); - if (--sc->ure_refcnt < 0) - cv_broadcast(&sc->ure_detachcv); - mutex_exit(&sc->ure_lock); + unm.un_mii_phyloc = un->un_phyno; + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, &unm); } static void -ure_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +ure_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct ure_chain *c = (struct ure_chain *)priv; - struct ure_softc *sc = c->uc_sc; - struct ifnet *ifp = GET_IFP(sc); - uint8_t *buf = c->uc_buf; - uint32_t total_len; - uint16_t pktlen = 0; - struct mbuf *m; + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; + uint16_t pkt_len = 0; + uint16_t pkt_count = 0; struct ure_rxpkt rxhdr; - mutex_enter(&sc->ure_rxlock); - - if (sc->ure_dying || sc->ure_stopping || - status == USBD_INVAL || status == USBD_NOT_STARTED || - status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) { - mutex_exit(&sc->ure_rxlock); - return; - } - - if (status != USBD_NORMAL_COMPLETION) { - if (usbd_ratecheck(&sc->ure_rx_notice)) - URE_PRINTF(sc, "usb errors on rx: %s\n", - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async( - sc->ure_ep[URE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - DPRINTFN(3, ("received %d bytes\n", total_len)); - - KASSERTMSG(total_len <= sc->ure_bufsz, "%u vs %u", - total_len, sc->ure_bufsz); + usbnet_isowned_rx(un); do { if (total_len < sizeof(rxhdr)) { DPRINTF(("too few bytes left for a packet header\n")); ifp->if_ierrors++; - goto done; + return; } - buf += roundup(pktlen, 8); + buf += roundup(pkt_len, 8); memcpy(&rxhdr, buf, sizeof(rxhdr)); total_len -= sizeof(rxhdr); - pktlen = le32toh(rxhdr.ure_pktlen) & URE_RXPKT_LEN_MASK; - DPRINTFN(4, ("next packet is %d bytes\n", pktlen)); - if (pktlen > total_len) { + pkt_len = le32toh(rxhdr.ure_pktlen) & URE_RXPKT_LEN_MASK; + DPRINTFN(4, ("next packet is %d bytes\n", pkt_len)); + if (pkt_len > total_len) { DPRINTF(("not enough bytes left for next packet\n")); ifp->if_ierrors++; - goto done; + return; } - total_len -= roundup(pktlen, 8); + total_len -= roundup(pkt_len, 8); buf += sizeof(rxhdr); - m = m_devget(buf, pktlen - ETHER_CRC_LEN, 0, ifp); - if (m == NULL) { - DPRINTF(("unable to allocate mbuf for next packet\n")); - ifp->if_ierrors++; - goto done; - } - - m->m_pkthdr.csum_flags = ure_rxcsum(ifp, &rxhdr); - - mutex_exit(&sc->ure_rxlock); - if_percpuq_enqueue(ifp->if_percpuq, m); - mutex_enter(&sc->ure_rxlock); + usbnet_enqueue(un, buf, pkt_len - ETHER_CRC_LEN, + ure_rxcsum(ifp, &rxhdr), 0, 0); - if (sc->ure_dying || sc->ure_stopping) { - mutex_exit(&sc->ure_rxlock); - return; - } + pkt_count++; } while (total_len > 0); -done: - if (sc->ure_dying || sc->ure_stopping) { - mutex_exit(&sc->ure_rxlock); - return; - } - mutex_exit(&sc->ure_rxlock); - - /* Setup new transfer. */ - usbd_setup_xfer(xfer, c, c->uc_buf, sc->ure_bufsz, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof); - usbd_transfer(xfer); + if (pkt_count) + rnd_add_uint32(usbnet_rndsrc(un), pkt_count); } static int @@ -1609,113 +1061,17 @@ ure_rxcsum(struct ifnet *ifp, struct ure return flags; } -static void -ure_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) -{ - struct ure_chain *c = priv; - struct ure_softc *sc = c->uc_sc; - struct ure_cdata *cd = &sc->ure_cdata; - struct ifnet *ifp = GET_IFP(sc); - - mutex_enter(&sc->ure_txlock); - if (sc->ure_stopping || sc->ure_dying) { - mutex_exit(&sc->ure_txlock); - return; - } - - DPRINTFN(2, ("tx completion\n")); - - KASSERT(cd->tx_cnt > 0); - cd->tx_cnt--; - - switch (status) { - case USBD_NOT_STARTED: - case USBD_CANCELLED: - break; - - case USBD_NORMAL_COMPLETION: - ifp->if_opackets++; - - if (!IFQ_IS_EMPTY(&ifp->if_snd)) { - ure_start_locked(ifp); - } - break; - - default: - ifp->if_oerrors++; - if (usbd_ratecheck(&sc->ure_tx_notice)) - URE_PRINTF(sc, "usb error on tx: %s\n", - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async( - sc->ure_ep[URE_ENDPT_TX]); - break; - } - - mutex_exit(&sc->ure_txlock); -} - -static int -ure_tx_list_init(struct ure_softc *sc) +static unsigned +ure_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - struct ure_cdata *cd; - struct ure_chain *c; - int i, error; - - cd = &sc->ure_cdata; - for (i = 0; i < URE_TX_LIST_CNT; i++) { - c = &cd->tx_chain[i]; - c->uc_sc = sc; - if (c->uc_xfer == NULL) { - error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_TX], - sc->ure_bufsz, USBD_FORCE_SHORT_XFER, 0, - &c->uc_xfer); - if (error) - return error; - c->uc_buf = usbd_get_buffer(c->uc_xfer); - } - } - - cd->tx_prod = cd->tx_cnt = 0; - - return 0; -} - -static int -ure_rx_list_init(struct ure_softc *sc) -{ - struct ure_cdata *cd; - struct ure_chain *c; - int i, error; - - cd = &sc->ure_cdata; - for (i = 0; i < URE_RX_LIST_CNT; i++) { - c = &cd->rx_chain[i]; - c->uc_sc = sc; - error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_RX], - sc->ure_bufsz, 0, 0, &c->uc_xfer); - if (error) - return error; - c->uc_buf = usbd_get_buffer(c->uc_xfer); - } - - return 0; -} - -static int -ure_encap(struct ure_softc *sc, struct mbuf *m, int idx) -{ - struct ifnet *ifp = GET_IFP(sc); - struct ure_chain *c; - usbd_status err; struct ure_txpkt txhdr; uint32_t frm_len = 0; - uint8_t *buf; + uint8_t *buf = c->unc_buf; - KASSERT(mutex_owned(&sc->ure_txlock)); + usbnet_isowned_tx(un); - c = &sc->ure_cdata.tx_chain[idx]; - buf = c->uc_buf; + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - sizeof(txhdr)) + return 0; /* header */ txhdr.ure_pktlen = htole32(m->m_pkthdr.len | URE_TXPKT_TX_FS | @@ -1729,21 +1085,9 @@ ure_encap(struct ure_softc *sc, struct m m_copydata(m, 0, m->m_pkthdr.len, buf); frm_len += m->m_pkthdr.len; - if (__predict_false(c->uc_xfer == NULL)) - return EIO; /* XXX plugged out or down */ - DPRINTFN(2, ("tx %d bytes\n", frm_len)); - usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, frm_len, - USBD_FORCE_SHORT_XFER, 10000, ure_txeof); - - err = usbd_transfer(c->uc_xfer); - if (err != USBD_IN_PROGRESS) { - /* XXXSMP IFNET_LOCK */ - ure_stop(ifp, 0); - return EIO; - } - return 0; + return frm_len; } /* @@ -1814,3 +1158,9 @@ ure_txcsum(struct mbuf *m) return reg; } + +#ifdef _MODULE +#include "ioconf.c" +#endif + +USBNET_MODULE(ure) Index: usb/if_urevar.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_urevar.h,v retrieving revision 1.3 diff -p -u -u -r1.3 if_urevar.h --- usb/if_urevar.h 23 Jun 2019 02:14:14 -0000 1.3 +++ usb/if_urevar.h 29 Aug 2019 06:22:05 -0000 @@ -72,10 +72,6 @@ struct ure_txpkt { #define URE_TXPKT_UDP_CS __BIT(31) } __packed; -#define URE_ENDPT_RX 0 -#define URE_ENDPT_TX 1 -#define URE_ENDPT_MAX 2 - #ifndef URE_TX_LIST_CNT #define URE_TX_LIST_CNT 4 #endif @@ -83,66 +79,11 @@ struct ure_txpkt { #define URE_RX_LIST_CNT 4 #endif -struct ure_chain { - struct ure_softc *uc_sc; - struct usbd_xfer *uc_xfer; - char *uc_buf; -}; - -struct ure_cdata { - struct ure_chain tx_chain[URE_TX_LIST_CNT]; - struct ure_chain rx_chain[URE_RX_LIST_CNT]; - int tx_prod; - int tx_cnt; -}; - -struct ure_softc { - device_t ure_dev; - struct usbd_device *ure_udev; - - /* usb */ - struct usbd_interface *ure_iface; - struct usb_task ure_tick_task; - int ure_ed[URE_ENDPT_MAX]; - struct usbd_pipe *ure_ep[URE_ENDPT_MAX]; - - /* ethernet */ - struct ethercom ure_ec; -#define GET_IFP(sc) (&(sc)->ure_ec.ec_if) - struct mii_data ure_mii; -#define GET_MII(sc) (&(sc)->ure_mii) - - kmutex_t ure_mii_lock; - kmutex_t ure_lock; - kmutex_t ure_rxlock; - kmutex_t ure_txlock; - kcondvar_t ure_detachcv; - int ure_refcnt; - - struct ure_cdata ure_cdata; - callout_t ure_stat_ch; - - struct timeval ure_rx_notice; - struct timeval ure_tx_notice; - u_int ure_bufsz; - - int ure_phyno; - - u_int ure_flags; -#define URE_FLAG_LINK 0x0001 +/* usbnet::un_flags values */ +#define URE_FLAG_VER_4C00 0x0001 +#define URE_FLAG_VER_4C10 0x0002 +#define URE_FLAG_VER_5C00 0x0004 +#define URE_FLAG_VER_5C10 0x0008 +#define URE_FLAG_VER_5C20 0x0010 +#define URE_FLAG_VER_5C30 0x0020 #define URE_FLAG_8152 0x1000 /* RTL8152 */ - - u_int ure_chip; -#define URE_CHIP_VER_4C00 0x01 -#define URE_CHIP_VER_4C10 0x02 -#define URE_CHIP_VER_5C00 0x04 -#define URE_CHIP_VER_5C10 0x08 -#define URE_CHIP_VER_5C20 0x10 -#define URE_CHIP_VER_5C30 0x20 - - krndsource_t ure_rnd_source; - - bool ure_dying; - bool ure_stopping; - bool ure_attached; -}; Index: usb/if_url.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_url.c,v retrieving revision 1.66 diff -p -u -u -r1.66 if_url.c --- usb/if_url.c 21 Jul 2019 10:27:56 -0000 1.66 +++ usb/if_url.c 29 Aug 2019 06:22:05 -0000 @@ -52,21 +52,6 @@ __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1 #endif #include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include #include #ifdef INET @@ -74,58 +59,49 @@ __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1 #include #endif -#include -#include #include -#include -#include -#include -#include +#include #include - /* Function declarations */ int url_match(device_t, cfdata_t, void *); void url_attach(device_t, device_t, void *); -int url_detach(device_t, int); -int url_activate(device_t, enum devact); -CFATTACH_DECL_NEW(url, sizeof(struct url_softc), url_match, url_attach, - url_detach, url_activate); +CFATTACH_DECL_NEW(url, sizeof(struct usbnet), url_match, url_attach, + usbnet_detach, usbnet_activate); -Static int url_openpipes(struct url_softc *); -Static int url_rx_list_init(struct url_softc *); -Static int url_tx_list_init(struct url_softc *); -Static int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *); -Static void url_start(struct ifnet *); -Static int url_send(struct url_softc *, struct mbuf *, int); -Static void url_txeof(struct usbd_xfer *, void *, usbd_status); -Static void url_rxeof(struct usbd_xfer *, void *, usbd_status); -Static void url_tick(void *); -Static void url_tick_task(void *); -Static int url_ioctl(struct ifnet *, u_long, void *); -Static void url_stop_task(struct url_softc *); -Static void url_stop(struct ifnet *, int); -Static void url_watchdog(struct ifnet *); -Static int url_ifmedia_change(struct ifnet *); -Static void url_ifmedia_status(struct ifnet *, struct ifmediareq *); -Static void url_lock_mii(struct url_softc *); -Static void url_unlock_mii(struct url_softc *); -Static int url_int_miibus_readreg(device_t, int, int, uint16_t *); -Static int url_int_miibus_writereg(device_t, int, int, uint16_t); -Static void url_miibus_statchg(struct ifnet *); -Static int url_init(struct ifnet *); -Static void url_setmulti(struct url_softc *); -Static void url_reset(struct url_softc *); - -Static int url_csr_read_1(struct url_softc *, int); -Static int url_csr_read_2(struct url_softc *, int); -Static int url_csr_write_1(struct url_softc *, int, int); -Static int url_csr_write_2(struct url_softc *, int, int); -Static int url_csr_write_4(struct url_softc *, int, int); -Static int url_mem(struct url_softc *, int, int, void *, int); +static unsigned url_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void url_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static int url_int_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static int url_int_mii_write_reg(struct usbnet *, int, int, uint16_t); +static int url_ioctl_cb(struct ifnet *, u_long, void *); +static void url_stop_cb(struct ifnet *, int); +static void url_mii_statchg_cb(struct ifnet *); +static int url_init(struct ifnet *); +static void url_setiff_locked(struct usbnet *); +static void url_setiff(struct usbnet *); +static void url_reset(struct usbnet *); + +static int url_csr_read_1(struct usbnet *, int); +static int url_csr_read_2(struct usbnet *, int); +static int url_csr_write_1(struct usbnet *, int, int); +static int url_csr_write_2(struct usbnet *, int, int); +static int url_csr_write_4(struct usbnet *, int, int); +static int url_mem(struct usbnet *, int, int, void *, int); + +static struct usbnet_ops url_ops = { + .uno_stop = url_stop_cb, + .uno_ioctl = url_ioctl_cb, + .uno_read_reg = url_int_mii_read_reg, + .uno_write_reg = url_int_mii_write_reg, + .uno_statchg = url_mii_statchg_cb, + .uno_tx_prepare = url_tx_prepare, + .uno_rx_loop = url_rx_loop, + .uno_init = url_init, +}; /* Macros */ #ifdef URL_DEBUG @@ -137,17 +113,17 @@ int urldebug = 0; #define DPRINTFN(n, x) #endif -#define URL_SETBIT(sc, reg, x) \ - url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x)) +#define URL_SETBIT(un, reg, x) \ + url_csr_write_1(un, reg, url_csr_read_1(un, reg) | (x)) -#define URL_SETBIT2(sc, reg, x) \ - url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x)) +#define URL_SETBIT2(un, reg, x) \ + url_csr_write_2(un, reg, url_csr_read_2(un, reg) | (x)) -#define URL_CLRBIT(sc, reg, x) \ - url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x)) +#define URL_CLRBIT(un, reg, x) \ + url_csr_write_1(un, reg, url_csr_read_1(un, reg) & ~(x)) -#define URL_CLRBIT2(sc, reg, x) \ - url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x)) +#define URL_CLRBIT2(un, reg, x) \ + url_csr_write_2(un, reg, url_csr_read_2(un, reg) & ~(x)) static const struct url_type { struct usb_devno url_dev; @@ -181,7 +157,8 @@ url_match(device_t parent, cfdata_t matc void url_attach(device_t parent, device_t self, void *aux) { - struct url_softc *sc = device_private(self); + USBNET_MII_DECL_DEFAULT(unm); + struct usbnet * const un = device_private(self); struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; struct usbd_interface *iface; @@ -189,20 +166,25 @@ url_attach(device_t parent, device_t sel usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; char *devinfop; - struct ifnet *ifp; - struct mii_data *mii; - u_char eaddr[ETHER_ADDR_LEN]; - int i, s; - - sc->sc_dev = self; + int i; aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = un; + un->un_ops = &url_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = URL_RX_LIST_CNT; + un->un_tx_list_cnt = URL_TX_LIST_CNT; + un->un_rx_bufsz = URL_BUFSZ; + un->un_tx_bufsz = URL_BUFSZ; + /* Move the device into the configured state. */ err = usbd_set_config_no(dev, URL_CONFIG_NO, 1); if (err) { @@ -211,10 +193,6 @@ url_attach(device_t parent, device_t sel goto bad; } - usb_init_task(&sc->sc_tick_task, url_tick_task, sc, 0); - rw_init(&sc->sc_mii_rwlock); - usb_init_task(&sc->sc_stop_task, (void (*)(void *))url_stop_task, sc, 0); - /* get control interface */ err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface); if (err) { @@ -223,17 +201,23 @@ url_attach(device_t parent, device_t sel goto bad; } - sc->sc_udev = dev; - sc->sc_ctl_iface = iface; - sc->sc_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags; + un->un_iface = iface; + un->un_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags; +#if 0 + if (un->un_flags & URL_EXT_PHY) { + un->un_read_reg_cb = url_ext_mii_read_reg; + un->un_write_reg_cb = url_ext_mii_write_reg; + } +#endif /* get interface descriptor */ - id = usbd_get_interface_descriptor(sc->sc_ctl_iface); + id = usbd_get_interface_descriptor(un->un_iface); /* find endpoints */ - sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1; + un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = + un->un_ed[USBNET_ENDPT_INTR] = 0; for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get endpoint %d\n", i); @@ -241,180 +225,64 @@ url_attach(device_t parent, device_t sel } if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) - sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */ + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) - sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */ + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) - sc->sc_intrin_no = ed->bEndpointAddress; /* Status */ + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } - if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 || - sc->sc_intrin_no == -1) { + if (un->un_ed[USBNET_ENDPT_RX] == 0 || + un->un_ed[USBNET_ENDPT_TX] == 0 || + un->un_ed[USBNET_ENDPT_INTR] == 0) { aprint_error_dev(self, "missing endpoint\n"); goto bad; } - s = splnet(); + /* Set these up now for url_mem(). */ + usbnet_attach(un, "urldet"); /* reset the adapter */ - url_reset(sc); + usbnet_lock(un); + url_reset(un); + usbnet_unlock(un); /* Get Ethernet Address */ - err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr, + usbnet_lock_mii(un); + err = url_mem(un, URL_CMD_READMEM, URL_IDR0, (void *)un->un_eaddr, ETHER_ADDR_LEN); + usbnet_unlock_mii(un); if (err) { aprint_error_dev(self, "read MAC address failed\n"); - splx(s); goto bad; } - /* Print Ethernet Address */ - aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); - /* initialize interface information */ - ifp = GET_IFP(sc); - ifp->if_softc = sc; - ifp->if_mtu = ETHERMTU; - strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_start = url_start; - ifp->if_ioctl = url_ioctl; - ifp->if_watchdog = url_watchdog; - ifp->if_init = url_init; - ifp->if_stop = url_stop; - - IFQ_SET_READY(&ifp->if_snd); - - /* - * Do ifmedia setup. - */ - mii = &sc->sc_mii; - mii->mii_ifp = ifp; - mii->mii_readreg = url_int_miibus_readreg; - mii->mii_writereg = url_int_miibus_writereg; -#if 0 - if (sc->sc_flags & URL_EXT_PHY) { - mii->mii_readreg = url_ext_miibus_readreg; - mii->mii_writereg = url_ext_miibus_writereg; - } -#endif - mii->mii_statchg = url_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - sc->sc_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, - url_ifmedia_change, url_ifmedia_status); - mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); - if (LIST_FIRST(&mii->mii_phys) == NULL) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - - /* attach the interface */ - if_attach(ifp); - ether_ifattach(ifp, eaddr); - - rnd_attach_source(&sc->rnd_source, device_xname(self), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&sc->sc_stat_ch, 0); - sc->sc_attached = 1; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev); - - if (!pmf_device_register(self, NULL, NULL)) - aprint_error_dev(self, "couldn't establish power handler\n"); + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, &unm); return; bad: - sc->sc_dying = 1; + usbnet_set_dying(un, true); return; } -/* detach */ -int -url_detach(device_t self, int flags) -{ - struct url_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - int s; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - /* Detached before attached finished */ - if (!sc->sc_attached) - return 0; - - pmf_device_deregister(self); - - /* - * XXX Halting callout guarantees no more tick tasks. What - * guarantees no more stop tasks? What guarantees no more - * calls to url_send? Don't we need to wait for if_detach or - * something? Should set sc->sc_dying here? Is device - * deactivation guaranteed to have already happened? - */ - callout_halt(&sc->sc_stat_ch, NULL); - - /* Remove any pending tasks */ - usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER, - NULL); - usb_rem_task_wait(sc->sc_udev, &sc->sc_stop_task, USB_TASKQ_DRIVER, - NULL); - - s = splusb(); - - if (--sc->sc_refcnt >= 0) { - /* Wait for processes to go away */ - usb_detach_waitold(sc->sc_dev); - } - - if (ifp->if_flags & IFF_RUNNING) - url_stop(GET_IFP(sc), 1); - - rnd_detach_source(&sc->rnd_source); - mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); - ether_ifdetach(ifp); - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->sc_pipe_tx != NULL) - aprint_debug_dev(self, "detach has active tx endpoint.\n"); - if (sc->sc_pipe_rx != NULL) - aprint_debug_dev(self, "detach has active rx endpoint.\n"); - if (sc->sc_pipe_intr != NULL) - aprint_debug_dev(self, "detach has active intr endpoint.\n"); -#endif - - sc->sc_attached = 0; - - splx(s); - - rw_destroy(&sc->sc_mii_rwlock); - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); - - return 0; -} - /* read/write memory */ -Static int -url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len) +static int +url_mem(struct usbnet *un, int cmd, int offset, void *buf, int len) { usb_device_request_t req; usbd_status err; - if (sc == NULL) - return 0; + usbnet_isowned_mii(un); DPRINTFN(0x200, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) + if (usbnet_isdying(un)) return 0; if (cmd == URL_CMD_READMEM) @@ -426,13 +294,10 @@ url_mem(struct url_softc *sc, int cmd, i USETW(req.wIndex, 0x0000); USETW(req.wLength, len); - sc->sc_refcnt++; - err = usbd_do_request(sc->sc_udev, &req, buf); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); + err = usbd_do_request(un->un_udev, &req, buf); if (err) { DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n", - device_xname(sc->sc_dev), + device_xname(un->un_dev), cmd == URL_CMD_READMEM ? "read" : "write", offset, err)); } @@ -441,252 +306,192 @@ url_mem(struct url_softc *sc, int cmd, i } /* read 1byte from register */ -Static int -url_csr_read_1(struct url_softc *sc, int reg) +static int +url_csr_read_1(struct usbnet *un, int reg) { uint8_t val = 0; DPRINTFN(0x100, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) - return 0; - - return url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val; + return url_mem(un, URL_CMD_READMEM, reg, &val, 1) ? 0 : val; } /* read 2bytes from register */ -Static int -url_csr_read_2(struct url_softc *sc, int reg) +static int +url_csr_read_2(struct usbnet *un, int reg) { uWord val; DPRINTFN(0x100, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); USETW(val, 0); - return url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val); + return url_mem(un, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val); } /* write 1byte to register */ -Static int -url_csr_write_1(struct url_softc *sc, int reg, int aval) +static int +url_csr_write_1(struct usbnet *un, int reg, int aval) { uint8_t val = aval; DPRINTFN(0x100, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0; + return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0; } /* write 2bytes to register */ -Static int -url_csr_write_2(struct url_softc *sc, int reg, int aval) +static int +url_csr_write_2(struct usbnet *un, int reg, int aval) { uWord val; DPRINTFN(0x100, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); USETW(val, aval); - if (sc->sc_dying) - return 0; - - return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0; + return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0; } /* write 4bytes to register */ -Static int -url_csr_write_4(struct url_softc *sc, int reg, int aval) +static int +url_csr_write_4(struct usbnet *un, int reg, int aval) { uDWord val; DPRINTFN(0x100, - ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); USETDW(val, aval); - if (sc->sc_dying) - return 0; - - return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0; + return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0; } -Static int -url_init(struct ifnet *ifp) +static int +url_init_locked(struct ifnet *ifp) { - struct url_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); + struct usbnet * const un = ifp->if_softc; const u_char *eaddr; - int i, rc, s; + int i; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) - return EIO; + usbnet_isowned(un); - s = splnet(); + if (usbnet_isdying(un)) + return EIO; /* Cancel pending I/O and free all TX/RX buffers */ - url_stop(ifp, 1); + usbnet_stop(un, ifp, 1); + + usbnet_lock_mii_un_locked(un); eaddr = CLLADDR(ifp->if_sadl); for (i = 0; i < ETHER_ADDR_LEN; i++) - url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]); + url_csr_write_1(un, URL_IDR0 + i, eaddr[i]); /* Init transmission control register */ - URL_CLRBIT(sc, URL_TCR, + URL_CLRBIT(un, URL_TCR, URL_TCR_TXRR1 | URL_TCR_TXRR0 | URL_TCR_IFG1 | URL_TCR_IFG0 | URL_TCR_NOCRC); /* Init receive control register */ - URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD); + URL_SETBIT2(un, URL_RCR, URL_RCR_TAIL | URL_RCR_AD); if (ifp->if_flags & IFF_BROADCAST) - URL_SETBIT2(sc, URL_RCR, URL_RCR_AB); + URL_SETBIT2(un, URL_RCR, URL_RCR_AB); else - URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB); + URL_CLRBIT2(un, URL_RCR, URL_RCR_AB); /* If we want promiscuous mode, accept all physical frames. */ if (ifp->if_flags & IFF_PROMISC) - URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); + URL_SETBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); else - URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); - + URL_CLRBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); /* Load the multicast filter */ - url_setmulti(sc); + url_setiff_locked(un); /* Enable RX and TX */ - URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE); - - if ((rc = mii_mediachg(mii)) == ENXIO) - rc = 0; - else if (rc != 0) - goto out; - - if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) { - if (url_openpipes(sc)) { - splx(s); - return EIO; - } - } - /* Initialize transmit ring */ - if (url_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return EIO; - } + URL_SETBIT(un, URL_CR, URL_CR_TE | URL_CR_RE); - /* Initialize receive ring */ - if (url_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return EIO; - } - /* Start up the receive pipe. */ - for (i = 0; i < URL_RX_LIST_CNT; i++) { - struct url_chain *c = &sc->sc_cdata.url_rx_chain[i]; + usbnet_unlock_mii_un_locked(un); - usbd_setup_xfer(c->url_xfer, c, c->url_buf, URL_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, url_rxeof); - (void)usbd_transfer(c->url_xfer); - DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev), - __func__)); - } + return usbnet_init_rx_tx(un); +} - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; +static int +url_init(struct ifnet *ifp) +{ + struct usbnet * const un = ifp->if_softc; - callout_reset(&sc->sc_stat_ch, hz, url_tick, sc); + usbnet_lock(un); + int ret = url_init_locked(ifp); + usbnet_unlock(un); -out: - splx(s); - return rc; + return ret; } -Static void -url_reset(struct url_softc *sc) +static void +url_reset(struct usbnet *un) { int i; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) + if (usbnet_isdying(un)) return; - URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST); + usbnet_lock_mii_un_locked(un); + URL_SETBIT(un, URL_CR, URL_CR_SOFT_RST); for (i = 0; i < URL_TX_TIMEOUT; i++) { - if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST)) + if (!(url_csr_read_1(un, URL_CR) & URL_CR_SOFT_RST)) break; delay(10); /* XXX */ } delay(10000); /* XXX */ -} - -int -url_activate(device_t self, enum devact act) -{ - struct url_softc *sc = device_private(self); - - DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev), - __func__, act)); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(&sc->sc_ec.ec_if); - sc->sc_dying = 1; - return 0; - default: - return EOPNOTSUPP; - } + usbnet_unlock_mii_un_locked(un); } #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26) - -Static void -url_setmulti(struct url_softc *sc) +static void +url_setiff_locked(struct usbnet *un) { - struct ethercom *ec = &sc->sc_ec; - struct ifnet *ifp; + struct ifnet * const ifp = usbnet_ifp(un); + struct ethercom *ec = usbnet_ec(un); struct ether_multi *enm; struct ether_multistep step; uint32_t hashes[2] = { 0, 0 }; int h = 0; int mcnt = 0; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->sc_dying) - return; + usbnet_isowned_mii(un); - ifp = GET_IFP(sc); + if (usbnet_isdying(un)) + return; if (ifp->if_flags & IFF_PROMISC) { - URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); + URL_SETBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); return; } else if (ifp->if_flags & IFF_ALLMULTI) { allmulti: ifp->if_flags |= IFF_ALLMULTI; - URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM); - URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP); + URL_SETBIT2(un, URL_RCR, URL_RCR_AAM); + URL_CLRBIT2(un, URL_RCR, URL_RCR_AAP); return; } /* first, zot all the existing hash bits */ - url_csr_write_4(sc, URL_MAR0, 0); - url_csr_write_4(sc, URL_MAR4, 0); + url_csr_write_4(un, URL_MAR0, 0); + url_csr_write_4(un, URL_MAR4, 0); /* now program new ones */ ETHER_LOCK(ec); @@ -710,337 +515,75 @@ allmulti: ifp->if_flags &= ~IFF_ALLMULTI; - URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); + URL_CLRBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); if (mcnt) { - URL_SETBIT2(sc, URL_RCR, URL_RCR_AM); + URL_SETBIT2(un, URL_RCR, URL_RCR_AM); } else { - URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM); + URL_CLRBIT2(un, URL_RCR, URL_RCR_AM); } - url_csr_write_4(sc, URL_MAR0, hashes[0]); - url_csr_write_4(sc, URL_MAR4, hashes[1]); + url_csr_write_4(un, URL_MAR0, hashes[0]); + url_csr_write_4(un, URL_MAR4, hashes[1]); } -Static int -url_openpipes(struct url_softc *sc) +static void +url_setiff(struct usbnet *un) { - usbd_status err; - int error = 0; - - if (sc->sc_dying) - return EIO; - - sc->sc_refcnt++; - - /* Open RX pipe */ - err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no, - USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx); - if (err) { - printf("%s: open rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - error = EIO; - goto done; - } - - /* Open TX pipe */ - err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no, - USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - error = EIO; - goto done; - } - -#if 0 - /* XXX: interrupt endpoint is not yet supported */ - /* Open Interrupt pipe */ - err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no, - USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc, - &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN, - url_intr, USBD_DEFAULT_INTERVAL); - if (err) { - printf("%s: open intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - error = EIO; - goto done; - } -#endif - - done: - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - - return error; -} - -Static int -url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m) -{ - struct mbuf *m_new = NULL; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - return ENOBUFS; - } - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - m_freem(m_new); - return ENOBUFS; - } - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - } else { - m_new = m; - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - m_new->m_data = m_new->m_ext.ext_buf; - } - - m_adj(m_new, ETHER_ALIGN); - c->url_mbuf = m_new; - - return 0; -} - - -Static int -url_rx_list_init(struct url_softc *sc) -{ - struct url_cdata *cd; - struct url_chain *c; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < URL_RX_LIST_CNT; i++) { - c = &cd->url_rx_chain[i]; - c->url_sc = sc; - if (url_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->url_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_pipe_rx, URL_BUFSZ, - 0, 0, &c->url_xfer); - if (error) - return error; - c->url_buf = usbd_get_buffer(c->url_xfer); - } - } - - return 0; + usbnet_lock(un); + usbnet_lock_mii_un_locked(un); + url_setiff_locked(un); + usbnet_unlock_mii_un_locked(un); + usbnet_unlock(un); } -Static int -url_tx_list_init(struct url_softc *sc) -{ - struct url_cdata *cd; - struct url_chain *c; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < URL_TX_LIST_CNT; i++) { - c = &cd->url_tx_chain[i]; - c->url_sc = sc; - c->url_mbuf = NULL; - if (c->url_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_pipe_tx, URL_BUFSZ, - USBD_FORCE_SHORT_XFER, 0, &c->url_xfer); - if (error) - return error; - c->url_buf = usbd_get_buffer(c->url_xfer); - } - } - - return 0; -} - -Static void -url_start(struct ifnet *ifp) -{ - struct url_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev), - __func__, sc->sc_link)); - - if (sc->sc_dying) - return; - - if (!sc->sc_link) - return; - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (url_send(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } - - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - bpf_mtap(ifp, m_head, BPF_D_OUT); - - ifp->if_flags |= IFF_OACTIVE; - - /* Set a timeout in case the chip goes out to lunch. */ - ifp->if_timer = 5; -} - -Static int -url_send(struct url_softc *sc, struct mbuf *m, int idx) +static unsigned +url_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { int total_len; - struct url_chain *c; - usbd_status err; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); + usbnet_isowned_tx(un); - c = &sc->sc_cdata.url_tx_chain[idx]; + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__)); + + KASSERT(un->un_tx_bufsz >= URL_MIN_FRAME_LEN); + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz) + return 0; /* Copy the mbuf data into a contiguous buffer */ - m_copydata(m, 0, m->m_pkthdr.len, c->url_buf); - c->url_mbuf = m; + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf); total_len = m->m_pkthdr.len; if (total_len < URL_MIN_FRAME_LEN) { - memset(c->url_buf + total_len, 0, + memset(c->unc_buf + total_len, 0, URL_MIN_FRAME_LEN - total_len); total_len = URL_MIN_FRAME_LEN; } - usbd_setup_xfer(c->url_xfer, c, c->url_buf, total_len, - USBD_FORCE_SHORT_XFER, URL_TX_TIMEOUT, url_txeof); - - /* Transmit */ - sc->sc_refcnt++; - err = usbd_transfer(c->url_xfer); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - if (err != USBD_IN_PROGRESS) { - printf("%s: url_send error=%s\n", device_xname(sc->sc_dev), - usbd_errstr(err)); - /* Stop the interface */ - usb_add_task(sc->sc_udev, &sc->sc_stop_task, - USB_TASKQ_DRIVER); - return EIO; - } - DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev), + DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev), __func__, total_len)); - sc->sc_cdata.url_tx_cnt++; - - return 0; -} - -Static void -url_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct url_chain *c = priv; - struct url_softc *sc = c->url_sc; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (sc->sc_dying) - return; - - s = splnet(); - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) { - sc->sc_refcnt++; - usbd_clear_endpoint_stall_async(sc->sc_pipe_tx); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - } - splx(s); - return; - } - - ifp->if_opackets++; - - m_freem(c->url_mbuf); - c->url_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - url_start(ifp); - - splx(s); + return total_len; } -Static void -url_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +static void +url_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct url_chain *c = priv; - struct url_softc *sc = c->url_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - uint32_t total_len; + struct ifnet *ifp = usbnet_ifp(un); url_rxhdr_t rxhdr; - int s; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (sc->sc_dying) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->sc_rx_errs++; - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: %u usb errors on rx: %s\n", - device_xname(sc->sc_dev), sc->sc_rx_errs, - usbd_errstr(status)); - sc->sc_rx_errs = 0; - } - if (status == USBD_STALLED) { - sc->sc_refcnt++; - usbd_clear_endpoint_stall_async(sc->sc_pipe_rx); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - } - goto done; - } - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + usbnet_isowned_rx(un); - memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__)); - if (total_len <= ETHER_CRC_LEN) { + if (total_len <= ETHER_CRC_LEN || total_len <= sizeof(rxhdr)) { ifp->if_ierrors++; - goto done; + return; } - memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr)); + memcpy(&rxhdr, c->unc_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr)); DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n", - device_xname(sc->sc_dev), + device_xname(un->un_dev), UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK, UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "", UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "", @@ -1049,345 +592,70 @@ url_rxeof(struct usbd_xfer *xfer, void * if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) { ifp->if_ierrors++; - goto done; + return; } total_len -= ETHER_CRC_LEN; - m = c->url_mbuf; - m->m_pkthdr.len = m->m_len = total_len; - m_set_rcvif(m, ifp); - - s = splnet(); - - if (url_newbuf(sc, c, NULL) == ENOBUFS) { - ifp->if_ierrors++; - goto done1; - } - - DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev), - __func__, m->m_len)); - if_percpuq_enqueue((ifp)->if_percpuq, (m)); - - done1: - splx(s); - - done: - /* Setup new transfer */ - usbd_setup_xfer(xfer, c, c->url_buf, URL_BUFSZ, USBD_SHORT_XFER_OK, - USBD_NO_TIMEOUT, url_rxeof); - sc->sc_refcnt++; - usbd_transfer(xfer); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); - - DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__)); + DPRINTF(("%s: %s: deliver %d\n", device_xname(un->un_dev), + __func__, total_len)); + usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0); } #if 0 -Static void url_intr(void) +static void url_intr(void) { } #endif -Static int -url_ioctl(struct ifnet *ifp, u_long cmd, void *data) +static int +url_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct url_softc *sc = ifp->if_softc; - int s, error = 0; + struct usbnet * const un = ifp->if_softc; - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return EIO; - - s = splnet(); - - error = ether_ioctl(ifp, cmd, data); - if (error == ENETRESET) { - if (ifp->if_flags & IFF_RUNNING) - url_setmulti(sc); - error = 0; + switch (cmd) { + case SIOCADDMULTI: + case SIOCDELMULTI: + url_setiff(un); + break; + default: + break; } - splx(s); - - return error; -} - -Static void -url_watchdog(struct ifnet *ifp) -{ - struct url_softc *sc = ifp->if_softc; - struct url_chain *c; - usbd_status stat; - int s; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->sc_dev)); - - s = splusb(); - c = &sc->sc_cdata.url_tx_chain[0]; - usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat); - url_txeof(c->url_xfer, c, stat); - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - url_start(ifp); - splx(s); -} - -Static void -url_stop_task(struct url_softc *sc) -{ - url_stop(GET_IFP(sc), 1); + return 0; } /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */ -Static void -url_stop(struct ifnet *ifp, int disable) +static void +url_stop_cb(struct ifnet *ifp, int disable) { - struct url_softc *sc = ifp->if_softc; - usbd_status err; - int i; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - ifp->if_timer = 0; - - url_reset(sc); - - callout_stop(&sc->sc_stat_ch); - - /* Stop transfers */ - /* RX endpoint */ - if (sc->sc_pipe_rx != NULL) { - err = usbd_abort_pipe(sc->sc_pipe_rx); - if (err) - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - - /* TX endpoint */ - if (sc->sc_pipe_tx != NULL) { - err = usbd_abort_pipe(sc->sc_pipe_tx); - if (err) - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - -#if 0 - /* XXX: Interrupt endpoint is not yet supported!! */ - /* Interrupt endpoint */ - if (sc->sc_pipe_intr != NULL) { - err = usbd_abort_pipe(sc->sc_pipe_intr); - if (err) - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - err = usbd_close_pipe(sc->sc_pipe_intr); - if (err) - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - sc->sc_pipe_intr = NULL; - } -#endif - - /* Free RX resources. */ - for (i = 0; i < URL_RX_LIST_CNT; i++) { - if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) { - m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf); - sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL; - } - if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer); - sc->sc_cdata.url_rx_chain[i].url_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < URL_TX_LIST_CNT; i++) { - if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) { - m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf); - sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL; - } - if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer); - sc->sc_cdata.url_tx_chain[i].url_xfer = NULL; - } - } + struct usbnet * const un = ifp->if_softc; - /* Close pipes */ - /* RX endpoint */ - if (sc->sc_pipe_rx != NULL) { - err = usbd_close_pipe(sc->sc_pipe_rx); - if (err) - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - sc->sc_pipe_rx = NULL; - } + usbnet_isowned(un); - /* TX endpoint */ - if (sc->sc_pipe_tx != NULL) { - err = usbd_close_pipe(sc->sc_pipe_tx); - if (err) - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - sc->sc_pipe_tx = NULL; - } + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - sc->sc_link = 0; - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + url_reset(un); } -/* Set media options */ -Static int -url_ifmedia_change(struct ifnet *ifp) +static int +url_int_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - struct url_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); - int rc; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; - - sc->sc_link = 0; - if ((rc = mii_mediachg(mii)) == ENXIO) - return 0; - return rc; -} - -/* Report current media status. */ -Static void -url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) -{ - struct url_softc *sc = ifp->if_softc; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return; - - ether_mediastatus(ifp, ifmr); -} - -Static void -url_tick(void *xsc) -{ - struct url_softc *sc = xsc; - - if (sc == NULL) - return; - - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); - - if (sc->sc_dying) - return; - - /* Perform periodic stuff in process context */ - usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER); -} - -Static void -url_tick_task(void *xsc) -{ - struct url_softc *sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - int s; - - if (sc == NULL) - return; - - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); - - if (sc->sc_dying) - return; - - ifp = GET_IFP(sc); - mii = GET_MII(sc); - - if (mii == NULL) - return; - - s = splnet(); - - mii_tick(mii); - if (!sc->sc_link) { - mii_pollstat(mii); - if (mii->mii_media_status & IFM_ACTIVE && - IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { - DPRINTF(("%s: %s: got link\n", - device_xname(sc->sc_dev), __func__)); - sc->sc_link++; - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - url_start(ifp); - } - } - - callout_reset(&sc->sc_stat_ch, hz, url_tick, sc); - - splx(s); -} - -/* Get exclusive access to the MII registers */ -Static void -url_lock_mii(struct url_softc *sc) -{ - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); - - sc->sc_refcnt++; - rw_enter(&sc->sc_mii_rwlock, RW_WRITER); -} - -Static void -url_unlock_mii(struct url_softc *sc) -{ - DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), - __func__)); - - rw_exit(&sc->sc_mii_rwlock); - if (--sc->sc_refcnt < 0) - usb_detach_wakeupold(sc->sc_dev); -} - -Static int -url_int_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) -{ - struct url_softc *sc; uint16_t data; - int rv = 0; + usbd_status err = USBD_NORMAL_COMPLETION; - if (dev == NULL) - return 0; - - sc = device_private(dev); + usbnet_isowned_mii(un); DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", - device_xname(sc->sc_dev), __func__, phy, reg)); - - if (sc->sc_dying) { -#ifdef DIAGNOSTIC - printf("%s: %s: dying\n", device_xname(sc->sc_dev), - __func__); -#endif - return -1; - } + device_xname(un->un_dev), __func__, phy, reg)); /* XXX: one PHY only for the RTL8150 internal PHY */ if (phy != 0) { DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", - device_xname(sc->sc_dev), __func__, phy)); - return -1; + device_xname(un->un_dev), __func__, phy)); + return EINVAL; } - url_lock_mii(sc); - switch (reg) { case MII_BMCR: /* Control Register */ reg = URL_BMCR; @@ -1411,57 +679,38 @@ url_int_miibus_readreg(device_t dev, int break; default: printf("%s: %s: bad register %04x\n", - device_xname(sc->sc_dev), __func__, reg); - rv = -1; - goto R_DONE; - break; + device_xname(un->un_dev), __func__, reg); + return EINVAL; } if (reg == URL_MSR) - data = url_csr_read_1(sc, reg); + data = url_csr_read_1(un, reg); else - data = url_csr_read_2(sc, reg); + data = url_csr_read_2(un, reg); *val = data; R_DONE: DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n", - device_xname(sc->sc_dev), __func__, phy, reg, *val)); + device_xname(un->un_dev), __func__, phy, reg, *val)); - url_unlock_mii(sc); - return rv; + return err; } -Static int -url_int_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +static int +url_int_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct url_softc *sc; - int rv = 0; - - if (dev == NULL) - return -1; - - sc = device_private(dev); + usbnet_isowned_mii(un); DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n", - device_xname(sc->sc_dev), __func__, phy, reg, val)); - - if (sc->sc_dying) { -#ifdef DIAGNOSTIC - printf("%s: %s: dying\n", device_xname(sc->sc_dev), - __func__); -#endif - return -1; - } + device_xname(un->un_dev), __func__, phy, reg, val)); /* XXX: one PHY only for the RTL8150 internal PHY */ if (phy != 0) { DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", - device_xname(sc->sc_dev), __func__, phy)); - return -1; + device_xname(un->un_dev), __func__, phy)); + return EINVAL; } - url_lock_mii(sc); - switch (reg) { case MII_BMCR: /* Control Register */ reg = URL_BMCR; @@ -1471,8 +720,7 @@ url_int_miibus_writereg(device_t dev, in break; case MII_PHYIDR1: case MII_PHYIDR2: - goto W_DONE; - break; + return 0; case MII_ANAR: /* Autonegotiation advertisement */ reg = URL_ANAR; break; @@ -1484,115 +732,93 @@ url_int_miibus_writereg(device_t dev, in break; default: printf("%s: %s: bad register %04x\n", - device_xname(sc->sc_dev), __func__, reg); - rv = -1; - goto W_DONE; - break; + device_xname(un->un_dev), __func__, reg); + return EINVAL; } if (reg == URL_MSR) - url_csr_write_1(sc, reg, val); + url_csr_write_1(un, reg, val); else - url_csr_write_2(sc, reg, val); - W_DONE: + url_csr_write_2(un, reg, val); - url_unlock_mii(sc); - return rv; + return 0; } -Static void -url_miibus_statchg(struct ifnet *ifp) +static void +url_mii_statchg_cb(struct ifnet *ifp) { -#ifdef URL_DEBUG - if (ifp == NULL) - return; + struct usbnet * const un = ifp->if_softc; DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__)); -#endif - /* Nothing to do */ + + /* XXX */ + usbnet_set_link(un, true); } #if 0 /* * external PHYs support, but not test. */ -Static int -url_ext_miibus_redreg(device_t dev, int phy, int reg) +static usbd_status +url_ext_mii_read_reg(struct usbnet *un, int phy, int reg) { - struct url_softc *sc = device_private(dev); uint16_t val; DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n", - device_xname(sc->sc_dev), __func__, phy, reg)); - - if (sc->sc_dying) { -#ifdef DIAGNOSTIC - printf("%s: %s: dying\n", device_xname(sc->sc_dev), - __func__); -#endif - return 0; - } - - url_lock_mii(sc); + device_xname(un->un_dev), __func__, phy, reg)); - url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK); + url_csr_write_1(un, URL_PHYADD, phy & URL_PHYADD_MASK); /* * RTL8150L will initiate a MII management data transaction * if PHYCNT_OWN bit is set 1 by software. After transaction, * this bit is auto cleared by TRL8150L. */ - url_csr_write_1(sc, URL_PHYCNT, + url_csr_write_1(un, URL_PHYCNT, (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR); for (i = 0; i < URL_TIMEOUT; i++) { - if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0) + if ((url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0) break; } if (i == URL_TIMEOUT) { - printf("%s: MII read timed out\n", device_xname(sc->sc_dev)); + printf("%s: MII read timed out\n", device_xname(un->un_dev)); } - val = url_csr_read_2(sc, URL_PHYDAT); + val = url_csr_read_2(un, URL_PHYDAT); DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", - device_xname(sc->sc_dev), __func__, phy, reg, val)); + device_xname(un->un_dev), __func__, phy, reg, val)); - url_unlock_mii(sc); - return val; + return USBD_NORMAL_COMPLETION; } -Static void -url_ext_miibus_writereg(device_t dev, int phy, int reg, int data) +static usbd_status +url_ext_mii_write_reg(struct usbnet *un, int phy, int reg, int data) { - struct url_softc *sc = device_private(dev); DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", - device_xname(sc->sc_dev), __func__, phy, reg, data)); + device_xname(un->un_dev), __func__, phy, reg, data)); - if (sc->sc_dying) { -#ifdef DIAGNOSTIC - printf("%s: %s: dying\n", device_xname(sc->sc_dev), - __func__); -#endif - return; - } - - url_lock_mii(sc); - - url_csr_write_2(sc, URL_PHYDAT, data); - url_csr_write_1(sc, URL_PHYADD, phy); - url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */ + url_csr_write_2(un, URL_PHYDAT, data); + url_csr_write_1(un, URL_PHYADD, phy); + url_csr_write_1(un, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */ for (i=0; i < URL_TIMEOUT; i++) { - if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) + if (url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN) break; } if (i == URL_TIMEOUT) { printf("%s: MII write timed out\n", - device_xname(sc->sc_dev)); + device_xname(un->un_dev)); + return USBD_TIMEOUT; } - url_unlock_mii(sc); - return; + return USBD_NORMAL_COMPLETION; } #endif + +#ifdef _MODULE +#include "ioconf.c" +#endif + +USBNET_MODULE(url) Index: usb/if_urlreg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_urlreg.h,v retrieving revision 1.13 diff -p -u -u -r1.13 if_urlreg.h --- usb/if_urlreg.h 21 Jul 2019 10:27:56 -0000 1.13 +++ usb/if_urlreg.h 29 Aug 2019 06:22:05 -0000 @@ -126,61 +126,3 @@ typedef uWord url_rxhdr_t; /* Recive Hea #define URL_RXHDR_RUNTPKT_MASK (0x2000) /* Runt packet */ #define URL_RXHDR_PHYPKT_MASK (0x4000) /* Physical match packet */ #define URL_RXHDR_MCASTPKT_MASK (0x8000) /* Multicast packet */ - -#define GET_IFP(sc) (&(sc)->sc_ec.ec_if) -#define GET_MII(sc) (&(sc)->sc_mii) - -struct url_chain { - struct url_softc *url_sc; - struct usbd_xfer *url_xfer; - char *url_buf; - struct mbuf *url_mbuf; -}; - -struct url_cdata { - struct url_chain url_tx_chain[URL_TX_LIST_CNT]; - struct url_chain url_rx_chain[URL_TX_LIST_CNT]; -#if 0 - /* XXX: Interrupt Endpoint is not yet supported! */ - struct url_intrpkg url_ibuf; -#endif - int url_tx_prod; - int url_tx_cnt; -}; - -struct url_softc { - device_t sc_dev; /* base device */ - struct usbd_device * sc_udev; - - /* USB */ - struct usbd_interface * sc_ctl_iface; - /* int sc_ctl_iface_no; */ - int sc_bulkin_no; /* bulk in endpoint */ - int sc_bulkout_no; /* bulk out endpoint */ - int sc_intrin_no; /* intr in endpoint */ - struct usbd_pipe * sc_pipe_rx; - struct usbd_pipe * sc_pipe_tx; - struct usbd_pipe * sc_pipe_intr; - struct callout sc_stat_ch; - u_int sc_rx_errs; - /* u_int sc_intr_errs; */ - struct timeval sc_rx_notice; - - /* Ethernet */ - struct ethercom sc_ec; /* ethernet common */ - struct mii_data sc_mii; - krwlock_t sc_mii_rwlock; - int sc_link; -#define sc_media url_mii.mii_media - krndsource_t rnd_source; - struct url_cdata sc_cdata; - - int sc_attached; - int sc_dying; - int sc_refcnt; - - struct usb_task sc_tick_task; - struct usb_task sc_stop_task; - - uint16_t sc_flags; -}; Index: usb/if_urndis.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_urndis.c,v retrieving revision 1.21 diff -p -u -u -r1.21 if_urndis.c --- usb/if_urndis.c 5 Mar 2019 08:25:03 -0000 1.21 +++ usb/if_urndis.c 29 Aug 2019 06:22:05 -0000 @@ -28,28 +28,9 @@ __KERNEL_RCSID(0, "$NetBSD: if_urndis.c, #endif #include -#include -#include -#include -#include #include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include + +#include #include #include @@ -59,49 +40,16 @@ __KERNEL_RCSID(0, "$NetBSD: if_urndis.c, #define RNDIS_TX_LIST_CNT 1 #define RNDIS_BUFSZ 1562 -struct urndis_softc; - -struct urndis_chain { - struct urndis_softc *sc_softc; - struct usbd_xfer *sc_xfer; - char *sc_buf; - struct mbuf *sc_mbuf; - int sc_idx; -}; - -struct urndis_cdata { - struct urndis_chain sc_rx_chain[RNDIS_RX_LIST_CNT]; - struct urndis_chain sc_tx_chain[RNDIS_TX_LIST_CNT]; - int sc_tx_cnt; -}; - -#define GET_IFP(sc) (&(sc)->sc_ec.ec_if) struct urndis_softc { - device_t sc_dev; + struct usbnet sc_un; - char sc_attached; - int sc_dying; - struct ethercom sc_ec; + int sc_ifaceno_ctl; /* RNDIS device info */ uint32_t sc_filter; uint32_t sc_maxppt; uint32_t sc_maxtsz; uint32_t sc_palign; - - /* USB goo */ - struct usbd_device * sc_udev; - int sc_ifaceno_ctl; - struct usbd_interface * sc_iface_ctl; - struct usbd_interface * sc_iface_data; - - struct timeval sc_rx_notice; - int sc_bulkin_no; - struct usbd_pipe * sc_bulkin_pipe; - int sc_bulkout_no; - struct usbd_pipe * sc_bulkout_pipe; - - struct urndis_cdata sc_data; }; #ifdef URNDIS_DEBUG @@ -110,67 +58,42 @@ struct urndis_softc { #define DPRINTF(x) #endif -#define DEVNAME(sc) (device_xname(sc->sc_dev)) +#define DEVNAME(un) (device_xname(un->un_dev)) #define URNDIS_RESPONSE_LEN 0x400 - -static int urndis_newbuf(struct urndis_softc *, struct urndis_chain *); - -static int urndis_ioctl(struct ifnet *, unsigned long, void *); #if 0 static void urndis_watchdog(struct ifnet *); #endif -static void urndis_start(struct ifnet *); -static void urndis_rxeof(struct usbd_xfer *, void *, usbd_status); -static void urndis_txeof(struct usbd_xfer *, void *, usbd_status); -static int urndis_rx_list_init(struct urndis_softc *); -static int urndis_tx_list_init(struct urndis_softc *); - static int urndis_init(struct ifnet *); -static void urndis_stop(struct ifnet *); +static void urndis_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned urndis_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); + +static int urndis_init_un(struct ifnet *, struct usbnet *); -static usbd_status urndis_ctrl_msg(struct urndis_softc *, uint8_t, uint8_t, - uint16_t, uint16_t, void *, size_t); -static usbd_status urndis_ctrl_send(struct urndis_softc *, void *, size_t); -static struct rndis_comp_hdr *urndis_ctrl_recv(struct urndis_softc *); - -static uint32_t urndis_ctrl_handle(struct urndis_softc *, - struct rndis_comp_hdr *, void **, size_t *); -static uint32_t urndis_ctrl_handle_init(struct urndis_softc *, +static uint32_t urndis_ctrl_handle_init(struct usbnet *, const struct rndis_comp_hdr *); -static uint32_t urndis_ctrl_handle_query(struct urndis_softc *, +static uint32_t urndis_ctrl_handle_query(struct usbnet *, const struct rndis_comp_hdr *, void **, size_t *); -static uint32_t urndis_ctrl_handle_reset(struct urndis_softc *, +static uint32_t urndis_ctrl_handle_reset(struct usbnet *, const struct rndis_comp_hdr *); -static uint32_t urndis_ctrl_init(struct urndis_softc *); -#if 0 -static uint32_t urndis_ctrl_halt(struct urndis_softc *); -#endif -static uint32_t urndis_ctrl_query(struct urndis_softc *, uint32_t, void *, - size_t, void **, size_t *); -static uint32_t urndis_ctrl_set(struct urndis_softc *, uint32_t, void *, +static uint32_t urndis_ctrl_set(struct usbnet *, uint32_t, void *, size_t); -#if 0 -static uint32_t urndis_ctrl_set_param(struct urndis_softc *, const char *, - uint32_t, void *, size_t); -static uint32_t urndis_ctrl_reset(struct urndis_softc *); -static uint32_t urndis_ctrl_keepalive(struct urndis_softc *); -#endif - -static int urndis_encap(struct urndis_softc *, struct mbuf *, int); -static void urndis_decap(struct urndis_softc *, struct urndis_chain *, - uint32_t); static int urndis_match(device_t, cfdata_t, void *); static void urndis_attach(device_t, device_t, void *); -static int urndis_detach(device_t, int); -static int urndis_activate(device_t, enum devact); + +static struct usbnet_ops urndis_ops = { + .uno_init = urndis_init, + .uno_tx_prepare = urndis_tx_prepare, + .uno_rx_loop = urndis_rx_loop, +}; CFATTACH_DECL_NEW(urndis, sizeof(struct urndis_softc), - urndis_match, urndis_attach, urndis_detach, urndis_activate); + urndis_match, urndis_attach, usbnet_detach, usbnet_activate); /* * Supported devices that we can't match by class IDs. @@ -178,10 +101,11 @@ CFATTACH_DECL_NEW(urndis, sizeof(struct static const struct usb_devno urndis_devs[] = { { USB_VENDOR_HTC, USB_PRODUCT_HTC_ANDROID }, { USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_ANDROID2 }, + { USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_ANDROID }, }; static usbd_status -urndis_ctrl_msg(struct urndis_softc *sc, uint8_t rt, uint8_t r, +urndis_ctrl_msg(struct usbnet *un, uint8_t rt, uint8_t r, uint16_t index, uint16_t value, void *buf, size_t buflen) { usb_device_request_t req; @@ -192,52 +116,57 @@ urndis_ctrl_msg(struct urndis_softc *sc, USETW(req.wIndex, index); USETW(req.wLength, buflen); - return usbd_do_request(sc->sc_udev, &req, buf); + return usbd_do_request(un->un_udev, &req, buf); } static usbd_status -urndis_ctrl_send(struct urndis_softc *sc, void *buf, size_t len) +urndis_ctrl_send(struct usbnet *un, void *buf, size_t len) { + struct urndis_softc *sc = usbnet_softc(un); usbd_status err; - if (sc->sc_dying) + if (usbnet_isdying(un)) return(0); - err = urndis_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE, UR_GET_STATUS, + err = urndis_ctrl_msg(un, UT_WRITE_CLASS_INTERFACE, UR_GET_STATUS, sc->sc_ifaceno_ctl, 0, buf, len); if (err != USBD_NORMAL_COMPLETION) - printf("%s: %s\n", DEVNAME(sc), usbd_errstr(err)); + printf("%s: %s\n", DEVNAME(un), usbd_errstr(err)); return err; } static struct rndis_comp_hdr * -urndis_ctrl_recv(struct urndis_softc *sc) +urndis_ctrl_recv(struct usbnet *un) { + struct urndis_softc *sc = usbnet_softc(un); struct rndis_comp_hdr *hdr; char *buf; usbd_status err; + if (usbnet_isdying(un)) + return(0); + buf = kmem_alloc(URNDIS_RESPONSE_LEN, KM_SLEEP); - err = urndis_ctrl_msg(sc, UT_READ_CLASS_INTERFACE, UR_CLEAR_FEATURE, + err = urndis_ctrl_msg(un, UT_READ_CLASS_INTERFACE, UR_CLEAR_FEATURE, sc->sc_ifaceno_ctl, 0, buf, URNDIS_RESPONSE_LEN); if (err != USBD_NORMAL_COMPLETION && err != USBD_SHORT_XFER) { - printf("%s: %s\n", DEVNAME(sc), usbd_errstr(err)); + printf("%s: %s\n", DEVNAME(un), usbd_errstr(err)); kmem_free(buf, URNDIS_RESPONSE_LEN); return NULL; } hdr = (struct rndis_comp_hdr *)buf; DPRINTF(("%s: urndis_ctrl_recv: type 0x%x len %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(hdr->rm_type), le32toh(hdr->rm_len))); if (le32toh(hdr->rm_len) > URNDIS_RESPONSE_LEN) { printf("%s: ctrl message error: wrong size %u > %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(hdr->rm_len), URNDIS_RESPONSE_LEN); kmem_free(buf, URNDIS_RESPONSE_LEN); @@ -248,12 +177,12 @@ urndis_ctrl_recv(struct urndis_softc *sc } static uint32_t -urndis_ctrl_handle(struct urndis_softc *sc, struct rndis_comp_hdr *hdr, +urndis_ctrl_handle(struct usbnet *un, struct rndis_comp_hdr *hdr, void **buf, size_t *bufsz) { uint32_t rval; - DPRINTF(("%s: urndis_ctrl_handle\n", DEVNAME(sc))); + DPRINTF(("%s: urndis_ctrl_handle\n", DEVNAME(un))); if (buf && bufsz) { *buf = NULL; @@ -262,15 +191,15 @@ urndis_ctrl_handle(struct urndis_softc * switch (le32toh(hdr->rm_type)) { case REMOTE_NDIS_INITIALIZE_CMPLT: - rval = urndis_ctrl_handle_init(sc, hdr); + rval = urndis_ctrl_handle_init(un, hdr); break; case REMOTE_NDIS_QUERY_CMPLT: - rval = urndis_ctrl_handle_query(sc, hdr, buf, bufsz); + rval = urndis_ctrl_handle_query(un, hdr, buf, bufsz); break; case REMOTE_NDIS_RESET_CMPLT: - rval = urndis_ctrl_handle_reset(sc, hdr); + rval = urndis_ctrl_handle_reset(un, hdr); break; case REMOTE_NDIS_KEEPALIVE_CMPLT: @@ -280,7 +209,7 @@ urndis_ctrl_handle(struct urndis_softc * default: printf("%s: ctrl message error: unknown event 0x%x\n", - DEVNAME(sc), le32toh(hdr->rm_type)); + DEVNAME(un), le32toh(hdr->rm_type)); rval = RNDIS_STATUS_FAILURE; } @@ -290,9 +219,9 @@ urndis_ctrl_handle(struct urndis_softc * } static uint32_t -urndis_ctrl_handle_init(struct urndis_softc *sc, - const struct rndis_comp_hdr *hdr) +urndis_ctrl_handle_init(struct usbnet *un, const struct rndis_comp_hdr *hdr) { + struct urndis_softc *sc = usbnet_softc(un); const struct rndis_init_comp *msg; msg = (const struct rndis_init_comp *) hdr; @@ -300,7 +229,7 @@ urndis_ctrl_handle_init(struct urndis_so DPRINTF(("%s: urndis_ctrl_handle_init: len %u rid %u status 0x%x " "ver_major %u ver_minor %u devflags 0x%x medium 0x%x pktmaxcnt %u " "pktmaxsz %u align %u aflistoffset %u aflistsz %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_len), le32toh(msg->rm_rid), le32toh(msg->rm_status), @@ -316,7 +245,7 @@ urndis_ctrl_handle_init(struct urndis_so if (le32toh(msg->rm_status) != RNDIS_STATUS_SUCCESS) { printf("%s: init failed 0x%x\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_status)); return le32toh(msg->rm_status); @@ -324,7 +253,7 @@ urndis_ctrl_handle_init(struct urndis_so if (le32toh(msg->rm_devflags) != RNDIS_DF_CONNECTIONLESS) { printf("%s: wrong device type (current type: 0x%x)\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_devflags)); return RNDIS_STATUS_FAILURE; @@ -332,7 +261,7 @@ urndis_ctrl_handle_init(struct urndis_so if (le32toh(msg->rm_medium) != RNDIS_MEDIUM_802_3) { printf("%s: medium not 802.3 (current medium: 0x%x)\n", - DEVNAME(sc), le32toh(msg->rm_medium)); + DEVNAME(un), le32toh(msg->rm_medium)); return RNDIS_STATUS_FAILURE; } @@ -340,7 +269,7 @@ urndis_ctrl_handle_init(struct urndis_so if (le32toh(msg->rm_ver_major) != RNDIS_MAJOR_VERSION || le32toh(msg->rm_ver_minor) != RNDIS_MINOR_VERSION) { printf("%s: version not %u.%u (current version: %u.%u)\n", - DEVNAME(sc), RNDIS_MAJOR_VERSION, RNDIS_MINOR_VERSION, + DEVNAME(un), RNDIS_MAJOR_VERSION, RNDIS_MINOR_VERSION, le32toh(msg->rm_ver_major), le32toh(msg->rm_ver_minor)); return RNDIS_STATUS_FAILURE; @@ -354,7 +283,7 @@ urndis_ctrl_handle_init(struct urndis_so } static uint32_t -urndis_ctrl_handle_query(struct urndis_softc *sc, +urndis_ctrl_handle_query(struct usbnet *un, const struct rndis_comp_hdr *hdr, void **buf, size_t *bufsz) { const struct rndis_query_comp *msg; @@ -363,7 +292,7 @@ urndis_ctrl_handle_query(struct urndis_s DPRINTF(("%s: urndis_ctrl_handle_query: len %u rid %u status 0x%x " "buflen %u bufoff %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_len), le32toh(msg->rm_rid), le32toh(msg->rm_status), @@ -377,7 +306,7 @@ urndis_ctrl_handle_query(struct urndis_s if (le32toh(msg->rm_status) != RNDIS_STATUS_SUCCESS) { printf("%s: query failed 0x%x\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_status)); return le32toh(msg->rm_status); @@ -388,7 +317,7 @@ urndis_ctrl_handle_query(struct urndis_s printf("%s: ctrl message error: invalid query info " "len/offset/end_position(%u/%u/%u) -> " "go out of buffer limit %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_infobuflen), le32toh(msg->rm_infobufoffset), le32toh(msg->rm_infobuflen) + @@ -412,9 +341,9 @@ urndis_ctrl_handle_query(struct urndis_s } static uint32_t -urndis_ctrl_handle_reset(struct urndis_softc *sc, - const struct rndis_comp_hdr *hdr) +urndis_ctrl_handle_reset(struct usbnet *un, const struct rndis_comp_hdr *hdr) { + struct urndis_softc *sc = usbnet_softc(un); const struct rndis_reset_comp *msg; uint32_t rval; @@ -424,13 +353,13 @@ urndis_ctrl_handle_reset(struct urndis_s DPRINTF(("%s: urndis_ctrl_handle_reset: len %u status 0x%x " "adrreset %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_len), rval, le32toh(msg->rm_adrreset))); if (rval != RNDIS_STATUS_SUCCESS) { - printf("%s: reset failed 0x%x\n", DEVNAME(sc), rval); + printf("%s: reset failed 0x%x\n", DEVNAME(un), rval); return rval; } @@ -438,11 +367,11 @@ urndis_ctrl_handle_reset(struct urndis_s uint32_t filter; filter = htole32(sc->sc_filter); - rval = urndis_ctrl_set(sc, OID_GEN_CURRENT_PACKET_FILTER, + rval = urndis_ctrl_set(un, OID_GEN_CURRENT_PACKET_FILTER, &filter, sizeof(filter)); if (rval != RNDIS_STATUS_SUCCESS) { printf("%s: unable to reset data filters\n", - DEVNAME(sc)); + DEVNAME(un)); return rval; } } @@ -451,7 +380,7 @@ urndis_ctrl_handle_reset(struct urndis_s } static uint32_t -urndis_ctrl_init(struct urndis_softc *sc) +urndis_ctrl_init(struct usbnet *un) { struct rndis_init_req *msg; uint32_t rval; @@ -467,7 +396,7 @@ urndis_ctrl_init(struct urndis_softc *sc DPRINTF(("%s: urndis_ctrl_init send: type %u len %u rid %u ver_major %u " "ver_minor %u max_xfersz %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_type), le32toh(msg->rm_len), le32toh(msg->rm_rid), @@ -475,26 +404,26 @@ urndis_ctrl_init(struct urndis_softc *sc le32toh(msg->rm_ver_minor), le32toh(msg->rm_max_xfersz))); - rval = urndis_ctrl_send(sc, msg, sizeof(*msg)); + rval = urndis_ctrl_send(un, msg, sizeof(*msg)); kmem_free(msg, sizeof(*msg)); if (rval != RNDIS_STATUS_SUCCESS) { - printf("%s: init failed\n", DEVNAME(sc)); + printf("%s: init failed\n", DEVNAME(un)); return rval; } - if ((hdr = urndis_ctrl_recv(sc)) == NULL) { - printf("%s: unable to get init response\n", DEVNAME(sc)); + if ((hdr = urndis_ctrl_recv(un)) == NULL) { + printf("%s: unable to get init response\n", DEVNAME(un)); return RNDIS_STATUS_FAILURE; } - rval = urndis_ctrl_handle(sc, hdr, NULL, NULL); + rval = urndis_ctrl_handle(un, hdr, NULL, NULL); return rval; } #if 0 static uint32_t -urndis_ctrl_halt(struct urndis_softc *sc) +urndis_ctrl_halt(struct usbnet *un) { struct rndis_halt_req *msg; uint32_t rval; @@ -505,23 +434,23 @@ urndis_ctrl_halt(struct urndis_softc *sc msg->rm_rid = 0; DPRINTF(("%s: urndis_ctrl_halt send: type %u len %u rid %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_type), le32toh(msg->rm_len), le32toh(msg->rm_rid))); - rval = urndis_ctrl_send(sc, msg, sizeof(*msg)); + rval = urndis_ctrl_send(un, msg, sizeof(*msg)); kmem_free(msg, sizeof(*msg)); if (rval != RNDIS_STATUS_SUCCESS) - printf("%s: halt failed\n", DEVNAME(sc)); + printf("%s: halt failed\n", DEVNAME(un)); return rval; } #endif static uint32_t -urndis_ctrl_query(struct urndis_softc *sc, uint32_t oid, +urndis_ctrl_query(struct usbnet *un, uint32_t oid, void *qbuf, size_t qlen, void **rbuf, size_t *rbufsz) { @@ -544,7 +473,7 @@ urndis_ctrl_query(struct urndis_softc *s DPRINTF(("%s: urndis_ctrl_query send: type %u len %u rid %u oid 0x%x " "infobuflen %u infobufoffset %u devicevchdl %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_type), le32toh(msg->rm_len), le32toh(msg->rm_rid), @@ -553,25 +482,25 @@ urndis_ctrl_query(struct urndis_softc *s le32toh(msg->rm_infobufoffset), le32toh(msg->rm_devicevchdl))); - rval = urndis_ctrl_send(sc, msg, sizeof(*msg)); + rval = urndis_ctrl_send(un, msg, sizeof(*msg)); kmem_free(msg, sizeof(*msg) + qlen); if (rval != RNDIS_STATUS_SUCCESS) { - printf("%s: query failed\n", DEVNAME(sc)); + printf("%s: query failed\n", DEVNAME(un)); return rval; } - if ((hdr = urndis_ctrl_recv(sc)) == NULL) { - printf("%s: unable to get query response\n", DEVNAME(sc)); + if ((hdr = urndis_ctrl_recv(un)) == NULL) { + printf("%s: unable to get query response\n", DEVNAME(un)); return RNDIS_STATUS_FAILURE; } - rval = urndis_ctrl_handle(sc, hdr, rbuf, rbufsz); + rval = urndis_ctrl_handle(un, hdr, rbuf, rbufsz); return rval; } static uint32_t -urndis_ctrl_set(struct urndis_softc *sc, uint32_t oid, void *buf, size_t len) +urndis_ctrl_set(struct usbnet *un, uint32_t oid, void *buf, size_t len) { struct rndis_set_req *msg; uint32_t rval; @@ -592,7 +521,7 @@ urndis_ctrl_set(struct urndis_softc *sc, DPRINTF(("%s: urndis_ctrl_set send: type %u len %u rid %u oid 0x%x " "infobuflen %u infobufoffset %u devicevchdl %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_type), le32toh(msg->rm_len), le32toh(msg->rm_rid), @@ -601,28 +530,28 @@ urndis_ctrl_set(struct urndis_softc *sc, le32toh(msg->rm_infobufoffset), le32toh(msg->rm_devicevchdl))); - rval = urndis_ctrl_send(sc, msg, sizeof(*msg)); + rval = urndis_ctrl_send(un, msg, sizeof(*msg)); kmem_free(msg, sizeof(*msg) + len); if (rval != RNDIS_STATUS_SUCCESS) { - printf("%s: set failed\n", DEVNAME(sc)); + printf("%s: set failed\n", DEVNAME(un)); return rval; } - if ((hdr = urndis_ctrl_recv(sc)) == NULL) { - printf("%s: unable to get set response\n", DEVNAME(sc)); + if ((hdr = urndis_ctrl_recv(un)) == NULL) { + printf("%s: unable to get set response\n", DEVNAME(un)); return RNDIS_STATUS_FAILURE; } - rval = urndis_ctrl_handle(sc, hdr, NULL, NULL); + rval = urndis_ctrl_handle(un, hdr, NULL, NULL); if (rval != RNDIS_STATUS_SUCCESS) - printf("%s: set failed 0x%x\n", DEVNAME(sc), rval); + printf("%s: set failed 0x%x\n", DEVNAME(un), rval); return rval; } #if 0 static uint32_t -urndis_ctrl_set_param(struct urndis_softc *sc, +urndis_ctrl_set_param(struct urndis_softc *un, const char *name, uint32_t type, void *buf, @@ -654,24 +583,24 @@ urndis_ctrl_set_param(struct urndis_soft DPRINTF(("%s: urndis_ctrl_set_param send: nameoffset %u namelen %u " "type 0x%x valueoffset %u valuelen %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(param->rm_nameoffset), le32toh(param->rm_namelen), le32toh(param->rm_type), le32toh(param->rm_valueoffset), le32toh(param->rm_valuelen))); - rval = urndis_ctrl_set(sc, OID_GEN_RNDIS_CONFIG_PARAMETER, param, tlen); + rval = urndis_ctrl_set(un, OID_GEN_RNDIS_CONFIG_PARAMETER, param, tlen); kmem_free(param, tlen); if (rval != RNDIS_STATUS_SUCCESS) - printf("%s: set param failed 0x%x\n", DEVNAME(sc), rval); + printf("%s: set param failed 0x%x\n", DEVNAME(un), rval); return rval; } /* XXX : adrreset, get it from response */ static uint32_t -urndis_ctrl_reset(struct urndis_softc *sc) +urndis_ctrl_reset(struct usbnet *un) { struct rndis_reset_req *reset; uint32_t rval; @@ -683,30 +612,30 @@ urndis_ctrl_reset(struct urndis_softc *s reset->rm_rid = 0; /* XXX rm_rid == reserved ... remove ? */ DPRINTF(("%s: urndis_ctrl_reset send: type %u len %u rid %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(reset->rm_type), le32toh(reset->rm_len), le32toh(reset->rm_rid))); - rval = urndis_ctrl_send(sc, reset, sizeof(*reset)); + rval = urndis_ctrl_send(un, reset, sizeof(*reset)); kmem_free(reset, sizeof(*reset)); if (rval != RNDIS_STATUS_SUCCESS) { - printf("%s: reset failed\n", DEVNAME(sc)); + printf("%s: reset failed\n", DEVNAME(un)); return rval; } - if ((hdr = urndis_ctrl_recv(sc)) == NULL) { - printf("%s: unable to get reset response\n", DEVNAME(sc)); + if ((hdr = urndis_ctrl_recv(un)) == NULL) { + printf("%s: unable to get reset response\n", DEVNAME(un)); return RNDIS_STATUS_FAILURE; } - rval = urndis_ctrl_handle(sc, hdr, NULL, NULL); + rval = urndis_ctrl_handle(un, hdr, NULL, NULL); return rval; } static uint32_t -urndis_ctrl_keepalive(struct urndis_softc *sc) +urndis_ctrl_keepalive(struct usbnet *un) { struct rndis_keepalive_req *keep; uint32_t rval; @@ -718,43 +647,44 @@ urndis_ctrl_keepalive(struct urndis_soft keep->rm_rid = 0; /* XXX rm_rid == reserved ... remove ? */ DPRINTF(("%s: urndis_ctrl_keepalive: type %u len %u rid %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(keep->rm_type), le32toh(keep->rm_len), le32toh(keep->rm_rid))); - rval = urndis_ctrl_send(sc, keep, sizeof(*keep)); + rval = urndis_ctrl_send(un, keep, sizeof(*keep)); kmem_free(keep, sizeof(*keep)); if (rval != RNDIS_STATUS_SUCCESS) { - printf("%s: keepalive failed\n", DEVNAME(sc)); + printf("%s: keepalive failed\n", DEVNAME(un)); return rval; } - if ((hdr = urndis_ctrl_recv(sc)) == NULL) { - printf("%s: unable to get keepalive response\n", DEVNAME(sc)); + if ((hdr = urndis_ctrl_recv(un)) == NULL) { + printf("%s: unable to get keepalive response\n", DEVNAME(un)); return RNDIS_STATUS_FAILURE; } - rval = urndis_ctrl_handle(sc, hdr, NULL, NULL); + rval = urndis_ctrl_handle(un, hdr, NULL, NULL); if (rval != RNDIS_STATUS_SUCCESS) { - printf("%s: keepalive failed 0x%x\n", DEVNAME(sc), rval); - urndis_ctrl_reset(sc); + printf("%s: keepalive failed 0x%x\n", DEVNAME(un), rval); + urndis_ctrl_reset(un); } return rval; } #endif -static int -urndis_encap(struct urndis_softc *sc, struct mbuf *m, int idx) +static unsigned +urndis_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - struct urndis_chain *c; - usbd_status err; struct rndis_packet_msg *msg; - c = &sc->sc_data.sc_tx_chain[idx]; + usbnet_isowned_tx(un); + + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - sizeof(*msg)) + return 0; - msg = (struct rndis_packet_msg *)c->sc_buf; + msg = (struct rndis_packet_msg *)c->unc_buf; memset(msg, 0, sizeof(*msg)); msg->rm_type = htole32(REMOTE_NDIS_PACKET_MSG); @@ -766,61 +696,44 @@ urndis_encap(struct urndis_softc *sc, st m_copydata(m, 0, m->m_pkthdr.len, ((char*)msg + RNDIS_DATA_OFFSET + RNDIS_HEADER_OFFSET)); - DPRINTF(("%s: urndis_encap type 0x%x len %u data(off %u len %u)\n", - DEVNAME(sc), + DPRINTF(("%s: %s type 0x%x len %u data(off %u len %u)\n", + __func__, + DEVNAME(un), le32toh(msg->rm_type), le32toh(msg->rm_len), le32toh(msg->rm_dataoffset), le32toh(msg->rm_datalen))); - c->sc_mbuf = m; - - usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, le32toh(msg->rm_len), - USBD_FORCE_SHORT_XFER, 10000, urndis_txeof); - - /* Transmit */ - err = usbd_transfer(c->sc_xfer); - if (err != USBD_IN_PROGRESS) { - urndis_stop(GET_IFP(sc)); - return(EIO); - } - - sc->sc_data.sc_tx_cnt++; - - return(0); + return le32toh(msg->rm_len); } static void -urndis_decap(struct urndis_softc *sc, struct urndis_chain *c, uint32_t len) +urndis_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len) { - struct mbuf *m; struct rndis_packet_msg *msg; - struct ifnet *ifp; - int s; + struct ifnet *ifp = usbnet_ifp(un); int offset; - ifp = GET_IFP(sc); offset = 0; - while (len > 1) { - msg = (struct rndis_packet_msg *)((char*)c->sc_buf + offset); - m = c->sc_mbuf; + while (total_len > 1) { + msg = (struct rndis_packet_msg *)((char*)c->unc_buf + offset); - DPRINTF(("%s: urndis_decap buffer size left %u\n", DEVNAME(sc), - len)); + DPRINTF(("%s: %s buffer size left %u\n", DEVNAME(un), __func__, + total_len)); - if (len < sizeof(*msg)) { - printf("%s: urndis_decap invalid buffer len %u < " + if (total_len < sizeof(*msg)) { + printf("%s: urndis_decap invalid buffer total_len %u < " "minimum header %zu\n", - DEVNAME(sc), - len, + DEVNAME(un), + total_len, sizeof(*msg)); return; } - DPRINTF(("%s: urndis_decap len %u data(off:%u len:%u) " + DPRINTF(("%s: urndis_decap total_len %u data(off:%u len:%u) " "oobdata(off:%u len:%u nb:%u) perpacket(off:%u len:%u)\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_len), le32toh(msg->rm_dataoffset), le32toh(msg->rm_datalen), @@ -832,24 +745,24 @@ urndis_decap(struct urndis_softc *sc, st if (le32toh(msg->rm_type) != REMOTE_NDIS_PACKET_MSG) { printf("%s: urndis_decap invalid type 0x%x != 0x%x\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_type), REMOTE_NDIS_PACKET_MSG); return; } if (le32toh(msg->rm_len) < sizeof(*msg)) { printf("%s: urndis_decap invalid msg len %u < %zu\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_len), sizeof(*msg)); return; } - if (le32toh(msg->rm_len) > len) { + if (le32toh(msg->rm_len) > total_len) { printf("%s: urndis_decap invalid msg len %u > buffer " - "len %u\n", - DEVNAME(sc), + "total_len %u\n", + DEVNAME(un), le32toh(msg->rm_len), - len); + total_len); return; } @@ -859,7 +772,7 @@ urndis_decap(struct urndis_softc *sc, st printf("%s: urndis_decap invalid data " "len/offset/end_position(%u/%u/%u) -> " "go out of receive buffer limit %u\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_datalen), le32toh(msg->rm_dataoffset), le32toh(msg->rm_dataoffset) + @@ -872,437 +785,68 @@ urndis_decap(struct urndis_softc *sc, st ifp->if_ierrors++; printf("%s: urndis_decap invalid ethernet size " "%d < %zu\n", - DEVNAME(sc), + DEVNAME(un), le32toh(msg->rm_datalen), sizeof(struct ether_header)); return; } - memcpy(mtod(m, char*), + usbnet_enqueue(un, ((char*)&msg->rm_dataoffset + le32toh(msg->rm_dataoffset)), - le32toh(msg->rm_datalen)); - m->m_pkthdr.len = m->m_len = le32toh(msg->rm_datalen); - - m_set_rcvif(m, ifp); - - s = splnet(); - - if (urndis_newbuf(sc, c) == ENOBUFS) { - ifp->if_ierrors++; - } else { - if_percpuq_enqueue(ifp->if_percpuq, m); - } - splx(s); + le32toh(msg->rm_datalen), 0, 0, 0); offset += le32toh(msg->rm_len); - len -= le32toh(msg->rm_len); + total_len -= le32toh(msg->rm_len); } } -static int -urndis_newbuf(struct urndis_softc *sc, struct urndis_chain *c) -{ - struct mbuf *m_new = NULL; - - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - printf("%s: no memory for rx list -- packet dropped!\n", - DEVNAME(sc)); - return ENOBUFS; - } - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - printf("%s: no memory for rx list -- packet dropped!\n", - DEVNAME(sc)); - m_freem(m_new); - return ENOBUFS; - } - m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; - - m_adj(m_new, ETHER_ALIGN); - c->sc_mbuf = m_new; - return 0; -} - -static int -urndis_rx_list_init(struct urndis_softc *sc) -{ - struct urndis_cdata *cd; - struct urndis_chain *c; - int i; - - cd = &sc->sc_data; - for (i = 0; i < RNDIS_RX_LIST_CNT; i++) { - c = &cd->sc_rx_chain[i]; - c->sc_softc = sc; - c->sc_idx = i; - - if (urndis_newbuf(sc, c) == ENOBUFS) - return ENOBUFS; - - if (c->sc_xfer == NULL) { - int err = usbd_create_xfer(sc->sc_bulkin_pipe, - RNDIS_BUFSZ, 0, 0, &c->sc_xfer); - if (err) - return err; - c->sc_buf = usbd_get_buffer(c->sc_xfer); - } - } - - return 0; -} - -static int -urndis_tx_list_init(struct urndis_softc *sc) -{ - struct urndis_cdata *cd; - struct urndis_chain *c; - int i; - - cd = &sc->sc_data; - for (i = 0; i < RNDIS_TX_LIST_CNT; i++) { - c = &cd->sc_tx_chain[i]; - c->sc_softc = sc; - c->sc_idx = i; - c->sc_mbuf = NULL; - if (c->sc_xfer == NULL) { - int err = usbd_create_xfer(sc->sc_bulkout_pipe, - RNDIS_BUFSZ, USBD_FORCE_SHORT_XFER, 0, &c->sc_xfer); - if (err) - return err; - c->sc_buf = usbd_get_buffer(c->sc_xfer); - } - } - return 0; -} - -static int -urndis_ioctl(struct ifnet *ifp, unsigned long command, void *data) -{ - struct urndis_softc *sc; - int s, error; - - sc = ifp->if_softc; - error = 0; - - if (sc->sc_dying) - return EIO; - - s = splnet(); - - switch(command) { - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - if (ifp->if_flags & IFF_UP) { - if (!(ifp->if_flags & IFF_RUNNING)) - urndis_init(ifp); - } else { - if (ifp->if_flags & IFF_RUNNING) - urndis_stop(ifp); - } - error = 0; - break; - - default: - error = ether_ioctl(ifp, command, data); - break; - } - - if (error == ENETRESET) - error = 0; - - splx(s); - return error; -} - #if 0 static void urndis_watchdog(struct ifnet *ifp) { - struct urndis_softc *sc; + struct urndis_softc *sc = usbnet_softc(un); - sc = ifp->if_softc; - - if (sc->sc_dying) + if (un->un_dying) return; ifp->if_oerrors++; - printf("%s: watchdog timeout\n", DEVNAME(sc)); + printf("%s: watchdog timeout\n", DEVNAME(un)); - urndis_ctrl_keepalive(sc); + urndis_ctrl_keepalive(un); } #endif static int -urndis_init(struct ifnet *ifp) +urndis_init_un(struct ifnet *ifp, struct usbnet *un) { - struct urndis_softc *sc; - int i, s; int err; - usbd_status usberr; - - sc = ifp->if_softc; if (ifp->if_flags & IFF_RUNNING) return 0; - err = urndis_ctrl_init(sc); + err = urndis_ctrl_init(un); if (err != RNDIS_STATUS_SUCCESS) return EIO; - s = splnet(); - - usberr = usbd_open_pipe(sc->sc_iface_data, sc->sc_bulkin_no, - USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe); - if (usberr) { - printf("%s: open rx pipe failed: %s\n", DEVNAME(sc), - usbd_errstr(err)); - splx(s); - return EIO; - } - - usberr = usbd_open_pipe(sc->sc_iface_data, sc->sc_bulkout_no, - USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe); - if (usberr) { - printf("%s: open tx pipe failed: %s\n", DEVNAME(sc), - usbd_errstr(err)); - splx(s); - return EIO; - } - - err = urndis_tx_list_init(sc); - if (err) { - printf("%s: tx list init failed\n", - DEVNAME(sc)); - splx(s); - return err; - } - - err = urndis_rx_list_init(sc); - if (err) { - printf("%s: rx list init failed\n", - DEVNAME(sc)); - splx(s); - return err; - } - - for (i = 0; i < RNDIS_RX_LIST_CNT; i++) { - struct urndis_chain *c; - - c = &sc->sc_data.sc_rx_chain[i]; - - usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, RNDIS_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, urndis_rxeof); - usbd_transfer(c->sc_xfer); - } - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); - return 0; -} - -static void -urndis_stop(struct ifnet *ifp) -{ - struct urndis_softc *sc; - usbd_status err; - int i; - - sc = ifp->if_softc; - - ifp->if_timer = 0; - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); - - if (sc->sc_bulkin_pipe != NULL) { - err = usbd_abort_pipe(sc->sc_bulkin_pipe); - if (err) - printf("%s: abort rx pipe failed: %s\n", - DEVNAME(sc), usbd_errstr(err)); - } - - if (sc->sc_bulkout_pipe != NULL) { - err = usbd_abort_pipe(sc->sc_bulkout_pipe); - if (err) - printf("%s: abort tx pipe failed: %s\n", - DEVNAME(sc), usbd_errstr(err)); - } - - for (i = 0; i < RNDIS_RX_LIST_CNT; i++) { - if (sc->sc_data.sc_rx_chain[i].sc_mbuf != NULL) { - m_freem(sc->sc_data.sc_rx_chain[i].sc_mbuf); - sc->sc_data.sc_rx_chain[i].sc_mbuf = NULL; - } - if (sc->sc_data.sc_rx_chain[i].sc_xfer != NULL) { - usbd_destroy_xfer(sc->sc_data.sc_rx_chain[i].sc_xfer); - sc->sc_data.sc_rx_chain[i].sc_xfer = NULL; - } - } - - for (i = 0; i < RNDIS_TX_LIST_CNT; i++) { - if (sc->sc_data.sc_tx_chain[i].sc_mbuf != NULL) { - m_freem(sc->sc_data.sc_tx_chain[i].sc_mbuf); - sc->sc_data.sc_tx_chain[i].sc_mbuf = NULL; - } - if (sc->sc_data.sc_tx_chain[i].sc_xfer != NULL) { - usbd_destroy_xfer(sc->sc_data.sc_tx_chain[i].sc_xfer); - sc->sc_data.sc_tx_chain[i].sc_xfer = NULL; - } - } - - /* Close pipes. */ - if (sc->sc_bulkin_pipe != NULL) { - err = usbd_close_pipe(sc->sc_bulkin_pipe); - if (err) - printf("%s: close rx pipe failed: %s\n", - DEVNAME(sc), usbd_errstr(err)); - sc->sc_bulkin_pipe = NULL; + usbnet_lock(un); + if (usbnet_isdying(un)) + err = EIO; + else { + usbnet_stop(un, ifp, 1); + err = usbnet_init_rx_tx(un); + usbnet_set_link(un, err == 0); } + usbnet_unlock(un); - if (sc->sc_bulkout_pipe != NULL) { - err = usbd_close_pipe(sc->sc_bulkout_pipe); - if (err) - printf("%s: close tx pipe failed: %s\n", - DEVNAME(sc), usbd_errstr(err)); - sc->sc_bulkout_pipe = NULL; - } + return err; } -static void -urndis_start(struct ifnet *ifp) +static int +urndis_init(struct ifnet *ifp) { - struct urndis_softc *sc; - struct mbuf *m_head = NULL; - - sc = ifp->if_softc; + struct usbnet *un = ifp->if_softc; - if (sc->sc_dying || (ifp->if_flags & IFF_OACTIVE)) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (urndis_encap(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - /* - * If there's a BPF listener, bounce a copy of this frame - * to him. - */ - bpf_mtap(ifp, m_head, BPF_D_OUT); - - ifp->if_flags |= IFF_OACTIVE; - - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 5; - - return; -} - -static void -urndis_rxeof(struct usbd_xfer *xfer, - void *priv, - usbd_status status) -{ - struct urndis_chain *c; - struct urndis_softc *sc; - struct ifnet *ifp; - uint32_t total_len; - - c = priv; - sc = c->sc_softc; - ifp = GET_IFP(sc); - total_len = 0; - - if (sc->sc_dying || !(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: usb errors on rx: %s\n", - DEVNAME(sc), usbd_errstr(status)); - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe); - - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - urndis_decap(sc, c, total_len); - -done: - /* Setup new transfer. */ - usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, RNDIS_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, urndis_rxeof); - usbd_transfer(c->sc_xfer); -} - -static void -urndis_txeof(struct usbd_xfer *xfer, - void *priv, - usbd_status status) -{ - struct urndis_chain *c; - struct urndis_softc *sc; - struct ifnet *ifp; - usbd_status err; - int s; - - c = priv; - sc = c->sc_softc; - ifp = GET_IFP(sc); - - DPRINTF(("%s: urndis_txeof\n", DEVNAME(sc))); - - if (sc->sc_dying) - return; - - s = splnet(); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", DEVNAME(sc), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_bulkout_pipe); - splx(s); - return; - } - - usbd_get_xfer_status(c->sc_xfer, NULL, NULL, NULL, &err); - - if (c->sc_mbuf != NULL) { - m_freem(c->sc_mbuf); - c->sc_mbuf = NULL; - } - - if (err) - ifp->if_oerrors++; - else - ifp->if_opackets++; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - urndis_start(ifp); - - splx(s); + return urndis_init_un(ifp, un); } static int @@ -1330,43 +874,51 @@ urndis_match(device_t parent, cfdata_t m static void urndis_attach(device_t parent, device_t self, void *aux) { - struct urndis_softc *sc; - struct usbif_attach_arg *uiaa; - struct ifnet *ifp; + struct urndis_softc *sc = device_private(self); + struct usbnet * const un = &sc->sc_un; + struct usbif_attach_arg *uiaa = aux; + struct usbd_device *dev = uiaa->uiaa_device; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; usb_config_descriptor_t *cd; + struct usbd_interface *iface_ctl; const usb_cdc_union_descriptor_t *ud; const usb_cdc_header_descriptor_t *desc; usbd_desc_iter_t iter; int if_ctl, if_data; int i, j, altcnt; - int s; - u_char eaddr[ETHER_ADDR_LEN]; void *buf; size_t bufsz; uint32_t filter; char *devinfop; - sc = device_private(self); - uiaa = aux; - sc->sc_dev = self; - sc->sc_udev = uiaa->uiaa_device; + KASSERT((void *)sc == un); aprint_naive("\n"); aprint_normal("\n"); - - devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0); + devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); - sc->sc_iface_ctl = uiaa->uiaa_iface; - id = usbd_get_interface_descriptor(sc->sc_iface_ctl); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &urndis_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = RNDIS_RX_LIST_CNT; + un->un_tx_list_cnt = RNDIS_TX_LIST_CNT; + un->un_rx_bufsz = RNDIS_BUFSZ; + un->un_tx_bufsz = RNDIS_BUFSZ; + + iface_ctl = uiaa->uiaa_iface; + un->un_iface = uiaa->uiaa_iface; + id = usbd_get_interface_descriptor(iface_ctl); if_ctl = id->bInterfaceNumber; sc->sc_ifaceno_ctl = if_ctl; if_data = -1; - usb_desc_iter_init(sc->sc_udev, &iter); + usb_desc_iter_init(un->un_udev, &iter); while ((desc = (const void *)usb_desc_iter_next(&iter)) != NULL) { if (desc->bDescriptorType != UDESC_CS_INTERFACE) { @@ -1384,7 +936,7 @@ urndis_attach(device_t parent, device_t if (if_data == -1) { DPRINTF(("urndis_attach: no union interface\n")); - sc->sc_iface_data = sc->sc_iface_ctl; + un->un_iface = iface_ctl; } else { DPRINTF(("urndis_attach: union interface: ctl %u, data %u\n", if_ctl, if_data)); @@ -1394,104 +946,94 @@ urndis_attach(device_t parent, device_t uiaa->uiaa_ifaces[i]); if (id != NULL && id->bInterfaceNumber == if_data) { - sc->sc_iface_data = uiaa->uiaa_ifaces[i]; + un->un_iface = uiaa->uiaa_ifaces[i]; uiaa->uiaa_ifaces[i] = NULL; } } } } - if (sc->sc_iface_data == NULL) { - aprint_error("%s: no data interface\n", DEVNAME(sc)); + if (un->un_iface == NULL) { + aprint_error("%s: no data interface\n", DEVNAME(un)); return; } - id = usbd_get_interface_descriptor(sc->sc_iface_data); - cd = usbd_get_config_descriptor(sc->sc_udev); + id = usbd_get_interface_descriptor(un->un_iface); + cd = usbd_get_config_descriptor(un->un_udev); altcnt = usbd_get_no_alts(cd, id->bInterfaceNumber); for (j = 0; j < altcnt; j++) { - if (usbd_set_interface(sc->sc_iface_data, j)) { + if (usbd_set_interface(un->un_iface, j)) { aprint_error("%s: interface alternate setting %u " - "failed\n", DEVNAME(sc), j); + "failed\n", DEVNAME(un), j); return; } /* Find endpoints. */ - id = usbd_get_interface_descriptor(sc->sc_iface_data); - sc->sc_bulkin_no = sc->sc_bulkout_no = -1; + id = usbd_get_interface_descriptor(un->un_iface); + un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 0; for (i = 0; i < id->bNumEndpoints; i++) { ed = usbd_interface2endpoint_descriptor( - sc->sc_iface_data, i); + un->un_iface, i); if (!ed) { aprint_error("%s: no descriptor for bulk " - "endpoint %u\n", DEVNAME(sc), i); + "endpoint %u\n", DEVNAME(un), i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_bulkin_no = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if ( UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_bulkout_no = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } } - if (sc->sc_bulkin_no != -1 && sc->sc_bulkout_no != -1) { + if (un->un_ed[USBNET_ENDPT_RX] != 0 && un->un_ed[USBNET_ENDPT_TX] != 0) { DPRINTF(("%s: in=0x%x, out=0x%x\n", - DEVNAME(sc), - sc->sc_bulkin_no, - sc->sc_bulkout_no)); - goto found; + DEVNAME(un), + un->un_ed[USBNET_ENDPT_RX], + un->un_ed[USBNET_ENDPT_TX])); + break; } } - if (sc->sc_bulkin_no == -1) - aprint_error("%s: could not find data bulk in\n", DEVNAME(sc)); - if (sc->sc_bulkout_no == -1 ) - aprint_error("%s: could not find data bulk out\n",DEVNAME(sc)); - return; - - found: - - ifp = GET_IFP(sc); - ifp->if_softc = sc; - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_start = urndis_start; - ifp->if_ioctl = urndis_ioctl; - ifp->if_init = urndis_init; + if (un->un_ed[USBNET_ENDPT_RX] == 0) + aprint_error("%s: could not find data bulk in\n", DEVNAME(un)); + if (un->un_ed[USBNET_ENDPT_TX] == 0) + aprint_error("%s: could not find data bulk out\n",DEVNAME(un)); + if (un->un_ed[USBNET_ENDPT_RX] == 0 || un->un_ed[USBNET_ENDPT_TX] == 0) + return; + #if 0 ifp->if_watchdog = urndis_watchdog; #endif - strlcpy(ifp->if_xname, DEVNAME(sc), IFNAMSIZ); - - IFQ_SET_READY(&ifp->if_snd); - - urndis_init(ifp); + usbnet_attach(un, "urndisdet"); - s = splnet(); + struct ifnet *ifp = usbnet_ifp(un); + urndis_init_un(ifp, un); - if (urndis_ctrl_query(sc, OID_802_3_PERMANENT_ADDRESS, NULL, 0, + if (urndis_ctrl_query(un, OID_802_3_PERMANENT_ADDRESS, NULL, 0, &buf, &bufsz) != RNDIS_STATUS_SUCCESS) { aprint_error("%s: unable to get hardware address\n", - DEVNAME(sc)); - urndis_stop(ifp); - splx(s); + DEVNAME(un)); + usbnet_lock(un); + usbnet_stop(un, ifp, 1); + usbnet_unlock(un); return; } if (bufsz == ETHER_ADDR_LEN) { - memcpy(eaddr, buf, ETHER_ADDR_LEN); - aprint_normal("%s: address %s\n", DEVNAME(sc), - ether_sprintf(eaddr)); + memcpy(un->un_eaddr, buf, ETHER_ADDR_LEN); kmem_free(buf, bufsz); } else { - aprint_error("%s: invalid address\n", DEVNAME(sc)); + aprint_error("%s: invalid address\n", DEVNAME(un)); kmem_free(buf, bufsz); - urndis_stop(ifp); - splx(s); + usbnet_lock(un); + usbnet_stop(un, ifp, 1); + usbnet_unlock(un); return; } @@ -1499,65 +1041,26 @@ urndis_attach(device_t parent, device_t sc->sc_filter = RNDIS_PACKET_TYPE_BROADCAST; sc->sc_filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST; filter = htole32(sc->sc_filter); - if (urndis_ctrl_set(sc, OID_GEN_CURRENT_PACKET_FILTER, &filter, + if (urndis_ctrl_set(un, OID_GEN_CURRENT_PACKET_FILTER, &filter, sizeof(filter)) != RNDIS_STATUS_SUCCESS) { - aprint_error("%s: unable to set data filters\n", DEVNAME(sc)); - urndis_stop(ifp); - splx(s); + aprint_error("%s: unable to set data filters\n", DEVNAME(un)); + usbnet_lock(un); + usbnet_stop(un, ifp, 1); + usbnet_unlock(un); return; } - if_attach(ifp); - ether_ifattach(ifp, eaddr); - sc->sc_attached = 1; + /* Turn off again now it has been identified. */ + usbnet_lock(un); + usbnet_stop(un, ifp, 1); + usbnet_unlock(un); - splx(s); + usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, NULL); } -static int -urndis_detach(device_t self, int flags) -{ - struct urndis_softc *sc; - struct ifnet *ifp; - int s; - - sc = device_private(self); - - DPRINTF(("urndis_detach: %s flags %u\n", DEVNAME(sc), - flags)); - - if (!sc->sc_attached) - return 0; - - s = splusb(); - - ifp = GET_IFP(sc); - - if (ifp->if_softc != NULL) { - ether_ifdetach(ifp); - if_detach(ifp); - } - - urndis_stop(ifp); - sc->sc_attached = 0; - - splx(s); - - return 0; -} - -static int -urndis_activate(device_t self, enum devact act) -{ - struct urndis_softc *sc; - - sc = device_private(self); - - switch (act) { - case DVACT_DEACTIVATE: - sc->sc_dying = 1; - return 0; - } +#ifdef _MODULE +#include "ioconf.c" +#endif - return EOPNOTSUPP; -} +USBNET_MODULE(urndis) Index: usb/if_urtwn.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_urtwn.c,v retrieving revision 1.71 diff -p -u -u -r1.71 if_urtwn.c --- usb/if_urtwn.c 25 Jul 2019 14:31:35 -0000 1.71 +++ usb/if_urtwn.c 29 Aug 2019 06:22:05 -0000 @@ -390,6 +390,9 @@ urtwn_attach(device_t parent, device_t s callout_init(&sc->sc_calib_to, 0); callout_setfunc(&sc->sc_calib_to, urtwn_calib_to, sc); + rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), + RND_TYPE_NET, RND_FLAG_DEFAULT); + error = usbd_set_config_no(sc->sc_udev, 1, 0); if (error != 0) { aprint_error_dev(self, "failed to set configuration" @@ -513,8 +516,6 @@ urtwn_attach(device_t parent, device_t s ifp->if_percpuq = if_percpuq_create(ifp); if_register(ifp); - rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); ieee80211_announce(ic); @@ -569,6 +570,8 @@ urtwn_detach(device_t self, int flags) usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); + rnd_detach_source(&sc->rnd_source); + callout_destroy(&sc->sc_scan_to); callout_destroy(&sc->sc_calib_to); Index: usb/ohci.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/ohci.c,v retrieving revision 1.289 diff -p -u -u -r1.289 ohci.c --- usb/ohci.c 22 Mar 2019 07:25:15 -0000 1.289 +++ usb/ohci.c 29 Aug 2019 06:22:05 -0000 @@ -1673,7 +1673,7 @@ ohci_device_bulk_done(struct usbd_xfer * int isread = (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN); - KASSERT(mutex_owned(&sc->sc_lock)); + KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock)); OHCIHIST_FUNC(); OHCIHIST_CALLED(); DPRINTFN(10, "xfer=%#jx, actlen=%jd", (uintptr_t)xfer, xfer->ux_actlen, Index: usb/uhub.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/uhub.c,v retrieving revision 1.142 diff -p -u -u -r1.142 uhub.c --- usb/uhub.c 5 May 2019 03:17:54 -0000 1.142 +++ usb/uhub.c 29 Aug 2019 06:22:05 -0000 @@ -98,8 +98,10 @@ fail: #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,1,FMT,A,B,C,D) #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,N,FMT,A,B,C,D) -#define UHUBHIST_FUNC() USBHIST_FUNC() -#define UHUBHIST_CALLED(name) USBHIST_CALLED(uhubdebug) +#define UHUBHIST_FUNC() USBHIST_FUNC() +#define UHUBHIST_CALLED(name) USBHIST_CALLED(uhubdebug) +#define UHUBHIST_CALLARGS(FMT,A,B,C,D) \ + USBHIST_CALLARGS(uhubdebug,FMT,A,B,C,D) struct uhub_softc { device_t sc_dev; /* base device */ @@ -486,9 +488,8 @@ uhub_explore(struct usbd_device *dev) int port; int change, status, reconnect; - UHUBHIST_FUNC(); UHUBHIST_CALLED(); - - DPRINTFN(10, "uhub%jd dev=%#jx addr=%jd speed=%ju", + UHUBHIST_FUNC(); + UHUBHIST_CALLARGS("uhub%jd dev=%#jx addr=%jd speed=%ju", device_unit(sc->sc_dev), (uintptr_t)dev, dev->ud_addr, dev->ud_speed); @@ -759,8 +760,8 @@ uhub_explore(struct usbd_device *dev) dev->ud_depth + 1, speed, port, up); /* XXX retry a few times? */ if (err) { - DPRINTF("usbd_new_device failed, error %jd", err, 0, 0, - 0); + DPRINTF("uhub%jd: usbd_new_device failed, error %jd", + device_unit(sc->sc_dev), err, 0, 0); /* Avoid addressing problems by disabling. */ /* usbd_reset_port(dev, port, &up->status); */ @@ -927,9 +928,8 @@ uhub_intr(struct usbd_xfer *xfer, void * { struct uhub_softc *sc = addr; - UHUBHIST_FUNC(); UHUBHIST_CALLED(); - - DPRINTFN(5, "uhub%jd", device_unit(sc->sc_dev), 0, 0, 0); + UHUBHIST_FUNC(); UHUBHIST_CALLARGS("called! uhub%jd status=%jx", + device_unit(sc->sc_dev), status, 0, 0); if (status == USBD_STALLED) usbd_clear_endpoint_stall_async(sc->sc_ipipe); Index: usb/usb.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usb.c,v retrieving revision 1.179 diff -p -u -u -r1.179 usb.c --- usb/usb.c 5 May 2019 03:17:54 -0000 1.179 +++ usb/usb.c 29 Aug 2019 06:22:05 -0000 @@ -585,10 +585,11 @@ usb_task_thread(void *arg) struct usb_taskq *taskq; bool mpsafe; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - taskq = arg; - DPRINTF("start taskq %#jx", (uintptr_t)taskq, 0, 0, 0); + + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "start taskq %#jx", + (uintptr_t)taskq, 0, 0, 0); mutex_enter(&taskq->lock); for (;;) { @@ -737,7 +738,7 @@ usbioctl(dev_t devt, u_long cmd, void *d struct usb_softc *sc; int unit = minor(devt); - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "cmd %#jx", cmd, 0, 0, 0); if (unit == USB_DEV_MINOR) { switch (cmd) { @@ -765,7 +766,6 @@ usbioctl(dev_t devt, u_long cmd, void *d return EIO; int error = 0; - DPRINTF("cmd %#jx", cmd, 0, 0, 0); switch (cmd) { #ifdef USB_DEBUG case USB_SETDEBUG: @@ -1149,9 +1149,8 @@ void usb_schedsoftintr(struct usbd_bus *bus) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - DPRINTFN(10, "polling=%jd", bus->ub_usepolling, 0, 0, 0); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "polling=%jd", bus->ub_usepolling, 0, 0, 0); /* In case the bus never finished setting up. */ if (__predict_false(bus->ub_soft == NULL)) Index: usb/usb.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usb.h,v retrieving revision 1.116 diff -p -u -u -r1.116 usb.h --- usb/usb.h 31 Jul 2018 16:44:30 -0000 1.116 +++ usb/usb.h 29 Aug 2019 06:22:05 -0000 @@ -207,6 +207,7 @@ typedef struct { uByte bDescriptorType; uByte bDescriptorSubtype; } UPACKED usb_descriptor_t; +#define USB_DESCRIPTOR_SIZE 3 typedef struct { uByte bLength; @@ -852,9 +853,9 @@ struct usb_endpoint_desc { }; struct usb_full_desc { - int ufd_config_index; - u_int ufd_size; - u_char *ufd_data; + int ufd_config_index; + unsigned ufd_size; + unsigned char *ufd_data; }; struct usb_string_desc { @@ -864,8 +865,8 @@ struct usb_string_desc { }; struct usb_ctl_report_desc { - int ucrd_size; - u_char ucrd_data[1024]; /* filled data size will vary */ + int ucrd_size; + unsigned char ucrd_data[1024]; /* filled data size will vary */ }; typedef struct { uint32_t cookie; } usb_event_cookie_t; @@ -927,17 +928,17 @@ struct usb_device_info_old { }; struct usb_ctl_report { - int ucr_report; - u_char ucr_data[1024]; /* filled data size will vary */ + int ucr_report; + unsigned char ucr_data[1024]; /* filled data size will vary */ }; struct usb_device_stats { - u_long uds_requests[4]; /* indexed by transfer type UE_* */ + unsigned long uds_requests[4]; /* indexed by transfer type UE_* */ }; struct usb_bulk_ra_wb_opt { - u_int ra_wb_buffer_size; - u_int ra_wb_request_size; + unsigned ra_wb_buffer_size; + unsigned ra_wb_request_size; }; /* Events that can be read from /dev/usb */ Index: usb/usb_mem.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usb_mem.c,v retrieving revision 1.70 diff -p -u -u -r1.70 usb_mem.c --- usb/usb_mem.c 28 Oct 2017 00:37:12 -0000 1.70 +++ usb/usb_mem.c 29 Aug 2019 06:22:05 -0000 @@ -115,8 +115,8 @@ usb_block_allocmem(bus_dma_tag_t tag, si usb_dma_block_t *b; int error; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - DPRINTFN(5, "size=%ju align=%ju", size, align, 0, 0); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "size=%ju align=%ju", size, align, 0, 0); ASSERT_SLEEPABLE(); KASSERT(size != 0); @@ -236,11 +236,11 @@ usb_valid_block_p(usb_dma_block_t *b, st Static void usb_block_freemem(usb_dma_block_t *b) { + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "size=%ju", b->size, 0, 0, 0); KASSERT(mutex_owned(&usb_blk_lock)); - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - DPRINTFN(6, "size=%ju", b->size, 0, 0, 0); #ifdef DEBUG LIST_REMOVE(b, next); #endif Index: usb/usb_subr.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usb_subr.c,v retrieving revision 1.235 diff -p -u -u -r1.235 usb_subr.c --- usb/usb_subr.c 23 Jul 2019 17:21:33 -0000 1.235 +++ usb/usb_subr.c 29 Aug 2019 06:22:05 -0000 @@ -122,13 +122,20 @@ usbd_get_string_desc(struct usbd_device usbd_status err; int actlen; + /* + * Pass a full-sized buffer to usbd_do_request_len(). At least + * one device has been seen returning additional data beyond the + * provided buffers (2-bytes written shortly after the request + * claims to have completed and returned the 2 byte header, + * corrupting other memory.) + */ req.bmRequestType = UT_READ_DEVICE; req.bRequest = UR_GET_DESCRIPTOR; USETW2(req.wValue, UDESC_STRING, sindex); USETW(req.wIndex, langid); USETW(req.wLength, 2); /* only size byte first */ - err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, - &actlen, USBD_DEFAULT_TIMEOUT); + err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc, + USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); if (err) return err; @@ -138,8 +145,8 @@ usbd_get_string_desc(struct usbd_device if (sdesc->bLength > sizeof(*sdesc)) return USBD_INVAL; USETW(req.wLength, sdesc->bLength); /* the whole string */ - err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, - &actlen, USBD_DEFAULT_TIMEOUT); + err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc, + USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); if (err) return err; @@ -313,7 +320,7 @@ usbd_delay_ms(struct usbd_device *dev, u usbd_status usbd_reset_port(struct usbd_device *dev, int port, usb_port_status_t *ps) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "port %jd", port, 0, 0, 0); usb_device_request_t req; usbd_status err; int n; @@ -356,32 +363,46 @@ usbd_reset_port(struct usbd_device *dev, usb_interface_descriptor_t * usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "iface/alt idx %jd/%jd", + ifaceidx, altidx, 0, 0); char *p = (char *)cd; char *end = p + UGETW(cd->wTotalLength); - usb_interface_descriptor_t *d; + usb_descriptor_t *desc; + usb_interface_descriptor_t *idesc; int curidx, lastidx, curaidx = 0; for (curidx = lastidx = -1; p < end; ) { - d = (usb_interface_descriptor_t *)p; + desc = (usb_descriptor_t *)p; + DPRINTFN(4, "idx=%jd(%jd) altidx=%jd(%jd)", ifaceidx, curidx, altidx, curaidx); - DPRINTFN(4, "len=%jd type=%jd", d->bLength, d->bDescriptorType, - 0, 0); - if (d->bLength == 0) - break; /* bad descriptor */ - p += d->bLength; - if (p <= end && d->bDescriptorType == UDESC_INTERFACE) { - if (d->bInterfaceNumber != lastidx) { - lastidx = d->bInterfaceNumber; - curidx++; - curaidx = 0; - } else - curaidx++; - if (ifaceidx == curidx && altidx == curaidx) - return d; + DPRINTFN(4, "len=%jd type=%jd", desc->bLength, + desc->bDescriptorType, 0, 0); + + if (desc->bLength < USB_DESCRIPTOR_SIZE) + break; + p += desc->bLength; + if (p > end) + break; + + if (desc->bDescriptorType != UDESC_INTERFACE) + continue; + idesc = (usb_interface_descriptor_t *)desc; + if (idesc->bLength < USB_INTERFACE_DESCRIPTOR_SIZE) + break; + + if (idesc->bInterfaceNumber != lastidx) { + lastidx = idesc->bInterfaceNumber; + curidx++; + curaidx = 0; + } else { + curaidx++; } + if (ifaceidx == curidx && altidx == curaidx) + return idesc; } + return NULL; } @@ -391,29 +412,39 @@ usbd_find_edesc(usb_config_descriptor_t { char *p = (char *)cd; char *end = p + UGETW(cd->wTotalLength); - usb_interface_descriptor_t *d; - usb_endpoint_descriptor_t *e; + usb_interface_descriptor_t *idesc; + usb_endpoint_descriptor_t *edesc; + usb_descriptor_t *desc; int curidx; - d = usbd_find_idesc(cd, ifaceidx, altidx); - if (d == NULL) + idesc = usbd_find_idesc(cd, ifaceidx, altidx); + if (idesc == NULL) return NULL; - if (endptidx >= d->bNumEndpoints) /* quick exit */ + if (endptidx >= idesc->bNumEndpoints) /* quick exit */ return NULL; curidx = -1; - for (p = (char *)d + d->bLength; p < end; ) { - e = (usb_endpoint_descriptor_t *)p; - if (e->bLength == 0) - break; /* bad descriptor */ - p += e->bLength; - if (p <= end && e->bDescriptorType == UDESC_INTERFACE) - return NULL; - if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) { - curidx++; - if (curidx == endptidx) - return e; - } + for (p = (char *)idesc + idesc->bLength; p < end; ) { + desc = (usb_descriptor_t *)p; + + if (desc->bLength < USB_DESCRIPTOR_SIZE) + break; + p += desc->bLength; + if (p > end) + break; + + if (desc->bDescriptorType == UDESC_INTERFACE) + break; + if (desc->bDescriptorType != UDESC_ENDPOINT) + continue; + + edesc = (usb_endpoint_descriptor_t *)desc; + if (edesc->bLength < USB_ENDPOINT_DESCRIPTOR_SIZE) + break; + + curidx++; + if (curidx == endptidx) + return edesc; } return NULL; } @@ -421,13 +452,14 @@ usbd_find_edesc(usb_config_descriptor_t usbd_status usbd_fill_iface_data(struct usbd_device *dev, int ifaceidx, int altidx) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "ifaceidx=%jd altidx=%jd", + ifaceidx, altidx, 0, 0); struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx]; usb_interface_descriptor_t *idesc; char *p, *end; int endpt, nendpt; - DPRINTFN(4, "ifaceidx=%jd altidx=%jd", ifaceidx, altidx, 0, 0); idesc = usbd_find_idesc(dev->ud_cdesc, ifaceidx, altidx); if (idesc == NULL) return USBD_INVAL; @@ -452,7 +484,8 @@ usbd_fill_iface_data(struct usbd_device DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd", (uintptr_t)p, (uintptr_t)end, ed->bLength, ed->bDescriptorType); - if (p + ed->bLength <= end && ed->bLength != 0 && + if (p + ed->bLength <= end && + ed->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE && ed->bDescriptorType == UDESC_ENDPOINT) goto found; if (ed->bLength == 0 || @@ -519,10 +552,11 @@ usbd_free_iface_data(struct usbd_device Static usbd_status usbd_set_config(struct usbd_device *dev, int conf) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); usb_device_request_t req; - DPRINTFN(5, "dev %#jx conf %jd", (uintptr_t)dev, conf, 0, 0); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx conf %jd", + (uintptr_t)dev, conf, 0, 0); req.bmRequestType = UT_WRITE_DEVICE; req.bRequest = UR_SET_CONFIG; @@ -535,7 +569,7 @@ usbd_set_config(struct usbd_device *dev, usbd_status usbd_set_config_no(struct usbd_device *dev, int no, int msg) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "%jd", no, 0, 0, 0); usb_config_descriptor_t cd; usbd_status err; int index; @@ -543,7 +577,6 @@ usbd_set_config_no(struct usbd_device *d if (no == USB_UNCONFIG_NO) return usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg); - DPRINTFN(5, "%jd", no, 0, 0, 0); /* Figure out what config index to use. */ for (index = 0; index < dev->ud_ddesc.bNumConfigurations; index++) { err = usbd_get_config_desc(dev, index, &cd); @@ -558,13 +591,14 @@ usbd_set_config_no(struct usbd_device *d usbd_status usbd_set_config_index(struct usbd_device *dev, int index, int msg) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev=%#jx index=%jd", + (uintptr_t)dev, index, 0, 0); usb_config_descriptor_t cd, *cdp; usb_bos_descriptor_t *bdp = NULL; usbd_status err; int i, ifcidx, nifc, len, selfpowered, power; - DPRINTFN(5, "dev=%#jx index=%jd", (uintptr_t)dev, index, 0, 0); if (index >= dev->ud_ddesc.bNumConfigurations && index != USB_UNCONFIG_INDEX) { @@ -659,7 +693,8 @@ usbd_set_config_index(struct usbd_device break; usbd_delay_ms(dev, 200); } - if (err || bdp->bDescriptorType != UDESC_BOS) { + if (err || bdp->bDescriptorType != UDESC_BOS || + UGETW(bdp->wTotalLength) != UGETW(bd.wTotalLength)) { DPRINTF("error %jd or bad desc %jd", err, bdp->bDescriptorType, 0, 0); kmem_free(bdp, blen); @@ -782,13 +817,13 @@ usbd_status usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface, struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev=%#jx addr=%jd iface=%#jx ep=%#jx", + (uintptr_t)dev, dev->ud_addr, (uintptr_t)iface, (uintptr_t)ep); struct usbd_pipe *p; usbd_status err; p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP); - DPRINTFN(1, "dev=%#jx addr=%jd iface=%#jx ep=%#jx", - (uintptr_t)dev, dev->ud_addr, (uintptr_t)iface, (uintptr_t)ep); DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0); p->up_dev = dev; p->up_iface = iface; @@ -1020,13 +1055,13 @@ usbd_status usbd_probe_and_attach(device_t parent, struct usbd_device *dev, int port, int addr) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "trying device specific drivers", 0, 0, 0, 0); usb_device_descriptor_t *dd = &dev->ud_ddesc; int confi, nifaces; usbd_status err; /* First try with device specific drivers. */ - DPRINTF("trying device specific drivers", 0, 0, 0, 0); err = usbd_attachwholedevice(parent, dev, port, 0); if (dev->ud_nifaces_claimed || err) return err; @@ -1137,18 +1172,17 @@ usbd_reattach_device(device_t parent, st usbd_status usbd_get_initial_ddesc(struct usbd_device *dev, usb_device_descriptor_t *desc) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0); usb_device_request_t req; char buf[64]; int res, actlen; - DPRINTFN(5, "dev %#jx", (uintptr_t)dev, 0, 0, 0); - req.bmRequestType = UT_READ_DEVICE; req.bRequest = UR_GET_DESCRIPTOR; USETW2(req.wValue, UDESC_DEVICE, 0); USETW(req.wIndex, 0); - USETW(req.wLength, 64); + USETW(req.wLength, 8); res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); if (res) @@ -1169,7 +1203,9 @@ usbd_status usbd_new_device(device_t parent, struct usbd_bus *bus, int depth, int speed, int port, struct usbd_port *up) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "bus=%#jx port=%jd depth=%jd speed=%jd", + (uintptr_t)bus, port, depth, speed); struct usbd_device *dev, *adev; struct usbd_device *hub; usb_device_descriptor_t *dd; @@ -1179,9 +1215,6 @@ usbd_new_device(device_t parent, struct int i; int p; - DPRINTF("bus=%#jx port=%jd depth=%jd speed=%jd", - (uintptr_t)bus, port, depth, speed); - if (bus->ub_methods->ubm_newdev != NULL) return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed, port, up); @@ -1431,9 +1464,9 @@ void usbd_remove_device(struct usbd_device *dev, struct usbd_port *up) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - DPRINTF("dev %#jx up %#jx", (uintptr_t)dev, (uintptr_t)up, 0, 0); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx up %#jx", + (uintptr_t)dev, (uintptr_t)up, 0, 0); if (dev->ud_pipe0 != NULL) usbd_kill_pipe(dev->ud_pipe0); @@ -1646,15 +1679,15 @@ usb_free_device(struct usbd_device *dev) int usb_disconnect_port(struct usbd_port *up, device_t parent, int flags) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); struct usbd_device *dev = up->up_dev; device_t subdev; char subdevname[16]; const char *hubname = device_xname(parent); int i, rc; - DPRINTFN(3, "up=%#jx dev=%#jx port=%jd", (uintptr_t)up, (uintptr_t)dev, - up->up_portno, 0); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "up=%#jx dev=%#jx port=%jd", + (uintptr_t)up, (uintptr_t)dev, up->up_portno, 0); if (dev == NULL) { return 0; Index: usb/usbdi.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usbdi.c,v retrieving revision 1.182 diff -p -u -u -r1.182 usbdi.c --- usb/usbdi.c 17 Feb 2019 04:17:31 -0000 1.182 +++ usb/usbdi.c 29 Aug 2019 06:22:05 -0000 @@ -76,9 +76,9 @@ static void usbd_request_async_cb(struct void usbd_dump_iface(struct usbd_interface *iface) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0); - USBHIST_LOG(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0); if (iface == NULL) return; USBHIST_LOG(usbdebug, " device = %#jx idesc = %#jx index = %d", @@ -91,9 +91,9 @@ usbd_dump_iface(struct usbd_interface *i void usbd_dump_device(struct usbd_device *dev) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0); - USBHIST_LOG(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0); if (dev == NULL) return; USBHIST_LOG(usbdebug, " bus = %#jx default_pipe = %#jx", @@ -108,9 +108,9 @@ usbd_dump_device(struct usbd_device *dev void usbd_dump_endpoint(struct usbd_endpoint *endp) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0); - USBHIST_LOG(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0); if (endp == NULL) return; USBHIST_LOG(usbdebug, " edesc = %#jx refcnt = %jd", @@ -125,9 +125,9 @@ usbd_dump_queue(struct usbd_pipe *pipe) { struct usbd_xfer *xfer; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0); - USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0); SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) { USBHIST_LOG(usbdebug, " xfer = %#jx", (uintptr_t)xfer, 0, 0, 0); @@ -137,9 +137,9 @@ usbd_dump_queue(struct usbd_pipe *pipe) void usbd_dump_pipe(struct usbd_pipe *pipe) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0); - USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0); if (pipe == NULL) return; usbd_dump_iface(pipe->up_iface); @@ -171,9 +171,8 @@ usbd_open_pipe_ival(struct usbd_interfac usbd_status err; int i; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - USBHIST_LOG(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx", + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx", (uintptr_t)iface, address, flags, 0); for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) { @@ -205,9 +204,8 @@ usbd_open_pipe_intr(struct usbd_interfac struct usbd_xfer *xfer; struct usbd_pipe *ipipe; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - USBHIST_LOG(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd", + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd", address, flags, len, 0); err = usbd_open_pipe_ival(iface, address, @@ -279,9 +277,7 @@ usbd_transfer(struct usbd_xfer *xfer) usbd_status err; unsigned int size, flags; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - USBHIST_LOG(usbdebug, + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd", (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running); KASSERT(xfer->ux_status == USBD_NOT_STARTED); @@ -327,8 +323,6 @@ usbd_transfer(struct usbd_xfer *xfer) /* xfer is not valid after the transfer method unless synchronous */ err = pipe->up_methods->upm_transfer(xfer); - USBHIST_LOG(usbdebug, "<- done transfer %#jx, err = %jd", - (uintptr_t)xfer, err, 0, 0); if (err != USBD_IN_PROGRESS && err) { /* @@ -336,6 +330,8 @@ usbd_transfer(struct usbd_xfer *xfer) * accepted by the HCD for some reason. It needs removing * from the pipe queue. */ + USBHIST_LOG(usbdebug, "xfer failed: %s, reinserting", + err, 0, 0, 0); usbd_lock_pipe(pipe); SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next); if (pipe->up_serialise) @@ -350,8 +346,8 @@ usbd_transfer(struct usbd_xfer *xfer) } if (err != USBD_IN_PROGRESS) { - USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)" - "(complete/error)", (uintptr_t)xfer, err, 0, 0); + USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)", + (uintptr_t)xfer, err, 0, 0); return err; } @@ -469,18 +465,19 @@ usbd_alloc_xfer(struct usbd_device *dev, { struct usbd_xfer *xfer; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); ASSERT_SLEEPABLE(); xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes); if (xfer == NULL) - return NULL; + goto out; xfer->ux_bus = dev->ud_bus; callout_init(&xfer->ux_callout, CALLOUT_MPSAFE); cv_init(&xfer->ux_cv, "usbxfer"); - USBHIST_LOG(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0); +out: + USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0); return xfer; } @@ -488,9 +485,9 @@ usbd_alloc_xfer(struct usbd_device *dev, static usbd_status usbd_free_xfer(struct usbd_xfer *xfer) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0); - USBHIST_LOG(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0); if (xfer->ux_buf) { usbd_free_buffer(xfer); } @@ -794,14 +791,14 @@ usbd_set_interface(struct usbd_interface usbd_status err; void *endpoints; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); if (LIST_FIRST(&iface->ui_pipes) != NULL) return USBD_IN_USE; endpoints = iface->ui_endpoints; int nendpt = iface->ui_idesc->bNumEndpoints; - USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd", + USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd", (uintptr_t)iface, (uintptr_t)endpoints, iface->ui_idesc->bNumEndpoints, 0); err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx); @@ -869,11 +866,11 @@ usbd_ar_pipe(struct usbd_pipe *pipe) { struct usbd_xfer *xfer; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0); KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock)); - USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0); #ifdef USB_DEBUG if (usbdebug > 5) usbd_dump_queue(pipe); @@ -907,9 +904,8 @@ usb_transfer_complete(struct usbd_xfer * int polling = bus->ub_usepolling; int repeat = pipe->up_repeat; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx status = %jd " + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd " "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status, xfer->ux_actlen); @@ -1028,10 +1024,9 @@ usb_insert_transfer(struct usbd_xfer *xf struct usbd_pipe *pipe = xfer->ux_pipe; usbd_status err; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - USBHIST_LOG(usbdebug, "xfer = %#jx pipe = %#jx running = %jd " - "timeout = %jd", (uintptr_t)xfer, (uintptr_t)pipe, + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, + "xfer = %#jx pipe = %#jx running = %jd timeout = %jd", + (uintptr_t)xfer, (uintptr_t)pipe, pipe->up_running, xfer->ux_timeout); KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock)); @@ -1060,7 +1055,7 @@ usbd_start_next(struct usbd_pipe *pipe) struct usbd_xfer *xfer; usbd_status err; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); KASSERT(pipe != NULL); KASSERT(pipe->up_methods != NULL); @@ -1072,7 +1067,7 @@ usbd_start_next(struct usbd_pipe *pipe) /* Get next request in queue. */ xfer = SIMPLEQ_FIRST(&pipe->up_queue); - USBHIST_LOG(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe, + USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe, (uintptr_t)xfer, 0, 0); if (xfer == NULL) { pipe->up_running = 0; @@ -1105,13 +1100,26 @@ usbd_status usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req, void *data, uint16_t flags, int *actlen, uint32_t timeout) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + size_t len = UGETW(req->wLength); + + return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout); +} + +usbd_status +usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req, + size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout) +{ struct usbd_xfer *xfer; usbd_status err; + KASSERT(len >= UGETW(req->wLength)); + + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx", + (uintptr_t)dev, (uintptr_t)req, flags, len); + ASSERT_SLEEPABLE(); - size_t len = UGETW(req->wLength); int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer); if (error) return error; Index: usb/usbdi.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usbdi.h,v retrieving revision 1.96 diff -p -u -u -r1.96 usbdi.h --- usb/usbdi.h 27 Jan 2019 02:08:42 -0000 1.96 +++ usb/usbdi.h 29 Aug 2019 06:22:05 -0000 @@ -143,6 +143,9 @@ usbd_status usbd_request_async(struct us usb_device_request_t *, void *, usbd_callback); usbd_status usbd_do_request_flags(struct usbd_device *, usb_device_request_t *, void *, uint16_t, int *, uint32_t); +usbd_status usbd_do_request_len(struct usbd_device *dev, + usb_device_request_t *req, size_t len, void *data, uint16_t flags, + int *actlen, uint32_t timeout); usb_interface_descriptor_t * usbd_get_interface_descriptor(struct usbd_interface *); Index: usb/usbdi_util.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usbdi_util.c,v retrieving revision 1.73 diff -p -u -u -r1.73 usbdi_util.c --- usb/usbdi_util.c 7 Feb 2019 13:20:41 -0000 1.73 +++ usb/usbdi_util.c 29 Aug 2019 06:22:05 -0000 @@ -62,9 +62,9 @@ usbd_get_desc(struct usbd_device *dev, i usb_device_request_t req; usbd_status err; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); - - DPRINTFN(3,"type=%jd, index=%jd, len=%jd", type, index, len, 0); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "type=%jd, index=%jd, len=%jd", + type, index, len, 0); /* * Provide hard-coded configuration descriptors @@ -93,10 +93,10 @@ usbd_status usbd_get_config_desc(struct usbd_device *dev, int confidx, usb_config_descriptor_t *d) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "confidx=%jd", confidx, 0, 0, 0); usbd_status err; - DPRINTFN(3, "confidx=%jd", confidx, 0, 0, 0); err = usbd_get_desc(dev, UDESC_CONFIG, confidx, USB_CONFIG_DESCRIPTOR_SIZE, d); if (err) @@ -112,9 +112,8 @@ usbd_get_config_desc(struct usbd_device usbd_status usbd_get_config_desc_full(struct usbd_device *dev, int conf, void *d, int size) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "conf=%jd", conf, 0, 0, 0); - DPRINTFN(3, "conf=%jd", conf, 0, 0, 0); return usbd_get_desc(dev, UDESC_CONFIG, conf, size, d); } @@ -122,10 +121,10 @@ usbd_status usbd_get_bos_desc(struct usbd_device *dev, int confidx, usb_bos_descriptor_t *d) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "confidx=%jd", confidx, 0, 0, 0); usbd_status err; - DPRINTFN(3, "confidx=%jd", confidx, 0, 0, 0); err = usbd_get_desc(dev, UDESC_BOS, confidx, USB_BOS_DESCRIPTOR_SIZE, d); if (err) @@ -141,9 +140,8 @@ usbd_get_bos_desc(struct usbd_device *de usbd_status usbd_get_bos_desc_full(struct usbd_device *dev, int conf, void *d, int size) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "conf=%jd", conf, 0, 0, 0); - DPRINTFN(3, "conf=%jd", conf, 0, 0, 0); return usbd_get_desc(dev, UDESC_BOS, conf, size, d); } @@ -173,10 +171,10 @@ usbd_get_device_status(struct usbd_devic usbd_status usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx", (uintptr_t)dev, 0, 0, 0); req.bmRequestType = UT_READ_CLASS_DEVICE; req.bRequest = UR_GET_STATUS; USETW(req.wValue, 0); @@ -188,10 +186,11 @@ usbd_get_hub_status(struct usbd_device * usbd_status usbd_set_address(struct usbd_device *dev, int addr) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx addr %jd", + (uintptr_t)dev, addr, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx addr %jd", (uintptr_t)dev, addr, 0, 0); req.bmRequestType = UT_WRITE_DEVICE; req.bRequest = UR_SET_ADDRESS; USETW(req.wValue, addr); @@ -203,10 +202,11 @@ usbd_set_address(struct usbd_device *dev usbd_status usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd", + (uintptr_t)dev, port, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx port %jd", (uintptr_t)dev, port, 0, 0); req.bmRequestType = UT_READ_CLASS_OTHER; req.bRequest = UR_GET_STATUS; USETW(req.wValue, 0); @@ -220,10 +220,11 @@ usbd_status usbd_get_port_status_ext(struct usbd_device *dev, int port, usb_port_status_ext_t *pse) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd", + (uintptr_t)dev, port, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx port %jd", (uintptr_t)dev, port, 0, 0); req.bmRequestType = UT_READ_CLASS_OTHER; req.bRequest = UR_GET_STATUS; USETW2(req.wValue, 0, UR_PST_EXT_PORT_STATUS); @@ -235,10 +236,11 @@ usbd_get_port_status_ext(struct usbd_dev usbd_status usbd_clear_hub_feature(struct usbd_device *dev, int sel) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx sel %jd", + (uintptr_t)dev, sel, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx sel %jd", (uintptr_t)dev, sel, 0, 0); req.bmRequestType = UT_WRITE_CLASS_DEVICE; req.bRequest = UR_CLEAR_FEATURE; USETW(req.wValue, sel); @@ -250,10 +252,11 @@ usbd_clear_hub_feature(struct usbd_devic usbd_status usbd_set_hub_feature(struct usbd_device *dev, int sel) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, + "dev %#jx sel %jd", (uintptr_t)dev, sel, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx sel %jd", (uintptr_t)dev, sel, 0, 0); req.bmRequestType = UT_WRITE_CLASS_DEVICE; req.bRequest = UR_SET_FEATURE; USETW(req.wValue, sel); @@ -265,10 +268,11 @@ usbd_set_hub_feature(struct usbd_device usbd_status usbd_clear_port_feature(struct usbd_device *dev, int port, int sel) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd sel %jd", + (uintptr_t)dev, port, sel, 0); usb_device_request_t req; - DPRINTF("dev %#jx port %jd sel %jd", (uintptr_t)dev, port, sel, 0); req.bmRequestType = UT_WRITE_CLASS_OTHER; req.bRequest = UR_CLEAR_FEATURE; USETW(req.wValue, sel); @@ -280,10 +284,11 @@ usbd_clear_port_feature(struct usbd_devi usbd_status usbd_set_port_feature(struct usbd_device *dev, int port, int sel) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd sel %.d", + (uintptr_t)dev, sel, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx port %jd sel %.d", (uintptr_t)dev, sel, 0, 0); req.bmRequestType = UT_WRITE_CLASS_OTHER; req.bRequest = UR_SET_FEATURE; USETW(req.wValue, sel); @@ -295,11 +300,11 @@ usbd_set_port_feature(struct usbd_device usbd_status usbd_set_port_u1_timeout(struct usbd_device *dev, int port, int timeout) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd timeout %.d", + (uintptr_t)dev, port, timeout, 0); usb_device_request_t req; - DPRINTF("dev %#jx port %jd timeout %.d", (uintptr_t)dev, port, - timeout, 0); req.bmRequestType = UT_WRITE_CLASS_OTHER; req.bRequest = UR_SET_FEATURE; USETW(req.wValue, UHF_PORT_U1_TIMEOUT); @@ -311,11 +316,11 @@ usbd_set_port_u1_timeout(struct usbd_dev usbd_status usbd_set_port_u2_timeout(struct usbd_device *dev, int port, int timeout) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd timeout %jd", + (uintptr_t)dev, port, timeout, 0); usb_device_request_t req; - DPRINTF("dev %#jx port %jd timeout %jd", (uintptr_t)dev, port, - timeout, 0); req.bmRequestType = UT_WRITE_CLASS_OTHER; req.bRequest = UR_SET_FEATURE; USETW(req.wValue, UHF_PORT_U2_TIMEOUT); @@ -331,12 +336,12 @@ usbd_get_protocol(struct usbd_interface struct usbd_device *dev; usb_device_request_t req; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "iface=%#jx, endpt=%jd", + (uintptr_t)iface, id->bInterfaceNumber, 0, 0); if (id == NULL) return USBD_IOERROR; - DPRINTFN(4, "iface=%#jx, endpt=%jd", (uintptr_t)iface, - id->bInterfaceNumber, 0, 0); usbd_interface2device_handle(iface, &dev); req.bmRequestType = UT_READ_CLASS_INTERFACE; @@ -354,12 +359,12 @@ usbd_set_protocol(struct usbd_interface struct usbd_device *dev; usb_device_request_t req; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "iface=%#jx, report=%jd, endpt=%jd", + (uintptr_t)iface, report, id->bInterfaceNumber, 0); if (id == NULL) return USBD_IOERROR; - DPRINTFN(4, "iface=%#jx, report=%jd, endpt=%jd", (uintptr_t)iface, - report, id->bInterfaceNumber, 0); usbd_interface2device_handle(iface, &dev); req.bmRequestType = UT_WRITE_CLASS_INTERFACE; @@ -378,9 +383,9 @@ usbd_set_report(struct usbd_interface *i struct usbd_device *dev; usb_device_request_t req; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "len=%jd", len, 0, 0, 0); - DPRINTFN(4, "len=%jd", len, 0, 0, 0); if (ifd == NULL) return USBD_IOERROR; usbd_interface2device_handle(iface, &dev); @@ -400,9 +405,8 @@ usbd_get_report(struct usbd_interface *i struct usbd_device *dev; usb_device_request_t req; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "len=%jd", len, 0, 0, 0); - DPRINTFN(4, "len=%jd", len, 0, 0, 0); if (ifd == NULL) return USBD_IOERROR; usbd_interface2device_handle(iface, &dev); @@ -421,9 +425,9 @@ usbd_set_idle(struct usbd_interface *ifa struct usbd_device *dev; usb_device_request_t req; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "duration %jd id %jd", duration, id, 0, 0); - DPRINTFN(4, "duration %jd id %jd", duration, id, 0, 0); if (ifd == NULL) return USBD_IOERROR; usbd_interface2device_handle(iface, &dev); @@ -439,10 +443,11 @@ usbd_status usbd_get_report_descriptor(struct usbd_device *dev, int ifcno, int size, void *d) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx ifcno %jd size %jd", + (uintptr_t)dev, ifcno, size, 0); usb_device_request_t req; - DPRINTF("dev %#jx ifcno %jd size %jd", (uintptr_t)dev, ifcno, size, 0); req.bmRequestType = UT_READ_INTERFACE; req.bRequest = UR_GET_DESCRIPTOR; USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ @@ -470,7 +475,9 @@ usbd_get_hid_descriptor(struct usbd_inte for (; p < end; p += hd->bLength) { hd = (usb_hid_descriptor_t *)p; - if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID) + if (p + hd->bLength <= end && + hd->bLength >= USB_HID_DESCRIPTOR_SIZE(0) && + hd->bDescriptorType == UDESC_HID) return hd; if (hd->bDescriptorType == UDESC_INTERFACE) break; @@ -494,6 +501,8 @@ usbd_read_report_desc(struct usbd_interf if (hid == NULL) return USBD_IOERROR; *sizep = UGETW(hid->descrs[0].wDescriptorLength); + if (*sizep == 0) + return USBD_INVAL; *descp = kmem_alloc(*sizep, KM_SLEEP); err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, *sizep, *descp); @@ -508,10 +517,10 @@ usbd_read_report_desc(struct usbd_interf usbd_status usbd_get_config(struct usbd_device *dev, uint8_t *conf) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0); usb_device_request_t req; - DPRINTF("dev %#jx", (uintptr_t)dev, 0, 0, 0); req.bmRequestType = UT_READ_DEVICE; req.bRequest = UR_GET_CONFIG; USETW(req.wValue, 0); @@ -526,10 +535,10 @@ usbd_bulk_transfer(struct usbd_xfer *xfe { usbd_status err; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "start transfer %jd bytes", *size, 0, 0, 0); usbd_setup_xfer(xfer, 0, buf, *size, flags, timeout, NULL); - DPRINTFN(1, "start transfer %jd bytes", *size, 0, 0, 0); err = usbd_sync_transfer_sig(xfer); usbd_get_xfer_status(xfer, NULL, NULL, size, NULL); @@ -549,11 +558,11 @@ usbd_intr_transfer(struct usbd_xfer *xfe { usbd_status err; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "start transfer %jd bytes", *size, 0, 0, 0); usbd_setup_xfer(xfer, 0, buf, *size, flags, timeout, NULL); - DPRINTFN(1, "start transfer %jd bytes", *size, 0, 0, 0); err = usbd_sync_transfer_sig(xfer); usbd_get_xfer_status(xfer, NULL, NULL, size, NULL); @@ -571,9 +580,10 @@ usbd_intr_transfer(struct usbd_xfer *xfe void usb_detach_waitold(device_t dv) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "waiting for dv %#jx", + (uintptr_t)dv, 0, 0, 0); - DPRINTFN(1, "waiting for dv %#jx", (uintptr_t)dv, 0, 0, 0); if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */ aprint_error_dev(dv, "usb_detach_waitold: didn't detach\n"); DPRINTFN(1, "done", 0, 0, 0, 0); @@ -582,9 +592,9 @@ usb_detach_waitold(device_t dv) void usb_detach_wakeupold(device_t dv) { - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); + USBHIST_CALLARGS(usbdebug, "for dv %#jx", (uintptr_t)dv, 0, 0, 0); - DPRINTFN(1, "for dv %#jx", (uintptr_t)dv, 0, 0, 0); wakeup(dv); /* XXXSMP ok */ } Index: usb/usbhist.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usbhist.h,v retrieving revision 1.4 diff -p -u -u -r1.4 usbhist.h --- usb/usbhist.h 23 Apr 2016 10:15:32 -0000 1.4 +++ usb/usbhist.h 29 Aug 2019 06:22:05 -0000 @@ -69,6 +69,16 @@ extern int usbdebug; KERNHIST_CALLED(usbhist); \ } \ } while (0) +#define USBHIST_CALLARGS(NAME,FMT,A,B,C,D) do { \ + if ((NAME) != 0) { \ + KERNHIST_CALLARGS(usbhist,FMT,A,B,C,D); \ + } \ +} while (0) +#define USBHIST_CALLARGSN(NAME,N,FMT,A,B,C,D) do { \ + if ((NAME) >= (N)) { \ + KERNHIST_CALLARGS(usbhist,FMT,A,B,C,D); \ + } \ +} while (0) #define USBHIST_FUNC() KERNHIST_FUNC(__func__) USBHIST_DECL(usbhist); @@ -83,6 +93,8 @@ USBHIST_DECL(usbhist); #define USBHIST_LOGN(N,NAME,FMT,A,B,C,D) do { } while(0) #define USBHIST_LOGM(N,NAME,FMT,A,B,C,D) do { } while(0) #define USBHIST_LOG(NAME,FMT,A,B,C,D) do { } while(0) +#define USBHIST_CALLARGS(NAME,FMT,A,B,C,D) +#define USBHIST_CALLARGSN(NAME,N,FMT,A,B,C,D) #define USBHIST_CALLED(NAME) #define USBHIST_FUNC() Index: usb/usbnet.c =================================================================== RCS file: usb/usbnet.c diff -N usb/usbnet.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ usb/usbnet.c 29 Aug 2019 06:22:05 -0000 @@ -0,0 +1,1566 @@ +/* $NetBSD$ */ + +/* + * Copyright (c) 2019 Matthew R. Green + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Common code shared between USB network drivers. + */ + +#include +__KERNEL_RCSID(0, "$NetBSD$"); + +#include +#include +#include +#include +#include + +#include +#include + +struct usbnet_cdata { + struct usbnet_chain *uncd_tx_chain; + struct usbnet_chain *uncd_rx_chain; + + int uncd_tx_prod; + int uncd_tx_cnt; +}; + +struct usbnet_private { + /* + * - unp_lock protects most of the structure, and the public one + * - unp_miilock must be held to access this device's MII bus + * - unp_rxlock protects the rx path and its data + * - unp_txlock protects the tx path and its data + * - unp_detachcv handles detach vs open references + */ + kmutex_t unp_lock; + kmutex_t unp_miilock; + kmutex_t unp_rxlock; + kmutex_t unp_txlock; + kcondvar_t unp_detachcv; + + struct usbnet_cdata unp_cdata; + + struct ethercom unp_ec; + struct mii_data unp_mii; + struct usb_task unp_ticktask; + struct callout unp_stat_ch; + struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX]; + + bool unp_dying; + bool unp_stopping; + bool unp_attached; + bool unp_link; + + int unp_refcnt; + int unp_timer; + int unp_if_flags; + unsigned unp_number; + + krndsource_t unp_rndsrc; + + struct timeval unp_rx_notice; + struct timeval unp_tx_notice; + struct timeval unp_intr_notice; +}; + +#define un_cdata(un) (&(un)->un_pri->unp_cdata) + +volatile unsigned usbnet_number; + +static int usbnet_modcmd(modcmd_t, void *); + +#ifdef USB_DEBUG +#ifndef USBNET_DEBUG +#define usbnetdebug 0 +#else +static int usbnetdebug = 1; + +SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup") +{ + int err; + const struct sysctlnode *rnode; + const struct sysctlnode *cnode; + + err = sysctl_createv(clog, 0, NULL, &rnode, + CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet", + SYSCTL_DESCR("usbnet global controls"), + NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); + + if (err) + goto fail; + + /* control debugging printfs */ + err = sysctl_createv(clog, 0, &rnode, &cnode, + CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, + "debug", SYSCTL_DESCR("Enable debugging output"), + NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL); + if (err) + goto fail; + + return; +fail: + aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); +} + +#endif /* USBNET_DEBUG */ +#endif /* USB_DEBUG */ + +#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D) +#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D) +#define USBNETHIST_FUNC() USBHIST_FUNC() +#define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) +#define USBNETHIST_CALLARGS(FMT,A,B,C,D) \ + USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D) +#define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \ + USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D) + +/* Callback vectors. */ + +static void +uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) +{ + if (un->un_ops->uno_stop) + (*un->un_ops->uno_stop)(ifp, disable); +} + +static int +uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) +{ + if (un->un_ops->uno_ioctl) + return (*un->un_ops->uno_ioctl)(ifp, cmd, data); + return 0; +} + +static int +uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) +{ + return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); +} + +static int +uno_init(struct usbnet *un, struct ifnet *ifp) +{ + return (*un->un_ops->uno_init)(ifp); +} + +static int +uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) +{ + return (*un->un_ops->uno_read_reg)(un, phy, reg, val); +} + +static int +uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) +{ + return (*un->un_ops->uno_write_reg)(un, phy, reg, val); +} + +static void +uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) +{ + (*un->un_ops->uno_statchg)(ifp); +} + +static unsigned +uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) +{ + return (*un->un_ops->uno_tx_prepare)(un, m, c); +} + +static void +uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) +{ + (*un->un_ops->uno_rx_loop)(un, c, total_len); +} + +static void +uno_tick(struct usbnet *un) +{ + if (un->un_ops->uno_tick) + (*un->un_ops->uno_tick)(un); +} + +static void +uno_intr(struct usbnet *un, usbd_status status) +{ + if (un->un_ops->uno_intr) + (*un->un_ops->uno_intr)(un, status); +} + +/* Interrupt handling. */ + +static struct mbuf * +usbnet_newbuf(size_t buflen) +{ + struct mbuf *m; + + MGETHDR(m, M_DONTWAIT, MT_DATA); + if (m == NULL) + return NULL; + + if (buflen > MHLEN - ETHER_ALIGN) { + MCLGET(m, M_DONTWAIT); + if (!(m->m_flags & M_EXT)) { + m_freem(m); + return NULL; + } + } + + m_adj(m, ETHER_ALIGN); + m->m_len = m->m_pkthdr.len = buflen; + + return m; +} + +/* + * usbnet_rxeof() is designed to be the done callback for rx completion. + * it provides generic setup and finalisation, calls a different usbnet + * rx_loop callback in the middle, which can use usbnet_enqueue() to + * enqueue a packet for higher levels (or usbnet_input() if previously + * using if_input() path.) + */ +void +usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen, + int csum_flags, uint32_t csum_data, int mbuf_flags) +{ + USBNETHIST_FUNC(); + struct ifnet * const ifp = usbnet_ifp(un); + struct usbnet_private * const unp __unused = un->un_pri; + struct mbuf *m; + + USBNETHIST_CALLARGSN(5, "%d: enter: len=%zu csf %x mbf %x", + unp->unp_number, buflen, csum_flags, mbuf_flags); + + usbnet_isowned_rx(un); + + m = usbnet_newbuf(buflen); + if (m == NULL) { + DPRINTF("%d: no memory", unp->unp_number, 0, 0, 0); + ifp->if_ierrors++; + return; + } + + m_set_rcvif(m, ifp); + m->m_pkthdr.csum_flags = csum_flags; + m->m_pkthdr.csum_data = csum_data; + m->m_flags |= mbuf_flags; + memcpy(mtod(m, uint8_t *), buf, buflen); + + /* push the packet up */ + if_percpuq_enqueue(ifp->if_percpuq, m); +} + +void +usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen) +{ + USBNETHIST_FUNC(); + struct ifnet * const ifp = usbnet_ifp(un); + struct usbnet_private * const unp __unused = un->un_pri; + struct mbuf *m; + + USBNETHIST_CALLARGSN(5, "%d: enter: buf %jx len %ju", + unp->unp_number, (uintptr_t)buf, buflen, 0); + + usbnet_isowned_rx(un); + + m = usbnet_newbuf(buflen); + if (m == NULL) { + ifp->if_ierrors++; + return; + } + + m_set_rcvif(m, ifp); + memcpy(mtod(m, char *), buf, buflen); + + /* push the packet up */ + if_input(ifp, m); +} + +/* + * A frame has been uploaded: pass the resulting mbuf chain up to + * the higher level protocols. + */ +static void +usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +{ + USBNETHIST_FUNC(); + struct usbnet_chain * const c = priv; + struct usbnet * const un = c->unc_un; + struct usbnet_private * const unp = un->un_pri; + struct ifnet * const ifp = usbnet_ifp(un); + uint32_t total_len; + + USBNETHIST_CALLARGSN(5, "%d: enter: status %x xfer %jx", + unp->unp_number, status, (uintptr_t)xfer, 0); + + mutex_enter(&unp->unp_rxlock); + + if (unp->unp_dying || unp->unp_stopping || + status == USBD_INVAL || status == USBD_NOT_STARTED || + status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) + goto out; + + if (status != USBD_NORMAL_COMPLETION) { + if (usbd_ratecheck(&unp->unp_rx_notice)) + aprint_error_dev(un->un_dev, "usb errors on rx: %s\n", + usbd_errstr(status)); + if (status == USBD_STALLED) + usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]); + goto done; + } + + usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + + if (total_len > un->un_rx_bufsz) { + aprint_error_dev(un->un_dev, + "rxeof: too large transfer (%u > %u)\n", + total_len, un->un_rx_bufsz); + goto done; + } + + uno_rx_loop(un, c, total_len); + usbnet_isowned_rx(un); + +done: + if (unp->unp_dying || unp->unp_stopping) + goto out; + + mutex_exit(&unp->unp_rxlock); + + /* Setup new transfer. */ + usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz, + un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); + usbd_transfer(xfer); + return; + +out: + mutex_exit(&unp->unp_rxlock); +} + +static void +usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet_chain * const c = priv; + struct usbnet * const un = c->unc_un; + struct usbnet_cdata * const cd = un_cdata(un); + struct usbnet_private * const unp = un->un_pri; + struct ifnet * const ifp = usbnet_ifp(un); + + USBNETHIST_CALLARGSN(5, "%d: enter: status %x xfer %jx", + unp->unp_number, status, (uintptr_t)xfer, 0); + + mutex_enter(&unp->unp_txlock); + if (unp->unp_stopping || unp->unp_dying) { + mutex_exit(&unp->unp_txlock); + return; + } + + KASSERT(cd->uncd_tx_cnt > 0); + cd->uncd_tx_cnt--; + + unp->unp_timer = 0; + + switch (status) { + case USBD_NOT_STARTED: + case USBD_CANCELLED: + break; + + case USBD_NORMAL_COMPLETION: + ifp->if_opackets++; + break; + + default: + + ifp->if_oerrors++; + if (usbd_ratecheck(&unp->unp_tx_notice)) + aprint_error_dev(un->un_dev, "usb error on tx: %s\n", + usbd_errstr(status)); + if (status == USBD_STALLED) + usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]); + break; + } + + mutex_exit(&unp->unp_txlock); + + if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd)) + (*ifp->if_start)(ifp); +} + +static void +usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) +{ + struct usbnet * const un = priv; + struct usbnet_private * const unp = un->un_pri; + struct usbnet_intr * const uni = un->un_intr; + struct ifnet * const ifp = usbnet_ifp(un); + + if (uni == NULL || unp->unp_dying || unp->unp_stopping || + status == USBD_INVAL || status == USBD_NOT_STARTED || + status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) + return; + + if (status != USBD_NORMAL_COMPLETION) { + if (usbd_ratecheck(&unp->unp_intr_notice)) { + aprint_error_dev(un->un_dev, "usb error on intr: %s\n", + usbd_errstr(status)); + } + if (status == USBD_STALLED) + usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]); + return; + } + + uno_intr(un, status); +} + +static void +usbnet_start_locked(struct ifnet *ifp) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet * const un = ifp->if_softc; + struct usbnet_cdata * const cd = un_cdata(un); + struct usbnet_private * const unp = un->un_pri; + struct mbuf *m; + unsigned length; + int idx; + + usbnet_isowned_tx(un); + KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt); + + if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) { + DPRINTF("start called no link (%x) or running (flags %x)", + unp->unp_link, ifp->if_flags, 0, 0); + return; + } + + if (cd->uncd_tx_cnt == un->un_tx_list_cnt) { + DPRINTF("start called, tx busy (%jx == %jx)", + cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0); + return; + } + + idx = cd->uncd_tx_prod; + while (cd->uncd_tx_cnt < un->un_tx_list_cnt) { + IFQ_POLL(&ifp->if_snd, m); + if (m == NULL) { + DPRINTF("start called, queue empty", 0, 0, 0, 0); + break; + } + KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz); + + struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; + + length = uno_tx_prepare(un, m, c); + if (length == 0) { + DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0); + ifp->if_oerrors++; + break; + } + + if (__predict_false(c->unc_xfer == NULL)) { + DPRINTF("unc_xfer is NULL", 0, 0, 0, 0); + ifp->if_oerrors++; + break; + } + + usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, + un->un_tx_xfer_flags, 10000, usbnet_txeof); + + /* Transmit */ + usbd_status err = usbd_transfer(c->unc_xfer); + if (err != USBD_IN_PROGRESS) { + DPRINTF("usbd_transfer on %jx for %ju bytes: %d", + (uintptr_t)c->unc_buf, length, err, 0); + ifp->if_oerrors++; + break; + } + + IFQ_DEQUEUE(&ifp->if_snd, m); + + /* + * If there's a BPF listener, bounce a copy of this frame + * to him. + */ + bpf_mtap(ifp, m, BPF_D_OUT); + m_freem(m); + + idx = (idx + 1) % un->un_tx_list_cnt; + cd->uncd_tx_cnt++; + } + cd->uncd_tx_prod = idx; + + /* + * Set a timeout in case the chip goes out to lunch. + */ + unp->unp_timer = 5; +} + +static void +usbnet_start(struct ifnet *ifp) +{ + struct usbnet * const un = ifp->if_softc; + struct usbnet_private * const unp = un->un_pri; + + mutex_enter(&unp->unp_txlock); + if (!unp->unp_stopping) + usbnet_start_locked(ifp); + mutex_exit(&unp->unp_txlock); +} + +/* + * Chain management. + * + * RX and TX are identical. Keep them that way. + */ + +/* Start of common RX functions */ + +static size_t +usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) +{ + return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt; +} + +static void +usbnet_rx_list_alloc(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + + cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP); +} + +static void +usbnet_rx_list_free(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + + if (cd->uncd_rx_chain) { + kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un)); + cd->uncd_rx_chain = NULL; + } +} + +static int +usbnet_rx_list_init(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + struct usbnet_private * const unp = un->un_pri; + + for (size_t i = 0; i < un->un_rx_list_cnt; i++) { + struct usbnet_chain *c = &cd->uncd_rx_chain[i]; + + c->unc_un = un; + if (c->unc_xfer == NULL) { + int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX], + un->un_rx_bufsz, un->un_rx_xfer_flags, 0, + &c->unc_xfer); + if (err) + return err; + c->unc_buf = usbd_get_buffer(c->unc_xfer); + } + } + + return 0; +} + +static void +usbnet_rx_list_fini(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + + for (size_t i = 0; i < un->un_rx_list_cnt; i++) { + struct usbnet_chain *c = &cd->uncd_rx_chain[i]; + + if (c->unc_xfer != NULL) { + usbd_destroy_xfer(c->unc_xfer); + c->unc_xfer = NULL; + c->unc_buf = NULL; + } + } +} + +/* End of common RX functions */ + +static void +usbnet_rx_start_pipes(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + struct usbnet_private * const unp = un->un_pri; + + mutex_enter(&unp->unp_rxlock); + mutex_enter(&unp->unp_txlock); + unp->unp_stopping = false; + + for (size_t i = 0; i < un->un_rx_list_cnt; i++) { + struct usbnet_chain *c = &cd->uncd_rx_chain[i]; + + usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz, + un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); + usbd_transfer(c->unc_xfer); + } + + mutex_exit(&unp->unp_txlock); + mutex_exit(&unp->unp_rxlock); +} + +/* Start of common TX functions */ + +static size_t +usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) +{ + return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt; +} + +static void +usbnet_tx_list_alloc(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + + cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP); +} + +static void +usbnet_tx_list_free(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + + if (cd->uncd_tx_chain) { + kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un)); + cd->uncd_tx_chain = NULL; + } +} + +static int +usbnet_tx_list_init(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + struct usbnet_private * const unp = un->un_pri; + + for (size_t i = 0; i < un->un_tx_list_cnt; i++) { + struct usbnet_chain *c = &cd->uncd_tx_chain[i]; + + c->unc_un = un; + if (c->unc_xfer == NULL) { + int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX], + un->un_tx_bufsz, un->un_tx_xfer_flags, 0, + &c->unc_xfer); + if (err) + return err; + c->unc_buf = usbd_get_buffer(c->unc_xfer); + } + } + + return 0; +} + +static void +usbnet_tx_list_fini(struct usbnet * const un) +{ + struct usbnet_cdata * const cd = un_cdata(un); + + for (size_t i = 0; i < un->un_tx_list_cnt; i++) { + struct usbnet_chain *c = &cd->uncd_tx_chain[i]; + + if (c->unc_xfer != NULL) { + usbd_destroy_xfer(c->unc_xfer); + c->unc_xfer = NULL; + c->unc_buf = NULL; + } + } + cd->uncd_tx_prod = cd->uncd_tx_cnt = 0; +} + +/* End of common TX functions */ + +/* Endpoint pipe management. */ + +static void +usbnet_ep_close_pipes(struct usbnet * const un) +{ + struct usbnet_private * const unp = un->un_pri; + + for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { + if (unp->unp_ep[i] == NULL) + continue; + usbd_status err = usbd_close_pipe(unp->unp_ep[i]); + if (err) + aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i, + usbd_errstr(err)); + unp->unp_ep[i] = NULL; + } +} + +static usbd_status +usbnet_ep_open_pipes(struct usbnet * const un) +{ + struct usbnet_intr * const uni = un->un_intr; + struct usbnet_private * const unp = un->un_pri; + + for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { + usbd_status err; + + if (un->un_ed[i] == 0) + continue; + + if (i == USBNET_ENDPT_INTR && uni) { + err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i], + USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un, + uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr, + uni->uni_interval); + } else { + err = usbd_open_pipe(un->un_iface, un->un_ed[i], + USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]); + } + if (err) { + usbnet_ep_close_pipes(un); + return err; + } + } + + return USBD_NORMAL_COMPLETION; +} + +static usbd_status +usbnet_ep_stop_pipes(struct usbnet * const un) +{ + struct usbnet_private * const unp = un->un_pri; + usbd_status err = USBD_NORMAL_COMPLETION; + + for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { + if (unp->unp_ep[i] == NULL) + continue; + usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]); + if (err == USBD_NORMAL_COMPLETION && err2) + err = err2; + } + + return err; +} + +int +usbnet_init_rx_tx(struct usbnet * const un) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet_private * const unp = un->un_pri; + struct ifnet * const ifp = usbnet_ifp(un); + usbd_status err; + int error = 0; + + usbnet_isowned(un); + + if (unp->unp_dying) { + return EIO; + } + unp->unp_refcnt++; + + /* Open RX and TX pipes. */ + err = usbnet_ep_open_pipes(un); + if (err) { + aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n", + usbd_errstr(err)); + error = EIO; + goto out; + } + + /* Init RX ring. */ + if (usbnet_rx_list_init(un)) { + aprint_error_dev(un->un_dev, "rx list init failed\n"); + error = ENOBUFS; + goto out; + } + + /* Init TX ring. */ + if (usbnet_tx_list_init(un)) { + aprint_error_dev(un->un_dev, "tx list init failed\n"); + error = ENOBUFS; + goto out; + } + + /* Start up the receive pipe(s). */ + usbnet_rx_start_pipes(un); + + /* Indicate we are up and running. */ +#if 0 + /* XXX if_mcast_op() can call this without ifnet locked */ + KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp)); +#endif + ifp->if_flags |= IFF_RUNNING; + + callout_schedule(&unp->unp_stat_ch, hz); + +out: + if (error) { + usbnet_rx_list_fini(un); + usbnet_tx_list_fini(un); + usbnet_ep_close_pipes(un); + } + if (--unp->unp_refcnt < 0) + cv_broadcast(&unp->unp_detachcv); + + usbnet_isowned(un); + + return error; +} + +/* MII management. */ + +/* + * Access functions for MII. Take the MII lock to call access MII regs. + * Two forms: usbnet (softc) lock currently held or not. + */ +void +usbnet_lock_mii(struct usbnet *un) +{ + struct usbnet_private * const unp = un->un_pri; + + mutex_enter(&unp->unp_lock); + unp->unp_refcnt++; + mutex_exit(&unp->unp_lock); + + mutex_enter(&unp->unp_miilock); +} + +void +usbnet_lock_mii_un_locked(struct usbnet *un) +{ + struct usbnet_private * const unp = un->un_pri; + + usbnet_isowned(un); + + unp->unp_refcnt++; + mutex_enter(&unp->unp_miilock); +} + +void +usbnet_unlock_mii(struct usbnet *un) +{ + struct usbnet_private * const unp = un->un_pri; + + mutex_exit(&unp->unp_miilock); + mutex_enter(&unp->unp_lock); + if (--unp->unp_refcnt < 0) + cv_broadcast(&unp->unp_detachcv); + mutex_exit(&unp->unp_lock); +} + +void +usbnet_unlock_mii_un_locked(struct usbnet *un) +{ + struct usbnet_private * const unp = un->un_pri; + + usbnet_isowned(un); + + mutex_exit(&unp->unp_miilock); + if (--unp->unp_refcnt < 0) + cv_broadcast(&unp->unp_detachcv); +} + +kmutex_t * +usbnet_mutex_mii(struct usbnet *un) +{ + struct usbnet_private * const unp = un->un_pri; + + return &unp->unp_miilock; +} + +int +usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) +{ + USBNETHIST_FUNC(); + struct usbnet * const un = device_private(dev); + struct usbnet_private * const unp = un->un_pri; + int err; + + mutex_enter(&unp->unp_lock); + if (unp->unp_dying) { + mutex_exit(&unp->unp_lock); + return EIO; + } + + usbnet_lock_mii_un_locked(un); + mutex_exit(&unp->unp_lock); + err = uno_read_reg(un, phy, reg, val); + usbnet_unlock_mii(un); + + if (err) { + USBNETHIST_CALLARGS("read PHY failed: %d", err, 0, 0, 0); + return err; + } + + return 0; +} + +int +usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) +{ + USBNETHIST_FUNC(); + struct usbnet * const un = device_private(dev); + struct usbnet_private * const unp = un->un_pri; + int err; + + mutex_enter(&unp->unp_lock); + if (unp->unp_dying) { + mutex_exit(&unp->unp_lock); + return EIO; + } + + usbnet_lock_mii_un_locked(un); + mutex_exit(&unp->unp_lock); + err = uno_write_reg(un, phy, reg, val); + usbnet_unlock_mii(un); + + if (err) { + USBNETHIST_CALLARGS("write PHY failed: %d", err, 0, 0, 0); + return err; + } + + return 0; +} + +void +usbnet_mii_statchg(struct ifnet *ifp) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet * const un = ifp->if_softc; + + uno_mii_statchg(un, ifp); +} + +static int +usbnet_media_upd(struct ifnet *ifp) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet * const un = ifp->if_softc; + struct usbnet_private * const unp = un->un_pri; + struct mii_data * const mii = usbnet_mii(un); + + if (unp->unp_dying) + return EIO; + + unp->unp_link = false; + + if (mii->mii_instance) { + struct mii_softc *miisc; + + LIST_FOREACH(miisc, &mii->mii_phys, mii_list) + mii_phy_reset(miisc); + } + + return ether_mediachange(ifp); +} + +/* ioctl */ + +static int +usbnet_ifflags_cb(struct ethercom *ec) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct ifnet *ifp = &ec->ec_if; + struct usbnet *un = ifp->if_softc; + struct usbnet_private * const unp = un->un_pri; + int rv = 0; + + mutex_enter(&unp->unp_lock); + + const int changed = ifp->if_flags ^ unp->unp_if_flags; + if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) { + unp->unp_if_flags = ifp->if_flags; + if ((changed & IFF_PROMISC) != 0) + rv = ENETRESET; + } else { + rv = ENETRESET; + } + + mutex_exit(&unp->unp_lock); + + return rv; +} + +static int +usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data) +{ + USBNETHIST_FUNC(); + struct usbnet * const un = ifp->if_softc; + struct usbnet_private * const unp __unused = un->un_pri; + int error; + + USBNETHIST_CALLARGSN(11, "%d: enter %jx data %x", + unp->unp_number, cmd, (uintptr_t)data, 0); + + if (un->un_ops->uno_override_ioctl) + return uno_override_ioctl(un, ifp, cmd, data); + + error = ether_ioctl(ifp, cmd, data); + if (error == ENETRESET) + error = uno_ioctl(un, ifp, cmd, data); + + return error; +} + +/* + * Generic stop network function: + * - mark as stopping + * - call DD routine to stop the device + * - turn off running, timer, statchg callout, link + * - stop transfers + * - free RX and TX resources + * - close pipes + * + * usbnet_stop() is exported for drivers to use, expects lock held. + * + * usbnet_stop_ifp() is for the if_stop handler. + */ +void +usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) +{ + struct usbnet_private * const unp = un->un_pri; + + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + + usbnet_isowned(un); + + mutex_enter(&unp->unp_rxlock); + mutex_enter(&unp->unp_txlock); + unp->unp_stopping = true; + mutex_exit(&unp->unp_txlock); + mutex_exit(&unp->unp_rxlock); + + uno_stop(un, ifp, disable); + + /* + * XXXSMP Would like to + * KASSERT(IFNET_LOCKED(ifp)) + * here but the locking order is: + * ifnet -> unlock -> rxlock -> txlock + * and unlock is already held. + */ + ifp->if_flags &= ~IFF_RUNNING; + unp->unp_timer = 0; + + callout_stop(&unp->unp_stat_ch); + + /* Stop transfers. */ + usbnet_ep_stop_pipes(un); + + /* Free RX/TX resources. */ + usbnet_rx_list_fini(un); + usbnet_tx_list_fini(un); + + /* Close pipes. */ + usbnet_ep_close_pipes(un); +} + +static void +usbnet_stop_ifp(struct ifnet *ifp, int disable) +{ + struct usbnet * const un = ifp->if_softc; + struct usbnet_private * const unp = un->un_pri; + + mutex_enter(&unp->unp_lock); + usbnet_stop(un, ifp, disable); + mutex_exit(&unp->unp_lock); +} + +/* + * Generic tick task function. + * + * usbnet_tick() is triggered from a callout, and triggers a call to + * usbnet_tick_task() from the usb_task subsystem. + */ +static void +usbnet_tick(void *arg) +{ + struct usbnet * const un = arg; + struct usbnet_private * const unp = un->un_pri; + + mutex_enter(&unp->unp_lock); + if (!unp->unp_stopping && !unp->unp_dying) { + /* Perform periodic stuff in process context */ + usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); + } + mutex_exit(&unp->unp_lock); +} + +static void +usbnet_watchdog(struct ifnet *ifp) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet * const un = ifp->if_softc; + struct usbnet_private * const unp = un->un_pri; + struct usbnet_cdata * const cd = un_cdata(un); + usbd_status err; + + ifp->if_oerrors++; + aprint_error_dev(un->un_dev, "watchdog timeout\n"); + + if (cd->uncd_tx_cnt > 0) { + DPRINTF("uncd_tx_cnt=%u non zero, aborting pipe", 0, 0, 0, 0); + err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); + if (err) + aprint_error_dev(un->un_dev, "pipe abort failed: %s\n", + usbd_errstr(err)); + if (cd->uncd_tx_cnt != 0) + DPRINTF("uncd_tx_cnt now %u", cd->uncd_tx_cnt, 0, 0, 0); + } + + if (!IFQ_IS_EMPTY(&ifp->if_snd)) + (*ifp->if_start)(ifp); +} + +static void +usbnet_tick_task(void *arg) +{ + struct usbnet * const un = arg; + struct usbnet_private * const unp = un->un_pri; + + mutex_enter(&unp->unp_lock); + if (unp->unp_stopping || unp->unp_dying) { + mutex_exit(&unp->unp_lock); + return; + } + + struct ifnet * const ifp = usbnet_ifp(un); + struct mii_data * const mii = usbnet_mii(un); + + unp->unp_refcnt++; + mutex_exit(&unp->unp_lock); + + if (ifp && unp->unp_timer != 0 && --unp->unp_timer == 0) + usbnet_watchdog(ifp); + + if (mii && ifp) { + mii_tick(mii); + + if (!unp->unp_link) + (*mii->mii_statchg)(ifp); + } + + /* Call driver if requested. */ + uno_tick(un); + + mutex_enter(&unp->unp_lock); + if (--unp->unp_refcnt < 0) + cv_broadcast(&unp->unp_detachcv); + if (!unp->unp_stopping && !unp->unp_dying) + callout_schedule(&unp->unp_stat_ch, hz); + mutex_exit(&unp->unp_lock); +} + +static int +usbnet_init(struct ifnet *ifp) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet * const un = ifp->if_softc; + + return uno_init(un, ifp); +} + + +/* Various accessors. */ + +void +usbnet_set_link(struct usbnet *un, bool link) +{ + un->un_pri->unp_link = link; +} + +void +usbnet_set_dying(struct usbnet *un, bool link) +{ + un->un_pri->unp_dying = link; +} + +struct ifnet * +usbnet_ifp(struct usbnet *un) +{ + return &un->un_pri->unp_ec.ec_if; +} + +struct ethercom * +usbnet_ec(struct usbnet *un) +{ + return &un->un_pri->unp_ec; +} + +struct mii_data * +usbnet_mii(struct usbnet *un) +{ + return un->un_pri->unp_ec.ec_mii; +} + +krndsource_t * +usbnet_rndsrc(struct usbnet *un) +{ + return &un->un_pri->unp_rndsrc; +} + +void * +usbnet_softc(struct usbnet *un) +{ + //return un->un_pri->unp_sc; + return un->un_sc; +} + +bool +usbnet_havelink(struct usbnet *un) +{ + return un->un_pri->unp_link; +} + +bool +usbnet_isdying(struct usbnet *un) +{ + return un->un_pri->unp_dying; +} + + +/* Locking. */ + +void +usbnet_lock(struct usbnet *un) +{ + mutex_enter(&un->un_pri->unp_lock); +} + +void +usbnet_unlock(struct usbnet *un) +{ + mutex_exit(&un->un_pri->unp_lock); +} + +kmutex_t * +usbnet_mutex(struct usbnet *un) +{ + return &un->un_pri->unp_lock; +} + +void +usbnet_lock_rx(struct usbnet *un) +{ + mutex_enter(&un->un_pri->unp_rxlock); +} + +void +usbnet_unlock_rx(struct usbnet *un) +{ + mutex_exit(&un->un_pri->unp_rxlock); +} + +kmutex_t * +usbnet_mutex_rx(struct usbnet *un) +{ + return &un->un_pri->unp_rxlock; +} + +void +usbnet_lock_tx(struct usbnet *un) +{ + mutex_enter(&un->un_pri->unp_txlock); +} + +void +usbnet_unlock_tx(struct usbnet *un) +{ + mutex_exit(&un->un_pri->unp_txlock); +} + +kmutex_t * +usbnet_mutex_tx(struct usbnet *un) +{ + return &un->un_pri->unp_txlock; +} + +/* Autoconf management. */ + +static bool +usbnet_empty_eaddr(struct usbnet * const un) +{ + return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 && + un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 && + un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0); +} + +/* + * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant + * 'usbnet'. The first is enough to enable device access (eg, endpoints + * are connected and commands can be sent), and the second connects the + * device to the system networking. + * + * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped. + * Also usable as driver detach directly. + * + * To skip ethernet configuration (eg, point-to-point), make sure that + * the un_eaddr[] is fully zero. + */ + +void +usbnet_attach(struct usbnet *un, + const char *detname) /* detach cv name */ +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + + /* Required inputs. */ + KASSERT(un->un_ops->uno_tx_prepare); + KASSERT(un->un_ops->uno_rx_loop); + KASSERT(un->un_ops->uno_init); + KASSERT(un->un_rx_bufsz); + KASSERT(un->un_tx_bufsz); + KASSERT(un->un_rx_list_cnt); + KASSERT(un->un_tx_list_cnt); + + /* Unfortunate fact. */ + KASSERT(un == device_private(un->un_dev)); + + un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP); + struct usbnet_private * const unp = un->un_pri; + + usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE); + callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE); + callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un); + + mutex_init(&unp->unp_miilock, MUTEX_DEFAULT, IPL_NONE); + mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); + mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); + mutex_init(&unp->unp_lock, MUTEX_DEFAULT, IPL_NONE); + cv_init(&unp->unp_detachcv, detname); + + rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev), + RND_TYPE_NET, RND_FLAG_DEFAULT); + + usbnet_rx_list_alloc(un); + usbnet_tx_list_alloc(un); + + unp->unp_number = atomic_inc_uint_nv(&usbnet_number); + + unp->unp_attached = true; +} + +static void +usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet_private * const unp = un->un_pri; + struct mii_data * const mii = &unp->unp_mii; + struct ifnet * const ifp = usbnet_ifp(un); + + KASSERT(un->un_ops->uno_read_reg); + KASSERT(un->un_ops->uno_write_reg); + KASSERT(un->un_ops->uno_statchg); + + mii->mii_ifp = ifp; + mii->mii_readreg = usbnet_mii_readreg; + mii->mii_writereg = usbnet_mii_writereg; + mii->mii_statchg = usbnet_mii_statchg; + mii->mii_flags = MIIF_AUTOTSLEEP; + + usbnet_ec(un)->ec_mii = mii; + ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus); + mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc, + unm->un_mii_offset, unm->un_mii_flags); + + if (LIST_FIRST(&mii->mii_phys) == NULL) { + ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); + ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); + } else + ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); +} + +void +usbnet_attach_ifp(struct usbnet *un, + unsigned if_flags, /* additional if_flags */ + unsigned if_extflags, /* additional if_extflags */ + const struct usbnet_mii *unm) /* additional mii_attach flags */ +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet_private * const unp = un->un_pri; + struct ifnet * const ifp = usbnet_ifp(un); + + KASSERT(unp->unp_attached); + + strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ); + ifp->if_flags = if_flags; + ifp->if_extflags = IFEF_MPSAFE | if_extflags; + ifp->if_ioctl = usbnet_ioctl; + ifp->if_start = usbnet_start; + ifp->if_init = usbnet_init; + ifp->if_stop = usbnet_stop_ifp; + + if (unm) + usbnet_attach_mii(un, unm); + else + unp->unp_link = true; + + /* Attach the interface. */ + int rv = if_initialize(ifp); + if (rv != 0) { + aprint_error_dev(un->un_dev, "if_initialize failed(%d)\n", rv); + return; + } + if (ifp->_if_input == NULL) + ifp->if_percpuq = if_percpuq_create(ifp); + if_register(ifp); + + /* + * If ethernet address is all zero, skip ether_ifattach() and + * instead attach bpf here.. + */ + if (!usbnet_empty_eaddr(un)) { + ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb); + aprint_normal_dev(un->un_dev, "Ethernet address %s\n", + ether_sprintf(un->un_eaddr)); + ether_ifattach(ifp, un->un_eaddr); + } else { + if_alloc_sadl(ifp); + bpf_attach(ifp, DLT_RAW, 0); + } + + /* Now ready, and attached. */ + IFQ_SET_READY(&ifp->if_snd); + ifp->if_softc = un; + + usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev); + + if (!pmf_device_register(un->un_dev, NULL, NULL)) + aprint_error_dev(un->un_dev, "couldn't establish power handler\n"); +} + +int +usbnet_detach(device_t self, int flags) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet * const un = device_private(self); + struct usbnet_private * const unp = un->un_pri; + + /* Detached before attached finished, so just bail out. */ + if (unp == NULL || !unp->unp_attached) + return 0; + + struct ifnet * const ifp = usbnet_ifp(un); + struct mii_data * const mii = usbnet_mii(un); + + mutex_enter(&unp->unp_lock); + unp->unp_dying = true; + mutex_exit(&unp->unp_lock); + + callout_halt(&unp->unp_stat_ch, NULL); + usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, NULL); + + if (ifp->if_flags & IFF_RUNNING) { + IFNET_LOCK(ifp); + usbnet_stop_ifp(ifp, 1); + IFNET_UNLOCK(ifp); + } + + mutex_enter(&unp->unp_lock); + unp->unp_refcnt--; + while (unp->unp_refcnt > 0) { + /* Wait for processes to go away */ + cv_wait(&unp->unp_detachcv, &unp->unp_lock); + } + mutex_exit(&unp->unp_lock); + + usbnet_rx_list_free(un); + usbnet_tx_list_free(un); + + callout_destroy(&unp->unp_stat_ch); + rnd_detach_source(&unp->unp_rndsrc); + + if (mii) { + mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY); + ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY); + } + if (ifp->if_softc) { + if (!usbnet_empty_eaddr(un)) + ether_ifdetach(ifp); + else + bpf_detach(ifp); + if_detach(ifp); + } + + cv_destroy(&unp->unp_detachcv); + mutex_destroy(&unp->unp_lock); + mutex_destroy(&unp->unp_rxlock); + mutex_destroy(&unp->unp_txlock); + mutex_destroy(&unp->unp_miilock); + + pmf_device_deregister(un->un_dev); + + usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev); + + kmem_free(unp, sizeof(*unp)); + + return 0; +} + +int +usbnet_activate(device_t self, devact_t act) +{ + USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet * const un = device_private(self); + struct usbnet_private * const unp = un->un_pri; + struct ifnet * const ifp = usbnet_ifp(un); + + switch (act) { + case DVACT_DEACTIVATE: + if_deactivate(ifp); + + mutex_enter(&unp->unp_lock); + unp->unp_dying = true; + mutex_exit(&unp->unp_lock); + + mutex_enter(&unp->unp_rxlock); + mutex_enter(&unp->unp_txlock); + unp->unp_stopping = true; + mutex_exit(&unp->unp_txlock); + mutex_exit(&unp->unp_rxlock); + + return 0; + default: + return EOPNOTSUPP; + } +} + +MODULE(MODULE_CLASS_MISC, usbnet, NULL); + +static int +usbnet_modcmd(modcmd_t cmd, void *arg) +{ + switch (cmd) { + case MODULE_CMD_INIT: + return 0; + case MODULE_CMD_FINI: + return 0; + case MODULE_CMD_STAT: + case MODULE_CMD_AUTOUNLOAD: + default: + return ENOTTY; + } +} Index: usb/usbnet.h =================================================================== RCS file: usb/usbnet.h diff -N usb/usbnet.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ usb/usbnet.h 29 Aug 2019 06:22:05 -0000 @@ -0,0 +1,397 @@ +/* $NetBSD$ */ + +/* + * Copyright (c) 2019 Matthew R. Green + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _DEV_USB_USBNET_H +#define _DEV_USB_USBNET_H + +/* + * Common code/data shared by all USB ethernet drivers (using these routines.) + * + * This framework provides the following features for USB ethernet drivers: + * + * - USB endpoint pipe handling + * - rx and tx chain handling + * - generic handlers or support for several struct ifnet callbacks + * - MII bus locking + * - interrupt handling + * - partial autoconf handling + * + * Consumers of this interface need to: + * + * - replace most softc members with "struct usbnet" usage, in particular + * use usbnet pointer for ifp->if_softc, and device_private (real softc + * can be stored in un_sc member) + * - use MII bus lock / access methods + * - usbnet_attach() to initialise and allocate rx/tx chains + * - usbnet_attach_ifp() to attach the interface, and either ether_ifattach() + * for ethernet devices, or if_alloc_sadl()/bpf_attach() pair otherwise. + * - usbnet_detach() to clean them up + * - usbnet_activate() for autoconf + * - interface ioctl and start have direct frontends with callbacks for + * device specific handling: + * - ioctl can use either a device-specific override (useful for special + * cases), but provides a normal handler with callback to handle + * ENETRESET conditions that should be sufficient for most users + * - start uses usbnet send callback + * - interface init and stop have helper functions + * - device specific init should use usbnet_init_rx_tx() to open pipes + * to the device and setup the rx/tx chains for use after any device + * specific setup + * - usbnet_stop() must be called with the un_lock held, and will + * call the device-specific usbnet stop callback, which enables the + * standard init calls stop idiom. + * - interrupt handling: + * - for rx, usbnet_init_rx_tx() will enable the receive pipes and + * call the rx_loop callback to handle device specific processing of + * packets, which can use usbnet_enqueue() to provide data to the + * higher layers + * - for tx, usbnet_start (if_start) will pull entries out of the + * transmit queue and use the send callback for the given mbuf. + * the usb callback will use usbnet_txeof() for the transmit + * completion function (internal to usbnet) + * - there is special interrupt pipe handling + * - timer/tick: + * - the uno_tick callback will be called once a second if present. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +/* + * Per-transfer data. + * + * Front-end must set un_rx_list_cnt and un_tx_list_cnt before + * calling usbnet_attach(), and then call usbnet_rx_tx_init() + * which will allocate the chain arrays, and must be NULL to + * indicate the first call. + */ +struct usbnet; +struct usbnet_chain { + struct usbnet *unc_un; + struct usbd_xfer *unc_xfer; + uint8_t *unc_buf; +}; + +/* + * Extend this as necessary. axe(4) claims to want 6 total, but + * does not implement them. + */ +enum usbnet_ep { + USBNET_ENDPT_RX, + USBNET_ENDPT_TX, + USBNET_ENDPT_INTR, + USBNET_ENDPT_MAX, +}; + +/* Interface stop callback. */ +typedef void (*usbnet_stop_cb)(struct ifnet *, int); +/* Interface ioctl callback. */ +typedef int (*usbnet_ioctl_cb)(struct ifnet *, u_long, void *); +/* Initialise device callback. */ +typedef int (*usbnet_init_cb)(struct ifnet *); + +/* MII read register callback. */ +typedef int (*usbnet_mii_read_reg_cb)(struct usbnet *, int reg, + int phy, uint16_t *val); +/* MII write register callback. */ +typedef int (*usbnet_mii_write_reg_cb)(struct usbnet *, int reg, + int phy, uint16_t val); +/* MII status change callback. */ +typedef void (*usbnet_mii_statchg_cb)(struct ifnet *); + +/* Prepare packet to send callback, returns length. */ +typedef unsigned (*usbnet_tx_prepare_cb)(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +/* Receive some packets callback. */ +typedef void (*usbnet_rx_loop_cb)(struct usbnet *, struct usbnet_chain *, + uint32_t); +/* Tick callback. */ +typedef void (*usbnet_tick_cb)(struct usbnet *); +/* Interrupt pipe callback. */ +typedef void (*usbnet_intr_cb)(struct usbnet *, usbd_status); + +struct usbnet_ops { + usbnet_stop_cb uno_stop; + usbnet_ioctl_cb uno_ioctl; + usbnet_ioctl_cb uno_override_ioctl; + usbnet_init_cb uno_init; + usbnet_mii_read_reg_cb uno_read_reg; + usbnet_mii_write_reg_cb uno_write_reg; + usbnet_mii_statchg_cb uno_statchg; + usbnet_tx_prepare_cb uno_tx_prepare; + usbnet_rx_loop_cb uno_rx_loop; + usbnet_tick_cb uno_tick; + usbnet_intr_cb uno_intr; +}; + +/* + * USB interrupt pipe support. Use this if usbd_open_pipe_intr() should + * be used for the interrupt pipe. + */ +struct usbnet_intr { + /* + * Point un_intr to this structure to use usbd_open_pipe_intr() not + * usbd_open_pipe() for USBNET_ENDPT_INTR, with this buffer, size, + * and interval. + */ + void *uni_buf; + unsigned uni_bufsz; + unsigned uni_interval; +}; + +/* + * Structure to setup MII. Use the USBNET_MII_DECL_DEFAULT() macro for + * sane default. Pass a copy to usbnet_attach_ifp(). Not used + * after the usbnet_attach_ifp() function returns. + */ +struct usbnet_mii { + int un_mii_flags; + int un_mii_capmask; + int un_mii_phyloc; + int un_mii_offset; +}; + +#define UBSNET_MII_DECL(name, capmask, loc, off, flags) \ + struct usbnet_mii name = { \ + .un_mii_capmask = capmask, \ + .un_mii_phyloc = loc, \ + .un_mii_offset = off, \ + .un_mii_flags = flags, \ + } +#define USBNET_MII_DECL_DEFAULT(name) \ + UBSNET_MII_DECL(name, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0) + +/* + * Generic USB ethernet structure. Use this as ifp->if_softc and set as + * device_private() in attach unless already using struct usbnet here. + * + * Devices without MII should call usbnet_attach_ifp() with have_mii set + * to true, and should ensure that the un_statchg_cb callback sets the + * un_link member. Devices without MII have this forced to true. + */ +struct usbnet_private; +struct usbnet { + /* + * This section should be filled in before calling + * usbnet_attach(). + */ + void *un_sc; /* real softc */ + device_t un_dev; + struct usbd_interface *un_iface; + struct usbd_device *un_udev; + struct usbnet_ops *un_ops; + struct usbnet_intr *un_intr; + + /* Inputs for rx/tx chain control. */ + unsigned un_rx_bufsz; + unsigned un_tx_bufsz; + unsigned un_rx_list_cnt; + unsigned un_tx_list_cnt; + int un_rx_xfer_flags; + int un_tx_xfer_flags; + + /* + * This section should be filled in before calling + * usbnet_attach_ifp(). + */ + enum usbnet_ep un_ed[USBNET_ENDPT_MAX]; + + /* MII specific. Not used without MII. */ + int un_phyno; + /* Ethernet specific. All zeroes indicates non-Ethernet. */ + uint8_t un_eaddr[ETHER_ADDR_LEN]; + + /* + * This section is for driver to use, not touched by usbnet. + */ + unsigned un_flags; + + /* + * This section is private to usbnet. Don't touch. + */ + struct usbnet_private *un_pri; + + /* + * NetBSD 9.x specific portion. Not needed afterwards. + * + * This structure will be passed to sysctl_teardown() in + * usbnet_detach() if it is not NULL. + */ + struct sysctllog *un_sysctl_log; +}; + +/* Various accessors. */ + +void usbnet_set_link(struct usbnet *, bool); +void usbnet_set_dying(struct usbnet *, bool); + +struct ifnet *usbnet_ifp(struct usbnet *); +struct ethercom *usbnet_ec(struct usbnet *); +struct mii_data *usbnet_mii(struct usbnet *); +krndsource_t *usbnet_rndsrc(struct usbnet *); +void *usbnet_softc(struct usbnet *); + +bool usbnet_havelink(struct usbnet *); +bool usbnet_isdying(struct usbnet *); + + +/* + * Locking. Note that the isowned() are implemented here so that + * empty-KASSERT() causes them to be elided for non-DIAG builds. + */ +void usbnet_lock(struct usbnet *); +void usbnet_unlock(struct usbnet *); +kmutex_t *usbnet_mutex(struct usbnet *); +static __inline__ void +usbnet_isowned(struct usbnet *un) +{ + KASSERT(mutex_owned(usbnet_mutex(un))); +} + +void usbnet_lock_rx(struct usbnet *); +void usbnet_unlock_rx(struct usbnet *); +kmutex_t *usbnet_mutex_rx(struct usbnet *); +static __inline__ void +usbnet_isowned_rx(struct usbnet *un) +{ + KASSERT(mutex_owned(usbnet_mutex_rx(un))); +} + +void usbnet_lock_tx(struct usbnet *); +void usbnet_unlock_tx(struct usbnet *); +kmutex_t *usbnet_mutex_tx(struct usbnet *); +static __inline__ void +usbnet_isowned_tx(struct usbnet *un) +{ + KASSERT(mutex_owned(usbnet_mutex_tx(un))); +} + +/* + * Endpoint / rx/tx chain management: + * + * usbnet_attach() initialises usbnet and allocates rx and tx chains + * usbnet_init_rx_tx() open pipes, initialises the rx/tx chains for use + * usbnet_stop() stops pipes, cleans (not frees) rx/tx chains, locked + * version assumes un_lock is held + * usbnet_detach() frees the rx/tx chains + * + * Setup un_ed[] with valid end points before calling usbnet_attach(). + * Call usbnet_init_rx_tx() to initialise pipes, which will be open + * upon success. + */ +int usbnet_init_rx_tx(struct usbnet * const); + +/* MII. */ +void usbnet_lock_mii(struct usbnet *); +void usbnet_lock_mii_un_locked(struct usbnet *); +void usbnet_unlock_mii(struct usbnet *); +void usbnet_unlock_mii_un_locked(struct usbnet *); +kmutex_t *usbnet_mutex_mii(struct usbnet *); +static __inline__ void +usbnet_isowned_mii(struct usbnet *un) +{ + KASSERT(mutex_owned(usbnet_mutex_mii(un))); +} + + +int usbnet_mii_readreg(device_t, int, int, uint16_t *); +int usbnet_mii_writereg(device_t, int, int, uint16_t); +void usbnet_mii_statchg(struct ifnet *); + +/* interrupt handling */ +void usbnet_enqueue(struct usbnet * const, uint8_t *, size_t, int, + uint32_t, int); +void usbnet_input(struct usbnet * const, uint8_t *, size_t); + +/* autoconf */ +void usbnet_attach(struct usbnet *un, const char *); +void usbnet_attach_ifp(struct usbnet *, unsigned, unsigned, const struct usbnet_mii *); +int usbnet_detach(device_t, int); +int usbnet_activate(device_t, devact_t); + +/* stop backend */ +void usbnet_stop(struct usbnet *, struct ifnet *, int); + +/* module hook up */ + +#ifdef _MODULE +#define USENET_INIT(name) \ + error = config_init_component(cfdriver_ioconf_##name, \ + cfattach_ioconf_##name, cfdata_ioconf_##name); +#define USENET_FINI(name) \ + error = config_fini_component(cfdriver_ioconf_##name, \ + cfattach_ioconf_##name, cfdata_ioconf_##name); +#else +#define USENET_INIT(name) +#define USENET_FINI(name) +#endif + +#define USBNET_MODULE(name) \ + \ +MODULE(MODULE_CLASS_DRIVER, if_##name, "usbnet"); \ + \ +static int \ +if_##name##_modcmd(modcmd_t cmd, void *aux) \ +{ \ + int error = 0; \ + \ + switch (cmd) { \ + case MODULE_CMD_INIT: \ + USENET_INIT(name) \ + return error; \ + case MODULE_CMD_FINI: \ + USENET_FINI(name) \ + return error; \ + default: \ + return ENOTTY; \ + } \ +} + +#endif /* _DEV_USB_USBNET_H */ Index: usb/usbroothub.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usbroothub.c,v retrieving revision 1.8 diff -p -u -u -r1.8 usbroothub.c --- usb/usbroothub.c 27 Jan 2019 02:08:42 -0000 1.8 +++ usb/usbroothub.c 29 Aug 2019 06:22:05 -0000 @@ -366,20 +366,20 @@ roothub_ctrl_start(struct usbd_xfer *xfe usb_device_request_t *req; usbd_status err = USBD_IOERROR; /* XXX STALL? */ uint16_t len, value; - int buflen, actlen; + int buflen, actlen = -1; void *buf; - USBHIST_FUNC(); USBHIST_CALLED(usbdebug); + USBHIST_FUNC(); KASSERT(xfer->ux_rqflags & URQ_REQUEST); req = &xfer->ux_request; - USBHIST_LOG(usbdebug, "type=%#2jx request=%#2jx", req->bmRequestType, - req->bRequest, 0, 0); - len = UGETW(req->wLength); value = UGETW(req->wValue); + USBHIST_CALLARGS(usbdebug, "type=%#jx request=%#jx len=%#jx value=%#jx", + req->bmRequestType, req->bRequest, len, value); + buf = len ? usbd_get_buffer(xfer) : NULL; buflen = 0; @@ -553,8 +553,6 @@ roothub_ctrl_start(struct usbd_xfer *xfe } actlen = bus->ub_methods->ubm_rhctrl(bus, req, buf, buflen); - USBHIST_LOG(usbdebug, "xfer %#jx buflen %jd actlen %jd", - (uintptr_t)xfer, buflen, actlen, 0); if (actlen < 0) goto fail; @@ -562,7 +560,8 @@ roothub_ctrl_start(struct usbd_xfer *xfe err = USBD_NORMAL_COMPLETION; fail: - USBHIST_LOG(usbdebug, "xfer %#jx err %jd", (uintptr_t)xfer, err, 0, 0); + USBHIST_LOG(usbdebug, "xfer %#jx buflen %jd actlen %jd err %jd", + (uintptr_t)xfer, buflen, actlen, err); xfer->ux_status = err; mutex_enter(bus->ub_lock); Index: usb/xhci.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/xhci.c,v retrieving revision 1.107.2.1 diff -p -u -u -r1.107.2.1 xhci.c --- usb/xhci.c 21 Aug 2019 14:34:56 -0000 1.107.2.1 +++ usb/xhci.c 29 Aug 2019 06:22:05 -0000 @@ -753,6 +753,8 @@ xhci_hc_reset(struct xhci_softc * const static void xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp) { + XHCIHIST_FUNC(); XHCIHIST_CALLED(); + /* XXX Cache this lot */ const uint32_t w0 = xhci_read_4(sc, ecp); @@ -776,11 +778,12 @@ xhci_id_protocols(struct xhci_softc *sc, case 0x0200: case 0x0300: case 0x0301: + case 0x0310: aprint_debug_dev(sc->sc_dev, " %s ports %d - %d\n", major == 3 ? "ss" : "hs", cpo, cpo + cpc -1); break; default: - aprint_debug_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n", + aprint_error_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n", major, minor); return; }