Home | History | Annotate | Line # | Download | only in usb
if_axen.c revision 1.48
      1 /*	$NetBSD: if_axen.c,v 1.48 2019/06/22 10:58:39 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.48 2019/06/22 10:58:39 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 	ifp->if_flags &= ~IFF_ALLMULTI;
    459 
    460 	if (ifp->if_flags & IFF_PROMISC) {
    461 		DPRINTF(("%s: promisc\n", device_xname(sc->axen_dev)));
    462 		rxmode |= AXEN_RXCTL_PROMISC;
    463 allmulti:	ifp->if_flags |= IFF_ALLMULTI;
    464 		rxmode |= AXEN_RXCTL_ACPT_ALL_MCAST
    465 		/* | AXEN_RXCTL_ACPT_PHY_MCAST */;
    466 	} else {
    467 		/* now program new ones */
    468 		DPRINTF(("%s: initializing hash table\n",
    469 		    device_xname(sc->axen_dev)));
    470 		ETHER_LOCK(ec);
    471 		ETHER_FIRST_MULTI(step, ec, enm);
    472 		while (enm != NULL) {
    473 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    474 			    ETHER_ADDR_LEN)) {
    475 				DPRINTF(("%s: allmulti\n",
    476 				    device_xname(sc->axen_dev)));
    477 				memset(hashtbl, 0, sizeof(hashtbl));
    478 				ETHER_UNLOCK(ec);
    479 				goto allmulti;
    480 			}
    481 			h = ether_crc32_be(enm->enm_addrlo,
    482 			    ETHER_ADDR_LEN) >> 26;
    483 			hashtbl[h / 8] |= 1 << (h % 8);
    484 			DPRINTF(("%s: %s added\n",
    485 			    device_xname(sc->axen_dev),
    486 			    ether_sprintf(enm->enm_addrlo)));
    487 			ETHER_NEXT_MULTI(step, enm);
    488 		}
    489 		ETHER_UNLOCK(ec);
    490 		rxmode |= AXEN_RXCTL_ACPT_MCAST;
    491 	}
    492 
    493 	axen_cmd(sc, AXEN_CMD_MAC_WRITE_FILTER, 8, AXEN_FILTER_MULTI, hashtbl);
    494 	wval = htole16(rxmode);
    495 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
    496 }
    497 
    498 static void
    499 axen_iff(struct axen_softc *sc)
    500 {
    501 
    502 	axen_lock_mii(sc);
    503 	axen_iff_locked(sc);
    504 	axen_unlock_mii(sc);
    505 }
    506 
    507 static void
    508 axen_reset(struct axen_softc *sc)
    509 {
    510 
    511 	KASSERT(mutex_owned(&sc->axen_lock));
    512 	if (sc->axen_dying)
    513 		return;
    514 	/* XXX What to reset? */
    515 
    516 	/* Wait a little while for the chip to get its brains in order. */
    517 	DELAY(1000);
    518 }
    519 
    520 #define AXEN_GPIO_WRITE(x, y) do {				\
    521 	axen_cmd(sc, AXEN_CMD_WRITE_GPIO, 0, (x), NULL);	\
    522 	usbd_delay_ms(sc->axen_udev, (y));			\
    523 } while (/*CONSTCOND*/0)
    524 
    525 static int
    526 axen_get_eaddr(struct axen_softc *sc, void *addr)
    527 {
    528 #if 1
    529 	return axen_cmd(sc, AXEN_CMD_MAC_READ_ETHER, 6, AXEN_CMD_MAC_NODE_ID,
    530 	    addr);
    531 #else
    532 	int i, retry;
    533 	uint8_t eeprom[20];
    534 	uint16_t csum;
    535 	uint16_t buf;
    536 
    537 	for (i = 0; i < 6; i++) {
    538 		/* set eeprom address */
    539 		buf = htole16(i);
    540 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_ADDR, &buf);
    541 
    542 		/* set eeprom command */
    543 		buf = htole16(AXEN_EEPROM_READ);
    544 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MAC_EEPROM_CMD, &buf);
    545 
    546 		/* check the value is ready */
    547 		retry = 3;
    548 		do {
    549 			buf = htole16(AXEN_EEPROM_READ);
    550 			usbd_delay_ms(sc->axen_udev, 10);
    551 			axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_MAC_EEPROM_CMD,
    552 			    &buf);
    553 			retry--;
    554 			if (retry < 0)
    555 				return EINVAL;
    556 		} while ((le16toh(buf) & 0xff) & AXEN_EEPROM_BUSY);
    557 
    558 		/* read data */
    559 		axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_EEPROM_READ,
    560 		    &eeprom[i * 2]);
    561 
    562 		/* sanity check */
    563 		if ((i == 0) && (eeprom[0] == 0xff))
    564 			return EINVAL;
    565 	}
    566 
    567 	/* check checksum */
    568 	csum = eeprom[6] + eeprom[7] + eeprom[8] + eeprom[9];
    569 	csum = (csum >> 8) + (csum & 0xff) + eeprom[10];
    570 	if (csum != 0xff) {
    571 		printf("eeprom checksum mismatchi(0x%02x)\n", csum);
    572 		return EINVAL;
    573 	}
    574 
    575 	memcpy(addr, eeprom, ETHER_ADDR_LEN);
    576 	return 0;
    577 #endif
    578 }
    579 
    580 static void
    581 axen_ax88179_init(struct axen_softc *sc)
    582 {
    583 	struct axen_qctrl qctrl;
    584 	uint16_t ctl, temp;
    585 	uint16_t wval;
    586 	uint8_t val;
    587 
    588 	axen_lock_mii(sc);
    589 
    590 	/* XXX: ? */
    591 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_UNK_05, &val);
    592 	DPRINTFN(5, ("AXEN_CMD_MAC_READ(0x05): 0x%02x\n", val));
    593 
    594 	/* check AX88179 version, UA1 / UA2 */
    595 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_GENERAL_STATUS, &val);
    596 	/* UA1 */
    597 	if (!(val & AXEN_GENERAL_STATUS_MASK)) {
    598 		sc->axen_rev = AXEN_REV_UA1;
    599 		DPRINTF(("AX88179 ver. UA1\n"));
    600 	} else {
    601 		sc->axen_rev = AXEN_REV_UA2;
    602 		DPRINTF(("AX88179 ver. UA2\n"));
    603 	}
    604 
    605 	/* power up ethernet PHY */
    606 	wval = htole16(0);
    607 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    608 
    609 	wval = htole16(AXEN_PHYPWR_RSTCTL_IPRL);
    610 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    611 	usbd_delay_ms(sc->axen_udev, 200);
    612 
    613 	/* set clock mode */
    614 	val = AXEN_PHYCLK_ACS | AXEN_PHYCLK_BCS;
    615 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val);
    616 	usbd_delay_ms(sc->axen_udev, 100);
    617 
    618 	/* set monitor mode (disable) */
    619 	val = AXEN_MONITOR_NONE;
    620 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val);
    621 
    622 	/* enable auto detach */
    623 	axen_cmd(sc, AXEN_CMD_EEPROM_READ, 2, AXEN_EEPROM_STAT, &wval);
    624 	temp = le16toh(wval);
    625 	DPRINTFN(2,("EEPROM0x43 = 0x%04x\n", temp));
    626 	if (!(temp == 0xffff) && !(temp & 0x0100)) {
    627 		/* Enable auto detach bit */
    628 		val = 0;
    629 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val);
    630 		val = AXEN_PHYCLK_ULR;
    631 		axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PHYCLK, &val);
    632 		usbd_delay_ms(sc->axen_udev, 100);
    633 
    634 		axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    635 		ctl = le16toh(wval);
    636 		ctl |= AXEN_PHYPWR_RSTCTL_AUTODETACH;
    637 		wval = htole16(ctl);
    638 		axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_PHYPWR_RSTCTL, &wval);
    639 		usbd_delay_ms(sc->axen_udev, 200);
    640 		aprint_error_dev(sc->axen_dev, "enable auto detach (0x%04x)\n",
    641 		    ctl);
    642 	}
    643 
    644 	/* bulkin queue setting */
    645 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_USB_UPLINK, &val);
    646 	switch (val) {
    647 	case AXEN_USB_FS:
    648 		DPRINTF(("uplink: USB1.1\n"));
    649 		qctrl.ctrl	 = 0x07;
    650 		qctrl.timer_low	 = 0xcc;
    651 		qctrl.timer_high = 0x4c;
    652 		qctrl.bufsize	 = AXEN_BUFSZ_LS - 1;
    653 		qctrl.ifg	 = 0x08;
    654 		break;
    655 	case AXEN_USB_HS:
    656 		DPRINTF(("uplink: USB2.0\n"));
    657 		qctrl.ctrl	 = 0x07;
    658 		qctrl.timer_low	 = 0x02;
    659 		qctrl.timer_high = 0xa0;
    660 		qctrl.bufsize	 = AXEN_BUFSZ_HS - 1;
    661 		qctrl.ifg	 = 0xff;
    662 		break;
    663 	case AXEN_USB_SS:
    664 		DPRINTF(("uplink: USB3.0\n"));
    665 		qctrl.ctrl	 = 0x07;
    666 		qctrl.timer_low	 = 0x4f;
    667 		qctrl.timer_high = 0x00;
    668 		qctrl.bufsize	 = AXEN_BUFSZ_SS - 1;
    669 		qctrl.ifg	 = 0xff;
    670 		break;
    671 	default:
    672 		aprint_error_dev(sc->axen_dev, "unknown uplink bus:0x%02x\n",
    673 		    val);
    674 		axen_unlock_mii(sc);
    675 		return;
    676 	}
    677 	axen_cmd(sc, AXEN_CMD_MAC_SET_RXSR, 5, AXEN_RX_BULKIN_QCTRL, &qctrl);
    678 
    679 	/*
    680 	 * set buffer high/low watermark to pause/resume.
    681 	 * write 2byte will set high/log simultaneous with AXEN_PAUSE_HIGH.
    682 	 * XXX: what is the best value? OSX driver uses 0x3c-0x4c as LOW-HIGH
    683 	 * watermark parameters.
    684 	 */
    685 	val = 0x34;
    686 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_LOW_WATERMARK, &val);
    687 	val = 0x52;
    688 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_PAUSE_HIGH_WATERMARK, &val);
    689 
    690 	/* Set RX/TX configuration. */
    691 	/* Set RX control register */
    692 	ctl = AXEN_RXCTL_IPE | AXEN_RXCTL_DROPCRCERR | AXEN_RXCTL_AUTOB;
    693 	wval = htole16(ctl);
    694 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
    695 
    696 	/* set monitor mode (enable) */
    697 	val = AXEN_MONITOR_PMETYPE | AXEN_MONITOR_PMEPOL | AXEN_MONITOR_RWMP;
    698 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_MONITOR_MODE, &val);
    699 	axen_cmd(sc, AXEN_CMD_MAC_READ, 1, AXEN_MONITOR_MODE, &val);
    700 	DPRINTF(("axen: Monitor mode = 0x%02x\n", val));
    701 
    702 	/* set medium type */
    703 	ctl = AXEN_MEDIUM_GIGA | AXEN_MEDIUM_FDX | AXEN_MEDIUM_EN_125MHZ |
    704 	    AXEN_MEDIUM_RXFLOW_CTRL_EN | AXEN_MEDIUM_TXFLOW_CTRL_EN |
    705 	    AXEN_MEDIUM_RECV_EN;
    706 	wval = htole16(ctl);
    707 	DPRINTF(("axen: set to medium mode: 0x%04x\n", ctl));
    708 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MEDIUM_STATUS, &wval);
    709 	usbd_delay_ms(sc->axen_udev, 100);
    710 
    711 	axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MEDIUM_STATUS, &wval);
    712 	DPRINTF(("axen: current medium mode: 0x%04x\n", le16toh(wval)));
    713 
    714 	axen_unlock_mii(sc);
    715 
    716 #if 0 /* XXX: TBD.... */
    717 #define GMII_LED_ACTIVE		0x1a
    718 #define GMII_PHY_PAGE_SEL	0x1e
    719 #define GMII_PHY_PAGE_SEL	0x1f
    720 #define GMII_PAGE_EXT		0x0007
    721 	axen_miibus_writereg(&sc->axen_dev, sc->axen_phyno, GMII_PHY_PAGE_SEL,
    722 	    GMII_PAGE_EXT);
    723 	axen_miibus_writereg(&sc->axen_dev, sc->axen_phyno, GMII_PHY_PAGE,
    724 	    0x002c);
    725 #endif
    726 
    727 #if 1 /* XXX: phy hack ? */
    728 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x1F, 0x0005);
    729 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x0C, 0x0000);
    730 	axen_miibus_readreg(sc->axen_dev, sc->axen_phyno, 0x0001, &wval);
    731 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x01,
    732 	    wval | 0x0080);
    733 	axen_miibus_writereg(sc->axen_dev, sc->axen_phyno, 0x1F, 0x0000);
    734 #endif
    735 }
    736 
    737 static void
    738 axen_setcoe(struct axen_softc *sc)
    739 {
    740 	struct ifnet *ifp = GET_IFP(sc);
    741 	uint64_t enabled = ifp->if_capenable;
    742 	uint8_t val;
    743 
    744 	KASSERT(mutex_owned(&sc->axen_mii_lock));
    745 
    746 	val = AXEN_RXCOE_OFF;
    747 	if (enabled & IFCAP_CSUM_IPv4_Rx)
    748 		val |= AXEN_RXCOE_IPv4;
    749 	if (enabled & IFCAP_CSUM_TCPv4_Rx)
    750 		val |= AXEN_RXCOE_TCPv4;
    751 	if (enabled & IFCAP_CSUM_UDPv4_Rx)
    752 		val |= AXEN_RXCOE_UDPv4;
    753 	if (enabled & IFCAP_CSUM_TCPv6_Rx)
    754 		val |= AXEN_RXCOE_TCPv6;
    755 	if (enabled & IFCAP_CSUM_UDPv6_Rx)
    756 		val |= AXEN_RXCOE_UDPv6;
    757 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_RX_COE, &val);
    758 
    759 	val = AXEN_TXCOE_OFF;
    760 	if (enabled & IFCAP_CSUM_IPv4_Tx)
    761 		val |= AXEN_TXCOE_IPv4;
    762 	if (enabled & IFCAP_CSUM_TCPv4_Tx)
    763 		val |= AXEN_TXCOE_TCPv4;
    764 	if (enabled & IFCAP_CSUM_UDPv4_Tx)
    765 		val |= AXEN_TXCOE_UDPv4;
    766 	if (enabled & IFCAP_CSUM_TCPv6_Tx)
    767 		val |= AXEN_TXCOE_TCPv6;
    768 	if (enabled & IFCAP_CSUM_UDPv6_Tx)
    769 		val |= AXEN_TXCOE_UDPv6;
    770 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_TX_COE, &val);
    771 }
    772 
    773 static int
    774 axen_match(device_t parent, cfdata_t match, void *aux)
    775 {
    776 	struct usb_attach_arg *uaa = aux;
    777 
    778 	return axen_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    779 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    780 }
    781 
    782 static void
    783 axen_attach(device_t parent, device_t self, void *aux)
    784 {
    785 	struct axen_softc * const sc = device_private(self);
    786 	struct usb_attach_arg *uaa = aux;
    787 	struct usbd_device *dev = uaa->uaa_device;
    788 	usbd_status err;
    789 	usb_interface_descriptor_t *id;
    790 	usb_endpoint_descriptor_t *ed;
    791 	struct mii_data	*mii;
    792 	uint8_t eaddr[ETHER_ADDR_LEN];
    793 	char *devinfop;
    794 	const char *devname = device_xname(self);
    795 	struct ifnet *ifp;
    796 	int i;
    797 
    798 	aprint_naive("\n");
    799 	aprint_normal("\n");
    800 
    801 	sc->axen_dev = self;
    802 	sc->axen_udev = dev;
    803 
    804 	devinfop = usbd_devinfo_alloc(dev, 0);
    805 	aprint_normal_dev(self, "%s\n", devinfop);
    806 	usbd_devinfo_free(devinfop);
    807 
    808 	err = usbd_set_config_no(dev, AXEN_CONFIG_NO, 1);
    809 	if (err) {
    810 		aprint_error_dev(self, "failed to set configuration"
    811 		    ", err=%s\n", usbd_errstr(err));
    812 		return;
    813 	}
    814 
    815 	sc->axen_flags = axen_lookup(uaa->uaa_vendor, uaa->uaa_product)->axen_flags;
    816 
    817 	usb_init_task(&sc->axen_tick_task, axen_tick_task, sc, USB_TASKQ_MPSAFE);
    818 
    819 	err = usbd_device2interface_handle(dev, AXEN_IFACE_IDX,&sc->axen_iface);
    820 	if (err) {
    821 		aprint_error_dev(self, "getting interface handle failed\n");
    822 		return;
    823 	}
    824 
    825 	sc->axen_product = uaa->uaa_product;
    826 	sc->axen_vendor = uaa->uaa_vendor;
    827 
    828 	id = usbd_get_interface_descriptor(sc->axen_iface);
    829 
    830 	/* decide on what our bufsize will be */
    831 	switch (sc->axen_udev->ud_speed) {
    832 	case USB_SPEED_SUPER:
    833 		sc->axen_rx_bufsz = AXEN_BUFSZ_SS * 1024;
    834 		break;
    835 	case USB_SPEED_HIGH:
    836 		sc->axen_rx_bufsz = AXEN_BUFSZ_HS * 1024;
    837 		break;
    838 	default:
    839 		sc->axen_rx_bufsz = AXEN_BUFSZ_LS * 1024;
    840 		break;
    841 	}
    842 
    843 	sc->axen_tx_bufsz = IP_MAXPACKET +
    844 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN +
    845 	    sizeof(struct axen_sframe_hdr);
    846 
    847 	/* Find endpoints. */
    848 	for (i = 0; i < id->bNumEndpoints; i++) {
    849 		ed = usbd_interface2endpoint_descriptor(sc->axen_iface, i);
    850 		if (!ed) {
    851 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    852 			return;
    853 		}
    854 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    855 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    856 			sc->axen_ed[AXEN_ENDPT_RX] = ed->bEndpointAddress;
    857 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    858 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    859 			sc->axen_ed[AXEN_ENDPT_TX] = ed->bEndpointAddress;
    860 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    861 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    862 			sc->axen_ed[AXEN_ENDPT_INTR] = ed->bEndpointAddress;
    863 		}
    864 	}
    865 
    866 	/* Set these up now for axen_cmd().  */
    867 	mutex_init(&sc->axen_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    868 	mutex_init(&sc->axen_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    869 	mutex_init(&sc->axen_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    870 	mutex_init(&sc->axen_lock, MUTEX_DEFAULT, IPL_NONE);
    871 	cv_init(&sc->axen_detachcv, "axendet");
    872 
    873 	sc->axen_phyno = AXEN_PHY_ID;
    874 	DPRINTF(("%s: phyno %d\n", device_xname(self), sc->axen_phyno));
    875 
    876 	/* Get station address.  */
    877 	axen_lock_mii(sc);
    878 	if (axen_get_eaddr(sc, &eaddr)) {
    879 		axen_unlock_mii(sc);
    880 		printf("EEPROM checksum error\n");
    881 		cv_destroy(&sc->axen_detachcv);
    882 		mutex_destroy(&sc->axen_lock);
    883 		mutex_destroy(&sc->axen_rxlock);
    884 		mutex_destroy(&sc->axen_txlock);
    885 		mutex_destroy(&sc->axen_mii_lock);
    886 		return;
    887 	}
    888 	axen_unlock_mii(sc);
    889 
    890 	axen_ax88179_init(sc);
    891 
    892 	/*
    893 	 * An ASIX chip was detected. Inform the world.
    894 	 */
    895 	if (sc->axen_flags & AX178A)
    896 		aprint_normal_dev(self, "AX88178a\n");
    897 	else if (sc->axen_flags & AX179)
    898 		aprint_normal_dev(self, "AX88179\n");
    899 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    900 
    901 	/* Initialize interface info.*/
    902 
    903 	ifp = &sc->sc_if;
    904 	ifp->if_softc = sc;
    905 	strlcpy(ifp->if_xname, devname, IFNAMSIZ);
    906 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    907 	ifp->if_extflags = IFEF_MPSAFE;
    908 	ifp->if_ioctl = axen_ioctl;
    909 	ifp->if_start = axen_start;
    910 	ifp->if_init = axen_init;
    911 	ifp->if_stop = axen_stop;
    912 
    913 	IFQ_SET_READY(&ifp->if_snd);
    914 
    915 	sc->axen_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
    916 
    917 	/* Adapter does not support TSOv6 (They call it LSOv2). */
    918 	ifp->if_capabilities |= IFCAP_TSOv4 |
    919 	    IFCAP_CSUM_IPv4_Rx	| IFCAP_CSUM_IPv4_Tx  |
    920 	    IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_TCPv4_Tx |
    921 	    IFCAP_CSUM_UDPv4_Rx | IFCAP_CSUM_UDPv4_Tx |
    922 	    IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_TCPv6_Tx |
    923 	    IFCAP_CSUM_UDPv6_Rx | IFCAP_CSUM_UDPv6_Tx;
    924 
    925 	/* Initialize MII/media info. */
    926 	mii = &sc->axen_mii;
    927 	mii->mii_ifp = ifp;
    928 	mii->mii_readreg = axen_miibus_readreg;
    929 	mii->mii_writereg = axen_miibus_writereg;
    930 	mii->mii_statchg = axen_miibus_statchg;
    931 	mii->mii_flags = MIIF_AUTOTSLEEP;
    932 
    933 	sc->axen_ec.ec_mii = mii;
    934 	ifmedia_init(&mii->mii_media, 0, axen_ifmedia_upd, axen_ifmedia_sts);
    935 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    936 
    937 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    938 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    939 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    940 	} else
    941 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    942 
    943 	/* Attach the interface. */
    944 	if_attach(ifp);
    945 	ether_ifattach(ifp, eaddr);
    946 	rnd_attach_source(&sc->rnd_source, device_xname(sc->axen_dev),
    947 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    948 
    949 	callout_init(&sc->axen_stat_ch, CALLOUT_MPSAFE);
    950 	callout_setfunc(&sc->axen_stat_ch, axen_tick, sc);
    951 
    952 	sc->axen_attached = true;
    953 
    954 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axen_udev,sc->axen_dev);
    955 
    956 	if (!pmf_device_register(self, NULL, NULL))
    957 		aprint_error_dev(self, "couldn't establish power handler\n");
    958 }
    959 
    960 static int
    961 axen_detach(device_t self, int flags)
    962 {
    963 	struct axen_softc * const sc = device_private(self);
    964 	struct ifnet *ifp = GET_IFP(sc);
    965 
    966 	mutex_enter(&sc->axen_lock);
    967 	sc->axen_dying = true;
    968 	mutex_exit(&sc->axen_lock);
    969 
    970 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
    971 
    972 	/* Detached before attached finished, so just bail out. */
    973 	if (!sc->axen_attached)
    974 		return 0;
    975 
    976 	pmf_device_deregister(self);
    977 
    978 	callout_halt(&sc->axen_stat_ch, NULL);
    979 	usb_rem_task_wait(sc->axen_udev, &sc->axen_tick_task,
    980 	    USB_TASKQ_DRIVER, NULL);
    981 
    982 	if (ifp->if_flags & IFF_RUNNING)
    983 		axen_stop(ifp, 1);
    984 
    985 	mutex_enter(&sc->axen_lock);
    986 	sc->axen_refcnt--;
    987 	while (sc->axen_refcnt > 0) {
    988 		/* Wait for processes to go away */
    989 		cv_wait(&sc->axen_detachcv, &sc->axen_lock);
    990 	}
    991 
    992 #ifdef DIAGNOSTIC
    993 	if (sc->axen_ep[AXEN_ENDPT_TX] != NULL ||
    994 	    sc->axen_ep[AXEN_ENDPT_RX] != NULL ||
    995 	    sc->axen_ep[AXEN_ENDPT_INTR] != NULL)
    996 		aprint_debug_dev(self, "detach has active endpoints\n");
    997 #endif
    998 
    999 	mutex_exit(&sc->axen_lock);
   1000 
   1001 	callout_destroy(&sc->axen_stat_ch);
   1002 	rnd_detach_source(&sc->rnd_source);
   1003 	mii_detach(&sc->axen_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1004 	ifmedia_delete_instance(&sc->axen_mii.mii_media, IFM_INST_ANY);
   1005 	ether_ifdetach(ifp);
   1006 	if_detach(ifp);
   1007 
   1008 	sc->axen_attached = false;
   1009 
   1010 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axen_udev,sc->axen_dev);
   1011 
   1012 	cv_destroy(&sc->axen_detachcv);
   1013 	mutex_destroy(&sc->axen_lock);
   1014 	mutex_destroy(&sc->axen_rxlock);
   1015 	mutex_destroy(&sc->axen_txlock);
   1016 	mutex_destroy(&sc->axen_mii_lock);
   1017 
   1018 	return 0;
   1019 }
   1020 
   1021 static int
   1022 axen_activate(device_t self, devact_t act)
   1023 {
   1024 	struct axen_softc * const sc = device_private(self);
   1025 	struct ifnet *ifp = GET_IFP(sc);
   1026 
   1027 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1028 
   1029 	switch (act) {
   1030 	case DVACT_DEACTIVATE:
   1031 		if_deactivate(ifp);
   1032 
   1033 		mutex_enter(&sc->axen_lock);
   1034 		sc->axen_dying = true;
   1035 		mutex_exit(&sc->axen_lock);
   1036 
   1037 		mutex_enter(&sc->axen_rxlock);
   1038 		mutex_enter(&sc->axen_txlock);
   1039 		sc->axen_stopping = true;
   1040 		mutex_exit(&sc->axen_txlock);
   1041 		mutex_exit(&sc->axen_rxlock);
   1042 
   1043 		return 0;
   1044 	default:
   1045 		return EOPNOTSUPP;
   1046 	}
   1047 }
   1048 
   1049 static struct mbuf *
   1050 axen_newbuf(void)
   1051 {
   1052 	struct mbuf *m;
   1053 
   1054 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1055 	if (m == NULL)
   1056 		return NULL;
   1057 
   1058 	MCLGET(m, M_DONTWAIT);
   1059 	if (!(m->m_flags & M_EXT)) {
   1060 		m_freem(m);
   1061 		return NULL;
   1062 	}
   1063 
   1064 	m->m_len = m->m_pkthdr.len = MCLBYTES;
   1065 	m_adj(m, ETHER_ALIGN);
   1066 
   1067 	return m;
   1068 }
   1069 
   1070 static int
   1071 axen_rx_list_init(struct axen_softc *sc)
   1072 {
   1073 	struct axen_cdata *cd;
   1074 	struct axen_chain *c;
   1075 	int i;
   1076 
   1077 	DPRINTF(("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1078 
   1079 	cd = &sc->axen_cdata;
   1080 	for (i = 0; i < AXEN_RX_LIST_CNT; i++) {
   1081 		c = &cd->axen_rx_chain[i];
   1082 		c->axen_sc = sc;
   1083 		if (c->axen_xfer == NULL) {
   1084 			int err = usbd_create_xfer(sc->axen_ep[AXEN_ENDPT_RX],
   1085 			    sc->axen_rx_bufsz, 0, 0, &c->axen_xfer);
   1086 			if (err)
   1087 				return err;
   1088 			c->axen_buf = usbd_get_buffer(c->axen_xfer);
   1089 		}
   1090 	}
   1091 
   1092 	return 0;
   1093 }
   1094 
   1095 static int
   1096 axen_tx_list_init(struct axen_softc *sc)
   1097 {
   1098 	struct axen_cdata *cd;
   1099 	struct axen_chain *c;
   1100 	int i;
   1101 
   1102 	DPRINTF(("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1103 
   1104 	cd = &sc->axen_cdata;
   1105 	for (i = 0; i < AXEN_TX_LIST_CNT; i++) {
   1106 		c = &cd->axen_tx_chain[i];
   1107 		c->axen_sc = sc;
   1108 		if (c->axen_xfer == NULL) {
   1109 			int err = usbd_create_xfer(sc->axen_ep[AXEN_ENDPT_TX],
   1110 			    sc->axen_tx_bufsz, USBD_FORCE_SHORT_XFER, 0,
   1111 			    &c->axen_xfer);
   1112 			if (err)
   1113 				return err;
   1114 			c->axen_buf = usbd_get_buffer(c->axen_xfer);
   1115 		}
   1116 	}
   1117 
   1118 	cd->axen_tx_prod = cd->axen_tx_cnt = 0;
   1119 
   1120 	return 0;
   1121 }
   1122 
   1123 /*
   1124  * A frame has been uploaded: pass the resulting mbuf chain up to
   1125  * the higher level protocols.
   1126  */
   1127 static void
   1128 axen_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1129 {
   1130 	struct axen_chain *c = (struct axen_chain *)priv;
   1131 	struct axen_softc * const sc = c->axen_sc;
   1132 	struct ifnet *ifp = GET_IFP(sc);
   1133 	uint8_t *buf = c->axen_buf;
   1134 	struct mbuf *m;
   1135 	uint32_t total_len;
   1136 	uint32_t rx_hdr, pkt_hdr;
   1137 	uint32_t *hdr_p;
   1138 	uint16_t hdr_offset, pkt_count;
   1139 	size_t pkt_len;
   1140 	size_t temp;
   1141 
   1142 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->axen_dev), __func__));
   1143 
   1144 	mutex_enter(&sc->axen_rxlock);
   1145 
   1146 	if (sc->axen_dying || sc->axen_stopping ||
   1147 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
   1148 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) {
   1149 		mutex_exit(&sc->axen_rxlock);
   1150 		return;
   1151 	}
   1152 
   1153 	if (status != USBD_NORMAL_COMPLETION) {
   1154 		if (usbd_ratecheck(&sc->axen_rx_notice))
   1155 			aprint_error_dev(sc->axen_dev, "usb errors on rx: %s\n",
   1156 			    usbd_errstr(status));
   1157 		if (status == USBD_STALLED)
   1158 			usbd_clear_endpoint_stall_async(sc->axen_ep[AXEN_ENDPT_RX]);
   1159 		goto done;
   1160 	}
   1161 
   1162 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1163 
   1164 	if (total_len < sizeof(pkt_hdr)) {
   1165 		aprint_error_dev(sc->axen_dev, "rxeof: too short transfer\n");
   1166 		ifp->if_ierrors++;
   1167 		goto done;
   1168 	}
   1169 
   1170 	/*
   1171 	 * buffer map
   1172 	 * [packet #0]...[packet #n][pkt hdr#0]..[pkt hdr#n][recv_hdr]
   1173 	 * each packet has 0xeeee as psuedo header..
   1174 	 */
   1175 	hdr_p = (uint32_t *)(buf + total_len - sizeof(uint32_t));
   1176 	rx_hdr = le32toh(*hdr_p);
   1177 	hdr_offset = (uint16_t)(rx_hdr >> 16);
   1178 	pkt_count  = (uint16_t)(rx_hdr & 0xffff);
   1179 
   1180 	if (total_len > sc->axen_rx_bufsz) {
   1181 		aprint_error_dev(sc->axen_dev,
   1182 		    "rxeof: too large transfer (%u > %u)\n",
   1183 		    total_len, sc->axen_rx_bufsz);
   1184 		goto done;
   1185 	}
   1186 
   1187 	/* sanity check */
   1188 	if (hdr_offset > total_len) {
   1189 		aprint_error_dev(sc->axen_dev,
   1190 		    "rxeof: invalid hdr offset (%u > %u)\n",
   1191 		    hdr_offset, total_len);
   1192 		ifp->if_ierrors++;
   1193 		usbd_delay_ms(sc->axen_udev, 100);
   1194 		goto done;
   1195 	}
   1196 
   1197 	/* point first packet header */
   1198 	hdr_p = (uint32_t *)(buf + hdr_offset);
   1199 
   1200 	/*
   1201 	 * ax88179 will pack multiple ip packet to a USB transaction.
   1202 	 * process all of packets in the buffer
   1203 	 */
   1204 
   1205 #if 1 /* XXX: paranoiac check. need to remove later */
   1206 #define AXEN_MAX_PACKED_PACKET 200
   1207 	if (pkt_count > AXEN_MAX_PACKED_PACKET) {
   1208 		DPRINTF(("%s: Too many packets (%d) in a transaction, discard.\n",
   1209 		    device_xname(sc->axen_dev), pkt_count));
   1210 		goto done;
   1211 	}
   1212 #endif
   1213 
   1214 	do {
   1215 		if ((buf[0] != 0xee) || (buf[1] != 0xee)) {
   1216 			aprint_error_dev(sc->axen_dev,
   1217 			    "invalid buffer(pkt#%d), continue\n", pkt_count);
   1218 			ifp->if_ierrors += pkt_count;
   1219 			goto done;
   1220 		}
   1221 
   1222 		pkt_hdr = le32toh(*hdr_p);
   1223 		pkt_len = (pkt_hdr >> 16) & 0x1fff;
   1224 		DPRINTFN(10,
   1225 		    ("%s: rxeof: packet#%d, pkt_hdr 0x%08x, pkt_len %zu\n",
   1226 		   device_xname(sc->axen_dev), pkt_count, pkt_hdr, pkt_len));
   1227 
   1228 		if (pkt_hdr & (AXEN_RXHDR_CRC_ERR | AXEN_RXHDR_DROP_ERR)) {
   1229 			ifp->if_ierrors++;
   1230 			/* move to next pkt header */
   1231 			DPRINTF(("%s: %s err (pkt#%d)\n",
   1232 			    device_xname(sc->axen_dev),
   1233 			    (pkt_hdr & AXEN_RXHDR_CRC_ERR) ? "crc" : "drop",
   1234 			    pkt_count));
   1235 			goto nextpkt;
   1236 		}
   1237 
   1238 		/* process each packet */
   1239 		/* allocate mbuf */
   1240 		m = axen_newbuf();
   1241 		if (m == NULL) {
   1242 			ifp->if_ierrors++;
   1243 			goto nextpkt;
   1244 		}
   1245 
   1246 		/* skip pseudo header (2byte) */
   1247 		m_set_rcvif(m, ifp);
   1248 		m->m_pkthdr.len = m->m_len = pkt_len - 6;
   1249 
   1250 		m->m_pkthdr.csum_flags = axen_csum_flags_rx(ifp, pkt_hdr);
   1251 		memcpy(mtod(m, char *), buf + 2, pkt_len - 6);
   1252 
   1253 		mutex_exit(&sc->axen_rxlock);
   1254 
   1255 		/* push the packet up */
   1256 		if_percpuq_enqueue((ifp)->if_percpuq, (m));
   1257 
   1258 		mutex_enter(&sc->axen_rxlock);
   1259 		if (sc->axen_stopping) {
   1260 			mutex_exit(&sc->axen_rxlock);
   1261 			return;
   1262 		}
   1263 
   1264 nextpkt:
   1265 		/*
   1266 		 * prepare next packet
   1267 		 * as each packet will be aligned 8byte boundary,
   1268 		 * need to fix up the start point of the buffer.
   1269 		 */
   1270 		temp = ((pkt_len + 7) & 0xfff8);
   1271 		buf = buf + temp;
   1272 		hdr_p++;
   1273 		pkt_count--;
   1274 	} while (pkt_count > 0);
   1275 
   1276 done:
   1277 	mutex_exit(&sc->axen_rxlock);
   1278 
   1279 	/* Setup new transfer. */
   1280 	usbd_setup_xfer(xfer, c, c->axen_buf, sc->axen_rx_bufsz,
   1281 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axen_rxeof);
   1282 	usbd_transfer(xfer);
   1283 
   1284 	DPRINTFN(10,("%s: %s: start rx\n",device_xname(sc->axen_dev),__func__));
   1285 }
   1286 
   1287 static int
   1288 axen_csum_flags_rx(struct ifnet *ifp, uint32_t pkt_hdr)
   1289 {
   1290 	int enabled_flags = ifp->if_csum_flags_rx;
   1291 	int csum_flags = 0;
   1292 	int l3_type, l4_type;
   1293 
   1294 	if (enabled_flags == 0)
   1295 		return 0;
   1296 
   1297 	l3_type = (pkt_hdr & AXEN_RXHDR_L3_TYPE_MASK) >>
   1298 	    AXEN_RXHDR_L3_TYPE_OFFSET;
   1299 
   1300 	if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4)
   1301 		csum_flags |= M_CSUM_IPv4;
   1302 
   1303 	l4_type = (pkt_hdr & AXEN_RXHDR_L4_TYPE_MASK) >>
   1304 	    AXEN_RXHDR_L4_TYPE_OFFSET;
   1305 
   1306 	switch (l4_type) {
   1307 	case AXEN_RXHDR_L4_TYPE_TCP:
   1308 		if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4)
   1309 			csum_flags |= M_CSUM_TCPv4;
   1310 		else
   1311 			csum_flags |= M_CSUM_TCPv6;
   1312 		break;
   1313 	case AXEN_RXHDR_L4_TYPE_UDP:
   1314 		if (l3_type == AXEN_RXHDR_L3_TYPE_IPV4)
   1315 			csum_flags |= M_CSUM_UDPv4;
   1316 		else
   1317 			csum_flags |= M_CSUM_UDPv6;
   1318 		break;
   1319 	default:
   1320 		break;
   1321 	}
   1322 
   1323 	csum_flags &= enabled_flags;
   1324 	if ((csum_flags & M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L3CSUM_ERR))
   1325 		csum_flags |= M_CSUM_IPv4_BAD;
   1326 	if ((csum_flags & ~M_CSUM_IPv4) && (pkt_hdr & AXEN_RXHDR_L4CSUM_ERR))
   1327 		csum_flags |= M_CSUM_TCP_UDP_BAD;
   1328 
   1329 	return csum_flags;
   1330 }
   1331 
   1332 /*
   1333  * A frame was downloaded to the chip. It's safe for us to clean up
   1334  * the list buffers.
   1335  */
   1336 static void
   1337 axen_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1338 {
   1339 	struct axen_chain *c = (struct axen_chain *)priv;
   1340 	struct axen_softc * const sc = c->axen_sc;
   1341 	struct axen_cdata *cd = &sc->axen_cdata;
   1342 	struct ifnet *ifp = GET_IFP(sc);
   1343 
   1344 	mutex_enter(&sc->axen_txlock);
   1345 	if (sc->axen_stopping || sc->axen_dying) {
   1346 		mutex_exit(&sc->axen_txlock);
   1347 		return;
   1348 	}
   1349 
   1350 	KASSERT(cd->axen_tx_cnt > 0);
   1351 	cd->axen_tx_cnt--;
   1352 
   1353 	sc->axen_timer = 0;
   1354 	ifp->if_flags &= ~IFF_OACTIVE;
   1355 
   1356 	switch (status) {
   1357 	case USBD_NOT_STARTED:
   1358 	case USBD_CANCELLED:
   1359 		break;
   1360 
   1361 	case USBD_NORMAL_COMPLETION:
   1362 		ifp->if_opackets++;
   1363 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1364 			axen_start_locked(ifp);
   1365 		break;
   1366 
   1367 	default:
   1368 
   1369 		ifp->if_oerrors++;
   1370 		if (usbd_ratecheck(&sc->axen_tx_notice))
   1371 			aprint_error_dev(sc->axen_dev, "usb error on tx: %s\n",
   1372 			    usbd_errstr(status));
   1373 		if (status == USBD_STALLED)
   1374 			usbd_clear_endpoint_stall_async(sc->axen_ep[AXEN_ENDPT_TX]);
   1375 		break;
   1376 	}
   1377 
   1378 	mutex_exit(&sc->axen_txlock);
   1379 }
   1380 
   1381 static void
   1382 axen_tick(void *xsc)
   1383 {
   1384 	struct axen_softc * const sc = xsc;
   1385 
   1386 	if (sc == NULL)
   1387 		return;
   1388 
   1389 	DPRINTFN(0xff,("%s: %s: enter\n", device_xname(sc->axen_dev),__func__));
   1390 
   1391 	mutex_enter(&sc->axen_lock);
   1392 	if (!sc->axen_stopping && !sc->axen_dying) {
   1393 		/* Perform periodic stuff in process context */
   1394 		usb_add_task(sc->axen_udev, &sc->axen_tick_task, USB_TASKQ_DRIVER);
   1395 	}
   1396 	mutex_exit(&sc->axen_lock);
   1397 }
   1398 
   1399 static void
   1400 axen_tick_task(void *xsc)
   1401 {
   1402 	struct axen_softc * const sc = xsc;
   1403 	struct ifnet *ifp;
   1404 	struct mii_data *mii;
   1405 
   1406 	if (sc == NULL)
   1407 		return;
   1408 
   1409 	mutex_enter(&sc->axen_lock);
   1410 	if (sc->axen_stopping || sc->axen_dying) {
   1411 		mutex_exit(&sc->axen_lock);
   1412 		return;
   1413 	}
   1414 
   1415 	ifp = GET_IFP(sc);
   1416 	mii = GET_MII(sc);
   1417 	if (mii == NULL) {
   1418 		mutex_exit(&sc->axen_lock);
   1419 		return;
   1420 	}
   1421 
   1422 	sc->axen_refcnt++;
   1423 	mutex_exit(&sc->axen_lock);
   1424 
   1425 	if (sc->axen_timer != 0 && --sc->axen_timer == 0)
   1426 		axen_watchdog(ifp);
   1427 
   1428 	mii_tick(mii);
   1429 
   1430 	if (sc->axen_link == 0)
   1431 		axen_miibus_statchg(ifp);
   1432 
   1433 	mutex_enter(&sc->axen_lock);
   1434 	if (--sc->axen_refcnt < 0)
   1435 		cv_broadcast(&sc->axen_detachcv);
   1436 	if (!sc->axen_stopping && !sc->axen_dying)
   1437 		callout_schedule(&sc->axen_stat_ch, hz);
   1438 	mutex_exit(&sc->axen_lock);
   1439 }
   1440 
   1441 static int
   1442 axen_encap(struct axen_softc *sc, struct mbuf *m, int idx)
   1443 {
   1444 	struct ifnet *ifp = GET_IFP(sc);
   1445 	struct axen_chain *c;
   1446 	usbd_status err;
   1447 	struct axen_sframe_hdr hdr;
   1448 	u_int length, boundary;
   1449 
   1450 	KASSERT(mutex_owned(&sc->axen_txlock));
   1451 
   1452 	c = &sc->axen_cdata.axen_tx_chain[idx];
   1453 
   1454 	/* XXX Is this needed?  wMaxPacketSize? */
   1455 	switch (sc->axen_udev->ud_speed) {
   1456 	case USB_SPEED_SUPER:
   1457 		boundary = 4096;
   1458 		break;
   1459 	case USB_SPEED_HIGH:
   1460 		boundary = 512;
   1461 		break;
   1462 	default:
   1463 		boundary = 64;
   1464 		break;
   1465 	}
   1466 
   1467 	length = m->m_pkthdr.len + sizeof(hdr);
   1468 	KASSERT(length <= sc->axen_tx_bufsz);
   1469 
   1470 	hdr.plen = htole32(m->m_pkthdr.len);
   1471 
   1472 	hdr.gso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) ?
   1473 	    m->m_pkthdr.segsz : 0;
   1474 	if ((length % boundary) == 0) {
   1475 		DPRINTF(("%s: boundary hit\n", device_xname(sc->axen_dev)));
   1476 		hdr.gso |= 0x80008000;	/* XXX enable padding */
   1477 	}
   1478 	hdr.gso = htole32(hdr.gso);
   1479 
   1480 	memcpy(c->axen_buf, &hdr, sizeof(hdr));
   1481 	m_copydata(m, 0, m->m_pkthdr.len, c->axen_buf + sizeof(hdr));
   1482 
   1483 	if (__predict_false(c->axen_xfer == NULL))
   1484 		return EIO;	/* XXX plugged out or down */
   1485 
   1486 	usbd_setup_xfer(c->axen_xfer, c, c->axen_buf, length,
   1487 	    USBD_FORCE_SHORT_XFER, 10000, axen_txeof);
   1488 
   1489 	/* Transmit */
   1490 	err = usbd_transfer(c->axen_xfer);
   1491 	if (err != USBD_IN_PROGRESS) {
   1492 		axen_stop(ifp, 0);
   1493 		return EIO;
   1494 	}
   1495 
   1496 	return 0;
   1497 }
   1498 
   1499 static void
   1500 axen_start_locked(struct ifnet *ifp)
   1501 {
   1502 	struct axen_softc * const sc = ifp->if_softc;
   1503 	struct mbuf *m;
   1504 	struct axen_cdata *cd = &sc->axen_cdata;
   1505 	int idx;
   1506 
   1507 	KASSERT(mutex_owned(&sc->axen_txlock));
   1508 
   1509 	if (sc->axen_link == 0)
   1510 		return;
   1511 
   1512 	if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING)
   1513 		return;
   1514 
   1515 	idx = cd->axen_tx_prod;
   1516 	while (cd->axen_tx_cnt < AXEN_TX_LIST_CNT) {
   1517 		IFQ_POLL(&ifp->if_snd, m);
   1518 		if (m == NULL)
   1519 			break;
   1520 
   1521 		if (axen_encap(sc, m, idx)) {
   1522 			ifp->if_flags |= IFF_OACTIVE; /* XXX */
   1523 			ifp->if_oerrors++;
   1524 			break;
   1525 		}
   1526 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1527 
   1528 		/*
   1529 		 * If there's a BPF listener, bounce a copy of this frame
   1530 		 * to him.
   1531 		 */
   1532 		bpf_mtap(ifp, m, BPF_D_OUT);
   1533 		m_freem(m);
   1534 
   1535 		idx = (idx + 1) % AXEN_TX_LIST_CNT;
   1536 		cd->axen_tx_cnt++;
   1537 	}
   1538 	cd->axen_tx_prod = idx;
   1539 
   1540 	if (cd->axen_tx_cnt >= AXEN_TX_LIST_CNT)
   1541 		ifp->if_flags |= IFF_OACTIVE;
   1542 
   1543 	/*
   1544 	 * Set a timeout in case the chip goes out to lunch.
   1545 	 */
   1546 	sc->axen_timer = 5;
   1547 }
   1548 
   1549 static void
   1550 axen_start(struct ifnet *ifp)
   1551 {
   1552 	struct axen_softc * const sc = ifp->if_softc;
   1553 
   1554 	mutex_enter(&sc->axen_txlock);
   1555 	if (!sc->axen_stopping)
   1556 		axen_start_locked(ifp);
   1557 	mutex_exit(&sc->axen_txlock);
   1558 }
   1559 
   1560 static int
   1561 axen_init_locked(struct ifnet *ifp)
   1562 {
   1563 	struct axen_softc * const sc = ifp->if_softc;
   1564 	struct axen_chain *c;
   1565 	usbd_status err;
   1566 	int i;
   1567 	uint16_t rxmode;
   1568 	uint16_t wval;
   1569 	uint8_t bval;
   1570 
   1571 	KASSERT(mutex_owned(&sc->axen_lock));
   1572 
   1573 	if (sc->axen_dying)
   1574 		return EIO;
   1575 
   1576 	/* Cancel pending I/O */
   1577 	axen_stop_locked(ifp, 1);
   1578 
   1579 	/* Reset the ethernet interface. */
   1580 	axen_reset(sc);
   1581 
   1582 	axen_lock_mii_sc_locked(sc);
   1583 
   1584 	/* XXX: ? */
   1585 	bval = 0x01;
   1586 	axen_cmd(sc, AXEN_CMD_MAC_WRITE, 1, AXEN_UNK_28, &bval);
   1587 
   1588 	/* Configure offloading engine. */
   1589 	axen_setcoe(sc);
   1590 
   1591 	/* Program promiscuous mode and multicast filters. */
   1592 	axen_iff_locked(sc);
   1593 
   1594 	/* Enable receiver, set RX mode */
   1595 	axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval);
   1596 	rxmode = le16toh(wval);
   1597 	rxmode |= AXEN_RXCTL_START;
   1598 	wval = htole16(rxmode);
   1599 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
   1600 
   1601 	axen_unlock_mii_sc_locked(sc);
   1602 
   1603 	/* Open RX and TX pipes. */
   1604 	err = usbd_open_pipe(sc->axen_iface, sc->axen_ed[AXEN_ENDPT_RX],
   1605 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axen_ep[AXEN_ENDPT_RX]);
   1606 	if (err) {
   1607 		aprint_error_dev(sc->axen_dev, "open rx pipe failed: %s\n",
   1608 		    usbd_errstr(err));
   1609 		return EIO;
   1610 	}
   1611 
   1612 	err = usbd_open_pipe(sc->axen_iface, sc->axen_ed[AXEN_ENDPT_TX],
   1613 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->axen_ep[AXEN_ENDPT_TX]);
   1614 	if (err) {
   1615 		aprint_error_dev(sc->axen_dev, "open tx pipe failed: %s\n",
   1616 		    usbd_errstr(err));
   1617 		return EIO;
   1618 	}
   1619 
   1620 	/* Init RX ring. */
   1621 	if (axen_rx_list_init(sc)) {
   1622 		aprint_error_dev(sc->axen_dev, "rx list init failed\n");
   1623 		return ENOBUFS;
   1624 	}
   1625 
   1626 	/* Init TX ring. */
   1627 	if (axen_tx_list_init(sc)) {
   1628 		aprint_error_dev(sc->axen_dev, "tx list init failed\n");
   1629 		return ENOBUFS;
   1630 	}
   1631 
   1632 	mutex_enter(&sc->axen_rxlock);
   1633 	mutex_enter(&sc->axen_txlock);
   1634 	sc->axen_stopping = false;
   1635 
   1636 	/* Start up the receive pipe. */
   1637 	for (i = 0; i < AXEN_RX_LIST_CNT; i++) {
   1638 		c = &sc->axen_cdata.axen_rx_chain[i];
   1639 
   1640 		usbd_setup_xfer(c->axen_xfer, c, c->axen_buf, sc->axen_rx_bufsz,
   1641 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axen_rxeof);
   1642 		usbd_transfer(c->axen_xfer);
   1643 	}
   1644 
   1645 	mutex_exit(&sc->axen_txlock);
   1646 	mutex_exit(&sc->axen_rxlock);
   1647 
   1648 	/* Indicate we are up and running. */
   1649 	ifp->if_flags |= IFF_RUNNING;
   1650 	ifp->if_flags &= ~IFF_OACTIVE;
   1651 
   1652 	callout_schedule(&sc->axen_stat_ch, hz);
   1653 	return 0;
   1654 }
   1655 
   1656 static int
   1657 axen_init(struct ifnet *ifp)
   1658 {
   1659 	struct axen_softc * const sc = ifp->if_softc;
   1660 
   1661 	mutex_enter(&sc->axen_lock);
   1662 	int ret = axen_init_locked(ifp);
   1663 	mutex_exit(&sc->axen_lock);
   1664 
   1665 	return ret;
   1666 }
   1667 
   1668 static int
   1669 axen_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1670 {
   1671 	struct axen_softc * const sc = ifp->if_softc;
   1672 	int error = 0;
   1673 
   1674 	switch (cmd) {
   1675 	case SIOCSIFFLAGS:
   1676 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1677 			break;
   1678 
   1679 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1680 		case IFF_RUNNING:
   1681 			axen_stop(ifp, 1);
   1682 			break;
   1683 		case IFF_UP:
   1684 			axen_init(ifp);
   1685 			break;
   1686 		case IFF_UP | IFF_RUNNING:
   1687 			if ((ifp->if_flags ^ sc->axen_if_flags) == IFF_PROMISC) {
   1688 				axen_iff(sc);
   1689 			} else
   1690 				axen_init(ifp);
   1691 			break;
   1692 		}
   1693 
   1694 		mutex_enter(&sc->axen_rxlock);
   1695 		mutex_enter(&sc->axen_txlock);
   1696 		sc->axen_if_flags = ifp->if_flags;
   1697 		mutex_exit(&sc->axen_txlock);
   1698 		mutex_exit(&sc->axen_rxlock);
   1699 		break;
   1700 
   1701 	default:
   1702 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
   1703 			break;
   1704 
   1705 		error = 0;
   1706 		switch (cmd) {
   1707 		case SIOCADDMULTI:
   1708 		case SIOCDELMULTI:
   1709 			axen_iff(sc);
   1710 			break;
   1711 		case SIOCSIFCAP:
   1712 			mutex_enter(&sc->axen_lock);
   1713 			axen_setcoe(sc);
   1714 			mutex_exit(&sc->axen_lock);
   1715 			break;
   1716 		default:
   1717 			break;
   1718 		}
   1719 		break;
   1720 	}
   1721 
   1722 	return error;
   1723 }
   1724 
   1725 static void
   1726 axen_watchdog(struct ifnet *ifp)
   1727 {
   1728 	struct axen_softc * const sc = ifp->if_softc;
   1729 	struct axen_chain *c;
   1730 	usbd_status stat;
   1731 
   1732 	ifp->if_oerrors++;
   1733 	aprint_error_dev(sc->axen_dev, "watchdog timeout\n");
   1734 
   1735 	c = &sc->axen_cdata.axen_tx_chain[0];
   1736 	usbd_get_xfer_status(c->axen_xfer, NULL, NULL, NULL, &stat);
   1737 	axen_txeof(c->axen_xfer, c, stat);
   1738 
   1739 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1740 		axen_start(ifp);
   1741 }
   1742 
   1743 /*
   1744  * Stop the adapter and free any mbufs allocated to the
   1745  * RX and TX lists.
   1746  */
   1747 static void
   1748 axen_stop_locked(struct ifnet *ifp, int disable)
   1749 {
   1750 	struct axen_softc * const sc = ifp->if_softc;
   1751 	struct axen_chain *c;
   1752 	usbd_status err;
   1753 	int i;
   1754 	uint16_t rxmode, wval;
   1755 
   1756 	KASSERT(mutex_owned(&sc->axen_lock));
   1757 	mutex_enter(&sc->axen_rxlock);
   1758 	mutex_enter(&sc->axen_txlock);
   1759 	sc->axen_stopping = true;
   1760 	mutex_exit(&sc->axen_txlock);
   1761 	mutex_exit(&sc->axen_rxlock);
   1762 
   1763 	axen_reset(sc);
   1764 
   1765 	/* Disable receiver, set RX mode */
   1766 	axen_lock_mii_sc_locked(sc);
   1767 	axen_cmd(sc, AXEN_CMD_MAC_READ2, 2, AXEN_MAC_RXCTL, &wval);
   1768 	rxmode = le16toh(wval);
   1769 	rxmode &= ~AXEN_RXCTL_START;
   1770 	wval = htole16(rxmode);
   1771 	axen_cmd(sc, AXEN_CMD_MAC_WRITE2, 2, AXEN_MAC_RXCTL, &wval);
   1772 	axen_unlock_mii_sc_locked(sc);
   1773 
   1774 	sc->axen_timer = 0;
   1775 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1776 
   1777 	callout_stop(&sc->axen_stat_ch);
   1778 	sc->axen_link = 0;
   1779 
   1780 	/* Stop transfers. */
   1781 	if (sc->axen_ep[AXEN_ENDPT_RX] != NULL) {
   1782 		err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_RX]);
   1783 		if (err) {
   1784 			aprint_error_dev(sc->axen_dev,
   1785 			    "abort rx pipe failed: %s\n", usbd_errstr(err));
   1786 
   1787 		}
   1788 	}
   1789 
   1790 	if (sc->axen_ep[AXEN_ENDPT_TX] != NULL) {
   1791 		err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_TX]);
   1792 		if (err) {
   1793 			aprint_error_dev(sc->axen_dev,
   1794 			    "abort tx pipe failed: %s\n", usbd_errstr(err));
   1795 		}
   1796 	}
   1797 
   1798 	if (sc->axen_ep[AXEN_ENDPT_INTR] != NULL) {
   1799 		err = usbd_abort_pipe(sc->axen_ep[AXEN_ENDPT_INTR]);
   1800 		if (err) {
   1801 			aprint_error_dev(sc->axen_dev,
   1802 			    "abort intr pipe failed: %s\n", usbd_errstr(err));
   1803 		}
   1804 	}
   1805 
   1806 	/* Free RX resources. */
   1807 	for (i = 0; i < AXEN_RX_LIST_CNT; i++) {
   1808 		c = &sc->axen_cdata.axen_rx_chain[i];
   1809 		if (c->axen_xfer != NULL) {
   1810 			usbd_destroy_xfer(c->axen_xfer);
   1811 			c->axen_xfer = NULL;
   1812 		}
   1813 	}
   1814 
   1815 	/* Free TX resources. */
   1816 	for (i = 0; i < AXEN_TX_LIST_CNT; i++) {
   1817 		c = &sc->axen_cdata.axen_tx_chain[i];
   1818 		if (c->axen_xfer != NULL) {
   1819 			usbd_destroy_xfer(c->axen_xfer);
   1820 			c->axen_xfer = NULL;
   1821 		}
   1822 	}
   1823 
   1824 	/* Close pipes. */
   1825 	if (sc->axen_ep[AXEN_ENDPT_RX] != NULL) {
   1826 		err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_RX]);
   1827 		if (err) {
   1828 			aprint_error_dev(sc->axen_dev,
   1829 			    "close rx pipe failed: %s\n", usbd_errstr(err));
   1830 		}
   1831 		sc->axen_ep[AXEN_ENDPT_RX] = NULL;
   1832 	}
   1833 
   1834 	if (sc->axen_ep[AXEN_ENDPT_TX] != NULL) {
   1835 		err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_TX]);
   1836 		if (err) {
   1837 			aprint_error_dev(sc->axen_dev,
   1838 			    "close tx pipe failed: %s\n", usbd_errstr(err));
   1839 		}
   1840 		sc->axen_ep[AXEN_ENDPT_TX] = NULL;
   1841 	}
   1842 
   1843 	if (sc->axen_ep[AXEN_ENDPT_INTR] != NULL) {
   1844 		err = usbd_close_pipe(sc->axen_ep[AXEN_ENDPT_INTR]);
   1845 		if (err) {
   1846 			aprint_error_dev(sc->axen_dev,
   1847 			    "close intr pipe failed: %s\n", usbd_errstr(err));
   1848 		}
   1849 		sc->axen_ep[AXEN_ENDPT_INTR] = NULL;
   1850 	}
   1851 }
   1852 
   1853 static void
   1854 axen_stop(struct ifnet *ifp, int disable)
   1855 {
   1856 	struct axen_softc * const sc = ifp->if_softc;
   1857 
   1858 	mutex_enter(&sc->axen_lock);
   1859 	axen_stop_locked(ifp, disable);
   1860 	mutex_exit(&sc->axen_lock);
   1861 }
   1862 
   1863 MODULE(MODULE_CLASS_DRIVER, if_axen, NULL);
   1864 
   1865 #ifdef _MODULE
   1866 #include "ioconf.c"
   1867 #endif
   1868 
   1869 static int
   1870 if_axen_modcmd(modcmd_t cmd, void *aux)
   1871 {
   1872 	int error = 0;
   1873 
   1874 	switch (cmd) {
   1875 	case MODULE_CMD_INIT:
   1876 #ifdef _MODULE
   1877 		error = config_init_component(cfdriver_ioconf_axen,
   1878 		    cfattach_ioconf_axen, cfdata_ioconf_axen);
   1879 #endif
   1880 		return error;
   1881 	case MODULE_CMD_FINI:
   1882 #ifdef _MODULE
   1883 		error = config_fini_component(cfdriver_ioconf_axen,
   1884 		    cfattach_ioconf_axen, cfdata_ioconf_axen);
   1885 #endif
   1886 		return error;
   1887 	default:
   1888 		return ENOTTY;
   1889 	}
   1890 }
   1891