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