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