Home | History | Annotate | Line # | Download | only in usb
if_smsc.c revision 1.51
      1 /*	$NetBSD: if_smsc.c,v 1.51 2019/08/10 02:17:36 mrg Exp $	*/
      2 
      3 /*	$OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $	*/
      4 /*	$FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
      5 /*-
      6  * Copyright (c) 2012
      7  *	Ben Gray <bgray (at) freebsd.org>.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * SMSC LAN9xxx devices (http://www.smsc.com/)
     33  *
     34  * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
     35  * support USB 2.0 and 10/100 Mbps Ethernet.
     36  *
     37  * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
     38  * The driver only covers the Ethernet part, the standard USB hub driver
     39  * supports the hub part.
     40  *
     41  * This driver is closely modelled on the Linux driver written and copyrighted
     42  * by SMSC.
     43  *
     44  * H/W TCP & UDP Checksum Offloading
     45  * ---------------------------------
     46  * The chip supports both tx and rx offloading of UDP & TCP checksums, this
     47  * feature can be dynamically enabled/disabled.
     48  *
     49  * RX checksuming is performed across bytes after the IPv4 header to the end of
     50  * the Ethernet frame, this means if the frame is padded with non-zero values
     51  * the H/W checksum will be incorrect, however the rx code compensates for this.
     52  *
     53  * TX checksuming is more complicated, the device requires a special header to
     54  * be prefixed onto the start of the frame which indicates the start and end
     55  * positions of the UDP or TCP frame.  This requires the driver to manually
     56  * go through the packet data and decode the headers prior to sending.
     57  * On Linux they generally provide cues to the location of the csum and the
     58  * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
     59  * hence this is not as optimal and therefore h/w TX checksum is currently not
     60  * implemented.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v 1.51 2019/08/10 02:17:36 mrg Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_usb.h"
     68 #endif
     69 
     70 #include <sys/param.h>
     71 #include <sys/module.h>
     72 
     73 #include <dev/usb/usbnet.h>
     74 
     75 #include <dev/usb/if_smscreg.h>
     76 
     77 #include "ioconf.h"
     78 
     79 #ifdef USB_DEBUG
     80 int smsc_debug = 0;
     81 #endif
     82 
     83 struct smsc_softc {
     84 	struct usbnet		smsc_un;
     85 
     86 	/*
     87 	 * The following stores the settings in the mac control (MAC_CSR)
     88 	 * register
     89 	 */
     90 	uint32_t		sc_mac_csr;
     91 	uint32_t		sc_rev_id;
     92 
     93 	uint32_t		sc_coe_ctrl;
     94 };
     95 
     96 #define SMSC_MIN_BUFSZ		2048
     97 #define SMSC_MAX_BUFSZ		18944
     98 
     99 /*
    100  * Various supported device vendors/products.
    101  */
    102 static const struct usb_devno smsc_devs[] = {
    103 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN89530 },
    104 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9530 },
    105 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9730 },
    106 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500 },
    107 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A },
    108 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_ALT },
    109 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_HAL },
    110 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
    111 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_ALT },
    112 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_SAL10 },
    113 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505 },
    114 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A },
    115 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_HAL },
    116 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
    117 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505_SAL10 },
    118 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14 },
    119 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_ALT },
    120 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
    121 };
    122 
    123 #ifdef USB_DEBUG
    124 #define smsc_dbg_printf(un, fmt, args...) \
    125 	do { \
    126 		if (smsc_debug > 0) \
    127 			printf("debug: " fmt, ##args); \
    128 	} while(0)
    129 #else
    130 #define smsc_dbg_printf(un, fmt, args...)
    131 #endif
    132 
    133 #define smsc_warn_printf(un, fmt, args...) \
    134 	printf("%s: warning: " fmt, device_xname((un)->un_dev), ##args)
    135 
    136 #define smsc_err_printf(un, fmt, args...) \
    137 	printf("%s: error: " fmt, device_xname((un)->un_dev), ##args)
    138 
    139 /* Function declarations */
    140 int		 smsc_match(device_t, cfdata_t, void *);
    141 void		 smsc_attach(device_t, device_t, void *);
    142 
    143 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc),
    144     smsc_match, smsc_attach, usbnet_detach, usbnet_activate);
    145 
    146 int		 smsc_chip_init(struct usbnet *);
    147 int		 smsc_setmacaddress(struct usbnet *, const uint8_t *);
    148 
    149 int		 smsc_init(struct ifnet *);
    150 int		 smsc_init_locked(struct ifnet *);
    151 int		 smsc_ioctl(struct ifnet *, u_long, void *);
    152 void		 smsc_stop_cb(struct ifnet *, int);
    153 
    154 void		 smsc_reset(struct smsc_softc *);
    155 
    156 static void	 smsc_miibus_statchg(struct ifnet *);
    157 int		 smsc_readreg(struct usbnet *, uint32_t, uint32_t *);
    158 int		 smsc_writereg(struct usbnet *, uint32_t, uint32_t);
    159 int		 smsc_wait_for_bits(struct usbnet *, uint32_t, uint32_t);
    160 usbd_status	 smsc_miibus_readreg(struct usbnet *, int, int, uint16_t *);
    161 usbd_status	 smsc_miibus_writereg(struct usbnet *, int, int, uint16_t);
    162 
    163 static int	 smsc_ioctl_cb(struct ifnet *, u_long, void *);
    164 static unsigned	 smsc_tx_prepare(struct usbnet *, struct mbuf *,
    165 		     struct usbnet_chain *);
    166 static void	 smsc_rxeof_loop(struct usbnet *, struct usbd_xfer *,
    167 		    struct usbnet_chain *, uint32_t);
    168 
    169 static struct usbnet_ops smsc_ops = {
    170 	.uno_stop = smsc_stop_cb,
    171 	.uno_ioctl = smsc_ioctl_cb,
    172 	.uno_read_reg = smsc_miibus_readreg,
    173 	.uno_write_reg = smsc_miibus_writereg,
    174 	.uno_statchg = smsc_miibus_statchg,
    175 	.uno_tx_prepare = smsc_tx_prepare,
    176 	.uno_rx_loop = smsc_rxeof_loop,
    177 	.uno_init = smsc_init,
    178 };
    179 
    180 int
    181 smsc_readreg(struct usbnet *un, uint32_t off, uint32_t *data)
    182 {
    183 	usb_device_request_t req;
    184 	uint32_t buf;
    185 	usbd_status err;
    186 
    187 	usbnet_isowned_mii(un);
    188 
    189 	if (usbnet_isdying(un))
    190 		return 0;
    191 
    192 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    193 	req.bRequest = SMSC_UR_READ_REG;
    194 	USETW(req.wValue, 0);
    195 	USETW(req.wIndex, off);
    196 	USETW(req.wLength, 4);
    197 
    198 	err = usbd_do_request(un->un_udev, &req, &buf);
    199 	if (err != 0)
    200 		smsc_warn_printf(un, "Failed to read register 0x%0x\n", off);
    201 
    202 	*data = le32toh(buf);
    203 
    204 	return err;
    205 }
    206 
    207 int
    208 smsc_writereg(struct usbnet *un, uint32_t off, uint32_t data)
    209 {
    210 	usb_device_request_t req;
    211 	uint32_t buf;
    212 	usbd_status err;
    213 
    214 	usbnet_isowned_mii(un);
    215 
    216 	if (usbnet_isdying(un))
    217 		return 0;
    218 
    219 	buf = htole32(data);
    220 
    221 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    222 	req.bRequest = SMSC_UR_WRITE_REG;
    223 	USETW(req.wValue, 0);
    224 	USETW(req.wIndex, off);
    225 	USETW(req.wLength, 4);
    226 
    227 	err = usbd_do_request(un->un_udev, &req, &buf);
    228 	if (err != 0)
    229 		smsc_warn_printf(un, "Failed to write register 0x%0x\n", off);
    230 
    231 	return err;
    232 }
    233 
    234 int
    235 smsc_wait_for_bits(struct usbnet *un, uint32_t reg, uint32_t bits)
    236 {
    237 	uint32_t val;
    238 	int err, i;
    239 
    240 	for (i = 0; i < 100; i++) {
    241 		if ((err = smsc_readreg(un, reg, &val)) != 0)
    242 			return err;
    243 		if (!(val & bits))
    244 			return 0;
    245 		DELAY(5);
    246 	}
    247 
    248 	return 1;
    249 }
    250 
    251 usbd_status
    252 smsc_miibus_readreg(struct usbnet *un, int phy, int reg, uint16_t *val)
    253 {
    254 	uint32_t addr;
    255 	uint32_t data = 0;
    256 	int rv = 0;
    257 
    258 	usbnet_isowned_mii(un);
    259 
    260 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    261 		smsc_warn_printf(un, "MII is busy\n");
    262 		rv = -1;
    263 		goto done;
    264 	}
    265 
    266 	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
    267 	smsc_writereg(un, SMSC_MII_ADDR, addr);
    268 
    269 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    270 		smsc_warn_printf(un, "MII read timeout\n");
    271 		rv = ETIMEDOUT;
    272 	}
    273 
    274 	smsc_readreg(un, SMSC_MII_DATA, &data);
    275 
    276 done:
    277 	*val = data & 0xffff;
    278 	return rv;
    279 }
    280 
    281 usbd_status
    282 smsc_miibus_writereg(struct usbnet *un, int phy, int reg, uint16_t val)
    283 {
    284 	uint32_t addr;
    285 
    286 	usbnet_isowned_mii(un);
    287 
    288 	if (un->un_phyno != phy)
    289 		return -1;
    290 
    291 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    292 		smsc_warn_printf(un, "MII is busy\n");
    293 		return -1;
    294 	}
    295 
    296 	smsc_writereg(un, SMSC_MII_DATA, val);
    297 
    298 	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
    299 	smsc_writereg(un, SMSC_MII_ADDR, addr);
    300 
    301 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    302 		smsc_warn_printf(un, "MII write timeout\n");
    303 		return ETIMEDOUT;
    304 	}
    305 
    306 	return 0;
    307 }
    308 
    309 void
    310 smsc_miibus_statchg(struct ifnet *ifp)
    311 {
    312 	struct usbnet * const un = ifp->if_softc;
    313 
    314 	if (usbnet_isdying(un))
    315 		return;
    316 
    317 	struct smsc_softc * const sc = usbnet_softc(un);
    318 	struct mii_data * const mii = usbnet_mii(un);
    319 	uint32_t flow;
    320 	uint32_t afc_cfg;
    321 
    322 	usbnet_set_link(un, false);
    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 				usbnet_set_link(un, true);
    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 (!usbnet_havelink(un))
    340 		return;
    341 
    342 	usbnet_lock_mii(un);
    343 	int err = smsc_readreg(un, SMSC_AFC_CFG, &afc_cfg);
    344 	usbnet_unlock_mii(un);
    345 	if (err) {
    346 		smsc_warn_printf(un, "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(un, "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 	} else {
    367 		smsc_dbg_printf(un, "half duplex operation\n");
    368 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
    369 		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
    370 
    371 		flow = 0;
    372 		afc_cfg |= 0xf;
    373 	}
    374 
    375 	usbnet_lock_mii(un);
    376 	err = smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    377 	err += smsc_writereg(un, SMSC_FLOW, flow);
    378 	err += smsc_writereg(un, SMSC_AFC_CFG, afc_cfg);
    379 	usbnet_unlock_mii(un);
    380 
    381 	if (err)
    382 		smsc_warn_printf(un, "media change failed, error %d\n", err);
    383 }
    384 
    385 static inline uint32_t
    386 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
    387 {
    388 
    389 	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
    390 }
    391 
    392 static void
    393 smsc_setiff_locked(struct usbnet *un)
    394 {
    395     	struct smsc_softc * const sc = usbnet_softc(un);
    396 	struct ifnet * const ifp = usbnet_ifp(un);
    397 	struct ethercom *ec = usbnet_ec(un);
    398 	struct ether_multi *enm;
    399 	struct ether_multistep step;
    400 	uint32_t hashtbl[2] = { 0, 0 };
    401 	uint32_t hash;
    402 
    403 	usbnet_isowned_mii(un);
    404 
    405 	if (usbnet_isdying(un))
    406 		return;
    407 
    408 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
    409 allmulti:
    410 		smsc_dbg_printf(un, "receive all multicast enabled\n");
    411 		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
    412 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
    413 		smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    414 		return;
    415 	} else {
    416 		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
    417 		sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
    418 	}
    419 
    420 	ETHER_LOCK(ec);
    421 	ETHER_FIRST_MULTI(step, ec, enm);
    422 	while (enm != NULL) {
    423 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    424 			ETHER_UNLOCK(ec);
    425 			goto allmulti;
    426 		}
    427 
    428 		hash = smsc_hash(enm->enm_addrlo);
    429 		hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
    430 		ETHER_NEXT_MULTI(step, enm);
    431 	}
    432 	ETHER_UNLOCK(ec);
    433 
    434 	/* Debug */
    435 	if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
    436 		smsc_dbg_printf(un, "receive select group of macs\n");
    437 	} else {
    438 		smsc_dbg_printf(un, "receive own packets only\n");
    439 	}
    440 
    441 	/* Write the hash table and mac control registers */
    442 
    443 	//XXX should we be doing this?
    444 	ifp->if_flags &= ~IFF_ALLMULTI;
    445 	smsc_writereg(un, SMSC_HASHH, hashtbl[1]);
    446 	smsc_writereg(un, SMSC_HASHL, hashtbl[0]);
    447 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    448 }
    449 
    450 static void
    451 smsc_setiff(struct usbnet *un)
    452 {
    453 	usbnet_lock_mii(un);
    454 	smsc_setiff_locked(un);
    455 	usbnet_unlock_mii(un);
    456 }
    457 
    458 static int
    459 smsc_setoe_locked(struct usbnet *un)
    460 {
    461 	struct smsc_softc * const sc = usbnet_softc(un);
    462 	struct ifnet * const ifp = usbnet_ifp(un);
    463 	uint32_t val;
    464 	int err;
    465 
    466 	usbnet_isowned_mii(un);
    467 
    468 	err = smsc_readreg(un, SMSC_COE_CTRL, &val);
    469 	if (err != 0) {
    470 		smsc_warn_printf(un, "failed to read SMSC_COE_CTRL (err=%d)\n",
    471 		    err);
    472 		return err;
    473 	}
    474 
    475 	/* Enable/disable the Rx checksum */
    476 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
    477 		val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    478 	else
    479 		val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    480 
    481 	/* Enable/disable the Tx checksum (currently not supported) */
    482 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx))
    483 		val |= SMSC_COE_CTRL_TX_EN;
    484 	else
    485 		val &= ~SMSC_COE_CTRL_TX_EN;
    486 
    487 	sc->sc_coe_ctrl = val;
    488 
    489 	err = smsc_writereg(un, SMSC_COE_CTRL, val);
    490 	if (err != 0) {
    491 		smsc_warn_printf(un, "failed to write SMSC_COE_CTRL (err=%d)\n",
    492 		    err);
    493 		return err;
    494 	}
    495 
    496 	return 0;
    497 }
    498 
    499 static void
    500 smsc_setoe(struct usbnet *un)
    501 {
    502 
    503 	usbnet_lock_mii(un);
    504 	smsc_setoe_locked(un);
    505 	usbnet_unlock_mii(un);
    506 }
    507 
    508 
    509 int
    510 smsc_setmacaddress(struct usbnet *un, const uint8_t *addr)
    511 {
    512 	int err;
    513 	uint32_t val;
    514 
    515 	smsc_dbg_printf(un, "setting mac address to "
    516 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
    517 	    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
    518 
    519 	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
    520 	if ((err = smsc_writereg(un, SMSC_MAC_ADDRL, val)) != 0)
    521 		goto done;
    522 
    523 	val = (addr[5] << 8) | addr[4];
    524 	err = smsc_writereg(un, SMSC_MAC_ADDRH, val);
    525 
    526 done:
    527 	return err;
    528 }
    529 
    530 void
    531 smsc_reset(struct smsc_softc *sc)
    532 {
    533 	struct usbnet * const un = &sc->smsc_un;
    534 
    535 	usbnet_isowned(un);
    536 	if (usbnet_isdying(un))
    537 		return;
    538 
    539 	/* Wait a little while for the chip to get its brains in order. */
    540 	DELAY(1000);
    541 
    542 	/* Reinitialize controller to achieve full reset. */
    543 	smsc_chip_init(un);
    544 }
    545 
    546 int
    547 smsc_init(struct ifnet *ifp)
    548 {
    549 	struct usbnet * const un = ifp->if_softc;
    550 
    551 	usbnet_lock(un);
    552 	int ret = smsc_init_locked(ifp);
    553 	usbnet_unlock(un);
    554 
    555 	return ret;
    556 }
    557 
    558 int
    559 smsc_init_locked(struct ifnet *ifp)
    560 {
    561 	struct usbnet * const un = ifp->if_softc;
    562 	struct smsc_softc * const sc = usbnet_softc(un);
    563 
    564 	if (usbnet_isdying(un))
    565 		return EIO;
    566 
    567 	/* Cancel pending I/O */
    568 	usbnet_stop(un, ifp, 1);
    569 
    570 	/* Reset the ethernet interface. */
    571 	smsc_reset(sc);
    572 
    573 	usbnet_lock_mii_un_locked(un);
    574 
    575 	/* Load the multicast filter. */
    576 	smsc_setiff_locked(un);
    577 
    578 	/* TCP/UDP checksum offload engines. */
    579 	smsc_setoe_locked(un);
    580 
    581 	usbnet_unlock_mii_un_locked(un);
    582 
    583 	return usbnet_init_rx_tx(un);
    584 }
    585 
    586 void
    587 smsc_stop_cb(struct ifnet *ifp, int disable)
    588 {
    589 	struct usbnet * const un = ifp->if_softc;
    590 	struct smsc_softc * const sc = usbnet_softc(un);
    591 
    592 	// XXXNH didn't do this before
    593 	smsc_reset(sc);
    594 }
    595 
    596 int
    597 smsc_chip_init(struct usbnet *un)
    598 {
    599 	struct smsc_softc * const sc = usbnet_softc(un);
    600 	uint32_t reg_val;
    601 	int burst_cap;
    602 	int err;
    603 
    604 	usbnet_lock_mii_un_locked(un);
    605 
    606 	/* Enter H/W config mode */
    607 	smsc_writereg(un, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
    608 
    609 	if ((err = smsc_wait_for_bits(un, SMSC_HW_CFG,
    610 	    SMSC_HW_CFG_LRST)) != 0) {
    611 		smsc_warn_printf(un, "timed-out waiting for reset to "
    612 		    "complete\n");
    613 		goto init_failed;
    614 	}
    615 
    616 	/* Reset the PHY */
    617 	smsc_writereg(un, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
    618 
    619 	if ((err = smsc_wait_for_bits(un, SMSC_PM_CTRL,
    620 	    SMSC_PM_CTRL_PHY_RST)) != 0) {
    621 		smsc_warn_printf(un, "timed-out waiting for phy reset to "
    622 		    "complete\n");
    623 		goto init_failed;
    624 	}
    625 	usbd_delay_ms(un->un_udev, 40);
    626 
    627 	/* Set the mac address */
    628 	struct ifnet * const ifp = usbnet_ifp(un);
    629 	const char *eaddr = CLLADDR(ifp->if_sadl);
    630 	if ((err = smsc_setmacaddress(un, eaddr)) != 0) {
    631 		smsc_warn_printf(un, "failed to set the MAC address\n");
    632 		goto init_failed;
    633 	}
    634 
    635 	/*
    636 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
    637 	 * sequence as used in the Linux driver.
    638 	 */
    639 	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) != 0) {
    640 		smsc_warn_printf(un, "failed to read HW_CFG: %d\n", err);
    641 		goto init_failed;
    642 	}
    643 	reg_val |= SMSC_HW_CFG_BIR;
    644 	smsc_writereg(un, SMSC_HW_CFG, reg_val);
    645 
    646 	/*
    647 	 * There is a so called 'turbo mode' that the linux driver supports, it
    648 	 * seems to allow you to jam multiple frames per Rx transaction.
    649 	 * By default this driver supports that and therefore allows multiple
    650 	 * frames per USB transfer.
    651 	 *
    652 	 * The xfer buffer size needs to reflect this as well, therefore based
    653 	 * on the calculations in the Linux driver the RX bufsize is set to
    654 	 * 18944,
    655 	 *     bufsz = (16 * 1024 + 5 * 512)
    656 	 *
    657 	 * Burst capability is the number of URBs that can be in a burst of
    658 	 * data/ethernet frames.
    659 	 */
    660 
    661 	if (un->un_udev->ud_speed == USB_SPEED_HIGH)
    662 		burst_cap = 37;
    663 	else
    664 		burst_cap = 128;
    665 
    666 	smsc_writereg(un, SMSC_BURST_CAP, burst_cap);
    667 
    668 	/* Set the default bulk in delay (magic value from Linux driver) */
    669 	smsc_writereg(un, SMSC_BULK_IN_DLY, 0x00002000);
    670 
    671 	/*
    672 	 * Initialise the RX interface
    673 	 */
    674 	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) < 0) {
    675 		smsc_warn_printf(un, "failed to read HW_CFG: (err = %d)\n",
    676 		    err);
    677 		goto init_failed;
    678 	}
    679 
    680 	/*
    681 	 * The following settings are used for 'turbo mode', a.k.a multiple
    682 	 * frames per Rx transaction (again info taken form Linux driver).
    683 	 */
    684 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
    685 
    686 	/*
    687 	 * set Rx data offset to ETHER_ALIGN which will make the IP header
    688 	 * align on a word boundary.
    689 	 */
    690 	reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;
    691 
    692 	smsc_writereg(un, SMSC_HW_CFG, reg_val);
    693 
    694 	/* Clear the status register ? */
    695 	smsc_writereg(un, SMSC_INTR_STATUS, 0xffffffff);
    696 
    697 	/* Read and display the revision register */
    698 	if ((err = smsc_readreg(un, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
    699 		smsc_warn_printf(un, "failed to read ID_REV (err = %d)\n", err);
    700 		goto init_failed;
    701 	}
    702 
    703 	/* GPIO/LED setup */
    704 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
    705 	    SMSC_LED_GPIO_CFG_FDX_LED;
    706 	smsc_writereg(un, SMSC_LED_GPIO_CFG, reg_val);
    707 
    708 	/*
    709 	 * Initialise the TX interface
    710 	 */
    711 	smsc_writereg(un, SMSC_FLOW, 0);
    712 
    713 	smsc_writereg(un, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
    714 
    715 	/* Read the current MAC configuration */
    716 	if ((err = smsc_readreg(un, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
    717 		smsc_warn_printf(un, "failed to read MAC_CSR (err=%d)\n", err);
    718 		goto init_failed;
    719 	}
    720 
    721 	/* disable pad stripping, collides with checksum offload */
    722 	sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;
    723 
    724 	/* Vlan */
    725 	smsc_writereg(un, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
    726 
    727 	/*
    728 	 * Start TX
    729 	 */
    730 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
    731 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    732 	smsc_writereg(un, SMSC_TX_CFG, SMSC_TX_CFG_ON);
    733 
    734 	/*
    735 	 * Start RX
    736 	 */
    737 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
    738 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    739 	usbnet_unlock_mii_un_locked(un);
    740 
    741 	return 0;
    742 
    743 init_failed:
    744 	usbnet_unlock_mii_un_locked(un);
    745 	smsc_err_printf(un, "smsc_chip_init failed (err=%d)\n", err);
    746 	return err;
    747 }
    748 
    749 static int
    750 smsc_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data)
    751 {
    752 	struct usbnet * const un = ifp->if_softc;
    753 
    754 	switch (cmd) {
    755 	case SIOCSIFFLAGS:
    756 	case SIOCSETHERCAP:
    757 	case SIOCADDMULTI:
    758 	case SIOCDELMULTI:
    759 		smsc_setiff(un);
    760 		break;
    761 	case SIOCSIFCAP:
    762 		smsc_setoe(un);
    763 		break;
    764 	default:
    765 		break;
    766 	}
    767 
    768 	return 0;
    769 }
    770 
    771 int
    772 smsc_match(device_t parent, cfdata_t match, void *aux)
    773 {
    774 	struct usb_attach_arg *uaa = aux;
    775 
    776 	return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
    777 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    778 }
    779 
    780 void
    781 smsc_attach(device_t parent, device_t self, void *aux)
    782 {
    783 	struct smsc_softc * const sc = device_private(self);
    784 	struct usbnet * const un = &sc->smsc_un;
    785 	struct usb_attach_arg *uaa = aux;
    786 	struct usbd_device *dev = uaa->uaa_device;
    787 	usb_interface_descriptor_t *id;
    788 	usb_endpoint_descriptor_t *ed;
    789 	char *devinfop;
    790 	unsigned bufsz;
    791 	int err, i;
    792 	uint32_t mac_h, mac_l;
    793 
    794 	/* Switch to usbnet for device_private() */
    795 	self->dv_private = un;
    796 
    797 	aprint_naive("\n");
    798 	aprint_normal("\n");
    799 
    800 	un->un_dev = self;
    801 	un->un_udev = dev;
    802 	un->un_sc = sc;
    803 	un->un_ops = &smsc_ops;
    804 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    805 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    806 	un->un_rx_list_cnt = SMSC_RX_LIST_CNT;
    807 	un->un_tx_list_cnt = SMSC_TX_LIST_CNT;
    808 
    809 	devinfop = usbd_devinfo_alloc(un->un_udev, 0);
    810 	aprint_normal_dev(self, "%s\n", devinfop);
    811 	usbd_devinfo_free(devinfop);
    812 
    813 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
    814 	if (err) {
    815 		aprint_error_dev(self, "failed to set configuration"
    816 		    ", err=%s\n", usbd_errstr(err));
    817 		return;
    818 	}
    819 
    820 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
    821 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &un->un_iface);
    822 	if (err) {
    823 		aprint_error_dev(self, "getting interface handle failed\n");
    824 		return;
    825 	}
    826 
    827 	id = usbd_get_interface_descriptor(un->un_iface);
    828 
    829 	if (dev->ud_speed >= USB_SPEED_HIGH) {
    830 		bufsz = SMSC_MAX_BUFSZ;
    831 	} else {
    832 		bufsz = SMSC_MIN_BUFSZ;
    833 	}
    834 	un->un_rx_bufsz = bufsz;
    835 	un->un_tx_bufsz = bufsz;
    836 
    837 	/* Find endpoints. */
    838 	for (i = 0; i < id->bNumEndpoints; i++) {
    839 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    840 		if (!ed) {
    841 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    842 			return;
    843 		}
    844 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    845 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    846 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    847 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    848 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    849 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    850 #if 0 /* not used yet */
    851 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    852 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    853 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
    854 #endif
    855 		}
    856 	}
    857 
    858 	usbnet_attach(un, "smscdet");
    859 
    860 #ifdef notyet
    861 	/*
    862 	 * We can do TCPv4, and UDPv4 checksums in hardware.
    863 	 */
    864 	struct ifnet *ifp = usbnet_ifp(un);
    865 
    866 	ifp->if_capabilities |=
    867 	    /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
    868 	    /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
    869 #endif
    870 	struct ethercom *ec = usbnet_ec(un);
    871 	ec->ec_capabilities = ETHERCAP_VLAN_MTU;
    872 
    873 	/* Setup some of the basics */
    874 	un->un_phyno = 1;
    875 
    876 	usbnet_lock_mii(un);
    877 	/*
    878 	 * Attempt to get the mac address, if an EEPROM is not attached this
    879 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
    880 	 * address based on urandom.
    881 	 */
    882 	memset(un->un_eaddr, 0xff, ETHER_ADDR_LEN);
    883 
    884 	prop_dictionary_t dict = device_properties(self);
    885 	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
    886 
    887 	if (eaprop != NULL) {
    888 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
    889 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
    890 		memcpy(un->un_eaddr, prop_data_data_nocopy(eaprop),
    891 		    ETHER_ADDR_LEN);
    892 	} else {
    893 		/* Check if there is already a MAC address in the register */
    894 		if ((smsc_readreg(un, SMSC_MAC_ADDRL, &mac_l) == 0) &&
    895 		    (smsc_readreg(un, SMSC_MAC_ADDRH, &mac_h) == 0)) {
    896 			un->un_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
    897 			un->un_eaddr[4] = (uint8_t)((mac_h) & 0xff);
    898 			un->un_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
    899 			un->un_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
    900 			un->un_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
    901 			un->un_eaddr[0] = (uint8_t)((mac_l) & 0xff);
    902 		}
    903 	}
    904 	usbnet_unlock_mii(un);
    905 
    906 	usbnet_attach_ifp(un, true, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
    907 	    0, 0);
    908 }
    909 
    910 void
    911 smsc_rxeof_loop(struct usbnet * un, struct usbd_xfer *xfer,
    912     struct usbnet_chain *c, uint32_t total_len)
    913 {
    914 	struct smsc_softc * const sc = usbnet_softc(un);
    915 	struct ifnet *ifp = usbnet_ifp(un);
    916 	uint8_t *buf = c->unc_buf;
    917 
    918 	usbnet_isowned_rx(un);
    919 
    920 	while (total_len != 0) {
    921 		uint32_t rxhdr;
    922 		if (total_len < sizeof(rxhdr)) {
    923 			smsc_dbg_printf(un, "total_len %d < sizeof(rxhdr) %zu\n",
    924 			    total_len, sizeof(rxhdr));
    925 			ifp->if_ierrors++;
    926 			return;
    927 		}
    928 
    929 		memcpy(&rxhdr, buf, sizeof(rxhdr));
    930 		rxhdr = le32toh(rxhdr);
    931 		buf += sizeof(rxhdr);
    932 		total_len -= sizeof(rxhdr);
    933 
    934 		if (rxhdr & SMSC_RX_STAT_COLLISION)
    935 			ifp->if_collisions++;
    936 
    937 		if (rxhdr & (SMSC_RX_STAT_ERROR
    938 			   | SMSC_RX_STAT_LENGTH_ERROR
    939 			   | SMSC_RX_STAT_MII_ERROR)) {
    940 			smsc_dbg_printf(un, "rx error (hdr 0x%08x)\n", rxhdr);
    941 			ifp->if_ierrors++;
    942 			return;
    943 		}
    944 
    945 		uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
    946 		smsc_dbg_printf(un, "rxeof total_len %d pktlen %d rxhdr "
    947 		    "0x%08x\n", total_len, pktlen, rxhdr);
    948 
    949 		if (pktlen < ETHER_HDR_LEN) {
    950 			smsc_dbg_printf(un, "pktlen %d < ETHER_HDR_LEN %d\n",
    951 			    pktlen, ETHER_HDR_LEN);
    952 			ifp->if_ierrors++;
    953 			return;
    954 		}
    955 
    956 		pktlen += ETHER_ALIGN;
    957 
    958 		if (pktlen > MCLBYTES) {
    959 			smsc_dbg_printf(un, "pktlen %d > MCLBYTES %d\n",
    960 			    pktlen, MCLBYTES);
    961 			ifp->if_ierrors++;
    962 			return;
    963 		}
    964 
    965 		if (pktlen > total_len) {
    966 			smsc_dbg_printf(un, "pktlen %d > total_len %d\n",
    967 			    pktlen, total_len);
    968 			ifp->if_ierrors++;
    969 			return;
    970 		}
    971 
    972 		uint8_t *pktbuf = buf + ETHER_ALIGN;
    973 		size_t buflen = pktlen;
    974 		int mbuf_flags = M_HASFCS;
    975 		int csum_flags = 0;
    976 		uint16_t csum_data = 0;
    977 
    978  		KASSERT(pktlen < MCLBYTES);
    979 
    980 		/* Check if RX TCP/UDP checksumming is being offloaded */
    981 		if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
    982 			smsc_dbg_printf(un,"RX checksum offload checking\n");
    983 			struct ether_header *eh = (struct ether_header *)pktbuf;
    984 			const size_t cssz = sizeof(csum_data);
    985 
    986 			/* Remove the extra 2 bytes of the csum */
    987 			buflen -= cssz;
    988 
    989 			/*
    990 			 * The checksum appears to be simplistically calculated
    991 			 * over the udp/tcp header and data up to the end of the
    992 			 * eth frame.  Which means if the eth frame is padded
    993 			 * the csum calculation is incorrectly performed over
    994 			 * the padding bytes as well. Therefore to be safe we
    995 			 * ignore the H/W csum on frames less than or equal to
    996 			 * 64 bytes.
    997 			 *
    998 			 * Ignore H/W csum for non-IPv4 packets.
    999 			 */
   1000 			smsc_dbg_printf(un,"Ethertype %02x pktlen %02x\n",
   1001 			    be16toh(eh->ether_type), pktlen);
   1002 			if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
   1003 			    pktlen > ETHER_MIN_LEN) {
   1004 
   1005 				csum_flags |=
   1006 				    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);
   1007 
   1008 				/*
   1009 				 * Copy the TCP/UDP checksum from the last 2
   1010 				 * bytes of the transfer and put in the
   1011 				 * csum_data field.
   1012 				 */
   1013 				memcpy(&csum_data, buf + pktlen - cssz, cssz);
   1014 
   1015 				/*
   1016 				 * The data is copied in network order, but the
   1017 				 * csum algorithm in the kernel expects it to be
   1018 				 * in host network order.
   1019 				 */
   1020 				csum_data = ntohs(csum_data);
   1021 				smsc_dbg_printf(un,
   1022 				    "RX checksum offloaded (0x%04x)\n",
   1023 				    csum_data);
   1024 			}
   1025 		}
   1026 
   1027 		/* round up to next longword */
   1028 		pktlen = (pktlen + 3) & ~0x3;
   1029 
   1030 		/* total_len does not include the padding */
   1031 		if (pktlen > total_len)
   1032 			pktlen = total_len;
   1033 
   1034 		buf += pktlen;
   1035 		total_len -= pktlen;
   1036 
   1037 		/* push the packet up */
   1038 		usbnet_enqueue(un, pktbuf, buflen, csum_flags, csum_data,
   1039 		    mbuf_flags);
   1040 	}
   1041 }
   1042 
   1043 static unsigned
   1044 smsc_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
   1045 {
   1046 	uint32_t txhdr;
   1047 	uint32_t frm_len = 0;
   1048 
   1049 	usbnet_isowned_tx(un);
   1050 
   1051 	/*
   1052 	 * Each frame is prefixed with two 32-bit values describing the
   1053 	 * length of the packet and buffer.
   1054 	 */
   1055 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
   1056 	    SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
   1057 	txhdr = htole32(txhdr);
   1058 	memcpy(c->unc_buf, &txhdr, sizeof(txhdr));
   1059 
   1060 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
   1061 	txhdr = htole32(txhdr);
   1062 	memcpy(c->unc_buf + 4, &txhdr, sizeof(txhdr));
   1063 
   1064 	frm_len += 8;
   1065 
   1066 	/* Next copy in the actual packet */
   1067 	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + frm_len);
   1068 	frm_len += m->m_pkthdr.len;
   1069 
   1070 	return frm_len;
   1071 }
   1072 
   1073 MODULE(MODULE_CLASS_DRIVER, if_smsc, "usbnet");
   1074 
   1075 #ifdef _MODULE
   1076 #include "ioconf.c"
   1077 #endif
   1078 
   1079 static int
   1080 if_smsc_modcmd(modcmd_t cmd, void *aux)
   1081 {
   1082 	int error = 0;
   1083 
   1084 	switch (cmd) {
   1085 	case MODULE_CMD_INIT:
   1086 #ifdef _MODULE
   1087 		error = config_init_component(cfdriver_ioconf_smsc,
   1088 		    cfattach_ioconf_smsc, cfdata_ioconf_smsc);
   1089 #endif
   1090 		return error;
   1091 	case MODULE_CMD_FINI:
   1092 #ifdef _MODULE
   1093 		error = config_fini_component(cfdriver_ioconf_smsc,
   1094 		    cfattach_ioconf_smsc, cfdata_ioconf_smsc);
   1095 #endif
   1096 		return error;
   1097 	default:
   1098 		return ENOTTY;
   1099 	}
   1100 }
   1101