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