Home | History | Annotate | Line # | Download | only in usb
if_smsc.c revision 1.12.2.2
      1 /*	$NetBSD: if_smsc.c,v 1.12.2.2 2014/08/10 06:54:58 tls Exp $	*/
      2 
      3 /*	$OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $	*/
      4 /* $FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
      5 /*-
      6  * Copyright (c) 2012
      7  *	Ben Gray <bgray (at) freebsd.org>.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * SMSC LAN9xxx devices (http://www.smsc.com/)
     33  *
     34  * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
     35  * support USB 2.0 and 10/100 Mbps Ethernet.
     36  *
     37  * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
     38  * The driver only covers the Ethernet part, the standard USB hub driver
     39  * supports the hub part.
     40  *
     41  * This driver is closely modelled on the Linux driver written and copyrighted
     42  * by SMSC.
     43  *
     44  * H/W TCP & UDP Checksum Offloading
     45  * ---------------------------------
     46  * The chip supports both tx and rx offloading of UDP & TCP checksums, this
     47  * feature can be dynamically enabled/disabled.
     48  *
     49  * RX checksuming is performed across bytes after the IPv4 header to the end of
     50  * the Ethernet frame, this means if the frame is padded with non-zero values
     51  * the H/W checksum will be incorrect, however the rx code compensates for this.
     52  *
     53  * TX checksuming is more complicated, the device requires a special header to
     54  * be prefixed onto the start of the frame which indicates the start and end
     55  * positions of the UDP or TCP frame.  This requires the driver to manually
     56  * go through the packet data and decode the headers prior to sending.
     57  * On Linux they generally provide cues to the location of the csum and the
     58  * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
     59  * hence this is not as optimal and therefore h/w TX checksum is currently not
     60  * implemented.
     61  */
     62 
     63 #ifdef _KERNEL_OPT
     64 #include "opt_inet.h"
     65 #endif
     66 
     67 #include <sys/param.h>
     68 #include <sys/bus.h>
     69 #include <sys/systm.h>
     70 #include <sys/sockio.h>
     71 #include <sys/mbuf.h>
     72 #include <sys/mutex.h>
     73 #include <sys/kernel.h>
     74 #include <sys/proc.h>
     75 #include <sys/socket.h>
     76 
     77 #include <sys/device.h>
     78 
     79 #include <sys/rnd.h>
     80 
     81 #include <net/if.h>
     82 #include <net/if_dl.h>
     83 #include <net/if_media.h>
     84 #include <net/if_ether.h>
     85 
     86 #include <net/bpf.h>
     87 
     88 #ifdef INET
     89 #include <netinet/in.h>
     90 #include <netinet/if_inarp.h>
     91 #endif
     92 
     93 #include <dev/mii/mii.h>
     94 #include <dev/mii/miivar.h>
     95 
     96 #include <dev/usb/usb.h>
     97 #include <dev/usb/usbdi.h>
     98 #include <dev/usb/usbdi_util.h>
     99 #include <dev/usb/usbdivar.h>
    100 #include <dev/usb/usbdevs.h>
    101 
    102 #include <dev/usb/if_smscreg.h>
    103 #include <dev/usb/if_smscvar.h>
    104 
    105 #include "ioconf.h"
    106 
    107 #ifdef USB_DEBUG
    108 int smsc_debug = 0;
    109 #endif
    110 
    111 #define ETHER_ALIGN 2
    112 /*
    113  * Various supported device vendors/products.
    114  */
    115 static const struct usb_devno smsc_devs[] = {
    116 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN89530 },
    117 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9530 },
    118 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9730 },
    119 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500 },
    120 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A },
    121 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_ALT },
    122 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_HAL },
    123 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
    124 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_ALT },
    125 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_SAL10 },
    126 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505 },
    127 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A },
    128 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_HAL },
    129 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
    130 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505_SAL10 },
    131 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14 },
    132 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_ALT },
    133 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
    134 };
    135 
    136 #ifdef USB_DEBUG
    137 #define smsc_dbg_printf(sc, fmt, args...) \
    138 	do { \
    139 		if (smsc_debug > 0) \
    140 			printf("debug: " fmt, ##args); \
    141 	} while(0)
    142 #else
    143 #define smsc_dbg_printf(sc, fmt, args...)
    144 #endif
    145 
    146 #define smsc_warn_printf(sc, fmt, args...) \
    147 	printf("%s: warning: " fmt, device_xname((sc)->sc_dev), ##args)
    148 
    149 #define smsc_err_printf(sc, fmt, args...) \
    150 	printf("%s: error: " fmt, device_xname((sc)->sc_dev), ##args)
    151 
    152 /* Function declarations */
    153 int		 smsc_chip_init(struct smsc_softc *);
    154 void		 smsc_setmulti(struct smsc_softc *);
    155 int		 smsc_setmacaddress(struct smsc_softc *, const uint8_t *);
    156 
    157 int		 smsc_match(device_t, cfdata_t, void *);
    158 void		 smsc_attach(device_t, device_t, void *);
    159 int		 smsc_detach(device_t, int);
    160 int		 smsc_activate(device_t, enum devact);
    161 
    162 int		 smsc_init(struct ifnet *);
    163 void		 smsc_start(struct ifnet *);
    164 int		 smsc_ioctl(struct ifnet *, u_long, void *);
    165 void		 smsc_stop(struct ifnet *, int);
    166 
    167 void		 smsc_reset(struct smsc_softc *);
    168 struct mbuf	*smsc_newbuf(void);
    169 
    170 void		 smsc_tick(void *);
    171 void		 smsc_tick_task(void *);
    172 void		 smsc_miibus_statchg(struct ifnet *);
    173 int		 smsc_miibus_readreg(device_t, int, int);
    174 void		 smsc_miibus_writereg(device_t, int, int, int);
    175 int		 smsc_ifmedia_upd(struct ifnet *);
    176 void		 smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    177 void		 smsc_lock_mii(struct smsc_softc *);
    178 void		 smsc_unlock_mii(struct smsc_softc *);
    179 
    180 int		 smsc_tx_list_init(struct smsc_softc *);
    181 int		 smsc_rx_list_init(struct smsc_softc *);
    182 int		 smsc_encap(struct smsc_softc *, struct mbuf *, int);
    183 void		 smsc_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    184 void		 smsc_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    185 
    186 int		 smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
    187 int		 smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
    188 int		 smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
    189 int		 smsc_sethwcsum(struct smsc_softc *);
    190 
    191 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach,
    192     smsc_detach, smsc_activate);
    193 
    194 int
    195 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
    196 {
    197 	usb_device_request_t req;
    198 	uint32_t buf;
    199 	usbd_status err;
    200 
    201 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    202 	req.bRequest = SMSC_UR_READ_REG;
    203 	USETW(req.wValue, 0);
    204 	USETW(req.wIndex, off);
    205 	USETW(req.wLength, 4);
    206 
    207 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    208 	if (err != 0)
    209 		smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
    210 
    211 	*data = le32toh(buf);
    212 
    213 	return (err);
    214 }
    215 
    216 int
    217 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
    218 {
    219 	usb_device_request_t req;
    220 	uint32_t buf;
    221 	usbd_status err;
    222 
    223 	buf = htole32(data);
    224 
    225 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    226 	req.bRequest = SMSC_UR_WRITE_REG;
    227 	USETW(req.wValue, 0);
    228 	USETW(req.wIndex, off);
    229 	USETW(req.wLength, 4);
    230 
    231 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    232 	if (err != 0)
    233 		smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
    234 
    235 	return (err);
    236 }
    237 
    238 int
    239 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
    240 {
    241 	uint32_t val;
    242 	int err, i;
    243 
    244 	for (i = 0; i < 100; i++) {
    245 		if ((err = smsc_read_reg(sc, reg, &val)) != 0)
    246 			return (err);
    247 		if (!(val & bits))
    248 			return (0);
    249 		DELAY(5);
    250 	}
    251 
    252 	return (1);
    253 }
    254 
    255 int
    256 smsc_miibus_readreg(device_t dev, int phy, int reg)
    257 {
    258 	struct smsc_softc *sc = device_private(dev);
    259 	uint32_t addr;
    260 	uint32_t val = 0;
    261 
    262 	smsc_lock_mii(sc);
    263 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    264 		smsc_warn_printf(sc, "MII is busy\n");
    265 		goto done;
    266 	}
    267 
    268 	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
    269 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
    270 
    271 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
    272 		smsc_warn_printf(sc, "MII read timeout\n");
    273 
    274 	smsc_read_reg(sc, SMSC_MII_DATA, &val);
    275 
    276 done:
    277 	smsc_unlock_mii(sc);
    278 
    279 	return (val & 0xFFFF);
    280 }
    281 
    282 void
    283 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
    284 {
    285 	struct smsc_softc *sc = device_private(dev);
    286 	uint32_t addr;
    287 
    288 	if (sc->sc_phyno != phy)
    289 		return;
    290 
    291 	smsc_lock_mii(sc);
    292 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    293 		smsc_warn_printf(sc, "MII is busy\n");
    294 		smsc_unlock_mii(sc);
    295 		return;
    296 	}
    297 
    298 	smsc_write_reg(sc, SMSC_MII_DATA, val);
    299 
    300 	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
    301 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
    302 	smsc_unlock_mii(sc);
    303 
    304 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
    305 		smsc_warn_printf(sc, "MII write timeout\n");
    306 }
    307 
    308 void
    309 smsc_miibus_statchg(struct ifnet *ifp)
    310 {
    311 	struct smsc_softc *sc = ifp->if_softc;
    312 	struct mii_data *mii = &sc->sc_mii;
    313 	int err;
    314 	uint32_t flow;
    315 	uint32_t afc_cfg;
    316 
    317 	if (mii == NULL || ifp == NULL ||
    318 	    (ifp->if_flags & IFF_RUNNING) == 0)
    319 		return;
    320 
    321 	/* Use the MII status to determine link status */
    322 	sc->sc_flags &= ~SMSC_FLAG_LINK;
    323 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    324 	    (IFM_ACTIVE | IFM_AVALID)) {
    325 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    326 			case IFM_10_T:
    327 			case IFM_100_TX:
    328 				sc->sc_flags |= SMSC_FLAG_LINK;
    329 				break;
    330 			case IFM_1000_T:
    331 				/* Gigabit ethernet not supported by chipset */
    332 				break;
    333 			default:
    334 				break;
    335 		}
    336 	}
    337 
    338 	/* Lost link, do nothing. */
    339 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
    340 		smsc_dbg_printf(sc, "link flag not set\n");
    341 		return;
    342 	}
    343 
    344 	err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
    345 	if (err) {
    346 		smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
    347 		    "error %d\n", err);
    348 		return;
    349 	}
    350 
    351 	/* Enable/disable full duplex operation and TX/RX pause */
    352 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
    353 		smsc_dbg_printf(sc, "full duplex operation\n");
    354 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
    355 		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
    356 
    357 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
    358 			flow = 0xffff0002;
    359 		else
    360 			flow = 0;
    361 
    362 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
    363 			afc_cfg |= 0xf;
    364 		else
    365 			afc_cfg &= ~0xf;
    366 
    367 	} else {
    368 		smsc_dbg_printf(sc, "half duplex operation\n");
    369 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
    370 		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
    371 
    372 		flow = 0;
    373 		afc_cfg |= 0xf;
    374 	}
    375 
    376 	err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    377 	err += smsc_write_reg(sc, SMSC_FLOW, flow);
    378 	err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
    379 	if (err)
    380 		smsc_warn_printf(sc, "media change failed, error %d\n", err);
    381 }
    382 
    383 int
    384 smsc_ifmedia_upd(struct ifnet *ifp)
    385 {
    386 	struct smsc_softc *sc = ifp->if_softc;
    387 	struct mii_data *mii = &sc->sc_mii;
    388 	int err;
    389 
    390 	if (mii->mii_instance) {
    391 		struct mii_softc *miisc;
    392 
    393 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    394 			mii_phy_reset(miisc);
    395 	}
    396 	err = mii_mediachg(mii);
    397 	return (err);
    398 }
    399 
    400 void
    401 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    402 {
    403 	struct smsc_softc *sc = ifp->if_softc;
    404 	struct mii_data *mii = &sc->sc_mii;
    405 
    406 	mii_pollstat(mii);
    407 
    408 	ifmr->ifm_active = mii->mii_media_active;
    409 	ifmr->ifm_status = mii->mii_media_status;
    410 }
    411 
    412 static inline uint32_t
    413 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
    414 {
    415 	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
    416 }
    417 
    418 void
    419 smsc_setmulti(struct smsc_softc *sc)
    420 {
    421 	struct ifnet		*ifp = &sc->sc_ec.ec_if;
    422 	struct ether_multi	*enm;
    423 	struct ether_multistep	 step;
    424 	uint32_t		 hashtbl[2] = { 0, 0 };
    425 	uint32_t		 hash;
    426 
    427 	if (sc->sc_dying)
    428 		return;
    429 
    430 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
    431 allmulti:
    432 		smsc_dbg_printf(sc, "receive all multicast enabled\n");
    433 		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
    434 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
    435 		smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    436 		return;
    437 	} else {
    438 		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
    439 		sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
    440 	}
    441 
    442 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    443 	while (enm != NULL) {
    444 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    445 		    ETHER_ADDR_LEN) != 0)
    446 			goto allmulti;
    447 
    448 		hash = smsc_hash(enm->enm_addrlo);
    449 		hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
    450 		ETHER_NEXT_MULTI(step, enm);
    451 	}
    452 
    453 	/* Debug */
    454 	if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
    455 		smsc_dbg_printf(sc, "receive select group of macs\n");
    456 	} else {
    457 		smsc_dbg_printf(sc, "receive own packets only\n");
    458 	}
    459 
    460 	/* Write the hash table and mac control registers */
    461 	ifp->if_flags &= ~IFF_ALLMULTI;
    462 	smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
    463 	smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
    464 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    465 }
    466 
    467 int
    468 smsc_sethwcsum(struct smsc_softc *sc)
    469 {
    470 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    471 	uint32_t val;
    472 	int err;
    473 
    474 	if (!ifp)
    475 		return EIO;
    476 
    477 	err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
    478 	if (err != 0) {
    479 		smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
    480 		    err);
    481 		return (err);
    482 	}
    483 
    484 	/* Enable/disable the Rx checksum */
    485 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx))
    486 		val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    487 	else
    488 		val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    489 
    490 	/* Enable/disable the Tx checksum (currently not supported) */
    491 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx))
    492 		val |= SMSC_COE_CTRL_TX_EN;
    493 	else
    494 		val &= ~SMSC_COE_CTRL_TX_EN;
    495 
    496 	sc->sc_coe_ctrl = val;
    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 	/* TCP/UDP checksum offload engines. */
    580 	smsc_sethwcsum(sc);
    581 
    582 	/* Open RX and TX pipes. */
    583 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
    584 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]);
    585 	if (err) {
    586 		printf("%s: open rx pipe failed: %s\n",
    587 		    device_xname(sc->sc_dev), usbd_errstr(err));
    588 		splx(s);
    589 		return EIO;
    590 	}
    591 
    592 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
    593 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]);
    594 	if (err) {
    595 		printf("%s: open tx pipe failed: %s\n",
    596 		    device_xname(sc->sc_dev), usbd_errstr(err));
    597 		splx(s);
    598 		return EIO;
    599 	}
    600 
    601 	/* Start up the receive pipe. */
    602 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
    603 		c = &sc->sc_cdata.rx_chain[i];
    604 		usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX],
    605 		    c, c->sc_buf, sc->sc_bufsz,
    606 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
    607 		    USBD_NO_TIMEOUT, smsc_rxeof);
    608 		usbd_transfer(c->sc_xfer);
    609 	}
    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 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    783 	const char *eaddr = CLLADDR(ifp->if_sadl);
    784 	if ((err = smsc_setmacaddress(sc, eaddr)) != 0) {
    785 		smsc_warn_printf(sc, "failed to set the MAC address\n");
    786 		goto init_failed;
    787 	}
    788 
    789 	/*
    790 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
    791 	 * sequence as used in the Linux driver.
    792 	 */
    793 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
    794 		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
    795 		goto init_failed;
    796 	}
    797 	reg_val |= SMSC_HW_CFG_BIR;
    798 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    799 
    800 	/*
    801 	 * There is a so called 'turbo mode' that the linux driver supports, it
    802 	 * seems to allow you to jam multiple frames per Rx transaction.
    803 	 * By default this driver supports that and therefore allows multiple
    804 	 * frames per USB transfer.
    805 	 *
    806 	 * The xfer buffer size needs to reflect this as well, therefore based
    807 	 * on the calculations in the Linux driver the RX bufsize is set to
    808 	 * 18944,
    809 	 *     bufsz = (16 * 1024 + 5 * 512)
    810 	 *
    811 	 * Burst capability is the number of URBs that can be in a burst of
    812 	 * data/ethernet frames.
    813 	 */
    814 
    815 	if (sc->sc_udev->speed == USB_SPEED_HIGH)
    816 		burst_cap = 37;
    817 	else
    818 		burst_cap = 128;
    819 
    820 	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
    821 
    822 	/* Set the default bulk in delay (magic value from Linux driver) */
    823 	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
    824 
    825 	/*
    826 	 * Initialise the RX interface
    827 	 */
    828 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
    829 		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
    830 		    err);
    831 		goto init_failed;
    832 	}
    833 
    834 	/*
    835 	 * The following settings are used for 'turbo mode', a.k.a multiple
    836 	 * frames per Rx transaction (again info taken form Linux driver).
    837 	 */
    838 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
    839 
    840 	/*
    841 	 * set Rx data offset to ETHER_ALIGN which will make the IP header
    842 	 * align on a word boundary.
    843 	 */
    844 	reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;
    845 
    846 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
    847 
    848 	/* Clear the status register ? */
    849 	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
    850 
    851 	/* Read and display the revision register */
    852 	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
    853 		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
    854 		goto init_failed;
    855 	}
    856 
    857 	/* GPIO/LED setup */
    858 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
    859 	    SMSC_LED_GPIO_CFG_FDX_LED;
    860 	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
    861 
    862 	/*
    863 	 * Initialise the TX interface
    864 	 */
    865 	smsc_write_reg(sc, SMSC_FLOW, 0);
    866 
    867 	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
    868 
    869 	/* Read the current MAC configuration */
    870 	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
    871 		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
    872 		goto init_failed;
    873 	}
    874 
    875 	/* disable pad stripping, collides with checksum offload */
    876 	sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;
    877 
    878 	/* Vlan */
    879 	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
    880 
    881 	/*
    882 	 * Start TX
    883 	 */
    884 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
    885 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    886 	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
    887 
    888 	/*
    889 	 * Start RX
    890 	 */
    891 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
    892 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
    893 
    894 	return (0);
    895 
    896 init_failed:
    897 	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
    898 	return (err);
    899 }
    900 
    901 int
    902 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    903 {
    904 	struct smsc_softc	*sc = ifp->if_softc;
    905 	struct ifreq /*const*/	*ifr = data;
    906 	int			s, error = 0;
    907 
    908 	if (sc->sc_dying)
    909 		return EIO;
    910 
    911 	s = splnet();
    912 
    913 	switch(cmd) {
    914 	case SIOCSIFFLAGS:
    915 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    916 			break;
    917 
    918 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
    919 		case IFF_RUNNING:
    920 			smsc_stop(ifp, 1);
    921 			break;
    922 		case IFF_UP:
    923 			smsc_init(ifp);
    924 			break;
    925 		case IFF_UP | IFF_RUNNING:
    926 			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 if (!(ifp->if_flags & IFF_PROMISC) &&
    933 			    sc->sc_if_flags & IFF_PROMISC) {
    934 				sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
    935 				smsc_write_reg(sc, SMSC_MAC_CSR,
    936 				    sc->sc_mac_csr);
    937 				smsc_setmulti(sc);
    938 			} else {
    939 				smsc_init(ifp);
    940 			}
    941 			break;
    942 		}
    943 		sc->sc_if_flags = ifp->if_flags;
    944 		break;
    945 
    946 	case SIOCGIFMEDIA:
    947 	case SIOCSIFMEDIA:
    948 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    949 		break;
    950 
    951 	default:
    952 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
    953 			break;
    954 
    955 		error = 0;
    956 
    957 		if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)
    958 			smsc_setmulti(sc);
    959 
    960 	}
    961 	splx(s);
    962 
    963 	return error;
    964 }
    965 
    966 int
    967 smsc_match(device_t parent, cfdata_t match, void *aux)
    968 {
    969 	struct usb_attach_arg *uaa = aux;
    970 
    971 	return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ?
    972 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    973 }
    974 
    975 void
    976 smsc_attach(device_t parent, device_t self, void *aux)
    977 {
    978 	struct smsc_softc *sc = device_private(self);
    979 	struct usb_attach_arg *uaa = aux;
    980 	usbd_device_handle dev = uaa->device;
    981 	usb_interface_descriptor_t *id;
    982 	usb_endpoint_descriptor_t *ed;
    983 	char *devinfop;
    984 	struct mii_data *mii;
    985 	struct ifnet *ifp;
    986 	int err, s, i;
    987 	uint32_t mac_h, mac_l;
    988 
    989 	sc->sc_dev = self;
    990 	sc->sc_udev = dev;
    991 
    992 	aprint_naive("\n");
    993 	aprint_normal("\n");
    994 
    995 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    996 	aprint_normal_dev(self, "%s\n", devinfop);
    997 	usbd_devinfo_free(devinfop);
    998 
    999 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
   1000 	if (err) {
   1001 		aprint_error_dev(self, "failed to set configuration"
   1002 		    ", err=%s\n", usbd_errstr(err));
   1003 		return;
   1004 	}
   1005 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
   1006 	usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 0);
   1007 	usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 0);
   1008 	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
   1009 
   1010 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
   1011 	if (err) {
   1012 		aprint_error_dev(self, "getting interface handle failed\n");
   1013 		return;
   1014 	}
   1015 
   1016 	id = usbd_get_interface_descriptor(sc->sc_iface);
   1017 
   1018 	if (sc->sc_udev->speed >= USB_SPEED_HIGH)
   1019 		sc->sc_bufsz = SMSC_MAX_BUFSZ;
   1020 	else
   1021 		sc->sc_bufsz = SMSC_MIN_BUFSZ;
   1022 
   1023 	/* Find endpoints. */
   1024 	for (i = 0; i < id->bNumEndpoints; i++) {
   1025 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
   1026 		if (!ed) {
   1027 			aprint_error_dev(self, "couldn't get ep %d\n", i);
   1028 			return;
   1029 		}
   1030 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1031 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1032 			sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
   1033 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
   1034 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1035 			sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
   1036 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1037 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
   1038 			sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
   1039 		}
   1040 	}
   1041 
   1042 	s = splnet();
   1043 
   1044 	ifp = &sc->sc_ec.ec_if;
   1045 	ifp->if_softc = sc;
   1046 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
   1047 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1048 	ifp->if_init = smsc_init;
   1049 	ifp->if_ioctl = smsc_ioctl;
   1050 	ifp->if_start = smsc_start;
   1051 	ifp->if_stop = smsc_stop;
   1052 
   1053 #ifdef notyet
   1054 	/*
   1055 	 * We can do TCPv4, and UDPv4 checksums in hardware.
   1056 	 */
   1057 	ifp->if_capabilities |=
   1058 	    /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
   1059 	    /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
   1060 #endif
   1061 
   1062 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
   1063 
   1064 	/* Setup some of the basics */
   1065 	sc->sc_phyno = 1;
   1066 
   1067 	/*
   1068 	 * Attempt to get the mac address, if an EEPROM is not attached this
   1069 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
   1070 	 * address based on urandom.
   1071 	 */
   1072 	memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);
   1073 
   1074 	prop_dictionary_t dict = device_properties(self);
   1075 	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
   1076 
   1077 	if (eaprop != NULL) {
   1078 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
   1079 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
   1080 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
   1081 		    ETHER_ADDR_LEN);
   1082 	} else
   1083 	/* Check if there is already a MAC address in the register */
   1084 	if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
   1085 	    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
   1086 		sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
   1087 		sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
   1088 		sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
   1089 		sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
   1090 		sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
   1091 		sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
   1092 	}
   1093 
   1094 	aprint_normal_dev(self, " Ethernet address %s\n", ether_sprintf(sc->sc_enaddr));
   1095 
   1096 	IFQ_SET_READY(&ifp->if_snd);
   1097 
   1098 	/* Initialize MII/media info. */
   1099 	mii = &sc->sc_mii;
   1100 	mii->mii_ifp = ifp;
   1101 	mii->mii_readreg = smsc_miibus_readreg;
   1102 	mii->mii_writereg = smsc_miibus_writereg;
   1103 	mii->mii_statchg = smsc_miibus_statchg;
   1104 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1105 	sc->sc_ec.ec_mii = mii;
   1106 	ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
   1107 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
   1108 
   1109 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1110 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1111 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1112 	} else
   1113 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1114 
   1115 	if_attach(ifp);
   1116 	ether_ifattach(ifp, sc->sc_enaddr);
   1117 
   1118 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
   1119 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
   1120 
   1121 	callout_init(&sc->sc_stat_ch, 0);
   1122 
   1123 	splx(s);
   1124 
   1125 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
   1126 }
   1127 
   1128 int
   1129 smsc_detach(device_t self, int flags)
   1130 {
   1131 	struct smsc_softc *sc = device_private(self);
   1132 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1133 	int s;
   1134 
   1135 	callout_stop(&sc->sc_stat_ch);
   1136 
   1137 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL)
   1138 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
   1139 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL)
   1140 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
   1141 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
   1142 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
   1143 
   1144 	/*
   1145 	 * Remove any pending tasks.  They cannot be executing because they run
   1146 	 * in the same thread as detach.
   1147 	 */
   1148 	usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
   1149 	usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
   1150 
   1151 	s = splusb();
   1152 
   1153 	if (--sc->sc_refcnt >= 0) {
   1154 		/* Wait for processes to go away */
   1155 		usb_detach_waitold(sc->sc_dev);
   1156 	}
   1157 
   1158 	if (ifp->if_flags & IFF_RUNNING)
   1159 		smsc_stop(ifp ,1);
   1160 
   1161 	rnd_detach_source(&sc->sc_rnd_source);
   1162 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1163 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   1164 	if (ifp->if_softc != NULL) {
   1165 		ether_ifdetach(ifp);
   1166 		if_detach(ifp);
   1167 	}
   1168 
   1169 #ifdef DIAGNOSTIC
   1170 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
   1171 	    sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
   1172 	    sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
   1173 		printf("%s: detach has active endpoints\n",
   1174 		    device_xname(sc->sc_dev));
   1175 #endif
   1176 
   1177 	if (--sc->sc_refcnt >= 0) {
   1178 		/* Wait for processes to go away. */
   1179 		usb_detach_waitold(sc->sc_dev);
   1180 	}
   1181 	splx(s);
   1182 
   1183 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
   1184 
   1185 	mutex_destroy(&sc->sc_mii_lock);
   1186 
   1187 	return (0);
   1188 }
   1189 
   1190 void
   1191 smsc_tick_task(void *xsc)
   1192 {
   1193 	int			 s;
   1194 	struct smsc_softc	*sc = xsc;
   1195 	struct ifnet		*ifp;
   1196 	struct mii_data		*mii;
   1197 
   1198 	if (sc == NULL)
   1199 		return;
   1200 
   1201 	if (sc->sc_dying)
   1202 		return;
   1203 	ifp = &sc->sc_ec.ec_if;
   1204 	mii = &sc->sc_mii;
   1205 	if (mii == NULL)
   1206 		return;
   1207 
   1208 	s = splnet();
   1209 
   1210 	mii_tick(mii);
   1211 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
   1212 		smsc_miibus_statchg(ifp);
   1213 	callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
   1214 
   1215 	splx(s);
   1216 }
   1217 
   1218 int
   1219 smsc_activate(device_t self, enum devact act)
   1220 {
   1221 	struct smsc_softc *sc = device_private(self);
   1222 
   1223 	switch (act) {
   1224 	case DVACT_DEACTIVATE:
   1225 		if_deactivate(&sc->sc_ec.ec_if);
   1226 		sc->sc_dying = 1;
   1227 		return 0;
   1228 	default:
   1229 		return EOPNOTSUPP;
   1230 	}
   1231 	return (0);
   1232 }
   1233 
   1234 void
   1235 smsc_lock_mii(struct smsc_softc *sc)
   1236 {
   1237 	sc->sc_refcnt++;
   1238 	mutex_enter(&sc->sc_mii_lock);
   1239 }
   1240 
   1241 void
   1242 smsc_unlock_mii(struct smsc_softc *sc)
   1243 {
   1244 	mutex_exit(&sc->sc_mii_lock);
   1245 	if (--sc->sc_refcnt < 0)
   1246 		usb_detach_wakeupold(sc->sc_dev);
   1247 }
   1248 
   1249 void
   1250 smsc_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1251 {
   1252 	struct smsc_chain	*c = (struct smsc_chain *)priv;
   1253 	struct smsc_softc	*sc = c->sc_sc;
   1254 	struct ifnet		*ifp = &sc->sc_ec.ec_if;
   1255 	u_char			*buf = c->sc_buf;
   1256 	uint32_t		total_len;
   1257 	uint32_t		rxhdr;
   1258 	uint16_t		pktlen;
   1259 	struct mbuf		*m;
   1260 	int			s;
   1261 
   1262 	if (sc->sc_dying)
   1263 		return;
   1264 
   1265 	if (!(ifp->if_flags & IFF_RUNNING))
   1266 		return;
   1267 
   1268 	if (status != USBD_NORMAL_COMPLETION) {
   1269 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1270 			return;
   1271 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
   1272 			printf("%s: usb errors on rx: %s\n",
   1273 			    device_xname(sc->sc_dev), usbd_errstr(status));
   1274 		}
   1275 		if (status == USBD_STALLED)
   1276 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
   1277 		goto done;
   1278 	}
   1279 
   1280 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1281 	smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
   1282 
   1283 	while (total_len != 0) {
   1284 		if (total_len < sizeof(rxhdr)) {
   1285 			smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n",
   1286 			    total_len, sizeof(rxhdr));
   1287 			ifp->if_ierrors++;
   1288 			goto done;
   1289 		}
   1290 
   1291 		memcpy(&rxhdr, buf, sizeof(rxhdr));
   1292 		rxhdr = le32toh(rxhdr);
   1293 		buf += sizeof(rxhdr);
   1294 		total_len -= sizeof(rxhdr);
   1295 
   1296 		if (rxhdr & SMSC_RX_STAT_ERROR) {
   1297 			smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
   1298 			ifp->if_ierrors++;
   1299 			goto done;
   1300 		}
   1301 
   1302 		pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
   1303 		smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
   1304 		    "0x%08x\n", total_len, pktlen, rxhdr);
   1305 
   1306 		pktlen += ETHER_ALIGN;
   1307 
   1308 		if (pktlen > MCLBYTES) {
   1309 			smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n",
   1310 			    pktlen, MCLBYTES);
   1311 			ifp->if_ierrors++;
   1312 			goto done;
   1313 		}
   1314 
   1315 		if (pktlen > total_len) {
   1316 			smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
   1317 			    pktlen, total_len);
   1318 			ifp->if_ierrors++;
   1319 			goto done;
   1320 		}
   1321 
   1322 		m = smsc_newbuf();
   1323 		if (m == NULL) {
   1324 			smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
   1325 			ifp->if_ierrors++;
   1326 			goto done;
   1327 		}
   1328 
   1329 		ifp->if_ipackets++;
   1330 		m->m_pkthdr.rcvif = ifp;
   1331 		m->m_pkthdr.len = m->m_len = pktlen;
   1332 		m->m_flags |= M_HASFCS;
   1333 		m_adj(m, ETHER_ALIGN);
   1334 
   1335 		KASSERT(m->m_len < MCLBYTES);
   1336 		memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len);
   1337 
   1338 		/* Check if RX TCP/UDP checksumming is being offloaded */
   1339 		if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
   1340 			smsc_dbg_printf(sc,"RX checksum offload checking\n");
   1341 			struct ether_header *eh;
   1342 
   1343 			eh = mtod(m, struct ether_header *);
   1344 
   1345 			/* Remove the extra 2 bytes of the csum */
   1346 			m_adj(m, -2);
   1347 
   1348 			/*
   1349 			 * The checksum appears to be simplistically calculated
   1350 			 * over the udp/tcp header and data up to the end of the
   1351 			 * eth frame.  Which means if the eth frame is padded
   1352 			 * the csum calculation is incorrectly performed over
   1353 			 * the padding bytes as well. Therefore to be safe we
   1354 			 * ignore the H/W csum on frames less than or equal to
   1355 			 * 64 bytes.
   1356 			 *
   1357 			 * Ignore H/W csum for non-IPv4 packets.
   1358 			 */
   1359 			smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n",
   1360 			    be16toh(eh->ether_type), pktlen);
   1361 			if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
   1362 			    pktlen > ETHER_MIN_LEN) {
   1363 
   1364 				m->m_pkthdr.csum_flags |=
   1365 				    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);
   1366 
   1367 				/*
   1368 				 * Copy the TCP/UDP checksum from the last 2
   1369 				 * bytes of the transfer and put in the
   1370 				 * csum_data field.
   1371 				 */
   1372 				memcpy(&m->m_pkthdr.csum_data,
   1373 				    buf + pktlen - 2, 2);
   1374 				/*
   1375 				 * The data is copied in network order, but the
   1376 				 * csum algorithm in the kernel expects it to be
   1377 				 * in host network order.
   1378 				 */
   1379 				m->m_pkthdr.csum_data =
   1380 				    ntohs(m->m_pkthdr.csum_data);
   1381 				smsc_dbg_printf(sc,
   1382 				    "RX checksum offloaded (0x%04x)\n",
   1383 				    m->m_pkthdr.csum_data);
   1384 			}
   1385 		}
   1386 
   1387 		/* round up to next longword */
   1388 		pktlen = (pktlen + 3) & ~0x3;
   1389 
   1390 		/* total_len does not include the padding */
   1391 		if (pktlen > total_len)
   1392 			pktlen = total_len;
   1393 
   1394 		buf += pktlen;
   1395 		total_len -= pktlen;
   1396 
   1397 		/* push the packet up */
   1398 		s = splnet();
   1399 		bpf_mtap(ifp, m);
   1400 		ifp->if_input(ifp, m);
   1401 		splx(s);
   1402 	}
   1403 
   1404 done:
   1405 	/* Setup new transfer. */
   1406 	usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX],
   1407 	    c, c->sc_buf, sc->sc_bufsz,
   1408 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1409 	    USBD_NO_TIMEOUT, smsc_rxeof);
   1410 	usbd_transfer(xfer);
   1411 
   1412 	return;
   1413 }
   1414 
   1415 void
   1416 smsc_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1417 {
   1418 	struct smsc_softc	*sc;
   1419 	struct smsc_chain	*c;
   1420 	struct ifnet		*ifp;
   1421 	int			s;
   1422 
   1423 	c = priv;
   1424 	sc = c->sc_sc;
   1425 	ifp = &sc->sc_ec.ec_if;
   1426 
   1427 	if (sc->sc_dying)
   1428 		return;
   1429 
   1430 	s = splnet();
   1431 
   1432 	ifp->if_timer = 0;
   1433 	ifp->if_flags &= ~IFF_OACTIVE;
   1434 
   1435 	if (status != USBD_NORMAL_COMPLETION) {
   1436 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1437 			splx(s);
   1438 			return;
   1439 		}
   1440 		ifp->if_oerrors++;
   1441 		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
   1442 		    usbd_errstr(status));
   1443 		if (status == USBD_STALLED)
   1444 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
   1445 		splx(s);
   1446 		return;
   1447 	}
   1448 	ifp->if_opackets++;
   1449 
   1450 	m_freem(c->sc_mbuf);
   1451 	c->sc_mbuf = NULL;
   1452 
   1453 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1454 		smsc_start(ifp);
   1455 
   1456 	splx(s);
   1457 }
   1458 
   1459 int
   1460 smsc_tx_list_init(struct smsc_softc *sc)
   1461 {
   1462 	struct smsc_cdata *cd;
   1463 	struct smsc_chain *c;
   1464 	int i;
   1465 
   1466 	cd = &sc->sc_cdata;
   1467 	for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
   1468 		c = &cd->tx_chain[i];
   1469 		c->sc_sc = sc;
   1470 		c->sc_idx = i;
   1471 		c->sc_mbuf = NULL;
   1472 		if (c->sc_xfer == NULL) {
   1473 			c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
   1474 			if (c->sc_xfer == NULL)
   1475 				return (ENOBUFS);
   1476 			c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
   1477 			    sc->sc_bufsz);
   1478 			if (c->sc_buf == NULL) {
   1479 				usbd_free_xfer(c->sc_xfer);
   1480 				return (ENOBUFS);
   1481 			}
   1482 		}
   1483 	}
   1484 
   1485 	return (0);
   1486 }
   1487 
   1488 int
   1489 smsc_rx_list_init(struct smsc_softc *sc)
   1490 {
   1491 	struct smsc_cdata *cd;
   1492 	struct smsc_chain *c;
   1493 	int i;
   1494 
   1495 	cd = &sc->sc_cdata;
   1496 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
   1497 		c = &cd->rx_chain[i];
   1498 		c->sc_sc = sc;
   1499 		c->sc_idx = i;
   1500 		c->sc_mbuf = NULL;
   1501 		if (c->sc_xfer == NULL) {
   1502 			c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
   1503 			if (c->sc_xfer == NULL)
   1504 				return (ENOBUFS);
   1505 			c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
   1506 			    sc->sc_bufsz);
   1507 			if (c->sc_buf == NULL) {
   1508 				usbd_free_xfer(c->sc_xfer);
   1509 				return (ENOBUFS);
   1510 			}
   1511 		}
   1512 	}
   1513 
   1514 	return (0);
   1515 }
   1516 
   1517 struct mbuf *
   1518 smsc_newbuf(void)
   1519 {
   1520 	struct mbuf	*m;
   1521 
   1522 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1523 	if (m == NULL)
   1524 		return (NULL);
   1525 
   1526 	MCLGET(m, M_DONTWAIT);
   1527 	if (!(m->m_flags & M_EXT)) {
   1528 		m_freem(m);
   1529 		return (NULL);
   1530 	}
   1531 
   1532 	return (m);
   1533 }
   1534 
   1535 int
   1536 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
   1537 {
   1538 	struct ifnet		*ifp = &sc->sc_ec.ec_if;
   1539 	struct smsc_chain	*c;
   1540 	usbd_status		 err;
   1541 	uint32_t		 txhdr;
   1542 	uint32_t		 frm_len = 0;
   1543 
   1544 	c = &sc->sc_cdata.tx_chain[idx];
   1545 
   1546 	/*
   1547 	 * Each frame is prefixed with two 32-bit values describing the
   1548 	 * length of the packet and buffer.
   1549 	 */
   1550 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
   1551 			SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
   1552 	txhdr = htole32(txhdr);
   1553 	memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
   1554 
   1555 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
   1556 	txhdr = htole32(txhdr);
   1557 	memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
   1558 
   1559 	frm_len += 8;
   1560 
   1561 	/* Next copy in the actual packet */
   1562 	m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
   1563 	frm_len += m->m_pkthdr.len;
   1564 
   1565 	c->sc_mbuf = m;
   1566 
   1567 	usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX],
   1568 	    c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1569 	    10000, smsc_txeof);
   1570 
   1571 	err = usbd_transfer(c->sc_xfer);
   1572 	/* XXXNH get task to stop interface */
   1573 	if (err != USBD_IN_PROGRESS) {
   1574 		smsc_stop(ifp, 0);
   1575 		return (EIO);
   1576 	}
   1577 
   1578 	sc->sc_cdata.tx_cnt++;
   1579 
   1580 	return (0);
   1581 }
   1582