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