1 /* $OpenBSD: if_smsc.c,v 1.39 2024/05/23 03:21:08 jsg Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
3 /*-
4 * Copyright (c) 2012
5 * Ben Gray <bgray@freebsd.org>.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * SMSC LAN9xxx devices (http://www.smsc.com/)
31 *
32 * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
33 * support USB 2.0 and 10/100 Mbps Ethernet.
34 *
35 * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
36 * The driver only covers the Ethernet part, the standard USB hub driver
37 * supports the hub part.
38 *
39 * This driver is closely modelled on the Linux driver written and copyrighted
40 * by SMSC.
41 *
42 * H/W TCP & UDP Checksum Offloading
43 * ---------------------------------
44 * The chip supports both tx and rx offloading of UDP & TCP checksums, this
45 * feature can be dynamically enabled/disabled.
46 *
47 * RX checksumming is performed across bytes after the IPv4 header to the end of
48 * the Ethernet frame, this means if the frame is padded with non-zero values
49 * the H/W checksum will be incorrect, however the rx code compensates for this.
50 *
51 * TX checksumming is more complicated, the device requires a special header to
52 * be prefixed onto the start of the frame which indicates the start and end
53 * positions of the UDP or TCP frame. This requires the driver to manually
54 * go through the packet data and decode the headers prior to sending.
55 * On Linux they generally provide cues to the location of the csum and the
56 * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
57 * hence this is not as optimal and therefore h/w tX checksum is currently not
58 * implemented.
59 */
60
61 #include "bpfilter.h"
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/sockio.h>
66 #include <sys/rwlock.h>
67 #include <sys/mbuf.h>
68
69 #include <sys/device.h>
70
71 #include <machine/bus.h>
72
73 #include <net/if.h>
74 #include <net/if_media.h>
75
76 #if NBPFILTER > 0
77 #include <net/bpf.h>
78 #endif
79
80 #include <netinet/in.h>
81 #include <netinet/if_ether.h>
82
83 #include <dev/mii/miivar.h>
84
85 #include <dev/usb/usb.h>
86 #include <dev/usb/usbdi.h>
87 #include <dev/usb/usbdi_util.h>
88 #include <dev/usb/usbdivar.h>
89 #include <dev/usb/usbdevs.h>
90
91 #include "if_smscreg.h"
92
93 /*
94 * Various supported device vendors/products.
95 */
96 static const struct usb_devno smsc_devs[] = {
97 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_LAN89530 },
98 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_LAN9530 },
99 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_LAN9730 },
100 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9500 },
101 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9500A },
102 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9500A_ALT },
103 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9500A_HAL },
104 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9500A_SAL10 },
105 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9500_ALT },
106 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9500_SAL10 },
107 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9505 },
108 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9505A },
109 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9505A_HAL },
110 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9505A_SAL10 },
111 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9505_SAL10 },
112 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9512_14 },
113 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9512_14_ALT },
114 { USB_VENDOR_SMC2, USB_PRODUCT_SMC2_SMSC9512_14_SAL10 }
115 };
116
117 #ifdef SMSC_DEBUG
118 static int smsc_debug = 0;
119 #define smsc_dbg_printf(sc, fmt, args...) \
120 do { \
121 if (smsc_debug > 0) \
122 printf("debug: " fmt, ##args); \
123 } while(0)
124 #else
125 #define smsc_dbg_printf(sc, fmt, args...)
126 #endif
127
128 #define smsc_warn_printf(sc, fmt, args...) \
129 printf("%s: warning: " fmt, (sc)->sc_dev.dv_xname, ##args)
130
131 #define smsc_err_printf(sc, fmt, args...) \
132 printf("%s: error: " fmt, (sc)->sc_dev.dv_xname, ##args)
133
134 int smsc_chip_init(struct smsc_softc *sc);
135 int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
136 void smsc_iff(struct smsc_softc *);
137 int smsc_setmacaddress(struct smsc_softc *, const uint8_t *);
138
139 int smsc_match(struct device *, void *, void *);
140 void smsc_attach(struct device *, struct device *, void *);
141 int smsc_detach(struct device *, int);
142
143 void smsc_init(void *);
144 void smsc_stop(struct smsc_softc *);
145 void smsc_start(struct ifnet *);
146 void smsc_reset(struct smsc_softc *);
147
148 void smsc_tick(void *);
149 void smsc_tick_task(void *);
150 void smsc_miibus_statchg(struct device *);
151 int smsc_miibus_readreg(struct device *, int, int);
152 void smsc_miibus_writereg(struct device *, int, int, int);
153 int smsc_ifmedia_upd(struct ifnet *);
154 void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
155 void smsc_lock_mii(struct smsc_softc *sc);
156 void smsc_unlock_mii(struct smsc_softc *sc);
157
158 int smsc_tx_list_init(struct smsc_softc *);
159 int smsc_rx_list_init(struct smsc_softc *);
160 int smsc_encap(struct smsc_softc *, struct mbuf *, int);
161 void smsc_rxeof(struct usbd_xfer *, void *, usbd_status);
162 void smsc_txeof(struct usbd_xfer *, void *, usbd_status);
163
164 int smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
165 int smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
166 int smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
167 int smsc_sethwcsum(struct smsc_softc *);
168
169 struct cfdriver smsc_cd = {
170 NULL, "smsc", DV_IFNET
171 };
172
173 const struct cfattach smsc_ca = {
174 sizeof(struct smsc_softc), smsc_match, smsc_attach, smsc_detach,
175 };
176
177 #if defined(__arm__) || defined(__arm64__)
178
179 #include <dev/ofw/openfirm.h>
180
181 void
smsc_enaddr_OF(struct smsc_softc * sc)182 smsc_enaddr_OF(struct smsc_softc *sc)
183 {
184 char *device = "/axi/usb/hub/ethernet";
185 char prop[128];
186 int node;
187
188 if (sc->sc_dev.dv_unit != 0)
189 return;
190
191 /*
192 * Get the Raspberry Pi MAC address from FDT. This is all
193 * much more complicated than strictly needed since the
194 * firmware device tree keeps changing as drivers get
195 * upstreamed. Sigh.
196 *
197 * Ultimately this should just use the "ethernet0" alias and
198 * the "local-mac-address" property.
199 */
200
201 if ((node = OF_finddevice("/aliases")) == -1)
202 return;
203 if (OF_getprop(node, "ethernet0", prop, sizeof(prop)) > 0 ||
204 OF_getprop(node, "ethernet", prop, sizeof(prop)) > 0)
205 device = prop;
206
207 if ((node = OF_finddevice(device)) == -1)
208 return;
209 if (OF_getprop(node, "local-mac-address", sc->sc_ac.ac_enaddr,
210 sizeof(sc->sc_ac.ac_enaddr)) != sizeof(sc->sc_ac.ac_enaddr)) {
211 OF_getprop(node, "mac-address", sc->sc_ac.ac_enaddr,
212 sizeof(sc->sc_ac.ac_enaddr));
213 }
214 }
215 #else
216 #define smsc_enaddr_OF(x) do {} while(0)
217 #endif
218
219 int
smsc_read_reg(struct smsc_softc * sc,uint32_t off,uint32_t * data)220 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
221 {
222 usb_device_request_t req;
223 uint32_t buf;
224 usbd_status err;
225
226 req.bmRequestType = UT_READ_VENDOR_DEVICE;
227 req.bRequest = SMSC_UR_READ_REG;
228 USETW(req.wValue, 0);
229 USETW(req.wIndex, off);
230 USETW(req.wLength, 4);
231
232 err = usbd_do_request(sc->sc_udev, &req, &buf);
233 if (err != 0)
234 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
235
236 *data = letoh32(buf);
237
238 return (err);
239 }
240
241 int
smsc_write_reg(struct smsc_softc * sc,uint32_t off,uint32_t data)242 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
243 {
244 usb_device_request_t req;
245 uint32_t buf;
246 usbd_status err;
247
248 buf = htole32(data);
249
250 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
251 req.bRequest = SMSC_UR_WRITE_REG;
252 USETW(req.wValue, 0);
253 USETW(req.wIndex, off);
254 USETW(req.wLength, 4);
255
256 err = usbd_do_request(sc->sc_udev, &req, &buf);
257 if (err != 0)
258 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
259
260 return (err);
261 }
262
263 int
smsc_wait_for_bits(struct smsc_softc * sc,uint32_t reg,uint32_t bits)264 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
265 {
266 uint32_t val;
267 int err, i;
268
269 for (i = 0; i < 100; i++) {
270 if ((err = smsc_read_reg(sc, reg, &val)) != 0)
271 return (err);
272 if (!(val & bits))
273 return (0);
274 DELAY(5);
275 }
276
277 return (1);
278 }
279
280 int
smsc_miibus_readreg(struct device * dev,int phy,int reg)281 smsc_miibus_readreg(struct device *dev, int phy, int reg)
282 {
283 struct smsc_softc *sc = (struct smsc_softc *)dev;
284 uint32_t addr;
285 uint32_t val = 0;
286
287 smsc_lock_mii(sc);
288 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
289 smsc_warn_printf(sc, "MII is busy\n");
290 goto done;
291 }
292
293 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
294 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
295
296 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
297 smsc_warn_printf(sc, "MII read timeout\n");
298
299 smsc_read_reg(sc, SMSC_MII_DATA, &val);
300
301 done:
302 smsc_unlock_mii(sc);
303 return (val & 0xFFFF);
304 }
305
306 void
smsc_miibus_writereg(struct device * dev,int phy,int reg,int val)307 smsc_miibus_writereg(struct device *dev, int phy, int reg, int val)
308 {
309 struct smsc_softc *sc = (struct smsc_softc *)dev;
310 uint32_t addr;
311
312 if (sc->sc_phyno != phy)
313 return;
314
315 smsc_lock_mii(sc);
316 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
317 smsc_warn_printf(sc, "MII is busy\n");
318 smsc_unlock_mii(sc);
319 return;
320 }
321
322 smsc_write_reg(sc, SMSC_MII_DATA, val);
323
324 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
325 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
326 smsc_unlock_mii(sc);
327
328 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
329 smsc_warn_printf(sc, "MII write timeout\n");
330 }
331
332 void
smsc_miibus_statchg(struct device * dev)333 smsc_miibus_statchg(struct device *dev)
334 {
335 struct smsc_softc *sc = (struct smsc_softc *)dev;
336 struct mii_data *mii = &sc->sc_mii;
337 struct ifnet *ifp = &sc->sc_ac.ac_if;
338 int err;
339 uint32_t flow;
340 uint32_t afc_cfg;
341
342 if (mii == NULL || ifp == NULL ||
343 (ifp->if_flags & IFF_RUNNING) == 0)
344 return;
345
346 /* Use the MII status to determine link status */
347 sc->sc_flags &= ~SMSC_FLAG_LINK;
348 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
349 (IFM_ACTIVE | IFM_AVALID)) {
350 switch (IFM_SUBTYPE(mii->mii_media_active)) {
351 case IFM_10_T:
352 case IFM_100_TX:
353 sc->sc_flags |= SMSC_FLAG_LINK;
354 break;
355 case IFM_1000_T:
356 /* Gigabit ethernet not supported by chipset */
357 break;
358 default:
359 break;
360 }
361 }
362
363 /* Lost link, do nothing. */
364 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
365 smsc_dbg_printf(sc, "link flag not set\n");
366 return;
367 }
368
369 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
370 if (err) {
371 smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
372 "error %d\n", err);
373 return;
374 }
375
376 /* Enable/disable full duplex operation and TX/RX pause */
377 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
378 smsc_dbg_printf(sc, "full duplex operation\n");
379 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
380 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
381
382 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
383 flow = 0xffff0002;
384 else
385 flow = 0;
386
387 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
388 afc_cfg |= 0xf;
389 else
390 afc_cfg &= ~0xf;
391
392 } else {
393 smsc_dbg_printf(sc, "half duplex operation\n");
394 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
395 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
396
397 flow = 0;
398 afc_cfg |= 0xf;
399 }
400
401 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
402 err += smsc_write_reg(sc, SMSC_FLOW, flow);
403 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
404 if (err)
405 smsc_warn_printf(sc, "media change failed, error %d\n", err);
406 }
407
408 int
smsc_ifmedia_upd(struct ifnet * ifp)409 smsc_ifmedia_upd(struct ifnet *ifp)
410 {
411 struct smsc_softc *sc = ifp->if_softc;
412 struct mii_data *mii = &sc->sc_mii;
413 int err;
414
415 if (mii->mii_instance) {
416 struct mii_softc *miisc;
417
418 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
419 mii_phy_reset(miisc);
420 }
421 err = mii_mediachg(mii);
422 return (err);
423 }
424
425 void
smsc_ifmedia_sts(struct ifnet * ifp,struct ifmediareq * ifmr)426 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
427 {
428 struct smsc_softc *sc = ifp->if_softc;
429 struct mii_data *mii = &sc->sc_mii;
430
431 mii_pollstat(mii);
432
433 ifmr->ifm_active = mii->mii_media_active;
434 ifmr->ifm_status = mii->mii_media_status;
435 }
436
437 static inline uint32_t
smsc_hash(uint8_t addr[ETHER_ADDR_LEN])438 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
439 {
440 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
441 }
442
443 void
smsc_iff(struct smsc_softc * sc)444 smsc_iff(struct smsc_softc *sc)
445 {
446 struct ifnet *ifp = &sc->sc_ac.ac_if;
447 struct arpcom *ac = &sc->sc_ac;
448 struct ether_multi *enm;
449 struct ether_multistep step;
450 uint32_t hashtbl[2] = { 0, 0 };
451 uint32_t hash;
452
453 if (usbd_is_dying(sc->sc_udev))
454 return;
455
456 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_HPFILT | SMSC_MAC_CSR_MCPAS |
457 SMSC_MAC_CSR_PRMS);
458 ifp->if_flags &= ~IFF_ALLMULTI;
459
460 if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) {
461 ifp->if_flags |= IFF_ALLMULTI;
462 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
463 if (ifp->if_flags & IFF_PROMISC)
464 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
465 } else {
466 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
467
468 ETHER_FIRST_MULTI(step, ac, enm);
469 while (enm != NULL) {
470 hash = smsc_hash(enm->enm_addrlo);
471
472 hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
473
474 ETHER_NEXT_MULTI(step, enm);
475 }
476 }
477
478 /* Debug */
479 if (sc->sc_mac_csr & SMSC_MAC_CSR_MCPAS)
480 smsc_dbg_printf(sc, "receive all multicast enabled\n");
481 else if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
482 smsc_dbg_printf(sc, "receive select group of macs\n");
483
484 /* Write the hash table and mac control registers */
485 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
486 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
487 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
488 }
489
490 int
smsc_sethwcsum(struct smsc_softc * sc)491 smsc_sethwcsum(struct smsc_softc *sc)
492 {
493 struct ifnet *ifp = &sc->sc_ac.ac_if;
494 uint32_t val;
495 int err;
496
497 if (!ifp)
498 return (-EIO);
499
500 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
501 if (err != 0) {
502 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
503 err);
504 return (err);
505 }
506
507 /* Enable/disable the Rx checksum */
508 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
509 val |= SMSC_COE_CTRL_RX_EN;
510 else
511 val &= ~SMSC_COE_CTRL_RX_EN;
512
513 /* Enable/disable the Tx checksum (currently not supported) */
514 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
515 val |= SMSC_COE_CTRL_TX_EN;
516 else
517 val &= ~SMSC_COE_CTRL_TX_EN;
518
519 err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
520 if (err != 0) {
521 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n",
522 err);
523 return (err);
524 }
525
526 return (0);
527 }
528
529 int
smsc_setmacaddress(struct smsc_softc * sc,const uint8_t * addr)530 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
531 {
532 int err;
533 uint32_t val;
534
535 smsc_dbg_printf(sc, "setting mac address to "
536 "%02x:%02x:%02x:%02x:%02x:%02x\n",
537 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
538
539 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
540 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
541 goto done;
542
543 val = (addr[5] << 8) | addr[4];
544 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
545
546 done:
547 return (err);
548 }
549
550 void
smsc_reset(struct smsc_softc * sc)551 smsc_reset(struct smsc_softc *sc)
552 {
553 if (usbd_is_dying(sc->sc_udev))
554 return;
555
556 /* Wait a little while for the chip to get its brains in order. */
557 DELAY(1000);
558
559 /* Reinitialize controller to achieve full reset. */
560 smsc_chip_init(sc);
561 }
562
563 void
smsc_init(void * xsc)564 smsc_init(void *xsc)
565 {
566 struct smsc_softc *sc = xsc;
567 struct ifnet *ifp = &sc->sc_ac.ac_if;
568 struct smsc_chain *c;
569 usbd_status err;
570 int s, i;
571
572 s = splnet();
573
574 /* Cancel pending I/O */
575 smsc_stop(sc);
576
577 /* Reset the ethernet interface. */
578 smsc_reset(sc);
579
580 /* Init RX ring. */
581 if (smsc_rx_list_init(sc) == ENOBUFS) {
582 printf("%s: rx list init failed\n", sc->sc_dev.dv_xname);
583 splx(s);
584 return;
585 }
586
587 /* Init TX ring. */
588 if (smsc_tx_list_init(sc) == ENOBUFS) {
589 printf("%s: tx list init failed\n", sc->sc_dev.dv_xname);
590 splx(s);
591 return;
592 }
593
594 /* Program promiscuous mode and multicast filters. */
595 smsc_iff(sc);
596
597 /* Open RX and TX pipes. */
598 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
599 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]);
600 if (err) {
601 printf("%s: open rx pipe failed: %s\n",
602 sc->sc_dev.dv_xname, usbd_errstr(err));
603 splx(s);
604 return;
605 }
606
607 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
608 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]);
609 if (err) {
610 printf("%s: open tx pipe failed: %s\n",
611 sc->sc_dev.dv_xname, usbd_errstr(err));
612 splx(s);
613 return;
614 }
615
616 /* Start up the receive pipe. */
617 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
618 c = &sc->sc_cdata.rx_chain[i];
619 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX],
620 c, c->sc_buf, sc->sc_bufsz,
621 USBD_SHORT_XFER_OK | USBD_NO_COPY,
622 USBD_NO_TIMEOUT, smsc_rxeof);
623 usbd_transfer(c->sc_xfer);
624 }
625
626 /* TCP/UDP checksum offload engines. */
627 smsc_sethwcsum(sc);
628
629 /* Indicate we are up and running. */
630 ifp->if_flags |= IFF_RUNNING;
631 ifq_clr_oactive(&ifp->if_snd);
632
633 timeout_add_sec(&sc->sc_stat_ch, 1);
634
635 splx(s);
636 }
637
638 void
smsc_start(struct ifnet * ifp)639 smsc_start(struct ifnet *ifp)
640 {
641 struct smsc_softc *sc = ifp->if_softc;
642 struct mbuf *m_head = NULL;
643
644 /* Don't send anything if there is no link or controller is busy. */
645 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
646 ifq_is_oactive(&ifp->if_snd)) {
647 return;
648 }
649
650 m_head = ifq_dequeue(&ifp->if_snd);
651 if (m_head == NULL)
652 return;
653
654 if (smsc_encap(sc, m_head, 0)) {
655 m_freem(m_head);
656 ifq_set_oactive(&ifp->if_snd);
657 return;
658 }
659
660 #if NBPFILTER > 0
661 if (ifp->if_bpf)
662 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
663 #endif
664 ifq_set_oactive(&ifp->if_snd);
665 }
666
667 void
smsc_tick(void * xsc)668 smsc_tick(void *xsc)
669 {
670 struct smsc_softc *sc = xsc;
671
672 if (sc == NULL)
673 return;
674
675 if (usbd_is_dying(sc->sc_udev))
676 return;
677
678 usb_add_task(sc->sc_udev, &sc->sc_tick_task);
679 }
680
681 void
smsc_stop(struct smsc_softc * sc)682 smsc_stop(struct smsc_softc *sc)
683 {
684 usbd_status err;
685 struct ifnet *ifp;
686 int i;
687
688 smsc_reset(sc);
689
690 ifp = &sc->sc_ac.ac_if;
691 ifp->if_timer = 0;
692 ifp->if_flags &= ~IFF_RUNNING;
693 ifq_clr_oactive(&ifp->if_snd);
694
695 timeout_del(&sc->sc_stat_ch);
696
697 /* Stop transfers. */
698 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
699 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
700 if (err) {
701 printf("%s: close rx pipe failed: %s\n",
702 sc->sc_dev.dv_xname, usbd_errstr(err));
703 }
704 sc->sc_ep[SMSC_ENDPT_RX] = NULL;
705 }
706
707 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
708 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
709 if (err) {
710 printf("%s: close tx pipe failed: %s\n",
711 sc->sc_dev.dv_xname, usbd_errstr(err));
712 }
713 sc->sc_ep[SMSC_ENDPT_TX] = NULL;
714 }
715
716 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
717 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
718 if (err) {
719 printf("%s: close intr pipe failed: %s\n",
720 sc->sc_dev.dv_xname, usbd_errstr(err));
721 }
722 sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
723 }
724
725 /* Free RX resources. */
726 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
727 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
728 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
729 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
730 }
731 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
732 usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
733 sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
734 }
735 }
736
737 /* Free TX resources. */
738 for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
739 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
740 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
741 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
742 }
743 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
744 usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
745 sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
746 }
747 }
748 }
749
750 int
smsc_chip_init(struct smsc_softc * sc)751 smsc_chip_init(struct smsc_softc *sc)
752 {
753 int err;
754 uint32_t reg_val;
755 int burst_cap;
756
757 /* Enter H/W config mode */
758 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
759
760 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
761 SMSC_HW_CFG_LRST)) != 0) {
762 smsc_warn_printf(sc, "timed-out waiting for reset to "
763 "complete\n");
764 goto init_failed;
765 }
766
767 /* Reset the PHY */
768 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
769
770 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
771 SMSC_PM_CTRL_PHY_RST)) != 0) {
772 smsc_warn_printf(sc, "timed-out waiting for phy reset to "
773 "complete\n");
774 goto init_failed;
775 }
776 usbd_delay_ms(sc->sc_udev, 40);
777
778 /* Set the mac address */
779 if ((err = smsc_setmacaddress(sc, sc->sc_ac.ac_enaddr)) != 0) {
780 smsc_warn_printf(sc, "failed to set the MAC address\n");
781 goto init_failed;
782 }
783
784 /*
785 * Don't know what the HW_CFG_BIR bit is, but following the reset
786 * sequence as used in the Linux driver.
787 */
788 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
789 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
790 goto init_failed;
791 }
792 reg_val |= SMSC_HW_CFG_BIR;
793 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
794
795 /*
796 * There is a so called 'turbo mode' that the linux driver supports, it
797 * seems to allow you to jam multiple frames per Rx transaction.
798 * By default this driver supports that and therefore allows multiple
799 * frames per URB.
800 *
801 * The xfer buffer size needs to reflect this as well, therefore based
802 * on the calculations in the Linux driver the RX bufsize is set to
803 * 18944,
804 * bufsz = (16 * 1024 + 5 * 512)
805 *
806 * Burst capability is the number of URBs that can be in a burst of
807 * data/ethernet frames.
808 */
809 #ifdef SMSC_TURBO
810 if (sc->sc_udev->speed == USB_SPEED_HIGH)
811 burst_cap = 37;
812 else
813 burst_cap = 128;
814 #else
815 burst_cap = 0;
816 #endif
817
818 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
819
820 /* Set the default bulk in delay (magic value from Linux driver) */
821 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
822
823
824
825 /*
826 * Initialise the RX interface
827 */
828 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
829 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
830 err);
831 goto init_failed;
832 }
833
834 /*
835 * The following settings are used for 'turbo mode', a.k.a multiple
836 * frames per Rx transaction (again info taken form Linux driver).
837 */
838 #ifdef SMSC_TURBO
839 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
840 #endif
841
842 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
843
844 /* Clear the status register ? */
845 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
846
847 /* Read and display the revision register */
848 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
849 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
850 goto init_failed;
851 }
852
853 /* GPIO/LED setup */
854 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
855 SMSC_LED_GPIO_CFG_FDX_LED;
856 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
857
858 /*
859 * Initialise the TX interface
860 */
861 smsc_write_reg(sc, SMSC_FLOW, 0);
862
863 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
864
865 /* Read the current MAC configuration */
866 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
867 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
868 goto init_failed;
869 }
870
871 /* Vlan */
872 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
873
874 /*
875 * Start TX
876 */
877 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
878 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
879 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
880
881 /*
882 * Start RX
883 */
884 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
885 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
886
887 return (0);
888
889 init_failed:
890 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
891 return (err);
892 }
893
894 int
smsc_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)895 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
896 {
897 struct smsc_softc *sc = ifp->if_softc;
898 struct ifreq *ifr = (struct ifreq *)data;
899 int s, error = 0;
900
901 s = splnet();
902
903 switch(cmd) {
904 case SIOCSIFADDR:
905 ifp->if_flags |= IFF_UP;
906 if (!(ifp->if_flags & IFF_RUNNING))
907 smsc_init(sc);
908 break;
909
910 case SIOCSIFFLAGS:
911 if (ifp->if_flags & IFF_UP) {
912 if (ifp->if_flags & IFF_RUNNING)
913 error = ENETRESET;
914 else
915 smsc_init(sc);
916 } else {
917 if (ifp->if_flags & IFF_RUNNING)
918 smsc_stop(sc);
919 }
920 break;
921
922 case SIOCGIFMEDIA:
923 case SIOCSIFMEDIA:
924 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
925 break;
926
927 default:
928 error = ether_ioctl(ifp, &sc->sc_ac, cmd, data);
929 }
930
931 if (error == ENETRESET) {
932 if (ifp->if_flags & IFF_RUNNING)
933 smsc_iff(sc);
934 error = 0;
935 }
936
937 splx(s);
938 return(error);
939 }
940
941 int
smsc_match(struct device * parent,void * match,void * aux)942 smsc_match(struct device *parent, void *match, void *aux)
943 {
944 struct usb_attach_arg *uaa = aux;
945
946 if (uaa->iface == NULL || uaa->configno != 1)
947 return UMATCH_NONE;
948
949 return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ?
950 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
951 }
952
953 void
smsc_attach(struct device * parent,struct device * self,void * aux)954 smsc_attach(struct device *parent, struct device *self, void *aux)
955 {
956 struct smsc_softc *sc = (struct smsc_softc *)self;
957 struct usb_attach_arg *uaa = aux;
958 usb_interface_descriptor_t *id;
959 usb_endpoint_descriptor_t *ed;
960 struct mii_data *mii;
961 struct ifnet *ifp;
962 uint32_t mac_h, mac_l;
963 int s, i;
964
965 sc->sc_udev = uaa->device;
966 sc->sc_iface = uaa->iface;
967
968 /* Setup the endpoints for the SMSC LAN95xx device(s) */
969 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc,
970 USB_TASK_TYPE_GENERIC);
971 rw_init(&sc->sc_mii_lock, "smscmii");
972 usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc,
973 USB_TASK_TYPE_GENERIC);
974
975 id = usbd_get_interface_descriptor(sc->sc_iface);
976
977 if (sc->sc_udev->speed >= USB_SPEED_HIGH)
978 sc->sc_bufsz = SMSC_MAX_BUFSZ;
979 else
980 sc->sc_bufsz = SMSC_MIN_BUFSZ;
981
982 /* Find endpoints. */
983 for (i = 0; i < id->bNumEndpoints; i++) {
984 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
985 if (!ed) {
986 printf("%s: couldn't get ep %d\n",
987 sc->sc_dev.dv_xname, i);
988 return;
989 }
990 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
991 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
992 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
993 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
994 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
995 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
996 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
997 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
998 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
999 }
1000 }
1001
1002 s = splnet();
1003
1004 ifp = &sc->sc_ac.ac_if;
1005 ifp->if_softc = sc;
1006 strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
1007 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1008 ifp->if_ioctl = smsc_ioctl;
1009 ifp->if_start = smsc_start;
1010 ifp->if_capabilities = IFCAP_VLAN_MTU;
1011
1012 /* Setup some of the basics */
1013 sc->sc_phyno = 1;
1014
1015 /*
1016 * Attempt to get the mac address, if an EEPROM is not attached this
1017 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1018 * address based on urandom.
1019 */
1020 memset(sc->sc_ac.ac_enaddr, 0xff, ETHER_ADDR_LEN);
1021
1022 /* Check if there is already a MAC address in the register */
1023 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1024 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1025 sc->sc_ac.ac_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1026 sc->sc_ac.ac_enaddr[4] = (uint8_t)((mac_h) & 0xff);
1027 sc->sc_ac.ac_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1028 sc->sc_ac.ac_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1029 sc->sc_ac.ac_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1030 sc->sc_ac.ac_enaddr[0] = (uint8_t)((mac_l) & 0xff);
1031 }
1032
1033 smsc_enaddr_OF(sc);
1034
1035 printf("%s: address %s\n", sc->sc_dev.dv_xname,
1036 ether_sprintf(sc->sc_ac.ac_enaddr));
1037
1038 /* Initialise the chip for the first time */
1039 smsc_chip_init(sc);
1040
1041 /* Initialize MII/media info. */
1042 mii = &sc->sc_mii;
1043 mii->mii_ifp = ifp;
1044 mii->mii_readreg = smsc_miibus_readreg;
1045 mii->mii_writereg = smsc_miibus_writereg;
1046 mii->mii_statchg = smsc_miibus_statchg;
1047 mii->mii_flags = MIIF_AUTOTSLEEP;
1048
1049 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
1050 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1051
1052 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1053 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1054 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1055 } else
1056 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1057
1058 if_attach(ifp);
1059 ether_ifattach(ifp);
1060
1061 timeout_set(&sc->sc_stat_ch, smsc_tick, sc);
1062
1063 splx(s);
1064 }
1065
1066 int
smsc_detach(struct device * self,int flags)1067 smsc_detach(struct device *self, int flags)
1068 {
1069 struct smsc_softc *sc = (struct smsc_softc *)self;
1070 struct ifnet *ifp = &sc->sc_ac.ac_if;
1071 int s;
1072
1073 if (timeout_initialized(&sc->sc_stat_ch))
1074 timeout_del(&sc->sc_stat_ch);
1075
1076 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL)
1077 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
1078 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL)
1079 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
1080 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1081 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
1082
1083 /*
1084 * Remove any pending tasks. They cannot be executing because they run
1085 * in the same thread as detach.
1086 */
1087 usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
1088 usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
1089
1090 s = splusb();
1091
1092 if (--sc->sc_refcnt >= 0) {
1093 /* Wait for processes to go away */
1094 usb_detach_wait(&sc->sc_dev);
1095 }
1096
1097 if (ifp->if_flags & IFF_RUNNING)
1098 smsc_stop(sc);
1099
1100 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1101 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
1102 if (ifp->if_softc != NULL) {
1103 ether_ifdetach(ifp);
1104 if_detach(ifp);
1105 }
1106
1107 #ifdef DIAGNOSTIC
1108 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
1109 sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
1110 sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1111 printf("%s: detach has active endpoints\n",
1112 sc->sc_dev.dv_xname);
1113 #endif
1114
1115 splx(s);
1116
1117 return (0);
1118 }
1119
1120 void
smsc_tick_task(void * xsc)1121 smsc_tick_task(void *xsc)
1122 {
1123 int s;
1124 struct smsc_softc *sc = xsc;
1125 struct mii_data *mii;
1126
1127 if (sc == NULL)
1128 return;
1129
1130 if (usbd_is_dying(sc->sc_udev))
1131 return;
1132 mii = &sc->sc_mii;
1133 if (mii == NULL)
1134 return;
1135
1136 s = splnet();
1137
1138 mii_tick(mii);
1139 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
1140 smsc_miibus_statchg(&sc->sc_dev);
1141 timeout_add_sec(&sc->sc_stat_ch, 1);
1142
1143 splx(s);
1144 }
1145
1146 void
smsc_lock_mii(struct smsc_softc * sc)1147 smsc_lock_mii(struct smsc_softc *sc)
1148 {
1149 sc->sc_refcnt++;
1150 rw_enter_write(&sc->sc_mii_lock);
1151 }
1152
1153 void
smsc_unlock_mii(struct smsc_softc * sc)1154 smsc_unlock_mii(struct smsc_softc *sc)
1155 {
1156 rw_exit_write(&sc->sc_mii_lock);
1157 if (--sc->sc_refcnt < 0)
1158 usb_detach_wakeup(&sc->sc_dev);
1159 }
1160
1161 void
smsc_rxeof(struct usbd_xfer * xfer,void * priv,usbd_status status)1162 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1163 {
1164 struct smsc_chain *c = (struct smsc_chain *)priv;
1165 struct smsc_softc *sc = c->sc_sc;
1166 struct ifnet *ifp = &sc->sc_ac.ac_if;
1167 u_char *buf = c->sc_buf;
1168 uint32_t total_len;
1169 uint16_t pktlen = 0;
1170 struct mbuf_list ml = MBUF_LIST_INITIALIZER();
1171 struct mbuf *m;
1172 int s;
1173 uint32_t rxhdr;
1174
1175 if (usbd_is_dying(sc->sc_udev))
1176 return;
1177
1178 if (!(ifp->if_flags & IFF_RUNNING))
1179 return;
1180
1181 if (status != USBD_NORMAL_COMPLETION) {
1182 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1183 return;
1184 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1185 printf("%s: usb errors on rx: %s\n",
1186 sc->sc_dev.dv_xname, usbd_errstr(status));
1187 }
1188 if (status == USBD_STALLED)
1189 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
1190 goto done;
1191 }
1192
1193 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1194 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
1195
1196 do {
1197 if (total_len < sizeof(rxhdr)) {
1198 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %d\n",
1199 total_len, sizeof(rxhdr));
1200 ifp->if_ierrors++;
1201 goto done;
1202 }
1203
1204 buf += pktlen;
1205
1206 memcpy(&rxhdr, buf, sizeof(rxhdr));
1207 rxhdr = letoh32(rxhdr);
1208 total_len -= sizeof(rxhdr);
1209
1210 if (rxhdr & SMSC_RX_STAT_ERROR) {
1211 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1212 ifp->if_ierrors++;
1213 goto done;
1214 }
1215
1216 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1217 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
1218 "0x%08x\n", total_len, pktlen, rxhdr);
1219 if (pktlen > total_len) {
1220 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
1221 pktlen, total_len);
1222 ifp->if_ierrors++;
1223 goto done;
1224 }
1225
1226 buf += sizeof(rxhdr);
1227
1228 if (total_len < pktlen)
1229 total_len = 0;
1230 else
1231 total_len -= pktlen;
1232
1233 m = m_devget(buf, pktlen, ETHER_ALIGN);
1234 if (m == NULL) {
1235 smsc_dbg_printf(sc, "m_devget returned NULL\n");
1236 ifp->if_ierrors++;
1237 goto done;
1238 }
1239
1240 ml_enqueue(&ml, m);
1241 } while (total_len > 0);
1242
1243 done:
1244 s = splnet();
1245 if_input(ifp, &ml);
1246 splx(s);
1247 memset(c->sc_buf, 0, sc->sc_bufsz);
1248
1249 /* Setup new transfer. */
1250 usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX],
1251 c, c->sc_buf, sc->sc_bufsz,
1252 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1253 USBD_NO_TIMEOUT, smsc_rxeof);
1254 usbd_transfer(xfer);
1255
1256 return;
1257 }
1258
1259 void
smsc_txeof(struct usbd_xfer * xfer,void * priv,usbd_status status)1260 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1261 {
1262 struct smsc_softc *sc;
1263 struct smsc_chain *c;
1264 struct ifnet *ifp;
1265 int s;
1266
1267 c = priv;
1268 sc = c->sc_sc;
1269 ifp = &sc->sc_ac.ac_if;
1270
1271 if (usbd_is_dying(sc->sc_udev))
1272 return;
1273
1274 s = splnet();
1275
1276 if (status != USBD_NORMAL_COMPLETION) {
1277 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1278 splx(s);
1279 return;
1280 }
1281 ifp->if_oerrors++;
1282 printf("%s: usb error on tx: %s\n", sc->sc_dev.dv_xname,
1283 usbd_errstr(status));
1284 if (status == USBD_STALLED)
1285 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
1286 splx(s);
1287 return;
1288 }
1289
1290 ifp->if_timer = 0;
1291 ifq_clr_oactive(&ifp->if_snd);
1292
1293 m_freem(c->sc_mbuf);
1294 c->sc_mbuf = NULL;
1295
1296 if (ifq_empty(&ifp->if_snd) == 0)
1297 smsc_start(ifp);
1298
1299 splx(s);
1300 }
1301
1302 int
smsc_tx_list_init(struct smsc_softc * sc)1303 smsc_tx_list_init(struct smsc_softc *sc)
1304 {
1305 struct smsc_cdata *cd;
1306 struct smsc_chain *c;
1307 int i;
1308
1309 cd = &sc->sc_cdata;
1310 for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
1311 c = &cd->tx_chain[i];
1312 c->sc_sc = sc;
1313 c->sc_idx = i;
1314 c->sc_mbuf = NULL;
1315 if (c->sc_xfer == NULL) {
1316 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1317 if (c->sc_xfer == NULL)
1318 return (ENOBUFS);
1319 c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1320 sc->sc_bufsz);
1321 if (c->sc_buf == NULL) {
1322 usbd_free_xfer(c->sc_xfer);
1323 return (ENOBUFS);
1324 }
1325 }
1326 }
1327
1328 return (0);
1329 }
1330
1331 int
smsc_rx_list_init(struct smsc_softc * sc)1332 smsc_rx_list_init(struct smsc_softc *sc)
1333 {
1334 struct smsc_cdata *cd;
1335 struct smsc_chain *c;
1336 int i;
1337
1338 cd = &sc->sc_cdata;
1339 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
1340 c = &cd->rx_chain[i];
1341 c->sc_sc = sc;
1342 c->sc_idx = i;
1343 c->sc_mbuf = NULL;
1344 if (c->sc_xfer == NULL) {
1345 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1346 if (c->sc_xfer == NULL)
1347 return (ENOBUFS);
1348 c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1349 sc->sc_bufsz);
1350 if (c->sc_buf == NULL) {
1351 usbd_free_xfer(c->sc_xfer);
1352 return (ENOBUFS);
1353 }
1354 }
1355 }
1356
1357 return (0);
1358 }
1359
1360 int
smsc_encap(struct smsc_softc * sc,struct mbuf * m,int idx)1361 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
1362 {
1363 struct smsc_chain *c;
1364 usbd_status err;
1365 uint32_t txhdr;
1366 uint32_t frm_len = 0;
1367
1368 c = &sc->sc_cdata.tx_chain[idx];
1369
1370 /*
1371 * Each frame is prefixed with two 32-bit values describing the
1372 * length of the packet and buffer.
1373 */
1374 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1375 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1376 txhdr = htole32(txhdr);
1377 memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
1378
1379 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1380 txhdr = htole32(txhdr);
1381 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
1382
1383 frm_len += 8;
1384
1385 /* Next copy in the actual packet */
1386 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
1387 frm_len += m->m_pkthdr.len;
1388
1389 c->sc_mbuf = m;
1390
1391 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX],
1392 c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1393 10000, smsc_txeof);
1394
1395 err = usbd_transfer(c->sc_xfer);
1396 if (err != USBD_IN_PROGRESS) {
1397 c->sc_mbuf = NULL;
1398 smsc_stop(sc);
1399 return (EIO);
1400 }
1401
1402 sc->sc_cdata.tx_cnt++;
1403
1404 return (0);
1405 }
1406