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