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