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