if_smsc.c revision 1.10.4.1 1 /* $NetBSD: if_smsc.c,v 1.10.4.1 2014/05/18 17:45:47 rmind Exp $ */
2
3 /* $OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $ */
4 /* $FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
5 /*-
6 * Copyright (c) 2012
7 * Ben Gray <bgray (at) freebsd.org>.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * SMSC LAN9xxx devices (http://www.smsc.com/)
33 *
34 * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
35 * support USB 2.0 and 10/100 Mbps Ethernet.
36 *
37 * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
38 * The driver only covers the Ethernet part, the standard USB hub driver
39 * supports the hub part.
40 *
41 * This driver is closely modelled on the Linux driver written and copyrighted
42 * by SMSC.
43 *
44 * H/W TCP & UDP Checksum Offloading
45 * ---------------------------------
46 * The chip supports both tx and rx offloading of UDP & TCP checksums, this
47 * feature can be dynamically enabled/disabled.
48 *
49 * RX checksuming is performed across bytes after the IPv4 header to the end of
50 * the Ethernet frame, this means if the frame is padded with non-zero values
51 * the H/W checksum will be incorrect, however the rx code compensates for this.
52 *
53 * TX checksuming is more complicated, the device requires a special header to
54 * be prefixed onto the start of the frame which indicates the start and end
55 * positions of the UDP or TCP frame. This requires the driver to manually
56 * go through the packet data and decode the headers prior to sending.
57 * On Linux they generally provide cues to the location of the csum and the
58 * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
59 * hence this is not as optimal and therefore h/w TX checksum is currently not
60 * implemented.
61 */
62
63 #ifdef _KERNEL_OPT
64 #include "opt_inet.h"
65 #endif
66
67 #include <sys/param.h>
68 #include <sys/bus.h>
69 #include <sys/systm.h>
70 #include <sys/sockio.h>
71 #include <sys/mbuf.h>
72 #include <sys/mutex.h>
73 #include <sys/kernel.h>
74 #include <sys/proc.h>
75 #include <sys/socket.h>
76
77 #include <sys/device.h>
78
79 #include <sys/rnd.h>
80
81 #include <net/if.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 #include <net/if_ether.h>
85
86 #include <net/bpf.h>
87
88 #ifdef INET
89 #include <netinet/in.h>
90 #include <netinet/if_inarp.h>
91 #endif
92
93 #include <dev/mii/mii.h>
94 #include <dev/mii/miivar.h>
95
96 #include <dev/usb/usb.h>
97 #include <dev/usb/usbdi.h>
98 #include <dev/usb/usbdi_util.h>
99 #include <dev/usb/usbdivar.h>
100 #include <dev/usb/usbdevs.h>
101
102 #include <dev/usb/if_smscreg.h>
103 #include <dev/usb/if_smscvar.h>
104
105 #include "ioconf.h"
106
107 #ifdef USB_DEBUG
108 int smsc_debug = 0;
109 #endif
110
111 /*
112 * Various supported device vendors/products.
113 */
114 static const struct usb_devno smsc_devs[] = {
115 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN89530 },
116 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9530 },
117 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9730 },
118 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500 },
119 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A },
120 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_ALT },
121 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_HAL },
122 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
123 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_ALT },
124 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_SAL10 },
125 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505 },
126 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A },
127 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_HAL },
128 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
129 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505_SAL10 },
130 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14 },
131 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_ALT },
132 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
133 };
134
135 #ifdef USB_DEBUG
136 #define smsc_dbg_printf(sc, fmt, args...) \
137 do { \
138 if (smsc_debug > 0) \
139 printf("debug: " fmt, ##args); \
140 } while(0)
141 #else
142 #define smsc_dbg_printf(sc, fmt, args...)
143 #endif
144
145 #define smsc_warn_printf(sc, fmt, args...) \
146 printf("%s: warning: " fmt, device_xname((sc)->sc_dev), ##args)
147
148 #define smsc_err_printf(sc, fmt, args...) \
149 printf("%s: error: " fmt, device_xname((sc)->sc_dev), ##args)
150
151 /* Function declarations */
152 int smsc_chip_init(struct smsc_softc *);
153 void smsc_setmulti(struct smsc_softc *);
154 int smsc_setmacaddress(struct smsc_softc *, const uint8_t *);
155
156 int smsc_match(device_t, cfdata_t, void *);
157 void smsc_attach(device_t, device_t, void *);
158 int smsc_detach(device_t, int);
159 int smsc_activate(device_t, enum devact);
160
161 int smsc_init(struct ifnet *);
162 void smsc_start(struct ifnet *);
163 int smsc_ioctl(struct ifnet *, u_long, void *);
164 void smsc_stop(struct ifnet *, int);
165
166 void smsc_reset(struct smsc_softc *);
167 struct mbuf *smsc_newbuf(void);
168
169 void smsc_tick(void *);
170 void smsc_tick_task(void *);
171 void smsc_miibus_statchg(struct ifnet *);
172 int smsc_miibus_readreg(device_t, int, int);
173 void smsc_miibus_writereg(device_t, int, int, int);
174 int smsc_ifmedia_upd(struct ifnet *);
175 void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
176 void smsc_lock_mii(struct smsc_softc *);
177 void smsc_unlock_mii(struct smsc_softc *);
178
179 int smsc_tx_list_init(struct smsc_softc *);
180 int smsc_rx_list_init(struct smsc_softc *);
181 int smsc_encap(struct smsc_softc *, struct mbuf *, int);
182 void smsc_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
183 void smsc_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
184
185 int smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
186 int smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
187 int smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
188 int smsc_sethwcsum(struct smsc_softc *);
189
190 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach,
191 smsc_detach, smsc_activate);
192
193 int
194 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
195 {
196 usb_device_request_t req;
197 uint32_t buf;
198 usbd_status err;
199
200 req.bmRequestType = UT_READ_VENDOR_DEVICE;
201 req.bRequest = SMSC_UR_READ_REG;
202 USETW(req.wValue, 0);
203 USETW(req.wIndex, off);
204 USETW(req.wLength, 4);
205
206 err = usbd_do_request(sc->sc_udev, &req, &buf);
207 if (err != 0)
208 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
209
210 *data = le32toh(buf);
211
212 return (err);
213 }
214
215 int
216 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
217 {
218 usb_device_request_t req;
219 uint32_t buf;
220 usbd_status err;
221
222 buf = htole32(data);
223
224 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
225 req.bRequest = SMSC_UR_WRITE_REG;
226 USETW(req.wValue, 0);
227 USETW(req.wIndex, off);
228 USETW(req.wLength, 4);
229
230 err = usbd_do_request(sc->sc_udev, &req, &buf);
231 if (err != 0)
232 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
233
234 return (err);
235 }
236
237 int
238 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
239 {
240 uint32_t val;
241 int err, i;
242
243 for (i = 0; i < 100; i++) {
244 if ((err = smsc_read_reg(sc, reg, &val)) != 0)
245 return (err);
246 if (!(val & bits))
247 return (0);
248 DELAY(5);
249 }
250
251 return (1);
252 }
253
254 int
255 smsc_miibus_readreg(device_t dev, int phy, int reg)
256 {
257 struct smsc_softc *sc = device_private(dev);
258 uint32_t addr;
259 uint32_t val = 0;
260
261 smsc_lock_mii(sc);
262 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
263 smsc_warn_printf(sc, "MII is busy\n");
264 goto done;
265 }
266
267 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
268 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
269
270 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
271 smsc_warn_printf(sc, "MII read timeout\n");
272
273 smsc_read_reg(sc, SMSC_MII_DATA, &val);
274
275 done:
276 smsc_unlock_mii(sc);
277
278 return (val & 0xFFFF);
279 }
280
281 void
282 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
283 {
284 struct smsc_softc *sc = device_private(dev);
285 uint32_t addr;
286
287 if (sc->sc_phyno != phy)
288 return;
289
290 smsc_lock_mii(sc);
291 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
292 smsc_warn_printf(sc, "MII is busy\n");
293 smsc_unlock_mii(sc);
294 return;
295 }
296
297 smsc_write_reg(sc, SMSC_MII_DATA, val);
298
299 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
300 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
301 smsc_unlock_mii(sc);
302
303 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
304 smsc_warn_printf(sc, "MII write timeout\n");
305 }
306
307 void
308 smsc_miibus_statchg(struct ifnet *ifp)
309 {
310 struct smsc_softc *sc = ifp->if_softc;
311 struct mii_data *mii = &sc->sc_mii;
312 int err;
313 uint32_t flow;
314 uint32_t afc_cfg;
315
316 if (mii == NULL || ifp == NULL ||
317 (ifp->if_flags & IFF_RUNNING) == 0)
318 return;
319
320 /* Use the MII status to determine link status */
321 sc->sc_flags &= ~SMSC_FLAG_LINK;
322 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
323 (IFM_ACTIVE | IFM_AVALID)) {
324 switch (IFM_SUBTYPE(mii->mii_media_active)) {
325 case IFM_10_T:
326 case IFM_100_TX:
327 sc->sc_flags |= SMSC_FLAG_LINK;
328 break;
329 case IFM_1000_T:
330 /* Gigabit ethernet not supported by chipset */
331 break;
332 default:
333 break;
334 }
335 }
336
337 /* Lost link, do nothing. */
338 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
339 smsc_dbg_printf(sc, "link flag not set\n");
340 return;
341 }
342
343 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
344 if (err) {
345 smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
346 "error %d\n", err);
347 return;
348 }
349
350 /* Enable/disable full duplex operation and TX/RX pause */
351 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
352 smsc_dbg_printf(sc, "full duplex operation\n");
353 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
354 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
355
356 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
357 flow = 0xffff0002;
358 else
359 flow = 0;
360
361 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
362 afc_cfg |= 0xf;
363 else
364 afc_cfg &= ~0xf;
365
366 } else {
367 smsc_dbg_printf(sc, "half duplex operation\n");
368 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
369 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
370
371 flow = 0;
372 afc_cfg |= 0xf;
373 }
374
375 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
376 err += smsc_write_reg(sc, SMSC_FLOW, flow);
377 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
378 if (err)
379 smsc_warn_printf(sc, "media change failed, error %d\n", err);
380 }
381
382 int
383 smsc_ifmedia_upd(struct ifnet *ifp)
384 {
385 struct smsc_softc *sc = ifp->if_softc;
386 struct mii_data *mii = &sc->sc_mii;
387 int err;
388
389 if (mii->mii_instance) {
390 struct mii_softc *miisc;
391
392 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
393 mii_phy_reset(miisc);
394 }
395 err = mii_mediachg(mii);
396 return (err);
397 }
398
399 void
400 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
401 {
402 struct smsc_softc *sc = ifp->if_softc;
403 struct mii_data *mii = &sc->sc_mii;
404
405 mii_pollstat(mii);
406
407 ifmr->ifm_active = mii->mii_media_active;
408 ifmr->ifm_status = mii->mii_media_status;
409 }
410
411 static inline uint32_t
412 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
413 {
414 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
415 }
416
417 void
418 smsc_setmulti(struct smsc_softc *sc)
419 {
420 struct ifnet *ifp = &sc->sc_ec.ec_if;
421 struct ether_multi *enm;
422 struct ether_multistep step;
423 uint32_t hashtbl[2] = { 0, 0 };
424 uint32_t hash;
425
426 if (sc->sc_dying)
427 return;
428
429 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
430 allmulti:
431 smsc_dbg_printf(sc, "receive all multicast enabled\n");
432 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
433 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
434 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
435 return;
436 } else {
437 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
438 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
439 }
440
441 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
442 while (enm != NULL) {
443 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
444 ETHER_ADDR_LEN) != 0)
445 goto allmulti;
446
447 hash = smsc_hash(enm->enm_addrlo);
448 hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
449 ETHER_NEXT_MULTI(step, enm);
450 }
451
452 /* Debug */
453 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
454 smsc_dbg_printf(sc, "receive select group of macs\n");
455 } else {
456 smsc_dbg_printf(sc, "receive own packets only\n");
457 }
458
459 /* Write the hash table and mac control registers */
460 ifp->if_flags &= ~IFF_ALLMULTI;
461 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
462 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
463 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
464 }
465
466 int
467 smsc_sethwcsum(struct smsc_softc *sc)
468 {
469 struct ifnet *ifp = &sc->sc_ec.ec_if;
470 uint32_t val;
471 int err;
472
473 if (!ifp)
474 return EIO;
475
476 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
477 if (err != 0) {
478 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
479 err);
480 return (err);
481 }
482
483 /* Enable/disable the Rx checksum */
484 if (ifp->if_capabilities & IFCAP_CSUM_IPv4_Rx)
485 val |= SMSC_COE_CTRL_RX_EN;
486 else
487 val &= ~SMSC_COE_CTRL_RX_EN;
488
489 /* Enable/disable the Tx checksum (currently not supported) */
490 if (ifp->if_capabilities & IFCAP_CSUM_IPv4_Tx)
491 val |= SMSC_COE_CTRL_TX_EN;
492 else
493 val &= ~SMSC_COE_CTRL_TX_EN;
494
495 err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
496 if (err != 0) {
497 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n",
498 err);
499 return (err);
500 }
501
502 return (0);
503 }
504
505 int
506 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
507 {
508 int err;
509 uint32_t val;
510
511 smsc_dbg_printf(sc, "setting mac address to "
512 "%02x:%02x:%02x:%02x:%02x:%02x\n",
513 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
514
515 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
516 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
517 goto done;
518
519 val = (addr[5] << 8) | addr[4];
520 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
521
522 done:
523 return (err);
524 }
525
526 void
527 smsc_reset(struct smsc_softc *sc)
528 {
529 if (sc->sc_dying)
530 return;
531
532 /* Wait a little while for the chip to get its brains in order. */
533 DELAY(1000);
534
535 /* Reinitialize controller to achieve full reset. */
536 smsc_chip_init(sc);
537 }
538
539 int
540 smsc_init(struct ifnet *ifp)
541 {
542 struct smsc_softc *sc = ifp->if_softc;
543 struct smsc_chain *c;
544 usbd_status err;
545 int s, i;
546
547 if (sc->sc_dying)
548 return EIO;
549
550 s = splnet();
551
552 /* Cancel pending I/O */
553 if (ifp->if_flags & IFF_RUNNING)
554 smsc_stop(ifp, 1);
555
556 /* Reset the ethernet interface. */
557 smsc_reset(sc);
558
559 /* Init RX ring. */
560 if (smsc_rx_list_init(sc) == ENOBUFS) {
561 aprint_error_dev(sc->sc_dev, "rx list init failed\n");
562 splx(s);
563 return EIO;
564 }
565
566 /* Init TX ring. */
567 if (smsc_tx_list_init(sc) == ENOBUFS) {
568 aprint_error_dev(sc->sc_dev, "tx list init failed\n");
569 splx(s);
570 return EIO;
571 }
572
573 /* Load the multicast filter. */
574 smsc_setmulti(sc);
575
576 /* Open RX and TX pipes. */
577 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
578 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]);
579 if (err) {
580 printf("%s: open rx pipe failed: %s\n",
581 device_xname(sc->sc_dev), usbd_errstr(err));
582 splx(s);
583 return EIO;
584 }
585
586 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
587 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]);
588 if (err) {
589 printf("%s: open tx pipe failed: %s\n",
590 device_xname(sc->sc_dev), usbd_errstr(err));
591 splx(s);
592 return EIO;
593 }
594
595 /* Start up the receive pipe. */
596 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
597 c = &sc->sc_cdata.rx_chain[i];
598 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX],
599 c, c->sc_buf, sc->sc_bufsz,
600 USBD_SHORT_XFER_OK | USBD_NO_COPY,
601 USBD_NO_TIMEOUT, smsc_rxeof);
602 usbd_transfer(c->sc_xfer);
603 }
604
605 /* TCP/UDP checksum offload engines. */
606 smsc_sethwcsum(sc);
607
608 /* Indicate we are up and running. */
609 ifp->if_flags |= IFF_RUNNING;
610 ifp->if_flags &= ~IFF_OACTIVE;
611
612 splx(s);
613
614 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
615
616 return 0;
617 }
618
619 void
620 smsc_start(struct ifnet *ifp)
621 {
622 struct smsc_softc *sc = ifp->if_softc;
623 struct mbuf *m_head = NULL;
624
625 /* Don't send anything if there is no link or controller is busy. */
626 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
627 return;
628 }
629
630 if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
631 return;
632
633 IFQ_POLL(&ifp->if_snd, m_head);
634 if (m_head == NULL)
635 return;
636
637 if (smsc_encap(sc, m_head, 0)) {
638 ifp->if_flags |= IFF_OACTIVE;
639 return;
640 }
641 IFQ_DEQUEUE(&ifp->if_snd, m_head);
642
643 bpf_mtap(ifp, m_head);
644
645 ifp->if_flags |= IFF_OACTIVE;
646
647 /*
648 * Set a timeout in case the chip goes out to lunch.
649 */
650 ifp->if_timer = 5;
651 }
652
653 void
654 smsc_tick(void *xsc)
655 {
656 struct smsc_softc *sc = xsc;
657
658 if (sc == NULL)
659 return;
660
661 if (sc->sc_dying)
662 return;
663
664 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
665 }
666
667 void
668 smsc_stop(struct ifnet *ifp, int disable)
669 {
670 usbd_status err;
671 struct smsc_softc *sc = ifp->if_softc;
672 int i;
673
674 smsc_reset(sc);
675
676 ifp = &sc->sc_ec.ec_if;
677 ifp->if_timer = 0;
678 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
679
680 callout_stop(&sc->sc_stat_ch);
681
682 /* Stop transfers. */
683 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
684 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
685 if (err) {
686 printf("%s: abort rx pipe failed: %s\n",
687 device_xname(sc->sc_dev), usbd_errstr(err));
688 }
689 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
690 if (err) {
691 printf("%s: close rx pipe failed: %s\n",
692 device_xname(sc->sc_dev), usbd_errstr(err));
693 }
694 sc->sc_ep[SMSC_ENDPT_RX] = NULL;
695 }
696
697 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
698 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
699 if (err) {
700 printf("%s: abort tx pipe failed: %s\n",
701 device_xname(sc->sc_dev), usbd_errstr(err));
702 }
703 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
704 if (err) {
705 printf("%s: close tx pipe failed: %s\n",
706 device_xname(sc->sc_dev), usbd_errstr(err));
707 }
708 sc->sc_ep[SMSC_ENDPT_TX] = NULL;
709 }
710
711 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
712 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
713 if (err) {
714 printf("%s: abort intr pipe failed: %s\n",
715 device_xname(sc->sc_dev), usbd_errstr(err));
716 }
717 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
718 if (err) {
719 printf("%s: close intr pipe failed: %s\n",
720 device_xname(sc->sc_dev), usbd_errstr(err));
721 }
722 sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
723 }
724
725 /* Free RX resources. */
726 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
727 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
728 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
729 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
730 }
731 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
732 usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
733 sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
734 }
735 }
736
737 /* Free TX resources. */
738 for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
739 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
740 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
741 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
742 }
743 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
744 usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
745 sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
746 }
747 }
748 }
749
750 int
751 smsc_chip_init(struct smsc_softc *sc)
752 {
753 int err;
754 uint32_t reg_val;
755 int burst_cap;
756
757 /* Enter H/W config mode */
758 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
759
760 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
761 SMSC_HW_CFG_LRST)) != 0) {
762 smsc_warn_printf(sc, "timed-out waiting for reset to "
763 "complete\n");
764 goto init_failed;
765 }
766
767 /* Reset the PHY */
768 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
769
770 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
771 SMSC_PM_CTRL_PHY_RST) != 0)) {
772 smsc_warn_printf(sc, "timed-out waiting for phy reset to "
773 "complete\n");
774 goto init_failed;
775 }
776 usbd_delay_ms(sc->sc_udev, 40);
777
778 /* Set the mac address */
779 struct ifnet *ifp = &sc->sc_ec.ec_if;
780 const char *eaddr = CLLADDR(ifp->if_sadl);
781 if ((err = smsc_setmacaddress(sc, eaddr)) != 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 USB transfer.
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 settings are used for 'turbo mode', a.k.a multiple
836 * frames per Rx transaction (again info taken form Linux driver).
837 */
838 #ifdef SMSC_TURBO
839 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
840 #endif
841
842 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
843
844 /* Clear the status register ? */
845 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
846
847 /* Read and display the revision register */
848 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
849 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
850 goto init_failed;
851 }
852
853 /* GPIO/LED setup */
854 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
855 SMSC_LED_GPIO_CFG_FDX_LED;
856 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
857
858 /*
859 * Initialise the TX interface
860 */
861 smsc_write_reg(sc, SMSC_FLOW, 0);
862
863 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
864
865 /* Read the current MAC configuration */
866 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
867 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
868 goto init_failed;
869 }
870
871 /* Vlan */
872 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
873
874 /*
875 * Start TX
876 */
877 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
878 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
879 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
880
881 /*
882 * Start RX
883 */
884 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
885 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
886
887 return (0);
888
889 init_failed:
890 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
891 return (err);
892 }
893
894 int
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, 0);
1000 usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 0);
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 total_len -= pktlen;
1299
1300 m = smsc_newbuf();
1301 if (m == NULL) {
1302 smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
1303 ifp->if_ierrors++;
1304 goto done;
1305 }
1306
1307 ifp->if_ipackets++;
1308 m->m_pkthdr.rcvif = ifp;
1309
1310 pktlen -= 2; // JDM
1311
1312 m->m_pkthdr.len = m->m_len = pktlen;
1313 #define ETHER_ALIGN 2
1314 m_adj(m, ETHER_ALIGN);
1315
1316 memcpy(mtod(m, char *), buf, pktlen);
1317
1318 /* push the packet up */
1319 s = splnet();
1320 bpf_mtap(ifp, m);
1321 ifp->if_input(ifp, m);
1322 splx(s);
1323 } while (total_len > 0);
1324
1325 done:
1326 memset(c->sc_buf, 0, sc->sc_bufsz);
1327
1328 /* Setup new transfer. */
1329 usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX],
1330 c, c->sc_buf, sc->sc_bufsz,
1331 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1332 USBD_NO_TIMEOUT, smsc_rxeof);
1333 usbd_transfer(xfer);
1334
1335 return;
1336 }
1337
1338 void
1339 smsc_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1340 {
1341 struct smsc_softc *sc;
1342 struct smsc_chain *c;
1343 struct ifnet *ifp;
1344 int s;
1345
1346 c = priv;
1347 sc = c->sc_sc;
1348 ifp = &sc->sc_ec.ec_if;
1349
1350 if (sc->sc_dying)
1351 return;
1352
1353 s = splnet();
1354
1355 ifp->if_timer = 0;
1356 ifp->if_flags &= ~IFF_OACTIVE;
1357
1358 if (status != USBD_NORMAL_COMPLETION) {
1359 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1360 splx(s);
1361 return;
1362 }
1363 ifp->if_oerrors++;
1364 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1365 usbd_errstr(status));
1366 if (status == USBD_STALLED)
1367 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
1368 splx(s);
1369 return;
1370 }
1371 ifp->if_opackets++;
1372
1373 m_freem(c->sc_mbuf);
1374 c->sc_mbuf = NULL;
1375
1376 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1377 smsc_start(ifp);
1378
1379 splx(s);
1380 }
1381
1382 int
1383 smsc_tx_list_init(struct smsc_softc *sc)
1384 {
1385 struct smsc_cdata *cd;
1386 struct smsc_chain *c;
1387 int i;
1388
1389 cd = &sc->sc_cdata;
1390 for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
1391 c = &cd->tx_chain[i];
1392 c->sc_sc = sc;
1393 c->sc_idx = i;
1394 c->sc_mbuf = NULL;
1395 if (c->sc_xfer == NULL) {
1396 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1397 if (c->sc_xfer == NULL)
1398 return (ENOBUFS);
1399 c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1400 sc->sc_bufsz);
1401 if (c->sc_buf == NULL) {
1402 usbd_free_xfer(c->sc_xfer);
1403 return (ENOBUFS);
1404 }
1405 }
1406 }
1407
1408 return (0);
1409 }
1410
1411 int
1412 smsc_rx_list_init(struct smsc_softc *sc)
1413 {
1414 struct smsc_cdata *cd;
1415 struct smsc_chain *c;
1416 int i;
1417
1418 cd = &sc->sc_cdata;
1419 for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
1420 c = &cd->rx_chain[i];
1421 c->sc_sc = sc;
1422 c->sc_idx = i;
1423 c->sc_mbuf = NULL;
1424 if (c->sc_xfer == NULL) {
1425 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1426 if (c->sc_xfer == NULL)
1427 return (ENOBUFS);
1428 c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1429 sc->sc_bufsz);
1430 if (c->sc_buf == NULL) {
1431 usbd_free_xfer(c->sc_xfer);
1432 return (ENOBUFS);
1433 }
1434 }
1435 }
1436
1437 return (0);
1438 }
1439
1440 struct mbuf *
1441 smsc_newbuf(void)
1442 {
1443 struct mbuf *m;
1444
1445 MGETHDR(m, M_DONTWAIT, MT_DATA);
1446 if (m == NULL)
1447 return (NULL);
1448
1449 MCLGET(m, M_DONTWAIT);
1450 if (!(m->m_flags & M_EXT)) {
1451 m_freem(m);
1452 return (NULL);
1453 }
1454
1455 return (m);
1456 }
1457
1458 int
1459 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
1460 {
1461 struct ifnet *ifp = &sc->sc_ec.ec_if;
1462 struct smsc_chain *c;
1463 usbd_status err;
1464 uint32_t txhdr;
1465 uint32_t frm_len = 0;
1466
1467 c = &sc->sc_cdata.tx_chain[idx];
1468
1469 /*
1470 * Each frame is prefixed with two 32-bit values describing the
1471 * length of the packet and buffer.
1472 */
1473 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1474 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1475 txhdr = htole32(txhdr);
1476 memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
1477
1478 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1479 txhdr = htole32(txhdr);
1480 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
1481
1482 frm_len += 8;
1483
1484 /* Next copy in the actual packet */
1485 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
1486 frm_len += m->m_pkthdr.len;
1487
1488 c->sc_mbuf = m;
1489
1490 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX],
1491 c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1492 10000, smsc_txeof);
1493
1494 err = usbd_transfer(c->sc_xfer);
1495 /* XXXNH get task to stop interface */
1496 if (err != USBD_IN_PROGRESS) {
1497 smsc_stop(ifp, 0);
1498 return (EIO);
1499 }
1500
1501 sc->sc_cdata.tx_cnt++;
1502
1503 return (0);
1504 }
1505