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