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