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