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