if_smsc.c revision 1.11 1 /* $NetBSD: if_smsc.c,v 1.11 2013/09/29 07:25:53 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 struct ifnet *ifp = &sc->sc_ec.ec_if;
782 const char *eaddr = CLLADDR(ifp->if_sadl);
783 if ((err = smsc_setmacaddress(sc, eaddr)) != 0) {
784 smsc_warn_printf(sc, "failed to set the MAC address\n");
785 goto init_failed;
786 }
787
788 /*
789 * Don't know what the HW_CFG_BIR bit is, but following the reset
790 * sequence as used in the Linux driver.
791 */
792 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
793 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
794 goto init_failed;
795 }
796 reg_val |= SMSC_HW_CFG_BIR;
797 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
798
799 /*
800 * There is a so called 'turbo mode' that the linux driver supports, it
801 * seems to allow you to jam multiple frames per Rx transaction.
802 * By default this driver supports that and therefore allows multiple
803 * frames per USB transfer.
804 *
805 * The xfer buffer size needs to reflect this as well, therefore based
806 * on the calculations in the Linux driver the RX bufsize is set to
807 * 18944,
808 * bufsz = (16 * 1024 + 5 * 512)
809 *
810 * Burst capability is the number of URBs that can be in a burst of
811 * data/ethernet frames.
812 */
813 #ifdef SMSC_TURBO
814 if (sc->sc_udev->speed == USB_SPEED_HIGH)
815 burst_cap = 37;
816 else
817 burst_cap = 128;
818 #else
819 burst_cap = 0;
820 #endif
821
822 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
823
824 /* Set the default bulk in delay (magic value from Linux driver) */
825 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
826
827 /*
828 * Initialise the RX interface
829 */
830 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
831 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
832 err);
833 goto init_failed;
834 }
835
836 /*
837 * The following settings are used for 'turbo mode', a.k.a multiple
838 * frames per Rx transaction (again info taken form Linux driver).
839 */
840 #ifdef SMSC_TURBO
841 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
842 #endif
843
844 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
845
846 /* Clear the status register ? */
847 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
848
849 /* Read and display the revision register */
850 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
851 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
852 goto init_failed;
853 }
854
855 /* GPIO/LED setup */
856 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
857 SMSC_LED_GPIO_CFG_FDX_LED;
858 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
859
860 /*
861 * Initialise the TX interface
862 */
863 smsc_write_reg(sc, SMSC_FLOW, 0);
864
865 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
866
867 /* Read the current MAC configuration */
868 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
869 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
870 goto init_failed;
871 }
872
873 /* Vlan */
874 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
875
876 /*
877 * Start TX
878 */
879 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
880 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
881 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
882
883 /*
884 * Start RX
885 */
886 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
887 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
888
889 return (0);
890
891 init_failed:
892 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
893 return (err);
894 }
895
896 int
897 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
898 {
899 struct smsc_softc *sc = ifp->if_softc;
900 struct ifreq /*const*/ *ifr = data;
901 int s, error = 0;
902
903 if (sc->sc_dying)
904 return EIO;
905
906 s = splnet();
907
908 switch(cmd) {
909 case SIOCSIFFLAGS:
910 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
911 break;
912
913 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
914 case IFF_RUNNING:
915 smsc_stop(ifp, 1);
916 break;
917 case IFF_UP:
918 smsc_init(ifp);
919 break;
920 case IFF_UP | IFF_RUNNING:
921 if (ifp->if_flags & IFF_PROMISC &&
922 !(sc->sc_if_flags & IFF_PROMISC)) {
923 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
924 smsc_write_reg(sc, SMSC_MAC_CSR,
925 sc->sc_mac_csr);
926 smsc_setmulti(sc);
927 } else if (!(ifp->if_flags & IFF_PROMISC) &&
928 sc->sc_if_flags & IFF_PROMISC) {
929 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
930 smsc_write_reg(sc, SMSC_MAC_CSR,
931 sc->sc_mac_csr);
932 smsc_setmulti(sc);
933 } else {
934 smsc_init(ifp);
935 }
936 break;
937 }
938 sc->sc_if_flags = ifp->if_flags;
939 break;
940
941 case SIOCGIFMEDIA:
942 case SIOCSIFMEDIA:
943 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
944 break;
945
946 default:
947 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
948 break;
949
950 error = 0;
951
952 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)
953 smsc_setmulti(sc);
954
955 }
956 splx(s);
957
958 return error;
959 }
960
961 int
962 smsc_match(device_t parent, cfdata_t match, void *aux)
963 {
964 struct usb_attach_arg *uaa = aux;
965
966 return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ?
967 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
968 }
969
970 void
971 smsc_attach(device_t parent, device_t self, void *aux)
972 {
973 struct smsc_softc *sc = device_private(self);
974 struct usb_attach_arg *uaa = aux;
975 usbd_device_handle dev = uaa->device;
976 usb_interface_descriptor_t *id;
977 usb_endpoint_descriptor_t *ed;
978 char *devinfop;
979 struct mii_data *mii;
980 struct ifnet *ifp;
981 int err, s, i;
982 uint32_t mac_h, mac_l;
983
984 sc->sc_dev = self;
985 sc->sc_udev = dev;
986
987 aprint_naive("\n");
988 aprint_normal("\n");
989
990 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
991 aprint_normal_dev(self, "%s\n", devinfop);
992 usbd_devinfo_free(devinfop);
993
994 err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
995 if (err) {
996 aprint_error_dev(self, "failed to set configuration"
997 ", err=%s\n", usbd_errstr(err));
998 return;
999 }
1000 /* Setup the endpoints for the SMSC LAN95xx device(s) */
1001 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 0);
1002 usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 0);
1003 mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
1004
1005 err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
1006 if (err) {
1007 aprint_error_dev(self, "getting interface handle failed\n");
1008 return;
1009 }
1010
1011 id = usbd_get_interface_descriptor(sc->sc_iface);
1012
1013 if (sc->sc_udev->speed >= USB_SPEED_HIGH)
1014 sc->sc_bufsz = SMSC_MAX_BUFSZ;
1015 else
1016 sc->sc_bufsz = SMSC_MIN_BUFSZ;
1017
1018 /* Find endpoints. */
1019 for (i = 0; i < id->bNumEndpoints; i++) {
1020 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
1021 if (!ed) {
1022 aprint_error_dev(self, "couldn't get ep %d\n", i);
1023 return;
1024 }
1025 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1026 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1027 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
1028 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1029 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1030 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
1031 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1032 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
1033 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
1034 }
1035 }
1036
1037 s = splnet();
1038
1039 ifp = &sc->sc_ec.ec_if;
1040 ifp->if_softc = sc;
1041 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
1042 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1043 ifp->if_init = smsc_init;
1044 ifp->if_ioctl = smsc_ioctl;
1045 ifp->if_start = smsc_start;
1046 ifp->if_stop = smsc_stop;
1047
1048 sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1049
1050 /* Setup some of the basics */
1051 sc->sc_phyno = 1;
1052
1053 /*
1054 * Attempt to get the mac address, if an EEPROM is not attached this
1055 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1056 * address based on urandom.
1057 */
1058 memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);
1059
1060 prop_dictionary_t dict = device_properties(self);
1061 prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
1062
1063 if (eaprop != NULL) {
1064 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
1065 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
1066 memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
1067 ETHER_ADDR_LEN);
1068 } else
1069 /* Check if there is already a MAC address in the register */
1070 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1071 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1072 sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1073 sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
1074 sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1075 sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1076 sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1077 sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
1078 }
1079
1080 aprint_normal_dev(self, " Ethernet address %s\n", ether_sprintf(sc->sc_enaddr));
1081
1082 IFQ_SET_READY(&ifp->if_snd);
1083
1084 /* Initialize MII/media info. */
1085 mii = &sc->sc_mii;
1086 mii->mii_ifp = ifp;
1087 mii->mii_readreg = smsc_miibus_readreg;
1088 mii->mii_writereg = smsc_miibus_writereg;
1089 mii->mii_statchg = smsc_miibus_statchg;
1090 mii->mii_flags = MIIF_AUTOTSLEEP;
1091 sc->sc_ec.ec_mii = mii;
1092 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
1093 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1094
1095 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1096 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1097 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1098 } else
1099 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1100
1101 if_attach(ifp);
1102 ether_ifattach(ifp, sc->sc_enaddr);
1103
1104 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
1105 RND_TYPE_NET, 0);
1106
1107 callout_init(&sc->sc_stat_ch, 0);
1108
1109 splx(s);
1110
1111 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
1112 }
1113
1114 int
1115 smsc_detach(device_t self, int flags)
1116 {
1117 struct smsc_softc *sc = device_private(self);
1118 struct ifnet *ifp = &sc->sc_ec.ec_if;
1119 int s;
1120
1121 callout_stop(&sc->sc_stat_ch);
1122
1123 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL)
1124 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
1125 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL)
1126 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
1127 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1128 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
1129
1130 /*
1131 * Remove any pending tasks. They cannot be executing because they run
1132 * in the same thread as detach.
1133 */
1134 usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
1135 usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
1136
1137 s = splusb();
1138
1139 if (--sc->sc_refcnt >= 0) {
1140 /* Wait for processes to go away */
1141 usb_detach_waitold(sc->sc_dev);
1142 }
1143
1144 if (ifp->if_flags & IFF_RUNNING)
1145 smsc_stop(ifp ,1);
1146
1147 rnd_detach_source(&sc->sc_rnd_source);
1148 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1149 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
1150 if (ifp->if_softc != NULL) {
1151 ether_ifdetach(ifp);
1152 if_detach(ifp);
1153 }
1154
1155 #ifdef DIAGNOSTIC
1156 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
1157 sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
1158 sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1159 printf("%s: detach has active endpoints\n",
1160 device_xname(sc->sc_dev));
1161 #endif
1162
1163 if (--sc->sc_refcnt >= 0) {
1164 /* Wait for processes to go away. */
1165 usb_detach_waitold(sc->sc_dev);
1166 }
1167 splx(s);
1168
1169 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
1170
1171 mutex_destroy(&sc->sc_mii_lock);
1172
1173 return (0);
1174 }
1175
1176 void
1177 smsc_tick_task(void *xsc)
1178 {
1179 int s;
1180 struct smsc_softc *sc = xsc;
1181 struct ifnet *ifp;
1182 struct mii_data *mii;
1183
1184 if (sc == NULL)
1185 return;
1186
1187 if (sc->sc_dying)
1188 return;
1189 ifp = &sc->sc_ec.ec_if;
1190 mii = &sc->sc_mii;
1191 if (mii == NULL)
1192 return;
1193
1194 s = splnet();
1195
1196 mii_tick(mii);
1197 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
1198 smsc_miibus_statchg(ifp);
1199 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
1200
1201 splx(s);
1202 }
1203
1204 int
1205 smsc_activate(device_t self, enum devact act)
1206 {
1207 struct smsc_softc *sc = device_private(self);
1208
1209 switch (act) {
1210 case DVACT_DEACTIVATE:
1211 if_deactivate(&sc->sc_ec.ec_if);
1212 sc->sc_dying = 1;
1213 return 0;
1214 default:
1215 return EOPNOTSUPP;
1216 }
1217 return (0);
1218 }
1219
1220 void
1221 smsc_lock_mii(struct smsc_softc *sc)
1222 {
1223 sc->sc_refcnt++;
1224 mutex_enter(&sc->sc_mii_lock);
1225 }
1226
1227 void
1228 smsc_unlock_mii(struct smsc_softc *sc)
1229 {
1230 mutex_exit(&sc->sc_mii_lock);
1231 if (--sc->sc_refcnt < 0)
1232 usb_detach_wakeupold(sc->sc_dev);
1233 }
1234
1235 void
1236 smsc_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1237 {
1238 struct smsc_chain *c = (struct smsc_chain *)priv;
1239 struct smsc_softc *sc = c->sc_sc;
1240 struct ifnet *ifp = &sc->sc_ec.ec_if;
1241 u_char *buf = c->sc_buf;
1242 uint32_t total_len;
1243 uint16_t pktlen = 0;
1244 struct mbuf *m;
1245 int s;
1246 uint32_t rxhdr;
1247
1248 if (sc->sc_dying)
1249 return;
1250
1251 if (!(ifp->if_flags & IFF_RUNNING))
1252 return;
1253
1254 if (status != USBD_NORMAL_COMPLETION) {
1255 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1256 return;
1257 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1258 printf("%s: usb errors on rx: %s\n",
1259 device_xname(sc->sc_dev), usbd_errstr(status));
1260 }
1261 if (status == USBD_STALLED)
1262 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
1263 goto done;
1264 }
1265
1266 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1267 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
1268
1269 do {
1270 if (total_len < sizeof(rxhdr)) {
1271 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n",
1272 total_len, sizeof(rxhdr));
1273 ifp->if_ierrors++;
1274 goto done;
1275 }
1276
1277 buf += pktlen;
1278
1279 memcpy(&rxhdr, buf, sizeof(rxhdr));
1280 rxhdr = le32toh(rxhdr);
1281 total_len -= sizeof(rxhdr);
1282
1283 if (rxhdr & SMSC_RX_STAT_ERROR) {
1284 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1285 ifp->if_ierrors++;
1286 goto done;
1287 }
1288
1289 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1290 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
1291 "0x%08x\n", total_len, pktlen, rxhdr);
1292 if (pktlen > total_len) {
1293 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
1294 pktlen, total_len);
1295 ifp->if_ierrors++;
1296 goto done;
1297 }
1298
1299 buf += sizeof(rxhdr);
1300 total_len -= pktlen;
1301
1302 m = smsc_newbuf();
1303 if (m == NULL) {
1304 smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
1305 ifp->if_ierrors++;
1306 goto done;
1307 }
1308
1309 ifp->if_ipackets++;
1310 m->m_pkthdr.rcvif = ifp;
1311
1312 pktlen -= 2; // JDM
1313
1314 m->m_pkthdr.len = m->m_len = pktlen;
1315 #define ETHER_ALIGN 2
1316 m_adj(m, ETHER_ALIGN);
1317
1318 memcpy(mtod(m, char *), buf, pktlen);
1319
1320 /* push the packet up */
1321 s = splnet();
1322 bpf_mtap(ifp, m);
1323 ifp->if_input(ifp, m);
1324 splx(s);
1325 } while (total_len > 0);
1326
1327 done:
1328 memset(c->sc_buf, 0, sc->sc_bufsz);
1329
1330 /* Setup new transfer. */
1331 usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX],
1332 c, c->sc_buf, sc->sc_bufsz,
1333 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1334 USBD_NO_TIMEOUT, smsc_rxeof);
1335 usbd_transfer(xfer);
1336
1337 return;
1338 }
1339
1340 void
1341 smsc_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1342 {
1343 struct smsc_softc *sc;
1344 struct smsc_chain *c;
1345 struct ifnet *ifp;
1346 int s;
1347
1348 c = priv;
1349 sc = c->sc_sc;
1350 ifp = &sc->sc_ec.ec_if;
1351
1352 if (sc->sc_dying)
1353 return;
1354
1355 s = splnet();
1356
1357 ifp->if_timer = 0;
1358 ifp->if_flags &= ~IFF_OACTIVE;
1359
1360 if (status != USBD_NORMAL_COMPLETION) {
1361 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1362 splx(s);
1363 return;
1364 }
1365 ifp->if_oerrors++;
1366 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1367 usbd_errstr(status));
1368 if (status == USBD_STALLED)
1369 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
1370 splx(s);
1371 return;
1372 }
1373 ifp->if_opackets++;
1374
1375 m_freem(c->sc_mbuf);
1376 c->sc_mbuf = NULL;
1377
1378 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1379 smsc_start(ifp);
1380
1381 splx(s);
1382 }
1383
1384 int
1385 smsc_tx_list_init(struct smsc_softc *sc)
1386 {
1387 struct smsc_cdata *cd;
1388 struct smsc_chain *c;
1389 int i;
1390
1391 cd = &sc->sc_cdata;
1392 for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
1393 c = &cd->tx_chain[i];
1394 c->sc_sc = sc;
1395 c->sc_idx = i;
1396 c->sc_mbuf = NULL;
1397 if (c->sc_xfer == NULL) {
1398 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1399 if (c->sc_xfer == NULL)
1400 return (ENOBUFS);
1401 c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1402 sc->sc_bufsz);
1403 if (c->sc_buf == NULL) {
1404 usbd_free_xfer(c->sc_xfer);
1405 return (ENOBUFS);
1406 }
1407 }
1408 }
1409
1410 return (0);
1411 }
1412
1413 int
1414 smsc_rx_list_init(struct smsc_softc *sc)
1415 {
1416 struct smsc_cdata *cd;
1417 struct smsc_chain *c;
1418 int i;
1419
1420 cd = &sc->sc_cdata;
1421 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
1422 c = &cd->rx_chain[i];
1423 c->sc_sc = sc;
1424 c->sc_idx = i;
1425 c->sc_mbuf = NULL;
1426 if (c->sc_xfer == NULL) {
1427 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1428 if (c->sc_xfer == NULL)
1429 return (ENOBUFS);
1430 c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1431 sc->sc_bufsz);
1432 if (c->sc_buf == NULL) {
1433 usbd_free_xfer(c->sc_xfer);
1434 return (ENOBUFS);
1435 }
1436 }
1437 }
1438
1439 return (0);
1440 }
1441
1442 struct mbuf *
1443 smsc_newbuf(void)
1444 {
1445 struct mbuf *m;
1446
1447 MGETHDR(m, M_DONTWAIT, MT_DATA);
1448 if (m == NULL)
1449 return (NULL);
1450
1451 MCLGET(m, M_DONTWAIT);
1452 if (!(m->m_flags & M_EXT)) {
1453 m_freem(m);
1454 return (NULL);
1455 }
1456
1457 return (m);
1458 }
1459
1460 int
1461 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
1462 {
1463 struct ifnet *ifp = &sc->sc_ec.ec_if;
1464 struct smsc_chain *c;
1465 usbd_status err;
1466 uint32_t txhdr;
1467 uint32_t frm_len = 0;
1468
1469 c = &sc->sc_cdata.tx_chain[idx];
1470
1471 /*
1472 * Each frame is prefixed with two 32-bit values describing the
1473 * length of the packet and buffer.
1474 */
1475 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1476 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1477 txhdr = htole32(txhdr);
1478 memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
1479
1480 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1481 txhdr = htole32(txhdr);
1482 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
1483
1484 frm_len += 8;
1485
1486 /* Next copy in the actual packet */
1487 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
1488 frm_len += m->m_pkthdr.len;
1489
1490 c->sc_mbuf = m;
1491
1492 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX],
1493 c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1494 10000, smsc_txeof);
1495
1496 err = usbd_transfer(c->sc_xfer);
1497 /* XXXNH get task to stop interface */
1498 if (err != USBD_IN_PROGRESS) {
1499 smsc_stop(ifp, 0);
1500 return (EIO);
1501 }
1502
1503 sc->sc_cdata.tx_cnt++;
1504
1505 return (0);
1506 }
1507