1 /* $NetBSD: if_axe.c,v 1.65 2013/09/12 21:03:11 martin Exp $ */ 2 /* $OpenBSD: if_axe.c,v 1.96 2010/01/09 05:33:08 jsg Exp $ */ 3 4 /* 5 * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * Copyright (c) 1997, 1998, 1999, 2000-2003 22 * Bill Paul <wpaul@windriver.com>. All rights reserved. 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 3. All advertising materials mentioning features or use of this software 33 * must display the following acknowledgement: 34 * This product includes software developed by Bill Paul. 35 * 4. Neither the name of the author nor the names of any co-contributors 36 * may be used to endorse or promote products derived from this software 37 * without specific prior written permission. 38 * 39 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 42 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 49 * THE POSSIBILITY OF SUCH DAMAGE. 50 */ 51 52 /* 53 * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the 54 * LinkSys USB200M and various other adapters. 55 * 56 * Manuals available from: 57 * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF 58 * Note: you need the manual for the AX88170 chip (USB 1.x ethernet 59 * controller) to find the definitions for the RX control register. 60 * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF 61 * 62 * Written by Bill Paul <wpaul@windriver.com> 63 * Senior Engineer 64 * Wind River Systems 65 */ 66 67 /* 68 * The AX88172 provides USB ethernet supports at 10 and 100Mbps. 69 * It uses an external PHY (reference designs use a RealTek chip), 70 * and has a 64-bit multicast hash filter. There is some information 71 * missing from the manual which one needs to know in order to make 72 * the chip function: 73 * 74 * - You must set bit 7 in the RX control register, otherwise the 75 * chip won't receive any packets. 76 * - You must initialize all 3 IPG registers, or you won't be able 77 * to send any packets. 78 * 79 * Note that this device appears to only support loading the station 80 * address via autoload from the EEPROM (i.e. there's no way to manaully 81 * set it). 82 * 83 * (Adam Weinberger wanted me to name this driver if_gir.c.) 84 */ 85 86 /* 87 * Ported to OpenBSD 3/28/2004 by Greg Taleck <taleck@oz.net> 88 * with bits and pieces from the aue and url drivers. 89 */ 90 91 #include <sys/cdefs.h> 92 __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.65 2013/09/12 21:03:11 martin Exp $"); 93 94 #ifdef _KERNEL_OPT 95 #include "opt_inet.h" 96 #endif 97 98 #include <sys/param.h> 99 #include <sys/bus.h> 100 #include <sys/device.h> 101 #include <sys/kernel.h> 102 #include <sys/mbuf.h> 103 #include <sys/module.h> 104 #include <sys/mutex.h> 105 #include <sys/socket.h> 106 #include <sys/sockio.h> 107 #include <sys/systm.h> 108 109 #include <sys/rnd.h> 110 111 #include <net/if.h> 112 #include <net/if_dl.h> 113 #include <net/if_ether.h> 114 #include <net/if_media.h> 115 116 #include <net/bpf.h> 117 118 #include <dev/mii/mii.h> 119 #include <dev/mii/miivar.h> 120 121 #include <dev/usb/usb.h> 122 #include <dev/usb/usbdi.h> 123 #include <dev/usb/usbdi_util.h> 124 #include <dev/usb/usbdivar.h> 125 #include <dev/usb/usbdevs.h> 126 127 #include <dev/usb/if_axereg.h> 128 129 #ifdef AXE_DEBUG 130 #define DPRINTF(x) do { if (axedebug) printf x; } while (0) 131 #define DPRINTFN(n,x) do { if (axedebug >= (n)) printf x; } while (0) 132 int axedebug = 0; 133 #else 134 #define DPRINTF(x) 135 #define DPRINTFN(n,x) 136 #endif 137 138 /* 139 * Various supported device vendors/products. 140 */ 141 static const struct axe_type axe_devs[] = { 142 { { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE2000}, 0 }, 143 { { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 }, 144 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET }, AX772 }, 145 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 }, 146 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772}, AX772 }, 147 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772A}, AX772 }, 148 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B}, AX772 | AX772B }, 149 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B_1}, AX772 | AX772B }, 150 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178}, AX178 }, 151 { { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T}, 0 }, 152 { { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055 }, AX178 }, 153 { { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR}, 0}, 154 { { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2}, AX772 }, 155 { { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0}, 156 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100}, 0 }, 157 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1 }, AX772 }, 158 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100C1 }, AX772 | AX772B }, 159 { { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 }, 160 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2 }, AX178 }, 161 { { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 }, 162 { { USB_VENDOR_LENOVO, USB_PRODUCT_LENOVO_ETHERNET }, AX772 | AX772B }, 163 { { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M}, 0 }, 164 { { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000 }, AX178 }, 165 { { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LAN_GTJU2}, AX178 }, 166 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2GT}, AX178 }, 167 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX}, 0 }, 168 { { USB_VENDOR_MSI, USB_PRODUCT_MSI_AX88772A}, AX772 }, 169 { { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120}, 0 }, 170 { { USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS }, AX772 }, 171 { { USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T }, AX178 }, 172 { { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 }, 173 { { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029}, 0 }, 174 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028 }, AX178 } 175 }; 176 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p)) 177 178 int axe_match(device_t, cfdata_t, void *); 179 void axe_attach(device_t, device_t, void *); 180 int axe_detach(device_t, int); 181 int axe_activate(device_t, devact_t); 182 183 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc), 184 axe_match, axe_attach, axe_detach, axe_activate); 185 186 static int axe_tx_list_init(struct axe_softc *); 187 static int axe_rx_list_init(struct axe_softc *); 188 static int axe_encap(struct axe_softc *, struct mbuf *, int); 189 static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 190 static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 191 static void axe_tick(void *); 192 static void axe_tick_task(void *); 193 static void axe_start(struct ifnet *); 194 static int axe_ioctl(struct ifnet *, u_long, void *); 195 static int axe_init(struct ifnet *); 196 static void axe_stop(struct ifnet *, int); 197 static void axe_watchdog(struct ifnet *); 198 static int axe_miibus_readreg(device_t, int, int); 199 static void axe_miibus_writereg(device_t, int, int, int); 200 static void axe_miibus_statchg(struct ifnet *); 201 static int axe_cmd(struct axe_softc *, int, int, int, void *); 202 static void axe_reset(struct axe_softc *sc); 203 static int axe_ifmedia_upd(struct ifnet *); 204 static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *); 205 206 static void axe_setmulti(struct axe_softc *); 207 static void axe_lock_mii(struct axe_softc *sc); 208 static void axe_unlock_mii(struct axe_softc *sc); 209 210 static void axe_ax88178_init(struct axe_softc *); 211 static void axe_ax88772_init(struct axe_softc *); 212 213 /* Get exclusive access to the MII registers */ 214 static void 215 axe_lock_mii(struct axe_softc *sc) 216 { 217 218 sc->axe_refcnt++; 219 mutex_enter(&sc->axe_mii_lock); 220 } 221 222 static void 223 axe_unlock_mii(struct axe_softc *sc) 224 { 225 226 mutex_exit(&sc->axe_mii_lock); 227 if (--sc->axe_refcnt < 0) 228 usb_detach_wakeupold((sc->axe_dev)); 229 } 230 231 static int 232 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf) 233 { 234 usb_device_request_t req; 235 usbd_status err; 236 237 KASSERT(mutex_owned(&sc->axe_mii_lock)); 238 239 if (sc->axe_dying) 240 return 0; 241 242 if (AXE_CMD_DIR(cmd)) 243 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 244 else 245 req.bmRequestType = UT_READ_VENDOR_DEVICE; 246 req.bRequest = AXE_CMD_CMD(cmd); 247 USETW(req.wValue, val); 248 USETW(req.wIndex, index); 249 USETW(req.wLength, AXE_CMD_LEN(cmd)); 250 251 err = usbd_do_request(sc->axe_udev, &req, buf); 252 253 if (err) { 254 DPRINTF(("axe_cmd err: cmd %d err %d\n", cmd, err)); 255 return -1; 256 } 257 return 0; 258 } 259 260 static int 261 axe_miibus_readreg(device_t dev, int phy, int reg) 262 { 263 struct axe_softc *sc = device_private(dev); 264 usbd_status err; 265 uint16_t val; 266 267 if (sc->axe_dying) { 268 DPRINTF(("axe: dying\n")); 269 return 0; 270 } 271 272 /* 273 * The chip tells us the MII address of any supported 274 * PHYs attached to the chip, so only read from those. 275 * 276 * But if the chip lies about its PHYs, read from any. 277 */ 278 val = 0; 279 280 if ((phy == sc->axe_phyaddrs[0]) || (phy == sc->axe_phyaddrs[1]) || 281 (sc->axe_flags & AXE_ANY_PHY)) { 282 axe_lock_mii(sc); 283 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL); 284 err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val); 285 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL); 286 axe_unlock_mii(sc); 287 288 if (err) { 289 aprint_error_dev(sc->axe_dev, "read PHY failed\n"); 290 return -1; 291 } 292 DPRINTF(("axe_miibus_readreg: phy 0x%x reg 0x%x val 0x%x\n", 293 phy, reg, val)); 294 295 if (val && val != 0xffff) 296 sc->axe_phyaddrs[0] = phy; 297 } else { 298 DPRINTF(("axe_miibus_readreg: ignore read from phy 0x%x\n", 299 phy)); 300 } 301 return le16toh(val); 302 } 303 304 static void 305 axe_miibus_writereg(device_t dev, int phy, int reg, int aval) 306 { 307 struct axe_softc *sc = device_private(dev); 308 usbd_status err; 309 uint16_t val; 310 311 if (sc->axe_dying) 312 return; 313 314 val = htole16(aval); 315 axe_lock_mii(sc); 316 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL); 317 err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val); 318 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL); 319 axe_unlock_mii(sc); 320 321 if (err) { 322 aprint_error_dev(sc->axe_dev, "write PHY failed\n"); 323 return; 324 } 325 } 326 327 static void 328 axe_miibus_statchg(struct ifnet *ifp) 329 { 330 struct axe_softc *sc = ifp->if_softc; 331 struct mii_data *mii = &sc->axe_mii; 332 int val, err; 333 334 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 335 val = AXE_MEDIA_FULL_DUPLEX; 336 else 337 val = 0; 338 339 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) { 340 val |= (AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC); 341 342 switch (IFM_SUBTYPE(mii->mii_media_active)) { 343 case IFM_1000_T: 344 val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK; 345 break; 346 case IFM_100_TX: 347 val |= AXE_178_MEDIA_100TX; 348 break; 349 case IFM_10_T: 350 /* doesn't need to be handled */ 351 break; 352 } 353 } 354 355 DPRINTF(("axe_miibus_statchg: val=0x%x\n", val)); 356 axe_lock_mii(sc); 357 err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL); 358 axe_unlock_mii(sc); 359 if (err) { 360 aprint_error_dev(sc->axe_dev, "media change failed\n"); 361 return; 362 } 363 } 364 365 /* 366 * Set media options 367 */ 368 static int 369 axe_ifmedia_upd(struct ifnet *ifp) 370 { 371 struct axe_softc *sc = ifp->if_softc; 372 struct mii_data *mii = &sc->axe_mii; 373 int rc; 374 375 sc->axe_link = 0; 376 377 if (mii->mii_instance) { 378 struct mii_softc *miisc; 379 380 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 381 mii_phy_reset(miisc); 382 } 383 384 if ((rc = mii_mediachg(mii)) == ENXIO) 385 return 0; 386 return rc; 387 } 388 389 /* 390 * Report current media status 391 */ 392 static void 393 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 394 { 395 struct axe_softc *sc = ifp->if_softc; 396 struct mii_data *mii = &sc->axe_mii; 397 398 mii_pollstat(mii); 399 ifmr->ifm_active = mii->mii_media_active; 400 ifmr->ifm_status = mii->mii_media_status; 401 } 402 403 static void 404 axe_setmulti(struct axe_softc *sc) 405 { 406 struct ifnet *ifp = &sc->sc_if; 407 struct ether_multi *enm; 408 struct ether_multistep step; 409 uint32_t h = 0; 410 uint16_t rxmode; 411 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 412 413 if (sc->axe_dying) 414 return; 415 416 axe_lock_mii(sc); 417 axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode); 418 rxmode = le16toh(rxmode); 419 420 rxmode &= ~(AXE_RXCMD_ALLMULTI | AXE_RXCMD_PROMISC); 421 422 /* If we want promiscuous mode, set the allframes bit */ 423 if (ifp->if_flags & IFF_PROMISC) { 424 rxmode |= AXE_RXCMD_PROMISC; 425 goto allmulti; 426 } 427 428 /* Now program new ones */ 429 ETHER_FIRST_MULTI(step, &sc->axe_ec, enm); 430 while (enm != NULL) { 431 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 432 ETHER_ADDR_LEN) != 0) 433 goto allmulti; 434 435 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26; 436 hashtbl[h >> 3] |= 1U << (h & 7); 437 ETHER_NEXT_MULTI(step, enm); 438 } 439 ifp->if_flags &= ~IFF_ALLMULTI; 440 axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl); 441 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); 442 axe_unlock_mii(sc); 443 return; 444 445 allmulti: 446 ifp->if_flags |= IFF_ALLMULTI; 447 rxmode |= AXE_RXCMD_ALLMULTI; 448 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); 449 axe_unlock_mii(sc); 450 } 451 452 static void 453 axe_reset(struct axe_softc *sc) 454 { 455 456 if (sc->axe_dying) 457 return; 458 /* XXX What to reset? */ 459 460 /* Wait a little while for the chip to get its brains in order. */ 461 DELAY(1000); 462 } 463 464 static void 465 axe_ax88178_init(struct axe_softc *sc) 466 { 467 #ifdef AXE_DEBUG 468 int gpio0 = 0, phymode = 0; 469 #endif 470 uint16_t eeprom; 471 472 axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL); 473 /* XXX magic */ 474 axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom); 475 axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL); 476 477 eeprom = le16toh(eeprom); 478 479 DPRINTF((" EEPROM is 0x%x\n", eeprom)); 480 481 /* if EEPROM is invalid we have to use to GPIO0 */ 482 #ifdef AXE_DEBUG 483 if (eeprom == 0xffff) { 484 phymode = 0; 485 gpio0 = 1; 486 } else { 487 phymode = eeprom & 7; 488 gpio0 = (eeprom & 0x80) ? 0 : 1; 489 } 490 #endif 491 492 DPRINTF(("use gpio0: %d, phymode %d\n", gpio0, phymode)); 493 494 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x008c, NULL); 495 usbd_delay_ms(sc->axe_udev, 40); 496 if ((eeprom >> 8) != 1) { 497 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL); 498 usbd_delay_ms(sc->axe_udev, 30); 499 500 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x001c, NULL); 501 usbd_delay_ms(sc->axe_udev, 300); 502 503 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL); 504 usbd_delay_ms(sc->axe_udev, 30); 505 } else { 506 DPRINTF(("axe gpio phymode == 1 path\n")); 507 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x0004, NULL); 508 usbd_delay_ms(sc->axe_udev, 30); 509 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x000c, NULL); 510 usbd_delay_ms(sc->axe_udev, 30); 511 } 512 513 /* soft reset */ 514 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL); 515 usbd_delay_ms(sc->axe_udev, 150); 516 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 517 AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL); 518 usbd_delay_ms(sc->axe_udev, 150); 519 /* Enable MII/GMII/RGMII for external PHY */ 520 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL); 521 usbd_delay_ms(sc->axe_udev, 10); 522 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); 523 } 524 525 static void 526 axe_ax88772_init(struct axe_softc *sc) 527 { 528 529 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL); 530 usbd_delay_ms(sc->axe_udev, 40); 531 532 if (sc->axe_phyaddrs[1] == AXE_INTPHY) { 533 /* ask for the embedded PHY */ 534 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL); 535 usbd_delay_ms(sc->axe_udev, 10); 536 537 /* power down and reset state, pin reset state */ 538 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL); 539 usbd_delay_ms(sc->axe_udev, 60); 540 541 /* power down/reset state, pin operating state */ 542 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 543 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL); 544 usbd_delay_ms(sc->axe_udev, 150); 545 546 /* power up, reset */ 547 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL); 548 549 /* power up, operating */ 550 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 551 AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL); 552 } else { 553 /* ask for external PHY */ 554 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL); 555 usbd_delay_ms(sc->axe_udev, 10); 556 557 /* power down internal PHY */ 558 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 559 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL); 560 } 561 562 usbd_delay_ms(sc->axe_udev, 150); 563 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL); 564 } 565 566 /* 567 * Probe for a AX88172 chip. 568 */ 569 int 570 axe_match(device_t parent, cfdata_t match, void *aux) 571 { 572 struct usb_attach_arg *uaa = aux; 573 574 return axe_lookup(uaa->vendor, uaa->product) != NULL ? 575 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 576 } 577 578 /* 579 * Attach the interface. Allocate softc structures, do ifmedia 580 * setup and ethernet/BPF attach. 581 */ 582 void 583 axe_attach(device_t parent, device_t self, void *aux) 584 { 585 struct axe_softc *sc = device_private(self); 586 struct usb_attach_arg *uaa = aux; 587 usbd_device_handle dev = uaa->device; 588 usbd_status err; 589 usb_interface_descriptor_t *id; 590 usb_endpoint_descriptor_t *ed; 591 struct mii_data *mii; 592 uint8_t eaddr[ETHER_ADDR_LEN]; 593 char *devinfop; 594 const char *devname = device_xname(self); 595 struct ifnet *ifp; 596 int i, s; 597 598 aprint_naive("\n"); 599 aprint_normal("\n"); 600 601 sc->axe_dev = self; 602 sc->axe_udev = dev; 603 604 devinfop = usbd_devinfo_alloc(dev, 0); 605 aprint_normal_dev(self, "%s\n", devinfop); 606 usbd_devinfo_free(devinfop); 607 608 err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1); 609 if (err) { 610 aprint_error_dev(self, "failed to set configuration" 611 ", err=%s\n", usbd_errstr(err)); 612 return; 613 } 614 615 sc->axe_flags = axe_lookup(uaa->vendor, uaa->product)->axe_flags; 616 617 mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE); 618 usb_init_task(&sc->axe_tick_task, axe_tick_task, sc, 0); 619 620 err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface); 621 if (err) { 622 aprint_error_dev(self, "getting interface handle failed\n"); 623 return; 624 } 625 626 sc->axe_product = uaa->product; 627 sc->axe_vendor = uaa->vendor; 628 629 id = usbd_get_interface_descriptor(sc->axe_iface); 630 631 /* decide on what our bufsize will be */ 632 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) 633 sc->axe_bufsz = (sc->axe_udev->speed == USB_SPEED_HIGH) ? 634 AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ; 635 else 636 sc->axe_bufsz = AXE_172_BUFSZ; 637 638 /* Find endpoints. */ 639 for (i = 0; i < id->bNumEndpoints; i++) { 640 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i); 641 if (ed == NULL) { 642 aprint_error_dev(self, "couldn't get ep %d\n", i); 643 return; 644 } 645 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 646 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 647 sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress; 648 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 649 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 650 sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress; 651 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 652 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 653 sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress; 654 } 655 } 656 657 s = splnet(); 658 659 /* We need the PHYID for init dance in some cases */ 660 axe_lock_mii(sc); 661 axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs); 662 663 DPRINTF((" phyaddrs[0]: %x phyaddrs[1]: %x\n", 664 sc->axe_phyaddrs[0], sc->axe_phyaddrs[1])); 665 666 if (sc->axe_flags & AX178) 667 axe_ax88178_init(sc); 668 else if (sc->axe_flags & AX772) 669 axe_ax88772_init(sc); 670 671 /* 672 * Get station address. 673 */ 674 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) 675 axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, &eaddr); 676 else 677 axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, &eaddr); 678 679 /* 680 * Load IPG values 681 */ 682 axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs); 683 axe_unlock_mii(sc); 684 685 /* 686 * An ASIX chip was detected. Inform the world. 687 */ 688 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); 689 690 /* Initialize interface info.*/ 691 ifp = &sc->sc_if; 692 ifp->if_softc = sc; 693 strncpy(ifp->if_xname, devname, IFNAMSIZ); 694 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 695 ifp->if_ioctl = axe_ioctl; 696 ifp->if_start = axe_start; 697 ifp->if_init = axe_init; 698 ifp->if_stop = axe_stop; 699 ifp->if_watchdog = axe_watchdog; 700 701 IFQ_SET_READY(&ifp->if_snd); 702 703 sc->axe_ec.ec_capabilities = ETHERCAP_VLAN_MTU; 704 705 /* Initialize MII/media info. */ 706 mii = &sc->axe_mii; 707 mii->mii_ifp = ifp; 708 mii->mii_readreg = axe_miibus_readreg; 709 mii->mii_writereg = axe_miibus_writereg; 710 mii->mii_statchg = axe_miibus_statchg; 711 mii->mii_flags = MIIF_AUTOTSLEEP; 712 713 sc->axe_ec.ec_mii = mii; 714 if (sc->axe_flags & AXE_MII) 715 ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd, 716 axe_ifmedia_sts); 717 else 718 ifmedia_init(&mii->mii_media, 0, ether_mediachange, 719 ether_mediastatus); 720 721 mii_attach(sc->axe_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 722 0); 723 724 if (LIST_EMPTY(&mii->mii_phys)) { 725 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 726 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 727 } else 728 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 729 730 /* Attach the interface. */ 731 if_attach(ifp); 732 ether_ifattach(ifp, eaddr); 733 rnd_attach_source(&sc->rnd_source, device_xname(sc->axe_dev), 734 RND_TYPE_NET, 0); 735 736 callout_init(&sc->axe_stat_ch, 0); 737 callout_setfunc(&sc->axe_stat_ch, axe_tick, sc); 738 739 sc->axe_attached = true; 740 splx(s); 741 742 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev, sc->axe_dev); 743 } 744 745 int 746 axe_detach(device_t self, int flags) 747 { 748 struct axe_softc *sc = device_private(self); 749 int s; 750 struct ifnet *ifp = &sc->sc_if; 751 752 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axe_dev), __func__)); 753 754 /* Detached before attached finished, so just bail out. */ 755 if (!sc->axe_attached) 756 return 0; 757 758 sc->axe_dying = true; 759 760 /* 761 * Remove any pending tasks. They cannot be executing because they run 762 * in the same thread as detach. 763 */ 764 usb_rem_task(sc->axe_udev, &sc->axe_tick_task); 765 766 s = splusb(); 767 768 if (ifp->if_flags & IFF_RUNNING) 769 axe_stop(ifp, 1); 770 771 callout_destroy(&sc->axe_stat_ch); 772 mutex_destroy(&sc->axe_mii_lock); 773 rnd_detach_source(&sc->rnd_source); 774 mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY); 775 ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY); 776 ether_ifdetach(ifp); 777 if_detach(ifp); 778 779 #ifdef DIAGNOSTIC 780 if (sc->axe_ep[AXE_ENDPT_TX] != NULL || 781 sc->axe_ep[AXE_ENDPT_RX] != NULL || 782 sc->axe_ep[AXE_ENDPT_INTR] != NULL) 783 aprint_debug_dev(self, "detach has active endpoints\n"); 784 #endif 785 786 sc->axe_attached = false; 787 788 if (--sc->axe_refcnt >= 0) { 789 /* Wait for processes to go away. */ 790 usb_detach_waitold(sc->axe_dev); 791 } 792 splx(s); 793 794 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev, sc->axe_dev); 795 796 return 0; 797 } 798 799 int 800 axe_activate(device_t self, devact_t act) 801 { 802 struct axe_softc *sc = device_private(self); 803 804 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->axe_dev), __func__)); 805 806 switch (act) { 807 case DVACT_DEACTIVATE: 808 if_deactivate(&sc->axe_ec.ec_if); 809 sc->axe_dying = true; 810 return 0; 811 default: 812 return EOPNOTSUPP; 813 } 814 } 815 816 static int 817 axe_rx_list_init(struct axe_softc *sc) 818 { 819 struct axe_cdata *cd; 820 struct axe_chain *c; 821 int i; 822 823 DPRINTF(("%s: %s: enter\n", device_xname(sc->axe_dev), __func__)); 824 825 cd = &sc->axe_cdata; 826 for (i = 0; i < AXE_RX_LIST_CNT; i++) { 827 c = &cd->axe_rx_chain[i]; 828 c->axe_sc = sc; 829 c->axe_idx = i; 830 if (c->axe_xfer == NULL) { 831 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev); 832 if (c->axe_xfer == NULL) 833 return ENOBUFS; 834 c->axe_buf = usbd_alloc_buffer(c->axe_xfer, 835 sc->axe_bufsz); 836 if (c->axe_buf == NULL) { 837 usbd_free_xfer(c->axe_xfer); 838 return ENOBUFS; 839 } 840 } 841 } 842 843 return 0; 844 } 845 846 static int 847 axe_tx_list_init(struct axe_softc *sc) 848 { 849 struct axe_cdata *cd; 850 struct axe_chain *c; 851 int i; 852 853 DPRINTF(("%s: %s: enter\n", device_xname(sc->axe_dev), __func__)); 854 855 cd = &sc->axe_cdata; 856 for (i = 0; i < AXE_TX_LIST_CNT; i++) { 857 c = &cd->axe_tx_chain[i]; 858 c->axe_sc = sc; 859 c->axe_idx = i; 860 if (c->axe_xfer == NULL) { 861 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev); 862 if (c->axe_xfer == NULL) 863 return ENOBUFS; 864 c->axe_buf = usbd_alloc_buffer(c->axe_xfer, 865 sc->axe_bufsz); 866 if (c->axe_buf == NULL) { 867 usbd_free_xfer(c->axe_xfer); 868 return ENOBUFS; 869 } 870 } 871 } 872 873 return 0; 874 } 875 876 /* 877 * A frame has been uploaded: pass the resulting mbuf chain up to 878 * the higher level protocols. 879 */ 880 static void 881 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 882 { 883 struct axe_softc *sc; 884 struct axe_chain *c; 885 struct ifnet *ifp; 886 uint8_t *buf; 887 uint32_t total_len; 888 u_int rxlen, pktlen; 889 struct mbuf *m; 890 struct axe_sframe_hdr hdr; 891 int s; 892 893 c = (struct axe_chain *)priv; 894 sc = c->axe_sc; 895 buf = c->axe_buf; 896 ifp = &sc->sc_if; 897 898 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->axe_dev),__func__)); 899 900 if (sc->axe_dying) 901 return; 902 903 if ((ifp->if_flags & IFF_RUNNING) == 0) 904 return; 905 906 if (status != USBD_NORMAL_COMPLETION) { 907 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 908 return; 909 if (usbd_ratecheck(&sc->axe_rx_notice)) 910 aprint_error_dev(sc->axe_dev, "usb errors on rx: %s\n", 911 usbd_errstr(status)); 912 if (status == USBD_STALLED) 913 usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]); 914 goto done; 915 } 916 917 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 918 919 do { 920 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) { 921 if (total_len < sizeof(hdr)) { 922 ifp->if_ierrors++; 923 goto done; 924 } 925 926 memcpy(&hdr, buf, sizeof(hdr)); 927 total_len -= sizeof(hdr); 928 buf += sizeof(hdr); 929 930 if (((le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK) ^ 931 (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK)) != 932 AXE_RH1M_RXLEN_MASK) { 933 ifp->if_ierrors++; 934 goto done; 935 } 936 937 rxlen = le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK; 938 if (total_len < rxlen) { 939 pktlen = total_len; 940 total_len = 0; 941 } else { 942 pktlen = rxlen; 943 rxlen = roundup2(rxlen, 2); 944 total_len -= rxlen; 945 } 946 947 } else { /* AX172 */ 948 pktlen = rxlen = total_len; 949 total_len = 0; 950 } 951 952 MGETHDR(m, M_DONTWAIT, MT_DATA); 953 if (m == NULL) { 954 ifp->if_ierrors++; 955 goto done; 956 } 957 958 if (pktlen > MHLEN - ETHER_ALIGN) { 959 MCLGET(m, M_DONTWAIT); 960 if ((m->m_flags & M_EXT) == 0) { 961 m_freem(m); 962 ifp->if_ierrors++; 963 goto done; 964 } 965 } 966 m->m_data += ETHER_ALIGN; 967 968 ifp->if_ipackets++; 969 m->m_pkthdr.rcvif = ifp; 970 m->m_pkthdr.len = m->m_len = pktlen; 971 972 memcpy(mtod(m, uint8_t *), buf, pktlen); 973 buf += rxlen; 974 975 s = splnet(); 976 977 bpf_mtap(ifp, m); 978 979 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->axe_dev), 980 __func__, m->m_len)); 981 (*(ifp)->if_input)((ifp), (m)); 982 983 splx(s); 984 985 } while (total_len > 0); 986 987 done: 988 989 /* Setup new transfer. */ 990 usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX], 991 c, c->axe_buf, sc->axe_bufsz, 992 USBD_SHORT_XFER_OK | USBD_NO_COPY, 993 USBD_NO_TIMEOUT, axe_rxeof); 994 usbd_transfer(xfer); 995 996 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->axe_dev), __func__)); 997 } 998 999 /* 1000 * A frame was downloaded to the chip. It's safe for us to clean up 1001 * the list buffers. 1002 */ 1003 1004 static void 1005 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1006 { 1007 struct axe_softc *sc; 1008 struct axe_chain *c; 1009 struct ifnet *ifp; 1010 int s; 1011 1012 c = priv; 1013 sc = c->axe_sc; 1014 ifp = &sc->sc_if; 1015 1016 if (sc->axe_dying) 1017 return; 1018 1019 s = splnet(); 1020 1021 if (status != USBD_NORMAL_COMPLETION) { 1022 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1023 splx(s); 1024 return; 1025 } 1026 ifp->if_oerrors++; 1027 aprint_error_dev(sc->axe_dev, "usb error on tx: %s\n", 1028 usbd_errstr(status)); 1029 if (status == USBD_STALLED) 1030 usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]); 1031 splx(s); 1032 return; 1033 } 1034 1035 ifp->if_timer = 0; 1036 ifp->if_flags &= ~IFF_OACTIVE; 1037 1038 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1039 axe_start(ifp); 1040 1041 ifp->if_opackets++; 1042 splx(s); 1043 } 1044 1045 static void 1046 axe_tick(void *xsc) 1047 { 1048 struct axe_softc *sc = xsc; 1049 1050 if (sc == NULL) 1051 return; 1052 1053 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->axe_dev), __func__)); 1054 1055 if (sc->axe_dying) 1056 return; 1057 1058 /* Perform periodic stuff in process context */ 1059 usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER); 1060 } 1061 1062 static void 1063 axe_tick_task(void *xsc) 1064 { 1065 int s; 1066 struct axe_softc *sc; 1067 struct ifnet *ifp; 1068 struct mii_data *mii; 1069 1070 sc = xsc; 1071 1072 if (sc == NULL) 1073 return; 1074 1075 if (sc->axe_dying) 1076 return; 1077 1078 ifp = &sc->sc_if; 1079 mii = &sc->axe_mii; 1080 1081 if (mii == NULL) 1082 return; 1083 1084 s = splnet(); 1085 1086 mii_tick(mii); 1087 if (sc->axe_link == 0 && 1088 (mii->mii_media_status & IFM_ACTIVE) != 0 && 1089 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1090 DPRINTF(("%s: %s: got link\n", device_xname(sc->axe_dev), 1091 __func__)); 1092 sc->axe_link++; 1093 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1094 axe_start(ifp); 1095 } 1096 1097 callout_schedule(&sc->axe_stat_ch, hz); 1098 1099 splx(s); 1100 } 1101 1102 static int 1103 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx) 1104 { 1105 struct ifnet *ifp = &sc->sc_if; 1106 struct axe_chain *c; 1107 usbd_status err; 1108 struct axe_sframe_hdr hdr; 1109 int length, boundary; 1110 1111 c = &sc->axe_cdata.axe_tx_chain[idx]; 1112 1113 /* 1114 * Copy the mbuf data into a contiguous buffer, leaving two 1115 * bytes at the beginning to hold the frame length. 1116 */ 1117 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) { 1118 boundary = (sc->axe_udev->speed == USB_SPEED_HIGH) ? 512 : 64; 1119 1120 hdr.len = htole16(m->m_pkthdr.len); 1121 hdr.ilen = ~hdr.len; 1122 1123 memcpy(c->axe_buf, &hdr, sizeof(hdr)); 1124 length = sizeof(hdr); 1125 1126 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length); 1127 length += m->m_pkthdr.len; 1128 1129 if ((length % boundary) == 0) { 1130 hdr.len = 0x0000; 1131 hdr.ilen = 0xffff; 1132 memcpy(c->axe_buf + length, &hdr, sizeof(hdr)); 1133 length += sizeof(hdr); 1134 } 1135 } else { 1136 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf); 1137 length = m->m_pkthdr.len; 1138 } 1139 1140 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX], 1141 c, c->axe_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 10000, 1142 axe_txeof); 1143 1144 /* Transmit */ 1145 err = usbd_transfer(c->axe_xfer); 1146 if (err != USBD_IN_PROGRESS) { 1147 axe_stop(ifp, 0); 1148 return EIO; 1149 } 1150 1151 sc->axe_cdata.axe_tx_cnt++; 1152 1153 return 0; 1154 } 1155 1156 static void 1157 axe_start(struct ifnet *ifp) 1158 { 1159 struct axe_softc *sc; 1160 struct mbuf *m; 1161 1162 sc = ifp->if_softc; 1163 1164 if ((sc->axe_flags & AXE_MII) != 0 && sc->axe_link == 0) 1165 return; 1166 1167 if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING) 1168 return; 1169 1170 IFQ_POLL(&ifp->if_snd, m); 1171 if (m == NULL) { 1172 return; 1173 } 1174 1175 if (axe_encap(sc, m, 0)) { 1176 ifp->if_flags |= IFF_OACTIVE; 1177 return; 1178 } 1179 IFQ_DEQUEUE(&ifp->if_snd, m); 1180 1181 /* 1182 * If there's a BPF listener, bounce a copy of this frame 1183 * to him. 1184 */ 1185 bpf_mtap(ifp, m); 1186 m_freem(m); 1187 1188 ifp->if_flags |= IFF_OACTIVE; 1189 1190 /* 1191 * Set a timeout in case the chip goes out to lunch. 1192 */ 1193 ifp->if_timer = 5; 1194 1195 return; 1196 } 1197 1198 static int 1199 axe_init(struct ifnet *ifp) 1200 { 1201 struct axe_softc *sc = ifp->if_softc; 1202 struct axe_chain *c; 1203 usbd_status err; 1204 int rxmode; 1205 int i, s; 1206 uint8_t eaddr[ETHER_ADDR_LEN]; 1207 1208 s = splnet(); 1209 1210 if (ifp->if_flags & IFF_RUNNING) 1211 axe_stop(ifp, 0); 1212 1213 /* 1214 * Cancel pending I/O and free all RX/TX buffers. 1215 */ 1216 axe_reset(sc); 1217 1218 /* Set MAC address */ 1219 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) { 1220 memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr)); 1221 axe_lock_mii(sc); 1222 axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, eaddr); 1223 axe_unlock_mii(sc); 1224 } 1225 1226 /* Enable RX logic. */ 1227 1228 /* Init RX ring. */ 1229 if (axe_rx_list_init(sc) == ENOBUFS) { 1230 aprint_error_dev(sc->axe_dev, "rx list init failed\n"); 1231 splx(s); 1232 return ENOBUFS; 1233 } 1234 1235 /* Init TX ring. */ 1236 if (axe_tx_list_init(sc) == ENOBUFS) { 1237 aprint_error_dev(sc->axe_dev, "tx list init failed\n"); 1238 splx(s); 1239 return ENOBUFS; 1240 } 1241 1242 /* Set transmitter IPG values */ 1243 axe_lock_mii(sc); 1244 if (sc->axe_flags & AX178 || sc->axe_flags & AX772) 1245 axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2], 1246 (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL); 1247 else { 1248 axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL); 1249 axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL); 1250 axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL); 1251 } 1252 1253 /* Enable receiver, set RX mode */ 1254 rxmode = AXE_RXCMD_BROADCAST | AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE; 1255 if (sc->axe_flags & AX772B) 1256 rxmode |= AXE_772B_RXCMD_RH1M; 1257 else if (sc->axe_flags & AX178 || sc->axe_flags & AX772) { 1258 if (sc->axe_udev->speed == USB_SPEED_HIGH) { 1259 /* Largest possible USB buffer size for AX88178 */ 1260 rxmode |= AXE_178_RXCMD_MFB; 1261 } 1262 } else 1263 rxmode |= AXE_172_RXCMD_UNICAST; 1264 1265 /* If we want promiscuous mode, set the allframes bit. */ 1266 if (ifp->if_flags & IFF_PROMISC) 1267 rxmode |= AXE_RXCMD_PROMISC; 1268 1269 if (ifp->if_flags & IFF_BROADCAST) 1270 rxmode |= AXE_RXCMD_BROADCAST; 1271 1272 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL); 1273 axe_unlock_mii(sc); 1274 1275 /* Load the multicast filter. */ 1276 axe_setmulti(sc); 1277 1278 /* Open RX and TX pipes. */ 1279 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX], 1280 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]); 1281 if (err) { 1282 aprint_error_dev(sc->axe_dev, "open rx pipe failed: %s\n", 1283 usbd_errstr(err)); 1284 splx(s); 1285 return EIO; 1286 } 1287 1288 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX], 1289 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]); 1290 if (err) { 1291 aprint_error_dev(sc->axe_dev, "open tx pipe failed: %s\n", 1292 usbd_errstr(err)); 1293 splx(s); 1294 return EIO; 1295 } 1296 1297 /* Start up the receive pipe. */ 1298 for (i = 0; i < AXE_RX_LIST_CNT; i++) { 1299 c = &sc->axe_cdata.axe_rx_chain[i]; 1300 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX], 1301 c, c->axe_buf, sc->axe_bufsz, 1302 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1303 axe_rxeof); 1304 usbd_transfer(c->axe_xfer); 1305 } 1306 1307 ifp->if_flags |= IFF_RUNNING; 1308 ifp->if_flags &= ~IFF_OACTIVE; 1309 1310 splx(s); 1311 1312 callout_schedule(&sc->axe_stat_ch, hz); 1313 return 0; 1314 } 1315 1316 static int 1317 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1318 { 1319 struct axe_softc *sc = ifp->if_softc; 1320 int s; 1321 int error = 0; 1322 1323 s = splnet(); 1324 1325 switch(cmd) { 1326 case SIOCSIFFLAGS: 1327 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1328 break; 1329 1330 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { 1331 case IFF_RUNNING: 1332 axe_stop(ifp, 1); 1333 break; 1334 case IFF_UP: 1335 axe_init(ifp); 1336 break; 1337 case IFF_UP | IFF_RUNNING: 1338 if ((ifp->if_flags ^ sc->axe_if_flags) == IFF_PROMISC) 1339 axe_setmulti(sc); 1340 else 1341 axe_init(ifp); 1342 break; 1343 } 1344 sc->axe_if_flags = ifp->if_flags; 1345 break; 1346 1347 default: 1348 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) 1349 break; 1350 1351 error = 0; 1352 1353 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) 1354 axe_setmulti(sc); 1355 1356 } 1357 splx(s); 1358 1359 return error; 1360 } 1361 1362 static void 1363 axe_watchdog(struct ifnet *ifp) 1364 { 1365 struct axe_softc *sc; 1366 struct axe_chain *c; 1367 usbd_status stat; 1368 int s; 1369 1370 sc = ifp->if_softc; 1371 1372 ifp->if_oerrors++; 1373 aprint_error_dev(sc->axe_dev, "watchdog timeout\n"); 1374 1375 s = splusb(); 1376 c = &sc->axe_cdata.axe_tx_chain[0]; 1377 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat); 1378 axe_txeof(c->axe_xfer, c, stat); 1379 1380 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1381 axe_start(ifp); 1382 splx(s); 1383 } 1384 1385 /* 1386 * Stop the adapter and free any mbufs allocated to the 1387 * RX and TX lists. 1388 */ 1389 static void 1390 axe_stop(struct ifnet *ifp, int disable) 1391 { 1392 struct axe_softc *sc = ifp->if_softc; 1393 usbd_status err; 1394 int i; 1395 1396 axe_reset(sc); 1397 1398 ifp->if_timer = 0; 1399 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1400 1401 callout_stop(&sc->axe_stat_ch); 1402 1403 /* Stop transfers. */ 1404 if (sc->axe_ep[AXE_ENDPT_RX] != NULL) { 1405 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]); 1406 if (err) { 1407 aprint_error_dev(sc->axe_dev, 1408 "abort rx pipe failed: %s\n", usbd_errstr(err)); 1409 } 1410 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]); 1411 if (err) { 1412 aprint_error_dev(sc->axe_dev, 1413 "close rx pipe failed: %s\n", usbd_errstr(err)); 1414 } 1415 sc->axe_ep[AXE_ENDPT_RX] = NULL; 1416 } 1417 1418 if (sc->axe_ep[AXE_ENDPT_TX] != NULL) { 1419 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]); 1420 if (err) { 1421 aprint_error_dev(sc->axe_dev, 1422 "abort tx pipe failed: %s\n", usbd_errstr(err)); 1423 } 1424 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]); 1425 if (err) { 1426 aprint_error_dev(sc->axe_dev, 1427 "close tx pipe failed: %s\n", usbd_errstr(err)); 1428 } 1429 sc->axe_ep[AXE_ENDPT_TX] = NULL; 1430 } 1431 1432 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) { 1433 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]); 1434 if (err) { 1435 aprint_error_dev(sc->axe_dev, 1436 "abort intr pipe failed: %s\n", usbd_errstr(err)); 1437 } 1438 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]); 1439 if (err) { 1440 aprint_error_dev(sc->axe_dev, 1441 "close intr pipe failed: %s\n", usbd_errstr(err)); 1442 } 1443 sc->axe_ep[AXE_ENDPT_INTR] = NULL; 1444 } 1445 1446 /* Free RX resources. */ 1447 for (i = 0; i < AXE_RX_LIST_CNT; i++) { 1448 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) { 1449 usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer); 1450 sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL; 1451 } 1452 } 1453 1454 /* Free TX resources. */ 1455 for (i = 0; i < AXE_TX_LIST_CNT; i++) { 1456 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) { 1457 usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer); 1458 sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL; 1459 } 1460 } 1461 1462 sc->axe_link = 0; 1463 } 1464 1465 MODULE(MODULE_CLASS_DRIVER, if_axe, "bpf"); 1466 1467 #ifdef _MODULE 1468 #include "ioconf.c" 1469 #endif 1470 1471 static int 1472 if_axe_modcmd(modcmd_t cmd, void *aux) 1473 { 1474 int error = 0; 1475 1476 switch (cmd) { 1477 case MODULE_CMD_INIT: 1478 #ifdef _MODULE 1479 error = config_init_component(cfdriver_ioconf_axe, 1480 cfattach_ioconf_axe, cfdata_ioconf_axe); 1481 #endif 1482 return error; 1483 case MODULE_CMD_FINI: 1484 #ifdef _MODULE 1485 error = config_fini_component(cfdriver_ioconf_axe, 1486 cfattach_ioconf_axe, cfdata_ioconf_axe); 1487 #endif 1488 return error; 1489 default: 1490 return ENOTTY; 1491 } 1492 } 1493