Home | History | Annotate | Line # | Download | only in usb
if_axe.c revision 1.67.8.2
      1 /*	$NetBSD: if_axe.c,v 1.67.8.2 2017/01/26 21:54:24 skrll Exp $	*/
      2 /*	$OpenBSD: if_axe.c,v 1.137 2016/04/13 11:03:37 mpi Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg (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  * Copyright (c) 1997, 1998, 1999, 2000-2003
     22  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
     23  *
     24  * Redistribution and use in source and binary forms, with or without
     25  * modification, are permitted provided that the following conditions
     26  * are met:
     27  * 1. Redistributions of source code must retain the above copyright
     28  *    notice, this list of conditions and the following disclaimer.
     29  * 2. Redistributions in binary form must reproduce the above copyright
     30  *    notice, this list of conditions and the following disclaimer in the
     31  *    documentation and/or other materials provided with the distribution.
     32  * 3. All advertising materials mentioning features or use of this software
     33  *    must display the following acknowledgement:
     34  *	This product includes software developed by Bill Paul.
     35  * 4. Neither the name of the author nor the names of any co-contributors
     36  *    may be used to endorse or promote products derived from this software
     37  *    without specific prior written permission.
     38  *
     39  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     42  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     43  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     44  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     45  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     49  * THE POSSIBILITY OF SUCH DAMAGE.
     50  */
     51 
     52 /*
     53  * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
     54  * Used in the LinkSys USB200M and various other adapters.
     55  *
     56  * Written by Bill Paul <wpaul (at) windriver.com>
     57  * Senior Engineer
     58  * Wind River Systems
     59  */
     60 
     61 /*
     62  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
     63  * It uses an external PHY (reference designs use a RealTek chip),
     64  * and has a 64-bit multicast hash filter. There is some information
     65  * missing from the manual which one needs to know in order to make
     66  * the chip function:
     67  *
     68  * - You must set bit 7 in the RX control register, otherwise the
     69  *   chip won't receive any packets.
     70  * - You must initialize all 3 IPG registers, or you won't be able
     71  *   to send any packets.
     72  *
     73  * Note that this device appears to only support loading the station
     74  * address via autoload from the EEPROM (i.e. there's no way to manually
     75  * set it).
     76  *
     77  * (Adam Weinberger wanted me to name this driver if_gir.c.)
     78  */
     79 
     80 /*
     81  * Ax88178 and Ax88772 support backported from the OpenBSD driver.
     82  * 2007/02/12, J.R. Oldroyd, fbsd (at) opal.com
     83  *
     84  * Manual here:
     85  * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
     86  * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
     87  */
     88 
     89 #include <sys/cdefs.h>
     90 __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.67.8.2 2017/01/26 21:54:24 skrll Exp $");
     91 
     92 #ifdef _KERNEL_OPT
     93 #include "opt_inet.h"
     94 #include "opt_usb.h"
     95 #endif
     96 
     97 #include <sys/param.h>
     98 #include <sys/bus.h>
     99 #include <sys/device.h>
    100 #include <sys/kernel.h>
    101 #include <sys/mbuf.h>
    102 #include <sys/module.h>
    103 #include <sys/mutex.h>
    104 #include <sys/socket.h>
    105 #include <sys/sockio.h>
    106 #include <sys/systm.h>
    107 
    108 #include <sys/rnd.h>
    109 
    110 #include <net/if.h>
    111 #include <net/if_dl.h>
    112 #include <net/if_ether.h>
    113 #include <net/if_media.h>
    114 
    115 #include <net/bpf.h>
    116 
    117 #include <dev/mii/mii.h>
    118 #include <dev/mii/miivar.h>
    119 
    120 #include <dev/usb/usb.h>
    121 #include <dev/usb/usbhist.h>
    122 #include <dev/usb/usbdi.h>
    123 #include <dev/usb/usbdi_util.h>
    124 #include <dev/usb/usbdivar.h>
    125 #include <dev/usb/usbdevs.h>
    126 
    127 #include <dev/usb/if_axereg.h>
    128 
    129 /*
    130  * AXE_178_MAX_FRAME_BURST
    131  * max frame burst size for Ax88178 and Ax88772
    132  *	0	2048 bytes
    133  *	1	4096 bytes
    134  *	2	8192 bytes
    135  *	3	16384 bytes
    136  * use the largest your system can handle without USB stalling.
    137  *
    138  * NB: 88772 parts appear to generate lots of input errors with
    139  * a 2K rx buffer and 8K is only slightly faster than 4K on an
    140  * EHCI port on a T42 so change at your own risk.
    141  */
    142 #define AXE_178_MAX_FRAME_BURST	1
    143 
    144 
    145 #ifdef USB_DEBUG
    146 #ifndef AXE_DEBUG
    147 #define axedebug 0
    148 #else
    149 static int axedebug = 20;
    150 
    151 SYSCTL_SETUP(sysctl_hw_axe_setup, "sysctl hw.axe setup")
    152 {
    153 	int err;
    154 	const struct sysctlnode *rnode;
    155 	const struct sysctlnode *cnode;
    156 
    157 	err = sysctl_createv(clog, 0, NULL, &rnode,
    158 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "axe",
    159 	    SYSCTL_DESCR("axe global controls"),
    160 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    161 
    162 	if (err)
    163 		goto fail;
    164 
    165 	/* control debugging printfs */
    166 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    167 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    168 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    169 	    NULL, 0, &axedebug, sizeof(axedebug), CTL_CREATE, CTL_EOL);
    170 	if (err)
    171 		goto fail;
    172 
    173 	return;
    174 fail:
    175 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    176 }
    177 
    178 #endif /* AXE_DEBUG */
    179 #endif /* USB_DEBUG */
    180 
    181 #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(axedebug,1,FMT,A,B,C,D)
    182 #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(axedebug,N,FMT,A,B,C,D)
    183 #define AXEHIST_FUNC()		USBHIST_FUNC()
    184 #define AXEHIST_CALLED(name)	USBHIST_CALLED(axedebug)
    185 
    186 /*
    187  * Various supported device vendors/products.
    188  */
    189 static const struct axe_type axe_devs[] = {
    190 	{ { USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_UFE2000}, 0 },
    191 	{ { USB_VENDOR_ACERCM,		USB_PRODUCT_ACERCM_EP1427X2}, 0 },
    192 	{ { USB_VENDOR_APPLE,		USB_PRODUCT_APPLE_ETHERNET }, AX772 },
    193 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88172}, 0 },
    194 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88772}, AX772 },
    195 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88772A}, AX772 },
    196 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88772B}, AX772B },
    197 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88772B_1}, AX772B },
    198 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88178}, AX178 },
    199 	{ { USB_VENDOR_ATEN,		USB_PRODUCT_ATEN_UC210T}, 0 },
    200 	{ { USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D5055 }, AX178 },
    201 	{ { USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USB2AR}, 0},
    202 	{ { USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_USB200MV2}, AX772A },
    203 	{ { USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
    204 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100}, 0 },
    205 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100B1 }, AX772 },
    206 	{ { USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DUBE100B1 }, AX772 },
    207 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100C1 }, AX772B },
    208 	{ { USB_VENDOR_GOODWAY,		USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
    209 	{ { USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_ETGUS2 }, AX178 },
    210 	{ { USB_VENDOR_JVC,		USB_PRODUCT_JVC_MP_PRX1}, 0 },
    211 	{ { USB_VENDOR_LENOVO,		USB_PRODUCT_LENOVO_ETHERNET }, AX772B },
    212 	{ { USB_VENDOR_LINKSYS, 	USB_PRODUCT_LINKSYS_HG20F9}, AX772B },
    213 	{ { USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_USB200M}, 0 },
    214 	{ { USB_VENDOR_LINKSYS4,	USB_PRODUCT_LINKSYS4_USB1000 }, AX178 },
    215 	{ { USB_VENDOR_LOGITEC,		USB_PRODUCT_LOGITEC_LAN_GTJU2}, AX178 },
    216 	{ { USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUAU2GT}, AX178 },
    217 	{ { USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
    218 	{ { USB_VENDOR_MSI,		USB_PRODUCT_MSI_AX88772A}, AX772 },
    219 	{ { USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA120}, 0 },
    220 	{ { USB_VENDOR_OQO,		USB_PRODUCT_OQO_ETHER01PLUS }, AX772 },
    221 	{ { USB_VENDOR_PLANEX3,		USB_PRODUCT_PLANEX3_GU1000T }, AX178 },
    222 	{ { USB_VENDOR_SITECOM,		USB_PRODUCT_SITECOM_LN029}, 0 },
    223 	{ { USB_VENDOR_SITECOMEU,	USB_PRODUCT_SITECOMEU_LN028 }, AX178 },
    224 	{ { USB_VENDOR_SITECOMEU,	USB_PRODUCT_SITECOMEU_LN031 }, AX178 },
    225 	{ { USB_VENDOR_SYSTEMTALKS,	USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
    226 };
    227 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
    228 
    229 static const struct ax88772b_mfb ax88772b_mfb_table[] = {
    230 	{ 0x8000, 0x8001, 2048 },
    231 	{ 0x8100, 0x8147, 4096 },
    232 	{ 0x8200, 0x81EB, 6144 },
    233 	{ 0x8300, 0x83D7, 8192 },
    234 	{ 0x8400, 0x851E, 16384 },
    235 	{ 0x8500, 0x8666, 20480 },
    236 	{ 0x8600, 0x87AE, 24576 },
    237 	{ 0x8700, 0x8A3D, 32768 }
    238 };
    239 
    240 int	axe_match(device_t, cfdata_t, void *);
    241 void	axe_attach(device_t, device_t, void *);
    242 int	axe_detach(device_t, int);
    243 int	axe_activate(device_t, devact_t);
    244 
    245 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc),
    246 	axe_match, axe_attach, axe_detach, axe_activate);
    247 
    248 static int	axe_tx_list_init(struct axe_softc *);
    249 #if 0
    250 static void	axe_tx_list_free(struct axe_softc *);
    251 #endif
    252 static int	axe_rx_list_init(struct axe_softc *);
    253 static void	axe_rx_list_free(struct axe_softc *);
    254 static int	axe_encap(struct axe_softc *, struct mbuf *, int);
    255 static void	axe_rxeof(struct usbd_xfer *, void *, usbd_status);
    256 static void	axe_txeof(struct usbd_xfer *, void *, usbd_status);
    257 static void	axe_tick(void *);
    258 static void	axe_tick_task(void *);
    259 static void	axe_start(struct ifnet *);
    260 static void	axe_start_locked(struct ifnet *);
    261 static int	axe_ioctl(struct ifnet *, u_long, void *);
    262 static int	axe_init(struct ifnet *);
    263 static int	axe_init_locked(struct ifnet *);
    264 static void	axe_stop(struct ifnet *, int);
    265 static void	axe_stop_locked(struct ifnet *, int);
    266 static void	axe_watchdog(struct ifnet *);
    267 static int	axe_miibus_readreg_locked(device_t, int, int);
    268 static int	axe_miibus_readreg(device_t, int, int);
    269 static void	axe_miibus_writereg_locked(device_t, int, int, int);
    270 static void	axe_miibus_writereg(device_t, int, int, int);
    271 static void	axe_miibus_statchg(struct ifnet *);
    272 static int	axe_cmd(struct axe_softc *, int, int, int, void *);
    273 static void	axe_reset(struct axe_softc *);
    274 
    275 static void	axe_setmulti(struct axe_softc *);
    276 static void	axe_lock_mii(struct axe_softc *);
    277 static void	axe_unlock_mii(struct axe_softc *);
    278 
    279 static void	axe_ax88178_init(struct axe_softc *);
    280 static void	axe_ax88772_init(struct axe_softc *);
    281 
    282 /* Get exclusive access to the MII registers */
    283 static void
    284 axe_lock_mii(struct axe_softc *sc)
    285 {
    286 
    287 	sc->axe_refcnt++;
    288 	mutex_enter(&sc->axe_mii_lock);
    289 }
    290 
    291 static void
    292 axe_unlock_mii(struct axe_softc *sc)
    293 {
    294 
    295 	mutex_exit(&sc->axe_mii_lock);
    296 	if (--sc->axe_refcnt < 0)
    297 		usb_detach_wakeupold((sc->axe_dev));
    298 }
    299 
    300 static int
    301 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
    302 {
    303 	AXEHIST_FUNC(); AXEHIST_CALLED();
    304 	usb_device_request_t req;
    305 	usbd_status err;
    306 
    307 	KASSERT(mutex_owned(&sc->axe_mii_lock));
    308 
    309 	if (sc->axe_dying)
    310 		return 0;
    311 
    312 	DPRINTFN(20, "cmd %#x index %#x val %#x", cmd, index, val, 0);
    313 
    314 	if (AXE_CMD_DIR(cmd))
    315 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    316 	else
    317 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    318 	req.bRequest = AXE_CMD_CMD(cmd);
    319 	USETW(req.wValue, val);
    320 	USETW(req.wIndex, index);
    321 	USETW(req.wLength, AXE_CMD_LEN(cmd));
    322 
    323 	err = usbd_do_request(sc->axe_udev, &req, buf);
    324 
    325 	if (err) {
    326 		DPRINTF("cmd %d err %d", cmd, err, 0, 0);
    327 		return -1;
    328 	}
    329 	return 0;
    330 }
    331 
    332 static int
    333 axe_miibus_readreg_locked(device_t dev, int phy, int reg)
    334 {
    335 	AXEHIST_FUNC(); AXEHIST_CALLED();
    336 	struct axe_softc *sc = device_private(dev);
    337 	usbd_status err;
    338 	uint16_t val;
    339 
    340 	DPRINTFN(30, "phy 0x%x reg 0x%x\n", phy, reg, 0, 0);
    341 
    342 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    343 
    344 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
    345 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    346 	if (err) {
    347 		aprint_error_dev(sc->axe_dev, "read PHY failed\n");
    348 		return -1;
    349 	}
    350 
    351 	val = le16toh(val);
    352 	if (AXE_IS_772(sc) && reg == MII_BMSR) {
    353 		/*
    354 		 * BMSR of AX88772 indicates that it supports extended
    355 		 * capability but the extended status register is
    356 		 * reserved for embedded ethernet PHY. So clear the
    357 		 * extended capability bit of BMSR.
    358 		 */
    359 		 val &= ~BMSR_EXTCAP;
    360 	}
    361 
    362 	DPRINTFN(30, "phy 0x%x reg 0x%x val %#x", phy, reg, val, 0);
    363 
    364 	return val;
    365 }
    366 
    367 static int
    368 axe_miibus_readreg(device_t dev, int phy, int reg)
    369 {
    370 	struct axe_softc *sc = device_private(dev);
    371 	int val;
    372 
    373 	if (sc->axe_dying)
    374 		return 0;
    375 
    376 	if (sc->axe_phyno != phy)
    377 		return 0;
    378 
    379 	axe_lock_mii(sc);
    380 	val = axe_miibus_readreg_locked(dev, phy, reg);
    381 	axe_unlock_mii(sc);
    382 
    383 	return val;
    384 }
    385 
    386 static void
    387 axe_miibus_writereg_locked(device_t dev, int phy, int reg, int aval)
    388 {
    389 	struct axe_softc *sc = device_private(dev);
    390 	usbd_status err;
    391 	uint16_t val;
    392 
    393 	val = htole16(aval);
    394 
    395 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    396 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
    397 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    398 
    399 	if (err) {
    400 		aprint_error_dev(sc->axe_dev, "write PHY failed\n");
    401 		return;
    402 	}
    403 }
    404 
    405 static void
    406 axe_miibus_writereg(device_t dev, int phy, int reg, int aval)
    407 {
    408 	struct axe_softc *sc = device_private(dev);
    409 
    410 	if (sc->axe_dying)
    411 		return;
    412 
    413 	if (sc->axe_phyno != phy)
    414 		return;
    415 
    416 	axe_lock_mii(sc);
    417 	axe_miibus_writereg_locked(dev, phy, reg, aval);
    418 	axe_unlock_mii(sc);
    419 }
    420 
    421 static void
    422 axe_miibus_statchg(struct ifnet *ifp)
    423 {
    424 	AXEHIST_FUNC(); AXEHIST_CALLED();
    425 
    426 	struct axe_softc *sc = ifp->if_softc;
    427 	struct mii_data *mii = &sc->axe_mii;
    428 	int val, err;
    429 
    430 	val = 0;
    431 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
    432 		val |= AXE_MEDIA_FULL_DUPLEX;
    433 		if (AXE_IS_178_FAMILY(sc)) {
    434 			if ((IFM_OPTIONS(mii->mii_media_active) &
    435 			    IFM_ETH_TXPAUSE) != 0)
    436 				val |= AXE_178_MEDIA_TXFLOW_CONTROL_EN;
    437 			if ((IFM_OPTIONS(mii->mii_media_active) &
    438 			    IFM_ETH_RXPAUSE) != 0)
    439 				val |= AXE_178_MEDIA_RXFLOW_CONTROL_EN;
    440 		}
    441 	}
    442 	if (AXE_IS_178_FAMILY(sc)) {
    443 		val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
    444 		if (sc->axe_flags & AX178)
    445 			val |= AXE_178_MEDIA_ENCK;
    446 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    447 		case IFM_1000_T:
    448 			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
    449 			break;
    450 		case IFM_100_TX:
    451 			val |= AXE_178_MEDIA_100TX;
    452 			break;
    453 		case IFM_10_T:
    454 			/* doesn't need to be handled */
    455 			break;
    456 		}
    457 	}
    458 
    459 	DPRINTF("val=0x%x", val, 0, 0, 0);
    460 	axe_lock_mii(sc);
    461 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
    462 	axe_unlock_mii(sc);
    463 	if (err) {
    464 		aprint_error_dev(sc->axe_dev, "media change failed\n");
    465 		return;
    466 	}
    467 }
    468 
    469 static void
    470 axe_setmulti(struct axe_softc *sc)
    471 {
    472 	AXEHIST_FUNC(); AXEHIST_CALLED();
    473 	struct ifnet *ifp = &sc->sc_if;
    474 	struct ether_multi *enm;
    475 	struct ether_multistep step;
    476 	uint32_t h = 0;
    477 	uint16_t rxmode;
    478 	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    479 
    480 	if (sc->axe_dying)
    481 		return;
    482 
    483 	axe_lock_mii(sc);
    484 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
    485 	rxmode = le16toh(rxmode);
    486 
    487 	rxmode &=
    488 	    ~(AXE_RXCMD_ALLMULTI | AXE_RXCMD_PROMISC |
    489 	    AXE_RXCMD_BROADCAST | AXE_RXCMD_MULTICAST);
    490 
    491 	rxmode |=
    492 	    (ifp->if_flags & IFF_BROADCAST) ? AXE_RXCMD_BROADCAST : 0;
    493 
    494 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
    495 		if (ifp->if_flags & IFF_PROMISC)
    496 			rxmode |= AXE_RXCMD_PROMISC;
    497 		goto allmulti;
    498 	}
    499 
    500 	/* Now program new ones */
    501 	ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
    502 	while (enm != NULL) {
    503 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    504 		    ETHER_ADDR_LEN) != 0)
    505 			goto allmulti;
    506 
    507 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
    508 		hashtbl[h >> 3] |= 1U << (h & 7);
    509 		ETHER_NEXT_MULTI(step, enm);
    510 	}
    511 	ifp->if_flags &= ~IFF_ALLMULTI;
    512 	rxmode |= AXE_RXCMD_MULTICAST;
    513 
    514 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
    515 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    516 	axe_unlock_mii(sc);
    517 	return;
    518 
    519  allmulti:
    520 	ifp->if_flags |= IFF_ALLMULTI;
    521 	rxmode |= AXE_RXCMD_ALLMULTI;
    522 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    523 	axe_unlock_mii(sc);
    524 }
    525 
    526 
    527 static void
    528 axe_reset(struct axe_softc *sc)
    529 {
    530 
    531 	if (sc->axe_dying)
    532 		return;
    533 
    534 	/*
    535 	 * softnet_lock can be taken when NET_MPAFE is not defined when calling
    536 	 * if_addr_init -> if_init.  This doesn't mixe well with the
    537 	 * usbd_delay_ms calls in the init routines as things like nd6_slowtimo
    538 	 * can fire during the wait and attempt to take softnet_lock and then
    539 	 * block the softclk thread meaing the wait never ends.
    540 	 */
    541 #ifndef NET_MPSAFE
    542 	/* XXX What to reset? */
    543 
    544 	/* Wait a little while for the chip to get its brains in order. */
    545 	DELAY(1000);
    546 #else
    547 	axe_lock_mii(sc);
    548 
    549 	if (sc->axe_flags & AX178) {
    550 		axe_ax88178_init(sc);
    551 	} else if (sc->axe_flags & AX772) {
    552 		axe_ax88772_init(sc);
    553 	} else if (sc->axe_flags & AX772A) {
    554 		axe_ax88772a_init(sc);
    555 	} else if (sc->axe_flags & AX772B) {
    556 		axe_ax88772b_init(sc);
    557 	}
    558 	axe_unlock_mii(sc);
    559 #endif
    560 }
    561 
    562 static int
    563 axe_get_phyno(struct axe_softc *sc, int sel)
    564 {
    565 	int phyno;
    566 
    567 	switch (AXE_PHY_TYPE(sc->axe_phyaddrs[sel])) {
    568 	case PHY_TYPE_100_HOME:
    569 		/* FALLTHROUGH */
    570 	case PHY_TYPE_GIG:
    571 		phyno = AXE_PHY_NO(sc->axe_phyaddrs[sel]);
    572 		break;
    573 	case PHY_TYPE_SPECIAL:
    574 		/* FALLTHROUGH */
    575 	case PHY_TYPE_RSVD:
    576 		/* FALLTHROUGH */
    577 	case PHY_TYPE_NON_SUP:
    578 		/* FALLTHROUGH */
    579 	default:
    580 		phyno = -1;
    581 		break;
    582 	}
    583 
    584 	return phyno;
    585 }
    586 
    587 #define	AXE_GPIO_WRITE(x, y)	do {				\
    588 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL);		\
    589 	usbd_delay_ms(sc->axe_udev, hztoms(y));			\
    590 } while (0)
    591 
    592 static void
    593 axe_ax88178_init(struct axe_softc *sc)
    594 {
    595 	AXEHIST_FUNC(); AXEHIST_CALLED();
    596 	int gpio0, ledmode, phymode;
    597 	uint16_t eeprom, val;
    598 
    599 	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
    600 	/* XXX magic */
    601 	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
    602 	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
    603 
    604 	eeprom = le16toh(eeprom);
    605 
    606 	DPRINTF("EEPROM is 0x%x", eeprom, 0, 0, 0);
    607 
    608 	/* if EEPROM is invalid we have to use to GPIO0 */
    609 	if (eeprom == 0xffff) {
    610 		phymode = AXE_PHY_MODE_MARVELL;
    611 		gpio0 = 1;
    612 		ledmode = 0;
    613 	} else {
    614 		phymode = eeprom & 0x7f;
    615 		gpio0 = (eeprom & 0x80) ? 0 : 1;
    616 		ledmode = eeprom >> 8;
    617 	}
    618 
    619 	DPRINTF("use gpio0: %d, phymode %d", gpio0, phymode, 0, 0);
    620 
    621 	/* Program GPIOs depending on PHY hardware. */
    622 	switch (phymode) {
    623 	case AXE_PHY_MODE_MARVELL:
    624 		if (gpio0 == 1) {
    625 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
    626 			    hz / 32);
    627 			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
    628 			    hz / 32);
    629 			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
    630 			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
    631 			    hz / 32);
    632 		} else {
    633 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
    634 			    AXE_GPIO1_EN, hz / 3);
    635 			if (ledmode == 1) {
    636 				AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
    637 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
    638 				    hz / 3);
    639 			} else {
    640 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
    641 				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
    642 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
    643 				    AXE_GPIO2_EN, hz / 4);
    644 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
    645 				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
    646 			}
    647 		}
    648 		break;
    649 	case AXE_PHY_MODE_CICADA:
    650 	case AXE_PHY_MODE_CICADA_V2:
    651 	case AXE_PHY_MODE_CICADA_V2_ASIX:
    652 		if (gpio0 == 1)
    653 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
    654 			    AXE_GPIO0_EN, hz / 32);
    655 		else
    656 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
    657 			    AXE_GPIO1_EN, hz / 32);
    658 		break;
    659 	case AXE_PHY_MODE_AGERE:
    660 		AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
    661 		    AXE_GPIO1_EN, hz / 32);
    662 		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
    663 		    AXE_GPIO2_EN, hz / 32);
    664 		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
    665 		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
    666 		    AXE_GPIO2_EN, hz / 32);
    667 		break;
    668 	case AXE_PHY_MODE_REALTEK_8211CL:
    669 	case AXE_PHY_MODE_REALTEK_8211BN:
    670 	case AXE_PHY_MODE_REALTEK_8251CL:
    671 		val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
    672 		    AXE_GPIO1 | AXE_GPIO1_EN;
    673 		AXE_GPIO_WRITE(val, hz / 32);
    674 		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
    675 		AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
    676 		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
    677 		if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
    678 			axe_miibus_writereg_locked(sc->axe_dev,
    679 			    sc->axe_phyno, 0x1F, 0x0005);
    680 			axe_miibus_writereg_locked(sc->axe_dev,
    681 			    sc->axe_phyno, 0x0C, 0x0000);
    682 			val = axe_miibus_readreg_locked(sc->axe_dev,
    683 			    sc->axe_phyno, 0x0001);
    684 			axe_miibus_writereg_locked(sc->axe_dev,
    685 			    sc->axe_phyno, 0x01, val | 0x0080);
    686 			axe_miibus_writereg_locked(sc->axe_dev,
    687 			    sc->axe_phyno, 0x1F, 0x0000);
    688 		}
    689 		break;
    690 	default:
    691 		/* Unknown PHY model or no need to program GPIOs. */
    692 		break;
    693 	}
    694 
    695 	/* soft reset */
    696 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
    697 	usbd_delay_ms(sc->axe_udev, 150);
    698 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
    699 	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
    700 	usbd_delay_ms(sc->axe_udev, 150);
    701 	/* Enable MII/GMII/RGMII interface to work with external PHY. */
    702 	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
    703 	usbd_delay_ms(sc->axe_udev, 10);
    704 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
    705 }
    706 
    707 static void
    708 axe_ax88772_init(struct axe_softc *sc)
    709 {
    710 	AXEHIST_FUNC(); AXEHIST_CALLED();
    711 
    712 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
    713 	usbd_delay_ms(sc->axe_udev, 40);
    714 
    715 	if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) {
    716 		/* ask for the embedded PHY */
    717 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0,
    718 		    AXE_SW_PHY_SELECT_EMBEDDED, NULL);
    719 		usbd_delay_ms(sc->axe_udev, 10);
    720 
    721 		/* power down and reset state, pin reset state */
    722 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
    723 		usbd_delay_ms(sc->axe_udev, 60);
    724 
    725 		/* power down/reset state, pin operating state */
    726 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
    727 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
    728 		usbd_delay_ms(sc->axe_udev, 150);
    729 
    730 		/* power up, reset */
    731 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
    732 
    733 		/* power up, operating */
    734 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
    735 		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
    736 	} else {
    737 		/* ask for external PHY */
    738 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_EXT,
    739 		    NULL);
    740 		usbd_delay_ms(sc->axe_udev, 10);
    741 
    742 		/* power down internal PHY */
    743 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
    744 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
    745 	}
    746 
    747 	usbd_delay_ms(sc->axe_udev, 150);
    748 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
    749 }
    750 
    751 static int
    752 axe_ifflags_cb(struct ethercom *ec)
    753 {
    754 	struct ifnet *ifp = &ec->ec_if;
    755 	struct axe_softc *sc = ifp->if_softc;
    756 	int rc = 0;
    757 
    758 	mutex_enter(&sc->axe_lock);
    759 	int change = ifp->if_flags ^ sc->axe_if_flags;
    760 	sc->axe_if_flags = ifp->if_flags;
    761 
    762 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
    763 		rc = ENETRESET;
    764 		goto out;
    765 	}
    766 
    767 	if ((change & IFF_PROMISC) != 0) {
    768 		axe_setmulti(sc);
    769 	}
    770 
    771 out:
    772 	mutex_exit(&sc->axe_lock);
    773 
    774 	return rc;
    775 }
    776 
    777 static void
    778 axe_ax88772_phywake(struct axe_softc *sc)
    779 {
    780 	AXEHIST_FUNC(); AXEHIST_CALLED();
    781 
    782 	if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) {
    783 		/* Manually select internal(embedded) PHY - MAC mode. */
    784 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0,
    785 		    AXE_SW_PHY_SELECT_EMBEDDED,
    786 		    NULL);
    787 		usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
    788 	} else {
    789 		/*
    790 		 * Manually select external PHY - MAC mode.
    791 		 * Reverse MII/RMII is for AX88772A PHY mode.
    792 		 */
    793 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
    794 		    AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
    795 		usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
    796 	}
    797 
    798 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
    799 	    AXE_SW_RESET_IPRL, NULL);
    800 
    801 	/* T1 = min 500ns everywhere */
    802 	usbd_delay_ms(sc->axe_udev, 150);
    803 
    804 	/* Take PHY out of power down. */
    805 	if (sc->axe_phyno == AXE_772_PHY_NO_EPHY) {
    806 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
    807 	} else {
    808 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRTE, NULL);
    809 	}
    810 
    811 	/* 772 T2 is 60ms. 772A T2 is 160ms, 772B T2 is 600ms */
    812 	usbd_delay_ms(sc->axe_udev, 600);
    813 
    814 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
    815 
    816 	/* T3 = 500ns everywhere */
    817 	usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
    818 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
    819 	usbd_delay_ms(sc->axe_udev, hztoms(hz / 32));
    820 }
    821 
    822 static void
    823 axe_ax88772a_init(struct axe_softc *sc)
    824 {
    825 	AXEHIST_FUNC(); AXEHIST_CALLED();
    826 
    827 	/* Reload EEPROM. */
    828 	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
    829 	axe_ax88772_phywake(sc);
    830 	/* Stop MAC. */
    831 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
    832 }
    833 
    834 static void
    835 axe_ax88772b_init(struct axe_softc *sc)
    836 {
    837 	AXEHIST_FUNC(); AXEHIST_CALLED();
    838 	uint16_t eeprom;
    839 	int i;
    840 
    841 	/* Reload EEPROM. */
    842 	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM , hz / 32);
    843 
    844 	/*
    845 	 * Save PHY power saving configuration(high byte) and
    846 	 * clear EEPROM checksum value(low byte).
    847 	 */
    848 	axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG, &eeprom);
    849 	sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00;
    850 
    851 	/*
    852 	 * Auto-loaded default station address from internal ROM is
    853 	 * 00:00:00:00:00:00 such that an explicit access to EEPROM
    854 	 * is required to get real station address.
    855 	 */
    856 	uint8_t *eaddr = sc->axe_enaddr;
    857 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
    858 		axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_NODE_ID + i,
    859 		    &eeprom);
    860 		eeprom = le16toh(eeprom);
    861 		*eaddr++ = (uint8_t)(eeprom & 0xFF);
    862 		*eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF);
    863 	}
    864 	/* Wakeup PHY. */
    865 	axe_ax88772_phywake(sc);
    866 	/* Stop MAC. */
    867 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
    868 }
    869 
    870 #undef	AXE_GPIO_WRITE
    871 
    872 /*
    873  * Probe for a AX88172 chip.
    874  */
    875 int
    876 axe_match(device_t parent, cfdata_t match, void *aux)
    877 {
    878 	struct usb_attach_arg *uaa = aux;
    879 
    880 	return axe_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    881 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    882 }
    883 
    884 /*
    885  * Attach the interface. Allocate softc structures, do ifmedia
    886  * setup and ethernet/BPF attach.
    887  */
    888 void
    889 axe_attach(device_t parent, device_t self, void *aux)
    890 {
    891 	AXEHIST_FUNC(); AXEHIST_CALLED();
    892 	struct axe_softc *sc = device_private(self);
    893 	struct usb_attach_arg *uaa = aux;
    894 	struct usbd_device *dev = uaa->uaa_device;
    895 	usbd_status err;
    896 	usb_interface_descriptor_t *id;
    897 	usb_endpoint_descriptor_t *ed;
    898 	struct mii_data	*mii;
    899 	char *devinfop;
    900 	const char *devname = device_xname(self);
    901 	struct ifnet *ifp;
    902 	int i, s;
    903 
    904 	aprint_naive("\n");
    905 	aprint_normal("\n");
    906 
    907 	sc->axe_dev = self;
    908 	sc->axe_udev = dev;
    909 
    910 	devinfop = usbd_devinfo_alloc(dev, 0);
    911 	aprint_normal_dev(self, "%s\n", devinfop);
    912 	usbd_devinfo_free(devinfop);
    913 
    914 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
    915 	if (err) {
    916 		aprint_error_dev(self, "failed to set configuration"
    917 		    ", err=%s\n", usbd_errstr(err));
    918 		return;
    919 	}
    920 
    921 	sc->axe_flags = axe_lookup(uaa->uaa_vendor, uaa->uaa_product)->axe_flags;
    922 
    923 	mutex_init(&sc->axe_lock, MUTEX_DEFAULT, IPL_NONE);
    924 	mutex_init(&sc->axe_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    925 	mutex_init(&sc->axe_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    926 	mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    927 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc, 0);
    928 
    929 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
    930 	if (err) {
    931 		aprint_error_dev(self, "getting interface handle failed\n");
    932 		return;
    933 	}
    934 
    935 	sc->axe_product = uaa->uaa_product;
    936 	sc->axe_vendor = uaa->uaa_vendor;
    937 
    938 	id = usbd_get_interface_descriptor(sc->axe_iface);
    939 
    940 	/* decide on what our bufsize will be */
    941 	if (AXE_IS_178_FAMILY(sc))
    942 		sc->axe_bufsz = (sc->axe_udev->ud_speed == USB_SPEED_HIGH) ?
    943 		    AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ;
    944 	else
    945 		sc->axe_bufsz = AXE_172_BUFSZ;
    946 
    947 	sc->axe_ed[AXE_ENDPT_RX] = -1;
    948 	sc->axe_ed[AXE_ENDPT_TX] = -1;
    949 	sc->axe_ed[AXE_ENDPT_INTR] = -1;
    950 
    951 	/* Find endpoints. */
    952 	for (i = 0; i < id->bNumEndpoints; i++) {
    953 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
    954 		if (ed == NULL) {
    955 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    956 			return;
    957 		}
    958 		const uint8_t xt = UE_GET_XFERTYPE(ed->bmAttributes);
    959 		const uint8_t dir = UE_GET_DIR(ed->bEndpointAddress);
    960 
    961 		if (dir == UE_DIR_IN && xt == UE_BULK &&
    962 		    sc->axe_ed[AXE_ENDPT_RX] == -1) {
    963 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
    964 		} else if (dir == UE_DIR_OUT && xt == UE_BULK &&
    965 		    sc->axe_ed[AXE_ENDPT_TX] == -1) {
    966 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
    967 		} else if (dir == UE_DIR_IN && xt == UE_INTERRUPT) {
    968 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
    969 		}
    970 	}
    971 
    972 	s = splnet();
    973 
    974 	/* We need the PHYID for init dance in some cases */
    975 	axe_lock_mii(sc);
    976 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
    977 
    978 	DPRINTF(" phyaddrs[0]: %x phyaddrs[1]: %x",
    979 	    sc->axe_phyaddrs[0], sc->axe_phyaddrs[1], 0, 0);
    980 	sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
    981 	if (sc->axe_phyno == -1)
    982 		sc->axe_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
    983 	if (sc->axe_phyno == -1) {
    984 		DPRINTF(" no valid PHY address found, assuming PHY address 0",
    985 		    0, 0, 0, 0);
    986 		sc->axe_phyno = 0;
    987 	}
    988 
    989 	/* Initialize controller and get station address. */
    990 
    991 	if (sc->axe_flags & AX178) {
    992 		axe_ax88178_init(sc);
    993 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, sc->axe_enaddr);
    994 	} else if (sc->axe_flags & AX772) {
    995 		axe_ax88772_init(sc);
    996 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, sc->axe_enaddr);
    997 	} else if (sc->axe_flags & AX772A) {
    998 		axe_ax88772a_init(sc);
    999 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, sc->axe_enaddr);
   1000 	} else if (sc->axe_flags & AX772B) {
   1001 		axe_ax88772b_init(sc);
   1002 	} else
   1003 		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, sc->axe_enaddr);
   1004 
   1005 	/*
   1006 	 * Fetch IPG values.
   1007 	 */
   1008 	if (sc->axe_flags & (AX772A | AX772B)) {
   1009 		/* Set IPG values. */
   1010 		sc->axe_ipgs[0] = AXE_IPG0_DEFAULT;
   1011 		sc->axe_ipgs[1] = AXE_IPG1_DEFAULT;
   1012 		sc->axe_ipgs[2] = AXE_IPG2_DEFAULT;
   1013 	} else
   1014 		axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->axe_ipgs);
   1015 
   1016 	axe_unlock_mii(sc);
   1017 
   1018 	/*
   1019 	 * An ASIX chip was detected. Inform the world.
   1020 	 */
   1021 	aprint_normal_dev(self, "Ethernet address %s\n",
   1022 	    ether_sprintf(sc->axe_enaddr));
   1023 
   1024 	/* Initialize interface info.*/
   1025 	ifp = &sc->sc_if;
   1026 	ifp->if_softc = sc;
   1027 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
   1028 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1029 	ifp->if_ioctl = axe_ioctl;
   1030 	ifp->if_start = axe_start;
   1031 	ifp->if_init = axe_init;
   1032 	ifp->if_stop = axe_stop;
   1033 	ifp->if_watchdog = axe_watchdog;
   1034 
   1035 	IFQ_SET_READY(&ifp->if_snd);
   1036 
   1037 	if (AXE_IS_178_FAMILY(sc))
   1038 		sc->axe_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
   1039 	if (sc->axe_flags & AX772B) {
   1040 		ifp->if_capabilities =
   1041 		    IFCAP_CSUM_IPv4_Rx |
   1042 		    IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
   1043 		    IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   1044 		/*
   1045 		 * Checksum offloading of AX88772B also works with VLAN
   1046 		 * tagged frames but there is no way to take advantage
   1047 		 * of the feature because vlan(4) assumes
   1048 		 * IFCAP_VLAN_HWTAGGING is prerequisite condition to
   1049 		 * support checksum offloading with VLAN. VLAN hardware
   1050 		 * tagging support of AX88772B is very limited so it's
   1051 		 * not possible to announce IFCAP_VLAN_HWTAGGING.
   1052 		 */
   1053 	}
   1054 	u_int adv_pause;
   1055 	if (sc->axe_flags & (AX772A | AX772B | AX178))
   1056 		adv_pause = MIIF_DOPAUSE;
   1057 	else
   1058 		adv_pause = 0;
   1059 	adv_pause = 0;
   1060 
   1061 	/* Initialize MII/media info. */
   1062 	mii = &sc->axe_mii;
   1063 	mii->mii_ifp = ifp;
   1064 	mii->mii_readreg = axe_miibus_readreg;
   1065 	mii->mii_writereg = axe_miibus_writereg;
   1066 	mii->mii_statchg = axe_miibus_statchg;
   1067 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1068 
   1069 	sc->axe_ec.ec_mii = mii;
   1070 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1071 
   1072 	mii_attach(sc->axe_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY,
   1073 	    adv_pause);
   1074 
   1075 	if (LIST_EMPTY(&mii->mii_phys)) {
   1076 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1077 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1078 	} else
   1079 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1080 
   1081 	/* Attach the interface. */
   1082 	if_attach(ifp);
   1083 	ether_ifattach(ifp, sc->axe_enaddr);
   1084 	ether_set_ifflags_cb(&sc->axe_ec, axe_ifflags_cb);
   1085 
   1086 	rnd_attach_source(&sc->rnd_source, device_xname(sc->axe_dev),
   1087 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
   1088 
   1089 	callout_init(&sc->axe_stat_ch, 0);
   1090 	callout_setfunc(&sc->axe_stat_ch, axe_tick, sc);
   1091 
   1092 	sc->axe_attached = true;
   1093 	splx(s);
   1094 
   1095 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev, sc->axe_dev);
   1096 
   1097 	if (!pmf_device_register(self, NULL, NULL))
   1098 		aprint_error_dev(self, "couldn't establish power handler\n");
   1099 }
   1100 
   1101 int
   1102 axe_detach(device_t self, int flags)
   1103 {
   1104 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1105 	struct axe_softc *sc = device_private(self);
   1106 	int s;
   1107 	struct ifnet *ifp = &sc->sc_if;
   1108 
   1109 	/* Detached before attached finished, so just bail out. */
   1110 	if (!sc->axe_attached)
   1111 		return 0;
   1112 
   1113 	pmf_device_deregister(self);
   1114 
   1115 	sc->axe_dying = true;
   1116 
   1117 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
   1118 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1119 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
   1120 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1121 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
   1122 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1123 
   1124 	/*
   1125 	 * Remove any pending tasks.  They cannot be executing because they run
   1126 	 * in the same thread as detach.
   1127 	 */
   1128 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
   1129 
   1130 	s = splusb();
   1131 
   1132 	if (ifp->if_flags & IFF_RUNNING)
   1133 		axe_stop(ifp, 1);
   1134 
   1135 
   1136 	if (--sc->axe_refcnt >= 0) {
   1137 		/* Wait for processes to go away. */
   1138 		usb_detach_waitold(sc->axe_dev);
   1139 	}
   1140 
   1141 	callout_destroy(&sc->axe_stat_ch);
   1142 	mutex_destroy(&sc->axe_lock);
   1143 	mutex_destroy(&sc->axe_txlock);
   1144 	mutex_destroy(&sc->axe_rxlock);
   1145 	mutex_destroy(&sc->axe_mii_lock);
   1146 	rnd_detach_source(&sc->rnd_source);
   1147 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1148 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
   1149 	ether_ifdetach(ifp);
   1150 	if_detach(ifp);
   1151 
   1152 #ifdef DIAGNOSTIC
   1153 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
   1154 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
   1155 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
   1156 		aprint_debug_dev(self, "detach has active endpoints\n");
   1157 #endif
   1158 
   1159 	sc->axe_attached = false;
   1160 
   1161 	splx(s);
   1162 
   1163 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev, sc->axe_dev);
   1164 
   1165 	return 0;
   1166 }
   1167 
   1168 int
   1169 axe_activate(device_t self, devact_t act)
   1170 {
   1171 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1172 	struct axe_softc *sc = device_private(self);
   1173 
   1174 	switch (act) {
   1175 	case DVACT_DEACTIVATE:
   1176 		if_deactivate(&sc->axe_ec.ec_if);
   1177 		sc->axe_dying = true;
   1178 		return 0;
   1179 	default:
   1180 		return EOPNOTSUPP;
   1181 	}
   1182 }
   1183 
   1184 static int
   1185 axe_rx_list_init(struct axe_softc *sc)
   1186 {
   1187 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1188 
   1189 	struct axe_cdata *cd;
   1190 	struct axe_chain *c;
   1191 	int i;
   1192 
   1193 	cd = &sc->axe_cdata;
   1194 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1195 		c = &cd->axe_rx_chain[i];
   1196 		c->axe_sc = sc;
   1197 		c->axe_idx = i;
   1198 		if (c->axe_xfer == NULL) {
   1199 			int err = usbd_create_xfer(sc->axe_ep[AXE_ENDPT_RX],
   1200 			    sc->axe_bufsz, USBD_SHORT_XFER_OK, 0, &c->axe_xfer);
   1201 			if (err)
   1202 				return err;
   1203 			c->axe_buf = usbd_get_buffer(c->axe_xfer);
   1204 		}
   1205 	}
   1206 
   1207 	return 0;
   1208 }
   1209 
   1210 static void
   1211 axe_rx_list_free(struct axe_softc *sc)
   1212 {
   1213 	/* Free RX resources */
   1214 	for (size_t i = 0; i < AXE_RX_LIST_CNT; i++) {
   1215 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
   1216 			usbd_destroy_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
   1217 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
   1218 		}
   1219 	}
   1220 }
   1221 
   1222 static int
   1223 axe_tx_list_init(struct axe_softc *sc)
   1224 {
   1225 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1226 	struct axe_cdata *cd;
   1227 	struct axe_chain *c;
   1228 	int i;
   1229 
   1230 	cd = &sc->axe_cdata;
   1231 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
   1232 		c = &cd->axe_tx_chain[i];
   1233 		c->axe_sc = sc;
   1234 		c->axe_idx = i;
   1235 		if (c->axe_xfer == NULL) {
   1236 			int err = usbd_create_xfer(sc->axe_ep[AXE_ENDPT_TX],
   1237 			    sc->axe_bufsz, USBD_FORCE_SHORT_XFER, 0,
   1238 			    &c->axe_xfer);
   1239 			if (err)
   1240 				return err;
   1241 			c->axe_buf = usbd_get_buffer(c->axe_xfer);
   1242 		}
   1243 	}
   1244 
   1245 	return 0;
   1246 }
   1247 
   1248 static void
   1249 axe_tx_list_free(struct axe_softc *sc)
   1250 {
   1251 	/* Free TX resources */
   1252 	for (size_t i = 0; i < AXE_TX_LIST_CNT; i++) {
   1253 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
   1254 			usbd_destroy_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
   1255 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
   1256 		}
   1257 	}
   1258 }
   1259 
   1260 /*
   1261  * A frame has been uploaded: pass the resulting mbuf chain up to
   1262  * the higher level protocols.
   1263  */
   1264 static void
   1265 axe_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1266 {
   1267 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1268 	struct axe_softc *sc;
   1269 	struct axe_chain *c;
   1270 	struct ifnet *ifp;
   1271 	uint8_t *buf;
   1272 	uint32_t total_len;
   1273 	struct mbuf *m;
   1274 	int s;
   1275 
   1276 	c = (struct axe_chain *)priv;
   1277 	sc = c->axe_sc;
   1278 	buf = c->axe_buf;
   1279 	ifp = &sc->sc_if;
   1280 
   1281 	if (sc->axe_dying)
   1282 		return;
   1283 
   1284 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1285 		return;
   1286 
   1287 	if (status != USBD_NORMAL_COMPLETION) {
   1288 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1289 			return;
   1290 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
   1291 			aprint_error_dev(sc->axe_dev, "usb errors on rx: %s\n",
   1292 			    usbd_errstr(status));
   1293 		}
   1294 		if (status == USBD_STALLED)
   1295 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
   1296 		goto done;
   1297 	}
   1298 
   1299 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1300 
   1301 	do {
   1302 		u_int pktlen = 0;
   1303 		u_int rxlen = 0;
   1304 		int flags = 0;
   1305 		if ((sc->axe_flags & AXSTD_FRAME) != 0) {
   1306 			struct axe_sframe_hdr hdr;
   1307 
   1308 			if (total_len < sizeof(hdr)) {
   1309 				ifp->if_ierrors++;
   1310 				goto done;
   1311 			}
   1312 
   1313 			memcpy(&hdr, buf, sizeof(hdr));
   1314 
   1315 			DPRINTFN(20, "total_len %#x len %x ilen %#x",
   1316 			    total_len,
   1317 			    (le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK),
   1318 			    (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK), 0);
   1319 
   1320 			total_len -= sizeof(hdr);
   1321 			buf += sizeof(hdr);
   1322 
   1323 			if (((le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK) ^
   1324 			    (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK)) !=
   1325 			    AXE_RH1M_RXLEN_MASK) {
   1326 				ifp->if_ierrors++;
   1327 				goto done;
   1328 			}
   1329 
   1330 			rxlen = le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK;
   1331 			if (total_len < rxlen) {
   1332 				pktlen = total_len;
   1333 				total_len = 0;
   1334 			} else {
   1335 				pktlen = rxlen;
   1336 				rxlen = roundup2(rxlen, 2);
   1337 				total_len -= rxlen;
   1338 			}
   1339 
   1340 		} else if ((sc->axe_flags & AXCSUM_FRAME) != 0) {
   1341 			struct axe_csum_hdr csum_hdr;
   1342 
   1343 			if (total_len <  sizeof(csum_hdr)) {
   1344 				ifp->if_ierrors++;
   1345 				goto done;
   1346 			}
   1347 
   1348 			memcpy(&csum_hdr, buf, sizeof(csum_hdr));
   1349 
   1350 			csum_hdr.len = le16toh(csum_hdr.len);
   1351 			csum_hdr.ilen = le16toh(csum_hdr.ilen);
   1352 			csum_hdr.cstatus = le16toh(csum_hdr.cstatus);
   1353 
   1354 			DPRINTFN(20, "total_len %#x len %#x ilen %#x"
   1355 			    " cstatus %#x", total_len,
   1356 			    csum_hdr.len, csum_hdr.ilen, csum_hdr.cstatus);
   1357 
   1358 			if ((AXE_CSUM_RXBYTES(csum_hdr.len) ^
   1359 			    AXE_CSUM_RXBYTES(csum_hdr.ilen)) !=
   1360 			    sc->sc_lenmask) {
   1361 				/* we lost sync */
   1362 				ifp->if_ierrors++;
   1363 				DPRINTFN(20, "len %#x ilen %#x lenmask %#x err",
   1364 				    AXE_CSUM_RXBYTES(csum_hdr.len),
   1365 				    AXE_CSUM_RXBYTES(csum_hdr.ilen),
   1366 				    sc->sc_lenmask, 0);
   1367 				goto done;
   1368 			}
   1369 			/*
   1370 			 * Get total transferred frame length including
   1371 			 * checksum header.  The length should be multiple
   1372 			 * of 4.
   1373 			 */
   1374 			pktlen = AXE_CSUM_RXBYTES(csum_hdr.len);
   1375 			u_int len = sizeof(csum_hdr) + pktlen;
   1376 			len = (len + 3) & ~3;
   1377 			if (total_len < len) {
   1378 				DPRINTFN(20, "total_len %#x < len %#x",
   1379 				    total_len, len, 0, 0);
   1380 				/* invalid length */
   1381 				ifp->if_ierrors++;
   1382 				goto done;
   1383 			}
   1384 			buf += sizeof(csum_hdr);
   1385 
   1386 			const uint16_t cstatus = csum_hdr.cstatus;
   1387 
   1388 			if (cstatus & AXE_CSUM_HDR_L3_TYPE_IPV4) {
   1389 				if (cstatus & AXE_CSUM_HDR_L4_CSUM_ERR)
   1390 					flags |= M_CSUM_TCP_UDP_BAD;
   1391 				if (cstatus & AXE_CSUM_HDR_L3_CSUM_ERR)
   1392 					flags |= M_CSUM_IPv4_BAD;
   1393 
   1394 				const uint16_t l4type =
   1395 				    cstatus & AXE_CSUM_HDR_L4_TYPE_MASK;
   1396 
   1397 				if (l4type == AXE_CSUM_HDR_L4_TYPE_TCP)
   1398 					flags |= M_CSUM_TCPv4;
   1399 				if (l4type == AXE_CSUM_HDR_L4_TYPE_UDP)
   1400 					flags |= M_CSUM_UDPv4;
   1401 			}
   1402 			if (total_len < len) {
   1403 				pktlen = total_len;
   1404 				total_len = 0;
   1405 			} else {
   1406 				total_len -= len;
   1407 				rxlen = len - sizeof(csum_hdr);
   1408 			}
   1409 			DPRINTFN(20, "total_len %#x len %#x pktlen %#x"
   1410 			    " rxlen %#x", total_len, len, pktlen, rxlen);
   1411 		} else { /* AX172 */
   1412 			pktlen = rxlen = total_len;
   1413 			total_len = 0;
   1414 		}
   1415 
   1416 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1417 		if (m == NULL) {
   1418 			ifp->if_ierrors++;
   1419 			goto done;
   1420 		}
   1421 
   1422 		if (pktlen > MHLEN - ETHER_ALIGN) {
   1423 			MCLGET(m, M_DONTWAIT);
   1424 			if ((m->m_flags & M_EXT) == 0) {
   1425 				m_freem(m);
   1426 				ifp->if_ierrors++;
   1427 				goto done;
   1428 			}
   1429 		}
   1430 		m->m_data += ETHER_ALIGN;
   1431 
   1432 		ifp->if_ipackets++;
   1433 		m->m_pkthdr.rcvif = ifp;
   1434 		m->m_pkthdr.len = m->m_len = pktlen;
   1435 		m->m_pkthdr.csum_flags = flags;
   1436 
   1437 		memcpy(mtod(m, uint8_t *), buf, pktlen);
   1438 		buf += rxlen;
   1439 
   1440 		DPRINTFN(10, "deliver %d (%#x)", m->m_len, m->m_len, 0, 0);
   1441 		s = splnet();
   1442 
   1443 		bpf_mtap(ifp, m);
   1444 
   1445 		(*(ifp)->if_input)((ifp), (m));
   1446 
   1447 		splx(s);
   1448 
   1449 	} while (total_len > 0);
   1450 
   1451  done:
   1452 
   1453 	/* Setup new transfer. */
   1454 	usbd_setup_xfer(xfer, c, c->axe_buf, sc->axe_bufsz,
   1455 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
   1456 	usbd_transfer(xfer);
   1457 
   1458 	DPRINTFN(10, "start rx", 0, 0, 0, 0);
   1459 }
   1460 
   1461 /*
   1462  * A frame was downloaded to the chip. It's safe for us to clean up
   1463  * the list buffers.
   1464  */
   1465 
   1466 static void
   1467 axe_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1468 {
   1469 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1470 	struct axe_chain *c = priv;
   1471 	struct axe_softc *sc = c->axe_sc;
   1472 	struct ifnet *ifp = &sc->sc_if;
   1473 	int s;
   1474 
   1475 
   1476 	if (sc->axe_dying)
   1477 		return;
   1478 
   1479 	s = splnet();
   1480 
   1481 	ifp->if_timer = 0;
   1482 	ifp->if_flags &= ~IFF_OACTIVE;
   1483 
   1484 	if (status != USBD_NORMAL_COMPLETION) {
   1485 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1486 			splx(s);
   1487 			return;
   1488 		}
   1489 		ifp->if_oerrors++;
   1490 		aprint_error_dev(sc->axe_dev, "usb error on tx: %s\n",
   1491 		    usbd_errstr(status));
   1492 		if (status == USBD_STALLED)
   1493 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
   1494 		splx(s);
   1495 		return;
   1496 	}
   1497 	ifp->if_opackets++;
   1498 
   1499 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1500 		axe_start(ifp);
   1501 
   1502 	splx(s);
   1503 }
   1504 
   1505 static void
   1506 axe_tick(void *xsc)
   1507 {
   1508 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1509 	struct axe_softc *sc = xsc;
   1510 
   1511 	if (sc == NULL)
   1512 		return;
   1513 
   1514 	if (sc->axe_dying)
   1515 		return;
   1516 
   1517 	/* Perform periodic stuff in process context */
   1518 	usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
   1519 }
   1520 
   1521 static void
   1522 axe_tick_task(void *xsc)
   1523 {
   1524 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1525 	int s;
   1526 	struct axe_softc *sc = xsc;
   1527 	struct ifnet *ifp;
   1528 	struct mii_data *mii;
   1529 
   1530 	if (sc == NULL)
   1531 		return;
   1532 
   1533 	if (sc->axe_dying)
   1534 		return;
   1535 
   1536 	ifp = &sc->sc_if;
   1537 	mii = &sc->axe_mii;
   1538 
   1539 	if (mii == NULL)
   1540 		return;
   1541 
   1542 	s = splnet();
   1543 
   1544 	mii_tick(mii);
   1545 	if (sc->axe_link == 0 &&
   1546 	    (mii->mii_media_status & IFM_ACTIVE) != 0 &&
   1547 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1548 		DPRINTF("got link", 0, 0, 0, 0);
   1549 		sc->axe_link++;
   1550 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1551 			axe_start(ifp);
   1552 	}
   1553 
   1554 	callout_schedule(&sc->axe_stat_ch, hz);
   1555 
   1556 	splx(s);
   1557 }
   1558 
   1559 static int
   1560 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
   1561 {
   1562 	struct ifnet *ifp = &sc->sc_if;
   1563 	struct axe_chain *c;
   1564 	usbd_status err;
   1565 	int length, boundary;
   1566 
   1567 	c = &sc->axe_cdata.axe_tx_chain[idx];
   1568 
   1569 	/*
   1570 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1571 	 * bytes at the beginning to hold the frame length.
   1572 	 */
   1573 	if (AXE_IS_178_FAMILY(sc)) {
   1574 	    	struct axe_sframe_hdr hdr;
   1575 
   1576 		boundary = (sc->axe_udev->ud_speed == USB_SPEED_HIGH) ? 512 : 64;
   1577 
   1578 		hdr.len = htole16(m->m_pkthdr.len);
   1579 		hdr.ilen = ~hdr.len;
   1580 
   1581 		memcpy(c->axe_buf, &hdr, sizeof(hdr));
   1582 		length = sizeof(hdr);
   1583 
   1584 		m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length);
   1585 		length += m->m_pkthdr.len;
   1586 
   1587 		if ((length % boundary) == 0) {
   1588 			hdr.len = 0x0000;
   1589 			hdr.ilen = 0xffff;
   1590 			memcpy(c->axe_buf + length, &hdr, sizeof(hdr));
   1591 			length += sizeof(hdr);
   1592 		}
   1593 	} else {
   1594 		m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
   1595 		length = m->m_pkthdr.len;
   1596 	}
   1597 
   1598 	usbd_setup_xfer(c->axe_xfer, c, c->axe_buf, length,
   1599 	    USBD_FORCE_SHORT_XFER, 10000, axe_txeof);
   1600 
   1601 	/* Transmit */
   1602 	err = usbd_transfer(c->axe_xfer);
   1603 	if (err != USBD_IN_PROGRESS) {
   1604 		axe_stop(ifp, 0);
   1605 		return EIO;
   1606 	}
   1607 
   1608 	sc->axe_cdata.axe_tx_cnt++;
   1609 
   1610 	return 0;
   1611 }
   1612 
   1613 
   1614 static void
   1615 axe_csum_cfg(struct axe_softc *sc)
   1616 {
   1617 	struct ifnet *ifp = &sc->sc_if;
   1618 	uint16_t csum1, csum2;
   1619 
   1620 	if ((sc->axe_flags & AX772B) != 0) {
   1621 		csum1 = 0;
   1622 		csum2 = 0;
   1623 		if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) != 0)
   1624 			csum1 |= AXE_TXCSUM_IP;
   1625 		if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) != 0)
   1626 			csum1 |= AXE_TXCSUM_TCP;
   1627 		if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) != 0)
   1628 			csum1 |= AXE_TXCSUM_UDP;
   1629 		if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) != 0)
   1630 			csum1 |= AXE_TXCSUM_TCPV6;
   1631 		if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) != 0)
   1632 			csum1 |= AXE_TXCSUM_UDPV6;
   1633 		axe_cmd(sc, AXE_772B_CMD_WRITE_TXCSUM, csum2, csum1, NULL);
   1634 		csum1 = 0;
   1635 		csum2 = 0;
   1636 
   1637 		if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0)
   1638 			csum1 |= AXE_RXCSUM_IP;
   1639 		if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) != 0)
   1640 			csum1 |= AXE_RXCSUM_TCP;
   1641 		if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) != 0)
   1642 			csum1 |= AXE_RXCSUM_UDP;
   1643 		if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) != 0)
   1644 			csum1 |= AXE_RXCSUM_TCPV6;
   1645 		if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) != 0)
   1646 			csum1 |= AXE_RXCSUM_UDPV6;
   1647 		axe_cmd(sc, AXE_772B_CMD_WRITE_RXCSUM, csum2, csum1, NULL);
   1648 	}
   1649 }
   1650 
   1651 static void
   1652 axe_start(struct ifnet *ifp)
   1653 {
   1654 	struct axe_softc *sc = ifp->if_softc;
   1655 
   1656 	mutex_enter(&sc->axe_txlock);
   1657 	axe_start_locked(ifp);
   1658 	mutex_exit(&sc->axe_txlock);
   1659 }
   1660 
   1661 static void
   1662 axe_start_locked(struct ifnet *ifp)
   1663 {
   1664 	struct axe_softc *sc = ifp->if_softc;
   1665 	struct mbuf *m;
   1666 
   1667 	if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
   1668 		return;
   1669 
   1670 	IFQ_POLL(&ifp->if_snd, m);
   1671 	if (m == NULL) {
   1672 		return;
   1673 	}
   1674 
   1675 	if (axe_encap(sc, m, 0)) {
   1676 		return;
   1677 	}
   1678 	IFQ_DEQUEUE(&ifp->if_snd, m);
   1679 
   1680 	/*
   1681 	 * If there's a BPF listener, bounce a copy of this frame
   1682 	 * to him.
   1683 	 */
   1684 	bpf_mtap(ifp, m);
   1685 	m_freem(m);
   1686 
   1687 	ifp->if_flags |= IFF_OACTIVE;
   1688 
   1689 	/*
   1690 	 * Set a timeout in case the chip goes out to lunch.
   1691 	 */
   1692 	ifp->if_timer = 5;
   1693 
   1694 	return;
   1695 }
   1696 
   1697 static int
   1698 axe_init(struct ifnet *ifp)
   1699 {
   1700 	struct axe_softc *sc = ifp->if_softc;
   1701 
   1702 	mutex_enter(&sc->axe_lock);
   1703 	int ret = axe_init_locked(ifp);
   1704 	mutex_exit(&sc->axe_lock);
   1705 
   1706 	return ret;
   1707 }
   1708 
   1709 static int
   1710 axe_init_locked(struct ifnet *ifp)
   1711 {
   1712 	AXEHIST_FUNC(); AXEHIST_CALLED();
   1713 	struct axe_softc *sc = ifp->if_softc;
   1714 	usbd_status err;
   1715 	int rxmode;
   1716 	int i, error;
   1717 
   1718 	axe_stop_locked(ifp, 0);
   1719 
   1720 	/*
   1721 	 * Cancel pending I/O and free all RX/TX buffers.
   1722 	 */
   1723 	axe_reset(sc);
   1724 
   1725 	axe_lock_mii(sc);
   1726 
   1727 #if 0
   1728 	ret = asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_2 |
   1729 			      AX_GPIO_GPO2EN, 5, in_pm);
   1730 #endif
   1731 	/* Set MAC address and transmitter IPG values. */
   1732 	if (AXE_IS_178_FAMILY(sc)) {
   1733 		axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, sc->axe_enaddr);
   1734 		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2],
   1735 		    (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL);
   1736 	} else {
   1737 		axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, sc->axe_enaddr);
   1738 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
   1739 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
   1740 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
   1741 	}
   1742 	if (AXE_IS_178_FAMILY(sc)) {
   1743 		sc->axe_flags &= ~(AXSTD_FRAME | AXCSUM_FRAME);
   1744 		if ((sc->axe_flags & AX772B) != 0 &&
   1745 		    (ifp->if_capenable & AX_RXCSUM) != 0) {
   1746 			sc->sc_lenmask = AXE_CSUM_HDR_LEN_MASK;
   1747 			sc->axe_flags |= AXCSUM_FRAME;
   1748 		} else {
   1749 			sc->sc_lenmask = AXE_HDR_LEN_MASK;
   1750 			sc->axe_flags |= AXSTD_FRAME;
   1751 		}
   1752 	}
   1753 
   1754 	/* Configure TX/RX checksum offloading. */
   1755 	axe_csum_cfg(sc);
   1756 
   1757 	if (sc->axe_flags & AX772B) {
   1758 		/* AX88772B uses different maximum frame burst configuration. */
   1759 		axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG,
   1760 		    ax88772b_mfb_table[AX88772B_MFB_16K].threshold,
   1761 		    ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL);
   1762 	}
   1763 	/* Enable receiver, set RX mode */
   1764 	rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
   1765 	if (AXE_IS_178_FAMILY(sc)) {
   1766 		if (sc->axe_flags & AX772B) {
   1767 			/*
   1768 			 * Select RX header format type 1.  Aligning IP
   1769 			 * header on 4 byte boundary is not needed when
   1770 			 * checksum offloading feature is not used
   1771 			 * because we always copy the received frame in
   1772 			 * RX handler.  When RX checksum offloading is
   1773 			 * active, aligning IP header is required to
   1774 			 * reflect actual frame length including RX
   1775 			 * header size.
   1776 			 */
   1777 			rxmode |= AXE_772B_RXCMD_HDR_TYPE_1;
   1778 			if (sc->axe_flags & AXCSUM_FRAME)
   1779 				rxmode |= AXE_772B_RXCMD_IPHDR_ALIGN;
   1780 		} else {
   1781 			/*
   1782 			 * Default Rx buffer size is too small to get
   1783 			 * maximum performance.
   1784 			 */
   1785 #if 0
   1786 			if (sc->axe_udev->ud_speed == USB_SPEED_HIGH) {
   1787 				/* Largest possible USB buffer size for AX88178 */
   1788 #endif
   1789 			rxmode |= AXE_178_RXCMD_MFB_16384;
   1790 		}
   1791 	} else {
   1792 		rxmode |= AXE_172_RXCMD_UNICAST;
   1793 	}
   1794 
   1795 
   1796 	/* If we want promiscuous mode, set the allframes bit. */
   1797 	if (ifp->if_flags & IFF_PROMISC)
   1798 		rxmode |= AXE_RXCMD_PROMISC;
   1799 
   1800 	if (ifp->if_flags & IFF_BROADCAST)
   1801 		rxmode |= AXE_RXCMD_BROADCAST;
   1802 
   1803 	DPRINTF("rxmode 0x%#x", rxmode, 0, 0, 0);
   1804 
   1805 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
   1806 	axe_unlock_mii(sc);
   1807 
   1808 	/* Load the multicast filter. */
   1809 	axe_setmulti(sc);
   1810 
   1811 	/* Open RX and TX pipes. */
   1812 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
   1813 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
   1814 	if (err) {
   1815 		aprint_error_dev(sc->axe_dev, "open rx pipe failed: %s\n",
   1816 		    usbd_errstr(err));
   1817 		error = EIO;
   1818 		goto fail;
   1819 	}
   1820 
   1821 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
   1822 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
   1823 	if (err) {
   1824 		aprint_error_dev(sc->axe_dev, "open tx pipe failed: %s\n",
   1825 		    usbd_errstr(err));
   1826 		error = EIO;
   1827 		goto fail1;
   1828 	}
   1829 
   1830 	/* Init RX ring. */
   1831 	if (axe_rx_list_init(sc) != 0) {
   1832 		aprint_error_dev(sc->axe_dev, "rx list init failed\n");
   1833 		error = ENOBUFS;
   1834 		goto fail2;
   1835 	}
   1836 
   1837 	/* Init TX ring. */
   1838 	if (axe_tx_list_init(sc) != 0) {
   1839 		aprint_error_dev(sc->axe_dev, "tx list init failed\n");
   1840 		error = ENOBUFS;
   1841 		goto fail3;
   1842 	}
   1843 
   1844 	/* Start up the receive pipe. */
   1845 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1846 		struct axe_chain *c = &sc->axe_cdata.axe_rx_chain[i];
   1847 		usbd_setup_xfer(c->axe_xfer, c, c->axe_buf, sc->axe_bufsz,
   1848 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
   1849 		usbd_transfer(c->axe_xfer);
   1850 	}
   1851 
   1852 	ifp->if_flags |= IFF_RUNNING;
   1853 	ifp->if_flags &= ~IFF_OACTIVE;
   1854 
   1855 	callout_schedule(&sc->axe_stat_ch, hz);
   1856 
   1857 	return 0;
   1858 fail3:
   1859 	axe_rx_list_free(sc);
   1860 fail2:
   1861 	usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1862 fail1:
   1863 	usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1864 fail:
   1865 	return error;
   1866 }
   1867 
   1868 static int
   1869 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1870 {
   1871 	struct axe_softc *sc = ifp->if_softc;
   1872 	int s;
   1873 	int error = 0;
   1874 
   1875 	s = splnet();
   1876 	error = ether_ioctl(ifp, cmd, data);
   1877 	splx(s);
   1878 
   1879 	if (error == ENETRESET) {
   1880 		error = 0;
   1881 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   1882 			;
   1883 		else if (ifp->if_flags & IFF_RUNNING) {
   1884 			mutex_enter(&sc->axe_lock);
   1885 			axe_setmulti(sc);
   1886 			mutex_exit(&sc->axe_lock);
   1887 		}
   1888 	}
   1889 
   1890 	return error;
   1891 }
   1892 
   1893 static void
   1894 axe_watchdog(struct ifnet *ifp)
   1895 {
   1896 	struct axe_softc *sc;
   1897 	struct axe_chain *c;
   1898 	usbd_status stat;
   1899 	int s;
   1900 
   1901 	sc = ifp->if_softc;
   1902 
   1903 	ifp->if_oerrors++;
   1904 	aprint_error_dev(sc->axe_dev, "watchdog timeout\n");
   1905 
   1906 	s = splusb();
   1907 	c = &sc->axe_cdata.axe_tx_chain[0];
   1908 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
   1909 	axe_txeof(c->axe_xfer, c, stat);
   1910 
   1911 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1912 		axe_start(ifp);
   1913 	splx(s);
   1914 }
   1915 
   1916 /*
   1917  * Stop the adapter and free any mbufs allocated to the
   1918  * RX and TX lists.
   1919  */
   1920 
   1921 static void
   1922 axe_stop(struct ifnet *ifp, int disable)
   1923 {
   1924 	struct axe_softc *sc = ifp->if_softc;
   1925 
   1926 	mutex_enter(&sc->axe_lock);
   1927 	axe_stop_locked(ifp, disable);
   1928 	mutex_exit(&sc->axe_lock);
   1929 }
   1930 
   1931 static void
   1932 axe_stop_locked(struct ifnet *ifp, int disable)
   1933 {
   1934 	struct axe_softc *sc = ifp->if_softc;
   1935 	usbd_status err;
   1936 
   1937 	ifp->if_timer = 0;
   1938 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1939 
   1940 	callout_stop(&sc->axe_stat_ch);
   1941 
   1942 	/* Stop transfers. */
   1943 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1944 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1945 		if (err) {
   1946 			aprint_error_dev(sc->axe_dev,
   1947 			    "abort rx pipe failed: %s\n", usbd_errstr(err));
   1948 		}
   1949 	}
   1950 
   1951 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1952 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1953 		if (err) {
   1954 			aprint_error_dev(sc->axe_dev,
   1955 			    "abort tx pipe failed: %s\n", usbd_errstr(err));
   1956 		}
   1957 	}
   1958 
   1959 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1960 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1961 		if (err) {
   1962 			aprint_error_dev(sc->axe_dev,
   1963 			    "abort intr pipe failed: %s\n", usbd_errstr(err));
   1964 		}
   1965 	}
   1966 
   1967 	axe_reset(sc);
   1968 
   1969 	axe_rx_list_free(sc);
   1970 
   1971 	axe_tx_list_free(sc);
   1972 
   1973 	/* Close pipes. */
   1974 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1975 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1976 		if (err) {
   1977 			aprint_error_dev(sc->axe_dev,
   1978 			    "close rx pipe failed: %s\n", usbd_errstr(err));
   1979 		}
   1980 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
   1981 	}
   1982 
   1983 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1984 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1985 		if (err) {
   1986 			aprint_error_dev(sc->axe_dev,
   1987 			    "close tx pipe failed: %s\n", usbd_errstr(err));
   1988 		}
   1989 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
   1990 	}
   1991 
   1992 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1993 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1994 		if (err) {
   1995 			aprint_error_dev(sc->axe_dev,
   1996 			    "close intr pipe failed: %s\n", usbd_errstr(err));
   1997 		}
   1998 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
   1999 	}
   2000 
   2001 	sc->axe_link = 0;
   2002 }
   2003 
   2004 MODULE(MODULE_CLASS_DRIVER, if_axe, "bpf");
   2005 
   2006 #ifdef _MODULE
   2007 #include "ioconf.c"
   2008 #endif
   2009 
   2010 static int
   2011 if_axe_modcmd(modcmd_t cmd, void *aux)
   2012 {
   2013 	int error = 0;
   2014 
   2015 	switch (cmd) {
   2016 	case MODULE_CMD_INIT:
   2017 #ifdef _MODULE
   2018 		error = config_init_component(cfdriver_ioconf_axe,
   2019 		    cfattach_ioconf_axe, cfdata_ioconf_axe);
   2020 #endif
   2021 		return error;
   2022 	case MODULE_CMD_FINI:
   2023 #ifdef _MODULE
   2024 		error = config_fini_component(cfdriver_ioconf_axe,
   2025 		    cfattach_ioconf_axe, cfdata_ioconf_axe);
   2026 #endif
   2027 		return error;
   2028 	default:
   2029 		return ENOTTY;
   2030 	}
   2031 }
   2032