1 /* $NetBSD: if_stge.c,v 1.33 2006/06/17 23:34:27 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Device driver for the Sundance Tech. TC9021 10/100/1000 41 * Ethernet controller. 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: if_stge.c,v 1.33 2006/06/17 23:34:27 christos Exp $"); 46 47 #include "bpfilter.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/callout.h> 52 #include <sys/mbuf.h> 53 #include <sys/malloc.h> 54 #include <sys/kernel.h> 55 #include <sys/socket.h> 56 #include <sys/ioctl.h> 57 #include <sys/errno.h> 58 #include <sys/device.h> 59 #include <sys/queue.h> 60 61 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */ 62 63 #include <net/if.h> 64 #include <net/if_dl.h> 65 #include <net/if_media.h> 66 #include <net/if_ether.h> 67 68 #if NBPFILTER > 0 69 #include <net/bpf.h> 70 #endif 71 72 #include <machine/bus.h> 73 #include <machine/intr.h> 74 75 #include <dev/mii/mii.h> 76 #include <dev/mii/miivar.h> 77 #include <dev/mii/mii_bitbang.h> 78 79 #include <dev/pci/pcireg.h> 80 #include <dev/pci/pcivar.h> 81 #include <dev/pci/pcidevs.h> 82 83 #include <dev/pci/if_stgereg.h> 84 85 /* #define STGE_CU_BUG 1 */ 86 #define STGE_VLAN_UNTAG 1 87 /* #define STGE_VLAN_CFI 1 */ 88 89 /* 90 * Transmit descriptor list size. 91 */ 92 #define STGE_NTXDESC 256 93 #define STGE_NTXDESC_MASK (STGE_NTXDESC - 1) 94 #define STGE_NEXTTX(x) (((x) + 1) & STGE_NTXDESC_MASK) 95 96 /* 97 * Receive descriptor list size. 98 */ 99 #define STGE_NRXDESC 256 100 #define STGE_NRXDESC_MASK (STGE_NRXDESC - 1) 101 #define STGE_NEXTRX(x) (((x) + 1) & STGE_NRXDESC_MASK) 102 103 /* 104 * Only interrupt every N frames. Must be a power-of-two. 105 */ 106 #define STGE_TXINTR_SPACING 16 107 #define STGE_TXINTR_SPACING_MASK (STGE_TXINTR_SPACING - 1) 108 109 /* 110 * Control structures are DMA'd to the TC9021 chip. We allocate them in 111 * a single clump that maps to a single DMA segment to make several things 112 * easier. 113 */ 114 struct stge_control_data { 115 /* 116 * The transmit descriptors. 117 */ 118 struct stge_tfd scd_txdescs[STGE_NTXDESC]; 119 120 /* 121 * The receive descriptors. 122 */ 123 struct stge_rfd scd_rxdescs[STGE_NRXDESC]; 124 }; 125 126 #define STGE_CDOFF(x) offsetof(struct stge_control_data, x) 127 #define STGE_CDTXOFF(x) STGE_CDOFF(scd_txdescs[(x)]) 128 #define STGE_CDRXOFF(x) STGE_CDOFF(scd_rxdescs[(x)]) 129 130 /* 131 * Software state for transmit and receive jobs. 132 */ 133 struct stge_descsoft { 134 struct mbuf *ds_mbuf; /* head of our mbuf chain */ 135 bus_dmamap_t ds_dmamap; /* our DMA map */ 136 }; 137 138 /* 139 * Software state per device. 140 */ 141 struct stge_softc { 142 struct device sc_dev; /* generic device information */ 143 bus_space_tag_t sc_st; /* bus space tag */ 144 bus_space_handle_t sc_sh; /* bus space handle */ 145 bus_dma_tag_t sc_dmat; /* bus DMA tag */ 146 struct ethercom sc_ethercom; /* ethernet common data */ 147 void *sc_sdhook; /* shutdown hook */ 148 int sc_rev; /* silicon revision */ 149 150 void *sc_ih; /* interrupt cookie */ 151 152 struct mii_data sc_mii; /* MII/media information */ 153 154 struct callout sc_tick_ch; /* tick callout */ 155 156 bus_dmamap_t sc_cddmamap; /* control data DMA map */ 157 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr 158 159 /* 160 * Software state for transmit and receive descriptors. 161 */ 162 struct stge_descsoft sc_txsoft[STGE_NTXDESC]; 163 struct stge_descsoft sc_rxsoft[STGE_NRXDESC]; 164 165 /* 166 * Control data structures. 167 */ 168 struct stge_control_data *sc_control_data; 169 #define sc_txdescs sc_control_data->scd_txdescs 170 #define sc_rxdescs sc_control_data->scd_rxdescs 171 172 #ifdef STGE_EVENT_COUNTERS 173 /* 174 * Event counters. 175 */ 176 struct evcnt sc_ev_txstall; /* Tx stalled */ 177 struct evcnt sc_ev_txdmaintr; /* Tx DMA interrupts */ 178 struct evcnt sc_ev_txindintr; /* Tx Indicate interrupts */ 179 struct evcnt sc_ev_rxintr; /* Rx interrupts */ 180 181 struct evcnt sc_ev_txseg1; /* Tx packets w/ 1 segment */ 182 struct evcnt sc_ev_txseg2; /* Tx packets w/ 2 segments */ 183 struct evcnt sc_ev_txseg3; /* Tx packets w/ 3 segments */ 184 struct evcnt sc_ev_txseg4; /* Tx packets w/ 4 segments */ 185 struct evcnt sc_ev_txseg5; /* Tx packets w/ 5 segments */ 186 struct evcnt sc_ev_txsegmore; /* Tx packets w/ more than 5 segments */ 187 struct evcnt sc_ev_txcopy; /* Tx packets that we had to copy */ 188 189 struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */ 190 struct evcnt sc_ev_rxtcpsum; /* TCP checksums checked in-bound */ 191 struct evcnt sc_ev_rxudpsum; /* UDP checksums checked in-bound */ 192 193 struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */ 194 struct evcnt sc_ev_txtcpsum; /* TCP checksums comp. out-bound */ 195 struct evcnt sc_ev_txudpsum; /* UDP checksums comp. out-bound */ 196 #endif /* STGE_EVENT_COUNTERS */ 197 198 int sc_txpending; /* number of Tx requests pending */ 199 int sc_txdirty; /* first dirty Tx descriptor */ 200 int sc_txlast; /* last used Tx descriptor */ 201 202 int sc_rxptr; /* next ready Rx descriptor/descsoft */ 203 int sc_rxdiscard; 204 int sc_rxlen; 205 struct mbuf *sc_rxhead; 206 struct mbuf *sc_rxtail; 207 struct mbuf **sc_rxtailp; 208 209 int sc_txthresh; /* Tx threshold */ 210 uint32_t sc_usefiber:1; /* if we're fiber */ 211 uint32_t sc_stge1023:1; /* are we a 1023 */ 212 uint32_t sc_DMACtrl; /* prototype DMACtrl register */ 213 uint32_t sc_MACCtrl; /* prototype MacCtrl register */ 214 uint16_t sc_IntEnable; /* prototype IntEnable register */ 215 uint16_t sc_ReceiveMode; /* prototype ReceiveMode register */ 216 uint8_t sc_PhyCtrl; /* prototype PhyCtrl register */ 217 }; 218 219 #define STGE_RXCHAIN_RESET(sc) \ 220 do { \ 221 (sc)->sc_rxtailp = &(sc)->sc_rxhead; \ 222 *(sc)->sc_rxtailp = NULL; \ 223 (sc)->sc_rxlen = 0; \ 224 } while (/*CONSTCOND*/0) 225 226 #define STGE_RXCHAIN_LINK(sc, m) \ 227 do { \ 228 *(sc)->sc_rxtailp = (sc)->sc_rxtail = (m); \ 229 (sc)->sc_rxtailp = &(m)->m_next; \ 230 } while (/*CONSTCOND*/0) 231 232 #ifdef STGE_EVENT_COUNTERS 233 #define STGE_EVCNT_INCR(ev) (ev)->ev_count++ 234 #else 235 #define STGE_EVCNT_INCR(ev) /* nothing */ 236 #endif 237 238 #define STGE_CDTXADDR(sc, x) ((sc)->sc_cddma + STGE_CDTXOFF((x))) 239 #define STGE_CDRXADDR(sc, x) ((sc)->sc_cddma + STGE_CDRXOFF((x))) 240 241 #define STGE_CDTXSYNC(sc, x, ops) \ 242 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \ 243 STGE_CDTXOFF((x)), sizeof(struct stge_tfd), (ops)) 244 245 #define STGE_CDRXSYNC(sc, x, ops) \ 246 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \ 247 STGE_CDRXOFF((x)), sizeof(struct stge_rfd), (ops)) 248 249 #define STGE_INIT_RXDESC(sc, x) \ 250 do { \ 251 struct stge_descsoft *__ds = &(sc)->sc_rxsoft[(x)]; \ 252 struct stge_rfd *__rfd = &(sc)->sc_rxdescs[(x)]; \ 253 \ 254 /* \ 255 * Note: We scoot the packet forward 2 bytes in the buffer \ 256 * so that the payload after the Ethernet header is aligned \ 257 * to a 4-byte boundary. \ 258 */ \ 259 __rfd->rfd_frag.frag_word0 = \ 260 htole64(FRAG_ADDR(__ds->ds_dmamap->dm_segs[0].ds_addr + 2) |\ 261 FRAG_LEN(MCLBYTES - 2)); \ 262 __rfd->rfd_next = \ 263 htole64((uint64_t)STGE_CDRXADDR((sc), STGE_NEXTRX((x)))); \ 264 __rfd->rfd_status = 0; \ 265 STGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \ 266 } while (/*CONSTCOND*/0) 267 268 #define STGE_TIMEOUT 1000 269 270 static void stge_start(struct ifnet *); 271 static void stge_watchdog(struct ifnet *); 272 static int stge_ioctl(struct ifnet *, u_long, caddr_t); 273 static int stge_init(struct ifnet *); 274 static void stge_stop(struct ifnet *, int); 275 276 static void stge_shutdown(void *); 277 278 static void stge_reset(struct stge_softc *); 279 static void stge_rxdrain(struct stge_softc *); 280 static int stge_add_rxbuf(struct stge_softc *, int); 281 static void stge_read_eeprom(struct stge_softc *, int, uint16_t *); 282 static void stge_tick(void *); 283 284 static void stge_stats_update(struct stge_softc *); 285 286 static void stge_set_filter(struct stge_softc *); 287 288 static int stge_intr(void *); 289 static void stge_txintr(struct stge_softc *); 290 static void stge_rxintr(struct stge_softc *); 291 292 static int stge_mii_readreg(struct device *, int, int); 293 static void stge_mii_writereg(struct device *, int, int, int); 294 static void stge_mii_statchg(struct device *); 295 296 static int stge_mediachange(struct ifnet *); 297 static void stge_mediastatus(struct ifnet *, struct ifmediareq *); 298 299 static int stge_match(struct device *, struct cfdata *, void *); 300 static void stge_attach(struct device *, struct device *, void *); 301 302 int stge_copy_small = 0; 303 304 CFATTACH_DECL(stge, sizeof(struct stge_softc), 305 stge_match, stge_attach, NULL, NULL); 306 307 static uint32_t stge_mii_bitbang_read(struct device *); 308 static void stge_mii_bitbang_write(struct device *, uint32_t); 309 310 static const struct mii_bitbang_ops stge_mii_bitbang_ops = { 311 stge_mii_bitbang_read, 312 stge_mii_bitbang_write, 313 { 314 PC_MgmtData, /* MII_BIT_MDO */ 315 PC_MgmtData, /* MII_BIT_MDI */ 316 PC_MgmtClk, /* MII_BIT_MDC */ 317 PC_MgmtDir, /* MII_BIT_DIR_HOST_PHY */ 318 0, /* MII_BIT_DIR_PHY_HOST */ 319 } 320 }; 321 322 /* 323 * Devices supported by this driver. 324 */ 325 static const struct stge_product { 326 pci_vendor_id_t stge_vendor; 327 pci_product_id_t stge_product; 328 const char *stge_name; 329 } stge_products[] = { 330 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_SUNDANCETI_ST1023, 331 "Sundance ST-1023 Gigabit Ethernet" }, 332 333 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_SUNDANCETI_ST2021, 334 "Sundance ST-2021 Gigabit Ethernet" }, 335 336 { PCI_VENDOR_TAMARACK, PCI_PRODUCT_TAMARACK_TC9021, 337 "Tamarack TC9021 Gigabit Ethernet" }, 338 339 { PCI_VENDOR_TAMARACK, PCI_PRODUCT_TAMARACK_TC9021_ALT, 340 "Tamarack TC9021 Gigabit Ethernet" }, 341 342 /* 343 * The Sundance sample boards use the Sundance vendor ID, 344 * but the Tamarack product ID. 345 */ 346 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_TAMARACK_TC9021, 347 "Sundance TC9021 Gigabit Ethernet" }, 348 349 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_TAMARACK_TC9021_ALT, 350 "Sundance TC9021 Gigabit Ethernet" }, 351 352 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DL4000, 353 "D-Link DL-4000 Gigabit Ethernet" }, 354 355 { PCI_VENDOR_ANTARES, PCI_PRODUCT_ANTARES_TC9021, 356 "Antares Gigabit Ethernet" }, 357 358 { 0, 0, 359 NULL }, 360 }; 361 362 static const struct stge_product * 363 stge_lookup(const struct pci_attach_args *pa) 364 { 365 const struct stge_product *sp; 366 367 for (sp = stge_products; sp->stge_name != NULL; sp++) { 368 if (PCI_VENDOR(pa->pa_id) == sp->stge_vendor && 369 PCI_PRODUCT(pa->pa_id) == sp->stge_product) 370 return (sp); 371 } 372 return (NULL); 373 } 374 375 static int 376 stge_match(struct device *parent, struct cfdata *cf, void *aux) 377 { 378 struct pci_attach_args *pa = aux; 379 380 if (stge_lookup(pa) != NULL) 381 return (1); 382 383 return (0); 384 } 385 386 static void 387 stge_attach(struct device *parent, struct device *self, void *aux) 388 { 389 struct stge_softc *sc = (struct stge_softc *) self; 390 struct pci_attach_args *pa = aux; 391 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 392 pci_chipset_tag_t pc = pa->pa_pc; 393 pci_intr_handle_t ih; 394 const char *intrstr = NULL; 395 bus_space_tag_t iot, memt; 396 bus_space_handle_t ioh, memh; 397 bus_dma_segment_t seg; 398 int ioh_valid, memh_valid; 399 int i, rseg, error; 400 const struct stge_product *sp; 401 uint8_t enaddr[ETHER_ADDR_LEN]; 402 403 callout_init(&sc->sc_tick_ch); 404 405 sp = stge_lookup(pa); 406 if (sp == NULL) { 407 printf("\n"); 408 panic("ste_attach: impossible"); 409 } 410 411 sc->sc_rev = PCI_REVISION(pa->pa_class); 412 413 printf(": %s, rev. %d\n", sp->stge_name, sc->sc_rev); 414 415 /* 416 * Map the device. 417 */ 418 ioh_valid = (pci_mapreg_map(pa, STGE_PCI_IOBA, 419 PCI_MAPREG_TYPE_IO, 0, 420 &iot, &ioh, NULL, NULL) == 0); 421 memh_valid = (pci_mapreg_map(pa, STGE_PCI_MMBA, 422 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0, 423 &memt, &memh, NULL, NULL) == 0); 424 425 if (memh_valid) { 426 sc->sc_st = memt; 427 sc->sc_sh = memh; 428 } else if (ioh_valid) { 429 sc->sc_st = iot; 430 sc->sc_sh = ioh; 431 } else { 432 printf("%s: unable to map device registers\n", 433 sc->sc_dev.dv_xname); 434 return; 435 } 436 437 sc->sc_dmat = pa->pa_dmat; 438 439 /* Enable bus mastering. */ 440 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 441 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) | 442 PCI_COMMAND_MASTER_ENABLE); 443 444 /* power up chip */ 445 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc, 446 NULL)) && error != EOPNOTSUPP) { 447 aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname, 448 error); 449 return; 450 } 451 /* 452 * Map and establish our interrupt. 453 */ 454 if (pci_intr_map(pa, &ih)) { 455 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname); 456 return; 457 } 458 intrstr = pci_intr_string(pc, ih); 459 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, stge_intr, sc); 460 if (sc->sc_ih == NULL) { 461 printf("%s: unable to establish interrupt", 462 sc->sc_dev.dv_xname); 463 if (intrstr != NULL) 464 printf(" at %s", intrstr); 465 printf("\n"); 466 return; 467 } 468 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 469 470 /* 471 * Allocate the control data structures, and create and load the 472 * DMA map for it. 473 */ 474 if ((error = bus_dmamem_alloc(sc->sc_dmat, 475 sizeof(struct stge_control_data), PAGE_SIZE, 0, &seg, 1, &rseg, 476 0)) != 0) { 477 printf("%s: unable to allocate control data, error = %d\n", 478 sc->sc_dev.dv_xname, error); 479 goto fail_0; 480 } 481 482 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, 483 sizeof(struct stge_control_data), (caddr_t *)&sc->sc_control_data, 484 BUS_DMA_COHERENT)) != 0) { 485 printf("%s: unable to map control data, error = %d\n", 486 sc->sc_dev.dv_xname, error); 487 goto fail_1; 488 } 489 490 if ((error = bus_dmamap_create(sc->sc_dmat, 491 sizeof(struct stge_control_data), 1, 492 sizeof(struct stge_control_data), 0, 0, &sc->sc_cddmamap)) != 0) { 493 printf("%s: unable to create control data DMA map, " 494 "error = %d\n", sc->sc_dev.dv_xname, error); 495 goto fail_2; 496 } 497 498 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap, 499 sc->sc_control_data, sizeof(struct stge_control_data), NULL, 500 0)) != 0) { 501 printf("%s: unable to load control data DMA map, error = %d\n", 502 sc->sc_dev.dv_xname, error); 503 goto fail_3; 504 } 505 506 /* 507 * Create the transmit buffer DMA maps. Note that rev B.3 508 * and earlier seem to have a bug regarding multi-fragment 509 * packets. We need to limit the number of Tx segments on 510 * such chips to 1. 511 */ 512 for (i = 0; i < STGE_NTXDESC; i++) { 513 if ((error = bus_dmamap_create(sc->sc_dmat, 514 ETHER_MAX_LEN_JUMBO, STGE_NTXFRAGS, MCLBYTES, 0, 0, 515 &sc->sc_txsoft[i].ds_dmamap)) != 0) { 516 printf("%s: unable to create tx DMA map %d, " 517 "error = %d\n", sc->sc_dev.dv_xname, i, error); 518 goto fail_4; 519 } 520 } 521 522 /* 523 * Create the receive buffer DMA maps. 524 */ 525 for (i = 0; i < STGE_NRXDESC; i++) { 526 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, 527 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].ds_dmamap)) != 0) { 528 printf("%s: unable to create rx DMA map %d, " 529 "error = %d\n", sc->sc_dev.dv_xname, i, error); 530 goto fail_5; 531 } 532 sc->sc_rxsoft[i].ds_mbuf = NULL; 533 } 534 535 /* 536 * Determine if we're copper or fiber. It affects how we 537 * reset the card. 538 */ 539 if (bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) & 540 AC_PhyMedia) 541 sc->sc_usefiber = 1; 542 else 543 sc->sc_usefiber = 0; 544 545 /* 546 * Reset the chip to a known state. 547 */ 548 stge_reset(sc); 549 550 /* 551 * Reading the station address from the EEPROM doesn't seem 552 * to work, at least on my sample boards. Instead, since 553 * the reset sequence does AutoInit, read it from the station 554 * address registers. For Sundance 1023 you can only read it 555 * from EEPROM. 556 */ 557 if (sp->stge_product != PCI_PRODUCT_SUNDANCETI_ST1023) { 558 enaddr[0] = bus_space_read_2(sc->sc_st, sc->sc_sh, 559 STGE_StationAddress0) & 0xff; 560 enaddr[1] = bus_space_read_2(sc->sc_st, sc->sc_sh, 561 STGE_StationAddress0) >> 8; 562 enaddr[2] = bus_space_read_2(sc->sc_st, sc->sc_sh, 563 STGE_StationAddress1) & 0xff; 564 enaddr[3] = bus_space_read_2(sc->sc_st, sc->sc_sh, 565 STGE_StationAddress1) >> 8; 566 enaddr[4] = bus_space_read_2(sc->sc_st, sc->sc_sh, 567 STGE_StationAddress2) & 0xff; 568 enaddr[5] = bus_space_read_2(sc->sc_st, sc->sc_sh, 569 STGE_StationAddress2) >> 8; 570 sc->sc_stge1023 = 0; 571 } else { 572 uint16_t myaddr[ETHER_ADDR_LEN / 2]; 573 for (i = 0; i <ETHER_ADDR_LEN / 2; i++) { 574 stge_read_eeprom(sc, STGE_EEPROM_StationAddress0 + i, 575 &myaddr[i]); 576 myaddr[i] = le16toh(myaddr[i]); 577 } 578 (void)memcpy(enaddr, myaddr, sizeof(enaddr)); 579 sc->sc_stge1023 = 1; 580 } 581 582 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname, 583 ether_sprintf(enaddr)); 584 585 /* 586 * Read some important bits from the PhyCtrl register. 587 */ 588 sc->sc_PhyCtrl = bus_space_read_1(sc->sc_st, sc->sc_sh, 589 STGE_PhyCtrl) & (PC_PhyDuplexPolarity | PC_PhyLnkPolarity); 590 591 /* 592 * Initialize our media structures and probe the MII. 593 */ 594 sc->sc_mii.mii_ifp = ifp; 595 sc->sc_mii.mii_readreg = stge_mii_readreg; 596 sc->sc_mii.mii_writereg = stge_mii_writereg; 597 sc->sc_mii.mii_statchg = stge_mii_statchg; 598 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, stge_mediachange, 599 stge_mediastatus); 600 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 601 MII_OFFSET_ANY, MIIF_DOPAUSE); 602 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 603 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 604 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 605 } else 606 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 607 608 ifp = &sc->sc_ethercom.ec_if; 609 strcpy(ifp->if_xname, sc->sc_dev.dv_xname); 610 ifp->if_softc = sc; 611 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 612 ifp->if_ioctl = stge_ioctl; 613 ifp->if_start = stge_start; 614 ifp->if_watchdog = stge_watchdog; 615 ifp->if_init = stge_init; 616 ifp->if_stop = stge_stop; 617 IFQ_SET_READY(&ifp->if_snd); 618 619 /* 620 * The manual recommends disabling early transmit, so we 621 * do. It's disabled anyway, if using IP checksumming, 622 * since the entire packet must be in the FIFO in order 623 * for the chip to perform the checksum. 624 */ 625 sc->sc_txthresh = 0x0fff; 626 627 /* 628 * Disable MWI if the PCI layer tells us to. 629 */ 630 sc->sc_DMACtrl = 0; 631 if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0) 632 sc->sc_DMACtrl |= DMAC_MWIDisable; 633 634 /* 635 * We can support 802.1Q VLAN-sized frames and jumbo 636 * Ethernet frames. 637 * 638 * XXX Figure out how to do hw-assisted VLAN tagging in 639 * XXX a reasonable way on this chip. 640 */ 641 sc->sc_ethercom.ec_capabilities |= 642 ETHERCAP_VLAN_MTU | /* XXX ETHERCAP_JUMBO_MTU | */ 643 ETHERCAP_VLAN_HWTAGGING; 644 645 /* 646 * We can do IPv4/TCPv4/UDPv4 checksums in hardware. 647 */ 648 sc->sc_ethercom.ec_if.if_capabilities |= 649 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx | 650 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx | 651 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx; 652 653 /* 654 * Attach the interface. 655 */ 656 if_attach(ifp); 657 ether_ifattach(ifp, enaddr); 658 659 #ifdef STGE_EVENT_COUNTERS 660 /* 661 * Attach event counters. 662 */ 663 evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC, 664 NULL, sc->sc_dev.dv_xname, "txstall"); 665 evcnt_attach_dynamic(&sc->sc_ev_txdmaintr, EVCNT_TYPE_INTR, 666 NULL, sc->sc_dev.dv_xname, "txdmaintr"); 667 evcnt_attach_dynamic(&sc->sc_ev_txindintr, EVCNT_TYPE_INTR, 668 NULL, sc->sc_dev.dv_xname, "txindintr"); 669 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR, 670 NULL, sc->sc_dev.dv_xname, "rxintr"); 671 672 evcnt_attach_dynamic(&sc->sc_ev_txseg1, EVCNT_TYPE_MISC, 673 NULL, sc->sc_dev.dv_xname, "txseg1"); 674 evcnt_attach_dynamic(&sc->sc_ev_txseg2, EVCNT_TYPE_MISC, 675 NULL, sc->sc_dev.dv_xname, "txseg2"); 676 evcnt_attach_dynamic(&sc->sc_ev_txseg3, EVCNT_TYPE_MISC, 677 NULL, sc->sc_dev.dv_xname, "txseg3"); 678 evcnt_attach_dynamic(&sc->sc_ev_txseg4, EVCNT_TYPE_MISC, 679 NULL, sc->sc_dev.dv_xname, "txseg4"); 680 evcnt_attach_dynamic(&sc->sc_ev_txseg5, EVCNT_TYPE_MISC, 681 NULL, sc->sc_dev.dv_xname, "txseg5"); 682 evcnt_attach_dynamic(&sc->sc_ev_txsegmore, EVCNT_TYPE_MISC, 683 NULL, sc->sc_dev.dv_xname, "txsegmore"); 684 evcnt_attach_dynamic(&sc->sc_ev_txcopy, EVCNT_TYPE_MISC, 685 NULL, sc->sc_dev.dv_xname, "txcopy"); 686 687 evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC, 688 NULL, sc->sc_dev.dv_xname, "rxipsum"); 689 evcnt_attach_dynamic(&sc->sc_ev_rxtcpsum, EVCNT_TYPE_MISC, 690 NULL, sc->sc_dev.dv_xname, "rxtcpsum"); 691 evcnt_attach_dynamic(&sc->sc_ev_rxudpsum, EVCNT_TYPE_MISC, 692 NULL, sc->sc_dev.dv_xname, "rxudpsum"); 693 evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC, 694 NULL, sc->sc_dev.dv_xname, "txipsum"); 695 evcnt_attach_dynamic(&sc->sc_ev_txtcpsum, EVCNT_TYPE_MISC, 696 NULL, sc->sc_dev.dv_xname, "txtcpsum"); 697 evcnt_attach_dynamic(&sc->sc_ev_txudpsum, EVCNT_TYPE_MISC, 698 NULL, sc->sc_dev.dv_xname, "txudpsum"); 699 #endif /* STGE_EVENT_COUNTERS */ 700 701 /* 702 * Make sure the interface is shutdown during reboot. 703 */ 704 sc->sc_sdhook = shutdownhook_establish(stge_shutdown, sc); 705 if (sc->sc_sdhook == NULL) 706 printf("%s: WARNING: unable to establish shutdown hook\n", 707 sc->sc_dev.dv_xname); 708 return; 709 710 /* 711 * Free any resources we've allocated during the failed attach 712 * attempt. Do this in reverse order and fall through. 713 */ 714 fail_5: 715 for (i = 0; i < STGE_NRXDESC; i++) { 716 if (sc->sc_rxsoft[i].ds_dmamap != NULL) 717 bus_dmamap_destroy(sc->sc_dmat, 718 sc->sc_rxsoft[i].ds_dmamap); 719 } 720 fail_4: 721 for (i = 0; i < STGE_NTXDESC; i++) { 722 if (sc->sc_txsoft[i].ds_dmamap != NULL) 723 bus_dmamap_destroy(sc->sc_dmat, 724 sc->sc_txsoft[i].ds_dmamap); 725 } 726 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap); 727 fail_3: 728 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap); 729 fail_2: 730 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data, 731 sizeof(struct stge_control_data)); 732 fail_1: 733 bus_dmamem_free(sc->sc_dmat, &seg, rseg); 734 fail_0: 735 return; 736 } 737 738 /* 739 * stge_shutdown: 740 * 741 * Make sure the interface is stopped at reboot time. 742 */ 743 static void 744 stge_shutdown(void *arg) 745 { 746 struct stge_softc *sc = arg; 747 748 stge_stop(&sc->sc_ethercom.ec_if, 1); 749 } 750 751 static void 752 stge_dma_wait(struct stge_softc *sc) 753 { 754 int i; 755 756 for (i = 0; i < STGE_TIMEOUT; i++) { 757 delay(2); 758 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl) & 759 DMAC_TxDMAInProg) == 0) 760 break; 761 } 762 763 if (i == STGE_TIMEOUT) 764 printf("%s: DMA wait timed out\n", sc->sc_dev.dv_xname); 765 } 766 767 /* 768 * stge_start: [ifnet interface function] 769 * 770 * Start packet transmission on the interface. 771 */ 772 static void 773 stge_start(struct ifnet *ifp) 774 { 775 struct stge_softc *sc = ifp->if_softc; 776 struct mbuf *m0; 777 struct stge_descsoft *ds; 778 struct stge_tfd *tfd; 779 bus_dmamap_t dmamap; 780 int error, firsttx, nexttx, opending, seg, totlen; 781 uint64_t csum_flags; 782 783 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 784 return; 785 786 /* 787 * Remember the previous number of pending transmissions 788 * and the first descriptor we will use. 789 */ 790 opending = sc->sc_txpending; 791 firsttx = STGE_NEXTTX(sc->sc_txlast); 792 793 /* 794 * Loop through the send queue, setting up transmit descriptors 795 * until we drain the queue, or use up all available transmit 796 * descriptors. 797 */ 798 for (;;) { 799 struct m_tag *mtag; 800 uint64_t tfc; 801 802 /* 803 * Grab a packet off the queue. 804 */ 805 IFQ_POLL(&ifp->if_snd, m0); 806 if (m0 == NULL) 807 break; 808 809 /* 810 * Leave one unused descriptor at the end of the 811 * list to prevent wrapping completely around. 812 */ 813 if (sc->sc_txpending == (STGE_NTXDESC - 1)) { 814 STGE_EVCNT_INCR(&sc->sc_ev_txstall); 815 break; 816 } 817 818 /* 819 * See if we have any VLAN stuff. 820 */ 821 mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m0); 822 823 /* 824 * Get the last and next available transmit descriptor. 825 */ 826 nexttx = STGE_NEXTTX(sc->sc_txlast); 827 tfd = &sc->sc_txdescs[nexttx]; 828 ds = &sc->sc_txsoft[nexttx]; 829 830 dmamap = ds->ds_dmamap; 831 832 /* 833 * Load the DMA map. If this fails, the packet either 834 * didn't fit in the alloted number of segments, or we 835 * were short on resources. For the too-many-segments 836 * case, we simply report an error and drop the packet, 837 * since we can't sanely copy a jumbo packet to a single 838 * buffer. 839 */ 840 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0, 841 BUS_DMA_NOWAIT); 842 if (error) { 843 if (error == EFBIG) { 844 printf("%s: Tx packet consumes too many " 845 "DMA segments, dropping...\n", 846 sc->sc_dev.dv_xname); 847 IFQ_DEQUEUE(&ifp->if_snd, m0); 848 m_freem(m0); 849 continue; 850 } 851 /* 852 * Short on resources, just stop for now. 853 */ 854 break; 855 } 856 857 IFQ_DEQUEUE(&ifp->if_snd, m0); 858 859 /* 860 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. 861 */ 862 863 /* Sync the DMA map. */ 864 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize, 865 BUS_DMASYNC_PREWRITE); 866 867 /* Initialize the fragment list. */ 868 for (totlen = 0, seg = 0; seg < dmamap->dm_nsegs; seg++) { 869 tfd->tfd_frags[seg].frag_word0 = 870 htole64(FRAG_ADDR(dmamap->dm_segs[seg].ds_addr) | 871 FRAG_LEN(dmamap->dm_segs[seg].ds_len)); 872 totlen += dmamap->dm_segs[seg].ds_len; 873 } 874 875 #ifdef STGE_EVENT_COUNTERS 876 switch (dmamap->dm_nsegs) { 877 case 1: 878 STGE_EVCNT_INCR(&sc->sc_ev_txseg1); 879 break; 880 case 2: 881 STGE_EVCNT_INCR(&sc->sc_ev_txseg2); 882 break; 883 case 3: 884 STGE_EVCNT_INCR(&sc->sc_ev_txseg3); 885 break; 886 case 4: 887 STGE_EVCNT_INCR(&sc->sc_ev_txseg4); 888 break; 889 case 5: 890 STGE_EVCNT_INCR(&sc->sc_ev_txseg5); 891 break; 892 default: 893 STGE_EVCNT_INCR(&sc->sc_ev_txsegmore); 894 break; 895 } 896 #endif /* STGE_EVENT_COUNTERS */ 897 898 /* 899 * Initialize checksumming flags in the descriptor. 900 * Byte-swap constants so the compiler can optimize. 901 */ 902 csum_flags = 0; 903 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) { 904 STGE_EVCNT_INCR(&sc->sc_ev_txipsum); 905 csum_flags |= TFD_IPChecksumEnable; 906 } 907 908 if (m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) { 909 STGE_EVCNT_INCR(&sc->sc_ev_txtcpsum); 910 csum_flags |= TFD_TCPChecksumEnable; 911 } else if (m0->m_pkthdr.csum_flags & M_CSUM_UDPv4) { 912 STGE_EVCNT_INCR(&sc->sc_ev_txudpsum); 913 csum_flags |= TFD_UDPChecksumEnable; 914 } 915 916 /* 917 * Initialize the descriptor and give it to the chip. 918 * Check to see if we have a VLAN tag to insert. 919 */ 920 921 tfc = TFD_FrameId(nexttx) | TFD_WordAlign(/*totlen & */3) | 922 TFD_FragCount(seg) | csum_flags | 923 (((nexttx & STGE_TXINTR_SPACING_MASK) == 0) ? 924 TFD_TxDMAIndicate : 0); 925 if (mtag) { 926 #if 0 927 struct ether_header *eh = 928 mtod(m0, struct ether_header *); 929 u_int16_t etype = ntohs(eh->ether_type); 930 printf("%s: xmit (tag %d) etype %x\n", 931 ifp->if_xname, *mtod(n, int *), etype); 932 #endif 933 tfc |= TFD_VLANTagInsert | 934 #ifdef STGE_VLAN_CFI 935 TFD_CFI | 936 #endif 937 TFD_VID(VLAN_TAG_VALUE(mtag)); 938 } 939 tfd->tfd_control = htole64(tfc); 940 941 /* Sync the descriptor. */ 942 STGE_CDTXSYNC(sc, nexttx, 943 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 944 945 /* 946 * Kick the transmit DMA logic. 947 */ 948 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl, 949 sc->sc_DMACtrl | DMAC_TxDMAPollNow); 950 951 /* 952 * Store a pointer to the packet so we can free it later. 953 */ 954 ds->ds_mbuf = m0; 955 956 /* Advance the tx pointer. */ 957 sc->sc_txpending++; 958 sc->sc_txlast = nexttx; 959 960 #if NBPFILTER > 0 961 /* 962 * Pass the packet to any BPF listeners. 963 */ 964 if (ifp->if_bpf) 965 bpf_mtap(ifp->if_bpf, m0); 966 #endif /* NBPFILTER > 0 */ 967 } 968 969 if (sc->sc_txpending == (STGE_NTXDESC - 1)) { 970 /* No more slots left; notify upper layer. */ 971 ifp->if_flags |= IFF_OACTIVE; 972 } 973 974 if (sc->sc_txpending != opending) { 975 /* 976 * We enqueued packets. If the transmitter was idle, 977 * reset the txdirty pointer. 978 */ 979 if (opending == 0) 980 sc->sc_txdirty = firsttx; 981 982 /* Set a watchdog timer in case the chip flakes out. */ 983 ifp->if_timer = 5; 984 } 985 } 986 987 /* 988 * stge_watchdog: [ifnet interface function] 989 * 990 * Watchdog timer handler. 991 */ 992 static void 993 stge_watchdog(struct ifnet *ifp) 994 { 995 struct stge_softc *sc = ifp->if_softc; 996 997 /* 998 * Sweep up first, since we don't interrupt every frame. 999 */ 1000 stge_txintr(sc); 1001 if (sc->sc_txpending != 0) { 1002 printf("%s: device timeout\n", sc->sc_dev.dv_xname); 1003 ifp->if_oerrors++; 1004 1005 (void) stge_init(ifp); 1006 1007 /* Try to get more packets going. */ 1008 stge_start(ifp); 1009 } 1010 } 1011 1012 /* 1013 * stge_ioctl: [ifnet interface function] 1014 * 1015 * Handle control requests from the operator. 1016 */ 1017 static int 1018 stge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1019 { 1020 struct stge_softc *sc = ifp->if_softc; 1021 struct ifreq *ifr = (struct ifreq *)data; 1022 int s, error; 1023 1024 s = splnet(); 1025 1026 switch (cmd) { 1027 case SIOCSIFMEDIA: 1028 case SIOCGIFMEDIA: 1029 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 1030 break; 1031 1032 default: 1033 error = ether_ioctl(ifp, cmd, data); 1034 if (error == ENETRESET) { 1035 /* 1036 * Multicast list has changed; set the hardware filter 1037 * accordingly. 1038 */ 1039 if (ifp->if_flags & IFF_RUNNING) 1040 stge_set_filter(sc); 1041 error = 0; 1042 } 1043 break; 1044 } 1045 1046 /* Try to get more packets going. */ 1047 stge_start(ifp); 1048 1049 splx(s); 1050 return (error); 1051 } 1052 1053 /* 1054 * stge_intr: 1055 * 1056 * Interrupt service routine. 1057 */ 1058 static int 1059 stge_intr(void *arg) 1060 { 1061 struct stge_softc *sc = arg; 1062 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1063 uint32_t txstat; 1064 int wantinit; 1065 uint16_t isr; 1066 1067 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatus) & 1068 IS_InterruptStatus) == 0) 1069 return (0); 1070 1071 for (wantinit = 0; wantinit == 0;) { 1072 isr = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatusAck); 1073 if ((isr & sc->sc_IntEnable) == 0) 1074 break; 1075 1076 /* Host interface errors. */ 1077 if (isr & IS_HostError) { 1078 printf("%s: Host interface error\n", 1079 sc->sc_dev.dv_xname); 1080 wantinit = 1; 1081 continue; 1082 } 1083 1084 /* Receive interrupts. */ 1085 if (isr & (IS_RxDMAComplete|IS_RFDListEnd)) { 1086 STGE_EVCNT_INCR(&sc->sc_ev_rxintr); 1087 stge_rxintr(sc); 1088 if (isr & IS_RFDListEnd) { 1089 printf("%s: receive ring overflow\n", 1090 sc->sc_dev.dv_xname); 1091 /* 1092 * XXX Should try to recover from this 1093 * XXX more gracefully. 1094 */ 1095 wantinit = 1; 1096 } 1097 } 1098 1099 /* Transmit interrupts. */ 1100 if (isr & (IS_TxDMAComplete|IS_TxComplete)) { 1101 #ifdef STGE_EVENT_COUNTERS 1102 if (isr & IS_TxDMAComplete) 1103 STGE_EVCNT_INCR(&sc->sc_ev_txdmaintr); 1104 #endif 1105 stge_txintr(sc); 1106 } 1107 1108 /* Statistics overflow. */ 1109 if (isr & IS_UpdateStats) 1110 stge_stats_update(sc); 1111 1112 /* Transmission errors. */ 1113 if (isr & IS_TxComplete) { 1114 STGE_EVCNT_INCR(&sc->sc_ev_txindintr); 1115 for (;;) { 1116 txstat = bus_space_read_4(sc->sc_st, sc->sc_sh, 1117 STGE_TxStatus); 1118 if ((txstat & TS_TxComplete) == 0) 1119 break; 1120 if (txstat & TS_TxUnderrun) { 1121 sc->sc_txthresh++; 1122 if (sc->sc_txthresh > 0x0fff) 1123 sc->sc_txthresh = 0x0fff; 1124 printf("%s: transmit underrun, new " 1125 "threshold: %d bytes\n", 1126 sc->sc_dev.dv_xname, 1127 sc->sc_txthresh << 5); 1128 } 1129 if (txstat & TS_MaxCollisions) 1130 printf("%s: excessive collisions\n", 1131 sc->sc_dev.dv_xname); 1132 } 1133 wantinit = 1; 1134 } 1135 1136 } 1137 1138 if (wantinit) 1139 stge_init(ifp); 1140 1141 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable, 1142 sc->sc_IntEnable); 1143 1144 /* Try to get more packets going. */ 1145 stge_start(ifp); 1146 1147 return (1); 1148 } 1149 1150 /* 1151 * stge_txintr: 1152 * 1153 * Helper; handle transmit interrupts. 1154 */ 1155 static void 1156 stge_txintr(struct stge_softc *sc) 1157 { 1158 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1159 struct stge_descsoft *ds; 1160 uint64_t control; 1161 int i; 1162 1163 ifp->if_flags &= ~IFF_OACTIVE; 1164 1165 /* 1166 * Go through our Tx list and free mbufs for those 1167 * frames which have been transmitted. 1168 */ 1169 for (i = sc->sc_txdirty; sc->sc_txpending != 0; 1170 i = STGE_NEXTTX(i), sc->sc_txpending--) { 1171 ds = &sc->sc_txsoft[i]; 1172 1173 STGE_CDTXSYNC(sc, i, 1174 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1175 1176 control = le64toh(sc->sc_txdescs[i].tfd_control); 1177 if ((control & TFD_TFDDone) == 0) 1178 break; 1179 1180 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 1181 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1182 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap); 1183 m_freem(ds->ds_mbuf); 1184 ds->ds_mbuf = NULL; 1185 } 1186 1187 /* Update the dirty transmit buffer pointer. */ 1188 sc->sc_txdirty = i; 1189 1190 /* 1191 * If there are no more pending transmissions, cancel the watchdog 1192 * timer. 1193 */ 1194 if (sc->sc_txpending == 0) 1195 ifp->if_timer = 0; 1196 } 1197 1198 /* 1199 * stge_rxintr: 1200 * 1201 * Helper; handle receive interrupts. 1202 */ 1203 static void 1204 stge_rxintr(struct stge_softc *sc) 1205 { 1206 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1207 struct stge_descsoft *ds; 1208 struct mbuf *m, *tailm; 1209 uint64_t status; 1210 int i, len; 1211 1212 for (i = sc->sc_rxptr;; i = STGE_NEXTRX(i)) { 1213 ds = &sc->sc_rxsoft[i]; 1214 1215 STGE_CDRXSYNC(sc, i, 1216 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1217 1218 status = le64toh(sc->sc_rxdescs[i].rfd_status); 1219 1220 if ((status & RFD_RFDDone) == 0) 1221 break; 1222 1223 if (__predict_false(sc->sc_rxdiscard)) { 1224 STGE_INIT_RXDESC(sc, i); 1225 if (status & RFD_FrameEnd) { 1226 /* Reset our state. */ 1227 sc->sc_rxdiscard = 0; 1228 } 1229 continue; 1230 } 1231 1232 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0, 1233 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1234 1235 m = ds->ds_mbuf; 1236 1237 /* 1238 * Add a new receive buffer to the ring. 1239 */ 1240 if (stge_add_rxbuf(sc, i) != 0) { 1241 /* 1242 * Failed, throw away what we've done so 1243 * far, and discard the rest of the packet. 1244 */ 1245 ifp->if_ierrors++; 1246 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0, 1247 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1248 STGE_INIT_RXDESC(sc, i); 1249 if ((status & RFD_FrameEnd) == 0) 1250 sc->sc_rxdiscard = 1; 1251 if (sc->sc_rxhead != NULL) 1252 m_freem(sc->sc_rxhead); 1253 STGE_RXCHAIN_RESET(sc); 1254 continue; 1255 } 1256 1257 #ifdef DIAGNOSTIC 1258 if (status & RFD_FrameStart) { 1259 KASSERT(sc->sc_rxhead == NULL); 1260 KASSERT(sc->sc_rxtailp == &sc->sc_rxhead); 1261 } 1262 #endif 1263 1264 STGE_RXCHAIN_LINK(sc, m); 1265 1266 /* 1267 * If this is not the end of the packet, keep 1268 * looking. 1269 */ 1270 if ((status & RFD_FrameEnd) == 0) { 1271 sc->sc_rxlen += m->m_len; 1272 continue; 1273 } 1274 1275 /* 1276 * Okay, we have the entire packet now... 1277 */ 1278 *sc->sc_rxtailp = NULL; 1279 m = sc->sc_rxhead; 1280 tailm = sc->sc_rxtail; 1281 1282 STGE_RXCHAIN_RESET(sc); 1283 1284 /* 1285 * If the packet had an error, drop it. Note we 1286 * count the error later in the periodic stats update. 1287 */ 1288 if (status & (RFD_RxFIFOOverrun | RFD_RxRuntFrame | 1289 RFD_RxAlignmentError | RFD_RxFCSError | 1290 RFD_RxLengthError)) { 1291 m_freem(m); 1292 continue; 1293 } 1294 1295 /* 1296 * No errors. 1297 * 1298 * Note we have configured the chip to not include 1299 * the CRC at the end of the packet. 1300 */ 1301 len = RFD_RxDMAFrameLen(status); 1302 tailm->m_len = len - sc->sc_rxlen; 1303 1304 /* 1305 * If the packet is small enough to fit in a 1306 * single header mbuf, allocate one and copy 1307 * the data into it. This greatly reduces 1308 * memory consumption when we receive lots 1309 * of small packets. 1310 */ 1311 if (stge_copy_small != 0 && len <= (MHLEN - 2)) { 1312 struct mbuf *nm; 1313 MGETHDR(nm, M_DONTWAIT, MT_DATA); 1314 if (nm == NULL) { 1315 ifp->if_ierrors++; 1316 m_freem(m); 1317 continue; 1318 } 1319 nm->m_data += 2; 1320 nm->m_pkthdr.len = nm->m_len = len; 1321 m_copydata(m, 0, len, mtod(nm, caddr_t)); 1322 m_freem(m); 1323 m = nm; 1324 } 1325 1326 /* 1327 * Set the incoming checksum information for the packet. 1328 */ 1329 if (status & RFD_IPDetected) { 1330 STGE_EVCNT_INCR(&sc->sc_ev_rxipsum); 1331 m->m_pkthdr.csum_flags |= M_CSUM_IPv4; 1332 if (status & RFD_IPError) 1333 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD; 1334 if (status & RFD_TCPDetected) { 1335 STGE_EVCNT_INCR(&sc->sc_ev_rxtcpsum); 1336 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4; 1337 if (status & RFD_TCPError) 1338 m->m_pkthdr.csum_flags |= 1339 M_CSUM_TCP_UDP_BAD; 1340 } else if (status & RFD_UDPDetected) { 1341 STGE_EVCNT_INCR(&sc->sc_ev_rxudpsum); 1342 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4; 1343 if (status & RFD_UDPError) 1344 m->m_pkthdr.csum_flags |= 1345 M_CSUM_TCP_UDP_BAD; 1346 } 1347 } 1348 1349 m->m_pkthdr.rcvif = ifp; 1350 m->m_pkthdr.len = len; 1351 1352 #if NBPFILTER > 0 1353 /* 1354 * Pass this up to any BPF listeners, but only 1355 * pass if up the stack if it's for us. 1356 */ 1357 if (ifp->if_bpf) 1358 bpf_mtap(ifp->if_bpf, m); 1359 #endif /* NBPFILTER > 0 */ 1360 #ifdef STGE_VLAN_UNTAG 1361 /* 1362 * Check for VLAN tagged packets 1363 */ 1364 if (status & RFD_VLANDetected) 1365 VLAN_INPUT_TAG(ifp, m, RFD_TCI(status), continue); 1366 1367 #endif 1368 #if 0 1369 if (status & RFD_VLANDetected) { 1370 struct ether_header *eh; 1371 u_int16_t etype; 1372 1373 eh = mtod(m, struct ether_header *); 1374 etype = ntohs(eh->ether_type); 1375 printf("%s: VLANtag detected (TCI %d) etype %x\n", 1376 ifp->if_xname, (u_int16_t) RFD_TCI(status), 1377 etype); 1378 } 1379 #endif 1380 /* Pass it on. */ 1381 (*ifp->if_input)(ifp, m); 1382 } 1383 1384 /* Update the receive pointer. */ 1385 sc->sc_rxptr = i; 1386 } 1387 1388 /* 1389 * stge_tick: 1390 * 1391 * One second timer, used to tick the MII. 1392 */ 1393 static void 1394 stge_tick(void *arg) 1395 { 1396 struct stge_softc *sc = arg; 1397 int s; 1398 1399 s = splnet(); 1400 mii_tick(&sc->sc_mii); 1401 stge_stats_update(sc); 1402 splx(s); 1403 1404 callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc); 1405 } 1406 1407 /* 1408 * stge_stats_update: 1409 * 1410 * Read the TC9021 statistics counters. 1411 */ 1412 static void 1413 stge_stats_update(struct stge_softc *sc) 1414 { 1415 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1416 bus_space_tag_t st = sc->sc_st; 1417 bus_space_handle_t sh = sc->sc_sh; 1418 1419 (void) bus_space_read_4(st, sh, STGE_OctetRcvOk); 1420 1421 ifp->if_ipackets += 1422 bus_space_read_4(st, sh, STGE_FramesRcvdOk); 1423 1424 ifp->if_ierrors += 1425 (u_int) bus_space_read_2(st, sh, STGE_FramesLostRxErrors); 1426 1427 (void) bus_space_read_4(st, sh, STGE_OctetXmtdOk); 1428 1429 ifp->if_opackets += 1430 bus_space_read_4(st, sh, STGE_FramesXmtdOk); 1431 1432 ifp->if_collisions += 1433 bus_space_read_4(st, sh, STGE_LateCollisions) + 1434 bus_space_read_4(st, sh, STGE_MultiColFrames) + 1435 bus_space_read_4(st, sh, STGE_SingleColFrames); 1436 1437 ifp->if_oerrors += 1438 (u_int) bus_space_read_2(st, sh, STGE_FramesAbortXSColls) + 1439 (u_int) bus_space_read_2(st, sh, STGE_FramesWEXDeferal); 1440 } 1441 1442 /* 1443 * stge_reset: 1444 * 1445 * Perform a soft reset on the TC9021. 1446 */ 1447 static void 1448 stge_reset(struct stge_softc *sc) 1449 { 1450 uint32_t ac; 1451 int i; 1452 1453 ac = bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl); 1454 1455 /* 1456 * Only assert RstOut if we're fiber. We need GMII clocks 1457 * to be present in order for the reset to complete on fiber 1458 * cards. 1459 */ 1460 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl, 1461 ac | AC_GlobalReset | AC_RxReset | AC_TxReset | 1462 AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit | 1463 (sc->sc_usefiber ? AC_RstOut : 0)); 1464 1465 delay(50000); 1466 1467 for (i = 0; i < STGE_TIMEOUT; i++) { 1468 delay(5000); 1469 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) & 1470 AC_ResetBusy) == 0) 1471 break; 1472 } 1473 1474 if (i == STGE_TIMEOUT) 1475 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname); 1476 1477 delay(1000); 1478 } 1479 1480 /* 1481 * stge_init: [ ifnet interface function ] 1482 * 1483 * Initialize the interface. Must be called at splnet(). 1484 */ 1485 static int 1486 stge_init(struct ifnet *ifp) 1487 { 1488 struct stge_softc *sc = ifp->if_softc; 1489 bus_space_tag_t st = sc->sc_st; 1490 bus_space_handle_t sh = sc->sc_sh; 1491 struct stge_descsoft *ds; 1492 int i, error = 0; 1493 1494 /* 1495 * Cancel any pending I/O. 1496 */ 1497 stge_stop(ifp, 0); 1498 1499 /* 1500 * Reset the chip to a known state. 1501 */ 1502 stge_reset(sc); 1503 1504 /* 1505 * Initialize the transmit descriptor ring. 1506 */ 1507 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs)); 1508 for (i = 0; i < STGE_NTXDESC; i++) { 1509 sc->sc_txdescs[i].tfd_next = htole64( 1510 STGE_CDTXADDR(sc, STGE_NEXTTX(i))); 1511 sc->sc_txdescs[i].tfd_control = htole64(TFD_TFDDone); 1512 } 1513 sc->sc_txpending = 0; 1514 sc->sc_txdirty = 0; 1515 sc->sc_txlast = STGE_NTXDESC - 1; 1516 1517 /* 1518 * Initialize the receive descriptor and receive job 1519 * descriptor rings. 1520 */ 1521 for (i = 0; i < STGE_NRXDESC; i++) { 1522 ds = &sc->sc_rxsoft[i]; 1523 if (ds->ds_mbuf == NULL) { 1524 if ((error = stge_add_rxbuf(sc, i)) != 0) { 1525 printf("%s: unable to allocate or map rx " 1526 "buffer %d, error = %d\n", 1527 sc->sc_dev.dv_xname, i, error); 1528 /* 1529 * XXX Should attempt to run with fewer receive 1530 * XXX buffers instead of just failing. 1531 */ 1532 stge_rxdrain(sc); 1533 goto out; 1534 } 1535 } else 1536 STGE_INIT_RXDESC(sc, i); 1537 } 1538 sc->sc_rxptr = 0; 1539 sc->sc_rxdiscard = 0; 1540 STGE_RXCHAIN_RESET(sc); 1541 1542 /* Set the station address. */ 1543 for (i = 0; i < 6; i++) 1544 bus_space_write_1(st, sh, STGE_StationAddress0 + i, 1545 LLADDR(ifp->if_sadl)[i]); 1546 1547 /* 1548 * Set the statistics masks. Disable all the RMON stats, 1549 * and disable selected stats in the non-RMON stats registers. 1550 */ 1551 bus_space_write_4(st, sh, STGE_RMONStatisticsMask, 0xffffffff); 1552 bus_space_write_4(st, sh, STGE_StatisticsMask, 1553 (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) | 1554 (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) | 1555 (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) | 1556 (1U << 21)); 1557 1558 /* Set up the receive filter. */ 1559 stge_set_filter(sc); 1560 1561 /* 1562 * Give the transmit and receive ring to the chip. 1563 */ 1564 bus_space_write_4(st, sh, STGE_TFDListPtrHi, 0); /* NOTE: 32-bit DMA */ 1565 bus_space_write_4(st, sh, STGE_TFDListPtrLo, 1566 STGE_CDTXADDR(sc, sc->sc_txdirty)); 1567 1568 bus_space_write_4(st, sh, STGE_RFDListPtrHi, 0); /* NOTE: 32-bit DMA */ 1569 bus_space_write_4(st, sh, STGE_RFDListPtrLo, 1570 STGE_CDRXADDR(sc, sc->sc_rxptr)); 1571 1572 /* 1573 * Initialize the Tx auto-poll period. It's OK to make this number 1574 * large (255 is the max, but we use 127) -- we explicitly kick the 1575 * transmit engine when there's actually a packet. 1576 */ 1577 bus_space_write_1(st, sh, STGE_TxDMAPollPeriod, 127); 1578 1579 /* ..and the Rx auto-poll period. */ 1580 bus_space_write_1(st, sh, STGE_RxDMAPollPeriod, 64); 1581 1582 /* Initialize the Tx start threshold. */ 1583 bus_space_write_2(st, sh, STGE_TxStartThresh, sc->sc_txthresh); 1584 1585 /* RX DMA thresholds, from linux */ 1586 bus_space_write_1(st, sh, STGE_RxDMABurstThresh, 0x30); 1587 bus_space_write_1(st, sh, STGE_RxDMAUrgentThresh, 0x30); 1588 1589 /* 1590 * Initialize the Rx DMA interrupt control register. We 1591 * request an interrupt after every incoming packet, but 1592 * defer it for 32us (64 * 512 ns). When the number of 1593 * interrupts pending reaches 8, we stop deferring the 1594 * interrupt, and signal it immediately. 1595 */ 1596 bus_space_write_4(st, sh, STGE_RxDMAIntCtrl, 1597 RDIC_RxFrameCount(8) | RDIC_RxDMAWaitTime(512)); 1598 1599 /* 1600 * Initialize the interrupt mask. 1601 */ 1602 sc->sc_IntEnable = IS_HostError | IS_TxComplete | IS_UpdateStats | 1603 IS_TxDMAComplete | IS_RxDMAComplete | IS_RFDListEnd; 1604 bus_space_write_2(st, sh, STGE_IntStatus, 0xffff); 1605 bus_space_write_2(st, sh, STGE_IntEnable, sc->sc_IntEnable); 1606 1607 /* 1608 * Configure the DMA engine. 1609 * XXX Should auto-tune TxBurstLimit. 1610 */ 1611 bus_space_write_4(st, sh, STGE_DMACtrl, sc->sc_DMACtrl | 1612 DMAC_TxBurstLimit(3)); 1613 1614 /* 1615 * Send a PAUSE frame when we reach 29,696 bytes in the Rx 1616 * FIFO, and send an un-PAUSE frame when the FIFO is totally 1617 * empty again. 1618 */ 1619 bus_space_write_2(st, sh, STGE_FlowOnTresh, 29696 / 16); 1620 bus_space_write_2(st, sh, STGE_FlowOffThresh, 0); 1621 1622 /* 1623 * Set the maximum frame size. 1624 */ 1625 bus_space_write_2(st, sh, STGE_MaxFrameSize, 1626 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 1627 ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ? 1628 ETHER_VLAN_ENCAP_LEN : 0)); 1629 1630 /* 1631 * Initialize MacCtrl -- do it before setting the media, 1632 * as setting the media will actually program the register. 1633 * 1634 * Note: We have to poke the IFS value before poking 1635 * anything else. 1636 */ 1637 sc->sc_MACCtrl = MC_IFSSelect(0); 1638 bus_space_write_4(st, sh, STGE_MACCtrl, sc->sc_MACCtrl); 1639 sc->sc_MACCtrl |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable; 1640 #ifdef STGE_VLAN_UNTAG 1641 sc->sc_MACCtrl |= MC_AutoVLANuntagging; 1642 #endif 1643 1644 if (sc->sc_rev >= 6) { /* >= B.2 */ 1645 /* Multi-frag frame bug work-around. */ 1646 bus_space_write_2(st, sh, STGE_DebugCtrl, 1647 bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0200); 1648 1649 /* Tx Poll Now bug work-around. */ 1650 bus_space_write_2(st, sh, STGE_DebugCtrl, 1651 bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0010); 1652 /* XXX ? from linux */ 1653 bus_space_write_2(st, sh, STGE_DebugCtrl, 1654 bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0020); 1655 } 1656 1657 /* 1658 * Set the current media. 1659 */ 1660 mii_mediachg(&sc->sc_mii); 1661 1662 /* 1663 * Start the one second MII clock. 1664 */ 1665 callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc); 1666 1667 /* 1668 * ...all done! 1669 */ 1670 ifp->if_flags |= IFF_RUNNING; 1671 ifp->if_flags &= ~IFF_OACTIVE; 1672 1673 out: 1674 if (error) 1675 printf("%s: interface not running\n", sc->sc_dev.dv_xname); 1676 return (error); 1677 } 1678 1679 /* 1680 * stge_drain: 1681 * 1682 * Drain the receive queue. 1683 */ 1684 static void 1685 stge_rxdrain(struct stge_softc *sc) 1686 { 1687 struct stge_descsoft *ds; 1688 int i; 1689 1690 for (i = 0; i < STGE_NRXDESC; i++) { 1691 ds = &sc->sc_rxsoft[i]; 1692 if (ds->ds_mbuf != NULL) { 1693 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap); 1694 ds->ds_mbuf->m_next = NULL; 1695 m_freem(ds->ds_mbuf); 1696 ds->ds_mbuf = NULL; 1697 } 1698 } 1699 } 1700 1701 /* 1702 * stge_stop: [ ifnet interface function ] 1703 * 1704 * Stop transmission on the interface. 1705 */ 1706 static void 1707 stge_stop(struct ifnet *ifp, int disable) 1708 { 1709 struct stge_softc *sc = ifp->if_softc; 1710 struct stge_descsoft *ds; 1711 int i; 1712 1713 /* 1714 * Stop the one second clock. 1715 */ 1716 callout_stop(&sc->sc_tick_ch); 1717 1718 /* Down the MII. */ 1719 mii_down(&sc->sc_mii); 1720 1721 /* 1722 * Disable interrupts. 1723 */ 1724 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable, 0); 1725 1726 /* 1727 * Stop receiver, transmitter, and stats update. 1728 */ 1729 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl, 1730 MC_StatisticsDisable | MC_TxDisable | MC_RxDisable); 1731 1732 /* 1733 * Stop the transmit and receive DMA. 1734 */ 1735 stge_dma_wait(sc); 1736 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrHi, 0); 1737 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrLo, 0); 1738 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrHi, 0); 1739 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrLo, 0); 1740 1741 /* 1742 * Release any queued transmit buffers. 1743 */ 1744 for (i = 0; i < STGE_NTXDESC; i++) { 1745 ds = &sc->sc_txsoft[i]; 1746 if (ds->ds_mbuf != NULL) { 1747 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap); 1748 m_freem(ds->ds_mbuf); 1749 ds->ds_mbuf = NULL; 1750 } 1751 } 1752 1753 if (disable) 1754 stge_rxdrain(sc); 1755 1756 /* 1757 * Mark the interface down and cancel the watchdog timer. 1758 */ 1759 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1760 ifp->if_timer = 0; 1761 } 1762 1763 static int 1764 stge_eeprom_wait(struct stge_softc *sc) 1765 { 1766 int i; 1767 1768 for (i = 0; i < STGE_TIMEOUT; i++) { 1769 delay(1000); 1770 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl) & 1771 EC_EepromBusy) == 0) 1772 return (0); 1773 } 1774 return (1); 1775 } 1776 1777 /* 1778 * stge_read_eeprom: 1779 * 1780 * Read data from the serial EEPROM. 1781 */ 1782 static void 1783 stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data) 1784 { 1785 1786 if (stge_eeprom_wait(sc)) 1787 printf("%s: EEPROM failed to come ready\n", 1788 sc->sc_dev.dv_xname); 1789 1790 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl, 1791 EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR)); 1792 if (stge_eeprom_wait(sc)) 1793 printf("%s: EEPROM read timed out\n", 1794 sc->sc_dev.dv_xname); 1795 *data = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromData); 1796 } 1797 1798 /* 1799 * stge_add_rxbuf: 1800 * 1801 * Add a receive buffer to the indicated descriptor. 1802 */ 1803 static int 1804 stge_add_rxbuf(struct stge_softc *sc, int idx) 1805 { 1806 struct stge_descsoft *ds = &sc->sc_rxsoft[idx]; 1807 struct mbuf *m; 1808 int error; 1809 1810 MGETHDR(m, M_DONTWAIT, MT_DATA); 1811 if (m == NULL) 1812 return (ENOBUFS); 1813 1814 MCLGET(m, M_DONTWAIT); 1815 if ((m->m_flags & M_EXT) == 0) { 1816 m_freem(m); 1817 return (ENOBUFS); 1818 } 1819 1820 m->m_data = m->m_ext.ext_buf + 2; 1821 m->m_len = MCLBYTES - 2; 1822 1823 if (ds->ds_mbuf != NULL) 1824 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap); 1825 1826 ds->ds_mbuf = m; 1827 1828 error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap, 1829 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT); 1830 if (error) { 1831 printf("%s: can't load rx DMA map %d, error = %d\n", 1832 sc->sc_dev.dv_xname, idx, error); 1833 panic("stge_add_rxbuf"); /* XXX */ 1834 } 1835 1836 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0, 1837 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD); 1838 1839 STGE_INIT_RXDESC(sc, idx); 1840 1841 return (0); 1842 } 1843 1844 /* 1845 * stge_set_filter: 1846 * 1847 * Set up the receive filter. 1848 */ 1849 static void 1850 stge_set_filter(struct stge_softc *sc) 1851 { 1852 struct ethercom *ec = &sc->sc_ethercom; 1853 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1854 struct ether_multi *enm; 1855 struct ether_multistep step; 1856 uint32_t crc; 1857 uint32_t mchash[2]; 1858 1859 sc->sc_ReceiveMode = RM_ReceiveUnicast; 1860 if (ifp->if_flags & IFF_BROADCAST) 1861 sc->sc_ReceiveMode |= RM_ReceiveBroadcast; 1862 1863 /* XXX: ST1023 only works in promiscuous mode */ 1864 if (sc->sc_stge1023) 1865 ifp->if_flags |= IFF_PROMISC; 1866 1867 if (ifp->if_flags & IFF_PROMISC) { 1868 sc->sc_ReceiveMode |= RM_ReceiveAllFrames; 1869 goto allmulti; 1870 } 1871 1872 /* 1873 * Set up the multicast address filter by passing all multicast 1874 * addresses through a CRC generator, and then using the low-order 1875 * 6 bits as an index into the 64 bit multicast hash table. The 1876 * high order bits select the register, while the rest of the bits 1877 * select the bit within the register. 1878 */ 1879 1880 memset(mchash, 0, sizeof(mchash)); 1881 1882 ETHER_FIRST_MULTI(step, ec, enm); 1883 if (enm == NULL) 1884 goto done; 1885 1886 while (enm != NULL) { 1887 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 1888 /* 1889 * We must listen to a range of multicast addresses. 1890 * For now, just accept all multicasts, rather than 1891 * trying to set only those filter bits needed to match 1892 * the range. (At this time, the only use of address 1893 * ranges is for IP multicast routing, for which the 1894 * range is big enough to require all bits set.) 1895 */ 1896 goto allmulti; 1897 } 1898 1899 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN); 1900 1901 /* Just want the 6 least significant bits. */ 1902 crc &= 0x3f; 1903 1904 /* Set the corresponding bit in the hash table. */ 1905 mchash[crc >> 5] |= 1 << (crc & 0x1f); 1906 1907 ETHER_NEXT_MULTI(step, enm); 1908 } 1909 1910 sc->sc_ReceiveMode |= RM_ReceiveMulticastHash; 1911 1912 ifp->if_flags &= ~IFF_ALLMULTI; 1913 goto done; 1914 1915 allmulti: 1916 ifp->if_flags |= IFF_ALLMULTI; 1917 sc->sc_ReceiveMode |= RM_ReceiveMulticast; 1918 1919 done: 1920 if ((ifp->if_flags & IFF_ALLMULTI) == 0) { 1921 /* 1922 * Program the multicast hash table. 1923 */ 1924 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable0, 1925 mchash[0]); 1926 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable1, 1927 mchash[1]); 1928 } 1929 1930 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_ReceiveMode, 1931 sc->sc_ReceiveMode); 1932 } 1933 1934 /* 1935 * stge_mii_readreg: [mii interface function] 1936 * 1937 * Read a PHY register on the MII of the TC9021. 1938 */ 1939 static int 1940 stge_mii_readreg(struct device *self, int phy, int reg) 1941 { 1942 1943 return (mii_bitbang_readreg(self, &stge_mii_bitbang_ops, phy, reg)); 1944 } 1945 1946 /* 1947 * stge_mii_writereg: [mii interface function] 1948 * 1949 * Write a PHY register on the MII of the TC9021. 1950 */ 1951 static void 1952 stge_mii_writereg(struct device *self, int phy, int reg, int val) 1953 { 1954 1955 mii_bitbang_writereg(self, &stge_mii_bitbang_ops, phy, reg, val); 1956 } 1957 1958 /* 1959 * stge_mii_statchg: [mii interface function] 1960 * 1961 * Callback from MII layer when media changes. 1962 */ 1963 static void 1964 stge_mii_statchg(struct device *self) 1965 { 1966 struct stge_softc *sc = (struct stge_softc *) self; 1967 1968 if (sc->sc_mii.mii_media_active & IFM_FDX) 1969 sc->sc_MACCtrl |= MC_DuplexSelect; 1970 else 1971 sc->sc_MACCtrl &= ~MC_DuplexSelect; 1972 1973 /* XXX 802.1x flow-control? */ 1974 1975 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl, sc->sc_MACCtrl); 1976 } 1977 1978 /* 1979 * sste_mii_bitbang_read: [mii bit-bang interface function] 1980 * 1981 * Read the MII serial port for the MII bit-bang module. 1982 */ 1983 static uint32_t 1984 stge_mii_bitbang_read(struct device *self) 1985 { 1986 struct stge_softc *sc = (void *) self; 1987 1988 return (bus_space_read_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl)); 1989 } 1990 1991 /* 1992 * stge_mii_bitbang_write: [mii big-bang interface function] 1993 * 1994 * Write the MII serial port for the MII bit-bang module. 1995 */ 1996 static void 1997 stge_mii_bitbang_write(struct device *self, uint32_t val) 1998 { 1999 struct stge_softc *sc = (void *) self; 2000 2001 bus_space_write_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl, 2002 val | sc->sc_PhyCtrl); 2003 } 2004 2005 /* 2006 * stge_mediastatus: [ifmedia interface function] 2007 * 2008 * Get the current interface media status. 2009 */ 2010 static void 2011 stge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 2012 { 2013 struct stge_softc *sc = ifp->if_softc; 2014 2015 mii_pollstat(&sc->sc_mii); 2016 ifmr->ifm_status = sc->sc_mii.mii_media_status; 2017 ifmr->ifm_active = sc->sc_mii.mii_media_active; 2018 } 2019 2020 /* 2021 * stge_mediachange: [ifmedia interface function] 2022 * 2023 * Set hardware to newly-selected media. 2024 */ 2025 static int 2026 stge_mediachange(struct ifnet *ifp) 2027 { 2028 struct stge_softc *sc = ifp->if_softc; 2029 2030 if (ifp->if_flags & IFF_UP) 2031 mii_mediachg(&sc->sc_mii); 2032 return (0); 2033 } 2034