xref: /openbsd/sys/dev/usb/if_axe.c (revision 91f110e0)
1 /*	$OpenBSD: if_axe.c,v 1.123 2013/11/15 10:17:39 pirofti 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 /*
52  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
53  * LinkSys USB200M and various other adapters.
54  *
55  * Manuals available from:
56  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
57  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
58  * controller) to find the definitions for the RX control register.
59  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
60  *
61  * Written by Bill Paul <wpaul@windriver.com>
62  * Senior Engineer
63  * Wind River Systems
64  */
65 
66 /*
67  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
68  * It uses an external PHY (reference designs use a RealTek chip),
69  * and has a 64-bit multicast hash filter. There is some information
70  * missing from the manual which one needs to know in order to make
71  * the chip function:
72  *
73  * - You must set bit 7 in the RX control register, otherwise the
74  *   chip won't receive any packets.
75  * - You must initialize all 3 IPG registers, or you won't be able
76  *   to send any packets.
77  *
78  * Note that this device appears to only support loading the station
79  * address via autoload from the EEPROM (i.e. there's no way to manually
80  * set it).
81  *
82  * (Adam Weinberger wanted me to name this driver if_gir.c.)
83  */
84 
85 /*
86  * Ported to OpenBSD 3/28/2004 by Greg Taleck <taleck@oz.net>
87  * with bits and pieces from the aue and url drivers.
88  */
89 
90 #include "bpfilter.h"
91 
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/sockio.h>
95 #include <sys/rwlock.h>
96 #include <sys/mbuf.h>
97 #include <sys/kernel.h>
98 #include <sys/socket.h>
99 
100 #include <sys/device.h>
101 
102 #include <machine/bus.h>
103 
104 #include <net/if.h>
105 #include <net/if_dl.h>
106 #include <net/if_media.h>
107 
108 #if NBPFILTER > 0
109 #include <net/bpf.h>
110 #endif
111 
112 #ifdef INET
113 #include <netinet/in.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/ip.h>
116 #include <netinet/if_ether.h>
117 #endif
118 
119 #include <dev/mii/mii.h>
120 #include <dev/mii/miivar.h>
121 
122 #include <dev/usb/usb.h>
123 #include <dev/usb/usbdi.h>
124 #include <dev/usb/usbdi_util.h>
125 #include <dev/usb/usbdivar.h>
126 #include <dev/usb/usbdevs.h>
127 
128 #include <dev/usb/if_axereg.h>
129 
130 #ifdef AXE_DEBUG
131 #define DPRINTF(x)	do { if (axedebug) printf x; } while (0)
132 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) printf x; } while (0)
133 int	axedebug = 0;
134 #else
135 #define DPRINTF(x)
136 #define DPRINTFN(n,x)
137 #endif
138 
139 /*
140  * Various supported device vendors/products.
141  */
142 const struct axe_type axe_devs[] = {
143 	{ { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UF200}, 0 },
144 	{ { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 },
145 	{ { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET }, AX772 },
146 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 },
147 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772}, AX772 },
148 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772A}, AX772 },
149 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B}, AX772 | AX772B },
150 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B_1}, AX772 | AX772B },
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_DLINK, USB_PRODUCT_DLINK_DUBE100C1 }, AX772 | AX772B },
160 	{ { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
161 	{ { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2 }, AX178 },
162 	{ { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 },
163 	{ { USB_VENDOR_LENOVO, USB_PRODUCT_LENOVO_ETHERNET }, AX772 | AX772B },
164 	{ { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_HG20F9}, AX772 | AX772B },
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(struct usbd_xfer *, void *, usbd_status);
203 void axe_txeof(struct usbd_xfer *, void *, 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_iff(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 	ifp = GET_IFP(sc);
364 	if (mii == NULL || ifp == NULL ||
365 	    (ifp->if_flags & IFF_RUNNING) == 0)
366 		return;
367 
368 	sc->axe_link = 0;
369 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
370 	    (IFM_ACTIVE | IFM_AVALID)) {
371 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
372 		    case IFM_10_T:
373 		    case IFM_100_TX:
374 			sc->axe_link++;
375 			break;
376 		    case IFM_1000_T:
377 			if ((sc->axe_flags & AX178) == 0)
378 			    break;
379 			sc->axe_link++;
380 			break;
381 		    default:
382 			break;
383 		}
384 	}
385 
386 	/* Lost link, do nothing. */
387 	if (sc->axe_link == 0)
388 		return;
389 
390 	val = 0;
391 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
392 		val |= AXE_MEDIA_FULL_DUPLEX;
393 
394 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
395 		val |= (AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC);
396 		if (sc->axe_flags & AX178)
397 			val |= AXE_178_MEDIA_ENCK;
398 
399 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
400 		case IFM_1000_T:
401 			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
402 			break;
403 		case IFM_100_TX:
404 			val |= AXE_178_MEDIA_100TX;
405 			break;
406 		case IFM_10_T:
407 			/* doesn't need to be handled */
408 			break;
409 		}
410 	}
411 
412 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
413 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
414 	if (err) {
415 		printf("%s: media change failed\n", sc->axe_dev.dv_xname);
416 		return;
417 	}
418 }
419 
420 /*
421  * Set media options.
422  */
423 int
424 axe_ifmedia_upd(struct ifnet *ifp)
425 {
426 	struct axe_softc	*sc = ifp->if_softc;
427 	struct mii_data		*mii = GET_MII(sc);
428 
429 	if (mii->mii_instance) {
430 		struct mii_softc	*miisc;
431 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
432 			mii_phy_reset(miisc);
433 	}
434 	mii_mediachg(mii);
435 
436 	return (0);
437 }
438 
439 /*
440  * Report current media status.
441  */
442 void
443 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
444 {
445 	struct axe_softc	*sc = ifp->if_softc;
446 	struct mii_data		*mii = GET_MII(sc);
447 
448 	mii_pollstat(mii);
449 	ifmr->ifm_active = mii->mii_media_active;
450 	ifmr->ifm_status = mii->mii_media_status;
451 }
452 
453 void
454 axe_iff(struct axe_softc *sc)
455 {
456 	struct ifnet		*ifp = GET_IFP(sc);
457 	struct arpcom		*ac = &sc->arpcom;
458 	struct ether_multi *enm;
459 	struct ether_multistep step;
460 	u_int32_t		h = 0;
461 	uWord			urxmode;
462 	u_int16_t		rxmode;
463 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
464 
465 	if (usbd_is_dying(sc->axe_udev))
466 		return;
467 
468 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, urxmode);
469 	rxmode = UGETW(urxmode);
470 	rxmode &= ~(AXE_RXCMD_ALLMULTI | AXE_RXCMD_MULTICAST |
471 	    AXE_RXCMD_PROMISC);
472 	ifp->if_flags &= ~IFF_ALLMULTI;
473 
474 	/*
475 	 * Always accept broadcast frames.
476 	 * Always accept frames destined to our station address.
477 	 */
478 	rxmode |= AXE_RXCMD_BROADCAST;
479 	if (!sc->axe_flags & AX178 && !sc->axe_flags & AX772)
480 		rxmode |= AXE_172_RXCMD_UNICAST;
481 
482 	if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) {
483 		ifp->if_flags |= IFF_ALLMULTI;
484 		rxmode |= AXE_RXCMD_ALLMULTI;
485 		if (ifp->if_flags & IFF_PROMISC)
486 			rxmode |= AXE_RXCMD_PROMISC;
487 	} else {
488 		rxmode |= AXE_RXCMD_MULTICAST;
489 
490 		/* now program new ones */
491 		ETHER_FIRST_MULTI(step, ac, enm);
492 		while (enm != NULL) {
493 			h = ether_crc32_be(enm->enm_addrlo,
494 			    ETHER_ADDR_LEN) >> 26;
495 
496 			hashtbl[h / 8] |= 1 << (h % 8);
497 
498 			ETHER_NEXT_MULTI(step, enm);
499 		}
500 	}
501 
502 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
503 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
504 }
505 
506 void
507 axe_reset(struct axe_softc *sc)
508 {
509 	if (usbd_is_dying(sc->axe_udev))
510 		return;
511 	/* XXX What to reset? */
512 
513 	/* Wait a little while for the chip to get its brains in order. */
514 	DELAY(1000);
515 	return;
516 }
517 
518 #define AXE_GPIO_WRITE(x,y) do {                                \
519 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL);          \
520 	usbd_delay_ms(sc->axe_udev, (y));			\
521 } while (0)
522 
523 void
524 axe_ax88178_init(struct axe_softc *sc)
525 {
526 	int gpio0 = 0, phymode = 0, ledmode;
527 	u_int16_t eeprom, val;
528 
529 	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
530 	/* XXX magic */
531 	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
532 	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
533 
534 	eeprom = letoh16(eeprom);
535 
536 	DPRINTF((" EEPROM is 0x%x\n", eeprom));
537 
538 	/* if EEPROM is invalid we have to use to GPIO0 */
539 	if (eeprom == 0xffff) {
540 		phymode = AXE_PHY_MODE_MARVELL;
541 		gpio0 = 1;
542 		ledmode = 0;
543 	} else {
544 		phymode = eeprom & 0x7f;
545 		gpio0 = (eeprom & 0x80) ? 0 : 1;
546 		ledmode = eeprom >> 8;
547 	}
548 
549 	DPRINTF(("use gpio0: %d, phymode 0x%02x, eeprom 0x%04x\n",
550 	    gpio0, phymode, eeprom));
551 
552 	/* power up external phy */
553 	AXE_GPIO_WRITE(AXE_GPIO1|AXE_GPIO1_EN | AXE_GPIO_RELOAD_EEPROM, 40);
554 	if (ledmode == 1) {
555 		AXE_GPIO_WRITE(AXE_GPIO1_EN, 30);
556 		AXE_GPIO_WRITE(AXE_GPIO1_EN | AXE_GPIO1, 30);
557 	} else {
558 		val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
559 	    	    AXE_GPIO1 | AXE_GPIO1_EN;
560 		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, 30);
561 		AXE_GPIO_WRITE(val | AXE_GPIO2_EN, 300);
562 		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, 30);
563 	}
564 
565 	/* initialize phy */
566 	if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
567 		axe_miibus_writereg(&sc->axe_dev, sc->axe_phyno, 0x1f, 0x0005);
568 		axe_miibus_writereg(&sc->axe_dev, sc->axe_phyno, 0x0c, 0x0000);
569 		val = axe_miibus_readreg(&sc->axe_dev, sc->axe_phyno, 0x0001);
570 		axe_miibus_writereg(&sc->axe_dev, sc->axe_phyno, 0x01,
571 		    val | 0x0080);
572 		axe_miibus_writereg(&sc->axe_dev, sc->axe_phyno, 0x1f, 0x0000);
573 	}
574 
575 	/* soft reset */
576 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
577 	usbd_delay_ms(sc->axe_udev, 150);
578 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
579 	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
580 	usbd_delay_ms(sc->axe_udev, 150);
581 	/* Enable MII/GMII/RGMII for external PHY */
582 	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
583 	usbd_delay_ms(sc->axe_udev, 10);
584 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
585 }
586 
587 void
588 axe_ax88772_init(struct axe_softc *sc)
589 {
590 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
591 	usbd_delay_ms(sc->axe_udev, 40);
592 
593 	if (sc->axe_phyno == AXE_PHY_NO_AX772_EPHY) {
594 		/* ask for the embedded PHY */
595 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
596 		usbd_delay_ms(sc->axe_udev, 10);
597 
598 		/* power down and reset state, pin reset state */
599 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
600 		usbd_delay_ms(sc->axe_udev, 60);
601 
602 		/* power down/reset state, pin operating state */
603 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
604 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
605 		usbd_delay_ms(sc->axe_udev, 150);
606 
607 		/* power up, reset */
608 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
609 
610 		/* power up, operating */
611 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
612 		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
613 	} else {
614 		/* ask for external PHY */
615 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
616 		usbd_delay_ms(sc->axe_udev, 10);
617 
618 		/* power down internal PHY */
619 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
620 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
621 	}
622 
623 	usbd_delay_ms(sc->axe_udev, 150);
624 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
625 }
626 
627 static int
628 axe_get_phyno(struct axe_softc *sc, int sel)
629 {
630 	int phyno = -1;
631 
632 	switch (AXE_PHY_TYPE(sc->axe_phyaddrs[sel])) {
633 	case PHY_TYPE_100_HOME:
634 	case PHY_TYPE_GIG:
635 		phyno  = AXE_PHY_NO(sc->axe_phyaddrs[sel]);
636 		break;
637 	case PHY_TYPE_SPECIAL:
638 		/* FALLTHROUGH */
639 	case PHY_TYPE_RSVD:
640 		/* FALLTHROUGH */
641 	case PHY_TYPE_NON_SUP:
642 		/* FALLTHROUGH */
643 	default:
644 		break;
645 	}
646 
647 	return (phyno);
648 }
649 
650 /*
651  * Probe for a AX88172 chip.
652  */
653 int
654 axe_match(struct device *parent, void *match, void *aux)
655 {
656 	struct usb_attach_arg *uaa = aux;
657 
658 	if (!uaa->iface)
659 		return(UMATCH_NONE);
660 
661 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
662 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
663 }
664 
665 /*
666  * Attach the interface. Allocate softc structures, do ifmedia
667  * setup and ethernet/BPF attach.
668  */
669 void
670 axe_attach(struct device *parent, struct device *self, void *aux)
671 {
672 	struct axe_softc *sc = (struct axe_softc *)self;
673 	struct usb_attach_arg *uaa = aux;
674 	struct usbd_device *dev = uaa->device;
675 	usbd_status err;
676 	usb_interface_descriptor_t *id;
677 	usb_endpoint_descriptor_t *ed;
678 	struct mii_data	*mii;
679 	u_char eaddr[ETHER_ADDR_LEN];
680 	char *devname = sc->axe_dev.dv_xname;
681 	struct ifnet *ifp;
682 	int i, s;
683 
684 	sc->axe_unit = self->dv_unit; /*device_get_unit(self);*/
685 	sc->axe_udev = dev;
686 
687 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
688 	if (err) {
689 		printf("%s: getting interface handle failed\n",
690 		    sc->axe_dev.dv_xname);
691 		return;
692 	}
693 
694 	sc->axe_flags = axe_lookup(uaa->vendor, uaa->product)->axe_flags;
695 
696 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc,
697 	    USB_TASK_TYPE_GENERIC);
698 	rw_init(&sc->axe_mii_lock, "axemii");
699 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc,
700 	    USB_TASK_TYPE_GENERIC);
701 
702 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
703 	if (err) {
704 		printf("%s: getting interface handle failed\n",
705 		    sc->axe_dev.dv_xname);
706 		return;
707 	}
708 
709 	sc->axe_product = uaa->product;
710 	sc->axe_vendor = uaa->vendor;
711 
712 	id = usbd_get_interface_descriptor(sc->axe_iface);
713 
714 	/* decide on what our bufsize will be */
715 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
716 		sc->axe_bufsz = (sc->axe_udev->speed == USB_SPEED_HIGH) ?
717 		    AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ;
718 	else
719 		sc->axe_bufsz = AXE_172_BUFSZ;
720 
721 	/* Find endpoints. */
722 	for (i = 0; i < id->bNumEndpoints; i++) {
723 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
724 		if (!ed) {
725 			printf("%s: couldn't get ep %d\n",
726 			    sc->axe_dev.dv_xname, i);
727 			return;
728 		}
729 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
730 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
731 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
732 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
733 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
734 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
735 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
736 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
737 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
738 		}
739 	}
740 
741 	s = splnet();
742 
743 	/* We need the PHYID for init dance in some cases */
744 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
745 
746 	DPRINTF((" phyaddrs[0]: %x phyaddrs[1]: %x\n",
747 	    sc->axe_phyaddrs[0], sc->axe_phyaddrs[1]));
748 
749 	sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
750 	if (sc->axe_phyno == -1)
751 		sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
752 	if (sc->axe_phyno == -1) {
753 		printf("%s:", sc->axe_dev.dv_xname);
754 		printf(" no valid PHY address found, assuming PHY address 0\n");
755 		sc->axe_phyno = 0;
756 	}
757 
758 	DPRINTF((" get_phyno %d\n", sc->axe_phyno));
759 
760 	if (sc->axe_flags & AX178)
761 		axe_ax88178_init(sc);
762 	else if (sc->axe_flags & AX772)
763 		axe_ax88772_init(sc);
764 
765 	/*
766 	 * Get station address.
767 	 */
768 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
769 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, &eaddr);
770 	else
771 		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, &eaddr);
772 
773 	/*
774 	 * Load IPG values
775 	 */
776 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
777 
778 	/*
779 	 * An ASIX chip was detected. Inform the world.
780 	 */
781 	printf("%s:", sc->axe_dev.dv_xname);
782 	if (sc->axe_flags & AX178)
783 		printf(" AX88178");
784 	else if (sc->axe_flags & AX772B)
785 		printf(" AX88772B");
786 	else if (sc->axe_flags & AX772)
787 		printf(" AX88772");
788 	else
789 		printf(" AX88172");
790 	printf(", address %s\n", ether_sprintf(eaddr));
791 
792 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
793 
794 	/* Initialize interface info.*/
795 	ifp = &sc->arpcom.ac_if;
796 	ifp->if_softc = sc;
797 	strlcpy(ifp->if_xname, devname, IFNAMSIZ);
798 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
799 	ifp->if_ioctl = axe_ioctl;
800 	ifp->if_start = axe_start;
801 	ifp->if_watchdog = axe_watchdog;
802 	IFQ_SET_READY(&ifp->if_snd);
803 
804 	ifp->if_capabilities = IFCAP_VLAN_MTU;
805 
806 	/* Initialize MII/media info. */
807 	mii = &sc->axe_mii;
808 	mii->mii_ifp = ifp;
809 	mii->mii_readreg = axe_miibus_readreg;
810 	mii->mii_writereg = axe_miibus_writereg;
811 	mii->mii_statchg = axe_miibus_statchg;
812 	mii->mii_flags = MIIF_AUTOTSLEEP;
813 
814 	ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd, axe_ifmedia_sts);
815 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
816 
817 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
818 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
819 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
820 	} else
821 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
822 
823 	/* Attach the interface. */
824 	if_attach(ifp);
825 	ether_ifattach(ifp);
826 
827 	timeout_set(&sc->axe_stat_ch, axe_tick, sc);
828 
829 	splx(s);
830 }
831 
832 int
833 axe_detach(struct device *self, int flags)
834 {
835 	struct axe_softc	*sc = (struct axe_softc *)self;
836 	int			s;
837 	struct ifnet		*ifp = GET_IFP(sc);
838 
839 	DPRINTFN(2,("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
840 
841 	if (timeout_initialized(&sc->axe_stat_ch))
842 		timeout_del(&sc->axe_stat_ch);
843 
844 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
845 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
846 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
847 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
848 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
849 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
850 
851 	/*
852 	 * Remove any pending tasks.  They cannot be executing because they run
853 	 * in the same thread as detach.
854 	 */
855 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
856 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
857 
858 	s = splusb();
859 
860 	if (--sc->axe_refcnt >= 0) {
861 		/* Wait for processes to go away */
862 		usb_detach_wait(&sc->axe_dev);
863 	}
864 
865 	if (ifp->if_flags & IFF_RUNNING)
866 		axe_stop(sc);
867 
868 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
869 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
870 	if (ifp->if_softc != NULL) {
871 		ether_ifdetach(ifp);
872 		if_detach(ifp);
873 	}
874 
875 #ifdef DIAGNOSTIC
876 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
877 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
878 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
879 		printf("%s: detach has active endpoints\n",
880 		    sc->axe_dev.dv_xname);
881 #endif
882 
883 	if (--sc->axe_refcnt >= 0) {
884 		/* Wait for processes to go away. */
885 		usb_detach_wait(&sc->axe_dev);
886 	}
887 	splx(s);
888 
889 	return (0);
890 }
891 
892 int
893 axe_activate(struct device *self, int act)
894 {
895 	struct axe_softc *sc = (struct axe_softc *)self;
896 
897 	DPRINTFN(2,("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
898 
899 	switch (act) {
900 	case DVACT_DEACTIVATE:
901 		usbd_deactivate(sc->axe_udev);
902 		break;
903 	}
904 	return (0);
905 }
906 
907 struct mbuf *
908 axe_newbuf(void)
909 {
910 	struct mbuf		*m;
911 
912 	MGETHDR(m, M_DONTWAIT, MT_DATA);
913 	if (m == NULL)
914 		return (NULL);
915 
916 	MCLGET(m, M_DONTWAIT);
917 	if (!(m->m_flags & M_EXT)) {
918 		m_freem(m);
919 		return (NULL);
920 	}
921 
922 	m->m_len = m->m_pkthdr.len = MCLBYTES;
923 	m_adj(m, ETHER_ALIGN);
924 
925 	return (m);
926 }
927 
928 int
929 axe_rx_list_init(struct axe_softc *sc)
930 {
931 	struct axe_cdata *cd;
932 	struct axe_chain *c;
933 	int i;
934 
935 	DPRINTF(("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
936 
937 	cd = &sc->axe_cdata;
938 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
939 		c = &cd->axe_rx_chain[i];
940 		c->axe_sc = sc;
941 		c->axe_idx = i;
942 		c->axe_mbuf = NULL;
943 		if (c->axe_xfer == NULL) {
944 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
945 			if (c->axe_xfer == NULL)
946 				return (ENOBUFS);
947 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
948 			    sc->axe_bufsz);
949 			if (c->axe_buf == NULL) {
950 				usbd_free_xfer(c->axe_xfer);
951 				return (ENOBUFS);
952 			}
953 		}
954 	}
955 
956 	return (0);
957 }
958 
959 int
960 axe_tx_list_init(struct axe_softc *sc)
961 {
962 	struct axe_cdata *cd;
963 	struct axe_chain *c;
964 	int i;
965 
966 	DPRINTF(("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
967 
968 	cd = &sc->axe_cdata;
969 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
970 		c = &cd->axe_tx_chain[i];
971 		c->axe_sc = sc;
972 		c->axe_idx = i;
973 		c->axe_mbuf = NULL;
974 		if (c->axe_xfer == NULL) {
975 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
976 			if (c->axe_xfer == NULL)
977 				return (ENOBUFS);
978 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
979 			    sc->axe_bufsz);
980 			if (c->axe_buf == NULL) {
981 				usbd_free_xfer(c->axe_xfer);
982 				return (ENOBUFS);
983 			}
984 		}
985 	}
986 
987 	return (0);
988 }
989 
990 /*
991  * A frame has been uploaded: pass the resulting mbuf chain up to
992  * the higher level protocols.
993  */
994 void
995 axe_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
996 {
997 	struct axe_chain	*c = (struct axe_chain *)priv;
998 	struct axe_softc	*sc = c->axe_sc;
999 	struct ifnet		*ifp = GET_IFP(sc);
1000 	u_char			*buf = c->axe_buf;
1001 	u_int32_t		total_len;
1002 	u_int16_t		pktlen = 0;
1003 	struct mbuf		*m;
1004 	struct axe_sframe_hdr	hdr;
1005 	int			s;
1006 
1007 	DPRINTFN(10,("%s: %s: enter\n", sc->axe_dev.dv_xname,__func__));
1008 
1009 	if (usbd_is_dying(sc->axe_udev))
1010 		return;
1011 
1012 	if (!(ifp->if_flags & IFF_RUNNING))
1013 		return;
1014 
1015 	if (status != USBD_NORMAL_COMPLETION) {
1016 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1017 			return;
1018 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
1019 			printf("%s: usb errors on rx: %s\n",
1020 			    sc->axe_dev.dv_xname, usbd_errstr(status));
1021 		}
1022 		if (status == USBD_STALLED)
1023 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
1024 		goto done;
1025 	}
1026 
1027 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1028 
1029 	do {
1030 		if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1031 			if (total_len < sizeof(hdr)) {
1032 				ifp->if_ierrors++;
1033 				goto done;
1034 			}
1035 
1036 			buf += pktlen;
1037 
1038 			memcpy(&hdr, buf, sizeof(hdr));
1039 			total_len -= sizeof(hdr);
1040 
1041 			if (((letoh16(hdr.len) & AXE_RH1M_RXLEN_MASK) ^
1042 			    (letoh16(hdr.ilen) & AXE_RH1M_RXLEN_MASK)) !=
1043 			    AXE_RH1M_RXLEN_MASK) {
1044 				ifp->if_ierrors++;
1045 				goto done;
1046 			}
1047 			pktlen = letoh16(hdr.len) & AXE_RH1M_RXLEN_MASK;
1048 			if (pktlen > total_len) {
1049 				ifp->if_ierrors++;
1050 				goto done;
1051 			}
1052 
1053 			buf += sizeof(hdr);
1054 
1055 			if ((pktlen % 2) != 0)
1056 				pktlen++;
1057 
1058 			if (total_len < pktlen)
1059 				total_len = 0;
1060 			else
1061 				total_len -= pktlen;
1062 		} else {
1063 			pktlen = total_len; /* crc on the end? */
1064 			total_len = 0;
1065 		}
1066 
1067 		m = axe_newbuf();
1068 		if (m == NULL) {
1069 			ifp->if_ierrors++;
1070 			goto done;
1071 		}
1072 
1073 		ifp->if_ipackets++;
1074 		m->m_pkthdr.rcvif = ifp;
1075 		m->m_pkthdr.len = m->m_len = pktlen;
1076 
1077 		memcpy(mtod(m, char *), buf, pktlen);
1078 
1079 		/* push the packet up */
1080 		s = splnet();
1081 #if NBPFILTER > 0
1082 		if (ifp->if_bpf)
1083 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1084 #endif
1085 
1086 		ether_input_mbuf(ifp, m);
1087 
1088 		splx(s);
1089 
1090 	} while (total_len > 0);
1091 
1092 done:
1093 	memset(c->axe_buf, 0, sc->axe_bufsz);
1094 
1095 	/* Setup new transfer. */
1096 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
1097 	    c, c->axe_buf, sc->axe_bufsz,
1098 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1099 	    USBD_NO_TIMEOUT, axe_rxeof);
1100 	usbd_transfer(xfer);
1101 
1102 	DPRINTFN(10,("%s: %s: start rx\n", sc->axe_dev.dv_xname, __func__));
1103 
1104 	return;
1105 }
1106 
1107 /*
1108  * A frame was downloaded to the chip. It's safe for us to clean up
1109  * the list buffers.
1110  */
1111 
1112 void
1113 axe_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1114 {
1115 	struct axe_softc	*sc;
1116 	struct axe_chain	*c;
1117 	struct ifnet		*ifp;
1118 	int			s;
1119 
1120 	c = priv;
1121 	sc = c->axe_sc;
1122 	ifp = &sc->arpcom.ac_if;
1123 
1124 	if (usbd_is_dying(sc->axe_udev))
1125 		return;
1126 
1127 	s = splnet();
1128 
1129 	if (status != USBD_NORMAL_COMPLETION) {
1130 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1131 			splx(s);
1132 			return;
1133 		}
1134 		ifp->if_oerrors++;
1135 		printf("axe%d: usb error on tx: %s\n", sc->axe_unit,
1136 		    usbd_errstr(status));
1137 		if (status == USBD_STALLED)
1138 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
1139 		splx(s);
1140 		return;
1141 	}
1142 
1143 	ifp->if_timer = 0;
1144 	ifp->if_flags &= ~IFF_OACTIVE;
1145 
1146 	m_freem(c->axe_mbuf);
1147 	c->axe_mbuf = NULL;
1148 
1149 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1150 		axe_start(ifp);
1151 
1152 	ifp->if_opackets++;
1153 	splx(s);
1154 	return;
1155 }
1156 
1157 void
1158 axe_tick(void *xsc)
1159 {
1160 	struct axe_softc *sc = xsc;
1161 
1162 	if (sc == NULL)
1163 		return;
1164 
1165 	DPRINTFN(0xff, ("%s: %s: enter\n", sc->axe_dev.dv_xname,
1166 			__func__));
1167 
1168 	if (usbd_is_dying(sc->axe_udev))
1169 		return;
1170 
1171 	/* Perform periodic stuff in process context */
1172 	usb_add_task(sc->axe_udev, &sc->axe_tick_task);
1173 
1174 }
1175 
1176 void
1177 axe_tick_task(void *xsc)
1178 {
1179 	int			s;
1180 	struct axe_softc	*sc;
1181 	struct ifnet		*ifp;
1182 	struct mii_data		*mii;
1183 
1184 	sc = xsc;
1185 
1186 	if (sc == NULL)
1187 		return;
1188 
1189 	if (usbd_is_dying(sc->axe_udev))
1190 		return;
1191 
1192 	ifp = GET_IFP(sc);
1193 	mii = GET_MII(sc);
1194 	if (mii == NULL)
1195 		return;
1196 
1197 	s = splnet();
1198 
1199 	mii_tick(mii);
1200 	if (sc->axe_link == 0)
1201 		axe_miibus_statchg(&sc->axe_dev);
1202 	timeout_add_sec(&sc->axe_stat_ch, 1);
1203 
1204 	splx(s);
1205 }
1206 
1207 int
1208 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
1209 {
1210 	struct axe_chain	*c;
1211 	usbd_status		err;
1212 	struct axe_sframe_hdr	hdr;
1213 	int			length, boundary;
1214 
1215 	c = &sc->axe_cdata.axe_tx_chain[idx];
1216 
1217 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1218 		boundary = (sc->axe_udev->speed == USB_SPEED_HIGH) ? 512 : 64;
1219 
1220 		hdr.len = htole16(m->m_pkthdr.len);
1221 		hdr.ilen = ~hdr.len;
1222 
1223 		memcpy(c->axe_buf, &hdr, sizeof(hdr));
1224 		length = sizeof(hdr);
1225 
1226 		m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length);
1227 		length += m->m_pkthdr.len;
1228 
1229 		if ((length % boundary) == 0) {
1230 			hdr.len = 0x0000;
1231 			hdr.ilen = 0xffff;
1232 			memcpy(c->axe_buf + length, &hdr, sizeof(hdr));
1233 			length += sizeof(hdr);
1234 		}
1235 
1236 	} else {
1237 		m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
1238 		length = m->m_pkthdr.len;
1239 	}
1240 
1241 	c->axe_mbuf = m;
1242 
1243 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
1244 	    c, c->axe_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1245 	    10000, axe_txeof);
1246 
1247 	/* Transmit */
1248 	err = usbd_transfer(c->axe_xfer);
1249 	if (err != USBD_IN_PROGRESS) {
1250 		axe_stop(sc);
1251 		return(EIO);
1252 	}
1253 
1254 	sc->axe_cdata.axe_tx_cnt++;
1255 
1256 	return(0);
1257 }
1258 
1259 void
1260 axe_start(struct ifnet *ifp)
1261 {
1262 	struct axe_softc	*sc;
1263 	struct mbuf		*m_head = NULL;
1264 
1265 	sc = ifp->if_softc;
1266 
1267 	if (!sc->axe_link)
1268 		return;
1269 
1270 	if (ifp->if_flags & IFF_OACTIVE)
1271 		return;
1272 
1273 	IFQ_POLL(&ifp->if_snd, m_head);
1274 	if (m_head == NULL)
1275 		return;
1276 
1277 	if (axe_encap(sc, m_head, 0)) {
1278 		ifp->if_flags |= IFF_OACTIVE;
1279 		return;
1280 	}
1281 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1282 
1283 	/*
1284 	 * If there's a BPF listener, bounce a copy of this frame
1285 	 * to him.
1286 	 */
1287 #if NBPFILTER > 0
1288 	if (ifp->if_bpf)
1289 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1290 #endif
1291 
1292 	ifp->if_flags |= IFF_OACTIVE;
1293 
1294 	/*
1295 	 * Set a timeout in case the chip goes out to lunch.
1296 	 */
1297 	ifp->if_timer = 5;
1298 
1299 	return;
1300 }
1301 
1302 void
1303 axe_init(void *xsc)
1304 {
1305 	struct axe_softc	*sc = xsc;
1306 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1307 	struct axe_chain	*c;
1308 	usbd_status		err;
1309 	uWord			urxmode;
1310 	int			rxmode;
1311 	int			i, s;
1312 
1313 	s = splnet();
1314 
1315 	/*
1316 	 * Cancel pending I/O and free all RX/TX buffers.
1317 	 */
1318 	axe_reset(sc);
1319 
1320 	/* set MAC address */
1321 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
1322 		axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0,
1323 		    &sc->arpcom.ac_enaddr);
1324 
1325 	/* Enable RX logic. */
1326 
1327 	/* Init RX ring. */
1328 	if (axe_rx_list_init(sc) == ENOBUFS) {
1329 		printf("axe%d: rx list init failed\n", sc->axe_unit);
1330 		splx(s);
1331 		return;
1332 	}
1333 
1334 	/* Init TX ring. */
1335 	if (axe_tx_list_init(sc) == ENOBUFS) {
1336 		printf("axe%d: tx list init failed\n", sc->axe_unit);
1337 		splx(s);
1338 		return;
1339 	}
1340 
1341 	/* Set transmitter IPG values */
1342 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
1343 		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2],
1344 		    (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL);
1345 	else {
1346 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1347 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1348 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1349 	}
1350 
1351 	/* Program promiscuous mode and multicast filters. */
1352 	axe_iff(sc);
1353 
1354 	/* Enable receiver, set RX mode */
1355 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, urxmode);
1356 	rxmode = UGETW(urxmode);
1357 	rxmode |= AXE_RXCMD_ENABLE;
1358 	if (sc->axe_flags & AX772B)
1359 		rxmode |= AXE_772B_RXCMD_RH1M;
1360 	else if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1361 		if (sc->axe_udev->speed == USB_SPEED_HIGH) {
1362 			/* largest possible USB buffer size for AX88178 */
1363 			rxmode |= AXE_178_RXCMD_MFB;
1364 		}
1365 	}
1366 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1367 
1368 	/* Open RX and TX pipes. */
1369 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1370 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1371 	if (err) {
1372 		printf("axe%d: open rx pipe failed: %s\n",
1373 		    sc->axe_unit, usbd_errstr(err));
1374 		splx(s);
1375 		return;
1376 	}
1377 
1378 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1379 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1380 	if (err) {
1381 		printf("axe%d: open tx pipe failed: %s\n",
1382 		    sc->axe_unit, usbd_errstr(err));
1383 		splx(s);
1384 		return;
1385 	}
1386 
1387 	/* Start up the receive pipe. */
1388 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1389 		c = &sc->axe_cdata.axe_rx_chain[i];
1390 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
1391 		    c, c->axe_buf, sc->axe_bufsz,
1392 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1393 		    USBD_NO_TIMEOUT, axe_rxeof);
1394 		usbd_transfer(c->axe_xfer);
1395 	}
1396 
1397 	sc->axe_link = 0;
1398 	ifp->if_flags |= IFF_RUNNING;
1399 	ifp->if_flags &= ~IFF_OACTIVE;
1400 
1401 	splx(s);
1402 
1403 	timeout_add_sec(&sc->axe_stat_ch, 1);
1404 	return;
1405 }
1406 
1407 int
1408 axe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1409 {
1410 	struct axe_softc	*sc = ifp->if_softc;
1411 	struct ifreq		*ifr = (struct ifreq *)data;
1412 	struct ifaddr		*ifa = (struct ifaddr *)data;
1413 	int			s, error = 0;
1414 
1415 	s = splnet();
1416 
1417 	switch(cmd) {
1418 	case SIOCSIFADDR:
1419 		ifp->if_flags |= IFF_UP;
1420 		if (!(ifp->if_flags & IFF_RUNNING))
1421 			axe_init(sc);
1422 #ifdef INET
1423 		if (ifa->ifa_addr->sa_family == AF_INET)
1424 			arp_ifinit(&sc->arpcom, ifa);
1425 #endif
1426 		break;
1427 
1428 	case SIOCSIFFLAGS:
1429 		if (ifp->if_flags & IFF_UP) {
1430 			if (ifp->if_flags & IFF_RUNNING)
1431 				error = ENETRESET;
1432 			else
1433 				axe_init(sc);
1434 		} else {
1435 			if (ifp->if_flags & IFF_RUNNING)
1436 				axe_stop(sc);
1437 		}
1438 		break;
1439 
1440 	case SIOCGIFMEDIA:
1441 	case SIOCSIFMEDIA:
1442 		error = ifmedia_ioctl(ifp, ifr, &sc->axe_mii.mii_media, cmd);
1443 		break;
1444 
1445 	default:
1446 		error = ether_ioctl(ifp, &sc->arpcom, cmd, data);
1447 	}
1448 
1449 	if (error == ENETRESET) {
1450 		if (ifp->if_flags & IFF_RUNNING)
1451 			axe_iff(sc);
1452 		error = 0;
1453 	}
1454 
1455 	splx(s);
1456 	return(error);
1457 }
1458 
1459 void
1460 axe_watchdog(struct ifnet *ifp)
1461 {
1462 	struct axe_softc	*sc;
1463 	struct axe_chain	*c;
1464 	usbd_status		stat;
1465 	int			s;
1466 
1467 	sc = ifp->if_softc;
1468 
1469 	ifp->if_oerrors++;
1470 	printf("axe%d: watchdog timeout\n", sc->axe_unit);
1471 
1472 	s = splusb();
1473 	c = &sc->axe_cdata.axe_tx_chain[0];
1474 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1475 	axe_txeof(c->axe_xfer, c, stat);
1476 
1477 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
1478 		axe_start(ifp);
1479 	splx(s);
1480 }
1481 
1482 /*
1483  * Stop the adapter and free any mbufs allocated to the
1484  * RX and TX lists.
1485  */
1486 void
1487 axe_stop(struct axe_softc *sc)
1488 {
1489 	usbd_status		err;
1490 	struct ifnet		*ifp;
1491 	int			i;
1492 
1493 	axe_reset(sc);
1494 
1495 	ifp = &sc->arpcom.ac_if;
1496 	ifp->if_timer = 0;
1497 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1498 
1499 	timeout_del(&sc->axe_stat_ch);
1500 
1501 	/* Stop transfers. */
1502 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1503 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1504 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1505 		if (err) {
1506 			printf("axe%d: close rx pipe failed: %s\n",
1507 			    sc->axe_unit, usbd_errstr(err));
1508 		}
1509 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
1510 	}
1511 
1512 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1513 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1514 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1515 		if (err) {
1516 			printf("axe%d: close tx pipe failed: %s\n",
1517 			    sc->axe_unit, usbd_errstr(err));
1518 		}
1519 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
1520 	}
1521 
1522 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1523 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1524 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1525 		if (err) {
1526 			printf("axe%d: close intr pipe failed: %s\n",
1527 			    sc->axe_unit, usbd_errstr(err));
1528 		}
1529 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1530 	}
1531 
1532 	/* Free RX resources. */
1533 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1534 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1535 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1536 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1537 		}
1538 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1539 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1540 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1541 		}
1542 	}
1543 
1544 	/* Free TX resources. */
1545 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1546 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1547 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1548 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1549 		}
1550 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1551 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1552 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1553 		}
1554 	}
1555 
1556 	sc->axe_link = 0;
1557 }
1558 
1559