if_smsc.c revision 1.41 1 /* $NetBSD: if_smsc.c,v 1.41 2019/01/27 02:08:42 pgoyette 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.41 2019/01/27 02:08:42 pgoyette Exp $");
65
66 #ifdef _KERNEL_OPT
67 #include "opt_usb.h"
68 #include "opt_inet.h"
69 #endif
70
71 #include <sys/param.h>
72 #include <sys/bus.h>
73 #include <sys/device.h>
74 #include <sys/kernel.h>
75 #include <sys/mbuf.h>
76 #include <sys/mutex.h>
77 #include <sys/proc.h>
78 #include <sys/rndsource.h>
79 #include <sys/socket.h>
80 #include <sys/sockio.h>
81 #include <sys/systm.h>
82
83 #include <net/if.h>
84 #include <net/if_dl.h>
85 #include <net/if_media.h>
86 #include <net/if_ether.h>
87
88 #include <net/bpf.h>
89
90 #ifdef INET
91 #include <netinet/in.h>
92 #include <netinet/if_inarp.h>
93 #endif
94
95 #include <dev/mii/mii.h>
96 #include <dev/mii/miivar.h>
97
98 #include <dev/usb/usb.h>
99 #include <dev/usb/usbdi.h>
100 #include <dev/usb/usbdi_util.h>
101 #include <dev/usb/usbdivar.h>
102 #include <dev/usb/usbdevs.h>
103
104 #include <dev/usb/if_smscreg.h>
105 #include <dev/usb/if_smscvar.h>
106
107 #include "ioconf.h"
108
109 #ifdef USB_DEBUG
110 int smsc_debug = 0;
111 #endif
112
113 #define ETHER_ALIGN 2
114 /*
115 * Various supported device vendors/products.
116 */
117 static const struct usb_devno smsc_devs[] = {
118 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN89530 },
119 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9530 },
120 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9730 },
121 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500 },
122 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A },
123 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_ALT },
124 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_HAL },
125 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
126 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_ALT },
127 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_SAL10 },
128 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505 },
129 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A },
130 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_HAL },
131 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
132 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505_SAL10 },
133 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14 },
134 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_ALT },
135 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
136 };
137
138 #ifdef USB_DEBUG
139 #define smsc_dbg_printf(sc, fmt, args...) \
140 do { \
141 if (smsc_debug > 0) \
142 printf("debug: " fmt, ##args); \
143 } while(0)
144 #else
145 #define smsc_dbg_printf(sc, fmt, args...)
146 #endif
147
148 #define smsc_warn_printf(sc, fmt, args...) \
149 printf("%s: warning: " fmt, device_xname((sc)->sc_dev), ##args)
150
151 #define smsc_err_printf(sc, fmt, args...) \
152 printf("%s: error: " fmt, device_xname((sc)->sc_dev), ##args)
153
154 /* Function declarations */
155 int smsc_chip_init(struct smsc_softc *);
156 void smsc_setmulti(struct smsc_softc *);
157 int smsc_setmacaddress(struct smsc_softc *, const uint8_t *);
158
159 int smsc_match(device_t, cfdata_t, void *);
160 void smsc_attach(device_t, device_t, void *);
161 int smsc_detach(device_t, int);
162 int smsc_activate(device_t, enum devact);
163
164 int smsc_init(struct ifnet *);
165 int smsc_init_locked(struct ifnet *);
166 void smsc_start(struct ifnet *);
167 void smsc_start_locked(struct ifnet *);
168 int smsc_ioctl(struct ifnet *, u_long, void *);
169 void smsc_stop(struct ifnet *, int);
170 void smsc_stop_locked(struct ifnet *, int);
171
172 void smsc_reset(struct smsc_softc *);
173 struct mbuf *smsc_newbuf(void);
174
175 void smsc_tick(void *);
176 void smsc_tick_task(void *);
177 void smsc_miibus_statchg(struct ifnet *);
178 void smsc_miibus_statchg_locked(struct ifnet *);
179 int smsc_miibus_readreg(device_t, int, int, uint16_t *);
180 int smsc_miibus_writereg(device_t, int, int, uint16_t);
181 int smsc_ifmedia_upd(struct ifnet *);
182 void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
183 void smsc_lock_mii(struct smsc_softc *);
184 void smsc_unlock_mii(struct smsc_softc *);
185
186 int smsc_tx_list_init(struct smsc_softc *);
187 void smsc_tx_list_free(struct smsc_softc *);
188 int smsc_rx_list_init(struct smsc_softc *);
189 void smsc_rx_list_free(struct smsc_softc *);
190 int smsc_encap(struct smsc_softc *, struct mbuf *, int);
191 void smsc_rxeof(struct usbd_xfer *, void *, usbd_status);
192 void smsc_txeof(struct usbd_xfer *, void *, usbd_status);
193
194 int smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
195 int smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
196 int smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
197 int smsc_sethwcsum(struct smsc_softc *);
198
199 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach,
200 smsc_detach, smsc_activate);
201
202 int
203 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
204 {
205 usb_device_request_t req;
206 uint32_t buf;
207 usbd_status err;
208
209 req.bmRequestType = UT_READ_VENDOR_DEVICE;
210 req.bRequest = SMSC_UR_READ_REG;
211 USETW(req.wValue, 0);
212 USETW(req.wIndex, off);
213 USETW(req.wLength, 4);
214
215 err = usbd_do_request(sc->sc_udev, &req, &buf);
216 if (err != 0)
217 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
218
219 *data = le32toh(buf);
220
221 return err;
222 }
223
224 int
225 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
226 {
227 usb_device_request_t req;
228 uint32_t buf;
229 usbd_status err;
230
231 buf = htole32(data);
232
233 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
234 req.bRequest = SMSC_UR_WRITE_REG;
235 USETW(req.wValue, 0);
236 USETW(req.wIndex, off);
237 USETW(req.wLength, 4);
238
239 err = usbd_do_request(sc->sc_udev, &req, &buf);
240 if (err != 0)
241 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
242
243 return err;
244 }
245
246 int
247 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
248 {
249 uint32_t val;
250 int err, i;
251
252 for (i = 0; i < 100; i++) {
253 if ((err = smsc_read_reg(sc, reg, &val)) != 0)
254 return err;
255 if (!(val & bits))
256 return 0;
257 DELAY(5);
258 }
259
260 return 1;
261 }
262
263 int
264 smsc_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
265 {
266 struct smsc_softc * const sc = device_private(dev);
267 uint32_t addr;
268 uint32_t data = 0;
269 int rv = 0;
270
271 smsc_lock_mii(sc);
272 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
273 smsc_warn_printf(sc, "MII is busy\n");
274 rv = -1;
275 goto done;
276 }
277
278 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
279 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
280
281 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
282 smsc_warn_printf(sc, "MII read timeout\n");
283 rv = ETIMEDOUT;
284 }
285
286 smsc_read_reg(sc, SMSC_MII_DATA, &data);
287
288 done:
289 smsc_unlock_mii(sc);
290
291 *val = data & 0xffff;
292 return rv;
293 }
294
295 int
296 smsc_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
297 {
298 struct smsc_softc * const sc = device_private(dev);
299 uint32_t addr;
300
301 if (sc->sc_phyno != phy)
302 return -1;
303
304 smsc_lock_mii(sc);
305 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
306 smsc_warn_printf(sc, "MII is busy\n");
307 smsc_unlock_mii(sc);
308 return -1;
309 }
310
311 smsc_write_reg(sc, SMSC_MII_DATA, val);
312
313 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
314 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
315 smsc_unlock_mii(sc);
316
317 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
318 smsc_warn_printf(sc, "MII write timeout\n");
319 return ETIMEDOUT;
320 }
321
322 return 0;
323 }
324
325 void
326 smsc_miibus_statchg(struct ifnet *ifp)
327 {
328 if (ifp == NULL)
329 return;
330
331 struct smsc_softc * const sc = ifp->if_softc;
332
333 mutex_enter(&sc->sc_lock);
334 if (sc->sc_dying) {
335 mutex_exit(&sc->sc_lock);
336 return;
337 }
338 smsc_miibus_statchg_locked(ifp);
339
340 mutex_exit(&sc->sc_lock);
341 }
342
343
344 void
345 smsc_miibus_statchg_locked(struct ifnet *ifp)
346 {
347 struct smsc_softc * const sc = ifp->if_softc;
348 struct mii_data * const mii = &sc->sc_mii;
349 int err;
350 uint32_t flow;
351 uint32_t afc_cfg;
352
353 KASSERT(mutex_owned(&sc->sc_lock));
354
355 if ((ifp->if_flags & IFF_RUNNING) == 0) {
356 smsc_dbg_printf(sc, "%s: not running\n", __func__);
357 return;
358 }
359
360 /* Use the MII status to determine link status */
361 sc->sc_flags &= ~SMSC_FLAG_LINK;
362 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
363 (IFM_ACTIVE | IFM_AVALID)) {
364 switch (IFM_SUBTYPE(mii->mii_media_active)) {
365 case IFM_10_T:
366 case IFM_100_TX:
367 sc->sc_flags |= SMSC_FLAG_LINK;
368 break;
369 case IFM_1000_T:
370 /* Gigabit ethernet not supported by chipset */
371 break;
372 default:
373 break;
374 }
375 }
376
377 /* Lost link, do nothing. */
378 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
379 smsc_dbg_printf(sc, "link flag not set\n");
380 return;
381 }
382
383 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
384 if (err) {
385 smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
386 "error %d\n", err);
387 return;
388 }
389
390 /* Enable/disable full duplex operation and TX/RX pause */
391 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
392 smsc_dbg_printf(sc, "full duplex operation\n");
393 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
394 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
395
396 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
397 flow = 0xffff0002;
398 else
399 flow = 0;
400
401 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
402 afc_cfg |= 0xf;
403 else
404 afc_cfg &= ~0xf;
405 } else {
406 smsc_dbg_printf(sc, "half duplex operation\n");
407 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
408 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
409
410 flow = 0;
411 afc_cfg |= 0xf;
412 }
413
414 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
415 err += smsc_write_reg(sc, SMSC_FLOW, flow);
416 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
417 if (err)
418 smsc_warn_printf(sc, "media change failed, error %d\n", err);
419 }
420
421 int
422 smsc_ifmedia_upd(struct ifnet *ifp)
423 {
424 struct smsc_softc * const sc = ifp->if_softc;
425 struct mii_data * const mii = &sc->sc_mii;
426 int err;
427
428 if (mii->mii_instance) {
429 struct mii_softc *miisc;
430
431 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
432 mii_phy_reset(miisc);
433 }
434 err = mii_mediachg(mii);
435 return err;
436 }
437
438 void
439 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
440 {
441 struct smsc_softc * const sc = ifp->if_softc;
442 struct mii_data * const mii = &sc->sc_mii;
443
444 /* SMSC_LOCK */
445
446 mii_pollstat(mii);
447
448 ifmr->ifm_active = mii->mii_media_active;
449 ifmr->ifm_status = mii->mii_media_status;
450
451 /* SMSC_UNLOCK */
452 }
453
454 static inline uint32_t
455 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
456 {
457
458 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
459 }
460
461 void
462 smsc_setmulti(struct smsc_softc *sc)
463 {
464 struct ifnet * const ifp = &sc->sc_ec.ec_if;
465 struct ether_multi *enm;
466 struct ether_multistep step;
467 uint32_t hashtbl[2] = { 0, 0 };
468 uint32_t hash;
469
470 KASSERT(mutex_owned(&sc->sc_lock));
471
472 if (sc->sc_dying)
473 return;
474
475 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
476 allmulti:
477 smsc_dbg_printf(sc, "receive all multicast enabled\n");
478 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
479 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
480 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
481 return;
482 } else {
483 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
484 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
485 }
486
487 ETHER_LOCK(&sc->sc_ec);
488 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
489 while (enm != NULL) {
490 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
491 ETHER_UNLOCK(&sc->sc_ec);
492 goto allmulti;
493 }
494
495 hash = smsc_hash(enm->enm_addrlo);
496 hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
497 ETHER_NEXT_MULTI(step, enm);
498 }
499 ETHER_UNLOCK(&sc->sc_ec);
500
501 /* Debug */
502 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
503 smsc_dbg_printf(sc, "receive select group of macs\n");
504 } else {
505 smsc_dbg_printf(sc, "receive own packets only\n");
506 }
507
508 /* Write the hash table and mac control registers */
509 ifp->if_flags &= ~IFF_ALLMULTI;
510 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
511 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
512 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
513 }
514
515 int
516 smsc_sethwcsum(struct smsc_softc *sc)
517 {
518 struct ifnet * const ifp = &sc->sc_ec.ec_if;
519 uint32_t val;
520 int err;
521
522 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
523 if (err != 0) {
524 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
525 err);
526 return err;
527 }
528
529 /* Enable/disable the Rx checksum */
530 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx))
531 val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
532 else
533 val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
534
535 /* Enable/disable the Tx checksum (currently not supported) */
536 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx))
537 val |= SMSC_COE_CTRL_TX_EN;
538 else
539 val &= ~SMSC_COE_CTRL_TX_EN;
540
541 sc->sc_coe_ctrl = val;
542
543 err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
544 if (err != 0) {
545 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n",
546 err);
547 return err;
548 }
549
550 return 0;
551 }
552
553 int
554 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
555 {
556 int err;
557 uint32_t val;
558
559 smsc_dbg_printf(sc, "setting mac address to "
560 "%02x:%02x:%02x:%02x:%02x:%02x\n",
561 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
562
563 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
564 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
565 goto done;
566
567 val = (addr[5] << 8) | addr[4];
568 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
569
570 done:
571 return err;
572 }
573
574 void
575 smsc_reset(struct smsc_softc *sc)
576 {
577 KASSERT(mutex_owned(&sc->sc_lock));
578 if (sc->sc_dying)
579 return;
580
581 /* Wait a little while for the chip to get its brains in order. */
582 DELAY(1000);
583
584 /* Reinitialize controller to achieve full reset. */
585 smsc_chip_init(sc);
586 }
587
588 int
589 smsc_init(struct ifnet *ifp)
590 {
591 struct smsc_softc * const sc = ifp->if_softc;
592
593 mutex_enter(&sc->sc_lock);
594 int ret = smsc_init_locked(ifp);
595 mutex_exit(&sc->sc_lock);
596
597 return ret;
598 }
599
600 int
601 smsc_init_locked(struct ifnet *ifp)
602 {
603 struct smsc_softc * const sc = ifp->if_softc;
604 usbd_status err;
605
606 if (sc->sc_dying)
607 return EIO;
608
609 /* Cancel pending I/O */
610 smsc_stop_locked(ifp, 1);
611
612 /* Reset the ethernet interface. */
613 smsc_reset(sc);
614
615 /* Load the multicast filter. */
616 smsc_setmulti(sc);
617
618 /* TCP/UDP checksum offload engines. */
619 smsc_sethwcsum(sc);
620
621 /* Open RX and TX pipes. */
622 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
623 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_RX]);
624 if (err) {
625 printf("%s: open rx pipe failed: %s\n",
626 device_xname(sc->sc_dev), usbd_errstr(err));
627 goto fail;
628 }
629
630 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
631 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_TX]);
632 if (err) {
633 printf("%s: open tx pipe failed: %s\n",
634 device_xname(sc->sc_dev), usbd_errstr(err));
635 goto fail1;
636 }
637
638 /* Init RX ring. */
639 if (smsc_rx_list_init(sc)) {
640 aprint_error_dev(sc->sc_dev, "rx list init failed\n");
641 goto fail2;
642 }
643
644 /* Init TX ring. */
645 if (smsc_tx_list_init(sc)) {
646 aprint_error_dev(sc->sc_dev, "tx list init failed\n");
647 goto fail3;
648 }
649
650 mutex_enter(&sc->sc_rxlock);
651 mutex_enter(&sc->sc_txlock);
652 sc->sc_stopping = false;
653
654 /* Start up the receive pipe. */
655 for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) {
656 struct smsc_chain * const c = &sc->sc_cdata.rx_chain[i];
657 usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, sc->sc_bufsz,
658 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, smsc_rxeof);
659 usbd_transfer(c->sc_xfer);
660 }
661
662 mutex_exit(&sc->sc_txlock);
663 mutex_exit(&sc->sc_rxlock);
664
665 /* Indicate we are up and running. */
666 ifp->if_flags |= IFF_RUNNING;
667 ifp->if_flags &= ~IFF_OACTIVE;
668
669 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
670
671 return 0;
672
673 fail3:
674 smsc_rx_list_free(sc);
675 fail2:
676 usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
677 fail1:
678 usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
679 fail:
680 return EIO;
681 }
682
683 void
684 smsc_start(struct ifnet *ifp)
685 {
686 struct smsc_softc * const sc = ifp->if_softc;
687 KASSERT(ifp->if_extflags & IFEF_MPSAFE);
688
689 mutex_enter(&sc->sc_txlock);
690 if (!sc->sc_stopping)
691 smsc_start_locked(ifp);
692 mutex_exit(&sc->sc_txlock);
693 }
694
695 void
696 smsc_start_locked(struct ifnet *ifp)
697 {
698 struct smsc_softc * const sc = ifp->if_softc;
699 struct mbuf *m_head = NULL;
700
701 KASSERT(mutex_owned(&sc->sc_txlock));
702
703 /* Don't send anything if there is no link or controller is busy. */
704 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
705 smsc_dbg_printf(sc, "%s: no link\n", __func__);
706 return;
707 }
708
709 /* Any free USB transfers? */
710 if (sc->sc_cdata.tx_free == 0) {
711 smsc_dbg_printf(sc, "%s: all USB transfers in use\n", __func__);
712 return;
713 }
714
715 if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING) {
716 smsc_dbg_printf(sc, "%s: not running\n", __func__);
717 return;
718 }
719
720 IFQ_POLL(&ifp->if_snd, m_head);
721 if (m_head == NULL)
722 return;
723
724 sc->sc_cdata.tx_free--;
725
726 IFQ_DEQUEUE(&ifp->if_snd, m_head);
727 if (smsc_encap(sc, m_head, sc->sc_cdata.tx_next)) {
728 m_free(m_head);
729 sc->sc_cdata.tx_free++;
730 return;
731 }
732
733 sc->sc_cdata.tx_next = (sc->sc_cdata.tx_next + 1) % SMSC_TX_LIST_CNT;
734
735 bpf_mtap(ifp, m_head, BPF_D_OUT);
736
737 if (sc->sc_cdata.tx_free == 0)
738 ifp->if_flags |= IFF_OACTIVE;
739
740 /*
741 * Set a timeout in case the chip goes out to lunch.
742 */
743 ifp->if_timer = 5;
744 }
745
746 void
747 smsc_tick(void *xsc)
748 {
749 struct smsc_softc * const sc = xsc;
750
751 if (sc == NULL)
752 return;
753
754 mutex_enter(&sc->sc_lock);
755
756 if (sc->sc_dying) {
757 mutex_exit(&sc->sc_lock);
758 return;
759 }
760
761 if (!sc->sc_ttpending) {
762 sc->sc_ttpending = true;
763 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
764 }
765
766 mutex_exit(&sc->sc_lock);
767 }
768
769 void
770 smsc_stop(struct ifnet *ifp, int disable)
771 {
772 struct smsc_softc * const sc = ifp->if_softc;
773
774 mutex_enter(&sc->sc_lock);
775 smsc_stop_locked(ifp, disable);
776 mutex_exit(&sc->sc_lock);
777 }
778
779 void
780 smsc_stop_locked(struct ifnet *ifp, int disable)
781 {
782 struct smsc_softc * const sc = ifp->if_softc;
783 usbd_status err;
784
785 KASSERT(mutex_owned(&sc->sc_lock));
786 mutex_enter(&sc->sc_rxlock);
787 mutex_enter(&sc->sc_txlock);
788 sc->sc_stopping = true;
789 mutex_exit(&sc->sc_txlock);
790 mutex_exit(&sc->sc_rxlock);
791
792 callout_stop(&sc->sc_stat_ch);
793
794 /* Stop transfers. */
795 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
796 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
797 if (err) {
798 printf("%s: abort rx pipe failed: %s\n",
799 device_xname(sc->sc_dev), usbd_errstr(err));
800 }
801 }
802
803 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
804 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
805 if (err) {
806 printf("%s: abort tx pipe failed: %s\n",
807 device_xname(sc->sc_dev), usbd_errstr(err));
808 }
809 }
810
811 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
812 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
813 if (err) {
814 printf("%s: abort intr pipe failed: %s\n",
815 device_xname(sc->sc_dev), usbd_errstr(err));
816 }
817 }
818
819 smsc_rx_list_free(sc);
820
821 smsc_tx_list_free(sc);
822
823 /* Close pipes */
824 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
825 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
826 if (err) {
827 printf("%s: close rx pipe failed: %s\n",
828 device_xname(sc->sc_dev), usbd_errstr(err));
829 }
830 sc->sc_ep[SMSC_ENDPT_RX] = NULL;
831 }
832
833 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
834 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
835 if (err) {
836 printf("%s: close tx pipe failed: %s\n",
837 device_xname(sc->sc_dev), usbd_errstr(err));
838 }
839 sc->sc_ep[SMSC_ENDPT_TX] = NULL;
840 }
841
842 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
843 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
844 if (err) {
845 printf("%s: close intr pipe failed: %s\n",
846 device_xname(sc->sc_dev), usbd_errstr(err));
847 }
848 sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
849 }
850
851 ifp->if_timer = 0;
852 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
853
854 if (disable) {
855 /* drain */
856 }
857 }
858
859 int
860 smsc_chip_init(struct smsc_softc *sc)
861 {
862 int err;
863 uint32_t reg_val;
864 int burst_cap;
865
866 /* Enter H/W config mode */
867 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
868
869 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
870 SMSC_HW_CFG_LRST)) != 0) {
871 smsc_warn_printf(sc, "timed-out waiting for reset to "
872 "complete\n");
873 goto init_failed;
874 }
875
876 /* Reset the PHY */
877 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
878
879 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
880 SMSC_PM_CTRL_PHY_RST)) != 0) {
881 smsc_warn_printf(sc, "timed-out waiting for phy reset to "
882 "complete\n");
883 goto init_failed;
884 }
885 usbd_delay_ms(sc->sc_udev, 40);
886
887 /* Set the mac address */
888 struct ifnet * const ifp = &sc->sc_ec.ec_if;
889 const char *eaddr = CLLADDR(ifp->if_sadl);
890 if ((err = smsc_setmacaddress(sc, eaddr)) != 0) {
891 smsc_warn_printf(sc, "failed to set the MAC address\n");
892 goto init_failed;
893 }
894
895 /*
896 * Don't know what the HW_CFG_BIR bit is, but following the reset
897 * sequence as used in the Linux driver.
898 */
899 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
900 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
901 goto init_failed;
902 }
903 reg_val |= SMSC_HW_CFG_BIR;
904 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
905
906 /*
907 * There is a so called 'turbo mode' that the linux driver supports, it
908 * seems to allow you to jam multiple frames per Rx transaction.
909 * By default this driver supports that and therefore allows multiple
910 * frames per USB transfer.
911 *
912 * The xfer buffer size needs to reflect this as well, therefore based
913 * on the calculations in the Linux driver the RX bufsize is set to
914 * 18944,
915 * bufsz = (16 * 1024 + 5 * 512)
916 *
917 * Burst capability is the number of URBs that can be in a burst of
918 * data/ethernet frames.
919 */
920
921 if (sc->sc_udev->ud_speed == USB_SPEED_HIGH)
922 burst_cap = 37;
923 else
924 burst_cap = 128;
925
926 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
927
928 /* Set the default bulk in delay (magic value from Linux driver) */
929 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
930
931 /*
932 * Initialise the RX interface
933 */
934 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
935 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
936 err);
937 goto init_failed;
938 }
939
940 /*
941 * The following settings are used for 'turbo mode', a.k.a multiple
942 * frames per Rx transaction (again info taken form Linux driver).
943 */
944 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
945
946 /*
947 * set Rx data offset to ETHER_ALIGN which will make the IP header
948 * align on a word boundary.
949 */
950 reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;
951
952 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
953
954 /* Clear the status register ? */
955 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
956
957 /* Read and display the revision register */
958 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
959 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
960 goto init_failed;
961 }
962
963 /* GPIO/LED setup */
964 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
965 SMSC_LED_GPIO_CFG_FDX_LED;
966 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
967
968 /*
969 * Initialise the TX interface
970 */
971 smsc_write_reg(sc, SMSC_FLOW, 0);
972
973 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
974
975 /* Read the current MAC configuration */
976 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
977 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
978 goto init_failed;
979 }
980
981 /* disable pad stripping, collides with checksum offload */
982 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;
983
984 /* Vlan */
985 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
986
987 /*
988 * Start TX
989 */
990 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
991 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
992 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
993
994 /*
995 * Start RX
996 */
997 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
998 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
999
1000 return 0;
1001
1002 init_failed:
1003 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1004 return err;
1005 }
1006
1007 static int
1008 smsc_ifflags_cb(struct ethercom *ec)
1009 {
1010 struct ifnet *ifp = &ec->ec_if;
1011 struct smsc_softc *sc = ifp->if_softc;
1012
1013 mutex_enter(&sc->sc_lock);
1014
1015 const int change = ifp->if_flags ^ sc->sc_if_flags;
1016 if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
1017 mutex_exit(&sc->sc_lock);
1018 return ENETRESET;
1019 }
1020
1021 smsc_dbg_printf(sc, "%s: change %x\n", __func__, change);
1022
1023 if ((change & IFF_PROMISC) != 0) {
1024 if (ifp->if_flags & IFF_PROMISC) {
1025 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
1026 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1027 } else if (!(ifp->if_flags & IFF_PROMISC)) {
1028 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
1029 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1030 }
1031 smsc_setmulti(sc);
1032 }
1033
1034 mutex_exit(&sc->sc_lock);
1035
1036 return 0;
1037 }
1038
1039
1040 int
1041 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1042 {
1043 struct smsc_softc * const sc = ifp->if_softc;
1044
1045 smsc_dbg_printf(sc, "%s: cmd %0lx data %p\n", __func__, cmd, data);
1046
1047 int error = ether_ioctl(ifp, cmd, data);
1048
1049 if (error == ENETRESET) {
1050 error = 0;
1051 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
1052 if (ifp->if_flags & IFF_RUNNING) {
1053 mutex_enter(&sc->sc_lock);
1054 smsc_setmulti(sc);
1055 mutex_exit(&sc->sc_lock);
1056 }
1057 }
1058 }
1059
1060 mutex_enter(&sc->sc_rxlock);
1061 mutex_enter(&sc->sc_txlock);
1062 sc->sc_if_flags = ifp->if_flags;
1063 mutex_exit(&sc->sc_txlock);
1064 mutex_exit(&sc->sc_rxlock);
1065
1066 return error;
1067 }
1068
1069 int
1070 smsc_match(device_t parent, cfdata_t match, void *aux)
1071 {
1072 struct usb_attach_arg *uaa = aux;
1073
1074 return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
1075 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
1076 }
1077
1078 void
1079 smsc_attach(device_t parent, device_t self, void *aux)
1080 {
1081 struct smsc_softc *sc = device_private(self);
1082 struct usb_attach_arg *uaa = aux;
1083 struct usbd_device *dev = uaa->uaa_device;
1084 usb_interface_descriptor_t *id;
1085 usb_endpoint_descriptor_t *ed;
1086 char *devinfop;
1087 struct mii_data *mii;
1088 struct ifnet *ifp;
1089 int err, i;
1090 uint32_t mac_h, mac_l;
1091
1092 sc->sc_dev = self;
1093 sc->sc_udev = dev;
1094 sc->sc_stopping = false;
1095
1096 aprint_naive("\n");
1097 aprint_normal("\n");
1098
1099 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
1100 aprint_normal_dev(self, "%s\n", devinfop);
1101 usbd_devinfo_free(devinfop);
1102
1103 err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
1104 if (err) {
1105 aprint_error_dev(self, "failed to set configuration"
1106 ", err=%s\n", usbd_errstr(err));
1107 return;
1108 }
1109
1110 /* Setup the endpoints for the SMSC LAN95xx device(s) */
1111 err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
1112 if (err) {
1113 aprint_error_dev(self, "getting interface handle failed\n");
1114 return;
1115 }
1116
1117 id = usbd_get_interface_descriptor(sc->sc_iface);
1118
1119 if (sc->sc_udev->ud_speed >= USB_SPEED_HIGH)
1120 sc->sc_bufsz = SMSC_MAX_BUFSZ;
1121 else
1122 sc->sc_bufsz = SMSC_MIN_BUFSZ;
1123
1124 /* Find endpoints. */
1125 for (i = 0; i < id->bNumEndpoints; i++) {
1126 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
1127 if (!ed) {
1128 aprint_error_dev(self, "couldn't get ep %d\n", i);
1129 return;
1130 }
1131 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1132 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1133 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
1134 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1135 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1136 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
1137 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1138 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
1139 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
1140 }
1141 }
1142
1143 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, USB_TASKQ_MPSAFE);
1144
1145 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
1146 mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1147 mutex_init(&sc->sc_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1148 mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
1149
1150 ifp = &sc->sc_ec.ec_if;
1151 ifp->if_softc = sc;
1152 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
1153 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1154 ifp->if_extflags = IFEF_MPSAFE;
1155 ifp->if_init = smsc_init;
1156 ifp->if_ioctl = smsc_ioctl;
1157 ifp->if_start = smsc_start;
1158 ifp->if_stop = smsc_stop;
1159
1160 #ifdef notyet
1161 /*
1162 * We can do TCPv4, and UDPv4 checksums in hardware.
1163 */
1164 ifp->if_capabilities |=
1165 /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
1166 /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
1167 #endif
1168
1169 sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1170
1171 /* Setup some of the basics */
1172 sc->sc_phyno = 1;
1173
1174 /*
1175 * Attempt to get the mac address, if an EEPROM is not attached this
1176 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1177 * address based on urandom.
1178 */
1179 memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);
1180
1181 prop_dictionary_t dict = device_properties(self);
1182 prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
1183
1184 if (eaprop != NULL) {
1185 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
1186 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
1187 memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
1188 ETHER_ADDR_LEN);
1189 } else {
1190 /* Check if there is already a MAC address in the register */
1191 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1192 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1193 sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1194 sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
1195 sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1196 sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1197 sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1198 sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
1199 }
1200 }
1201
1202 aprint_normal_dev(self, "Ethernet address %s\n",
1203 ether_sprintf(sc->sc_enaddr));
1204
1205 IFQ_SET_READY(&ifp->if_snd);
1206
1207 /* Initialize MII/media info. */
1208 mii = &sc->sc_mii;
1209 mii->mii_ifp = ifp;
1210 mii->mii_readreg = smsc_miibus_readreg;
1211 mii->mii_writereg = smsc_miibus_writereg;
1212 mii->mii_statchg = smsc_miibus_statchg;
1213 mii->mii_flags = MIIF_AUTOTSLEEP;
1214 sc->sc_ec.ec_mii = mii;
1215 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
1216 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1217
1218 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1219 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1220 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1221 } else
1222 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1223
1224 callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE);
1225
1226 if_initialize(ifp);
1227 sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if);
1228 ether_ifattach(ifp, sc->sc_enaddr);
1229 ether_set_ifflags_cb(&sc->sc_ec, smsc_ifflags_cb);
1230 if_register(ifp);
1231
1232 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
1233 RND_TYPE_NET, RND_FLAG_DEFAULT);
1234
1235 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
1236 }
1237
1238 int
1239 smsc_detach(device_t self, int flags)
1240 {
1241 struct smsc_softc *sc = device_private(self);
1242 struct ifnet *ifp = &sc->sc_ec.ec_if;
1243
1244 mutex_enter(&sc->sc_lock);
1245 sc->sc_dying = true;
1246 mutex_exit(&sc->sc_lock);
1247
1248 callout_halt(&sc->sc_stat_ch, NULL);
1249
1250 if (ifp->if_flags & IFF_RUNNING)
1251 smsc_stop_locked(ifp, 1);
1252
1253 /*
1254 * Remove any pending tasks. They cannot be executing because they run
1255 * in the same thread as detach.
1256 */
1257 usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER,
1258 NULL);
1259
1260 mutex_enter(&sc->sc_lock);
1261 sc->sc_refcnt--;
1262 while (sc->sc_refcnt > 0) {
1263 /* Wait for processes to go away */
1264 cv_wait(&sc->sc_detachcv, &sc->sc_lock);
1265 }
1266
1267 #ifdef DIAGNOSTIC
1268 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
1269 sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
1270 sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1271 printf("%s: detach has active endpoints\n",
1272 device_xname(sc->sc_dev));
1273 #endif
1274
1275 mutex_exit(&sc->sc_lock);
1276
1277 rnd_detach_source(&sc->sc_rnd_source);
1278 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1279 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
1280 if (ifp->if_softc != NULL) {
1281 ether_ifdetach(ifp);
1282 if_detach(ifp);
1283 }
1284
1285 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
1286
1287 cv_destroy(&sc->sc_detachcv);
1288 mutex_destroy(&sc->sc_mii_lock);
1289 mutex_destroy(&sc->sc_rxlock);
1290 mutex_destroy(&sc->sc_txlock);
1291 mutex_destroy(&sc->sc_lock);
1292
1293 return 0;
1294 }
1295
1296 void
1297 smsc_tick_task(void *xsc)
1298 {
1299 struct smsc_softc * const sc = xsc;
1300
1301 if (sc == NULL)
1302 return;
1303
1304 mutex_enter(&sc->sc_lock);
1305
1306 if (sc->sc_dying) {
1307 mutex_exit(&sc->sc_lock);
1308 return;
1309 }
1310
1311 struct ifnet * const ifp = &sc->sc_ec.ec_if;
1312 struct mii_data * const mii = &sc->sc_mii;
1313
1314 sc->sc_refcnt++;
1315 mutex_exit(&sc->sc_lock);
1316
1317 mii_tick(mii);
1318 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
1319 smsc_miibus_statchg(ifp);
1320
1321 mutex_enter(&sc->sc_lock);
1322 sc->sc_ttpending = false;
1323
1324 if (--sc->sc_refcnt < 0)
1325 cv_broadcast(&sc->sc_detachcv);
1326
1327 if (sc->sc_dying) {
1328 mutex_exit(&sc->sc_lock);
1329 return;
1330 }
1331 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
1332
1333 mutex_exit(&sc->sc_lock);
1334 }
1335
1336 int
1337 smsc_activate(device_t self, enum devact act)
1338 {
1339 struct smsc_softc *sc = device_private(self);
1340
1341 switch (act) {
1342 case DVACT_DEACTIVATE:
1343 if_deactivate(&sc->sc_ec.ec_if);
1344
1345 mutex_enter(&sc->sc_lock);
1346 sc->sc_dying = true;
1347
1348 mutex_enter(&sc->sc_rxlock);
1349 mutex_enter(&sc->sc_txlock);
1350 sc->sc_stopping = true;
1351 mutex_exit(&sc->sc_txlock);
1352 mutex_exit(&sc->sc_rxlock);
1353
1354 mutex_exit(&sc->sc_lock);
1355 return 0;
1356 default:
1357 return EOPNOTSUPP;
1358 }
1359 return 0;
1360 }
1361
1362 void
1363 smsc_lock_mii(struct smsc_softc *sc)
1364 {
1365
1366 mutex_enter(&sc->sc_lock);
1367 sc->sc_refcnt++;
1368 mutex_exit(&sc->sc_lock);
1369
1370 mutex_enter(&sc->sc_mii_lock);
1371 }
1372
1373 void
1374 smsc_unlock_mii(struct smsc_softc *sc)
1375 {
1376
1377 mutex_exit(&sc->sc_mii_lock);
1378 mutex_enter(&sc->sc_lock);
1379 if (--sc->sc_refcnt < 0)
1380 cv_broadcast(&sc->sc_detachcv);
1381 mutex_exit(&sc->sc_lock);
1382 }
1383
1384 void
1385 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1386 {
1387 struct smsc_chain * const c = (struct smsc_chain *)priv;
1388 struct smsc_softc * const sc = c->sc_sc;
1389 struct ifnet * const ifp = &sc->sc_ec.ec_if;
1390 u_char *buf = c->sc_buf;
1391 uint32_t total_len;
1392
1393 mutex_enter(&sc->sc_rxlock);
1394 if (sc->sc_stopping) {
1395 smsc_dbg_printf(sc, "%s: stopping\n", __func__);
1396 mutex_exit(&sc->sc_rxlock);
1397 return;
1398 }
1399
1400 if (!(sc->sc_if_flags & IFF_RUNNING)) {
1401 smsc_dbg_printf(sc, "%s: not running\n", __func__);
1402 mutex_exit(&sc->sc_rxlock);
1403 return;
1404 }
1405
1406 if (status != USBD_NORMAL_COMPLETION) {
1407 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1408 mutex_exit(&sc->sc_rxlock);
1409 return;
1410 }
1411 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1412 printf("%s: usb errors on rx: %s\n",
1413 device_xname(sc->sc_dev), usbd_errstr(status));
1414 }
1415 if (status == USBD_STALLED)
1416 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
1417 goto done;
1418 }
1419
1420 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1421 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
1422
1423 while (total_len != 0) {
1424 uint32_t rxhdr;
1425 if (total_len < sizeof(rxhdr)) {
1426 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n",
1427 total_len, sizeof(rxhdr));
1428 ifp->if_ierrors++;
1429 goto done;
1430 }
1431
1432 memcpy(&rxhdr, buf, sizeof(rxhdr));
1433 rxhdr = le32toh(rxhdr);
1434 buf += sizeof(rxhdr);
1435 total_len -= sizeof(rxhdr);
1436
1437 if (rxhdr & SMSC_RX_STAT_COLLISION)
1438 ifp->if_collisions++;
1439
1440 if (rxhdr & (SMSC_RX_STAT_ERROR
1441 | SMSC_RX_STAT_LENGTH_ERROR
1442 | SMSC_RX_STAT_MII_ERROR)) {
1443 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1444 ifp->if_ierrors++;
1445 goto done;
1446 }
1447
1448 uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1449 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
1450 "0x%08x\n", total_len, pktlen, rxhdr);
1451
1452 if (pktlen < ETHER_HDR_LEN) {
1453 smsc_dbg_printf(sc, "pktlen %d < ETHER_HDR_LEN %d\n",
1454 pktlen, ETHER_HDR_LEN);
1455 ifp->if_ierrors++;
1456 goto done;
1457 }
1458
1459 pktlen += ETHER_ALIGN;
1460
1461 if (pktlen > MCLBYTES) {
1462 smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n",
1463 pktlen, MCLBYTES);
1464 ifp->if_ierrors++;
1465 goto done;
1466 }
1467
1468 if (pktlen > total_len) {
1469 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
1470 pktlen, total_len);
1471 ifp->if_ierrors++;
1472 goto done;
1473 }
1474
1475 struct mbuf *m = smsc_newbuf();
1476 if (m == NULL) {
1477 smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
1478 ifp->if_ierrors++;
1479 goto done;
1480 }
1481
1482 m_set_rcvif(m, ifp);
1483 m->m_pkthdr.len = m->m_len = pktlen;
1484 m->m_flags |= M_HASFCS;
1485 m_adj(m, ETHER_ALIGN);
1486
1487 KASSERT(m->m_len < MCLBYTES);
1488 memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len);
1489
1490 /* Check if RX TCP/UDP checksumming is being offloaded */
1491 if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
1492 smsc_dbg_printf(sc,"RX checksum offload checking\n");
1493 struct ether_header *eh;
1494
1495 eh = mtod(m, struct ether_header *);
1496
1497 /* Remove the extra 2 bytes of the csum */
1498 m_adj(m, -2);
1499
1500 /*
1501 * The checksum appears to be simplistically calculated
1502 * over the udp/tcp header and data up to the end of the
1503 * eth frame. Which means if the eth frame is padded
1504 * the csum calculation is incorrectly performed over
1505 * the padding bytes as well. Therefore to be safe we
1506 * ignore the H/W csum on frames less than or equal to
1507 * 64 bytes.
1508 *
1509 * Ignore H/W csum for non-IPv4 packets.
1510 */
1511 smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n",
1512 be16toh(eh->ether_type), pktlen);
1513 if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
1514 pktlen > ETHER_MIN_LEN) {
1515
1516 m->m_pkthdr.csum_flags |=
1517 (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);
1518
1519 /*
1520 * Copy the TCP/UDP checksum from the last 2
1521 * bytes of the transfer and put in the
1522 * csum_data field.
1523 */
1524 memcpy(&m->m_pkthdr.csum_data,
1525 buf + pktlen - 2, 2);
1526 /*
1527 * The data is copied in network order, but the
1528 * csum algorithm in the kernel expects it to be
1529 * in host network order.
1530 */
1531 m->m_pkthdr.csum_data =
1532 ntohs(m->m_pkthdr.csum_data);
1533 smsc_dbg_printf(sc,
1534 "RX checksum offloaded (0x%04x)\n",
1535 m->m_pkthdr.csum_data);
1536 }
1537 }
1538
1539 /* round up to next longword */
1540 pktlen = (pktlen + 3) & ~0x3;
1541
1542 /* total_len does not include the padding */
1543 if (pktlen > total_len)
1544 pktlen = total_len;
1545
1546 buf += pktlen;
1547 total_len -= pktlen;
1548
1549 mutex_exit(&sc->sc_rxlock);
1550
1551 /* push the packet up */
1552 if_percpuq_enqueue(sc->sc_ipq, m);
1553
1554 mutex_enter(&sc->sc_rxlock);
1555 if (sc->sc_stopping) {
1556 smsc_dbg_printf(sc, "%s: stopping\n", __func__);
1557 mutex_exit(&sc->sc_rxlock);
1558 return;
1559 }
1560 }
1561
1562 done:
1563 mutex_exit(&sc->sc_rxlock);
1564
1565 /* Setup new transfer. */
1566 usbd_setup_xfer(xfer, c, c->sc_buf, sc->sc_bufsz, USBD_SHORT_XFER_OK,
1567 USBD_NO_TIMEOUT, smsc_rxeof);
1568 usbd_transfer(xfer);
1569
1570 return;
1571 }
1572
1573 void
1574 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1575 {
1576 struct smsc_chain *c = priv;
1577 struct smsc_softc *sc = c->sc_sc;
1578 struct ifnet *ifp = &sc->sc_ec.ec_if;
1579
1580 mutex_enter(&sc->sc_txlock);
1581 if (sc->sc_stopping) {
1582 smsc_dbg_printf(sc, "%s: stopping\n", __func__);
1583 mutex_exit(&sc->sc_txlock);
1584 return;
1585 }
1586
1587 sc->sc_cdata.tx_free++;
1588 ifp->if_timer = 0;
1589 ifp->if_flags &= ~IFF_OACTIVE;
1590
1591 if (status != USBD_NORMAL_COMPLETION) {
1592 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1593 mutex_exit(&sc->sc_txlock);
1594 return;
1595 }
1596 ifp->if_oerrors++;
1597 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1598 usbd_errstr(status));
1599 if (status == USBD_STALLED)
1600 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
1601 mutex_exit(&sc->sc_txlock);
1602 return;
1603 }
1604 ifp->if_opackets++;
1605
1606 m_freem(c->sc_mbuf);
1607 c->sc_mbuf = NULL;
1608
1609 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1610 smsc_start_locked(ifp);
1611
1612 mutex_exit(&sc->sc_txlock);
1613 }
1614
1615 int
1616 smsc_tx_list_init(struct smsc_softc *sc)
1617 {
1618 struct smsc_cdata *cd = &sc->sc_cdata;
1619 struct smsc_chain *c;
1620 int i;
1621
1622 for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
1623 c = &cd->tx_chain[i];
1624 c->sc_sc = sc;
1625 c->sc_idx = i;
1626 c->sc_mbuf = NULL;
1627 if (c->sc_xfer == NULL) {
1628 int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_TX],
1629 sc->sc_bufsz, USBD_FORCE_SHORT_XFER, 0,
1630 &c->sc_xfer);
1631 if (error)
1632 return EIO;
1633 c->sc_buf = usbd_get_buffer(c->sc_xfer);
1634 }
1635 }
1636
1637 cd->tx_free = SMSC_TX_LIST_CNT;
1638 cd->tx_next = 0;
1639
1640 return 0;
1641 }
1642
1643 void
1644 smsc_tx_list_free(struct smsc_softc *sc)
1645 {
1646 /* Free TX resources. */
1647 for (size_t i = 0; i < SMSC_TX_LIST_CNT; i++) {
1648 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
1649 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
1650 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
1651 }
1652 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
1653 usbd_destroy_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
1654 sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
1655 }
1656 }
1657 }
1658
1659 int
1660 smsc_rx_list_init(struct smsc_softc *sc)
1661 {
1662 struct smsc_cdata *cd = &sc->sc_cdata;
1663 struct smsc_chain *c;
1664 int i;
1665
1666 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
1667 c = &cd->rx_chain[i];
1668 c->sc_sc = sc;
1669 c->sc_idx = i;
1670 c->sc_mbuf = NULL;
1671 if (c->sc_xfer == NULL) {
1672 int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_RX],
1673 sc->sc_bufsz, USBD_SHORT_XFER_OK, 0, &c->sc_xfer);
1674 if (error)
1675 return error;
1676 c->sc_buf = usbd_get_buffer(c->sc_xfer);
1677 }
1678 }
1679
1680 return 0;
1681 }
1682
1683 void
1684 smsc_rx_list_free(struct smsc_softc *sc)
1685 {
1686 /* Free RX resources. */
1687 for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) {
1688 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
1689 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
1690 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
1691 }
1692 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
1693 usbd_destroy_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
1694 sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
1695 }
1696 }
1697 }
1698
1699 struct mbuf *
1700 smsc_newbuf(void)
1701 {
1702 struct mbuf *m;
1703
1704 MGETHDR(m, M_DONTWAIT, MT_DATA);
1705 if (m == NULL)
1706 return NULL;
1707
1708 MCLGET(m, M_DONTWAIT);
1709 if (!(m->m_flags & M_EXT)) {
1710 m_freem(m);
1711 return NULL;
1712 }
1713
1714 return m;
1715 }
1716
1717 int
1718 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
1719 {
1720 struct smsc_chain * const c = &sc->sc_cdata.tx_chain[idx];
1721 uint32_t txhdr;
1722 uint32_t frm_len = 0;
1723
1724 /*
1725 * Each frame is prefixed with two 32-bit values describing the
1726 * length of the packet and buffer.
1727 */
1728 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1729 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1730 txhdr = htole32(txhdr);
1731 memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
1732
1733 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1734 txhdr = htole32(txhdr);
1735 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
1736
1737 frm_len += 8;
1738
1739 /* Next copy in the actual packet */
1740 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
1741 frm_len += m->m_pkthdr.len;
1742
1743 c->sc_mbuf = m;
1744
1745 usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, frm_len,
1746 USBD_FORCE_SHORT_XFER, 10000, smsc_txeof);
1747
1748 usbd_status err = usbd_transfer(c->sc_xfer);
1749 if (err != USBD_IN_PROGRESS) {
1750 return EIO;
1751 }
1752
1753 return 0;
1754 }
1755