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