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