1 /* $NetBSD: if_aue.c,v 1.71 2001/12/03 01:47:12 augustss Exp $ */ 2 /* 3 * Copyright (c) 1997, 1998, 1999, 2000 4 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Bill Paul. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $ 34 */ 35 36 /* 37 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver. 38 * Datasheet is available from http://www.admtek.com.tw. 39 * 40 * Written by Bill Paul <wpaul@ee.columbia.edu> 41 * Electrical Engineering Department 42 * Columbia University, New York City 43 */ 44 45 /* 46 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet 47 * support: the control endpoint for reading/writing registers, burst 48 * read endpoint for packet reception, burst write for packet transmission 49 * and one for "interrupts." The chip uses the same RX filter scheme 50 * as the other ADMtek ethernet parts: one perfect filter entry for the 51 * the station address and a 64-bit multicast hash table. The chip supports 52 * both MII and HomePNA attachments. 53 * 54 * Since the maximum data transfer speed of USB is supposed to be 12Mbps, 55 * you're never really going to get 100Mbps speeds from this device. I 56 * think the idea is to allow the device to connect to 10 or 100Mbps 57 * networks, not necessarily to provide 100Mbps performance. Also, since 58 * the controller uses an external PHY chip, it's possible that board 59 * designers might simply choose a 10Mbps PHY. 60 * 61 * Registers are accessed using usbd_do_request(). Packet transfers are 62 * done using usbd_transfer() and friends. 63 */ 64 65 /* 66 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson. 67 */ 68 69 /* 70 * TODO: 71 * better error messages from rxstat 72 * split out if_auevar.h 73 * add thread to avoid register reads from interrupt context 74 * more error checks 75 * investigate short rx problem 76 * proper cleanup on errors 77 */ 78 79 #include <sys/cdefs.h> 80 __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1.71 2001/12/03 01:47:12 augustss Exp $"); 81 82 #if defined(__NetBSD__) 83 #include "opt_inet.h" 84 #include "opt_ns.h" 85 #include "bpfilter.h" 86 #include "rnd.h" 87 #elif defined(__OpenBSD__) 88 #include "bpfilter.h" 89 #endif /* defined(__OpenBSD__) */ 90 91 #include <sys/param.h> 92 #include <sys/systm.h> 93 #include <sys/sockio.h> 94 #include <sys/lock.h> 95 #include <sys/mbuf.h> 96 #include <sys/malloc.h> 97 #include <sys/kernel.h> 98 #include <sys/socket.h> 99 100 #include <sys/device.h> 101 #if NRND > 0 102 #include <sys/rnd.h> 103 #endif 104 105 #include <net/if.h> 106 #if defined(__NetBSD__) 107 #include <net/if_arp.h> 108 #endif 109 #include <net/if_dl.h> 110 #include <net/if_media.h> 111 112 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m)) 113 114 #if NBPFILTER > 0 115 #include <net/bpf.h> 116 #endif 117 118 #if defined(__NetBSD__) 119 #include <net/if_ether.h> 120 #ifdef INET 121 #include <netinet/in.h> 122 #include <netinet/if_inarp.h> 123 #endif 124 #endif /* defined(__NetBSD__) */ 125 126 #if defined(__OpenBSD__) 127 #ifdef INET 128 #include <netinet/in.h> 129 #include <netinet/in_systm.h> 130 #include <netinet/in_var.h> 131 #include <netinet/ip.h> 132 #include <netinet/if_ether.h> 133 #endif 134 #endif /* defined(__OpenBSD__) */ 135 136 #ifdef NS 137 #include <netns/ns.h> 138 #include <netns/ns_if.h> 139 #endif 140 141 #include <dev/mii/mii.h> 142 #include <dev/mii/miivar.h> 143 144 #include <dev/usb/usb.h> 145 #include <dev/usb/usbdi.h> 146 #include <dev/usb/usbdi_util.h> 147 #include <dev/usb/usbdevs.h> 148 149 #include <dev/usb/if_auereg.h> 150 151 #ifdef AUE_DEBUG 152 #define DPRINTF(x) if (auedebug) logprintf x 153 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x 154 int auedebug = 0; 155 #else 156 #define DPRINTF(x) 157 #define DPRINTFN(n,x) 158 #endif 159 160 /* 161 * Various supported device vendors/products. 162 */ 163 struct aue_type { 164 struct usb_devno aue_dev; 165 u_int16_t aue_flags; 166 #define LSYS 0x0001 /* use Linksys reset */ 167 #define PNA 0x0002 /* has Home PNA */ 168 #define PII 0x0004 /* Pegasus II chip */ 169 }; 170 171 Static const struct aue_type aue_devs[] = { 172 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA|PII }, 173 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII }, 174 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX3}, 0 }, 175 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA }, 176 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA }, 177 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII }, 178 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII }, 179 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII }, 180 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA }, 181 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 }, 182 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 }, 183 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 }, 184 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA }, 185 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII }, 186 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 }, 187 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA }, 188 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 }, 189 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII }, 190 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 }, 191 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII }, 192 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS|PII }, 193 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS }, 194 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS }, 195 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA }, 196 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS|PII }, 197 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS|PII }, 198 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, 0 }, 199 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 }, 200 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, 0 }, 201 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 }, 202 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, PII }, 203 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 }, 204 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 }, 205 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII }, 206 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 }, 207 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS|PII }, 208 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS }, 209 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS }, 210 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS|PNA }, 211 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS }, 212 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS|PII }, 213 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 }, 214 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 }, 215 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII }, 216 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII }, 217 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 }, 218 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 }, 219 }; 220 #define aue_lookup(v, p) ((struct aue_type *)usb_lookup(aue_devs, v, p)) 221 222 USB_DECLARE_DRIVER(aue); 223 224 Static void aue_reset_pegasus_II(struct aue_softc *sc); 225 Static int aue_tx_list_init(struct aue_softc *); 226 Static int aue_rx_list_init(struct aue_softc *); 227 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *); 228 Static int aue_send(struct aue_softc *, struct mbuf *, int); 229 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 230 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 231 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 232 Static void aue_tick(void *); 233 Static void aue_tick_task(void *); 234 Static void aue_start(struct ifnet *); 235 Static int aue_ioctl(struct ifnet *, u_long, caddr_t); 236 Static void aue_init(void *); 237 Static void aue_stop(struct aue_softc *); 238 Static void aue_watchdog(struct ifnet *); 239 Static int aue_openpipes(struct aue_softc *); 240 Static int aue_ifmedia_upd(struct ifnet *); 241 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *); 242 243 Static int aue_eeprom_getword(struct aue_softc *, int); 244 Static void aue_read_mac(struct aue_softc *, u_char *); 245 Static int aue_miibus_readreg(device_ptr_t, int, int); 246 Static void aue_miibus_writereg(device_ptr_t, int, int, int); 247 Static void aue_miibus_statchg(device_ptr_t); 248 249 Static void aue_lock_mii(struct aue_softc *); 250 Static void aue_unlock_mii(struct aue_softc *); 251 252 Static void aue_setmulti(struct aue_softc *); 253 Static u_int32_t aue_crc(caddr_t); 254 Static void aue_reset(struct aue_softc *); 255 256 Static int aue_csr_read_1(struct aue_softc *, int); 257 Static int aue_csr_write_1(struct aue_softc *, int, int); 258 Static int aue_csr_read_2(struct aue_softc *, int); 259 Static int aue_csr_write_2(struct aue_softc *, int, int); 260 261 #define AUE_SETBIT(sc, reg, x) \ 262 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x)) 263 264 #define AUE_CLRBIT(sc, reg, x) \ 265 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x)) 266 267 Static int 268 aue_csr_read_1(struct aue_softc *sc, int reg) 269 { 270 usb_device_request_t req; 271 usbd_status err; 272 uByte val = 0; 273 274 if (sc->aue_dying) 275 return (0); 276 277 req.bmRequestType = UT_READ_VENDOR_DEVICE; 278 req.bRequest = AUE_UR_READREG; 279 USETW(req.wValue, 0); 280 USETW(req.wIndex, reg); 281 USETW(req.wLength, 1); 282 283 err = usbd_do_request(sc->aue_udev, &req, &val); 284 285 if (err) { 286 DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n", 287 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 288 return (0); 289 } 290 291 return (val); 292 } 293 294 Static int 295 aue_csr_read_2(struct aue_softc *sc, int reg) 296 { 297 usb_device_request_t req; 298 usbd_status err; 299 uWord val; 300 301 if (sc->aue_dying) 302 return (0); 303 304 req.bmRequestType = UT_READ_VENDOR_DEVICE; 305 req.bRequest = AUE_UR_READREG; 306 USETW(req.wValue, 0); 307 USETW(req.wIndex, reg); 308 USETW(req.wLength, 2); 309 310 err = usbd_do_request(sc->aue_udev, &req, &val); 311 312 if (err) { 313 DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n", 314 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 315 return (0); 316 } 317 318 return (UGETW(val)); 319 } 320 321 Static int 322 aue_csr_write_1(struct aue_softc *sc, int reg, int aval) 323 { 324 usb_device_request_t req; 325 usbd_status err; 326 uByte val; 327 328 if (sc->aue_dying) 329 return (0); 330 331 val = aval; 332 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 333 req.bRequest = AUE_UR_WRITEREG; 334 USETW(req.wValue, val); 335 USETW(req.wIndex, reg); 336 USETW(req.wLength, 1); 337 338 err = usbd_do_request(sc->aue_udev, &req, &val); 339 340 if (err) { 341 DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n", 342 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 343 return (-1); 344 } 345 346 return (0); 347 } 348 349 Static int 350 aue_csr_write_2(struct aue_softc *sc, int reg, int aval) 351 { 352 usb_device_request_t req; 353 usbd_status err; 354 uWord val; 355 356 if (sc->aue_dying) 357 return (0); 358 359 USETW(val, aval); 360 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 361 req.bRequest = AUE_UR_WRITEREG; 362 USETW(req.wValue, aval); 363 USETW(req.wIndex, reg); 364 USETW(req.wLength, 2); 365 366 err = usbd_do_request(sc->aue_udev, &req, &val); 367 368 if (err) { 369 DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n", 370 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 371 return (-1); 372 } 373 374 return (0); 375 } 376 377 /* 378 * Read a word of data stored in the EEPROM at address 'addr.' 379 */ 380 Static int 381 aue_eeprom_getword(struct aue_softc *sc, int addr) 382 { 383 int i; 384 385 aue_csr_write_1(sc, AUE_EE_REG, addr); 386 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ); 387 388 for (i = 0; i < AUE_TIMEOUT; i++) { 389 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE) 390 break; 391 } 392 393 if (i == AUE_TIMEOUT) { 394 printf("%s: EEPROM read timed out\n", 395 USBDEVNAME(sc->aue_dev)); 396 } 397 398 return (aue_csr_read_2(sc, AUE_EE_DATA)); 399 } 400 401 /* 402 * Read the MAC from the EEPROM. It's at offset 0. 403 */ 404 Static void 405 aue_read_mac(struct aue_softc *sc, u_char *dest) 406 { 407 int i; 408 int off = 0; 409 int word; 410 411 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 412 413 for (i = 0; i < 3; i++) { 414 word = aue_eeprom_getword(sc, off + i); 415 dest[2 * i] = (u_char)word; 416 dest[2 * i + 1] = (u_char)(word >> 8); 417 } 418 } 419 420 /* Get exclusive access to the MII registers */ 421 Static void 422 aue_lock_mii(struct aue_softc *sc) 423 { 424 sc->aue_refcnt++; 425 lockmgr(&sc->aue_mii_lock, LK_EXCLUSIVE, NULL); 426 } 427 428 Static void 429 aue_unlock_mii(struct aue_softc *sc) 430 { 431 lockmgr(&sc->aue_mii_lock, LK_RELEASE, NULL); 432 if (--sc->aue_refcnt < 0) 433 usb_detach_wakeup(USBDEV(sc->aue_dev)); 434 } 435 436 Static int 437 aue_miibus_readreg(device_ptr_t dev, int phy, int reg) 438 { 439 struct aue_softc *sc = USBGETSOFTC(dev); 440 int i; 441 u_int16_t val; 442 443 if (sc->aue_dying) { 444 #ifdef DIAGNOSTIC 445 printf("%s: dying\n", USBDEVNAME(sc->aue_dev)); 446 #endif 447 return 0; 448 } 449 450 #if 0 451 /* 452 * The Am79C901 HomePNA PHY actually contains 453 * two transceivers: a 1Mbps HomePNA PHY and a 454 * 10Mbps full/half duplex ethernet PHY with 455 * NWAY autoneg. However in the ADMtek adapter, 456 * only the 1Mbps PHY is actually connected to 457 * anything, so we ignore the 10Mbps one. It 458 * happens to be configured for MII address 3, 459 * so we filter that out. 460 */ 461 if (sc->aue_vendor == USB_VENDOR_ADMTEK && 462 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { 463 if (phy == 3) 464 return (0); 465 } 466 #endif 467 468 aue_lock_mii(sc); 469 aue_csr_write_1(sc, AUE_PHY_ADDR, phy); 470 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ); 471 472 for (i = 0; i < AUE_TIMEOUT; i++) { 473 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) 474 break; 475 } 476 477 if (i == AUE_TIMEOUT) { 478 printf("%s: MII read timed out\n", USBDEVNAME(sc->aue_dev)); 479 } 480 481 val = aue_csr_read_2(sc, AUE_PHY_DATA); 482 483 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n", 484 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val)); 485 486 aue_unlock_mii(sc); 487 return (val); 488 } 489 490 Static void 491 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data) 492 { 493 struct aue_softc *sc = USBGETSOFTC(dev); 494 int i; 495 496 #if 0 497 if (sc->aue_vendor == USB_VENDOR_ADMTEK && 498 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { 499 if (phy == 3) 500 return; 501 } 502 #endif 503 504 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n", 505 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data)); 506 507 aue_lock_mii(sc); 508 aue_csr_write_2(sc, AUE_PHY_DATA, data); 509 aue_csr_write_1(sc, AUE_PHY_ADDR, phy); 510 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE); 511 512 for (i = 0; i < AUE_TIMEOUT; i++) { 513 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) 514 break; 515 } 516 517 if (i == AUE_TIMEOUT) { 518 printf("%s: MII read timed out\n", 519 USBDEVNAME(sc->aue_dev)); 520 } 521 aue_unlock_mii(sc); 522 } 523 524 Static void 525 aue_miibus_statchg(device_ptr_t dev) 526 { 527 struct aue_softc *sc = USBGETSOFTC(dev); 528 struct mii_data *mii = GET_MII(sc); 529 530 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 531 532 aue_lock_mii(sc); 533 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); 534 535 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { 536 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); 537 } else { 538 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); 539 } 540 541 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 542 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); 543 else 544 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); 545 546 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); 547 aue_unlock_mii(sc); 548 549 /* 550 * Set the LED modes on the LinkSys adapter. 551 * This turns on the 'dual link LED' bin in the auxmode 552 * register of the Broadcom PHY. 553 */ 554 if (!sc->aue_dying && (sc->aue_flags & LSYS)) { 555 u_int16_t auxmode; 556 auxmode = aue_miibus_readreg(dev, 0, 0x1b); 557 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04); 558 } 559 DPRINTFN(5,("%s: %s: exit\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 560 } 561 562 #define AUE_POLY 0xEDB88320 563 #define AUE_BITS 6 564 565 Static u_int32_t 566 aue_crc(caddr_t addr) 567 { 568 u_int32_t idx, bit, data, crc; 569 570 /* Compute CRC for the address value. */ 571 crc = 0xFFFFFFFF; /* initial value */ 572 573 for (idx = 0; idx < 6; idx++) { 574 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) 575 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0); 576 } 577 578 return (crc & ((1 << AUE_BITS) - 1)); 579 } 580 581 Static void 582 aue_setmulti(struct aue_softc *sc) 583 { 584 struct ifnet *ifp; 585 struct ether_multi *enm; 586 struct ether_multistep step; 587 u_int32_t h = 0, i; 588 589 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 590 591 ifp = GET_IFP(sc); 592 593 if (ifp->if_flags & IFF_PROMISC) { 594 allmulti: 595 ifp->if_flags |= IFF_ALLMULTI; 596 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); 597 return; 598 } 599 600 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); 601 602 /* first, zot all the existing hash bits */ 603 for (i = 0; i < 8; i++) 604 aue_csr_write_1(sc, AUE_MAR0 + i, 0); 605 606 /* now program new ones */ 607 #if defined(__NetBSD__) 608 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm); 609 #else 610 ETHER_FIRST_MULTI(step, &sc->arpcom, enm); 611 #endif 612 while (enm != NULL) { 613 if (memcmp(enm->enm_addrlo, 614 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) 615 goto allmulti; 616 617 h = aue_crc(enm->enm_addrlo); 618 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7)); 619 ETHER_NEXT_MULTI(step, enm); 620 } 621 622 ifp->if_flags &= ~IFF_ALLMULTI; 623 } 624 625 Static void 626 aue_reset_pegasus_II(struct aue_softc *sc) 627 { 628 /* Magic constants taken from Linux driver. */ 629 aue_csr_write_1(sc, AUE_REG_1D, 0); 630 aue_csr_write_1(sc, AUE_REG_7B, 2); 631 #if 0 632 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode) 633 aue_csr_write_1(sc, AUE_REG_81, 6); 634 else 635 #endif 636 aue_csr_write_1(sc, AUE_REG_81, 2); 637 } 638 639 Static void 640 aue_reset(struct aue_softc *sc) 641 { 642 int i; 643 644 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 645 646 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC); 647 648 for (i = 0; i < AUE_TIMEOUT; i++) { 649 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC)) 650 break; 651 } 652 653 if (i == AUE_TIMEOUT) 654 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev)); 655 656 /* 657 * The PHY(s) attached to the Pegasus chip may be held 658 * in reset until we flip on the GPIO outputs. Make sure 659 * to set the GPIO pins high so that the PHY(s) will 660 * be enabled. 661 * 662 * Note: We force all of the GPIO pins low first, *then* 663 * enable the ones we want. 664 */ 665 aue_csr_write_1(sc, AUE_GPIO0, 666 AUE_GPIO_OUT0 | AUE_GPIO_SEL0); 667 aue_csr_write_1(sc, AUE_GPIO0, 668 AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1); 669 670 #if 0 671 /* XXX what is mii_mode supposed to be */ 672 if (sc->aue_mii_mode && (sc->aue_flags & PNA)) 673 aue_csr_write_1(sc, AUE_GPIO1, 0x34); 674 else 675 aue_csr_write_1(sc, AUE_GPIO1, 0x26); 676 #endif 677 678 /* Grrr. LinkSys has to be different from everyone else. */ 679 if (sc->aue_flags & LSYS) { 680 aue_csr_write_1(sc, AUE_GPIO0, 681 AUE_GPIO_SEL0 | AUE_GPIO_SEL1); 682 aue_csr_write_1(sc, AUE_GPIO0, 683 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0); 684 } 685 686 if (sc->aue_flags & PII) 687 aue_reset_pegasus_II(sc); 688 689 /* Wait a little while for the chip to get its brains in order. */ 690 delay(10000); /* XXX */ 691 } 692 693 /* 694 * Probe for a Pegasus chip. 695 */ 696 USB_MATCH(aue) 697 { 698 USB_MATCH_START(aue, uaa); 699 700 if (uaa->iface != NULL) 701 return (UMATCH_NONE); 702 703 return (aue_lookup(uaa->vendor, uaa->product) != NULL ? 704 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 705 } 706 707 /* 708 * Attach the interface. Allocate softc structures, do ifmedia 709 * setup and ethernet/BPF attach. 710 */ 711 USB_ATTACH(aue) 712 { 713 USB_ATTACH_START(aue, sc, uaa); 714 char devinfo[1024]; 715 int s; 716 u_char eaddr[ETHER_ADDR_LEN]; 717 struct ifnet *ifp; 718 struct mii_data *mii; 719 usbd_device_handle dev = uaa->device; 720 usbd_interface_handle iface; 721 usbd_status err; 722 usb_interface_descriptor_t *id; 723 usb_endpoint_descriptor_t *ed; 724 int i; 725 726 DPRINTFN(5,(" : aue_attach: sc=%p", sc)); 727 728 usbd_devinfo(dev, 0, devinfo); 729 USB_ATTACH_SETUP; 730 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo); 731 732 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1); 733 if (err) { 734 printf("%s: setting config no failed\n", 735 USBDEVNAME(sc->aue_dev)); 736 USB_ATTACH_ERROR_RETURN; 737 } 738 739 usb_init_task(&sc->aue_tick_task, aue_tick_task, sc); 740 usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc); 741 lockinit(&sc->aue_mii_lock, PZERO, "auemii", 0, 0); 742 743 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface); 744 if (err) { 745 printf("%s: getting interface handle failed\n", 746 USBDEVNAME(sc->aue_dev)); 747 USB_ATTACH_ERROR_RETURN; 748 } 749 750 sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags; 751 752 sc->aue_udev = dev; 753 sc->aue_iface = iface; 754 sc->aue_product = uaa->product; 755 sc->aue_vendor = uaa->vendor; 756 757 id = usbd_get_interface_descriptor(iface); 758 759 /* Find endpoints. */ 760 for (i = 0; i < id->bNumEndpoints; i++) { 761 ed = usbd_interface2endpoint_descriptor(iface, i); 762 if (ed == NULL) { 763 printf("%s: couldn't get endpoint descriptor %d\n", 764 USBDEVNAME(sc->aue_dev), i); 765 USB_ATTACH_ERROR_RETURN; 766 } 767 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 768 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 769 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress; 770 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 771 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 772 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress; 773 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 774 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 775 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress; 776 } 777 } 778 779 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 || 780 sc->aue_ed[AUE_ENDPT_INTR] == 0) { 781 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev)); 782 USB_ATTACH_ERROR_RETURN; 783 } 784 785 786 s = splnet(); 787 788 /* Reset the adapter. */ 789 aue_reset(sc); 790 791 /* 792 * Get station address from the EEPROM. 793 */ 794 aue_read_mac(sc, eaddr); 795 796 /* 797 * A Pegasus chip was detected. Inform the world. 798 */ 799 ifp = GET_IFP(sc); 800 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev), 801 ether_sprintf(eaddr)); 802 803 /* Initialize interface info.*/ 804 ifp->if_softc = sc; 805 ifp->if_mtu = ETHERMTU; 806 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 807 ifp->if_ioctl = aue_ioctl; 808 ifp->if_start = aue_start; 809 ifp->if_watchdog = aue_watchdog; 810 #if defined(__OpenBSD__) 811 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 812 #endif 813 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ); 814 815 IFQ_SET_READY(&ifp->if_snd); 816 817 /* Initialize MII/media info. */ 818 mii = &sc->aue_mii; 819 mii->mii_ifp = ifp; 820 mii->mii_readreg = aue_miibus_readreg; 821 mii->mii_writereg = aue_miibus_writereg; 822 mii->mii_statchg = aue_miibus_statchg; 823 mii->mii_flags = MIIF_AUTOTSLEEP; 824 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts); 825 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 826 if (LIST_FIRST(&mii->mii_phys) == NULL) { 827 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 828 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 829 } else 830 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 831 832 /* Attach the interface. */ 833 if_attach(ifp); 834 Ether_ifattach(ifp, eaddr); 835 #if NRND > 0 836 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev), 837 RND_TYPE_NET, 0); 838 #endif 839 840 usb_callout_init(sc->aue_stat_ch); 841 842 sc->aue_attached = 1; 843 splx(s); 844 845 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, 846 USBDEV(sc->aue_dev)); 847 848 USB_ATTACH_SUCCESS_RETURN; 849 } 850 851 USB_DETACH(aue) 852 { 853 USB_DETACH_START(aue, sc); 854 struct ifnet *ifp = GET_IFP(sc); 855 int s; 856 857 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 858 859 if (!sc->aue_attached) { 860 /* Detached before attached finished, so just bail out. */ 861 return (0); 862 } 863 864 usb_uncallout(sc->aue_stat_ch, aue_tick, sc); 865 /* 866 * Remove any pending tasks. They cannot be executing because they run 867 * in the same thread as detach. 868 */ 869 usb_rem_task(sc->aue_udev, &sc->aue_tick_task); 870 usb_rem_task(sc->aue_udev, &sc->aue_stop_task); 871 872 s = splusb(); 873 874 if (ifp->if_flags & IFF_RUNNING) 875 aue_stop(sc); 876 877 #if defined(__NetBSD__) 878 #if NRND > 0 879 rnd_detach_source(&sc->rnd_source); 880 #endif 881 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY); 882 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY); 883 ether_ifdetach(ifp); 884 #endif /* __NetBSD__ */ 885 886 if_detach(ifp); 887 888 #ifdef DIAGNOSTIC 889 if (sc->aue_ep[AUE_ENDPT_TX] != NULL || 890 sc->aue_ep[AUE_ENDPT_RX] != NULL || 891 sc->aue_ep[AUE_ENDPT_INTR] != NULL) 892 printf("%s: detach has active endpoints\n", 893 USBDEVNAME(sc->aue_dev)); 894 #endif 895 896 sc->aue_attached = 0; 897 898 if (--sc->aue_refcnt >= 0) { 899 /* Wait for processes to go away. */ 900 usb_detach_wait(USBDEV(sc->aue_dev)); 901 } 902 splx(s); 903 904 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, 905 USBDEV(sc->aue_dev)); 906 907 return (0); 908 } 909 910 int 911 aue_activate(device_ptr_t self, enum devact act) 912 { 913 struct aue_softc *sc = (struct aue_softc *)self; 914 915 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 916 917 switch (act) { 918 case DVACT_ACTIVATE: 919 return (EOPNOTSUPP); 920 break; 921 922 case DVACT_DEACTIVATE: 923 if_deactivate(&sc->aue_ec.ec_if); 924 sc->aue_dying = 1; 925 break; 926 } 927 return (0); 928 } 929 930 /* 931 * Initialize an RX descriptor and attach an MBUF cluster. 932 */ 933 Static int 934 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m) 935 { 936 struct mbuf *m_new = NULL; 937 938 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 939 940 if (m == NULL) { 941 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 942 if (m_new == NULL) { 943 printf("%s: no memory for rx list " 944 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev)); 945 return (ENOBUFS); 946 } 947 948 MCLGET(m_new, M_DONTWAIT); 949 if (!(m_new->m_flags & M_EXT)) { 950 printf("%s: no memory for rx list " 951 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev)); 952 m_freem(m_new); 953 return (ENOBUFS); 954 } 955 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 956 } else { 957 m_new = m; 958 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 959 m_new->m_data = m_new->m_ext.ext_buf; 960 } 961 962 m_adj(m_new, ETHER_ALIGN); 963 c->aue_mbuf = m_new; 964 965 return (0); 966 } 967 968 Static int 969 aue_rx_list_init(struct aue_softc *sc) 970 { 971 struct aue_cdata *cd; 972 struct aue_chain *c; 973 int i; 974 975 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 976 977 cd = &sc->aue_cdata; 978 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 979 c = &cd->aue_rx_chain[i]; 980 c->aue_sc = sc; 981 c->aue_idx = i; 982 if (aue_newbuf(sc, c, NULL) == ENOBUFS) 983 return (ENOBUFS); 984 if (c->aue_xfer == NULL) { 985 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); 986 if (c->aue_xfer == NULL) 987 return (ENOBUFS); 988 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ); 989 if (c->aue_buf == NULL) 990 return (ENOBUFS); /* XXX free xfer */ 991 } 992 } 993 994 return (0); 995 } 996 997 Static int 998 aue_tx_list_init(struct aue_softc *sc) 999 { 1000 struct aue_cdata *cd; 1001 struct aue_chain *c; 1002 int i; 1003 1004 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1005 1006 cd = &sc->aue_cdata; 1007 for (i = 0; i < AUE_TX_LIST_CNT; i++) { 1008 c = &cd->aue_tx_chain[i]; 1009 c->aue_sc = sc; 1010 c->aue_idx = i; 1011 c->aue_mbuf = NULL; 1012 if (c->aue_xfer == NULL) { 1013 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); 1014 if (c->aue_xfer == NULL) 1015 return (ENOBUFS); 1016 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ); 1017 if (c->aue_buf == NULL) 1018 return (ENOBUFS); 1019 } 1020 } 1021 1022 return (0); 1023 } 1024 1025 Static void 1026 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1027 { 1028 struct aue_softc *sc = priv; 1029 struct ifnet *ifp = GET_IFP(sc); 1030 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf; 1031 1032 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1033 1034 if (sc->aue_dying) 1035 return; 1036 1037 if (!(ifp->if_flags & IFF_RUNNING)) 1038 return; 1039 1040 if (status != USBD_NORMAL_COMPLETION) { 1041 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1042 return; 1043 } 1044 sc->aue_intr_errs++; 1045 if (usbd_ratecheck(&sc->aue_rx_notice)) { 1046 printf("%s: %u usb errors on intr: %s\n", 1047 USBDEVNAME(sc->aue_dev), sc->aue_intr_errs, 1048 usbd_errstr(status)); 1049 sc->aue_intr_errs = 0; 1050 } 1051 if (status == USBD_STALLED) 1052 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]); 1053 return; 1054 } 1055 1056 if (p->aue_txstat0) 1057 ifp->if_oerrors++; 1058 1059 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL)) 1060 ifp->if_collisions++; 1061 } 1062 1063 /* 1064 * A frame has been uploaded: pass the resulting mbuf chain up to 1065 * the higher level protocols. 1066 */ 1067 Static void 1068 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1069 { 1070 struct aue_chain *c = priv; 1071 struct aue_softc *sc = c->aue_sc; 1072 struct ifnet *ifp = GET_IFP(sc); 1073 struct mbuf *m; 1074 u_int32_t total_len; 1075 struct aue_rxpkt r; 1076 int s; 1077 1078 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1079 1080 if (sc->aue_dying) 1081 return; 1082 1083 if (!(ifp->if_flags & IFF_RUNNING)) 1084 return; 1085 1086 if (status != USBD_NORMAL_COMPLETION) { 1087 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1088 return; 1089 sc->aue_rx_errs++; 1090 if (usbd_ratecheck(&sc->aue_rx_notice)) { 1091 printf("%s: %u usb errors on rx: %s\n", 1092 USBDEVNAME(sc->aue_dev), sc->aue_rx_errs, 1093 usbd_errstr(status)); 1094 sc->aue_rx_errs = 0; 1095 } 1096 if (status == USBD_STALLED) 1097 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]); 1098 goto done; 1099 } 1100 1101 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1102 1103 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len); 1104 1105 if (total_len <= 4 + ETHER_CRC_LEN) { 1106 ifp->if_ierrors++; 1107 goto done; 1108 } 1109 1110 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r)); 1111 1112 /* Turn off all the non-error bits in the rx status word. */ 1113 r.aue_rxstat &= AUE_RXSTAT_MASK; 1114 if (r.aue_rxstat) { 1115 ifp->if_ierrors++; 1116 goto done; 1117 } 1118 1119 /* No errors; receive the packet. */ 1120 m = c->aue_mbuf; 1121 total_len -= ETHER_CRC_LEN + 4; 1122 m->m_pkthdr.len = m->m_len = total_len; 1123 ifp->if_ipackets++; 1124 1125 m->m_pkthdr.rcvif = ifp; 1126 1127 s = splnet(); 1128 1129 /* XXX ugly */ 1130 if (aue_newbuf(sc, c, NULL) == ENOBUFS) { 1131 ifp->if_ierrors++; 1132 goto done1; 1133 } 1134 1135 #if NBPFILTER > 0 1136 /* 1137 * Handle BPF listeners. Let the BPF user see the packet, but 1138 * don't pass it up to the ether_input() layer unless it's 1139 * a broadcast packet, multicast packet, matches our ethernet 1140 * address or the interface is in promiscuous mode. 1141 */ 1142 if (ifp->if_bpf) 1143 BPF_MTAP(ifp, m); 1144 #endif 1145 1146 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev), 1147 __FUNCTION__, m->m_len)); 1148 IF_INPUT(ifp, m); 1149 done1: 1150 splx(s); 1151 1152 done: 1153 1154 /* Setup new transfer. */ 1155 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX], 1156 c, c->aue_buf, AUE_BUFSZ, 1157 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1158 USBD_NO_TIMEOUT, aue_rxeof); 1159 usbd_transfer(xfer); 1160 1161 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev), 1162 __FUNCTION__)); 1163 } 1164 1165 /* 1166 * A frame was downloaded to the chip. It's safe for us to clean up 1167 * the list buffers. 1168 */ 1169 1170 Static void 1171 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1172 { 1173 struct aue_chain *c = priv; 1174 struct aue_softc *sc = c->aue_sc; 1175 struct ifnet *ifp = GET_IFP(sc); 1176 int s; 1177 1178 if (sc->aue_dying) 1179 return; 1180 1181 s = splnet(); 1182 1183 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev), 1184 __FUNCTION__, status)); 1185 1186 ifp->if_timer = 0; 1187 ifp->if_flags &= ~IFF_OACTIVE; 1188 1189 if (status != USBD_NORMAL_COMPLETION) { 1190 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1191 splx(s); 1192 return; 1193 } 1194 ifp->if_oerrors++; 1195 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev), 1196 usbd_errstr(status)); 1197 if (status == USBD_STALLED) 1198 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]); 1199 splx(s); 1200 return; 1201 } 1202 1203 ifp->if_opackets++; 1204 1205 m_freem(c->aue_mbuf); 1206 c->aue_mbuf = NULL; 1207 1208 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1209 aue_start(ifp); 1210 1211 splx(s); 1212 } 1213 1214 Static void 1215 aue_tick(void *xsc) 1216 { 1217 struct aue_softc *sc = xsc; 1218 1219 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1220 1221 if (sc == NULL) 1222 return; 1223 1224 if (sc->aue_dying) 1225 return; 1226 1227 /* Perform periodic stuff in process context. */ 1228 usb_add_task(sc->aue_udev, &sc->aue_tick_task); 1229 } 1230 1231 Static void 1232 aue_tick_task(void *xsc) 1233 { 1234 struct aue_softc *sc = xsc; 1235 struct ifnet *ifp; 1236 struct mii_data *mii; 1237 int s; 1238 1239 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1240 1241 if (sc->aue_dying) 1242 return; 1243 1244 ifp = GET_IFP(sc); 1245 mii = GET_MII(sc); 1246 if (mii == NULL) 1247 return; 1248 1249 s = splnet(); 1250 1251 mii_tick(mii); 1252 if (!sc->aue_link) { 1253 mii_pollstat(mii); /* XXX FreeBSD has removed this call */ 1254 if (mii->mii_media_status & IFM_ACTIVE && 1255 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1256 DPRINTFN(2,("%s: %s: got link\n", 1257 USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1258 sc->aue_link++; 1259 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1260 aue_start(ifp); 1261 } 1262 } 1263 1264 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc); 1265 1266 splx(s); 1267 } 1268 1269 Static int 1270 aue_send(struct aue_softc *sc, struct mbuf *m, int idx) 1271 { 1272 int total_len; 1273 struct aue_chain *c; 1274 usbd_status err; 1275 1276 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1277 1278 c = &sc->aue_cdata.aue_tx_chain[idx]; 1279 1280 /* 1281 * Copy the mbuf data into a contiguous buffer, leaving two 1282 * bytes at the beginning to hold the frame length. 1283 */ 1284 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2); 1285 c->aue_mbuf = m; 1286 1287 /* 1288 * The ADMtek documentation says that the packet length is 1289 * supposed to be specified in the first two bytes of the 1290 * transfer, however it actually seems to ignore this info 1291 * and base the frame size on the bulk transfer length. 1292 */ 1293 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len; 1294 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); 1295 total_len = m->m_pkthdr.len + 2; 1296 1297 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX], 1298 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1299 AUE_TX_TIMEOUT, aue_txeof); 1300 1301 /* Transmit */ 1302 err = usbd_transfer(c->aue_xfer); 1303 if (err != USBD_IN_PROGRESS) { 1304 printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev), 1305 usbd_errstr(err)); 1306 /* Stop the interface from process context. */ 1307 usb_add_task(sc->aue_udev, &sc->aue_stop_task); 1308 return (EIO); 1309 } 1310 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev), 1311 __FUNCTION__, total_len)); 1312 1313 sc->aue_cdata.aue_tx_cnt++; 1314 1315 return (0); 1316 } 1317 1318 Static void 1319 aue_start(struct ifnet *ifp) 1320 { 1321 struct aue_softc *sc = ifp->if_softc; 1322 struct mbuf *m_head = NULL; 1323 1324 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev), 1325 __FUNCTION__, sc->aue_link)); 1326 1327 if (sc->aue_dying) 1328 return; 1329 1330 if (!sc->aue_link) 1331 return; 1332 1333 if (ifp->if_flags & IFF_OACTIVE) 1334 return; 1335 1336 IFQ_POLL(&ifp->if_snd, m_head); 1337 if (m_head == NULL) 1338 return; 1339 1340 if (aue_send(sc, m_head, 0)) { 1341 ifp->if_flags |= IFF_OACTIVE; 1342 return; 1343 } 1344 1345 IFQ_DEQUEUE(&ifp->if_snd, m_head); 1346 1347 #if NBPFILTER > 0 1348 /* 1349 * If there's a BPF listener, bounce a copy of this frame 1350 * to him. 1351 */ 1352 if (ifp->if_bpf) 1353 BPF_MTAP(ifp, m_head); 1354 #endif 1355 1356 ifp->if_flags |= IFF_OACTIVE; 1357 1358 /* 1359 * Set a timeout in case the chip goes out to lunch. 1360 */ 1361 ifp->if_timer = 5; 1362 } 1363 1364 Static void 1365 aue_init(void *xsc) 1366 { 1367 struct aue_softc *sc = xsc; 1368 struct ifnet *ifp = GET_IFP(sc); 1369 struct mii_data *mii = GET_MII(sc); 1370 int i, s; 1371 u_char *eaddr; 1372 1373 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1374 1375 if (sc->aue_dying) 1376 return; 1377 1378 if (ifp->if_flags & IFF_RUNNING) 1379 return; 1380 1381 s = splnet(); 1382 1383 /* 1384 * Cancel pending I/O and free all RX/TX buffers. 1385 */ 1386 aue_reset(sc); 1387 1388 #if defined(__OpenBSD__) 1389 eaddr = sc->arpcom.ac_enaddr; 1390 #elif defined(__NetBSD__) 1391 eaddr = LLADDR(ifp->if_sadl); 1392 #endif /* defined(__NetBSD__) */ 1393 for (i = 0; i < ETHER_ADDR_LEN; i++) 1394 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]); 1395 1396 /* If we want promiscuous mode, set the allframes bit. */ 1397 if (ifp->if_flags & IFF_PROMISC) 1398 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1399 else 1400 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1401 1402 /* Init TX ring. */ 1403 if (aue_tx_list_init(sc) == ENOBUFS) { 1404 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev)); 1405 splx(s); 1406 return; 1407 } 1408 1409 /* Init RX ring. */ 1410 if (aue_rx_list_init(sc) == ENOBUFS) { 1411 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev)); 1412 splx(s); 1413 return; 1414 } 1415 1416 /* Load the multicast filter. */ 1417 aue_setmulti(sc); 1418 1419 /* Enable RX and TX */ 1420 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB); 1421 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB); 1422 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR); 1423 1424 mii_mediachg(mii); 1425 1426 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) { 1427 if (aue_openpipes(sc)) { 1428 splx(s); 1429 return; 1430 } 1431 } 1432 1433 ifp->if_flags |= IFF_RUNNING; 1434 ifp->if_flags &= ~IFF_OACTIVE; 1435 1436 splx(s); 1437 1438 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc); 1439 } 1440 1441 Static int 1442 aue_openpipes(struct aue_softc *sc) 1443 { 1444 struct aue_chain *c; 1445 usbd_status err; 1446 int i; 1447 1448 /* Open RX and TX pipes. */ 1449 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX], 1450 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]); 1451 if (err) { 1452 printf("%s: open rx pipe failed: %s\n", 1453 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1454 return (EIO); 1455 } 1456 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX], 1457 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]); 1458 if (err) { 1459 printf("%s: open tx pipe failed: %s\n", 1460 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1461 return (EIO); 1462 } 1463 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR], 1464 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc, 1465 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr, 1466 AUE_INTR_INTERVAL); 1467 if (err) { 1468 printf("%s: open intr pipe failed: %s\n", 1469 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1470 return (EIO); 1471 } 1472 1473 /* Start up the receive pipe. */ 1474 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 1475 c = &sc->aue_cdata.aue_rx_chain[i]; 1476 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX], 1477 c, c->aue_buf, AUE_BUFSZ, 1478 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1479 aue_rxeof); 1480 (void)usbd_transfer(c->aue_xfer); /* XXX */ 1481 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev), 1482 __FUNCTION__)); 1483 1484 } 1485 return (0); 1486 } 1487 1488 /* 1489 * Set media options. 1490 */ 1491 Static int 1492 aue_ifmedia_upd(struct ifnet *ifp) 1493 { 1494 struct aue_softc *sc = ifp->if_softc; 1495 struct mii_data *mii = GET_MII(sc); 1496 1497 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1498 1499 if (sc->aue_dying) 1500 return (0); 1501 1502 sc->aue_link = 0; 1503 if (mii->mii_instance) { 1504 struct mii_softc *miisc; 1505 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 1506 miisc = LIST_NEXT(miisc, mii_list)) 1507 mii_phy_reset(miisc); 1508 } 1509 mii_mediachg(mii); 1510 1511 return (0); 1512 } 1513 1514 /* 1515 * Report current media status. 1516 */ 1517 Static void 1518 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 1519 { 1520 struct aue_softc *sc = ifp->if_softc; 1521 struct mii_data *mii = GET_MII(sc); 1522 1523 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1524 1525 mii_pollstat(mii); 1526 ifmr->ifm_active = mii->mii_media_active; 1527 ifmr->ifm_status = mii->mii_media_status; 1528 } 1529 1530 Static int 1531 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1532 { 1533 struct aue_softc *sc = ifp->if_softc; 1534 struct ifaddr *ifa = (struct ifaddr *)data; 1535 struct ifreq *ifr = (struct ifreq *)data; 1536 struct mii_data *mii; 1537 int s, error = 0; 1538 1539 if (sc->aue_dying) 1540 return (EIO); 1541 1542 s = splnet(); 1543 1544 switch(command) { 1545 case SIOCSIFADDR: 1546 ifp->if_flags |= IFF_UP; 1547 aue_init(sc); 1548 1549 switch (ifa->ifa_addr->sa_family) { 1550 #ifdef INET 1551 case AF_INET: 1552 #if defined(__NetBSD__) 1553 arp_ifinit(ifp, ifa); 1554 #else 1555 arp_ifinit(&sc->arpcom, ifa); 1556 #endif 1557 break; 1558 #endif /* INET */ 1559 #ifdef NS 1560 case AF_NS: 1561 { 1562 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 1563 1564 if (ns_nullhost(*ina)) 1565 ina->x_host = *(union ns_host *) 1566 LLADDR(ifp->if_sadl); 1567 else 1568 memcpy(LLADDR(ifp->if_sadl), 1569 ina->x_host.c_host, 1570 ifp->if_addrlen); 1571 break; 1572 } 1573 #endif /* NS */ 1574 } 1575 break; 1576 1577 case SIOCSIFMTU: 1578 if (ifr->ifr_mtu > ETHERMTU) 1579 error = EINVAL; 1580 else 1581 ifp->if_mtu = ifr->ifr_mtu; 1582 break; 1583 1584 case SIOCSIFFLAGS: 1585 if (ifp->if_flags & IFF_UP) { 1586 if (ifp->if_flags & IFF_RUNNING && 1587 ifp->if_flags & IFF_PROMISC && 1588 !(sc->aue_if_flags & IFF_PROMISC)) { 1589 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1590 } else if (ifp->if_flags & IFF_RUNNING && 1591 !(ifp->if_flags & IFF_PROMISC) && 1592 sc->aue_if_flags & IFF_PROMISC) { 1593 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1594 } else if (!(ifp->if_flags & IFF_RUNNING)) 1595 aue_init(sc); 1596 } else { 1597 if (ifp->if_flags & IFF_RUNNING) 1598 aue_stop(sc); 1599 } 1600 sc->aue_if_flags = ifp->if_flags; 1601 error = 0; 1602 break; 1603 case SIOCADDMULTI: 1604 case SIOCDELMULTI: 1605 error = (command == SIOCADDMULTI) ? 1606 ether_addmulti(ifr, &sc->aue_ec) : 1607 ether_delmulti(ifr, &sc->aue_ec); 1608 if (error == ENETRESET) { 1609 aue_init(sc); 1610 } 1611 aue_setmulti(sc); 1612 error = 0; 1613 break; 1614 case SIOCGIFMEDIA: 1615 case SIOCSIFMEDIA: 1616 mii = GET_MII(sc); 1617 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1618 break; 1619 default: 1620 error = EINVAL; 1621 break; 1622 } 1623 1624 splx(s); 1625 1626 return (error); 1627 } 1628 1629 Static void 1630 aue_watchdog(struct ifnet *ifp) 1631 { 1632 struct aue_softc *sc = ifp->if_softc; 1633 struct aue_chain *c; 1634 usbd_status stat; 1635 int s; 1636 1637 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1638 1639 ifp->if_oerrors++; 1640 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev)); 1641 1642 s = splusb(); 1643 c = &sc->aue_cdata.aue_tx_chain[0]; 1644 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat); 1645 aue_txeof(c->aue_xfer, c, stat); 1646 1647 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1648 aue_start(ifp); 1649 splx(s); 1650 } 1651 1652 /* 1653 * Stop the adapter and free any mbufs allocated to the 1654 * RX and TX lists. 1655 */ 1656 Static void 1657 aue_stop(struct aue_softc *sc) 1658 { 1659 usbd_status err; 1660 struct ifnet *ifp; 1661 int i; 1662 1663 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1664 1665 ifp = GET_IFP(sc); 1666 ifp->if_timer = 0; 1667 1668 aue_csr_write_1(sc, AUE_CTL0, 0); 1669 aue_csr_write_1(sc, AUE_CTL1, 0); 1670 aue_reset(sc); 1671 usb_uncallout(sc->aue_stat_ch, aue_tick, sc); 1672 1673 /* Stop transfers. */ 1674 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) { 1675 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]); 1676 if (err) { 1677 printf("%s: abort rx pipe failed: %s\n", 1678 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1679 } 1680 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]); 1681 if (err) { 1682 printf("%s: close rx pipe failed: %s\n", 1683 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1684 } 1685 sc->aue_ep[AUE_ENDPT_RX] = NULL; 1686 } 1687 1688 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) { 1689 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]); 1690 if (err) { 1691 printf("%s: abort tx pipe failed: %s\n", 1692 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1693 } 1694 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]); 1695 if (err) { 1696 printf("%s: close tx pipe failed: %s\n", 1697 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1698 } 1699 sc->aue_ep[AUE_ENDPT_TX] = NULL; 1700 } 1701 1702 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) { 1703 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]); 1704 if (err) { 1705 printf("%s: abort intr pipe failed: %s\n", 1706 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1707 } 1708 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]); 1709 if (err) { 1710 printf("%s: close intr pipe failed: %s\n", 1711 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1712 } 1713 sc->aue_ep[AUE_ENDPT_INTR] = NULL; 1714 } 1715 1716 /* Free RX resources. */ 1717 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 1718 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) { 1719 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf); 1720 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL; 1721 } 1722 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) { 1723 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer); 1724 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL; 1725 } 1726 } 1727 1728 /* Free TX resources. */ 1729 for (i = 0; i < AUE_TX_LIST_CNT; i++) { 1730 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) { 1731 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf); 1732 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL; 1733 } 1734 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) { 1735 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer); 1736 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL; 1737 } 1738 } 1739 1740 sc->aue_link = 0; 1741 1742 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1743 } 1744