]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/net/if_axge.c
Add ELF Tool Chain's brandelf(1) to contrib
[FreeBSD/FreeBSD.git] / sys / dev / usb / net / if_axge.c
1 /*-
2  * Copyright (c) 2013-2014 Kevin Lo
3  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * ASIX Electronics AX88178A/AX88179 USB 2.0/3.0 gigabit ethernet driver.
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/condvar.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45
46 #include <net/if.h>
47 #include <net/if_var.h>
48
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include "usbdevs.h"
53
54 #define USB_DEBUG_VAR   axge_debug
55 #include <dev/usb/usb_debug.h>
56 #include <dev/usb/usb_process.h>
57
58 #include <dev/usb/net/usb_ethernet.h>
59 #include <dev/usb/net/if_axgereg.h>
60
61 /*
62  * Various supported device vendors/products.
63  */
64
65 static const STRUCT_USB_HOST_ID axge_devs[] = {
66 #define AXGE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
67         AXGE_DEV(ASIX, AX88178A),
68         AXGE_DEV(ASIX, AX88179),
69         AXGE_DEV(DLINK, DUB1312),
70         AXGE_DEV(LENOVO, GIGALAN),
71         AXGE_DEV(SITECOMEU, LN032),
72 #undef AXGE_DEV
73 };
74
75 static const struct {
76         uint8_t ctrl;
77         uint8_t timer_l;
78         uint8_t timer_h;
79         uint8_t size;
80         uint8_t ifg;
81 } __packed axge_bulk_size[] = {
82         { 7, 0x4f, 0x00, 0x12, 0xff },
83         { 7, 0x20, 0x03, 0x16, 0xff },
84         { 7, 0xae, 0x07, 0x18, 0xff },
85         { 7, 0xcc, 0x4c, 0x18, 0x08 }
86 };
87
88 /* prototypes */
89
90 static device_probe_t axge_probe;
91 static device_attach_t axge_attach;
92 static device_detach_t axge_detach;
93
94 static usb_callback_t axge_bulk_read_callback;
95 static usb_callback_t axge_bulk_write_callback;
96
97 static miibus_readreg_t axge_miibus_readreg;
98 static miibus_writereg_t axge_miibus_writereg;
99 static miibus_statchg_t axge_miibus_statchg;
100
101 static uether_fn_t axge_attach_post;
102 static uether_fn_t axge_init;
103 static uether_fn_t axge_stop;
104 static uether_fn_t axge_start;
105 static uether_fn_t axge_tick;
106 static uether_fn_t axge_setmulti;
107 static uether_fn_t axge_setpromisc;
108
109 static int      axge_read_mem(struct axge_softc *, uint8_t, uint16_t,
110                     uint16_t, void *, int);
111 static void     axge_write_mem(struct axge_softc *, uint8_t, uint16_t,
112                     uint16_t, void *, int);
113 static uint8_t  axge_read_cmd_1(struct axge_softc *, uint8_t, uint16_t);
114 static uint16_t axge_read_cmd_2(struct axge_softc *, uint8_t, uint16_t,
115                     uint16_t);
116 static void     axge_write_cmd_1(struct axge_softc *, uint8_t, uint16_t,
117                     uint8_t);
118 static void     axge_write_cmd_2(struct axge_softc *, uint8_t, uint16_t,
119                     uint16_t, uint16_t);
120 static void     axge_chip_init(struct axge_softc *);
121 static void     axge_reset(struct axge_softc *);
122
123 static int      axge_attach_post_sub(struct usb_ether *);
124 static int      axge_ifmedia_upd(struct ifnet *);
125 static void     axge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
126 static int      axge_ioctl(struct ifnet *, u_long, caddr_t);
127 static void     axge_rx_frame(struct usb_ether *, struct usb_page_cache *, int);
128 static void     axge_rxeof(struct usb_ether *, struct usb_page_cache *,
129                     unsigned int, unsigned int, uint32_t);
130 static void     axge_csum_cfg(struct usb_ether *);
131
132 #define AXGE_CSUM_FEATURES      (CSUM_IP | CSUM_TCP | CSUM_UDP)
133
134 #ifdef USB_DEBUG
135 static int axge_debug = 0;
136
137 static SYSCTL_NODE(_hw_usb, OID_AUTO, axge, CTLFLAG_RW, 0, "USB axge");
138 SYSCTL_INT(_hw_usb_axge, OID_AUTO, debug, CTLFLAG_RWTUN, &axge_debug, 0,
139     "Debug level");
140 #endif
141
142 static const struct usb_config axge_config[AXGE_N_TRANSFER] = {
143         [AXGE_BULK_DT_WR] = {
144                 .type = UE_BULK,
145                 .endpoint = UE_ADDR_ANY,
146                 .direction = UE_DIR_OUT,
147                 .frames = 16,
148                 .bufsize = 16 * MCLBYTES,
149                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
150                 .callback = axge_bulk_write_callback,
151                 .timeout = 10000,       /* 10 seconds */
152         },
153         [AXGE_BULK_DT_RD] = {
154                 .type = UE_BULK,
155                 .endpoint = UE_ADDR_ANY,
156                 .direction = UE_DIR_IN,
157                 .bufsize = 65536,
158                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
159                 .callback = axge_bulk_read_callback,
160                 .timeout = 0,           /* no timeout */
161         },
162 };
163
164 static device_method_t axge_methods[] = {
165         /* Device interface. */
166         DEVMETHOD(device_probe,         axge_probe),
167         DEVMETHOD(device_attach,        axge_attach),
168         DEVMETHOD(device_detach,        axge_detach),
169
170         /* MII interface. */
171         DEVMETHOD(miibus_readreg,       axge_miibus_readreg),
172         DEVMETHOD(miibus_writereg,      axge_miibus_writereg),
173         DEVMETHOD(miibus_statchg,       axge_miibus_statchg),
174
175         DEVMETHOD_END
176 };
177
178 static driver_t axge_driver = {
179         .name = "axge",
180         .methods = axge_methods,
181         .size = sizeof(struct axge_softc),
182 };
183
184 static devclass_t axge_devclass;
185
186 DRIVER_MODULE(axge, uhub, axge_driver, axge_devclass, NULL, NULL);
187 DRIVER_MODULE(miibus, axge, miibus_driver, miibus_devclass, NULL, NULL);
188 MODULE_DEPEND(axge, uether, 1, 1, 1);
189 MODULE_DEPEND(axge, usb, 1, 1, 1);
190 MODULE_DEPEND(axge, ether, 1, 1, 1);
191 MODULE_DEPEND(axge, miibus, 1, 1, 1);
192 MODULE_VERSION(axge, 1);
193
194 static const struct usb_ether_methods axge_ue_methods = {
195         .ue_attach_post = axge_attach_post,
196         .ue_attach_post_sub = axge_attach_post_sub,
197         .ue_start = axge_start,
198         .ue_init = axge_init,
199         .ue_stop = axge_stop,
200         .ue_tick = axge_tick,
201         .ue_setmulti = axge_setmulti,
202         .ue_setpromisc = axge_setpromisc,
203         .ue_mii_upd = axge_ifmedia_upd,
204         .ue_mii_sts = axge_ifmedia_sts,
205 };
206
207 static int
208 axge_read_mem(struct axge_softc *sc, uint8_t cmd, uint16_t index,
209     uint16_t val, void *buf, int len)
210 {
211         struct usb_device_request req;
212
213         AXGE_LOCK_ASSERT(sc, MA_OWNED);
214
215         req.bmRequestType = UT_READ_VENDOR_DEVICE;
216         req.bRequest = cmd;
217         USETW(req.wValue, val);
218         USETW(req.wIndex, index);
219         USETW(req.wLength, len);
220
221         return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
222 }
223
224 static void
225 axge_write_mem(struct axge_softc *sc, uint8_t cmd, uint16_t index,
226     uint16_t val, void *buf, int len)
227 {
228         struct usb_device_request req;
229
230         AXGE_LOCK_ASSERT(sc, MA_OWNED);
231
232         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
233         req.bRequest = cmd;
234         USETW(req.wValue, val);
235         USETW(req.wIndex, index);
236         USETW(req.wLength, len);
237
238         if (uether_do_request(&sc->sc_ue, &req, buf, 1000)) {
239                 /* Error ignored. */
240         }
241 }
242
243 static uint8_t
244 axge_read_cmd_1(struct axge_softc *sc, uint8_t cmd, uint16_t reg)
245 {
246         uint8_t val;
247
248         axge_read_mem(sc, cmd, 1, reg, &val, 1);
249         return (val);
250 }
251
252 static uint16_t
253 axge_read_cmd_2(struct axge_softc *sc, uint8_t cmd, uint16_t index,
254     uint16_t reg)
255 {
256         uint8_t val[2];
257
258         axge_read_mem(sc, cmd, index, reg, &val, 2);
259         return (UGETW(val));
260 }
261
262 static void
263 axge_write_cmd_1(struct axge_softc *sc, uint8_t cmd, uint16_t reg, uint8_t val)
264 {
265         axge_write_mem(sc, cmd, 1, reg, &val, 1);
266 }
267
268 static void
269 axge_write_cmd_2(struct axge_softc *sc, uint8_t cmd, uint16_t index,
270     uint16_t reg, uint16_t val)
271 {
272         uint8_t temp[2];
273
274         USETW(temp, val);
275         axge_write_mem(sc, cmd, index, reg, &temp, 2);
276 }
277
278 static int
279 axge_miibus_readreg(device_t dev, int phy, int reg)
280 {
281         struct axge_softc *sc;
282         uint16_t val;
283         int locked;
284
285         sc = device_get_softc(dev);
286         locked = mtx_owned(&sc->sc_mtx);
287         if (!locked)
288                 AXGE_LOCK(sc);
289
290         val = axge_read_cmd_2(sc, AXGE_ACCESS_PHY, reg, phy);
291
292         if (!locked)
293                 AXGE_UNLOCK(sc);
294
295         return (val);
296 }
297
298 static int
299 axge_miibus_writereg(device_t dev, int phy, int reg, int val)
300 {
301         struct axge_softc *sc;
302         int locked;
303
304         sc = device_get_softc(dev);
305         if (sc->sc_phyno != phy)
306                 return (0);
307         locked = mtx_owned(&sc->sc_mtx);
308         if (!locked)
309                 AXGE_LOCK(sc);
310
311         axge_write_cmd_2(sc, AXGE_ACCESS_PHY, reg, phy, val);
312
313         if (!locked)
314                 AXGE_UNLOCK(sc);
315
316         return (0);
317 }
318
319 static void
320 axge_miibus_statchg(device_t dev)
321 {
322         struct axge_softc *sc;
323         struct mii_data *mii;
324         struct ifnet *ifp;
325         uint8_t link_status, tmp[5];
326         uint16_t val;
327         int locked;
328
329         sc = device_get_softc(dev);
330         mii = GET_MII(sc);
331         locked = mtx_owned(&sc->sc_mtx);
332         if (!locked)
333                 AXGE_LOCK(sc);
334
335         ifp = uether_getifp(&sc->sc_ue);
336         if (mii == NULL || ifp == NULL ||
337             (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
338                 goto done;
339
340         sc->sc_flags &= ~AXGE_FLAG_LINK;
341         if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
342             (IFM_ACTIVE | IFM_AVALID)) {
343                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
344                 case IFM_10_T:
345                 case IFM_100_TX:
346                 case IFM_1000_T:
347                         sc->sc_flags |= AXGE_FLAG_LINK;
348                         break;
349                 default:
350                         break;
351                 }
352         }
353
354         /* Lost link, do nothing. */
355         if ((sc->sc_flags & AXGE_FLAG_LINK) == 0)
356                 goto done;
357
358         link_status = axge_read_cmd_1(sc, AXGE_ACCESS_MAC, AXGE_PLSR);
359
360         val = 0;
361         if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
362                 val |= MSR_FD;
363                 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
364                         val |= MSR_TFC;
365                 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
366                         val |= MSR_RFC;
367         }
368         val |=  MSR_RE;
369         switch (IFM_SUBTYPE(mii->mii_media_active)) {
370         case IFM_1000_T:
371                 val |= MSR_GM | MSR_EN_125MHZ;
372                 if (link_status & PLSR_USB_SS)
373                         memcpy(tmp, &axge_bulk_size[0], 5);
374                 else if (link_status & PLSR_USB_HS)
375                         memcpy(tmp, &axge_bulk_size[1], 5);
376                 else
377                         memcpy(tmp, &axge_bulk_size[3], 5);
378                 break;
379         case IFM_100_TX:
380                 val |= MSR_PS;
381                 if (link_status & (PLSR_USB_SS | PLSR_USB_HS))
382                         memcpy(tmp, &axge_bulk_size[2], 5);
383                 else
384                         memcpy(tmp, &axge_bulk_size[3], 5);
385                 break;
386         case IFM_10_T:
387                 memcpy(tmp, &axge_bulk_size[3], 5);
388                 break;
389         }
390         /* Rx bulk configuration. */
391         axge_write_mem(sc, AXGE_ACCESS_MAC, 5, AXGE_RX_BULKIN_QCTRL, tmp, 5);
392         axge_write_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_MSR, val);
393 done:
394         if (!locked)
395                 AXGE_UNLOCK(sc);
396 }
397
398 static void
399 axge_chip_init(struct axge_softc *sc)
400 {
401         /* Power up ethernet PHY. */
402         axge_write_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_EPPRCR, 0);
403         axge_write_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_EPPRCR, EPPRCR_IPRL);
404         uether_pause(&sc->sc_ue, hz / 4);
405         axge_write_cmd_1(sc, AXGE_ACCESS_MAC, AXGE_CLK_SELECT,
406             AXGE_CLK_SELECT_ACS | AXGE_CLK_SELECT_BCS);
407         uether_pause(&sc->sc_ue, hz / 10);
408 }
409
410 static void
411 axge_reset(struct axge_softc *sc)
412 {
413         struct usb_config_descriptor *cd;
414         usb_error_t err;
415
416         cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
417
418         err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
419             cd->bConfigurationValue);
420         if (err)
421                 DPRINTF("reset failed (ignored)\n");
422
423         /* Wait a little while for the chip to get its brains in order. */
424         uether_pause(&sc->sc_ue, hz / 100);
425
426         /* Reinitialize controller to achieve full reset. */
427         axge_chip_init(sc);
428 }
429
430 static void
431 axge_attach_post(struct usb_ether *ue)
432 {
433         struct axge_softc *sc;
434
435         sc = uether_getsc(ue);
436         sc->sc_phyno = 3;
437
438         /* Initialize controller and get station address. */
439         axge_chip_init(sc);
440         axge_read_mem(sc, AXGE_ACCESS_MAC, ETHER_ADDR_LEN, AXGE_NIDR,
441             ue->ue_eaddr, ETHER_ADDR_LEN);
442 }
443
444 static int
445 axge_attach_post_sub(struct usb_ether *ue)
446 {
447         struct axge_softc *sc;
448         struct ifnet *ifp;
449         int error;
450
451         sc = uether_getsc(ue);
452         ifp = ue->ue_ifp;
453         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
454         ifp->if_start = uether_start;
455         ifp->if_ioctl = axge_ioctl;
456         ifp->if_init = uether_init;
457         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
458         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
459         IFQ_SET_READY(&ifp->if_snd);
460
461         ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_TXCSUM | IFCAP_RXCSUM;
462         ifp->if_hwassist = AXGE_CSUM_FEATURES;
463         ifp->if_capenable = ifp->if_capabilities;
464
465         mtx_lock(&Giant);
466         error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
467             uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
468             BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, MIIF_DOPAUSE);
469         mtx_unlock(&Giant);
470
471         return (error);
472 }
473
474 /*
475  * Set media options.
476  */
477 static int
478 axge_ifmedia_upd(struct ifnet *ifp)
479 {
480         struct axge_softc *sc;
481         struct mii_data *mii;
482         struct mii_softc *miisc;
483         int error;
484
485         sc = ifp->if_softc;
486         mii = GET_MII(sc);
487         AXGE_LOCK_ASSERT(sc, MA_OWNED);
488
489         LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
490             PHY_RESET(miisc);
491         error = mii_mediachg(mii);
492
493         return (error);
494 }
495
496 /*
497  * Report current media status.
498  */
499 static void
500 axge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
501 {
502         struct axge_softc *sc;
503         struct mii_data *mii;
504
505         sc = ifp->if_softc;
506         mii = GET_MII(sc);
507         AXGE_LOCK(sc);
508         mii_pollstat(mii);
509         ifmr->ifm_active = mii->mii_media_active;
510         ifmr->ifm_status = mii->mii_media_status;
511         AXGE_UNLOCK(sc);
512 }
513
514 /*
515  * Probe for a AX88179 chip.
516  */
517 static int
518 axge_probe(device_t dev)
519 {
520         struct usb_attach_arg *uaa;
521
522         uaa = device_get_ivars(dev);
523         if (uaa->usb_mode != USB_MODE_HOST)
524                 return (ENXIO);
525         if (uaa->info.bConfigIndex != AXGE_CONFIG_IDX)
526                 return (ENXIO);
527         if (uaa->info.bIfaceIndex != AXGE_IFACE_IDX)
528                 return (ENXIO);
529
530         return (usbd_lookup_id_by_uaa(axge_devs, sizeof(axge_devs), uaa));
531 }
532
533 /*
534  * Attach the interface. Allocate softc structures, do ifmedia
535  * setup and ethernet/BPF attach.
536  */
537 static int
538 axge_attach(device_t dev)
539 {
540         struct usb_attach_arg *uaa;
541         struct axge_softc *sc;
542         struct usb_ether *ue;
543         uint8_t iface_index;
544         int error;
545
546         uaa = device_get_ivars(dev);
547         sc = device_get_softc(dev);
548         ue = &sc->sc_ue;
549
550         device_set_usb_desc(dev);
551         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
552
553         iface_index = AXGE_IFACE_IDX;
554         error = usbd_transfer_setup(uaa->device, &iface_index,
555             sc->sc_xfer, axge_config, AXGE_N_TRANSFER, sc, &sc->sc_mtx);
556         if (error) {
557                 device_printf(dev, "allocating USB transfers failed\n");
558                 goto detach;
559         }
560
561         ue->ue_sc = sc;
562         ue->ue_dev = dev;
563         ue->ue_udev = uaa->device;
564         ue->ue_mtx = &sc->sc_mtx;
565         ue->ue_methods = &axge_ue_methods;
566
567         error = uether_ifattach(ue);
568         if (error) {
569                 device_printf(dev, "could not attach interface\n");
570                 goto detach;
571         }
572         return (0);                     /* success */
573
574 detach:
575         axge_detach(dev);
576         return (ENXIO);                 /* failure */
577 }
578
579 static int
580 axge_detach(device_t dev)
581 {
582         struct axge_softc *sc;
583         struct usb_ether *ue;
584
585         sc = device_get_softc(dev);
586         ue = &sc->sc_ue;
587         usbd_transfer_unsetup(sc->sc_xfer, AXGE_N_TRANSFER);
588         uether_ifdetach(ue);
589         mtx_destroy(&sc->sc_mtx);
590
591         return (0);
592 }
593
594 static void
595 axge_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
596 {
597         struct axge_softc *sc;
598         struct usb_ether *ue;
599         struct usb_page_cache *pc;
600         int actlen;
601
602         sc = usbd_xfer_softc(xfer);
603         ue = &sc->sc_ue;
604         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
605
606         switch (USB_GET_STATE(xfer)) {
607         case USB_ST_TRANSFERRED:
608                 pc = usbd_xfer_get_frame(xfer, 0);
609                 axge_rx_frame(ue, pc, actlen);
610
611                 /* FALLTHROUGH */
612         case USB_ST_SETUP:
613 tr_setup:
614                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
615                 usbd_transfer_submit(xfer);
616                 uether_rxflush(ue);
617                 break;
618
619         default:
620                 if (error != USB_ERR_CANCELLED) {
621                         usbd_xfer_set_stall(xfer);
622                         goto tr_setup;
623                 }
624                 break;
625         }
626 }
627
628 static void
629 axge_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
630 {
631         struct axge_softc *sc;
632         struct ifnet *ifp;
633         struct usb_page_cache *pc;
634         struct mbuf *m;
635         uint32_t txhdr;
636         int nframes, pos;
637
638         sc = usbd_xfer_softc(xfer);
639         ifp = uether_getifp(&sc->sc_ue);
640
641         switch (USB_GET_STATE(xfer)) {
642         case USB_ST_TRANSFERRED:
643                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
644                 /* FALLTHROUGH */
645         case USB_ST_SETUP:
646 tr_setup:
647                 if ((sc->sc_flags & AXGE_FLAG_LINK) == 0 ||
648                     (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
649                         /*
650                          * Don't send anything if there is no link or
651                          * controller is busy.
652                          */
653                         return;
654                 }
655
656                 for (nframes = 0; nframes < 16 &&
657                     !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
658                         IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
659                         if (m == NULL)
660                                 break;
661                         usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
662                                 nframes);
663                         pos = 0;
664                         pc = usbd_xfer_get_frame(xfer, nframes);
665                         txhdr = htole32(m->m_pkthdr.len);
666                         usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
667                         txhdr = 0;
668                         txhdr = htole32(txhdr);
669                         usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
670                         pos += 8;
671                         usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
672                         pos += m->m_pkthdr.len;
673                         if ((pos % usbd_xfer_max_framelen(xfer)) == 0)
674                                 txhdr |= 0x80008000;
675
676                         /*
677                          * XXX
678                          * Update TX packet counter here. This is not
679                          * correct way but it seems that there is no way
680                          * to know how many packets are sent at the end
681                          * of transfer because controller combines
682                          * multiple writes into single one if there is
683                          * room in TX buffer of controller.
684                          */
685                         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
686
687                         /*
688                          * if there's a BPF listener, bounce a copy
689                          * of this frame to him:
690                          */
691                         BPF_MTAP(ifp, m);
692
693                         m_freem(m);
694
695                         /* Set frame length. */
696                         usbd_xfer_set_frame_len(xfer, nframes, pos);
697                 }
698                 if (nframes != 0) {
699                         usbd_xfer_set_frames(xfer, nframes);
700                         usbd_transfer_submit(xfer);
701                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
702                 }
703                 return;
704                 /* NOTREACHED */
705         default:
706                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
707                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
708
709                 if (error != USB_ERR_CANCELLED) {
710                         usbd_xfer_set_stall(xfer);
711                         goto tr_setup;
712                 }
713                 return;
714
715         }
716 }
717
718 static void
719 axge_tick(struct usb_ether *ue)
720 {
721         struct axge_softc *sc;
722         struct mii_data *mii;
723
724         sc = uether_getsc(ue);
725         mii = GET_MII(sc);
726         AXGE_LOCK_ASSERT(sc, MA_OWNED);
727
728         mii_tick(mii);
729         if ((sc->sc_flags & AXGE_FLAG_LINK) == 0) {
730                 axge_miibus_statchg(ue->ue_dev);
731                 if ((sc->sc_flags & AXGE_FLAG_LINK) != 0)
732                         axge_start(ue);
733         }
734 }
735
736 static void
737 axge_setmulti(struct usb_ether *ue)
738 {
739         struct axge_softc *sc;
740         struct ifnet *ifp;
741         struct ifmultiaddr *ifma;
742         uint32_t h;
743         uint16_t rxmode;
744         uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
745
746         sc = uether_getsc(ue);
747         ifp = uether_getifp(ue);
748         h = 0;
749         AXGE_LOCK_ASSERT(sc, MA_OWNED);
750
751         rxmode = axge_read_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_RCR);
752         if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
753                 rxmode |= RCR_AMALL;
754                 axge_write_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_RCR, rxmode);
755                 return;
756         }
757         rxmode &= ~RCR_AMALL;
758
759         if_maddr_rlock(ifp);
760         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
761                 if (ifma->ifma_addr->sa_family != AF_LINK)
762                         continue;
763                 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
764                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
765                 hashtbl[h / 8] |= 1 << (h % 8);
766         }
767         if_maddr_runlock(ifp);
768
769         axge_write_mem(sc, AXGE_ACCESS_MAC, 8, AXGE_MFA, (void *)&hashtbl, 8);
770         axge_write_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_RCR, rxmode);
771 }
772
773 static void
774 axge_setpromisc(struct usb_ether *ue)
775 {
776         struct axge_softc *sc;
777         struct ifnet *ifp;
778         uint16_t rxmode;
779
780         sc = uether_getsc(ue);
781         ifp = uether_getifp(ue);
782         rxmode = axge_read_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_RCR);
783
784         if (ifp->if_flags & IFF_PROMISC)
785                 rxmode |= RCR_PRO;
786         else
787                 rxmode &= ~RCR_PRO;
788
789         axge_write_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_RCR, rxmode);
790         axge_setmulti(ue);
791 }
792
793 static void
794 axge_start(struct usb_ether *ue)
795 {
796         struct axge_softc *sc;
797
798         sc = uether_getsc(ue);
799         /*
800          * Start the USB transfers, if not already started.
801          */
802         usbd_transfer_start(sc->sc_xfer[AXGE_BULK_DT_RD]);
803         usbd_transfer_start(sc->sc_xfer[AXGE_BULK_DT_WR]);
804 }
805
806 static void
807 axge_init(struct usb_ether *ue)
808 {
809         struct axge_softc *sc;
810         struct ifnet *ifp;
811         uint16_t rxmode;
812
813         sc = uether_getsc(ue);
814         ifp = uether_getifp(ue);
815         AXGE_LOCK_ASSERT(sc, MA_OWNED);
816
817         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
818                 return;
819
820         /*
821          * Cancel pending I/O and free all RX/TX buffers.
822          */
823         axge_stop(ue);
824
825         axge_reset(sc);
826
827         /* Set MAC address. */
828         axge_write_mem(sc, AXGE_ACCESS_MAC, ETHER_ADDR_LEN, AXGE_NIDR,
829             IF_LLADDR(ifp), ETHER_ADDR_LEN);
830
831         axge_write_cmd_1(sc, AXGE_ACCESS_MAC, AXGE_PWLLR, 0x34);
832         axge_write_cmd_1(sc, AXGE_ACCESS_MAC, AXGE_PWLHR, 0x52);
833
834         /* Configure TX/RX checksum offloading. */
835         axge_csum_cfg(ue);
836
837         /* Configure RX settings. */
838         rxmode = (RCR_AM | RCR_SO | RCR_DROP_CRCE);
839         if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
840                 rxmode |= RCR_IPE;
841
842         /* If we want promiscuous mode, set the allframes bit. */
843         if (ifp->if_flags & IFF_PROMISC)
844                 rxmode |= RCR_PRO;
845
846         if (ifp->if_flags & IFF_BROADCAST)
847                 rxmode |= RCR_AB;
848
849         axge_write_cmd_2(sc, AXGE_ACCESS_MAC, 2, AXGE_RCR, rxmode);
850
851         axge_write_cmd_1(sc, AXGE_ACCESS_MAC, AXGE_MMSR, 
852             MMSR_PME_TYPE | MMSR_PME_POL | MMSR_RWMP);
853
854         /* Load the multicast filter. */
855         axge_setmulti(ue);
856
857         usbd_xfer_set_stall(sc->sc_xfer[AXGE_BULK_DT_WR]);
858
859         ifp->if_drv_flags |= IFF_DRV_RUNNING;
860         /* Switch to selected media. */
861         axge_ifmedia_upd(ifp);
862 }
863
864 static void
865 axge_stop(struct usb_ether *ue)
866 {
867         struct axge_softc *sc;
868         struct ifnet *ifp;
869
870         sc = uether_getsc(ue);
871         ifp = uether_getifp(ue);
872
873         AXGE_LOCK_ASSERT(sc, MA_OWNED);
874
875         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
876         sc->sc_flags &= ~AXGE_FLAG_LINK;
877
878         /*
879          * Stop all the transfers, if not already stopped:
880          */
881         usbd_transfer_stop(sc->sc_xfer[AXGE_BULK_DT_WR]);
882         usbd_transfer_stop(sc->sc_xfer[AXGE_BULK_DT_RD]);
883 }
884
885 static int
886 axge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
887 {
888         struct usb_ether *ue;
889         struct axge_softc *sc;
890         struct ifreq *ifr;
891         int error, mask, reinit;
892
893         ue = ifp->if_softc;
894         sc = uether_getsc(ue);
895         ifr = (struct ifreq *)data;
896         error = 0;
897         reinit = 0;
898         if (cmd == SIOCSIFCAP) {
899                 AXGE_LOCK(sc);
900                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
901                 if ((mask & IFCAP_TXCSUM) != 0 &&
902                     (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
903                         ifp->if_capenable ^= IFCAP_TXCSUM;
904                         if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
905                                 ifp->if_hwassist |= AXGE_CSUM_FEATURES;
906                         else
907                                 ifp->if_hwassist &= ~AXGE_CSUM_FEATURES;
908                         reinit++;
909                 }
910                 if ((mask & IFCAP_RXCSUM) != 0 &&
911                     (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
912                         ifp->if_capenable ^= IFCAP_RXCSUM;
913                         reinit++;
914                 }
915                 if (reinit > 0 && ifp->if_drv_flags & IFF_DRV_RUNNING)
916                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
917                 else
918                         reinit = 0;
919                 AXGE_UNLOCK(sc);
920                 if (reinit > 0)
921                         uether_init(ue);
922         } else
923                 error = uether_ioctl(ifp, cmd, data);
924
925         return (error);
926 }
927
928 static void
929 axge_rx_frame(struct usb_ether *ue, struct usb_page_cache *pc, int actlen)
930 {
931         uint32_t pos;
932         uint32_t pkt_cnt;
933         uint32_t rxhdr;
934         uint32_t pkt_hdr;
935         uint32_t hdr_off;
936         uint32_t pktlen; 
937
938         /* verify we have enough data */
939         if (actlen < (int)sizeof(rxhdr))
940                 return;
941
942         pos = 0;
943
944         usbd_copy_out(pc, actlen - sizeof(rxhdr), &rxhdr, sizeof(rxhdr));
945         rxhdr = le32toh(rxhdr);
946
947         pkt_cnt = (uint16_t)rxhdr;
948         hdr_off = (uint16_t)(rxhdr >> 16);
949
950         while (pkt_cnt--) {
951                 /* verify the header offset */
952                 if ((int)(hdr_off + sizeof(pkt_hdr)) > actlen) {
953                         DPRINTF("End of packet headers\n");
954                         break;
955                 }
956                 if ((int)pos >= actlen) {
957                         DPRINTF("Data position reached end\n");
958                         break;
959                 }
960                 usbd_copy_out(pc, hdr_off, &pkt_hdr, sizeof(pkt_hdr));
961
962                 pkt_hdr = le32toh(pkt_hdr);
963                 pktlen = (pkt_hdr >> 16) & 0x1fff;
964                 if (pkt_hdr & (AXGE_RXHDR_CRC_ERR | AXGE_RXHDR_DROP_ERR)) {
965                         DPRINTF("Dropped a packet\n");
966                         if_inc_counter(ue->ue_ifp, IFCOUNTER_IERRORS, 1);
967                 }
968                 if (pktlen >= 6 && (int)(pos + pktlen) <= actlen) {
969                         axge_rxeof(ue, pc, pos + 2, pktlen - 6, pkt_hdr);
970                 } else {
971                         DPRINTF("Invalid packet pos=%d len=%d\n",
972                             (int)pos, (int)pktlen);
973                 }
974                 pos += (pktlen + 7) & ~7;
975                 hdr_off += sizeof(pkt_hdr);
976         }
977 }
978
979 static void
980 axge_rxeof(struct usb_ether *ue, struct usb_page_cache *pc,
981     unsigned int offset, unsigned int len, uint32_t pkt_hdr)
982 {
983         struct ifnet *ifp;
984         struct mbuf *m;
985
986         ifp = ue->ue_ifp;
987         if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) {
988                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
989                 return;
990         }
991
992         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
993         if (m == NULL) {
994                 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
995                 return;
996         }
997         m->m_pkthdr.rcvif = ifp;
998         m->m_len = m->m_pkthdr.len = len + ETHER_ALIGN;
999         m_adj(m, ETHER_ALIGN);
1000
1001         usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
1002
1003         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1004
1005         if ((pkt_hdr & (AXGE_RXHDR_L4CSUM_ERR | AXGE_RXHDR_L3CSUM_ERR)) == 0) {
1006                 if ((pkt_hdr & AXGE_RXHDR_L4_TYPE_MASK) ==
1007                     AXGE_RXHDR_L4_TYPE_TCP ||
1008                     (pkt_hdr & AXGE_RXHDR_L4_TYPE_MASK) ==
1009                     AXGE_RXHDR_L4_TYPE_UDP) {
1010                         m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1011                             CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
1012                         m->m_pkthdr.csum_data = 0xffff;
1013                 }
1014         }
1015
1016         _IF_ENQUEUE(&ue->ue_rxq, m);
1017 }
1018
1019 static void
1020 axge_csum_cfg(struct usb_ether *ue)
1021 {
1022         struct axge_softc *sc;
1023         struct ifnet *ifp;
1024         uint8_t csum;
1025
1026         sc = uether_getsc(ue);
1027         AXGE_LOCK_ASSERT(sc, MA_OWNED);
1028         ifp = uether_getifp(ue);
1029
1030         csum = 0;
1031         if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
1032                 csum |= CTCR_IP | CTCR_TCP | CTCR_UDP;
1033         axge_write_cmd_1(sc, AXGE_ACCESS_MAC, AXGE_CTCR, csum);
1034
1035         csum = 0;
1036         if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1037                 csum |= CRCR_IP | CRCR_TCP | CRCR_UDP;
1038         axge_write_cmd_1(sc, AXGE_ACCESS_MAC, AXGE_CRCR, csum);
1039 }