Bring in the entire FreeBSD-5 USB infrastructure. As of this commit my
[dragonfly.git] / sys / dev / netif / axe / if_axe.c
CommitLineData
1550dfd9
MD
1/*
2 * Copyright (c) 1997, 1998, 1999, 2000-2003
3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $FreeBSD: src/sys/dev/usb/if_axe.c,v 1.10 2003/12/08 07:54:14 obrien Exp $
33 * $DragonFly: src/sys/dev/netif/axe/if_axe.c,v 1.1 2003/12/30 01:01:46 dillon Exp $
34 */
35/*
36 * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
37 * LinkSys USB200M and various other adapters.
38 *
39 * Manuals available from:
40 * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
41 * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
42 * controller) to find the definitions for the RX control register.
43 * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
44 *
45 * Written by Bill Paul <wpaul@windriver.com>
46 * Senior Engineer
47 * Wind River Systems
48 */
49
50/*
51 * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
52 * It uses an external PHY (reference designs use a RealTek chip),
53 * and has a 64-bit multicast hash filter. There is some information
54 * missing from the manual which one needs to know in order to make
55 * the chip function:
56 *
57 * - You must set bit 7 in the RX control register, otherwise the
58 * chip won't receive any packets.
59 * - You must initialize all 3 IPG registers, or you won't be able
60 * to send any packets.
61 *
62 * Note that this device appears to only support loading the station
63 * address via autload from the EEPROM (i.e. there's no way to manaully
64 * set it).
65 *
66 * (Adam Weinberger wanted me to name this driver if_gir.c.)
67 */
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/sockio.h>
72#include <sys/mbuf.h>
73#include <sys/malloc.h>
74#include <sys/kernel.h>
75#include <sys/socket.h>
76
77#include <net/if.h>
78#include <net/if_arp.h>
79#include <net/ethernet.h>
80#include <net/if_dl.h>
81#include <net/if_media.h>
82
83#include <net/bpf.h>
84
85#include <sys/bus.h>
86#include <machine/bus.h>
87
88#include <bus/usb/usb.h>
89#include <bus/usb/usbdi.h>
90#include <bus/usb/usbdi_util.h>
91#include <bus/usb/usbdivar.h>
92#include <bus/usb/usbdevs.h>
93#include <bus/usb/usb_ethersubr.h>
94
95#include "../mii_layer//mii.h"
96#include "../mii_layer/miivar.h"
97
98/* "controller miibus0" required. See GENERIC if you get errors here. */
99#include "miibus_if.h"
100
101#include "if_axereg.h"
102
103/*
104 * Various supported device vendors/products.
105 */
106Static struct axe_type axe_devs[] = {
107 { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172 },
108 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100 },
109 { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS_USB200M },
110 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120 },
111 { 0, 0 }
112};
113
114Static struct usb_qdat axe_qdat;
115
116Static int axe_match(device_ptr_t);
117Static int axe_attach(device_ptr_t);
118Static int axe_detach(device_ptr_t);
119
120Static int axe_tx_list_init(struct axe_softc *);
121Static int axe_rx_list_init(struct axe_softc *);
122Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
123Static int axe_encap(struct axe_softc *, struct mbuf *, int);
124Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
125Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
126Static void axe_tick(void *);
127Static void axe_rxstart(struct ifnet *);
128Static void axe_start(struct ifnet *);
129Static int axe_ioctl(struct ifnet *, u_long, caddr_t);
130Static void axe_init(void *);
131Static void axe_stop(struct axe_softc *);
132Static void axe_watchdog(struct ifnet *);
133Static void axe_shutdown(device_ptr_t);
134Static int axe_miibus_readreg(device_ptr_t, int, int);
135Static int axe_miibus_writereg(device_ptr_t, int, int, int);
136Static void axe_miibus_statchg(device_ptr_t);
137Static int axe_cmd(struct axe_softc *, int, int, int, void *);
138Static int axe_ifmedia_upd(struct ifnet *);
139Static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
140
141Static void axe_setmulti(struct axe_softc *);
142Static uint32_t axe_mchash(const uint8_t *);
143
144Static device_method_t axe_methods[] = {
145 /* Device interface */
146 DEVMETHOD(device_probe, axe_match),
147 DEVMETHOD(device_attach, axe_attach),
148 DEVMETHOD(device_detach, axe_detach),
149 DEVMETHOD(device_shutdown, axe_shutdown),
150
151 /* bus interface */
152 DEVMETHOD(bus_print_child, bus_generic_print_child),
153 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
154
155 /* MII interface */
156 DEVMETHOD(miibus_readreg, axe_miibus_readreg),
157 DEVMETHOD(miibus_writereg, axe_miibus_writereg),
158 DEVMETHOD(miibus_statchg, axe_miibus_statchg),
159
160 { 0, 0 }
161};
162
163Static driver_t axe_driver = {
164 "axe",
165 axe_methods,
166 sizeof(struct axe_softc)
167};
168
169Static devclass_t axe_devclass;
170
171DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, usbd_driver_load, 0);
172DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
173MODULE_DEPEND(axe, usb, 1, 1, 1);
174MODULE_DEPEND(axe, miibus, 1, 1, 1);
175
176Static int
177axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
178{
179 usb_device_request_t req;
180 usbd_status err;
181
182 if (sc->axe_dying)
183 return(0);
184
185 if (AXE_CMD_DIR(cmd))
186 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
187 else
188 req.bmRequestType = UT_READ_VENDOR_DEVICE;
189 req.bRequest = AXE_CMD_CMD(cmd);
190 USETW(req.wValue, val);
191 USETW(req.wIndex, index);
192 USETW(req.wLength, AXE_CMD_LEN(cmd));
193
194 err = usbd_do_request(sc->axe_udev, &req, buf);
195
196 if (err)
197 return(-1);
198
199 return(0);
200}
201
202Static int
203axe_miibus_readreg(device_ptr_t dev, int phy, int reg)
204{
205 struct axe_softc *sc = USBGETSOFTC(dev);
206 usbd_status err;
207 u_int16_t val;
208
209 if (sc->axe_dying)
210 return(0);
211
212#ifdef notdef
213 /*
214 * The chip tells us the MII address of any supported
215 * PHYs attached to the chip, so only read from those.
216 */
217
218 if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
219 return (0);
220
221 if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
222 return (0);
223#endif
224 if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
225 return (0);
226
227 AXE_LOCK(sc);
228 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
229 err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
230 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
231 AXE_UNLOCK(sc);
232
233 if (err) {
234 printf("axe%d: read PHY failed\n", sc->axe_unit);
235 return(-1);
236 }
237
238 if (val)
239 sc->axe_phyaddrs[0] = phy;
240
241 return (val);
242}
243
244Static int
245axe_miibus_writereg(device_ptr_t dev, int phy, int reg, int val)
246{
247 struct axe_softc *sc = USBGETSOFTC(dev);
248 usbd_status err;
249
250 if (sc->axe_dying)
251 return(0);
252
253 AXE_LOCK(sc);
254 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
255 err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
256 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
257 AXE_UNLOCK(sc);
258
259 if (err) {
260 printf("axe%d: write PHY failed\n", sc->axe_unit);
261 return(-1);
262 }
263
264 return (0);
265}
266
267Static void
268axe_miibus_statchg(device_ptr_t dev)
269{
270#ifdef notdef
271 struct axe_softc *sc = USBGETSOFTC(dev);
272 struct mii_data *mii = GET_MII(sc);
273#endif
274 /* doesn't seem to be necessary */
275
276 return;
277}
278
279/*
280 * Set media options.
281 */
282Static int
283axe_ifmedia_upd(struct ifnet *ifp)
284{
285 struct axe_softc *sc = ifp->if_softc;
286 struct mii_data *mii = GET_MII(sc);
287
288 sc->axe_link = 0;
289 if (mii->mii_instance) {
290 struct mii_softc *miisc;
291 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
292 mii_phy_reset(miisc);
293 }
294 mii_mediachg(mii);
295
296 return (0);
297}
298
299/*
300 * Report current media status.
301 */
302Static void
303axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
304{
305 struct axe_softc *sc = ifp->if_softc;
306 struct mii_data *mii = GET_MII(sc);
307
308 mii_pollstat(mii);
309 ifmr->ifm_active = mii->mii_media_active;
310 ifmr->ifm_status = mii->mii_media_status;
311
312 return;
313}
314
315Static uint32_t
316axe_mchash(const uint8_t *addr)
317{
318 uint32_t crc, carry;
319 int idx, bit;
320 uint8_t data;
321
322 /* Compute CRC for the address value. */
323 crc = 0xFFFFFFFF; /* initial value */
324
325 for (idx = 0; idx < 6; idx++) {
326 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) {
327 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
328 crc <<= 1;
329 if (carry)
330 crc = (crc ^ 0x04c11db6) | carry;
331 }
332 }
333
334 /* return the filter bit position */
335 return((crc >> 26) & 0x0000003F);
336}
337
338Static void
339axe_setmulti(struct axe_softc *sc)
340{
341 struct ifnet *ifp;
342 struct ifmultiaddr *ifma;
343 u_int32_t h = 0;
344 u_int16_t rxmode;
345 u_int8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
346
347 ifp = &sc->arpcom.ac_if;
348
349 AXE_LOCK(sc);
350 axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
351
352 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
353 rxmode |= AXE_RXCMD_ALLMULTI;
354 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
355 AXE_UNLOCK(sc);
356 return;
357 } else
358 rxmode &= ~AXE_RXCMD_ALLMULTI;
359
360 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
361 if (ifma->ifma_addr->sa_family != AF_LINK)
362 continue;
363 h = axe_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
364 hashtbl[h / 8] |= 1 << (h % 8);
365 }
366
367 axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
368 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
369 AXE_UNLOCK(sc);
370
371 return;
372}
373
374Static void
375axe_reset(struct axe_softc *sc)
376{
377 if (sc->axe_dying)
378 return;
379
380 if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1) ||
381 usbd_device2interface_handle(sc->axe_udev, AXE_IFACE_IDX,
382 &sc->axe_iface)) {
383 printf("axe%d: getting interface handle failed\n",
384 sc->axe_unit);
385 }
386
387 /* Wait a little while for the chip to get its brains in order. */
388 DELAY(1000);
389 return;
390}
391
392/*
393 * Probe for a AX88172 chip.
394 */
395USB_MATCH(axe)
396{
397 USB_MATCH_START(axe, uaa);
398 struct axe_type *t;
399
400 if (!uaa->iface)
401 return(UMATCH_NONE);
402
403 t = axe_devs;
404 while(t->axe_vid) {
405 if (uaa->vendor == t->axe_vid &&
406 uaa->product == t->axe_did) {
407 return(UMATCH_VENDOR_PRODUCT);
408 }
409 t++;
410 }
411
412 return(UMATCH_NONE);
413}
414
415/*
416 * Attach the interface. Allocate softc structures, do ifmedia
417 * setup and ethernet/BPF attach.
418 */
419USB_ATTACH(axe)
420{
421 USB_ATTACH_START(axe, sc, uaa);
422 char devinfo[1024];
423 u_char eaddr[ETHER_ADDR_LEN];
424 struct ifnet *ifp;
425 usb_interface_descriptor_t *id;
426 usb_endpoint_descriptor_t *ed;
427 int i;
428
429 bzero(sc, sizeof(struct axe_softc));
430 sc->axe_udev = uaa->device;
431 sc->axe_dev = self;
432 sc->axe_unit = device_get_unit(self);
433
434 if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1)) {
435 printf("axe%d: getting interface handle failed\n",
436 sc->axe_unit);
437 USB_ATTACH_ERROR_RETURN;
438 }
439
440 if (usbd_device2interface_handle(uaa->device,
441 AXE_IFACE_IDX, &sc->axe_iface)) {
442 printf("axe%d: getting interface handle failed\n",
443 sc->axe_unit);
444 USB_ATTACH_ERROR_RETURN;
445 }
446
447 id = usbd_get_interface_descriptor(sc->axe_iface);
448
449 usbd_devinfo(uaa->device, 0, devinfo);
450 device_set_desc_copy(self, devinfo);
451 printf("%s: %s\n", USBDEVNAME(self), devinfo);
452
453 /* Find endpoints. */
454 for (i = 0; i < id->bNumEndpoints; i++) {
455 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
456 if (!ed) {
457 printf("axe%d: couldn't get ep %d\n",
458 sc->axe_unit, i);
459 USB_ATTACH_ERROR_RETURN;
460 }
461 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
462 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
463 sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
464 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
465 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
466 sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
467 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
468 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
469 sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
470 }
471 }
472
473 mtx_init(&sc->axe_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
474 MTX_DEF | MTX_RECURSE);
475 AXE_LOCK(sc);
476
477 /*
478 * Get station address.
479 */
480 axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
481
482 /*
483 * Load IPG values and PHY indexes.
484 */
485 axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
486 axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
487
488 /*
489 * Work around broken adapters that appear to lie about
490 * their PHY addresses.
491 */
492 sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
493
494 /*
495 * An ASIX chip was detected. Inform the world.
496 */
497 printf("axe%d: Ethernet address: %6D\n", sc->axe_unit, eaddr, ":");
498
499 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
500
501 ifp = &sc->arpcom.ac_if;
502 ifp->if_softc = sc;
503 if_initname(ifp, "axe", sc->axe_unit);
504 ifp->if_mtu = ETHERMTU;
505 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
506 ifp->if_ioctl = axe_ioctl;
507 ifp->if_output = ether_output;
508 ifp->if_start = axe_start;
509 ifp->if_watchdog = axe_watchdog;
510 ifp->if_init = axe_init;
511 ifp->if_baudrate = 10000000;
512 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
513
514 axe_qdat.ifp = ifp;
515 axe_qdat.if_rxstart = axe_rxstart;
516
517 if (mii_phy_probe(self, &sc->axe_miibus,
518 axe_ifmedia_upd, axe_ifmedia_sts)) {
519 printf("axe%d: MII without any PHY!\n", sc->axe_unit);
520 AXE_UNLOCK(sc);
521 mtx_destroy(&sc->axe_mtx);
522 USB_ATTACH_ERROR_RETURN;
523 }
524
525 /*
526 * Call MI attach routine.
527 */
528
529 ether_ifattach(ifp, eaddr);
530 callout_handle_init(&sc->axe_stat_ch);
531 usb_register_netisr();
532
533 sc->axe_dying = 0;
534
535 AXE_UNLOCK(sc);
536
537 USB_ATTACH_SUCCESS_RETURN;
538}
539
540Static int
541axe_detach(device_ptr_t dev)
542{
543 struct axe_softc *sc;
544 struct ifnet *ifp;
545
546 sc = device_get_softc(dev);
547 AXE_LOCK(sc);
548 ifp = &sc->arpcom.ac_if;
549
550 sc->axe_dying = 1;
551 untimeout(axe_tick, sc, sc->axe_stat_ch);
552 ether_ifdetach(ifp);
553
554 if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
555 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
556 if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
557 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
558 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
559 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
560
561 AXE_UNLOCK(sc);
562 mtx_destroy(&sc->axe_mtx);
563
564 return(0);
565}
566
567/*
568 * Initialize an RX descriptor and attach an MBUF cluster.
569 */
570Static int
571axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
572{
573 struct mbuf *m_new = NULL;
574
575 if (m == NULL) {
576 m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
577 if (m_new == NULL) {
578 printf("axe%d: no memory for rx list "
579 "-- packet dropped!\n", sc->axe_unit);
580 return(ENOBUFS);
581 }
582 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
583 } else {
584 m_new = m;
585 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
586 m_new->m_data = m_new->m_ext.ext_buf;
587 }
588
589 m_adj(m_new, ETHER_ALIGN);
590 c->axe_mbuf = m_new;
591
592 return(0);
593}
594
595Static int
596axe_rx_list_init(struct axe_softc *sc)
597{
598 struct axe_cdata *cd;
599 struct axe_chain *c;
600 int i;
601
602 cd = &sc->axe_cdata;
603 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
604 c = &cd->axe_rx_chain[i];
605 c->axe_sc = sc;
606 c->axe_idx = i;
607 if (axe_newbuf(sc, c, NULL) == ENOBUFS)
608 return(ENOBUFS);
609 if (c->axe_xfer == NULL) {
610 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
611 if (c->axe_xfer == NULL)
612 return(ENOBUFS);
613 }
614 }
615
616 return(0);
617}
618
619Static int
620axe_tx_list_init(struct axe_softc *sc)
621{
622 struct axe_cdata *cd;
623 struct axe_chain *c;
624 int i;
625
626 cd = &sc->axe_cdata;
627 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
628 c = &cd->axe_tx_chain[i];
629 c->axe_sc = sc;
630 c->axe_idx = i;
631 c->axe_mbuf = NULL;
632 if (c->axe_xfer == NULL) {
633 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
634 if (c->axe_xfer == NULL)
635 return(ENOBUFS);
636 }
637 c->axe_buf = malloc(AXE_BUFSZ, M_USBDEV, M_NOWAIT);
638 if (c->axe_buf == NULL)
639 return(ENOBUFS);
640 }
641
642 return(0);
643}
644
645Static void
646axe_rxstart(struct ifnet *ifp)
647{
648 struct axe_softc *sc;
649 struct axe_chain *c;
650
651 sc = ifp->if_softc;
652 AXE_LOCK(sc);
653 c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
654
655 if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
656 ifp->if_ierrors++;
657 AXE_UNLOCK(sc);
658 return;
659 }
660
661 /* Setup new transfer. */
662 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
663 c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
664 USBD_NO_TIMEOUT, axe_rxeof);
665 usbd_transfer(c->axe_xfer);
666 AXE_UNLOCK(sc);
667
668 return;
669}
670
671/*
672 * A frame has been uploaded: pass the resulting mbuf chain up to
673 * the higher level protocols.
674 */
675Static void
676axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
677{
678 struct axe_softc *sc;
679 struct axe_chain *c;
680 struct mbuf *m;
681 struct ifnet *ifp;
682 int total_len = 0;
683
684 c = priv;
685 sc = c->axe_sc;
686 AXE_LOCK(sc);
687 ifp = &sc->arpcom.ac_if;
688
689 if (!(ifp->if_flags & IFF_RUNNING)) {
690 AXE_UNLOCK(sc);
691 return;
692 }
693
694 if (status != USBD_NORMAL_COMPLETION) {
695 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
696 AXE_UNLOCK(sc);
697 return;
698 }
699 if (usbd_ratecheck(&sc->axe_rx_notice))
700 printf("axe%d: usb error on rx: %s\n", sc->axe_unit,
701 usbd_errstr(status));
702 if (status == USBD_STALLED)
703 usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
704 goto done;
705 }
706
707 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
708
709 m = c->axe_mbuf;
710
711 if (total_len < sizeof(struct ether_header)) {
712 ifp->if_ierrors++;
713 goto done;
714 }
715
716 ifp->if_ipackets++;
717 m->m_pkthdr.rcvif = (struct ifnet *)&axe_qdat;
718 m->m_pkthdr.len = m->m_len = total_len;
719
720 /* Put the packet on the special USB input queue. */
721 usb_ether_input(m);
722 AXE_UNLOCK(sc);
723
724 return;
725done:
726 /* Setup new transfer. */
727 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
728 c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
729 USBD_NO_TIMEOUT, axe_rxeof);
730 usbd_transfer(c->axe_xfer);
731 AXE_UNLOCK(sc);
732
733 return;
734}
735
736/*
737 * A frame was downloaded to the chip. It's safe for us to clean up
738 * the list buffers.
739 */
740
741Static void
742axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
743{
744 struct axe_softc *sc;
745 struct axe_chain *c;
746 struct ifnet *ifp;
747 usbd_status err;
748
749 c = priv;
750 sc = c->axe_sc;
751 AXE_LOCK(sc);
752 ifp = &sc->arpcom.ac_if;
753
754 if (status != USBD_NORMAL_COMPLETION) {
755 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
756 AXE_UNLOCK(sc);
757 return;
758 }
759 printf("axe%d: usb error on tx: %s\n", sc->axe_unit,
760 usbd_errstr(status));
761 if (status == USBD_STALLED)
762 usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_TX]);
763 AXE_UNLOCK(sc);
764 return;
765 }
766
767 ifp->if_timer = 0;
768 ifp->if_flags &= ~IFF_OACTIVE;
769 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &err);
770
771 if (c->axe_mbuf != NULL) {
772 c->axe_mbuf->m_pkthdr.rcvif = ifp;
773 usb_tx_done(c->axe_mbuf);
774 c->axe_mbuf = NULL;
775 }
776
777 if (err)
778 ifp->if_oerrors++;
779 else
780 ifp->if_opackets++;
781
782 AXE_UNLOCK(sc);
783
784 return;
785}
786
787Static void
788axe_tick(void *xsc)
789{
790 struct axe_softc *sc;
791 struct ifnet *ifp;
792 struct mii_data *mii;
793
794 sc = xsc;
795
796 if (sc == NULL)
797 return;
798
799 AXE_LOCK(sc);
800
801 ifp = &sc->arpcom.ac_if;
802 mii = GET_MII(sc);
803 if (mii == NULL) {
804 AXE_UNLOCK(sc);
805 return;
806 }
807
808 mii_tick(mii);
809 if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
810 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
811 sc->axe_link++;
812 if (ifp->if_snd.ifq_head != NULL)
813 axe_start(ifp);
814 }
815
816 sc->axe_stat_ch = timeout(axe_tick, sc, hz);
817
818 AXE_UNLOCK(sc);
819
820 return;
821}
822
823Static int
824axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
825{
826 struct axe_chain *c;
827 usbd_status err;
828
829 c = &sc->axe_cdata.axe_tx_chain[idx];
830
831 /*
832 * Copy the mbuf data into a contiguous buffer, leaving two
833 * bytes at the beginning to hold the frame length.
834 */
835 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
836 c->axe_mbuf = m;
837
838 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
839 c, c->axe_buf, m->m_pkthdr.len, 0, 10000, axe_txeof);
840
841 /* Transmit */
842 err = usbd_transfer(c->axe_xfer);
843 if (err != USBD_IN_PROGRESS) {
844 axe_stop(sc);
845 return(EIO);
846 }
847
848 sc->axe_cdata.axe_tx_cnt++;
849
850 return(0);
851}
852
853Static void
854axe_start(struct ifnet *ifp)
855{
856 struct axe_softc *sc;
857 struct mbuf *m_head = NULL;
858
859 sc = ifp->if_softc;
860 AXE_LOCK(sc);
861
862 if (!sc->axe_link) {
863 AXE_UNLOCK(sc);
864 return;
865 }
866
867 if (ifp->if_flags & IFF_OACTIVE) {
868 AXE_UNLOCK(sc);
869 return;
870 }
871
872 IF_DEQUEUE(&ifp->if_snd, m_head);
873 if (m_head == NULL) {
874 AXE_UNLOCK(sc);
875 return;
876 }
877
878 if (axe_encap(sc, m_head, 0)) {
879 IF_PREPEND(&ifp->if_snd, m_head);
880 ifp->if_flags |= IFF_OACTIVE;
881 AXE_UNLOCK(sc);
882 return;
883 }
884
885 /*
886 * If there's a BPF listener, bounce a copy of this frame
887 * to him.
888 */
889 BPF_MTAP(ifp, m_head);
890
891 ifp->if_flags |= IFF_OACTIVE;
892
893 /*
894 * Set a timeout in case the chip goes out to lunch.
895 */
896 ifp->if_timer = 5;
897 AXE_UNLOCK(sc);
898
899 return;
900}
901
902Static void
903axe_init(void *xsc)
904{
905 struct axe_softc *sc = xsc;
906 struct ifnet *ifp = &sc->arpcom.ac_if;
907 struct axe_chain *c;
908 usbd_status err;
909 int i;
910 int rxmode;
911
912 if (ifp->if_flags & IFF_RUNNING)
913 return;
914
915 AXE_LOCK(sc);
916
917 /*
918 * Cancel pending I/O and free all RX/TX buffers.
919 */
920
921 axe_reset(sc);
922
923#ifdef notdef
924 /* Set MAC address */
925 axe_mac(sc, sc->arpcom.ac_enaddr, 1);
926#endif
927
928 /* Enable RX logic. */
929
930 /* Init TX ring. */
931 if (axe_tx_list_init(sc) == ENOBUFS) {
932 printf("axe%d: tx list init failed\n", sc->axe_unit);
933 AXE_UNLOCK(sc);
934 return;
935 }
936
937 /* Init RX ring. */
938 if (axe_rx_list_init(sc) == ENOBUFS) {
939 printf("axe%d: rx list init failed\n", sc->axe_unit);
940 AXE_UNLOCK(sc);
941 return;
942 }
943
944 /* Set transmitter IPG values */
945 axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
946 axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
947 axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
948
949 /* Enable receiver, set RX mode */
950 rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
951
952 /* If we want promiscuous mode, set the allframes bit. */
953 if (ifp->if_flags & IFF_PROMISC)
954 rxmode |= AXE_RXCMD_PROMISC;
955
956 if (ifp->if_flags & IFF_BROADCAST)
957 rxmode |= AXE_RXCMD_BROADCAST;
958
959 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
960
961 /* Load the multicast filter. */
962 axe_setmulti(sc);
963
964 /* Open RX and TX pipes. */
965 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
966 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
967 if (err) {
968 printf("axe%d: open rx pipe failed: %s\n",
969 sc->axe_unit, usbd_errstr(err));
970 AXE_UNLOCK(sc);
971 return;
972 }
973
974 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
975 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
976 if (err) {
977 printf("axe%d: open tx pipe failed: %s\n",
978 sc->axe_unit, usbd_errstr(err));
979 AXE_UNLOCK(sc);
980 return;
981 }
982
983 /* Start up the receive pipe. */
984 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
985 c = &sc->axe_cdata.axe_rx_chain[i];
986 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
987 c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
988 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
989 usbd_transfer(c->axe_xfer);
990 }
991
992 ifp->if_flags |= IFF_RUNNING;
993 ifp->if_flags &= ~IFF_OACTIVE;
994
995 AXE_UNLOCK(sc);
996
997 sc->axe_stat_ch = timeout(axe_tick, sc, hz);
998
999 return;
1000}
1001
1002Static int
1003axe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1004{
1005 struct axe_softc *sc = ifp->if_softc;
1006 struct ifreq *ifr = (struct ifreq *)data;
1007 struct mii_data *mii;
1008 u_int16_t rxmode;
1009 int error = 0;
1010
1011 switch(command) {
1012 case SIOCSIFFLAGS:
1013 if (ifp->if_flags & IFF_UP) {
1014 if (ifp->if_flags & IFF_RUNNING &&
1015 ifp->if_flags & IFF_PROMISC &&
1016 !(sc->axe_if_flags & IFF_PROMISC)) {
1017 AXE_LOCK(sc);
1018 axe_cmd(sc, AXE_CMD_RXCTL_READ,
1019 0, 0, (void *)&rxmode);
1020 rxmode |= AXE_RXCMD_PROMISC;
1021 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1022 0, rxmode, NULL);
1023 AXE_UNLOCK(sc);
1024 axe_setmulti(sc);
1025 } else if (ifp->if_flags & IFF_RUNNING &&
1026 !(ifp->if_flags & IFF_PROMISC) &&
1027 sc->axe_if_flags & IFF_PROMISC) {
1028 AXE_LOCK(sc);
1029 axe_cmd(sc, AXE_CMD_RXCTL_READ,
1030 0, 0, (void *)&rxmode);
1031 rxmode &= ~AXE_RXCMD_PROMISC;
1032 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1033 0, rxmode, NULL);
1034 AXE_UNLOCK(sc);
1035 axe_setmulti(sc);
1036 } else if (!(ifp->if_flags & IFF_RUNNING))
1037 axe_init(sc);
1038 } else {
1039 if (ifp->if_flags & IFF_RUNNING)
1040 axe_stop(sc);
1041 }
1042 sc->axe_if_flags = ifp->if_flags;
1043 error = 0;
1044 break;
1045 case SIOCADDMULTI:
1046 case SIOCDELMULTI:
1047 axe_setmulti(sc);
1048 error = 0;
1049 break;
1050 case SIOCGIFMEDIA:
1051 case SIOCSIFMEDIA:
1052 mii = GET_MII(sc);
1053 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1054 break;
1055
1056 default:
1057 error = ether_ioctl(ifp, command, data);
1058 break;
1059 }
1060
1061 AXE_UNLOCK(sc);
1062
1063 return(error);
1064}
1065
1066Static void
1067axe_watchdog(struct ifnet *ifp)
1068{
1069 struct axe_softc *sc;
1070 struct axe_chain *c;
1071 usbd_status stat;
1072
1073 sc = ifp->if_softc;
1074 AXE_LOCK(sc);
1075
1076 ifp->if_oerrors++;
1077 printf("axe%d: watchdog timeout\n", sc->axe_unit);
1078
1079 c = &sc->axe_cdata.axe_tx_chain[0];
1080 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1081 axe_txeof(c->axe_xfer, c, stat);
1082
1083 AXE_UNLOCK(sc);
1084
1085 if (ifp->if_snd.ifq_head != NULL)
1086 axe_start(ifp);
1087
1088 return;
1089}
1090
1091/*
1092 * Stop the adapter and free any mbufs allocated to the
1093 * RX and TX lists.
1094 */
1095Static void
1096axe_stop(struct axe_softc *sc)
1097{
1098 usbd_status err;
1099 struct ifnet *ifp;
1100 int i;
1101
1102 AXE_LOCK(sc);
1103
1104 axe_reset(sc);
1105
1106 ifp = &sc->arpcom.ac_if;
1107 ifp->if_timer = 0;
1108
1109 untimeout(axe_tick, sc, sc->axe_stat_ch);
1110
1111 /* Stop transfers. */
1112 if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1113 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1114 if (err) {
1115 printf("axe%d: abort rx pipe failed: %s\n",
1116 sc->axe_unit, usbd_errstr(err));
1117 }
1118 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1119 if (err) {
1120 printf("axe%d: close rx pipe failed: %s\n",
1121 sc->axe_unit, usbd_errstr(err));
1122 }
1123 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1124 }
1125
1126 if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1127 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1128 if (err) {
1129 printf("axe%d: abort tx pipe failed: %s\n",
1130 sc->axe_unit, usbd_errstr(err));
1131 }
1132 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1133 if (err) {
1134 printf("axe%d: close tx pipe failed: %s\n",
1135 sc->axe_unit, usbd_errstr(err));
1136 }
1137 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1138 }
1139
1140 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1141 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1142 if (err) {
1143 printf("axe%d: abort intr pipe failed: %s\n",
1144 sc->axe_unit, usbd_errstr(err));
1145 }
1146 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1147 if (err) {
1148 printf("axe%d: close intr pipe failed: %s\n",
1149 sc->axe_unit, usbd_errstr(err));
1150 }
1151 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1152 }
1153
1154 /* Free RX resources. */
1155 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1156 if (sc->axe_cdata.axe_rx_chain[i].axe_buf != NULL) {
1157 free(sc->axe_cdata.axe_rx_chain[i].axe_buf, M_USBDEV);
1158 sc->axe_cdata.axe_rx_chain[i].axe_buf = NULL;
1159 }
1160 if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1161 m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1162 sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1163 }
1164 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1165 usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1166 sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1167 }
1168 }
1169
1170 /* Free TX resources. */
1171 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1172 if (sc->axe_cdata.axe_tx_chain[i].axe_buf != NULL) {
1173 free(sc->axe_cdata.axe_tx_chain[i].axe_buf, M_USBDEV);
1174 sc->axe_cdata.axe_tx_chain[i].axe_buf = NULL;
1175 }
1176 if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1177 m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1178 sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1179 }
1180 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1181 usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1182 sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1183 }
1184 }
1185
1186 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1187 sc->axe_link = 0;
1188 AXE_UNLOCK(sc);
1189
1190 return;
1191}
1192
1193/*
1194 * Stop all chip I/O so that the kernel's probe routines don't
1195 * get confused by errant DMAs when rebooting.
1196 */
1197Static void
1198axe_shutdown(device_ptr_t dev)
1199{
1200 struct axe_softc *sc;
1201
1202 sc = device_get_softc(dev);
1203
1204 axe_stop(sc);
1205
1206 return;
1207}