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