Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / dev / netif / axe / if_axe.c
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.12 2005/08/29 10:19:51 sephe 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 #include <sys/thread2.h>
77
78 #include <net/if.h>
79 #include <net/ifq_var.h>
80 #include <net/if_arp.h>
81 #include <net/ethernet.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84
85 #include <net/bpf.h>
86
87 #include <sys/bus.h>
88 #include <machine/bus.h>
89
90 #include <bus/usb/usb.h>
91 #include <bus/usb/usbdi.h>
92 #include <bus/usb/usbdi_util.h>
93 #include <bus/usb/usbdivar.h>
94 #include <bus/usb/usbdevs.h>
95 #include <bus/usb/usb_ethersubr.h>
96
97 #include "../mii_layer//mii.h"
98 #include "../mii_layer/miivar.h"
99
100 /* "controller miibus0" required.  See GENERIC if you get errors here. */
101 #include "miibus_if.h"
102
103 #include "if_axereg.h"
104
105 /*
106  * Various supported device vendors/products.
107  */
108 Static struct axe_type axe_devs[] = {
109         { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172 },
110         { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100 },
111         { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M },
112         { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120 },
113         { 0, 0 }
114 };
115
116 Static int axe_match(device_ptr_t);
117 Static int axe_attach(device_ptr_t);
118 Static int axe_detach(device_ptr_t);
119
120 Static int axe_tx_list_init(struct axe_softc *);
121 Static int axe_rx_list_init(struct axe_softc *);
122 Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
123 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
124 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
125 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
126 Static void axe_tick(void *);
127 Static void axe_rxstart(struct ifnet *);
128 Static void axe_start(struct ifnet *);
129 Static int axe_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
130 Static void axe_init(void *);
131 Static void axe_stop(struct axe_softc *);
132 Static void axe_watchdog(struct ifnet *);
133 Static void axe_shutdown(device_ptr_t);
134 Static int axe_miibus_readreg(device_ptr_t, int, int);
135 Static int axe_miibus_writereg(device_ptr_t, int, int, int);
136 Static void axe_miibus_statchg(device_ptr_t);
137 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
138 Static int axe_ifmedia_upd(struct ifnet *);
139 Static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
140
141 Static void axe_setmulti(struct axe_softc *);
142 Static uint32_t axe_mchash(const uint8_t *);
143
144 Static 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
163 Static driver_t axe_driver = {
164         "axe",
165         axe_methods,
166         sizeof(struct axe_softc)
167 };
168
169 Static devclass_t axe_devclass;
170
171 DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, usbd_driver_load, 0);
172 DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
173 MODULE_DEPEND(axe, usb, 1, 1, 1);
174 MODULE_DEPEND(axe, miibus, 1, 1, 1);
175
176 Static int
177 axe_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
202 Static int
203 axe_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         crit_enter();
210
211         if (sc->axe_dying) {
212                 crit_exit();
213                 return(0);
214         }
215
216 #ifdef notdef
217         /*
218          * The chip tells us the MII address of any supported
219          * PHYs attached to the chip, so only read from those.
220          */
221
222         if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0]) {
223                 crit_exit();
224                 return (0);
225         }
226
227         if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1]) {
228                 crit_Exit();
229                 return (0);
230         }
231 #endif
232         if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy) {
233                 crit_exit();
234                 return (0);
235         }
236
237         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
238         err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
239         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
240
241         crit_exit();
242
243         if (err) {
244                 if_printf(&sc->arpcom.ac_if, "read PHY failed\n");
245                 return(-1);
246         }
247
248         if (val)
249                 sc->axe_phyaddrs[0] = phy;
250
251         return (val);
252 }
253
254 Static int
255 axe_miibus_writereg(device_ptr_t dev, int phy, int reg, int val)
256 {
257         struct axe_softc        *sc = USBGETSOFTC(dev);
258         usbd_status             err;
259
260         crit_enter();
261
262         if (sc->axe_dying) {
263                 crit_exit();
264                 return(0);
265         }
266
267         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
268         err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
269         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
270
271         crit_exit();
272
273         if (err) {
274                 if_printf(&sc->arpcom.ac_if, "write PHY failed\n");
275                 return(-1);
276         }
277
278         return (0);
279 }
280
281 Static void
282 axe_miibus_statchg(device_ptr_t dev)
283 {
284 #ifdef notdef
285         struct axe_softc        *sc = USBGETSOFTC(dev);
286         struct mii_data         *mii = GET_MII(sc);
287 #endif
288         /* doesn't seem to be necessary */
289
290         return;
291 }
292
293 /*
294  * Set media options.
295  */
296 Static int
297 axe_ifmedia_upd(struct ifnet *ifp)
298 {
299         struct axe_softc        *sc = ifp->if_softc;
300         struct mii_data         *mii = GET_MII(sc);
301
302         sc->axe_link = 0;
303         if (mii->mii_instance) {
304                 struct mii_softc        *miisc;
305                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
306                          mii_phy_reset(miisc);
307         }
308         mii_mediachg(mii);
309
310         return (0);
311 }
312
313 /*
314  * Report current media status.
315  */
316 Static void
317 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
318 {
319         struct axe_softc        *sc = ifp->if_softc;
320         struct mii_data         *mii = GET_MII(sc);
321
322         mii_pollstat(mii);
323         ifmr->ifm_active = mii->mii_media_active;
324         ifmr->ifm_status = mii->mii_media_status;
325
326         return;
327 }
328
329 Static uint32_t
330 axe_mchash(const uint8_t *addr)
331 {
332         uint32_t crc, carry;
333         int idx, bit;
334         uint8_t data;
335
336         /* Compute CRC for the address value. */
337         crc = 0xFFFFFFFF; /* initial value */
338
339         for (idx = 0; idx < 6; idx++) {
340                 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) {
341                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
342                         crc <<= 1;
343                         if (carry)
344                                 crc = (crc ^ 0x04c11db6) | carry;
345                 }
346         }
347
348         /* return the filter bit position */
349         return((crc >> 26) & 0x0000003F);
350 }
351
352 Static void
353 axe_setmulti(struct axe_softc *sc)
354 {
355         struct ifnet *ifp = &sc->arpcom.ac_if;
356         struct ifmultiaddr      *ifma;
357         u_int32_t               h = 0;
358         u_int16_t               rxmode;
359         u_int8_t                hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
360
361         crit_enter();
362
363         axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
364
365         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
366                 rxmode |= AXE_RXCMD_ALLMULTI;
367                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
368                 crit_exit();
369                 return;
370         } else
371                 rxmode &= ~AXE_RXCMD_ALLMULTI;
372
373         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
374                 if (ifma->ifma_addr->sa_family != AF_LINK)
375                         continue;
376                 h = axe_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
377                 hashtbl[h / 8] |= 1 << (h % 8);
378         }
379
380         axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
381         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
382
383         crit_exit();
384 }
385
386 Static void
387 axe_reset(struct axe_softc *sc)
388 {
389         if (sc->axe_dying)
390                 return;
391
392         if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1) ||
393             usbd_device2interface_handle(sc->axe_udev, AXE_IFACE_IDX,
394             &sc->axe_iface)) {
395                 if_printf(&sc->arpcom.ac_if,
396                           "getting interface handle failed\n");
397         }
398
399         /* Wait a little while for the chip to get its brains in order. */
400         DELAY(1000);
401         return;
402 }
403
404 /*
405  * Probe for a AX88172 chip.
406  */
407 USB_MATCH(axe)
408 {
409         USB_MATCH_START(axe, uaa);
410         struct axe_type                 *t;
411
412         if (!uaa->iface)
413                 return(UMATCH_NONE);
414
415         t = axe_devs;
416         while(t->axe_vid) {
417                 if (uaa->vendor == t->axe_vid &&
418                     uaa->product == t->axe_did) {
419                         return(UMATCH_VENDOR_PRODUCT);
420                 }
421                 t++;
422         }
423
424         return(UMATCH_NONE);
425 }
426
427 /*
428  * Attach the interface. Allocate softc structures, do ifmedia
429  * setup and ethernet/BPF attach.
430  */
431 USB_ATTACH(axe)
432 {
433         USB_ATTACH_START(axe, sc, uaa);
434         char                    devinfo[1024];
435         u_char                  eaddr[ETHER_ADDR_LEN];
436         struct ifnet            *ifp;
437         usb_interface_descriptor_t      *id;
438         usb_endpoint_descriptor_t       *ed;
439         int                     i;
440
441         sc->axe_udev = uaa->device;
442         callout_init(&sc->axe_stat_timer);
443
444         if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1)) {
445                 device_printf(self, "setting config no %d failed\n",
446                     AXE_CONFIG_NO);
447                 USB_ATTACH_ERROR_RETURN;
448         }
449
450         if (usbd_device2interface_handle(uaa->device,
451             AXE_IFACE_IDX, &sc->axe_iface)) {
452                 device_printf(self, "getting interface handle failed\n");
453                 USB_ATTACH_ERROR_RETURN;
454         }
455
456         id = usbd_get_interface_descriptor(sc->axe_iface);
457
458         usbd_devinfo(uaa->device, 0, devinfo);
459         device_set_desc_copy(self, devinfo);
460         device_printf(self, "%s\n", devinfo);
461
462         /* Find endpoints. */
463         for (i = 0; i < id->bNumEndpoints; i++) {
464                 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
465                 if (!ed) {
466                         device_printf(self, "couldn't get ep %d\n", i);
467                         USB_ATTACH_ERROR_RETURN;
468                 }
469                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
470                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
471                         sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
472                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
473                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
474                         sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
475                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
476                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
477                         sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
478                 }
479         }
480
481         /*
482          * Get station address.
483          */
484         axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
485
486         /*
487          * Load IPG values and PHY indexes.
488          */
489         axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
490         axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
491
492         /*
493          * Work around broken adapters that appear to lie about
494          * their PHY addresses.
495          */
496         sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
497
498         ifp = &sc->arpcom.ac_if;
499         ifp->if_softc = sc;
500         if_initname(ifp, device_get_name(self), device_get_unit(self));
501         ifp->if_mtu = ETHERMTU;
502         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
503         ifp->if_ioctl = axe_ioctl;
504         ifp->if_start = axe_start;
505         ifp->if_watchdog = axe_watchdog;
506         ifp->if_init = axe_init;
507         ifp->if_baudrate = 10000000;
508         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
509         ifq_set_ready(&ifp->if_snd);
510
511         if (mii_phy_probe(self, &sc->axe_miibus,
512             axe_ifmedia_upd, axe_ifmedia_sts)) {
513                 device_printf(self, "MII without any PHY!\n");
514                 USB_ATTACH_ERROR_RETURN;
515         }
516
517         /*
518          * Call MI attach routine.
519          */
520
521         ether_ifattach(ifp, eaddr);
522
523         sc->axe_dying = 0;
524
525         usb_register_netisr();
526
527         USB_ATTACH_SUCCESS_RETURN;
528 }
529
530 Static int
531 axe_detach(device_ptr_t dev)
532 {
533         struct axe_softc *sc = device_get_softc(dev);
534         struct ifnet *ifp = &sc->arpcom.ac_if;
535
536         crit_enter();
537
538         sc->axe_dying = 1;
539         callout_stop(&sc->axe_stat_timer);
540         ether_ifdetach(ifp);
541
542         if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
543                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
544         if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
545                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
546         if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
547                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
548
549         crit_exit();
550
551         return(0);
552 }
553
554 /*
555  * Initialize an RX descriptor and attach an MBUF cluster.
556  */
557 Static int
558 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
559 {
560         struct mbuf             *m_new = NULL;
561
562         if (m == NULL) {
563                 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
564                 if (m_new == NULL) {
565                         if_printf(&sc->arpcom.ac_if, "no memory for rx list "
566                             "-- packet dropped!\n");
567                         return(ENOBUFS);
568                 }
569                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
570         } else {
571                 m_new = m;
572                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
573                 m_new->m_data = m_new->m_ext.ext_buf;
574         }
575
576         m_adj(m_new, ETHER_ALIGN);
577         c->axe_mbuf = m_new;
578
579         return(0);
580 }
581
582 Static int
583 axe_rx_list_init(struct axe_softc *sc)
584 {
585         struct axe_cdata        *cd;
586         struct axe_chain        *c;
587         int                     i;
588
589         cd = &sc->axe_cdata;
590         for (i = 0; i < AXE_RX_LIST_CNT; i++) {
591                 c = &cd->axe_rx_chain[i];
592                 c->axe_sc = sc;
593                 c->axe_idx = i;
594                 if (axe_newbuf(sc, c, NULL) == ENOBUFS)
595                         return(ENOBUFS);
596                 if (c->axe_xfer == NULL) {
597                         c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
598                         if (c->axe_xfer == NULL)
599                                 return(ENOBUFS);
600                 }
601         }
602
603         return(0);
604 }
605
606 Static int
607 axe_tx_list_init(struct axe_softc *sc)
608 {
609         struct axe_cdata        *cd;
610         struct axe_chain        *c;
611         int                     i;
612
613         cd = &sc->axe_cdata;
614         for (i = 0; i < AXE_TX_LIST_CNT; i++) {
615                 c = &cd->axe_tx_chain[i];
616                 c->axe_sc = sc;
617                 c->axe_idx = i;
618                 c->axe_mbuf = NULL;
619                 if (c->axe_xfer == NULL) {
620                         c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
621                         if (c->axe_xfer == NULL)
622                                 return(ENOBUFS);
623                 }
624                 c->axe_buf = malloc(AXE_BUFSZ, M_USBDEV, M_WAITOK);
625                 if (c->axe_buf == NULL)
626                         return(ENOBUFS);
627         }
628
629         return(0);
630 }
631
632 Static void
633 axe_rxstart(struct ifnet *ifp)
634 {
635         struct axe_softc *sc = ifp->if_softc;
636         struct axe_chain *c;
637
638         crit_enter();
639         c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
640
641         if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
642                 ifp->if_ierrors++;
643                 crit_exit();
644                 return;
645         }
646
647         /* Setup new transfer. */
648         usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
649             c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
650             USBD_NO_TIMEOUT, axe_rxeof);
651         usbd_transfer(c->axe_xfer);
652
653         crit_exit();
654 }
655
656 /*
657  * A frame has been uploaded: pass the resulting mbuf chain up to
658  * the higher level protocols.
659  */
660 Static void
661 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
662 {
663         struct axe_chain *c = priv;
664         struct axe_softc *sc = c->axe_sc;
665         struct ifnet *ifp = &sc->arpcom.ac_if;
666         struct mbuf *m;
667         int total_len = 0;
668
669         crit_enter();
670
671         if (!(ifp->if_flags & IFF_RUNNING)) {
672                 crit_exit();
673                 return;
674         }
675
676         if (status != USBD_NORMAL_COMPLETION) {
677                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
678                         crit_exit();
679                         return;
680                 }
681                 if (usbd_ratecheck(&sc->axe_rx_notice)) {
682                         if_printf(ifp, "usb error on rx: %s\n",
683                             usbd_errstr(status));
684                 }
685                 if (status == USBD_STALLED)
686                         usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
687                 goto done;
688         }
689
690         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
691
692         m = c->axe_mbuf;
693
694         if (total_len < sizeof(struct ether_header)) {
695                 ifp->if_ierrors++;
696                 goto done;
697         }
698
699         ifp->if_ipackets++;
700         m->m_pkthdr.rcvif = ifp;
701         m->m_pkthdr.len = m->m_len = total_len;
702
703         /* Put the packet on the special USB input queue. */
704         usb_ether_input(m);
705         axe_rxstart(ifp);
706
707         crit_exit();
708         return;
709 done:
710         /* Setup new transfer. */
711         usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
712             c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
713             USBD_NO_TIMEOUT, axe_rxeof);
714         usbd_transfer(c->axe_xfer);
715
716         crit_exit();
717 }
718
719 /*
720  * A frame was downloaded to the chip. It's safe for us to clean up
721  * the list buffers.
722  */
723
724 Static void
725 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
726 {
727         struct axe_chain *c = priv;
728         struct axe_softc *sc = c->axe_sc;
729         struct ifnet *ifp = &sc->arpcom.ac_if;
730         usbd_status err;
731
732         crit_enter();
733
734         if (status != USBD_NORMAL_COMPLETION) {
735                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
736                         crit_exit();
737                         return;
738                 }
739                 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
740                 if (status == USBD_STALLED)
741                         usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_TX]);
742                 crit_exit();
743                 return;
744         }
745
746         ifp->if_timer = 0;
747         ifp->if_flags &= ~IFF_OACTIVE;
748         usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &err);
749
750         if (c->axe_mbuf != NULL) {
751                 m_freem(c->axe_mbuf);
752                 c->axe_mbuf = NULL;
753         }
754
755         if (err)
756                 ifp->if_oerrors++;
757         else
758                 ifp->if_opackets++;
759
760         if (!ifq_is_empty(&ifp->if_snd))
761                 (*ifp->if_start)(ifp);
762
763         crit_exit();
764 }
765
766 Static void
767 axe_tick(void *xsc)
768 {
769         struct axe_softc *sc = xsc;
770         struct ifnet *ifp = &sc->arpcom.ac_if;
771         struct mii_data         *mii;
772
773         mii = GET_MII(sc);
774         if (mii == NULL)
775                 return;
776
777         crit_enter();
778
779         mii_tick(mii);
780         if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
781             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
782                 sc->axe_link++;
783                 if (!ifq_is_empty(&ifp->if_snd))
784                         axe_start(ifp);
785         }
786
787         callout_reset(&sc->axe_stat_timer, hz, axe_tick, sc);
788
789         crit_exit();
790 }
791
792 Static int
793 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
794 {
795         struct axe_chain        *c;
796         usbd_status             err;
797
798         c = &sc->axe_cdata.axe_tx_chain[idx];
799
800         /*
801          * Copy the mbuf data into a contiguous buffer, leaving two
802          * bytes at the beginning to hold the frame length.
803          */
804         m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
805         c->axe_mbuf = m;
806
807         usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
808             c, c->axe_buf, m->m_pkthdr.len, 0, 10000, axe_txeof);
809
810         /* Transmit */
811         err = usbd_transfer(c->axe_xfer);
812         if (err != USBD_IN_PROGRESS) {
813                 axe_stop(sc);
814                 return(EIO);
815         }
816
817         sc->axe_cdata.axe_tx_cnt++;
818
819         return(0);
820 }
821
822 Static void
823 axe_start(struct ifnet *ifp)
824 {
825         struct axe_softc *sc = ifp->if_softc;
826         struct mbuf *m_head = NULL;
827
828         crit_enter();
829
830         if (!sc->axe_link) {
831                 crit_exit();
832                 return;
833         }
834
835         if (ifp->if_flags & IFF_OACTIVE) {
836                 crit_exit();
837                 return;
838         }
839
840         m_head = ifq_poll(&ifp->if_snd);
841         if (m_head == NULL) {
842                 crit_exit();
843                 return;
844         }
845
846         if (axe_encap(sc, m_head, 0)) {
847                 ifp->if_flags |= IFF_OACTIVE;
848                 crit_exit();
849                 return;
850         }
851         m_head = ifq_dequeue(&ifp->if_snd);
852
853         /*
854          * If there's a BPF listener, bounce a copy of this frame
855          * to him.
856          */
857         BPF_MTAP(ifp, m_head);
858
859         ifp->if_flags |= IFF_OACTIVE;
860
861         /*
862          * Set a timeout in case the chip goes out to lunch.
863          */
864         ifp->if_timer = 5;
865
866         crit_exit();
867 }
868
869 Static void
870 axe_init(void *xsc)
871 {
872         struct axe_softc        *sc = xsc;
873         struct ifnet            *ifp = &sc->arpcom.ac_if;
874         struct axe_chain        *c;
875         usbd_status             err;
876         int i, rxmode;
877
878         crit_enter();
879
880         if (ifp->if_flags & IFF_RUNNING) {
881                 crit_exit();
882                 return;
883         }
884
885         /*
886          * Cancel pending I/O and free all RX/TX buffers.
887          */
888
889         axe_reset(sc);
890
891 #ifdef notdef
892         /* Set MAC address */
893         axe_mac(sc, sc->arpcom.ac_enaddr, 1);
894 #endif
895
896         /* Enable RX logic. */
897
898         /* Init TX ring. */
899         if (axe_tx_list_init(sc) == ENOBUFS) {
900                 crit_exit();
901                 if_printf(ifp, "tx list init failed\n");
902                 return;
903         }
904
905         /* Init RX ring. */
906         if (axe_rx_list_init(sc) == ENOBUFS) {
907                 crit_exit();
908                 if_printf(ifp, "rx list init failed\n");
909                 return;
910         }
911
912         /* Set transmitter IPG values */
913         axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
914         axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
915         axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
916
917         /* Enable receiver, set RX mode */
918         rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
919
920         /* If we want promiscuous mode, set the allframes bit. */
921         if (ifp->if_flags & IFF_PROMISC)
922                 rxmode |= AXE_RXCMD_PROMISC;
923
924         if (ifp->if_flags & IFF_BROADCAST)
925                 rxmode |= AXE_RXCMD_BROADCAST;
926
927         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
928
929         /* Load the multicast filter. */
930         axe_setmulti(sc);
931
932         /* Open RX and TX pipes. */
933         err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
934             USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
935         if (err) {
936                 crit_exit();
937                 if_printf(ifp, "open rx pipe failed: %s\n", usbd_errstr(err));
938                 return;
939         }
940
941         err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
942             USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
943         if (err) {
944                 crit_exit();
945                 if_printf(ifp, "open tx pipe failed: %s\n", usbd_errstr(err));
946                 return;
947         }
948
949         /* Start up the receive pipe. */
950         for (i = 0; i < AXE_RX_LIST_CNT; i++) {
951                 c = &sc->axe_cdata.axe_rx_chain[i];
952                 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
953                     c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
954                     USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
955                 usbd_transfer(c->axe_xfer);
956         }
957
958         ifp->if_flags |= IFF_RUNNING;
959         ifp->if_flags &= ~IFF_OACTIVE;
960
961         callout_reset(&sc->axe_stat_timer, hz, axe_tick, sc);
962
963         crit_exit();
964 }
965
966 Static int
967 axe_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
968 {
969         struct axe_softc        *sc = ifp->if_softc;
970         struct ifreq            *ifr = (struct ifreq *)data;
971         struct mii_data         *mii;
972         u_int16_t               rxmode;
973         int error = 0;
974
975         crit_enter();
976
977         switch(command) {
978         case SIOCSIFFLAGS:
979                 if (ifp->if_flags & IFF_UP) {
980                         if (ifp->if_flags & IFF_RUNNING &&
981                             ifp->if_flags & IFF_PROMISC &&
982                             !(sc->axe_if_flags & IFF_PROMISC)) {
983                                 axe_cmd(sc, AXE_CMD_RXCTL_READ,
984                                         0, 0, (void *)&rxmode);
985                                 rxmode |= AXE_RXCMD_PROMISC;
986                                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
987                                         0, rxmode, NULL);
988                                 axe_setmulti(sc);
989                         } else if (ifp->if_flags & IFF_RUNNING &&
990                             !(ifp->if_flags & IFF_PROMISC) &&
991                             sc->axe_if_flags & IFF_PROMISC) {
992                                 axe_cmd(sc, AXE_CMD_RXCTL_READ,
993                                         0, 0, (void *)&rxmode);
994                                 rxmode &= ~AXE_RXCMD_PROMISC;
995                                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
996                                         0, rxmode, NULL);
997                                 axe_setmulti(sc);
998                         } else if (!(ifp->if_flags & IFF_RUNNING))
999                                 axe_init(sc);
1000                 } else {
1001                         if (ifp->if_flags & IFF_RUNNING)
1002                                 axe_stop(sc);
1003                 }
1004                 sc->axe_if_flags = ifp->if_flags;
1005                 error = 0;
1006                 break;
1007         case SIOCADDMULTI:
1008         case SIOCDELMULTI:
1009                 axe_setmulti(sc);
1010                 error = 0;
1011                 break;
1012         case SIOCGIFMEDIA:
1013         case SIOCSIFMEDIA:
1014                 mii = GET_MII(sc);
1015                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1016                 break;
1017
1018         default:
1019                 error = ether_ioctl(ifp, command, data);
1020                 break;
1021         }
1022
1023         crit_exit();
1024
1025         return(error);
1026 }
1027
1028 Static void
1029 axe_watchdog(struct ifnet *ifp)
1030 {
1031         struct axe_softc *sc = ifp->if_softc;
1032         struct axe_chain *c;
1033         usbd_status stat;
1034
1035         crit_enter();
1036
1037         ifp->if_oerrors++;
1038         if_printf(ifp, "watchdog timeout\n");
1039
1040         c = &sc->axe_cdata.axe_tx_chain[0];
1041         usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1042         axe_txeof(c->axe_xfer, c, stat);
1043
1044         if (!ifq_is_empty(&ifp->if_snd))
1045                 axe_start(ifp);
1046
1047         crit_exit();
1048 }
1049
1050 /*
1051  * Stop the adapter and free any mbufs allocated to the
1052  * RX and TX lists.
1053  */
1054 Static void
1055 axe_stop(struct axe_softc *sc)
1056 {
1057         usbd_status             err;
1058         struct ifnet            *ifp;
1059         int i;
1060
1061         crit_enter();
1062
1063         axe_reset(sc);
1064
1065         ifp = &sc->arpcom.ac_if;
1066         ifp->if_timer = 0;
1067
1068         callout_stop(&sc->axe_stat_timer);
1069
1070         /* Stop transfers. */
1071         if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1072                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1073                 if (err) {
1074                         if_printf(ifp, "abort rx pipe failed: %s\n",
1075                             usbd_errstr(err));
1076                 }
1077                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1078                 if (err) {
1079                         if_printf(ifp, "close rx pipe failed: %s\n",
1080                             usbd_errstr(err));
1081                 }
1082                 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1083         }
1084
1085         if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1086                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1087                 if (err) {
1088                         if_printf(ifp, "abort tx pipe failed: %s\n",
1089                             usbd_errstr(err));
1090                 }
1091                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1092                 if (err) {
1093                         if_printf(ifp, "close tx pipe failed: %s\n",
1094                             usbd_errstr(err));
1095                 }
1096                 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1097         }
1098
1099         if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1100                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1101                 if (err) {
1102                         if_printf(ifp, "abort intr pipe failed: %s\n",
1103                             usbd_errstr(err));
1104                 }
1105                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1106                 if (err) {
1107                         if_printf(ifp, "close intr pipe failed: %s\n",
1108                             usbd_errstr(err));
1109                 }
1110                 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1111         }
1112
1113         /* Free RX resources. */
1114         for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1115                 if (sc->axe_cdata.axe_rx_chain[i].axe_buf != NULL) {
1116                         free(sc->axe_cdata.axe_rx_chain[i].axe_buf, M_USBDEV);
1117                         sc->axe_cdata.axe_rx_chain[i].axe_buf = NULL;
1118                 }
1119                 if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1120                         m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1121                         sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1122                 }
1123                 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1124                         usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1125                         sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1126                 }
1127         }
1128
1129         /* Free TX resources. */
1130         for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1131                 if (sc->axe_cdata.axe_tx_chain[i].axe_buf != NULL) {
1132                         free(sc->axe_cdata.axe_tx_chain[i].axe_buf, M_USBDEV);
1133                         sc->axe_cdata.axe_tx_chain[i].axe_buf = NULL;
1134                 }
1135                 if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1136                         m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1137                         sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1138                 }
1139                 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1140                         usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1141                         sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1142                 }
1143         }
1144
1145         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1146         sc->axe_link = 0;
1147
1148         crit_exit();
1149 }
1150
1151 /*
1152  * Stop all chip I/O so that the kernel's probe routines don't
1153  * get confused by errant DMAs when rebooting.
1154  */
1155 Static void
1156 axe_shutdown(device_ptr_t dev)
1157 {
1158         struct axe_softc        *sc;
1159
1160         sc = device_get_softc(dev);
1161
1162         axe_stop(sc);
1163 }