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