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