Home | History | Annotate | Line # | Download | only in usb
if_smsc.c revision 1.33.2.3
      1 /*	$NetBSD: if_smsc.c,v 1.33.2.3 2018/09/30 01:45:51 pgoyette Exp $	*/
      2 
      3 /*	$OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $	*/
      4 /*	$FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
      5 /*-
      6  * Copyright (c) 2012
      7  *	Ben Gray <bgray (at) freebsd.org>.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * SMSC LAN9xxx devices (http://www.smsc.com/)
     33  *
     34  * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
     35  * support USB 2.0 and 10/100 Mbps Ethernet.
     36  *
     37  * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
     38  * The driver only covers the Ethernet part, the standard USB hub driver
     39  * supports the hub part.
     40  *
     41  * This driver is closely modelled on the Linux driver written and copyrighted
     42  * by SMSC.
     43  *
     44  * H/W TCP & UDP Checksum Offloading
     45  * ---------------------------------
     46  * The chip supports both tx and rx offloading of UDP & TCP checksums, this
     47  * feature can be dynamically enabled/disabled.
     48  *
     49  * RX checksuming is performed across bytes after the IPv4 header to the end of
     50  * the Ethernet frame, this means if the frame is padded with non-zero values
     51  * the H/W checksum will be incorrect, however the rx code compensates for this.
     52  *
     53  * TX checksuming is more complicated, the device requires a special header to
     54  * be prefixed onto the start of the frame which indicates the start and end
     55  * positions of the UDP or TCP frame.  This requires the driver to manually
     56  * go through the packet data and decode the headers prior to sending.
     57  * On Linux they generally provide cues to the location of the csum and the
     58  * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
     59  * hence this is not as optimal and therefore h/w TX checksum is currently not
     60  * implemented.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v 1.33.2.3 2018/09/30 01:45:51 pgoyette Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_usb.h"
     68 #include "opt_inet.h"
     69 #endif
     70 
     71 #include <sys/param.h>
     72 #include <sys/bus.h>
     73 #include <sys/device.h>
     74 #include <sys/kernel.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/mutex.h>
     77 #include <sys/proc.h>
     78 #include <sys/rndsource.h>
     79 #include <sys/socket.h>
     80 #include <sys/sockio.h>
     81 #include <sys/systm.h>
     82 
     83 #include <net/if.h>
     84 #include <net/if_dl.h>
     85 #include <net/if_media.h>
     86 #include <net/if_ether.h>
     87 
     88 #include <net/bpf.h>
     89 
     90 #ifdef INET
     91 #include <netinet/in.h>
     92 #include <netinet/if_inarp.h>
     93 #endif
     94 
     95 #include <dev/mii/mii.h>
     96 #include <dev/mii/miivar.h>
     97 
     98 #include <dev/usb/usb.h>
     99 #include <dev/usb/usbdi.h>
    100 #include <dev/usb/usbdi_util.h>
    101 #include <dev/usb/usbdivar.h>
    102 #include <dev/usb/usbdevs.h>
    103 
    104 #include <dev/usb/if_smscreg.h>
    105 #include <dev/usb/if_smscvar.h>
    106 
    107 #include "ioconf.h"
    108 
    109 #ifdef USB_DEBUG
    110 int smsc_debug = 0;
    111 #endif
    112 
    113 #define ETHER_ALIGN 2
    114 /*
    115  * Various supported device vendors/products.
    116  */
    117 static const struct usb_devno smsc_devs[] = {
    118 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN89530 },
    119 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9530 },
    120 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9730 },
    121 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500 },
    122 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A },
    123 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_ALT },
    124 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_HAL },
    125 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
    126 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_ALT },
    127 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_SAL10 },
    128 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505 },
    129 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A },
    130 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_HAL },
    131 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
    132 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505_SAL10 },
    133 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14 },
    134 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_ALT },
    135 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
    136 };
    137 
    138 #ifdef USB_DEBUG
    139 #define smsc_dbg_printf(sc, fmt, args...) \
    140 	do { \
    141 		if (smsc_debug > 0) \
    142 			printf("debug: " fmt, ##args); \
    143 	} while(0)
    144 #else
    145 #define smsc_dbg_printf(sc, fmt, args...)
    146 #endif
    147 
    148 #define smsc_warn_printf(sc, fmt, args...) \
    149 	printf("%s: warning: " fmt, device_xname((sc)->sc_dev), ##args)
    150 
    151 #define smsc_err_printf(sc, fmt, args...) \
    152 	printf("%s: error: " fmt, device_xname((sc)->sc_dev), ##args)
    153 
    154 /* Function declarations */
    155 int		 smsc_chip_init(struct smsc_softc *);
    156 void		 smsc_setmulti(struct smsc_softc *);
    157 int		 smsc_setmacaddress(struct smsc_softc *, const uint8_t *);
    158 
    159 int		 smsc_match(device_t, cfdata_t, void *);
    160 void		 smsc_attach(device_t, device_t, void *);
    161 int		 smsc_detach(device_t, int);
    162 int		 smsc_activate(device_t, enum devact);
    163 
    164 int		 smsc_init(struct ifnet *);
    165 int		 smsc_init_locked(struct ifnet *);
    166 void		 smsc_start(struct ifnet *);
    167 void		 smsc_start_locked(struct ifnet *);
    168 int		 smsc_ioctl(struct ifnet *, u_long, void *);
    169 void		 smsc_stop(struct ifnet *, int);
    170 void		 smsc_stop_locked(struct ifnet *, int);
    171 
    172 void		 smsc_reset(struct smsc_softc *);
    173 struct mbuf	*smsc_newbuf(void);
    174 
    175 void		 smsc_tick(void *);
    176 void		 smsc_tick_task(void *);
    177 void		 smsc_miibus_statchg(struct ifnet *);
    178 void		 smsc_miibus_statchg_locked(struct ifnet *);
    179 int		 smsc_miibus_readreg(device_t, int, int);
    180 void		 smsc_miibus_writereg(device_t, int, int, int);
    181 int		 smsc_ifmedia_upd(struct ifnet *);
    182 void		 smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    183 void		 smsc_lock_mii(struct smsc_softc *);
    184 void		 smsc_unlock_mii(struct smsc_softc *);
    185 
    186 int		 smsc_tx_list_init(struct smsc_softc *);
    187 void		 smsc_tx_list_free(struct smsc_softc *);
    188 int		 smsc_rx_list_init(struct smsc_softc *);
    189 void		 smsc_rx_list_free(struct smsc_softc *);
    190 int		 smsc_encap(struct smsc_softc *, struct mbuf *, int);
    191 void		 smsc_rxeof(struct usbd_xfer *, void *, usbd_status);
    192 void		 smsc_txeof(struct usbd_xfer *, void *, usbd_status);
    193 
    194 int		 smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
    195 int		 smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
    196 int		 smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
    197 int		 smsc_sethwcsum(struct smsc_softc *);
    198 
    199 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach,
    200     smsc_detach, smsc_activate);
    201 
    202 int
    203 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
    204 {
    205 	usb_device_request_t req;
    206 	uint32_t buf;
    207 	usbd_status err;
    208 
    209 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    210 	req.bRequest = SMSC_UR_READ_REG;
    211 	USETW(req.wValue, 0);
    212 	USETW(req.wIndex, off);
    213 	USETW(req.wLength, 4);
    214 
    215 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    216 	if (err != 0)
    217 		smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
    218 
    219 	*data = le32toh(buf);
    220 
    221 	return err;
    222 }
    223 
    224 int
    225 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
    226 {
    227 	usb_device_request_t req;
    228 	uint32_t buf;
    229 	usbd_status err;
    230 
    231 	buf = htole32(data);
    232 
    233 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    234 	req.bRequest = SMSC_UR_WRITE_REG;
    235 	USETW(req.wValue, 0);
    236 	USETW(req.wIndex, off);
    237 	USETW(req.wLength, 4);
    238 
    239 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    240 	if (err != 0)
    241 		smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
    242 
    243 	return err;
    244 }
    245 
    246 int
    247 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
    248 {
    249 	uint32_t val;
    250 	int err, i;
    251 
    252 	for (i = 0; i < 100; i++) {
    253 		if ((err = smsc_read_reg(sc, reg, &val)) != 0)
    254 			return err;
    255 		if (!(val & bits))
    256 			return 0;
    257 		DELAY(5);
    258 	}
    259 
    260 	return 1;
    261 }
    262 
    263 int
    264 smsc_miibus_readreg(device_t dev, int phy, int reg)
    265 {
    266 	struct smsc_softc * const sc = device_private(dev);
    267 	uint32_t addr;
    268 	uint32_t val = 0;
    269 
    270 	smsc_lock_mii(sc);
    271 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    272 		smsc_warn_printf(sc, "MII is busy\n");
    273 		goto done;
    274 	}
    275 
    276 	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
    277 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
    278 
    279 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
    280 		smsc_warn_printf(sc, "MII read timeout\n");
    281 
    282 	smsc_read_reg(sc, SMSC_MII_DATA, &val);
    283 
    284 done:
    285 	smsc_unlock_mii(sc);
    286 
    287 	return val & 0xffff;
    288 }
    289 
    290 void
    291 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
    292 {
    293 	struct smsc_softc * const sc = device_private(dev);
    294 	uint32_t addr;
    295 
    296 	if (sc->sc_phyno != phy)
    297 		return;
    298 
    299 	smsc_lock_mii(sc);
    300 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    301 		smsc_warn_printf(sc, "MII is busy\n");
    302 		smsc_unlock_mii(sc);
    303 		return;
    304 	}
    305 
    306 	smsc_write_reg(sc, SMSC_MII_DATA, val);
    307 
    308 	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
    309 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
    310 	smsc_unlock_mii(sc);
    311 
    312 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
    313 		smsc_warn_printf(sc, "MII write timeout\n");
    314 }
    315 
    316 void
    317 smsc_miibus_statchg(struct ifnet *ifp)
    318 {
    319 	if (ifp == NULL)
    320 		return;
    321 
    322 	struct smsc_softc * const sc = ifp->if_softc;
    323 
    324 	mutex_enter(&sc->sc_lock);
    325 	if (sc->sc_dying) {
    326 		mutex_exit(&sc->sc_lock);
    327 		return;
    328 	}
    329 	smsc_miibus_statchg_locked(ifp);
    330 
    331 	mutex_exit(&sc->sc_lock);
    332 }
    333 
    334 
    335 void
    336 smsc_miibus_statchg_locked(struct ifnet *ifp)
    337 {
    338 	struct smsc_softc * const sc = ifp->if_softc;
    339 	struct mii_data * const mii = &sc->sc_mii;
    340 	int err;
    341 	uint32_t flow;
    342 	uint32_t afc_cfg;
    343 
    344 	KASSERT(mutex_owned(&sc->sc_lock));
    345 
    346 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    347 		smsc_dbg_printf(sc, "%s: not running\n", __func__);
    348 		return;
    349 	}
    350 
    351 	/* Use the MII status to determine link status */
    352 	sc->sc_flags &= ~SMSC_FLAG_LINK;
    353 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    354 	    (IFM_ACTIVE | IFM_AVALID)) {
    355 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    356 			case IFM_10_T:
    357 			case IFM_100_TX:
    358 				sc->sc_flags |= SMSC_FLAG_LINK;
    359 				break;
    360 			case IFM_1000_T:
    361 				/* Gigabit ethernet not supported by chipset */
    362 				break;
    363 			default:
    364 				break;
    365 		}
    366 	}
    367 
    368 	/* Lost link, do nothing. */
    369 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
    370 		smsc_dbg_printf(sc, "link flag not set\n");
    371 		return;
    372 	}
    373 
    374 	err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
    375 	if (err) {
    376 		smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
    377 		    "error %d\n", err);
    378 		return;
    379 	}
    380 
    381 	/* Enable/disable full duplex operation and TX/RX pause */
    382 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
    383 		smsc_dbg_printf(sc, "full duplex operation\n");
    384 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
    385 		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
    386 
    387 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
    388 			flow = 0xffff0002;
    389 		else
    390 			flow = 0;
    391 
    392 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
    393 			afc_cfg |= 0xf;
    394 		else
    395 			afc_cfg &= ~0xf;
    396 	} else {
    397 		smsc_dbg_printf(sc, "half duplex operation\n");
    398 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
    399 		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
    400 
    401 		flow = 0;
    402 		afc_cfg |= 0xf;
    403 	}
    404 
    405 	err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    406 	err += smsc_write_reg(sc, SMSC_FLOW, flow);
    407 	err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
    408 	if (err)
    409 		smsc_warn_printf(sc, "media change failed, error %d\n", err);
    410 }
    411 
    412 int
    413 smsc_ifmedia_upd(struct ifnet *ifp)
    414 {
    415 	struct smsc_softc * const sc = ifp->if_softc;
    416 	struct mii_data * const mii = &sc->sc_mii;
    417 	int err;
    418 
    419 	if (mii->mii_instance) {
    420 		struct mii_softc *miisc;
    421 
    422 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    423 			mii_phy_reset(miisc);
    424 	}
    425 	err = mii_mediachg(mii);
    426 	return err;
    427 }
    428 
    429 void
    430 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    431 {
    432 	struct smsc_softc * const sc = ifp->if_softc;
    433 	struct mii_data * const mii = &sc->sc_mii;
    434 
    435 	/* SMSC_LOCK */
    436 
    437 	mii_pollstat(mii);
    438 
    439 	ifmr->ifm_active = mii->mii_media_active;
    440 	ifmr->ifm_status = mii->mii_media_status;
    441 
    442 	/* SMSC_UNLOCK */
    443 }
    444 
    445 static inline uint32_t
    446 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
    447 {
    448 
    449 	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
    450 }
    451 
    452 void
    453 smsc_setmulti(struct smsc_softc *sc)
    454 {
    455 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    456 	struct ether_multi *enm;
    457 	struct ether_multistep step;
    458 	uint32_t hashtbl[2] = { 0, 0 };
    459 	uint32_t hash;
    460 
    461 	KASSERT(mutex_owned(&sc->sc_lock));
    462 
    463 	if (sc->sc_dying)
    464 		return;
    465 
    466 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
    467 allmulti:
    468 		smsc_dbg_printf(sc, "receive all multicast enabled\n");
    469 		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
    470 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
    471 		smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    472 		return;
    473 	} else {
    474 		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
    475 		sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
    476 	}
    477 
    478 	ETHER_LOCK(&sc->sc_ec);
    479 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    480 	while (enm != NULL) {
    481 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    482 			ETHER_UNLOCK(&sc->sc_ec);
    483 			goto allmulti;
    484 		}
    485 
    486 		hash = smsc_hash(enm->enm_addrlo);
    487 		hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
    488 		ETHER_NEXT_MULTI(step, enm);
    489 	}
    490 	ETHER_UNLOCK(&sc->sc_ec);
    491 
    492 	/* Debug */
    493 	if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
    494 		smsc_dbg_printf(sc, "receive select group of macs\n");
    495 	} else {
    496 		smsc_dbg_printf(sc, "receive own packets only\n");
    497 	}
    498 
    499 	/* Write the hash table and mac control registers */
    500 	ifp->if_flags &= ~IFF_ALLMULTI;
    501 	smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
    502 	smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
    503 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    504 }
    505 
    506 int
    507 smsc_sethwcsum(struct smsc_softc *sc)
    508 {
    509 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    510 	uint32_t val;
    511 	int err;
    512 
    513 	err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
    514 	if (err != 0) {
    515 		smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
    516 		    err);
    517 		return err;
    518 	}
    519 
    520 	/* Enable/disable the Rx checksum */
    521 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx))
    522 		val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    523 	else
    524 		val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    525 
    526 	/* Enable/disable the Tx checksum (currently not supported) */
    527 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx))
    528 		val |= SMSC_COE_CTRL_TX_EN;
    529 	else
    530 		val &= ~SMSC_COE_CTRL_TX_EN;
    531 
    532 	sc->sc_coe_ctrl = val;
    533 
    534 	err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
    535 	if (err != 0) {
    536 		smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n",
    537 		    err);
    538 		return err;
    539 	}
    540 
    541 	return 0;
    542 }
    543 
    544 int
    545 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
    546 {
    547 	int err;
    548 	uint32_t val;
    549 
    550 	smsc_dbg_printf(sc, "setting mac address to "
    551 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
    552 	    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
    553 
    554 	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
    555 	if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
    556 		goto done;
    557 
    558 	val = (addr[5] << 8) | addr[4];
    559 	err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
    560 
    561 done:
    562 	return err;
    563 }
    564 
    565 void
    566 smsc_reset(struct smsc_softc *sc)
    567 {
    568 	KASSERT(mutex_owned(&sc->sc_lock));
    569 	if (sc->sc_dying)
    570 		return;
    571 
    572 	/* Wait a little while for the chip to get its brains in order. */
    573 	DELAY(1000);
    574 
    575 	/* Reinitialize controller to achieve full reset. */
    576 	smsc_chip_init(sc);
    577 }
    578 
    579 int
    580 smsc_init(struct ifnet *ifp)
    581 {
    582 	struct smsc_softc * const sc = ifp->if_softc;
    583 
    584 	mutex_enter(&sc->sc_lock);
    585 	int ret = smsc_init_locked(ifp);
    586 	mutex_exit(&sc->sc_lock);
    587 
    588 	return ret;
    589 }
    590 
    591 int
    592 smsc_init_locked(struct ifnet *ifp)
    593 {
    594 	struct smsc_softc * const sc = ifp->if_softc;
    595 	usbd_status err;
    596 
    597 	if (sc->sc_dying)
    598 		return EIO;
    599 
    600 	/* Cancel pending I/O */
    601 	smsc_stop_locked(ifp, 1);
    602 
    603 	/* Reset the ethernet interface. */
    604 	smsc_reset(sc);
    605 
    606 	/* Load the multicast filter. */
    607 	smsc_setmulti(sc);
    608 
    609 	/* TCP/UDP checksum offload engines. */
    610 	smsc_sethwcsum(sc);
    611 
    612 	/* Open RX and TX pipes. */
    613 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
    614 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_RX]);
    615 	if (err) {
    616 		printf("%s: open rx pipe failed: %s\n",
    617 		    device_xname(sc->sc_dev), usbd_errstr(err));
    618 		goto fail;
    619 	}
    620 
    621 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
    622 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_TX]);
    623 	if (err) {
    624 		printf("%s: open tx pipe failed: %s\n",
    625 		    device_xname(sc->sc_dev), usbd_errstr(err));
    626 		goto fail1;
    627 	}
    628 
    629 	/* Init RX ring. */
    630 	if (smsc_rx_list_init(sc)) {
    631 		aprint_error_dev(sc->sc_dev, "rx list init failed\n");
    632 		goto fail2;
    633 	}
    634 
    635 	/* Init TX ring. */
    636 	if (smsc_tx_list_init(sc)) {
    637 		aprint_error_dev(sc->sc_dev, "tx list init failed\n");
    638 		goto fail3;
    639 	}
    640 
    641 	mutex_enter(&sc->sc_rxlock);
    642 	mutex_enter(&sc->sc_txlock);
    643 	sc->sc_stopping = false;
    644 
    645 	/* Start up the receive pipe. */
    646 	for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) {
    647 		struct smsc_chain * const c = &sc->sc_cdata.rx_chain[i];
    648 		usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, sc->sc_bufsz,
    649 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, smsc_rxeof);
    650 		usbd_transfer(c->sc_xfer);
    651 	}
    652 
    653 	mutex_exit(&sc->sc_txlock);
    654 	mutex_exit(&sc->sc_rxlock);
    655 
    656 	/* Indicate we are up and running. */
    657 	ifp->if_flags |= IFF_RUNNING;
    658 	ifp->if_flags &= ~IFF_OACTIVE;
    659 
    660 	callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
    661 
    662 	return 0;
    663 
    664 fail3:
    665 	smsc_rx_list_free(sc);
    666 fail2:
    667 	usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
    668 fail1:
    669 	usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
    670 fail:
    671 	return EIO;
    672 }
    673 
    674 void
    675 smsc_start(struct ifnet *ifp)
    676 {
    677 	struct smsc_softc * const sc = ifp->if_softc;
    678 	KASSERT(ifp->if_extflags & IFEF_MPSAFE);
    679 
    680 	mutex_enter(&sc->sc_txlock);
    681 	if (!sc->sc_stopping)
    682 		smsc_start_locked(ifp);
    683 	mutex_exit(&sc->sc_txlock);
    684 }
    685 
    686 void
    687 smsc_start_locked(struct ifnet *ifp)
    688 {
    689 	struct smsc_softc * const sc = ifp->if_softc;
    690 	struct mbuf *m_head = NULL;
    691 
    692 	KASSERT(mutex_owned(&sc->sc_txlock));
    693 
    694 	/* Don't send anything if there is no link or controller is busy. */
    695 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
    696 		smsc_dbg_printf(sc, "%s: no link\n", __func__);
    697 		return;
    698 	}
    699 
    700 	/* Any free USB transfers? */
    701 	if (sc->sc_cdata.tx_free == 0) {
    702 		smsc_dbg_printf(sc, "%s: all USB transfers in use\n", __func__);
    703 		return;
    704 	}
    705 
    706 	if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING) {
    707 		smsc_dbg_printf(sc, "%s: not running\n", __func__);
    708 		return;
    709 	}
    710 
    711 	IFQ_POLL(&ifp->if_snd, m_head);
    712 	if (m_head == NULL)
    713 		return;
    714 
    715 	sc->sc_cdata.tx_free--;
    716 
    717 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    718 	if (smsc_encap(sc, m_head, sc->sc_cdata.tx_next)) {
    719 		m_free(m_head);
    720 		sc->sc_cdata.tx_free++;
    721 		return;
    722 	}
    723 
    724 	sc->sc_cdata.tx_next = (sc->sc_cdata.tx_next + 1) % SMSC_TX_LIST_CNT;
    725 
    726 	bpf_mtap(ifp, m_head, BPF_D_OUT);
    727 
    728 	if (sc->sc_cdata.tx_free == 0)
    729 		ifp->if_flags |= IFF_OACTIVE;
    730 
    731 	/*
    732 	 * Set a timeout in case the chip goes out to lunch.
    733 	 */
    734 	ifp->if_timer = 5;
    735 }
    736 
    737 void
    738 smsc_tick(void *xsc)
    739 {
    740 	struct smsc_softc * const sc = xsc;
    741 
    742 	if (sc == NULL)
    743 		return;
    744 
    745 	mutex_enter(&sc->sc_lock);
    746 
    747 	if (sc->sc_dying) {
    748 		mutex_exit(&sc->sc_lock);
    749 		return;
    750 	}
    751 
    752 	if (!sc->sc_ttpending) {
    753 		sc->sc_ttpending = true;
    754 		usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
    755 	}
    756 
    757 	mutex_exit(&sc->sc_lock);
    758 }
    759 
    760 void
    761 smsc_stop(struct ifnet *ifp, int disable)
    762 {
    763 	struct smsc_softc * const sc = ifp->if_softc;
    764 
    765 	mutex_enter(&sc->sc_lock);
    766 	smsc_stop_locked(ifp, disable);
    767 	mutex_exit(&sc->sc_lock);
    768 }
    769 
    770 void
    771 smsc_stop_locked(struct ifnet *ifp, int disable)
    772 {
    773 	struct smsc_softc * const sc = ifp->if_softc;
    774 	usbd_status err;
    775 
    776 	KASSERT(mutex_owned(&sc->sc_lock));
    777 	mutex_enter(&sc->sc_rxlock);
    778 	mutex_enter(&sc->sc_txlock);
    779 	sc->sc_stopping = true;
    780 	mutex_exit(&sc->sc_txlock);
    781 	mutex_exit(&sc->sc_rxlock);
    782 
    783 	callout_stop(&sc->sc_stat_ch);
    784 
    785 	/* Stop transfers. */
    786 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
    787 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
    788 		if (err) {
    789 			printf("%s: abort rx pipe failed: %s\n",
    790 			    device_xname(sc->sc_dev), usbd_errstr(err));
    791 		}
    792 	}
    793 
    794 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
    795 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
    796 		if (err) {
    797 			printf("%s: abort tx pipe failed: %s\n",
    798 			    device_xname(sc->sc_dev), usbd_errstr(err));
    799 		}
    800 	}
    801 
    802 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
    803 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
    804 		if (err) {
    805 			printf("%s: abort intr pipe failed: %s\n",
    806 			    device_xname(sc->sc_dev), usbd_errstr(err));
    807 		}
    808 	}
    809 
    810 	smsc_rx_list_free(sc);
    811 
    812 	smsc_tx_list_free(sc);
    813 
    814 	/* Close pipes */
    815 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
    816 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
    817 		if (err) {
    818 			printf("%s: close rx pipe failed: %s\n",
    819 			    device_xname(sc->sc_dev), usbd_errstr(err));
    820 		}
    821 		sc->sc_ep[SMSC_ENDPT_RX] = NULL;
    822 	}
    823 
    824 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
    825 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
    826 		if (err) {
    827 			printf("%s: close tx pipe failed: %s\n",
    828 			    device_xname(sc->sc_dev), usbd_errstr(err));
    829 		}
    830 		sc->sc_ep[SMSC_ENDPT_TX] = NULL;
    831 	}
    832 
    833 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
    834 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
    835 		if (err) {
    836 			printf("%s: close intr pipe failed: %s\n",
    837 			    device_xname(sc->sc_dev), usbd_errstr(err));
    838 		}
    839 		sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
    840 	}
    841 
    842 	ifp->if_timer = 0;
    843 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    844 
    845 	if (disable) {
    846 		/* drain */
    847 	}
    848 }
    849 
    850 int
    851 smsc_chip_init(struct smsc_softc *sc)
    852 {
    853 	int err;
    854 	uint32_t reg_val;
    855 	int burst_cap;
    856 
    857 	/* Enter H/W config mode */
    858 	smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
    859 
    860 	if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
    861 	    SMSC_HW_CFG_LRST)) != 0) {
    862 		smsc_warn_printf(sc, "timed-out waiting for reset to "
    863 		    "complete\n");
    864 		goto init_failed;
    865 	}
    866 
    867 	/* Reset the PHY */
    868 	smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
    869 
    870 	if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
    871 	    SMSC_PM_CTRL_PHY_RST)) != 0) {
    872 		smsc_warn_printf(sc, "timed-out waiting for phy reset to "
    873 		    "complete\n");
    874 		goto init_failed;
    875 	}
    876 	usbd_delay_ms(sc->sc_udev, 40);
    877 
    878 	/* Set the mac address */
    879 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    880 	const char *eaddr = CLLADDR(ifp->if_sadl);
    881 	if ((err = smsc_setmacaddress(sc, eaddr)) != 0) {
    882 		smsc_warn_printf(sc, "failed to set the MAC address\n");
    883 		goto init_failed;
    884 	}
    885 
    886 	/*
    887 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
    888 	 * sequence as used in the Linux driver.
    889 	 */
    890 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
    891 		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
    892 		goto init_failed;
    893 	}
    894 	reg_val |= SMSC_HW_CFG_BIR;
    895 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    896 
    897 	/*
    898 	 * There is a so called 'turbo mode' that the linux driver supports, it
    899 	 * seems to allow you to jam multiple frames per Rx transaction.
    900 	 * By default this driver supports that and therefore allows multiple
    901 	 * frames per USB transfer.
    902 	 *
    903 	 * The xfer buffer size needs to reflect this as well, therefore based
    904 	 * on the calculations in the Linux driver the RX bufsize is set to
    905 	 * 18944,
    906 	 *     bufsz = (16 * 1024 + 5 * 512)
    907 	 *
    908 	 * Burst capability is the number of URBs that can be in a burst of
    909 	 * data/ethernet frames.
    910 	 */
    911 
    912 	if (sc->sc_udev->ud_speed == USB_SPEED_HIGH)
    913 		burst_cap = 37;
    914 	else
    915 		burst_cap = 128;
    916 
    917 	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
    918 
    919 	/* Set the default bulk in delay (magic value from Linux driver) */
    920 	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
    921 
    922 	/*
    923 	 * Initialise the RX interface
    924 	 */
    925 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
    926 		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
    927 		    err);
    928 		goto init_failed;
    929 	}
    930 
    931 	/*
    932 	 * The following settings are used for 'turbo mode', a.k.a multiple
    933 	 * frames per Rx transaction (again info taken form Linux driver).
    934 	 */
    935 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
    936 
    937 	/*
    938 	 * set Rx data offset to ETHER_ALIGN which will make the IP header
    939 	 * align on a word boundary.
    940 	 */
    941 	reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;
    942 
    943 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    944 
    945 	/* Clear the status register ? */
    946 	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
    947 
    948 	/* Read and display the revision register */
    949 	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
    950 		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
    951 		goto init_failed;
    952 	}
    953 
    954 	/* GPIO/LED setup */
    955 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
    956 	    SMSC_LED_GPIO_CFG_FDX_LED;
    957 	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
    958 
    959 	/*
    960 	 * Initialise the TX interface
    961 	 */
    962 	smsc_write_reg(sc, SMSC_FLOW, 0);
    963 
    964 	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
    965 
    966 	/* Read the current MAC configuration */
    967 	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
    968 		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
    969 		goto init_failed;
    970 	}
    971 
    972 	/* disable pad stripping, collides with checksum offload */
    973 	sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;
    974 
    975 	/* Vlan */
    976 	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
    977 
    978 	/*
    979 	 * Start TX
    980 	 */
    981 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
    982 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    983 	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
    984 
    985 	/*
    986 	 * Start RX
    987 	 */
    988 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
    989 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    990 
    991 	return 0;
    992 
    993 init_failed:
    994 	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
    995 	return err;
    996 }
    997 
    998 static int
    999 smsc_ifflags_cb(struct ethercom *ec)
   1000 {
   1001 	struct ifnet *ifp = &ec->ec_if;
   1002 	struct smsc_softc *sc = ifp->if_softc;
   1003 
   1004 	mutex_enter(&sc->sc_lock);
   1005 
   1006 	const int change = ifp->if_flags ^ sc->sc_if_flags;
   1007 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   1008 		mutex_exit(&sc->sc_lock);
   1009 		return ENETRESET;
   1010 	}
   1011 
   1012 	smsc_dbg_printf(sc, "%s: change %x\n", __func__, change);
   1013 
   1014 	if ((change & IFF_PROMISC) != 0) {
   1015 		if (ifp->if_flags & IFF_PROMISC) {
   1016 			sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
   1017 			smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
   1018 		} else if (!(ifp->if_flags & IFF_PROMISC)) {
   1019 			sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
   1020 			smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
   1021 		}
   1022 		smsc_setmulti(sc);
   1023 	}
   1024 
   1025 	mutex_exit(&sc->sc_lock);
   1026 
   1027 	return 0;
   1028 }
   1029 
   1030 
   1031 int
   1032 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1033 {
   1034 	struct smsc_softc * const sc = ifp->if_softc;
   1035 
   1036 	smsc_dbg_printf(sc, "%s: cmd %0lx data %p\n", __func__, cmd, data);
   1037 
   1038 	int error = ether_ioctl(ifp, cmd, data);
   1039 
   1040 	if (error == ENETRESET) {
   1041 		error = 0;
   1042 		if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
   1043 			if (ifp->if_flags & IFF_RUNNING) {
   1044 				mutex_enter(&sc->sc_lock);
   1045 				smsc_setmulti(sc);
   1046 				mutex_exit(&sc->sc_lock);
   1047 			}
   1048 		}
   1049 	}
   1050 
   1051 	mutex_enter(&sc->sc_rxlock);
   1052 	mutex_enter(&sc->sc_txlock);
   1053 	sc->sc_if_flags = ifp->if_flags;
   1054 	mutex_exit(&sc->sc_txlock);
   1055 	mutex_exit(&sc->sc_rxlock);
   1056 
   1057 	return error;
   1058 }
   1059 
   1060 int
   1061 smsc_match(device_t parent, cfdata_t match, void *aux)
   1062 {
   1063 	struct usb_attach_arg *uaa = aux;
   1064 
   1065 	return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
   1066 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
   1067 }
   1068 
   1069 void
   1070 smsc_attach(device_t parent, device_t self, void *aux)
   1071 {
   1072 	struct smsc_softc *sc = device_private(self);
   1073 	struct usb_attach_arg *uaa = aux;
   1074 	struct usbd_device *dev = uaa->uaa_device;
   1075 	usb_interface_descriptor_t *id;
   1076 	usb_endpoint_descriptor_t *ed;
   1077 	char *devinfop;
   1078 	struct mii_data *mii;
   1079 	struct ifnet *ifp;
   1080 	int err, i;
   1081 	uint32_t mac_h, mac_l;
   1082 
   1083 	sc->sc_dev = self;
   1084 	sc->sc_udev = dev;
   1085 	sc->sc_stopping = false;
   1086 
   1087 	aprint_naive("\n");
   1088 	aprint_normal("\n");
   1089 
   1090 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
   1091 	aprint_normal_dev(self, "%s\n", devinfop);
   1092 	usbd_devinfo_free(devinfop);
   1093 
   1094 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
   1095 	if (err) {
   1096 		aprint_error_dev(self, "failed to set configuration"
   1097 		    ", err=%s\n", usbd_errstr(err));
   1098 		return;
   1099 	}
   1100 
   1101 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
   1102 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
   1103 	if (err) {
   1104 		aprint_error_dev(self, "getting interface handle failed\n");
   1105 		return;
   1106 	}
   1107 
   1108 	id = usbd_get_interface_descriptor(sc->sc_iface);
   1109 
   1110 	if (sc->sc_udev->ud_speed >= USB_SPEED_HIGH)
   1111 		sc->sc_bufsz = SMSC_MAX_BUFSZ;
   1112 	else
   1113 		sc->sc_bufsz = SMSC_MIN_BUFSZ;
   1114 
   1115 	/* Find endpoints. */
   1116 	for (i = 0; i < id->bNumEndpoints; i++) {
   1117 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
   1118 		if (!ed) {
   1119 			aprint_error_dev(self, "couldn't get ep %d\n", i);
   1120 			return;
   1121 		}
   1122 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1123 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1124 			sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
   1125 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
   1126 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1127 			sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
   1128 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1129 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
   1130 			sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
   1131 		}
   1132 	}
   1133 
   1134 	usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, USB_TASKQ_MPSAFE);
   1135 
   1136 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
   1137 	mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1138 	mutex_init(&sc->sc_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1139 	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
   1140 
   1141 	ifp = &sc->sc_ec.ec_if;
   1142 	ifp->if_softc = sc;
   1143 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
   1144 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1145 	ifp->if_extflags = IFEF_MPSAFE;
   1146 	ifp->if_init = smsc_init;
   1147 	ifp->if_ioctl = smsc_ioctl;
   1148 	ifp->if_start = smsc_start;
   1149 	ifp->if_stop = smsc_stop;
   1150 
   1151 #ifdef notyet
   1152 	/*
   1153 	 * We can do TCPv4, and UDPv4 checksums in hardware.
   1154 	 */
   1155 	ifp->if_capabilities |=
   1156 	    /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
   1157 	    /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
   1158 #endif
   1159 
   1160 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
   1161 
   1162 	/* Setup some of the basics */
   1163 	sc->sc_phyno = 1;
   1164 
   1165 	/*
   1166 	 * Attempt to get the mac address, if an EEPROM is not attached this
   1167 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
   1168 	 * address based on urandom.
   1169 	 */
   1170 	memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);
   1171 
   1172 	prop_dictionary_t dict = device_properties(self);
   1173 	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
   1174 
   1175 	if (eaprop != NULL) {
   1176 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
   1177 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
   1178 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
   1179 		    ETHER_ADDR_LEN);
   1180 	} else {
   1181 		/* Check if there is already a MAC address in the register */
   1182 		if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
   1183 		    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
   1184 			sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
   1185 			sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
   1186 			sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
   1187 			sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
   1188 			sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
   1189 			sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
   1190 		}
   1191 	}
   1192 
   1193 	aprint_normal_dev(self, "Ethernet address %s\n",
   1194 	    ether_sprintf(sc->sc_enaddr));
   1195 
   1196 	IFQ_SET_READY(&ifp->if_snd);
   1197 
   1198 	/* Initialize MII/media info. */
   1199 	mii = &sc->sc_mii;
   1200 	mii->mii_ifp = ifp;
   1201 	mii->mii_readreg = smsc_miibus_readreg;
   1202 	mii->mii_writereg = smsc_miibus_writereg;
   1203 	mii->mii_statchg = smsc_miibus_statchg;
   1204 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1205 	sc->sc_ec.ec_mii = mii;
   1206 	ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
   1207 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
   1208 
   1209 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1210 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1211 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1212 	} else
   1213 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1214 
   1215 	callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE);
   1216 
   1217 	if_initialize(ifp);
   1218 	sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if);
   1219 	ether_ifattach(ifp, sc->sc_enaddr);
   1220 	ether_set_ifflags_cb(&sc->sc_ec, smsc_ifflags_cb);
   1221 	if_register(ifp);
   1222 
   1223 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
   1224 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
   1225 
   1226 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
   1227 }
   1228 
   1229 int
   1230 smsc_detach(device_t self, int flags)
   1231 {
   1232 	struct smsc_softc *sc = device_private(self);
   1233 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1234 
   1235 	mutex_enter(&sc->sc_lock);
   1236 	sc->sc_dying = true;
   1237 	mutex_exit(&sc->sc_lock);
   1238 
   1239 	callout_halt(&sc->sc_stat_ch, NULL);
   1240 
   1241 	if (ifp->if_flags & IFF_RUNNING)
   1242 		smsc_stop_locked(ifp, 1);
   1243 
   1244 	/*
   1245 	 * Remove any pending tasks.  They cannot be executing because they run
   1246 	 * in the same thread as detach.
   1247 	 */
   1248 	usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER,
   1249 	    NULL);
   1250 
   1251 	mutex_enter(&sc->sc_lock);
   1252 	sc->sc_refcnt--;
   1253 	while (sc->sc_refcnt > 0) {
   1254 		/* Wait for processes to go away */
   1255 		cv_wait(&sc->sc_detachcv, &sc->sc_lock);
   1256 	}
   1257 
   1258 #ifdef DIAGNOSTIC
   1259 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
   1260 	    sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
   1261 	    sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
   1262 		printf("%s: detach has active endpoints\n",
   1263 		    device_xname(sc->sc_dev));
   1264 #endif
   1265 
   1266 	mutex_exit(&sc->sc_lock);
   1267 
   1268 	rnd_detach_source(&sc->sc_rnd_source);
   1269 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1270 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   1271 	if (ifp->if_softc != NULL) {
   1272 		ether_ifdetach(ifp);
   1273 		if_detach(ifp);
   1274 	}
   1275 
   1276 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
   1277 
   1278 	cv_destroy(&sc->sc_detachcv);
   1279 	mutex_destroy(&sc->sc_mii_lock);
   1280 	mutex_destroy(&sc->sc_rxlock);
   1281 	mutex_destroy(&sc->sc_txlock);
   1282 	mutex_destroy(&sc->sc_lock);
   1283 
   1284 	return 0;
   1285 }
   1286 
   1287 void
   1288 smsc_tick_task(void *xsc)
   1289 {
   1290 	struct smsc_softc * const sc = xsc;
   1291 
   1292 	if (sc == NULL)
   1293 		return;
   1294 
   1295 	mutex_enter(&sc->sc_lock);
   1296 
   1297 	if (sc->sc_dying) {
   1298 		mutex_exit(&sc->sc_lock);
   1299 		return;
   1300 	}
   1301 
   1302 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1303 	struct mii_data * const mii = &sc->sc_mii;
   1304 
   1305 	sc->sc_refcnt++;
   1306 	mutex_exit(&sc->sc_lock);
   1307 
   1308 	mii_tick(mii);
   1309 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
   1310 		smsc_miibus_statchg(ifp);
   1311 
   1312 	mutex_enter(&sc->sc_lock);
   1313 	sc->sc_ttpending = false;
   1314 
   1315 	if (--sc->sc_refcnt < 0)
   1316 		cv_broadcast(&sc->sc_detachcv);
   1317 
   1318 	if (sc->sc_dying) {
   1319 		mutex_exit(&sc->sc_lock);
   1320 		return;
   1321 	}
   1322 	callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
   1323 
   1324 	mutex_exit(&sc->sc_lock);
   1325 }
   1326 
   1327 int
   1328 smsc_activate(device_t self, enum devact act)
   1329 {
   1330 	struct smsc_softc *sc = device_private(self);
   1331 
   1332 	switch (act) {
   1333 	case DVACT_DEACTIVATE:
   1334 		if_deactivate(&sc->sc_ec.ec_if);
   1335 
   1336 		mutex_enter(&sc->sc_lock);
   1337 		sc->sc_dying = true;
   1338 
   1339 		mutex_enter(&sc->sc_rxlock);
   1340 		mutex_enter(&sc->sc_txlock);
   1341 		sc->sc_stopping = true;
   1342 		mutex_exit(&sc->sc_txlock);
   1343 		mutex_exit(&sc->sc_rxlock);
   1344 
   1345 		mutex_exit(&sc->sc_lock);
   1346 		return 0;
   1347 	default:
   1348 		return EOPNOTSUPP;
   1349 	}
   1350 	return 0;
   1351 }
   1352 
   1353 void
   1354 smsc_lock_mii(struct smsc_softc *sc)
   1355 {
   1356 
   1357 	mutex_enter(&sc->sc_lock);
   1358 	sc->sc_refcnt++;
   1359 	mutex_exit(&sc->sc_lock);
   1360 
   1361 	mutex_enter(&sc->sc_mii_lock);
   1362 }
   1363 
   1364 void
   1365 smsc_unlock_mii(struct smsc_softc *sc)
   1366 {
   1367 
   1368 	mutex_exit(&sc->sc_mii_lock);
   1369 	mutex_enter(&sc->sc_lock);
   1370 	if (--sc->sc_refcnt < 0)
   1371 		cv_broadcast(&sc->sc_detachcv);
   1372 	mutex_exit(&sc->sc_lock);
   1373 }
   1374 
   1375 void
   1376 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1377 {
   1378 	struct smsc_chain * const c = (struct smsc_chain *)priv;
   1379 	struct smsc_softc * const sc = c->sc_sc;
   1380 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1381 	u_char *buf = c->sc_buf;
   1382 	uint32_t total_len;
   1383 
   1384 	mutex_enter(&sc->sc_rxlock);
   1385 	if (sc->sc_stopping) {
   1386 		smsc_dbg_printf(sc, "%s: stopping\n", __func__);
   1387 		mutex_exit(&sc->sc_rxlock);
   1388 		return;
   1389 	}
   1390 
   1391 	if (!(sc->sc_if_flags & IFF_RUNNING)) {
   1392 		smsc_dbg_printf(sc, "%s: not running\n", __func__);
   1393 		mutex_exit(&sc->sc_rxlock);
   1394 		return;
   1395 	}
   1396 
   1397 	if (status != USBD_NORMAL_COMPLETION) {
   1398 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1399 	    		mutex_exit(&sc->sc_rxlock);
   1400 			return;
   1401 		}
   1402 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
   1403 			printf("%s: usb errors on rx: %s\n",
   1404 			    device_xname(sc->sc_dev), usbd_errstr(status));
   1405 		}
   1406 		if (status == USBD_STALLED)
   1407 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
   1408 		goto done;
   1409 	}
   1410 
   1411 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1412 	smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
   1413 
   1414 	while (total_len != 0) {
   1415 		uint32_t rxhdr;
   1416 		if (total_len < sizeof(rxhdr)) {
   1417 			smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n",
   1418 			    total_len, sizeof(rxhdr));
   1419 			ifp->if_ierrors++;
   1420 			goto done;
   1421 		}
   1422 
   1423 		memcpy(&rxhdr, buf, sizeof(rxhdr));
   1424 		rxhdr = le32toh(rxhdr);
   1425 		buf += sizeof(rxhdr);
   1426 		total_len -= sizeof(rxhdr);
   1427 
   1428 		if (rxhdr & SMSC_RX_STAT_COLLISION)
   1429 			ifp->if_collisions++;
   1430 
   1431 		if (rxhdr & (SMSC_RX_STAT_ERROR
   1432 		           | SMSC_RX_STAT_LENGTH_ERROR
   1433 		           | SMSC_RX_STAT_MII_ERROR)) {
   1434 			smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
   1435 			ifp->if_ierrors++;
   1436 			goto done;
   1437 		}
   1438 
   1439 		uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
   1440 		smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
   1441 		    "0x%08x\n", total_len, pktlen, rxhdr);
   1442 
   1443 		if (pktlen < ETHER_HDR_LEN) {
   1444 			smsc_dbg_printf(sc, "pktlen %d < ETHER_HDR_LEN %d\n",
   1445 			    pktlen, ETHER_HDR_LEN);
   1446 			ifp->if_ierrors++;
   1447 			goto done;
   1448 		}
   1449 
   1450 		pktlen += ETHER_ALIGN;
   1451 
   1452 		if (pktlen > MCLBYTES) {
   1453 			smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n",
   1454 			    pktlen, MCLBYTES);
   1455 			ifp->if_ierrors++;
   1456 			goto done;
   1457 		}
   1458 
   1459 		if (pktlen > total_len) {
   1460 			smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
   1461 			    pktlen, total_len);
   1462 			ifp->if_ierrors++;
   1463 			goto done;
   1464 		}
   1465 
   1466 		struct mbuf *m = smsc_newbuf();
   1467 		if (m == NULL) {
   1468 			smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
   1469 			ifp->if_ierrors++;
   1470 			goto done;
   1471 		}
   1472 
   1473 		m_set_rcvif(m, ifp);
   1474 		m->m_pkthdr.len = m->m_len = pktlen;
   1475 		m->m_flags |= M_HASFCS;
   1476 		m_adj(m, ETHER_ALIGN);
   1477 
   1478 		KASSERT(m->m_len < MCLBYTES);
   1479 		memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len);
   1480 
   1481 		/* Check if RX TCP/UDP checksumming is being offloaded */
   1482 		if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
   1483 			smsc_dbg_printf(sc,"RX checksum offload checking\n");
   1484 			struct ether_header *eh;
   1485 
   1486 			eh = mtod(m, struct ether_header *);
   1487 
   1488 			/* Remove the extra 2 bytes of the csum */
   1489 			m_adj(m, -2);
   1490 
   1491 			/*
   1492 			 * The checksum appears to be simplistically calculated
   1493 			 * over the udp/tcp header and data up to the end of the
   1494 			 * eth frame.  Which means if the eth frame is padded
   1495 			 * the csum calculation is incorrectly performed over
   1496 			 * the padding bytes as well. Therefore to be safe we
   1497 			 * ignore the H/W csum on frames less than or equal to
   1498 			 * 64 bytes.
   1499 			 *
   1500 			 * Ignore H/W csum for non-IPv4 packets.
   1501 			 */
   1502 			smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n",
   1503 			    be16toh(eh->ether_type), pktlen);
   1504 			if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
   1505 			    pktlen > ETHER_MIN_LEN) {
   1506 
   1507 				m->m_pkthdr.csum_flags |=
   1508 				    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);
   1509 
   1510 				/*
   1511 				 * Copy the TCP/UDP checksum from the last 2
   1512 				 * bytes of the transfer and put in the
   1513 				 * csum_data field.
   1514 				 */
   1515 				memcpy(&m->m_pkthdr.csum_data,
   1516 				    buf + pktlen - 2, 2);
   1517 				/*
   1518 				 * The data is copied in network order, but the
   1519 				 * csum algorithm in the kernel expects it to be
   1520 				 * in host network order.
   1521 				 */
   1522 				m->m_pkthdr.csum_data =
   1523 				    ntohs(m->m_pkthdr.csum_data);
   1524 				smsc_dbg_printf(sc,
   1525 				    "RX checksum offloaded (0x%04x)\n",
   1526 				    m->m_pkthdr.csum_data);
   1527 			}
   1528 		}
   1529 
   1530 		/* round up to next longword */
   1531 		pktlen = (pktlen + 3) & ~0x3;
   1532 
   1533 		/* total_len does not include the padding */
   1534 		if (pktlen > total_len)
   1535 			pktlen = total_len;
   1536 
   1537 		buf += pktlen;
   1538 		total_len -= pktlen;
   1539 
   1540 		mutex_exit(&sc->sc_rxlock);
   1541 
   1542 		/* push the packet up */
   1543 		if_percpuq_enqueue(sc->sc_ipq, m);
   1544 
   1545 		mutex_enter(&sc->sc_rxlock);
   1546 		if (sc->sc_stopping) {
   1547 			smsc_dbg_printf(sc, "%s: stopping\n", __func__);
   1548 			mutex_exit(&sc->sc_rxlock);
   1549 			return;
   1550 		}
   1551 	}
   1552 
   1553 done:
   1554 	mutex_exit(&sc->sc_rxlock);
   1555 
   1556 	/* Setup new transfer. */
   1557 	usbd_setup_xfer(xfer, c, c->sc_buf, sc->sc_bufsz, USBD_SHORT_XFER_OK,
   1558 	    USBD_NO_TIMEOUT, smsc_rxeof);
   1559 	usbd_transfer(xfer);
   1560 
   1561 	return;
   1562 }
   1563 
   1564 void
   1565 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1566 {
   1567 	struct smsc_chain *c = priv;
   1568 	struct smsc_softc *sc = c->sc_sc;
   1569 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1570 
   1571 	mutex_enter(&sc->sc_txlock);
   1572 	if (sc->sc_stopping) {
   1573 		smsc_dbg_printf(sc, "%s: stopping\n", __func__);
   1574 		mutex_exit(&sc->sc_txlock);
   1575 		return;
   1576 	}
   1577 
   1578 	sc->sc_cdata.tx_free++;
   1579 	ifp->if_timer = 0;
   1580 	ifp->if_flags &= ~IFF_OACTIVE;
   1581 
   1582 	if (status != USBD_NORMAL_COMPLETION) {
   1583 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1584 			mutex_exit(&sc->sc_txlock);
   1585 			return;
   1586 		}
   1587 		ifp->if_oerrors++;
   1588 		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
   1589 		    usbd_errstr(status));
   1590 		if (status == USBD_STALLED)
   1591 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
   1592 		mutex_exit(&sc->sc_txlock);
   1593 		return;
   1594 	}
   1595 	ifp->if_opackets++;
   1596 
   1597 	m_freem(c->sc_mbuf);
   1598 	c->sc_mbuf = NULL;
   1599 
   1600 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1601 		smsc_start_locked(ifp);
   1602 
   1603 	mutex_exit(&sc->sc_txlock);
   1604 }
   1605 
   1606 int
   1607 smsc_tx_list_init(struct smsc_softc *sc)
   1608 {
   1609 	struct smsc_cdata *cd = &sc->sc_cdata;
   1610 	struct smsc_chain *c;
   1611 	int i;
   1612 
   1613 	for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
   1614 		c = &cd->tx_chain[i];
   1615 		c->sc_sc = sc;
   1616 		c->sc_idx = i;
   1617 		c->sc_mbuf = NULL;
   1618 		if (c->sc_xfer == NULL) {
   1619 			int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_TX],
   1620 			    sc->sc_bufsz, USBD_FORCE_SHORT_XFER, 0,
   1621 			    &c->sc_xfer);
   1622 			if (error)
   1623 				return EIO;
   1624 			c->sc_buf = usbd_get_buffer(c->sc_xfer);
   1625 		}
   1626 	}
   1627 
   1628 	cd->tx_free = SMSC_TX_LIST_CNT;
   1629 	cd->tx_next = 0;
   1630 
   1631 	return 0;
   1632 }
   1633 
   1634 void
   1635 smsc_tx_list_free(struct smsc_softc *sc)
   1636 {
   1637 	/* Free TX resources. */
   1638 	for (size_t i = 0; i < SMSC_TX_LIST_CNT; i++) {
   1639 		if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
   1640 			m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
   1641 			sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
   1642 		}
   1643 		if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
   1644 			usbd_destroy_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
   1645 			sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
   1646 		}
   1647 	}
   1648 }
   1649 
   1650 int
   1651 smsc_rx_list_init(struct smsc_softc *sc)
   1652 {
   1653 	struct smsc_cdata *cd = &sc->sc_cdata;
   1654 	struct smsc_chain *c;
   1655 	int i;
   1656 
   1657 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
   1658 		c = &cd->rx_chain[i];
   1659 		c->sc_sc = sc;
   1660 		c->sc_idx = i;
   1661 		c->sc_mbuf = NULL;
   1662 		if (c->sc_xfer == NULL) {
   1663 			int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_RX],
   1664 			    sc->sc_bufsz, USBD_SHORT_XFER_OK, 0, &c->sc_xfer);
   1665 			if (error)
   1666 				return error;
   1667 			c->sc_buf = usbd_get_buffer(c->sc_xfer);
   1668 		}
   1669 	}
   1670 
   1671 	return 0;
   1672 }
   1673 
   1674 void
   1675 smsc_rx_list_free(struct smsc_softc *sc)
   1676 {
   1677 	/* Free RX resources. */
   1678 	for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) {
   1679 		if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
   1680 			m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
   1681 			sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
   1682 		}
   1683 		if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
   1684 			usbd_destroy_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
   1685 			sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
   1686 		}
   1687 	}
   1688 }
   1689 
   1690 struct mbuf *
   1691 smsc_newbuf(void)
   1692 {
   1693 	struct mbuf *m;
   1694 
   1695 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1696 	if (m == NULL)
   1697 		return NULL;
   1698 
   1699 	MCLGET(m, M_DONTWAIT);
   1700 	if (!(m->m_flags & M_EXT)) {
   1701 		m_freem(m);
   1702 		return NULL;
   1703 	}
   1704 
   1705 	return m;
   1706 }
   1707 
   1708 int
   1709 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
   1710 {
   1711 	struct smsc_chain * const c = &sc->sc_cdata.tx_chain[idx];
   1712 	uint32_t txhdr;
   1713 	uint32_t frm_len = 0;
   1714 
   1715 	/*
   1716 	 * Each frame is prefixed with two 32-bit values describing the
   1717 	 * length of the packet and buffer.
   1718 	 */
   1719 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
   1720 	    SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
   1721 	txhdr = htole32(txhdr);
   1722 	memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
   1723 
   1724 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
   1725 	txhdr = htole32(txhdr);
   1726 	memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
   1727 
   1728 	frm_len += 8;
   1729 
   1730 	/* Next copy in the actual packet */
   1731 	m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
   1732 	frm_len += m->m_pkthdr.len;
   1733 
   1734 	c->sc_mbuf = m;
   1735 
   1736 	usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, frm_len,
   1737 	    USBD_FORCE_SHORT_XFER, 10000, smsc_txeof);
   1738 
   1739 	usbd_status err = usbd_transfer(c->sc_xfer);
   1740 	if (err != USBD_IN_PROGRESS) {
   1741 		return EIO;
   1742 	}
   1743 
   1744 	return 0;
   1745 }
   1746