Home | History | Annotate | Line # | Download | only in usb
if_axen.c revision 1.49
      1 /*	$NetBSD: if_axen.c,v 1.49 2019/06/28 01:57:43 mrg Exp $	*/
      2 /*	$OpenBSD: if_axen.c,v 1.3 2013/10/21 10:10:22 yuo Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2013 Yojiro UO <yuo (at) openbsd.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 /*
     21  * ASIX Electronics AX88178a USB 2.0 ethernet and AX88179 USB 3.0 Ethernet
     22  * driver.
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: if_axen.c,v 1.49 2019/06/28 01:57:43 mrg Exp $");
     27 
     28 #ifdef _KERNEL_OPT
     29 #include "opt_inet.h"
     30 #include "opt_usb.h"
     31 #endif
     32 
     33 #include <sys/param.h>
     34 #include <sys/bus.h>
     35 #include <sys/device.h>
     36 #include <sys/kernel.h>
     37 #include <sys/mbuf.h>
     38 #include <sys/module.h>
     39 #include <sys/socket.h>
     40 #include <sys/sockio.h>
     41 #include <sys/systm.h>
     42 
     43 #include <sys/rndsource.h>
     44 
     45 #include <net/if.h>
     46 #include <net/if_dl.h>
     47 #include <net/if_ether.h>
     48 #include <net/if_media.h>
     49 
     50 #include <net/bpf.h>
     51 
     52 #include <netinet/in.h>		/* XXX for netinet/ip.h */
     53 #include <netinet/ip.h>		/* XXX for IP_MAXPACKET */
     54 
     55 #include <dev/mii/mii.h>
     56 #include <dev/mii/miivar.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbdi.h>
     60 #include <dev/usb/usbdi_util.h>
     61 #include <dev/usb/usbdivar.h>
     62 #include <dev/usb/usbdevs.h>
     63 
     64 #include <dev/usb/if_axenreg.h>
     65 
     66 #ifdef AXEN_DEBUG
     67 #define DPRINTF(x)	do { if (axendebug) printf x; } while (/*CONSTCOND*/0)
     68 #define DPRINTFN(n, x)	do { if (axendebug >= (n)) printf x; } while (/*CONSTCOND*/0)
     69 int	axendebug = 0;
     70 #else
     71 #define DPRINTF(x)
     72 #define DPRINTFN(n, x)
     73 #endif
     74 
     75 struct axen_softc;
     76 
     77 struct axen_chain {
     78 	struct axen_softc	*axen_sc;
     79 	struct usbd_xfer	*axen_xfer;
     80 	uint8_t			*axen_buf;
     81 };
     82 
     83 struct axen_cdata {
     84 	struct axen_chain	axen_tx_chain[AXEN_TX_LIST_CNT];
     85 	struct axen_chain	axen_rx_chain[AXEN_RX_LIST_CNT];
     86 	int			axen_tx_prod;
     87 	int			axen_tx_cnt;
     88 };
     89 
     90 struct axen_softc {
     91 	device_t		axen_dev;
     92 	struct ethercom		axen_ec;
     93 	struct mii_data		axen_mii;
     94 	krndsource_t		rnd_source;
     95 	struct usbd_device *	axen_udev;
     96 	struct usbd_interface *	axen_iface;
     97 
     98 	uint16_t		axen_vendor;
     99 	uint16_t		axen_product;
    100 	uint16_t		axen_flags;
    101 	uint16_t		axen_timer;
    102 
    103 	int			axen_ed[AXEN_ENDPT_MAX];
    104 	struct usbd_pipe	*axen_ep[AXEN_ENDPT_MAX];
    105 	int			axen_if_flags;
    106 	struct axen_cdata	axen_cdata;
    107 	struct callout		axen_stat_ch;
    108 
    109 	int			axen_refcnt;
    110 	bool			axen_dying;
    111 	bool			axen_stopping;
    112 	bool			axen_attached;
    113 
    114 	struct usb_task		axen_tick_task;
    115 
    116 	kmutex_t		axen_lock;
    117 	kmutex_t		axen_mii_lock;
    118 	kmutex_t		axen_rxlock;
    119 	kmutex_t		axen_txlock;
    120 	kcondvar_t		axen_detachcv;
    121 
    122 	int			axen_link;
    123 
    124 	int			axen_phyno;
    125 	struct timeval		axen_rx_notice;
    126 	struct timeval		axen_tx_notice;
    127 	u_int			axen_rx_bufsz;
    128 	u_int			axen_tx_bufsz;
    129 	int			axen_rev;
    130 
    131 #define sc_if	axen_ec.ec_if
    132 };
    133 
    134 #define GET_MII(sc) (&(sc)->axen_mii)
    135 #define GET_IFP(sc) (&(sc)->sc_if)
    136 
    137 struct axen_type {
    138 	struct usb_devno	axen_dev;
    139 	uint16_t		axen_flags;
    140 #define AX178A	0x0001		/* AX88178a */
    141 #define AX179	0x0002		/* AX88179 */
    142 };
    143 
    144 /*
    145  * Various supported device vendors/products.
    146  */
    147 static const struct axen_type axen_devs[] = {
    148 #if 0 /* not tested */
    149 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178A}, AX178A },
    150 #endif
    151 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88179}, AX179 },
    152 	{ { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUB1312}, AX179 }
    153 };
    154 
    155 #define axen_lookup(v, p) ((const struct axen_type *)usb_lookup(axen_devs, v, p))
    156 
    157 static int	axen_match(device_t, cfdata_t, void *);
    158 static void	axen_attach(device_t, device_t, void *);
    159 static int	axen_detach(device_t, int);
    160 static int	axen_activate(device_t, devact_t);
    161 
    162 CFATTACH_DECL_NEW(axen, sizeof(struct axen_softc),
    163 	axen_match, axen_attach, axen_detach, axen_activate);
    164 
    165 static int	axen_tx_list_init(struct axen_softc *);
    166 static int	axen_rx_list_init(struct axen_softc *);
    167 static struct mbuf *axen_newbuf(void);
    168 static int	axen_encap(struct axen_softc *, struct mbuf *, int);
    169 static void	axen_rxeof(struct usbd_xfer *, void *, usbd_status);
    170 static int	axen_csum_flags_rx(struct ifnet *, uint32_t);
    171 static void	axen_txeof(struct usbd_xfer *, void *, usbd_status);
    172 static void	axen_tick(void *);
    173 static void	axen_tick_task(void *);
    174 static void	axen_start(struct ifnet *);
    175 static void	axen_start_locked(struct ifnet *);
    176 static int	axen_ioctl(struct ifnet *, u_long, void *);
    177 static int	axen_init(struct ifnet *);
    178 static void	axen_stop(struct ifnet *, int);
    179 static void	axen_stop_locked(struct ifnet *, int);
    180 static void	axen_watchdog(struct ifnet *);
    181 static int	axen_miibus_readreg(device_t, int, int, uint16_t *);
    182 static int	axen_miibus_writereg(device_t, int, int, uint16_t);
    183 static void	axen_miibus_statchg(struct ifnet *);
    184 static int	axen_cmd(struct axen_softc *, int, int, int, void *);
    185 static int	axen_ifmedia_upd(struct ifnet *);
    186 static void	axen_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    187 static void	axen_reset(struct axen_softc *);
    188 static int	axen_get_eaddr(struct axen_softc *, void *);
    189 static void	axen_iff(struct axen_softc *);
    190 static void	axen_ax88179_init(struct axen_softc *);
    191 static void	axen_setcoe(struct axen_softc *);
    192 
    193 /*
    194  * Access functions for MII.  Take the MII lock to call axen_cmd().
    195  * Two forms: softc lock currently held or not.
    196  */
    197 static void
    198 axen_lock_mii(struct axen_softc *sc)
    199 {
    200 
    201 	mutex_enter(&sc->axen_lock);
    202 	sc->axen_refcnt++;
    203 	mutex_exit(&sc->axen_lock);
    204 
    205 	mutex_enter(&sc->axen_mii_lock);
    206 }
    207 
    208 static void
    209 axen_lock_mii_sc_locked(struct axen_softc *sc)
    210 {
    211 	KASSERT(mutex_owned(&sc->axen_lock));
    212 
    213 	sc->axen_refcnt++;
    214 	mutex_enter(&sc->axen_mii_lock);
    215 }
    216 
    217 static void
    218 axen_unlock_mii(struct axen_softc *sc)
    219 {
    220 
    221 	mutex_exit(&sc->axen_mii_lock);
    222 	mutex_enter(&sc->axen_lock);
    223 	if (--sc->axen_refcnt < 0)
    224 		cv_broadcast(&sc->axen_detachcv);
    225 	mutex_exit(&sc->axen_lock);
    226 }
    227 
    228 static void
    229 axen_unlock_mii_sc_locked(struct axen_softc *sc)
    230 {
    231 	KASSERT(mutex_owned(&sc->axen_lock));
    232 
    233 	mutex_exit(&sc->axen_mii_lock);
    234 	if (--sc->axen_refcnt < 0)
    235 		cv_broadcast(&sc->axen_detachcv);
    236 }
    237 
    238 static int
    239 axen_cmd(struct axen_softc *sc, int cmd, int index, int val, void *buf)
    240 {
    241 	usb_device_request_t req;
    242 	usbd_status err;
    243 
    244 	KASSERT(mutex_owned(&sc->axen_mii_lock));
    245 
    246 	if (sc->axen_dying)
    247 		return 0;
    248 
    249 	if (AXEN_CMD_DIR(cmd))
    250 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    251 	else
    252 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    253 	req.bRequest = AXEN_CMD_CMD(cmd);
    254 	USETW(req.wValue, val);
    255 	USETW(req.wIndex, index);
    256 	USETW(req.wLength, AXEN_CMD_LEN(cmd));
    257 
    258 	err = usbd_do_request(sc->axen_udev, &req, buf);
    259 	DPRINTFN(5, ("axen_cmd: cmd 0x%04x val 0x%04x len %d\n",
    260 	    cmd, val, AXEN_CMD_LEN(cmd)));
    261 
    262 	if (err) {
    263 		DPRINTF(("%s: cmd: %d, error: %d\n", __func__, cmd, err));
    264 		return -1;
    265 	}
    266 
    267 	return 0;
    268 }
    269 
    270 static int
    271 axen_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    272 {
    273 	struct axen_softc * const sc = device_private(dev);
    274 	usbd_status err;
    275 	uint16_t data;
    276 
    277 	mutex_enter(&sc->axen_lock);
    278 	if (sc->axen_dying || sc->axen_phyno != phy) {
    279 		mutex_exit(&sc->axen_lock);
    280 		return -1;
    281 	}
    282 	mutex_exit(&sc->axen_lock);
    283 
    284 	axen_lock_mii(sc);
    285 	err = axen_cmd(sc, AXEN_CMD_MII_READ_REG, reg, phy, &data);
    286 	axen_unlock_mii(sc);
    287 
    288 	if (err) {
    289 		aprint_error_dev(sc->axen_dev, "read PHY failed: %d\n", err);
    290 		return err;
    291 	}
    292 
    293 	*val = le16toh(data);
    294 	DPRINTFN(2,("axen_miibus_readreg: phy 0x%x reg 0x%x val 0x%hx\n",
    295 	    phy, reg, *val));
    296 
    297 	if (reg == MII_BMSR) {
    298 		*val &= ~BMSR_EXTCAP;
    299 	}
    300 
    301 	return 0;
    302 }
    303 
    304 static int
    305 axen_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    306 {
    307 	struct axen_softc * const sc = device_private(dev);
    308 	usbd_status err;
    309 	uint16_t uval;
    310 
    311 	mutex_enter(&sc->axen_lock);
    312 	if (sc->axen_dying || sc->axen_phyno != phy) {
    313 		mutex_exit(&sc->axen_lock);
    314 		return -1;
    315 	}
    316 	mutex_exit(&sc->axen_lock);
    317 
    318 	uval = htole16(val);
    319 
    320 	axen_lock_mii(sc);
    321 	err = axen_cmd(sc, AXEN_CMD_MII_WRITE_REG, reg, phy, &uval);
    322 	axen_unlock_mii(sc);
    323 
    324 	DPRINTFN(2, ("axen_miibus_writereg: phy 0x%x reg 0x%x val 0x%04hx\n",
    325 	    phy, reg, val));
    326 
    327 	if (err) {
    328 		aprint_error_dev(sc->axen_dev, "write PHY failed: %d\n", err);
    329 		return err;
    330 	}
    331 
    332 	return 0;
    333 }
    334 
    335 static void
    336 axen_miibus_statchg(struct ifnet *ifp)
    337 {
    338 	struct axen_softc * const sc = ifp->if_softc;
    339 	struct mii_data *mii = GET_MII(sc);
    340 	int err;
    341 	uint16_t val;
    342 	uint16_t wval;
    343 
    344 	if (sc->axen_dying)
    345 		return;
    346 
    347 	sc->axen_link = 0;
    348 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    349 	    (IFM_ACTIVE | IFM_AVALID)) {
    350 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    351 		case IFM_10_T:
    352 		case IFM_100_TX:
    353 			sc->axen_link++;
    354 			break;
    355 		case IFM_1000_T:
    356 			sc->axen_link++;
    357 			break;
    358 		default:
    359 			break;
    360 		}
    361 	}
    362 
    363 	/* Lost link, do nothing. */
    364 	if (sc->axen_link == 0)
    365 		return;
    366 
    367 	val = 0;
    368 	if ((mii->mii_media_active & IFM_FDX) != 0)
    369 		val |= AXEN_MEDIUM_FDX;
    370 
    371 	val |= AXEN_MEDIUM_RXFLOW_CTRL_EN | AXEN_MEDIUM_TXFLOW_CTRL_EN |
    372 	    AXEN_MEDIUM_RECV_EN;
    373 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
    374 	case IFM_1000_T:
    375 		val |= AXEN_MEDIUM_GIGA | AXEN_MEDIUM_EN_125MHZ;
    376 		break;
    377 	case IFM_100_TX:
    378 		val |= AXEN_MEDIUM_PS;
    379 		break;
    380 	case IFM_10_T:
    381 		/* doesn't need to be handled */
    382 		break;
    383 	}
    384 
    385 	DPRINTF(("%s: val=0x%x\n", __func__, val));
    386 	wval = htole16(val);
    387 	axen_lock_mii(sc);
    388 	err = axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MEDIUM_STATUS, &wval);
    389 	axen_unlock_mii(sc);
    390 	if (err) {
    391 		aprint_error_dev(sc->axen_dev, "media change failed\n");
    392 		return;
    393 	}
    394 }
    395 
    396 /*
    397  * Set media options.
    398  */
    399 static int
    400 axen_ifmedia_upd(struct ifnet *ifp)
    401 {
    402 	struct axen_softc * const sc = ifp->if_softc;
    403 	struct mii_data *mii = GET_MII(sc);
    404 	int rc;
    405 
    406 	sc->axen_link = 0;
    407 
    408 	if (mii->mii_instance) {
    409 		struct mii_softc *miisc;
    410 
    411 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    412 			mii_phy_reset(miisc);
    413 	}
    414 
    415 	if ((rc = mii_mediachg(mii)) == ENXIO)
    416 		return 0;
    417 	return rc;
    418 }
    419 
    420 /*
    421  * Report current media status.
    422  */
    423 static void
    424 axen_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    425 {
    426 	struct axen_softc * const sc = ifp->if_softc;
    427 	struct mii_data *mii = GET_MII(sc);
    428 
    429 	mii_pollstat(mii);
    430 	ifmr->ifm_active = mii->mii_media_active;
    431 	ifmr->ifm_status = mii->mii_media_status;
    432 }
    433 
    434 static void
    435 axen_iff_locked(struct axen_softc *sc)
    436 {
    437 	struct ifnet *ifp = GET_IFP(sc);
    438 	struct ethercom *ec = &sc->axen_ec;
    439 	struct ether_multi *enm;
    440 	struct ether_multistep step;
    441 	uint32_t h = 0;
    442 	uint16_t rxmode;
    443 	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    444 	uint16_t wval;
    445 
    446 	if (sc->axen_dying)
    447 		return;
    448 
    449 	KASSERT(mutex_owned(&sc->axen_mii_lock));
    450 
    451 	rxmode = 0;
    452 
    453 	/* Enable receiver, set RX mode */
    454 	axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval);
    455 	rxmode = le16toh(wval);
    456 	rxmode &= ~(AXEN_RXCTL_ACPT_ALL_MCAST | AXEN_RXCTL_PROMISC |
    457 	    AXEN_RXCTL_ACPT_MCAST);
    458 
    459 	if (ifp->if_flags & IFF_PROMISC) {
    460 		DPRINTF(("%s: promisc\n", device_xname(sc->axen_dev)));
    461 		rxmode |= AXEN_RXCTL_PROMISC;
    462 allmulti:
    463 		ETHER_LOCK(ec);
    464 		ec->ec_flags |= ETHER_F_ALLMULTI;
    465 		ETHER_UNLOCK(ec);
    466 		rxmode |= AXEN_RXCTL_ACPT_ALL_MCAST
    467 		/* | AXEN_RXCTL_ACPT_PHY_MCAST */;
    468 	} else {
    469 		/* now program new ones */
    470 		DPRINTF(("%s: initializing hash table\n",
    471 		    device_xname(sc->axen_dev)));
    472 		ETHER_LOCK(ec);
    473 		ec->ec_flags &= ~ETHER_F_ALLMULTI;
    474 
    475 		ETHER_FIRST_MULTI(step, ec, enm);
    476 		while (enm != NULL) {
    477 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    478 			    ETHER_ADDR_LEN)) {
    479 				DPRINTF(("%s: allmulti\n",
    480 				    device_xname(sc->axen_dev)));
    481 				memset(hashtbl, 0, sizeof(hashtbl));
    482 				ETHER_UNLOCK(ec);
    483 				goto allmulti;
    484 			}
    485 			h = ether_crc32_be(enm->enm_addrlo,
    486 			    ETHER_ADDR_LEN) >> 26;
    487 			hashtbl[h / 8] |= 1 << (h % 8);
    488 			DPRINTF(("%s: %s added\n",
    489 			    device_xname(sc->axen_dev),
    490 			    ether_sprintf(enm->enm_addrlo)));
    491 			ETHER_NEXT_MULTI(step, enm);
    492 		}
    493 		ETHER_UNLOCK(ec);
    494 		rxmode |= AXEN_RXCTL_ACPT_MCAST;
    495 	}
    496 
    497 	axen_cmd(sc, AXEN_CMD_MAC_WRITE_FILTER, 8, AXEN_FILTER_MULTI, hashtbl);
    498 	wval = htole16(rxmode);
    499 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
    500 }
    501 
    502 static void
    503 axen_iff(struct axen_softc *sc)
    504 {
    505 
    506 	axen_lock_mii(sc);
    507 	axen_iff_locked(sc);
    508 	axen_unlock_mii(sc);
    509 }
    510 
    511 static void
    512 axen_reset(struct axen_softc *sc)
    513 {
    514 
    515 	KASSERT(mutex_owned(&sc->axen_lock));
    516 	if (sc->axen_dying)
    517 		return;
    518 	/* XXX What to reset? */
    519 
    520 	/* Wait a little while for the chip to get its brains in order. */
    521 	DELAY(1000);
    522 }
    523 
    524 #define AXEN_GPIO_WRITE(x, y) do {				\
    525 	axen_cmd(sc, AXEN_CMD_WRITE_GPIO, 0, (x), NULL);	\
    526 	usbd_delay_ms(sc->axen_udev, (y));			\
    527 } while (/*CONSTCOND*/0)
    528 
    529 static int
    530 axen_get_eaddr(struct axen_softc *sc, void *addr)
    531 {
    532 #if 1
    533 	return axen_cmd(sc, AXEN_CMD_MAC_READ_ETHER, 6, AXEN_CMD_MAC_NODE_ID,
    534 	    addr);
    535 #else
    536 	int i, retry;
    537 	uint8_t eeprom[20];
    538 	uint16_t csum;
    539 	uint16_t buf;
    540 
    541 	for (i = 0; i < 6; i++) {
    542 		/* set eeprom address */
    543 		buf = htole16(i);
    544 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_ADDR, &buf);
    545 
    546 		/* set eeprom command */
    547 		buf = htole16(AXEN_EEPROM_READ);
    548 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_CMD, &buf);
    549 
    550 		/* check the value is ready */
    551 		retry = 3;
    552 		do {
    553 			buf = htole16(AXEN_EEPROM_READ);
    554 			usbd_delay_ms(sc->axen_udev, 10);
    555 			axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_MAC_EEPROM_CMD,
    556 			    &buf);
    557 			retry--;
    558 			if (retry < 0)
    559 				return EINVAL;
    560 		} while ((le16toh(buf) & 0xff) & AXEN_EEPROM_BUSY);
    561 
    562 		/* read data */
    563 		axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_EEPROM_READ,
    564 		    &eeprom[i * 2]);
    565 
    566 		/* sanity check */
    567 		if ((i == 0) && (eeprom[0] == 0xff))
    568 			return EINVAL;
    569 	}
    570 
    571 	/* check checksum */
    572 	csum = eeprom[6] + eeprom[7] + eeprom[8] + eeprom[9];
    573 	csum = (csum >> 8) + (csum & 0xff) + eeprom[10];
    574 	if (csum != 0xff) {
    575 		printf("eeprom checksum mismatchi(0x%02x)\n", csum);
    576 		return EINVAL;
    577 	}
    578 
    579 	memcpy(addr, eeprom, ETHER_ADDR_LEN);
    580 	return 0;
    581 #endif
    582 }
    583 
    584 static void
    585 axen_ax88179_init(struct axen_softc *sc)
    586 {
    587 	struct axen_qctrl qctrl;
    588 	uint16_t ctl, temp;
    589 	uint16_t wval;
    590 	uint8_t val;
    591 
    592 	axen_lock_mii(sc);
    593 
    594 	/* XXX: ? */
    595 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_UNK_05, &val);
    596 	DPRINTFN(5, ("AXEN_CMD_MAC_READ(0x05): 0x%02x\n", val));
    597 
    598 	/* check AX88179 version, UA1 / UA2 */
    599 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_GENERAL_STATUS, &val);
    600 	/* UA1 */
    601 	if (!(val & AXEN_GENERAL_STATUS_MASK)) {
    602 		sc->axen_rev = AXEN_REV_UA1;
    603 		DPRINTF(("AX88179 ver. UA1\n"));
    604 	} else {
    605 		sc->axen_rev = AXEN_REV_UA2;
    606 		DPRINTF(("AX88179 ver. UA2\n"));
    607 	}
    608 
    609 	/* power up ethernet PHY */
    610 	wval = htole16(0);
    611 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    612 
    613 	wval = htole16(AXEN_PHYPWR_RSTCTL_IPRL);
    614 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    615 	usbd_delay_ms(sc->axen_udev, 200);
    616 
    617 	/* set clock mode */
    618 	val = AXEN_PHYCLK_ACS | AXEN_PHYCLK_BCS;
    619 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val);
    620 	usbd_delay_ms(sc->axen_udev, 100);
    621 
    622 	/* set monitor mode (disable) */
    623 	val = AXEN_MONITOR_NONE;
    624 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val);
    625 
    626 	/* enable auto detach */
    627 	axen_cmd(sc, AXEN_CMD_EEPROM_READ, 2, AXEN_EEPROM_STAT, &wval);
    628 	temp = le16toh(wval);
    629 	DPRINTFN(2,("EEPROM0x43 = 0x%04x\n", temp));
    630 	if (!(temp == 0xffff) && !(temp & 0x0100)) {
    631 		/* Enable auto detach bit */
    632 		val = 0;
    633 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val);
    634 		val = AXEN_PHYCLK_ULR;
    635 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val);
    636 		usbd_delay_ms(sc->axen_udev, 100);
    637 
    638 		axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    639 		ctl = le16toh(wval);
    640 		ctl |= AXEN_PHYPWR_RSTCTL_AUTODETACH;
    641 		wval = htole16(ctl);
    642 		axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    643 		usbd_delay_ms(sc->axen_udev, 200);
    644 		aprint_error_dev(sc->axen_dev, "enable auto detach (0x%04x)\n",
    645 		    ctl);
    646 	}
    647 
    648 	/* bulkin queue setting */
    649 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_USB_UPLINK, &val);
    650 	switch (val) {
    651 	case AXEN_USB_FS:
    652 		DPRINTF(("uplink: USB1.1\n"));
    653 		qctrl.ctrl	 = 0x07;
    654 		qctrl.timer_low	 = 0xcc;
    655 		qctrl.timer_high = 0x4c;
    656 		qctrl.bufsize	 = AXEN_BUFSZ_LS - 1;
    657 		qctrl.ifg	 = 0x08;
    658 		break;
    659 	case AXEN_USB_HS:
    660 		DPRINTF(("uplink: USB2.0\n"));
    661 		qctrl.ctrl	 = 0x07;
    662 		qctrl.timer_low	 = 0x02;
    663 		qctrl.timer_high = 0xa0;
    664 		qctrl.bufsize	 = AXEN_BUFSZ_HS - 1;
    665 		qctrl.ifg	 = 0xff;
    666 		break;
    667 	case AXEN_USB_SS:
    668 		DPRINTF(("uplink: USB3.0\n"));
    669 		qctrl.ctrl	 = 0x07;
    670 		qctrl.timer_low	 = 0x4f;
    671 		qctrl.timer_high = 0x00;
    672 		qctrl.bufsize	 = AXEN_BUFSZ_SS - 1;
    673 		qctrl.ifg	 = 0xff;
    674 		break;
    675 	default:
    676 		aprint_error_dev(sc->axen_dev, "unknown uplink bus:0x%02x\n",
    677 		    val);
    678 		axen_unlock_mii(sc);
    679 		return;
    680 	}
    681 	axen_cmd(sc, AXEN_CMD_MAC_SET_RXSR, 5, AXEN_RX_BULKIN_QCTRL, &qctrl);
    682 
    683 	/*
    684 	 * set buffer high/low watermark to pause/resume.
    685 	 * write 2byte will set high/log simultaneous with AXEN_PAUSE_HIGH.
    686 	 * XXX: what is the best value? OSX driver uses 0x3c-0x4c as LOW-HIGH
    687 	 * watermark parameters.
    688 	 */
    689 	val = 0x34;
    690 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_LOW_WATERMARK, &val);
    691 	val = 0x52;
    692 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_HIGH_WATERMARK, &val);
    693 
    694 	/* Set RX/TX configuration. */
    695 	/* Set RX control register */
    696 	ctl = AXEN_RXCTL_IPE | AXEN_RXCTL_DROPCRCERR | AXEN_RXCTL_AUTOB;
    697 	wval = htole16(ctl);
    698 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
    699 
    700 	/* set monitor mode (enable) */
    701 	val = AXEN_MONITOR_PMETYPE | AXEN_MONITOR_PMEPOL | AXEN_MONITOR_RWMP;
    702 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val);
    703 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_MONITOR_MODE, &val);
    704 	DPRINTF(("axen: Monitor mode = 0x%02x\n", val));
    705 
    706 	/* set medium type */
    707 	ctl = AXEN_MEDIUM_GIGA | AXEN_MEDIUM_FDX | AXEN_MEDIUM_EN_125MHZ |
    708 	    AXEN_MEDIUM_RXFLOW_CTRL_EN | AXEN_MEDIUM_TXFLOW_CTRL_EN |
    709 	    AXEN_MEDIUM_RECV_EN;
    710 	wval = htole16(ctl);
    711 	DPRINTF(("axen: set to medium mode: 0x%04x\n", ctl));
    712 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MEDIUM_STATUS, &wval);
    713 	usbd_delay_ms(sc->axen_udev, 100);
    714 
    715 	axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MEDIUM_STATUS, &wval);
    716 	DPRINTF(("axen: current medium mode: 0x%04x\n", le16toh(wval)));
    717 
    718 	axen_unlock_mii(sc);
    719 
    720 #if 0 /* XXX: TBD.... */
    721 #define GMII_LED_ACTIVE		0x1a
    722 #define GMII_PHY_PAGE_SEL	0x1e
    723 #define GMII_PHY_PAGE_SEL	0x1f
    724 #define GMII_PAGE_EXT		0x0007
    725 	axen_miibus_writereg(&sc->axen_dev, sc->axen_phyno, GMII_PHY_PAGE_SEL,
    726 	    GMII_PAGE_EXT);
    727 	axen_miibus_writereg(&sc->axen_dev, sc->axen_phyno, GMII_PHY_PAGE,
    728 	    0x002c);
    729 #endif
    730 
    731 #if 1 /* XXX: phy hack ? */
    732 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x1F, 0x0005);
    733 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x0C, 0x0000);
    734 	axen_miibus_readreg(sc->axen_dev, sc->axen_phyno, 0x0001, &wval);
    735 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x01,
    736 	    wval | 0x0080);
    737 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x1F, 0x0000);
    738 #endif
    739 }
    740 
    741 static void
    742 axen_setcoe(struct axen_softc *sc)
    743 {
    744 	struct ifnet *ifp = GET_IFP(sc);
    745 	uint64_t enabled = ifp->if_capenable;
    746 	uint8_t val;
    747 
    748 	KASSERT(mutex_owned(&sc->axen_mii_lock));
    749 
    750 	val = AXEN_RXCOE_OFF;
    751 	if (enabled & IFCAP_CSUM_IPv4_Rx)
    752 		val |= AXEN_RXCOE_IPv4;
    753 	if (enabled & IFCAP_CSUM_TCPv4_Rx)
    754 		val |= AXEN_RXCOE_TCPv4;
    755 	if (enabled & IFCAP_CSUM_UDPv4_Rx)
    756 		val |= AXEN_RXCOE_UDPv4;
    757 	if (enabled & IFCAP_CSUM_TCPv6_Rx)
    758 		val |= AXEN_RXCOE_TCPv6;
    759 	if (enabled & IFCAP_CSUM_UDPv6_Rx)
    760 		val |= AXEN_RXCOE_UDPv6;
    761 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_RX_COE, &val);
    762 
    763 	val = AXEN_TXCOE_OFF;
    764 	if (enabled & IFCAP_CSUM_IPv4_Tx)
    765 		val |= AXEN_TXCOE_IPv4;
    766 	if (enabled & IFCAP_CSUM_TCPv4_Tx)
    767 		val |= AXEN_TXCOE_TCPv4;
    768 	if (enabled & IFCAP_CSUM_UDPv4_Tx)
    769 		val |= AXEN_TXCOE_UDPv4;
    770 	if (enabled & IFCAP_CSUM_TCPv6_Tx)
    771 		val |= AXEN_TXCOE_TCPv6;
    772 	if (enabled & IFCAP_CSUM_UDPv6_Tx)
    773 		val |= AXEN_TXCOE_UDPv6;
    774 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_TX_COE, &val);
    775 }
    776 
    777 static int
    778 axen_match(device_t parent, cfdata_t match, void *aux)
    779 {
    780 	struct usb_attach_arg *uaa = aux;
    781 
    782 	return axen_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    783 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    784 }
    785 
    786 static void
    787 axen_attach(device_t parent, device_t self, void *aux)
    788 {
    789 	struct axen_softc * const sc = device_private(self);
    790 	struct usb_attach_arg *uaa = aux;
    791 	struct usbd_device *dev = uaa->uaa_device;
    792 	usbd_status err;
    793 	usb_interface_descriptor_t *id;
    794 	usb_endpoint_descriptor_t *ed;
    795 	struct mii_data	*mii;
    796 	uint8_t eaddr[ETHER_ADDR_LEN];
    797 	char *devinfop;
    798 	const char *devname = device_xname(self);
    799 	struct ifnet *ifp;
    800 	int i;
    801 
    802 	aprint_naive("\n");
    803 	aprint_normal("\n");
    804 
    805 	sc->axen_dev = self;
    806 	sc->axen_udev = dev;
    807 
    808 	devinfop = usbd_devinfo_alloc(dev, 0);
    809 	aprint_normal_dev(self, "%s\n", devinfop);
    810 	usbd_devinfo_free(devinfop);
    811 
    812 	err = usbd_set_config_no(dev, AXEN_CONFIG_NO, 1);
    813 	if (err) {
    814 		aprint_error_dev(self, "failed to set configuration"
    815 		    ", err=%s\n", usbd_errstr(err));
    816 		return;
    817 	}
    818 
    819 	sc->axen_flags = axen_lookup(uaa->uaa_vendor, uaa->uaa_product)->axen_flags;
    820 
    821 	usb_init_task(&sc->axen_tick_task, axen_tick_task, sc, USB_TASKQ_MPSAFE);
    822 
    823 	err = usbd_device2interface_handle(dev, AXEN_IFACE_IDX,&sc->axen_iface);
    824 	if (err) {
    825 		aprint_error_dev(self, "getting interface handle failed\n");
    826 		return;
    827 	}
    828 
    829 	sc->axen_product = uaa->uaa_product;
    830 	sc->axen_vendor = uaa->uaa_vendor;
    831 
    832 	id = usbd_get_interface_descriptor(sc->axen_iface);
    833 
    834 	/* decide on what our bufsize will be */
    835 	switch (sc->axen_udev->ud_speed) {
    836 	case USB_SPEED_SUPER:
    837 		sc->axen_rx_bufsz = AXEN_BUFSZ_SS * 1024;
    838 		break;
    839 	case USB_SPEED_HIGH:
    840 		sc->axen_rx_bufsz = AXEN_BUFSZ_HS * 1024;
    841 		break;
    842 	default:
    843 		sc->axen_rx_bufsz = AXEN_BUFSZ_LS * 1024;
    844 		break;
    845 	}
    846 
    847 	sc->axen_tx_bufsz = IP_MAXPACKET +
    848 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN +
    849 	    sizeof(struct axen_sframe_hdr);
    850 
    851 	/* Find endpoints. */
    852 	for (i = 0; i < id->bNumEndpoints; i++) {
    853 		ed = usbd_interface2endpoint_descriptor(sc->axen_iface, i);
    854 		if (!ed) {
    855 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    856 			return;
    857 		}
    858 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    859 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    860 			sc->axen_ed[AXEN_ENDPT_RX] = ed->bEndpointAddress;
    861 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    862 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    863 			sc->axen_ed[AXEN_ENDPT_TX] = ed->bEndpointAddress;
    864 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    865 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    866 			sc->axen_ed[AXEN_ENDPT_INTR] = ed->bEndpointAddress;
    867 		}
    868 	}
    869 
    870 	/* Set these up now for axen_cmd().  */
    871 	mutex_init(&sc->axen_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    872 	mutex_init(&sc->axen_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    873 	mutex_init(&sc->axen_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    874 	mutex_init(&sc->axen_lock, MUTEX_DEFAULT, IPL_NONE);
    875 	cv_init(&sc->axen_detachcv, "axendet");
    876 
    877 	sc->axen_phyno = AXEN_PHY_ID;
    878 	DPRINTF(("%s: phyno %d\n", device_xname(self), sc->axen_phyno));
    879 
    880 	/* Get station address.  */
    881 	axen_lock_mii(sc);
    882 	if (axen_get_eaddr(sc, &eaddr)) {
    883 		axen_unlock_mii(sc);
    884 		printf("EEPROM checksum error\n");
    885 		cv_destroy(&sc->axen_detachcv);
    886 		mutex_destroy(&sc->axen_lock);
    887 		mutex_destroy(&sc->axen_rxlock);
    888 		mutex_destroy(&sc->axen_txlock);
    889 		mutex_destroy(&sc->axen_mii_lock);
    890 		return;
    891 	}
    892 	axen_unlock_mii(sc);
    893 
    894 	axen_ax88179_init(sc);
    895 
    896 	/*
    897 	 * An ASIX chip was detected. Inform the world.
    898 	 */
    899 	if (sc->axen_flags & AX178A)
    900 		aprint_normal_dev(self, "AX88178a\n");
    901 	else if (sc->axen_flags & AX179)
    902 		aprint_normal_dev(self, "AX88179\n");
    903 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    904 
    905 	/* Initialize interface info.*/
    906 
    907 	ifp = &sc->sc_if;
    908 	ifp->if_softc = sc;
    909 	strlcpy(ifp->if_xname, devname, IFNAMSIZ);
    910 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    911 	ifp->if_extflags = IFEF_MPSAFE;
    912 	ifp->if_ioctl = axen_ioctl;
    913 	ifp->if_start = axen_start;
    914 	ifp->if_init = axen_init;
    915 	ifp->if_stop = axen_stop;
    916 
    917 	IFQ_SET_READY(&ifp->if_snd);
    918 
    919 	sc->axen_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
    920 
    921 	/* Adapter does not support TSOv6 (They call it LSOv2). */
    922 	ifp->if_capabilities |= IFCAP_TSOv4 |
    923 	    IFCAP_CSUM_IPv4_Rx	| IFCAP_CSUM_IPv4_Tx  |
    924 	    IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_TCPv4_Tx |
    925 	    IFCAP_CSUM_UDPv4_Rx | IFCAP_CSUM_UDPv4_Tx |
    926 	    IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_TCPv6_Tx |
    927 	    IFCAP_CSUM_UDPv6_Rx | IFCAP_CSUM_UDPv6_Tx;
    928 
    929 	/* Initialize MII/media info. */
    930 	mii = &sc->axen_mii;
    931 	mii->mii_ifp = ifp;
    932 	mii->mii_readreg = axen_miibus_readreg;
    933 	mii->mii_writereg = axen_miibus_writereg;
    934 	mii->mii_statchg = axen_miibus_statchg;
    935 	mii->mii_flags = MIIF_AUTOTSLEEP;
    936 
    937 	sc->axen_ec.ec_mii = mii;
    938 	ifmedia_init(&mii->mii_media, 0, axen_ifmedia_upd, axen_ifmedia_sts);
    939 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    940 
    941 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    942 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    943 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    944 	} else
    945 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    946 
    947 	/* Attach the interface. */
    948 	if_attach(ifp);
    949 	ether_ifattach(ifp, eaddr);
    950 	rnd_attach_source(&sc->rnd_source, device_xname(sc->axen_dev),
    951 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    952 
    953 	callout_init(&sc->axen_stat_ch, CALLOUT_MPSAFE);
    954 	callout_setfunc(&sc->axen_stat_ch, axen_tick, sc);
    955 
    956 	sc->axen_attached = true;
    957 
    958 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axen_udev,sc->axen_dev);
    959 
    960 	if (!pmf_device_register(self, NULL, NULL))
    961 		aprint_error_dev(self, "couldn't establish power handler\n");
    962 }
    963 
    964 static int
    965 axen_detach(device_t self, int flags)
    966 {
    967 	struct axen_softc * const sc = device_private(self);
    968 	struct ifnet *ifp = GET_IFP(sc);
    969 
    970 	mutex_enter(&sc->axen_lock);
    971 	sc->axen_dying = true;
    972 	mutex_exit(&sc->axen_lock);
    973 
    974 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
    975 
    976 	/* Detached before attached finished, so just bail out. */
    977 	if (!sc->axen_attached)
    978 		return 0;
    979 
    980 	pmf_device_deregister(self);
    981 
    982 	callout_halt(&sc->axen_stat_ch, NULL);
    983 	usb_rem_task_wait(sc->axen_udev, &sc->axen_tick_task,
    984 	    USB_TASKQ_DRIVER, NULL);
    985 
    986 	if (ifp->if_flags & IFF_RUNNING) {
    987 		IFNET_LOCK(ifp);
    988 		axen_stop(ifp, 1);
    989 		IFNET_UNLOCK(ifp);
    990 	}
    991 
    992 	mutex_enter(&sc->axen_lock);
    993 	sc->axen_refcnt--;
    994 	while (sc->axen_refcnt > 0) {
    995 		/* Wait for processes to go away */
    996 		cv_wait(&sc->axen_detachcv, &sc->axen_lock);
    997 	}
    998 
    999 #ifdef DIAGNOSTIC
   1000 	if (sc->axen_ep[AXEN_ENDPT_TX] != NULL ||
   1001 	    sc->axen_ep[AXEN_ENDPT_RX] != NULL ||
   1002 	    sc->axen_ep[AXEN_ENDPT_INTR] != NULL)
   1003 		aprint_debug_dev(self, "detach has active endpoints\n");
   1004 #endif
   1005 
   1006 	mutex_exit(&sc->axen_lock);
   1007 
   1008 	callout_destroy(&sc->axen_stat_ch);
   1009 	rnd_detach_source(&sc->rnd_source);
   1010 	mii_detach(&sc->axen_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1011 	ifmedia_delete_instance(&sc->axen_mii.mii_media, IFM_INST_ANY);
   1012 	ether_ifdetach(ifp);
   1013 	if_detach(ifp);
   1014 
   1015 	sc->axen_attached = false;
   1016 
   1017 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axen_udev,sc->axen_dev);
   1018 
   1019 	cv_destroy(&sc->axen_detachcv);
   1020 	mutex_destroy(&sc->axen_lock);
   1021 	mutex_destroy(&sc->axen_rxlock);
   1022 	mutex_destroy(&sc->axen_txlock);
   1023 	mutex_destroy(&sc->axen_mii_lock);
   1024 
   1025 	return 0;
   1026 }
   1027 
   1028 static int
   1029 axen_activate(device_t self, devact_t act)
   1030 {
   1031 	struct axen_softc * const sc = device_private(self);
   1032 	struct ifnet *ifp = GET_IFP(sc);
   1033 
   1034 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1035 
   1036 	switch (act) {
   1037 	case DVACT_DEACTIVATE:
   1038 		if_deactivate(ifp);
   1039 
   1040 		mutex_enter(&sc->axen_lock);
   1041 		sc->axen_dying = true;
   1042 		mutex_exit(&sc->axen_lock);
   1043 
   1044 		mutex_enter(&sc->axen_rxlock);
   1045 		mutex_enter(&sc->axen_txlock);
   1046 		sc->axen_stopping = true;
   1047 		mutex_exit(&sc->axen_txlock);
   1048 		mutex_exit(&sc->axen_rxlock);
   1049 
   1050 		return 0;
   1051 	default:
   1052 		return EOPNOTSUPP;
   1053 	}
   1054 }
   1055 
   1056 static struct mbuf *
   1057 axen_newbuf(void)
   1058 {
   1059 	struct mbuf *m;
   1060 
   1061 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1062 	if (m == NULL)
   1063 		return NULL;
   1064 
   1065 	MCLGET(m, M_DONTWAIT);
   1066 	if (!(m->m_flags & M_EXT)) {
   1067 		m_freem(m);
   1068 		return NULL;
   1069 	}
   1070 
   1071 	m->m_len = m->m_pkthdr.len = MCLBYTES;
   1072 	m_adj(m, ETHER_ALIGN);
   1073 
   1074 	return m;
   1075 }
   1076 
   1077 static int
   1078 axen_rx_list_init(struct axen_softc *sc)
   1079 {
   1080 	struct axen_cdata *cd;
   1081 	struct axen_chain *c;
   1082 	int i;
   1083 
   1084 	DPRINTF(("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1085 
   1086 	cd = &sc->axen_cdata;
   1087 	for (i = 0; i < AXEN_RX_LIST_CNT; i++) {
   1088 		c = &cd->axen_rx_chain[i];
   1089 		c->axen_sc = sc;
   1090 		if (c->axen_xfer == NULL) {
   1091 			int err = usbd_create_xfer(sc->axen_ep[AXEN_ENDPT_RX],
   1092 			    sc->axen_rx_bufsz, 0, 0, &c->axen_xfer);
   1093 			if (err)
   1094 				return err;
   1095 			c->axen_buf = usbd_get_buffer(c->axen_xfer);
   1096 		}
   1097 	}
   1098 
   1099 	return 0;
   1100 }
   1101 
   1102 static int
   1103 axen_tx_list_init(struct axen_softc *sc)
   1104 {
   1105 	struct axen_cdata *cd;
   1106 	struct axen_chain *c;
   1107 	int i;
   1108 
   1109 	DPRINTF(("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1110 
   1111 	cd = &sc->axen_cdata;
   1112 	for (i = 0; i < AXEN_TX_LIST_CNT; i++) {
   1113 		c = &cd->axen_tx_chain[i];
   1114 		c->axen_sc = sc;
   1115 		if (c->axen_xfer == NULL) {
   1116 			int err = usbd_create_xfer(sc->axen_ep[AXEN_ENDPT_TX],
   1117 			    sc->axen_tx_bufsz, USBD_FORCE_SHORT_XFER, 0,
   1118 			    &c->axen_xfer);
   1119 			if (err)
   1120 				return err;
   1121 			c->axen_buf = usbd_get_buffer(c->axen_xfer);
   1122 		}
   1123 	}
   1124 
   1125 	cd->axen_tx_prod = cd->axen_tx_cnt = 0;
   1126 
   1127 	return 0;
   1128 }
   1129 
   1130 /*
   1131  * A frame has been uploaded: pass the resulting mbuf chain up to
   1132  * the higher level protocols.
   1133  */
   1134 static void
   1135 axen_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1136 {
   1137 	struct axen_chain *c = (struct axen_chain *)priv;
   1138 	struct axen_softc * const sc = c->axen_sc;
   1139 	struct ifnet *ifp = GET_IFP(sc);
   1140 	uint8_t *buf = c->axen_buf;
   1141 	struct mbuf *m;
   1142 	uint32_t total_len;
   1143 	uint32_t rx_hdr, pkt_hdr;
   1144 	uint32_t *hdr_p;
   1145 	uint16_t hdr_offset, pkt_count;
   1146 	size_t pkt_len;
   1147 	size_t temp;
   1148 
   1149 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1150 
   1151 	mutex_enter(&sc->axen_rxlock);
   1152 
   1153 	if (sc->axen_dying || sc->axen_stopping ||
   1154 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
   1155 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) {
   1156 		mutex_exit(&sc->axen_rxlock);
   1157 		return;
   1158 	}
   1159 
   1160 	if (status != USBD_NORMAL_COMPLETION) {
   1161 		if (usbd_ratecheck(&sc->axen_rx_notice))
   1162 			aprint_error_dev(sc->axen_dev, "usb errors on rx: %s\n",
   1163 			    usbd_errstr(status));
   1164 		if (status == USBD_STALLED)
   1165 			usbd_clear_endpoint_stall_async(sc->axen_ep[AXEN_ENDPT_RX]);
   1166 		goto done;
   1167 	}
   1168 
   1169 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1170 
   1171 	if (total_len < sizeof(pkt_hdr)) {
   1172 		aprint_error_dev(sc->axen_dev, "rxeof: too short transfer\n");
   1173 		ifp->if_ierrors++;
   1174 		goto done;
   1175 	}
   1176 
   1177 	/*
   1178 	 * buffer map
   1179 	 * [packet #0]...[packet #n][pkt hdr#0]..[pkt hdr#n][recv_hdr]
   1180 	 * each packet has 0xeeee as psuedo header..
   1181 	 */
   1182 	hdr_p = (uint32_t *)(buf + total_len - sizeof(uint32_t));
   1183 	rx_hdr = le32toh(*hdr_p);
   1184 	hdr_offset = (uint16_t)(rx_hdr >> 16);
   1185 	pkt_count  = (uint16_t)(rx_hdr & 0xffff);
   1186 
   1187 	if (total_len > sc->axen_rx_bufsz) {
   1188 		aprint_error_dev(sc->axen_dev,
   1189 		    "rxeof: too large transfer (%u > %u)\n",
   1190 		    total_len, sc->axen_rx_bufsz);
   1191 		goto done;
   1192 	}
   1193 
   1194 	/* sanity check */
   1195 	if (hdr_offset > total_len) {
   1196 		aprint_error_dev(sc->axen_dev,
   1197 		    "rxeof: invalid hdr offset (%u > %u)\n",
   1198 		    hdr_offset, total_len);
   1199 		ifp->if_ierrors++;
   1200 		usbd_delay_ms(sc->axen_udev, 100);
   1201 		goto done;
   1202 	}
   1203 
   1204 	/* point first packet header */
   1205 	hdr_p = (uint32_t *)(buf + hdr_offset);
   1206 
   1207 	/*
   1208 	 * ax88179 will pack multiple ip packet to a USB transaction.
   1209 	 * process all of packets in the buffer
   1210 	 */
   1211 
   1212 #if 1 /* XXX: paranoiac check. need to remove later */
   1213 #define AXEN_MAX_PACKED_PACKET 200
   1214 	if (pkt_count > AXEN_MAX_PACKED_PACKET) {
   1215 		DPRINTF(("%s: Too many packets (%d) in a transaction, discard.\n",
   1216 		    device_xname(sc->axen_dev), pkt_count));
   1217 		goto done;
   1218 	}
   1219 #endif
   1220 
   1221 	do {
   1222 		if ((buf[0] != 0xee) || (buf[1] != 0xee)) {
   1223 			aprint_error_dev(sc->axen_dev,
   1224 			    "invalid buffer(pkt#%d), continue\n", pkt_count);
   1225 			ifp->if_ierrors += pkt_count;
   1226 			goto done;
   1227 		}
   1228 
   1229 		pkt_hdr = le32toh(*hdr_p);
   1230 		pkt_len = (pkt_hdr >> 16) & 0x1fff;
   1231 		DPRINTFN(10,
   1232 		    ("%s: rxeof: packet#%d, pkt_hdr 0x%08x, pkt_len %zu\n",
   1233 		   device_xname(sc->axen_dev), pkt_count, pkt_hdr, pkt_len));
   1234 
   1235 		if (pkt_hdr & (AXEN_RXHDR_CRC_ERR | AXEN_RXHDR_DROP_ERR)) {
   1236 			ifp->if_ierrors++;
   1237 			/* move to next pkt header */
   1238 			DPRINTF(("%s: %s err (pkt#%d)\n",
   1239 			    device_xname(sc->axen_dev),
   1240 			    (pkt_hdr & AXEN_RXHDR_CRC_ERR) ? "crc" : "drop",
   1241 			    pkt_count));
   1242 			goto nextpkt;
   1243 		}
   1244 
   1245 		/* process each packet */
   1246 		/* allocate mbuf */
   1247 		m = axen_newbuf();
   1248 		if (m == NULL) {
   1249 			ifp->if_ierrors++;
   1250 			goto nextpkt;
   1251 		}
   1252 
   1253 		/* skip pseudo header (2byte) */
   1254 		m_set_rcvif(m, ifp);
   1255 		m->m_pkthdr.len = m->m_len = pkt_len - 6;
   1256 
   1257 		m->m_pkthdr.csum_flags = axen_csum_flags_rx(ifp, pkt_hdr);
   1258 		memcpy(mtod(m, char *), buf + 2, pkt_len - 6);
   1259 
   1260 		mutex_exit(&sc->axen_rxlock);
   1261 
   1262 		/* push the packet up */
   1263 		if_percpuq_enqueue((ifp)->if_percpuq, (m));
   1264 
   1265 		mutex_enter(&sc->axen_rxlock);
   1266 		if (sc->axen_dying || sc->axen_stopping) {
   1267 			mutex_exit(&sc->axen_rxlock);
   1268 			return;
   1269 		}
   1270 
   1271 nextpkt:
   1272 		/*
   1273 		 * prepare next packet
   1274 		 * as each packet will be aligned 8byte boundary,
   1275 		 * need to fix up the start point of the buffer.
   1276 		 */
   1277 		temp = ((pkt_len + 7) & 0xfff8);
   1278 		buf = buf + temp;
   1279 		hdr_p++;
   1280 		pkt_count--;
   1281 	} while (pkt_count > 0);
   1282 
   1283 done:
   1284 	if (sc->axen_dying || sc->axen_stopping) {
   1285 		mutex_exit(&sc->axen_rxlock);
   1286 		return;
   1287 	}
   1288 
   1289 	mutex_exit(&sc->axen_rxlock);
   1290 
   1291 	/* Setup new transfer. */
   1292 	usbd_setup_xfer(xfer, c, c->axen_buf, sc->axen_rx_bufsz,
   1293 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axen_rxeof);
   1294 	usbd_transfer(xfer);
   1295 
   1296 	DPRINTFN(10,("%s: %s: start rx\n",device_xname(sc->axen_dev),__func__));
   1297 }
   1298 
   1299 static int
   1300 axen_csum_flags_rx(struct ifnet *ifp, uint32_t pkt_hdr)
   1301 {
   1302 	int enabled_flags = ifp->if_csum_flags_rx;
   1303 	int csum_flags = 0;
   1304 	int l3_type, l4_type;
   1305 
   1306 	if (enabled_flags == 0)
   1307 		return 0;
   1308 
   1309 	l3_type = (pkt_hdr & AXEN_RXHDR_L3_TYPE_MASK) >>
   1310 	    AXEN_RXHDR_L3_TYPE_OFFSET;
   1311 
   1312 	if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4)
   1313 		csum_flags |= M_CSUM_IPv4;
   1314 
   1315 	l4_type = (pkt_hdr & AXEN_RXHDR_L4_TYPE_MASK) >>
   1316 	    AXEN_RXHDR_L4_TYPE_OFFSET;
   1317 
   1318 	switch (l4_type) {
   1319 	case AXEN_RXHDR_L4_TYPE_TCP:
   1320 		if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4)
   1321 			csum_flags |= M_CSUM_TCPv4;
   1322 		else
   1323 			csum_flags |= M_CSUM_TCPv6;
   1324 		break;
   1325 	case AXEN_RXHDR_L4_TYPE_UDP:
   1326 		if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4)
   1327 			csum_flags |= M_CSUM_UDPv4;
   1328 		else
   1329 			csum_flags |= M_CSUM_UDPv6;
   1330 		break;
   1331 	default:
   1332 		break;
   1333 	}
   1334 
   1335 	csum_flags &= enabled_flags;
   1336 	if ((csum_flags & M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L3CSUM_ERR))
   1337 		csum_flags |= M_CSUM_IPv4_BAD;
   1338 	if ((csum_flags & ~M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L4CSUM_ERR))
   1339 		csum_flags |= M_CSUM_TCP_UDP_BAD;
   1340 
   1341 	return csum_flags;
   1342 }
   1343 
   1344 /*
   1345  * A frame was downloaded to the chip. It's safe for us to clean up
   1346  * the list buffers.
   1347  */
   1348 static void
   1349 axen_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1350 {
   1351 	struct axen_chain *c = (struct axen_chain *)priv;
   1352 	struct axen_softc * const sc = c->axen_sc;
   1353 	struct axen_cdata *cd = &sc->axen_cdata;
   1354 	struct ifnet *ifp = GET_IFP(sc);
   1355 
   1356 	mutex_enter(&sc->axen_txlock);
   1357 	if (sc->axen_stopping || sc->axen_dying) {
   1358 		mutex_exit(&sc->axen_txlock);
   1359 		return;
   1360 	}
   1361 
   1362 	KASSERT(cd->axen_tx_cnt > 0);
   1363 	cd->axen_tx_cnt--;
   1364 
   1365 	sc->axen_timer = 0;
   1366 
   1367 	switch (status) {
   1368 	case USBD_NOT_STARTED:
   1369 	case USBD_CANCELLED:
   1370 		break;
   1371 
   1372 	case USBD_NORMAL_COMPLETION:
   1373 		ifp->if_opackets++;
   1374 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1375 			axen_start_locked(ifp);
   1376 		break;
   1377 
   1378 	default:
   1379 
   1380 		ifp->if_oerrors++;
   1381 		if (usbd_ratecheck(&sc->axen_tx_notice))
   1382 			aprint_error_dev(sc->axen_dev, "usb error on tx: %s\n",
   1383 			    usbd_errstr(status));
   1384 		if (status == USBD_STALLED)
   1385 			usbd_clear_endpoint_stall_async(sc->axen_ep[AXEN_ENDPT_TX]);
   1386 		break;
   1387 	}
   1388 
   1389 	mutex_exit(&sc->axen_txlock);
   1390 }
   1391 
   1392 static void
   1393 axen_tick(void *xsc)
   1394 {
   1395 	struct axen_softc * const sc = xsc;
   1396 
   1397 	if (sc == NULL)
   1398 		return;
   1399 
   1400 	DPRINTFN(0xff,("%s: %s: enter\n", device_xname(sc->axen_dev),__func__));
   1401 
   1402 	mutex_enter(&sc->axen_lock);
   1403 	if (!sc->axen_stopping && !sc->axen_dying) {
   1404 		/* Perform periodic stuff in process context */
   1405 		usb_add_task(sc->axen_udev, &sc->axen_tick_task, USB_TASKQ_DRIVER);
   1406 	}
   1407 	mutex_exit(&sc->axen_lock);
   1408 }
   1409 
   1410 static void
   1411 axen_tick_task(void *xsc)
   1412 {
   1413 	struct axen_softc * const sc = xsc;
   1414 	struct ifnet *ifp;
   1415 	struct mii_data *mii;
   1416 
   1417 	if (sc == NULL)
   1418 		return;
   1419 
   1420 	mutex_enter(&sc->axen_lock);
   1421 	if (sc->axen_stopping || sc->axen_dying) {
   1422 		mutex_exit(&sc->axen_lock);
   1423 		return;
   1424 	}
   1425 
   1426 	ifp = GET_IFP(sc);
   1427 	mii = GET_MII(sc);
   1428 	if (mii == NULL) {
   1429 		mutex_exit(&sc->axen_lock);
   1430 		return;
   1431 	}
   1432 
   1433 	sc->axen_refcnt++;
   1434 	mutex_exit(&sc->axen_lock);
   1435 
   1436 	if (sc->axen_timer != 0 && --sc->axen_timer == 0)
   1437 		axen_watchdog(ifp);
   1438 
   1439 	mii_tick(mii);
   1440 
   1441 	if (sc->axen_link == 0)
   1442 		axen_miibus_statchg(ifp);
   1443 
   1444 	mutex_enter(&sc->axen_lock);
   1445 	if (--sc->axen_refcnt < 0)
   1446 		cv_broadcast(&sc->axen_detachcv);
   1447 	if (!sc->axen_stopping && !sc->axen_dying)
   1448 		callout_schedule(&sc->axen_stat_ch, hz);
   1449 	mutex_exit(&sc->axen_lock);
   1450 }
   1451 
   1452 static int
   1453 axen_encap(struct axen_softc *sc, struct mbuf *m, int idx)
   1454 {
   1455 	struct ifnet *ifp = GET_IFP(sc);
   1456 	struct axen_chain *c;
   1457 	usbd_status err;
   1458 	struct axen_sframe_hdr hdr;
   1459 	u_int length, boundary;
   1460 
   1461 	KASSERT(mutex_owned(&sc->axen_txlock));
   1462 
   1463 	c = &sc->axen_cdata.axen_tx_chain[idx];
   1464 
   1465 	/* XXX Is this needed?  wMaxPacketSize? */
   1466 	switch (sc->axen_udev->ud_speed) {
   1467 	case USB_SPEED_SUPER:
   1468 		boundary = 4096;
   1469 		break;
   1470 	case USB_SPEED_HIGH:
   1471 		boundary = 512;
   1472 		break;
   1473 	default:
   1474 		boundary = 64;
   1475 		break;
   1476 	}
   1477 
   1478 	length = m->m_pkthdr.len + sizeof(hdr);
   1479 	KASSERT(length <= sc->axen_tx_bufsz);
   1480 
   1481 	hdr.plen = htole32(m->m_pkthdr.len);
   1482 
   1483 	hdr.gso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) ?
   1484 	    m->m_pkthdr.segsz : 0;
   1485 	if ((length % boundary) == 0) {
   1486 		DPRINTF(("%s: boundary hit\n", device_xname(sc->axen_dev)));
   1487 		hdr.gso |= 0x80008000;	/* XXX enable padding */
   1488 	}
   1489 	hdr.gso = htole32(hdr.gso);
   1490 
   1491 	memcpy(c->axen_buf, &hdr, sizeof(hdr));
   1492 	m_copydata(m, 0, m->m_pkthdr.len, c->axen_buf + sizeof(hdr));
   1493 
   1494 	if (__predict_false(c->axen_xfer == NULL))
   1495 		return EIO;	/* XXX plugged out or down */
   1496 
   1497 	usbd_setup_xfer(c->axen_xfer, c, c->axen_buf, length,
   1498 	    USBD_FORCE_SHORT_XFER, 10000, axen_txeof);
   1499 
   1500 	/* Transmit */
   1501 	err = usbd_transfer(c->axen_xfer);
   1502 	if (err != USBD_IN_PROGRESS) {
   1503 		/* XXXSMP IFNET_LOCK */
   1504 		axen_stop(ifp, 0);
   1505 		return EIO;
   1506 	}
   1507 
   1508 	return 0;
   1509 }
   1510 
   1511 static void
   1512 axen_start_locked(struct ifnet *ifp)
   1513 {
   1514 	struct axen_softc * const sc = ifp->if_softc;
   1515 	struct mbuf *m;
   1516 	struct axen_cdata *cd = &sc->axen_cdata;
   1517 	int idx;
   1518 
   1519 	KASSERT(mutex_owned(&sc->axen_txlock));
   1520 	KASSERT(cd->axen_tx_cnt <= AXEN_TX_LIST_CNT);
   1521 
   1522 	if (sc->axen_link == 0 || (ifp->if_flags & IFF_RUNNING) == 0)
   1523 		return;
   1524 
   1525 	idx = cd->axen_tx_prod;
   1526 	while (cd->axen_tx_cnt < AXEN_TX_LIST_CNT) {
   1527 		IFQ_POLL(&ifp->if_snd, m);
   1528 		if (m == NULL)
   1529 			break;
   1530 
   1531 		if (axen_encap(sc, m, idx)) {
   1532 			ifp->if_oerrors++;
   1533 			break;
   1534 		}
   1535 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1536 
   1537 		/*
   1538 		 * If there's a BPF listener, bounce a copy of this frame
   1539 		 * to him.
   1540 		 */
   1541 		bpf_mtap(ifp, m, BPF_D_OUT);
   1542 		m_freem(m);
   1543 
   1544 		idx = (idx + 1) % AXEN_TX_LIST_CNT;
   1545 		cd->axen_tx_cnt++;
   1546 	}
   1547 	cd->axen_tx_prod = idx;
   1548 
   1549 	/*
   1550 	 * Set a timeout in case the chip goes out to lunch.
   1551 	 */
   1552 	sc->axen_timer = 5;
   1553 }
   1554 
   1555 static void
   1556 axen_start(struct ifnet *ifp)
   1557 {
   1558 	struct axen_softc * const sc = ifp->if_softc;
   1559 
   1560 	mutex_enter(&sc->axen_txlock);
   1561 	if (!sc->axen_stopping)
   1562 		axen_start_locked(ifp);
   1563 	mutex_exit(&sc->axen_txlock);
   1564 }
   1565 
   1566 static int
   1567 axen_init_locked(struct ifnet *ifp)
   1568 {
   1569 	struct axen_softc * const sc = ifp->if_softc;
   1570 	struct axen_chain *c;
   1571 	usbd_status err;
   1572 	int i;
   1573 	uint16_t rxmode;
   1574 	uint16_t wval;
   1575 	uint8_t bval;
   1576 
   1577 	KASSERT(mutex_owned(&sc->axen_lock));
   1578 
   1579 	if (sc->axen_dying)
   1580 		return EIO;
   1581 
   1582 	/* Cancel pending I/O */
   1583 	axen_stop_locked(ifp, 1);
   1584 
   1585 	/* Reset the ethernet interface. */
   1586 	axen_reset(sc);
   1587 
   1588 	axen_lock_mii_sc_locked(sc);
   1589 
   1590 	/* XXX: ? */
   1591 	bval = 0x01;
   1592 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_UNK_28, &bval);
   1593 
   1594 	/* Configure offloading engine. */
   1595 	axen_setcoe(sc);
   1596 
   1597 	/* Program promiscuous mode and multicast filters. */
   1598 	axen_iff_locked(sc);
   1599 
   1600 	/* Enable receiver, set RX mode */
   1601 	axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval);
   1602 	rxmode = le16toh(wval);
   1603 	rxmode |= AXEN_RXCTL_START;
   1604 	wval = htole16(rxmode);
   1605 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
   1606 
   1607 	axen_unlock_mii_sc_locked(sc);
   1608 
   1609 	/* Open RX and TX pipes. */
   1610 	err = usbd_open_pipe(sc->axen_iface, sc->axen_ed[AXEN_ENDPT_RX],
   1611 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axen_ep[AXEN_ENDPT_RX]);
   1612 	if (err) {
   1613 		aprint_error_dev(sc->axen_dev, "open rx pipe failed: %s\n",
   1614 		    usbd_errstr(err));
   1615 		return EIO;
   1616 	}
   1617 
   1618 	err = usbd_open_pipe(sc->axen_iface, sc->axen_ed[AXEN_ENDPT_TX],
   1619 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axen_ep[AXEN_ENDPT_TX]);
   1620 	if (err) {
   1621 		aprint_error_dev(sc->axen_dev, "open tx pipe failed: %s\n",
   1622 		    usbd_errstr(err));
   1623 		return EIO;
   1624 	}
   1625 
   1626 	/* Init RX ring. */
   1627 	if (axen_rx_list_init(sc)) {
   1628 		aprint_error_dev(sc->axen_dev, "rx list init failed\n");
   1629 		return ENOBUFS;
   1630 	}
   1631 
   1632 	/* Init TX ring. */
   1633 	if (axen_tx_list_init(sc)) {
   1634 		aprint_error_dev(sc->axen_dev, "tx list init failed\n");
   1635 		return ENOBUFS;
   1636 	}
   1637 
   1638 	mutex_enter(&sc->axen_rxlock);
   1639 	mutex_enter(&sc->axen_txlock);
   1640 	sc->axen_stopping = false;
   1641 
   1642 	/* Start up the receive pipe. */
   1643 	for (i = 0; i < AXEN_RX_LIST_CNT; i++) {
   1644 		c = &sc->axen_cdata.axen_rx_chain[i];
   1645 
   1646 		usbd_setup_xfer(c->axen_xfer, c, c->axen_buf, sc->axen_rx_bufsz,
   1647 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axen_rxeof);
   1648 		usbd_transfer(c->axen_xfer);
   1649 	}
   1650 
   1651 	mutex_exit(&sc->axen_txlock);
   1652 	mutex_exit(&sc->axen_rxlock);
   1653 
   1654 	/* Indicate we are up and running. */
   1655 	KASSERT(IFNET_LOCKED(ifp));
   1656 	ifp->if_flags |= IFF_RUNNING;
   1657 
   1658 	callout_schedule(&sc->axen_stat_ch, hz);
   1659 	return 0;
   1660 }
   1661 
   1662 static int
   1663 axen_init(struct ifnet *ifp)
   1664 {
   1665 	struct axen_softc * const sc = ifp->if_softc;
   1666 
   1667 	mutex_enter(&sc->axen_lock);
   1668 	int ret = axen_init_locked(ifp);
   1669 	mutex_exit(&sc->axen_lock);
   1670 
   1671 	return ret;
   1672 }
   1673 
   1674 static int
   1675 axen_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1676 {
   1677 	struct axen_softc * const sc = ifp->if_softc;
   1678 	int error = 0;
   1679 
   1680 	switch (cmd) {
   1681 	case SIOCSIFFLAGS:
   1682 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1683 			break;
   1684 
   1685 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1686 		case IFF_RUNNING:
   1687 			axen_stop(ifp, 1);
   1688 			break;
   1689 		case IFF_UP:
   1690 			axen_init(ifp);
   1691 			break;
   1692 		case IFF_UP | IFF_RUNNING:
   1693 			if ((ifp->if_flags ^ sc->axen_if_flags) == IFF_PROMISC) {
   1694 				axen_iff(sc);
   1695 			} else
   1696 				axen_init(ifp);
   1697 			break;
   1698 		}
   1699 
   1700 		mutex_enter(&sc->axen_rxlock);
   1701 		mutex_enter(&sc->axen_txlock);
   1702 		sc->axen_if_flags = ifp->if_flags;
   1703 		mutex_exit(&sc->axen_txlock);
   1704 		mutex_exit(&sc->axen_rxlock);
   1705 		break;
   1706 
   1707 	default:
   1708 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
   1709 			break;
   1710 
   1711 		error = 0;
   1712 		switch (cmd) {
   1713 		case SIOCADDMULTI:
   1714 		case SIOCDELMULTI:
   1715 			axen_iff(sc);
   1716 			break;
   1717 		case SIOCSIFCAP:
   1718 			mutex_enter(&sc->axen_lock);
   1719 			axen_setcoe(sc);
   1720 			mutex_exit(&sc->axen_lock);
   1721 			break;
   1722 		default:
   1723 			break;
   1724 		}
   1725 		break;
   1726 	}
   1727 
   1728 	return error;
   1729 }
   1730 
   1731 static void
   1732 axen_watchdog(struct ifnet *ifp)
   1733 {
   1734 	struct axen_softc * const sc = ifp->if_softc;
   1735 	struct axen_chain *c;
   1736 	usbd_status stat;
   1737 
   1738 	ifp->if_oerrors++;
   1739 	aprint_error_dev(sc->axen_dev, "watchdog timeout\n");
   1740 
   1741 	c = &sc->axen_cdata.axen_tx_chain[0];
   1742 	usbd_get_xfer_status(c->axen_xfer, NULL, NULL, NULL, &stat);
   1743 	axen_txeof(c->axen_xfer, c, stat);
   1744 
   1745 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1746 		axen_start(ifp);
   1747 }
   1748 
   1749 /*
   1750  * Stop the adapter and free any mbufs allocated to the
   1751  * RX and TX lists.
   1752  */
   1753 static void
   1754 axen_stop_locked(struct ifnet *ifp, int disable)
   1755 {
   1756 	struct axen_softc * const sc = ifp->if_softc;
   1757 	struct axen_chain *c;
   1758 	usbd_status err;
   1759 	int i;
   1760 	uint16_t rxmode, wval;
   1761 
   1762 	KASSERT(mutex_owned(&sc->axen_lock));
   1763 	mutex_enter(&sc->axen_rxlock);
   1764 	mutex_enter(&sc->axen_txlock);
   1765 	sc->axen_stopping = true;
   1766 	mutex_exit(&sc->axen_txlock);
   1767 	mutex_exit(&sc->axen_rxlock);
   1768 
   1769 	axen_reset(sc);
   1770 
   1771 	/* Disable receiver, set RX mode */
   1772 	axen_lock_mii_sc_locked(sc);
   1773 	axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval);
   1774 	rxmode = le16toh(wval);
   1775 	rxmode &= ~AXEN_RXCTL_START;
   1776 	wval = htole16(rxmode);
   1777 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
   1778 	axen_unlock_mii_sc_locked(sc);
   1779 
   1780 	/*
   1781 	 * XXXSMP Would like to
   1782 	 *	KASSERT(IFNET_LOCKED(ifp))
   1783 	 * here but the locking order is:
   1784 	 *	ifnet -> sc lock -> rxlock -> txlock
   1785 	 * and sc lock is already held.
   1786 	 */
   1787 	ifp->if_flags &= ~IFF_RUNNING;
   1788 	sc->axen_timer = 0;
   1789 
   1790 	callout_stop(&sc->axen_stat_ch);
   1791 	sc->axen_link = 0;
   1792 
   1793 	/* Stop transfers. */
   1794 	if (sc->axen_ep[AXEN_ENDPT_RX] != NULL) {
   1795 		err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_RX]);
   1796 		if (err) {
   1797 			aprint_error_dev(sc->axen_dev,
   1798 			    "abort rx pipe failed: %s\n", usbd_errstr(err));
   1799 
   1800 		}
   1801 	}
   1802 
   1803 	if (sc->axen_ep[AXEN_ENDPT_TX] != NULL) {
   1804 		err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_TX]);
   1805 		if (err) {
   1806 			aprint_error_dev(sc->axen_dev,
   1807 			    "abort tx pipe failed: %s\n", usbd_errstr(err));
   1808 		}
   1809 	}
   1810 
   1811 	if (sc->axen_ep[AXEN_ENDPT_INTR] != NULL) {
   1812 		err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_INTR]);
   1813 		if (err) {
   1814 			aprint_error_dev(sc->axen_dev,
   1815 			    "abort intr pipe failed: %s\n", usbd_errstr(err));
   1816 		}
   1817 	}
   1818 
   1819 	/* Free RX resources. */
   1820 	for (i = 0; i < AXEN_RX_LIST_CNT; i++) {
   1821 		c = &sc->axen_cdata.axen_rx_chain[i];
   1822 		if (c->axen_xfer != NULL) {
   1823 			usbd_destroy_xfer(c->axen_xfer);
   1824 			c->axen_xfer = NULL;
   1825 		}
   1826 	}
   1827 
   1828 	/* Free TX resources. */
   1829 	for (i = 0; i < AXEN_TX_LIST_CNT; i++) {
   1830 		c = &sc->axen_cdata.axen_tx_chain[i];
   1831 		if (c->axen_xfer != NULL) {
   1832 			usbd_destroy_xfer(c->axen_xfer);
   1833 			c->axen_xfer = NULL;
   1834 		}
   1835 	}
   1836 
   1837 	/* Close pipes. */
   1838 	if (sc->axen_ep[AXEN_ENDPT_RX] != NULL) {
   1839 		err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_RX]);
   1840 		if (err) {
   1841 			aprint_error_dev(sc->axen_dev,
   1842 			    "close rx pipe failed: %s\n", usbd_errstr(err));
   1843 		}
   1844 		sc->axen_ep[AXEN_ENDPT_RX] = NULL;
   1845 	}
   1846 
   1847 	if (sc->axen_ep[AXEN_ENDPT_TX] != NULL) {
   1848 		err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_TX]);
   1849 		if (err) {
   1850 			aprint_error_dev(sc->axen_dev,
   1851 			    "close tx pipe failed: %s\n", usbd_errstr(err));
   1852 		}
   1853 		sc->axen_ep[AXEN_ENDPT_TX] = NULL;
   1854 	}
   1855 
   1856 	if (sc->axen_ep[AXEN_ENDPT_INTR] != NULL) {
   1857 		err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_INTR]);
   1858 		if (err) {
   1859 			aprint_error_dev(sc->axen_dev,
   1860 			    "close intr pipe failed: %s\n", usbd_errstr(err));
   1861 		}
   1862 		sc->axen_ep[AXEN_ENDPT_INTR] = NULL;
   1863 	}
   1864 }
   1865 
   1866 static void
   1867 axen_stop(struct ifnet *ifp, int disable)
   1868 {
   1869 	struct axen_softc * const sc = ifp->if_softc;
   1870 
   1871 	mutex_enter(&sc->axen_lock);
   1872 	axen_stop_locked(ifp, disable);
   1873 	mutex_exit(&sc->axen_lock);
   1874 }
   1875 
   1876 MODULE(MODULE_CLASS_DRIVER, if_axen, NULL);
   1877 
   1878 #ifdef _MODULE
   1879 #include "ioconf.c"
   1880 #endif
   1881 
   1882 static int
   1883 if_axen_modcmd(modcmd_t cmd, void *aux)
   1884 {
   1885 	int error = 0;
   1886 
   1887 	switch (cmd) {
   1888 	case MODULE_CMD_INIT:
   1889 #ifdef _MODULE
   1890 		error = config_init_component(cfdriver_ioconf_axen,
   1891 		    cfattach_ioconf_axen, cfdata_ioconf_axen);
   1892 #endif
   1893 		return error;
   1894 	case MODULE_CMD_FINI:
   1895 #ifdef _MODULE
   1896 		error = config_fini_component(cfdriver_ioconf_axen,
   1897 		    cfattach_ioconf_axen, cfdata_ioconf_axen);
   1898 #endif
   1899 		return error;
   1900 	default:
   1901 		return ENOTTY;
   1902 	}
   1903 }
   1904