1 /* $NetBSD: if_pcn.c,v 1.49 2010/01/19 22:07:01 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Device driver for the AMD PCnet-PCI series of Ethernet 40 * chips: 41 * 42 * * Am79c970 PCnet-PCI Single-Chip Ethernet Controller for PCI 43 * Local Bus 44 * 45 * * Am79c970A PCnet-PCI II Single-Chip Full-Duplex Ethernet Controller 46 * for PCI Local Bus 47 * 48 * * Am79c971 PCnet-FAST Single-Chip Full-Duplex 10/100Mbps 49 * Ethernet Controller for PCI Local Bus 50 * 51 * * Am79c972 PCnet-FAST+ Enhanced 10/100Mbps PCI Ethernet Controller 52 * with OnNow Support 53 * 54 * * Am79c973/Am79c975 PCnet-FAST III Single-Chip 10/100Mbps PCI 55 * Ethernet Controller with Integrated PHY 56 * 57 * This also supports the virtual PCnet-PCI Ethernet interface found 58 * in VMware. 59 * 60 * TODO: 61 * 62 * * Split this into bus-specific and bus-independent portions. 63 * The core could also be used for the ILACC (Am79900) 32-bit 64 * Ethernet chip (XXX only if we use an ILACC-compatible SWSTYLE). 65 */ 66 67 #include <sys/cdefs.h> 68 __KERNEL_RCSID(0, "$NetBSD: if_pcn.c,v 1.49 2010/01/19 22:07:01 pooka Exp $"); 69 70 #include "rnd.h" 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/callout.h> 75 #include <sys/mbuf.h> 76 #include <sys/malloc.h> 77 #include <sys/kernel.h> 78 #include <sys/socket.h> 79 #include <sys/ioctl.h> 80 #include <sys/errno.h> 81 #include <sys/device.h> 82 #include <sys/queue.h> 83 84 #if NRND > 0 85 #include <sys/rnd.h> 86 #endif 87 88 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */ 89 90 #include <net/if.h> 91 #include <net/if_dl.h> 92 #include <net/if_media.h> 93 #include <net/if_ether.h> 94 95 #include <net/bpf.h> 96 97 #include <sys/bus.h> 98 #include <sys/intr.h> 99 #include <machine/endian.h> 100 101 #include <dev/mii/mii.h> 102 #include <dev/mii/miivar.h> 103 104 #include <dev/ic/am79900reg.h> 105 #include <dev/ic/lancereg.h> 106 107 #include <dev/pci/pcireg.h> 108 #include <dev/pci/pcivar.h> 109 #include <dev/pci/pcidevs.h> 110 111 #include <dev/pci/if_pcnreg.h> 112 113 /* 114 * Transmit descriptor list size. This is arbitrary, but allocate 115 * enough descriptors for 128 pending transmissions, and 4 segments 116 * per packet. This MUST work out to a power of 2. 117 * 118 * NOTE: We can't have any more than 512 Tx descriptors, SO BE CAREFUL! 119 * 120 * So we play a little trick here. We give each packet up to 16 121 * DMA segments, but only allocate the max of 512 descriptors. The 122 * transmit logic can deal with this, we just are hoping to sneak by. 123 */ 124 #define PCN_NTXSEGS 16 125 #define PCN_NTXSEGS_VMWARE 8 /* bug in VMware's emulation */ 126 127 #define PCN_TXQUEUELEN 128 128 #define PCN_TXQUEUELEN_MASK (PCN_TXQUEUELEN - 1) 129 #define PCN_NTXDESC 512 130 #define PCN_NTXDESC_MASK (PCN_NTXDESC - 1) 131 #define PCN_NEXTTX(x) (((x) + 1) & PCN_NTXDESC_MASK) 132 #define PCN_NEXTTXS(x) (((x) + 1) & PCN_TXQUEUELEN_MASK) 133 134 /* Tx interrupt every N + 1 packets. */ 135 #define PCN_TXINTR_MASK 7 136 137 /* 138 * Receive descriptor list size. We have one Rx buffer per incoming 139 * packet, so this logic is a little simpler. 140 */ 141 #define PCN_NRXDESC 128 142 #define PCN_NRXDESC_MASK (PCN_NRXDESC - 1) 143 #define PCN_NEXTRX(x) (((x) + 1) & PCN_NRXDESC_MASK) 144 145 /* 146 * Control structures are DMA'd to the PCnet chip. We allocate them in 147 * a single clump that maps to a single DMA segment to make several things 148 * easier. 149 */ 150 struct pcn_control_data { 151 /* The transmit descriptors. */ 152 struct letmd pcd_txdescs[PCN_NTXDESC]; 153 154 /* The receive descriptors. */ 155 struct lermd pcd_rxdescs[PCN_NRXDESC]; 156 157 /* The init block. */ 158 struct leinit pcd_initblock; 159 }; 160 161 #define PCN_CDOFF(x) offsetof(struct pcn_control_data, x) 162 #define PCN_CDTXOFF(x) PCN_CDOFF(pcd_txdescs[(x)]) 163 #define PCN_CDRXOFF(x) PCN_CDOFF(pcd_rxdescs[(x)]) 164 #define PCN_CDINITOFF PCN_CDOFF(pcd_initblock) 165 166 /* 167 * Software state for transmit jobs. 168 */ 169 struct pcn_txsoft { 170 struct mbuf *txs_mbuf; /* head of our mbuf chain */ 171 bus_dmamap_t txs_dmamap; /* our DMA map */ 172 int txs_firstdesc; /* first descriptor in packet */ 173 int txs_lastdesc; /* last descriptor in packet */ 174 }; 175 176 /* 177 * Software state for receive jobs. 178 */ 179 struct pcn_rxsoft { 180 struct mbuf *rxs_mbuf; /* head of our mbuf chain */ 181 bus_dmamap_t rxs_dmamap; /* our DMA map */ 182 }; 183 184 /* 185 * Description of Rx FIFO watermarks for various revisions. 186 */ 187 static const char * const pcn_79c970_rcvfw[] = { 188 "16 bytes", 189 "64 bytes", 190 "128 bytes", 191 NULL, 192 }; 193 194 static const char * const pcn_79c971_rcvfw[] = { 195 "16 bytes", 196 "64 bytes", 197 "112 bytes", 198 NULL, 199 }; 200 201 /* 202 * Description of Tx start points for various revisions. 203 */ 204 static const char * const pcn_79c970_xmtsp[] = { 205 "8 bytes", 206 "64 bytes", 207 "128 bytes", 208 "248 bytes", 209 }; 210 211 static const char * const pcn_79c971_xmtsp[] = { 212 "20 bytes", 213 "64 bytes", 214 "128 bytes", 215 "248 bytes", 216 }; 217 218 static const char * const pcn_79c971_xmtsp_sram[] = { 219 "44 bytes", 220 "64 bytes", 221 "128 bytes", 222 "store-and-forward", 223 }; 224 225 /* 226 * Description of Tx FIFO watermarks for various revisions. 227 */ 228 static const char * const pcn_79c970_xmtfw[] = { 229 "16 bytes", 230 "64 bytes", 231 "128 bytes", 232 NULL, 233 }; 234 235 static const char * const pcn_79c971_xmtfw[] = { 236 "16 bytes", 237 "64 bytes", 238 "108 bytes", 239 NULL, 240 }; 241 242 /* 243 * Software state per device. 244 */ 245 struct pcn_softc { 246 device_t sc_dev; /* generic device information */ 247 bus_space_tag_t sc_st; /* bus space tag */ 248 bus_space_handle_t sc_sh; /* bus space handle */ 249 bus_dma_tag_t sc_dmat; /* bus DMA tag */ 250 struct ethercom sc_ethercom; /* Ethernet common data */ 251 252 /* Points to our media routines, etc. */ 253 const struct pcn_variant *sc_variant; 254 255 void *sc_ih; /* interrupt cookie */ 256 257 struct mii_data sc_mii; /* MII/media information */ 258 259 callout_t sc_tick_ch; /* tick callout */ 260 261 bus_dmamap_t sc_cddmamap; /* control data DMA map */ 262 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr 263 264 /* Software state for transmit and receive descriptors. */ 265 struct pcn_txsoft sc_txsoft[PCN_TXQUEUELEN]; 266 struct pcn_rxsoft sc_rxsoft[PCN_NRXDESC]; 267 268 /* Control data structures */ 269 struct pcn_control_data *sc_control_data; 270 #define sc_txdescs sc_control_data->pcd_txdescs 271 #define sc_rxdescs sc_control_data->pcd_rxdescs 272 #define sc_initblock sc_control_data->pcd_initblock 273 274 #ifdef PCN_EVENT_COUNTERS 275 /* Event counters. */ 276 struct evcnt sc_ev_txsstall; /* Tx stalled due to no txs */ 277 struct evcnt sc_ev_txdstall; /* Tx stalled due to no txd */ 278 struct evcnt sc_ev_txintr; /* Tx interrupts */ 279 struct evcnt sc_ev_rxintr; /* Rx interrupts */ 280 struct evcnt sc_ev_babl; /* BABL in pcn_intr() */ 281 struct evcnt sc_ev_miss; /* MISS in pcn_intr() */ 282 struct evcnt sc_ev_merr; /* MERR in pcn_intr() */ 283 284 struct evcnt sc_ev_txseg1; /* Tx packets w/ 1 segment */ 285 struct evcnt sc_ev_txseg2; /* Tx packets w/ 2 segments */ 286 struct evcnt sc_ev_txseg3; /* Tx packets w/ 3 segments */ 287 struct evcnt sc_ev_txseg4; /* Tx packets w/ 4 segments */ 288 struct evcnt sc_ev_txseg5; /* Tx packets w/ 5 segments */ 289 struct evcnt sc_ev_txsegmore; /* Tx packets w/ more than 5 segments */ 290 struct evcnt sc_ev_txcopy; /* Tx copies required */ 291 #endif /* PCN_EVENT_COUNTERS */ 292 293 const char * const *sc_rcvfw_desc; /* Rx FIFO watermark info */ 294 int sc_rcvfw; 295 296 const char * const *sc_xmtsp_desc; /* Tx start point info */ 297 int sc_xmtsp; 298 299 const char * const *sc_xmtfw_desc; /* Tx FIFO watermark info */ 300 int sc_xmtfw; 301 302 int sc_flags; /* misc. flags; see below */ 303 int sc_swstyle; /* the software style in use */ 304 305 int sc_txfree; /* number of free Tx descriptors */ 306 int sc_txnext; /* next ready Tx descriptor */ 307 308 int sc_txsfree; /* number of free Tx jobs */ 309 int sc_txsnext; /* next free Tx job */ 310 int sc_txsdirty; /* dirty Tx jobs */ 311 312 int sc_rxptr; /* next ready Rx descriptor/job */ 313 314 uint32_t sc_csr5; /* prototype CSR5 register */ 315 uint32_t sc_mode; /* prototype MODE register */ 316 317 #if NRND > 0 318 rndsource_element_t rnd_source; /* random source */ 319 #endif 320 }; 321 322 /* sc_flags */ 323 #define PCN_F_HAS_MII 0x0001 /* has MII */ 324 325 #ifdef PCN_EVENT_COUNTERS 326 #define PCN_EVCNT_INCR(ev) (ev)->ev_count++ 327 #else 328 #define PCN_EVCNT_INCR(ev) /* nothing */ 329 #endif 330 331 #define PCN_CDTXADDR(sc, x) ((sc)->sc_cddma + PCN_CDTXOFF((x))) 332 #define PCN_CDRXADDR(sc, x) ((sc)->sc_cddma + PCN_CDRXOFF((x))) 333 #define PCN_CDINITADDR(sc) ((sc)->sc_cddma + PCN_CDINITOFF) 334 335 #define PCN_CDTXSYNC(sc, x, n, ops) \ 336 do { \ 337 int __x, __n; \ 338 \ 339 __x = (x); \ 340 __n = (n); \ 341 \ 342 /* If it will wrap around, sync to the end of the ring. */ \ 343 if ((__x + __n) > PCN_NTXDESC) { \ 344 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \ 345 PCN_CDTXOFF(__x), sizeof(struct letmd) * \ 346 (PCN_NTXDESC - __x), (ops)); \ 347 __n -= (PCN_NTXDESC - __x); \ 348 __x = 0; \ 349 } \ 350 \ 351 /* Now sync whatever is left. */ \ 352 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \ 353 PCN_CDTXOFF(__x), sizeof(struct letmd) * __n, (ops)); \ 354 } while (/*CONSTCOND*/0) 355 356 #define PCN_CDRXSYNC(sc, x, ops) \ 357 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \ 358 PCN_CDRXOFF((x)), sizeof(struct lermd), (ops)) 359 360 #define PCN_CDINITSYNC(sc, ops) \ 361 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \ 362 PCN_CDINITOFF, sizeof(struct leinit), (ops)) 363 364 #define PCN_INIT_RXDESC(sc, x) \ 365 do { \ 366 struct pcn_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \ 367 struct lermd *__rmd = &(sc)->sc_rxdescs[(x)]; \ 368 struct mbuf *__m = __rxs->rxs_mbuf; \ 369 \ 370 /* \ 371 * Note: We scoot the packet forward 2 bytes in the buffer \ 372 * so that the payload after the Ethernet header is aligned \ 373 * to a 4-byte boundary. \ 374 */ \ 375 __m->m_data = __m->m_ext.ext_buf + 2; \ 376 \ 377 if ((sc)->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) { \ 378 __rmd->rmd2 = \ 379 htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 2); \ 380 __rmd->rmd0 = 0; \ 381 } else { \ 382 __rmd->rmd2 = 0; \ 383 __rmd->rmd0 = \ 384 htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 2); \ 385 } \ 386 __rmd->rmd1 = htole32(LE_R1_OWN|LE_R1_ONES| \ 387 (LE_BCNT(MCLBYTES - 2) & LE_R1_BCNT_MASK)); \ 388 PCN_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);\ 389 } while(/*CONSTCOND*/0) 390 391 static void pcn_start(struct ifnet *); 392 static void pcn_watchdog(struct ifnet *); 393 static int pcn_ioctl(struct ifnet *, u_long, void *); 394 static int pcn_init(struct ifnet *); 395 static void pcn_stop(struct ifnet *, int); 396 397 static bool pcn_shutdown(device_t, int); 398 399 static void pcn_reset(struct pcn_softc *); 400 static void pcn_rxdrain(struct pcn_softc *); 401 static int pcn_add_rxbuf(struct pcn_softc *, int); 402 static void pcn_tick(void *); 403 404 static void pcn_spnd(struct pcn_softc *); 405 406 static void pcn_set_filter(struct pcn_softc *); 407 408 static int pcn_intr(void *); 409 static void pcn_txintr(struct pcn_softc *); 410 static int pcn_rxintr(struct pcn_softc *); 411 412 static int pcn_mii_readreg(device_t, int, int); 413 static void pcn_mii_writereg(device_t, int, int, int); 414 static void pcn_mii_statchg(device_t); 415 416 static void pcn_79c970_mediainit(struct pcn_softc *); 417 static int pcn_79c970_mediachange(struct ifnet *); 418 static void pcn_79c970_mediastatus(struct ifnet *, struct ifmediareq *); 419 420 static void pcn_79c971_mediainit(struct pcn_softc *); 421 422 /* 423 * Description of a PCnet-PCI variant. Used to select media access 424 * method, mostly, and to print a nice description of the chip. 425 */ 426 static const struct pcn_variant { 427 const char *pcv_desc; 428 void (*pcv_mediainit)(struct pcn_softc *); 429 uint16_t pcv_chipid; 430 } pcn_variants[] = { 431 { "Am79c970 PCnet-PCI", 432 pcn_79c970_mediainit, 433 PARTID_Am79c970 }, 434 435 { "Am79c970A PCnet-PCI II", 436 pcn_79c970_mediainit, 437 PARTID_Am79c970A }, 438 439 { "Am79c971 PCnet-FAST", 440 pcn_79c971_mediainit, 441 PARTID_Am79c971 }, 442 443 { "Am79c972 PCnet-FAST+", 444 pcn_79c971_mediainit, 445 PARTID_Am79c972 }, 446 447 { "Am79c973 PCnet-FAST III", 448 pcn_79c971_mediainit, 449 PARTID_Am79c973 }, 450 451 { "Am79c975 PCnet-FAST III", 452 pcn_79c971_mediainit, 453 PARTID_Am79c975 }, 454 455 { "Unknown PCnet-PCI variant", 456 pcn_79c971_mediainit, 457 0 }, 458 }; 459 460 int pcn_copy_small = 0; 461 462 static int pcn_match(device_t, cfdata_t, void *); 463 static void pcn_attach(device_t, device_t, void *); 464 465 CFATTACH_DECL_NEW(pcn, sizeof(struct pcn_softc), 466 pcn_match, pcn_attach, NULL, NULL); 467 468 /* 469 * Routines to read and write the PCnet-PCI CSR/BCR space. 470 */ 471 472 static inline uint32_t 473 pcn_csr_read(struct pcn_softc *sc, int reg) 474 { 475 476 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg); 477 return (bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_RDP)); 478 } 479 480 static inline void 481 pcn_csr_write(struct pcn_softc *sc, int reg, uint32_t val) 482 { 483 484 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg); 485 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RDP, val); 486 } 487 488 static inline uint32_t 489 pcn_bcr_read(struct pcn_softc *sc, int reg) 490 { 491 492 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg); 493 return (bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_BDP)); 494 } 495 496 static inline void 497 pcn_bcr_write(struct pcn_softc *sc, int reg, uint32_t val) 498 { 499 500 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg); 501 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_BDP, val); 502 } 503 504 static bool 505 pcn_is_vmware(const char *enaddr) 506 { 507 508 /* 509 * VMware uses the OUI 00:0c:29 for auto-generated MAC 510 * addresses. 511 */ 512 if (enaddr[0] == 0x00 && enaddr[1] == 0x0c && enaddr[2] == 0x29) 513 return (TRUE); 514 515 /* 516 * VMware uses the OUI 00:50:56 for manually-set MAC 517 * addresses (and some auto-generated ones). 518 */ 519 if (enaddr[0] == 0x00 && enaddr[1] == 0x50 && enaddr[2] == 0x56) 520 return (TRUE); 521 522 return (FALSE); 523 } 524 525 static const struct pcn_variant * 526 pcn_lookup_variant(uint16_t chipid) 527 { 528 const struct pcn_variant *pcv; 529 530 for (pcv = pcn_variants; pcv->pcv_chipid != 0; pcv++) { 531 if (chipid == pcv->pcv_chipid) 532 return (pcv); 533 } 534 535 /* 536 * This covers unknown chips, which we simply treat like 537 * a generic PCnet-FAST. 538 */ 539 return (pcv); 540 } 541 542 static int 543 pcn_match(device_t parent, cfdata_t cf, void *aux) 544 { 545 struct pci_attach_args *pa = aux; 546 547 /* 548 * IBM Makes a PCI variant of this card which shows up as a 549 * Trident Microsystems 4DWAVE DX (ethernet network, revision 0x25) 550 * this card is truly a pcn card, so we have a special case match for 551 * it 552 */ 553 554 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TRIDENT && 555 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TRIDENT_4DWAVE_DX && 556 PCI_CLASS(pa->pa_class) == PCI_CLASS_NETWORK) 557 return(1); 558 559 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD) 560 return (0); 561 562 switch (PCI_PRODUCT(pa->pa_id)) { 563 case PCI_PRODUCT_AMD_PCNET_PCI: 564 /* Beat if_le_pci.c */ 565 return (10); 566 } 567 568 return (0); 569 } 570 571 static void 572 pcn_attach(device_t parent, device_t self, void *aux) 573 { 574 struct pcn_softc *sc = device_private(self); 575 struct pci_attach_args *pa = aux; 576 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 577 pci_chipset_tag_t pc = pa->pa_pc; 578 pci_intr_handle_t ih; 579 const char *intrstr = NULL; 580 bus_space_tag_t iot, memt; 581 bus_space_handle_t ioh, memh; 582 bus_dma_segment_t seg; 583 int ioh_valid, memh_valid; 584 int ntxsegs, i, rseg, error; 585 uint32_t chipid, reg; 586 uint8_t enaddr[ETHER_ADDR_LEN]; 587 prop_object_t obj; 588 bool is_vmware; 589 590 sc->sc_dev = self; 591 callout_init(&sc->sc_tick_ch, 0); 592 593 aprint_normal(": AMD PCnet-PCI Ethernet\n"); 594 595 /* 596 * Map the device. 597 */ 598 ioh_valid = (pci_mapreg_map(pa, PCN_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 599 &iot, &ioh, NULL, NULL) == 0); 600 memh_valid = (pci_mapreg_map(pa, PCN_PCI_CBMEM, 601 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0, 602 &memt, &memh, NULL, NULL) == 0); 603 604 if (memh_valid) { 605 sc->sc_st = memt; 606 sc->sc_sh = memh; 607 } else if (ioh_valid) { 608 sc->sc_st = iot; 609 sc->sc_sh = ioh; 610 } else { 611 aprint_error_dev(self, "unable to map device registers\n"); 612 return; 613 } 614 615 sc->sc_dmat = pa->pa_dmat; 616 617 /* Make sure bus mastering is enabled. */ 618 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 619 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) | 620 PCI_COMMAND_MASTER_ENABLE); 621 622 /* power up chip */ 623 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self, 624 NULL)) && error != EOPNOTSUPP) { 625 aprint_error_dev(self, "cannot activate %d\n", error); 626 return; 627 } 628 629 /* 630 * Reset the chip to a known state. This also puts the 631 * chip into 32-bit mode. 632 */ 633 pcn_reset(sc); 634 635 /* 636 * On some systems with the chip is an on-board device, the 637 * EEPROM is not used. Handle this by reading the MAC address 638 * from the CSRs (assuming that boot firmware has written 639 * it there). 640 */ 641 obj = prop_dictionary_get(device_properties(sc->sc_dev), 642 "am79c970-no-eeprom"); 643 if (prop_bool_true(obj)) { 644 for (i = 0; i < 3; i++) { 645 uint32_t val; 646 val = pcn_csr_read(sc, LE_CSR12 + i); 647 enaddr[2 * i] = val & 0xff; 648 enaddr[2 * i + 1] = (val >> 8) & 0xff; 649 } 650 } else { 651 for (i = 0; i < ETHER_ADDR_LEN; i++) { 652 enaddr[i] = bus_space_read_1(sc->sc_st, sc->sc_sh, 653 PCN32_APROM + i); 654 } 655 } 656 657 /* Check to see if this is a VMware emulated network interface. */ 658 is_vmware = pcn_is_vmware(enaddr); 659 660 /* 661 * Now that the device is mapped, attempt to figure out what 662 * kind of chip we have. Note that IDL has all 32 bits of 663 * the chip ID when we're in 32-bit mode. 664 */ 665 chipid = pcn_csr_read(sc, LE_CSR88); 666 sc->sc_variant = pcn_lookup_variant(CHIPID_PARTID(chipid)); 667 668 aprint_normal_dev(self, "%s rev %d, Ethernet address %s\n", 669 sc->sc_variant->pcv_desc, CHIPID_VER(chipid), 670 ether_sprintf(enaddr)); 671 672 /* 673 * VMware has a bug in its network interface emulation; we must 674 * limit the number of Tx segments. 675 */ 676 if (is_vmware) { 677 ntxsegs = PCN_NTXSEGS_VMWARE; 678 prop_dictionary_set_bool(device_properties(sc->sc_dev), 679 "am79c970-vmware-tx-bug", TRUE); 680 aprint_verbose_dev(self, 681 "VMware Tx segment count bug detected\n"); 682 } else { 683 ntxsegs = PCN_NTXSEGS; 684 } 685 686 /* 687 * Map and establish our interrupt. 688 */ 689 if (pci_intr_map(pa, &ih)) { 690 aprint_error_dev(self, "unable to map interrupt\n"); 691 return; 692 } 693 intrstr = pci_intr_string(pc, ih); 694 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, pcn_intr, sc); 695 if (sc->sc_ih == NULL) { 696 aprint_error_dev(self, "unable to establish interrupt"); 697 if (intrstr != NULL) 698 aprint_error(" at %s", intrstr); 699 aprint_error("\n"); 700 return; 701 } 702 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 703 704 /* 705 * Allocate the control data structures, and create and load the 706 * DMA map for it. 707 */ 708 if ((error = bus_dmamem_alloc(sc->sc_dmat, 709 sizeof(struct pcn_control_data), PAGE_SIZE, 0, &seg, 1, &rseg, 710 0)) != 0) { 711 aprint_error_dev(self, "unable to allocate control data, " 712 "error = %d\n", error); 713 goto fail_0; 714 } 715 716 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, 717 sizeof(struct pcn_control_data), (void **)&sc->sc_control_data, 718 BUS_DMA_COHERENT)) != 0) { 719 aprint_error_dev(self, "unable to map control data, " 720 "error = %d\n", error); 721 goto fail_1; 722 } 723 724 if ((error = bus_dmamap_create(sc->sc_dmat, 725 sizeof(struct pcn_control_data), 1, 726 sizeof(struct pcn_control_data), 0, 0, &sc->sc_cddmamap)) != 0) { 727 aprint_error_dev(self, "unable to create control data DMA map, " 728 "error = %d\n", error); 729 goto fail_2; 730 } 731 732 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap, 733 sc->sc_control_data, sizeof(struct pcn_control_data), NULL, 734 0)) != 0) { 735 aprint_error_dev(self, 736 "unable to load control data DMA map, error = %d\n", error); 737 goto fail_3; 738 } 739 740 /* Create the transmit buffer DMA maps. */ 741 for (i = 0; i < PCN_TXQUEUELEN; i++) { 742 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 743 ntxsegs, MCLBYTES, 0, 0, 744 &sc->sc_txsoft[i].txs_dmamap)) != 0) { 745 aprint_error_dev(self, 746 "unable to create tx DMA map %d, error = %d\n", 747 i, error); 748 goto fail_4; 749 } 750 } 751 752 /* Create the receive buffer DMA maps. */ 753 for (i = 0; i < PCN_NRXDESC; i++) { 754 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, 755 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) { 756 aprint_error_dev(self, 757 "unable to create rx DMA map %d, error = %d\n", 758 i, error); 759 goto fail_5; 760 } 761 sc->sc_rxsoft[i].rxs_mbuf = NULL; 762 } 763 764 /* Initialize our media structures. */ 765 (*sc->sc_variant->pcv_mediainit)(sc); 766 767 /* 768 * Initialize FIFO watermark info. 769 */ 770 switch (sc->sc_variant->pcv_chipid) { 771 case PARTID_Am79c970: 772 case PARTID_Am79c970A: 773 sc->sc_rcvfw_desc = pcn_79c970_rcvfw; 774 sc->sc_xmtsp_desc = pcn_79c970_xmtsp; 775 sc->sc_xmtfw_desc = pcn_79c970_xmtfw; 776 break; 777 778 default: 779 sc->sc_rcvfw_desc = pcn_79c971_rcvfw; 780 /* 781 * Read BCR25 to determine how much SRAM is 782 * on the board. If > 0, then we the chip 783 * uses different Start Point thresholds. 784 * 785 * Note BCR25 and BCR26 are loaded from the 786 * EEPROM on RST, and unaffected by S_RESET, 787 * so we don't really have to worry about 788 * them except for this. 789 */ 790 reg = pcn_bcr_read(sc, LE_BCR25) & 0x00ff; 791 if (reg != 0) 792 sc->sc_xmtsp_desc = pcn_79c971_xmtsp_sram; 793 else 794 sc->sc_xmtsp_desc = pcn_79c971_xmtsp; 795 sc->sc_xmtfw_desc = pcn_79c971_xmtfw; 796 break; 797 } 798 799 /* 800 * Set up defaults -- see the tables above for what these 801 * values mean. 802 * 803 * XXX How should we tune RCVFW and XMTFW? 804 */ 805 sc->sc_rcvfw = 1; /* minimum for full-duplex */ 806 sc->sc_xmtsp = 1; 807 sc->sc_xmtfw = 0; 808 809 ifp = &sc->sc_ethercom.ec_if; 810 strcpy(ifp->if_xname, device_xname(self)); 811 ifp->if_softc = sc; 812 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 813 ifp->if_ioctl = pcn_ioctl; 814 ifp->if_start = pcn_start; 815 ifp->if_watchdog = pcn_watchdog; 816 ifp->if_init = pcn_init; 817 ifp->if_stop = pcn_stop; 818 IFQ_SET_READY(&ifp->if_snd); 819 820 /* Attach the interface. */ 821 if_attach(ifp); 822 ether_ifattach(ifp, enaddr); 823 #if NRND > 0 824 rnd_attach_source(&sc->rnd_source, device_xname(self), 825 RND_TYPE_NET, 0); 826 #endif 827 828 #ifdef PCN_EVENT_COUNTERS 829 /* Attach event counters. */ 830 evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC, 831 NULL, device_xname(self), "txsstall"); 832 evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC, 833 NULL, device_xname(self), "txdstall"); 834 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR, 835 NULL, device_xname(self), "txintr"); 836 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR, 837 NULL, device_xname(self), "rxintr"); 838 evcnt_attach_dynamic(&sc->sc_ev_babl, EVCNT_TYPE_MISC, 839 NULL, device_xname(self), "babl"); 840 evcnt_attach_dynamic(&sc->sc_ev_miss, EVCNT_TYPE_MISC, 841 NULL, device_xname(self), "miss"); 842 evcnt_attach_dynamic(&sc->sc_ev_merr, EVCNT_TYPE_MISC, 843 NULL, device_xname(self), "merr"); 844 845 evcnt_attach_dynamic(&sc->sc_ev_txseg1, EVCNT_TYPE_MISC, 846 NULL, device_xname(self), "txseg1"); 847 evcnt_attach_dynamic(&sc->sc_ev_txseg2, EVCNT_TYPE_MISC, 848 NULL, device_xname(self), "txseg2"); 849 evcnt_attach_dynamic(&sc->sc_ev_txseg3, EVCNT_TYPE_MISC, 850 NULL, device_xname(self), "txseg3"); 851 evcnt_attach_dynamic(&sc->sc_ev_txseg4, EVCNT_TYPE_MISC, 852 NULL, device_xname(self), "txseg4"); 853 evcnt_attach_dynamic(&sc->sc_ev_txseg5, EVCNT_TYPE_MISC, 854 NULL, device_xname(self), "txseg5"); 855 evcnt_attach_dynamic(&sc->sc_ev_txsegmore, EVCNT_TYPE_MISC, 856 NULL, device_xname(self), "txsegmore"); 857 evcnt_attach_dynamic(&sc->sc_ev_txcopy, EVCNT_TYPE_MISC, 858 NULL, device_xname(self), "txcopy"); 859 #endif /* PCN_EVENT_COUNTERS */ 860 861 /* 862 * Establish power handler with shutdown hook, to make sure 863 * the interface is shutdown during reboot. 864 */ 865 if (pmf_device_register1(self, NULL, NULL, pcn_shutdown)) 866 pmf_class_network_register(self, ifp); 867 else 868 aprint_error_dev(self, "couldn't establish power handler\n"); 869 870 return; 871 872 /* 873 * Free any resources we've allocated during the failed attach 874 * attempt. Do this in reverse order and fall through. 875 */ 876 fail_5: 877 for (i = 0; i < PCN_NRXDESC; i++) { 878 if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 879 bus_dmamap_destroy(sc->sc_dmat, 880 sc->sc_rxsoft[i].rxs_dmamap); 881 } 882 fail_4: 883 for (i = 0; i < PCN_TXQUEUELEN; i++) { 884 if (sc->sc_txsoft[i].txs_dmamap != NULL) 885 bus_dmamap_destroy(sc->sc_dmat, 886 sc->sc_txsoft[i].txs_dmamap); 887 } 888 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap); 889 fail_3: 890 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap); 891 fail_2: 892 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data, 893 sizeof(struct pcn_control_data)); 894 fail_1: 895 bus_dmamem_free(sc->sc_dmat, &seg, rseg); 896 fail_0: 897 return; 898 } 899 900 /* 901 * pcn_shutdown: 902 * 903 * Make sure the interface is stopped at reboot time. 904 */ 905 static bool 906 pcn_shutdown(device_t self, int howto) 907 { 908 struct pcn_softc *sc = device_private(self); 909 910 pcn_stop(&sc->sc_ethercom.ec_if, 1); 911 /* explicitly reset the chip for some onboard one with lazy firmware */ 912 pcn_reset(sc); 913 914 return true; 915 } 916 917 /* 918 * pcn_start: [ifnet interface function] 919 * 920 * Start packet transmission on the interface. 921 */ 922 static void 923 pcn_start(struct ifnet *ifp) 924 { 925 struct pcn_softc *sc = ifp->if_softc; 926 struct mbuf *m0, *m; 927 struct pcn_txsoft *txs; 928 bus_dmamap_t dmamap; 929 int error, nexttx, lasttx = -1, ofree, seg; 930 931 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 932 return; 933 934 /* 935 * Remember the previous number of free descriptors and 936 * the first descriptor we'll use. 937 */ 938 ofree = sc->sc_txfree; 939 940 /* 941 * Loop through the send queue, setting up transmit descriptors 942 * until we drain the queue, or use up all available transmit 943 * descriptors. 944 */ 945 for (;;) { 946 /* Grab a packet off the queue. */ 947 IFQ_POLL(&ifp->if_snd, m0); 948 if (m0 == NULL) 949 break; 950 m = NULL; 951 952 /* Get a work queue entry. */ 953 if (sc->sc_txsfree == 0) { 954 PCN_EVCNT_INCR(&sc->sc_ev_txsstall); 955 break; 956 } 957 958 txs = &sc->sc_txsoft[sc->sc_txsnext]; 959 dmamap = txs->txs_dmamap; 960 961 /* 962 * Load the DMA map. If this fails, the packet either 963 * didn't fit in the alloted number of segments, or we 964 * were short on resources. In this case, we'll copy 965 * and try again. 966 */ 967 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0, 968 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) { 969 PCN_EVCNT_INCR(&sc->sc_ev_txcopy); 970 MGETHDR(m, M_DONTWAIT, MT_DATA); 971 if (m == NULL) { 972 printf("%s: unable to allocate Tx mbuf\n", 973 device_xname(sc->sc_dev)); 974 break; 975 } 976 if (m0->m_pkthdr.len > MHLEN) { 977 MCLGET(m, M_DONTWAIT); 978 if ((m->m_flags & M_EXT) == 0) { 979 printf("%s: unable to allocate Tx " 980 "cluster\n", 981 device_xname(sc->sc_dev)); 982 m_freem(m); 983 break; 984 } 985 } 986 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *)); 987 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len; 988 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, 989 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT); 990 if (error) { 991 printf("%s: unable to load Tx buffer, " 992 "error = %d\n", device_xname(sc->sc_dev), 993 error); 994 break; 995 } 996 } 997 998 /* 999 * Ensure we have enough descriptors free to describe 1000 * the packet. Note, we always reserve one descriptor 1001 * at the end of the ring as a termination point, to 1002 * prevent wrap-around. 1003 */ 1004 if (dmamap->dm_nsegs > (sc->sc_txfree - 1)) { 1005 /* 1006 * Not enough free descriptors to transmit this 1007 * packet. We haven't committed anything yet, 1008 * so just unload the DMA map, put the packet 1009 * back on the queue, and punt. Notify the upper 1010 * layer that there are not more slots left. 1011 * 1012 * XXX We could allocate an mbuf and copy, but 1013 * XXX is it worth it? 1014 */ 1015 ifp->if_flags |= IFF_OACTIVE; 1016 bus_dmamap_unload(sc->sc_dmat, dmamap); 1017 if (m != NULL) 1018 m_freem(m); 1019 PCN_EVCNT_INCR(&sc->sc_ev_txdstall); 1020 break; 1021 } 1022 1023 IFQ_DEQUEUE(&ifp->if_snd, m0); 1024 if (m != NULL) { 1025 m_freem(m0); 1026 m0 = m; 1027 } 1028 1029 /* 1030 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. 1031 */ 1032 1033 /* Sync the DMA map. */ 1034 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize, 1035 BUS_DMASYNC_PREWRITE); 1036 1037 #ifdef PCN_EVENT_COUNTERS 1038 switch (dmamap->dm_nsegs) { 1039 case 1: 1040 PCN_EVCNT_INCR(&sc->sc_ev_txseg1); 1041 break; 1042 case 2: 1043 PCN_EVCNT_INCR(&sc->sc_ev_txseg2); 1044 break; 1045 case 3: 1046 PCN_EVCNT_INCR(&sc->sc_ev_txseg3); 1047 break; 1048 case 4: 1049 PCN_EVCNT_INCR(&sc->sc_ev_txseg4); 1050 break; 1051 case 5: 1052 PCN_EVCNT_INCR(&sc->sc_ev_txseg5); 1053 break; 1054 default: 1055 PCN_EVCNT_INCR(&sc->sc_ev_txsegmore); 1056 break; 1057 } 1058 #endif /* PCN_EVENT_COUNTERS */ 1059 1060 /* 1061 * Initialize the transmit descriptors. 1062 */ 1063 if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) { 1064 for (nexttx = sc->sc_txnext, seg = 0; 1065 seg < dmamap->dm_nsegs; 1066 seg++, nexttx = PCN_NEXTTX(nexttx)) { 1067 /* 1068 * If this is the first descriptor we're 1069 * enqueueing, don't set the OWN bit just 1070 * yet. That could cause a race condition. 1071 * We'll do it below. 1072 */ 1073 sc->sc_txdescs[nexttx].tmd0 = 0; 1074 sc->sc_txdescs[nexttx].tmd2 = 1075 htole32(dmamap->dm_segs[seg].ds_addr); 1076 sc->sc_txdescs[nexttx].tmd1 = 1077 htole32(LE_T1_ONES | 1078 (nexttx == sc->sc_txnext ? 0 : LE_T1_OWN) | 1079 (LE_BCNT(dmamap->dm_segs[seg].ds_len) & 1080 LE_T1_BCNT_MASK)); 1081 lasttx = nexttx; 1082 } 1083 } else { 1084 for (nexttx = sc->sc_txnext, seg = 0; 1085 seg < dmamap->dm_nsegs; 1086 seg++, nexttx = PCN_NEXTTX(nexttx)) { 1087 /* 1088 * If this is the first descriptor we're 1089 * enqueueing, don't set the OWN bit just 1090 * yet. That could cause a race condition. 1091 * We'll do it below. 1092 */ 1093 sc->sc_txdescs[nexttx].tmd0 = 1094 htole32(dmamap->dm_segs[seg].ds_addr); 1095 sc->sc_txdescs[nexttx].tmd2 = 0; 1096 sc->sc_txdescs[nexttx].tmd1 = 1097 htole32(LE_T1_ONES | 1098 (nexttx == sc->sc_txnext ? 0 : LE_T1_OWN) | 1099 (LE_BCNT(dmamap->dm_segs[seg].ds_len) & 1100 LE_T1_BCNT_MASK)); 1101 lasttx = nexttx; 1102 } 1103 } 1104 1105 KASSERT(lasttx != -1); 1106 /* Interrupt on the packet, if appropriate. */ 1107 if ((sc->sc_txsnext & PCN_TXINTR_MASK) == 0) 1108 sc->sc_txdescs[lasttx].tmd1 |= htole32(LE_T1_LTINT); 1109 1110 /* Set `start of packet' and `end of packet' appropriately. */ 1111 sc->sc_txdescs[lasttx].tmd1 |= htole32(LE_T1_ENP); 1112 sc->sc_txdescs[sc->sc_txnext].tmd1 |= 1113 htole32(LE_T1_OWN|LE_T1_STP); 1114 1115 /* Sync the descriptors we're using. */ 1116 PCN_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs, 1117 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1118 1119 /* Kick the transmitter. */ 1120 pcn_csr_write(sc, LE_CSR0, LE_C0_INEA|LE_C0_TDMD); 1121 1122 /* 1123 * Store a pointer to the packet so we can free it later, 1124 * and remember what txdirty will be once the packet is 1125 * done. 1126 */ 1127 txs->txs_mbuf = m0; 1128 txs->txs_firstdesc = sc->sc_txnext; 1129 txs->txs_lastdesc = lasttx; 1130 1131 /* Advance the tx pointer. */ 1132 sc->sc_txfree -= dmamap->dm_nsegs; 1133 sc->sc_txnext = nexttx; 1134 1135 sc->sc_txsfree--; 1136 sc->sc_txsnext = PCN_NEXTTXS(sc->sc_txsnext); 1137 1138 /* Pass the packet to any BPF listeners. */ 1139 if (ifp->if_bpf) 1140 bpf_ops->bpf_mtap(ifp->if_bpf, m0); 1141 } 1142 1143 if (sc->sc_txsfree == 0 || sc->sc_txfree == 0) { 1144 /* No more slots left; notify upper layer. */ 1145 ifp->if_flags |= IFF_OACTIVE; 1146 } 1147 1148 if (sc->sc_txfree != ofree) { 1149 /* Set a watchdog timer in case the chip flakes out. */ 1150 ifp->if_timer = 5; 1151 } 1152 } 1153 1154 /* 1155 * pcn_watchdog: [ifnet interface function] 1156 * 1157 * Watchdog timer handler. 1158 */ 1159 static void 1160 pcn_watchdog(struct ifnet *ifp) 1161 { 1162 struct pcn_softc *sc = ifp->if_softc; 1163 1164 /* 1165 * Since we're not interrupting every packet, sweep 1166 * up before we report an error. 1167 */ 1168 pcn_txintr(sc); 1169 1170 if (sc->sc_txfree != PCN_NTXDESC) { 1171 printf("%s: device timeout (txfree %d txsfree %d)\n", 1172 device_xname(sc->sc_dev), sc->sc_txfree, sc->sc_txsfree); 1173 ifp->if_oerrors++; 1174 1175 /* Reset the interface. */ 1176 (void) pcn_init(ifp); 1177 } 1178 1179 /* Try to get more packets going. */ 1180 pcn_start(ifp); 1181 } 1182 1183 /* 1184 * pcn_ioctl: [ifnet interface function] 1185 * 1186 * Handle control requests from the operator. 1187 */ 1188 static int 1189 pcn_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1190 { 1191 struct pcn_softc *sc = ifp->if_softc; 1192 struct ifreq *ifr = (struct ifreq *) data; 1193 int s, error; 1194 1195 s = splnet(); 1196 1197 switch (cmd) { 1198 case SIOCSIFMEDIA: 1199 case SIOCGIFMEDIA: 1200 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 1201 break; 1202 1203 default: 1204 error = ether_ioctl(ifp, cmd, data); 1205 if (error == ENETRESET) { 1206 /* 1207 * Multicast list has changed; set the hardware filter 1208 * accordingly. 1209 */ 1210 if (ifp->if_flags & IFF_RUNNING) 1211 error = pcn_init(ifp); 1212 else 1213 error = 0; 1214 } 1215 break; 1216 } 1217 1218 /* Try to get more packets going. */ 1219 pcn_start(ifp); 1220 1221 splx(s); 1222 return (error); 1223 } 1224 1225 /* 1226 * pcn_intr: 1227 * 1228 * Interrupt service routine. 1229 */ 1230 static int 1231 pcn_intr(void *arg) 1232 { 1233 struct pcn_softc *sc = arg; 1234 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1235 uint32_t csr0; 1236 int wantinit, handled = 0; 1237 1238 for (wantinit = 0; wantinit == 0;) { 1239 csr0 = pcn_csr_read(sc, LE_CSR0); 1240 if ((csr0 & LE_C0_INTR) == 0) 1241 break; 1242 1243 #if NRND > 0 1244 if (RND_ENABLED(&sc->rnd_source)) 1245 rnd_add_uint32(&sc->rnd_source, csr0); 1246 #endif 1247 1248 /* ACK the bits and re-enable interrupts. */ 1249 pcn_csr_write(sc, LE_CSR0, csr0 & 1250 (LE_C0_INEA|LE_C0_BABL|LE_C0_MISS|LE_C0_MERR|LE_C0_RINT| 1251 LE_C0_TINT|LE_C0_IDON)); 1252 1253 handled = 1; 1254 1255 if (csr0 & LE_C0_RINT) { 1256 PCN_EVCNT_INCR(&sc->sc_ev_rxintr); 1257 wantinit = pcn_rxintr(sc); 1258 } 1259 1260 if (csr0 & LE_C0_TINT) { 1261 PCN_EVCNT_INCR(&sc->sc_ev_txintr); 1262 pcn_txintr(sc); 1263 } 1264 1265 if (csr0 & LE_C0_ERR) { 1266 if (csr0 & LE_C0_BABL) { 1267 PCN_EVCNT_INCR(&sc->sc_ev_babl); 1268 ifp->if_oerrors++; 1269 } 1270 if (csr0 & LE_C0_MISS) { 1271 PCN_EVCNT_INCR(&sc->sc_ev_miss); 1272 ifp->if_ierrors++; 1273 } 1274 if (csr0 & LE_C0_MERR) { 1275 PCN_EVCNT_INCR(&sc->sc_ev_merr); 1276 printf("%s: memory error\n", 1277 device_xname(sc->sc_dev)); 1278 wantinit = 1; 1279 break; 1280 } 1281 } 1282 1283 if ((csr0 & LE_C0_RXON) == 0) { 1284 printf("%s: receiver disabled\n", 1285 device_xname(sc->sc_dev)); 1286 ifp->if_ierrors++; 1287 wantinit = 1; 1288 } 1289 1290 if ((csr0 & LE_C0_TXON) == 0) { 1291 printf("%s: transmitter disabled\n", 1292 device_xname(sc->sc_dev)); 1293 ifp->if_oerrors++; 1294 wantinit = 1; 1295 } 1296 } 1297 1298 if (handled) { 1299 if (wantinit) 1300 pcn_init(ifp); 1301 1302 /* Try to get more packets going. */ 1303 pcn_start(ifp); 1304 } 1305 1306 return (handled); 1307 } 1308 1309 /* 1310 * pcn_spnd: 1311 * 1312 * Suspend the chip. 1313 */ 1314 static void 1315 pcn_spnd(struct pcn_softc *sc) 1316 { 1317 int i; 1318 1319 pcn_csr_write(sc, LE_CSR5, sc->sc_csr5 | LE_C5_SPND); 1320 1321 for (i = 0; i < 10000; i++) { 1322 if (pcn_csr_read(sc, LE_CSR5) & LE_C5_SPND) 1323 return; 1324 delay(5); 1325 } 1326 1327 printf("%s: WARNING: chip failed to enter suspended state\n", 1328 device_xname(sc->sc_dev)); 1329 } 1330 1331 /* 1332 * pcn_txintr: 1333 * 1334 * Helper; handle transmit interrupts. 1335 */ 1336 static void 1337 pcn_txintr(struct pcn_softc *sc) 1338 { 1339 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1340 struct pcn_txsoft *txs; 1341 uint32_t tmd1, tmd2, tmd; 1342 int i, j; 1343 1344 ifp->if_flags &= ~IFF_OACTIVE; 1345 1346 /* 1347 * Go through our Tx list and free mbufs for those 1348 * frames which have been transmitted. 1349 */ 1350 for (i = sc->sc_txsdirty; sc->sc_txsfree != PCN_TXQUEUELEN; 1351 i = PCN_NEXTTXS(i), sc->sc_txsfree++) { 1352 txs = &sc->sc_txsoft[i]; 1353 1354 PCN_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs, 1355 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1356 1357 tmd1 = le32toh(sc->sc_txdescs[txs->txs_lastdesc].tmd1); 1358 if (tmd1 & LE_T1_OWN) 1359 break; 1360 1361 /* 1362 * Slightly annoying -- we have to loop through the 1363 * descriptors we've used looking for ERR, since it 1364 * can appear on any descriptor in the chain. 1365 */ 1366 for (j = txs->txs_firstdesc;; j = PCN_NEXTTX(j)) { 1367 tmd = le32toh(sc->sc_txdescs[j].tmd1); 1368 if (tmd & LE_T1_ERR) { 1369 ifp->if_oerrors++; 1370 if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) 1371 tmd2 = le32toh(sc->sc_txdescs[j].tmd0); 1372 else 1373 tmd2 = le32toh(sc->sc_txdescs[j].tmd2); 1374 if (tmd2 & LE_T2_UFLO) { 1375 if (sc->sc_xmtsp < LE_C80_XMTSP_MAX) { 1376 sc->sc_xmtsp++; 1377 printf("%s: transmit " 1378 "underrun; new threshold: " 1379 "%s\n", 1380 device_xname(sc->sc_dev), 1381 sc->sc_xmtsp_desc[ 1382 sc->sc_xmtsp]); 1383 pcn_spnd(sc); 1384 pcn_csr_write(sc, LE_CSR80, 1385 LE_C80_RCVFW(sc->sc_rcvfw) | 1386 LE_C80_XMTSP(sc->sc_xmtsp) | 1387 LE_C80_XMTFW(sc->sc_xmtfw)); 1388 pcn_csr_write(sc, LE_CSR5, 1389 sc->sc_csr5); 1390 } else { 1391 printf("%s: transmit " 1392 "underrun\n", 1393 device_xname(sc->sc_dev)); 1394 } 1395 } else if (tmd2 & LE_T2_BUFF) { 1396 printf("%s: transmit buffer error\n", 1397 device_xname(sc->sc_dev)); 1398 } 1399 if (tmd2 & LE_T2_LCOL) 1400 ifp->if_collisions++; 1401 if (tmd2 & LE_T2_RTRY) 1402 ifp->if_collisions += 16; 1403 goto next_packet; 1404 } 1405 if (j == txs->txs_lastdesc) 1406 break; 1407 } 1408 if (tmd1 & LE_T1_ONE) 1409 ifp->if_collisions++; 1410 else if (tmd & LE_T1_MORE) { 1411 /* Real number is unknown. */ 1412 ifp->if_collisions += 2; 1413 } 1414 ifp->if_opackets++; 1415 next_packet: 1416 sc->sc_txfree += txs->txs_dmamap->dm_nsegs; 1417 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap, 1418 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1419 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap); 1420 m_freem(txs->txs_mbuf); 1421 txs->txs_mbuf = NULL; 1422 } 1423 1424 /* Update the dirty transmit buffer pointer. */ 1425 sc->sc_txsdirty = i; 1426 1427 /* 1428 * If there are no more pending transmissions, cancel the watchdog 1429 * timer. 1430 */ 1431 if (sc->sc_txsfree == PCN_TXQUEUELEN) 1432 ifp->if_timer = 0; 1433 } 1434 1435 /* 1436 * pcn_rxintr: 1437 * 1438 * Helper; handle receive interrupts. 1439 */ 1440 static int 1441 pcn_rxintr(struct pcn_softc *sc) 1442 { 1443 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1444 struct pcn_rxsoft *rxs; 1445 struct mbuf *m; 1446 uint32_t rmd1; 1447 int i, len; 1448 1449 for (i = sc->sc_rxptr;; i = PCN_NEXTRX(i)) { 1450 rxs = &sc->sc_rxsoft[i]; 1451 1452 PCN_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1453 1454 rmd1 = le32toh(sc->sc_rxdescs[i].rmd1); 1455 1456 if (rmd1 & LE_R1_OWN) 1457 break; 1458 1459 /* 1460 * Check for errors and make sure the packet fit into 1461 * a single buffer. We have structured this block of 1462 * code the way it is in order to compress it into 1463 * one test in the common case (no error). 1464 */ 1465 if (__predict_false((rmd1 & (LE_R1_STP|LE_R1_ENP|LE_R1_ERR)) != 1466 (LE_R1_STP|LE_R1_ENP))) { 1467 /* Make sure the packet is in a single buffer. */ 1468 if ((rmd1 & (LE_R1_STP|LE_R1_ENP)) != 1469 (LE_R1_STP|LE_R1_ENP)) { 1470 printf("%s: packet spilled into next buffer\n", 1471 device_xname(sc->sc_dev)); 1472 return (1); /* pcn_intr() will re-init */ 1473 } 1474 1475 /* 1476 * If the packet had an error, simple recycle the 1477 * buffer. 1478 */ 1479 if (rmd1 & LE_R1_ERR) { 1480 ifp->if_ierrors++; 1481 /* 1482 * If we got an overflow error, chances 1483 * are there will be a CRC error. In 1484 * this case, just print the overflow 1485 * error, and skip the others. 1486 */ 1487 if (rmd1 & LE_R1_OFLO) 1488 printf("%s: overflow error\n", 1489 device_xname(sc->sc_dev)); 1490 else { 1491 #define PRINTIT(x, str) \ 1492 if (rmd1 & (x)) \ 1493 printf("%s: %s\n", \ 1494 device_xname(sc->sc_dev), \ 1495 str); 1496 PRINTIT(LE_R1_FRAM, "framing error"); 1497 PRINTIT(LE_R1_CRC, "CRC error"); 1498 PRINTIT(LE_R1_BUFF, "buffer error"); 1499 } 1500 #undef PRINTIT 1501 PCN_INIT_RXDESC(sc, i); 1502 continue; 1503 } 1504 } 1505 1506 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 1507 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1508 1509 /* 1510 * No errors; receive the packet. 1511 */ 1512 if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) 1513 len = le32toh(sc->sc_rxdescs[i].rmd0) & LE_R1_BCNT_MASK; 1514 else 1515 len = le32toh(sc->sc_rxdescs[i].rmd2) & LE_R1_BCNT_MASK; 1516 1517 /* 1518 * The LANCE family includes the CRC with every packet; 1519 * trim it off here. 1520 */ 1521 len -= ETHER_CRC_LEN; 1522 1523 /* 1524 * If the packet is small enough to fit in a 1525 * single header mbuf, allocate one and copy 1526 * the data into it. This greatly reduces 1527 * memory consumption when we receive lots 1528 * of small packets. 1529 * 1530 * Otherwise, we add a new buffer to the receive 1531 * chain. If this fails, we drop the packet and 1532 * recycle the old buffer. 1533 */ 1534 if (pcn_copy_small != 0 && len <= (MHLEN - 2)) { 1535 MGETHDR(m, M_DONTWAIT, MT_DATA); 1536 if (m == NULL) 1537 goto dropit; 1538 m->m_data += 2; 1539 memcpy(mtod(m, void *), 1540 mtod(rxs->rxs_mbuf, void *), len); 1541 PCN_INIT_RXDESC(sc, i); 1542 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 1543 rxs->rxs_dmamap->dm_mapsize, 1544 BUS_DMASYNC_PREREAD); 1545 } else { 1546 m = rxs->rxs_mbuf; 1547 if (pcn_add_rxbuf(sc, i) != 0) { 1548 dropit: 1549 ifp->if_ierrors++; 1550 PCN_INIT_RXDESC(sc, i); 1551 bus_dmamap_sync(sc->sc_dmat, 1552 rxs->rxs_dmamap, 0, 1553 rxs->rxs_dmamap->dm_mapsize, 1554 BUS_DMASYNC_PREREAD); 1555 continue; 1556 } 1557 } 1558 1559 m->m_pkthdr.rcvif = ifp; 1560 m->m_pkthdr.len = m->m_len = len; 1561 1562 /* Pass this up to any BPF listeners. */ 1563 if (ifp->if_bpf) 1564 bpf_ops->bpf_mtap(ifp->if_bpf, m); 1565 1566 /* Pass it on. */ 1567 (*ifp->if_input)(ifp, m); 1568 ifp->if_ipackets++; 1569 } 1570 1571 /* Update the receive pointer. */ 1572 sc->sc_rxptr = i; 1573 return (0); 1574 } 1575 1576 /* 1577 * pcn_tick: 1578 * 1579 * One second timer, used to tick the MII. 1580 */ 1581 static void 1582 pcn_tick(void *arg) 1583 { 1584 struct pcn_softc *sc = arg; 1585 int s; 1586 1587 s = splnet(); 1588 mii_tick(&sc->sc_mii); 1589 splx(s); 1590 1591 callout_reset(&sc->sc_tick_ch, hz, pcn_tick, sc); 1592 } 1593 1594 /* 1595 * pcn_reset: 1596 * 1597 * Perform a soft reset on the PCnet-PCI. 1598 */ 1599 static void 1600 pcn_reset(struct pcn_softc *sc) 1601 { 1602 1603 /* 1604 * The PCnet-PCI chip is reset by reading from the 1605 * RESET register. Note that while the NE2100 LANCE 1606 * boards require a write after the read, the PCnet-PCI 1607 * chips do not require this. 1608 * 1609 * Since we don't know if we're in 16-bit or 32-bit 1610 * mode right now, issue both (it's safe) in the 1611 * hopes that one will succeed. 1612 */ 1613 (void) bus_space_read_2(sc->sc_st, sc->sc_sh, PCN16_RESET); 1614 (void) bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_RESET); 1615 1616 /* Wait 1ms for it to finish. */ 1617 delay(1000); 1618 1619 /* 1620 * Select 32-bit I/O mode by issuing a 32-bit write to the 1621 * RDP. Since the RAP is 0 after a reset, writing a 0 1622 * to RDP is safe (since it simply clears CSR0). 1623 */ 1624 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RDP, 0); 1625 } 1626 1627 /* 1628 * pcn_init: [ifnet interface function] 1629 * 1630 * Initialize the interface. Must be called at splnet(). 1631 */ 1632 static int 1633 pcn_init(struct ifnet *ifp) 1634 { 1635 struct pcn_softc *sc = ifp->if_softc; 1636 struct pcn_rxsoft *rxs; 1637 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 1638 int i, error = 0; 1639 uint32_t reg; 1640 1641 /* Cancel any pending I/O. */ 1642 pcn_stop(ifp, 0); 1643 1644 /* Reset the chip to a known state. */ 1645 pcn_reset(sc); 1646 1647 /* 1648 * On the Am79c970, select SSTYLE 2, and SSTYLE 3 on everything 1649 * else. 1650 * 1651 * XXX It'd be really nice to use SSTYLE 2 on all the chips, 1652 * because the structure layout is compatible with ILACC, 1653 * but the burst mode is only available in SSTYLE 3, and 1654 * burst mode should provide some performance enhancement. 1655 */ 1656 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970) 1657 sc->sc_swstyle = LE_B20_SSTYLE_PCNETPCI2; 1658 else 1659 sc->sc_swstyle = LE_B20_SSTYLE_PCNETPCI3; 1660 pcn_bcr_write(sc, LE_BCR20, sc->sc_swstyle); 1661 1662 /* Initialize the transmit descriptor ring. */ 1663 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs)); 1664 PCN_CDTXSYNC(sc, 0, PCN_NTXDESC, 1665 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1666 sc->sc_txfree = PCN_NTXDESC; 1667 sc->sc_txnext = 0; 1668 1669 /* Initialize the transmit job descriptors. */ 1670 for (i = 0; i < PCN_TXQUEUELEN; i++) 1671 sc->sc_txsoft[i].txs_mbuf = NULL; 1672 sc->sc_txsfree = PCN_TXQUEUELEN; 1673 sc->sc_txsnext = 0; 1674 sc->sc_txsdirty = 0; 1675 1676 /* 1677 * Initialize the receive descriptor and receive job 1678 * descriptor rings. 1679 */ 1680 for (i = 0; i < PCN_NRXDESC; i++) { 1681 rxs = &sc->sc_rxsoft[i]; 1682 if (rxs->rxs_mbuf == NULL) { 1683 if ((error = pcn_add_rxbuf(sc, i)) != 0) { 1684 printf("%s: unable to allocate or map rx " 1685 "buffer %d, error = %d\n", 1686 device_xname(sc->sc_dev), i, error); 1687 /* 1688 * XXX Should attempt to run with fewer receive 1689 * XXX buffers instead of just failing. 1690 */ 1691 pcn_rxdrain(sc); 1692 goto out; 1693 } 1694 } else 1695 PCN_INIT_RXDESC(sc, i); 1696 } 1697 sc->sc_rxptr = 0; 1698 1699 /* Initialize MODE for the initialization block. */ 1700 sc->sc_mode = 0; 1701 if (ifp->if_flags & IFF_PROMISC) 1702 sc->sc_mode |= LE_C15_PROM; 1703 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1704 sc->sc_mode |= LE_C15_DRCVBC; 1705 1706 /* 1707 * If we have MII, simply select MII in the MODE register, 1708 * and clear ASEL. Otherwise, let ASEL stand (for now), 1709 * and leave PORTSEL alone (it is ignored with ASEL is set). 1710 */ 1711 if (sc->sc_flags & PCN_F_HAS_MII) { 1712 pcn_bcr_write(sc, LE_BCR2, 1713 pcn_bcr_read(sc, LE_BCR2) & ~LE_B2_ASEL); 1714 sc->sc_mode |= LE_C15_PORTSEL(PORTSEL_MII); 1715 1716 /* 1717 * Disable MII auto-negotiation. We handle that in 1718 * our own MII layer. 1719 */ 1720 pcn_bcr_write(sc, LE_BCR32, 1721 pcn_bcr_read(sc, LE_BCR32) | LE_B32_DANAS); 1722 } 1723 1724 /* 1725 * Set the Tx and Rx descriptor ring addresses in the init 1726 * block, the TLEN and RLEN other fields of the init block 1727 * MODE register. 1728 */ 1729 sc->sc_initblock.init_rdra = htole32(PCN_CDRXADDR(sc, 0)); 1730 sc->sc_initblock.init_tdra = htole32(PCN_CDTXADDR(sc, 0)); 1731 sc->sc_initblock.init_mode = htole32(sc->sc_mode | 1732 ((ffs(PCN_NTXDESC) - 1) << 28) | 1733 ((ffs(PCN_NRXDESC) - 1) << 20)); 1734 1735 /* Set the station address in the init block. */ 1736 sc->sc_initblock.init_padr[0] = htole32(enaddr[0] | 1737 (enaddr[1] << 8) | (enaddr[2] << 16) | (enaddr[3] << 24)); 1738 sc->sc_initblock.init_padr[1] = htole32(enaddr[4] | 1739 (enaddr[5] << 8)); 1740 1741 /* Set the multicast filter in the init block. */ 1742 pcn_set_filter(sc); 1743 1744 /* Initialize CSR3. */ 1745 pcn_csr_write(sc, LE_CSR3, LE_C3_MISSM|LE_C3_IDONM|LE_C3_DXSUFLO); 1746 1747 /* Initialize CSR4. */ 1748 pcn_csr_write(sc, LE_CSR4, LE_C4_DMAPLUS|LE_C4_APAD_XMT| 1749 LE_C4_MFCOM|LE_C4_RCVCCOM|LE_C4_TXSTRTM); 1750 1751 /* Initialize CSR5. */ 1752 sc->sc_csr5 = LE_C5_LTINTEN|LE_C5_SINTE; 1753 pcn_csr_write(sc, LE_CSR5, sc->sc_csr5); 1754 1755 /* 1756 * If we have an Am79c971 or greater, initialize CSR7. 1757 * 1758 * XXX Might be nice to use the MII auto-poll interrupt someday. 1759 */ 1760 switch (sc->sc_variant->pcv_chipid) { 1761 case PARTID_Am79c970: 1762 case PARTID_Am79c970A: 1763 /* Not available on these chips. */ 1764 break; 1765 1766 default: 1767 pcn_csr_write(sc, LE_CSR7, LE_C7_FASTSPNDE); 1768 break; 1769 } 1770 1771 /* 1772 * On the Am79c970A and greater, initialize BCR18 to 1773 * enable burst mode. 1774 * 1775 * Also enable the "no underflow" option on the Am79c971 and 1776 * higher, which prevents the chip from generating transmit 1777 * underflows, yet sill provides decent performance. Note if 1778 * chip is not connected to external SRAM, then we still have 1779 * to handle underflow errors (the NOUFLO bit is ignored in 1780 * that case). 1781 */ 1782 reg = pcn_bcr_read(sc, LE_BCR18); 1783 switch (sc->sc_variant->pcv_chipid) { 1784 case PARTID_Am79c970: 1785 break; 1786 1787 case PARTID_Am79c970A: 1788 reg |= LE_B18_BREADE|LE_B18_BWRITE; 1789 break; 1790 1791 default: 1792 reg |= LE_B18_BREADE|LE_B18_BWRITE|LE_B18_NOUFLO; 1793 break; 1794 } 1795 pcn_bcr_write(sc, LE_BCR18, reg); 1796 1797 /* 1798 * Initialize CSR80 (FIFO thresholds for Tx and Rx). 1799 */ 1800 pcn_csr_write(sc, LE_CSR80, LE_C80_RCVFW(sc->sc_rcvfw) | 1801 LE_C80_XMTSP(sc->sc_xmtsp) | LE_C80_XMTFW(sc->sc_xmtfw)); 1802 1803 /* 1804 * Send the init block to the chip, and wait for it 1805 * to be processed. 1806 */ 1807 PCN_CDINITSYNC(sc, BUS_DMASYNC_PREWRITE); 1808 pcn_csr_write(sc, LE_CSR1, PCN_CDINITADDR(sc) & 0xffff); 1809 pcn_csr_write(sc, LE_CSR2, (PCN_CDINITADDR(sc) >> 16) & 0xffff); 1810 pcn_csr_write(sc, LE_CSR0, LE_C0_INIT); 1811 delay(100); 1812 for (i = 0; i < 10000; i++) { 1813 if (pcn_csr_read(sc, LE_CSR0) & LE_C0_IDON) 1814 break; 1815 delay(10); 1816 } 1817 PCN_CDINITSYNC(sc, BUS_DMASYNC_POSTWRITE); 1818 if (i == 10000) { 1819 printf("%s: timeout processing init block\n", 1820 device_xname(sc->sc_dev)); 1821 error = EIO; 1822 goto out; 1823 } 1824 1825 /* Set the media. */ 1826 if ((error = mii_ifmedia_change(&sc->sc_mii)) != 0) 1827 goto out; 1828 1829 /* Enable interrupts and external activity (and ACK IDON). */ 1830 pcn_csr_write(sc, LE_CSR0, LE_C0_INEA|LE_C0_STRT|LE_C0_IDON); 1831 1832 if (sc->sc_flags & PCN_F_HAS_MII) { 1833 /* Start the one second MII clock. */ 1834 callout_reset(&sc->sc_tick_ch, hz, pcn_tick, sc); 1835 } 1836 1837 /* ...all done! */ 1838 ifp->if_flags |= IFF_RUNNING; 1839 ifp->if_flags &= ~IFF_OACTIVE; 1840 1841 out: 1842 if (error) 1843 printf("%s: interface not running\n", device_xname(sc->sc_dev)); 1844 return (error); 1845 } 1846 1847 /* 1848 * pcn_rxdrain: 1849 * 1850 * Drain the receive queue. 1851 */ 1852 static void 1853 pcn_rxdrain(struct pcn_softc *sc) 1854 { 1855 struct pcn_rxsoft *rxs; 1856 int i; 1857 1858 for (i = 0; i < PCN_NRXDESC; i++) { 1859 rxs = &sc->sc_rxsoft[i]; 1860 if (rxs->rxs_mbuf != NULL) { 1861 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap); 1862 m_freem(rxs->rxs_mbuf); 1863 rxs->rxs_mbuf = NULL; 1864 } 1865 } 1866 } 1867 1868 /* 1869 * pcn_stop: [ifnet interface function] 1870 * 1871 * Stop transmission on the interface. 1872 */ 1873 static void 1874 pcn_stop(struct ifnet *ifp, int disable) 1875 { 1876 struct pcn_softc *sc = ifp->if_softc; 1877 struct pcn_txsoft *txs; 1878 int i; 1879 1880 if (sc->sc_flags & PCN_F_HAS_MII) { 1881 /* Stop the one second clock. */ 1882 callout_stop(&sc->sc_tick_ch); 1883 1884 /* Down the MII. */ 1885 mii_down(&sc->sc_mii); 1886 } 1887 1888 /* Stop the chip. */ 1889 pcn_csr_write(sc, LE_CSR0, LE_C0_STOP); 1890 1891 /* Release any queued transmit buffers. */ 1892 for (i = 0; i < PCN_TXQUEUELEN; i++) { 1893 txs = &sc->sc_txsoft[i]; 1894 if (txs->txs_mbuf != NULL) { 1895 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap); 1896 m_freem(txs->txs_mbuf); 1897 txs->txs_mbuf = NULL; 1898 } 1899 } 1900 1901 /* Mark the interface as down and cancel the watchdog timer. */ 1902 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1903 ifp->if_timer = 0; 1904 1905 if (disable) 1906 pcn_rxdrain(sc); 1907 } 1908 1909 /* 1910 * pcn_add_rxbuf: 1911 * 1912 * Add a receive buffer to the indicated descriptor. 1913 */ 1914 static int 1915 pcn_add_rxbuf(struct pcn_softc *sc, int idx) 1916 { 1917 struct pcn_rxsoft *rxs = &sc->sc_rxsoft[idx]; 1918 struct mbuf *m; 1919 int error; 1920 1921 MGETHDR(m, M_DONTWAIT, MT_DATA); 1922 if (m == NULL) 1923 return (ENOBUFS); 1924 1925 MCLGET(m, M_DONTWAIT); 1926 if ((m->m_flags & M_EXT) == 0) { 1927 m_freem(m); 1928 return (ENOBUFS); 1929 } 1930 1931 if (rxs->rxs_mbuf != NULL) 1932 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap); 1933 1934 rxs->rxs_mbuf = m; 1935 1936 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap, 1937 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, 1938 BUS_DMA_READ|BUS_DMA_NOWAIT); 1939 if (error) { 1940 printf("%s: can't load rx DMA map %d, error = %d\n", 1941 device_xname(sc->sc_dev), idx, error); 1942 panic("pcn_add_rxbuf"); 1943 } 1944 1945 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 1946 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD); 1947 1948 PCN_INIT_RXDESC(sc, idx); 1949 1950 return (0); 1951 } 1952 1953 /* 1954 * pcn_set_filter: 1955 * 1956 * Set up the receive filter. 1957 */ 1958 static void 1959 pcn_set_filter(struct pcn_softc *sc) 1960 { 1961 struct ethercom *ec = &sc->sc_ethercom; 1962 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1963 struct ether_multi *enm; 1964 struct ether_multistep step; 1965 uint32_t crc; 1966 1967 /* 1968 * Set up the multicast address filter by passing all multicast 1969 * addresses through a CRC generator, and then using the high 1970 * order 6 bits as an index into the 64-bit logical address 1971 * filter. The high order bits select the word, while the rest 1972 * of the bits select the bit within the word. 1973 */ 1974 1975 if (ifp->if_flags & IFF_PROMISC) 1976 goto allmulti; 1977 1978 sc->sc_initblock.init_ladrf[0] = 1979 sc->sc_initblock.init_ladrf[1] = 1980 sc->sc_initblock.init_ladrf[2] = 1981 sc->sc_initblock.init_ladrf[3] = 0; 1982 1983 ETHER_FIRST_MULTI(step, ec, enm); 1984 while (enm != NULL) { 1985 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 1986 /* 1987 * We must listen to a range of multicast addresses. 1988 * For now, just accept all multicasts, rather than 1989 * trying to set only those filter bits needed to match 1990 * the range. (At this time, the only use of address 1991 * ranges is for IP multicast routing, for which the 1992 * range is big enough to require all bits set.) 1993 */ 1994 goto allmulti; 1995 } 1996 1997 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN); 1998 1999 /* Just want the 6 most significant bits. */ 2000 crc >>= 26; 2001 2002 /* Set the corresponding bit in the filter. */ 2003 sc->sc_initblock.init_ladrf[crc >> 4] |= 2004 htole16(1 << (crc & 0xf)); 2005 2006 ETHER_NEXT_MULTI(step, enm); 2007 } 2008 2009 ifp->if_flags &= ~IFF_ALLMULTI; 2010 return; 2011 2012 allmulti: 2013 ifp->if_flags |= IFF_ALLMULTI; 2014 sc->sc_initblock.init_ladrf[0] = 2015 sc->sc_initblock.init_ladrf[1] = 2016 sc->sc_initblock.init_ladrf[2] = 2017 sc->sc_initblock.init_ladrf[3] = 0xffff; 2018 } 2019 2020 /* 2021 * pcn_79c970_mediainit: 2022 * 2023 * Initialize media for the Am79c970. 2024 */ 2025 static void 2026 pcn_79c970_mediainit(struct pcn_softc *sc) 2027 { 2028 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 2029 const char *sep = ""; 2030 2031 sc->sc_mii.mii_ifp = ifp; 2032 2033 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, pcn_79c970_mediachange, 2034 pcn_79c970_mediastatus); 2035 2036 #define ADD(str, m, d) \ 2037 do { \ 2038 printf("%s%s", sep, str); \ 2039 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|(m), (d), NULL); \ 2040 sep = ", "; \ 2041 } while (/*CONSTCOND*/0) 2042 2043 printf("%s: ", device_xname(sc->sc_dev)); 2044 ADD("10base5", IFM_10_5, PORTSEL_AUI); 2045 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A) 2046 ADD("10base5-FDX", IFM_10_5|IFM_FDX, PORTSEL_AUI); 2047 ADD("10baseT", IFM_10_T, PORTSEL_10T); 2048 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A) 2049 ADD("10baseT-FDX", IFM_10_T|IFM_FDX, PORTSEL_10T); 2050 ADD("auto", IFM_AUTO, 0); 2051 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A) 2052 ADD("auto-FDX", IFM_AUTO|IFM_FDX, 0); 2053 printf("\n"); 2054 2055 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 2056 } 2057 2058 /* 2059 * pcn_79c970_mediastatus: [ifmedia interface function] 2060 * 2061 * Get the current interface media status (Am79c970 version). 2062 */ 2063 static void 2064 pcn_79c970_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 2065 { 2066 struct pcn_softc *sc = ifp->if_softc; 2067 2068 /* 2069 * The currently selected media is always the active media. 2070 * Note: We have no way to determine what media the AUTO 2071 * process picked. 2072 */ 2073 ifmr->ifm_active = sc->sc_mii.mii_media.ifm_media; 2074 } 2075 2076 /* 2077 * pcn_79c970_mediachange: [ifmedia interface function] 2078 * 2079 * Set hardware to newly-selected media (Am79c970 version). 2080 */ 2081 static int 2082 pcn_79c970_mediachange(struct ifnet *ifp) 2083 { 2084 struct pcn_softc *sc = ifp->if_softc; 2085 uint32_t reg; 2086 2087 if (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media) == IFM_AUTO) { 2088 /* 2089 * CSR15:PORTSEL doesn't matter. Just set BCR2:ASEL. 2090 */ 2091 reg = pcn_bcr_read(sc, LE_BCR2); 2092 reg |= LE_B2_ASEL; 2093 pcn_bcr_write(sc, LE_BCR2, reg); 2094 } else { 2095 /* 2096 * Clear BCR2:ASEL and set the new CSR15:PORTSEL value. 2097 */ 2098 reg = pcn_bcr_read(sc, LE_BCR2); 2099 reg &= ~LE_B2_ASEL; 2100 pcn_bcr_write(sc, LE_BCR2, reg); 2101 2102 reg = pcn_csr_read(sc, LE_CSR15); 2103 reg = (reg & ~LE_C15_PORTSEL(PORTSEL_MASK)) | 2104 LE_C15_PORTSEL(sc->sc_mii.mii_media.ifm_cur->ifm_data); 2105 pcn_csr_write(sc, LE_CSR15, reg); 2106 } 2107 2108 if ((sc->sc_mii.mii_media.ifm_media & IFM_FDX) != 0) { 2109 reg = LE_B9_FDEN; 2110 if (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media) == IFM_10_5) 2111 reg |= LE_B9_AUIFD; 2112 pcn_bcr_write(sc, LE_BCR9, reg); 2113 } else 2114 pcn_bcr_write(sc, LE_BCR9, 0); 2115 2116 return (0); 2117 } 2118 2119 /* 2120 * pcn_79c971_mediainit: 2121 * 2122 * Initialize media for the Am79c971. 2123 */ 2124 static void 2125 pcn_79c971_mediainit(struct pcn_softc *sc) 2126 { 2127 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 2128 2129 /* We have MII. */ 2130 sc->sc_flags |= PCN_F_HAS_MII; 2131 2132 /* 2133 * The built-in 10BASE-T interface is mapped to the MII 2134 * on the PCNet-FAST. Unfortunately, there's no EEPROM 2135 * word that tells us which PHY to use. 2136 * This driver used to ignore all but the first PHY to 2137 * answer, but this code was removed to support multiple 2138 * external PHYs. As the default instance will be the first 2139 * one to answer, no harm is done by letting the possibly 2140 * non-connected internal PHY show up. 2141 */ 2142 2143 /* Initialize our media structures and probe the MII. */ 2144 sc->sc_mii.mii_ifp = ifp; 2145 sc->sc_mii.mii_readreg = pcn_mii_readreg; 2146 sc->sc_mii.mii_writereg = pcn_mii_writereg; 2147 sc->sc_mii.mii_statchg = pcn_mii_statchg; 2148 2149 sc->sc_ethercom.ec_mii = &sc->sc_mii; 2150 ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange, 2151 ether_mediastatus); 2152 2153 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 2154 MII_OFFSET_ANY, 0); 2155 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 2156 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 2157 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 2158 } else 2159 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 2160 } 2161 2162 /* 2163 * pcn_mii_readreg: [mii interface function] 2164 * 2165 * Read a PHY register on the MII. 2166 */ 2167 static int 2168 pcn_mii_readreg(device_t self, int phy, int reg) 2169 { 2170 struct pcn_softc *sc = device_private(self); 2171 uint32_t rv; 2172 2173 pcn_bcr_write(sc, LE_BCR33, reg | (phy << PHYAD_SHIFT)); 2174 rv = pcn_bcr_read(sc, LE_BCR34) & LE_B34_MIIMD; 2175 if (rv == 0xffff) 2176 return (0); 2177 2178 return (rv); 2179 } 2180 2181 /* 2182 * pcn_mii_writereg: [mii interface function] 2183 * 2184 * Write a PHY register on the MII. 2185 */ 2186 static void 2187 pcn_mii_writereg(device_t self, int phy, int reg, int val) 2188 { 2189 struct pcn_softc *sc = device_private(self); 2190 2191 pcn_bcr_write(sc, LE_BCR33, reg | (phy << PHYAD_SHIFT)); 2192 pcn_bcr_write(sc, LE_BCR34, val); 2193 } 2194 2195 /* 2196 * pcn_mii_statchg: [mii interface function] 2197 * 2198 * Callback from MII layer when media changes. 2199 */ 2200 static void 2201 pcn_mii_statchg(device_t self) 2202 { 2203 struct pcn_softc *sc = device_private(self); 2204 2205 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0) 2206 pcn_bcr_write(sc, LE_BCR9, LE_B9_FDEN); 2207 else 2208 pcn_bcr_write(sc, LE_BCR9, 0); 2209 } 2210