if_axe.c revision 1.49 1 /* $NetBSD: if_axe.c,v 1.49 2011/08/25 02:27:31 pgoyette Exp $ */
2 /* $OpenBSD: if_axe.c,v 1.96 2010/01/09 05:33:08 jsg 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 USB 2.0 ethernet driver. Used in the
54 * LinkSys USB200M and various other adapters.
55 *
56 * Manuals available from:
57 * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
58 * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
59 * controller) to find the definitions for the RX control register.
60 * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
61 *
62 * Written by Bill Paul <wpaul (at) windriver.com>
63 * Senior Engineer
64 * Wind River Systems
65 */
66
67 /*
68 * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
69 * It uses an external PHY (reference designs use a RealTek chip),
70 * and has a 64-bit multicast hash filter. There is some information
71 * missing from the manual which one needs to know in order to make
72 * the chip function:
73 *
74 * - You must set bit 7 in the RX control register, otherwise the
75 * chip won't receive any packets.
76 * - You must initialize all 3 IPG registers, or you won't be able
77 * to send any packets.
78 *
79 * Note that this device appears to only support loading the station
80 * address via autload from the EEPROM (i.e. there's no way to manaully
81 * set it).
82 *
83 * (Adam Weinberger wanted me to name this driver if_gir.c.)
84 */
85
86 /*
87 * Ported to OpenBSD 3/28/2004 by Greg Taleck <taleck (at) oz.net>
88 * with bits and pieces from the aue and url drivers.
89 */
90
91 #include <sys/cdefs.h>
92 __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.49 2011/08/25 02:27:31 pgoyette Exp $");
93
94 #if defined(__NetBSD__)
95 #ifndef _MODULE
96 #include "opt_inet.h"
97 #include "rnd.h"
98 #endif
99 #endif
100
101
102 #include <sys/param.h>
103 #include <sys/bus.h>
104 #include <sys/device.h>
105 #include <sys/kernel.h>
106 #include <sys/mbuf.h>
107 #include <sys/module.h>
108 #include <sys/mutex.h>
109 #include <sys/socket.h>
110 #include <sys/sockio.h>
111 #include <sys/systm.h>
112
113 #if NRND > 0
114 #include <sys/rnd.h>
115 #endif
116
117 #include <net/if.h>
118 #include <net/if_dl.h>
119 #include <net/if_ether.h>
120 #include <net/if_media.h>
121
122 #include <net/bpf.h>
123
124 #include <dev/mii/mii.h>
125 #include <dev/mii/miivar.h>
126
127 #include <dev/usb/usb.h>
128 #include <dev/usb/usbdi.h>
129 #include <dev/usb/usbdi_util.h>
130 #include <dev/usb/usbdivar.h>
131 #include <dev/usb/usbdevs.h>
132
133 #include <dev/usb/if_axereg.h>
134
135 #ifdef AXE_DEBUG
136 #define DPRINTF(x) do { if (axedebug) printf x; } while (0)
137 #define DPRINTFN(n,x) do { if (axedebug >= (n)) printf x; } while (0)
138 int axedebug = 0;
139 #else
140 #define DPRINTF(x)
141 #define DPRINTFN(n,x)
142 #endif
143
144 /*
145 * Various supported device vendors/products.
146 */
147 static const struct axe_type axe_devs[] = {
148 { { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE2000}, 0 },
149 { { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 },
150 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET }, AX772 },
151 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 },
152 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772}, AX772 },
153 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772A}, AX772 },
154 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178}, AX178 },
155 { { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T}, 0 },
156 { { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055 }, AX178 },
157 { { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR}, 0},
158 { { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2}, AX772 },
159 { { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
160 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100}, 0 },
161 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1 }, AX772 },
162 { { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
163 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2 }, AX178 },
164 { { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 },
165 { { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M}, 0 },
166 { { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000 }, AX178 },
167 { { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LAN_GTJU2}, AX178 },
168 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2GT}, AX178 },
169 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
170 { { USB_VENDOR_MSI, USB_PRODUCT_MSI_AX88772A}, AX772 },
171 { { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120}, 0 },
172 { { USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS }, AX772 },
173 { { USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T }, AX178 },
174 { { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
175 { { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029}, 0 },
176 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028 }, AX178 }
177 };
178 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
179
180 int axe_match(device_t, cfdata_t, void *);
181 void axe_attach(device_t, device_t, void *);
182 int axe_detach(device_t, int);
183 int axe_activate(device_t, devact_t);
184
185 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc),
186 axe_match, axe_attach, axe_detach, axe_activate);
187
188 static int axe_tx_list_init(struct axe_softc *);
189 static int axe_rx_list_init(struct axe_softc *);
190 static int axe_encap(struct axe_softc *, struct mbuf *, int);
191 static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
192 static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
193 static void axe_tick(void *);
194 static void axe_tick_task(void *);
195 static void axe_start(struct ifnet *);
196 static int axe_ioctl(struct ifnet *, u_long, void *);
197 static int axe_init(struct ifnet *);
198 static void axe_stop(struct ifnet *, int);
199 static void axe_watchdog(struct ifnet *);
200 static int axe_miibus_readreg(device_t, int, int);
201 static void axe_miibus_writereg(device_t, int, int, int);
202 static void axe_miibus_statchg(device_t);
203 static int axe_cmd(struct axe_softc *, int, int, int, void *);
204 static void axe_reset(struct axe_softc *sc);
205 static int axe_ifmedia_upd(struct ifnet *);
206 static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
207
208 static void axe_setmulti(struct axe_softc *);
209 static void axe_lock_mii(struct axe_softc *sc);
210 static void axe_unlock_mii(struct axe_softc *sc);
211
212 static void axe_ax88178_init(struct axe_softc *);
213 static void axe_ax88772_init(struct axe_softc *);
214
215 extern int usbdebug; /* XXX */
216
217 /* Get exclusive access to the MII registers */
218 static void
219 axe_lock_mii(struct axe_softc *sc)
220 {
221
222 sc->axe_refcnt++;
223 mutex_enter(&sc->axe_mii_lock);
224 }
225
226 static void
227 axe_unlock_mii(struct axe_softc *sc)
228 {
229
230 mutex_exit(&sc->axe_mii_lock);
231 if (--sc->axe_refcnt < 0)
232 usb_detach_wakeup((sc->axe_dev));
233 }
234
235 static int
236 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
237 {
238 usb_device_request_t req;
239 usbd_status err;
240
241 KASSERT(mutex_owned(&sc->axe_mii_lock));
242
243 if (sc->axe_dying)
244 return 0;
245
246 if (AXE_CMD_DIR(cmd))
247 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
248 else
249 req.bmRequestType = UT_READ_VENDOR_DEVICE;
250 req.bRequest = AXE_CMD_CMD(cmd);
251 USETW(req.wValue, val);
252 USETW(req.wIndex, index);
253 USETW(req.wLength, AXE_CMD_LEN(cmd));
254
255 err = usbd_do_request(sc->axe_udev, &req, buf);
256
257 if (err) {
258 DPRINTF(("axe_cmd err: cmd %d err %d\n", cmd, err));
259 return -1;
260 }
261 return 0;
262 }
263
264 static int
265 axe_miibus_readreg(device_t dev, int phy, int reg)
266 {
267 struct axe_softc *sc = device_private(dev);
268 usbd_status err;
269 uint16_t val;
270
271 if (sc->axe_dying) {
272 DPRINTF(("axe: dying\n"));
273 return 0;
274 }
275
276 /*
277 * The chip tells us the MII address of any supported
278 * PHYs attached to the chip, so only read from those.
279 *
280 * But if the chip lies about its PHYs, read from any.
281 */
282 val = 0;
283
284 if ((phy == sc->axe_phyaddrs[0]) || (phy == sc->axe_phyaddrs[1]) ||
285 (sc->axe_flags & AXE_ANY_PHY)) {
286 axe_lock_mii(sc);
287 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
288 err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
289 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
290 axe_unlock_mii(sc);
291
292 if (err) {
293 aprint_error_dev(sc->axe_dev, "read PHY failed\n");
294 return -1;
295 }
296 DPRINTF(("axe_miibus_readreg: phy 0x%x reg 0x%x val 0x%x\n",
297 phy, reg, val));
298
299 if (val && val != 0xffff)
300 sc->axe_phyaddrs[0] = phy;
301 } else {
302 DPRINTF(("axe_miibus_readreg: ignore read from phy 0x%x\n",
303 phy));
304 }
305 return le16toh(val);
306 }
307
308 static void
309 axe_miibus_writereg(device_t dev, int phy, int reg, int aval)
310 {
311 struct axe_softc *sc = device_private(dev);
312 usbd_status err;
313 uint16_t val;
314
315 if (sc->axe_dying)
316 return;
317
318 val = htole16(aval);
319 axe_lock_mii(sc);
320 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
321 err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
322 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
323 axe_unlock_mii(sc);
324
325 if (err) {
326 aprint_error_dev(sc->axe_dev, "write PHY failed\n");
327 return;
328 }
329 }
330
331 static void
332 axe_miibus_statchg(device_t dev)
333 {
334 struct axe_softc *sc = device_private(dev);
335 struct mii_data *mii = &sc->axe_mii;
336 int val, err;
337
338 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
339 val = AXE_MEDIA_FULL_DUPLEX;
340 else
341 val = 0;
342
343 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
344 val |= (AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC);
345
346 switch (IFM_SUBTYPE(mii->mii_media_active)) {
347 case IFM_1000_T:
348 val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
349 break;
350 case IFM_100_TX:
351 val |= AXE_178_MEDIA_100TX;
352 break;
353 case IFM_10_T:
354 /* doesn't need to be handled */
355 break;
356 }
357 }
358
359 DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
360 axe_lock_mii(sc);
361 err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
362 axe_unlock_mii(sc);
363 if (err) {
364 aprint_error_dev(sc->axe_dev, "media change failed\n");
365 return;
366 }
367 }
368
369 /*
370 * Set media options
371 */
372 static int
373 axe_ifmedia_upd(struct ifnet *ifp)
374 {
375 struct axe_softc *sc = ifp->if_softc;
376 struct mii_data *mii = &sc->axe_mii;
377 int rc;
378
379 sc->axe_link = 0;
380
381 if (mii->mii_instance) {
382 struct mii_softc *miisc;
383
384 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
385 mii_phy_reset(miisc);
386 }
387
388 if ((rc = mii_mediachg(mii)) == ENXIO)
389 return 0;
390 return rc;
391 }
392
393 /*
394 * Report current media status
395 */
396 static void
397 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
398 {
399 struct axe_softc *sc = ifp->if_softc;
400 struct mii_data *mii = &sc->axe_mii;
401
402 mii_pollstat(mii);
403 ifmr->ifm_active = mii->mii_media_active;
404 ifmr->ifm_status = mii->mii_media_status;
405 }
406
407 static void
408 axe_setmulti(struct axe_softc *sc)
409 {
410 struct ifnet *ifp = &sc->sc_if;
411 struct ether_multi *enm;
412 struct ether_multistep step;
413 uint32_t h = 0;
414 uint16_t rxmode;
415 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
416
417 if (sc->axe_dying)
418 return;
419
420 axe_lock_mii(sc);
421 axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
422 rxmode = le16toh(rxmode);
423
424 rxmode &= ~(AXE_RXCMD_ALLMULTI | AXE_RXCMD_PROMISC);
425
426 /* If we want promiscuous mode, set the allframes bit */
427 if (ifp->if_flags & IFF_PROMISC) {
428 rxmode |= AXE_RXCMD_PROMISC;
429 goto allmulti;
430 }
431
432 /* Now program new ones */
433 ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
434 while (enm != NULL) {
435 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
436 ETHER_ADDR_LEN) != 0)
437 goto allmulti;
438
439 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
440 hashtbl[h >> 3] |= 1U << (h & 7);
441 ETHER_NEXT_MULTI(step, enm);
442 }
443 ifp->if_flags &= ~IFF_ALLMULTI;
444 axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
445 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
446 axe_unlock_mii(sc);
447 return;
448
449 allmulti:
450 ifp->if_flags |= IFF_ALLMULTI;
451 rxmode |= AXE_RXCMD_ALLMULTI;
452 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
453 axe_unlock_mii(sc);
454 }
455
456 static void
457 axe_reset(struct axe_softc *sc)
458 {
459
460 if (sc->axe_dying)
461 return;
462 /* XXX What to reset? */
463
464 /* Wait a little while for the chip to get its brains in order. */
465 DELAY(1000);
466 }
467
468 static void
469 axe_ax88178_init(struct axe_softc *sc)
470 {
471 int gpio0 = 0, phymode = 0;
472 uint16_t eeprom;
473
474 axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
475 /* XXX magic */
476 axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
477 axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
478
479 eeprom = le16toh(eeprom);
480
481 DPRINTF((" EEPROM is 0x%x\n", eeprom));
482
483 /* if EEPROM is invalid we have to use to GPIO0 */
484 if (eeprom == 0xffff) {
485 phymode = 0;
486 gpio0 = 1;
487 } else {
488 phymode = eeprom & 7;
489 gpio0 = (eeprom & 0x80) ? 0 : 1;
490 }
491
492 DPRINTF(("use gpio0: %d, phymode %d\n", gpio0, phymode));
493
494 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x008c, NULL);
495 usbd_delay_ms(sc->axe_udev, 40);
496 if ((eeprom >> 8) != 1) {
497 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
498 usbd_delay_ms(sc->axe_udev, 30);
499
500 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x001c, NULL);
501 usbd_delay_ms(sc->axe_udev, 300);
502
503 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
504 usbd_delay_ms(sc->axe_udev, 30);
505 } else {
506 DPRINTF(("axe gpio phymode == 1 path\n"));
507 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x0004, NULL);
508 usbd_delay_ms(sc->axe_udev, 30);
509 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x000c, NULL);
510 usbd_delay_ms(sc->axe_udev, 30);
511 }
512
513 /* soft reset */
514 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
515 usbd_delay_ms(sc->axe_udev, 150);
516 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
517 AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
518 usbd_delay_ms(sc->axe_udev, 150);
519 /* Enable MII/GMII/RGMII for external PHY */
520 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
521 usbd_delay_ms(sc->axe_udev, 10);
522 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
523 }
524
525 static void
526 axe_ax88772_init(struct axe_softc *sc)
527 {
528
529 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
530 usbd_delay_ms(sc->axe_udev, 40);
531
532 if (sc->axe_phyaddrs[1] == AXE_INTPHY) {
533 /* ask for the embedded PHY */
534 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
535 usbd_delay_ms(sc->axe_udev, 10);
536
537 /* power down and reset state, pin reset state */
538 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
539 usbd_delay_ms(sc->axe_udev, 60);
540
541 /* power down/reset state, pin operating state */
542 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
543 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
544 usbd_delay_ms(sc->axe_udev, 150);
545
546 /* power up, reset */
547 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
548
549 /* power up, operating */
550 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
551 AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
552 } else {
553 /* ask for external PHY */
554 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
555 usbd_delay_ms(sc->axe_udev, 10);
556
557 /* power down internal PHY */
558 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
559 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
560 }
561
562 usbd_delay_ms(sc->axe_udev, 150);
563 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
564 }
565
566 /*
567 * Probe for a AX88172 chip.
568 */
569 int
570 axe_match(device_t parent, cfdata_t match, void *aux)
571 {
572 struct usb_attach_arg *uaa = aux;
573
574 return axe_lookup(uaa->vendor, uaa->product) != NULL ?
575 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
576 }
577
578 /*
579 * Attach the interface. Allocate softc structures, do ifmedia
580 * setup and ethernet/BPF attach.
581 */
582 void
583 axe_attach(device_t parent, device_t self, void *aux)
584 {
585 struct axe_softc *sc = device_private(self);
586 struct usb_attach_arg *uaa = aux;
587 usbd_device_handle dev = uaa->device;
588 usbd_status err;
589 usb_interface_descriptor_t *id;
590 usb_endpoint_descriptor_t *ed;
591 struct mii_data *mii;
592 uint8_t eaddr[ETHER_ADDR_LEN];
593 char *devinfop;
594 const char *devname = device_xname(self);
595 struct ifnet *ifp;
596 int i, s;
597
598 aprint_naive("\n");
599 aprint_normal("\n");
600
601 sc->axe_dev = self;
602 sc->axe_udev = dev;
603
604 devinfop = usbd_devinfo_alloc(dev, 0);
605 aprint_normal_dev(self, "%s\n", devinfop);
606 usbd_devinfo_free(devinfop);
607
608 err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
609 if (err) {
610 aprint_error_dev(self, "getting interface handle failed\n");
611 return;
612 }
613
614 sc->axe_flags = axe_lookup(uaa->vendor, uaa->product)->axe_flags;
615
616 mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE);
617 usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
618
619 err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
620 if (err) {
621 aprint_error_dev(self, "getting interface handle failed\n");
622 return;
623 }
624
625 sc->axe_product = uaa->product;
626 sc->axe_vendor = uaa->vendor;
627
628 id = usbd_get_interface_descriptor(sc->axe_iface);
629
630 /* decide on what our bufsize will be */
631 if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
632 sc->axe_bufsz = (sc->axe_udev->speed == USB_SPEED_HIGH) ?
633 AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ;
634 else
635 sc->axe_bufsz = AXE_172_BUFSZ;
636
637 /* Find endpoints. */
638 for (i = 0; i < id->bNumEndpoints; i++) {
639 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
640 if (ed == NULL) {
641 aprint_error_dev(self, "couldn't get ep %d\n", i);
642 return;
643 }
644 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
645 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
646 sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
647 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
648 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
649 sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
650 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
651 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
652 sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
653 }
654 }
655
656 s = splnet();
657
658 /* We need the PHYID for init dance in some cases */
659 axe_lock_mii(sc);
660 axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
661
662 DPRINTF((" phyaddrs[0]: %x phyaddrs[1]: %x\n",
663 sc->axe_phyaddrs[0], sc->axe_phyaddrs[1]));
664
665 if (sc->axe_flags & AX178)
666 axe_ax88178_init(sc);
667 else if (sc->axe_flags & AX772)
668 axe_ax88772_init(sc);
669
670 /*
671 * Get station address.
672 */
673 if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
674 axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, &eaddr);
675 else
676 axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, &eaddr);
677
678 /*
679 * Load IPG values
680 */
681 axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
682 axe_unlock_mii(sc);
683
684 /*
685 * An ASIX chip was detected. Inform the world.
686 */
687 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
688
689 /* Initialize interface info.*/
690 ifp = &sc->sc_if;
691 ifp->if_softc = sc;
692 strncpy(ifp->if_xname, devname, IFNAMSIZ);
693 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
694 ifp->if_ioctl = axe_ioctl;
695 ifp->if_start = axe_start;
696 ifp->if_init = axe_init;
697 ifp->if_stop = axe_stop;
698 ifp->if_watchdog = axe_watchdog;
699
700 IFQ_SET_READY(&ifp->if_snd);
701
702 sc->axe_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
703
704 /* Initialize MII/media info. */
705 mii = &sc->axe_mii;
706 mii->mii_ifp = ifp;
707 mii->mii_readreg = axe_miibus_readreg;
708 mii->mii_writereg = axe_miibus_writereg;
709 mii->mii_statchg = axe_miibus_statchg;
710 mii->mii_flags = MIIF_AUTOTSLEEP;
711
712 sc->axe_ec.ec_mii = mii;
713 if (sc->axe_flags & AXE_MII)
714 ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd,
715 axe_ifmedia_sts);
716 else
717 ifmedia_init(&mii->mii_media, 0, ether_mediachange,
718 ether_mediastatus);
719
720 mii_attach(sc->axe_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY,
721 0);
722
723 if (LIST_EMPTY(&mii->mii_phys)) {
724 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
725 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
726 } else
727 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
728
729 /* Attach the interface. */
730 if_attach(ifp);
731 ether_ifattach(ifp, eaddr);
732 #if NRND > 0
733 rnd_attach_source(&sc->rnd_source, device_xname(sc->axe_dev),
734 RND_TYPE_NET, 0);
735 #endif
736
737 callout_init(&sc->axe_stat_ch, 0);
738 callout_setfunc(&sc->axe_stat_ch, axe_tick, sc);
739
740 sc->axe_attached = true;
741 splx(s);
742
743 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev, sc->axe_dev);
744 }
745
746 int
747 axe_detach(device_t self, int flags)
748 {
749 struct axe_softc *sc = device_private(self);
750 int s;
751 struct ifnet *ifp = &sc->sc_if;
752
753 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axe_dev), __func__));
754
755 /* Detached before attached finished, so just bail out. */
756 if (!sc->axe_attached)
757 return 0;
758
759 sc->axe_dying = true;
760
761 /*
762 * Remove any pending tasks. They cannot be executing because they run
763 * in the same thread as detach.
764 */
765 usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
766
767 s = splusb();
768
769 if (ifp->if_flags & IFF_RUNNING)
770 axe_stop(ifp, 1);
771
772 callout_destroy(&sc->axe_stat_ch);
773 mutex_destroy(&sc->axe_mii_lock);
774 #if NRND > 0
775 rnd_detach_source(&sc->rnd_source);
776 #endif
777 mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
778 ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
779 ether_ifdetach(ifp);
780 if_detach(ifp);
781
782 #ifdef DIAGNOSTIC
783 if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
784 sc->axe_ep[AXE_ENDPT_RX] != NULL ||
785 sc->axe_ep[AXE_ENDPT_INTR] != NULL)
786 aprint_debug_dev(self, "detach has active endpoints\n");
787 #endif
788
789 sc->axe_attached = false;
790
791 if (--sc->axe_refcnt >= 0) {
792 /* Wait for processes to go away. */
793 usb_detach_wait((sc->axe_dev));
794 }
795 splx(s);
796
797 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev, sc->axe_dev);
798
799 return 0;
800 }
801
802 int
803 axe_activate(device_t self, devact_t act)
804 {
805 struct axe_softc *sc = device_private(self);
806
807 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axe_dev), __func__));
808
809 switch (act) {
810 case DVACT_DEACTIVATE:
811 if_deactivate(&sc->axe_ec.ec_if);
812 sc->axe_dying = true;
813 return 0;
814 default:
815 return EOPNOTSUPP;
816 }
817 }
818
819 static int
820 axe_rx_list_init(struct axe_softc *sc)
821 {
822 struct axe_cdata *cd;
823 struct axe_chain *c;
824 int i;
825
826 DPRINTF(("%s: %s: enter\n", device_xname(sc->axe_dev), __func__));
827
828 cd = &sc->axe_cdata;
829 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
830 c = &cd->axe_rx_chain[i];
831 c->axe_sc = sc;
832 c->axe_idx = i;
833 if (c->axe_xfer == NULL) {
834 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
835 if (c->axe_xfer == NULL)
836 return ENOBUFS;
837 c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
838 sc->axe_bufsz);
839 if (c->axe_buf == NULL) {
840 usbd_free_xfer(c->axe_xfer);
841 return ENOBUFS;
842 }
843 }
844 }
845
846 return 0;
847 }
848
849 static int
850 axe_tx_list_init(struct axe_softc *sc)
851 {
852 struct axe_cdata *cd;
853 struct axe_chain *c;
854 int i;
855
856 DPRINTF(("%s: %s: enter\n", device_xname(sc->axe_dev), __func__));
857
858 cd = &sc->axe_cdata;
859 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
860 c = &cd->axe_tx_chain[i];
861 c->axe_sc = sc;
862 c->axe_idx = i;
863 if (c->axe_xfer == NULL) {
864 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
865 if (c->axe_xfer == NULL)
866 return ENOBUFS;
867 c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
868 sc->axe_bufsz);
869 if (c->axe_buf == NULL) {
870 usbd_free_xfer(c->axe_xfer);
871 return ENOBUFS;
872 }
873 }
874 }
875
876 return 0;
877 }
878
879 /*
880 * A frame has been uploaded: pass the resulting mbuf chain up to
881 * the higher level protocols.
882 */
883 static void
884 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
885 {
886 struct axe_softc *sc;
887 struct axe_chain *c;
888 struct ifnet *ifp;
889 uint8_t *buf;
890 uint32_t total_len;
891 u_int rxlen, pktlen;
892 struct mbuf *m;
893 struct axe_sframe_hdr hdr;
894 int s;
895
896 c = (struct axe_chain *)priv;
897 sc = c->axe_sc;
898 buf = c->axe_buf;
899 ifp = &sc->sc_if;
900
901 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->axe_dev),__func__));
902
903 if (sc->axe_dying)
904 return;
905
906 if ((ifp->if_flags & IFF_RUNNING) == 0)
907 return;
908
909 if (status != USBD_NORMAL_COMPLETION) {
910 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
911 return;
912 if (usbd_ratecheck(&sc->axe_rx_notice))
913 aprint_error_dev(sc->axe_dev, "usb errors on rx: %s\n",
914 usbd_errstr(status));
915 if (status == USBD_STALLED)
916 usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
917 goto done;
918 }
919
920 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
921
922 do {
923 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
924 if (total_len < sizeof(hdr)) {
925 ifp->if_ierrors++;
926 goto done;
927 }
928
929 memcpy(&hdr, buf, sizeof(hdr));
930 total_len -= sizeof(hdr);
931 buf += sizeof(hdr);
932
933 if ((hdr.len ^ hdr.ilen) != 0xffff) {
934 ifp->if_ierrors++;
935 goto done;
936 }
937
938 rxlen = le16toh(hdr.len);
939 if (total_len < rxlen) {
940 pktlen = total_len;
941 total_len = 0;
942 } else {
943 pktlen = rxlen;
944 rxlen = roundup2(rxlen, 2);
945 total_len -= rxlen;
946 }
947
948 } else { /* AX172 */
949 pktlen = rxlen = total_len;
950 total_len = 0;
951 }
952
953 MGETHDR(m, M_DONTWAIT, MT_DATA);
954 if (m == NULL) {
955 ifp->if_ierrors++;
956 goto done;
957 }
958
959 if (pktlen > MHLEN - ETHER_ALIGN) {
960 MCLGET(m, M_DONTWAIT);
961 if ((m->m_flags & M_EXT) == 0) {
962 m_freem(m);
963 ifp->if_ierrors++;
964 goto done;
965 }
966 }
967 m->m_data += ETHER_ALIGN;
968
969 ifp->if_ipackets++;
970 m->m_pkthdr.rcvif = ifp;
971 m->m_pkthdr.len = m->m_len = pktlen;
972
973 memcpy(mtod(m, uint8_t *), buf, pktlen);
974 buf += rxlen;
975
976 s = splnet();
977
978 bpf_mtap(ifp, m);
979
980 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->axe_dev),
981 __func__, m->m_len));
982 (*(ifp)->if_input)((ifp), (m));
983
984 splx(s);
985
986 } while (total_len > 0);
987
988 done:
989
990 /* Setup new transfer. */
991 usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
992 c, c->axe_buf, sc->axe_bufsz,
993 USBD_SHORT_XFER_OK | USBD_NO_COPY,
994 USBD_NO_TIMEOUT, axe_rxeof);
995 usbd_transfer(xfer);
996
997 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->axe_dev), __func__));
998 }
999
1000 /*
1001 * A frame was downloaded to the chip. It's safe for us to clean up
1002 * the list buffers.
1003 */
1004
1005 static void
1006 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1007 {
1008 struct axe_softc *sc;
1009 struct axe_chain *c;
1010 struct ifnet *ifp;
1011 int s;
1012
1013 c = priv;
1014 sc = c->axe_sc;
1015 ifp = &sc->sc_if;
1016
1017 if (sc->axe_dying)
1018 return;
1019
1020 s = splnet();
1021
1022 if (status != USBD_NORMAL_COMPLETION) {
1023 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1024 splx(s);
1025 return;
1026 }
1027 ifp->if_oerrors++;
1028 aprint_error_dev(sc->axe_dev, "usb error on tx: %s\n",
1029 usbd_errstr(status));
1030 if (status == USBD_STALLED)
1031 usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
1032 splx(s);
1033 return;
1034 }
1035
1036 ifp->if_timer = 0;
1037 ifp->if_flags &= ~IFF_OACTIVE;
1038
1039 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1040 axe_start(ifp);
1041
1042 ifp->if_opackets++;
1043 splx(s);
1044 }
1045
1046 static void
1047 axe_tick(void *xsc)
1048 {
1049 struct axe_softc *sc = xsc;
1050
1051 if (sc == NULL)
1052 return;
1053
1054 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->axe_dev), __func__));
1055
1056 if (sc->axe_dying)
1057 return;
1058
1059 /* Perform periodic stuff in process context */
1060 usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
1061 }
1062
1063 static void
1064 axe_tick_task(void *xsc)
1065 {
1066 int s;
1067 struct axe_softc *sc;
1068 struct ifnet *ifp;
1069 struct mii_data *mii;
1070
1071 sc = xsc;
1072
1073 if (sc == NULL)
1074 return;
1075
1076 if (sc->axe_dying)
1077 return;
1078
1079 ifp = &sc->sc_if;
1080 mii = &sc->axe_mii;
1081
1082 if (mii == NULL)
1083 return;
1084
1085 s = splnet();
1086
1087 mii_tick(mii);
1088 if (sc->axe_link == 0 &&
1089 (mii->mii_media_status & IFM_ACTIVE) != 0 &&
1090 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1091 DPRINTF(("%s: %s: got link\n", device_xname(sc->axe_dev),
1092 __func__));
1093 sc->axe_link++;
1094 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1095 axe_start(ifp);
1096 }
1097
1098 callout_schedule(&sc->axe_stat_ch, hz);
1099
1100 splx(s);
1101 }
1102
1103 static int
1104 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
1105 {
1106 struct ifnet *ifp = &sc->sc_if;
1107 struct axe_chain *c;
1108 usbd_status err;
1109 struct axe_sframe_hdr hdr;
1110 int length, boundary;
1111
1112 c = &sc->axe_cdata.axe_tx_chain[idx];
1113
1114 /*
1115 * Copy the mbuf data into a contiguous buffer, leaving two
1116 * bytes at the beginning to hold the frame length.
1117 */
1118 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1119 boundary = (sc->axe_udev->speed == USB_SPEED_HIGH) ? 512 : 64;
1120
1121 hdr.len = htole16(m->m_pkthdr.len);
1122 hdr.ilen = ~hdr.len;
1123
1124 memcpy(c->axe_buf, &hdr, sizeof(hdr));
1125 length = sizeof(hdr);
1126
1127 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length);
1128 length += m->m_pkthdr.len;
1129
1130 if ((length % boundary) == 0) {
1131 hdr.len = 0x0000;
1132 hdr.ilen = 0xffff;
1133 memcpy(c->axe_buf + length, &hdr, sizeof(hdr));
1134 length += sizeof(hdr);
1135 }
1136 } else {
1137 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
1138 length = m->m_pkthdr.len;
1139 }
1140
1141 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
1142 c, c->axe_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 10000,
1143 axe_txeof);
1144
1145 /* Transmit */
1146 err = usbd_transfer(c->axe_xfer);
1147 if (err != USBD_IN_PROGRESS) {
1148 axe_stop(ifp, 0);
1149 return EIO;
1150 }
1151
1152 sc->axe_cdata.axe_tx_cnt++;
1153
1154 return 0;
1155 }
1156
1157 static void
1158 axe_start(struct ifnet *ifp)
1159 {
1160 struct axe_softc *sc;
1161 struct mbuf *m;
1162
1163 sc = ifp->if_softc;
1164
1165 if ((sc->axe_flags & AXE_MII) != 0 && sc->axe_link == 0)
1166 return;
1167
1168 if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
1169 return;
1170
1171 IFQ_POLL(&ifp->if_snd, m);
1172 if (m == NULL) {
1173 return;
1174 }
1175
1176 if (axe_encap(sc, m, 0)) {
1177 ifp->if_flags |= IFF_OACTIVE;
1178 return;
1179 }
1180 IFQ_DEQUEUE(&ifp->if_snd, m);
1181
1182 /*
1183 * If there's a BPF listener, bounce a copy of this frame
1184 * to him.
1185 */
1186 bpf_mtap(ifp, m);
1187 m_freem(m);
1188
1189 ifp->if_flags |= IFF_OACTIVE;
1190
1191 /*
1192 * Set a timeout in case the chip goes out to lunch.
1193 */
1194 ifp->if_timer = 5;
1195
1196 return;
1197 }
1198
1199 static int
1200 axe_init(struct ifnet *ifp)
1201 {
1202 struct axe_softc *sc = ifp->if_softc;
1203 struct axe_chain *c;
1204 usbd_status err;
1205 int rxmode;
1206 int i, s;
1207 uint8_t eaddr[ETHER_ADDR_LEN];
1208
1209 s = splnet();
1210
1211 if (ifp->if_flags & IFF_RUNNING)
1212 axe_stop(ifp, 0);
1213
1214 /*
1215 * Cancel pending I/O and free all RX/TX buffers.
1216 */
1217 axe_reset(sc);
1218
1219 /* Set MAC address */
1220 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1221 memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
1222 axe_lock_mii(sc);
1223 axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, eaddr);
1224 axe_unlock_mii(sc);
1225 }
1226
1227 /* Enable RX logic. */
1228
1229 /* Init RX ring. */
1230 if (axe_rx_list_init(sc) == ENOBUFS) {
1231 aprint_error_dev(sc->axe_dev, "rx list init failed\n");
1232 splx(s);
1233 return ENOBUFS;
1234 }
1235
1236 /* Init TX ring. */
1237 if (axe_tx_list_init(sc) == ENOBUFS) {
1238 aprint_error_dev(sc->axe_dev, "tx list init failed\n");
1239 splx(s);
1240 return ENOBUFS;
1241 }
1242
1243 /* Set transmitter IPG values */
1244 axe_lock_mii(sc);
1245 if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
1246 axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2],
1247 (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL);
1248 else {
1249 axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1250 axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1251 axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1252 }
1253
1254 /* Enable receiver, set RX mode */
1255 rxmode = AXE_RXCMD_BROADCAST | AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE;
1256 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1257 if (sc->axe_udev->speed == USB_SPEED_HIGH) {
1258 /* Largest possible USB buffer size for AX88178 */
1259 rxmode |= AXE_178_RXCMD_MFB;
1260 }
1261 } else
1262 rxmode |= AXE_172_RXCMD_UNICAST;
1263
1264 /* If we want promiscuous mode, set the allframes bit. */
1265 if (ifp->if_flags & IFF_PROMISC)
1266 rxmode |= AXE_RXCMD_PROMISC;
1267
1268 if (ifp->if_flags & IFF_BROADCAST)
1269 rxmode |= AXE_RXCMD_BROADCAST;
1270
1271 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1272 axe_unlock_mii(sc);
1273
1274 /* Load the multicast filter. */
1275 axe_setmulti(sc);
1276
1277 /* Open RX and TX pipes. */
1278 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1279 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1280 if (err) {
1281 aprint_error_dev(sc->axe_dev, "open rx pipe failed: %s\n",
1282 usbd_errstr(err));
1283 splx(s);
1284 return EIO;
1285 }
1286
1287 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1288 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1289 if (err) {
1290 aprint_error_dev(sc->axe_dev, "open tx pipe failed: %s\n",
1291 usbd_errstr(err));
1292 splx(s);
1293 return EIO;
1294 }
1295
1296 /* Start up the receive pipe. */
1297 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1298 c = &sc->axe_cdata.axe_rx_chain[i];
1299 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
1300 c, c->axe_buf, sc->axe_bufsz,
1301 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1302 axe_rxeof);
1303 usbd_transfer(c->axe_xfer);
1304 }
1305
1306 ifp->if_flags |= IFF_RUNNING;
1307 ifp->if_flags &= ~IFF_OACTIVE;
1308
1309 splx(s);
1310
1311 callout_schedule(&sc->axe_stat_ch, hz);
1312 return 0;
1313 }
1314
1315 static int
1316 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1317 {
1318 struct axe_softc *sc = ifp->if_softc;
1319 int s;
1320 int error = 0;
1321
1322 s = splnet();
1323
1324 switch(cmd) {
1325 case SIOCSIFFLAGS:
1326 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1327 break;
1328
1329 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1330 case IFF_RUNNING:
1331 axe_stop(ifp, 1);
1332 break;
1333 case IFF_UP:
1334 axe_init(ifp);
1335 break;
1336 case IFF_UP | IFF_RUNNING:
1337 if ((ifp->if_flags ^ sc->axe_if_flags) == IFF_PROMISC)
1338 axe_setmulti(sc);
1339 else
1340 axe_init(ifp);
1341 break;
1342 }
1343 sc->axe_if_flags = ifp->if_flags;
1344 break;
1345
1346 default:
1347 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
1348 break;
1349
1350 error = 0;
1351
1352 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)
1353 axe_setmulti(sc);
1354
1355 }
1356 splx(s);
1357
1358 return error;
1359 }
1360
1361 static void
1362 axe_watchdog(struct ifnet *ifp)
1363 {
1364 struct axe_softc *sc;
1365 struct axe_chain *c;
1366 usbd_status stat;
1367 int s;
1368
1369 sc = ifp->if_softc;
1370
1371 ifp->if_oerrors++;
1372 aprint_error_dev(sc->axe_dev, "watchdog timeout\n");
1373
1374 s = splusb();
1375 c = &sc->axe_cdata.axe_tx_chain[0];
1376 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1377 axe_txeof(c->axe_xfer, c, stat);
1378
1379 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1380 axe_start(ifp);
1381 splx(s);
1382 }
1383
1384 /*
1385 * Stop the adapter and free any mbufs allocated to the
1386 * RX and TX lists.
1387 */
1388 static void
1389 axe_stop(struct ifnet *ifp, int disable)
1390 {
1391 struct axe_softc *sc = ifp->if_softc;
1392 usbd_status err;
1393 int i;
1394
1395 axe_reset(sc);
1396
1397 ifp->if_timer = 0;
1398 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1399
1400 callout_stop(&sc->axe_stat_ch);
1401
1402 /* Stop transfers. */
1403 if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1404 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1405 if (err) {
1406 aprint_error_dev(sc->axe_dev,
1407 "abort rx pipe failed: %s\n", usbd_errstr(err));
1408 }
1409 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1410 if (err) {
1411 aprint_error_dev(sc->axe_dev,
1412 "close rx pipe failed: %s\n", usbd_errstr(err));
1413 }
1414 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1415 }
1416
1417 if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1418 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1419 if (err) {
1420 aprint_error_dev(sc->axe_dev,
1421 "abort tx pipe failed: %s\n", usbd_errstr(err));
1422 }
1423 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1424 if (err) {
1425 aprint_error_dev(sc->axe_dev,
1426 "close tx pipe failed: %s\n", usbd_errstr(err));
1427 }
1428 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1429 }
1430
1431 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1432 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1433 if (err) {
1434 aprint_error_dev(sc->axe_dev,
1435 "abort intr pipe failed: %s\n", usbd_errstr(err));
1436 }
1437 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1438 if (err) {
1439 aprint_error_dev(sc->axe_dev,
1440 "close intr pipe failed: %s\n", usbd_errstr(err));
1441 }
1442 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1443 }
1444
1445 /* Free RX resources. */
1446 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1447 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1448 usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1449 sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1450 }
1451 }
1452
1453 /* Free TX resources. */
1454 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1455 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1456 usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1457 sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1458 }
1459 }
1460
1461 sc->axe_link = 0;
1462 }
1463
1464 MODULE(MODULE_CLASS_DRIVER, if_axe, NULL);
1465
1466 #ifdef _MODULE
1467 #include "ioconf.c"
1468 #endif
1469
1470 static int
1471 if_axe_modcmd(modcmd_t cmd, void *aux)
1472 {
1473 int error = 0;
1474
1475 switch (cmd) {
1476 case MODULE_CMD_INIT:
1477 #ifdef _MODULE
1478 usbdebug++; /* XXX */
1479 error = config_init_component(cfdriver_ioconf_axe,
1480 cfattach_ioconf_axe, cfdata_ioconf_axe);
1481 usbdebug--; /* XXX */
1482 #endif
1483 return error;
1484 case MODULE_CMD_FINI:
1485 #ifdef _MODULE
1486 error = config_fini_component(cfdriver_ioconf_axe,
1487 cfattach_ioconf_axe, cfdata_ioconf_axe);
1488 #endif
1489 return error;
1490 default:
1491 return ENOTTY;
1492 }
1493 }
1494