if_axe.c revision 1.84.2.5 1 /* $NetBSD: if_axe.c,v 1.84.2.5 2018/09/30 01:45:51 pgoyette Exp $ */
2 /* $OpenBSD: if_axe.c,v 1.137 2016/04/13 11:03:37 mpi Exp $ */
3
4 /*
5 * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * Copyright (c) 1997, 1998, 1999, 2000-2003
22 * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by Bill Paul.
35 * 4. Neither the name of the author nor the names of any co-contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
49 * THE POSSIBILITY OF SUCH DAMAGE.
50 */
51
52 /*
53 * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
54 * Used in the LinkSys USB200M and various other adapters.
55 *
56 * Written by Bill Paul <wpaul (at) windriver.com>
57 * Senior Engineer
58 * Wind River Systems
59 */
60
61 /*
62 * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
63 * It uses an external PHY (reference designs use a RealTek chip),
64 * and has a 64-bit multicast hash filter. There is some information
65 * missing from the manual which one needs to know in order to make
66 * the chip function:
67 *
68 * - You must set bit 7 in the RX control register, otherwise the
69 * chip won't receive any packets.
70 * - You must initialize all 3 IPG registers, or you won't be able
71 * to send any packets.
72 *
73 * Note that this device appears to only support loading the station
74 * address via autoload from the EEPROM (i.e. there's no way to manually
75 * set it).
76 *
77 * (Adam Weinberger wanted me to name this driver if_gir.c.)
78 */
79
80 /*
81 * Ax88178 and Ax88772 support backported from the OpenBSD driver.
82 * 2007/02/12, J.R. Oldroyd, fbsd (at) opal.com
83 *
84 * Manual here:
85 * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
86 * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
87 */
88
89 #include <sys/cdefs.h>
90 __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.84.2.5 2018/09/30 01:45:51 pgoyette Exp $");
91
92 #ifdef _KERNEL_OPT
93 #include "opt_inet.h"
94 #include "opt_usb.h"
95 #include "opt_net_mpsafe.h"
96 #endif
97
98 #include <sys/param.h>
99 #include <sys/bus.h>
100 #include <sys/device.h>
101 #include <sys/kernel.h>
102 #include <sys/mbuf.h>
103 #include <sys/module.h>
104 #include <sys/mutex.h>
105 #include <sys/socket.h>
106 #include <sys/sockio.h>
107 #include <sys/systm.h>
108
109 #include <sys/rndsource.h>
110
111 #include <net/if.h>
112 #include <net/if_dl.h>
113 #include <net/if_ether.h>
114 #include <net/if_media.h>
115
116 #include <net/bpf.h>
117
118 #include <dev/mii/mii.h>
119 #include <dev/mii/miivar.h>
120
121 #include <dev/usb/usb.h>
122 #include <dev/usb/usbhist.h>
123 #include <dev/usb/usbdi.h>
124 #include <dev/usb/usbdi_util.h>
125 #include <dev/usb/usbdivar.h>
126 #include <dev/usb/usbdevs.h>
127
128 #include <dev/usb/if_axereg.h>
129
130 /*
131 * AXE_178_MAX_FRAME_BURST
132 * max frame burst size for Ax88178 and Ax88772
133 * 0 2048 bytes
134 * 1 4096 bytes
135 * 2 8192 bytes
136 * 3 16384 bytes
137 * use the largest your system can handle without USB stalling.
138 *
139 * NB: 88772 parts appear to generate lots of input errors with
140 * a 2K rx buffer and 8K is only slightly faster than 4K on an
141 * EHCI port on a T42 so change at your own risk.
142 */
143 #define AXE_178_MAX_FRAME_BURST 1
144
145
146 #ifdef USB_DEBUG
147 #ifndef AXE_DEBUG
148 #define axedebug 0
149 #else
150 static int axedebug = 20;
151
152 SYSCTL_SETUP(sysctl_hw_axe_setup, "sysctl hw.axe setup")
153 {
154 int err;
155 const struct sysctlnode *rnode;
156 const struct sysctlnode *cnode;
157
158 err = sysctl_createv(clog, 0, NULL, &rnode,
159 CTLFLAG_PERMANENT, CTLTYPE_NODE, "axe",
160 SYSCTL_DESCR("axe global controls"),
161 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
162
163 if (err)
164 goto fail;
165
166 /* control debugging printfs */
167 err = sysctl_createv(clog, 0, &rnode, &cnode,
168 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
169 "debug", SYSCTL_DESCR("Enable debugging output"),
170 NULL, 0, &axedebug, sizeof(axedebug), CTL_CREATE, CTL_EOL);
171 if (err)
172 goto fail;
173
174 return;
175 fail:
176 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
177 }
178
179 #endif /* AXE_DEBUG */
180 #endif /* USB_DEBUG */
181
182 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(axedebug,1,FMT,A,B,C,D)
183 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(axedebug,N,FMT,A,B,C,D)
184 #define AXEHIST_FUNC() USBHIST_FUNC()
185 #define AXEHIST_CALLED(name) USBHIST_CALLED(axedebug)
186
187 /*
188 * Various supported device vendors/products.
189 */
190 static const struct axe_type axe_devs[] = {
191 { { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE2000}, 0 },
192 { { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 },
193 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET }, AX772 },
194 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 },
195 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772}, AX772 },
196 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772A}, AX772 },
197 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B}, AX772B },
198 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B_1}, AX772B },
199 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178}, AX178 },
200 { { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T}, 0 },
201 { { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055 }, AX178 },
202 { { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR}, 0},
203 { { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2}, AX772A },
204 { { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
205 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100}, 0 },
206 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1 }, AX772 },
207 { { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DUBE100B1 }, AX772 },
208 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100C1 }, AX772B },
209 { { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
210 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2 }, AX178 },
211 { { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 },
212 { { USB_VENDOR_LENOVO, USB_PRODUCT_LENOVO_ETHERNET }, AX772B },
213 { { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_HG20F9}, AX772B },
214 { { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M}, 0 },
215 { { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000 }, AX178 },
216 { { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LAN_GTJU2}, AX178 },
217 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2GT}, AX178 },
218 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
219 { { USB_VENDOR_MSI, USB_PRODUCT_MSI_AX88772A}, AX772 },
220 { { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120}, 0 },
221 { { USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS }, AX772 },
222 { { USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T }, AX178 },
223 { { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029}, 0 },
224 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028 }, AX178 },
225 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN031 }, AX178 },
226 { { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
227 };
228 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
229
230 static const struct ax88772b_mfb ax88772b_mfb_table[] = {
231 { 0x8000, 0x8001, 2048 },
232 { 0x8100, 0x8147, 4096 },
233 { 0x8200, 0x81EB, 6144 },
234 { 0x8300, 0x83D7, 8192 },
235 { 0x8400, 0x851E, 16384 },
236 { 0x8500, 0x8666, 20480 },
237 { 0x8600, 0x87AE, 24576 },
238 { 0x8700, 0x8A3D, 32768 }
239 };
240
241 int axe_match(device_t, cfdata_t, void *);
242 void axe_attach(device_t, device_t, void *);
243 int axe_detach(device_t, int);
244 int axe_activate(device_t, devact_t);
245
246 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc),
247 axe_match, axe_attach, axe_detach, axe_activate);
248
249 static int axe_tx_list_init(struct axe_softc *);
250 static int axe_rx_list_init(struct axe_softc *);
251 static int axe_encap(struct axe_softc *, struct mbuf *, int);
252 static void axe_rxeof(struct usbd_xfer *, void *, usbd_status);
253 static void axe_txeof(struct usbd_xfer *, void *, usbd_status);
254 static void axe_tick(void *);
255 static void axe_tick_task(void *);
256 static void axe_start(struct ifnet *);
257 static int axe_ioctl(struct ifnet *, u_long, void *);
258 static int axe_init(struct ifnet *);
259 static void axe_stop(struct ifnet *, int);
260 static void axe_watchdog(struct ifnet *);
261 static int axe_miibus_readreg_locked(device_t, int, int);
262 static int axe_miibus_readreg(device_t, int, int);
263 static void axe_miibus_writereg_locked(device_t, int, int, int);
264 static void axe_miibus_writereg(device_t, int, int, int);
265 static void axe_miibus_statchg(struct ifnet *);
266 static int axe_cmd(struct axe_softc *, int, int, int, void *);
267 static void axe_reset(struct axe_softc *);
268
269 static void axe_setmulti(struct axe_softc *);
270 static void axe_lock_mii(struct axe_softc *);
271 static void axe_unlock_mii(struct axe_softc *);
272
273 static void axe_ax88178_init(struct axe_softc *);
274 static void axe_ax88772_init(struct axe_softc *);
275 static void axe_ax88772a_init(struct axe_softc *);
276 static void axe_ax88772b_init(struct axe_softc *);
277
278 /* Get exclusive access to the MII registers */
279 static void
280 axe_lock_mii(struct axe_softc *sc)
281 {
282
283 sc->axe_refcnt++;
284 mutex_enter(&sc->axe_mii_lock);
285 }
286
287 static void
288 axe_unlock_mii(struct axe_softc *sc)
289 {
290
291 mutex_exit(&sc->axe_mii_lock);
292 if (--sc->axe_refcnt < 0)
293 usb_detach_wakeupold((sc->axe_dev));
294 }
295
296 static int
297 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
298 {
299 AXEHIST_FUNC(); AXEHIST_CALLED();
300 usb_device_request_t req;
301 usbd_status err;
302
303 KASSERT(mutex_owned(&sc->axe_mii_lock));
304
305 if (sc->axe_dying)
306 return -1;
307
308 DPRINTFN(20, "cmd %#jx index %#jx val %#jx", cmd, index, val, 0);
309
310 if (AXE_CMD_DIR(cmd))
311 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
312 else
313 req.bmRequestType = UT_READ_VENDOR_DEVICE;
314 req.bRequest = AXE_CMD_CMD(cmd);
315 USETW(req.wValue, val);
316 USETW(req.wIndex, index);
317 USETW(req.wLength, AXE_CMD_LEN(cmd));
318
319 err = usbd_do_request(sc->axe_udev, &req, buf);
320
321 if (err) {
322 DPRINTF("cmd %jd err %jd", cmd, err, 0, 0);
323 return -1;
324 }
325 return 0;
326 }
327
328 static int
329 axe_miibus_readreg_locked(device_t dev, int phy, int reg)
330 {
331 AXEHIST_FUNC(); AXEHIST_CALLED();
332 struct axe_softc *sc = device_private(dev);
333 usbd_status err;
334 uint16_t val;
335
336 DPRINTFN(30, "phy 0x%jx reg 0x%jx\n", phy, reg, 0, 0);
337
338 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
339
340 err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
341 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
342 if (err) {
343 aprint_error_dev(sc->axe_dev, "read PHY failed\n");
344 return -1;
345 }
346
347 val = le16toh(val);
348 if (AXE_IS_772(sc) && reg == MII_BMSR) {
349 /*
350 * BMSR of AX88772 indicates that it supports extended
351 * capability but the extended status register is
352 * reserved for embedded ethernet PHY. So clear the
353 * extended capability bit of BMSR.
354 */
355 val &= ~BMSR_EXTCAP;
356 }
357
358 DPRINTFN(30, "phy 0x%jx reg 0x%jx val %#jx", phy, reg, val, 0);
359
360 return val;
361 }
362
363 static int
364 axe_miibus_readreg(device_t dev, int phy, int reg)
365 {
366 struct axe_softc *sc = device_private(dev);
367 int val;
368
369 if (sc->axe_dying)
370 return 0;
371
372 if (sc->axe_phyno != phy)
373 return 0;
374
375 axe_lock_mii(sc);
376 val = axe_miibus_readreg_locked(dev, phy, reg);
377 axe_unlock_mii(sc);
378
379 return val;
380 }
381
382 static void
383 axe_miibus_writereg_locked(device_t dev, int phy, int reg, int aval)
384 {
385 struct axe_softc *sc = device_private(dev);
386 usbd_status err;
387 uint16_t val;
388
389 val = htole16(aval);
390
391 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
392 err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
393 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
394
395 if (err) {
396 aprint_error_dev(sc->axe_dev, "write PHY failed\n");
397 return;
398 }
399 }
400
401 static void
402 axe_miibus_writereg(device_t dev, int phy, int reg, int aval)
403 {
404 struct axe_softc *sc = device_private(dev);
405
406 if (sc->axe_dying)
407 return;
408
409 if (sc->axe_phyno != phy)
410 return;
411
412 axe_lock_mii(sc);
413 axe_miibus_writereg_locked(dev, phy, reg, aval);
414 axe_unlock_mii(sc);
415 }
416
417 static void
418 axe_miibus_statchg(struct ifnet *ifp)
419 {
420 AXEHIST_FUNC(); AXEHIST_CALLED();
421
422 struct axe_softc *sc = ifp->if_softc;
423 struct mii_data *mii = &sc->axe_mii;
424 int val, err;
425
426 val = 0;
427 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
428 val |= AXE_MEDIA_FULL_DUPLEX;
429 if (AXE_IS_178_FAMILY(sc)) {
430 if ((IFM_OPTIONS(mii->mii_media_active) &
431 IFM_ETH_TXPAUSE) != 0)
432 val |= AXE_178_MEDIA_TXFLOW_CONTROL_EN;
433 if ((IFM_OPTIONS(mii->mii_media_active) &
434 IFM_ETH_RXPAUSE) != 0)
435 val |= AXE_178_MEDIA_RXFLOW_CONTROL_EN;
436 }
437 }
438 if (AXE_IS_178_FAMILY(sc)) {
439 val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
440 if (sc->axe_flags & AX178)
441 val |= AXE_178_MEDIA_ENCK;
442 switch (IFM_SUBTYPE(mii->mii_media_active)) {
443 case IFM_1000_T:
444 val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
445 break;
446 case IFM_100_TX:
447 val |= AXE_178_MEDIA_100TX;
448 break;
449 case IFM_10_T:
450 /* doesn't need to be handled */
451 break;
452 }
453 }
454
455 DPRINTF("val=0x%jx", val, 0, 0, 0);
456 axe_lock_mii(sc);
457 err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
458 axe_unlock_mii(sc);
459 if (err) {
460 aprint_error_dev(sc->axe_dev, "media change failed\n");
461 return;
462 }
463 }
464
465 static void
466 axe_setmulti(struct axe_softc *sc)
467 {
468 AXEHIST_FUNC(); AXEHIST_CALLED();
469 struct ifnet *ifp = &sc->sc_if;
470 struct ether_multi *enm;
471 struct ether_multistep step;
472 uint32_t h = 0;
473 uint16_t rxmode;
474 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
475
476 if (sc->axe_dying)
477 return;
478
479 axe_lock_mii(sc);
480 if (axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode)) {
481 axe_unlock_mii(sc);
482 aprint_error_dev(sc->axe_dev, "can't read rxmode");
483 return;
484 }
485 rxmode = le16toh(rxmode);
486
487 rxmode &=
488 ~(AXE_RXCMD_ALLMULTI | AXE_RXCMD_PROMISC |
489 AXE_RXCMD_BROADCAST | AXE_RXCMD_MULTICAST);
490
491 rxmode |=
492 (ifp->if_flags & IFF_BROADCAST) ? AXE_RXCMD_BROADCAST : 0;
493
494 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
495 if (ifp->if_flags & IFF_PROMISC)
496 rxmode |= AXE_RXCMD_PROMISC;
497 goto allmulti;
498 }
499
500 /* Now program new ones */
501 ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
502 while (enm != NULL) {
503 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
504 ETHER_ADDR_LEN) != 0)
505 goto allmulti;
506
507 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
508 hashtbl[h >> 3] |= 1U << (h & 7);
509 ETHER_NEXT_MULTI(step, enm);
510 }
511 ifp->if_flags &= ~IFF_ALLMULTI;
512 rxmode |= AXE_RXCMD_MULTICAST;
513
514 axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, hashtbl);
515 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
516 axe_unlock_mii(sc);
517 return;
518
519 allmulti:
520 ifp->if_flags |= IFF_ALLMULTI;
521 rxmode |= AXE_RXCMD_ALLMULTI;
522 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
523 axe_unlock_mii(sc);
524 }
525
526 static void
527 axe_ax_init(struct axe_softc *sc)
528 {
529 int cmd = AXE_178_CMD_READ_NODEID;
530
531 if (sc->axe_flags & AX178) {
532 axe_ax88178_init(sc);
533 } else if (sc->axe_flags & AX772) {
534 axe_ax88772_init(sc);
535 } else if (sc->axe_flags & AX772A) {
536 axe_ax88772a_init(sc);
537 } else if (sc->axe_flags & AX772B) {
538 axe_ax88772b_init(sc);
539 return;
540 } else {
541 cmd = AXE_172_CMD_READ_NODEID;
542 }
543
544 if (axe_cmd(sc, cmd, 0, 0, sc->axe_enaddr)) {
545 aprint_error_dev(sc->axe_dev,
546 "failed to read ethernet address\n");
547 }
548 }
549
550
551 static void
552 axe_reset(struct axe_softc *sc)
553 {
554
555 if (sc->axe_dying)
556 return;
557
558 /*
559 * softnet_lock can be taken when NET_MPAFE is not defined when calling
560 * if_addr_init -> if_init. This doesn't mixe well with the
561 * usbd_delay_ms calls in the init routines as things like nd6_slowtimo
562 * can fire during the wait and attempt to take softnet_lock and then
563 * block the softclk thread meaing the wait never ends.
564 */
565 #ifndef NET_MPSAFE
566 /* XXX What to reset? */
567
568 /* Wait a little while for the chip to get its brains in order. */
569 DELAY(1000);
570 #else
571 axe_lock_mii(sc);
572
573 axe_ax_init(sc);
574
575 axe_unlock_mii(sc);
576 #endif
577 }
578
579 static int
580 axe_get_phyno(struct axe_softc *sc, int sel)
581 {
582 int phyno;
583
584 switch (AXE_PHY_TYPE(sc->axe_phyaddrs[sel])) {
585 case PHY_TYPE_100_HOME:
586 /* FALLTHROUGH */
587 case PHY_TYPE_GIG:
588 phyno = AXE_PHY_NO(sc->axe_phyaddrs[sel]);
589 break;
590 case PHY_TYPE_SPECIAL:
591 /* FALLTHROUGH */
592 case PHY_TYPE_RSVD:
593 /* FALLTHROUGH */
594 case PHY_TYPE_NON_SUP:
595 /* FALLTHROUGH */
596 default:
597 phyno = -1;
598 break;
599 }
600
601 return phyno;
602 }
603
604 #define AXE_GPIO_WRITE(x, y) do { \
605 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL); \
606 usbd_delay_ms(sc->axe_udev, hztoms(y)); \
607 } while (0)
608
609 static void
610 axe_ax88178_init(struct axe_softc *sc)
611 {
612 AXEHIST_FUNC(); AXEHIST_CALLED();
613 int gpio0, ledmode, phymode;
614 uint16_t eeprom, val;
615
616 axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
617 /* XXX magic */
618 if (axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom) != 0)
619 eeprom = 0xffff;
620 axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
621
622 eeprom = le16toh(eeprom);
623
624 DPRINTF("EEPROM is 0x%jx", eeprom, 0, 0, 0);
625
626 /* if EEPROM is invalid we have to use to GPIO0 */
627 if (eeprom == 0xffff) {
628 phymode = AXE_PHY_MODE_MARVELL;
629 gpio0 = 1;
630 ledmode = 0;
631 } else {
632 phymode = eeprom & 0x7f;
633 gpio0 = (eeprom & 0x80) ? 0 : 1;
634 ledmode = eeprom >> 8;
635 }
636
637 DPRINTF("use gpio0: %jd, phymode %jd", gpio0, phymode, 0, 0);
638
639 /* Program GPIOs depending on PHY hardware. */
640 switch (phymode) {
641 case AXE_PHY_MODE_MARVELL:
642 if (gpio0 == 1) {
643 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
644 hz / 32);
645 AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
646 hz / 32);
647 AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
648 AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
649 hz / 32);
650 } else {
651 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
652 AXE_GPIO1_EN, hz / 3);
653 if (ledmode == 1) {
654 AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
655 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
656 hz / 3);
657 } else {
658 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
659 AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
660 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
661 AXE_GPIO2_EN, hz / 4);
662 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
663 AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
664 }
665 }
666 break;
667 case AXE_PHY_MODE_CICADA:
668 case AXE_PHY_MODE_CICADA_V2:
669 case AXE_PHY_MODE_CICADA_V2_ASIX:
670 if (gpio0 == 1)
671 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
672 AXE_GPIO0_EN, hz / 32);
673 else
674 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
675 AXE_GPIO1_EN, hz / 32);
676 break;
677 case AXE_PHY_MODE_AGERE:
678 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
679 AXE_GPIO1_EN, hz / 32);
680 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
681 AXE_GPIO2_EN, hz / 32);
682 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
683 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
684 AXE_GPIO2_EN, hz / 32);
685 break;
686 case AXE_PHY_MODE_REALTEK_8211CL:
687 case AXE_PHY_MODE_REALTEK_8211BN:
688 case AXE_PHY_MODE_REALTEK_8251CL:
689 val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
690 AXE_GPIO1 | AXE_GPIO1_EN;
691 AXE_GPIO_WRITE(val, hz / 32);
692 AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
693 AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
694 AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
695 if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
696 axe_miibus_writereg_locked(sc->axe_dev,
697 sc->axe_phyno, 0x1F, 0x0005);
698 axe_miibus_writereg_locked(sc->axe_dev,
699 sc->axe_phyno, 0x0C, 0x0000);
700 val = axe_miibus_readreg_locked(sc->axe_dev,
701 sc->axe_phyno, 0x0001);
702 axe_miibus_writereg_locked(sc->axe_dev,
703 sc->axe_phyno, 0x01, val | 0x0080);
704 axe_miibus_writereg_locked(sc->axe_dev,
705 sc->axe_phyno, 0x1F, 0x0000);
706 }
707 break;
708 default:
709 /* Unknown PHY model or no need to program GPIOs. */
710 break;
711 }
712
713 /* soft reset */
714 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
715 usbd_delay_ms(sc->axe_udev, 150);
716 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
717 AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
718 usbd_delay_ms(sc->axe_udev, 150);
719 /* Enable MII/GMII/RGMII interface to work with external PHY. */
720 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
721 usbd_delay_ms(sc->axe_udev, 10);
722 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
723 }
724
725 static void
726 axe_ax88772_init(struct axe_softc *sc)
727 {
728 AXEHIST_FUNC(); AXEHIST_CALLED();
729
730 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
731 usbd_delay_ms(sc->axe_udev, 40);
732
733 if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) {
734 /* ask for the embedded PHY */
735 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0,
736 AXE_SW_PHY_SELECT_EMBEDDED, NULL);
737 usbd_delay_ms(sc->axe_udev, 10);
738
739 /* power down and reset state, pin reset state */
740 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
741 usbd_delay_ms(sc->axe_udev, 60);
742
743 /* power down/reset state, pin operating state */
744 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
745 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
746 usbd_delay_ms(sc->axe_udev, 150);
747
748 /* power up, reset */
749 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
750
751 /* power up, operating */
752 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
753 AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
754 } else {
755 /* ask for external PHY */
756 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_EXT,
757 NULL);
758 usbd_delay_ms(sc->axe_udev, 10);
759
760 /* power down internal PHY */
761 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
762 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
763 }
764
765 usbd_delay_ms(sc->axe_udev, 150);
766 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
767 }
768
769 static void
770 axe_ax88772_phywake(struct axe_softc *sc)
771 {
772 AXEHIST_FUNC(); AXEHIST_CALLED();
773
774 if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) {
775 /* Manually select internal(embedded) PHY - MAC mode. */
776 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0,
777 AXE_SW_PHY_SELECT_EMBEDDED, NULL);
778 usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
779 } else {
780 /*
781 * Manually select external PHY - MAC mode.
782 * Reverse MII/RMII is for AX88772A PHY mode.
783 */
784 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
785 AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
786 usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
787 }
788
789 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
790 AXE_SW_RESET_IPRL, NULL);
791
792 /* T1 = min 500ns everywhere */
793 usbd_delay_ms(sc->axe_udev, 150);
794
795 /* Take PHY out of power down. */
796 if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) {
797 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
798 } else {
799 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRTE, NULL);
800 }
801
802 /* 772 T2 is 60ms. 772A T2 is 160ms, 772B T2 is 600ms */
803 usbd_delay_ms(sc->axe_udev, 600);
804
805 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
806
807 /* T3 = 500ns everywhere */
808 usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
809 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
810 usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
811 }
812
813 static void
814 axe_ax88772a_init(struct axe_softc *sc)
815 {
816 AXEHIST_FUNC(); AXEHIST_CALLED();
817
818 /* Reload EEPROM. */
819 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
820 axe_ax88772_phywake(sc);
821 /* Stop MAC. */
822 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
823 }
824
825 static void
826 axe_ax88772b_init(struct axe_softc *sc)
827 {
828 AXEHIST_FUNC(); AXEHIST_CALLED();
829 uint16_t eeprom;
830 int i;
831
832 /* Reload EEPROM. */
833 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM , hz / 32);
834
835 /*
836 * Save PHY power saving configuration(high byte) and
837 * clear EEPROM checksum value(low byte).
838 */
839 if (axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG,
840 &eeprom)) {
841 aprint_error_dev(sc->axe_dev, "failed to read eeprom\n");
842 return;
843 }
844
845 sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00;
846
847 /*
848 * Auto-loaded default station address from internal ROM is
849 * 00:00:00:00:00:00 such that an explicit access to EEPROM
850 * is required to get real station address.
851 */
852 uint8_t *eaddr = sc->axe_enaddr;
853 for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
854 if (axe_cmd(sc, AXE_CMD_SROM_READ, 0,
855 AXE_EEPROM_772B_NODE_ID + i, &eeprom)) {
856 aprint_error_dev(sc->axe_dev,
857 "failed to read eeprom\n");
858 eeprom = 0;
859 }
860 eeprom = le16toh(eeprom);
861 *eaddr++ = (uint8_t)(eeprom & 0xFF);
862 *eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF);
863 }
864 /* Wakeup PHY. */
865 axe_ax88772_phywake(sc);
866 /* Stop MAC. */
867 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
868 }
869
870 #undef AXE_GPIO_WRITE
871
872 /*
873 * Probe for a AX88172 chip.
874 */
875 int
876 axe_match(device_t parent, cfdata_t match, void *aux)
877 {
878 struct usb_attach_arg *uaa = aux;
879
880 return axe_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
881 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
882 }
883
884 /*
885 * Attach the interface. Allocate softc structures, do ifmedia
886 * setup and ethernet/BPF attach.
887 */
888 void
889 axe_attach(device_t parent, device_t self, void *aux)
890 {
891 AXEHIST_FUNC(); AXEHIST_CALLED();
892 struct axe_softc *sc = device_private(self);
893 struct usb_attach_arg *uaa = aux;
894 struct usbd_device *dev = uaa->uaa_device;
895 usbd_status err;
896 usb_interface_descriptor_t *id;
897 usb_endpoint_descriptor_t *ed;
898 struct mii_data *mii;
899 char *devinfop;
900 const char *devname = device_xname(self);
901 struct ifnet *ifp;
902 int i, s;
903
904 aprint_naive("\n");
905 aprint_normal("\n");
906
907 sc->axe_dev = self;
908 sc->axe_udev = dev;
909
910 devinfop = usbd_devinfo_alloc(dev, 0);
911 aprint_normal_dev(self, "%s\n", devinfop);
912 usbd_devinfo_free(devinfop);
913
914 err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
915 if (err) {
916 aprint_error_dev(self, "failed to set configuration"
917 ", err=%s\n", usbd_errstr(err));
918 return;
919 }
920
921 sc->axe_flags = axe_lookup(uaa->uaa_vendor, uaa->uaa_product)->axe_flags;
922
923 mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE);
924 usb_init_task(&sc->axe_tick_task, axe_tick_task, sc, 0);
925
926 err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
927 if (err) {
928 aprint_error_dev(self, "getting interface handle failed\n");
929 return;
930 }
931
932 sc->axe_product = uaa->uaa_product;
933 sc->axe_vendor = uaa->uaa_vendor;
934
935 id = usbd_get_interface_descriptor(sc->axe_iface);
936
937 /* decide on what our bufsize will be */
938 if (AXE_IS_178_FAMILY(sc))
939 sc->axe_bufsz = (sc->axe_udev->ud_speed == USB_SPEED_HIGH) ?
940 AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ;
941 else
942 sc->axe_bufsz = AXE_172_BUFSZ;
943
944 sc->axe_ed[AXE_ENDPT_RX] = -1;
945 sc->axe_ed[AXE_ENDPT_TX] = -1;
946 sc->axe_ed[AXE_ENDPT_INTR] = -1;
947
948 /* Find endpoints. */
949 for (i = 0; i < id->bNumEndpoints; i++) {
950 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
951 if (ed == NULL) {
952 aprint_error_dev(self, "couldn't get ep %d\n", i);
953 return;
954 }
955 const uint8_t xt = UE_GET_XFERTYPE(ed->bmAttributes);
956 const uint8_t dir = UE_GET_DIR(ed->bEndpointAddress);
957
958 if (dir == UE_DIR_IN && xt == UE_BULK &&
959 sc->axe_ed[AXE_ENDPT_RX] == -1) {
960 sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
961 } else if (dir == UE_DIR_OUT && xt == UE_BULK &&
962 sc->axe_ed[AXE_ENDPT_TX] == -1) {
963 sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
964 } else if (dir == UE_DIR_IN && xt == UE_INTERRUPT) {
965 sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
966 }
967 }
968
969 s = splnet();
970
971 /* We need the PHYID for init dance in some cases */
972 axe_lock_mii(sc);
973 if (axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, &sc->axe_phyaddrs)) {
974 aprint_error_dev(self, "failed to read phyaddrs\n");
975 return;
976 }
977
978 DPRINTF(" phyaddrs[0]: %jx phyaddrs[1]: %jx",
979 sc->axe_phyaddrs[0], sc->axe_phyaddrs[1], 0, 0);
980 sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
981 if (sc->axe_phyno == -1)
982 sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
983 if (sc->axe_phyno == -1) {
984 DPRINTF(" no valid PHY address found, assuming PHY address 0",
985 0, 0, 0, 0);
986 sc->axe_phyno = 0;
987 }
988
989 /* Initialize controller and get station address. */
990
991 axe_ax_init(sc);
992
993 /*
994 * Fetch IPG values.
995 */
996 if (sc->axe_flags & (AX772A | AX772B)) {
997 /* Set IPG values. */
998 sc->axe_ipgs[0] = AXE_IPG0_DEFAULT;
999 sc->axe_ipgs[1] = AXE_IPG1_DEFAULT;
1000 sc->axe_ipgs[2] = AXE_IPG2_DEFAULT;
1001 } else {
1002 if (axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->axe_ipgs)) {
1003 aprint_error_dev(self, "failed to read ipg\n");
1004 return;
1005 }
1006 }
1007
1008 axe_unlock_mii(sc);
1009
1010 /*
1011 * An ASIX chip was detected. Inform the world.
1012 */
1013 aprint_normal_dev(self, "Ethernet address %s\n",
1014 ether_sprintf(sc->axe_enaddr));
1015
1016 /* Initialize interface info.*/
1017 ifp = &sc->sc_if;
1018 ifp->if_softc = sc;
1019 strlcpy(ifp->if_xname, devname, IFNAMSIZ);
1020 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1021 ifp->if_ioctl = axe_ioctl;
1022 ifp->if_start = axe_start;
1023 ifp->if_init = axe_init;
1024 ifp->if_stop = axe_stop;
1025 ifp->if_watchdog = axe_watchdog;
1026
1027 IFQ_SET_READY(&ifp->if_snd);
1028
1029 if (AXE_IS_178_FAMILY(sc))
1030 sc->axe_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1031 if (sc->axe_flags & AX772B) {
1032 ifp->if_capabilities =
1033 IFCAP_CSUM_IPv4_Rx |
1034 IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
1035 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
1036 /*
1037 * Checksum offloading of AX88772B also works with VLAN
1038 * tagged frames but there is no way to take advantage
1039 * of the feature because vlan(4) assumes
1040 * IFCAP_VLAN_HWTAGGING is prerequisite condition to
1041 * support checksum offloading with VLAN. VLAN hardware
1042 * tagging support of AX88772B is very limited so it's
1043 * not possible to announce IFCAP_VLAN_HWTAGGING.
1044 */
1045 }
1046 u_int adv_pause;
1047 if (sc->axe_flags & (AX772A | AX772B | AX178))
1048 adv_pause = MIIF_DOPAUSE;
1049 else
1050 adv_pause = 0;
1051 adv_pause = 0;
1052
1053 /* Initialize MII/media info. */
1054 mii = &sc->axe_mii;
1055 mii->mii_ifp = ifp;
1056 mii->mii_readreg = axe_miibus_readreg;
1057 mii->mii_writereg = axe_miibus_writereg;
1058 mii->mii_statchg = axe_miibus_statchg;
1059 mii->mii_flags = MIIF_AUTOTSLEEP;
1060
1061 sc->axe_ec.ec_mii = mii;
1062 ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
1063
1064 mii_attach(sc->axe_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY,
1065 adv_pause);
1066
1067 if (LIST_EMPTY(&mii->mii_phys)) {
1068 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1069 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1070 } else
1071 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1072
1073 /* Attach the interface. */
1074 if_attach(ifp);
1075 ether_ifattach(ifp, sc->axe_enaddr);
1076 rnd_attach_source(&sc->rnd_source, device_xname(sc->axe_dev),
1077 RND_TYPE_NET, RND_FLAG_DEFAULT);
1078
1079 callout_init(&sc->axe_stat_ch, 0);
1080 callout_setfunc(&sc->axe_stat_ch, axe_tick, sc);
1081
1082 sc->axe_attached = true;
1083 splx(s);
1084
1085 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev, sc->axe_dev);
1086
1087 if (!pmf_device_register(self, NULL, NULL))
1088 aprint_error_dev(self, "couldn't establish power handler\n");
1089 }
1090
1091 int
1092 axe_detach(device_t self, int flags)
1093 {
1094 AXEHIST_FUNC(); AXEHIST_CALLED();
1095 struct axe_softc *sc = device_private(self);
1096 int s;
1097 struct ifnet *ifp = &sc->sc_if;
1098
1099 /* Detached before attached finished, so just bail out. */
1100 if (!sc->axe_attached)
1101 return 0;
1102
1103 pmf_device_deregister(self);
1104
1105 sc->axe_dying = true;
1106
1107 if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
1108 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1109 if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
1110 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1111 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
1112 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1113
1114 callout_halt(&sc->axe_stat_ch, NULL);
1115 usb_rem_task_wait(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER,
1116 NULL);
1117
1118 s = splusb();
1119
1120 if (ifp->if_flags & IFF_RUNNING)
1121 axe_stop(ifp, 1);
1122
1123
1124 if (--sc->axe_refcnt >= 0) {
1125 /* Wait for processes to go away. */
1126 usb_detach_waitold(sc->axe_dev);
1127 }
1128
1129 callout_destroy(&sc->axe_stat_ch);
1130 mutex_destroy(&sc->axe_mii_lock);
1131 rnd_detach_source(&sc->rnd_source);
1132 mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1133 ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
1134 ether_ifdetach(ifp);
1135 if_detach(ifp);
1136
1137 #ifdef DIAGNOSTIC
1138 if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
1139 sc->axe_ep[AXE_ENDPT_RX] != NULL ||
1140 sc->axe_ep[AXE_ENDPT_INTR] != NULL)
1141 aprint_debug_dev(self, "detach has active endpoints\n");
1142 #endif
1143
1144 sc->axe_attached = false;
1145
1146 splx(s);
1147
1148 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev, sc->axe_dev);
1149
1150 return 0;
1151 }
1152
1153 int
1154 axe_activate(device_t self, devact_t act)
1155 {
1156 AXEHIST_FUNC(); AXEHIST_CALLED();
1157 struct axe_softc *sc = device_private(self);
1158
1159 switch (act) {
1160 case DVACT_DEACTIVATE:
1161 if_deactivate(&sc->axe_ec.ec_if);
1162 sc->axe_dying = true;
1163 return 0;
1164 default:
1165 return EOPNOTSUPP;
1166 }
1167 }
1168
1169 static int
1170 axe_rx_list_init(struct axe_softc *sc)
1171 {
1172 AXEHIST_FUNC(); AXEHIST_CALLED();
1173
1174 struct axe_cdata *cd;
1175 struct axe_chain *c;
1176 int i;
1177
1178 cd = &sc->axe_cdata;
1179 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1180 c = &cd->axe_rx_chain[i];
1181 c->axe_sc = sc;
1182 c->axe_idx = i;
1183 if (c->axe_xfer == NULL) {
1184 int err = usbd_create_xfer(sc->axe_ep[AXE_ENDPT_RX],
1185 sc->axe_bufsz, 0, 0, &c->axe_xfer);
1186 if (err)
1187 return err;
1188 c->axe_buf = usbd_get_buffer(c->axe_xfer);
1189 }
1190 }
1191
1192 return 0;
1193 }
1194
1195 static int
1196 axe_tx_list_init(struct axe_softc *sc)
1197 {
1198 AXEHIST_FUNC(); AXEHIST_CALLED();
1199 struct axe_cdata *cd;
1200 struct axe_chain *c;
1201 int i;
1202
1203 cd = &sc->axe_cdata;
1204 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1205 c = &cd->axe_tx_chain[i];
1206 c->axe_sc = sc;
1207 c->axe_idx = i;
1208 if (c->axe_xfer == NULL) {
1209 int err = usbd_create_xfer(sc->axe_ep[AXE_ENDPT_TX],
1210 sc->axe_bufsz, USBD_FORCE_SHORT_XFER, 0,
1211 &c->axe_xfer);
1212 if (err)
1213 return err;
1214 c->axe_buf = usbd_get_buffer(c->axe_xfer);
1215 }
1216 }
1217
1218 return 0;
1219 }
1220
1221 /*
1222 * A frame has been uploaded: pass the resulting mbuf chain up to
1223 * the higher level protocols.
1224 */
1225 static void
1226 axe_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
1227 {
1228 AXEHIST_FUNC(); AXEHIST_CALLED();
1229 struct axe_softc *sc;
1230 struct axe_chain *c;
1231 struct ifnet *ifp;
1232 uint8_t *buf;
1233 uint32_t total_len;
1234 struct mbuf *m;
1235 int s;
1236
1237 c = (struct axe_chain *)priv;
1238 sc = c->axe_sc;
1239 buf = c->axe_buf;
1240 ifp = &sc->sc_if;
1241
1242 if (sc->axe_dying)
1243 return;
1244
1245 if ((ifp->if_flags & IFF_RUNNING) == 0)
1246 return;
1247
1248 if (status != USBD_NORMAL_COMPLETION) {
1249 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1250 return;
1251 if (usbd_ratecheck(&sc->axe_rx_notice)) {
1252 aprint_error_dev(sc->axe_dev, "usb errors on rx: %s\n",
1253 usbd_errstr(status));
1254 }
1255 if (status == USBD_STALLED)
1256 usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
1257 goto done;
1258 }
1259
1260 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1261
1262 do {
1263 u_int pktlen = 0;
1264 u_int rxlen = 0;
1265 int flags = 0;
1266 if ((sc->axe_flags & AXSTD_FRAME) != 0) {
1267 struct axe_sframe_hdr hdr;
1268
1269 if (total_len < sizeof(hdr)) {
1270 ifp->if_ierrors++;
1271 goto done;
1272 }
1273
1274 memcpy(&hdr, buf, sizeof(hdr));
1275
1276 DPRINTFN(20, "total_len %#jx len %jx ilen %#jx",
1277 total_len,
1278 (le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK),
1279 (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK), 0);
1280
1281 total_len -= sizeof(hdr);
1282 buf += sizeof(hdr);
1283
1284 if (((le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK) ^
1285 (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK)) !=
1286 AXE_RH1M_RXLEN_MASK) {
1287 ifp->if_ierrors++;
1288 goto done;
1289 }
1290
1291 rxlen = le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK;
1292 if (total_len < rxlen) {
1293 pktlen = total_len;
1294 total_len = 0;
1295 } else {
1296 pktlen = rxlen;
1297 rxlen = roundup2(rxlen, 2);
1298 total_len -= rxlen;
1299 }
1300
1301 } else if ((sc->axe_flags & AXCSUM_FRAME) != 0) {
1302 struct axe_csum_hdr csum_hdr;
1303
1304 if (total_len < sizeof(csum_hdr)) {
1305 ifp->if_ierrors++;
1306 goto done;
1307 }
1308
1309 memcpy(&csum_hdr, buf, sizeof(csum_hdr));
1310
1311 csum_hdr.len = le16toh(csum_hdr.len);
1312 csum_hdr.ilen = le16toh(csum_hdr.ilen);
1313 csum_hdr.cstatus = le16toh(csum_hdr.cstatus);
1314
1315 DPRINTFN(20, "total_len %#jx len %#jx ilen %#jx"
1316 " cstatus %#jx", total_len,
1317 csum_hdr.len, csum_hdr.ilen, csum_hdr.cstatus);
1318
1319 if ((AXE_CSUM_RXBYTES(csum_hdr.len) ^
1320 AXE_CSUM_RXBYTES(csum_hdr.ilen)) !=
1321 sc->sc_lenmask) {
1322 /* we lost sync */
1323 ifp->if_ierrors++;
1324 DPRINTFN(20, "len %#jx ilen %#jx lenmask %#jx "
1325 "err",
1326 AXE_CSUM_RXBYTES(csum_hdr.len),
1327 AXE_CSUM_RXBYTES(csum_hdr.ilen),
1328 sc->sc_lenmask, 0);
1329 goto done;
1330 }
1331 /*
1332 * Get total transferred frame length including
1333 * checksum header. The length should be multiple
1334 * of 4.
1335 */
1336 pktlen = AXE_CSUM_RXBYTES(csum_hdr.len);
1337 u_int len = sizeof(csum_hdr) + pktlen;
1338 len = (len + 3) & ~3;
1339 if (total_len < len) {
1340 DPRINTFN(20, "total_len %#jx < len %#jx",
1341 total_len, len, 0, 0);
1342 /* invalid length */
1343 ifp->if_ierrors++;
1344 goto done;
1345 }
1346 buf += sizeof(csum_hdr);
1347
1348 const uint16_t cstatus = csum_hdr.cstatus;
1349
1350 if (cstatus & AXE_CSUM_HDR_L3_TYPE_IPV4) {
1351 if (cstatus & AXE_CSUM_HDR_L4_CSUM_ERR)
1352 flags |= M_CSUM_TCP_UDP_BAD;
1353 if (cstatus & AXE_CSUM_HDR_L3_CSUM_ERR)
1354 flags |= M_CSUM_IPv4_BAD;
1355
1356 const uint16_t l4type =
1357 cstatus & AXE_CSUM_HDR_L4_TYPE_MASK;
1358
1359 if (l4type == AXE_CSUM_HDR_L4_TYPE_TCP)
1360 flags |= M_CSUM_TCPv4;
1361 if (l4type == AXE_CSUM_HDR_L4_TYPE_UDP)
1362 flags |= M_CSUM_UDPv4;
1363 }
1364 if (total_len < len) {
1365 pktlen = total_len;
1366 total_len = 0;
1367 } else {
1368 total_len -= len;
1369 rxlen = len - sizeof(csum_hdr);
1370 }
1371 DPRINTFN(20, "total_len %#jx len %#jx pktlen %#jx"
1372 " rxlen %#jx", total_len, len, pktlen, rxlen);
1373 } else { /* AX172 */
1374 pktlen = rxlen = total_len;
1375 total_len = 0;
1376 }
1377
1378 MGETHDR(m, M_DONTWAIT, MT_DATA);
1379 if (m == NULL) {
1380 ifp->if_ierrors++;
1381 goto done;
1382 }
1383
1384 if (pktlen > MHLEN - ETHER_ALIGN) {
1385 MCLGET(m, M_DONTWAIT);
1386 if ((m->m_flags & M_EXT) == 0) {
1387 m_freem(m);
1388 ifp->if_ierrors++;
1389 goto done;
1390 }
1391 }
1392 m->m_data += ETHER_ALIGN;
1393
1394 m_set_rcvif(m, ifp);
1395 m->m_pkthdr.len = m->m_len = pktlen;
1396 m->m_pkthdr.csum_flags = flags;
1397
1398 memcpy(mtod(m, uint8_t *), buf, pktlen);
1399 buf += rxlen;
1400
1401 DPRINTFN(10, "deliver %jd (%#jx)", m->m_len, m->m_len, 0, 0);
1402
1403 s = splnet();
1404
1405 if_percpuq_enqueue((ifp)->if_percpuq, (m));
1406
1407 splx(s);
1408
1409 } while (total_len > 0);
1410
1411 done:
1412
1413 /* Setup new transfer. */
1414 usbd_setup_xfer(xfer, c, c->axe_buf, sc->axe_bufsz,
1415 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
1416 usbd_transfer(xfer);
1417
1418 DPRINTFN(10, "start rx", 0, 0, 0, 0);
1419 }
1420
1421 /*
1422 * A frame was downloaded to the chip. It's safe for us to clean up
1423 * the list buffers.
1424 */
1425
1426 static void
1427 axe_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
1428 {
1429 AXEHIST_FUNC(); AXEHIST_CALLED();
1430 struct axe_chain *c = priv;
1431 struct axe_softc *sc = c->axe_sc;
1432 struct ifnet *ifp = &sc->sc_if;
1433 int s;
1434
1435
1436 if (sc->axe_dying)
1437 return;
1438
1439 s = splnet();
1440
1441 ifp->if_timer = 0;
1442 ifp->if_flags &= ~IFF_OACTIVE;
1443
1444 if (status != USBD_NORMAL_COMPLETION) {
1445 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1446 splx(s);
1447 return;
1448 }
1449 ifp->if_oerrors++;
1450 aprint_error_dev(sc->axe_dev, "usb error on tx: %s\n",
1451 usbd_errstr(status));
1452 if (status == USBD_STALLED)
1453 usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
1454 splx(s);
1455 return;
1456 }
1457 ifp->if_opackets++;
1458
1459 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1460 axe_start(ifp);
1461
1462 splx(s);
1463 }
1464
1465 static void
1466 axe_tick(void *xsc)
1467 {
1468 AXEHIST_FUNC(); AXEHIST_CALLED();
1469 struct axe_softc *sc = xsc;
1470
1471 if (sc == NULL)
1472 return;
1473
1474 if (sc->axe_dying)
1475 return;
1476
1477 /* Perform periodic stuff in process context */
1478 usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
1479 }
1480
1481 static void
1482 axe_tick_task(void *xsc)
1483 {
1484 AXEHIST_FUNC(); AXEHIST_CALLED();
1485 int s;
1486 struct axe_softc *sc = xsc;
1487 struct ifnet *ifp;
1488 struct mii_data *mii;
1489
1490 if (sc == NULL)
1491 return;
1492
1493 if (sc->axe_dying)
1494 return;
1495
1496 ifp = &sc->sc_if;
1497 mii = &sc->axe_mii;
1498
1499 if (mii == NULL)
1500 return;
1501
1502 s = splnet();
1503
1504 mii_tick(mii);
1505 if (sc->axe_link == 0 &&
1506 (mii->mii_media_status & IFM_ACTIVE) != 0 &&
1507 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1508 DPRINTF("got link", 0, 0, 0, 0);
1509 sc->axe_link++;
1510 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1511 axe_start(ifp);
1512 }
1513
1514 callout_schedule(&sc->axe_stat_ch, hz);
1515
1516 splx(s);
1517 }
1518
1519 static int
1520 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
1521 {
1522 struct ifnet *ifp = &sc->sc_if;
1523 struct axe_chain *c;
1524 usbd_status err;
1525 int length, boundary;
1526
1527 c = &sc->axe_cdata.axe_tx_chain[idx];
1528
1529 /*
1530 * Copy the mbuf data into a contiguous buffer, leaving two
1531 * bytes at the beginning to hold the frame length.
1532 */
1533 if (AXE_IS_178_FAMILY(sc)) {
1534 struct axe_sframe_hdr hdr;
1535
1536 boundary = (sc->axe_udev->ud_speed == USB_SPEED_HIGH) ? 512 : 64;
1537
1538 hdr.len = htole16(m->m_pkthdr.len);
1539 hdr.ilen = ~hdr.len;
1540
1541 memcpy(c->axe_buf, &hdr, sizeof(hdr));
1542 length = sizeof(hdr);
1543
1544 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length);
1545 length += m->m_pkthdr.len;
1546
1547 if ((length % boundary) == 0) {
1548 hdr.len = 0x0000;
1549 hdr.ilen = 0xffff;
1550 memcpy(c->axe_buf + length, &hdr, sizeof(hdr));
1551 length += sizeof(hdr);
1552 }
1553 } else {
1554 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
1555 length = m->m_pkthdr.len;
1556 }
1557
1558 usbd_setup_xfer(c->axe_xfer, c, c->axe_buf, length,
1559 USBD_FORCE_SHORT_XFER, 10000, axe_txeof);
1560
1561 /* Transmit */
1562 err = usbd_transfer(c->axe_xfer);
1563 if (err != USBD_IN_PROGRESS) {
1564 axe_stop(ifp, 0);
1565 return EIO;
1566 }
1567
1568 sc->axe_cdata.axe_tx_cnt++;
1569
1570 return 0;
1571 }
1572
1573
1574 static void
1575 axe_csum_cfg(struct axe_softc *sc)
1576 {
1577 struct ifnet *ifp = &sc->sc_if;
1578 uint16_t csum1, csum2;
1579
1580 if ((sc->axe_flags & AX772B) != 0) {
1581 csum1 = 0;
1582 csum2 = 0;
1583 if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) != 0)
1584 csum1 |= AXE_TXCSUM_IP;
1585 if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) != 0)
1586 csum1 |= AXE_TXCSUM_TCP;
1587 if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) != 0)
1588 csum1 |= AXE_TXCSUM_UDP;
1589 if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) != 0)
1590 csum1 |= AXE_TXCSUM_TCPV6;
1591 if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) != 0)
1592 csum1 |= AXE_TXCSUM_UDPV6;
1593 axe_cmd(sc, AXE_772B_CMD_WRITE_TXCSUM, csum2, csum1, NULL);
1594 csum1 = 0;
1595 csum2 = 0;
1596
1597 if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0)
1598 csum1 |= AXE_RXCSUM_IP;
1599 if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) != 0)
1600 csum1 |= AXE_RXCSUM_TCP;
1601 if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) != 0)
1602 csum1 |= AXE_RXCSUM_UDP;
1603 if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) != 0)
1604 csum1 |= AXE_RXCSUM_TCPV6;
1605 if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) != 0)
1606 csum1 |= AXE_RXCSUM_UDPV6;
1607 axe_cmd(sc, AXE_772B_CMD_WRITE_RXCSUM, csum2, csum1, NULL);
1608 }
1609 }
1610
1611 static void
1612 axe_start(struct ifnet *ifp)
1613 {
1614 struct axe_softc *sc;
1615 struct mbuf *m;
1616
1617 sc = ifp->if_softc;
1618
1619 if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
1620 return;
1621
1622 IFQ_POLL(&ifp->if_snd, m);
1623 if (m == NULL) {
1624 return;
1625 }
1626
1627 if (axe_encap(sc, m, 0)) {
1628 ifp->if_flags |= IFF_OACTIVE;
1629 return;
1630 }
1631 IFQ_DEQUEUE(&ifp->if_snd, m);
1632
1633 /*
1634 * If there's a BPF listener, bounce a copy of this frame
1635 * to him.
1636 */
1637 bpf_mtap(ifp, m, BPF_D_OUT);
1638 m_freem(m);
1639
1640 ifp->if_flags |= IFF_OACTIVE;
1641
1642 /*
1643 * Set a timeout in case the chip goes out to lunch.
1644 */
1645 ifp->if_timer = 5;
1646
1647 return;
1648 }
1649
1650 static int
1651 axe_init(struct ifnet *ifp)
1652 {
1653 AXEHIST_FUNC(); AXEHIST_CALLED();
1654 struct axe_softc *sc = ifp->if_softc;
1655 struct axe_chain *c;
1656 usbd_status err;
1657 int rxmode;
1658 int i, s;
1659
1660 s = splnet();
1661
1662 if (ifp->if_flags & IFF_RUNNING)
1663 axe_stop(ifp, 0);
1664
1665 /*
1666 * Cancel pending I/O and free all RX/TX buffers.
1667 */
1668 axe_reset(sc);
1669
1670 axe_lock_mii(sc);
1671
1672 #if 0
1673 ret = asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_2 |
1674 AX_GPIO_GPO2EN, 5, in_pm);
1675 #endif
1676 /* Set MAC address and transmitter IPG values. */
1677 if (AXE_IS_178_FAMILY(sc)) {
1678 axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, sc->axe_enaddr);
1679 axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2],
1680 (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL);
1681 } else {
1682 axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, sc->axe_enaddr);
1683 axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1684 axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1685 axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1686 }
1687 if (AXE_IS_178_FAMILY(sc)) {
1688 sc->axe_flags &= ~(AXSTD_FRAME | AXCSUM_FRAME);
1689 if ((sc->axe_flags & AX772B) != 0 &&
1690 (ifp->if_capenable & AX_RXCSUM) != 0) {
1691 sc->sc_lenmask = AXE_CSUM_HDR_LEN_MASK;
1692 sc->axe_flags |= AXCSUM_FRAME;
1693 } else {
1694 sc->sc_lenmask = AXE_HDR_LEN_MASK;
1695 sc->axe_flags |= AXSTD_FRAME;
1696 }
1697 }
1698
1699 /* Configure TX/RX checksum offloading. */
1700 axe_csum_cfg(sc);
1701
1702 if (sc->axe_flags & AX772B) {
1703 /* AX88772B uses different maximum frame burst configuration. */
1704 axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG,
1705 ax88772b_mfb_table[AX88772B_MFB_16K].threshold,
1706 ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL);
1707 }
1708 /* Enable receiver, set RX mode */
1709 rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
1710 if (AXE_IS_178_FAMILY(sc)) {
1711 if (sc->axe_flags & AX772B) {
1712 /*
1713 * Select RX header format type 1. Aligning IP
1714 * header on 4 byte boundary is not needed when
1715 * checksum offloading feature is not used
1716 * because we always copy the received frame in
1717 * RX handler. When RX checksum offloading is
1718 * active, aligning IP header is required to
1719 * reflect actual frame length including RX
1720 * header size.
1721 */
1722 rxmode |= AXE_772B_RXCMD_HDR_TYPE_1;
1723 if (sc->axe_flags & AXCSUM_FRAME)
1724 rxmode |= AXE_772B_RXCMD_IPHDR_ALIGN;
1725 } else {
1726 /*
1727 * Default Rx buffer size is too small to get
1728 * maximum performance.
1729 */
1730 #if 0
1731 if (sc->axe_udev->ud_speed == USB_SPEED_HIGH) {
1732 /* Largest possible USB buffer size for AX88178 */
1733 #endif
1734 rxmode |= AXE_178_RXCMD_MFB_16384;
1735 }
1736 } else {
1737 rxmode |= AXE_172_RXCMD_UNICAST;
1738 }
1739
1740
1741 /* If we want promiscuous mode, set the allframes bit. */
1742 if (ifp->if_flags & IFF_PROMISC)
1743 rxmode |= AXE_RXCMD_PROMISC;
1744
1745 if (ifp->if_flags & IFF_BROADCAST)
1746 rxmode |= AXE_RXCMD_BROADCAST;
1747
1748 DPRINTF("rxmode 0x%#jx", rxmode, 0, 0, 0);
1749
1750 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1751 axe_unlock_mii(sc);
1752
1753 /* Load the multicast filter. */
1754 axe_setmulti(sc);
1755
1756 /* Open RX and TX pipes. */
1757 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1758 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1759 if (err) {
1760 aprint_error_dev(sc->axe_dev, "open rx pipe failed: %s\n",
1761 usbd_errstr(err));
1762 splx(s);
1763 return EIO;
1764 }
1765
1766 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1767 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1768 if (err) {
1769 aprint_error_dev(sc->axe_dev, "open tx pipe failed: %s\n",
1770 usbd_errstr(err));
1771 splx(s);
1772 return EIO;
1773 }
1774
1775 /* Init RX ring. */
1776 if (axe_rx_list_init(sc) != 0) {
1777 aprint_error_dev(sc->axe_dev, "rx list init failed\n");
1778 splx(s);
1779 return ENOBUFS;
1780 }
1781
1782 /* Init TX ring. */
1783 if (axe_tx_list_init(sc) != 0) {
1784 aprint_error_dev(sc->axe_dev, "tx list init failed\n");
1785 splx(s);
1786 return ENOBUFS;
1787 }
1788
1789 /* Start up the receive pipe. */
1790 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1791 c = &sc->axe_cdata.axe_rx_chain[i];
1792 usbd_setup_xfer(c->axe_xfer, c, c->axe_buf, sc->axe_bufsz,
1793 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
1794 usbd_transfer(c->axe_xfer);
1795 }
1796
1797 ifp->if_flags |= IFF_RUNNING;
1798 ifp->if_flags &= ~IFF_OACTIVE;
1799
1800 splx(s);
1801
1802 callout_schedule(&sc->axe_stat_ch, hz);
1803 return 0;
1804 }
1805
1806 static int
1807 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1808 {
1809 struct axe_softc *sc = ifp->if_softc;
1810 int s;
1811 int error = 0;
1812
1813 s = splnet();
1814
1815 switch(cmd) {
1816 case SIOCSIFFLAGS:
1817 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1818 break;
1819
1820 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1821 case IFF_RUNNING:
1822 axe_stop(ifp, 1);
1823 break;
1824 case IFF_UP:
1825 axe_init(ifp);
1826 break;
1827 case IFF_UP | IFF_RUNNING:
1828 if ((ifp->if_flags ^ sc->axe_if_flags) == IFF_PROMISC)
1829 axe_setmulti(sc);
1830 else
1831 axe_init(ifp);
1832 break;
1833 }
1834 sc->axe_if_flags = ifp->if_flags;
1835 break;
1836
1837 default:
1838 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
1839 break;
1840
1841 error = 0;
1842
1843 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)
1844 axe_setmulti(sc);
1845
1846 }
1847 splx(s);
1848
1849 return error;
1850 }
1851
1852 static void
1853 axe_watchdog(struct ifnet *ifp)
1854 {
1855 struct axe_softc *sc;
1856 struct axe_chain *c;
1857 usbd_status stat;
1858 int s;
1859
1860 sc = ifp->if_softc;
1861
1862 ifp->if_oerrors++;
1863 aprint_error_dev(sc->axe_dev, "watchdog timeout\n");
1864
1865 s = splusb();
1866 c = &sc->axe_cdata.axe_tx_chain[0];
1867 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1868 axe_txeof(c->axe_xfer, c, stat);
1869
1870 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1871 axe_start(ifp);
1872 splx(s);
1873 }
1874
1875 /*
1876 * Stop the adapter and free any mbufs allocated to the
1877 * RX and TX lists.
1878 */
1879 static void
1880 axe_stop(struct ifnet *ifp, int disable)
1881 {
1882 struct axe_softc *sc = ifp->if_softc;
1883 usbd_status err;
1884 int i;
1885
1886 ifp->if_timer = 0;
1887 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1888
1889 callout_stop(&sc->axe_stat_ch);
1890
1891 /* Stop transfers. */
1892 if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1893 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1894 if (err) {
1895 aprint_error_dev(sc->axe_dev,
1896 "abort rx pipe failed: %s\n", usbd_errstr(err));
1897 }
1898 }
1899
1900 if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1901 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1902 if (err) {
1903 aprint_error_dev(sc->axe_dev,
1904 "abort tx pipe failed: %s\n", usbd_errstr(err));
1905 }
1906 }
1907
1908 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1909 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1910 if (err) {
1911 aprint_error_dev(sc->axe_dev,
1912 "abort intr pipe failed: %s\n", usbd_errstr(err));
1913 }
1914 }
1915
1916 axe_reset(sc);
1917
1918 /* Free RX resources. */
1919 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1920 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1921 usbd_destroy_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1922 sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1923 }
1924 }
1925
1926 /* Free TX resources. */
1927 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1928 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1929 usbd_destroy_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1930 sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1931 }
1932 }
1933
1934 /* Close pipes. */
1935 if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1936 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1937 if (err) {
1938 aprint_error_dev(sc->axe_dev,
1939 "close rx pipe failed: %s\n", usbd_errstr(err));
1940 }
1941 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1942 }
1943
1944 if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1945 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1946 if (err) {
1947 aprint_error_dev(sc->axe_dev,
1948 "close tx pipe failed: %s\n", usbd_errstr(err));
1949 }
1950 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1951 }
1952
1953 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1954 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1955 if (err) {
1956 aprint_error_dev(sc->axe_dev,
1957 "close intr pipe failed: %s\n", usbd_errstr(err));
1958 }
1959 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1960 }
1961
1962 sc->axe_link = 0;
1963 }
1964
1965 MODULE(MODULE_CLASS_DRIVER, if_axe, NULL);
1966
1967 #ifdef _MODULE
1968 #include "ioconf.c"
1969 #endif
1970
1971 static int
1972 if_axe_modcmd(modcmd_t cmd, void *aux)
1973 {
1974 int error = 0;
1975
1976 switch (cmd) {
1977 case MODULE_CMD_INIT:
1978 #ifdef _MODULE
1979 error = config_init_component(cfdriver_ioconf_axe,
1980 cfattach_ioconf_axe, cfdata_ioconf_axe);
1981 #endif
1982 return error;
1983 case MODULE_CMD_FINI:
1984 #ifdef _MODULE
1985 error = config_fini_component(cfdriver_ioconf_axe,
1986 cfattach_ioconf_axe, cfdata_ioconf_axe);
1987 #endif
1988 return error;
1989 default:
1990 return ENOTTY;
1991 }
1992 }
1993