1 /* $OpenBSD: if_url.c,v 1.63 2011/07/03 15:47:17 matthew Exp $ */ 2 /* $NetBSD: if_url.c,v 1.6 2002/09/29 10:19:21 martin Exp $ */ 3 /* 4 * Copyright (c) 2001, 2002 5 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the author nor the names of any co-contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 /* 34 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at 35 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf 36 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf 37 */ 38 39 /* 40 * TODO: 41 * Interrupt Endpoint support 42 * External PHYs 43 */ 44 45 #include "bpfilter.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/rwlock.h> 50 #include <sys/mbuf.h> 51 #include <sys/kernel.h> 52 #include <sys/proc.h> 53 #include <sys/socket.h> 54 55 #include <sys/device.h> 56 57 #include <net/if.h> 58 #include <net/if_arp.h> 59 #include <net/if_dl.h> 60 #include <net/if_media.h> 61 62 #if NBPFILTER > 0 63 #include <net/bpf.h> 64 #endif 65 66 #ifdef INET 67 #include <netinet/in.h> 68 #include <netinet/in_systm.h> 69 #include <netinet/in_var.h> 70 #include <netinet/ip.h> 71 #include <netinet/if_ether.h> 72 #endif 73 74 #include <dev/mii/mii.h> 75 #include <dev/mii/miivar.h> 76 #include <dev/mii/urlphyreg.h> 77 78 #include <dev/usb/usb.h> 79 #include <dev/usb/usbdi.h> 80 #include <dev/usb/usbdi_util.h> 81 #include <dev/usb/usbdevs.h> 82 83 #include <dev/usb/if_urlreg.h> 84 85 86 /* Function declarations */ 87 int url_match(struct device *, void *, void *); 88 void url_attach(struct device *, struct device *, void *); 89 int url_detach(struct device *, int); 90 int url_activate(struct device *, int); 91 92 struct cfdriver url_cd = { 93 NULL, "url", DV_IFNET 94 }; 95 96 const struct cfattach url_ca = { 97 sizeof(struct url_softc), 98 url_match, 99 url_attach, 100 url_detach, 101 url_activate, 102 }; 103 104 int url_openpipes(struct url_softc *); 105 int url_rx_list_init(struct url_softc *); 106 int url_tx_list_init(struct url_softc *); 107 int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *); 108 void url_start(struct ifnet *); 109 int url_send(struct url_softc *, struct mbuf *, int); 110 void url_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 111 void url_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 112 void url_tick(void *); 113 void url_tick_task(void *); 114 int url_ioctl(struct ifnet *, u_long, caddr_t); 115 void url_stop_task(struct url_softc *); 116 void url_stop(struct ifnet *, int); 117 void url_watchdog(struct ifnet *); 118 int url_ifmedia_change(struct ifnet *); 119 void url_ifmedia_status(struct ifnet *, struct ifmediareq *); 120 void url_lock_mii(struct url_softc *); 121 void url_unlock_mii(struct url_softc *); 122 int url_int_miibus_readreg(struct device *, int, int); 123 void url_int_miibus_writereg(struct device *, int, int, int); 124 void url_miibus_statchg(struct device *); 125 int url_init(struct ifnet *); 126 void url_setmulti(struct url_softc *); 127 void url_reset(struct url_softc *); 128 129 int url_csr_read_1(struct url_softc *, int); 130 int url_csr_read_2(struct url_softc *, int); 131 int url_csr_write_1(struct url_softc *, int, int); 132 int url_csr_write_2(struct url_softc *, int, int); 133 int url_csr_write_4(struct url_softc *, int, int); 134 int url_mem(struct url_softc *, int, int, void *, int); 135 136 /* Macros */ 137 #ifdef URL_DEBUG 138 #define DPRINTF(x) do { if (urldebug) printf x; } while (0) 139 #define DPRINTFN(n,x) do { if (urldebug >= (n)) printf x; } while (0) 140 int urldebug = 0; 141 #else 142 #define DPRINTF(x) 143 #define DPRINTFN(n,x) 144 #endif 145 146 #define URL_SETBIT(sc, reg, x) \ 147 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x)) 148 149 #define URL_SETBIT2(sc, reg, x) \ 150 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x)) 151 152 #define URL_CLRBIT(sc, reg, x) \ 153 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x)) 154 155 #define URL_CLRBIT2(sc, reg, x) \ 156 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x)) 157 158 static const struct url_type { 159 struct usb_devno url_dev; 160 u_int16_t url_flags; 161 #define URL_EXT_PHY 0x0001 162 } url_devs [] = { 163 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0}, 164 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RTL8151}, 0}, 165 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0}, 166 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0}, 167 {{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0}, 168 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150}, 0}, 169 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8151}, 0}, 170 {{ USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_PRESTIGE}, 0} 171 }; 172 #define url_lookup(v, p) ((struct url_type *)usb_lookup(url_devs, v, p)) 173 174 175 /* Probe */ 176 int 177 url_match(struct device *parent, void *match, void *aux) 178 { 179 struct usb_attach_arg *uaa = aux; 180 181 if (uaa->iface != NULL) 182 return (UMATCH_NONE); 183 184 return (url_lookup(uaa->vendor, uaa->product) != NULL ? 185 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 186 } 187 /* Attach */ 188 void 189 url_attach(struct device *parent, struct device *self, void *aux) 190 { 191 struct url_softc *sc = (struct url_softc *)self; 192 struct usb_attach_arg *uaa = aux; 193 usbd_device_handle dev = uaa->device; 194 usbd_interface_handle iface; 195 usbd_status err; 196 usb_interface_descriptor_t *id; 197 usb_endpoint_descriptor_t *ed; 198 char *devname = sc->sc_dev.dv_xname; 199 struct ifnet *ifp; 200 struct mii_data *mii; 201 u_char eaddr[ETHER_ADDR_LEN]; 202 int i, s; 203 204 sc->sc_udev = dev; 205 206 /* Move the device into the configured state. */ 207 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1); 208 if (err) { 209 printf("%s: setting config no failed\n", devname); 210 goto bad; 211 } 212 213 usb_init_task(&sc->sc_tick_task, url_tick_task, sc, 214 USB_TASK_TYPE_GENERIC); 215 rw_init(&sc->sc_mii_lock, "urlmii"); 216 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) url_stop_task, sc, 217 USB_TASK_TYPE_GENERIC); 218 219 /* get control interface */ 220 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface); 221 if (err) { 222 printf("%s: failed to get interface, err=%s\n", devname, 223 usbd_errstr(err)); 224 goto bad; 225 } 226 227 sc->sc_ctl_iface = iface; 228 sc->sc_flags = url_lookup(uaa->vendor, uaa->product)->url_flags; 229 230 /* get interface descriptor */ 231 id = usbd_get_interface_descriptor(sc->sc_ctl_iface); 232 233 /* find endpoints */ 234 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1; 235 for (i = 0; i < id->bNumEndpoints; i++) { 236 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i); 237 if (ed == NULL) { 238 printf("%s: couldn't get endpoint %d\n", devname, i); 239 goto bad; 240 } 241 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 242 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 243 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */ 244 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 245 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) 246 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */ 247 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT && 248 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 249 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */ 250 } 251 252 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 || 253 sc->sc_intrin_no == -1) { 254 printf("%s: missing endpoint\n", devname); 255 goto bad; 256 } 257 258 s = splnet(); 259 260 /* reset the adapter */ 261 url_reset(sc); 262 263 /* Get Ethernet Address */ 264 err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr, 265 ETHER_ADDR_LEN); 266 if (err) { 267 printf("%s: read MAC address failed\n", devname); 268 splx(s); 269 goto bad; 270 } 271 272 /* Print Ethernet Address */ 273 printf("%s: address %s\n", devname, ether_sprintf(eaddr)); 274 275 bcopy(eaddr, (char *)&sc->sc_ac.ac_enaddr, ETHER_ADDR_LEN); 276 /* initialize interface information */ 277 ifp = GET_IFP(sc); 278 ifp->if_softc = sc; 279 strlcpy(ifp->if_xname, devname, IFNAMSIZ); 280 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 281 ifp->if_start = url_start; 282 ifp->if_ioctl = url_ioctl; 283 ifp->if_watchdog = url_watchdog; 284 285 IFQ_SET_READY(&ifp->if_snd); 286 287 ifp->if_capabilities = IFCAP_VLAN_MTU; 288 289 /* 290 * Do ifmedia setup. 291 */ 292 mii = &sc->sc_mii; 293 mii->mii_ifp = ifp; 294 mii->mii_readreg = url_int_miibus_readreg; 295 mii->mii_writereg = url_int_miibus_writereg; 296 #if 0 297 if (sc->sc_flags & URL_EXT_PHY) { 298 mii->mii_readreg = url_ext_miibus_readreg; 299 mii->mii_writereg = url_ext_miibus_writereg; 300 } 301 #endif 302 mii->mii_statchg = url_miibus_statchg; 303 mii->mii_flags = MIIF_AUTOTSLEEP; 304 ifmedia_init(&mii->mii_media, 0, 305 url_ifmedia_change, url_ifmedia_status); 306 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 307 if (LIST_FIRST(&mii->mii_phys) == NULL) { 308 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 309 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 310 } else 311 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 312 313 /* attach the interface */ 314 if_attach(ifp); 315 ether_ifattach(ifp); 316 317 timeout_set(&sc->sc_stat_ch, url_tick, sc); 318 319 splx(s); 320 321 return; 322 323 bad: 324 usbd_deactivate(sc->sc_udev); 325 } 326 327 /* detach */ 328 int 329 url_detach(struct device *self, int flags) 330 { 331 struct url_softc *sc = (struct url_softc *)self; 332 struct ifnet *ifp = GET_IFP(sc); 333 int s; 334 335 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 336 337 if (timeout_initialized(&sc->sc_stat_ch)) 338 timeout_del(&sc->sc_stat_ch); 339 340 /* Remove any pending tasks */ 341 usb_rem_task(sc->sc_udev, &sc->sc_tick_task); 342 usb_rem_task(sc->sc_udev, &sc->sc_stop_task); 343 344 s = splusb(); 345 346 if (--sc->sc_refcnt >= 0) { 347 /* Wait for processes to go away */ 348 usb_detach_wait(&sc->sc_dev); 349 } 350 351 if (ifp->if_flags & IFF_RUNNING) 352 url_stop(GET_IFP(sc), 1); 353 354 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 355 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 356 if (ifp->if_softc != NULL) { 357 ether_ifdetach(ifp); 358 if_detach(ifp); 359 } 360 361 #ifdef DIAGNOSTIC 362 if (sc->sc_pipe_tx != NULL) 363 printf("%s: detach has active tx endpoint.\n", 364 sc->sc_dev.dv_xname); 365 if (sc->sc_pipe_rx != NULL) 366 printf("%s: detach has active rx endpoint.\n", 367 sc->sc_dev.dv_xname); 368 if (sc->sc_pipe_intr != NULL) 369 printf("%s: detach has active intr endpoint.\n", 370 sc->sc_dev.dv_xname); 371 #endif 372 373 splx(s); 374 375 return (0); 376 } 377 378 /* read/write memory */ 379 int 380 url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len) 381 { 382 usb_device_request_t req; 383 usbd_status err; 384 385 if (sc == NULL) 386 return (0); 387 388 DPRINTFN(0x200, 389 ("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 390 391 if (usbd_is_dying(sc->sc_udev)) 392 return (0); 393 394 if (cmd == URL_CMD_READMEM) 395 req.bmRequestType = UT_READ_VENDOR_DEVICE; 396 else 397 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 398 req.bRequest = URL_REQ_MEM; 399 USETW(req.wValue, offset); 400 USETW(req.wIndex, 0x0000); 401 USETW(req.wLength, len); 402 403 sc->sc_refcnt++; 404 err = usbd_do_request(sc->sc_udev, &req, buf); 405 if (--sc->sc_refcnt < 0) 406 usb_detach_wakeup(&sc->sc_dev); 407 if (err) { 408 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n", 409 sc->sc_dev.dv_xname, 410 cmd == URL_CMD_READMEM ? "read" : "write", 411 offset, err)); 412 } 413 414 return (err); 415 } 416 417 /* read 1byte from register */ 418 int 419 url_csr_read_1(struct url_softc *sc, int reg) 420 { 421 u_int8_t val = 0; 422 423 DPRINTFN(0x100, 424 ("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 425 426 return (url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val); 427 } 428 429 /* read 2bytes from register */ 430 int 431 url_csr_read_2(struct url_softc *sc, int reg) 432 { 433 uWord val; 434 435 DPRINTFN(0x100, 436 ("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 437 438 USETW(val, 0); 439 return (url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val)); 440 } 441 442 /* write 1byte to register */ 443 int 444 url_csr_write_1(struct url_softc *sc, int reg, int aval) 445 { 446 u_int8_t val = aval; 447 448 DPRINTFN(0x100, 449 ("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 450 451 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0); 452 } 453 454 /* write 2bytes to register */ 455 int 456 url_csr_write_2(struct url_softc *sc, int reg, int aval) 457 { 458 uWord val; 459 460 DPRINTFN(0x100, 461 ("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 462 463 USETW(val, aval); 464 465 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0); 466 } 467 468 /* write 4bytes to register */ 469 int 470 url_csr_write_4(struct url_softc *sc, int reg, int aval) 471 { 472 uDWord val; 473 474 DPRINTFN(0x100, 475 ("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 476 477 USETDW(val, aval); 478 479 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0); 480 } 481 482 int 483 url_init(struct ifnet *ifp) 484 { 485 struct url_softc *sc = ifp->if_softc; 486 struct mii_data *mii = GET_MII(sc); 487 u_char *eaddr; 488 int i, s; 489 490 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 491 492 s = splnet(); 493 494 /* Cancel pending I/O and free all TX/RX buffers */ 495 url_stop(ifp, 1); 496 497 eaddr = sc->sc_ac.ac_enaddr; 498 for (i = 0; i < ETHER_ADDR_LEN; i++) 499 url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]); 500 501 /* Init transmission control register */ 502 URL_CLRBIT(sc, URL_TCR, 503 URL_TCR_TXRR1 | URL_TCR_TXRR0 | 504 URL_TCR_IFG1 | URL_TCR_IFG0 | 505 URL_TCR_NOCRC); 506 507 /* Init receive control register */ 508 URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD); 509 if (ifp->if_flags & IFF_BROADCAST) 510 URL_SETBIT2(sc, URL_RCR, URL_RCR_AB); 511 else 512 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB); 513 514 /* If we want promiscuous mode, accept all physical frames. */ 515 if (ifp->if_flags & IFF_PROMISC) 516 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 517 else 518 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 519 520 521 /* Initialize transmit ring */ 522 if (url_tx_list_init(sc) == ENOBUFS) { 523 printf("%s: tx list init failed\n", sc->sc_dev.dv_xname); 524 splx(s); 525 return (EIO); 526 } 527 528 /* Initialize receive ring */ 529 if (url_rx_list_init(sc) == ENOBUFS) { 530 printf("%s: rx list init failed\n", sc->sc_dev.dv_xname); 531 splx(s); 532 return (EIO); 533 } 534 535 /* Load the multicast filter */ 536 url_setmulti(sc); 537 538 /* Enable RX and TX */ 539 URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE); 540 541 mii_mediachg(mii); 542 543 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) { 544 if (url_openpipes(sc)) { 545 splx(s); 546 return (EIO); 547 } 548 } 549 550 ifp->if_flags |= IFF_RUNNING; 551 ifp->if_flags &= ~IFF_OACTIVE; 552 553 splx(s); 554 555 timeout_add_sec(&sc->sc_stat_ch, 1); 556 557 return (0); 558 } 559 560 void 561 url_reset(struct url_softc *sc) 562 { 563 int i; 564 565 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 566 567 if (usbd_is_dying(sc->sc_udev)) 568 return; 569 570 URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST); 571 572 for (i = 0; i < URL_TX_TIMEOUT; i++) { 573 if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST)) 574 break; 575 delay(10); /* XXX */ 576 } 577 578 delay(10000); /* XXX */ 579 } 580 581 int 582 url_activate(struct device *self, int act) 583 { 584 struct url_softc *sc = (struct url_softc *)self; 585 586 DPRINTF(("%s: %s: enter, act=%d\n", sc->sc_dev.dv_xname, 587 __func__, act)); 588 589 switch (act) { 590 case DVACT_DEACTIVATE: 591 usbd_deactivate(sc->sc_udev); 592 break; 593 } 594 595 return (0); 596 } 597 598 #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26) 599 600 601 void 602 url_setmulti(struct url_softc *sc) 603 { 604 struct ifnet *ifp; 605 struct ether_multi *enm; 606 struct ether_multistep step; 607 u_int32_t hashes[2] = { 0, 0 }; 608 int h = 0; 609 int mcnt = 0; 610 611 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 612 613 if (usbd_is_dying(sc->sc_udev)) 614 return; 615 616 ifp = GET_IFP(sc); 617 618 if (ifp->if_flags & IFF_PROMISC) { 619 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 620 return; 621 } else if (ifp->if_flags & IFF_ALLMULTI) { 622 allmulti: 623 ifp->if_flags |= IFF_ALLMULTI; 624 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM); 625 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP); 626 return; 627 } 628 629 /* first, zot all the existing hash bits */ 630 url_csr_write_4(sc, URL_MAR0, 0); 631 url_csr_write_4(sc, URL_MAR4, 0); 632 633 /* now program new ones */ 634 ETHER_FIRST_MULTI(step, &sc->sc_ac, enm); 635 while (enm != NULL) { 636 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 637 ETHER_ADDR_LEN) != 0) 638 goto allmulti; 639 640 h = url_calchash(enm->enm_addrlo); 641 if (h < 32) 642 hashes[0] |= (1 << h); 643 else 644 hashes[1] |= (1 << (h -32)); 645 mcnt++; 646 ETHER_NEXT_MULTI(step, enm); 647 } 648 649 ifp->if_flags &= ~IFF_ALLMULTI; 650 651 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 652 653 if (mcnt){ 654 URL_SETBIT2(sc, URL_RCR, URL_RCR_AM); 655 } else { 656 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM); 657 } 658 url_csr_write_4(sc, URL_MAR0, hashes[0]); 659 url_csr_write_4(sc, URL_MAR4, hashes[1]); 660 } 661 662 int 663 url_openpipes(struct url_softc *sc) 664 { 665 struct url_chain *c; 666 usbd_status err; 667 int i; 668 int error = 0; 669 670 if (usbd_is_dying(sc->sc_udev)) 671 return (EIO); 672 673 sc->sc_refcnt++; 674 675 /* Open RX pipe */ 676 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no, 677 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx); 678 if (err) { 679 printf("%s: open rx pipe failed: %s\n", 680 sc->sc_dev.dv_xname, usbd_errstr(err)); 681 error = EIO; 682 goto done; 683 } 684 685 /* Open TX pipe */ 686 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no, 687 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx); 688 if (err) { 689 printf("%s: open tx pipe failed: %s\n", 690 sc->sc_dev.dv_xname, usbd_errstr(err)); 691 error = EIO; 692 goto done; 693 } 694 695 #if 0 696 /* XXX: interrupt endpoint is not yet supported */ 697 /* Open Interrupt pipe */ 698 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no, 699 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc, 700 &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN, 701 url_intr, URL_INTR_INTERVAL); 702 if (err) { 703 printf("%s: open intr pipe failed: %s\n", 704 sc->sc_dev.dv_xname, usbd_errstr(err)); 705 error = EIO; 706 goto done; 707 } 708 #endif 709 710 711 /* Start up the receive pipe. */ 712 for (i = 0; i < URL_RX_LIST_CNT; i++) { 713 c = &sc->sc_cdata.url_rx_chain[i]; 714 usbd_setup_xfer(c->url_xfer, sc->sc_pipe_rx, 715 c, c->url_buf, URL_BUFSZ, 716 USBD_SHORT_XFER_OK | USBD_NO_COPY, 717 USBD_NO_TIMEOUT, url_rxeof); 718 (void)usbd_transfer(c->url_xfer); 719 DPRINTF(("%s: %s: start read\n", sc->sc_dev.dv_xname, 720 __func__)); 721 } 722 723 done: 724 if (--sc->sc_refcnt < 0) 725 usb_detach_wakeup(&sc->sc_dev); 726 727 return (error); 728 } 729 730 int 731 url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m) 732 { 733 struct mbuf *m_new = NULL; 734 735 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 736 737 if (m == NULL) { 738 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 739 if (m_new == NULL) { 740 printf("%s: no memory for rx list " 741 "-- packet dropped!\n", sc->sc_dev.dv_xname); 742 return (ENOBUFS); 743 } 744 MCLGET(m_new, M_DONTWAIT); 745 if (!(m_new->m_flags & M_EXT)) { 746 printf("%s: no memory for rx list " 747 "-- packet dropped!\n", sc->sc_dev.dv_xname); 748 m_freem(m_new); 749 return (ENOBUFS); 750 } 751 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 752 } else { 753 m_new = m; 754 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 755 m_new->m_data = m_new->m_ext.ext_buf; 756 } 757 758 m_adj(m_new, ETHER_ALIGN); 759 c->url_mbuf = m_new; 760 761 return (0); 762 } 763 764 765 int 766 url_rx_list_init(struct url_softc *sc) 767 { 768 struct url_cdata *cd; 769 struct url_chain *c; 770 int i; 771 772 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 773 774 cd = &sc->sc_cdata; 775 for (i = 0; i < URL_RX_LIST_CNT; i++) { 776 c = &cd->url_rx_chain[i]; 777 c->url_sc = sc; 778 c->url_idx = i; 779 if (url_newbuf(sc, c, NULL) == ENOBUFS) 780 return (ENOBUFS); 781 if (c->url_xfer == NULL) { 782 c->url_xfer = usbd_alloc_xfer(sc->sc_udev); 783 if (c->url_xfer == NULL) 784 return (ENOBUFS); 785 c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ); 786 if (c->url_buf == NULL) { 787 usbd_free_xfer(c->url_xfer); 788 return (ENOBUFS); 789 } 790 } 791 } 792 793 return (0); 794 } 795 796 int 797 url_tx_list_init(struct url_softc *sc) 798 { 799 struct url_cdata *cd; 800 struct url_chain *c; 801 int i; 802 803 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 804 805 cd = &sc->sc_cdata; 806 for (i = 0; i < URL_TX_LIST_CNT; i++) { 807 c = &cd->url_tx_chain[i]; 808 c->url_sc = sc; 809 c->url_idx = i; 810 c->url_mbuf = NULL; 811 if (c->url_xfer == NULL) { 812 c->url_xfer = usbd_alloc_xfer(sc->sc_udev); 813 if (c->url_xfer == NULL) 814 return (ENOBUFS); 815 c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ); 816 if (c->url_buf == NULL) { 817 usbd_free_xfer(c->url_xfer); 818 return (ENOBUFS); 819 } 820 } 821 } 822 823 return (0); 824 } 825 826 void 827 url_start(struct ifnet *ifp) 828 { 829 struct url_softc *sc = ifp->if_softc; 830 struct mbuf *m_head = NULL; 831 832 DPRINTF(("%s: %s: enter, link=%d\n", sc->sc_dev.dv_xname, 833 __func__, sc->sc_link)); 834 835 if (usbd_is_dying(sc->sc_udev)) 836 return; 837 838 if (!sc->sc_link) 839 return; 840 841 if (ifp->if_flags & IFF_OACTIVE) 842 return; 843 844 IFQ_POLL(&ifp->if_snd, m_head); 845 if (m_head == NULL) 846 return; 847 848 if (url_send(sc, m_head, 0)) { 849 ifp->if_flags |= IFF_OACTIVE; 850 return; 851 } 852 853 IFQ_DEQUEUE(&ifp->if_snd, m_head); 854 855 #if NBPFILTER > 0 856 if (ifp->if_bpf) 857 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT); 858 #endif 859 860 ifp->if_flags |= IFF_OACTIVE; 861 862 /* Set a timeout in case the chip goes out to lunch. */ 863 ifp->if_timer = 5; 864 } 865 866 int 867 url_send(struct url_softc *sc, struct mbuf *m, int idx) 868 { 869 int total_len; 870 struct url_chain *c; 871 usbd_status err; 872 873 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__)); 874 875 c = &sc->sc_cdata.url_tx_chain[idx]; 876 877 /* Copy the mbuf data into a contiguous buffer */ 878 m_copydata(m, 0, m->m_pkthdr.len, c->url_buf); 879 c->url_mbuf = m; 880 total_len = m->m_pkthdr.len; 881 882 if (total_len < URL_MIN_FRAME_LEN) { 883 bzero(c->url_buf + total_len, URL_MIN_FRAME_LEN - total_len); 884 total_len = URL_MIN_FRAME_LEN; 885 } 886 usbd_setup_xfer(c->url_xfer, sc->sc_pipe_tx, c, c->url_buf, total_len, 887 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 888 URL_TX_TIMEOUT, url_txeof); 889 890 /* Transmit */ 891 sc->sc_refcnt++; 892 err = usbd_transfer(c->url_xfer); 893 if (--sc->sc_refcnt < 0) 894 usb_detach_wakeup(&sc->sc_dev); 895 if (err != USBD_IN_PROGRESS) { 896 printf("%s: url_send error=%s\n", sc->sc_dev.dv_xname, 897 usbd_errstr(err)); 898 /* Stop the interface */ 899 usb_add_task(sc->sc_udev, &sc->sc_stop_task); 900 return (EIO); 901 } 902 903 DPRINTF(("%s: %s: send %d bytes\n", sc->sc_dev.dv_xname, 904 __func__, total_len)); 905 906 sc->sc_cdata.url_tx_cnt++; 907 908 return (0); 909 } 910 911 void 912 url_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 913 { 914 struct url_chain *c = priv; 915 struct url_softc *sc = c->url_sc; 916 struct ifnet *ifp = GET_IFP(sc); 917 int s; 918 919 if (usbd_is_dying(sc->sc_udev)) 920 return; 921 922 s = splnet(); 923 924 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 925 926 ifp->if_timer = 0; 927 ifp->if_flags &= ~IFF_OACTIVE; 928 929 if (status != USBD_NORMAL_COMPLETION) { 930 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 931 splx(s); 932 return; 933 } 934 ifp->if_oerrors++; 935 printf("%s: usb error on tx: %s\n", sc->sc_dev.dv_xname, 936 usbd_errstr(status)); 937 if (status == USBD_STALLED) { 938 sc->sc_refcnt++; 939 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx); 940 if (--sc->sc_refcnt < 0) 941 usb_detach_wakeup(&sc->sc_dev); 942 } 943 splx(s); 944 return; 945 } 946 947 ifp->if_opackets++; 948 949 m_freem(c->url_mbuf); 950 c->url_mbuf = NULL; 951 952 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 953 url_start(ifp); 954 955 splx(s); 956 } 957 958 void 959 url_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 960 { 961 struct url_chain *c = priv; 962 struct url_softc *sc = c->url_sc; 963 struct ifnet *ifp = GET_IFP(sc); 964 struct mbuf *m; 965 u_int32_t total_len; 966 url_rxhdr_t rxhdr; 967 int s; 968 969 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__)); 970 971 if (usbd_is_dying(sc->sc_udev)) 972 return; 973 974 if (status != USBD_NORMAL_COMPLETION) { 975 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 976 return; 977 sc->sc_rx_errs++; 978 if (usbd_ratecheck(&sc->sc_rx_notice)) { 979 printf("%s: %u usb errors on rx: %s\n", 980 sc->sc_dev.dv_xname, sc->sc_rx_errs, 981 usbd_errstr(status)); 982 sc->sc_rx_errs = 0; 983 } 984 if (status == USBD_STALLED) { 985 sc->sc_refcnt++; 986 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx); 987 if (--sc->sc_refcnt < 0) 988 usb_detach_wakeup(&sc->sc_dev); 989 } 990 goto done; 991 } 992 993 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 994 995 memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len); 996 997 if (total_len <= ETHER_CRC_LEN) { 998 ifp->if_ierrors++; 999 goto done; 1000 } 1001 1002 memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr)); 1003 1004 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n", 1005 sc->sc_dev.dv_xname, 1006 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK, 1007 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "", 1008 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "", 1009 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "", 1010 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : "")); 1011 1012 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) { 1013 ifp->if_ierrors++; 1014 goto done; 1015 } 1016 1017 ifp->if_ipackets++; 1018 total_len -= ETHER_CRC_LEN; 1019 1020 m = c->url_mbuf; 1021 m->m_pkthdr.len = m->m_len = total_len; 1022 m->m_pkthdr.rcvif = ifp; 1023 1024 s = splnet(); 1025 1026 if (url_newbuf(sc, c, NULL) == ENOBUFS) { 1027 ifp->if_ierrors++; 1028 goto done1; 1029 } 1030 1031 #if NBPFILTER > 0 1032 if (ifp->if_bpf) 1033 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN); 1034 #endif 1035 1036 DPRINTF(("%s: %s: deliver %d\n", sc->sc_dev.dv_xname, 1037 __func__, m->m_len)); 1038 ether_input_mbuf(ifp, m); 1039 1040 done1: 1041 splx(s); 1042 1043 done: 1044 /* Setup new transfer */ 1045 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->url_buf, URL_BUFSZ, 1046 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1047 USBD_NO_TIMEOUT, url_rxeof); 1048 sc->sc_refcnt++; 1049 usbd_transfer(xfer); 1050 if (--sc->sc_refcnt < 0) 1051 usb_detach_wakeup(&sc->sc_dev); 1052 1053 DPRINTF(("%s: %s: start rx\n", sc->sc_dev.dv_xname, __func__)); 1054 } 1055 1056 int 1057 url_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1058 { 1059 struct url_softc *sc = ifp->if_softc; 1060 struct ifaddr *ifa = (struct ifaddr *)data; 1061 struct ifreq *ifr = (struct ifreq *)data; 1062 struct mii_data *mii; 1063 int s, error = 0; 1064 1065 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 1066 1067 if (usbd_is_dying(sc->sc_udev)) 1068 return (EIO); 1069 1070 s = splnet(); 1071 1072 switch (cmd) { 1073 case SIOCSIFADDR: 1074 ifp->if_flags |= IFF_UP; 1075 url_init(ifp); 1076 1077 switch (ifa->ifa_addr->sa_family) { 1078 #ifdef INET 1079 case AF_INET: 1080 arp_ifinit(&sc->sc_ac, ifa); 1081 break; 1082 #endif /* INET */ 1083 } 1084 break; 1085 1086 case SIOCSIFFLAGS: 1087 if (ifp->if_flags & IFF_UP) { 1088 if (ifp->if_flags & IFF_RUNNING && 1089 ifp->if_flags & IFF_PROMISC) { 1090 URL_SETBIT2(sc, URL_RCR, 1091 URL_RCR_AAM|URL_RCR_AAP); 1092 } else if (ifp->if_flags & IFF_RUNNING && 1093 !(ifp->if_flags & IFF_PROMISC)) { 1094 URL_CLRBIT2(sc, URL_RCR, 1095 URL_RCR_AAM|URL_RCR_AAP); 1096 } else if (!(ifp->if_flags & IFF_RUNNING)) 1097 url_init(ifp); 1098 } else { 1099 if (ifp->if_flags & IFF_RUNNING) 1100 url_stop(ifp, 1); 1101 } 1102 error = 0; 1103 break; 1104 1105 case SIOCGIFMEDIA: 1106 case SIOCSIFMEDIA: 1107 mii = GET_MII(sc); 1108 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd); 1109 break; 1110 1111 default: 1112 error = ether_ioctl(ifp, &sc->sc_ac, cmd, data); 1113 } 1114 1115 if (error == ENETRESET) { 1116 if (ifp->if_flags & IFF_RUNNING) 1117 url_setmulti(sc); 1118 error = 0; 1119 } 1120 1121 splx(s); 1122 return (error); 1123 } 1124 1125 void 1126 url_watchdog(struct ifnet *ifp) 1127 { 1128 struct url_softc *sc = ifp->if_softc; 1129 struct url_chain *c; 1130 usbd_status stat; 1131 int s; 1132 1133 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 1134 1135 ifp->if_oerrors++; 1136 printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname); 1137 1138 s = splusb(); 1139 c = &sc->sc_cdata.url_tx_chain[0]; 1140 usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat); 1141 url_txeof(c->url_xfer, c, stat); 1142 1143 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1144 url_start(ifp); 1145 splx(s); 1146 } 1147 1148 void 1149 url_stop_task(struct url_softc *sc) 1150 { 1151 url_stop(GET_IFP(sc), 1); 1152 } 1153 1154 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */ 1155 void 1156 url_stop(struct ifnet *ifp, int disable) 1157 { 1158 struct url_softc *sc = ifp->if_softc; 1159 usbd_status err; 1160 int i; 1161 1162 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 1163 1164 ifp->if_timer = 0; 1165 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1166 1167 url_reset(sc); 1168 1169 timeout_del(&sc->sc_stat_ch); 1170 1171 /* Stop transfers */ 1172 /* RX endpoint */ 1173 if (sc->sc_pipe_rx != NULL) { 1174 err = usbd_abort_pipe(sc->sc_pipe_rx); 1175 if (err) 1176 printf("%s: abort rx pipe failed: %s\n", 1177 sc->sc_dev.dv_xname, usbd_errstr(err)); 1178 err = usbd_close_pipe(sc->sc_pipe_rx); 1179 if (err) 1180 printf("%s: close rx pipe failed: %s\n", 1181 sc->sc_dev.dv_xname, usbd_errstr(err)); 1182 sc->sc_pipe_rx = NULL; 1183 } 1184 1185 /* TX endpoint */ 1186 if (sc->sc_pipe_tx != NULL) { 1187 err = usbd_abort_pipe(sc->sc_pipe_tx); 1188 if (err) 1189 printf("%s: abort tx pipe failed: %s\n", 1190 sc->sc_dev.dv_xname, usbd_errstr(err)); 1191 err = usbd_close_pipe(sc->sc_pipe_tx); 1192 if (err) 1193 printf("%s: close tx pipe failed: %s\n", 1194 sc->sc_dev.dv_xname, usbd_errstr(err)); 1195 sc->sc_pipe_tx = NULL; 1196 } 1197 1198 #if 0 1199 /* XXX: Interrupt endpoint is not yet supported!! */ 1200 /* Interrupt endpoint */ 1201 if (sc->sc_pipe_intr != NULL) { 1202 err = usbd_abort_pipe(sc->sc_pipe_intr); 1203 if (err) 1204 printf("%s: abort intr pipe failed: %s\n", 1205 sc->sc_dev.dv_xname, usbd_errstr(err)); 1206 err = usbd_close_pipe(sc->sc_pipe_intr); 1207 if (err) 1208 printf("%s: close intr pipe failed: %s\n", 1209 sc->sc_dev.dv_xname, usbd_errstr(err)); 1210 sc->sc_pipe_intr = NULL; 1211 } 1212 #endif 1213 1214 /* Free RX resources. */ 1215 for (i = 0; i < URL_RX_LIST_CNT; i++) { 1216 if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) { 1217 m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf); 1218 sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL; 1219 } 1220 if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) { 1221 usbd_free_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer); 1222 sc->sc_cdata.url_rx_chain[i].url_xfer = NULL; 1223 } 1224 } 1225 1226 /* Free TX resources. */ 1227 for (i = 0; i < URL_TX_LIST_CNT; i++) { 1228 if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) { 1229 m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf); 1230 sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL; 1231 } 1232 if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) { 1233 usbd_free_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer); 1234 sc->sc_cdata.url_tx_chain[i].url_xfer = NULL; 1235 } 1236 } 1237 1238 sc->sc_link = 0; 1239 } 1240 1241 /* Set media options */ 1242 int 1243 url_ifmedia_change(struct ifnet *ifp) 1244 { 1245 struct url_softc *sc = ifp->if_softc; 1246 struct mii_data *mii = GET_MII(sc); 1247 1248 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 1249 1250 if (usbd_is_dying(sc->sc_udev)) 1251 return (0); 1252 1253 sc->sc_link = 0; 1254 if (mii->mii_instance) { 1255 struct mii_softc *miisc; 1256 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 1257 miisc = LIST_NEXT(miisc, mii_list)) 1258 mii_phy_reset(miisc); 1259 } 1260 1261 return (mii_mediachg(mii)); 1262 } 1263 1264 /* Report current media status. */ 1265 void 1266 url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) 1267 { 1268 struct url_softc *sc = ifp->if_softc; 1269 struct mii_data *mii = GET_MII(sc); 1270 1271 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 1272 1273 if (usbd_is_dying(sc->sc_udev)) 1274 return; 1275 1276 if ((ifp->if_flags & IFF_RUNNING) == 0) { 1277 ifmr->ifm_active = IFM_ETHER | IFM_NONE; 1278 ifmr->ifm_status = 0; 1279 return; 1280 } 1281 1282 mii_pollstat(mii); 1283 ifmr->ifm_active = mii->mii_media_active; 1284 ifmr->ifm_status = mii->mii_media_status; 1285 } 1286 1287 void 1288 url_tick(void *xsc) 1289 { 1290 struct url_softc *sc = xsc; 1291 1292 if (sc == NULL) 1293 return; 1294 1295 DPRINTFN(0xff, ("%s: %s: enter\n", sc->sc_dev.dv_xname, 1296 __func__)); 1297 1298 if (usbd_is_dying(sc->sc_udev)) 1299 return; 1300 1301 /* Perform periodic stuff in process context */ 1302 usb_add_task(sc->sc_udev, &sc->sc_tick_task); 1303 } 1304 1305 void 1306 url_tick_task(void *xsc) 1307 { 1308 struct url_softc *sc = xsc; 1309 struct ifnet *ifp; 1310 struct mii_data *mii; 1311 int s; 1312 1313 if (sc == NULL) 1314 return; 1315 1316 DPRINTFN(0xff, ("%s: %s: enter\n", sc->sc_dev.dv_xname, 1317 __func__)); 1318 1319 if (usbd_is_dying(sc->sc_udev)) 1320 return; 1321 1322 ifp = GET_IFP(sc); 1323 mii = GET_MII(sc); 1324 1325 if (mii == NULL) 1326 return; 1327 1328 s = splnet(); 1329 1330 mii_tick(mii); 1331 if (!sc->sc_link && mii->mii_media_status & IFM_ACTIVE && 1332 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1333 DPRINTF(("%s: %s: got link\n", 1334 sc->sc_dev.dv_xname, __func__)); 1335 sc->sc_link++; 1336 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1337 url_start(ifp); 1338 } 1339 1340 timeout_add_sec(&sc->sc_stat_ch, 1); 1341 1342 splx(s); 1343 } 1344 1345 /* Get exclusive access to the MII registers */ 1346 void 1347 url_lock_mii(struct url_softc *sc) 1348 { 1349 DPRINTFN(0xff, ("%s: %s: enter\n", sc->sc_dev.dv_xname, 1350 __func__)); 1351 1352 sc->sc_refcnt++; 1353 rw_enter_write(&sc->sc_mii_lock); 1354 } 1355 1356 void 1357 url_unlock_mii(struct url_softc *sc) 1358 { 1359 DPRINTFN(0xff, ("%s: %s: enter\n", sc->sc_dev.dv_xname, 1360 __func__)); 1361 1362 rw_exit_write(&sc->sc_mii_lock); 1363 if (--sc->sc_refcnt < 0) 1364 usb_detach_wakeup(&sc->sc_dev); 1365 } 1366 1367 int 1368 url_int_miibus_readreg(struct device *dev, int phy, int reg) 1369 { 1370 struct url_softc *sc; 1371 u_int16_t val; 1372 1373 if (dev == NULL) 1374 return (0); 1375 1376 sc = (void *)dev; 1377 1378 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", 1379 sc->sc_dev.dv_xname, __func__, phy, reg)); 1380 1381 if (usbd_is_dying(sc->sc_udev)) { 1382 #ifdef DIAGNOSTIC 1383 printf("%s: %s: dying\n", sc->sc_dev.dv_xname, 1384 __func__); 1385 #endif 1386 return (0); 1387 } 1388 1389 /* XXX: one PHY only for the RTL8150 internal PHY */ 1390 if (phy != 0) { 1391 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 1392 sc->sc_dev.dv_xname, __func__, phy)); 1393 return (0); 1394 } 1395 1396 url_lock_mii(sc); 1397 1398 switch (reg) { 1399 case MII_BMCR: /* Control Register */ 1400 reg = URL_BMCR; 1401 break; 1402 case MII_BMSR: /* Status Register */ 1403 reg = URL_BMSR; 1404 break; 1405 case MII_PHYIDR1: 1406 case MII_PHYIDR2: 1407 val = 0; 1408 goto R_DONE; 1409 break; 1410 case MII_ANAR: /* Autonegotiation advertisement */ 1411 reg = URL_ANAR; 1412 break; 1413 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 1414 reg = URL_ANLP; 1415 break; 1416 case URLPHY_MSR: /* Media Status Register */ 1417 reg = URL_MSR; 1418 break; 1419 default: 1420 printf("%s: %s: bad register %04x\n", 1421 sc->sc_dev.dv_xname, __func__, reg); 1422 val = 0; 1423 goto R_DONE; 1424 break; 1425 } 1426 1427 if (reg == URL_MSR) 1428 val = url_csr_read_1(sc, reg); 1429 else 1430 val = url_csr_read_2(sc, reg); 1431 1432 R_DONE: 1433 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", 1434 sc->sc_dev.dv_xname, __func__, phy, reg, val)); 1435 1436 url_unlock_mii(sc); 1437 return (val); 1438 } 1439 1440 void 1441 url_int_miibus_writereg(struct device *dev, int phy, int reg, int data) 1442 { 1443 struct url_softc *sc; 1444 1445 if (dev == NULL) 1446 return; 1447 1448 sc = (void *)dev; 1449 1450 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", 1451 sc->sc_dev.dv_xname, __func__, phy, reg, data)); 1452 1453 if (usbd_is_dying(sc->sc_udev)) { 1454 #ifdef DIAGNOSTIC 1455 printf("%s: %s: dying\n", sc->sc_dev.dv_xname, 1456 __func__); 1457 #endif 1458 return; 1459 } 1460 1461 /* XXX: one PHY only for the RTL8150 internal PHY */ 1462 if (phy != 0) { 1463 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 1464 sc->sc_dev.dv_xname, __func__, phy)); 1465 return; 1466 } 1467 1468 url_lock_mii(sc); 1469 1470 switch (reg) { 1471 case MII_BMCR: /* Control Register */ 1472 reg = URL_BMCR; 1473 break; 1474 case MII_BMSR: /* Status Register */ 1475 reg = URL_BMSR; 1476 break; 1477 case MII_PHYIDR1: 1478 case MII_PHYIDR2: 1479 goto W_DONE; 1480 break; 1481 case MII_ANAR: /* Autonegotiation advertisement */ 1482 reg = URL_ANAR; 1483 break; 1484 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 1485 reg = URL_ANLP; 1486 break; 1487 case URLPHY_MSR: /* Media Status Register */ 1488 reg = URL_MSR; 1489 break; 1490 default: 1491 printf("%s: %s: bad register %04x\n", 1492 sc->sc_dev.dv_xname, __func__, reg); 1493 goto W_DONE; 1494 break; 1495 } 1496 1497 if (reg == URL_MSR) 1498 url_csr_write_1(sc, reg, data); 1499 else 1500 url_csr_write_2(sc, reg, data); 1501 W_DONE: 1502 1503 url_unlock_mii(sc); 1504 return; 1505 } 1506 1507 void 1508 url_miibus_statchg(struct device *dev) 1509 { 1510 #ifdef URL_DEBUG 1511 struct url_softc *sc; 1512 1513 if (dev == NULL) 1514 return; 1515 1516 sc = (void *)dev; 1517 DPRINTF(("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 1518 #endif 1519 /* Nothing to do */ 1520 } 1521 1522 #if 0 1523 /* 1524 * external PHYs support, but not test. 1525 */ 1526 int 1527 url_ext_miibus_redreg(struct device *dev, int phy, int reg) 1528 { 1529 struct url_softc *sc = (void *)dev; 1530 u_int16_t val; 1531 1532 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n", 1533 sc->sc_dev.dv_xname, __func__, phy, reg)); 1534 1535 if (usbd_is_dying(sc->sc_udev)) { 1536 #ifdef DIAGNOSTIC 1537 printf("%s: %s: dying\n", sc->sc_dev.dv_xname, 1538 __func__); 1539 #endif 1540 return (0); 1541 } 1542 1543 url_lock_mii(sc); 1544 1545 url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK); 1546 /* 1547 * RTL8150L will initiate a MII management data transaction 1548 * if PHYCNT_OWN bit is set 1 by software. After transaction, 1549 * this bit is auto cleared by TRL8150L. 1550 */ 1551 url_csr_write_1(sc, URL_PHYCNT, 1552 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR); 1553 for (i = 0; i < URL_TIMEOUT; i++) { 1554 if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0) 1555 break; 1556 } 1557 if (i == URL_TIMEOUT) { 1558 printf("%s: MII read timed out\n", sc->sc_dev.dv_xname); 1559 } 1560 1561 val = url_csr_read_2(sc, URL_PHYDAT); 1562 1563 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", 1564 sc->sc_dev.dv_xname, __func__, phy, reg, val)); 1565 1566 url_unlock_mii(sc); 1567 return (val); 1568 } 1569 1570 void 1571 url_ext_miibus_writereg(struct device *dev, int phy, int reg, int data) 1572 { 1573 struct url_softc *sc = (void *)dev; 1574 1575 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", 1576 sc->sc_dev.dv_xname, __func__, phy, reg, data)); 1577 1578 if (usbd_is_dying(sc->sc_udev)) { 1579 #ifdef DIAGNOSTIC 1580 printf("%s: %s: dying\n", sc->sc_dev.dv_xname, 1581 __func__); 1582 #endif 1583 return; 1584 } 1585 1586 url_lock_mii(sc); 1587 1588 url_csr_write_2(sc, URL_PHYDAT, data); 1589 url_csr_write_1(sc, URL_PHYADD, phy); 1590 url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */ 1591 1592 for (i=0; i < URL_TIMEOUT; i++) { 1593 if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) 1594 break; 1595 } 1596 1597 if (i == URL_TIMEOUT) { 1598 printf("%s: MII write timed out\n", 1599 sc->sc_dev.dv_xname); 1600 } 1601 1602 url_unlock_mii(sc); 1603 return; 1604 } 1605 #endif 1606 1607