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