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