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