Home | History | Annotate | Line # | Download | only in usb
if_smsc.c revision 1.7.2.2
      1 /*	$NetBSD: if_smsc.c,v 1.7.2.2 2013/02/13 01:36:16 riz 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 "vlan.h"
     64 #include "opt_usb.h"
     65 
     66 #include <sys/param.h>
     67 #include <sys/bus.h>
     68 #include <sys/systm.h>
     69 #include <sys/sockio.h>
     70 #include <sys/mbuf.h>
     71 #include <sys/mutex.h>
     72 #include <sys/kernel.h>
     73 #include <sys/proc.h>
     74 #include <sys/socket.h>
     75 
     76 #include <sys/device.h>
     77 
     78 #include <sys/rnd.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/in_systm.h>
     90 #include <netinet/in_var.h>
     91 #include <netinet/ip.h>
     92 #include <netinet/if_ether.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 void		 smsc_start(struct ifnet *);
    165 int		 smsc_ioctl(struct ifnet *, u_long, void *);
    166 void		 smsc_stop(struct ifnet *, int);
    167 
    168 void		 smsc_reset(struct smsc_softc *);
    169 struct mbuf	*smsc_newbuf(void);
    170 
    171 void		 smsc_tick(void *);
    172 void		 smsc_tick_task(void *);
    173 void		 smsc_miibus_statchg(device_t);
    174 int		 smsc_miibus_readreg(device_t, int, int);
    175 void		 smsc_miibus_writereg(device_t, int, int, int);
    176 int		 smsc_ifmedia_upd(struct ifnet *);
    177 void		 smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    178 void		 smsc_lock_mii(struct smsc_softc *);
    179 void		 smsc_unlock_mii(struct smsc_softc *);
    180 
    181 int		 smsc_tx_list_init(struct smsc_softc *);
    182 int		 smsc_rx_list_init(struct smsc_softc *);
    183 int		 smsc_encap(struct smsc_softc *, struct mbuf *, int);
    184 void		 smsc_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    185 void		 smsc_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    186 
    187 int		 smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
    188 int		 smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
    189 int		 smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
    190 int		 smsc_sethwcsum(struct smsc_softc *);
    191 
    192 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach,
    193     smsc_detach, smsc_activate);
    194 
    195 int
    196 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
    197 {
    198 	usb_device_request_t req;
    199 	uint32_t buf;
    200 	usbd_status err;
    201 
    202 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    203 	req.bRequest = SMSC_UR_READ_REG;
    204 	USETW(req.wValue, 0);
    205 	USETW(req.wIndex, off);
    206 	USETW(req.wLength, 4);
    207 
    208 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    209 	if (err != 0)
    210 		smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
    211 
    212 	*data = le32toh(buf);
    213 
    214 	return (err);
    215 }
    216 
    217 int
    218 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
    219 {
    220 	usb_device_request_t req;
    221 	uint32_t buf;
    222 	usbd_status err;
    223 
    224 	buf = htole32(data);
    225 
    226 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    227 	req.bRequest = SMSC_UR_WRITE_REG;
    228 	USETW(req.wValue, 0);
    229 	USETW(req.wIndex, off);
    230 	USETW(req.wLength, 4);
    231 
    232 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    233 	if (err != 0)
    234 		smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
    235 
    236 	return (err);
    237 }
    238 
    239 int
    240 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
    241 {
    242 	uint32_t val;
    243 	int err, i;
    244 
    245 	for (i = 0; i < 100; i++) {
    246 		if ((err = smsc_read_reg(sc, reg, &val)) != 0)
    247 			return (err);
    248 		if (!(val & bits))
    249 			return (0);
    250 		DELAY(5);
    251 	}
    252 
    253 	return (1);
    254 }
    255 
    256 int
    257 smsc_miibus_readreg(device_t dev, int phy, int reg)
    258 {
    259 	struct smsc_softc *sc = device_private(dev);
    260 	uint32_t addr;
    261 	uint32_t val = 0;
    262 
    263 	smsc_lock_mii(sc);
    264 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    265 		smsc_warn_printf(sc, "MII is busy\n");
    266 		goto done;
    267 	}
    268 
    269 	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
    270 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
    271 
    272 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
    273 		smsc_warn_printf(sc, "MII read timeout\n");
    274 
    275 	smsc_read_reg(sc, SMSC_MII_DATA, &val);
    276 
    277 done:
    278 	smsc_unlock_mii(sc);
    279 
    280 	return (val & 0xFFFF);
    281 }
    282 
    283 void
    284 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
    285 {
    286 	struct smsc_softc *sc = device_private(dev);
    287 	uint32_t addr;
    288 
    289 	if (sc->sc_phyno != phy)
    290 		return;
    291 
    292 	smsc_lock_mii(sc);
    293 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    294 		smsc_warn_printf(sc, "MII is busy\n");
    295 		smsc_unlock_mii(sc);
    296 		return;
    297 	}
    298 
    299 	smsc_write_reg(sc, SMSC_MII_DATA, val);
    300 
    301 	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
    302 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
    303 	smsc_unlock_mii(sc);
    304 
    305 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
    306 		smsc_warn_printf(sc, "MII write timeout\n");
    307 }
    308 
    309 void
    310 smsc_miibus_statchg(device_t dev)
    311 {
    312 	struct smsc_softc *sc = device_private(dev);
    313 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    314 	struct mii_data *mii = &sc->sc_mii;
    315 	int err;
    316 	uint32_t flow;
    317 	uint32_t afc_cfg;
    318 
    319 	if (mii == NULL || ifp == NULL ||
    320 	    (ifp->if_flags & IFF_RUNNING) == 0)
    321 		return;
    322 
    323 	/* Use the MII status to determine link status */
    324 	sc->sc_flags &= ~SMSC_FLAG_LINK;
    325 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    326 	    (IFM_ACTIVE | IFM_AVALID)) {
    327 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    328 			case IFM_10_T:
    329 			case IFM_100_TX:
    330 				sc->sc_flags |= SMSC_FLAG_LINK;
    331 				break;
    332 			case IFM_1000_T:
    333 				/* Gigabit ethernet not supported by chipset */
    334 				break;
    335 			default:
    336 				break;
    337 		}
    338 	}
    339 
    340 	/* Lost link, do nothing. */
    341 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
    342 		smsc_dbg_printf(sc, "link flag not set\n");
    343 		return;
    344 	}
    345 
    346 	err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
    347 	if (err) {
    348 		smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
    349 		    "error %d\n", err);
    350 		return;
    351 	}
    352 
    353 	/* Enable/disable full duplex operation and TX/RX pause */
    354 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
    355 		smsc_dbg_printf(sc, "full duplex operation\n");
    356 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
    357 		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
    358 
    359 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
    360 			flow = 0xffff0002;
    361 		else
    362 			flow = 0;
    363 
    364 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
    365 			afc_cfg |= 0xf;
    366 		else
    367 			afc_cfg &= ~0xf;
    368 
    369 	} else {
    370 		smsc_dbg_printf(sc, "half duplex operation\n");
    371 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
    372 		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
    373 
    374 		flow = 0;
    375 		afc_cfg |= 0xf;
    376 	}
    377 
    378 	err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    379 	err += smsc_write_reg(sc, SMSC_FLOW, flow);
    380 	err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
    381 	if (err)
    382 		smsc_warn_printf(sc, "media change failed, error %d\n", err);
    383 }
    384 
    385 int
    386 smsc_ifmedia_upd(struct ifnet *ifp)
    387 {
    388 	struct smsc_softc *sc = ifp->if_softc;
    389 	struct mii_data *mii = &sc->sc_mii;
    390 	int err;
    391 
    392 	if (mii->mii_instance) {
    393 		struct mii_softc *miisc;
    394 
    395 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    396 			mii_phy_reset(miisc);
    397 	}
    398 	err = mii_mediachg(mii);
    399 	return (err);
    400 }
    401 
    402 void
    403 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    404 {
    405 	struct smsc_softc *sc = ifp->if_softc;
    406 	struct mii_data *mii = &sc->sc_mii;
    407 
    408 	mii_pollstat(mii);
    409 
    410 	ifmr->ifm_active = mii->mii_media_active;
    411 	ifmr->ifm_status = mii->mii_media_status;
    412 }
    413 
    414 static inline uint32_t
    415 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
    416 {
    417 	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
    418 }
    419 
    420 void
    421 smsc_setmulti(struct smsc_softc *sc)
    422 {
    423 	struct ifnet		*ifp = &sc->sc_ec.ec_if;
    424 	struct ether_multi	*enm;
    425 	struct ether_multistep	 step;
    426 	uint32_t		 hashtbl[2] = { 0, 0 };
    427 	uint32_t		 hash;
    428 
    429 	if (sc->sc_dying)
    430 		return;
    431 
    432 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
    433 allmulti:
    434 		smsc_dbg_printf(sc, "receive all multicast enabled\n");
    435 		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
    436 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
    437 		smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    438 		return;
    439 	} else {
    440 		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
    441 		sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
    442 	}
    443 
    444 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    445 	while (enm != NULL) {
    446 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    447 		    ETHER_ADDR_LEN) != 0)
    448 			goto allmulti;
    449 
    450 		hash = smsc_hash(enm->enm_addrlo);
    451 		hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
    452 		ETHER_NEXT_MULTI(step, enm);
    453 	}
    454 
    455 	/* Debug */
    456 	if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
    457 		smsc_dbg_printf(sc, "receive select group of macs\n");
    458 	} else {
    459 		smsc_dbg_printf(sc, "receive own packets only\n");
    460 	}
    461 
    462 	/* Write the hash table and mac control registers */
    463 	ifp->if_flags &= ~IFF_ALLMULTI;
    464 	smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
    465 	smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
    466 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    467 }
    468 
    469 int
    470 smsc_sethwcsum(struct smsc_softc *sc)
    471 {
    472 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    473 	uint32_t val;
    474 	int err;
    475 
    476 	if (!ifp)
    477 		return EIO;
    478 
    479 	err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
    480 	if (err != 0) {
    481 		smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
    482 		    err);
    483 		return (err);
    484 	}
    485 
    486 	/* Enable/disable the Rx checksum */
    487 	if (ifp->if_capabilities & IFCAP_CSUM_IPv4_Rx)
    488 		val |= SMSC_COE_CTRL_RX_EN;
    489 	else
    490 		val &= ~SMSC_COE_CTRL_RX_EN;
    491 
    492 	/* Enable/disable the Tx checksum (currently not supported) */
    493 	if (ifp->if_capabilities & IFCAP_CSUM_IPv4_Tx)
    494 		val |= SMSC_COE_CTRL_TX_EN;
    495 	else
    496 		val &= ~SMSC_COE_CTRL_TX_EN;
    497 
    498 	err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
    499 	if (err != 0) {
    500 		smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n",
    501 		    err);
    502 		return (err);
    503 	}
    504 
    505 	return (0);
    506 }
    507 
    508 int
    509 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
    510 {
    511 	int err;
    512 	uint32_t val;
    513 
    514 	smsc_dbg_printf(sc, "setting mac address to "
    515 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
    516 	    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
    517 
    518 	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
    519 	if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
    520 		goto done;
    521 
    522 	val = (addr[5] << 8) | addr[4];
    523 	err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
    524 
    525 done:
    526 	return (err);
    527 }
    528 
    529 void
    530 smsc_reset(struct smsc_softc *sc)
    531 {
    532 	if (sc->sc_dying)
    533 		return;
    534 
    535 	/* Wait a little while for the chip to get its brains in order. */
    536 	DELAY(1000);
    537 
    538 	/* Reinitialize controller to achieve full reset. */
    539 	smsc_chip_init(sc);
    540 }
    541 
    542 int
    543 smsc_init(struct ifnet *ifp)
    544 {
    545 	struct smsc_softc	*sc = ifp->if_softc;
    546 	struct smsc_chain	*c;
    547 	usbd_status		 err;
    548 	int			 s, i;
    549 
    550 	if (sc->sc_dying)
    551 		return EIO;
    552 
    553 	s = splnet();
    554 
    555 	/* Cancel pending I/O */
    556 	if (ifp->if_flags & IFF_RUNNING)
    557 		smsc_stop(ifp, 1);
    558 
    559 	/* Reset the ethernet interface. */
    560 	smsc_reset(sc);
    561 
    562 	/* Init RX ring. */
    563 	if (smsc_rx_list_init(sc) == ENOBUFS) {
    564 		aprint_error_dev(sc->sc_dev, "rx list init failed\n");
    565 		splx(s);
    566 		return EIO;
    567 	}
    568 
    569 	/* Init TX ring. */
    570 	if (smsc_tx_list_init(sc) == ENOBUFS) {
    571 		aprint_error_dev(sc->sc_dev, "tx list init failed\n");
    572 		splx(s);
    573 		return EIO;
    574 	}
    575 
    576 	/* Load the multicast filter. */
    577 	smsc_setmulti(sc);
    578 
    579 	/* Open RX and TX pipes. */
    580 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
    581 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]);
    582 	if (err) {
    583 		printf("%s: open rx pipe failed: %s\n",
    584 		    device_xname(sc->sc_dev), usbd_errstr(err));
    585 		splx(s);
    586 		return EIO;
    587 	}
    588 
    589 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
    590 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]);
    591 	if (err) {
    592 		printf("%s: open tx pipe failed: %s\n",
    593 		    device_xname(sc->sc_dev), usbd_errstr(err));
    594 		splx(s);
    595 		return EIO;
    596 	}
    597 
    598 	/* Start up the receive pipe. */
    599 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
    600 		c = &sc->sc_cdata.rx_chain[i];
    601 		usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX],
    602 		    c, c->sc_buf, sc->sc_bufsz,
    603 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
    604 		    USBD_NO_TIMEOUT, smsc_rxeof);
    605 		usbd_transfer(c->sc_xfer);
    606 	}
    607 
    608 	/* TCP/UDP checksum offload engines. */
    609 	smsc_sethwcsum(sc);
    610 
    611 	/* Indicate we are up and running. */
    612 	ifp->if_flags |= IFF_RUNNING;
    613 	ifp->if_flags &= ~IFF_OACTIVE;
    614 
    615 	splx(s);
    616 
    617 	callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
    618 
    619 	return 0;
    620 }
    621 
    622 void
    623 smsc_start(struct ifnet *ifp)
    624 {
    625 	struct smsc_softc	*sc = ifp->if_softc;
    626 	struct mbuf		*m_head = NULL;
    627 
    628 	/* Don't send anything if there is no link or controller is busy. */
    629 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
    630 		return;
    631 	}
    632 
    633 	if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
    634 		return;
    635 
    636 	IFQ_POLL(&ifp->if_snd, m_head);
    637 	if (m_head == NULL)
    638 		return;
    639 
    640 	if (smsc_encap(sc, m_head, 0)) {
    641 		ifp->if_flags |= IFF_OACTIVE;
    642 		return;
    643 	}
    644 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    645 
    646 	bpf_mtap(ifp, m_head);
    647 
    648 	ifp->if_flags |= IFF_OACTIVE;
    649 
    650 	/*
    651 	 * Set a timeout in case the chip goes out to lunch.
    652 	 */
    653 	ifp->if_timer = 5;
    654 }
    655 
    656 void
    657 smsc_tick(void *xsc)
    658 {
    659 	struct smsc_softc *sc = xsc;
    660 
    661 	if (sc == NULL)
    662 		return;
    663 
    664 	if (sc->sc_dying)
    665 		return;
    666 
    667 	usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
    668 }
    669 
    670 void
    671 smsc_stop(struct ifnet *ifp, int disable)
    672 {
    673 	usbd_status		err;
    674 	struct smsc_softc	*sc = ifp->if_softc;
    675 	int			i;
    676 
    677 	smsc_reset(sc);
    678 
    679 	ifp = &sc->sc_ec.ec_if;
    680 	ifp->if_timer = 0;
    681 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    682 
    683 	callout_stop(&sc->sc_stat_ch);
    684 
    685 	/* Stop transfers. */
    686 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
    687 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
    688 		if (err) {
    689 			printf("%s: abort rx pipe failed: %s\n",
    690 			    device_xname(sc->sc_dev), usbd_errstr(err));
    691 		}
    692 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
    693 		if (err) {
    694 			printf("%s: close rx pipe failed: %s\n",
    695 			    device_xname(sc->sc_dev), usbd_errstr(err));
    696 		}
    697 		sc->sc_ep[SMSC_ENDPT_RX] = NULL;
    698 	}
    699 
    700 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
    701 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
    702 		if (err) {
    703 			printf("%s: abort tx pipe failed: %s\n",
    704 			    device_xname(sc->sc_dev), usbd_errstr(err));
    705 		}
    706 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
    707 		if (err) {
    708 			printf("%s: close tx pipe failed: %s\n",
    709 			    device_xname(sc->sc_dev), usbd_errstr(err));
    710 		}
    711 		sc->sc_ep[SMSC_ENDPT_TX] = NULL;
    712 	}
    713 
    714 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
    715 		err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
    716 		if (err) {
    717 			printf("%s: abort intr pipe failed: %s\n",
    718 			    device_xname(sc->sc_dev), usbd_errstr(err));
    719 		}
    720 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
    721 		if (err) {
    722 			printf("%s: close intr pipe failed: %s\n",
    723 			    device_xname(sc->sc_dev), usbd_errstr(err));
    724 		}
    725 		sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
    726 	}
    727 
    728 	/* Free RX resources. */
    729 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
    730 		if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
    731 			m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
    732 			sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
    733 		}
    734 		if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
    735 			usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
    736 			sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
    737 		}
    738 	}
    739 
    740 	/* Free TX resources. */
    741 	for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
    742 		if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
    743 			m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
    744 			sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
    745 		}
    746 		if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
    747 			usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
    748 			sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
    749 		}
    750 	}
    751 }
    752 
    753 int
    754 smsc_chip_init(struct smsc_softc *sc)
    755 {
    756 	int err;
    757 	uint32_t reg_val;
    758 	int burst_cap;
    759 
    760 	/* Enter H/W config mode */
    761 	smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
    762 
    763 	if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
    764 	    SMSC_HW_CFG_LRST)) != 0) {
    765 		smsc_warn_printf(sc, "timed-out waiting for reset to "
    766 		    "complete\n");
    767 		goto init_failed;
    768 	}
    769 
    770 	/* Reset the PHY */
    771 	smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
    772 
    773 	if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
    774 	    SMSC_PM_CTRL_PHY_RST) != 0)) {
    775 		smsc_warn_printf(sc, "timed-out waiting for phy reset to "
    776 		    "complete\n");
    777 		goto init_failed;
    778 	}
    779 	usbd_delay_ms(sc->sc_udev, 40);
    780 
    781 	/* Set the mac address */
    782 	if ((err = smsc_setmacaddress(sc, sc->sc_enaddr)) != 0) {
    783 		smsc_warn_printf(sc, "failed to set the MAC address\n");
    784 		goto init_failed;
    785 	}
    786 
    787 	/*
    788 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
    789 	 * sequence as used in the Linux driver.
    790 	 */
    791 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
    792 		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
    793 		goto init_failed;
    794 	}
    795 	reg_val |= SMSC_HW_CFG_BIR;
    796 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    797 
    798 	/*
    799 	 * There is a so called 'turbo mode' that the linux driver supports, it
    800 	 * seems to allow you to jam multiple frames per Rx transaction.
    801 	 * By default this driver supports that and therefore allows multiple
    802 	 * frames per URB.
    803 	 *
    804 	 * The xfer buffer size needs to reflect this as well, therefore based
    805 	 * on the calculations in the Linux driver the RX bufsize is set to
    806 	 * 18944,
    807 	 *     bufsz = (16 * 1024 + 5 * 512)
    808 	 *
    809 	 * Burst capability is the number of URBs that can be in a burst of
    810 	 * data/ethernet frames.
    811 	 */
    812 #ifdef SMSC_TURBO
    813 	if (sc->sc_udev->speed == USB_SPEED_HIGH)
    814 		burst_cap = 37;
    815 	else
    816 		burst_cap = 128;
    817 #else
    818 	burst_cap = 0;
    819 #endif
    820 
    821 	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
    822 
    823 	/* Set the default bulk in delay (magic value from Linux driver) */
    824 	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
    825 
    826 	/*
    827 	 * Initialise the RX interface
    828 	 */
    829 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
    830 		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
    831 		    err);
    832 		goto init_failed;
    833 	}
    834 
    835 	/*
    836 	 * The following setings are used for 'turbo mode', a.k.a multiple
    837 	 * frames per Rx transaction (again info taken form Linux driver).
    838 	 */
    839 #ifdef SMSC_TURBO
    840 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
    841 #endif
    842 
    843 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    844 
    845 	/* Clear the status register ? */
    846 	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
    847 
    848 	/* Read and display the revision register */
    849 	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
    850 		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
    851 		goto init_failed;
    852 	}
    853 
    854 	/* GPIO/LED setup */
    855 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
    856 	    SMSC_LED_GPIO_CFG_FDX_LED;
    857 	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
    858 
    859 	/*
    860 	 * Initialise the TX interface
    861 	 */
    862 	smsc_write_reg(sc, SMSC_FLOW, 0);
    863 
    864 	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
    865 
    866 	/* Read the current MAC configuration */
    867 	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
    868 		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
    869 		goto init_failed;
    870 	}
    871 
    872 	/* Vlan */
    873 	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
    874 
    875 	/*
    876 	 * Start TX
    877 	 */
    878 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
    879 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    880 	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
    881 
    882 	/*
    883 	 * Start RX
    884 	 */
    885 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
    886 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    887 
    888 	return (0);
    889 
    890 init_failed:
    891 	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
    892 	return (err);
    893 }
    894 
    895 int
    896 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    897 {
    898 	struct smsc_softc	*sc = ifp->if_softc;
    899 	struct ifreq /*const*/	*ifr = data;
    900 	int			s, error = 0;
    901 
    902 	if (sc->sc_dying)
    903 		return EIO;
    904 
    905 	s = splnet();
    906 
    907 	switch(cmd) {
    908 	case SIOCSIFFLAGS:
    909 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    910 			break;
    911 
    912 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
    913 		case IFF_RUNNING:
    914 			smsc_stop(ifp, 1);
    915 			break;
    916 		case IFF_UP:
    917 			smsc_init(ifp);
    918 			break;
    919 		case IFF_UP | IFF_RUNNING:
    920 			if (ifp->if_flags & IFF_PROMISC &&
    921 			    !(sc->sc_if_flags & IFF_PROMISC)) {
    922 				sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
    923 				smsc_write_reg(sc, SMSC_MAC_CSR,
    924 				    sc->sc_mac_csr);
    925 				smsc_setmulti(sc);
    926 			} else if (!(ifp->if_flags & IFF_PROMISC) &&
    927 			    sc->sc_if_flags & IFF_PROMISC) {
    928 				sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
    929 				smsc_write_reg(sc, SMSC_MAC_CSR,
    930 				    sc->sc_mac_csr);
    931 				smsc_setmulti(sc);
    932 			} else {
    933 				smsc_init(ifp);
    934 			}
    935 			break;
    936 		}
    937 		sc->sc_if_flags = ifp->if_flags;
    938 		break;
    939 
    940 	case SIOCGIFMEDIA:
    941 	case SIOCSIFMEDIA:
    942 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    943 		break;
    944 
    945 	default:
    946 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
    947 			break;
    948 
    949 		error = 0;
    950 
    951 		if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)
    952 			smsc_setmulti(sc);
    953 
    954 	}
    955 	splx(s);
    956 
    957 	return error;
    958 }
    959 
    960 int
    961 smsc_match(device_t parent, cfdata_t match, void *aux)
    962 {
    963 	struct usb_attach_arg *uaa = aux;
    964 
    965 	return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ?
    966 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    967 }
    968 
    969 void
    970 smsc_attach(device_t parent, device_t self, void *aux)
    971 {
    972 	struct smsc_softc *sc = device_private(self);
    973 	struct usb_attach_arg *uaa = aux;
    974 	usbd_device_handle dev = uaa->device;
    975 	usb_interface_descriptor_t *id;
    976 	usb_endpoint_descriptor_t *ed;
    977 	char *devinfop;
    978 	struct mii_data *mii;
    979 	struct ifnet *ifp;
    980 	int err, s, i;
    981 	uint32_t mac_h, mac_l;
    982 
    983 	sc->sc_dev = self;
    984 	sc->sc_udev = dev;
    985 
    986 	aprint_naive("\n");
    987 	aprint_normal("\n");
    988 
    989 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    990 	aprint_normal_dev(self, "%s\n", devinfop);
    991 	usbd_devinfo_free(devinfop);
    992 
    993 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
    994 	if (err) {
    995 		aprint_error_dev(self, "failed to set configuration"
    996 		    ", err=%s\n", usbd_errstr(err));
    997 		return;
    998 	}
    999 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
   1000 	usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc);
   1001 	usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc);
   1002 	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
   1003 
   1004 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
   1005 	if (err) {
   1006 		aprint_error_dev(self, "getting interface handle failed\n");
   1007 		return;
   1008 	}
   1009 
   1010 	id = usbd_get_interface_descriptor(sc->sc_iface);
   1011 
   1012 	if (sc->sc_udev->speed >= USB_SPEED_HIGH)
   1013 		sc->sc_bufsz = SMSC_MAX_BUFSZ;
   1014 	else
   1015 		sc->sc_bufsz = SMSC_MIN_BUFSZ;
   1016 
   1017 	/* Find endpoints. */
   1018 	for (i = 0; i < id->bNumEndpoints; i++) {
   1019 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
   1020 		if (!ed) {
   1021 			aprint_error_dev(self, "couldn't get ep %d\n", i);
   1022 			return;
   1023 		}
   1024 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1025 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1026 			sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
   1027 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
   1028 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1029 			sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
   1030 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1031 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
   1032 			sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
   1033 		}
   1034 	}
   1035 
   1036 	s = splnet();
   1037 
   1038 	ifp = &sc->sc_ec.ec_if;
   1039 	ifp->if_softc = sc;
   1040 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
   1041 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1042 	ifp->if_init = smsc_init;
   1043 	ifp->if_ioctl = smsc_ioctl;
   1044 	ifp->if_start = smsc_start;
   1045 	ifp->if_stop = smsc_stop;
   1046 
   1047         sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
   1048 
   1049 	/* Setup some of the basics */
   1050 	sc->sc_phyno = 1;
   1051 
   1052 	/*
   1053 	 * Attempt to get the mac address, if an EEPROM is not attached this
   1054 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
   1055 	 * address based on urandom.
   1056 	 */
   1057 	memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);
   1058 
   1059 	prop_dictionary_t dict = device_properties(self);
   1060 	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
   1061 
   1062 	if (eaprop != NULL) {
   1063 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
   1064 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
   1065 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
   1066 		    ETHER_ADDR_LEN);
   1067 	} else
   1068 	/* Check if there is already a MAC address in the register */
   1069 	if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
   1070 	    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
   1071 		sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
   1072 		sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
   1073 		sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
   1074 		sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
   1075 		sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
   1076 		sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
   1077 	}
   1078 
   1079 	aprint_normal_dev(self, " Ethernet address %s\n", ether_sprintf(sc->sc_enaddr));
   1080 
   1081 	IFQ_SET_READY(&ifp->if_snd);
   1082 
   1083 	/* Initialize MII/media info. */
   1084 	mii = &sc->sc_mii;
   1085 	mii->mii_ifp = ifp;
   1086 	mii->mii_readreg = smsc_miibus_readreg;
   1087 	mii->mii_writereg = smsc_miibus_writereg;
   1088 	mii->mii_statchg = smsc_miibus_statchg;
   1089 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1090 	sc->sc_ec.ec_mii = mii;
   1091 	ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
   1092 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
   1093 
   1094 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1095 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1096 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1097 	} else
   1098 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1099 
   1100 	if_attach(ifp);
   1101 	ether_ifattach(ifp, sc->sc_enaddr);
   1102 
   1103 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
   1104 	    RND_TYPE_NET, 0);
   1105 
   1106 	callout_init(&sc->sc_stat_ch, 0);
   1107 
   1108 	splx(s);
   1109 
   1110 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
   1111 }
   1112 
   1113 int
   1114 smsc_detach(device_t self, int flags)
   1115 {
   1116 	struct smsc_softc *sc = device_private(self);
   1117 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1118 	int s;
   1119 
   1120 	callout_stop(&sc->sc_stat_ch);
   1121 
   1122 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL)
   1123 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
   1124 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL)
   1125 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
   1126 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
   1127 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
   1128 
   1129 	/*
   1130 	 * Remove any pending tasks.  They cannot be executing because they run
   1131 	 * in the same thread as detach.
   1132 	 */
   1133 	usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
   1134 	usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
   1135 
   1136 	s = splusb();
   1137 
   1138 	if (--sc->sc_refcnt >= 0) {
   1139 		/* Wait for processes to go away */
   1140 		usb_detach_wait(sc->sc_dev);
   1141 	}
   1142 
   1143 	if (ifp->if_flags & IFF_RUNNING)
   1144 		smsc_stop(ifp ,1);
   1145 
   1146 	rnd_detach_source(&sc->sc_rnd_source);
   1147 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1148 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   1149 	if (ifp->if_softc != NULL) {
   1150 		ether_ifdetach(ifp);
   1151 		if_detach(ifp);
   1152 	}
   1153 
   1154 #ifdef DIAGNOSTIC
   1155 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
   1156 	    sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
   1157 	    sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
   1158 		printf("%s: detach has active endpoints\n",
   1159 		    device_xname(sc->sc_dev));
   1160 #endif
   1161 
   1162 	if (--sc->sc_refcnt >= 0) {
   1163 		/* Wait for processes to go away. */
   1164 		usb_detach_wait(sc->sc_dev);
   1165 	}
   1166 	splx(s);
   1167 
   1168 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
   1169 
   1170 	mutex_destroy(&sc->sc_mii_lock);
   1171 
   1172 	return (0);
   1173 }
   1174 
   1175 void
   1176 smsc_tick_task(void *xsc)
   1177 {
   1178 	int			 s;
   1179 	struct smsc_softc	*sc = xsc;
   1180 	struct ifnet		*ifp;
   1181 	struct mii_data		*mii;
   1182 
   1183 	if (sc == NULL)
   1184 		return;
   1185 
   1186 	if (sc->sc_dying)
   1187 		return;
   1188 	ifp = &sc->sc_ec.ec_if;
   1189 	mii = &sc->sc_mii;
   1190 	if (mii == NULL)
   1191 		return;
   1192 
   1193 	s = splnet();
   1194 
   1195 	mii_tick(mii);
   1196 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
   1197 		smsc_miibus_statchg(sc->sc_dev);
   1198 	callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
   1199 
   1200 	splx(s);
   1201 }
   1202 
   1203 int
   1204 smsc_activate(device_t self, enum devact act)
   1205 {
   1206 	struct smsc_softc *sc = device_private(self);
   1207 
   1208         switch (act) {
   1209 	case DVACT_DEACTIVATE:
   1210 		if_deactivate(&sc->sc_ec.ec_if);
   1211 		sc->sc_dying = 1;
   1212 		return 0;
   1213 	default:
   1214 		return EOPNOTSUPP;
   1215 	}
   1216 	return (0);
   1217 }
   1218 
   1219 void
   1220 smsc_lock_mii(struct smsc_softc *sc)
   1221 {
   1222 	sc->sc_refcnt++;
   1223 	mutex_enter(&sc->sc_mii_lock);
   1224 }
   1225 
   1226 void
   1227 smsc_unlock_mii(struct smsc_softc *sc)
   1228 {
   1229 	mutex_exit(&sc->sc_mii_lock);
   1230 	if (--sc->sc_refcnt < 0)
   1231 		usb_detach_wakeup(sc->sc_dev);
   1232 }
   1233 
   1234 void
   1235 smsc_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1236 {
   1237 	struct smsc_chain	*c = (struct smsc_chain *)priv;
   1238 	struct smsc_softc	*sc = c->sc_sc;
   1239 	struct ifnet		*ifp = &sc->sc_ec.ec_if;
   1240 	u_char			*buf = c->sc_buf;
   1241 	uint32_t		total_len;
   1242 	uint16_t		pktlen = 0;
   1243 	struct mbuf		*m;
   1244 	int			s;
   1245 	uint32_t		rxhdr;
   1246 
   1247 	if (sc->sc_dying)
   1248 		return;
   1249 
   1250 	if (!(ifp->if_flags & IFF_RUNNING))
   1251 		return;
   1252 
   1253 	if (status != USBD_NORMAL_COMPLETION) {
   1254 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1255 			return;
   1256 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
   1257 			printf("%s: usb errors on rx: %s\n",
   1258 			    device_xname(sc->sc_dev), usbd_errstr(status));
   1259 		}
   1260 		if (status == USBD_STALLED)
   1261 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
   1262 		goto done;
   1263 	}
   1264 
   1265 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1266 	smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
   1267 
   1268 	do {
   1269 		if (total_len < sizeof(rxhdr)) {
   1270 			smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n",
   1271 			    total_len, sizeof(rxhdr));
   1272 			ifp->if_ierrors++;
   1273 			goto done;
   1274 		}
   1275 
   1276 		buf += pktlen;
   1277 
   1278 		memcpy(&rxhdr, buf, sizeof(rxhdr));
   1279 		rxhdr = le32toh(rxhdr);
   1280 		total_len -= sizeof(rxhdr);
   1281 
   1282 		if (rxhdr & SMSC_RX_STAT_ERROR) {
   1283 			smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
   1284 			ifp->if_ierrors++;
   1285 			goto done;
   1286 		}
   1287 
   1288 		pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
   1289 		smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
   1290 		    "0x%08x\n", total_len, pktlen, rxhdr);
   1291 		if (pktlen > total_len) {
   1292 			smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
   1293 			    pktlen, total_len);
   1294 			ifp->if_ierrors++;
   1295 			goto done;
   1296 		}
   1297 
   1298 		buf += sizeof(rxhdr);
   1299 		total_len -= pktlen;
   1300 
   1301 		m = smsc_newbuf();
   1302 		if (m == NULL) {
   1303 			smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
   1304 			ifp->if_ierrors++;
   1305 			goto done;
   1306 		}
   1307 
   1308 		ifp->if_ipackets++;
   1309 		m->m_pkthdr.rcvif = ifp;
   1310 
   1311 		pktlen -= 2;	// JDM
   1312 
   1313 		m->m_pkthdr.len = m->m_len = pktlen;
   1314 #define ETHER_ALIGN 2
   1315 		m_adj(m, ETHER_ALIGN);
   1316 
   1317 		memcpy(mtod(m, char *), buf, pktlen);
   1318 
   1319 		/* push the packet up */
   1320 		s = splnet();
   1321 		bpf_mtap(ifp, m);
   1322 		ifp->if_input(ifp, m);
   1323 		splx(s);
   1324 	} while (total_len > 0);
   1325 
   1326 done:
   1327 	memset(c->sc_buf, 0, sc->sc_bufsz);
   1328 
   1329 	/* Setup new transfer. */
   1330 	usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX],
   1331 	    c, c->sc_buf, sc->sc_bufsz,
   1332 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1333 	    USBD_NO_TIMEOUT, smsc_rxeof);
   1334 	usbd_transfer(xfer);
   1335 
   1336 	return;
   1337 }
   1338 
   1339 void
   1340 smsc_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1341 {
   1342 	struct smsc_softc	*sc;
   1343 	struct smsc_chain	*c;
   1344 	struct ifnet		*ifp;
   1345 	int			s;
   1346 
   1347 	c = priv;
   1348 	sc = c->sc_sc;
   1349 	ifp = &sc->sc_ec.ec_if;
   1350 
   1351 	if (sc->sc_dying)
   1352 		return;
   1353 
   1354 	s = splnet();
   1355 
   1356 	ifp->if_timer = 0;
   1357 	ifp->if_flags &= ~IFF_OACTIVE;
   1358 
   1359 	if (status != USBD_NORMAL_COMPLETION) {
   1360 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1361 			splx(s);
   1362 			return;
   1363 		}
   1364 		ifp->if_oerrors++;
   1365 		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
   1366 		    usbd_errstr(status));
   1367 		if (status == USBD_STALLED)
   1368 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
   1369 		splx(s);
   1370 		return;
   1371 	}
   1372 	ifp->if_opackets++;
   1373 
   1374 	m_freem(c->sc_mbuf);
   1375 	c->sc_mbuf = NULL;
   1376 
   1377 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1378 		smsc_start(ifp);
   1379 
   1380 	splx(s);
   1381 }
   1382 
   1383 int
   1384 smsc_tx_list_init(struct smsc_softc *sc)
   1385 {
   1386 	struct smsc_cdata *cd;
   1387 	struct smsc_chain *c;
   1388 	int i;
   1389 
   1390 	cd = &sc->sc_cdata;
   1391 	for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
   1392 		c = &cd->tx_chain[i];
   1393 		c->sc_sc = sc;
   1394 		c->sc_idx = i;
   1395 		c->sc_mbuf = NULL;
   1396 		if (c->sc_xfer == NULL) {
   1397 			c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
   1398 			if (c->sc_xfer == NULL)
   1399 				return (ENOBUFS);
   1400 			c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
   1401 			    sc->sc_bufsz);
   1402 			if (c->sc_buf == NULL) {
   1403 				usbd_free_xfer(c->sc_xfer);
   1404 				return (ENOBUFS);
   1405 			}
   1406 		}
   1407 	}
   1408 
   1409 	return (0);
   1410 }
   1411 
   1412 int
   1413 smsc_rx_list_init(struct smsc_softc *sc)
   1414 {
   1415 	struct smsc_cdata *cd;
   1416 	struct smsc_chain *c;
   1417 	int i;
   1418 
   1419 	cd = &sc->sc_cdata;
   1420 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
   1421 		c = &cd->rx_chain[i];
   1422 		c->sc_sc = sc;
   1423 		c->sc_idx = i;
   1424 		c->sc_mbuf = NULL;
   1425 		if (c->sc_xfer == NULL) {
   1426 			c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
   1427 			if (c->sc_xfer == NULL)
   1428 				return (ENOBUFS);
   1429 			c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
   1430 			    sc->sc_bufsz);
   1431 			if (c->sc_buf == NULL) {
   1432 				usbd_free_xfer(c->sc_xfer);
   1433 				return (ENOBUFS);
   1434 			}
   1435 		}
   1436 	}
   1437 
   1438 	return (0);
   1439 }
   1440 
   1441 struct mbuf *
   1442 smsc_newbuf(void)
   1443 {
   1444 	struct mbuf	*m;
   1445 
   1446 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1447 	if (m == NULL)
   1448 		return (NULL);
   1449 
   1450 	MCLGET(m, M_DONTWAIT);
   1451 	if (!(m->m_flags & M_EXT)) {
   1452 		m_freem(m);
   1453 		return (NULL);
   1454 	}
   1455 
   1456 	return (m);
   1457 }
   1458 
   1459 int
   1460 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
   1461 {
   1462 	struct ifnet		*ifp = &sc->sc_ec.ec_if;
   1463 	struct smsc_chain	*c;
   1464 	usbd_status		 err;
   1465 	uint32_t		 txhdr;
   1466 	uint32_t		 frm_len = 0;
   1467 
   1468 	c = &sc->sc_cdata.tx_chain[idx];
   1469 
   1470 	/*
   1471 	 * Each frame is prefixed with two 32-bit values describing the
   1472 	 * length of the packet and buffer.
   1473 	 */
   1474 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
   1475 			SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
   1476 	txhdr = htole32(txhdr);
   1477 	memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
   1478 
   1479 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
   1480 	txhdr = htole32(txhdr);
   1481 	memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
   1482 
   1483 	frm_len += 8;
   1484 
   1485 	/* Next copy in the actual packet */
   1486 	m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
   1487 	frm_len += m->m_pkthdr.len;
   1488 
   1489 	c->sc_mbuf = m;
   1490 
   1491 	usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX],
   1492 	    c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1493 	    10000, smsc_txeof);
   1494 
   1495 	err = usbd_transfer(c->sc_xfer);
   1496 	/* XXXNH get task to stop interface */
   1497 	if (err != USBD_IN_PROGRESS) {
   1498 		smsc_stop(ifp, 0);
   1499 		return (EIO);
   1500 	}
   1501 
   1502 	sc->sc_cdata.tx_cnt++;
   1503 
   1504 	return (0);
   1505 }
   1506