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