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