xref: /netbsd-src/sys/dev/usb/if_smsc.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: if_smsc.c,v 1.69 2020/06/27 13:33:26 jmcneill 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@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.69 2020/06/27 13:33:26 jmcneill 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 int	 smsc_init_locked(struct ifnet *);
178 static void	 smsc_uno_stop(struct ifnet *, int);
179 
180 static void	 smsc_reset(struct smsc_softc *);
181 
182 static void	 smsc_uno_miibus_statchg(struct ifnet *);
183 static int	 smsc_readreg(struct usbnet *, uint32_t, uint32_t *);
184 static int	 smsc_writereg(struct usbnet *, uint32_t, uint32_t);
185 static int	 smsc_wait_for_bits(struct usbnet *, uint32_t, uint32_t);
186 static int	 smsc_uno_miibus_readreg(struct usbnet *, int, int, uint16_t *);
187 static int	 smsc_uno_miibus_writereg(struct usbnet *, int, int, uint16_t);
188 
189 static int	 smsc_uno_ioctl(struct ifnet *, u_long, void *);
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_read_reg = smsc_uno_miibus_readreg,
199 	.uno_write_reg = smsc_uno_miibus_writereg,
200 	.uno_statchg = smsc_uno_miibus_statchg,
201 	.uno_tx_prepare = smsc_uno_tx_prepare,
202 	.uno_rx_loop = smsc_uno_rx_loop,
203 	.uno_init = smsc_uno_init,
204 };
205 
206 static int
207 smsc_readreg(struct usbnet *un, uint32_t off, uint32_t *data)
208 {
209 	usb_device_request_t req;
210 	uint32_t buf;
211 	usbd_status err;
212 
213 	usbnet_isowned_core(un);
214 
215 	if (usbnet_isdying(un))
216 		return 0;
217 
218 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
219 	req.bRequest = SMSC_UR_READ_REG;
220 	USETW(req.wValue, 0);
221 	USETW(req.wIndex, off);
222 	USETW(req.wLength, 4);
223 
224 	err = usbd_do_request(un->un_udev, &req, &buf);
225 	if (err != 0)
226 		smsc_warn_printf(un, "Failed to read register 0x%0x\n", off);
227 
228 	*data = le32toh(buf);
229 
230 	return err;
231 }
232 
233 static int
234 smsc_writereg(struct usbnet *un, uint32_t off, uint32_t data)
235 {
236 	usb_device_request_t req;
237 	uint32_t buf;
238 	usbd_status err;
239 
240 	usbnet_isowned_core(un);
241 
242 	if (usbnet_isdying(un))
243 		return 0;
244 
245 	buf = htole32(data);
246 
247 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
248 	req.bRequest = SMSC_UR_WRITE_REG;
249 	USETW(req.wValue, 0);
250 	USETW(req.wIndex, off);
251 	USETW(req.wLength, 4);
252 
253 	err = usbd_do_request(un->un_udev, &req, &buf);
254 	if (err != 0)
255 		smsc_warn_printf(un, "Failed to write register 0x%0x\n", off);
256 
257 	return err;
258 }
259 
260 static int
261 smsc_wait_for_bits(struct usbnet *un, uint32_t reg, uint32_t bits)
262 {
263 	uint32_t val;
264 	int err, i;
265 
266 	for (i = 0; i < 100; i++) {
267 		if ((err = smsc_readreg(un, reg, &val)) != 0)
268 			return err;
269 		if (!(val & bits))
270 			return 0;
271 		DELAY(5);
272 	}
273 
274 	return 1;
275 }
276 
277 static int
278 smsc_uno_miibus_readreg(struct usbnet *un, int phy, int reg, uint16_t *val)
279 {
280 	uint32_t addr;
281 	uint32_t data = 0;
282 
283 	if (un->un_phyno != phy)
284 		return EINVAL;
285 
286 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
287 		smsc_warn_printf(un, "MII is busy\n");
288 		return ETIMEDOUT;
289 	}
290 
291 	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
292 	smsc_writereg(un, SMSC_MII_ADDR, addr);
293 
294 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
295 		smsc_warn_printf(un, "MII read timeout\n");
296 		return ETIMEDOUT;
297 	}
298 
299 	smsc_readreg(un, SMSC_MII_DATA, &data);
300 
301 	*val = data & 0xffff;
302 	return 0;
303 }
304 
305 static int
306 smsc_uno_miibus_writereg(struct usbnet *un, int phy, int reg, uint16_t val)
307 {
308 	uint32_t addr;
309 
310 	if (un->un_phyno != phy)
311 		return EINVAL;
312 
313 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
314 		smsc_warn_printf(un, "MII is busy\n");
315 		return ETIMEDOUT;
316 	}
317 
318 	smsc_writereg(un, SMSC_MII_DATA, val);
319 
320 	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
321 	smsc_writereg(un, SMSC_MII_ADDR, addr);
322 
323 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
324 		smsc_warn_printf(un, "MII write timeout\n");
325 		return ETIMEDOUT;
326 	}
327 
328 	return 0;
329 }
330 
331 static void
332 smsc_uno_miibus_statchg(struct ifnet *ifp)
333 {
334 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
335 	struct usbnet * const un = ifp->if_softc;
336 
337 	if (usbnet_isdying(un))
338 		return;
339 
340 	struct smsc_softc * const sc = usbnet_softc(un);
341 	struct mii_data * const mii = usbnet_mii(un);
342 	uint32_t flow;
343 	uint32_t afc_cfg;
344 
345 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
346 	    (IFM_ACTIVE | IFM_AVALID)) {
347 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
348 			case IFM_10_T:
349 			case IFM_100_TX:
350 				usbnet_set_link(un, true);
351 				break;
352 			case IFM_1000_T:
353 				/* Gigabit ethernet not supported by chipset */
354 				break;
355 			default:
356 				break;
357 		}
358 	}
359 
360 	/* Lost link, do nothing. */
361 	if (!usbnet_havelink(un))
362 		return;
363 
364 	int err = smsc_readreg(un, SMSC_AFC_CFG, &afc_cfg);
365 	if (err) {
366 		smsc_warn_printf(un, "failed to read initial AFC_CFG, "
367 		    "error %d\n", err);
368 		return;
369 	}
370 
371 	/* Enable/disable full duplex operation and TX/RX pause */
372 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
373 		DPRINTF("full duplex operation", 0, 0, 0, 0);
374 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
375 		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
376 
377 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
378 			flow = 0xffff0002;
379 		else
380 			flow = 0;
381 
382 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
383 			afc_cfg |= 0xf;
384 		else
385 			afc_cfg &= ~0xf;
386 	} else {
387 		DPRINTF("half duplex operation", 0, 0, 0, 0);
388 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
389 		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
390 
391 		flow = 0;
392 		afc_cfg |= 0xf;
393 	}
394 
395 	err = smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
396 	err += smsc_writereg(un, SMSC_FLOW, flow);
397 	err += smsc_writereg(un, SMSC_AFC_CFG, afc_cfg);
398 
399 	if (err)
400 		smsc_warn_printf(un, "media change failed, error %d\n", err);
401 }
402 
403 static inline uint32_t
404 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
405 {
406 
407 	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
408 }
409 
410 static void
411 smsc_setiff_locked(struct usbnet *un)
412 {
413 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
414 	struct smsc_softc * const sc = usbnet_softc(un);
415 	struct ifnet * const ifp = usbnet_ifp(un);
416 	struct ethercom *ec = usbnet_ec(un);
417 	struct ether_multi *enm;
418 	struct ether_multistep step;
419 	uint32_t hashtbl[2] = { 0, 0 };
420 	uint32_t hash;
421 
422 	usbnet_isowned_core(un);
423 
424 	if (usbnet_isdying(un))
425 		return;
426 
427 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
428 allmulti:
429 		DPRINTF("receive all multicast enabled", 0, 0, 0, 0);
430 		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
431 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
432 		smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
433 		return;
434 	} else {
435 		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
436 		sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
437 	}
438 
439 	ETHER_LOCK(ec);
440 	ETHER_FIRST_MULTI(step, ec, enm);
441 	while (enm != NULL) {
442 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
443 			ETHER_UNLOCK(ec);
444 			goto allmulti;
445 		}
446 
447 		hash = smsc_hash(enm->enm_addrlo);
448 		hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
449 		ETHER_NEXT_MULTI(step, enm);
450 	}
451 	ETHER_UNLOCK(ec);
452 
453 	/* Debug */
454 	if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
455 		DPRINTF("receive select group of macs", 0, 0, 0, 0);
456 	} else {
457 		DPRINTF("receive own packets only", 0, 0, 0, 0);
458 	}
459 
460 	/* Write the hash table and mac control registers */
461 
462 	//XXX should we be doing this?
463 	ifp->if_flags &= ~IFF_ALLMULTI;
464 	smsc_writereg(un, SMSC_HASHH, hashtbl[1]);
465 	smsc_writereg(un, SMSC_HASHL, hashtbl[0]);
466 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
467 }
468 
469 static int
470 smsc_setoe_locked(struct usbnet *un)
471 {
472 	struct smsc_softc * const sc = usbnet_softc(un);
473 	struct ifnet * const ifp = usbnet_ifp(un);
474 	uint32_t val;
475 	int err;
476 
477 	usbnet_isowned_core(un);
478 
479 	err = smsc_readreg(un, SMSC_COE_CTRL, &val);
480 	if (err != 0) {
481 		smsc_warn_printf(un, "failed to read SMSC_COE_CTRL (err=%d)\n",
482 		    err);
483 		return err;
484 	}
485 
486 	/* Enable/disable the Rx checksum */
487 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
488 		val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
489 	else
490 		val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
491 
492 	/* Enable/disable the Tx checksum (currently not supported) */
493 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx))
494 		val |= SMSC_COE_CTRL_TX_EN;
495 	else
496 		val &= ~SMSC_COE_CTRL_TX_EN;
497 
498 	sc->sc_coe_ctrl = val;
499 
500 	err = smsc_writereg(un, SMSC_COE_CTRL, val);
501 	if (err != 0) {
502 		smsc_warn_printf(un, "failed to write SMSC_COE_CTRL (err=%d)\n",
503 		    err);
504 		return err;
505 	}
506 
507 	return 0;
508 }
509 
510 static int
511 smsc_setmacaddress(struct usbnet *un, const uint8_t *addr)
512 {
513 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
514 	int err;
515 	uint32_t val;
516 
517 	DPRINTF("setting mac address to %02jx:%02jx:%02jx:...", addr[0],
518 	    addr[1], addr[2], 0);
519 
520 	DPRINTF("... %02jx:%02jx:%02jx", addr[3], addr[4], addr[5], 0);
521 
522 	val = ((uint32_t)addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8)
523 	    | addr[0];
524 	if ((err = smsc_writereg(un, SMSC_MAC_ADDRL, val)) != 0)
525 		goto done;
526 
527 	val = (addr[5] << 8) | addr[4];
528 	err = smsc_writereg(un, SMSC_MAC_ADDRH, val);
529 
530 done:
531 	return err;
532 }
533 
534 static void
535 smsc_reset(struct smsc_softc *sc)
536 {
537 	struct usbnet * const un = &sc->smsc_un;
538 
539 	usbnet_isowned_core(un);
540 	if (usbnet_isdying(un))
541 		return;
542 
543 	/* Wait a little while for the chip to get its brains in order. */
544 	DELAY(1000);
545 
546 	/* Reinitialize controller to achieve full reset. */
547 	smsc_chip_init(un);
548 }
549 
550 static int
551 smsc_uno_init(struct ifnet *ifp)
552 {
553 	struct usbnet * const un = ifp->if_softc;
554 
555 	usbnet_lock_core(un);
556 	usbnet_busy(un);
557 	int ret = smsc_init_locked(ifp);
558 	usbnet_unbusy(un);
559 	usbnet_unlock_core(un);
560 
561 	return ret;
562 }
563 
564 static int
565 smsc_init_locked(struct ifnet *ifp)
566 {
567 	struct usbnet * const un = ifp->if_softc;
568 	struct smsc_softc * const sc = usbnet_softc(un);
569 
570 	usbnet_isowned_core(un);
571 
572 	if (usbnet_isdying(un))
573 		return EIO;
574 
575 	/* Cancel pending I/O */
576 	usbnet_stop(un, ifp, 1);
577 
578 	/* Reset the ethernet interface. */
579 	smsc_reset(sc);
580 
581 	/* Load the multicast filter. */
582 	smsc_setiff_locked(un);
583 
584 	/* TCP/UDP checksum offload engines. */
585 	smsc_setoe_locked(un);
586 
587 	return usbnet_init_rx_tx(un);
588 }
589 
590 static void
591 smsc_uno_stop(struct ifnet *ifp, int disable)
592 {
593 	struct usbnet * const un = ifp->if_softc;
594 	struct smsc_softc * const sc = usbnet_softc(un);
595 
596 	// XXXNH didn't do this before
597 	smsc_reset(sc);
598 }
599 
600 static int
601 smsc_chip_init(struct usbnet *un)
602 {
603 	struct smsc_softc * const sc = usbnet_softc(un);
604 	uint32_t reg_val;
605 	int burst_cap;
606 	int err;
607 
608 	usbnet_isowned_core(un);
609 
610 	/* Enter H/W config mode */
611 	smsc_writereg(un, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
612 
613 	if ((err = smsc_wait_for_bits(un, SMSC_HW_CFG,
614 	    SMSC_HW_CFG_LRST)) != 0) {
615 		smsc_warn_printf(un, "timed-out waiting for reset to "
616 		    "complete\n");
617 		goto init_failed;
618 	}
619 
620 	/* Reset the PHY */
621 	smsc_writereg(un, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
622 
623 	if ((err = smsc_wait_for_bits(un, SMSC_PM_CTRL,
624 	    SMSC_PM_CTRL_PHY_RST)) != 0) {
625 		smsc_warn_printf(un, "timed-out waiting for phy reset to "
626 		    "complete\n");
627 		goto init_failed;
628 	}
629 	usbd_delay_ms(un->un_udev, 40);
630 
631 	/* Set the mac address */
632 	struct ifnet * const ifp = usbnet_ifp(un);
633 	const char *eaddr = CLLADDR(ifp->if_sadl);
634 	if ((err = smsc_setmacaddress(un, eaddr)) != 0) {
635 		smsc_warn_printf(un, "failed to set the MAC address\n");
636 		goto init_failed;
637 	}
638 
639 	/*
640 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
641 	 * sequence as used in the Linux driver.
642 	 */
643 	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) != 0) {
644 		smsc_warn_printf(un, "failed to read HW_CFG: %d\n", err);
645 		goto init_failed;
646 	}
647 	reg_val |= SMSC_HW_CFG_BIR;
648 	smsc_writereg(un, SMSC_HW_CFG, reg_val);
649 
650 	/*
651 	 * There is a so called 'turbo mode' that the linux driver supports, it
652 	 * seems to allow you to jam multiple frames per Rx transaction.
653 	 * By default this driver supports that and therefore allows multiple
654 	 * frames per USB transfer.
655 	 *
656 	 * The xfer buffer size needs to reflect this as well, therefore based
657 	 * on the calculations in the Linux driver the RX bufsize is set to
658 	 * 18944,
659 	 *     bufsz = (16 * 1024 + 5 * 512)
660 	 *
661 	 * Burst capability is the number of URBs that can be in a burst of
662 	 * data/ethernet frames.
663 	 */
664 
665 	if (un->un_udev->ud_speed == USB_SPEED_HIGH)
666 		burst_cap = 37;
667 	else
668 		burst_cap = 128;
669 
670 	smsc_writereg(un, SMSC_BURST_CAP, burst_cap);
671 
672 	/* Set the default bulk in delay (magic value from Linux driver) */
673 	smsc_writereg(un, SMSC_BULK_IN_DLY, 0x00002000);
674 
675 	/*
676 	 * Initialise the RX interface
677 	 */
678 	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) < 0) {
679 		smsc_warn_printf(un, "failed to read HW_CFG: (err = %d)\n",
680 		    err);
681 		goto init_failed;
682 	}
683 
684 	/*
685 	 * The following settings are used for 'turbo mode', a.k.a multiple
686 	 * frames per Rx transaction (again info taken form Linux driver).
687 	 */
688 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
689 
690 	/*
691 	 * set Rx data offset to ETHER_ALIGN which will make the IP header
692 	 * align on a word boundary.
693 	 */
694 	reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;
695 
696 	smsc_writereg(un, SMSC_HW_CFG, reg_val);
697 
698 	/* Clear the status register ? */
699 	smsc_writereg(un, SMSC_INTR_STATUS, 0xffffffff);
700 
701 	/* Read and display the revision register */
702 	if ((err = smsc_readreg(un, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
703 		smsc_warn_printf(un, "failed to read ID_REV (err = %d)\n", err);
704 		goto init_failed;
705 	}
706 
707 	/* GPIO/LED setup */
708 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
709 	    SMSC_LED_GPIO_CFG_FDX_LED;
710 	smsc_writereg(un, SMSC_LED_GPIO_CFG, reg_val);
711 
712 	/*
713 	 * Initialise the TX interface
714 	 */
715 	smsc_writereg(un, SMSC_FLOW, 0);
716 
717 	smsc_writereg(un, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
718 
719 	/* Read the current MAC configuration */
720 	if ((err = smsc_readreg(un, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
721 		smsc_warn_printf(un, "failed to read MAC_CSR (err=%d)\n", err);
722 		goto init_failed;
723 	}
724 
725 	/* disable pad stripping, collides with checksum offload */
726 	sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;
727 
728 	/* Vlan */
729 	smsc_writereg(un, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
730 
731 	/*
732 	 * Start TX
733 	 */
734 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
735 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
736 	smsc_writereg(un, SMSC_TX_CFG, SMSC_TX_CFG_ON);
737 
738 	/*
739 	 * Start RX
740 	 */
741 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
742 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
743 
744 	return 0;
745 
746 init_failed:
747 	smsc_err_printf(un, "smsc_chip_init failed (err=%d)\n", err);
748 	return err;
749 }
750 
751 static int
752 smsc_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
753 {
754 	struct usbnet * const un = ifp->if_softc;
755 
756 	usbnet_lock_core(un);
757 	usbnet_busy(un);
758 
759 	switch (cmd) {
760 	case SIOCSIFFLAGS:
761 	case SIOCSETHERCAP:
762 	case SIOCADDMULTI:
763 	case SIOCDELMULTI:
764 		smsc_setiff_locked(un);
765 		break;
766 	case SIOCSIFCAP:
767 		smsc_setoe_locked(un);
768 		break;
769 	default:
770 		break;
771 	}
772 
773 	usbnet_unbusy(un);
774 	usbnet_unlock_core(un);
775 
776 	return 0;
777 }
778 
779 static int
780 smsc_match(device_t parent, cfdata_t match, void *aux)
781 {
782 	struct usb_attach_arg *uaa = aux;
783 
784 	return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
785 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
786 }
787 
788 static void
789 smsc_attach(device_t parent, device_t self, void *aux)
790 {
791 	USBNET_MII_DECL_DEFAULT(unm);
792 	struct smsc_softc * const sc = device_private(self);
793 	struct usbnet * const un = &sc->smsc_un;
794 	struct usb_attach_arg *uaa = aux;
795 	struct usbd_device *dev = uaa->uaa_device;
796 	usb_interface_descriptor_t *id;
797 	usb_endpoint_descriptor_t *ed;
798 	char *devinfop;
799 	unsigned bufsz;
800 	int err, i;
801 	uint32_t mac_h, mac_l;
802 
803 	KASSERT((void *)sc == un);
804 
805 	aprint_naive("\n");
806 	aprint_normal("\n");
807 
808 	un->un_dev = self;
809 	un->un_udev = dev;
810 	un->un_sc = sc;
811 	un->un_ops = &smsc_ops;
812 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
813 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
814 	un->un_rx_list_cnt = SMSC_RX_LIST_CNT;
815 	un->un_tx_list_cnt = SMSC_TX_LIST_CNT;
816 
817 	devinfop = usbd_devinfo_alloc(un->un_udev, 0);
818 	aprint_normal_dev(self, "%s\n", devinfop);
819 	usbd_devinfo_free(devinfop);
820 
821 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
822 	if (err) {
823 		aprint_error_dev(self, "failed to set configuration"
824 		    ", err=%s\n", usbd_errstr(err));
825 		return;
826 	}
827 
828 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
829 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &un->un_iface);
830 	if (err) {
831 		aprint_error_dev(self, "getting interface handle failed\n");
832 		return;
833 	}
834 
835 	id = usbd_get_interface_descriptor(un->un_iface);
836 
837 	if (dev->ud_speed >= USB_SPEED_HIGH) {
838 		bufsz = SMSC_MAX_BUFSZ;
839 	} else {
840 		bufsz = SMSC_MIN_BUFSZ;
841 	}
842 	un->un_rx_bufsz = bufsz;
843 	un->un_tx_bufsz = bufsz;
844 
845 	/* Find endpoints. */
846 	for (i = 0; i < id->bNumEndpoints; i++) {
847 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
848 		if (!ed) {
849 			aprint_error_dev(self, "couldn't get ep %d\n", i);
850 			return;
851 		}
852 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
853 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
854 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
855 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
856 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
857 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
858 #if 0 /* not used yet */
859 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
860 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
861 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
862 #endif
863 		}
864 	}
865 
866 	usbnet_attach(un, "smscdet");
867 
868 #ifdef notyet
869 	/*
870 	 * We can do TCPv4, and UDPv4 checksums in hardware.
871 	 */
872 	struct ifnet *ifp = usbnet_ifp(un);
873 
874 	ifp->if_capabilities |=
875 	    /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
876 	    /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
877 #endif
878 	struct ethercom *ec = usbnet_ec(un);
879 	ec->ec_capabilities = ETHERCAP_VLAN_MTU;
880 
881 	/* Setup some of the basics */
882 	un->un_phyno = 1;
883 
884 	usbnet_lock_core(un);
885 	usbnet_busy(un);
886 	/*
887 	 * Attempt to get the mac address, if an EEPROM is not attached this
888 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
889 	 * address based on urandom.
890 	 */
891 	memset(un->un_eaddr, 0xff, ETHER_ADDR_LEN);
892 
893 	prop_dictionary_t dict = device_properties(self);
894 	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
895 
896 	if (eaprop != NULL) {
897 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
898 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
899 		memcpy(un->un_eaddr, prop_data_value(eaprop),
900 		    ETHER_ADDR_LEN);
901 	} else {
902 		/* Check if there is already a MAC address in the register */
903 		if ((smsc_readreg(un, SMSC_MAC_ADDRL, &mac_l) == 0) &&
904 		    (smsc_readreg(un, SMSC_MAC_ADDRH, &mac_h) == 0)) {
905 			un->un_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
906 			un->un_eaddr[4] = (uint8_t)((mac_h) & 0xff);
907 			un->un_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
908 			un->un_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
909 			un->un_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
910 			un->un_eaddr[0] = (uint8_t)((mac_l) & 0xff);
911 		}
912 	}
913 	usbnet_unbusy(un);
914 	usbnet_unlock_core(un);
915 
916 	usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
917 	    0, &unm);
918 }
919 
920 static void
921 smsc_uno_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
922 {
923 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
924 	struct smsc_softc * const sc = usbnet_softc(un);
925 	struct ifnet *ifp = usbnet_ifp(un);
926 	uint8_t *buf = c->unc_buf;
927 
928 	DPRINTF("total_len %jd/%#jx", total_len, total_len, 0, 0);
929 	while (total_len != 0) {
930 		uint32_t rxhdr;
931 		if (total_len < sizeof(rxhdr)) {
932 			DPRINTF("total_len %jd < sizeof(rxhdr) %jd",
933 			    total_len, sizeof(rxhdr), 0, 0);
934 			if_statinc(ifp, if_ierrors);
935 			return;
936 		}
937 
938 		memcpy(&rxhdr, buf, sizeof(rxhdr));
939 		rxhdr = le32toh(rxhdr);
940 		buf += sizeof(rxhdr);
941 		total_len -= sizeof(rxhdr);
942 
943 		if (rxhdr & SMSC_RX_STAT_COLLISION)
944 			if_statinc(ifp, if_collisions);
945 
946 		if (rxhdr & (SMSC_RX_STAT_ERROR
947 			   | SMSC_RX_STAT_LENGTH_ERROR
948 			   | SMSC_RX_STAT_MII_ERROR)) {
949 			DPRINTF("rx error (hdr 0x%08jx)", rxhdr, 0, 0, 0);
950 			if_statinc(ifp, if_ierrors);
951 			return;
952 		}
953 
954 		uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
955 		DPRINTF("total_len %jd pktlen %jd rxhdr 0x%08jx", total_len,
956 		    pktlen, rxhdr, 0);
957 
958 		if (pktlen < ETHER_HDR_LEN) {
959 			DPRINTF("pktlen %jd < ETHER_HDR_LEN %jd", pktlen,
960 			    ETHER_HDR_LEN, 0, 0);
961 			if_statinc(ifp, if_ierrors);
962 			return;
963 		}
964 
965 		pktlen += ETHER_ALIGN;
966 
967 		if (pktlen > MCLBYTES) {
968 			DPRINTF("pktlen %jd > MCLBYTES %jd", pktlen, MCLBYTES, 0,
969 			    0);
970 			if_statinc(ifp, if_ierrors);
971 			return;
972 		}
973 
974 		if (pktlen > total_len) {
975 			DPRINTF("pktlen %jd > total_len %jd", pktlen, total_len,
976 			    0, 0);
977 			if_statinc(ifp, if_ierrors);
978 			return;
979 		}
980 
981 		uint8_t *pktbuf = buf + ETHER_ALIGN;
982 		size_t buflen = pktlen - ETHER_ALIGN;
983 		int mbuf_flags = M_HASFCS;
984 		int csum_flags = 0;
985 		uint16_t csum_data = 0;
986 
987  		KASSERT(pktlen < MCLBYTES);
988 
989 		/* Check if RX TCP/UDP checksumming is being offloaded */
990 		if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
991 			DPRINTF("RX checksum offload checking", 0, 0, 0, 0);
992 			struct ether_header *eh = (struct ether_header *)pktbuf;
993 			const size_t cssz = sizeof(csum_data);
994 
995 			/* Remove the extra 2 bytes of the csum */
996 			buflen -= cssz;
997 
998 			/*
999 			 * The checksum appears to be simplistically calculated
1000 			 * over the udp/tcp header and data up to the end of the
1001 			 * eth frame.  Which means if the eth frame is padded
1002 			 * the csum calculation is incorrectly performed over
1003 			 * the padding bytes as well. Therefore to be safe we
1004 			 * ignore the H/W csum on frames less than or equal to
1005 			 * 64 bytes.
1006 			 *
1007 			 * Ignore H/W csum for non-IPv4 packets.
1008 			 */
1009 			DPRINTF("Ethertype %02jx pktlen %02jx",
1010 			    be16toh(eh->ether_type), pktlen, 0, 0);
1011 			if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
1012 			    pktlen > ETHER_MIN_LEN) {
1013 
1014 				csum_flags |=
1015 				    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);
1016 
1017 				/*
1018 				 * Copy the TCP/UDP checksum from the last 2
1019 				 * bytes of the transfer and put in the
1020 				 * csum_data field.
1021 				 */
1022 				memcpy(&csum_data, buf + pktlen - cssz, cssz);
1023 
1024 				/*
1025 				 * The data is copied in network order, but the
1026 				 * csum algorithm in the kernel expects it to be
1027 				 * in host network order.
1028 				 */
1029 				csum_data = ntohs(csum_data);
1030 				DPRINTF("RX checksum offloaded (0x%04jx)",
1031 				    csum_data, 0, 0, 0);
1032 			}
1033 		}
1034 
1035 		/* round up to next longword */
1036 		pktlen = (pktlen + 3) & ~0x3;
1037 
1038 		/* total_len does not include the padding */
1039 		if (pktlen > total_len)
1040 			pktlen = total_len;
1041 
1042 		buf += pktlen;
1043 		total_len -= pktlen;
1044 
1045 		/* push the packet up */
1046 		usbnet_enqueue(un, pktbuf, buflen, csum_flags, csum_data,
1047 		    mbuf_flags);
1048 	}
1049 }
1050 
1051 static unsigned
1052 smsc_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
1053 {
1054 	uint32_t txhdr;
1055 	uint32_t frm_len = 0;
1056 
1057 	const size_t hdrsz = sizeof(txhdr) * 2;
1058 
1059 	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - hdrsz)
1060 		return 0;
1061 
1062 	/*
1063 	 * Each frame is prefixed with two 32-bit values describing the
1064 	 * length of the packet and buffer.
1065 	 */
1066 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1067 	    SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1068 	txhdr = htole32(txhdr);
1069 	memcpy(c->unc_buf, &txhdr, sizeof(txhdr));
1070 
1071 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1072 	txhdr = htole32(txhdr);
1073 	memcpy(c->unc_buf + sizeof(txhdr), &txhdr, sizeof(txhdr));
1074 
1075 	frm_len += hdrsz;
1076 
1077 	/* Next copy in the actual packet */
1078 	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + frm_len);
1079 	frm_len += m->m_pkthdr.len;
1080 
1081 	return frm_len;
1082 }
1083 
1084 #ifdef _MODULE
1085 #include "ioconf.c"
1086 #endif
1087 
1088 USBNET_MODULE(smsc)
1089