Home | History | Annotate | Line # | Download | only in usb
if_smsc.c revision 1.22.2.37
      1 /*	$NetBSD: if_smsc.c,v 1.22.2.37 2017/08/30 10:08:22 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 	/* Any free USB transfers? */
    698 	if (sc->sc_cdata.tx_free == 0) {
    699 		smsc_dbg_printf(sc, "%s: all USB transfers in use\n", __func__);
    700 		return;
    701 	}
    702 
    703 	if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING) {
    704 		smsc_dbg_printf(sc, "%s: not running\n", __func__);
    705 		return;
    706 	}
    707 
    708 	IFQ_POLL(&ifp->if_snd, m_head);
    709 	if (m_head == NULL)
    710 		return;
    711 
    712 	sc->sc_cdata.tx_free--;
    713 
    714 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    715 	if (smsc_encap(sc, m_head, sc->sc_cdata.tx_next)) {
    716 		m_free(m_head);
    717 		sc->sc_cdata.tx_free++;
    718 		return;
    719 	}
    720 
    721 	sc->sc_cdata.tx_next = (sc->sc_cdata.tx_next + 1) % SMSC_TX_LIST_CNT;
    722 
    723 	bpf_mtap(ifp, m_head);
    724 
    725 	if (sc->sc_cdata.tx_free == 0)
    726 		ifp->if_flags |= IFF_OACTIVE;
    727 
    728 	/*
    729 	 * Set a timeout in case the chip goes out to lunch.
    730 	 */
    731 	ifp->if_timer = 5;
    732 }
    733 
    734 void
    735 smsc_tick(void *xsc)
    736 {
    737 	struct smsc_softc * const sc = xsc;
    738 
    739 	if (sc == NULL)
    740 		return;
    741 
    742 	mutex_enter(&sc->sc_lock);
    743 
    744 	if (sc->sc_dying) {
    745 		mutex_exit(&sc->sc_lock);
    746 		return;
    747 	}
    748 
    749 	if (!sc->sc_ttpending) {
    750 		sc->sc_ttpending = true;
    751 		usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
    752 	}
    753 
    754 	mutex_exit(&sc->sc_lock);
    755 }
    756 
    757 void
    758 smsc_stop(struct ifnet *ifp, int disable)
    759 {
    760 	struct smsc_softc * const sc = ifp->if_softc;
    761 
    762 	mutex_enter(&sc->sc_lock);
    763 	smsc_stop_locked(ifp, disable);
    764 	mutex_exit(&sc->sc_lock);
    765 }
    766 
    767 void
    768 smsc_stop_locked(struct ifnet *ifp, int disable)
    769 {
    770 	struct smsc_softc * const sc = ifp->if_softc;
    771 	usbd_status err;
    772 
    773 	KASSERT(mutex_owned(&sc->sc_lock));
    774 	mutex_enter(&sc->sc_rxlock);
    775 	mutex_enter(&sc->sc_txlock);
    776 	sc->sc_stopping = true;
    777 	mutex_exit(&sc->sc_txlock);
    778 	mutex_exit(&sc->sc_rxlock);
    779 
    780 	callout_stop(&sc->sc_stat_ch);
    781 
    782 	/* Stop transfers. */
    783 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
    784 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
    785 		if (err) {
    786 			printf("%s: abort rx pipe failed: %s\n",
    787 			    device_xname(sc->sc_dev), usbd_errstr(err));
    788 		}
    789 	}
    790 
    791 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
    792 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
    793 		if (err) {
    794 			printf("%s: abort tx pipe failed: %s\n",
    795 			    device_xname(sc->sc_dev), usbd_errstr(err));
    796 		}
    797 	}
    798 
    799 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
    800 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
    801 		if (err) {
    802 			printf("%s: abort intr pipe failed: %s\n",
    803 			    device_xname(sc->sc_dev), usbd_errstr(err));
    804 		}
    805 	}
    806 
    807 	smsc_rx_list_free(sc);
    808 
    809 	smsc_tx_list_free(sc);
    810 
    811 	/* Close pipes */
    812 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
    813 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
    814 		if (err) {
    815 			printf("%s: close rx pipe failed: %s\n",
    816 			    device_xname(sc->sc_dev), usbd_errstr(err));
    817 		}
    818 		sc->sc_ep[SMSC_ENDPT_RX] = NULL;
    819 	}
    820 
    821 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
    822 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
    823 		if (err) {
    824 			printf("%s: close tx pipe failed: %s\n",
    825 			    device_xname(sc->sc_dev), usbd_errstr(err));
    826 		}
    827 		sc->sc_ep[SMSC_ENDPT_TX] = NULL;
    828 	}
    829 
    830 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
    831 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
    832 		if (err) {
    833 			printf("%s: close intr pipe failed: %s\n",
    834 			    device_xname(sc->sc_dev), usbd_errstr(err));
    835 		}
    836 		sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
    837 	}
    838 
    839 	ifp->if_timer = 0;
    840 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    841 
    842 	if (disable) {
    843 		/* drain */
    844 	}
    845 }
    846 
    847 int
    848 smsc_chip_init(struct smsc_softc *sc)
    849 {
    850 	int err;
    851 	uint32_t reg_val;
    852 	int burst_cap;
    853 
    854 	/* Enter H/W config mode */
    855 	smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
    856 
    857 	if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
    858 	    SMSC_HW_CFG_LRST)) != 0) {
    859 		smsc_warn_printf(sc, "timed-out waiting for reset to "
    860 		    "complete\n");
    861 		goto init_failed;
    862 	}
    863 
    864 	/* Reset the PHY */
    865 	smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
    866 
    867 	if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
    868 	    SMSC_PM_CTRL_PHY_RST)) != 0) {
    869 		smsc_warn_printf(sc, "timed-out waiting for phy reset to "
    870 		    "complete\n");
    871 		goto init_failed;
    872 	}
    873 	usbd_delay_ms(sc->sc_udev, 40);
    874 
    875 	/* Set the mac address */
    876 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    877 	const char *eaddr = CLLADDR(ifp->if_sadl);
    878 	if ((err = smsc_setmacaddress(sc, eaddr)) != 0) {
    879 		smsc_warn_printf(sc, "failed to set the MAC address\n");
    880 		goto init_failed;
    881 	}
    882 
    883 	/*
    884 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
    885 	 * sequence as used in the Linux driver.
    886 	 */
    887 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
    888 		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
    889 		goto init_failed;
    890 	}
    891 	reg_val |= SMSC_HW_CFG_BIR;
    892 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    893 
    894 	/*
    895 	 * There is a so called 'turbo mode' that the linux driver supports, it
    896 	 * seems to allow you to jam multiple frames per Rx transaction.
    897 	 * By default this driver supports that and therefore allows multiple
    898 	 * frames per USB transfer.
    899 	 *
    900 	 * The xfer buffer size needs to reflect this as well, therefore based
    901 	 * on the calculations in the Linux driver the RX bufsize is set to
    902 	 * 18944,
    903 	 *     bufsz = (16 * 1024 + 5 * 512)
    904 	 *
    905 	 * Burst capability is the number of URBs that can be in a burst of
    906 	 * data/ethernet frames.
    907 	 */
    908 
    909 	if (sc->sc_udev->ud_speed == USB_SPEED_HIGH)
    910 		burst_cap = 37;
    911 	else
    912 		burst_cap = 128;
    913 
    914 	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
    915 
    916 	/* Set the default bulk in delay (magic value from Linux driver) */
    917 	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
    918 
    919 	/*
    920 	 * Initialise the RX interface
    921 	 */
    922 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
    923 		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
    924 		    err);
    925 		goto init_failed;
    926 	}
    927 
    928 	/*
    929 	 * The following settings are used for 'turbo mode', a.k.a multiple
    930 	 * frames per Rx transaction (again info taken form Linux driver).
    931 	 */
    932 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
    933 
    934 	/*
    935 	 * set Rx data offset to ETHER_ALIGN which will make the IP header
    936 	 * align on a word boundary.
    937 	 */
    938 	reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;
    939 
    940 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    941 
    942 	/* Clear the status register ? */
    943 	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
    944 
    945 	/* Read and display the revision register */
    946 	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
    947 		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
    948 		goto init_failed;
    949 	}
    950 
    951 	/* GPIO/LED setup */
    952 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
    953 	    SMSC_LED_GPIO_CFG_FDX_LED;
    954 	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
    955 
    956 	/*
    957 	 * Initialise the TX interface
    958 	 */
    959 	smsc_write_reg(sc, SMSC_FLOW, 0);
    960 
    961 	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
    962 
    963 	/* Read the current MAC configuration */
    964 	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
    965 		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
    966 		goto init_failed;
    967 	}
    968 
    969 	/* disable pad stripping, collides with checksum offload */
    970 	sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;
    971 
    972 	/* Vlan */
    973 	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
    974 
    975 	/*
    976 	 * Start TX
    977 	 */
    978 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
    979 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    980 	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
    981 
    982 	/*
    983 	 * Start RX
    984 	 */
    985 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
    986 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    987 
    988 	return 0;
    989 
    990 init_failed:
    991 	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
    992 	return err;
    993 }
    994 
    995 static int
    996 smsc_ifflags_cb(struct ethercom *ec)
    997 {
    998 	struct ifnet *ifp = &ec->ec_if;
    999 	struct smsc_softc *sc = ifp->if_softc;
   1000 
   1001 	mutex_enter(&sc->sc_lock);
   1002 
   1003 	const int change = ifp->if_flags ^ sc->sc_if_flags;
   1004 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   1005 		mutex_exit(&sc->sc_lock);
   1006 		return ENETRESET;
   1007 	}
   1008 
   1009 	smsc_dbg_printf(sc, "%s: change %x\n", __func__, change);
   1010 
   1011 	if ((change & IFF_PROMISC) != 0) {
   1012 		if (ifp->if_flags & IFF_PROMISC) {
   1013 			sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
   1014 			smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
   1015 		} else 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 		}
   1019 		smsc_setmulti(sc);
   1020 	}
   1021 
   1022 	mutex_exit(&sc->sc_lock);
   1023 
   1024 	return 0;
   1025 }
   1026 
   1027 
   1028 int
   1029 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1030 {
   1031 	struct smsc_softc * const sc = ifp->if_softc;
   1032 
   1033 	smsc_dbg_printf(sc, "%s: cmd %0lx data %p\n", __func__, cmd, data);
   1034 
   1035 	int error = ether_ioctl(ifp, cmd, data);
   1036 
   1037 	if (error == ENETRESET) {
   1038 		error = 0;
   1039 		if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
   1040 			if (ifp->if_flags & IFF_RUNNING) {
   1041 				mutex_enter(&sc->sc_lock);
   1042 				smsc_setmulti(sc);
   1043 				mutex_exit(&sc->sc_lock);
   1044 			}
   1045 		}
   1046 	}
   1047 
   1048 	mutex_enter(&sc->sc_rxlock);
   1049 	mutex_enter(&sc->sc_txlock);
   1050 	sc->sc_if_flags = ifp->if_flags;
   1051 	mutex_exit(&sc->sc_txlock);
   1052 	mutex_exit(&sc->sc_rxlock);
   1053 
   1054 	return error;
   1055 }
   1056 
   1057 int
   1058 smsc_match(device_t parent, cfdata_t match, void *aux)
   1059 {
   1060 	struct usb_attach_arg *uaa = aux;
   1061 
   1062 	return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
   1063 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
   1064 }
   1065 
   1066 void
   1067 smsc_attach(device_t parent, device_t self, void *aux)
   1068 {
   1069 	struct smsc_softc *sc = device_private(self);
   1070 	struct usb_attach_arg *uaa = aux;
   1071 	struct usbd_device *dev = uaa->uaa_device;
   1072 	usb_interface_descriptor_t *id;
   1073 	usb_endpoint_descriptor_t *ed;
   1074 	char *devinfop;
   1075 	struct mii_data *mii;
   1076 	struct ifnet *ifp;
   1077 	int err, i;
   1078 	uint32_t mac_h, mac_l;
   1079 
   1080 	sc->sc_dev = self;
   1081 	sc->sc_udev = dev;
   1082 	sc->sc_stopping = false;
   1083 
   1084 	aprint_naive("\n");
   1085 	aprint_normal("\n");
   1086 
   1087 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
   1088 	aprint_normal_dev(self, "%s\n", devinfop);
   1089 	usbd_devinfo_free(devinfop);
   1090 
   1091 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
   1092 	if (err) {
   1093 		aprint_error_dev(self, "failed to set configuration"
   1094 		    ", err=%s\n", usbd_errstr(err));
   1095 		return;
   1096 	}
   1097 
   1098 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
   1099 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
   1100 	if (err) {
   1101 		aprint_error_dev(self, "getting interface handle failed\n");
   1102 		return;
   1103 	}
   1104 
   1105 	id = usbd_get_interface_descriptor(sc->sc_iface);
   1106 
   1107 	if (sc->sc_udev->ud_speed >= USB_SPEED_HIGH)
   1108 		sc->sc_bufsz = SMSC_MAX_BUFSZ;
   1109 	else
   1110 		sc->sc_bufsz = SMSC_MIN_BUFSZ;
   1111 
   1112 	/* Find endpoints. */
   1113 	for (i = 0; i < id->bNumEndpoints; i++) {
   1114 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
   1115 		if (!ed) {
   1116 			aprint_error_dev(self, "couldn't get ep %d\n", i);
   1117 			return;
   1118 		}
   1119 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1120 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1121 			sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
   1122 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
   1123 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1124 			sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
   1125 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1126 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
   1127 			sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
   1128 		}
   1129 	}
   1130 
   1131 	usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, USB_TASKQ_MPSAFE);
   1132 
   1133 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
   1134 	mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1135 	mutex_init(&sc->sc_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1136 	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
   1137 
   1138 	ifp = &sc->sc_ec.ec_if;
   1139 	ifp->if_softc = sc;
   1140 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
   1141 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1142 	ifp->if_extflags = IFEF_START_MPSAFE;
   1143 	ifp->if_init = smsc_init;
   1144 	ifp->if_ioctl = smsc_ioctl;
   1145 	ifp->if_start = smsc_start;
   1146 	ifp->if_stop = smsc_stop;
   1147 
   1148 #ifdef notyet
   1149 	/*
   1150 	 * We can do TCPv4, and UDPv4 checksums in hardware.
   1151 	 */
   1152 	ifp->if_capabilities |=
   1153 	    /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
   1154 	    /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
   1155 #endif
   1156 
   1157 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
   1158 
   1159 	/* Setup some of the basics */
   1160 	sc->sc_phyno = 1;
   1161 
   1162 	/*
   1163 	 * Attempt to get the mac address, if an EEPROM is not attached this
   1164 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
   1165 	 * address based on urandom.
   1166 	 */
   1167 	memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);
   1168 
   1169 	prop_dictionary_t dict = device_properties(self);
   1170 	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
   1171 
   1172 	if (eaprop != NULL) {
   1173 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
   1174 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
   1175 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
   1176 		    ETHER_ADDR_LEN);
   1177 	} else {
   1178 		/* Check if there is already a MAC address in the register */
   1179 		if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
   1180 		    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
   1181 			sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
   1182 			sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
   1183 			sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
   1184 			sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
   1185 			sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
   1186 			sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
   1187 		}
   1188 	}
   1189 
   1190 	aprint_normal_dev(self, "Ethernet address %s\n",
   1191 	    ether_sprintf(sc->sc_enaddr));
   1192 
   1193 	IFQ_SET_READY(&ifp->if_snd);
   1194 
   1195 	/* Initialize MII/media info. */
   1196 	mii = &sc->sc_mii;
   1197 	mii->mii_ifp = ifp;
   1198 	mii->mii_readreg = smsc_miibus_readreg;
   1199 	mii->mii_writereg = smsc_miibus_writereg;
   1200 	mii->mii_statchg = smsc_miibus_statchg;
   1201 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1202 	sc->sc_ec.ec_mii = mii;
   1203 	ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
   1204 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
   1205 
   1206 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1207 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1208 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1209 	} else
   1210 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1211 
   1212 	callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE);
   1213 
   1214 	if_initialize(ifp);
   1215 	sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if);
   1216 	ether_ifattach(ifp, sc->sc_enaddr);
   1217 	ether_set_ifflags_cb(&sc->sc_ec, smsc_ifflags_cb);
   1218 	if_register(ifp);
   1219 
   1220 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
   1221 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
   1222 
   1223 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
   1224 }
   1225 
   1226 int
   1227 smsc_detach(device_t self, int flags)
   1228 {
   1229 	struct smsc_softc *sc = device_private(self);
   1230 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1231 
   1232 	mutex_enter(&sc->sc_lock);
   1233 	sc->sc_dying = true;
   1234 	mutex_exit(&sc->sc_lock);
   1235 
   1236 	callout_halt(&sc->sc_stat_ch, NULL);
   1237 
   1238 	if (ifp->if_flags & IFF_RUNNING)
   1239 		smsc_stop_locked(ifp, 1);
   1240 
   1241 	/*
   1242 	 * Remove any pending tasks.  They cannot be executing because they run
   1243 	 * in the same thread as detach.
   1244 	 */
   1245 	usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
   1246 
   1247 	mutex_enter(&sc->sc_lock);
   1248 	sc->sc_refcnt--;
   1249 	while (sc->sc_refcnt > 0) {
   1250 		/* Wait for processes to go away */
   1251 		cv_wait(&sc->sc_detachcv, &sc->sc_lock);
   1252 	}
   1253 
   1254 #ifdef DIAGNOSTIC
   1255 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
   1256 	    sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
   1257 	    sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
   1258 		printf("%s: detach has active endpoints\n",
   1259 		    device_xname(sc->sc_dev));
   1260 #endif
   1261 
   1262 	mutex_exit(&sc->sc_lock);
   1263 
   1264 	rnd_detach_source(&sc->sc_rnd_source);
   1265 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1266 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   1267 	if (ifp->if_softc != NULL) {
   1268 		ether_ifdetach(ifp);
   1269 		if_detach(ifp);
   1270 	}
   1271 
   1272 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
   1273 
   1274 	cv_destroy(&sc->sc_detachcv);
   1275 	mutex_destroy(&sc->sc_mii_lock);
   1276 	mutex_destroy(&sc->sc_rxlock);
   1277 	mutex_destroy(&sc->sc_txlock);
   1278 	mutex_destroy(&sc->sc_lock);
   1279 
   1280 	return 0;
   1281 }
   1282 
   1283 void
   1284 smsc_tick_task(void *xsc)
   1285 {
   1286 	struct smsc_softc * const sc = xsc;
   1287 
   1288 	if (sc == NULL)
   1289 		return;
   1290 
   1291 	mutex_enter(&sc->sc_lock);
   1292 
   1293 	if (sc->sc_dying) {
   1294 		mutex_exit(&sc->sc_lock);
   1295 		return;
   1296 	}
   1297 
   1298 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1299 	struct mii_data * const mii = &sc->sc_mii;
   1300 
   1301 	sc->sc_refcnt++;
   1302 	mutex_exit(&sc->sc_lock);
   1303 
   1304 	mii_tick(mii);
   1305 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
   1306 		smsc_miibus_statchg(ifp);
   1307 
   1308 	mutex_enter(&sc->sc_lock);
   1309 	sc->sc_ttpending = false;
   1310 
   1311 	if (--sc->sc_refcnt < 0)
   1312 		cv_broadcast(&sc->sc_detachcv);
   1313 
   1314 	if (sc->sc_dying) {
   1315 		mutex_exit(&sc->sc_lock);
   1316 		return;
   1317 	}
   1318 	callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
   1319 
   1320 	mutex_exit(&sc->sc_lock);
   1321 }
   1322 
   1323 int
   1324 smsc_activate(device_t self, enum devact act)
   1325 {
   1326 	struct smsc_softc *sc = device_private(self);
   1327 
   1328 	switch (act) {
   1329 	case DVACT_DEACTIVATE:
   1330 		if_deactivate(&sc->sc_ec.ec_if);
   1331 
   1332 		mutex_enter(&sc->sc_lock);
   1333 		sc->sc_dying = true;
   1334 
   1335 		mutex_enter(&sc->sc_rxlock);
   1336 		mutex_enter(&sc->sc_txlock);
   1337 		sc->sc_stopping = true;
   1338 		mutex_exit(&sc->sc_txlock);
   1339 		mutex_exit(&sc->sc_rxlock);
   1340 
   1341 		mutex_exit(&sc->sc_lock);
   1342 		return 0;
   1343 	default:
   1344 		return EOPNOTSUPP;
   1345 	}
   1346 	return 0;
   1347 }
   1348 
   1349 void
   1350 smsc_lock_mii(struct smsc_softc *sc)
   1351 {
   1352 
   1353 	mutex_enter(&sc->sc_lock);
   1354 	sc->sc_refcnt++;
   1355 	mutex_exit(&sc->sc_lock);
   1356 
   1357 	mutex_enter(&sc->sc_mii_lock);
   1358 }
   1359 
   1360 void
   1361 smsc_unlock_mii(struct smsc_softc *sc)
   1362 {
   1363 
   1364 	mutex_exit(&sc->sc_mii_lock);
   1365 	mutex_enter(&sc->sc_lock);
   1366 	if (--sc->sc_refcnt < 0)
   1367 		cv_broadcast(&sc->sc_detachcv);
   1368 	mutex_exit(&sc->sc_lock);
   1369 }
   1370 
   1371 void
   1372 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1373 {
   1374 	struct smsc_chain * const c = (struct smsc_chain *)priv;
   1375 	struct smsc_softc * const sc = c->sc_sc;
   1376 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1377 	u_char *buf = c->sc_buf;
   1378 	uint32_t total_len;
   1379 
   1380 	mutex_enter(&sc->sc_rxlock);
   1381 	if (sc->sc_stopping) {
   1382 		smsc_dbg_printf(sc, "%s: stopping\n", __func__);
   1383 		mutex_exit(&sc->sc_rxlock);
   1384 		return;
   1385 	}
   1386 
   1387 	if (!(sc->sc_if_flags & IFF_RUNNING)) {
   1388 		smsc_dbg_printf(sc, "%s: not running\n", __func__);
   1389 		mutex_exit(&sc->sc_rxlock);
   1390 		return;
   1391 	}
   1392 
   1393 	if (status != USBD_NORMAL_COMPLETION) {
   1394 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1395 	    		mutex_exit(&sc->sc_rxlock);
   1396 			return;
   1397 		}
   1398 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
   1399 			printf("%s: usb errors on rx: %s\n",
   1400 			    device_xname(sc->sc_dev), usbd_errstr(status));
   1401 		}
   1402 		if (status == USBD_STALLED)
   1403 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
   1404 		goto done;
   1405 	}
   1406 
   1407 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1408 	smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
   1409 
   1410 	while (total_len != 0) {
   1411 		uint32_t rxhdr;
   1412 		if (total_len < sizeof(rxhdr)) {
   1413 			smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n",
   1414 			    total_len, sizeof(rxhdr));
   1415 			ifp->if_ierrors++;
   1416 			goto done;
   1417 		}
   1418 
   1419 		memcpy(&rxhdr, buf, sizeof(rxhdr));
   1420 		rxhdr = le32toh(rxhdr);
   1421 		buf += sizeof(rxhdr);
   1422 		total_len -= sizeof(rxhdr);
   1423 
   1424 		if (rxhdr & SMSC_RX_STAT_COLLISION)
   1425 			ifp->if_collisions++;
   1426 
   1427 		if (rxhdr & (SMSC_RX_STAT_ERROR
   1428 		           | SMSC_RX_STAT_LENGTH_ERROR
   1429 		           | SMSC_RX_STAT_MII_ERROR)) {
   1430 			smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
   1431 			ifp->if_ierrors++;
   1432 			goto done;
   1433 		}
   1434 
   1435 		uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
   1436 		smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
   1437 		    "0x%08x\n", total_len, pktlen, rxhdr);
   1438 
   1439 		if (pktlen < ETHER_HDR_LEN) {
   1440 			smsc_dbg_printf(sc, "pktlen %d < ETHER_HDR_LEN %d\n",
   1441 			    pktlen, ETHER_HDR_LEN);
   1442 			ifp->if_ierrors++;
   1443 			goto done;
   1444 		}
   1445 
   1446 		pktlen += ETHER_ALIGN;
   1447 
   1448 		if (pktlen > MCLBYTES) {
   1449 			smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n",
   1450 			    pktlen, MCLBYTES);
   1451 			ifp->if_ierrors++;
   1452 			goto done;
   1453 		}
   1454 
   1455 		if (pktlen > total_len) {
   1456 			smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
   1457 			    pktlen, total_len);
   1458 			ifp->if_ierrors++;
   1459 			goto done;
   1460 		}
   1461 
   1462 		struct mbuf *m = smsc_newbuf();
   1463 		if (m == NULL) {
   1464 			smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
   1465 			ifp->if_ierrors++;
   1466 			goto done;
   1467 		}
   1468 
   1469 		m_set_rcvif(m, ifp);
   1470 		m->m_pkthdr.len = m->m_len = pktlen;
   1471 		m->m_flags |= M_HASFCS;
   1472 		m_adj(m, ETHER_ALIGN);
   1473 
   1474 		KASSERT(m->m_len < MCLBYTES);
   1475 		memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len);
   1476 
   1477 		/* Check if RX TCP/UDP checksumming is being offloaded */
   1478 		if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
   1479 			smsc_dbg_printf(sc,"RX checksum offload checking\n");
   1480 			struct ether_header *eh;
   1481 
   1482 			eh = mtod(m, struct ether_header *);
   1483 
   1484 			/* Remove the extra 2 bytes of the csum */
   1485 			m_adj(m, -2);
   1486 
   1487 			/*
   1488 			 * The checksum appears to be simplistically calculated
   1489 			 * over the udp/tcp header and data up to the end of the
   1490 			 * eth frame.  Which means if the eth frame is padded
   1491 			 * the csum calculation is incorrectly performed over
   1492 			 * the padding bytes as well. Therefore to be safe we
   1493 			 * ignore the H/W csum on frames less than or equal to
   1494 			 * 64 bytes.
   1495 			 *
   1496 			 * Ignore H/W csum for non-IPv4 packets.
   1497 			 */
   1498 			smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n",
   1499 			    be16toh(eh->ether_type), pktlen);
   1500 			if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
   1501 			    pktlen > ETHER_MIN_LEN) {
   1502 
   1503 				m->m_pkthdr.csum_flags |=
   1504 				    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);
   1505 
   1506 				/*
   1507 				 * Copy the TCP/UDP checksum from the last 2
   1508 				 * bytes of the transfer and put in the
   1509 				 * csum_data field.
   1510 				 */
   1511 				memcpy(&m->m_pkthdr.csum_data,
   1512 				    buf + pktlen - 2, 2);
   1513 				/*
   1514 				 * The data is copied in network order, but the
   1515 				 * csum algorithm in the kernel expects it to be
   1516 				 * in host network order.
   1517 				 */
   1518 				m->m_pkthdr.csum_data =
   1519 				    ntohs(m->m_pkthdr.csum_data);
   1520 				smsc_dbg_printf(sc,
   1521 				    "RX checksum offloaded (0x%04x)\n",
   1522 				    m->m_pkthdr.csum_data);
   1523 			}
   1524 		}
   1525 
   1526 		/* round up to next longword */
   1527 		pktlen = (pktlen + 3) & ~0x3;
   1528 
   1529 		/* total_len does not include the padding */
   1530 		if (pktlen > total_len)
   1531 			pktlen = total_len;
   1532 
   1533 		buf += pktlen;
   1534 		total_len -= pktlen;
   1535 
   1536 		mutex_exit(&sc->sc_rxlock);
   1537 
   1538 		/* push the packet up */
   1539 		if_percpuq_enqueue(sc->sc_ipq, m);
   1540 
   1541 		mutex_enter(&sc->sc_rxlock);
   1542 		if (sc->sc_stopping) {
   1543 			smsc_dbg_printf(sc, "%s: stopping\n", __func__);
   1544 			mutex_exit(&sc->sc_rxlock);
   1545 			return;
   1546 		}
   1547 	}
   1548 
   1549 done:
   1550 	mutex_exit(&sc->sc_rxlock);
   1551 
   1552 	/* Setup new transfer. */
   1553 	usbd_setup_xfer(xfer, c, c->sc_buf, sc->sc_bufsz, USBD_SHORT_XFER_OK,
   1554 	    USBD_NO_TIMEOUT, smsc_rxeof);
   1555 	usbd_transfer(xfer);
   1556 
   1557 	return;
   1558 }
   1559 
   1560 void
   1561 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1562 {
   1563 	struct smsc_chain *c = priv;
   1564 	struct smsc_softc *sc = c->sc_sc;
   1565 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1566 
   1567 	mutex_enter(&sc->sc_txlock);
   1568 	if (sc->sc_stopping) {
   1569 		smsc_dbg_printf(sc, "%s: stopping\n", __func__);
   1570 		mutex_exit(&sc->sc_txlock);
   1571 		return;
   1572 	}
   1573 
   1574 	sc->sc_cdata.tx_free++;
   1575 	ifp->if_timer = 0;
   1576 	ifp->if_flags &= ~IFF_OACTIVE;
   1577 
   1578 	if (status != USBD_NORMAL_COMPLETION) {
   1579 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1580 			mutex_exit(&sc->sc_txlock);
   1581 			return;
   1582 		}
   1583 		ifp->if_oerrors++;
   1584 		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
   1585 		    usbd_errstr(status));
   1586 		if (status == USBD_STALLED)
   1587 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
   1588 		mutex_exit(&sc->sc_txlock);
   1589 		return;
   1590 	}
   1591 	ifp->if_opackets++;
   1592 
   1593 	m_freem(c->sc_mbuf);
   1594 	c->sc_mbuf = NULL;
   1595 
   1596 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1597 		smsc_start_locked(ifp);
   1598 
   1599 	mutex_exit(&sc->sc_txlock);
   1600 }
   1601 
   1602 int
   1603 smsc_tx_list_init(struct smsc_softc *sc)
   1604 {
   1605 	struct smsc_cdata *cd = &sc->sc_cdata;
   1606 	struct smsc_chain *c;
   1607 	int i;
   1608 
   1609 	for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
   1610 		c = &cd->tx_chain[i];
   1611 		c->sc_sc = sc;
   1612 		c->sc_idx = i;
   1613 		c->sc_mbuf = NULL;
   1614 		if (c->sc_xfer == NULL) {
   1615 			int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_TX],
   1616 			    sc->sc_bufsz, USBD_FORCE_SHORT_XFER, 0,
   1617 			    &c->sc_xfer);
   1618 			if (error)
   1619 				return EIO;
   1620 			c->sc_buf = usbd_get_buffer(c->sc_xfer);
   1621 		}
   1622 	}
   1623 
   1624 	cd->tx_free = SMSC_TX_LIST_CNT;
   1625 	cd->tx_next = 0;
   1626 
   1627 	return 0;
   1628 }
   1629 
   1630 void
   1631 smsc_tx_list_free(struct smsc_softc *sc)
   1632 {
   1633 	/* Free TX resources. */
   1634 	for (size_t i = 0; i < SMSC_TX_LIST_CNT; i++) {
   1635 		if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
   1636 			m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
   1637 			sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
   1638 		}
   1639 		if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
   1640 			usbd_destroy_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
   1641 			sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
   1642 		}
   1643 	}
   1644 }
   1645 
   1646 int
   1647 smsc_rx_list_init(struct smsc_softc *sc)
   1648 {
   1649 	struct smsc_cdata *cd = &sc->sc_cdata;
   1650 	struct smsc_chain *c;
   1651 	int i;
   1652 
   1653 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
   1654 		c = &cd->rx_chain[i];
   1655 		c->sc_sc = sc;
   1656 		c->sc_idx = i;
   1657 		c->sc_mbuf = NULL;
   1658 		if (c->sc_xfer == NULL) {
   1659 			int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_RX],
   1660 			    sc->sc_bufsz, USBD_SHORT_XFER_OK, 0, &c->sc_xfer);
   1661 			if (error)
   1662 				return error;
   1663 			c->sc_buf = usbd_get_buffer(c->sc_xfer);
   1664 		}
   1665 	}
   1666 
   1667 	return 0;
   1668 }
   1669 
   1670 void
   1671 smsc_rx_list_free(struct smsc_softc *sc)
   1672 {
   1673 	/* Free RX resources. */
   1674 	for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) {
   1675 		if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
   1676 			m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
   1677 			sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
   1678 		}
   1679 		if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
   1680 			usbd_destroy_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
   1681 			sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
   1682 		}
   1683 	}
   1684 }
   1685 
   1686 struct mbuf *
   1687 smsc_newbuf(void)
   1688 {
   1689 	struct mbuf *m;
   1690 
   1691 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1692 	if (m == NULL)
   1693 		return NULL;
   1694 
   1695 	MCLGET(m, M_DONTWAIT);
   1696 	if (!(m->m_flags & M_EXT)) {
   1697 		m_freem(m);
   1698 		return NULL;
   1699 	}
   1700 
   1701 	return m;
   1702 }
   1703 
   1704 int
   1705 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
   1706 {
   1707 	struct smsc_chain * const c = &sc->sc_cdata.tx_chain[idx];
   1708 	uint32_t txhdr;
   1709 	uint32_t frm_len = 0;
   1710 
   1711 	/*
   1712 	 * Each frame is prefixed with two 32-bit values describing the
   1713 	 * length of the packet and buffer.
   1714 	 */
   1715 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
   1716 	    SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
   1717 	txhdr = htole32(txhdr);
   1718 	memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
   1719 
   1720 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
   1721 	txhdr = htole32(txhdr);
   1722 	memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
   1723 
   1724 	frm_len += 8;
   1725 
   1726 	/* Next copy in the actual packet */
   1727 	m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
   1728 	frm_len += m->m_pkthdr.len;
   1729 
   1730 	c->sc_mbuf = m;
   1731 
   1732 	usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, frm_len,
   1733 	    USBD_FORCE_SHORT_XFER, 10000, smsc_txeof);
   1734 
   1735 	usbd_status err = usbd_transfer(c->sc_xfer);
   1736 	if (err != USBD_IN_PROGRESS) {
   1737 		return EIO;
   1738 	}
   1739 
   1740 	return 0;
   1741 }
   1742