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