if_axe.c revision 226479
1184610Salfred/*- 2184610Salfred * Copyright (c) 1997, 1998, 1999, 2000-2003 3184610Salfred * Bill Paul <wpaul@windriver.com>. All rights reserved. 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 3. All advertising materials mentioning features or use of this software 14184610Salfred * must display the following acknowledgement: 15184610Salfred * This product includes software developed by Bill Paul. 16184610Salfred * 4. Neither the name of the author nor the names of any co-contributors 17184610Salfred * may be used to endorse or promote products derived from this software 18184610Salfred * without specific prior written permission. 19184610Salfred * 20184610Salfred * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30184610Salfred * THE POSSIBILITY OF SUCH DAMAGE. 31184610Salfred */ 32184610Salfred 33184610Salfred#include <sys/cdefs.h> 34184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb/net/if_axe.c 226479 2011-10-17 19:51:38Z yongari $"); 35184610Salfred 36184610Salfred/* 37188412Sthompsa * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver. 38188412Sthompsa * Used in the LinkSys USB200M and various other adapters. 39184610Salfred * 40184610Salfred * Manuals available from: 41184610Salfred * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF 42184610Salfred * Note: you need the manual for the AX88170 chip (USB 1.x ethernet 43184610Salfred * controller) to find the definitions for the RX control register. 44184610Salfred * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF 45184610Salfred * 46184610Salfred * Written by Bill Paul <wpaul@windriver.com> 47184610Salfred * Senior Engineer 48184610Salfred * Wind River Systems 49184610Salfred */ 50184610Salfred 51184610Salfred/* 52184610Salfred * The AX88172 provides USB ethernet supports at 10 and 100Mbps. 53184610Salfred * It uses an external PHY (reference designs use a RealTek chip), 54184610Salfred * and has a 64-bit multicast hash filter. There is some information 55184610Salfred * missing from the manual which one needs to know in order to make 56184610Salfred * the chip function: 57184610Salfred * 58184610Salfred * - You must set bit 7 in the RX control register, otherwise the 59184610Salfred * chip won't receive any packets. 60184610Salfred * - You must initialize all 3 IPG registers, or you won't be able 61184610Salfred * to send any packets. 62184610Salfred * 63184610Salfred * Note that this device appears to only support loading the station 64184610Salfred * address via autload from the EEPROM (i.e. there's no way to manaully 65184610Salfred * set it). 66184610Salfred * 67184610Salfred * (Adam Weinberger wanted me to name this driver if_gir.c.) 68184610Salfred */ 69184610Salfred 70184610Salfred/* 71184610Salfred * Ax88178 and Ax88772 support backported from the OpenBSD driver. 72184610Salfred * 2007/02/12, J.R. Oldroyd, fbsd@opal.com 73184610Salfred * 74184610Salfred * Manual here: 75184610Salfred * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf 76184610Salfred * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf 77184610Salfred */ 78184610Salfred 79194677Sthompsa#include <sys/stdint.h> 80194677Sthompsa#include <sys/stddef.h> 81194677Sthompsa#include <sys/param.h> 82194677Sthompsa#include <sys/queue.h> 83194677Sthompsa#include <sys/types.h> 84194677Sthompsa#include <sys/systm.h> 85194677Sthompsa#include <sys/kernel.h> 86194677Sthompsa#include <sys/bus.h> 87194677Sthompsa#include <sys/module.h> 88194677Sthompsa#include <sys/lock.h> 89194677Sthompsa#include <sys/mutex.h> 90194677Sthompsa#include <sys/condvar.h> 91194677Sthompsa#include <sys/sysctl.h> 92194677Sthompsa#include <sys/sx.h> 93194677Sthompsa#include <sys/unistd.h> 94194677Sthompsa#include <sys/callout.h> 95194677Sthompsa#include <sys/malloc.h> 96194677Sthompsa#include <sys/priv.h> 97194677Sthompsa 98194677Sthompsa#include <dev/usb/usb.h> 99194677Sthompsa#include <dev/usb/usbdi.h> 100194677Sthompsa#include <dev/usb/usbdi_util.h> 101188746Sthompsa#include "usbdevs.h" 102184610Salfred 103184610Salfred#define USB_DEBUG_VAR axe_debug 104194677Sthompsa#include <dev/usb/usb_debug.h> 105188942Sthompsa#include <dev/usb/usb_process.h> 106184610Salfred 107188942Sthompsa#include <dev/usb/net/usb_ethernet.h> 108188942Sthompsa#include <dev/usb/net/if_axereg.h> 109184610Salfred 110188412Sthompsa/* 111188412Sthompsa * AXE_178_MAX_FRAME_BURST 112188412Sthompsa * max frame burst size for Ax88178 and Ax88772 113188412Sthompsa * 0 2048 bytes 114188412Sthompsa * 1 4096 bytes 115188412Sthompsa * 2 8192 bytes 116188412Sthompsa * 3 16384 bytes 117188412Sthompsa * use the largest your system can handle without USB stalling. 118188412Sthompsa * 119188412Sthompsa * NB: 88772 parts appear to generate lots of input errors with 120188412Sthompsa * a 2K rx buffer and 8K is only slightly faster than 4K on an 121188412Sthompsa * EHCI port on a T42 so change at your own risk. 122188412Sthompsa */ 123188412Sthompsa#define AXE_178_MAX_FRAME_BURST 1 124184610Salfred 125207077Sthompsa#ifdef USB_DEBUG 126184610Salfredstatic int axe_debug = 0; 127184610Salfred 128192502SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe"); 129192502SthompsaSYSCTL_INT(_hw_usb_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0, 130184610Salfred "Debug level"); 131184610Salfred#endif 132184610Salfred 133184610Salfred/* 134184610Salfred * Various supported device vendors/products. 135184610Salfred */ 136223486Shselaskystatic const STRUCT_USB_HOST_ID axe_devs[] = { 137201028Sthompsa#define AXE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) } 138201028Sthompsa AXE_DEV(ABOCOM, UF200, 0), 139201028Sthompsa AXE_DEV(ACERCM, EP1427X2, 0), 140201028Sthompsa AXE_DEV(APPLE, ETHERNET, AXE_FLAG_772), 141201028Sthompsa AXE_DEV(ASIX, AX88172, 0), 142201028Sthompsa AXE_DEV(ASIX, AX88178, AXE_FLAG_178), 143201028Sthompsa AXE_DEV(ASIX, AX88772, AXE_FLAG_772), 144215969Syongari AXE_DEV(ASIX, AX88772A, AXE_FLAG_772A), 145224020Syongari AXE_DEV(ASIX, AX88772B, AXE_FLAG_772B), 146201028Sthompsa AXE_DEV(ATEN, UC210T, 0), 147201028Sthompsa AXE_DEV(BELKIN, F5D5055, AXE_FLAG_178), 148201028Sthompsa AXE_DEV(BILLIONTON, USB2AR, 0), 149215969Syongari AXE_DEV(CISCOLINKSYS, USB200MV2, AXE_FLAG_772A), 150201028Sthompsa AXE_DEV(COREGA, FETHER_USB2_TX, 0), 151201028Sthompsa AXE_DEV(DLINK, DUBE100, 0), 152201028Sthompsa AXE_DEV(DLINK, DUBE100B1, AXE_FLAG_772), 153201028Sthompsa AXE_DEV(GOODWAY, GWUSB2E, 0), 154201028Sthompsa AXE_DEV(IODATA, ETGUS2, AXE_FLAG_178), 155201028Sthompsa AXE_DEV(JVC, MP_PRX1, 0), 156201028Sthompsa AXE_DEV(LINKSYS2, USB200M, 0), 157201028Sthompsa AXE_DEV(LINKSYS4, USB1000, AXE_FLAG_178), 158212980Ssanpei AXE_DEV(LOGITEC, LAN_GTJU2A, AXE_FLAG_178), 159201028Sthompsa AXE_DEV(MELCO, LUAU2KTX, 0), 160212980Ssanpei AXE_DEV(MELCO, LUA3U2AGT, AXE_FLAG_178), 161201028Sthompsa AXE_DEV(NETGEAR, FA120, 0), 162201028Sthompsa AXE_DEV(OQO, ETHER01PLUS, AXE_FLAG_772), 163201028Sthompsa AXE_DEV(PLANEX3, GU1000T, AXE_FLAG_178), 164201028Sthompsa AXE_DEV(SITECOM, LN029, 0), 165201028Sthompsa AXE_DEV(SITECOMEU, LN028, AXE_FLAG_178), 166201028Sthompsa AXE_DEV(SYSTEMTALKS, SGCX2UL, 0), 167201028Sthompsa#undef AXE_DEV 168184610Salfred}; 169184610Salfred 170184610Salfredstatic device_probe_t axe_probe; 171184610Salfredstatic device_attach_t axe_attach; 172184610Salfredstatic device_detach_t axe_detach; 173184610Salfred 174193045Sthompsastatic usb_callback_t axe_bulk_read_callback; 175193045Sthompsastatic usb_callback_t axe_bulk_write_callback; 176184610Salfred 177188412Sthompsastatic miibus_readreg_t axe_miibus_readreg; 178188412Sthompsastatic miibus_writereg_t axe_miibus_writereg; 179188412Sthompsastatic miibus_statchg_t axe_miibus_statchg; 180184610Salfred 181193045Sthompsastatic uether_fn_t axe_attach_post; 182193045Sthompsastatic uether_fn_t axe_init; 183193045Sthompsastatic uether_fn_t axe_stop; 184193045Sthompsastatic uether_fn_t axe_start; 185193045Sthompsastatic uether_fn_t axe_tick; 186193045Sthompsastatic uether_fn_t axe_setmulti; 187193045Sthompsastatic uether_fn_t axe_setpromisc; 188184610Salfred 189188412Sthompsastatic int axe_ifmedia_upd(struct ifnet *); 190188412Sthompsastatic void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *); 191188412Sthompsastatic int axe_cmd(struct axe_softc *, int, int, int, void *); 192188412Sthompsastatic void axe_ax88178_init(struct axe_softc *); 193188412Sthompsastatic void axe_ax88772_init(struct axe_softc *); 194224020Syongaristatic void axe_ax88772_phywake(struct axe_softc *); 195215969Syongaristatic void axe_ax88772a_init(struct axe_softc *); 196224020Syongaristatic void axe_ax88772b_init(struct axe_softc *); 197186730Salfredstatic int axe_get_phyno(struct axe_softc *, int); 198184610Salfred 199192984Sthompsastatic const struct usb_config axe_config[AXE_N_TRANSFER] = { 200184610Salfred 201187259Sthompsa [AXE_BULK_DT_WR] = { 202184610Salfred .type = UE_BULK, 203184610Salfred .endpoint = UE_ADDR_ANY, 204184610Salfred .direction = UE_DIR_OUT, 205216284Syongari .frames = 16, 206216284Syongari .bufsize = 16 * MCLBYTES, 207190734Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 208190734Sthompsa .callback = axe_bulk_write_callback, 209190734Sthompsa .timeout = 10000, /* 10 seconds */ 210184610Salfred }, 211184610Salfred 212187259Sthompsa [AXE_BULK_DT_RD] = { 213184610Salfred .type = UE_BULK, 214184610Salfred .endpoint = UE_ADDR_ANY, 215184610Salfred .direction = UE_DIR_IN, 216197566Sthompsa .bufsize = 16384, /* bytes */ 217190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 218190734Sthompsa .callback = axe_bulk_read_callback, 219190734Sthompsa .timeout = 0, /* no timeout */ 220184610Salfred }, 221184610Salfred}; 222184610Salfred 223224020Syongaristatic const struct ax88772b_mfb ax88772b_mfb_table[] = { 224224020Syongari { 0x8000, 0x8001, 2048 }, 225224020Syongari { 0x8100, 0x8147, 4096}, 226224020Syongari { 0x8200, 0x81EB, 6144}, 227224020Syongari { 0x8300, 0x83D7, 8192}, 228224020Syongari { 0x8400, 0x851E, 16384}, 229224020Syongari { 0x8500, 0x8666, 20480}, 230224020Syongari { 0x8600, 0x87AE, 24576}, 231224020Syongari { 0x8700, 0x8A3D, 32768} 232224020Syongari}; 233224020Syongari 234184610Salfredstatic device_method_t axe_methods[] = { 235184610Salfred /* Device interface */ 236184610Salfred DEVMETHOD(device_probe, axe_probe), 237184610Salfred DEVMETHOD(device_attach, axe_attach), 238184610Salfred DEVMETHOD(device_detach, axe_detach), 239184610Salfred 240184610Salfred /* bus interface */ 241184610Salfred DEVMETHOD(bus_print_child, bus_generic_print_child), 242184610Salfred 243184610Salfred /* MII interface */ 244188412Sthompsa DEVMETHOD(miibus_readreg, axe_miibus_readreg), 245188412Sthompsa DEVMETHOD(miibus_writereg, axe_miibus_writereg), 246188412Sthompsa DEVMETHOD(miibus_statchg, axe_miibus_statchg), 247184610Salfred 248184610Salfred {0, 0} 249184610Salfred}; 250184610Salfred 251184610Salfredstatic driver_t axe_driver = { 252184610Salfred .name = "axe", 253184610Salfred .methods = axe_methods, 254184610Salfred .size = sizeof(struct axe_softc), 255184610Salfred}; 256184610Salfred 257184610Salfredstatic devclass_t axe_devclass; 258184610Salfred 259189275SthompsaDRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, NULL, 0); 260184610SalfredDRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0); 261188942SthompsaMODULE_DEPEND(axe, uether, 1, 1, 1); 262188942SthompsaMODULE_DEPEND(axe, usb, 1, 1, 1); 263188412SthompsaMODULE_DEPEND(axe, ether, 1, 1, 1); 264188412SthompsaMODULE_DEPEND(axe, miibus, 1, 1, 1); 265212122SthompsaMODULE_VERSION(axe, 1); 266184610Salfred 267192984Sthompsastatic const struct usb_ether_methods axe_ue_methods = { 268188412Sthompsa .ue_attach_post = axe_attach_post, 269188412Sthompsa .ue_start = axe_start, 270188412Sthompsa .ue_init = axe_init, 271188412Sthompsa .ue_stop = axe_stop, 272188412Sthompsa .ue_tick = axe_tick, 273188412Sthompsa .ue_setmulti = axe_setmulti, 274188412Sthompsa .ue_setpromisc = axe_setpromisc, 275188412Sthompsa .ue_mii_upd = axe_ifmedia_upd, 276188412Sthompsa .ue_mii_sts = axe_ifmedia_sts, 277188412Sthompsa}; 278188412Sthompsa 279188412Sthompsastatic int 280188412Sthompsaaxe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf) 281184610Salfred{ 282192984Sthompsa struct usb_device_request req; 283193045Sthompsa usb_error_t err; 284184610Salfred 285188412Sthompsa AXE_LOCK_ASSERT(sc, MA_OWNED); 286188412Sthompsa 287184610Salfred req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ? 288184610Salfred UT_WRITE_VENDOR_DEVICE : 289184610Salfred UT_READ_VENDOR_DEVICE); 290184610Salfred req.bRequest = AXE_CMD_CMD(cmd); 291184610Salfred USETW(req.wValue, val); 292184610Salfred USETW(req.wIndex, index); 293188412Sthompsa USETW(req.wLength, AXE_CMD_LEN(cmd)); 294184610Salfred 295194228Sthompsa err = uether_do_request(&sc->sc_ue, &req, buf, 1000); 296184610Salfred 297188412Sthompsa return (err); 298184610Salfred} 299184610Salfred 300184610Salfredstatic int 301188412Sthompsaaxe_miibus_readreg(device_t dev, int phy, int reg) 302184610Salfred{ 303184610Salfred struct axe_softc *sc = device_get_softc(dev); 304184610Salfred uint16_t val; 305188412Sthompsa int locked; 306184610Salfred 307188412Sthompsa if (sc->sc_phyno != phy) 308188412Sthompsa return (0); 309184610Salfred 310188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 311188412Sthompsa if (!locked) 312188412Sthompsa AXE_LOCK(sc); 313186730Salfred 314188412Sthompsa axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL); 315188412Sthompsa axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val); 316188412Sthompsa axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL); 317184610Salfred 318184610Salfred val = le16toh(val); 319215968Syongari if (AXE_IS_772(sc) && reg == MII_BMSR) { 320186730Salfred /* 321186730Salfred * BMSR of AX88772 indicates that it supports extended 322186730Salfred * capability but the extended status register is 323186730Salfred * revered for embedded ethernet PHY. So clear the 324186730Salfred * extended capability bit of BMSR. 325186730Salfred */ 326186730Salfred val &= ~BMSR_EXTCAP; 327186730Salfred } 328184610Salfred 329188412Sthompsa if (!locked) 330188412Sthompsa AXE_UNLOCK(sc); 331184610Salfred return (val); 332184610Salfred} 333184610Salfred 334184610Salfredstatic int 335188412Sthompsaaxe_miibus_writereg(device_t dev, int phy, int reg, int val) 336184610Salfred{ 337184610Salfred struct axe_softc *sc = device_get_softc(dev); 338188412Sthompsa int locked; 339184610Salfred 340189522Sthompsa val = htole32(val); 341184610Salfred 342186730Salfred if (sc->sc_phyno != phy) 343188412Sthompsa return (0); 344186730Salfred 345188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 346188412Sthompsa if (!locked) 347188412Sthompsa AXE_LOCK(sc); 348184610Salfred 349188412Sthompsa axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL); 350188412Sthompsa axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val); 351188412Sthompsa axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL); 352188412Sthompsa 353188412Sthompsa if (!locked) 354188412Sthompsa AXE_UNLOCK(sc); 355184610Salfred return (0); 356184610Salfred} 357184610Salfred 358184610Salfredstatic void 359188412Sthompsaaxe_miibus_statchg(device_t dev) 360184610Salfred{ 361184610Salfred struct axe_softc *sc = device_get_softc(dev); 362184610Salfred struct mii_data *mii = GET_MII(sc); 363188553Sthompsa struct ifnet *ifp; 364184610Salfred uint16_t val; 365188412Sthompsa int err, locked; 366184610Salfred 367188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 368188412Sthompsa if (!locked) 369188412Sthompsa AXE_LOCK(sc); 370184610Salfred 371194228Sthompsa ifp = uether_getifp(&sc->sc_ue); 372188553Sthompsa if (mii == NULL || ifp == NULL || 373188553Sthompsa (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 374188553Sthompsa goto done; 375188553Sthompsa 376188553Sthompsa sc->sc_flags &= ~AXE_FLAG_LINK; 377188553Sthompsa if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 378188553Sthompsa (IFM_ACTIVE | IFM_AVALID)) { 379188553Sthompsa switch (IFM_SUBTYPE(mii->mii_media_active)) { 380188553Sthompsa case IFM_10_T: 381188553Sthompsa case IFM_100_TX: 382188553Sthompsa sc->sc_flags |= AXE_FLAG_LINK; 383188553Sthompsa break; 384188553Sthompsa case IFM_1000_T: 385188553Sthompsa if ((sc->sc_flags & AXE_FLAG_178) == 0) 386188553Sthompsa break; 387188553Sthompsa sc->sc_flags |= AXE_FLAG_LINK; 388188553Sthompsa break; 389188553Sthompsa default: 390188553Sthompsa break; 391188553Sthompsa } 392188553Sthompsa } 393188553Sthompsa 394188553Sthompsa /* Lost link, do nothing. */ 395188553Sthompsa if ((sc->sc_flags & AXE_FLAG_LINK) == 0) 396188553Sthompsa goto done; 397188553Sthompsa 398188553Sthompsa val = 0; 399188553Sthompsa if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) 400188553Sthompsa val |= AXE_MEDIA_FULL_DUPLEX; 401215968Syongari if (AXE_IS_178_FAMILY(sc)) { 402186730Salfred val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC; 403188553Sthompsa if ((sc->sc_flags & AXE_FLAG_178) != 0) 404188553Sthompsa val |= AXE_178_MEDIA_ENCK; 405184610Salfred switch (IFM_SUBTYPE(mii->mii_media_active)) { 406184610Salfred case IFM_1000_T: 407184610Salfred val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK; 408184610Salfred break; 409184610Salfred case IFM_100_TX: 410184610Salfred val |= AXE_178_MEDIA_100TX; 411184610Salfred break; 412184610Salfred case IFM_10_T: 413184610Salfred /* doesn't need to be handled */ 414184610Salfred break; 415184610Salfred } 416184610Salfred } 417188412Sthompsa err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL); 418188412Sthompsa if (err) 419188412Sthompsa device_printf(dev, "media change failed, error %d\n", err); 420188553Sthompsadone: 421188412Sthompsa if (!locked) 422188412Sthompsa AXE_UNLOCK(sc); 423184610Salfred} 424184610Salfred 425184610Salfred/* 426184610Salfred * Set media options. 427184610Salfred */ 428184610Salfredstatic int 429188412Sthompsaaxe_ifmedia_upd(struct ifnet *ifp) 430184610Salfred{ 431184610Salfred struct axe_softc *sc = ifp->if_softc; 432184610Salfred struct mii_data *mii = GET_MII(sc); 433221407Smarius struct mii_softc *miisc; 434188553Sthompsa int error; 435184610Salfred 436188412Sthompsa AXE_LOCK_ASSERT(sc, MA_OWNED); 437184610Salfred 438221407Smarius LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 439221407Smarius PHY_RESET(miisc); 440188553Sthompsa error = mii_mediachg(mii); 441188553Sthompsa return (error); 442184610Salfred} 443184610Salfred 444184610Salfred/* 445184610Salfred * Report current media status. 446184610Salfred */ 447184610Salfredstatic void 448188412Sthompsaaxe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 449184610Salfred{ 450184610Salfred struct axe_softc *sc = ifp->if_softc; 451188412Sthompsa struct mii_data *mii = GET_MII(sc); 452184610Salfred 453188412Sthompsa AXE_LOCK(sc); 454188412Sthompsa mii_pollstat(mii); 455188412Sthompsa ifmr->ifm_active = mii->mii_media_active; 456188412Sthompsa ifmr->ifm_status = mii->mii_media_status; 457226479Syongari AXE_UNLOCK(sc); 458184610Salfred} 459184610Salfred 460184610Salfredstatic void 461192984Sthompsaaxe_setmulti(struct usb_ether *ue) 462184610Salfred{ 463194228Sthompsa struct axe_softc *sc = uether_getsc(ue); 464194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 465188412Sthompsa struct ifmultiaddr *ifma; 466188412Sthompsa uint32_t h = 0; 467184610Salfred uint16_t rxmode; 468188412Sthompsa uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 469184610Salfred 470188412Sthompsa AXE_LOCK_ASSERT(sc, MA_OWNED); 471184610Salfred 472188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode); 473184610Salfred rxmode = le16toh(rxmode); 474184610Salfred 475188412Sthompsa if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { 476184610Salfred rxmode |= AXE_RXCMD_ALLMULTI; 477188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); 478184610Salfred return; 479184610Salfred } 480184610Salfred rxmode &= ~AXE_RXCMD_ALLMULTI; 481184610Salfred 482195049Srwatson if_maddr_rlock(ifp); 483188412Sthompsa TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 484188412Sthompsa { 485188412Sthompsa if (ifma->ifma_addr->sa_family != AF_LINK) 486188412Sthompsa continue; 487188412Sthompsa h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 488188412Sthompsa ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 489188412Sthompsa hashtbl[h / 8] |= 1 << (h % 8); 490188412Sthompsa } 491195049Srwatson if_maddr_runlock(ifp); 492184610Salfred 493188412Sthompsa axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl); 494188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); 495184610Salfred} 496184610Salfred 497186730Salfredstatic int 498186730Salfredaxe_get_phyno(struct axe_softc *sc, int sel) 499186730Salfred{ 500188412Sthompsa int phyno; 501186730Salfred 502186730Salfred switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) { 503186730Salfred case PHY_TYPE_100_HOME: 504186730Salfred case PHY_TYPE_GIG: 505188412Sthompsa phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]); 506186730Salfred break; 507186730Salfred case PHY_TYPE_SPECIAL: 508186730Salfred /* FALLTHROUGH */ 509186730Salfred case PHY_TYPE_RSVD: 510186730Salfred /* FALLTHROUGH */ 511186730Salfred case PHY_TYPE_NON_SUP: 512186730Salfred /* FALLTHROUGH */ 513186730Salfred default: 514186730Salfred phyno = -1; 515186730Salfred break; 516186730Salfred } 517186730Salfred 518186730Salfred return (phyno); 519186730Salfred} 520186730Salfred 521212130Sthompsa#define AXE_GPIO_WRITE(x, y) do { \ 522212130Sthompsa axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL); \ 523212130Sthompsa uether_pause(ue, (y)); \ 524212130Sthompsa} while (0) 525212130Sthompsa 526184610Salfredstatic void 527188412Sthompsaaxe_ax88178_init(struct axe_softc *sc) 528184610Salfred{ 529212130Sthompsa struct usb_ether *ue; 530222581Syongari int gpio0, ledmode, phymode; 531212130Sthompsa uint16_t eeprom, val; 532184610Salfred 533212130Sthompsa ue = &sc->sc_ue; 534188412Sthompsa axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL); 535184610Salfred /* XXX magic */ 536188412Sthompsa axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom); 537184610Salfred eeprom = le16toh(eeprom); 538188412Sthompsa axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL); 539184610Salfred 540184610Salfred /* if EEPROM is invalid we have to use to GPIO0 */ 541184610Salfred if (eeprom == 0xffff) { 542212130Sthompsa phymode = AXE_PHY_MODE_MARVELL; 543184610Salfred gpio0 = 1; 544222581Syongari ledmode = 0; 545184610Salfred } else { 546212130Sthompsa phymode = eeprom & 0x7f; 547184610Salfred gpio0 = (eeprom & 0x80) ? 0 : 1; 548222581Syongari ledmode = eeprom >> 8; 549184610Salfred } 550184610Salfred 551212130Sthompsa if (bootverbose) 552215960Syongari device_printf(sc->sc_ue.ue_dev, 553215960Syongari "EEPROM data : 0x%04x, phymode : 0x%02x\n", eeprom, 554215960Syongari phymode); 555212130Sthompsa /* Program GPIOs depending on PHY hardware. */ 556212130Sthompsa switch (phymode) { 557212130Sthompsa case AXE_PHY_MODE_MARVELL: 558212130Sthompsa if (gpio0 == 1) { 559212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN, 560212130Sthompsa hz / 32); 561212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN, 562212130Sthompsa hz / 32); 563212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4); 564212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN, 565212130Sthompsa hz / 32); 566222581Syongari } else { 567212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 | 568222581Syongari AXE_GPIO1_EN, hz / 3); 569222581Syongari if (ledmode == 1) { 570222581Syongari AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3); 571222581Syongari AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN, 572222581Syongari hz / 3); 573222581Syongari } else { 574222581Syongari AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | 575222581Syongari AXE_GPIO2 | AXE_GPIO2_EN, hz / 32); 576222581Syongari AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | 577222581Syongari AXE_GPIO2_EN, hz / 4); 578222581Syongari AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | 579222581Syongari AXE_GPIO2 | AXE_GPIO2_EN, hz / 32); 580222581Syongari } 581222581Syongari } 582212130Sthompsa break; 583212130Sthompsa case AXE_PHY_MODE_CICADA: 584215960Syongari case AXE_PHY_MODE_CICADA_V2: 585215960Syongari case AXE_PHY_MODE_CICADA_V2_ASIX: 586212130Sthompsa if (gpio0 == 1) 587212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 | 588212130Sthompsa AXE_GPIO0_EN, hz / 32); 589212130Sthompsa else 590212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 | 591212130Sthompsa AXE_GPIO1_EN, hz / 32); 592212130Sthompsa break; 593212130Sthompsa case AXE_PHY_MODE_AGERE: 594212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 | 595212130Sthompsa AXE_GPIO1_EN, hz / 32); 596212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 | 597212130Sthompsa AXE_GPIO2_EN, hz / 32); 598212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4); 599212130Sthompsa AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 | 600212130Sthompsa AXE_GPIO2_EN, hz / 32); 601212130Sthompsa break; 602212130Sthompsa case AXE_PHY_MODE_REALTEK_8211CL: 603212130Sthompsa case AXE_PHY_MODE_REALTEK_8211BN: 604212130Sthompsa case AXE_PHY_MODE_REALTEK_8251CL: 605212130Sthompsa val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN : 606212130Sthompsa AXE_GPIO1 | AXE_GPIO1_EN; 607212130Sthompsa AXE_GPIO_WRITE(val, hz / 32); 608212130Sthompsa AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32); 609212130Sthompsa AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4); 610212130Sthompsa AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32); 611212130Sthompsa if (phymode == AXE_PHY_MODE_REALTEK_8211CL) { 612212130Sthompsa axe_miibus_writereg(ue->ue_dev, sc->sc_phyno, 613212130Sthompsa 0x1F, 0x0005); 614212130Sthompsa axe_miibus_writereg(ue->ue_dev, sc->sc_phyno, 615212130Sthompsa 0x0C, 0x0000); 616212130Sthompsa val = axe_miibus_readreg(ue->ue_dev, sc->sc_phyno, 617212130Sthompsa 0x0001); 618212130Sthompsa axe_miibus_writereg(ue->ue_dev, sc->sc_phyno, 619212130Sthompsa 0x01, val | 0x0080); 620212130Sthompsa axe_miibus_writereg(ue->ue_dev, sc->sc_phyno, 621212130Sthompsa 0x1F, 0x0000); 622212130Sthompsa } 623212130Sthompsa break; 624212130Sthompsa default: 625212130Sthompsa /* Unknown PHY model or no need to program GPIOs. */ 626212130Sthompsa break; 627184610Salfred } 628184610Salfred 629184610Salfred /* soft reset */ 630188412Sthompsa axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL); 631212130Sthompsa uether_pause(ue, hz / 4); 632184610Salfred 633188412Sthompsa axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 634184610Salfred AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL); 635212130Sthompsa uether_pause(ue, hz / 4); 636186730Salfred /* Enable MII/GMII/RGMII interface to work with external PHY. */ 637188412Sthompsa axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL); 638212130Sthompsa uether_pause(ue, hz / 4); 639184610Salfred 640188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); 641184610Salfred} 642184610Salfred 643184610Salfredstatic void 644188412Sthompsaaxe_ax88772_init(struct axe_softc *sc) 645184610Salfred{ 646188412Sthompsa axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL); 647194228Sthompsa uether_pause(&sc->sc_ue, hz / 16); 648184610Salfred 649186730Salfred if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) { 650184610Salfred /* ask for the embedded PHY */ 651188412Sthompsa axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL); 652194228Sthompsa uether_pause(&sc->sc_ue, hz / 64); 653184610Salfred 654184610Salfred /* power down and reset state, pin reset state */ 655188412Sthompsa axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 656184610Salfred AXE_SW_RESET_CLEAR, NULL); 657194228Sthompsa uether_pause(&sc->sc_ue, hz / 16); 658184610Salfred 659184610Salfred /* power down/reset state, pin operating state */ 660188412Sthompsa axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 661184610Salfred AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL); 662194228Sthompsa uether_pause(&sc->sc_ue, hz / 4); 663184610Salfred 664184610Salfred /* power up, reset */ 665188412Sthompsa axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL); 666184610Salfred 667184610Salfred /* power up, operating */ 668188412Sthompsa axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 669184610Salfred AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL); 670184610Salfred } else { 671184610Salfred /* ask for external PHY */ 672188412Sthompsa axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL); 673194228Sthompsa uether_pause(&sc->sc_ue, hz / 64); 674184610Salfred 675184610Salfred /* power down internal PHY */ 676188412Sthompsa axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 677184610Salfred AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL); 678184610Salfred } 679184610Salfred 680194228Sthompsa uether_pause(&sc->sc_ue, hz / 4); 681188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); 682184610Salfred} 683184610Salfred 684184610Salfredstatic void 685224020Syongariaxe_ax88772_phywake(struct axe_softc *sc) 686215969Syongari{ 687215969Syongari struct usb_ether *ue; 688215969Syongari 689215969Syongari ue = &sc->sc_ue; 690215969Syongari if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) { 691215969Syongari /* Manually select internal(embedded) PHY - MAC mode. */ 692215969Syongari axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB | 693215969Syongari AXE_SW_PHY_SELECT_EMBEDDED | AXE_SW_PHY_SELECT_SS_MII, 694215969Syongari NULL); 695215969Syongari uether_pause(&sc->sc_ue, hz / 32); 696215969Syongari } else { 697215969Syongari /* 698215969Syongari * Manually select external PHY - MAC mode. 699215969Syongari * Reverse MII/RMII is for AX88772A PHY mode. 700215969Syongari */ 701215969Syongari axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB | 702215969Syongari AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL); 703215969Syongari uether_pause(&sc->sc_ue, hz / 32); 704215969Syongari } 705215969Syongari /* Take PHY out of power down. */ 706215969Syongari axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD | 707215969Syongari AXE_SW_RESET_IPRL, NULL); 708215969Syongari uether_pause(&sc->sc_ue, hz / 4); 709215969Syongari axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL); 710215969Syongari uether_pause(&sc->sc_ue, hz); 711215969Syongari axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL); 712215969Syongari uether_pause(&sc->sc_ue, hz / 32); 713215969Syongari axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL); 714215969Syongari uether_pause(&sc->sc_ue, hz / 32); 715224020Syongari} 716224020Syongari 717224020Syongaristatic void 718224020Syongariaxe_ax88772a_init(struct axe_softc *sc) 719224020Syongari{ 720224020Syongari struct usb_ether *ue; 721224020Syongari 722224020Syongari ue = &sc->sc_ue; 723224020Syongari /* Reload EEPROM. */ 724224020Syongari AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32); 725224020Syongari axe_ax88772_phywake(sc); 726224020Syongari /* Stop MAC. */ 727215969Syongari axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); 728215969Syongari} 729215969Syongari 730224020Syongaristatic void 731224020Syongariaxe_ax88772b_init(struct axe_softc *sc) 732224020Syongari{ 733224020Syongari struct usb_ether *ue; 734224020Syongari uint16_t eeprom; 735224020Syongari uint8_t *eaddr; 736224020Syongari int i; 737224020Syongari 738224020Syongari ue = &sc->sc_ue; 739224020Syongari /* Reload EEPROM. */ 740224020Syongari AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32); 741224020Syongari /* 742224020Syongari * Save PHY power saving configuration(high byte) and 743224020Syongari * clear EEPROM checksum value(low byte). 744224020Syongari */ 745224020Syongari axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG, &eeprom); 746224020Syongari sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00; 747224020Syongari 748224020Syongari /* 749224020Syongari * Auto-loaded default station address from internal ROM is 750224020Syongari * 00:00:00:00:00:00 such that an explicit access to EEPROM 751224020Syongari * is required to get real station address. 752224020Syongari */ 753224020Syongari eaddr = ue->ue_eaddr; 754224020Syongari for (i = 0; i < ETHER_ADDR_LEN / 2; i++) { 755224020Syongari axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_NODE_ID + i, 756224020Syongari &eeprom); 757224020Syongari eeprom = le16toh(eeprom); 758224020Syongari *eaddr++ = (uint8_t)(eeprom & 0xFF); 759224020Syongari *eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF); 760224020Syongari } 761224020Syongari /* Wakeup PHY. */ 762224020Syongari axe_ax88772_phywake(sc); 763224020Syongari /* Stop MAC. */ 764224020Syongari axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); 765224020Syongari} 766224020Syongari 767215969Syongari#undef AXE_GPIO_WRITE 768215969Syongari 769215969Syongaristatic void 770188412Sthompsaaxe_reset(struct axe_softc *sc) 771184610Salfred{ 772192984Sthompsa struct usb_config_descriptor *cd; 773193045Sthompsa usb_error_t err; 774184610Salfred 775194228Sthompsa cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev); 776184610Salfred 777194228Sthompsa err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx, 778188412Sthompsa cd->bConfigurationValue); 779188412Sthompsa if (err) 780188412Sthompsa DPRINTF("reset failed (ignored)\n"); 781188412Sthompsa 782188412Sthompsa /* Wait a little while for the chip to get its brains in order. */ 783194228Sthompsa uether_pause(&sc->sc_ue, hz / 100); 784215966Syongari 785215966Syongari /* Reinitialize controller to achieve full reset. */ 786215966Syongari if (sc->sc_flags & AXE_FLAG_178) 787215966Syongari axe_ax88178_init(sc); 788215966Syongari else if (sc->sc_flags & AXE_FLAG_772) 789215966Syongari axe_ax88772_init(sc); 790215969Syongari else if (sc->sc_flags & AXE_FLAG_772A) 791215969Syongari axe_ax88772a_init(sc); 792224020Syongari else if (sc->sc_flags & AXE_FLAG_772B) 793224020Syongari axe_ax88772b_init(sc); 794188412Sthompsa} 795188412Sthompsa 796188412Sthompsastatic void 797192984Sthompsaaxe_attach_post(struct usb_ether *ue) 798188412Sthompsa{ 799194228Sthompsa struct axe_softc *sc = uether_getsc(ue); 800188412Sthompsa 801184610Salfred /* 802184610Salfred * Load PHY indexes first. Needed by axe_xxx_init(). 803184610Salfred */ 804188412Sthompsa axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs); 805212130Sthompsa if (bootverbose) 806212130Sthompsa device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n", 807212130Sthompsa sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]); 808186730Salfred sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI); 809186730Salfred if (sc->sc_phyno == -1) 810186730Salfred sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC); 811186730Salfred if (sc->sc_phyno == -1) { 812188412Sthompsa device_printf(sc->sc_ue.ue_dev, 813188412Sthompsa "no valid PHY address found, assuming PHY address 0\n"); 814186730Salfred sc->sc_phyno = 0; 815186730Salfred } 816184610Salfred 817224020Syongari /* Initialize controller and get station address. */ 818215968Syongari if (sc->sc_flags & AXE_FLAG_178) { 819188412Sthompsa axe_ax88178_init(sc); 820215968Syongari sc->sc_tx_bufsz = 16 * 1024; 821224020Syongari axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr); 822215968Syongari } else if (sc->sc_flags & AXE_FLAG_772) { 823188412Sthompsa axe_ax88772_init(sc); 824215968Syongari sc->sc_tx_bufsz = 8 * 1024; 825224020Syongari axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr); 826215969Syongari } else if (sc->sc_flags & AXE_FLAG_772A) { 827215969Syongari axe_ax88772a_init(sc); 828215969Syongari sc->sc_tx_bufsz = 8 * 1024; 829188412Sthompsa axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr); 830224020Syongari } else if (sc->sc_flags & AXE_FLAG_772B) { 831224020Syongari axe_ax88772b_init(sc); 832224020Syongari sc->sc_tx_bufsz = 8 * 1024; 833224020Syongari } else 834188412Sthompsa axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr); 835184610Salfred 836184610Salfred /* 837184610Salfred * Fetch IPG values. 838184610Salfred */ 839224020Syongari if (sc->sc_flags & (AXE_FLAG_772A | AXE_FLAG_772B)) { 840215969Syongari /* Set IPG values. */ 841215969Syongari sc->sc_ipgs[0] = 0x15; 842215969Syongari sc->sc_ipgs[1] = 0x16; 843215969Syongari sc->sc_ipgs[2] = 0x1A; 844215969Syongari } else 845215969Syongari axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs); 846188412Sthompsa} 847184610Salfred 848188412Sthompsa/* 849188412Sthompsa * Probe for a AX88172 chip. 850188412Sthompsa */ 851188412Sthompsastatic int 852188412Sthompsaaxe_probe(device_t dev) 853188412Sthompsa{ 854192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 855184610Salfred 856192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 857188412Sthompsa return (ENXIO); 858188412Sthompsa if (uaa->info.bConfigIndex != AXE_CONFIG_IDX) 859188412Sthompsa return (ENXIO); 860188412Sthompsa if (uaa->info.bIfaceIndex != AXE_IFACE_IDX) 861188412Sthompsa return (ENXIO); 862184610Salfred 863194228Sthompsa return (usbd_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa)); 864188412Sthompsa} 865184610Salfred 866188412Sthompsa/* 867188412Sthompsa * Attach the interface. Allocate softc structures, do ifmedia 868188412Sthompsa * setup and ethernet/BPF attach. 869188412Sthompsa */ 870188412Sthompsastatic int 871188412Sthompsaaxe_attach(device_t dev) 872188412Sthompsa{ 873192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 874188412Sthompsa struct axe_softc *sc = device_get_softc(dev); 875192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 876188412Sthompsa uint8_t iface_index; 877188412Sthompsa int error; 878184610Salfred 879188412Sthompsa sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 880184610Salfred 881194228Sthompsa device_set_usb_desc(dev); 882184610Salfred 883188412Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 884184610Salfred 885188412Sthompsa iface_index = AXE_IFACE_IDX; 886194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 887188412Sthompsa axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx); 888188412Sthompsa if (error) { 889199816Sthompsa device_printf(dev, "allocating USB transfers failed\n"); 890188412Sthompsa goto detach; 891188412Sthompsa } 892184610Salfred 893188412Sthompsa ue->ue_sc = sc; 894188412Sthompsa ue->ue_dev = dev; 895188412Sthompsa ue->ue_udev = uaa->device; 896188412Sthompsa ue->ue_mtx = &sc->sc_mtx; 897188412Sthompsa ue->ue_methods = &axe_ue_methods; 898184610Salfred 899194228Sthompsa error = uether_ifattach(ue); 900184610Salfred if (error) { 901188412Sthompsa device_printf(dev, "could not attach interface\n"); 902188412Sthompsa goto detach; 903184610Salfred } 904188412Sthompsa return (0); /* success */ 905184610Salfred 906188412Sthompsadetach: 907188412Sthompsa axe_detach(dev); 908188412Sthompsa return (ENXIO); /* failure */ 909184610Salfred} 910184610Salfred 911184610Salfredstatic int 912184610Salfredaxe_detach(device_t dev) 913184610Salfred{ 914184610Salfred struct axe_softc *sc = device_get_softc(dev); 915192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 916184610Salfred 917194228Sthompsa usbd_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER); 918194228Sthompsa uether_ifdetach(ue); 919184610Salfred mtx_destroy(&sc->sc_mtx); 920184610Salfred 921184610Salfred return (0); 922184610Salfred} 923184610Salfred 924184610Salfred#if (AXE_BULK_BUF_SIZE >= 0x10000) 925184610Salfred#error "Please update axe_bulk_read_callback()!" 926184610Salfred#endif 927184610Salfred 928184610Salfredstatic void 929194677Sthompsaaxe_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 930184610Salfred{ 931194677Sthompsa struct axe_softc *sc = usbd_xfer_softc(xfer); 932192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 933194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 934184610Salfred struct axe_sframe_hdr hdr; 935194677Sthompsa struct usb_page_cache *pc; 936197566Sthompsa int err, pos, len; 937194677Sthompsa int actlen; 938184610Salfred 939194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 940194677Sthompsa 941184610Salfred switch (USB_GET_STATE(xfer)) { 942184610Salfred case USB_ST_TRANSFERRED: 943184610Salfred pos = 0; 944197566Sthompsa len = 0; 945197566Sthompsa err = 0; 946197566Sthompsa 947194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 948215968Syongari if (AXE_IS_178_FAMILY(sc)) { 949197566Sthompsa while (pos < actlen) { 950197566Sthompsa if ((pos + sizeof(hdr)) > actlen) { 951184610Salfred /* too little data */ 952197566Sthompsa err = EINVAL; 953184610Salfred break; 954184610Salfred } 955194677Sthompsa usbd_copy_out(pc, pos, &hdr, sizeof(hdr)); 956184610Salfred 957184610Salfred if ((hdr.len ^ hdr.ilen) != 0xFFFF) { 958184610Salfred /* we lost sync */ 959197566Sthompsa err = EINVAL; 960184610Salfred break; 961184610Salfred } 962184610Salfred pos += sizeof(hdr); 963184610Salfred 964184610Salfred len = le16toh(hdr.len); 965197566Sthompsa if ((pos + len) > actlen) { 966184610Salfred /* invalid length */ 967197566Sthompsa err = EINVAL; 968184610Salfred break; 969184610Salfred } 970213436Syongari uether_rxbuf(ue, pc, pos, len); 971184610Salfred 972197566Sthompsa pos += len + (len % 2); 973184610Salfred } 974213436Syongari } else 975213436Syongari uether_rxbuf(ue, pc, 0, actlen); 976184610Salfred 977197566Sthompsa if (err != 0) 978197566Sthompsa ifp->if_ierrors++; 979184610Salfred 980188412Sthompsa /* FALLTHROUGH */ 981184610Salfred case USB_ST_SETUP: 982184610Salfredtr_setup: 983194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 984194228Sthompsa usbd_transfer_submit(xfer); 985194228Sthompsa uether_rxflush(ue); 986184610Salfred return; 987184610Salfred 988184610Salfred default: /* Error */ 989194677Sthompsa DPRINTF("bulk read error, %s\n", usbd_errstr(error)); 990188412Sthompsa 991194677Sthompsa if (error != USB_ERR_CANCELLED) { 992184610Salfred /* try to clear stall first */ 993194677Sthompsa usbd_xfer_set_stall(xfer); 994188412Sthompsa goto tr_setup; 995184610Salfred } 996184610Salfred return; 997184610Salfred 998184610Salfred } 999184610Salfred} 1000184610Salfred 1001184610Salfred#if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4))) 1002184610Salfred#error "Please update axe_bulk_write_callback()!" 1003184610Salfred#endif 1004184610Salfred 1005184610Salfredstatic void 1006194677Sthompsaaxe_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 1007184610Salfred{ 1008194677Sthompsa struct axe_softc *sc = usbd_xfer_softc(xfer); 1009184610Salfred struct axe_sframe_hdr hdr; 1010194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 1011194677Sthompsa struct usb_page_cache *pc; 1012184610Salfred struct mbuf *m; 1013216284Syongari int nframes, pos; 1014184610Salfred 1015184610Salfred switch (USB_GET_STATE(xfer)) { 1016184610Salfred case USB_ST_TRANSFERRED: 1017184610Salfred DPRINTFN(11, "transfer complete\n"); 1018213424Syongari ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1019188412Sthompsa /* FALLTHROUGH */ 1020184610Salfred case USB_ST_SETUP: 1021188412Sthompsatr_setup: 1022213424Syongari if ((sc->sc_flags & AXE_FLAG_LINK) == 0 || 1023213424Syongari (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) { 1024184610Salfred /* 1025213424Syongari * Don't send anything if there is no link or 1026213424Syongari * controller is busy. 1027184610Salfred */ 1028188412Sthompsa return; 1029184610Salfred } 1030184610Salfred 1031216284Syongari for (nframes = 0; nframes < 16 && 1032216284Syongari !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) { 1033184610Salfred IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1034216284Syongari if (m == NULL) 1035216284Syongari break; 1036216284Syongari usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES, 1037216284Syongari nframes); 1038216284Syongari pos = 0; 1039216284Syongari pc = usbd_xfer_get_frame(xfer, nframes); 1040215968Syongari if (AXE_IS_178_FAMILY(sc)) { 1041184610Salfred hdr.len = htole16(m->m_pkthdr.len); 1042184610Salfred hdr.ilen = ~hdr.len; 1043194677Sthompsa usbd_copy_in(pc, pos, &hdr, sizeof(hdr)); 1044184610Salfred pos += sizeof(hdr); 1045216284Syongari usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len); 1046216284Syongari pos += m->m_pkthdr.len; 1047216284Syongari if ((pos % 512) == 0) { 1048216284Syongari hdr.len = 0; 1049216284Syongari hdr.ilen = 0xffff; 1050216284Syongari usbd_copy_in(pc, pos, &hdr, 1051216284Syongari sizeof(hdr)); 1052216284Syongari pos += sizeof(hdr); 1053216284Syongari } 1054216284Syongari } else { 1055216284Syongari usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len); 1056216284Syongari pos += m->m_pkthdr.len; 1057184610Salfred } 1058184610Salfred 1059184610Salfred /* 1060213423Syongari * XXX 1061213423Syongari * Update TX packet counter here. This is not 1062213423Syongari * correct way but it seems that there is no way 1063213423Syongari * to know how many packets are sent at the end 1064213423Syongari * of transfer because controller combines 1065213423Syongari * multiple writes into single one if there is 1066213423Syongari * room in TX buffer of controller. 1067213423Syongari */ 1068213423Syongari ifp->if_opackets++; 1069213423Syongari 1070213423Syongari /* 1071188412Sthompsa * if there's a BPF listener, bounce a copy 1072188412Sthompsa * of this frame to him: 1073188412Sthompsa */ 1074184610Salfred BPF_MTAP(ifp, m); 1075184610Salfred 1076184610Salfred m_freem(m); 1077184610Salfred 1078216284Syongari /* Set frame length. */ 1079216284Syongari usbd_xfer_set_frame_len(xfer, nframes, pos); 1080184610Salfred } 1081216284Syongari if (nframes != 0) { 1082216284Syongari usbd_xfer_set_frames(xfer, nframes); 1083216284Syongari usbd_transfer_submit(xfer); 1084216284Syongari ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1085216284Syongari } 1086184610Salfred return; 1087216284Syongari /* NOTREACHED */ 1088184610Salfred default: /* Error */ 1089184610Salfred DPRINTFN(11, "transfer error, %s\n", 1090194677Sthompsa usbd_errstr(error)); 1091184610Salfred 1092188412Sthompsa ifp->if_oerrors++; 1093213424Syongari ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1094188412Sthompsa 1095194677Sthompsa if (error != USB_ERR_CANCELLED) { 1096184610Salfred /* try to clear stall first */ 1097194677Sthompsa usbd_xfer_set_stall(xfer); 1098188412Sthompsa goto tr_setup; 1099184610Salfred } 1100184610Salfred return; 1101184610Salfred 1102184610Salfred } 1103184610Salfred} 1104184610Salfred 1105184610Salfredstatic void 1106192984Sthompsaaxe_tick(struct usb_ether *ue) 1107184610Salfred{ 1108194228Sthompsa struct axe_softc *sc = uether_getsc(ue); 1109184610Salfred struct mii_data *mii = GET_MII(sc); 1110184610Salfred 1111188412Sthompsa AXE_LOCK_ASSERT(sc, MA_OWNED); 1112188412Sthompsa 1113184610Salfred mii_tick(mii); 1114188553Sthompsa if ((sc->sc_flags & AXE_FLAG_LINK) == 0) { 1115188553Sthompsa axe_miibus_statchg(ue->ue_dev); 1116188553Sthompsa if ((sc->sc_flags & AXE_FLAG_LINK) != 0) 1117188553Sthompsa axe_start(ue); 1118186730Salfred } 1119184610Salfred} 1120184610Salfred 1121184610Salfredstatic void 1122192984Sthompsaaxe_start(struct usb_ether *ue) 1123184610Salfred{ 1124194228Sthompsa struct axe_softc *sc = uether_getsc(ue); 1125184610Salfred 1126188412Sthompsa /* 1127188412Sthompsa * start the USB transfers, if not already started: 1128188412Sthompsa */ 1129194228Sthompsa usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]); 1130194228Sthompsa usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]); 1131184610Salfred} 1132184610Salfred 1133184610Salfredstatic void 1134192984Sthompsaaxe_init(struct usb_ether *ue) 1135184610Salfred{ 1136194228Sthompsa struct axe_softc *sc = uether_getsc(ue); 1137194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 1138184610Salfred uint16_t rxmode; 1139184610Salfred 1140188412Sthompsa AXE_LOCK_ASSERT(sc, MA_OWNED); 1141184610Salfred 1142215963Syongari if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 1143215963Syongari return; 1144215963Syongari 1145188412Sthompsa /* Cancel pending I/O */ 1146188412Sthompsa axe_stop(ue); 1147184610Salfred 1148215962Syongari axe_reset(sc); 1149215962Syongari 1150197567Sthompsa /* Set MAC address. */ 1151215968Syongari if (AXE_IS_178_FAMILY(sc)) 1152197567Sthompsa axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp)); 1153197567Sthompsa else 1154197567Sthompsa axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp)); 1155184610Salfred 1156184610Salfred /* Set transmitter IPG values */ 1157215968Syongari if (AXE_IS_178_FAMILY(sc)) 1158188412Sthompsa axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2], 1159184610Salfred (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL); 1160215968Syongari else { 1161188412Sthompsa axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL); 1162188412Sthompsa axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL); 1163188412Sthompsa axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL); 1164184610Salfred } 1165184610Salfred 1166224020Syongari /* AX88772B uses different maximum frame burst configuration. */ 1167224020Syongari if (sc->sc_flags & AXE_FLAG_772B) 1168224020Syongari axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG, 1169224020Syongari ax88772b_mfb_table[AX88772B_MFB_16K].threshold, 1170224020Syongari ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL); 1171224020Syongari 1172224020Syongari /* Enable receiver, set RX mode. */ 1173184610Salfred rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE); 1174215968Syongari if (AXE_IS_178_FAMILY(sc)) { 1175224020Syongari if (sc->sc_flags & AXE_FLAG_772B) { 1176224020Syongari /* 1177224020Syongari * Select RX header format type 1. Aligning IP 1178224020Syongari * header on 4 byte boundary is not needed 1179224020Syongari * because we always copy the received frame in 1180224020Syongari * RX handler. 1181224020Syongari */ 1182224020Syongari rxmode |= AXE_772B_RXCMD_HDR_TYPE_1; 1183224020Syongari } else { 1184224020Syongari /* 1185224020Syongari * Default Rx buffer size is too small to get 1186224020Syongari * maximum performance. 1187224020Syongari */ 1188224020Syongari rxmode |= AXE_178_RXCMD_MFB_16384; 1189224020Syongari } 1190184610Salfred } else { 1191184610Salfred rxmode |= AXE_172_RXCMD_UNICAST; 1192184610Salfred } 1193184610Salfred 1194184610Salfred /* If we want promiscuous mode, set the allframes bit. */ 1195188412Sthompsa if (ifp->if_flags & IFF_PROMISC) 1196184610Salfred rxmode |= AXE_RXCMD_PROMISC; 1197188412Sthompsa 1198188412Sthompsa if (ifp->if_flags & IFF_BROADCAST) 1199184610Salfred rxmode |= AXE_RXCMD_BROADCAST; 1200184610Salfred 1201188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); 1202188412Sthompsa 1203184610Salfred /* Load the multicast filter. */ 1204188412Sthompsa axe_setmulti(ue); 1205184610Salfred 1206194677Sthompsa usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]); 1207184610Salfred 1208188412Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 1209215964Syongari /* Switch to selected media. */ 1210215964Syongari axe_ifmedia_upd(ifp); 1211188412Sthompsa axe_start(ue); 1212184610Salfred} 1213184610Salfred 1214184610Salfredstatic void 1215192984Sthompsaaxe_setpromisc(struct usb_ether *ue) 1216184610Salfred{ 1217194228Sthompsa struct axe_softc *sc = uether_getsc(ue); 1218194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 1219184610Salfred uint16_t rxmode; 1220184610Salfred 1221188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode); 1222184610Salfred 1223184610Salfred rxmode = le16toh(rxmode); 1224184610Salfred 1225188412Sthompsa if (ifp->if_flags & IFF_PROMISC) { 1226184610Salfred rxmode |= AXE_RXCMD_PROMISC; 1227184610Salfred } else { 1228184610Salfred rxmode &= ~AXE_RXCMD_PROMISC; 1229184610Salfred } 1230184610Salfred 1231188412Sthompsa axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); 1232184610Salfred 1233188412Sthompsa axe_setmulti(ue); 1234184610Salfred} 1235184610Salfred 1236184610Salfredstatic void 1237192984Sthompsaaxe_stop(struct usb_ether *ue) 1238184610Salfred{ 1239194228Sthompsa struct axe_softc *sc = uether_getsc(ue); 1240194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 1241184610Salfred 1242188412Sthompsa AXE_LOCK_ASSERT(sc, MA_OWNED); 1243184610Salfred 1244213424Syongari ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1245186730Salfred sc->sc_flags &= ~AXE_FLAG_LINK; 1246184610Salfred 1247184610Salfred /* 1248184610Salfred * stop all the transfers, if not already stopped: 1249184610Salfred */ 1250194228Sthompsa usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]); 1251194228Sthompsa usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]); 1252184610Salfred} 1253