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