Merge branch 'vendor/HOSTAPD'
[dragonfly.git] / sys / bus / u4b / net / if_axe.c
1 /*-
2  * Copyright (c) 1997, 1998, 1999, 2000-2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*
34  * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
35  * Used in the LinkSys USB200M and various other adapters.
36  *
37  * Manuals available from:
38  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
39  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
40  * controller) to find the definitions for the RX control register.
41  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
42  *
43  * Written by Bill Paul <wpaul@windriver.com>
44  * Senior Engineer
45  * Wind River Systems
46  */
47
48 /*
49  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
50  * It uses an external PHY (reference designs use a RealTek chip),
51  * and has a 64-bit multicast hash filter. There is some information
52  * missing from the manual which one needs to know in order to make
53  * the chip function:
54  *
55  * - You must set bit 7 in the RX control register, otherwise the
56  *   chip won't receive any packets.
57  * - You must initialize all 3 IPG registers, or you won't be able
58  *   to send any packets.
59  *
60  * Note that this device appears to only support loading the station
61  * address via autload from the EEPROM (i.e. there's no way to manaully
62  * set it).
63  *
64  * (Adam Weinberger wanted me to name this driver if_gir.c.)
65  */
66
67 /*
68  * Ax88178 and Ax88772 support backported from the OpenBSD driver.
69  * 2007/02/12, J.R. Oldroyd, fbsd@opal.com
70  *
71  * Manual here:
72  * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
73  * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
74  */
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/bus.h>
79 #include <sys/condvar.h>
80 #include <sys/endian.h>
81 #include <sys/kernel.h>
82 #include <sys/lock.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/module.h>
86 #include <sys/socket.h>
87 #include <sys/sockio.h>
88 #include <sys/sysctl.h>
89
90 #include <net/if.h>
91 #include <net/ethernet.h>
92 #include <net/if_types.h>
93 #include <net/if_media.h>
94 #include <net/vlan/if_vlan_var.h>
95 #include <net/ifq_var.h>
96
97 #include <dev/netif/mii_layer/mii.h>
98 #include <dev/netif/mii_layer/miivar.h>
99
100 #include <bus/u4b/usb.h>
101 #include <bus/u4b/usbdi.h>
102 #include <bus/u4b/usbdi_util.h>
103 #include "usbdevs.h"
104
105 #define USB_DEBUG_VAR axe_debug
106 #include <bus/u4b/usb_debug.h>
107 #include <bus/u4b/usb_process.h>
108
109 #include <bus/u4b/net/usb_ethernet.h>
110 #include <bus/u4b/net/if_axereg.h>
111
112 /*
113  * AXE_178_MAX_FRAME_BURST
114  * max frame burst size for Ax88178 and Ax88772
115  *      0       2048 bytes
116  *      1       4096 bytes
117  *      2       8192 bytes
118  *      3       16384 bytes
119  * use the largest your system can handle without USB stalling.
120  *
121  * NB: 88772 parts appear to generate lots of input errors with
122  * a 2K rx buffer and 8K is only slightly faster than 4K on an
123  * EHCI port on a T42 so change at your own risk.
124  */
125 #define AXE_178_MAX_FRAME_BURST 1
126
127 #define AXE_CSUM_FEATURES       (CSUM_IP | CSUM_TCP | CSUM_UDP)
128
129 #ifdef USB_DEBUG
130 static int axe_debug = 0;
131
132 static SYSCTL_NODE(_hw_usb, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe");
133 SYSCTL_INT(_hw_usb_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0,
134     "Debug level");
135 #endif
136
137 /*
138  * Various supported device vendors/products.
139  */
140 static const STRUCT_USB_HOST_ID axe_devs[] = {
141 #define AXE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
142         AXE_DEV(ABOCOM, UF200, 0),
143         AXE_DEV(ACERCM, EP1427X2, 0),
144         AXE_DEV(APPLE, ETHERNET, AXE_FLAG_772),
145         AXE_DEV(ASIX, AX88172, 0),
146         AXE_DEV(ASIX, AX88178, AXE_FLAG_178),
147         AXE_DEV(ASIX, AX88772, AXE_FLAG_772),
148         AXE_DEV(ASIX, AX88772A, AXE_FLAG_772A),
149         AXE_DEV(ASIX, AX88772B, AXE_FLAG_772B),
150         AXE_DEV(ASIX, AX88772B_1, AXE_FLAG_772B),
151         AXE_DEV(ATEN, UC210T, 0),
152         AXE_DEV(BELKIN, F5D5055, AXE_FLAG_178),
153         AXE_DEV(BILLIONTON, USB2AR, 0),
154         AXE_DEV(CISCOLINKSYS, USB200MV2, AXE_FLAG_772A),
155         AXE_DEV(COREGA, FETHER_USB2_TX, 0),
156         AXE_DEV(DLINK, DUBE100, 0),
157         AXE_DEV(DLINK, DUBE100B1, AXE_FLAG_772),
158         AXE_DEV(DLINK, DUBE100C1, AXE_FLAG_772B),
159         AXE_DEV(GOODWAY, GWUSB2E, 0),
160         AXE_DEV(IODATA, ETGUS2, AXE_FLAG_178),
161         AXE_DEV(JVC, MP_PRX1, 0),
162         AXE_DEV(LENOVO, ETHERNET, AXE_FLAG_772B),
163         AXE_DEV(LINKSYS2, USB200M, 0),
164         AXE_DEV(LINKSYS4, USB1000, AXE_FLAG_178),
165         AXE_DEV(LOGITEC, LAN_GTJU2A, AXE_FLAG_178),
166         AXE_DEV(MELCO, LUAU2KTX, 0),
167         AXE_DEV(MELCO, LUA3U2AGT, AXE_FLAG_178),
168         AXE_DEV(NETGEAR, FA120, 0),
169         AXE_DEV(OQO, ETHER01PLUS, AXE_FLAG_772),
170         AXE_DEV(PLANEX3, GU1000T, AXE_FLAG_178),
171         AXE_DEV(SITECOM, LN029, 0),
172         AXE_DEV(SITECOMEU, LN028, AXE_FLAG_178),
173         AXE_DEV(SYSTEMTALKS, SGCX2UL, 0),
174 #undef AXE_DEV
175 };
176
177 static device_probe_t axe_probe;
178 static device_attach_t axe_attach;
179 static device_detach_t axe_detach;
180
181 static usb_callback_t axe_bulk_read_callback;
182 static usb_callback_t axe_bulk_write_callback;
183
184 static miibus_readreg_t axe_miibus_readreg;
185 static miibus_writereg_t axe_miibus_writereg;
186 static miibus_statchg_t axe_miibus_statchg;
187
188 /*
189 static int axe_miibus_readreg(device_t dev, int phy, int reg);
190 static int axe_miibus_writereg(device_t dev, int phy, int reg, int val);
191 static void axe_miibus_statchg(device_t dev);
192 */
193 static uether_fn_t axe_attach_post;
194 static uether_fn_t axe_init;
195 static uether_fn_t axe_stop;
196 static uether_fn_t axe_start;
197 static uether_fn_t axe_tick;
198 static uether_fn_t axe_setmulti;
199 static uether_fn_t axe_setpromisc;
200
201 static int      axe_attach_post_sub(struct usb_ether *);
202 static int      axe_ifmedia_upd(struct ifnet *);
203 static void     axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
204 static int      axe_cmd(struct axe_softc *, int, int, int, void *);
205 static void     axe_ax88178_init(struct axe_softc *);
206 static void     axe_ax88772_init(struct axe_softc *);
207 static void     axe_ax88772_phywake(struct axe_softc *);
208 static void     axe_ax88772a_init(struct axe_softc *);
209 static void     axe_ax88772b_init(struct axe_softc *);
210 static int      axe_get_phyno(struct axe_softc *, int);
211 static int      axe_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
212 static int      axe_rx_frame(struct usb_ether *, struct usb_page_cache *, int);
213 static int      axe_rxeof(struct usb_ether *, struct usb_page_cache *,
214                     unsigned int offset, unsigned int, struct axe_csum_hdr *);
215 static void     axe_csum_cfg(struct usb_ether *);
216
217 static const struct usb_config axe_config[AXE_N_TRANSFER] = {
218
219         [AXE_BULK_DT_WR] = {
220                 .type = UE_BULK,
221                 .endpoint = UE_ADDR_ANY,
222                 .direction = UE_DIR_OUT,
223                 .frames = 16,
224                 .bufsize = 16 * MCLBYTES,
225                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
226                 .callback = axe_bulk_write_callback,
227                 .timeout = 10000,       /* 10 seconds */
228         },
229
230         [AXE_BULK_DT_RD] = {
231                 .type = UE_BULK,
232                 .endpoint = UE_ADDR_ANY,
233                 .direction = UE_DIR_IN,
234                 .bufsize = 16384,       /* bytes */
235                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
236                 .callback = axe_bulk_read_callback,
237                 .timeout = 0,   /* no timeout */
238         },
239 };
240
241 static const struct ax88772b_mfb ax88772b_mfb_table[] = {
242         { 0x8000, 0x8001, 2048 },
243         { 0x8100, 0x8147, 4096},
244         { 0x8200, 0x81EB, 6144},
245         { 0x8300, 0x83D7, 8192},
246         { 0x8400, 0x851E, 16384},
247         { 0x8500, 0x8666, 20480},
248         { 0x8600, 0x87AE, 24576},
249         { 0x8700, 0x8A3D, 32768}
250 };
251
252 static device_method_t axe_methods[] = {
253         /* Device interface */
254         DEVMETHOD(device_probe, axe_probe),
255         DEVMETHOD(device_attach, axe_attach),
256         DEVMETHOD(device_detach, axe_detach),
257
258         /* MII interface */
259         DEVMETHOD(miibus_readreg, axe_miibus_readreg),
260         DEVMETHOD(miibus_writereg, axe_miibus_writereg),
261         DEVMETHOD(miibus_statchg, axe_miibus_statchg),
262
263         DEVMETHOD_END
264 };
265
266 static driver_t axe_driver = {
267         .name = "axe",
268         .methods = axe_methods,
269         .size = sizeof(struct axe_softc),
270 };
271
272 static devclass_t axe_devclass;
273
274 DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, NULL, NULL);
275 DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, NULL, NULL);
276 MODULE_DEPEND(axe, uether, 1, 1, 1);
277 MODULE_DEPEND(axe, usb, 1, 1, 1);
278 MODULE_DEPEND(axe, ether, 1, 1, 1);
279 MODULE_DEPEND(axe, miibus, 1, 1, 1);
280 MODULE_VERSION(axe, 1);
281
282 static const struct usb_ether_methods axe_ue_methods = {
283         .ue_attach_post = axe_attach_post,
284         .ue_attach_post_sub = axe_attach_post_sub,
285         .ue_start = axe_start,
286         .ue_init = axe_init,
287         .ue_stop = axe_stop,
288         .ue_tick = axe_tick,
289         .ue_setmulti = axe_setmulti,
290         .ue_setpromisc = axe_setpromisc,
291         .ue_mii_upd = axe_ifmedia_upd,
292         .ue_mii_sts = axe_ifmedia_sts,
293 };
294
295 static int
296 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
297 {
298         struct usb_device_request req;
299         usb_error_t err;
300
301         AXE_LOCK_ASSERT(sc);
302
303         req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
304             UT_WRITE_VENDOR_DEVICE :
305             UT_READ_VENDOR_DEVICE);
306         req.bRequest = AXE_CMD_CMD(cmd);
307         USETW(req.wValue, val);
308         USETW(req.wIndex, index);
309         USETW(req.wLength, AXE_CMD_LEN(cmd));
310
311         err = uether_do_request(&sc->sc_ue, &req, buf, 1000);
312
313         return (err);
314 }
315
316 static int
317 axe_miibus_readreg(device_t dev, int phy, int reg)
318 {
319         struct axe_softc *sc = device_get_softc(dev);
320         uint16_t val;
321         int locked;
322
323         locked = lockowned(&sc->sc_lock);
324         if(!locked)
325                 AXE_LOCK(sc);
326         
327         if(phy != sc->sc_phyno){
328                 if(!locked)
329                         AXE_UNLOCK(sc);
330                 return(0);
331         }
332
333         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
334         axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
335         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
336
337         val = le16toh(val);
338         if (AXE_IS_772(sc) && reg == MII_BMSR) {
339                 /*
340                  * BMSR of AX88772 indicates that it supports extended
341                  * capability but the extended status register is
342                  * revered for embedded ethernet PHY. So clear the
343                  * extended capability bit of BMSR.
344                  */
345                 val &= ~BMSR_EXTCAP;
346         }
347
348         if (!locked)
349                 AXE_UNLOCK(sc);
350         return (val);
351 }
352
353 static int
354 axe_miibus_writereg(device_t dev, int phy, int reg, int val)
355 {
356         struct axe_softc *sc = device_get_softc(dev);
357         int locked;
358
359         val = htole32(val);
360         locked = lockowned(&sc->sc_lock);
361         if (!locked)
362                 AXE_LOCK(sc);
363
364         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
365         axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
366         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
367
368         if (!locked)
369                 AXE_UNLOCK(sc);
370         return (0);
371 }
372
373 static void
374 axe_miibus_statchg(device_t dev)
375 {
376         struct axe_softc *sc = device_get_softc(dev);
377         struct mii_data *mii = GET_MII(sc);
378         struct ifnet *ifp;
379         uint16_t val;
380         int err, locked;
381
382         locked = lockowned(&sc->sc_lock);
383         if (!locked)
384                 AXE_LOCK(sc);
385
386         ifp = uether_getifp(&sc->sc_ue);
387         if (mii == NULL || ifp == NULL ||
388             (ifp->if_flags & IFF_RUNNING) == 0)
389                 goto done;
390
391         sc->sc_flags &= ~AXE_FLAG_LINK;
392         if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
393             (IFM_ACTIVE | IFM_AVALID)) {
394                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
395                 case IFM_10_T:
396                 case IFM_100_TX:
397                         sc->sc_flags |= AXE_FLAG_LINK;
398                         break;
399                 case IFM_1000_T:
400                         if ((sc->sc_flags & AXE_FLAG_178) == 0)
401                                 break;
402                         sc->sc_flags |= AXE_FLAG_LINK;
403                         DPRINTFN(11, "miibus_statchg: link should be up\n");
404                         break;
405                 default:
406                         break;
407                 }
408         } else {
409                 DPRINTFN(11, "miibus_statchg: not active or not valid: %x\n", mii->mii_media_status);
410         }
411
412         /* Lost link, do nothing. */
413         if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
414                 goto done;
415         }
416
417         val = 0;
418         if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
419                 val |= AXE_MEDIA_FULL_DUPLEX;
420                 if (AXE_IS_178_FAMILY(sc)) {
421                         if ((IFM_OPTIONS(mii->mii_media_active) &
422                             IFM_ETH_TXPAUSE) != 0)
423                                 val |= AXE_178_MEDIA_TXFLOW_CONTROL_EN;
424                         if ((IFM_OPTIONS(mii->mii_media_active) &
425                             IFM_ETH_RXPAUSE) != 0)
426                                 val |= AXE_178_MEDIA_RXFLOW_CONTROL_EN;
427                 }
428         }
429         if (AXE_IS_178_FAMILY(sc)) {
430                 val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
431                 if ((sc->sc_flags & AXE_FLAG_178) != 0)
432                         val |= AXE_178_MEDIA_ENCK;
433                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
434                 case IFM_1000_T:
435                         val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
436                         break;
437                 case IFM_100_TX:
438                         val |= AXE_178_MEDIA_100TX;
439                         break;
440                 case IFM_10_T:
441                         /* doesn't need to be handled */
442                         break;
443                 }
444         }
445         err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
446         if (err)
447                 device_printf(dev, "media change failed, error %d\n", err);
448 done:
449         if (!locked)
450                 AXE_UNLOCK(sc);
451 }
452
453 /*
454  * Set media options.
455  */
456 static int
457 axe_ifmedia_upd(struct ifnet *ifp)
458 {
459         struct axe_softc *sc = ifp->if_softc;
460         struct mii_data *mii = GET_MII(sc);
461         struct mii_softc *miisc;
462         int error;
463
464         AXE_LOCK_ASSERT(sc);
465
466         LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
467                 mii_phy_reset(miisc);
468         error = mii_mediachg(mii);
469         return (error);
470 }
471
472 /*
473  * Report current media status.
474  */
475 static void
476 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
477 {
478         struct axe_softc *sc = ifp->if_softc;
479         struct mii_data *mii = GET_MII(sc);
480
481         AXE_LOCK(sc);
482         mii_pollstat(mii);
483         ifmr->ifm_active = mii->mii_media_active;
484         ifmr->ifm_status = mii->mii_media_status;
485         AXE_UNLOCK(sc);
486 }
487
488 static void
489 axe_setmulti(struct usb_ether *ue)
490 {
491         struct axe_softc *sc = uether_getsc(ue);
492         struct ifnet *ifp = uether_getifp(ue);
493         struct ifmultiaddr *ifma;
494         uint32_t h = 0;
495         uint16_t rxmode;
496         uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
497
498         AXE_LOCK_ASSERT(sc);
499
500         axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
501         rxmode = le16toh(rxmode);
502
503         if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
504                 rxmode |= AXE_RXCMD_ALLMULTI;
505                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
506                 return;
507         }
508         rxmode &= ~AXE_RXCMD_ALLMULTI;
509
510         /* if_maddr_rlock(ifp); */
511         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
512         {
513                 if (ifma->ifma_addr->sa_family != AF_LINK)
514                         continue;
515                 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
516                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
517                 hashtbl[h / 8] |= 1 << (h % 8);
518         }
519 /*      if_maddr_runlock(ifp); */
520
521         axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
522         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
523 }
524
525 static int
526 axe_get_phyno(struct axe_softc *sc, int sel)
527 {
528         int phyno;
529
530         switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
531         case PHY_TYPE_100_HOME:
532         case PHY_TYPE_GIG:
533                 phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
534                 break;
535         case PHY_TYPE_SPECIAL:
536                 /* FALLTHROUGH */
537         case PHY_TYPE_RSVD:
538                 /* FALLTHROUGH */
539         case PHY_TYPE_NON_SUP:
540                 /* FALLTHROUGH */
541         default:
542                 phyno = -1;
543                 break;
544         }
545
546         return (phyno);
547 }
548
549 #define AXE_GPIO_WRITE(x, y)    do {                            \
550         axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL);          \
551         uether_pause(ue, (y));                                  \
552 } while (0)
553
554 static void
555 axe_ax88178_init(struct axe_softc *sc)
556 {
557         struct usb_ether *ue;
558         int gpio0, ledmode, phymode;
559         uint16_t eeprom, val;
560
561         ue = &sc->sc_ue;
562         axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
563         /* XXX magic */
564         axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
565         eeprom = le16toh(eeprom);
566         axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
567
568         /* if EEPROM is invalid we have to use to GPIO0 */
569         if (eeprom == 0xffff) {
570                 phymode = AXE_PHY_MODE_MARVELL;
571                 gpio0 = 1;
572                 ledmode = 0;
573         } else {
574                 phymode = eeprom & 0x7f;
575                 gpio0 = (eeprom & 0x80) ? 0 : 1;
576                 ledmode = eeprom >> 8;
577         }
578
579         if (bootverbose)
580                 device_printf(sc->sc_ue.ue_dev,
581                     "EEPROM data : 0x%04x, phymode : 0x%02x\n", eeprom,
582                     phymode);
583         /* Program GPIOs depending on PHY hardware. */
584         switch (phymode) {
585         case AXE_PHY_MODE_MARVELL:
586                 if (gpio0 == 1) {
587                         AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
588                             hz / 32);
589                         AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
590                             hz / 32);
591                         AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
592                         AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
593                             hz / 32);
594                 } else {
595                         AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
596                             AXE_GPIO1_EN, hz / 3);
597                         if (ledmode == 1) {
598                                 AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
599                                 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
600                                     hz / 3);
601                         } else {
602                                 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
603                                     AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
604                                 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
605                                     AXE_GPIO2_EN, hz / 4);
606                                 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
607                                     AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
608                         }
609                 }
610                 break;
611         case AXE_PHY_MODE_CICADA:
612         case AXE_PHY_MODE_CICADA_V2:
613         case AXE_PHY_MODE_CICADA_V2_ASIX:
614                 if (gpio0 == 1)
615                         AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
616                             AXE_GPIO0_EN, hz / 32);
617                 else
618                         AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
619                             AXE_GPIO1_EN, hz / 32);
620                 break;
621         case AXE_PHY_MODE_AGERE:
622                 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
623                     AXE_GPIO1_EN, hz / 32);
624                 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
625                     AXE_GPIO2_EN, hz / 32);
626                 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
627                 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
628                     AXE_GPIO2_EN, hz / 32);
629                 break;
630         case AXE_PHY_MODE_REALTEK_8211CL:
631         case AXE_PHY_MODE_REALTEK_8211BN:
632         case AXE_PHY_MODE_REALTEK_8251CL:
633                 val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
634                     AXE_GPIO1 | AXE_GPIO1_EN;
635                 AXE_GPIO_WRITE(val, hz / 32);
636                 AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
637                 AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
638                 AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
639                 if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
640                         axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
641                             0x1F, 0x0005);
642                         axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
643                             0x0C, 0x0000);
644                         val = axe_miibus_readreg(ue->ue_dev, sc->sc_phyno,
645                             0x0001);
646                         axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
647                             0x01, val | 0x0080);
648                         axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
649                             0x1F, 0x0000);
650                 }
651                 break;
652         default:
653                 /* Unknown PHY model or no need to program GPIOs. */
654                 break;
655         }
656
657         /* soft reset */
658         axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
659         uether_pause(ue, hz / 4);
660
661         axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
662             AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
663         uether_pause(ue, hz / 4);
664         /* Enable MII/GMII/RGMII interface to work with external PHY. */
665         axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
666         uether_pause(ue, hz / 4);
667
668         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
669 }
670
671 static void
672 axe_ax88772_init(struct axe_softc *sc)
673 {
674         axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
675         uether_pause(&sc->sc_ue, hz / 16);
676
677         if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
678                 /* ask for the embedded PHY */
679                 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
680                 uether_pause(&sc->sc_ue, hz / 64);
681
682                 /* power down and reset state, pin reset state */
683                 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
684                     AXE_SW_RESET_CLEAR, NULL);
685                 uether_pause(&sc->sc_ue, hz / 16);
686
687                 /* power down/reset state, pin operating state */
688                 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
689                     AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
690                 uether_pause(&sc->sc_ue, hz / 4);
691
692                 /* power up, reset */
693                 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
694
695                 /* power up, operating */
696                 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
697                     AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
698         } else {
699                 /* ask for external PHY */
700                 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
701                 uether_pause(&sc->sc_ue, hz / 64);
702
703                 /* power down internal PHY */
704                 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
705                     AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
706         }
707
708         uether_pause(&sc->sc_ue, hz / 4);
709         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
710 }
711
712 static void
713 axe_ax88772_phywake(struct axe_softc *sc)
714 {
715         struct usb_ether *ue;
716
717         ue = &sc->sc_ue;
718         if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
719                 /* Manually select internal(embedded) PHY - MAC mode. */
720                 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
721                     AXE_SW_PHY_SELECT_EMBEDDED | AXE_SW_PHY_SELECT_SS_MII,
722                     NULL);
723                 uether_pause(&sc->sc_ue, hz / 32);
724         } else {
725                 /*
726                  * Manually select external PHY - MAC mode.
727                  * Reverse MII/RMII is for AX88772A PHY mode.
728                  */
729                 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
730                     AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
731                 uether_pause(&sc->sc_ue, hz / 32);
732         }
733         /* Take PHY out of power down. */
734         axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
735             AXE_SW_RESET_IPRL, NULL);
736         uether_pause(&sc->sc_ue, hz / 4);
737         axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
738         uether_pause(&sc->sc_ue, hz);
739         axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
740         uether_pause(&sc->sc_ue, hz / 32);
741         axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
742         uether_pause(&sc->sc_ue, hz / 32);
743 }
744
745 static void
746 axe_ax88772a_init(struct axe_softc *sc)
747 {
748         struct usb_ether *ue;
749
750         ue = &sc->sc_ue;
751         /* Reload EEPROM. */
752         AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
753         axe_ax88772_phywake(sc);
754         /* Stop MAC. */
755         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
756 }
757
758 static void
759 axe_ax88772b_init(struct axe_softc *sc)
760 {
761         struct usb_ether *ue;
762         uint16_t eeprom;
763         uint8_t *eaddr;
764         int i;
765
766         ue = &sc->sc_ue;
767         /* Reload EEPROM. */
768         AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
769         /*
770          * Save PHY power saving configuration(high byte) and
771          * clear EEPROM checksum value(low byte).
772          */
773         axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG, &eeprom);
774         sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00;
775
776         /*
777          * Auto-loaded default station address from internal ROM is
778          * 00:00:00:00:00:00 such that an explicit access to EEPROM
779          * is required to get real station address.
780          */
781         eaddr = ue->ue_eaddr;
782         for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
783                 axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_NODE_ID + i,
784                     &eeprom);
785                 eeprom = le16toh(eeprom);
786                 *eaddr++ = (uint8_t)(eeprom & 0xFF);
787                 *eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF);
788         }
789         /* Wakeup PHY. */
790         axe_ax88772_phywake(sc);
791         /* Stop MAC. */
792         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
793 }
794
795 #undef  AXE_GPIO_WRITE
796
797 static void
798 axe_reset(struct axe_softc *sc)
799 {
800         struct usb_config_descriptor *cd;
801         usb_error_t err;
802
803         cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
804
805         err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_lock,
806             cd->bConfigurationValue);
807         if (err)
808                 DPRINTF("reset failed (ignored)\n");
809
810         /* Wait a little while for the chip to get its brains in order. */
811         uether_pause(&sc->sc_ue, hz / 100);
812
813         /* Reinitialize controller to achieve full reset. */
814         if (sc->sc_flags & AXE_FLAG_178)
815                 axe_ax88178_init(sc);
816         else if (sc->sc_flags & AXE_FLAG_772)
817                 axe_ax88772_init(sc);
818         else if (sc->sc_flags & AXE_FLAG_772A)
819                 axe_ax88772a_init(sc);
820         else if (sc->sc_flags & AXE_FLAG_772B)
821                 axe_ax88772b_init(sc);
822 }
823
824 static void
825 axe_attach_post(struct usb_ether *ue)
826 {
827         struct axe_softc *sc = uether_getsc(ue);
828
829         /*
830          * Load PHY indexes first. Needed by axe_xxx_init().
831          */
832         axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
833         if (bootverbose)
834                 device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n",
835                     sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]);
836         sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
837         if (sc->sc_phyno == -1)
838                 sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
839         if (sc->sc_phyno == -1) {
840                 device_printf(sc->sc_ue.ue_dev,
841                     "no valid PHY address found, assuming PHY address 0\n");
842                 sc->sc_phyno = 0;
843         }
844
845         /* Initialize controller and get station address. */
846         if (sc->sc_flags & AXE_FLAG_178) {
847                 axe_ax88178_init(sc);
848                 axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
849         } else if (sc->sc_flags & AXE_FLAG_772) {
850                 axe_ax88772_init(sc);
851                 axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
852         } else if (sc->sc_flags & AXE_FLAG_772A) {
853                 axe_ax88772a_init(sc);
854                 axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
855         } else if (sc->sc_flags & AXE_FLAG_772B) {
856                 axe_ax88772b_init(sc);
857         } else
858                 axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
859
860         /*
861          * Fetch IPG values.
862          */
863         if (sc->sc_flags & (AXE_FLAG_772A | AXE_FLAG_772B)) {
864                 /* Set IPG values. */
865                 sc->sc_ipgs[0] = 0x15;
866                 sc->sc_ipgs[1] = 0x16;
867                 sc->sc_ipgs[2] = 0x1A;
868         } else {
869                 axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
870         }
871 }
872
873 static int
874 axe_attach_post_sub(struct usb_ether *ue)
875 {
876         struct axe_softc *sc;
877         struct ifnet *ifp;
878         u_int adv_pause;
879         int error;
880
881         sc = uether_getsc(ue);
882         ifp = ue->ue_ifp;
883         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
884         ifp->if_start = uether_start;
885         ifp->if_ioctl = axe_ioctl;
886         ifp->if_init = uether_init;
887         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
888         /* XXX
889         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
890         */
891         ifq_set_ready(&ifp->if_snd);
892
893         if (AXE_IS_178_FAMILY(sc))
894                 ifp->if_capabilities |= IFCAP_VLAN_MTU;
895         if (sc->sc_flags & AXE_FLAG_772B) {
896                 ifp->if_capabilities |= IFCAP_TXCSUM | IFCAP_RXCSUM;
897                 ifp->if_hwassist = AXE_CSUM_FEATURES;
898                 /*
899                  * Checksum offloading of AX88772B also works with VLAN
900                  * tagged frames but there is no way to take advantage
901                  * of the feature because vlan(4) assumes
902                  * IFCAP_VLAN_HWTAGGING is prerequisite condition to
903                  * support checksum offloading with VLAN. VLAN hardware
904                  * tagging support of AX88772B is very limited so it's
905                  * not possible to announce IFCAP_VLAN_HWTAGGING.
906                  */
907         }
908         ifp->if_capenable = ifp->if_capabilities;
909         if (sc->sc_flags & (AXE_FLAG_772A | AXE_FLAG_772B | AXE_FLAG_178))
910                 adv_pause = MIIF_DOPAUSE;
911         else
912                 adv_pause = 0;
913
914         error = mii_phy_probe(ue->ue_dev, &ue->ue_miibus, 
915                 uether_ifmedia_upd, ue->ue_methods->ue_mii_sts);
916         /* XXX
917         error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
918             uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
919             BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, adv_pause);
920         */
921         return (error);
922 }
923
924 /*
925  * Probe for a AX88172 chip.
926  */
927 static int
928 axe_probe(device_t dev)
929 {
930         struct usb_attach_arg *uaa = device_get_ivars(dev);
931
932         if (uaa->usb_mode != USB_MODE_HOST)
933                 return (ENXIO);
934         if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
935                 return (ENXIO);
936         if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
937                 return (ENXIO);
938
939         return (usbd_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
940 }
941
942 /*
943  * Attach the interface. Allocate softc structures, do ifmedia
944  * setup and ethernet/BPF attach.
945  */
946 static int
947 axe_attach(device_t dev)
948 {
949         struct usb_attach_arg *uaa = device_get_ivars(dev);
950         struct axe_softc *sc = device_get_softc(dev);
951         struct usb_ether *ue = &sc->sc_ue;
952         uint8_t iface_index;
953         int error;
954
955         sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
956
957         device_set_usb_desc(dev);
958
959         lockinit(&sc->sc_lock, device_get_nameunit(dev), 0, 0);
960
961         iface_index = AXE_IFACE_IDX;
962         error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
963             axe_config, AXE_N_TRANSFER, sc, &sc->sc_lock);
964         if (error) {
965                 device_printf(dev, "allocating USB transfers failed\n");
966                 goto detach;
967         }
968
969         ue->ue_sc = sc;
970         ue->ue_dev = dev;
971         ue->ue_udev = uaa->device;
972         ue->ue_lock = &sc->sc_lock;
973         ue->ue_methods = &axe_ue_methods;
974
975         error = uether_ifattach(ue);
976         if (error) {
977                 device_printf(dev, "could not attach interface\n");
978                 goto detach;
979         }
980         return (0);                     /* success */
981
982 detach:
983         axe_detach(dev);
984         return (ENXIO);                 /* failure */
985 }
986
987 static int
988 axe_detach(device_t dev)
989 {
990         struct axe_softc *sc = device_get_softc(dev);
991         struct usb_ether *ue = &sc->sc_ue;
992
993         usbd_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
994         uether_ifdetach(ue);
995         lockuninit(&sc->sc_lock);
996
997         return (0);
998 }
999
1000 #if (AXE_BULK_BUF_SIZE >= 0x10000)
1001 #error "Please update axe_bulk_read_callback()!"
1002 #endif
1003
1004 static void
1005 axe_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1006 {
1007         struct axe_softc *sc = usbd_xfer_softc(xfer);
1008         struct usb_ether *ue = &sc->sc_ue;
1009         struct usb_page_cache *pc;
1010         int actlen;
1011
1012         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1013
1014         switch (USB_GET_STATE(xfer)) {
1015         case USB_ST_TRANSFERRED:
1016                 pc = usbd_xfer_get_frame(xfer, 0);
1017                 axe_rx_frame(ue, pc, actlen);
1018
1019                 /* FALLTHROUGH */
1020         case USB_ST_SETUP:
1021 tr_setup:
1022                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1023                 usbd_transfer_submit(xfer);
1024                 uether_rxflush(ue);
1025                 return;
1026
1027         default:                        /* Error */
1028                 DPRINTF("bulk read error, %s\n", usbd_errstr(error));
1029
1030                 if (error != USB_ERR_CANCELLED) {
1031                         /* try to clear stall first */
1032                         usbd_xfer_set_stall(xfer);
1033                         goto tr_setup;
1034                 }
1035                 return;
1036
1037         }
1038 }
1039
1040 static int
1041 axe_rx_frame(struct usb_ether *ue, struct usb_page_cache *pc, int actlen)
1042 {
1043         struct axe_softc *sc;
1044         struct axe_sframe_hdr hdr;
1045         struct axe_csum_hdr csum_hdr;
1046         int error, len, pos;
1047
1048         sc = uether_getsc(ue);
1049         pos = 0;
1050         len = 0;
1051         error = 0;
1052         if ((sc->sc_flags & AXE_FLAG_STD_FRAME) != 0) {
1053                 while (pos < actlen) {
1054                         if ((int)(pos + sizeof(hdr)) > actlen) {
1055                                 /* too little data */
1056                                 error = EINVAL;
1057                                 break;
1058                         }
1059                         usbd_copy_out(pc, pos, &hdr, sizeof(hdr));
1060
1061                         if ((hdr.len ^ hdr.ilen) != sc->sc_lenmask) {
1062                                 /* we lost sync */
1063                                 error = EINVAL;
1064                                 break;
1065                         }
1066                         pos += sizeof(hdr);
1067                         len = le16toh(hdr.len);
1068                         if (pos + len > actlen) {
1069                                 /* invalid length */
1070                                 error = EINVAL;
1071                                 break;
1072                         }
1073                         axe_rxeof(ue, pc, pos, len, NULL);
1074                         pos += len + (len % 2);
1075                 }
1076         } else if ((sc->sc_flags & AXE_FLAG_CSUM_FRAME) != 0) {
1077                 while (pos < actlen) {
1078                         if ((int)(pos + sizeof(csum_hdr)) > actlen) {
1079                                 /* too little data */
1080                                 error = EINVAL;
1081                                 break;
1082                         }
1083                         usbd_copy_out(pc, pos, &csum_hdr, sizeof(csum_hdr));
1084
1085                         csum_hdr.len = le16toh(csum_hdr.len);
1086                         csum_hdr.ilen = le16toh(csum_hdr.ilen);
1087                         csum_hdr.cstatus = le16toh(csum_hdr.cstatus);
1088                         if ((AXE_CSUM_RXBYTES(csum_hdr.len) ^
1089                             AXE_CSUM_RXBYTES(csum_hdr.ilen)) !=
1090                             sc->sc_lenmask) {
1091                                 /* we lost sync */
1092                                 error = EINVAL;
1093                                 break;
1094                         }
1095                         /*
1096                          * Get total transferred frame length including
1097                          * checksum header.  The length should be multiple
1098                          * of 4.
1099                          */
1100                         len = sizeof(csum_hdr) + AXE_CSUM_RXBYTES(csum_hdr.len);
1101                         len = (len + 3) & ~3;
1102                         if (pos + len > actlen) {
1103                                 /* invalid length */
1104                                 error = EINVAL;
1105                                 break;
1106                         }
1107                         axe_rxeof(ue, pc, pos + sizeof(csum_hdr),
1108                             AXE_CSUM_RXBYTES(csum_hdr.len), &csum_hdr);
1109                         pos += len;
1110                 }
1111         } else
1112                 axe_rxeof(ue, pc, 0, actlen, NULL);
1113
1114         if (error != 0)
1115                 IFNET_STAT_INC(ue->ue_ifp, ierrors, 1);
1116         return (error);
1117 }
1118
1119 static int
1120 axe_rxeof(struct usb_ether *ue, struct usb_page_cache *pc, unsigned int offset,
1121     unsigned int len, struct axe_csum_hdr *csum_hdr)
1122 {
1123         struct ifnet *ifp = ue->ue_ifp;
1124         struct mbuf *m;
1125
1126         if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) {
1127                 IFNET_STAT_INC(ifp, ierrors, 1);
1128                 return (EINVAL);
1129         }
1130
1131         m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1132         if (m == NULL) {
1133                 IFNET_STAT_INC(ifp, iqdrops, 1);
1134                 return (ENOMEM);
1135         }
1136         m->m_len = m->m_pkthdr.len = MCLBYTES;
1137         m_adj(m, ETHER_ALIGN);
1138
1139         usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
1140
1141         IFNET_STAT_INC(ifp, ipackets, 1);
1142         m->m_pkthdr.rcvif = ifp;
1143         m->m_pkthdr.len = m->m_len = len;
1144
1145         if (csum_hdr != NULL && csum_hdr->cstatus & AXE_CSUM_HDR_L3_TYPE_IPV4) {
1146                 if ((csum_hdr->cstatus & (AXE_CSUM_HDR_L4_CSUM_ERR |
1147                     AXE_CSUM_HDR_L3_CSUM_ERR)) == 0) {
1148                         m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED |
1149                             CSUM_IP_VALID;
1150                         if ((csum_hdr->cstatus & AXE_CSUM_HDR_L4_TYPE_MASK) ==
1151                             AXE_CSUM_HDR_L4_TYPE_TCP ||
1152                             (csum_hdr->cstatus & AXE_CSUM_HDR_L4_TYPE_MASK) ==
1153                             AXE_CSUM_HDR_L4_TYPE_UDP) {
1154                                 m->m_pkthdr.csum_flags |=
1155                                     CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1156                                 m->m_pkthdr.csum_data = 0xffff;
1157                         }
1158                 }
1159         }
1160         
1161         IF_ENQUEUE(&ue->ue_rxq, m);
1162         return (0);
1163 }
1164
1165 #if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
1166 #error "Please update axe_bulk_write_callback()!"
1167 #endif
1168
1169 static void
1170 axe_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1171 {
1172         struct axe_softc *sc = usbd_xfer_softc(xfer);
1173         struct axe_sframe_hdr hdr;
1174         struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1175         struct usb_page_cache *pc;
1176         struct mbuf *m;
1177         int nframes, pos;
1178
1179         DPRINTFN(11, "starting transfer\n");
1180
1181         switch (USB_GET_STATE(xfer)) {
1182         case USB_ST_TRANSFERRED:
1183                 DPRINTFN(11, "transfer complete\n");
1184
1185                 ifq_clr_oactive(&ifp->if_snd);
1186                 
1187                 /* FALLTHROUGH */
1188         case USB_ST_SETUP:
1189 tr_setup:
1190                 if ((sc->sc_flags & AXE_FLAG_LINK) == 0 ||
1191                     ifq_is_oactive(&ifp->if_snd)) {
1192                         /*
1193                          * Don't send anything if there is no link or
1194                          * controller is busy.
1195                          */
1196                         DPRINTFN(11, "controller busy:  sc_flags: %x if_flags %x\n",sc->sc_flags, ifp->if_flags);
1197                         return;
1198                 }
1199
1200                 DPRINTFN(11, "copying frames, 16 at a time\n");
1201                 for (nframes = 0; nframes < 16 &&
1202                     !ifq_is_empty(&ifp->if_snd); nframes++) {
1203                         m = ifq_dequeue(&ifp->if_snd);
1204                         if (m == NULL)
1205                                 break;
1206                         usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1207                             nframes);
1208                         pos = 0;
1209                         pc = usbd_xfer_get_frame(xfer, nframes);
1210                         if (AXE_IS_178_FAMILY(sc)) {
1211                                 hdr.len = htole16(m->m_pkthdr.len);
1212                                 hdr.ilen = ~hdr.len;
1213                                 /*
1214                                  * If upper stack computed checksum, driver
1215                                  * should tell controller not to insert
1216                                  * computed checksum for checksum offloading
1217                                  * enabled controller.
1218                                  */
1219                                 if (ifp->if_capabilities & IFCAP_TXCSUM) {
1220                                         if ((m->m_pkthdr.csum_flags &
1221                                             AXE_CSUM_FEATURES) != 0)
1222                                                 hdr.len |= htole16(
1223                                                     AXE_TX_CSUM_PSEUDO_HDR);
1224                                         else
1225                                                 hdr.len |= htole16(
1226                                                     AXE_TX_CSUM_DIS);
1227                                 }
1228                                 DPRINTFN(11, "usbd copy in\n"); 
1229                                 usbd_copy_in(pc, pos, &hdr, sizeof(hdr));
1230                                 pos += sizeof(hdr);
1231                                 usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
1232                                 pos += m->m_pkthdr.len;
1233                                 if ((pos % 512) == 0) {
1234                                         hdr.len = 0;
1235                                         hdr.ilen = 0xffff;
1236                                         usbd_copy_in(pc, pos, &hdr,
1237                                             sizeof(hdr));
1238                                         pos += sizeof(hdr);
1239                                 }
1240                         } else {
1241                                 usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
1242                                 pos += m->m_pkthdr.len;
1243                         }
1244
1245                         /*
1246                          * XXX
1247                          * Update TX packet counter here. This is not
1248                          * correct way but it seems that there is no way
1249                          * to know how many packets are sent at the end
1250                          * of transfer because controller combines
1251                          * multiple writes into single one if there is
1252                          * room in TX buffer of controller.
1253                          */
1254                         IFNET_STAT_INC(ifp, opackets, 1);
1255
1256                         /*
1257                          * if there's a BPF listener, bounce a copy
1258                          * of this frame to him:
1259                          */
1260                         BPF_MTAP(ifp, m);
1261
1262                         m_freem(m);
1263
1264                         /* Set frame length. */
1265                         usbd_xfer_set_frame_len(xfer, nframes, pos);
1266                 }
1267                 if (nframes != 0) {
1268                         usbd_xfer_set_frames(xfer, nframes);
1269                         DPRINTFN(5, "submitting transfer\n");
1270                         usbd_transfer_submit(xfer);
1271                         ifq_set_oactive(&ifp->if_snd);
1272                 }
1273                 return;
1274                 /* NOTREACHED */
1275         default:                        /* Error */
1276                 DPRINTFN(11, "transfer error, %s\n",
1277                     usbd_errstr(error));
1278
1279                 IFNET_STAT_INC(ifp, oerrors, 1);
1280                 ifq_clr_oactive(&ifp->if_snd);
1281                 if (error != USB_ERR_CANCELLED) {
1282                         /* try to clear stall first */
1283                         usbd_xfer_set_stall(xfer);
1284                         goto tr_setup;
1285                 }
1286                 return;
1287
1288         }
1289 }
1290
1291 static void
1292 axe_tick(struct usb_ether *ue)
1293 {
1294         struct axe_softc *sc = uether_getsc(ue);
1295         struct mii_data *mii = GET_MII(sc);
1296
1297         AXE_LOCK_ASSERT(sc);
1298
1299         mii_tick(mii);
1300         if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
1301                 axe_miibus_statchg(ue->ue_dev);
1302                 if ((sc->sc_flags & AXE_FLAG_LINK) != 0)
1303                         axe_start(ue);
1304         }
1305 }
1306
1307 static void
1308 axe_start(struct usb_ether *ue)
1309 {
1310         struct axe_softc *sc = uether_getsc(ue);
1311
1312         /*
1313          * start the USB transfers, if not already started:
1314          */
1315         usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
1316         usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
1317 }
1318
1319 static void
1320 axe_csum_cfg(struct usb_ether *ue)
1321 {
1322         struct axe_softc *sc;
1323         struct ifnet *ifp;
1324         uint16_t csum1, csum2;
1325
1326         sc = uether_getsc(ue);
1327         AXE_LOCK_ASSERT(sc);
1328
1329         if ((sc->sc_flags & AXE_FLAG_772B) != 0) {
1330                 ifp = uether_getifp(ue);
1331                 csum1 = 0;
1332                 csum2 = 0;
1333                 if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
1334                         csum1 |= AXE_TXCSUM_IP | AXE_TXCSUM_TCP |
1335                             AXE_TXCSUM_UDP;
1336                 axe_cmd(sc, AXE_772B_CMD_WRITE_TXCSUM, csum2, csum1, NULL);
1337                 csum1 = 0;
1338                 csum2 = 0;
1339                 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1340                         csum1 |= AXE_RXCSUM_IP | AXE_RXCSUM_IPVE |
1341                             AXE_RXCSUM_TCP | AXE_RXCSUM_UDP | AXE_RXCSUM_ICMP |
1342                             AXE_RXCSUM_IGMP;
1343                 axe_cmd(sc, AXE_772B_CMD_WRITE_RXCSUM, csum2, csum1, NULL);
1344         }
1345 }
1346
1347 static void
1348 axe_init(struct usb_ether *ue)
1349 {
1350         struct axe_softc *sc = uether_getsc(ue);
1351         struct ifnet *ifp = uether_getifp(ue);
1352         int rxmode;
1353
1354         AXE_LOCK_ASSERT(sc);
1355
1356         
1357         if ((ifp->if_flags & IFF_RUNNING) != 0)
1358                 return;
1359         
1360         /* Cancel pending I/O */
1361         axe_stop(ue);
1362
1363         axe_reset(sc);
1364
1365         /* Set MAC address and transmitter IPG values. */
1366         if (AXE_IS_178_FAMILY(sc)) {
1367                 axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
1368                 axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
1369                     (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
1370         } else {
1371                 axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
1372                 axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
1373                 axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
1374                 axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
1375         }
1376
1377         if (AXE_IS_178_FAMILY(sc)) {
1378                 sc->sc_flags &= ~(AXE_FLAG_STD_FRAME | AXE_FLAG_CSUM_FRAME);
1379                 if ((sc->sc_flags & AXE_FLAG_772B) != 0) {
1380                         sc->sc_lenmask = AXE_CSUM_HDR_LEN_MASK;
1381                         sc->sc_flags |= AXE_FLAG_CSUM_FRAME;
1382                 } else {
1383                         sc->sc_lenmask = AXE_HDR_LEN_MASK;
1384                         sc->sc_flags |= AXE_FLAG_STD_FRAME;
1385                 }
1386         }
1387
1388         /* Configure TX/RX checksum offloading. */
1389         axe_csum_cfg(ue);
1390
1391         if (sc->sc_flags & AXE_FLAG_772B) {
1392                 /* AX88772B uses different maximum frame burst configuration. */
1393                 axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG,
1394                     ax88772b_mfb_table[AX88772B_MFB_16K].threshold,
1395                     ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL);
1396         }
1397
1398         /* Enable receiver, set RX mode. */
1399         rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
1400         if (AXE_IS_178_FAMILY(sc)) {
1401                 if (sc->sc_flags & AXE_FLAG_772B) {
1402                         /*
1403                          * Select RX header format type 1.  Aligning IP
1404                          * header on 4 byte boundary is not needed when
1405                          * checksum offloading feature is not used
1406                          * because we always copy the received frame in
1407                          * RX handler.  When RX checksum offloading is
1408                          * active, aligning IP header is required to
1409                          * reflect actual frame length including RX
1410                          * header size.
1411                          */
1412                         rxmode |= AXE_772B_RXCMD_HDR_TYPE_1;
1413                         if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1414                                 rxmode |= AXE_772B_RXCMD_IPHDR_ALIGN;
1415                 } else {
1416                         /*
1417                          * Default Rx buffer size is too small to get
1418                          * maximum performance.
1419                          */
1420                         rxmode |= AXE_178_RXCMD_MFB_16384;
1421                 }
1422         } else {
1423                 rxmode |= AXE_172_RXCMD_UNICAST;
1424         }
1425
1426         /* If we want promiscuous mode, set the allframes bit. */
1427         if (ifp->if_flags & IFF_PROMISC)
1428                 rxmode |= AXE_RXCMD_PROMISC;
1429
1430         if (ifp->if_flags & IFF_BROADCAST)
1431                 rxmode |= AXE_RXCMD_BROADCAST;
1432
1433         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1434
1435         /* Load the multicast filter. */
1436         axe_setmulti(ue);
1437
1438         usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
1439
1440         
1441         ifp->if_flags |= IFF_RUNNING;
1442         
1443         /* Switch to selected media. */
1444         axe_ifmedia_upd(ifp);
1445 }
1446
1447 static void
1448 axe_setpromisc(struct usb_ether *ue)
1449 {
1450         struct axe_softc *sc = uether_getsc(ue);
1451         struct ifnet *ifp = uether_getifp(ue);
1452         uint16_t rxmode;
1453
1454         axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
1455
1456         rxmode = le16toh(rxmode);
1457
1458         if (ifp->if_flags & IFF_PROMISC) {
1459                 rxmode |= AXE_RXCMD_PROMISC;
1460         } else {
1461                 rxmode &= ~AXE_RXCMD_PROMISC;
1462         }
1463
1464         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1465
1466         axe_setmulti(ue);
1467 }
1468
1469 static void
1470 axe_stop(struct usb_ether *ue)
1471 {
1472         struct axe_softc *sc = uether_getsc(ue);
1473         struct ifnet *ifp = uether_getifp(ue);
1474
1475         AXE_LOCK_ASSERT(sc);
1476
1477         
1478         ifp->if_flags &= ~IFF_RUNNING;
1479         ifq_clr_oactive(&ifp->if_snd);
1480         
1481         sc->sc_flags &= ~AXE_FLAG_LINK;
1482
1483         /*
1484          * stop all the transfers, if not already stopped:
1485          */
1486         usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
1487         usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
1488 }
1489
1490 static int
1491 axe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *uc)
1492 {
1493         struct usb_ether *ue = ifp->if_softc;
1494         struct axe_softc *sc;
1495         struct ifreq *ifr;
1496         int error, mask, reinit;
1497
1498         sc = uether_getsc(ue);
1499         ifr = (struct ifreq *)data;
1500         error = 0;
1501         reinit = 0;
1502         if (cmd == SIOCSIFCAP) {
1503                 AXE_LOCK(sc);
1504                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1505                 if ((mask & IFCAP_TXCSUM) != 0 &&
1506                     (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
1507                         ifp->if_capenable ^= IFCAP_TXCSUM;
1508                         if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
1509                                 ifp->if_hwassist |= AXE_CSUM_FEATURES;
1510                         else
1511                                 ifp->if_hwassist &= ~AXE_CSUM_FEATURES;
1512                         reinit++;
1513                 }
1514                 if ((mask & IFCAP_RXCSUM) != 0 &&
1515                     (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1516                         ifp->if_capenable ^= IFCAP_RXCSUM;
1517                         reinit++;
1518                 }
1519                 if (reinit > 0 && ifp->if_flags & IFF_RUNNING) 
1520                         ifp->if_flags &= ~IFF_RUNNING; 
1521                 else 
1522                         reinit = 0;
1523                 AXE_UNLOCK(sc);
1524                 if (reinit > 0)
1525                         uether_init(ue);
1526         } else
1527                 error = uether_ioctl(ifp, cmd, data, uc);
1528
1529         return (error);
1530 }