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