1 /*	$OpenBSD: if_axe.c,v 1.28 2005/07/18 07:38:43 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1998, 1999, 2000-2003
5  *	Bill Paul <wpaul@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 #include <sys/cdefs.h>
36 
37 /*
38  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
39  * LinkSys USB200M and various other adapters.
40  *
41  * Manuals available from:
42  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
43  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
44  * controller) to find the definitions for the RX control register.
45  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
46  *
47  * Written by Bill Paul <wpaul@windriver.com>
48  * Senior Engineer
49  * Wind River Systems
50  */
51 
52 /*
53  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
54  * It uses an external PHY (reference designs use a RealTek chip),
55  * and has a 64-bit multicast hash filter. There is some information
56  * missing from the manual which one needs to know in order to make
57  * the chip function:
58  *
59  * - You must set bit 7 in the RX control register, otherwise the
60  *   chip won't receive any packets.
61  * - You must initialize all 3 IPG registers, or you won't be able
62  *   to send any packets.
63  *
64  * Note that this device appears to only support loading the station
65  * address via autload from the EEPROM (i.e. there's no way to manaully
66  * set it).
67  *
68  * (Adam Weinberger wanted me to name this driver if_gir.c.)
69  */
70 
71 /*
72  * Ported to OpenBSD 3/28/2004 by Greg Taleck <taleck@oz.net>
73  * with bits and pieces from the aue and url drivers.
74  */
75 
76 #if defined(__NetBSD__)
77 #include "opt_inet.h"
78 #include "opt_ns.h"
79 #include "rnd.h"
80 #endif
81 
82 #include "bpfilter.h"
83 
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/sockio.h>
87 #include <sys/lock.h>
88 #include <sys/mbuf.h>
89 #include <sys/kernel.h>
90 #if defined(__OpenBSD__)
91 #include <sys/proc.h>
92 #endif
93 #include <sys/socket.h>
94 
95 #include <sys/device.h>
96 #if NRND > 0
97 #include <sys/rnd.h>
98 #endif
99 
100 #include <net/if.h>
101 #if defined(__NetBSD__)
102 #include <net/if_arp.h>
103 #endif
104 #include <net/if_dl.h>
105 #include <net/if_media.h>
106 
107 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
108 
109 #if NBPFILTER > 0
110 #include <net/bpf.h>
111 #endif
112 
113 #if defined(__NetBSD__)
114 #include <net/if_ether.h>
115 #ifdef INET
116 #include <netinet/in.h>
117 #include <netinet/if_inarp.h>
118 #endif
119 #endif /* defined(__NetBSD__) */
120 
121 #if defined(__OpenBSD__)
122 #ifdef INET
123 #include <netinet/in.h>
124 #include <netinet/in_systm.h>
125 #include <netinet/in_var.h>
126 #include <netinet/ip.h>
127 #include <netinet/if_ether.h>
128 #endif
129 #endif /* defined(__OpenBSD__) */
130 
131 #include <dev/mii/mii.h>
132 #include <dev/mii/miivar.h>
133 
134 #include <dev/usb/usb.h>
135 #include <dev/usb/usbdi.h>
136 #include <dev/usb/usbdi_util.h>
137 #include <dev/usb/usbdevs.h>
138 
139 #include <dev/usb/if_axereg.h>
140 
141 #ifdef AXE_DEBUG
142 #define DPRINTF(x)	do { if (axedebug) logprintf x; } while (0)
143 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) logprintf x; } while (0)
144 int	axedebug = 0;
145 #else
146 #define DPRINTF(x)
147 #define DPRINTFN(n,x)
148 #endif
149 
150 /*
151  * Various supported device vendors/products.
152  */
153 Static const struct axe_type axe_devs[] = {
154 	{ { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UF200}, 0 },
155 	{ { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 },
156 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 },
157 	{ { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T}, 0 },
158 	{ { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR}, 0},
159 	{ { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
160 	{ { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100}, 0 },
161 	{ { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
162 	{ { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 },
163 	{ { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M}, 0 },
164 	{ { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
165 	{ { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120}, 0 },
166 	{ { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
167 	{ { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029}, 0 },
168 	{ { 0, 0}, 0 }
169 };
170 
171 #define axe_lookup(v, p) ((struct axe_type *)usb_lookup(axe_devs, v, p))
172 
173 USB_DECLARE_DRIVER_CLASS(axe, DV_IFNET);
174 
175 Static int axe_tx_list_init(struct axe_softc *);
176 Static int axe_rx_list_init(struct axe_softc *);
177 Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
178 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
179 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
180 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
181 Static void axe_tick(void *);
182 Static void axe_tick_task(void *);
183 Static void axe_rxstart(struct ifnet *);
184 Static void axe_start(struct ifnet *);
185 Static int axe_ioctl(struct ifnet *, u_long, caddr_t);
186 Static void axe_init(void *);
187 Static void axe_stop(struct axe_softc *);
188 Static void axe_watchdog(struct ifnet *);
189 Static int axe_miibus_readreg(device_ptr_t, int, int);
190 Static void axe_miibus_writereg(device_ptr_t, int, int, int);
191 Static void axe_miibus_statchg(device_ptr_t);
192 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
193 Static int axe_ifmedia_upd(struct ifnet *);
194 Static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
195 Static void axe_reset(struct axe_softc *sc);
196 
197 Static void axe_setmulti(struct axe_softc *);
198 Static void axe_lock_mii(struct axe_softc *sc);
199 Static void axe_unlock_mii(struct axe_softc *sc);
200 
201 /* Get exclusive access to the MII registers */
202 Static void
axe_lock_mii(struct axe_softc * sc)203 axe_lock_mii(struct axe_softc *sc)
204 {
205 	sc->axe_refcnt++;
206 	usb_lockmgr(&sc->axe_mii_lock, LK_EXCLUSIVE, NULL, curproc);
207 }
208 
209 Static void
axe_unlock_mii(struct axe_softc * sc)210 axe_unlock_mii(struct axe_softc *sc)
211 {
212 	usb_lockmgr(&sc->axe_mii_lock, LK_RELEASE, NULL, curproc);
213 	if (--sc->axe_refcnt < 0)
214 		usb_detach_wakeup(USBDEV(sc->axe_dev));
215 }
216 
217 Static int
axe_cmd(struct axe_softc * sc,int cmd,int index,int val,void * buf)218 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
219 {
220 	usb_device_request_t	req;
221 	usbd_status		err;
222 
223 	if (sc->axe_dying)
224 		return(0);
225 
226 	axe_lock_mii(sc);
227 	if (AXE_CMD_DIR(cmd))
228 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
229 	else
230 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
231 	req.bRequest = AXE_CMD_CMD(cmd);
232 	USETW(req.wValue, val);
233 	USETW(req.wIndex, index);
234 	USETW(req.wLength, AXE_CMD_LEN(cmd));
235 
236 	err = usbd_do_request(sc->axe_udev, &req, buf);
237 	axe_unlock_mii(sc);
238 
239 	if (err)
240 		return(-1);
241 
242 	return(0);
243 }
244 
245 Static int
axe_miibus_readreg(device_ptr_t dev,int phy,int reg)246 axe_miibus_readreg(device_ptr_t dev, int phy, int reg)
247 {
248 	struct axe_softc	*sc = USBGETSOFTC(dev);
249 	usbd_status		err;
250 	uWord			val;
251 
252 	if (sc->axe_dying) {
253 		DPRINTF(("axe: dying\n"));
254 		return(0);
255 	}
256 
257 #ifdef notdef
258 	/*
259 	 * The chip tells us the MII address of any supported
260 	 * PHYs attached to the chip, so only read from those.
261 	 */
262 
263 	if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
264 		return (0);
265 
266 	if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
267 		return (0);
268 #endif
269 	if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
270 		return (0);
271 
272 	USETW(val, 0);
273 
274 	axe_lock_mii(sc);
275 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
276 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, val);
277 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
278 	axe_unlock_mii(sc);
279 
280 	if (err) {
281 		printf("axe%d: read PHY failed\n", sc->axe_unit);
282 		return(-1);
283 	}
284 
285 	if (UGETW(val))
286 		sc->axe_phyaddrs[0] = phy;
287 
288 	return (UGETW(val));
289 }
290 
291 Static void
axe_miibus_writereg(device_ptr_t dev,int phy,int reg,int val)292 axe_miibus_writereg(device_ptr_t dev, int phy, int reg, int val)
293 {
294 	struct axe_softc	*sc = USBGETSOFTC(dev);
295 	usbd_status		err;
296 	uWord			uval;
297 
298 	if (sc->axe_dying)
299 		return;
300 
301 	USETW(uval, val);
302 
303 	axe_lock_mii(sc);
304 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
305 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, uval);
306 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
307 	axe_unlock_mii(sc);
308 
309 	if (err) {
310 		printf("axe%d: write PHY failed\n", sc->axe_unit);
311 		return;
312 	}
313 }
314 
315 Static void
axe_miibus_statchg(device_ptr_t dev)316 axe_miibus_statchg(device_ptr_t dev)
317 {
318 	struct axe_softc	*sc = USBGETSOFTC(dev);
319 	struct mii_data		*mii = GET_MII(sc);
320 	int			val, err;
321 
322 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
323 		val = AXE_MEDIA_FULL_DUPLEX;
324 	else
325 		val = 0;
326 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
327 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
328 	if (err) {
329 		printf("%s: media change failed\n", USBDEVNAME(sc->axe_dev));
330 		return;
331 	}
332 }
333 
334 /*
335  * Set media options.
336  */
337 Static int
axe_ifmedia_upd(struct ifnet * ifp)338 axe_ifmedia_upd(struct ifnet *ifp)
339 {
340         struct axe_softc        *sc = ifp->if_softc;
341         struct mii_data         *mii = GET_MII(sc);
342 
343         sc->axe_link = 0;
344         if (mii->mii_instance) {
345                 struct mii_softc        *miisc;
346                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
347                          mii_phy_reset(miisc);
348         }
349         mii_mediachg(mii);
350 
351         return (0);
352 }
353 
354 /*
355  * Report current media status.
356  */
357 Static void
axe_ifmedia_sts(struct ifnet * ifp,struct ifmediareq * ifmr)358 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
359 {
360         struct axe_softc        *sc = ifp->if_softc;
361         struct mii_data         *mii = GET_MII(sc);
362 
363         mii_pollstat(mii);
364         ifmr->ifm_active = mii->mii_media_active;
365         ifmr->ifm_status = mii->mii_media_status;
366 }
367 
368 Static void
axe_setmulti(struct axe_softc * sc)369 axe_setmulti(struct axe_softc *sc)
370 {
371 	struct ifnet		*ifp;
372 	struct ether_multi *enm;
373 	struct ether_multistep step;
374 	u_int32_t		h = 0;
375 	uWord			urxmode;
376 	u_int16_t		rxmode;
377 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
378 
379 	if (sc->axe_dying)
380 		return;
381 
382 	ifp = GET_IFP(sc);
383 
384 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, urxmode);
385 	rxmode = UGETW(urxmode);
386 
387 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
388 allmulti:
389 		rxmode |= AXE_RXCMD_ALLMULTI;
390 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
391 		return;
392 	} else
393 		rxmode &= ~AXE_RXCMD_ALLMULTI;
394 
395 	/* now program new ones */
396 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
397 	while (enm != NULL) {
398 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
399 			   ETHER_ADDR_LEN) != 0)
400 			goto allmulti;
401 
402 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
403 		hashtbl[h / 8] |= 1 << (h % 8);
404 		ETHER_NEXT_MULTI(step, enm);
405 	}
406 
407 	ifp->if_flags &= ~IFF_ALLMULTI;
408 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
409 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
410 	return;
411 }
412 
413 Static void
axe_reset(struct axe_softc * sc)414 axe_reset(struct axe_softc *sc)
415 {
416 	if (sc->axe_dying)
417 		return;
418 	/* XXX What to reset? */
419 
420 	/* Wait a little while for the chip to get its brains in order. */
421 	DELAY(1000);
422 	return;
423 }
424 
425 /*
426  * Probe for a AX88172 chip.
427  */
USB_MATCH(axe)428 USB_MATCH(axe)
429 {
430 	USB_MATCH_START(axe, uaa);
431 
432 	if (!uaa->iface) {
433 		return(UMATCH_NONE);
434 	}
435 
436 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
437 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
438 }
439 
440 /*
441  * Attach the interface. Allocate softc structures, do ifmedia
442  * setup and ethernet/BPF attach.
443  */
USB_ATTACH(axe)444 USB_ATTACH(axe)
445 {
446 	USB_ATTACH_START(axe, sc, uaa);
447 	usbd_device_handle dev = uaa->device;
448 	usbd_status err;
449 	usb_interface_descriptor_t *id;
450 	usb_endpoint_descriptor_t *ed;
451 	struct mii_data	*mii;
452 	u_char eaddr[ETHER_ADDR_LEN];
453 	char devinfo[1024];
454 	char *devname = USBDEVNAME(sc->axe_dev);
455 	struct ifnet *ifp;
456 	int i, s;
457 
458         usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
459 	USB_ATTACH_SETUP;
460 
461 	sc->axe_unit = self->dv_unit; /*device_get_unit(self);*/
462 
463 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
464 	if (err) {
465 		printf("axe%d: getting interface handle failed\n",
466 		    sc->axe_unit);
467 		USB_ATTACH_ERROR_RETURN;
468 	}
469 
470 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
471 	lockinit(&sc->axe_mii_lock, PZERO, "axemii", 0, LK_CANRECURSE);
472 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
473 
474 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
475 	if (err) {
476 		printf("axe%d: getting interface handle failed\n",
477 			       sc->axe_unit);
478 		USB_ATTACH_ERROR_RETURN;
479 	}
480 
481 	sc->axe_udev = dev;
482 	sc->axe_product = uaa->product;
483 	sc->axe_vendor = uaa->vendor;
484 
485 	id = usbd_get_interface_descriptor(sc->axe_iface);
486 
487 	printf("%s: %s", USBDEVNAME(sc->axe_dev), devinfo);
488 
489 	/* Find endpoints. */
490 	for (i = 0; i < id->bNumEndpoints; i++) {
491 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
492 		if (!ed) {
493 			printf(" couldn't get ep %d\n", i);
494 			USB_ATTACH_ERROR_RETURN;
495 		}
496 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
497 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
498 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
499 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
500 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
501 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
502 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
503 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
504 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
505 		}
506 	}
507 
508 	s = splnet();
509 
510 	/*
511 	 * Get station address.
512 	 */
513 	axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
514 
515 	/*
516 	 * Load IPG values and PHY indexes.
517 	 */
518 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
519 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
520 
521 	/*
522 	 * Work around broken adapters that appear to lie about
523 	 * their PHY addresses.
524 	 */
525 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
526 
527 	/*
528 	 * An ASIX chip was detected. Inform the world.
529 	 */
530 	printf(", address %s\n", ether_sprintf(eaddr));
531 
532 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
533 
534 	/* Initialize interface info.*/
535 	ifp = &sc->arpcom.ac_if;
536 	ifp->if_softc = sc;
537 	strlcpy(ifp->if_xname, devname, IFNAMSIZ);
538 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
539 	ifp->if_ioctl = axe_ioctl;
540 	ifp->if_start = axe_start;
541 
542 	ifp->if_watchdog = axe_watchdog;
543 
544 /*	ifp->if_baudrate = 10000000; */
545 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
546 
547 	IFQ_SET_READY(&ifp->if_snd);
548 
549 	/* Initialize MII/media info. */
550 	mii = &sc->axe_mii;
551 	mii->mii_ifp = ifp;
552 	mii->mii_readreg = axe_miibus_readreg;
553 	mii->mii_writereg = axe_miibus_writereg;
554 	mii->mii_statchg = axe_miibus_statchg;
555 	mii->mii_flags = MIIF_AUTOTSLEEP;
556 
557 	ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd, axe_ifmedia_sts);
558 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
559 
560 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
561 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
562 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
563 	} else
564 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
565 
566 	/* Attach the interface. */
567 	if_attach(ifp);
568 	Ether_ifattach(ifp, eaddr);
569 #if NRND > 0
570 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->axe_dev),
571 	    RND_TYPE_NET, 0);
572 #endif
573 
574 	usb_callout_init(sc->axe_stat_ch);
575 
576 	sc->axe_attached = 1;
577 	splx(s);
578 
579 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev,
580 			   USBDEV(sc->axe_dev));
581 
582 	USB_ATTACH_SUCCESS_RETURN;
583 }
584 
USB_DETACH(axe)585 USB_DETACH(axe)
586 {
587 	USB_DETACH_START(axe, sc);
588 	int			s;
589 	struct ifnet		*ifp = GET_IFP(sc);
590 
591 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
592 
593 	/* Detached before attached finished, so just bail out. */
594 	if (!sc->axe_attached)
595 		return (0);
596 
597 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
598 
599 	sc->axe_dying = 1;
600 
601 	ether_ifdetach(ifp);
602 
603 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
604 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
605 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
606 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
607 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
608 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
609 
610 	/*
611 	 * Remove any pending tasks.  They cannot be executing because they run
612 	 * in the same thread as detach.
613 	 */
614 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
615 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
616 
617 	s = splusb();
618 
619 	if (--sc->axe_refcnt >= 0) {
620 		/* Wait for processes to go away */
621 		usb_detach_wait(USBDEV(sc->axe_dev));
622 	}
623 
624 	if (ifp->if_flags & IFF_RUNNING)
625 		axe_stop(sc);
626 
627 #if defined(__NetBSD__)
628 #if NRND > 0
629 	rnd_detach_source(&sc->rnd_source);
630 #endif
631 #endif /* __NetBSD__ */
632 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
633 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
634 	ether_ifdetach(ifp);
635 	if_detach(ifp);
636 
637 #ifdef DIAGNOSTIC
638 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
639 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
640 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
641 		printf("%s: detach has active endpoints\n",
642 		       USBDEVNAME(sc->axe_dev));
643 #endif
644 
645 	sc->axe_attached = 0;
646 
647 	if (--sc->axe_refcnt >= 0) {
648 		/* Wait for processes to go away. */
649 		usb_detach_wait(USBDEV(sc->axe_dev));
650 	}
651 	splx(s);
652 
653 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev,
654 			   USBDEV(sc->axe_dev));
655 
656 	return (0);
657 }
658 
659 int
axe_activate(device_ptr_t self,enum devact act)660 axe_activate(device_ptr_t self, enum devact act)
661 {
662 	struct axe_softc *sc = (struct axe_softc *)self;
663 
664 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
665 
666 	switch (act) {
667 	case DVACT_ACTIVATE:
668 		return (EOPNOTSUPP);
669 		break;
670 
671 	case DVACT_DEACTIVATE:
672 		if_deactivate(&sc->axe_ec.ec_if);
673 		sc->axe_dying = 1;
674 		break;
675 	}
676 	return (0);
677 }
678 
679 /*
680  * Initialize an RX descriptor and attach an MBUF cluster.
681  */
682 Static int
axe_newbuf(struct axe_softc * sc,struct axe_chain * c,struct mbuf * m)683 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
684 {
685 	struct mbuf		*m_new = NULL;
686 
687 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
688 
689 	if (m == NULL) {
690 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
691 		if (m_new == NULL) {
692 			printf("%s: no memory for rx list "
693 			    "-- packet dropped!\n", USBDEVNAME(sc->axe_dev));
694 			return (ENOBUFS);
695 		}
696 
697 		MCLGET(m_new, M_DONTWAIT);
698 		if (!(m_new->m_flags & M_EXT)) {
699 			printf("%s: no memory for rx list "
700 			    "-- packet dropped!\n", USBDEVNAME(sc->axe_dev));
701 			m_freem(m_new);
702 			return (ENOBUFS);
703 		}
704 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
705 	} else {
706 		m_new = m;
707 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
708 		m_new->m_data = m_new->m_ext.ext_buf;
709 	}
710 
711 	m_adj(m_new, ETHER_ALIGN);
712 	c->axe_mbuf = m_new;
713 
714 	return (0);
715 }
716 
717 Static int
axe_rx_list_init(struct axe_softc * sc)718 axe_rx_list_init(struct axe_softc *sc)
719 {
720 	struct axe_cdata *cd;
721 	struct axe_chain *c;
722 	int i;
723 
724 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
725 
726 	cd = &sc->axe_cdata;
727 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
728 		c = &cd->axe_rx_chain[i];
729 		c->axe_sc = sc;
730 		c->axe_idx = i;
731 		if (axe_newbuf(sc, c, NULL) == ENOBUFS)
732 			return (ENOBUFS);
733 		if (c->axe_xfer == NULL) {
734 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
735 			if (c->axe_xfer == NULL)
736 				return (ENOBUFS);
737 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
738 			if (c->axe_buf == NULL) {
739 				usbd_free_xfer(c->axe_xfer);
740 				return (ENOBUFS);
741 			}
742 		}
743 	}
744 
745 	return (0);
746 }
747 
748 Static int
axe_tx_list_init(struct axe_softc * sc)749 axe_tx_list_init(struct axe_softc *sc)
750 {
751 	struct axe_cdata *cd;
752 	struct axe_chain *c;
753 	int i;
754 
755 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
756 
757 	cd = &sc->axe_cdata;
758 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
759 		c = &cd->axe_tx_chain[i];
760 		c->axe_sc = sc;
761 		c->axe_idx = i;
762 		c->axe_mbuf = NULL;
763 		if (c->axe_xfer == NULL) {
764 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
765 			if (c->axe_xfer == NULL)
766 				return (ENOBUFS);
767 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
768 			if (c->axe_buf == NULL) {
769 				usbd_free_xfer(c->axe_xfer);
770 				return (ENOBUFS);
771 			}
772 		}
773 	}
774 
775 	return (0);
776 }
777 
778 Static void
axe_rxstart(struct ifnet * ifp)779 axe_rxstart(struct ifnet *ifp)
780 {
781 	struct axe_softc	*sc;
782 	struct axe_chain	*c;
783 
784 	sc = ifp->if_softc;
785 	axe_lock_mii(sc);
786 	c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
787 
788 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
789 		ifp->if_ierrors++;
790 		axe_unlock_mii(sc);
791 		return;
792 	}
793 
794 	/* Setup new transfer. */
795 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
796 	    c, c->axe_buf, AXE_BUFSZ,
797 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
798 	    USBD_NO_TIMEOUT, axe_rxeof);
799 	usbd_transfer(c->axe_xfer);
800 	axe_unlock_mii(sc);
801 
802 	return;
803 }
804 
805 /*
806  * A frame has been uploaded: pass the resulting mbuf chain up to
807  * the higher level protocols.
808  */
809 Static void
axe_rxeof(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)810 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
811 {
812 	struct axe_softc	*sc;
813 	struct axe_chain	*c;
814 	struct ifnet		*ifp;
815 	struct mbuf		*m;
816 	u_int32_t		total_len;
817 	int			s;
818 
819 	c = priv;
820 	sc = c->axe_sc;
821 	ifp = GET_IFP(sc);
822 
823 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
824 
825 	if (sc->axe_dying)
826 		return;
827 
828 	if (!(ifp->if_flags & IFF_RUNNING))
829 		return;
830 
831 	if (status != USBD_NORMAL_COMPLETION) {
832 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
833 			return;
834 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
835 			printf("%s: usb errors on rx: %s\n",
836 			    USBDEVNAME(sc->axe_dev), usbd_errstr(status));
837 		}
838 		if (status == USBD_STALLED)
839 			usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
840 		goto done;
841 	}
842 
843 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
844 
845 	m = c->axe_mbuf;
846 
847 	if (total_len <= sizeof(struct ether_header)) {
848 		ifp->if_ierrors++;
849 		goto done;
850 	}
851 
852 	ifp->if_ipackets++;
853 	m->m_pkthdr.rcvif = ifp;
854 	m->m_pkthdr.len = m->m_len = total_len;
855 
856 
857 	memcpy(mtod(c->axe_mbuf, char *), c->axe_buf, total_len);
858 
859 	/* No errors; receive the packet. */
860 	total_len -= ETHER_CRC_LEN + 4;
861 
862 	s = splnet();
863 
864 	/* XXX ugly */
865 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
866 		ifp->if_ierrors++;
867 		goto done1;
868 	}
869 
870 #if NBPFILTER > 0
871 	if (ifp->if_bpf)
872 		BPF_MTAP(ifp, m);
873 #endif
874 
875 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
876 		    __func__, m->m_len));
877 	IF_INPUT(ifp, m);
878  done1:
879 	splx(s);
880 
881  done:
882 
883 	/* Setup new transfer. */
884 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
885 	    c, c->axe_buf, AXE_BUFSZ,
886 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
887 	    USBD_NO_TIMEOUT, axe_rxeof);
888 	usbd_transfer(xfer);
889 
890 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
891 		    __func__));
892 	return;
893 }
894 
895 /*
896  * A frame was downloaded to the chip. It's safe for us to clean up
897  * the list buffers.
898  */
899 
900 Static void
axe_txeof(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)901 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
902 {
903 	struct axe_softc	*sc;
904 	struct axe_chain	*c;
905 	struct ifnet		*ifp;
906 	int			s;
907 
908 	c = priv;
909 	sc = c->axe_sc;
910 	ifp = &sc->arpcom.ac_if;
911 
912 	if (sc->axe_dying)
913 		return;
914 
915 	s = splnet();
916 
917 	if (status != USBD_NORMAL_COMPLETION) {
918 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
919 			splx(s);
920 			return;
921 		}
922 		ifp->if_oerrors++;
923 		printf("axe%d: usb error on tx: %s\n", sc->axe_unit,
924 		    usbd_errstr(status));
925 		if (status == USBD_STALLED)
926 			usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_TX]);
927 		splx(s);
928 		return;
929 	}
930 
931 	ifp->if_timer = 0;
932 	ifp->if_flags &= ~IFF_OACTIVE;
933 
934 	m_freem(c->axe_mbuf);
935 	c->axe_mbuf = NULL;
936 
937 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
938 		axe_start(ifp);
939 
940 	ifp->if_opackets++;
941 	splx(s);
942 	return;
943 }
944 
945 Static void
axe_tick(void * xsc)946 axe_tick(void *xsc)
947 {
948 	struct axe_softc *sc = xsc;
949 
950 	if (sc == NULL)
951 		return;
952 
953 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),
954 			__func__));
955 
956 	if (sc->axe_dying)
957 		return;
958 
959 	/* Perform periodic stuff in process context */
960 	usb_add_task(sc->axe_udev, &sc->axe_tick_task);
961 
962 }
963 
964 Static void
axe_tick_task(void * xsc)965 axe_tick_task(void *xsc)
966 {
967 	int			s;
968 	struct axe_softc	*sc;
969 	struct ifnet		*ifp;
970 	struct mii_data		*mii;
971 
972 	sc = xsc;
973 
974 	if (sc == NULL)
975 		return;
976 
977 	if (sc->axe_dying)
978 		return;
979 
980 	ifp = GET_IFP(sc);
981 	mii = GET_MII(sc);
982 	if (mii == NULL)
983 		return;
984 
985 	s = splnet();
986 
987 	mii_tick(mii);
988 	if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
989 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
990 		DPRINTF(("%s: %s: got link\n",
991 			 USBDEVNAME(sc->axe_dev), __func__));
992 		sc->axe_link++;
993 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
994 			   axe_start(ifp);
995 	}
996 
997 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
998 
999 	splx(s);
1000 }
1001 
1002 Static int
axe_encap(struct axe_softc * sc,struct mbuf * m,int idx)1003 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
1004 {
1005 	struct axe_chain	*c;
1006 	usbd_status		err;
1007 
1008 	c = &sc->axe_cdata.axe_tx_chain[idx];
1009 
1010 	/*
1011 	 * Copy the mbuf data into a contiguous buffer, leaving two
1012 	 * bytes at the beginning to hold the frame length.
1013 	 */
1014 	m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
1015 	c->axe_mbuf = m;
1016 
1017 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
1018 	    c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER, 10000,
1019 	    axe_txeof);
1020 
1021 	/* Transmit */
1022 	err = usbd_transfer(c->axe_xfer);
1023 	if (err != USBD_IN_PROGRESS) {
1024 		axe_stop(sc);
1025 		return(EIO);
1026 	}
1027 
1028 	sc->axe_cdata.axe_tx_cnt++;
1029 
1030 	return(0);
1031 }
1032 
1033 Static void
axe_start(struct ifnet * ifp)1034 axe_start(struct ifnet *ifp)
1035 {
1036 	struct axe_softc	*sc;
1037 	struct mbuf		*m_head = NULL;
1038 
1039 	sc = ifp->if_softc;
1040 
1041 	if (!sc->axe_link) {
1042 		return;
1043 	}
1044 
1045 	if (ifp->if_flags & IFF_OACTIVE) {
1046 		return;
1047 	}
1048 
1049 	IF_DEQUEUE(&ifp->if_snd, m_head);
1050 	if (m_head == NULL) {
1051 		return;
1052 	}
1053 
1054 	if (axe_encap(sc, m_head, 0)) {
1055 		IF_PREPEND(&ifp->if_snd, m_head);
1056 		ifp->if_flags |= IFF_OACTIVE;
1057 		return;
1058 	}
1059 
1060 	/*
1061 	 * If there's a BPF listener, bounce a copy of this frame
1062 	 * to him.
1063 	 */
1064 #if NBPFILTER > 0
1065 	 if (ifp->if_bpf)
1066 	 	BPF_MTAP(ifp, m_head);
1067 #endif
1068 
1069 	ifp->if_flags |= IFF_OACTIVE;
1070 
1071 	/*
1072 	 * Set a timeout in case the chip goes out to lunch.
1073 	 */
1074 	ifp->if_timer = 5;
1075 
1076 	return;
1077 }
1078 
1079 Static void
axe_init(void * xsc)1080 axe_init(void *xsc)
1081 {
1082 	struct axe_softc	*sc = xsc;
1083 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1084 	struct axe_chain	*c;
1085 	usbd_status		err;
1086 	int			rxmode;
1087 	int			i, s;
1088 
1089 	if (ifp->if_flags & IFF_RUNNING)
1090 		return;
1091 
1092 	s = splnet();
1093 
1094 	/*
1095 	 * Cancel pending I/O and free all RX/TX buffers.
1096 	 */
1097 	axe_reset(sc);
1098 
1099 	/* Enable RX logic. */
1100 
1101 	/* Init RX ring. */
1102 	if (axe_rx_list_init(sc) == ENOBUFS) {
1103 		printf("axe%d: rx list init failed\n", sc->axe_unit);
1104 		splx(s);
1105 		return;
1106 	}
1107 
1108 	/* Init TX ring. */
1109 	if (axe_tx_list_init(sc) == ENOBUFS) {
1110 		printf("axe%d: tx list init failed\n", sc->axe_unit);
1111 		splx(s);
1112 		return;
1113 	}
1114 
1115 	/* Set transmitter IPG values */
1116 	axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1117 	axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1118 	axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1119 
1120 	/* Enable receiver, set RX mode */
1121 	rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
1122 
1123 	/* If we want promiscuous mode, set the allframes bit. */
1124 	if (ifp->if_flags & IFF_PROMISC)
1125 		rxmode |= AXE_RXCMD_PROMISC;
1126 
1127 	if (ifp->if_flags & IFF_BROADCAST)
1128 		rxmode |= AXE_RXCMD_BROADCAST;
1129 
1130 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1131 
1132 	/* Load the multicast filter. */
1133 	axe_setmulti(sc);
1134 
1135 	/* Open RX and TX pipes. */
1136 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1137 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1138 	if (err) {
1139 		printf("axe%d: open rx pipe failed: %s\n",
1140 		    sc->axe_unit, usbd_errstr(err));
1141 		splx(s);
1142 		return;
1143 	}
1144 
1145 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1146 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1147 	if (err) {
1148 		printf("axe%d: open tx pipe failed: %s\n",
1149 		    sc->axe_unit, usbd_errstr(err));
1150 		splx(s);
1151 		return;
1152 	}
1153 
1154 	/* Start up the receive pipe. */
1155 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1156 		c = &sc->axe_cdata.axe_rx_chain[i];
1157 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
1158 		    c, c->axe_buf, AXE_BUFSZ,
1159 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1160 		    USBD_NO_TIMEOUT, axe_rxeof);
1161 		usbd_transfer(c->axe_xfer);
1162 	}
1163 
1164 	ifp->if_flags |= IFF_RUNNING;
1165 	ifp->if_flags &= ~IFF_OACTIVE;
1166 
1167 	splx(s);
1168 
1169 	usb_callout_init(sc->axe_stat_ch);
1170 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
1171 	return;
1172 }
1173 
1174 Static int
axe_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1175 axe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1176 {
1177 	struct axe_softc	*sc = ifp->if_softc;
1178 	struct ifreq		*ifr = (struct ifreq *)data;
1179 	struct ifaddr		*ifa = (struct ifaddr *)data;
1180 	struct mii_data		*mii;
1181 	uWord			rxmode;
1182 	int			error = 0;
1183 
1184 	switch(cmd) {
1185 	case SIOCSIFADDR:
1186 		ifp->if_flags |= IFF_UP;
1187 		axe_init(sc);
1188 
1189 		switch (ifa->ifa_addr->sa_family) {
1190 #ifdef INET
1191 		case AF_INET:
1192 			arp_ifinit(&sc->arpcom, ifa);
1193 			break;
1194 #endif /* INET */
1195 		}
1196 		break;
1197 
1198 	case SIOCSIFMTU:
1199 		if (ifr->ifr_mtu > ETHERMTU)
1200 			error = EINVAL;
1201 		else
1202 			ifp->if_mtu = ifr->ifr_mtu;
1203 		break;
1204 
1205 	case SIOCSIFFLAGS:
1206 		if (ifp->if_flags & IFF_UP) {
1207 			if (ifp->if_flags & IFF_RUNNING &&
1208 			    ifp->if_flags & IFF_PROMISC &&
1209 			    !(sc->axe_if_flags & IFF_PROMISC)) {
1210 
1211 				axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, rxmode);
1212 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0,
1213 				    UGETW(rxmode) | AXE_RXCMD_PROMISC, NULL);
1214 
1215 				axe_setmulti(sc);
1216 			} else if (ifp->if_flags & IFF_RUNNING &&
1217 			    !(ifp->if_flags & IFF_PROMISC) &&
1218 			    sc->axe_if_flags & IFF_PROMISC) {
1219 				axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, rxmode);
1220 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0,
1221 				    UGETW(rxmode) & ~AXE_RXCMD_PROMISC, NULL);
1222 				axe_setmulti(sc);
1223 			} else if (!(ifp->if_flags & IFF_RUNNING))
1224 				axe_init(sc);
1225 		} else {
1226 			if (ifp->if_flags & IFF_RUNNING)
1227 				axe_stop(sc);
1228 		}
1229 		sc->axe_if_flags = ifp->if_flags;
1230 		error = 0;
1231 		break;
1232 	case SIOCADDMULTI:
1233 	case SIOCDELMULTI:
1234 		error = (cmd == SIOCADDMULTI) ?
1235 		    ether_addmulti(ifr, &sc->arpcom) :
1236 		    ether_delmulti(ifr, &sc->arpcom);
1237 
1238 		if (error == ENETRESET) {
1239 			/*
1240 			 * Multicast list has changed; set the hardware
1241 			 * filter accordingly.
1242 			 */
1243 			if (ifp->if_flags & IFF_RUNNING)
1244 				axe_setmulti(sc);
1245 			error = 0;
1246 		}
1247 		break;
1248 	case SIOCGIFMEDIA:
1249 	case SIOCSIFMEDIA:
1250 		mii = GET_MII(sc);
1251 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1252 		break;
1253 
1254 	default:
1255 		error = EINVAL;
1256 		break;
1257 	}
1258 
1259 	return(error);
1260 }
1261 
1262 Static void
axe_watchdog(struct ifnet * ifp)1263 axe_watchdog(struct ifnet *ifp)
1264 {
1265 	struct axe_softc	*sc;
1266 	struct axe_chain	*c;
1267 	usbd_status		stat;
1268 	int			s;
1269 
1270 	sc = ifp->if_softc;
1271 
1272 	ifp->if_oerrors++;
1273 	printf("axe%d: watchdog timeout\n", sc->axe_unit);
1274 
1275 	s = splusb();
1276 	c = &sc->axe_cdata.axe_tx_chain[0];
1277 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1278 	axe_txeof(c->axe_xfer, c, stat);
1279 
1280 	if (ifp->if_snd.ifq_head != NULL)
1281 		axe_start(ifp);
1282 	splx(s);
1283 }
1284 
1285 /*
1286  * Stop the adapter and free any mbufs allocated to the
1287  * RX and TX lists.
1288  */
1289 Static void
axe_stop(struct axe_softc * sc)1290 axe_stop(struct axe_softc *sc)
1291 {
1292 	usbd_status		err;
1293 	struct ifnet		*ifp;
1294 	int			i;
1295 
1296 	axe_reset(sc);
1297 
1298 	ifp = &sc->arpcom.ac_if;
1299 	ifp->if_timer = 0;
1300 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1301 
1302 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
1303 
1304 	/* Stop transfers. */
1305 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1306 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1307 		if (err) {
1308 			printf("axe%d: abort rx pipe failed: %s\n",
1309 		    	sc->axe_unit, usbd_errstr(err));
1310 		}
1311 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1312 		if (err) {
1313 			printf("axe%d: close rx pipe failed: %s\n",
1314 		    	sc->axe_unit, usbd_errstr(err));
1315 		}
1316 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
1317 	}
1318 
1319 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1320 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1321 		if (err) {
1322 			printf("axe%d: abort tx pipe failed: %s\n",
1323 		    	sc->axe_unit, usbd_errstr(err));
1324 		}
1325 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1326 		if (err) {
1327 			printf("axe%d: close tx pipe failed: %s\n",
1328 			    sc->axe_unit, usbd_errstr(err));
1329 		}
1330 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
1331 	}
1332 
1333 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1334 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1335 		if (err) {
1336 			printf("axe%d: abort intr pipe failed: %s\n",
1337 		    	sc->axe_unit, usbd_errstr(err));
1338 		}
1339 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1340 		if (err) {
1341 			printf("axe%d: close intr pipe failed: %s\n",
1342 			    sc->axe_unit, usbd_errstr(err));
1343 		}
1344 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1345 	}
1346 
1347 	/* Free RX resources. */
1348 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1349 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1350 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1351 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1352 		}
1353 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1354 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1355 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1356 		}
1357 	}
1358 
1359 	/* Free TX resources. */
1360 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1361 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1362 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1363 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1364 		}
1365 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1366 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1367 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1368 		}
1369 	}
1370 
1371 	sc->axe_link = 0;
1372 }
1373 
1374