Index: files.usb =================================================================== RCS file: /cvsroot/src/sys/dev/usb/files.usb,v retrieving revision 1.163 diff -p -u -r1.163 files.usb --- files.usb 11 Aug 2019 11:17:35 -0000 1.163 +++ files.usb 12 Aug 2019 17:10:22 -0000 @@ -334,7 +334,7 @@ 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 @@ -344,22 +344,22 @@ 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 @@ -374,7 +374,7 @@ 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 Index: if_aue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_aue.c,v retrieving revision 1.156 diff -p -u -r1.156 if_aue.c --- if_aue.c 7 Aug 2019 06:31:03 -0000 1.156 +++ if_aue.c 12 Aug 2019 17:10:22 -0000 @@ -87,41 +87,16 @@ __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1 #include #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 - #ifdef AUE_DEBUG #define DPRINTF(x) if (auedebug) printf x #define DPRINTFN(n, x) if (auedebug >= (n)) printf x @@ -131,6 +106,21 @@ int auedebug = 0; #define DPRINTFN(n, x) #endif +#define AUE_TX_LIST_CNT 1 +#define AUE_RX_LIST_CNT 1 + +struct aue_softc { + struct usbnet aue_un; + struct usbnet_intr aue_intr; + struct aue_intrpkt aue_ibuf; +}; + +#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 */ + /* * Various supported device vendors/products. */ @@ -142,7 +132,7 @@ struct aue_type { #define PII 0x0004 /* Pegasus II chip */ }; -Static const struct aue_type aue_devs[] = { +static const struct aue_type aue_devs[] = { {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII }, {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA | PII }, {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII }, @@ -210,49 +200,43 @@ Static const struct aue_type aue_devs[] int aue_match(device_t, cfdata_t, void *); void aue_attach(device_t, device_t, void *); -int aue_detach(device_t, int); -int aue_activate(device_t, enum devact); CFATTACH_DECL_NEW(aue, sizeof(struct aue_softc), aue_match, aue_attach, - aue_detach, aue_activate); + usbnet_detach, usbnet_activate); + +static void aue_reset_pegasus_II(struct aue_softc *); + +static void aue_stop_cb(struct ifnet *, int); +static int aue_ioctl_cb(struct ifnet *, u_long, void *); +static usbd_status aue_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static usbd_status aue_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void aue_mii_statchg(struct ifnet *); +static unsigned aue_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void aue_rx_loop(struct usbnet *un, struct usbd_xfer *xfer, + struct usbnet_chain *c, uint32_t total_len); +static int aue_init(struct ifnet *); +static void aue_intr(struct usbnet *, usbd_status); + +static struct usbnet_ops aue_ops = { + .uno_stop = aue_stop_cb, + .uno_ioctl = aue_ioctl_cb, + .uno_read_reg = aue_mii_read_reg, + .uno_write_reg = aue_mii_write_reg, + .uno_statchg = aue_mii_statchg, + .uno_tx_prepare = aue_tx_prepare, + .uno_rx_loop = aue_rx_loop, + .uno_init = aue_init, + .uno_intr = aue_intr, +}; -Static void aue_multithread(void *); +static uint32_t aue_crc(void *); +static void aue_reset(struct aue_softc *); -Static void aue_reset_pegasus_II(struct aue_softc *); -Static int aue_tx_list_init(struct aue_softc *); -Static int aue_rx_list_init(struct aue_softc *); -Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *); -Static int aue_send(struct aue_softc *, struct mbuf *, int); -Static void aue_intr(struct usbd_xfer *, void *, usbd_status); -Static void aue_rxeof(struct usbd_xfer *, void *, usbd_status); -Static void aue_txeof(struct usbd_xfer *, void *, usbd_status); -Static void aue_tick(void *); -Static void aue_tick_task(void *); -Static void aue_start(struct ifnet *); -Static int aue_ioctl(struct ifnet *, u_long, void *); -Static void aue_init(void *); -Static void aue_stop(struct aue_softc *); -Static void aue_watchdog(struct ifnet *); -Static int aue_openpipes(struct aue_softc *); -Static int aue_ifmedia_upd(struct ifnet *); - -Static int aue_eeprom_getword(struct aue_softc *, int); -Static void aue_read_mac(struct aue_softc *, u_char *); -Static int aue_miibus_readreg(device_t, int, int, uint16_t *); -Static int aue_miibus_writereg(device_t, int, int, uint16_t); -Static void aue_miibus_statchg(struct ifnet *); - -Static void aue_lock_mii(struct aue_softc *); -Static void aue_unlock_mii(struct aue_softc *); - -Static void aue_setmulti(struct aue_softc *); -Static uint32_t aue_crc(void *); -Static void aue_reset(struct aue_softc *); - -Static int aue_csr_read_1(struct aue_softc *, int); -Static int aue_csr_write_1(struct aue_softc *, int, int); -Static int aue_csr_read_2(struct aue_softc *, int); -Static int aue_csr_write_2(struct aue_softc *, int, int); +static int aue_csr_read_1(struct aue_softc *, int); +static int aue_csr_write_1(struct aue_softc *, int, int); +static int aue_csr_read_2(struct aue_softc *, int); +static int aue_csr_write_2(struct aue_softc *, int, int); #define AUE_SETBIT(sc, reg, x) \ aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x)) @@ -260,14 +244,17 @@ Static int aue_csr_write_2(struct aue_so #define AUE_CLRBIT(sc, reg, x) \ aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x)) -Static int +static int aue_csr_read_1(struct aue_softc *sc, int reg) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uByte val = 0; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -276,25 +263,28 @@ aue_csr_read_1(struct aue_softc *sc, int USETW(req.wIndex, reg); USETW(req.wLength, 1); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return 0; } return val; } -Static int +static int aue_csr_read_2(struct aue_softc *sc, int reg) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uWord val; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -303,25 +293,28 @@ aue_csr_read_2(struct aue_softc *sc, int USETW(req.wIndex, reg); USETW(req.wLength, 2); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return 0; } return UGETW(val); } -Static int +static int aue_csr_write_1(struct aue_softc *sc, int reg, int aval) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uByte val; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; val = aval; @@ -331,25 +324,28 @@ aue_csr_write_1(struct aue_softc *sc, in USETW(req.wIndex, reg); USETW(req.wLength, 1); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return -1; } return 0; } -Static int +static int aue_csr_write_2(struct aue_softc *sc, int reg, int aval) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uWord val; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; USETW(val, aval); @@ -359,11 +355,11 @@ aue_csr_write_2(struct aue_softc *sc, in USETW(req.wIndex, reg); USETW(req.wLength, 2); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return -1; } @@ -373,10 +369,11 @@ aue_csr_write_2(struct aue_softc *sc, in /* * Read a word of data stored in the EEPROM at address 'addr.' */ -Static int +static int aue_eeprom_getword(struct aue_softc *sc, int addr) { - int i; + struct usbnet * const un = &sc->aue_un; + int i; aue_csr_write_1(sc, AUE_EE_REG, addr); aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ); @@ -388,7 +385,7 @@ aue_eeprom_getword(struct aue_softc *sc, if (i == AUE_TIMEOUT) { printf("%s: EEPROM read timed out\n", - device_xname(sc->aue_dev)); + device_xname(un->un_dev)); } return aue_csr_read_2(sc, AUE_EE_DATA); @@ -397,14 +394,17 @@ aue_eeprom_getword(struct aue_softc *sc, /* * Read the MAC from the EEPROM. It's at offset 0. */ -Static void -aue_read_mac(struct aue_softc *sc, u_char *dest) +static void +aue_read_mac(struct usbnet *un, u_char *dest) { + struct aue_softc *sc = usbnet_softc(un); int i; int off = 0; int word; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + usbnet_isowned_mii(un); + + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); for (i = 0; i < 3; i++) { word = aue_eeprom_getword(sc, off + i); @@ -413,34 +413,13 @@ aue_read_mac(struct aue_softc *sc, u_cha } } -/* Get exclusive access to the MII registers */ -Static void -aue_lock_mii(struct aue_softc *sc) +static usbd_status +aue_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - sc->aue_refcnt++; - mutex_enter(&sc->aue_mii_lock); -} - -Static void -aue_unlock_mii(struct aue_softc *sc) -{ - mutex_exit(&sc->aue_mii_lock); - if (--sc->aue_refcnt < 0) - usb_detach_wakeupold(sc->aue_dev); -} - -Static int -aue_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) -{ - struct aue_softc *sc = device_private(dev); - int i, rv = 0; + struct aue_softc *sc = usbnet_softc(un); + int i; - if (sc->aue_dying) { -#ifdef DIAGNOSTIC - printf("%s: dying\n", device_xname(sc->aue_dev)); -#endif - return -1; - } + usbnet_isowned_mii(un); #if 0 /* @@ -456,11 +435,10 @@ aue_miibus_readreg(device_t dev, int phy if (sc->aue_vendor == USB_VENDOR_ADMTEK && sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { if (phy == 3) - return -1; + return USBD_INVAL; } #endif - aue_lock_mii(sc); aue_csr_write_1(sc, AUE_PHY_ADDR, phy); aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ); @@ -470,39 +448,37 @@ aue_miibus_readreg(device_t dev, int phy } if (i == AUE_TIMEOUT) { - printf("%s: MII read timed out\n", device_xname(sc->aue_dev)); - rv = ETIMEDOUT; - goto out; + printf("%s: MII read timed out\n", device_xname(un->un_dev)); + return USBD_TIMEOUT; } *val = aue_csr_read_2(sc, AUE_PHY_DATA); DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04hx\n", - device_xname(sc->aue_dev), __func__, phy, reg, *val)); + device_xname(un->un_dev), __func__, phy, reg, *val)); -out: - aue_unlock_mii(sc); - return rv; + return USBD_NORMAL_COMPLETION; } -Static int -aue_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +static usbd_status +aue_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct aue_softc *sc = device_private(dev); - int i, rv = 0; + struct aue_softc *sc = usbnet_softc(un); + int i; + + usbnet_isowned_mii(un); #if 0 if (sc->aue_vendor == USB_VENDOR_ADMTEK && sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { if (phy == 3) - return -1; + return USBD_INVAL; } #endif DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04hx\n", - device_xname(sc->aue_dev), __func__, phy, reg, val)); + device_xname(un->un_dev), __func__, phy, reg, val)); - aue_lock_mii(sc); aue_csr_write_2(sc, AUE_PHY_DATA, val); aue_csr_write_1(sc, AUE_PHY_ADDR, phy); aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE); @@ -513,23 +489,23 @@ aue_miibus_writereg(device_t dev, int ph } if (i == AUE_TIMEOUT) { - printf("%s: MII read timed out\n", device_xname(sc->aue_dev)); - rv = ETIMEDOUT; + printf("%s: MII read timed out\n", device_xname(un->un_dev)); + return USBD_TIMEOUT; } - aue_unlock_mii(sc); - return rv; + return USBD_NORMAL_COMPLETION; } -Static void -aue_miibus_statchg(struct ifnet *ifp) +static void +aue_mii_statchg(struct ifnet *ifp) { - struct aue_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); + struct usbnet *un = ifp->if_softc; + struct aue_softc *sc = usbnet_softc(un); + struct mii_data *mii = usbnet_mii(un); - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - aue_lock_mii(sc); + usbnet_lock_mii(un); AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { @@ -544,25 +520,31 @@ aue_miibus_statchg(struct ifnet *ifp) AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); - aue_unlock_mii(sc); + + usbnet_set_link(un, false); + if (mii->mii_media_status & IFM_ACTIVE && + IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { + usbnet_set_link(un, true); + } /* * Set the LED modes on the LinkSys adapter. * This turns on the 'dual link LED' bin in the auxmode * register of the Broadcom PHY. */ - if (!sc->aue_dying && (sc->aue_flags & LSYS)) { + if (!usbnet_isdying(un) && (un->un_flags & LSYS)) { uint16_t auxmode; - aue_miibus_readreg(sc->aue_dev, 0, 0x1b, &auxmode); - aue_miibus_writereg(sc->aue_dev, 0, 0x1b, auxmode | 0x04); + aue_mii_read_reg(un, 0, 0x1b, &auxmode); + aue_mii_write_reg(un, 0, 0x1b, auxmode | 0x04); } - DPRINTFN(5,("%s: %s: exit\n", device_xname(sc->aue_dev), __func__)); + usbnet_unlock_mii(un); + DPRINTFN(5,("%s: %s: exit\n", device_xname(un->un_dev), __func__)); } #define AUE_POLY 0xEDB88320 #define AUE_BITS 6 -Static uint32_t +static uint32_t aue_crc(void *addrv) { uint32_t idx, bit, data, crc; @@ -579,19 +561,20 @@ aue_crc(void *addrv) return crc & ((1 << AUE_BITS) - 1); } -Static void -aue_setmulti(struct aue_softc *sc) +static void +aue_setiff_locked(struct usbnet *un) { - struct ethercom *ec = &sc->aue_ec; - struct ifnet *ifp; + struct aue_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, i; uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - ifp = GET_IFP(sc); + usbnet_isowned_mii(un); if (ifp->if_flags & IFF_PROMISC) { allmulti: @@ -625,26 +608,35 @@ allmulti: ifp->if_flags &= ~IFF_ALLMULTI; } -Static void +static void +aue_setiff(struct usbnet *un) +{ + usbnet_lock_mii(un); + aue_setiff_locked(un); + usbnet_unlock_mii(un); +} + +static void aue_reset_pegasus_II(struct aue_softc *sc) { /* Magic constants taken from Linux driver. */ aue_csr_write_1(sc, AUE_REG_1D, 0); aue_csr_write_1(sc, AUE_REG_7B, 2); #if 0 - if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode) + if ((un->un_flags & HAS_HOME_PNA) && mii_mode) aue_csr_write_1(sc, AUE_REG_81, 6); else #endif aue_csr_write_1(sc, AUE_REG_81, 2); } -Static void +static void aue_reset(struct aue_softc *sc) { + struct usbnet * const un = &sc->aue_un; int i; - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(2,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC); @@ -654,11 +646,11 @@ aue_reset(struct aue_softc *sc) } if (i == AUE_TIMEOUT) - printf("%s: reset failed\n", device_xname(sc->aue_dev)); + printf("%s: reset failed\n", device_xname(un->un_dev)); #if 0 /* XXX what is mii_mode supposed to be */ - if (sc->aue_mii_mode && (sc->aue_flags & PNA)) + if (sc->sc_mii_mode && (un->un_flags & PNA)) aue_csr_write_1(sc, AUE_GPIO1, 0x34); else aue_csr_write_1(sc, AUE_GPIO1, 0x26); @@ -673,7 +665,7 @@ aue_reset(struct aue_softc *sc) * Note: We force all of the GPIO pins low first, *then* * enable the ones we want. */ - if (sc->aue_flags & LSYS) { + if (un->un_flags & LSYS) { /* Grrr. LinkSys has to be different from everyone else. */ aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0 | AUE_GPIO_SEL1); @@ -684,11 +676,11 @@ aue_reset(struct aue_softc *sc) aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1); - if (sc->aue_flags & PII) + if (un->un_flags & PII) aue_reset_pegasus_II(sc); /* Wait a little while for the chip to get its brains in order. */ - delay(10000); /* XXX */ + usbd_delay_ms(un->un_udev, 10000); /* XXX */ } /* @@ -730,31 +722,42 @@ aue_match(device_t parent, cfdata_t matc void aue_attach(device_t parent, device_t self, void *aux) { - struct aue_softc *sc = device_private(self); + struct aue_softc * const sc = device_private(self); + struct usbnet * const un = &sc->aue_un; struct usb_attach_arg *uaa = aux; char *devinfop; - int s; - u_char eaddr[ETHER_ADDR_LEN]; - struct ifnet *ifp; - struct mii_data *mii; 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,(" : aue_attach: sc=%p", sc)); + KASSERT((void *)sc == un); - sc->aue_dev = self; + DPRINTFN(5,(" : aue_attach: sc=%p", sc)); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(uaa->uaa_device, 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 = &aue_ops; + un->un_intr = &sc->aue_intr; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = AUE_RX_LIST_CNT; + un->un_tx_list_cnt = AUE_RX_LIST_CNT; + un->un_rx_bufsz = AUE_BUFSZ; + un->un_tx_bufsz = AUE_BUFSZ; + + sc->aue_intr.uni_buf = &sc->aue_ibuf; + sc->aue_intr.uni_bufsz = sizeof(sc->aue_ibuf); + sc->aue_intr.uni_interval = AUE_INTR_INTERVAL; + err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -762,43 +765,19 @@ aue_attach(device_t parent, device_t sel return; } - usb_init_task(&sc->aue_tick_task, aue_tick_task, sc, 0); - usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc, 0); - mutex_init(&sc->aue_mii_lock, MUTEX_DEFAULT, IPL_NONE); - - err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface); + err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->aue_closing = 0; - - mutex_init(&sc->aue_mcmtx, MUTEX_DRIVER, IPL_NET); - cv_init(&sc->aue_domc, "auemc"); - cv_init(&sc->aue_closemc, "auemccl"); - - err = kthread_create(PRI_NONE, 0, NULL, - aue_multithread, sc, &sc->aue_thread, - "%s-mc", device_xname(sc->aue_dev)); - - if (err) { - aprint_error_dev(self, - "creating multicast configuration thread\n"); - return; - } - sc->aue_flags = aue_lookup(uaa->uaa_vendor, - uaa->uaa_product)->aue_flags; - sc->aue_udev = dev; - sc->aue_iface = iface; - sc->aue_product = uaa->uaa_product; - sc->aue_vendor = uaa->uaa_vendor; + un->un_flags = aue_lookup(uaa->uaa_vendor, uaa->uaa_product)->aue_flags; - 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 endpoint descriptor %d\n", i); @@ -806,24 +785,27 @@ aue_attach(device_t parent, device_t sel } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->aue_ed[AUE_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->aue_ed[AUE_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->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } } - if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 || - sc->aue_ed[AUE_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; } + /* First level attach. */ + usbnet_attach(un, "auedet"); - s = splnet(); + usbnet_lock_mii(un); /* Reset the adapter. */ aue_reset(sc); @@ -831,277 +813,20 @@ aue_attach(device_t parent, device_t sel /* * Get station address from the EEPROM. */ - aue_read_mac(sc, eaddr); - - /* - * A Pegasus chip was detected. Inform the world. - */ - ifp = GET_IFP(sc); - aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); - - /* Initialize interface info.*/ - ifp->if_softc = sc; - ifp->if_mtu = ETHERMTU; - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_ioctl = aue_ioctl; - ifp->if_start = aue_start; - ifp->if_watchdog = aue_watchdog; - strlcpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ); - - IFQ_SET_READY(&ifp->if_snd); - - /* Initialize MII/media info. */ - mii = &sc->aue_mii; - mii->mii_ifp = ifp; - mii->mii_readreg = aue_miibus_readreg; - mii->mii_writereg = aue_miibus_writereg; - mii->mii_statchg = aue_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - sc->aue_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus); - 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->aue_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&(sc->aue_stat_ch), 0); - - sc->aue_attached = 1; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev); - - if (!pmf_device_register(self, NULL, NULL)) - aprint_error_dev(self, "couldn't establish power handler\n"); - - return; -} - -int -aue_detach(device_t self, int flags) -{ - struct aue_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - int s; - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - if (!sc->aue_attached) { - /* Detached before attached finished, so just bail out. */ - 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 aue_send? Don't we need to wait for if_detach or - * something? Should we set sc->aue_dying here? Is device - * deactivation guaranteed to have already happened? - */ - callout_halt(&sc->aue_stat_ch, NULL); - usb_rem_task_wait(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER, - NULL); - usb_rem_task_wait(sc->aue_udev, &sc->aue_stop_task, USB_TASKQ_DRIVER, - NULL); - - sc->aue_closing = 1; - cv_signal(&sc->aue_domc); - - mutex_enter(&sc->aue_mcmtx); - cv_wait(&sc->aue_closemc,&sc->aue_mcmtx); - mutex_exit(&sc->aue_mcmtx); - - mutex_destroy(&sc->aue_mcmtx); - cv_destroy(&sc->aue_domc); - cv_destroy(&sc->aue_closemc); - - s = splusb(); - - if (ifp->if_flags & IFF_RUNNING) - aue_stop(sc); - - rnd_detach_source(&sc->rnd_source); - mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY); - ether_ifdetach(ifp); - - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->aue_ep[AUE_ENDPT_TX] != NULL || - sc->aue_ep[AUE_ENDPT_RX] != NULL || - sc->aue_ep[AUE_ENDPT_INTR] != NULL) - aprint_error_dev(self, "detach has active endpoints\n"); -#endif - - sc->aue_attached = 0; - - if (--sc->aue_refcnt >= 0) { - /* Wait for processes to go away. */ - usb_detach_waitold(sc->aue_dev); - } - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev); - - mutex_destroy(&sc->aue_mii_lock); -#if 0 - mutex_destroy(&sc->wkmtx); -#endif - return 0; -} - -int -aue_activate(device_t self, enum devact act) -{ - struct aue_softc *sc = device_private(self); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(&sc->aue_ec.ec_if); - sc->aue_dying = 1; - return 0; - default: - return EOPNOTSUPP; - } -} - -/* - * Initialize an RX descriptor and attach an MBUF cluster. - */ -Static int -aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m) -{ - struct mbuf *m_new = NULL; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - aprint_error_dev(sc->aue_dev, "no memory for rx list " - "-- packet dropped!\n"); - return ENOBUFS; - } - - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - aprint_error_dev(sc->aue_dev, "no memory for rx " - "list -- packet dropped!\n"); - 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; - } + aue_read_mac(un, un->un_eaddr); - m_adj(m_new, ETHER_ALIGN); - c->aue_mbuf = m_new; + usbnet_unlock_mii(un); - return 0; + usbnet_attach_ifp(un, true, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, 0); } -Static int -aue_rx_list_init(struct aue_softc *sc) +static void +aue_intr(struct usbnet *un, usbd_status status) { - struct aue_cdata *cd; - struct aue_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - cd = &sc->aue_cdata; - for (i = 0; i < AUE_RX_LIST_CNT; i++) { - c = &cd->aue_rx_chain[i]; - c->aue_sc = sc; - if (aue_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->aue_xfer == NULL) { - int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_RX], - AUE_BUFSZ, 0, 0, &c->aue_xfer); - if (err) { - return err; - } - c->aue_buf = usbd_get_buffer(c->aue_xfer); - } - } - - return 0; -} - -Static int -aue_tx_list_init(struct aue_softc *sc) -{ - struct aue_cdata *cd; - struct aue_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - cd = &sc->aue_cdata; - for (i = 0; i < AUE_TX_LIST_CNT; i++) { - c = &cd->aue_tx_chain[i]; - c->aue_sc = sc; - c->aue_mbuf = NULL; - if (c->aue_xfer == NULL) { - int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_TX], - AUE_BUFSZ, USBD_FORCE_SHORT_XFER, 0, &c->aue_xfer); - if (err) { - return err; - } - c->aue_buf = usbd_get_buffer(c->aue_xfer); - } - } - - return 0; -} - -Static void -aue_intr(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct aue_softc *sc = priv; - struct ifnet *ifp = GET_IFP(sc); - struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf; - - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (sc->aue_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - return; - } - sc->aue_intr_errs++; - if (usbd_ratecheck(&sc->aue_rx_notice)) { - aprint_debug_dev(sc->aue_dev, - "%u usb errors on intr: %s\n", sc->aue_intr_errs, - usbd_errstr(status)); - sc->aue_intr_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]); - return; - } + struct ifnet *ifp = usbnet_ifp(un); + struct aue_softc *sc = usbnet_softc(un); + struct aue_intrpkt *p = &sc->aue_ibuf; if (p->aue_txstat0) ifp->if_oerrors++; @@ -1110,216 +835,54 @@ aue_intr(struct usbd_xfer *xfer, void *p ifp->if_collisions++; } -/* - * A frame has been uploaded: pass the resulting mbuf chain up to - * the higher level protocols. - */ -Static void -aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +static void +aue_rx_loop(struct usbnet *un, struct usbd_xfer *xfer, + struct usbnet_chain *c, uint32_t total_len) { - struct aue_chain *c = priv; - struct aue_softc *sc = c->aue_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - uint32_t total_len; + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; struct aue_rxpkt r; - int s; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - if (sc->aue_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->aue_rx_errs++; - if (usbd_ratecheck(&sc->aue_rx_notice)) { - aprint_error_dev(sc->aue_dev, - "%u usb errors on rx: %s\n", sc->aue_rx_errs, - usbd_errstr(status)); - sc->aue_rx_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - - memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len); + usbnet_isowned_rx(un); if (total_len <= 4 + ETHER_CRC_LEN) { ifp->if_ierrors++; - goto done; + return; } - memcpy(&r, c->aue_buf + total_len - 4, sizeof(r)); + memcpy(&r, buf + total_len - 4, sizeof(r)); /* Turn off all the non-error bits in the rx status word. */ r.aue_rxstat &= AUE_RXSTAT_MASK; if (r.aue_rxstat) { ifp->if_ierrors++; - goto done; + return; } /* No errors; receive the packet. */ - m = c->aue_mbuf; total_len -= ETHER_CRC_LEN + 4; - m->m_pkthdr.len = m->m_len = total_len; - - m_set_rcvif(m, ifp); - - s = splnet(); - - /* XXX ugly */ - if (aue_newbuf(sc, c, NULL) == ENOBUFS) { - ifp->if_ierrors++; - goto done1; - } - DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->aue_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->aue_buf, AUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof); - usbd_transfer(xfer); - - DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->aue_dev), - __func__)); + usbnet_enqueue(un, buf, total_len, 0, 0, 0); } -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ - -Static void -aue_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct aue_chain *c = priv; - struct aue_softc *sc = c->aue_sc; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (sc->aue_dying) - return; - - s = splnet(); - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->aue_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++; - aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n", - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]); - splx(s); - return; - } - - ifp->if_opackets++; - - m_freem(c->aue_mbuf); - c->aue_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - aue_start(ifp); - - splx(s); -} - -Static void -aue_tick(void *xsc) -{ - struct aue_softc *sc = xsc; - - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (sc == NULL) - return; - - if (sc->aue_dying) - return; - - /* Perform periodic stuff in process context. */ - usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER); -} - -Static void -aue_tick_task(void *xsc) -{ - struct aue_softc *sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - int s; - - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (sc->aue_dying) - return; - - ifp = GET_IFP(sc); - mii = GET_MII(sc); - if (mii == NULL) - return; - - s = splnet(); - - mii_tick(mii); - if (!sc->aue_link) { - mii_pollstat(mii); /* XXX FreeBSD has removed this call */ - if (mii->mii_media_status & IFM_ACTIVE && - IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { - DPRINTFN(2,("%s: %s: got link\n", - device_xname(sc->aue_dev), __func__)); - sc->aue_link++; - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - aue_start(ifp); - } - } - - callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc)); - - splx(s); -} - -Static int -aue_send(struct aue_softc *sc, struct mbuf *m, int idx) +static unsigned +aue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { + uint8_t *buf = c->unc_buf; int total_len; - struct aue_chain *c; - usbd_status err; - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); + usbnet_isowned_tx(un); + + if (m->m_pkthdr.len > un->un_tx_bufsz - 2) + return 0; - c = &sc->aue_cdata.aue_tx_chain[idx]; + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); /* * 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->aue_buf + 2); - c->aue_mbuf = m; + m_copydata(m, 0, m->m_pkthdr.len, buf + 2); /* * The ADMtek documentation says that the packet length is @@ -1327,96 +890,32 @@ aue_send(struct aue_softc *sc, struct mb * transfer, however it actually seems to ignore this info * and base the frame size on the bulk transfer length. */ - c->aue_buf[0] = (uint8_t)m->m_pkthdr.len; - c->aue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); + buf[0] = (uint8_t)m->m_pkthdr.len; + buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); total_len = m->m_pkthdr.len + 2; - usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, total_len, - USBD_FORCE_SHORT_XFER, AUE_TX_TIMEOUT, aue_txeof); - - /* Transmit */ - err = usbd_transfer(c->aue_xfer); - if (err != USBD_IN_PROGRESS) { - aprint_error_dev(sc->aue_dev, "aue_send error=%s\n", - usbd_errstr(err)); - /* Stop the interface from process context. */ - usb_add_task(sc->aue_udev, &sc->aue_stop_task, - USB_TASKQ_DRIVER); - return EIO; - } - DPRINTFN(5,("%s: %s: send %d bytes\n", device_xname(sc->aue_dev), - __func__, total_len)); - - sc->aue_cdata.aue_tx_cnt++; - - return 0; + return total_len; } -Static void -aue_start(struct ifnet *ifp) +static int +aue_init(struct ifnet *ifp) { - struct aue_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - DPRINTFN(5,("%s: %s: enter, link=%d\n", device_xname(sc->aue_dev), - __func__, sc->aue_link)); - - if (sc->aue_dying) - return; - - if (!sc->aue_link) - return; - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (aue_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 -aue_init(void *xsc) -{ - struct aue_softc *sc = xsc; - struct ifnet *ifp = GET_IFP(sc); - struct mii_data *mii = GET_MII(sc); - int i, s; + struct usbnet * const un = ifp->if_softc; + struct aue_softc *sc = usbnet_softc(un); + int i; const u_char *eaddr; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->aue_dying) - return; + if (usbnet_isdying(un)) + return EIO; - if (ifp->if_flags & IFF_RUNNING) - return; + /* Cancel pending I/O */ + usbnet_stop(un, ifp, 1); - s = splnet(); + usbnet_lock_mii(un); - /* - * Cancel pending I/O and free all RX/TX buffers. - */ + /* Reset the interface. */ aue_reset(sc); eaddr = CLLADDR(ifp->if_sadl); @@ -1429,322 +928,41 @@ aue_init(void *xsc) else AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); - if (sc->aue_ep[AUE_ENDPT_RX] == NULL) { - if (aue_openpipes(sc)) { - splx(s); - return; - } - } - /* Init TX ring. */ - if (aue_tx_list_init(sc)) { - aprint_error_dev(sc->aue_dev, "tx list init failed\n"); - splx(s); - return; - } - - /* Init RX ring. */ - if (aue_rx_list_init(sc)) { - aprint_error_dev(sc->aue_dev, "rx list init failed\n"); - splx(s); - return; - } - - /* Start up the receive pipe. */ - for (i = 0; i < AUE_RX_LIST_CNT; i++) { - struct aue_chain *c = &sc->aue_cdata.aue_rx_chain[i]; - - usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, AUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof); - (void)usbd_transfer(c->aue_xfer); /* XXX */ - DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->aue_dev), - __func__)); - - } - /* Load the multicast filter. */ - aue_setmulti(sc); + aue_setiff_locked(un); - /* Enable RX and TX */ - aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB); - AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB); - AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR); + usbnet_unlock_mii(un); - mii_mediachg(mii); - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); - - callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc)); + return usbnet_init_rx_tx(un); } -Static int -aue_openpipes(struct aue_softc *sc) +static int +aue_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - usbd_status err; + struct usbnet * const un = ifp->if_softc; - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX], - USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]); - if (err) { - aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX], - USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR], - USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc, - &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr, - AUE_INTR_INTERVAL); - if (err) { - aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } - - return 0; -} - -/* - * Set media options. - */ -Static int -aue_ifmedia_upd(struct ifnet *ifp) -{ - struct aue_softc *sc = ifp->if_softc; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - if (sc->aue_dying) - return 0; - - return ether_mediachange(ifp); -} - -Static int -aue_ioctl(struct ifnet *ifp, u_long command, void *data) -{ - struct aue_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; - - if (sc->aue_dying) - return EIO; - - s = splnet(); - - switch (command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - aue_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->aue_if_flags & IFF_PROMISC)) { - AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); - } else if (ifp->if_flags & IFF_RUNNING && - !(ifp->if_flags & IFF_PROMISC) && - sc->aue_if_flags & IFF_PROMISC) { - AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); - } else if (!(ifp->if_flags & IFF_RUNNING)) - aue_init(sc); - } else { - if (ifp->if_flags & IFF_RUNNING) - aue_stop(sc); - } - sc->aue_if_flags = ifp->if_flags; - error = 0; + switch (cmd) { + case SIOCADDMULTI: + case SIOCDELMULTI: + aue_init(ifp); + aue_setiff(un); break; default: - if ((error = ether_ioctl(ifp, command, data)) == ENETRESET) { - if (ifp->if_flags & IFF_RUNNING) { - cv_signal(&sc->aue_domc); - } - error = 0; - } break; } - splx(s); - - return error; -} - -Static void -aue_watchdog(struct ifnet *ifp) -{ - struct aue_softc *sc = ifp->if_softc; - struct aue_chain *c; - usbd_status stat; - int s; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - ifp->if_oerrors++; - aprint_error_dev(sc->aue_dev, "watchdog timeout\n"); - - s = splusb(); - c = &sc->aue_cdata.aue_tx_chain[0]; - usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat); - aue_txeof(c->aue_xfer, c, stat); - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - aue_start(ifp); - splx(s); + return 0; } -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -Static void -aue_stop(struct aue_softc *sc) +static void +aue_stop_cb(struct ifnet *ifp, int disable) { - usbd_status err; - struct ifnet *ifp; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - ifp = GET_IFP(sc); - ifp->if_timer = 0; + struct usbnet * const un = ifp->if_softc; + struct aue_softc * const sc = usbnet_softc(un); + usbnet_lock_mii(un); aue_csr_write_1(sc, AUE_CTL0, 0); aue_csr_write_1(sc, AUE_CTL1, 0); aue_reset(sc); - callout_stop(&sc->aue_stat_ch); - - /* Stop transfers. */ - if (sc->aue_ep[AUE_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]); - if (err) { - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - sc->aue_ep[AUE_ENDPT_RX] = NULL; - } - - if (sc->aue_ep[AUE_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]); - if (err) { - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - } - - if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < AUE_RX_LIST_CNT; i++) { - if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) { - m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf); - sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL; - } - if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) { - usbd_destroy_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer); - sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < AUE_TX_LIST_CNT; i++) { - if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) { - m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf); - sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL; - } - if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) { - usbd_destroy_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer); - sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL; - } - } - - /* Close pipes */ - if (sc->aue_ep[AUE_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - sc->aue_ep[AUE_ENDPT_TX] = NULL; - } - - if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - sc->aue_ep[AUE_ENDPT_INTR] = NULL; - } - - sc->aue_link = 0; - - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); -} - -Static void -aue_multithread(void *arg) -{ - struct aue_softc *sc; - int s; - - sc = (struct aue_softc *)arg; - - while (1) { - mutex_enter(&sc->aue_mcmtx); - cv_wait(&sc->aue_domc,&sc->aue_mcmtx); - mutex_exit(&sc->aue_mcmtx); - - if (sc->aue_closing) - break; - - s = splnet(); - aue_init(sc); - /* XXX called by aue_init, but rc ifconfig hangs without it: */ - aue_setmulti(sc); - splx(s); - } - - cv_signal(&sc->aue_closemc); - - kthread_exit(0); + usbnet_unlock_mii(un); } Index: if_auereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_auereg.h,v retrieving revision 1.30 diff -p -u -r1.30 if_auereg.h --- if_auereg.h 1 Aug 2019 00:10:22 -0000 1.30 +++ if_auereg.h 12 Aug 2019 17:10:22 -0000 @@ -66,10 +66,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,73 +194,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; -}; - -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: if_cue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_cue.c,v retrieving revision 1.84 diff -p -u -r1.84 if_cue.c --- if_cue.c 28 May 2019 07:41:50 -0000 1.84 +++ if_cue.c 12 Aug 2019 17:10:22 -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. @@ -65,31 +66,15 @@ __KERNEL_RCSID(0, "$NetBSD: if_cue.c,v 1 #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 @@ -101,10 +86,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 +137,35 @@ 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); - -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 *); + usbnet_detach, usbnet_activate); -#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_cb(struct usbnet *, struct usbd_xfer *, + struct usbnet_chain *, uint32_t); +static int cue_ioctl_cb(struct ifnet *, u_long, void *); +static int cue_init(struct ifnet *); +static void cue_stop_cb(struct ifnet *, int); + +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_cb, + .uno_init = cue_init, +}; -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 +174,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 +205,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 +237,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 +273,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 +285,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 +303,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 +328,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 +342,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 +358,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 +412,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 +433,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 +462,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 +488,203 @@ 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)); - - /* 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; -} - -int -cue_detach(device_t self, int flags) -{ - 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); - - 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; + cue_getmac(un); - 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; + usbnet_attach_ifp(un, false, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, 0); } -Static int -cue_tx_list_init(struct cue_softc *sc) +static void +cue_rx_loop_cb(struct usbnet *un, struct usbd_xfer *xfer, + struct usbnet_chain *c, uint32_t total_len) { - 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); - } - } - - return 0; -} - -/* - * 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) -{ - 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), + DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(un->un_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)) { + /* Collect statistics. */ + if (cue_csr_read_2(un, CUE_RX_FRAMEERR)) 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) { + 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); -} - -Static void -cue_tick_task(void *xsc) -{ - 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); + /* No errors; receive the packet. */ + total_len = len; - if (cue_csr_read_2(sc, CUE_RX_FRAMEERR)) - ifp->if_ierrors++; + DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(un->un_dev), + __func__, total_len)); + usbnet_enqueue(un, buf + 2, total_len, 0, 0, 0); } -Static int -cue_send(struct cue_softc *sc, struct mbuf *m, int idx) +static unsigned +cue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { + struct ifnet *ifp = usbnet_ifp(un); int total_len; - struct cue_chain *c; - usbd_status err; - c = &sc->cue_cdata.cue_tx_chain[idx]; + if (m->m_pkthdr.len > un->un_tx_bufsz - 2) + return 0; + + /* Collect statistics. */ + 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); /* * 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)); + device_xname(un->un_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(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); - - 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)); -} - -Static int -cue_open_pipes(struct cue_softc *sc) -{ - 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; - } - - /* 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); - } + cue_csr_write_1(un, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK); - return 0; + return usbnet_init_rx_tx(un); } -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); - - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - arp_ifinit(ifp, ifa); - break; -#endif /* INET */ - } - break; + struct usbnet * const un = ifp->if_softc; - 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; -} - -Static void -cue_watchdog(struct ifnet *ifp) -{ - 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__)); - - if (sc->cue_dying) - return; - - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->cue_dev)); - - 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); + return 0; } -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -Static void -cue_stop(struct cue_softc *sc) +/* Stop and reset the adapter. */ +static void +cue_stop_cb(struct ifnet *ifp, int disable) { - 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; + struct usbnet * const un = ifp->if_softc; - 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; - } + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + cue_csr_write_1(un, CUE_ETHCTL, 0); + cue_reset(un); } Index: if_cuereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_cuereg.h,v retrieving revision 1.22 diff -p -u -r1.22 if_cuereg.h --- if_cuereg.h 21 Jul 2019 10:27:56 -0000 1.22 +++ if_cuereg.h 12 Aug 2019 17:10:22 -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: if_kue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_kue.c,v retrieving revision 1.97 diff -p -u -r1.97 if_kue.c --- if_kue.c 1 Aug 2019 00:10:22 -0000 1.97 +++ if_kue.c 12 Aug 2019 17:10:22 -0000 @@ -80,32 +80,16 @@ __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1 #include #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 @@ -160,37 +144,38 @@ 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_cb(struct usbnet *un, struct usbd_xfer *xfer, + struct usbnet_chain *c, uint32_t total_len); +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_cb, + .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 int kue_init(struct ifnet *); -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 +183,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 +205,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 +230,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,33 +287,34 @@ 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 (ifp->if_flags & IFF_PROMISC) { allmulti: ifp->if_flags |= IFF_ALLMULTI; sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI; 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; } @@ -354,10 +340,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 +352,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 +387,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 = USBD_FORCE_SHORT_XFER; + 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 +424,67 @@ 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[KUE_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[KUE_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; + un->un_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress; } } - if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) { + if (un->un_ed[KUE_ENDPT_RX] == 0 || un->un_ed[KUE_ENDPT_TX] == 0) { aprint_error_dev(self, "missing endpoint\n"); return; } + /* First level attach. */ + 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); + /* XXXMRG what uses this? */ 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, false, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, 0); } 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,99 +492,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; - 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; - 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); } /* @@ -633,252 +500,90 @@ 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_cb(struct usbnet *un, struct usbd_xfer *xfer, + 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; + int pktlen; + + DPRINTFN(10,("%s: %s: status=%d total_len=%d len=%d\n", + device_xname(un->un_dev), __func__, + status, total_len, le16dec(buf))); if (total_len <= 1) - goto done; + return; - pktlen = le16dec(c->kue_buf); + pktlen = le16dec(buf); if (pktlen > total_len - 2) - pktlen = total_len - 2; + 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; - } + return; } - 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), + DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(un->un_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__)); + usbnet_enqueue(un, buf + ETHER_ALIGN, pktlen, 0, 0, 0); } -/* - * 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) +static unsigned +kue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - 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); + int total_len, pkt_len; - splx(s); -} + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); -static int -kue_send(struct kue_softc *sc, struct mbuf *m, int idx) -{ - int total_len; - struct kue_chain *c; - usbd_status err; + pkt_len = ETHER_ALIGN + m->m_pkthdr.len; + total_len = roundup2(pkt_len, 64); - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - c = &sc->kue_cdata.kue_tx_chain[idx]; + if (total_len > un->un_tx_bufsz) + return 0; /* Frame length is specified in the first 2 bytes of the buffer. */ - le16enc(c->kue_buf, (uint16_t)m->m_pkthdr.len); + le16enc(c->unc_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. + * Copy the mbuf data into a contiguous buffer after the frame length, + * possibly zeroing the rest of the buffer. */ - 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); + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + ETHER_ALIGN); + if (total_len - pkt_len > 0) + memset(c->unc_buf + pkt_len, 0, total_len - pkt_len); - 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; + return total_len; } -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; - } - - 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); - - ifp->if_flags |= IFF_OACTIVE; - - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 6; -} - -static void -kue_init(void *xsc) +static int +kue_init(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 -1; - 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; + else + sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC; - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); + kue_setword(un, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); /* I'm not sure how to tune these. */ #if 0 @@ -886,266 +591,27 @@ kue_init(void *xsc) * 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); -} - -static int -kue_open_pipes(struct kue_softc *sc) -{ - 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; - } - - 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; - } - - return 0; + return usbnet_init_rx_tx(un); } 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; - - 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); 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); -} - -/* - * 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; - } - - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + return 0; } Index: if_kuereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_kuereg.h,v retrieving revision 1.23 diff -p -u -r1.23 if_kuereg.h --- if_kuereg.h 1 Aug 2019 00:10:22 -0000 1.23 +++ if_kuereg.h 12 Aug 2019 17:10:22 -0000 @@ -1,4 +1,5 @@ /* $NetBSD: if_kuereg.h,v 1.23 2019/08/01 00:10:22 mrg Exp $ */ + /* * Copyright (c) 1997, 1998, 1999, 2000 * Bill Paul . All rights reserved. @@ -141,6 +142,7 @@ struct kue_type { uint16_t kue_did; }; +#if 0 struct kue_softc; struct kue_chain { @@ -155,28 +157,12 @@ struct kue_cdata { int kue_tx_prod; int kue_tx_cnt; }; +#endif struct kue_softc { - device_t kue_dev; + struct usbnet kue_un; - 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: if_mue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_mue.c,v retrieving revision 1.51 diff -p -u -r1.51 if_mue.c --- if_mue.c 1 Aug 2019 00:10:22 -0000 1.51 +++ if_mue.c 12 Aug 2019 17:10:22 -0000 @@ -28,55 +28,31 @@ __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 +// XXX checkme #include #include /* XXX for struct ip */ #include /* XXX for struct ip6_hdr */ -#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 +62,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,76 +85,58 @@ 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 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 usbd_status mue_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static usbd_status 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 usbd_xfer *, + 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 void mue_rxeof(struct usbd_xfer *, void *, usbd_status); -static void mue_txeof(struct usbd_xfer *, void *, usbd_status); +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 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_SETBIT(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_CLRBIT(un, reg, x) \ + mue_csr_write(un, reg, mue_csr_read(un, reg) & ~(x)) -#define MUE_WAIT_SET(sc, reg, set, fail) \ - mue_wait_for_bits(sc, reg, set, ~0, fail) +#define MUE_WAIT_SET(un, reg, set, fail) \ + mue_wait_for_bits(un, 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)) @@ -183,17 +144,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); @@ -203,9 +164,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; } @@ -213,13 +174,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); @@ -229,9 +190,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; } @@ -239,153 +200,92 @@ 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) +static usbd_status +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; - } - - if (sc->mue_phyno != phy) - return -1; + usbnet_isowned_mii(un); - 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 USBD_IN_USE; } - 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 USBD_TIMEOUT; } - 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 USBD_NORMAL_COMPLETION; } -static int -mue_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +static usbd_status +mue_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct mue_softc *sc = device_private(dev); - int rv = 0; - - if (sc->mue_dying) { - DPRINTF(sc, "dying\n"); - return -1; - } + usbnet_isowned_mii(un); - if (sc->mue_phyno != phy) { - DPRINTF(sc, "sc->mue_phyno (%d) != phy (%d)\n", - sc->mue_phyno, phy); - return -1; + if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { + MUE_PRINTF(un, "not ready\n"); + return USBD_IN_USE; } - 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; - } - - 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; - } -out: - mue_unlock_mii(sc); - return rv; + if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) { + MUE_PRINTF(un, "timed out\n"); + return USBD_TIMEOUT; + } + + return USBD_NORMAL_COMPLETION; } 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__); + if (usbnet_isdying(un)) return; - } - - sc = ifp->if_softc; - mii = GET_MII(sc); - if ((ifp->if_flags & IFF_RUNNING) == 0) { - DPRINTF(sc, "not running\n"); - return; - } - - if (mii == NULL) { - DPRINTF(sc, "mii not ready\n"); - return; - } - - sc->mue_link = 0; + usbnet_set_link(un, false); 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; @@ -393,22 +293,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); } @@ -425,10 +325,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; @@ -441,60 +341,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); + mue_csr_write(un, 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); - - sc->mue_link = 0; - - if (mii->mii_instance) { - struct mii_softc *miisc; - LIST_FOREACH(miisc, &mii->mii_phys, mii_list) - mii_phy_reset(miisc); - } - return ether_mediachange(ifp); + 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; @@ -503,76 +384,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); } @@ -580,15 +461,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) { @@ -598,125 +479,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; @@ -727,12 +608,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 @@ -740,112 +621,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)); } /* @@ -857,13 +737,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; @@ -884,32 +764,32 @@ 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); + 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(un->un_udev, 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); @@ -919,95 +799,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 | @@ -1015,210 +882,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, ether_mediastatus); - 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); + usbnet_attach_ifp(un, true, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, 0); } -static int -mue_detach(device_t self, int flags) +static unsigned +mue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - 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); - } -} - -static int -mue_encap(struct mue_softc *sc, struct mbuf *m, int idx) -{ - 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; @@ -1228,12 +915,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; @@ -1245,9 +930,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; @@ -1259,31 +944,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 */ + memcpy(c->unc_buf, &hdr, sizeof(hdr)); + m_copydata(m, 0, len, c->unc_buf + sizeof(hdr)); - 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; - } - - 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; @@ -1306,10 +977,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; } @@ -1332,10 +999,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; @@ -1344,15 +1011,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); @@ -1364,9 +1031,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; @@ -1402,117 +1069,95 @@ 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 usbd_xfer *xfer, + 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; @@ -1524,34 +1169,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); - ifp->if_ierrors++; - goto done; - } - - m = mue_newbuf(); - if (__predict_false(m == NULL)) { - MUE_PRINTF(sc, "failed to allocate mbuf\n"); + pktlen + sizeof(*hdrp) > total_len)) { + MUE_PRINTF(un, "invalid packet length %d\n", pktlen); 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 { @@ -1580,380 +1214,93 @@ 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) { - 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; - } + mue_setmtu(un); - /* Init TX ring. */ - if (mue_tx_list_init(sc)) { - MUE_PRINTF(sc, "failed to init tx list\n"); - splx(s); - return ENOBUFS; - } - - mue_startup_rx_pipes(sc); - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); - - callout_reset(&sc->mue_stat_ch, hz, mue_tick, sc); - - return 0; + return usbnet_init_rx_tx(un); } 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); - - 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; + struct usbnet * const un = ifp->if_softc; - 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); - - return m; -} +/* XXX module is built but no MODULE() or modcmd */ Index: if_muevar.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_muevar.h,v retrieving revision 1.8 diff -p -u -r1.8 if_muevar.h --- if_muevar.h 26 May 2019 04:52:07 -0000 1.8 +++ if_muevar.h 12 Aug 2019 17:10:22 -0000 @@ -20,6 +20,7 @@ #ifndef _IF_MUEVAR_H_ #define _IF_MUEVAR_H_ +#if 0 #include struct mue_chain { @@ -27,19 +28,22 @@ struct mue_chain { struct usbd_xfer *mue_xfer; char *mue_buf; }; +#endif -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 +#if 0 +struct mue_cdata { + struct mue_chain mue_tx_chain[MUE_TX_LIST_CNT]; struct mue_chain mue_rx_chain[MUE_RX_LIST_CNT]; int mue_tx_prod; int mue_tx_cnt; }; +#endif struct mue_rxbuf_hdr { uint32_t rx_cmd_a; @@ -74,55 +78,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: if_upl.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_upl.c,v retrieving revision 1.64 diff -p -u -r1.64 if_upl.c --- if_upl.c 21 Jul 2019 10:27:56 -0000 1.64 +++ if_upl.c 12 Aug 2019 17:10:22 -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. @@ -43,21 +44,11 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #include #include -#include -#include -#include #include -#include -#include -#include +#include -#include #include -#include -#include - -#include #ifdef INET #include @@ -65,12 +56,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 +68,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,61 +77,13 @@ __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; - + struct usbnet sc_un; + struct usbnet_intr sc_intr; 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 @@ -163,7 +98,7 @@ 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 }, { 0, 0 } @@ -171,29 +106,29 @@ Static struct upl_type sc_devs[] = { 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); + 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 *); +static void upl_intr_cb(struct usbnet *, usbd_status); +static void upl_rx_loop(struct usbnet *, struct usbd_xfer *, + 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, + .uno_intr = upl_intr_cb, +}; -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 +137,51 @@ 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 usbnet * const un = &sc->sc_un; 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)); + KASSERT((void *)sc == un); - sc->sc_dev = self; + DPRINTFN(5,(" : upl_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); + sc->sc_intr.uni_buf = &sc->sc_ibuf; + sc->sc_intr.uni_bufsz = sizeof(sc->sc_ibuf); + sc->sc_intr.uni_interval = UPL_INTR_INTERVAL; + + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &upl_ops; + un->un_intr = &sc->sc_intr; + 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,17 +189,13 @@ 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); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->sc_iface = iface; + un->un_iface = iface; id = usbd_get_interface_descriptor(iface); /* Find endpoints. */ @@ -267,34 +207,25 @@ upl_attach(device_t parent, device_t sel } 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; + 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->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = 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(); - /* 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 +233,96 @@ 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; - } + usbnet_attach(un, "upldet"); + usbnet_attach_ifp(un, false, IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX, + 0, 0); } -/* - * Initialize an RX descriptor and attach an MBUF cluster. - */ -Static int -upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m) +static void +upl_rx_loop(struct usbnet * un, struct usbd_xfer *xfer, + struct usbnet_chain *c, uint32_t total_len) { - 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; -} - -/* - * 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) -{ - 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; - } + device_xname(un->un_dev), __func__, status, total_len)); - 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 + usbnet_input(un, c->unc_buf, total_len); } -/* - * 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) +static unsigned +upl_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - struct upl_chain *c = priv; - struct upl_softc *sc = c->upl_sc; - struct ifnet *ifp = &sc->sc_if; - int s; + int total_len; - 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++; - - m_freem(c->upl_mbuf); - c->upl_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - upl_start(ifp); - - splx(s); -} - -Static int -upl_send(struct upl_softc *sc, struct mbuf *m, int idx) -{ - int total_len; - struct upl_chain *c; - usbd_status err; - - 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 (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; + 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_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; + return total_len; } -Static void -upl_init(void *xsc) +static int +upl_init(struct ifnet *ifp) { - struct upl_softc *sc = xsc; - struct ifnet *ifp = &sc->sc_if; - int s; - - if (sc->sc_dying) - return; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); + struct usbnet * const un = ifp->if_softc; - 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); -} - -Static int -upl_openpipes(struct upl_softc *sc) -{ - 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)); + if (usbnet_isdying(un)) 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; + return usbnet_init_rx_tx(un); } -Static void -upl_intr(struct usbd_xfer *xfer, void *priv, - usbd_status status) +static void +upl_intr_cb(struct usbnet *un, usbd_status status) { - struct upl_softc *sc = priv; - struct ifnet *ifp = &sc->sc_if; + struct upl_softc *sc = usbnet_softc(un); 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; - - 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), + DPRINTFN(10,("%s: %s: stat=0x%02x\n", device_xname(un->un_dev), __func__, stat)); - } -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 (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; + if (cmd == SIOCSIFMTU) { + struct ifreq *ifr = data; - 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; + return EINVAL; } - - 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; - } - - 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 Index: if_url.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_url.c,v retrieving revision 1.67 diff -p -u -r1.67 if_url.c --- if_url.c 1 Aug 2019 00:10:22 -0000 1.67 +++ if_url.c 12 Aug 2019 17:10:22 -0000 @@ -53,78 +53,58 @@ __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1 #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 - /* 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_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_cb(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void url_rx_loop_cb(struct usbnet *, struct usbd_xfer *, + struct usbnet_chain *, uint32_t); +static usbd_status url_int_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static usbd_status 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(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_cb, + .uno_rx_loop = url_rx_loop_cb, + .uno_init = url_init, +}; /* Macros */ #ifdef URL_DEBUG @@ -136,17 +116,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; @@ -180,7 +160,7 @@ 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); + struct usbnet * const un = device_private(self); struct usb_attach_arg *uaa = aux; struct usbd_device *dev = uaa->uaa_device; struct usbd_interface *iface; @@ -188,20 +168,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) { @@ -210,10 +195,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) { @@ -222,17 +203,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] = -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); @@ -240,182 +227,65 @@ 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] == -1 || un->un_ed[USBNET_ENDPT_TX] == -1 || + un->un_ed[USBNET_ENDPT_INTR] == -1) { 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); + url_reset(un); /* Get Ethernet Address */ - err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr, + usbnet_lock(un); + err = url_mem(un, URL_CMD_READMEM, URL_IDR0, (void *)un->un_eaddr, ETHER_ADDR_LEN); + usbnet_unlock(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, ether_mediastatus); - 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, true, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, 0); 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(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; + usbnet_lock_mii_un_locked(un); + if (cmd == URL_CMD_READMEM) req.bmRequestType = UT_READ_VENDOR_DEVICE; else @@ -425,267 +295,210 @@ 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)); } + usbnet_unlock_mii_un_locked(un); + return err; } /* 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__)); - - if (sc->sc_dying) - return 0; + ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - 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(un); /* Enable RX and TX */ - URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE); + URL_SETBIT(un, 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; - } + usbnet_unlock_mii_un_locked(un); - /* 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]; - - 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(un); + 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); + usbnet_unlock(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(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) + usbnet_isowned(un); + + if (usbnet_isdying(un)) return; - ifp = GET_IFP(sc); + usbnet_lock_mii_un_locked(un); if (ifp->if_flags & IFF_PROMISC) { - URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); - return; + URL_SETBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP); + goto out; } 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); - return; + URL_SETBIT2(un, URL_RCR, URL_RCR_AAM); + URL_CLRBIT2(un, URL_RCR, URL_RCR_AAP); + goto out; } /* 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); @@ -709,337 +522,69 @@ 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); - } else { - URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM); - } - url_csr_write_4(sc, URL_MAR0, hashes[0]); - url_csr_write_4(sc, URL_MAR4, hashes[1]); -} - -Static int -url_openpipes(struct url_softc *sc) -{ - 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; + URL_SETBIT2(un, URL_RCR, URL_RCR_AM); } 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; -} - -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; + URL_CLRBIT2(un, URL_RCR, URL_RCR_AM); } + url_csr_write_4(un, URL_MAR0, hashes[0]); + url_csr_write_4(un, URL_MAR4, hashes[1]); - 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; +out: + usbnet_unlock_mii_un_locked(un); } -Static int -url_send(struct url_softc *sc, struct mbuf *m, int idx) +static unsigned +url_tx_prepare_cb(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 (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_cb(struct usbnet *un, struct usbd_xfer *xfer, + 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; - } + usbnet_isowned_rx(un); - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__)); - memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len); - - 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" : "", @@ -1048,328 +593,66 @@ 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; + struct usbnet * const un = ifp->if_softc; - url_reset(sc); + DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - 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; - } - } - - /* 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; - } - - sc->sc_link = 0; - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); -} - -/* Set media options */ -Static int -url_ifmedia_change(struct ifnet *ifp) -{ - struct url_softc *sc = ifp->if_softc; - - DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - if (sc->sc_dying) - return 0; - - sc->sc_link = 0; - - return ether_mediachange(ifp); -} - -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); + url_reset(un); } -Static int -url_int_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) +static usbd_status +url_int_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - struct url_softc *sc; uint16_t data; - int rv = 0; - - if (dev == NULL) - return 0; - - sc = device_private(dev); + usbd_status err = USBD_NORMAL_COMPLETION; 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 USBD_INVAL; } - url_lock_mii(sc); - switch (reg) { case MII_BMCR: /* Control Register */ reg = URL_BMCR; @@ -1393,57 +676,40 @@ 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; + device_xname(un->un_dev), __func__, reg); + err = USBD_INVAL; goto R_DONE; break; } 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 usbd_status +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); + usbd_status err = USBD_NORMAL_COMPLETION; 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 USBD_INVAL; } - url_lock_mii(sc); - switch (reg) { case MII_BMCR: /* Control Register */ reg = URL_BMCR; @@ -1466,24 +732,23 @@ 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; + device_xname(un->un_dev), __func__, reg); + err = USBD_INVAL; goto W_DONE; break; } 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); + url_csr_write_2(un, reg, val); W_DONE: - url_unlock_mii(sc); - return rv; + return err; } -Static void -url_miibus_statchg(struct ifnet *ifp) +static void +url_mii_statchg_cb(struct ifnet *ifp) { #ifdef URL_DEBUG if (ifp == NULL) @@ -1498,83 +763,60 @@ url_miibus_statchg(struct ifnet *ifp) /* * 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)); + device_xname(un->un_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); - - 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 Index: if_urlreg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_urlreg.h,v retrieving revision 1.13 diff -p -u -r1.13 if_urlreg.h --- if_urlreg.h 21 Jul 2019 10:27:56 -0000 1.13 +++ if_urlreg.h 12 Aug 2019 17:10:22 -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: usbnet.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usbnet.c,v retrieving revision 1.13 diff -p -u -r1.13 usbnet.c --- usbnet.c 11 Aug 2019 23:55:43 -0000 1.13 +++ usbnet.c 12 Aug 2019 17:10:22 -0000 @@ -1137,6 +1137,12 @@ usbnet_set_link(struct usbnet *un, bool 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) { Index: usbnet.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/usbnet.h,v retrieving revision 1.10 diff -p -u -r1.10 usbnet.h --- usbnet.h 11 Aug 2019 01:29:45 -0000 1.10 +++ usbnet.h 12 Aug 2019 17:10:22 -0000 @@ -232,6 +232,7 @@ struct usbnet { /* 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 *);