1 /* $OpenBSD: if_cas.c,v 1.25 2009/03/29 21:53:52 sthen Exp $ */ 2 3 /* 4 * 5 * Copyright (C) 2007 Mark Kettenis. 6 * Copyright (C) 2001 Eduardo Horvath. 7 * All rights reserved. 8 * 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 * Driver for Sun Cassini ethernet controllers. 35 * 36 * There are basically two variants of this chip: Cassini and 37 * Cassini+. We can distinguish between the two by revision: 0x10 and 38 * up are Cassini+. The most important difference is that Cassini+ 39 * has a second RX descriptor ring. Cassini+ will not work without 40 * configuring that second ring. However, since we don't use it we 41 * don't actually fill the descriptors, and only hand off the first 42 * four to the chip. 43 */ 44 45 #include "bpfilter.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/timeout.h> 50 #include <sys/mbuf.h> 51 #include <sys/syslog.h> 52 #include <sys/malloc.h> 53 #include <sys/kernel.h> 54 #include <sys/socket.h> 55 #include <sys/ioctl.h> 56 #include <sys/errno.h> 57 #include <sys/device.h> 58 59 #include <machine/endian.h> 60 61 #include <net/if.h> 62 #include <net/if_dl.h> 63 #include <net/if_media.h> 64 65 #ifdef INET 66 #include <netinet/in.h> 67 #include <netinet/if_ether.h> 68 #endif 69 70 #if NBPFILTER > 0 71 #include <net/bpf.h> 72 #endif 73 74 #include <machine/bus.h> 75 #include <machine/intr.h> 76 77 #include <dev/mii/mii.h> 78 #include <dev/mii/miivar.h> 79 #include <dev/mii/mii_bitbang.h> 80 81 #include <dev/pci/if_casreg.h> 82 #include <dev/pci/if_casvar.h> 83 84 #include <dev/pci/pcivar.h> 85 #include <dev/pci/pcireg.h> 86 #include <dev/pci/pcidevs.h> 87 88 #ifdef __sparc64__ 89 #include <dev/ofw/openfirm.h> 90 #endif 91 92 #define TRIES 10000 93 94 struct cfdriver cas_cd = { 95 NULL, "cas", DV_IFNET 96 }; 97 98 int cas_match(struct device *, void *, void *); 99 void cas_attach(struct device *, struct device *, void *); 100 int cas_pci_enaddr(struct cas_softc *, struct pci_attach_args *); 101 102 struct cfattach cas_ca = { 103 sizeof(struct cas_softc), cas_match, cas_attach 104 }; 105 106 void cas_config(struct cas_softc *); 107 void cas_start(struct ifnet *); 108 void cas_stop(struct ifnet *, int); 109 int cas_ioctl(struct ifnet *, u_long, caddr_t); 110 void cas_tick(void *); 111 void cas_watchdog(struct ifnet *); 112 void cas_shutdown(void *); 113 int cas_init(struct ifnet *); 114 void cas_init_regs(struct cas_softc *); 115 int cas_ringsize(int); 116 int cas_cringsize(int); 117 int cas_meminit(struct cas_softc *); 118 void cas_mifinit(struct cas_softc *); 119 int cas_bitwait(struct cas_softc *, bus_space_handle_t, int, 120 u_int32_t, u_int32_t); 121 void cas_reset(struct cas_softc *); 122 int cas_reset_rx(struct cas_softc *); 123 int cas_reset_tx(struct cas_softc *); 124 int cas_disable_rx(struct cas_softc *); 125 int cas_disable_tx(struct cas_softc *); 126 void cas_rxdrain(struct cas_softc *); 127 int cas_add_rxbuf(struct cas_softc *, int idx); 128 void cas_setladrf(struct cas_softc *); 129 int cas_encap(struct cas_softc *, struct mbuf *, u_int32_t *); 130 131 /* MII methods & callbacks */ 132 int cas_mii_readreg(struct device *, int, int); 133 void cas_mii_writereg(struct device *, int, int, int); 134 void cas_mii_statchg(struct device *); 135 int cas_pcs_readreg(struct device *, int, int); 136 void cas_pcs_writereg(struct device *, int, int, int); 137 138 int cas_mediachange(struct ifnet *); 139 void cas_mediastatus(struct ifnet *, struct ifmediareq *); 140 141 int cas_eint(struct cas_softc *, u_int); 142 int cas_rint(struct cas_softc *); 143 int cas_tint(struct cas_softc *, u_int32_t); 144 int cas_pint(struct cas_softc *); 145 int cas_intr(void *); 146 147 #ifdef CAS_DEBUG 148 #define DPRINTF(sc, x) if ((sc)->sc_arpcom.ac_if.if_flags & IFF_DEBUG) \ 149 printf x 150 #else 151 #define DPRINTF(sc, x) /* nothing */ 152 #endif 153 154 const struct pci_matchid cas_pci_devices[] = { 155 { PCI_VENDOR_SUN, PCI_PRODUCT_SUN_CASSINI }, 156 { PCI_VENDOR_NS, PCI_PRODUCT_NS_SATURN } 157 }; 158 159 int 160 cas_match(struct device *parent, void *cf, void *aux) 161 { 162 return (pci_matchbyid((struct pci_attach_args *)aux, cas_pci_devices, 163 sizeof(cas_pci_devices)/sizeof(cas_pci_devices[0]))); 164 } 165 166 #define PROMHDR_PTR_DATA 0x18 167 #define PROMDATA_PTR_VPD 0x08 168 #define PROMDATA_DATA2 0x0a 169 170 static const u_int8_t cas_promhdr[] = { 0x55, 0xaa }; 171 static const u_int8_t cas_promdat[] = { 172 'P', 'C', 'I', 'R', 173 PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8, 174 PCI_PRODUCT_SUN_CASSINI & 0xff, PCI_PRODUCT_SUN_CASSINI >> 8 175 }; 176 177 static const u_int8_t cas_promdat2[] = { 178 0x18, 0x00, /* structure length */ 179 0x00, /* structure revision */ 180 0x00, /* interface revision */ 181 PCI_SUBCLASS_NETWORK_ETHERNET, /* subclass code */ 182 PCI_CLASS_NETWORK /* class code */ 183 }; 184 185 int 186 cas_pci_enaddr(struct cas_softc *sc, struct pci_attach_args *pa) 187 { 188 struct pci_vpd_largeres *res; 189 struct pci_vpd *vpd; 190 bus_space_handle_t romh; 191 bus_space_tag_t romt; 192 bus_size_t romsize; 193 u_int8_t buf[32], *desc; 194 pcireg_t address, mask; 195 int dataoff, vpdoff, len; 196 int rv = -1; 197 198 address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG); 199 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, 0xfffffffe); 200 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG); 201 address |= PCI_ROM_ENABLE; 202 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, address); 203 204 romt = pa->pa_memt; 205 romsize = PCI_ROM_SIZE(mask); 206 if (bus_space_map(romt, PCI_ROM_ADDR(address), romsize, 0, &romh)) { 207 romsize = 0; 208 goto fail; 209 } 210 211 bus_space_read_region_1(romt, romh, 0, buf, sizeof(buf)); 212 if (bcmp(buf, cas_promhdr, sizeof(cas_promhdr))) 213 goto fail; 214 215 dataoff = buf[PROMHDR_PTR_DATA] | (buf[PROMHDR_PTR_DATA + 1] << 8); 216 if (dataoff < 0x1c) 217 goto fail; 218 219 bus_space_read_region_1(romt, romh, dataoff, buf, sizeof(buf)); 220 if (bcmp(buf, cas_promdat, sizeof(cas_promdat)) || 221 bcmp(buf + PROMDATA_DATA2, cas_promdat2, sizeof(cas_promdat2))) 222 goto fail; 223 224 vpdoff = buf[PROMDATA_PTR_VPD] | (buf[PROMDATA_PTR_VPD + 1] << 8); 225 if (vpdoff < 0x1c) 226 goto fail; 227 228 next: 229 bus_space_read_region_1(romt, romh, vpdoff, buf, sizeof(buf)); 230 if (!PCI_VPDRES_ISLARGE(buf[0])) 231 goto fail; 232 233 res = (struct pci_vpd_largeres *)buf; 234 vpdoff += sizeof(*res); 235 236 len = ((res->vpdres_len_msb << 8) + res->vpdres_len_lsb); 237 switch(PCI_VPDRES_LARGE_NAME(res->vpdres_byte0)) { 238 case PCI_VPDRES_TYPE_IDENTIFIER_STRING: 239 /* Skip identifier string. */ 240 vpdoff += len; 241 goto next; 242 243 case PCI_VPDRES_TYPE_VPD: 244 while (len > 0) { 245 bus_space_read_region_1(romt, romh, vpdoff, 246 buf, sizeof(buf)); 247 248 vpd = (struct pci_vpd *)buf; 249 vpdoff += sizeof(*vpd) + vpd->vpd_len; 250 len -= sizeof(*vpd) + vpd->vpd_len; 251 252 /* 253 * We're looking for an "Enhanced" VPD... 254 */ 255 if (vpd->vpd_key0 != 'Z') 256 continue; 257 258 desc = buf + sizeof(*vpd); 259 260 /* 261 * ...which is an instance property... 262 */ 263 if (desc[0] != 'I') 264 continue; 265 desc += 3; 266 267 /* 268 * ...that's a byte array with the proper 269 * length for a MAC address... 270 */ 271 if (desc[0] != 'B' || desc[1] != ETHER_ADDR_LEN) 272 continue; 273 desc += 2; 274 275 /* 276 * ...named "local-mac-address". 277 */ 278 if (strcmp(desc, "local-mac-address") != 0) 279 continue; 280 desc += strlen("local-mac-address") + 1; 281 282 bcopy(desc, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN); 283 rv = 0; 284 } 285 break; 286 287 default: 288 goto fail; 289 } 290 291 fail: 292 if (romsize != 0) 293 bus_space_unmap(romt, romh, romsize); 294 295 address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG); 296 address &= ~PCI_ROM_ENABLE; 297 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, address); 298 299 return (rv); 300 } 301 302 void 303 cas_attach(struct device *parent, struct device *self, void *aux) 304 { 305 struct pci_attach_args *pa = aux; 306 struct cas_softc *sc = (void *)self; 307 pci_intr_handle_t ih; 308 #ifdef __sparc64__ 309 /* XXX the following declarations should be elsewhere */ 310 extern void myetheraddr(u_char *); 311 #endif 312 const char *intrstr = NULL; 313 bus_size_t size; 314 int gotenaddr = 0; 315 316 sc->sc_rev = PCI_REVISION(pa->pa_class); 317 sc->sc_dmatag = pa->pa_dmat; 318 319 #define PCI_CAS_BASEADDR 0x10 320 if (pci_mapreg_map(pa, PCI_CAS_BASEADDR, PCI_MAPREG_TYPE_MEM, 0, 321 &sc->sc_memt, &sc->sc_memh, NULL, &size, 0) != 0) { 322 printf(": can't map registers\n"); 323 return; 324 } 325 326 if (cas_pci_enaddr(sc, pa) == 0) 327 gotenaddr = 1; 328 329 #ifdef __sparc64__ 330 if (!gotenaddr) { 331 if (OF_getprop(PCITAG_NODE(pa->pa_tag), "local-mac-address", 332 sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0) 333 myetheraddr(sc->sc_arpcom.ac_enaddr); 334 gotenaddr = 1; 335 } 336 #endif 337 #ifdef __powerpc__ 338 if (!gotenaddr) { 339 pci_ether_hw_addr(pa->pa_pc, sc->sc_arpcom.ac_enaddr); 340 gotenaddr = 1; 341 } 342 #endif 343 344 sc->sc_burst = 16; /* XXX */ 345 346 if (pci_intr_map(pa, &ih) != 0) { 347 printf(": couldn't map interrupt\n"); 348 bus_space_unmap(sc->sc_memt, sc->sc_memh, size); 349 return; 350 } 351 intrstr = pci_intr_string(pa->pa_pc, ih); 352 sc->sc_ih = pci_intr_establish(pa->pa_pc, 353 ih, IPL_NET, cas_intr, sc, self->dv_xname); 354 if (sc->sc_ih == NULL) { 355 printf(": couldn't establish interrupt"); 356 if (intrstr != NULL) 357 printf(" at %s", intrstr); 358 printf("\n"); 359 bus_space_unmap(sc->sc_memt, sc->sc_memh, size); 360 return; 361 } 362 363 printf(": %s", intrstr); 364 365 /* 366 * call the main configure 367 */ 368 cas_config(sc); 369 } 370 371 /* 372 * cas_config: 373 * 374 * Attach a Cassini interface to the system. 375 */ 376 void 377 cas_config(struct cas_softc *sc) 378 { 379 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 380 struct mii_data *mii = &sc->sc_mii; 381 struct mii_softc *child; 382 int i, error; 383 384 /* Make sure the chip is stopped. */ 385 ifp->if_softc = sc; 386 cas_reset(sc); 387 388 /* 389 * Allocate the control data structures, and create and load the 390 * DMA map for it. 391 */ 392 if ((error = bus_dmamem_alloc(sc->sc_dmatag, 393 sizeof(struct cas_control_data), CAS_PAGE_SIZE, 0, &sc->sc_cdseg, 394 1, &sc->sc_cdnseg, 0)) != 0) { 395 printf("\n%s: unable to allocate control data, error = %d\n", 396 sc->sc_dev.dv_xname, error); 397 goto fail_0; 398 } 399 400 /* XXX should map this in with correct endianness */ 401 if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg, 402 sizeof(struct cas_control_data), (caddr_t *)&sc->sc_control_data, 403 BUS_DMA_COHERENT)) != 0) { 404 printf("\n%s: unable to map control data, error = %d\n", 405 sc->sc_dev.dv_xname, error); 406 goto fail_1; 407 } 408 409 if ((error = bus_dmamap_create(sc->sc_dmatag, 410 sizeof(struct cas_control_data), 1, 411 sizeof(struct cas_control_data), 0, 0, &sc->sc_cddmamap)) != 0) { 412 printf("\n%s: unable to create control data DMA map, " 413 "error = %d\n", sc->sc_dev.dv_xname, error); 414 goto fail_2; 415 } 416 417 if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap, 418 sc->sc_control_data, sizeof(struct cas_control_data), NULL, 419 0)) != 0) { 420 printf("\n%s: unable to load control data DMA map, error = %d\n", 421 sc->sc_dev.dv_xname, error); 422 goto fail_3; 423 } 424 425 bzero(sc->sc_control_data, sizeof(struct cas_control_data)); 426 427 /* 428 * Create the receive buffer DMA maps. 429 */ 430 for (i = 0; i < CAS_NRXDESC; i++) { 431 bus_dma_segment_t seg; 432 caddr_t kva; 433 int rseg; 434 435 if ((error = bus_dmamem_alloc(sc->sc_dmatag, CAS_PAGE_SIZE, 436 CAS_PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 437 printf("\n%s: unable to alloc rx DMA mem %d, " 438 "error = %d\n", sc->sc_dev.dv_xname, i, error); 439 goto fail_5; 440 } 441 sc->sc_rxsoft[i].rxs_dmaseg = seg; 442 443 if ((error = bus_dmamem_map(sc->sc_dmatag, &seg, rseg, 444 CAS_PAGE_SIZE, &kva, BUS_DMA_NOWAIT)) != 0) { 445 printf("\n%s: unable to alloc rx DMA mem %d, " 446 "error = %d\n", sc->sc_dev.dv_xname, i, error); 447 goto fail_5; 448 } 449 sc->sc_rxsoft[i].rxs_kva = kva; 450 451 if ((error = bus_dmamap_create(sc->sc_dmatag, CAS_PAGE_SIZE, 1, 452 CAS_PAGE_SIZE, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) { 453 printf("\n%s: unable to create rx DMA map %d, " 454 "error = %d\n", sc->sc_dev.dv_xname, i, error); 455 goto fail_5; 456 } 457 458 if ((error = bus_dmamap_load(sc->sc_dmatag, 459 sc->sc_rxsoft[i].rxs_dmamap, kva, CAS_PAGE_SIZE, NULL, 460 BUS_DMA_NOWAIT)) != 0) { 461 printf("\n%s: unable to load rx DMA map %d, " 462 "error = %d\n", sc->sc_dev.dv_xname, i, error); 463 goto fail_5; 464 } 465 } 466 467 /* 468 * Create the transmit buffer DMA maps. 469 */ 470 for (i = 0; i < CAS_NTXDESC; i++) { 471 if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 472 CAS_NTXSEGS, MCLBYTES, 0, BUS_DMA_NOWAIT, 473 &sc->sc_txd[i].sd_map)) != 0) { 474 printf("\n%s: unable to create tx DMA map %d, " 475 "error = %d\n", sc->sc_dev.dv_xname, i, error); 476 goto fail_6; 477 } 478 sc->sc_txd[i].sd_mbuf = NULL; 479 } 480 481 /* 482 * From this point forward, the attachment cannot fail. A failure 483 * before this point releases all resources that may have been 484 * allocated. 485 */ 486 487 /* Announce ourselves. */ 488 printf(", address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr)); 489 490 /* Get RX FIFO size */ 491 sc->sc_rxfifosize = 16 * 1024; 492 493 /* Initialize ifnet structure. */ 494 strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, sizeof ifp->if_xname); 495 ifp->if_softc = sc; 496 ifp->if_flags = 497 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; 498 ifp->if_start = cas_start; 499 ifp->if_ioctl = cas_ioctl; 500 ifp->if_watchdog = cas_watchdog; 501 IFQ_SET_MAXLEN(&ifp->if_snd, CAS_NTXDESC - 1); 502 IFQ_SET_READY(&ifp->if_snd); 503 504 ifp->if_capabilities = IFCAP_VLAN_MTU; 505 506 /* Initialize ifmedia structures and MII info */ 507 mii->mii_ifp = ifp; 508 mii->mii_readreg = cas_mii_readreg; 509 mii->mii_writereg = cas_mii_writereg; 510 mii->mii_statchg = cas_mii_statchg; 511 512 ifmedia_init(&mii->mii_media, 0, cas_mediachange, cas_mediastatus); 513 514 bus_space_write_4(sc->sc_memt, sc->sc_memh, CAS_MII_DATAPATH_MODE, 0); 515 516 cas_mifinit(sc); 517 518 if (sc->sc_mif_config & CAS_MIF_CONFIG_MDI1) { 519 sc->sc_mif_config |= CAS_MIF_CONFIG_PHY_SEL; 520 bus_space_write_4(sc->sc_memt, sc->sc_memh, 521 CAS_MIF_CONFIG, sc->sc_mif_config); 522 } 523 524 mii_attach(&sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY, 525 MII_OFFSET_ANY, 0); 526 527 child = LIST_FIRST(&mii->mii_phys); 528 if (child == NULL && 529 sc->sc_mif_config & (CAS_MIF_CONFIG_MDI0|CAS_MIF_CONFIG_MDI1)) { 530 /* 531 * Try the external PCS SERDES if we didn't find any 532 * MII devices. 533 */ 534 bus_space_write_4(sc->sc_memt, sc->sc_memh, 535 CAS_MII_DATAPATH_MODE, CAS_MII_DATAPATH_SERDES); 536 537 bus_space_write_4(sc->sc_memt, sc->sc_memh, 538 CAS_MII_CONFIG, CAS_MII_CONFIG_ENABLE); 539 540 mii->mii_readreg = cas_pcs_readreg; 541 mii->mii_writereg = cas_pcs_writereg; 542 543 mii_attach(&sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY, 544 MII_OFFSET_ANY, MIIF_NOISOLATE); 545 } 546 547 child = LIST_FIRST(&mii->mii_phys); 548 if (child == NULL) { 549 /* No PHY attached */ 550 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 551 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL); 552 } else { 553 /* 554 * Walk along the list of attached MII devices and 555 * establish an `MII instance' to `phy number' 556 * mapping. We'll use this mapping in media change 557 * requests to determine which phy to use to program 558 * the MIF configuration register. 559 */ 560 for (; child != NULL; child = LIST_NEXT(child, mii_list)) { 561 /* 562 * Note: we support just two PHYs: the built-in 563 * internal device and an external on the MII 564 * connector. 565 */ 566 if (child->mii_phy > 1 || child->mii_inst > 1) { 567 printf("%s: cannot accommodate MII device %s" 568 " at phy %d, instance %d\n", 569 sc->sc_dev.dv_xname, 570 child->mii_dev.dv_xname, 571 child->mii_phy, child->mii_inst); 572 continue; 573 } 574 575 sc->sc_phys[child->mii_inst] = child->mii_phy; 576 } 577 578 /* 579 * XXX - we can really do the following ONLY if the 580 * phy indeed has the auto negotiation capability!! 581 */ 582 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO); 583 } 584 585 /* Attach the interface. */ 586 if_attach(ifp); 587 ether_ifattach(ifp); 588 589 sc->sc_sh = shutdownhook_establish(cas_shutdown, sc); 590 if (sc->sc_sh == NULL) 591 panic("cas_config: can't establish shutdownhook"); 592 593 timeout_set(&sc->sc_tick_ch, cas_tick, sc); 594 return; 595 596 /* 597 * Free any resources we've allocated during the failed attach 598 * attempt. Do this in reverse order and fall through. 599 */ 600 fail_6: 601 for (i = 0; i < CAS_NTXDESC; i++) { 602 if (sc->sc_txd[i].sd_map != NULL) 603 bus_dmamap_destroy(sc->sc_dmatag, 604 sc->sc_txd[i].sd_map); 605 } 606 fail_5: 607 for (i = 0; i < CAS_NRXDESC; i++) { 608 if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 609 bus_dmamap_destroy(sc->sc_dmatag, 610 sc->sc_rxsoft[i].rxs_dmamap); 611 } 612 bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap); 613 fail_3: 614 bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap); 615 fail_2: 616 bus_dmamem_unmap(sc->sc_dmatag, (caddr_t)sc->sc_control_data, 617 sizeof(struct cas_control_data)); 618 fail_1: 619 bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg); 620 fail_0: 621 return; 622 } 623 624 625 void 626 cas_tick(void *arg) 627 { 628 struct cas_softc *sc = arg; 629 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 630 bus_space_tag_t t = sc->sc_memt; 631 bus_space_handle_t mac = sc->sc_memh; 632 int s; 633 u_int32_t v; 634 635 /* unload collisions counters */ 636 v = bus_space_read_4(t, mac, CAS_MAC_EXCESS_COLL_CNT) + 637 bus_space_read_4(t, mac, CAS_MAC_LATE_COLL_CNT); 638 ifp->if_collisions += v + 639 bus_space_read_4(t, mac, CAS_MAC_NORM_COLL_CNT) + 640 bus_space_read_4(t, mac, CAS_MAC_FIRST_COLL_CNT); 641 ifp->if_oerrors += v; 642 643 /* read error counters */ 644 ifp->if_ierrors += 645 bus_space_read_4(t, mac, CAS_MAC_RX_LEN_ERR_CNT) + 646 bus_space_read_4(t, mac, CAS_MAC_RX_ALIGN_ERR) + 647 bus_space_read_4(t, mac, CAS_MAC_RX_CRC_ERR_CNT) + 648 bus_space_read_4(t, mac, CAS_MAC_RX_CODE_VIOL); 649 650 /* clear the hardware counters */ 651 bus_space_write_4(t, mac, CAS_MAC_NORM_COLL_CNT, 0); 652 bus_space_write_4(t, mac, CAS_MAC_FIRST_COLL_CNT, 0); 653 bus_space_write_4(t, mac, CAS_MAC_EXCESS_COLL_CNT, 0); 654 bus_space_write_4(t, mac, CAS_MAC_LATE_COLL_CNT, 0); 655 bus_space_write_4(t, mac, CAS_MAC_RX_LEN_ERR_CNT, 0); 656 bus_space_write_4(t, mac, CAS_MAC_RX_ALIGN_ERR, 0); 657 bus_space_write_4(t, mac, CAS_MAC_RX_CRC_ERR_CNT, 0); 658 bus_space_write_4(t, mac, CAS_MAC_RX_CODE_VIOL, 0); 659 660 s = splnet(); 661 mii_tick(&sc->sc_mii); 662 splx(s); 663 664 timeout_add_sec(&sc->sc_tick_ch, 1); 665 } 666 667 int 668 cas_bitwait(struct cas_softc *sc, bus_space_handle_t h, int r, 669 u_int32_t clr, u_int32_t set) 670 { 671 int i; 672 u_int32_t reg; 673 674 for (i = TRIES; i--; DELAY(100)) { 675 reg = bus_space_read_4(sc->sc_memt, h, r); 676 if ((reg & clr) == 0 && (reg & set) == set) 677 return (1); 678 } 679 680 return (0); 681 } 682 683 void 684 cas_reset(struct cas_softc *sc) 685 { 686 bus_space_tag_t t = sc->sc_memt; 687 bus_space_handle_t h = sc->sc_memh; 688 int s; 689 690 s = splnet(); 691 DPRINTF(sc, ("%s: cas_reset\n", sc->sc_dev.dv_xname)); 692 cas_reset_rx(sc); 693 cas_reset_tx(sc); 694 695 /* Do a full reset */ 696 bus_space_write_4(t, h, CAS_RESET, 697 CAS_RESET_RX | CAS_RESET_TX | CAS_RESET_BLOCK_PCS); 698 if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_RX | CAS_RESET_TX, 0)) 699 printf("%s: cannot reset device\n", sc->sc_dev.dv_xname); 700 splx(s); 701 } 702 703 704 /* 705 * cas_rxdrain: 706 * 707 * Drain the receive queue. 708 */ 709 void 710 cas_rxdrain(struct cas_softc *sc) 711 { 712 /* Nothing to do yet. */ 713 } 714 715 /* 716 * Reset the whole thing. 717 */ 718 void 719 cas_stop(struct ifnet *ifp, int disable) 720 { 721 struct cas_softc *sc = (struct cas_softc *)ifp->if_softc; 722 struct cas_sxd *sd; 723 u_int32_t i; 724 725 DPRINTF(sc, ("%s: cas_stop\n", sc->sc_dev.dv_xname)); 726 727 timeout_del(&sc->sc_tick_ch); 728 729 /* 730 * Mark the interface down and cancel the watchdog timer. 731 */ 732 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 733 ifp->if_timer = 0; 734 735 mii_down(&sc->sc_mii); 736 737 cas_reset_rx(sc); 738 cas_reset_tx(sc); 739 740 /* 741 * Release any queued transmit buffers. 742 */ 743 for (i = 0; i < CAS_NTXDESC; i++) { 744 sd = &sc->sc_txd[i]; 745 if (sd->sd_mbuf != NULL) { 746 bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0, 747 sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 748 bus_dmamap_unload(sc->sc_dmatag, sd->sd_map); 749 m_freem(sd->sd_mbuf); 750 sd->sd_mbuf = NULL; 751 } 752 } 753 sc->sc_tx_cnt = sc->sc_tx_prod = sc->sc_tx_cons = 0; 754 755 if (disable) 756 cas_rxdrain(sc); 757 } 758 759 760 /* 761 * Reset the receiver 762 */ 763 int 764 cas_reset_rx(struct cas_softc *sc) 765 { 766 bus_space_tag_t t = sc->sc_memt; 767 bus_space_handle_t h = sc->sc_memh; 768 769 /* 770 * Resetting while DMA is in progress can cause a bus hang, so we 771 * disable DMA first. 772 */ 773 cas_disable_rx(sc); 774 bus_space_write_4(t, h, CAS_RX_CONFIG, 0); 775 /* Wait till it finishes */ 776 if (!cas_bitwait(sc, h, CAS_RX_CONFIG, 1, 0)) 777 printf("%s: cannot disable rx dma\n", sc->sc_dev.dv_xname); 778 /* Wait 5ms extra. */ 779 delay(5000); 780 781 /* Finally, reset the ERX */ 782 bus_space_write_4(t, h, CAS_RESET, CAS_RESET_RX); 783 /* Wait till it finishes */ 784 if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_RX, 0)) { 785 printf("%s: cannot reset receiver\n", sc->sc_dev.dv_xname); 786 return (1); 787 } 788 return (0); 789 } 790 791 792 /* 793 * Reset the transmitter 794 */ 795 int 796 cas_reset_tx(struct cas_softc *sc) 797 { 798 bus_space_tag_t t = sc->sc_memt; 799 bus_space_handle_t h = sc->sc_memh; 800 801 /* 802 * Resetting while DMA is in progress can cause a bus hang, so we 803 * disable DMA first. 804 */ 805 cas_disable_tx(sc); 806 bus_space_write_4(t, h, CAS_TX_CONFIG, 0); 807 /* Wait till it finishes */ 808 if (!cas_bitwait(sc, h, CAS_TX_CONFIG, 1, 0)) 809 printf("%s: cannot disable tx dma\n", sc->sc_dev.dv_xname); 810 /* Wait 5ms extra. */ 811 delay(5000); 812 813 /* Finally, reset the ETX */ 814 bus_space_write_4(t, h, CAS_RESET, CAS_RESET_TX); 815 /* Wait till it finishes */ 816 if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_TX, 0)) { 817 printf("%s: cannot reset transmitter\n", 818 sc->sc_dev.dv_xname); 819 return (1); 820 } 821 return (0); 822 } 823 824 /* 825 * disable receiver. 826 */ 827 int 828 cas_disable_rx(struct cas_softc *sc) 829 { 830 bus_space_tag_t t = sc->sc_memt; 831 bus_space_handle_t h = sc->sc_memh; 832 u_int32_t cfg; 833 834 /* Flip the enable bit */ 835 cfg = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG); 836 cfg &= ~CAS_MAC_RX_ENABLE; 837 bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, cfg); 838 839 /* Wait for it to finish */ 840 return (cas_bitwait(sc, h, CAS_MAC_RX_CONFIG, CAS_MAC_RX_ENABLE, 0)); 841 } 842 843 /* 844 * disable transmitter. 845 */ 846 int 847 cas_disable_tx(struct cas_softc *sc) 848 { 849 bus_space_tag_t t = sc->sc_memt; 850 bus_space_handle_t h = sc->sc_memh; 851 u_int32_t cfg; 852 853 /* Flip the enable bit */ 854 cfg = bus_space_read_4(t, h, CAS_MAC_TX_CONFIG); 855 cfg &= ~CAS_MAC_TX_ENABLE; 856 bus_space_write_4(t, h, CAS_MAC_TX_CONFIG, cfg); 857 858 /* Wait for it to finish */ 859 return (cas_bitwait(sc, h, CAS_MAC_TX_CONFIG, CAS_MAC_TX_ENABLE, 0)); 860 } 861 862 /* 863 * Initialize interface. 864 */ 865 int 866 cas_meminit(struct cas_softc *sc) 867 { 868 struct cas_rxsoft *rxs; 869 int i, error; 870 871 rxs = (void *)&error; 872 873 /* 874 * Initialize the transmit descriptor ring. 875 */ 876 for (i = 0; i < CAS_NTXDESC; i++) { 877 sc->sc_txdescs[i].cd_flags = 0; 878 sc->sc_txdescs[i].cd_addr = 0; 879 } 880 CAS_CDTXSYNC(sc, 0, CAS_NTXDESC, 881 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 882 883 /* 884 * Initialize the receive descriptor and receive job 885 * descriptor rings. 886 */ 887 for (i = 0; i < CAS_NRXDESC; i++) 888 CAS_INIT_RXDESC(sc, i, i); 889 sc->sc_rxdptr = 0; 890 sc->sc_rxptr = 0; 891 892 /* 893 * Initialize the receive completion ring. 894 */ 895 for (i = 0; i < CAS_NRXCOMP; i++) { 896 sc->sc_rxcomps[i].cc_word[0] = 0; 897 sc->sc_rxcomps[i].cc_word[1] = 0; 898 sc->sc_rxcomps[i].cc_word[2] = 0; 899 sc->sc_rxcomps[i].cc_word[3] = CAS_DMA_WRITE(CAS_RC3_OWN); 900 CAS_CDRXCSYNC(sc, i, 901 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 902 } 903 904 return (0); 905 } 906 907 int 908 cas_ringsize(int sz) 909 { 910 switch (sz) { 911 case 32: 912 return CAS_RING_SZ_32; 913 case 64: 914 return CAS_RING_SZ_64; 915 case 128: 916 return CAS_RING_SZ_128; 917 case 256: 918 return CAS_RING_SZ_256; 919 case 512: 920 return CAS_RING_SZ_512; 921 case 1024: 922 return CAS_RING_SZ_1024; 923 case 2048: 924 return CAS_RING_SZ_2048; 925 case 4096: 926 return CAS_RING_SZ_4096; 927 case 8192: 928 return CAS_RING_SZ_8192; 929 default: 930 printf("cas: invalid Receive Descriptor ring size %d\n", sz); 931 return CAS_RING_SZ_32; 932 } 933 } 934 935 int 936 cas_cringsize(int sz) 937 { 938 int i; 939 940 for (i = 0; i < 9; i++) 941 if (sz == (128 << i)) 942 return i; 943 944 printf("cas: invalid completion ring size %d\n", sz); 945 return 128; 946 } 947 948 /* 949 * Initialization of interface; set up initialization block 950 * and transmit/receive descriptor rings. 951 */ 952 int 953 cas_init(struct ifnet *ifp) 954 { 955 struct cas_softc *sc = (struct cas_softc *)ifp->if_softc; 956 bus_space_tag_t t = sc->sc_memt; 957 bus_space_handle_t h = sc->sc_memh; 958 int s; 959 u_int max_frame_size; 960 u_int32_t v; 961 962 s = splnet(); 963 964 DPRINTF(sc, ("%s: cas_init: calling stop\n", sc->sc_dev.dv_xname)); 965 /* 966 * Initialization sequence. The numbered steps below correspond 967 * to the sequence outlined in section 6.3.5.1 in the Ethernet 968 * Channel Engine manual (part of the PCIO manual). 969 * See also the STP2002-STQ document from Sun Microsystems. 970 */ 971 972 /* step 1 & 2. Reset the Ethernet Channel */ 973 cas_stop(ifp, 0); 974 cas_reset(sc); 975 DPRINTF(sc, ("%s: cas_init: restarting\n", sc->sc_dev.dv_xname)); 976 977 /* Re-initialize the MIF */ 978 cas_mifinit(sc); 979 980 /* step 3. Setup data structures in host memory */ 981 cas_meminit(sc); 982 983 /* step 4. TX MAC registers & counters */ 984 cas_init_regs(sc); 985 max_frame_size = ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN; 986 v = (max_frame_size) | (0x2000 << 16) /* Burst size */; 987 bus_space_write_4(t, h, CAS_MAC_MAC_MAX_FRAME, v); 988 989 /* step 5. RX MAC registers & counters */ 990 cas_setladrf(sc); 991 992 /* step 6 & 7. Program Descriptor Ring Base Addresses */ 993 KASSERT((CAS_CDTXADDR(sc, 0) & 0x1fff) == 0); 994 bus_space_write_4(t, h, CAS_TX_RING_PTR_HI, 995 (((uint64_t)CAS_CDTXADDR(sc,0)) >> 32)); 996 bus_space_write_4(t, h, CAS_TX_RING_PTR_LO, CAS_CDTXADDR(sc, 0)); 997 998 KASSERT((CAS_CDRXADDR(sc, 0) & 0x1fff) == 0); 999 bus_space_write_4(t, h, CAS_RX_DRING_PTR_HI, 1000 (((uint64_t)CAS_CDRXADDR(sc,0)) >> 32)); 1001 bus_space_write_4(t, h, CAS_RX_DRING_PTR_LO, CAS_CDRXADDR(sc, 0)); 1002 1003 KASSERT((CAS_CDRXCADDR(sc, 0) & 0x1fff) == 0); 1004 bus_space_write_4(t, h, CAS_RX_CRING_PTR_HI, 1005 (((uint64_t)CAS_CDRXCADDR(sc,0)) >> 32)); 1006 bus_space_write_4(t, h, CAS_RX_CRING_PTR_LO, CAS_CDRXCADDR(sc, 0)); 1007 1008 if (CAS_PLUS(sc)) { 1009 KASSERT((CAS_CDRXADDR2(sc, 0) & 0x1fff) == 0); 1010 bus_space_write_4(t, h, CAS_RX_DRING_PTR_HI2, 1011 (((uint64_t)CAS_CDRXADDR2(sc,0)) >> 32)); 1012 bus_space_write_4(t, h, CAS_RX_DRING_PTR_LO2, 1013 CAS_CDRXADDR2(sc, 0)); 1014 } 1015 1016 /* step 8. Global Configuration & Interrupt Mask */ 1017 bus_space_write_4(t, h, CAS_INTMASK, 1018 ~(CAS_INTR_TX_INTME|CAS_INTR_TX_EMPTY| 1019 CAS_INTR_TX_TAG_ERR| 1020 CAS_INTR_RX_DONE|CAS_INTR_RX_NOBUF| 1021 CAS_INTR_RX_TAG_ERR| 1022 CAS_INTR_RX_COMP_FULL|CAS_INTR_PCS| 1023 CAS_INTR_MAC_CONTROL|CAS_INTR_MIF| 1024 CAS_INTR_BERR)); 1025 bus_space_write_4(t, h, CAS_MAC_RX_MASK, 1026 CAS_MAC_RX_DONE|CAS_MAC_RX_FRAME_CNT); 1027 bus_space_write_4(t, h, CAS_MAC_TX_MASK, CAS_MAC_TX_XMIT_DONE); 1028 bus_space_write_4(t, h, CAS_MAC_CONTROL_MASK, 0); /* XXXX */ 1029 1030 /* step 9. ETX Configuration: use mostly default values */ 1031 1032 /* Enable DMA */ 1033 v = cas_ringsize(CAS_NTXDESC /*XXX*/) << 10; 1034 bus_space_write_4(t, h, CAS_TX_CONFIG, 1035 v|CAS_TX_CONFIG_TXDMA_EN|(1<<24)|(1<<29)); 1036 bus_space_write_4(t, h, CAS_TX_KICK, 0); 1037 1038 /* step 10. ERX Configuration */ 1039 1040 /* Encode Receive Descriptor ring size */ 1041 v = cas_ringsize(CAS_NRXDESC) << CAS_RX_CONFIG_RXDRNG_SZ_SHIFT; 1042 if (CAS_PLUS(sc)) 1043 v |= cas_ringsize(32) << CAS_RX_CONFIG_RXDRNG2_SZ_SHIFT; 1044 1045 /* Encode Receive Completion ring size */ 1046 v |= cas_cringsize(CAS_NRXCOMP) << CAS_RX_CONFIG_RXCRNG_SZ_SHIFT; 1047 1048 /* Enable DMA */ 1049 bus_space_write_4(t, h, CAS_RX_CONFIG, 1050 v|(2<<CAS_RX_CONFIG_FBOFF_SHFT)|CAS_RX_CONFIG_RXDMA_EN); 1051 1052 /* 1053 * The following value is for an OFF Threshold of about 3/4 full 1054 * and an ON Threshold of 1/4 full. 1055 */ 1056 bus_space_write_4(t, h, CAS_RX_PAUSE_THRESH, 1057 (3 * sc->sc_rxfifosize / 256) | 1058 ( (sc->sc_rxfifosize / 256) << 12)); 1059 bus_space_write_4(t, h, CAS_RX_BLANKING, (6<<12)|6); 1060 1061 /* step 11. Configure Media */ 1062 mii_mediachg(&sc->sc_mii); 1063 1064 /* step 12. RX_MAC Configuration Register */ 1065 v = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG); 1066 v |= CAS_MAC_RX_ENABLE | CAS_MAC_RX_STRIP_CRC; 1067 bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, v); 1068 1069 /* step 14. Issue Transmit Pending command */ 1070 1071 /* step 15. Give the receiver a swift kick */ 1072 bus_space_write_4(t, h, CAS_RX_KICK, CAS_NRXDESC-4); 1073 if (CAS_PLUS(sc)) 1074 bus_space_write_4(t, h, CAS_RX_KICK2, 4); 1075 1076 /* Start the one second timer. */ 1077 timeout_add_sec(&sc->sc_tick_ch, 1); 1078 1079 ifp->if_flags |= IFF_RUNNING; 1080 ifp->if_flags &= ~IFF_OACTIVE; 1081 ifp->if_timer = 0; 1082 splx(s); 1083 1084 return (0); 1085 } 1086 1087 void 1088 cas_init_regs(struct cas_softc *sc) 1089 { 1090 bus_space_tag_t t = sc->sc_memt; 1091 bus_space_handle_t h = sc->sc_memh; 1092 u_int32_t v, r; 1093 1094 /* These regs are not cleared on reset */ 1095 sc->sc_inited = 0; 1096 if (!sc->sc_inited) { 1097 1098 /* Wooo. Magic values. */ 1099 bus_space_write_4(t, h, CAS_MAC_IPG0, 0); 1100 bus_space_write_4(t, h, CAS_MAC_IPG1, 8); 1101 bus_space_write_4(t, h, CAS_MAC_IPG2, 4); 1102 1103 bus_space_write_4(t, h, CAS_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN); 1104 /* Max frame and max burst size */ 1105 v = ETHER_MAX_LEN | (0x2000 << 16) /* Burst size */; 1106 bus_space_write_4(t, h, CAS_MAC_MAC_MAX_FRAME, v); 1107 1108 bus_space_write_4(t, h, CAS_MAC_PREAMBLE_LEN, 0x7); 1109 bus_space_write_4(t, h, CAS_MAC_JAM_SIZE, 0x4); 1110 bus_space_write_4(t, h, CAS_MAC_ATTEMPT_LIMIT, 0x10); 1111 /* Dunno.... */ 1112 bus_space_write_4(t, h, CAS_MAC_CONTROL_TYPE, 0x8088); 1113 bus_space_write_4(t, h, CAS_MAC_RANDOM_SEED, 1114 ((sc->sc_arpcom.ac_enaddr[5]<<8)|sc->sc_arpcom.ac_enaddr[4])&0x3ff); 1115 1116 /* Secondary MAC addresses set to 0:0:0:0:0:0 */ 1117 for (r = CAS_MAC_ADDR3; r < CAS_MAC_ADDR42; r += 4) 1118 bus_space_write_4(t, h, r, 0); 1119 1120 /* MAC control addr set to 0:1:c2:0:1:80 */ 1121 bus_space_write_4(t, h, CAS_MAC_ADDR42, 0x0001); 1122 bus_space_write_4(t, h, CAS_MAC_ADDR43, 0xc200); 1123 bus_space_write_4(t, h, CAS_MAC_ADDR44, 0x0180); 1124 1125 /* MAC filter addr set to 0:0:0:0:0:0 */ 1126 bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER0, 0); 1127 bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER1, 0); 1128 bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER2, 0); 1129 1130 bus_space_write_4(t, h, CAS_MAC_ADR_FLT_MASK1_2, 0); 1131 bus_space_write_4(t, h, CAS_MAC_ADR_FLT_MASK0, 0); 1132 1133 /* Hash table initialized to 0 */ 1134 for (r = CAS_MAC_HASH0; r <= CAS_MAC_HASH15; r += 4) 1135 bus_space_write_4(t, h, r, 0); 1136 1137 sc->sc_inited = 1; 1138 } 1139 1140 /* Counters need to be zeroed */ 1141 bus_space_write_4(t, h, CAS_MAC_NORM_COLL_CNT, 0); 1142 bus_space_write_4(t, h, CAS_MAC_FIRST_COLL_CNT, 0); 1143 bus_space_write_4(t, h, CAS_MAC_EXCESS_COLL_CNT, 0); 1144 bus_space_write_4(t, h, CAS_MAC_LATE_COLL_CNT, 0); 1145 bus_space_write_4(t, h, CAS_MAC_DEFER_TMR_CNT, 0); 1146 bus_space_write_4(t, h, CAS_MAC_PEAK_ATTEMPTS, 0); 1147 bus_space_write_4(t, h, CAS_MAC_RX_FRAME_COUNT, 0); 1148 bus_space_write_4(t, h, CAS_MAC_RX_LEN_ERR_CNT, 0); 1149 bus_space_write_4(t, h, CAS_MAC_RX_ALIGN_ERR, 0); 1150 bus_space_write_4(t, h, CAS_MAC_RX_CRC_ERR_CNT, 0); 1151 bus_space_write_4(t, h, CAS_MAC_RX_CODE_VIOL, 0); 1152 1153 /* Un-pause stuff */ 1154 bus_space_write_4(t, h, CAS_MAC_SEND_PAUSE_CMD, 0); 1155 1156 /* 1157 * Set the station address. 1158 */ 1159 bus_space_write_4(t, h, CAS_MAC_ADDR0, 1160 (sc->sc_arpcom.ac_enaddr[4]<<8) | sc->sc_arpcom.ac_enaddr[5]); 1161 bus_space_write_4(t, h, CAS_MAC_ADDR1, 1162 (sc->sc_arpcom.ac_enaddr[2]<<8) | sc->sc_arpcom.ac_enaddr[3]); 1163 bus_space_write_4(t, h, CAS_MAC_ADDR2, 1164 (sc->sc_arpcom.ac_enaddr[0]<<8) | sc->sc_arpcom.ac_enaddr[1]); 1165 } 1166 1167 /* 1168 * Receive interrupt. 1169 */ 1170 int 1171 cas_rint(struct cas_softc *sc) 1172 { 1173 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1174 bus_space_tag_t t = sc->sc_memt; 1175 bus_space_handle_t h = sc->sc_memh; 1176 struct cas_rxsoft *rxs; 1177 struct mbuf *m; 1178 u_int64_t word[4]; 1179 int len, off, idx; 1180 int i, skip; 1181 caddr_t cp; 1182 1183 for (i = sc->sc_rxptr;; i = CAS_NEXTRX(i + skip)) { 1184 CAS_CDRXCSYNC(sc, i, 1185 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1186 1187 word[0] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[0]); 1188 word[1] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[1]); 1189 word[2] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[2]); 1190 word[3] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[3]); 1191 1192 /* Stop if the hardware still owns the descriptor. */ 1193 if ((word[0] & CAS_RC0_TYPE) == 0 || word[3] & CAS_RC3_OWN) 1194 break; 1195 1196 len = CAS_RC1_HDR_LEN(word[1]); 1197 if (len > 0) { 1198 off = CAS_RC1_HDR_OFF(word[1]); 1199 idx = CAS_RC1_HDR_IDX(word[1]); 1200 rxs = &sc->sc_rxsoft[idx]; 1201 1202 DPRINTF(sc, ("hdr at idx %d, off %d, len %d\n", 1203 idx, off, len)); 1204 1205 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0, 1206 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1207 1208 cp = rxs->rxs_kva + off * 256 + ETHER_ALIGN; 1209 m = m_devget(cp, len, ETHER_ALIGN, ifp, NULL); 1210 1211 if (word[0] & CAS_RC0_RELEASE_HDR) 1212 cas_add_rxbuf(sc, idx); 1213 1214 if (m != NULL) { 1215 1216 #if NBPFILTER > 0 1217 /* 1218 * Pass this up to any BPF listeners, but only 1219 * pass it up the stack if its for us. 1220 */ 1221 if (ifp->if_bpf) 1222 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN); 1223 #endif /* NBPFILTER > 0 */ 1224 1225 ifp->if_ipackets++; 1226 ether_input_mbuf(ifp, m); 1227 } else 1228 ifp->if_ierrors++; 1229 } 1230 1231 len = CAS_RC0_DATA_LEN(word[0]); 1232 if (len > 0) { 1233 off = CAS_RC0_DATA_OFF(word[0]); 1234 idx = CAS_RC0_DATA_IDX(word[0]); 1235 rxs = &sc->sc_rxsoft[idx]; 1236 1237 DPRINTF(sc, ("data at idx %d, off %d, len %d\n", 1238 idx, off, len)); 1239 1240 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0, 1241 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1242 1243 /* XXX We should not be copying the packet here. */ 1244 cp = rxs->rxs_kva + off + ETHER_ALIGN; 1245 m = m_devget(cp, len, ETHER_ALIGN, ifp, NULL); 1246 1247 if (word[0] & CAS_RC0_RELEASE_DATA) 1248 cas_add_rxbuf(sc, idx); 1249 1250 if (m != NULL) { 1251 #if NBPFILTER > 0 1252 /* 1253 * Pass this up to any BPF listeners, but only 1254 * pass it up the stack if its for us. 1255 */ 1256 if (ifp->if_bpf) 1257 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN); 1258 #endif /* NBPFILTER > 0 */ 1259 1260 ifp->if_ipackets++; 1261 ether_input_mbuf(ifp, m); 1262 } else 1263 ifp->if_ierrors++; 1264 } 1265 1266 if (word[0] & CAS_RC0_SPLIT) 1267 printf("split packet\n"); 1268 1269 skip = CAS_RC0_SKIP(word[0]); 1270 } 1271 1272 while (sc->sc_rxptr != i) { 1273 sc->sc_rxcomps[sc->sc_rxptr].cc_word[0] = 0; 1274 sc->sc_rxcomps[sc->sc_rxptr].cc_word[1] = 0; 1275 sc->sc_rxcomps[sc->sc_rxptr].cc_word[2] = 0; 1276 sc->sc_rxcomps[sc->sc_rxptr].cc_word[3] = 1277 CAS_DMA_WRITE(CAS_RC3_OWN); 1278 CAS_CDRXCSYNC(sc, sc->sc_rxptr, 1279 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1280 1281 sc->sc_rxptr = CAS_NEXTRX(sc->sc_rxptr); 1282 } 1283 1284 bus_space_write_4(t, h, CAS_RX_COMP_TAIL, sc->sc_rxptr); 1285 1286 DPRINTF(sc, ("cas_rint: done sc->rxptr %d, complete %d\n", 1287 sc->sc_rxptr, bus_space_read_4(t, h, CAS_RX_COMPLETION))); 1288 1289 return (1); 1290 } 1291 1292 /* 1293 * cas_add_rxbuf: 1294 * 1295 * Add a receive buffer to the indicated descriptor. 1296 */ 1297 int 1298 cas_add_rxbuf(struct cas_softc *sc, int idx) 1299 { 1300 bus_space_tag_t t = sc->sc_memt; 1301 bus_space_handle_t h = sc->sc_memh; 1302 1303 CAS_INIT_RXDESC(sc, sc->sc_rxdptr, idx); 1304 1305 if ((sc->sc_rxdptr % 4) == 0) 1306 bus_space_write_4(t, h, CAS_RX_KICK, sc->sc_rxdptr); 1307 1308 if (++sc->sc_rxdptr == CAS_NRXDESC) 1309 sc->sc_rxdptr = 0; 1310 1311 return (0); 1312 } 1313 1314 int 1315 cas_eint(struct cas_softc *sc, u_int status) 1316 { 1317 if ((status & CAS_INTR_MIF) != 0) { 1318 #ifdef CAS_DEBUG 1319 printf("%s: link status changed\n", sc->sc_dev.dv_xname); 1320 #endif 1321 return (1); 1322 } 1323 1324 printf("%s: status=%b\n", sc->sc_dev.dv_xname, status, CAS_INTR_BITS); 1325 return (1); 1326 } 1327 1328 int 1329 cas_pint(struct cas_softc *sc) 1330 { 1331 bus_space_tag_t t = sc->sc_memt; 1332 bus_space_handle_t seb = sc->sc_memh; 1333 u_int32_t status; 1334 1335 status = bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS); 1336 status |= bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS); 1337 #ifdef CAS_DEBUG 1338 if (status) 1339 printf("%s: link status changed\n", sc->sc_dev.dv_xname); 1340 #endif 1341 return (1); 1342 } 1343 1344 int 1345 cas_intr(void *v) 1346 { 1347 struct cas_softc *sc = (struct cas_softc *)v; 1348 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1349 bus_space_tag_t t = sc->sc_memt; 1350 bus_space_handle_t seb = sc->sc_memh; 1351 u_int32_t status; 1352 int r = 0; 1353 1354 status = bus_space_read_4(t, seb, CAS_STATUS); 1355 DPRINTF(sc, ("%s: cas_intr: cplt %xstatus %b\n", 1356 sc->sc_dev.dv_xname, (status>>19), status, CAS_INTR_BITS)); 1357 1358 if ((status & CAS_INTR_PCS) != 0) 1359 r |= cas_pint(sc); 1360 1361 if ((status & (CAS_INTR_TX_TAG_ERR | CAS_INTR_RX_TAG_ERR | 1362 CAS_INTR_RX_COMP_FULL | CAS_INTR_BERR)) != 0) 1363 r |= cas_eint(sc, status); 1364 1365 if ((status & (CAS_INTR_TX_EMPTY | CAS_INTR_TX_INTME)) != 0) 1366 r |= cas_tint(sc, status); 1367 1368 if ((status & (CAS_INTR_RX_DONE | CAS_INTR_RX_NOBUF)) != 0) 1369 r |= cas_rint(sc); 1370 1371 /* We should eventually do more than just print out error stats. */ 1372 if (status & CAS_INTR_TX_MAC) { 1373 int txstat = bus_space_read_4(t, seb, CAS_MAC_TX_STATUS); 1374 #ifdef CAS_DEBUG 1375 if (txstat & ~CAS_MAC_TX_XMIT_DONE) 1376 printf("%s: MAC tx fault, status %x\n", 1377 sc->sc_dev.dv_xname, txstat); 1378 #endif 1379 if (txstat & (CAS_MAC_TX_UNDERRUN | CAS_MAC_TX_PKT_TOO_LONG)) 1380 cas_init(ifp); 1381 } 1382 if (status & CAS_INTR_RX_MAC) { 1383 int rxstat = bus_space_read_4(t, seb, CAS_MAC_RX_STATUS); 1384 #ifdef CAS_DEBUG 1385 if (rxstat & ~CAS_MAC_RX_DONE) 1386 printf("%s: MAC rx fault, status %x\n", 1387 sc->sc_dev.dv_xname, rxstat); 1388 #endif 1389 /* 1390 * On some chip revisions CAS_MAC_RX_OVERFLOW happen often 1391 * due to a silicon bug so handle them silently. 1392 */ 1393 if (rxstat & CAS_MAC_RX_OVERFLOW) { 1394 ifp->if_ierrors++; 1395 cas_init(ifp); 1396 } 1397 #ifdef CAS_DEBUG 1398 else if (rxstat & ~(CAS_MAC_RX_DONE | CAS_MAC_RX_FRAME_CNT)) 1399 printf("%s: MAC rx fault, status %x\n", 1400 sc->sc_dev.dv_xname, rxstat); 1401 #endif 1402 } 1403 return (r); 1404 } 1405 1406 1407 void 1408 cas_watchdog(struct ifnet *ifp) 1409 { 1410 struct cas_softc *sc = ifp->if_softc; 1411 1412 DPRINTF(sc, ("cas_watchdog: CAS_RX_CONFIG %x CAS_MAC_RX_STATUS %x " 1413 "CAS_MAC_RX_CONFIG %x\n", 1414 bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_RX_CONFIG), 1415 bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_MAC_RX_STATUS), 1416 bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_MAC_RX_CONFIG))); 1417 1418 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname); 1419 ++ifp->if_oerrors; 1420 1421 /* Try to get more packets going. */ 1422 cas_init(ifp); 1423 } 1424 1425 /* 1426 * Initialize the MII Management Interface 1427 */ 1428 void 1429 cas_mifinit(struct cas_softc *sc) 1430 { 1431 bus_space_tag_t t = sc->sc_memt; 1432 bus_space_handle_t mif = sc->sc_memh; 1433 1434 /* Configure the MIF in frame mode */ 1435 sc->sc_mif_config = bus_space_read_4(t, mif, CAS_MIF_CONFIG); 1436 sc->sc_mif_config &= ~CAS_MIF_CONFIG_BB_ENA; 1437 bus_space_write_4(t, mif, CAS_MIF_CONFIG, sc->sc_mif_config); 1438 } 1439 1440 /* 1441 * MII interface 1442 * 1443 * The Cassini MII interface supports at least three different operating modes: 1444 * 1445 * Bitbang mode is implemented using data, clock and output enable registers. 1446 * 1447 * Frame mode is implemented by loading a complete frame into the frame 1448 * register and polling the valid bit for completion. 1449 * 1450 * Polling mode uses the frame register but completion is indicated by 1451 * an interrupt. 1452 * 1453 */ 1454 int 1455 cas_mii_readreg(struct device *self, int phy, int reg) 1456 { 1457 struct cas_softc *sc = (void *)self; 1458 bus_space_tag_t t = sc->sc_memt; 1459 bus_space_handle_t mif = sc->sc_memh; 1460 int n; 1461 u_int32_t v; 1462 1463 #ifdef CAS_DEBUG 1464 if (sc->sc_debug) 1465 printf("cas_mii_readreg: phy %d reg %d\n", phy, reg); 1466 #endif 1467 1468 /* Construct the frame command */ 1469 v = (reg << CAS_MIF_REG_SHIFT) | (phy << CAS_MIF_PHY_SHIFT) | 1470 CAS_MIF_FRAME_READ; 1471 1472 bus_space_write_4(t, mif, CAS_MIF_FRAME, v); 1473 for (n = 0; n < 100; n++) { 1474 DELAY(1); 1475 v = bus_space_read_4(t, mif, CAS_MIF_FRAME); 1476 if (v & CAS_MIF_FRAME_TA0) 1477 return (v & CAS_MIF_FRAME_DATA); 1478 } 1479 1480 printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname); 1481 return (0); 1482 } 1483 1484 void 1485 cas_mii_writereg(struct device *self, int phy, int reg, int val) 1486 { 1487 struct cas_softc *sc = (void *)self; 1488 bus_space_tag_t t = sc->sc_memt; 1489 bus_space_handle_t mif = sc->sc_memh; 1490 int n; 1491 u_int32_t v; 1492 1493 #ifdef CAS_DEBUG 1494 if (sc->sc_debug) 1495 printf("cas_mii_writereg: phy %d reg %d val %x\n", 1496 phy, reg, val); 1497 #endif 1498 1499 /* Construct the frame command */ 1500 v = CAS_MIF_FRAME_WRITE | 1501 (phy << CAS_MIF_PHY_SHIFT) | 1502 (reg << CAS_MIF_REG_SHIFT) | 1503 (val & CAS_MIF_FRAME_DATA); 1504 1505 bus_space_write_4(t, mif, CAS_MIF_FRAME, v); 1506 for (n = 0; n < 100; n++) { 1507 DELAY(1); 1508 v = bus_space_read_4(t, mif, CAS_MIF_FRAME); 1509 if (v & CAS_MIF_FRAME_TA0) 1510 return; 1511 } 1512 1513 printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname); 1514 } 1515 1516 void 1517 cas_mii_statchg(struct device *dev) 1518 { 1519 struct cas_softc *sc = (void *)dev; 1520 #ifdef CAS_DEBUG 1521 int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media); 1522 #endif 1523 bus_space_tag_t t = sc->sc_memt; 1524 bus_space_handle_t mac = sc->sc_memh; 1525 u_int32_t v; 1526 1527 #ifdef CAS_DEBUG 1528 if (sc->sc_debug) 1529 printf("cas_mii_statchg: status change: phy = %d\n", 1530 sc->sc_phys[instance]); 1531 #endif 1532 1533 /* Set tx full duplex options */ 1534 bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, 0); 1535 delay(10000); /* reg must be cleared and delay before changing. */ 1536 v = CAS_MAC_TX_ENA_IPG0|CAS_MAC_TX_NGU|CAS_MAC_TX_NGU_LIMIT| 1537 CAS_MAC_TX_ENABLE; 1538 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) { 1539 v |= CAS_MAC_TX_IGN_CARRIER|CAS_MAC_TX_IGN_COLLIS; 1540 } 1541 bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, v); 1542 1543 /* XIF Configuration */ 1544 v = CAS_MAC_XIF_TX_MII_ENA; 1545 v |= CAS_MAC_XIF_LINK_LED; 1546 1547 /* MII needs echo disable if half duplex. */ 1548 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) 1549 /* turn on full duplex LED */ 1550 v |= CAS_MAC_XIF_FDPLX_LED; 1551 else 1552 /* half duplex -- disable echo */ 1553 v |= CAS_MAC_XIF_ECHO_DISABL; 1554 1555 switch (IFM_SUBTYPE(sc->sc_mii.mii_media_active)) { 1556 case IFM_1000_T: /* Gigabit using GMII interface */ 1557 case IFM_1000_SX: 1558 v |= CAS_MAC_XIF_GMII_MODE; 1559 break; 1560 default: 1561 v &= ~CAS_MAC_XIF_GMII_MODE; 1562 } 1563 bus_space_write_4(t, mac, CAS_MAC_XIF_CONFIG, v); 1564 } 1565 1566 int 1567 cas_pcs_readreg(struct device *self, int phy, int reg) 1568 { 1569 struct cas_softc *sc = (void *)self; 1570 bus_space_tag_t t = sc->sc_memt; 1571 bus_space_handle_t pcs = sc->sc_memh; 1572 1573 #ifdef CAS_DEBUG 1574 if (sc->sc_debug) 1575 printf("cas_pcs_readreg: phy %d reg %d\n", phy, reg); 1576 #endif 1577 1578 if (phy != CAS_PHYAD_EXTERNAL) 1579 return (0); 1580 1581 switch (reg) { 1582 case MII_BMCR: 1583 reg = CAS_MII_CONTROL; 1584 break; 1585 case MII_BMSR: 1586 reg = CAS_MII_STATUS; 1587 break; 1588 case MII_ANAR: 1589 reg = CAS_MII_ANAR; 1590 break; 1591 case MII_ANLPAR: 1592 reg = CAS_MII_ANLPAR; 1593 break; 1594 case MII_EXTSR: 1595 return (EXTSR_1000XFDX|EXTSR_1000XHDX); 1596 default: 1597 return (0); 1598 } 1599 1600 return bus_space_read_4(t, pcs, reg); 1601 } 1602 1603 void 1604 cas_pcs_writereg(struct device *self, int phy, int reg, int val) 1605 { 1606 struct cas_softc *sc = (void *)self; 1607 bus_space_tag_t t = sc->sc_memt; 1608 bus_space_handle_t pcs = sc->sc_memh; 1609 int reset = 0; 1610 1611 #ifdef CAS_DEBUG 1612 if (sc->sc_debug) 1613 printf("cas_pcs_writereg: phy %d reg %d val %x\n", 1614 phy, reg, val); 1615 #endif 1616 1617 if (phy != CAS_PHYAD_EXTERNAL) 1618 return; 1619 1620 if (reg == MII_ANAR) 1621 bus_space_write_4(t, pcs, CAS_MII_CONFIG, 0); 1622 1623 switch (reg) { 1624 case MII_BMCR: 1625 reset = (val & CAS_MII_CONTROL_RESET); 1626 reg = CAS_MII_CONTROL; 1627 break; 1628 case MII_BMSR: 1629 reg = CAS_MII_STATUS; 1630 break; 1631 case MII_ANAR: 1632 reg = CAS_MII_ANAR; 1633 break; 1634 case MII_ANLPAR: 1635 reg = CAS_MII_ANLPAR; 1636 break; 1637 default: 1638 return; 1639 } 1640 1641 bus_space_write_4(t, pcs, reg, val); 1642 1643 if (reset) 1644 cas_bitwait(sc, pcs, CAS_MII_CONTROL, CAS_MII_CONTROL_RESET, 0); 1645 1646 if (reg == CAS_MII_ANAR || reset) 1647 bus_space_write_4(t, pcs, CAS_MII_CONFIG, 1648 CAS_MII_CONFIG_ENABLE); 1649 } 1650 1651 int 1652 cas_mediachange(struct ifnet *ifp) 1653 { 1654 struct cas_softc *sc = ifp->if_softc; 1655 struct mii_data *mii = &sc->sc_mii; 1656 1657 if (mii->mii_instance) { 1658 struct mii_softc *miisc; 1659 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 1660 mii_phy_reset(miisc); 1661 } 1662 1663 return (mii_mediachg(&sc->sc_mii)); 1664 } 1665 1666 void 1667 cas_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 1668 { 1669 struct cas_softc *sc = ifp->if_softc; 1670 1671 mii_pollstat(&sc->sc_mii); 1672 ifmr->ifm_active = sc->sc_mii.mii_media_active; 1673 ifmr->ifm_status = sc->sc_mii.mii_media_status; 1674 } 1675 1676 /* 1677 * Process an ioctl request. 1678 */ 1679 int 1680 cas_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1681 { 1682 struct cas_softc *sc = ifp->if_softc; 1683 struct ifaddr *ifa = (struct ifaddr *)data; 1684 struct ifreq *ifr = (struct ifreq *)data; 1685 int s, error = 0; 1686 1687 s = splnet(); 1688 1689 switch (cmd) { 1690 case SIOCSIFADDR: 1691 ifp->if_flags |= IFF_UP; 1692 if ((ifp->if_flags & IFF_RUNNING) == 0) 1693 cas_init(ifp); 1694 #ifdef INET 1695 if (ifa->ifa_addr->sa_family == AF_INET) 1696 arp_ifinit(&sc->sc_arpcom, ifa); 1697 #endif 1698 break; 1699 1700 case SIOCSIFFLAGS: 1701 if (ifp->if_flags & IFF_UP) { 1702 if ((ifp->if_flags & IFF_RUNNING) && 1703 ((ifp->if_flags ^ sc->sc_if_flags) & 1704 (IFF_ALLMULTI | IFF_PROMISC)) != 0) 1705 cas_setladrf(sc); 1706 else { 1707 if ((ifp->if_flags & IFF_RUNNING) == 0) 1708 cas_init(ifp); 1709 } 1710 } else { 1711 if (ifp->if_flags & IFF_RUNNING) 1712 cas_stop(ifp, 1); 1713 } 1714 sc->sc_if_flags = ifp->if_flags; 1715 1716 #ifdef CAS_DEBUG 1717 sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0; 1718 #endif 1719 break; 1720 1721 case SIOCGIFMEDIA: 1722 case SIOCSIFMEDIA: 1723 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1724 break; 1725 1726 default: 1727 error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data); 1728 } 1729 1730 if (error == ENETRESET) { 1731 if (ifp->if_flags & IFF_RUNNING) 1732 cas_setladrf(sc); 1733 error = 0; 1734 } 1735 1736 splx(s); 1737 return (error); 1738 } 1739 1740 1741 void 1742 cas_shutdown(void *arg) 1743 { 1744 struct cas_softc *sc = (struct cas_softc *)arg; 1745 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1746 1747 cas_stop(ifp, 1); 1748 } 1749 1750 /* 1751 * Set up the logical address filter. 1752 */ 1753 void 1754 cas_setladrf(struct cas_softc *sc) 1755 { 1756 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1757 struct ether_multi *enm; 1758 struct ether_multistep step; 1759 struct arpcom *ac = &sc->sc_arpcom; 1760 bus_space_tag_t t = sc->sc_memt; 1761 bus_space_handle_t h = sc->sc_memh; 1762 u_int32_t crc, hash[16], v; 1763 int i; 1764 1765 /* Get current RX configuration */ 1766 v = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG); 1767 1768 1769 /* 1770 * Turn off promiscuous mode, promiscuous group mode (all multicast), 1771 * and hash filter. Depending on the case, the right bit will be 1772 * enabled. 1773 */ 1774 v &= ~(CAS_MAC_RX_PROMISCUOUS|CAS_MAC_RX_HASH_FILTER| 1775 CAS_MAC_RX_PROMISC_GRP); 1776 1777 if ((ifp->if_flags & IFF_PROMISC) != 0) { 1778 /* Turn on promiscuous mode */ 1779 v |= CAS_MAC_RX_PROMISCUOUS; 1780 ifp->if_flags |= IFF_ALLMULTI; 1781 goto chipit; 1782 } 1783 1784 /* 1785 * Set up multicast address filter by passing all multicast addresses 1786 * through a crc generator, and then using the high order 8 bits as an 1787 * index into the 256 bit logical address filter. The high order 4 1788 * bits selects the word, while the other 4 bits select the bit within 1789 * the word (where bit 0 is the MSB). 1790 */ 1791 1792 /* Clear hash table */ 1793 for (i = 0; i < 16; i++) 1794 hash[i] = 0; 1795 1796 1797 ETHER_FIRST_MULTI(step, ac, enm); 1798 while (enm != NULL) { 1799 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 1800 /* 1801 * We must listen to a range of multicast addresses. 1802 * For now, just accept all multicasts, rather than 1803 * trying to set only those filter bits needed to match 1804 * the range. (At this time, the only use of address 1805 * ranges is for IP multicast routing, for which the 1806 * range is big enough to require all bits set.) 1807 * XXX use the addr filter for this 1808 */ 1809 ifp->if_flags |= IFF_ALLMULTI; 1810 v |= CAS_MAC_RX_PROMISC_GRP; 1811 goto chipit; 1812 } 1813 1814 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN); 1815 1816 /* Just want the 8 most significant bits. */ 1817 crc >>= 24; 1818 1819 /* Set the corresponding bit in the filter. */ 1820 hash[crc >> 4] |= 1 << (15 - (crc & 15)); 1821 1822 ETHER_NEXT_MULTI(step, enm); 1823 } 1824 1825 v |= CAS_MAC_RX_HASH_FILTER; 1826 ifp->if_flags &= ~IFF_ALLMULTI; 1827 1828 /* Now load the hash table into the chip (if we are using it) */ 1829 for (i = 0; i < 16; i++) { 1830 bus_space_write_4(t, h, 1831 CAS_MAC_HASH0 + i * (CAS_MAC_HASH1-CAS_MAC_HASH0), 1832 hash[i]); 1833 } 1834 1835 chipit: 1836 bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, v); 1837 } 1838 1839 int 1840 cas_encap(struct cas_softc *sc, struct mbuf *mhead, u_int32_t *bixp) 1841 { 1842 u_int64_t flags; 1843 u_int32_t cur, frag, i; 1844 bus_dmamap_t map; 1845 1846 cur = frag = *bixp; 1847 map = sc->sc_txd[cur].sd_map; 1848 1849 if (bus_dmamap_load_mbuf(sc->sc_dmatag, map, mhead, 1850 BUS_DMA_NOWAIT) != 0) { 1851 return (ENOBUFS); 1852 } 1853 1854 if ((sc->sc_tx_cnt + map->dm_nsegs) > (CAS_NTXDESC - 2)) { 1855 bus_dmamap_unload(sc->sc_dmatag, map); 1856 return (ENOBUFS); 1857 } 1858 1859 bus_dmamap_sync(sc->sc_dmatag, map, 0, map->dm_mapsize, 1860 BUS_DMASYNC_PREWRITE); 1861 1862 for (i = 0; i < map->dm_nsegs; i++) { 1863 sc->sc_txdescs[frag].cd_addr = 1864 CAS_DMA_WRITE(map->dm_segs[i].ds_addr); 1865 flags = (map->dm_segs[i].ds_len & CAS_TD_BUFSIZE) | 1866 (i == 0 ? CAS_TD_START_OF_PACKET : 0) | 1867 ((i == (map->dm_nsegs - 1)) ? CAS_TD_END_OF_PACKET : 0); 1868 sc->sc_txdescs[frag].cd_flags = CAS_DMA_WRITE(flags); 1869 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cddmamap, 1870 CAS_CDTXOFF(frag), sizeof(struct cas_desc), 1871 BUS_DMASYNC_PREWRITE); 1872 cur = frag; 1873 if (++frag == CAS_NTXDESC) 1874 frag = 0; 1875 } 1876 1877 sc->sc_tx_cnt += map->dm_nsegs; 1878 sc->sc_txd[*bixp].sd_map = sc->sc_txd[cur].sd_map; 1879 sc->sc_txd[cur].sd_map = map; 1880 sc->sc_txd[cur].sd_mbuf = mhead; 1881 1882 bus_space_write_4(sc->sc_memt, sc->sc_memh, CAS_TX_KICK, frag); 1883 1884 *bixp = frag; 1885 1886 /* sync descriptors */ 1887 1888 return (0); 1889 } 1890 1891 /* 1892 * Transmit interrupt. 1893 */ 1894 int 1895 cas_tint(struct cas_softc *sc, u_int32_t status) 1896 { 1897 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1898 struct cas_sxd *sd; 1899 u_int32_t cons, comp; 1900 1901 comp = bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_TX_COMPLETION); 1902 cons = sc->sc_tx_cons; 1903 while (cons != comp) { 1904 sd = &sc->sc_txd[cons]; 1905 if (sd->sd_mbuf != NULL) { 1906 bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0, 1907 sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1908 bus_dmamap_unload(sc->sc_dmatag, sd->sd_map); 1909 m_freem(sd->sd_mbuf); 1910 sd->sd_mbuf = NULL; 1911 ifp->if_opackets++; 1912 } 1913 sc->sc_tx_cnt--; 1914 if (++cons == CAS_NTXDESC) 1915 cons = 0; 1916 } 1917 sc->sc_tx_cons = cons; 1918 1919 if (sc->sc_tx_cnt < CAS_NTXDESC - 2) 1920 ifp->if_flags &= ~IFF_OACTIVE; 1921 if (sc->sc_tx_cnt == 0) 1922 ifp->if_timer = 0; 1923 1924 cas_start(ifp); 1925 1926 return (1); 1927 } 1928 1929 void 1930 cas_start(struct ifnet *ifp) 1931 { 1932 struct cas_softc *sc = ifp->if_softc; 1933 struct mbuf *m; 1934 u_int32_t bix; 1935 1936 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 1937 return; 1938 1939 bix = sc->sc_tx_prod; 1940 while (sc->sc_txd[bix].sd_mbuf == NULL) { 1941 IFQ_POLL(&ifp->if_snd, m); 1942 if (m == NULL) 1943 break; 1944 1945 #if NBPFILTER > 0 1946 /* 1947 * If BPF is listening on this interface, let it see the 1948 * packet before we commit it to the wire. 1949 */ 1950 if (ifp->if_bpf) 1951 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 1952 #endif 1953 1954 /* 1955 * Encapsulate this packet and start it going... 1956 * or fail... 1957 */ 1958 if (cas_encap(sc, m, &bix)) { 1959 ifp->if_flags |= IFF_OACTIVE; 1960 break; 1961 } 1962 1963 IFQ_DEQUEUE(&ifp->if_snd, m); 1964 ifp->if_timer = 5; 1965 } 1966 1967 sc->sc_tx_prod = bix; 1968 } 1969