1 /* $OpenBSD: if_et.c,v 1.43 2023/11/10 15:51:20 bluhm Exp $ */ 2 /* 3 * Copyright (c) 2007 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Sepherosa Ziehau <sepherosa@gmail.com> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 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 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $DragonFly: src/sys/dev/netif/et/if_et.c,v 1.1 2007/10/12 14:12:42 sephe Exp $ 36 */ 37 38 #include "bpfilter.h" 39 40 #include <sys/param.h> 41 #include <sys/endian.h> 42 #include <sys/systm.h> 43 #include <sys/sockio.h> 44 #include <sys/mbuf.h> 45 #include <sys/queue.h> 46 #include <sys/kernel.h> 47 #include <sys/device.h> 48 #include <sys/timeout.h> 49 #include <sys/socket.h> 50 51 #include <machine/bus.h> 52 53 #include <net/if.h> 54 #include <net/if_dl.h> 55 #include <net/if_media.h> 56 57 #include <netinet/in.h> 58 #include <netinet/if_ether.h> 59 60 #if NBPFILTER > 0 61 #include <net/bpf.h> 62 #endif 63 64 #include <dev/mii/miivar.h> 65 66 #include <dev/pci/pcireg.h> 67 #include <dev/pci/pcivar.h> 68 #include <dev/pci/pcidevs.h> 69 70 #include <dev/pci/if_etreg.h> 71 72 /* XXX temporary porting goop */ 73 #define KKASSERT(cond) if (!(cond)) panic("KKASSERT: %s in %s", #cond, __func__) 74 #undef KASSERT 75 #define KASSERT(cond, complaint) if (!(cond)) panic complaint 76 77 /* these macros in particular need to die, so gross */ 78 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask)) 79 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask)) 80 #define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask)) 81 /* XXX end porting goop */ 82 83 int et_match(struct device *, void *, void *); 84 void et_attach(struct device *, struct device *, void *); 85 int et_detach(struct device *, int); 86 87 int et_miibus_readreg(struct device *, int, int); 88 void et_miibus_writereg(struct device *, int, int, int); 89 void et_miibus_statchg(struct device *); 90 91 int et_init(struct ifnet *); 92 int et_ioctl(struct ifnet *, u_long, caddr_t); 93 void et_start(struct ifnet *); 94 void et_watchdog(struct ifnet *); 95 int et_ifmedia_upd(struct ifnet *); 96 void et_ifmedia_sts(struct ifnet *, struct ifmediareq *); 97 98 int et_intr(void *); 99 void et_enable_intrs(struct et_softc *, uint32_t); 100 void et_disable_intrs(struct et_softc *); 101 void et_rxeof(struct et_softc *); 102 void et_txeof(struct et_softc *); 103 void et_txtick(void *); 104 105 int et_dma_alloc(struct et_softc *); 106 void et_dma_free(struct et_softc *); 107 int et_dma_mem_create(struct et_softc *, bus_size_t, 108 void **, bus_addr_t *, bus_dmamap_t *, bus_dma_segment_t *); 109 void et_dma_mem_destroy(struct et_softc *, void *, bus_dmamap_t); 110 int et_dma_mbuf_create(struct et_softc *); 111 void et_dma_mbuf_destroy(struct et_softc *, int, const int[]); 112 113 int et_init_tx_ring(struct et_softc *); 114 int et_init_rx_ring(struct et_softc *); 115 void et_free_tx_ring(struct et_softc *); 116 void et_free_rx_ring(struct et_softc *); 117 int et_encap(struct et_softc *, struct mbuf **); 118 int et_newbuf(struct et_rxbuf_data *, int, int, int); 119 int et_newbuf_cluster(struct et_rxbuf_data *, int, int); 120 int et_newbuf_hdr(struct et_rxbuf_data *, int, int); 121 122 void et_stop(struct et_softc *); 123 int et_chip_init(struct et_softc *); 124 void et_chip_attach(struct et_softc *); 125 void et_init_mac(struct et_softc *); 126 void et_init_rxmac(struct et_softc *); 127 void et_init_txmac(struct et_softc *); 128 int et_init_rxdma(struct et_softc *); 129 int et_init_txdma(struct et_softc *); 130 int et_start_rxdma(struct et_softc *); 131 int et_start_txdma(struct et_softc *); 132 int et_stop_rxdma(struct et_softc *); 133 int et_stop_txdma(struct et_softc *); 134 int et_enable_txrx(struct et_softc *); 135 void et_reset(struct et_softc *); 136 int et_bus_config(struct et_softc *); 137 void et_get_eaddr(struct et_softc *, uint8_t[]); 138 void et_setmulti(struct et_softc *); 139 void et_tick(void *); 140 141 static int et_rx_intr_npkts = 32; 142 static int et_rx_intr_delay = 20; /* x10 usec */ 143 static int et_tx_intr_nsegs = 128; 144 static uint32_t et_timer = 1000 * 1000 * 1000; /* nanosec */ 145 146 struct et_bsize { 147 int bufsize; 148 et_newbuf_t newbuf; 149 }; 150 151 static const struct et_bsize et_bufsize[ET_RX_NRING] = { 152 { .bufsize = 0, .newbuf = et_newbuf_hdr }, 153 { .bufsize = 0, .newbuf = et_newbuf_cluster }, 154 }; 155 156 const struct pci_matchid et_devices[] = { 157 { PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310_FE }, 158 { PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310_GBE } 159 }; 160 161 const struct cfattach et_ca = { 162 sizeof (struct et_softc), et_match, et_attach, et_detach 163 }; 164 165 struct cfdriver et_cd = { 166 NULL, "et", DV_IFNET 167 }; 168 169 int 170 et_match(struct device *dev, void *match, void *aux) 171 { 172 return pci_matchbyid((struct pci_attach_args *)aux, et_devices, 173 sizeof (et_devices) / sizeof (et_devices[0])); 174 } 175 176 void 177 et_attach(struct device *parent, struct device *self, void *aux) 178 { 179 struct et_softc *sc = (struct et_softc *)self; 180 struct pci_attach_args *pa = aux; 181 pci_chipset_tag_t pc = pa->pa_pc; 182 pci_intr_handle_t ih; 183 const char *intrstr; 184 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 185 pcireg_t memtype; 186 int error; 187 188 /* 189 * Initialize tunables 190 */ 191 sc->sc_rx_intr_npkts = et_rx_intr_npkts; 192 sc->sc_rx_intr_delay = et_rx_intr_delay; 193 sc->sc_tx_intr_nsegs = et_tx_intr_nsegs; 194 sc->sc_timer = et_timer; 195 196 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ET_PCIR_BAR); 197 if (pci_mapreg_map(pa, ET_PCIR_BAR, memtype, 0, &sc->sc_mem_bt, 198 &sc->sc_mem_bh, NULL, &sc->sc_mem_size, 0)) { 199 printf(": can't map mem space\n"); 200 return; 201 } 202 203 if (pci_intr_map(pa, &ih) != 0) { 204 printf(": can't map interrupt\n"); 205 return; 206 } 207 208 intrstr = pci_intr_string(pc, ih); 209 sc->sc_irq_handle = pci_intr_establish(pc, ih, IPL_NET, et_intr, sc, 210 sc->sc_dev.dv_xname); 211 if (sc->sc_irq_handle == NULL) { 212 printf(": could not establish interrupt"); 213 if (intrstr != NULL) 214 printf(" at %s", intrstr); 215 printf("\n"); 216 return; 217 } 218 printf(": %s", intrstr); 219 220 sc->sc_dmat = pa->pa_dmat; 221 sc->sc_pct = pa->pa_pc; 222 sc->sc_pcitag = pa->pa_tag; 223 224 error = et_bus_config(sc); 225 if (error) 226 return; 227 228 et_get_eaddr(sc, sc->sc_arpcom.ac_enaddr); 229 230 printf(", address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr)); 231 232 CSR_WRITE_4(sc, ET_PM, 233 ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE | ET_PM_RXCLK_GATE); 234 235 et_reset(sc); 236 237 et_disable_intrs(sc); 238 239 error = et_dma_alloc(sc); 240 if (error) 241 return; 242 243 ifp->if_softc = sc; 244 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 245 ifp->if_ioctl = et_ioctl; 246 ifp->if_start = et_start; 247 ifp->if_watchdog = et_watchdog; 248 ifq_init_maxlen(&ifp->if_snd, ET_TX_NDESC); 249 strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 250 251 ifp->if_capabilities = IFCAP_VLAN_MTU; 252 253 et_chip_attach(sc); 254 255 sc->sc_miibus.mii_ifp = ifp; 256 sc->sc_miibus.mii_readreg = et_miibus_readreg; 257 sc->sc_miibus.mii_writereg = et_miibus_writereg; 258 sc->sc_miibus.mii_statchg = et_miibus_statchg; 259 260 ifmedia_init(&sc->sc_miibus.mii_media, 0, et_ifmedia_upd, 261 et_ifmedia_sts); 262 mii_attach(self, &sc->sc_miibus, 0xffffffff, MII_PHY_ANY, 263 MII_OFFSET_ANY, 0); 264 if (LIST_FIRST(&sc->sc_miibus.mii_phys) == NULL) { 265 printf("%s: no PHY found!\n", sc->sc_dev.dv_xname); 266 ifmedia_add(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_MANUAL, 267 0, NULL); 268 ifmedia_set(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_MANUAL); 269 } else 270 ifmedia_set(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_AUTO); 271 272 if_attach(ifp); 273 ether_ifattach(ifp); 274 275 timeout_set(&sc->sc_tick, et_tick, sc); 276 timeout_set(&sc->sc_txtick, et_txtick, sc); 277 } 278 279 int 280 et_detach(struct device *self, int flags) 281 { 282 struct et_softc *sc = (struct et_softc *)self; 283 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 284 int s; 285 286 s = splnet(); 287 et_stop(sc); 288 splx(s); 289 290 mii_detach(&sc->sc_miibus, MII_PHY_ANY, MII_OFFSET_ANY); 291 292 /* Delete all remaining media. */ 293 ifmedia_delete_instance(&sc->sc_miibus.mii_media, IFM_INST_ANY); 294 295 ether_ifdetach(ifp); 296 if_detach(ifp); 297 et_dma_free(sc); 298 299 if (sc->sc_irq_handle != NULL) { 300 pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle); 301 sc->sc_irq_handle = NULL; 302 } 303 304 bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size); 305 306 return 0; 307 } 308 309 int 310 et_miibus_readreg(struct device *dev, int phy, int reg) 311 { 312 struct et_softc *sc = (struct et_softc *)dev; 313 uint32_t val; 314 int i, ret; 315 316 /* Stop any pending operations */ 317 CSR_WRITE_4(sc, ET_MII_CMD, 0); 318 319 val = __SHIFTIN(phy, ET_MII_ADDR_PHY) | 320 __SHIFTIN(reg, ET_MII_ADDR_REG); 321 CSR_WRITE_4(sc, ET_MII_ADDR, val); 322 323 /* Start reading */ 324 CSR_WRITE_4(sc, ET_MII_CMD, ET_MII_CMD_READ); 325 326 #define NRETRY 50 327 328 for (i = 0; i < NRETRY; ++i) { 329 val = CSR_READ_4(sc, ET_MII_IND); 330 if ((val & (ET_MII_IND_BUSY | ET_MII_IND_INVALID)) == 0) 331 break; 332 DELAY(50); 333 } 334 if (i == NRETRY) { 335 printf("%s: read phy %d, reg %d timed out\n", 336 sc->sc_dev.dv_xname, phy, reg); 337 ret = 0; 338 goto back; 339 } 340 341 #undef NRETRY 342 343 val = CSR_READ_4(sc, ET_MII_STAT); 344 ret = __SHIFTOUT(val, ET_MII_STAT_VALUE); 345 346 back: 347 /* Make sure that the current operation is stopped */ 348 CSR_WRITE_4(sc, ET_MII_CMD, 0); 349 return ret; 350 } 351 352 void 353 et_miibus_writereg(struct device *dev, int phy, int reg, int val0) 354 { 355 struct et_softc *sc = (struct et_softc *)dev; 356 uint32_t val; 357 int i; 358 359 /* Stop any pending operations */ 360 CSR_WRITE_4(sc, ET_MII_CMD, 0); 361 362 val = __SHIFTIN(phy, ET_MII_ADDR_PHY) | 363 __SHIFTIN(reg, ET_MII_ADDR_REG); 364 CSR_WRITE_4(sc, ET_MII_ADDR, val); 365 366 /* Start writing */ 367 CSR_WRITE_4(sc, ET_MII_CTRL, __SHIFTIN(val0, ET_MII_CTRL_VALUE)); 368 369 #define NRETRY 100 370 371 for (i = 0; i < NRETRY; ++i) { 372 val = CSR_READ_4(sc, ET_MII_IND); 373 if ((val & ET_MII_IND_BUSY) == 0) 374 break; 375 DELAY(50); 376 } 377 if (i == NRETRY) { 378 printf("%s: write phy %d, reg %d timed out\n", 379 sc->sc_dev.dv_xname, phy, reg); 380 et_miibus_readreg(dev, phy, reg); 381 } 382 383 #undef NRETRY 384 385 /* Make sure that the current operation is stopped */ 386 CSR_WRITE_4(sc, ET_MII_CMD, 0); 387 } 388 389 void 390 et_miibus_statchg(struct device *dev) 391 { 392 struct et_softc *sc = (struct et_softc *)dev; 393 struct mii_data *mii = &sc->sc_miibus; 394 uint32_t cfg2, ctrl; 395 396 cfg2 = CSR_READ_4(sc, ET_MAC_CFG2); 397 cfg2 &= ~(ET_MAC_CFG2_MODE_MII | ET_MAC_CFG2_MODE_GMII | 398 ET_MAC_CFG2_FDX | ET_MAC_CFG2_BIGFRM); 399 cfg2 |= ET_MAC_CFG2_LENCHK | ET_MAC_CFG2_CRC | ET_MAC_CFG2_PADCRC | 400 __SHIFTIN(7, ET_MAC_CFG2_PREAMBLE_LEN); 401 402 ctrl = CSR_READ_4(sc, ET_MAC_CTRL); 403 ctrl &= ~(ET_MAC_CTRL_GHDX | ET_MAC_CTRL_MODE_MII); 404 405 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) { 406 cfg2 |= ET_MAC_CFG2_MODE_GMII; 407 } else { 408 cfg2 |= ET_MAC_CFG2_MODE_MII; 409 ctrl |= ET_MAC_CTRL_MODE_MII; 410 } 411 412 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 413 cfg2 |= ET_MAC_CFG2_FDX; 414 else 415 ctrl |= ET_MAC_CTRL_GHDX; 416 417 CSR_WRITE_4(sc, ET_MAC_CTRL, ctrl); 418 CSR_WRITE_4(sc, ET_MAC_CFG2, cfg2); 419 } 420 421 int 422 et_ifmedia_upd(struct ifnet *ifp) 423 { 424 struct et_softc *sc = ifp->if_softc; 425 struct mii_data *mii = &sc->sc_miibus; 426 427 if (mii->mii_instance != 0) { 428 struct mii_softc *miisc; 429 430 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 431 mii_phy_reset(miisc); 432 } 433 mii_mediachg(mii); 434 435 return 0; 436 } 437 438 void 439 et_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 440 { 441 struct et_softc *sc = ifp->if_softc; 442 struct mii_data *mii = &sc->sc_miibus; 443 444 mii_pollstat(mii); 445 ifmr->ifm_active = mii->mii_media_active; 446 ifmr->ifm_status = mii->mii_media_status; 447 } 448 449 void 450 et_stop(struct et_softc *sc) 451 { 452 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 453 454 timeout_del(&sc->sc_tick); 455 timeout_del(&sc->sc_txtick); 456 457 et_stop_rxdma(sc); 458 et_stop_txdma(sc); 459 460 et_disable_intrs(sc); 461 462 et_free_tx_ring(sc); 463 et_free_rx_ring(sc); 464 465 et_reset(sc); 466 467 sc->sc_tx = 0; 468 sc->sc_tx_intr = 0; 469 470 ifp->if_timer = 0; 471 ifp->if_flags &= ~IFF_RUNNING; 472 ifq_clr_oactive(&ifp->if_snd); 473 } 474 475 int 476 et_bus_config(struct et_softc *sc) 477 { 478 uint32_t val; //, max_plsz; 479 // uint16_t ack_latency, replay_timer; 480 481 /* 482 * Test whether EEPROM is valid 483 * NOTE: Read twice to get the correct value 484 */ 485 pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_EEPROM_MISC); 486 val = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_EEPROM_MISC); 487 488 if (val & ET_PCIM_EEPROM_STATUS_ERROR) { 489 printf("%s: EEPROM status error 0x%02x\n", 490 sc->sc_dev.dv_xname, val); 491 return ENXIO; 492 } 493 494 /* TODO: LED */ 495 #if 0 496 /* 497 * Configure ACK latency and replay timer according to 498 * max playload size 499 */ 500 val = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_DEVICE_CAPS); 501 max_plsz = val & ET_PCIM_DEVICE_CAPS_MAX_PLSZ; 502 503 switch (max_plsz) { 504 case ET_PCIV_DEVICE_CAPS_PLSZ_128: 505 ack_latency = ET_PCIV_ACK_LATENCY_128; 506 replay_timer = ET_PCIV_REPLAY_TIMER_128; 507 break; 508 509 case ET_PCIV_DEVICE_CAPS_PLSZ_256: 510 ack_latency = ET_PCIV_ACK_LATENCY_256; 511 replay_timer = ET_PCIV_REPLAY_TIMER_256; 512 break; 513 514 default: 515 ack_latency = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 516 ET_PCIR_ACK_LATENCY) >> 16; 517 replay_timer = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 518 ET_PCIR_REPLAY_TIMER) >> 16; 519 printf("%s: ack latency %u, replay timer %u\n", 520 sc->sc_dev.dv_xname, ack_latency, replay_timer); 521 break; 522 } 523 if (ack_latency != 0) { 524 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 525 ET_PCIR_ACK_LATENCY, ack_latency << 16); 526 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 527 ET_PCIR_REPLAY_TIMER, replay_timer << 16); 528 } 529 530 /* 531 * Set L0s and L1 latency timer to 2us 532 */ 533 val = ET_PCIV_L0S_LATENCY(2) | ET_PCIV_L1_LATENCY(2); 534 pci_conf_write(sc->sc_pct, sc->sc_pcitag, ET_PCIR_L0S_L1_LATENCY, 535 val << 24); 536 537 /* 538 * Set max read request size to 2048 bytes 539 */ 540 val = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 541 ET_PCIR_DEVICE_CTRL) >> 16; 542 val &= ~ET_PCIM_DEVICE_CTRL_MAX_RRSZ; 543 val |= ET_PCIV_DEVICE_CTRL_RRSZ_2K; 544 pci_conf_write(sc->sc_pct, sc->sc_pcitag, ET_PCIR_DEVICE_CTRL, 545 val << 16); 546 #endif 547 548 return 0; 549 } 550 551 void 552 et_get_eaddr(struct et_softc *sc, uint8_t eaddr[]) 553 { 554 uint32_t r; 555 556 r = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_MACADDR_LO); 557 eaddr[0] = r & 0xff; 558 eaddr[1] = (r >> 8) & 0xff; 559 eaddr[2] = (r >> 16) & 0xff; 560 eaddr[3] = (r >> 24) & 0xff; 561 r = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_MACADDR_HI); 562 eaddr[4] = r & 0xff; 563 eaddr[5] = (r >> 8) & 0xff; 564 } 565 566 void 567 et_reset(struct et_softc *sc) 568 { 569 CSR_WRITE_4(sc, ET_MAC_CFG1, 570 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC | 571 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC | 572 ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST); 573 574 CSR_WRITE_4(sc, ET_SWRST, 575 ET_SWRST_TXDMA | ET_SWRST_RXDMA | 576 ET_SWRST_TXMAC | ET_SWRST_RXMAC | 577 ET_SWRST_MAC | ET_SWRST_MAC_STAT | ET_SWRST_MMC); 578 579 CSR_WRITE_4(sc, ET_MAC_CFG1, 580 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC | 581 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC); 582 CSR_WRITE_4(sc, ET_MAC_CFG1, 0); 583 } 584 585 void 586 et_disable_intrs(struct et_softc *sc) 587 { 588 CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff); 589 } 590 591 void 592 et_enable_intrs(struct et_softc *sc, uint32_t intrs) 593 { 594 CSR_WRITE_4(sc, ET_INTR_MASK, ~intrs); 595 } 596 597 int 598 et_dma_alloc(struct et_softc *sc) 599 { 600 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring; 601 struct et_txstatus_data *txsd = &sc->sc_tx_status; 602 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring; 603 struct et_rxstatus_data *rxsd = &sc->sc_rx_status; 604 int i, error; 605 606 /* 607 * Create TX ring DMA stuffs 608 */ 609 error = et_dma_mem_create(sc, ET_TX_RING_SIZE, 610 (void **)&tx_ring->tr_desc, &tx_ring->tr_paddr, &tx_ring->tr_dmap, 611 &tx_ring->tr_seg); 612 if (error) { 613 printf("%s: can't create TX ring DMA stuffs\n", 614 sc->sc_dev.dv_xname); 615 return error; 616 } 617 618 /* 619 * Create TX status DMA stuffs 620 */ 621 error = et_dma_mem_create(sc, sizeof(uint32_t), 622 (void **)&txsd->txsd_status, 623 &txsd->txsd_paddr, &txsd->txsd_dmap, &txsd->txsd_seg); 624 if (error) { 625 printf("%s: can't create TX status DMA stuffs\n", 626 sc->sc_dev.dv_xname); 627 return error; 628 } 629 630 /* 631 * Create DMA stuffs for RX rings 632 */ 633 for (i = 0; i < ET_RX_NRING; ++i) { 634 static const uint32_t rx_ring_posreg[ET_RX_NRING] = 635 { ET_RX_RING0_POS, ET_RX_RING1_POS }; 636 637 struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[i]; 638 639 error = et_dma_mem_create(sc, ET_RX_RING_SIZE, 640 (void **)&rx_ring->rr_desc, 641 &rx_ring->rr_paddr, &rx_ring->rr_dmap, &rx_ring->rr_seg); 642 if (error) { 643 printf("%s: can't create DMA stuffs for " 644 "the %d RX ring\n", sc->sc_dev.dv_xname, i); 645 return error; 646 } 647 rx_ring->rr_posreg = rx_ring_posreg[i]; 648 } 649 650 /* 651 * Create RX stat ring DMA stuffs 652 */ 653 error = et_dma_mem_create(sc, ET_RXSTAT_RING_SIZE, 654 (void **)&rxst_ring->rsr_stat, 655 &rxst_ring->rsr_paddr, &rxst_ring->rsr_dmap, &rxst_ring->rsr_seg); 656 if (error) { 657 printf("%s: can't create RX stat ring DMA stuffs\n", 658 sc->sc_dev.dv_xname); 659 return error; 660 } 661 662 /* 663 * Create RX status DMA stuffs 664 */ 665 error = et_dma_mem_create(sc, sizeof(struct et_rxstatus), 666 (void **)&rxsd->rxsd_status, 667 &rxsd->rxsd_paddr, &rxsd->rxsd_dmap, &rxsd->rxsd_seg); 668 if (error) { 669 printf("%s: can't create RX status DMA stuffs\n", 670 sc->sc_dev.dv_xname); 671 return error; 672 } 673 674 /* 675 * Create mbuf DMA stuffs 676 */ 677 error = et_dma_mbuf_create(sc); 678 if (error) 679 return error; 680 681 return 0; 682 } 683 684 void 685 et_dma_free(struct et_softc *sc) 686 { 687 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring; 688 struct et_txstatus_data *txsd = &sc->sc_tx_status; 689 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring; 690 struct et_rxstatus_data *rxsd = &sc->sc_rx_status; 691 int i, rx_done[ET_RX_NRING]; 692 693 /* 694 * Destroy TX ring DMA stuffs 695 */ 696 et_dma_mem_destroy(sc, tx_ring->tr_desc, tx_ring->tr_dmap); 697 698 /* 699 * Destroy TX status DMA stuffs 700 */ 701 et_dma_mem_destroy(sc, txsd->txsd_status, txsd->txsd_dmap); 702 703 /* 704 * Destroy DMA stuffs for RX rings 705 */ 706 for (i = 0; i < ET_RX_NRING; ++i) { 707 struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[i]; 708 709 et_dma_mem_destroy(sc, rx_ring->rr_desc, rx_ring->rr_dmap); 710 } 711 712 /* 713 * Destroy RX stat ring DMA stuffs 714 */ 715 et_dma_mem_destroy(sc, rxst_ring->rsr_stat, rxst_ring->rsr_dmap); 716 717 /* 718 * Destroy RX status DMA stuffs 719 */ 720 et_dma_mem_destroy(sc, rxsd->rxsd_status, rxsd->rxsd_dmap); 721 722 /* 723 * Destroy mbuf DMA stuffs 724 */ 725 for (i = 0; i < ET_RX_NRING; ++i) 726 rx_done[i] = ET_RX_NDESC; 727 et_dma_mbuf_destroy(sc, ET_TX_NDESC, rx_done); 728 } 729 730 int 731 et_dma_mbuf_create(struct et_softc *sc) 732 { 733 struct et_txbuf_data *tbd = &sc->sc_tx_data; 734 int i, error, rx_done[ET_RX_NRING]; 735 736 /* 737 * Create spare DMA map for RX mbufs 738 */ 739 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0, 740 BUS_DMA_NOWAIT, &sc->sc_mbuf_tmp_dmap); 741 if (error) { 742 printf("%s: can't create spare mbuf DMA map\n", 743 sc->sc_dev.dv_xname); 744 return error; 745 } 746 747 /* 748 * Create DMA maps for RX mbufs 749 */ 750 bzero(rx_done, sizeof(rx_done)); 751 for (i = 0; i < ET_RX_NRING; ++i) { 752 struct et_rxbuf_data *rbd = &sc->sc_rx_data[i]; 753 int j; 754 755 for (j = 0; j < ET_RX_NDESC; ++j) { 756 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, 757 MCLBYTES, 0, BUS_DMA_NOWAIT, 758 &rbd->rbd_buf[j].rb_dmap); 759 if (error) { 760 printf("%s: can't create %d RX mbuf " 761 "for %d RX ring\n", sc->sc_dev.dv_xname, 762 j, i); 763 rx_done[i] = j; 764 et_dma_mbuf_destroy(sc, 0, rx_done); 765 return error; 766 } 767 } 768 rx_done[i] = ET_RX_NDESC; 769 770 rbd->rbd_softc = sc; 771 rbd->rbd_ring = &sc->sc_rx_ring[i]; 772 } 773 774 /* 775 * Create DMA maps for TX mbufs 776 */ 777 for (i = 0; i < ET_TX_NDESC; ++i) { 778 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 779 0, BUS_DMA_NOWAIT, &tbd->tbd_buf[i].tb_dmap); 780 if (error) { 781 printf("%s: can't create %d TX mbuf " 782 "DMA map\n", sc->sc_dev.dv_xname, i); 783 et_dma_mbuf_destroy(sc, i, rx_done); 784 return error; 785 } 786 } 787 788 return 0; 789 } 790 791 void 792 et_dma_mbuf_destroy(struct et_softc *sc, int tx_done, const int rx_done[]) 793 { 794 struct et_txbuf_data *tbd = &sc->sc_tx_data; 795 int i; 796 797 /* 798 * Destroy DMA maps for RX mbufs 799 */ 800 for (i = 0; i < ET_RX_NRING; ++i) { 801 struct et_rxbuf_data *rbd = &sc->sc_rx_data[i]; 802 int j; 803 804 for (j = 0; j < rx_done[i]; ++j) { 805 struct et_rxbuf *rb = &rbd->rbd_buf[j]; 806 807 KASSERT(rb->rb_mbuf == NULL, 808 ("RX mbuf in %d RX ring is not freed yet\n", i)); 809 bus_dmamap_destroy(sc->sc_dmat, rb->rb_dmap); 810 } 811 } 812 813 /* 814 * Destroy DMA maps for TX mbufs 815 */ 816 for (i = 0; i < tx_done; ++i) { 817 struct et_txbuf *tb = &tbd->tbd_buf[i]; 818 819 KASSERT(tb->tb_mbuf == NULL, ("TX mbuf is not freed yet\n")); 820 bus_dmamap_destroy(sc->sc_dmat, tb->tb_dmap); 821 } 822 823 /* 824 * Destroy spare mbuf DMA map 825 */ 826 bus_dmamap_destroy(sc->sc_dmat, sc->sc_mbuf_tmp_dmap); 827 } 828 829 int 830 et_dma_mem_create(struct et_softc *sc, bus_size_t size, 831 void **addr, bus_addr_t *paddr, bus_dmamap_t *dmap, bus_dma_segment_t *seg) 832 { 833 int error, nsegs; 834 835 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, BUS_DMA_NOWAIT, 836 dmap); 837 if (error) { 838 printf("%s: can't create DMA map\n", sc->sc_dev.dv_xname); 839 return error; 840 } 841 842 error = bus_dmamem_alloc(sc->sc_dmat, size, ET_ALIGN, 0, seg, 843 1, &nsegs, BUS_DMA_WAITOK | BUS_DMA_ZERO); 844 if (error) { 845 printf("%s: can't allocate DMA mem\n", sc->sc_dev.dv_xname); 846 return error; 847 } 848 849 error = bus_dmamem_map(sc->sc_dmat, seg, nsegs, 850 size, (caddr_t *)addr, BUS_DMA_NOWAIT); 851 if (error) { 852 printf("%s: can't map DMA mem\n", sc->sc_dev.dv_xname); 853 return (error); 854 } 855 856 error = bus_dmamap_load(sc->sc_dmat, *dmap, *addr, size, NULL, 857 BUS_DMA_WAITOK); 858 if (error) { 859 printf("%s: can't load DMA mem\n", sc->sc_dev.dv_xname); 860 bus_dmamem_free(sc->sc_dmat, (bus_dma_segment_t *)addr, 1); 861 return error; 862 } 863 864 *paddr = (*dmap)->dm_segs[0].ds_addr; 865 866 return 0; 867 } 868 869 void 870 et_dma_mem_destroy(struct et_softc *sc, void *addr, bus_dmamap_t dmap) 871 { 872 bus_dmamap_unload(sc->sc_dmat, dmap); 873 bus_dmamem_free(sc->sc_dmat, (bus_dma_segment_t *)&addr, 1); 874 } 875 876 void 877 et_chip_attach(struct et_softc *sc) 878 { 879 uint32_t val; 880 881 /* 882 * Perform minimal initialization 883 */ 884 885 /* Disable loopback */ 886 CSR_WRITE_4(sc, ET_LOOPBACK, 0); 887 888 /* Reset MAC */ 889 CSR_WRITE_4(sc, ET_MAC_CFG1, 890 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC | 891 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC | 892 ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST); 893 894 /* 895 * Setup half duplex mode 896 */ 897 val = __SHIFTIN(10, ET_MAC_HDX_ALT_BEB_TRUNC) | 898 __SHIFTIN(15, ET_MAC_HDX_REXMIT_MAX) | 899 __SHIFTIN(55, ET_MAC_HDX_COLLWIN) | 900 ET_MAC_HDX_EXC_DEFER; 901 CSR_WRITE_4(sc, ET_MAC_HDX, val); 902 903 /* Clear MAC control */ 904 CSR_WRITE_4(sc, ET_MAC_CTRL, 0); 905 906 /* Reset MII */ 907 CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST); 908 909 /* Bring MAC out of reset state */ 910 CSR_WRITE_4(sc, ET_MAC_CFG1, 0); 911 912 /* Enable memory controllers */ 913 CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE); 914 } 915 916 int 917 et_intr(void *xsc) 918 { 919 struct et_softc *sc = xsc; 920 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 921 uint32_t intrs; 922 923 if ((ifp->if_flags & IFF_RUNNING) == 0) 924 return (0); 925 926 intrs = CSR_READ_4(sc, ET_INTR_STATUS); 927 if (intrs == 0 || intrs == 0xffffffff) 928 return (0); 929 930 et_disable_intrs(sc); 931 intrs &= ET_INTRS; 932 if (intrs == 0) /* Not interested */ 933 goto back; 934 935 if (intrs & ET_INTR_RXEOF) 936 et_rxeof(sc); 937 if (intrs & (ET_INTR_TXEOF | ET_INTR_TIMER)) 938 et_txeof(sc); 939 if (intrs & ET_INTR_TIMER) 940 CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer); 941 back: 942 et_enable_intrs(sc, ET_INTRS); 943 944 return (1); 945 } 946 947 int 948 et_init(struct ifnet *ifp) 949 { 950 struct et_softc *sc = ifp->if_softc; 951 int error, i, s; 952 953 s = splnet(); 954 955 et_stop(sc); 956 957 for (i = 0; i < ET_RX_NRING; ++i) { 958 sc->sc_rx_data[i].rbd_bufsize = et_bufsize[i].bufsize; 959 sc->sc_rx_data[i].rbd_newbuf = et_bufsize[i].newbuf; 960 } 961 962 error = et_init_tx_ring(sc); 963 if (error) 964 goto back; 965 966 error = et_init_rx_ring(sc); 967 if (error) 968 goto back; 969 970 error = et_chip_init(sc); 971 if (error) 972 goto back; 973 974 error = et_enable_txrx(sc); 975 if (error) 976 goto back; 977 978 error = et_start_rxdma(sc); 979 if (error) 980 goto back; 981 982 error = et_start_txdma(sc); 983 if (error) 984 goto back; 985 986 et_enable_intrs(sc, ET_INTRS); 987 988 timeout_add_sec(&sc->sc_tick, 1); 989 990 CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer); 991 992 ifp->if_flags |= IFF_RUNNING; 993 ifq_clr_oactive(&ifp->if_snd); 994 back: 995 if (error) 996 et_stop(sc); 997 998 splx(s); 999 1000 return (0); 1001 } 1002 1003 int 1004 et_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1005 { 1006 struct et_softc *sc = ifp->if_softc; 1007 struct ifreq *ifr = (struct ifreq *)data; 1008 int s, error = 0; 1009 1010 s = splnet(); 1011 1012 switch (cmd) { 1013 case SIOCSIFADDR: 1014 ifp->if_flags |= IFF_UP; 1015 if (!(ifp->if_flags & IFF_RUNNING)) 1016 et_init(ifp); 1017 break; 1018 1019 case SIOCSIFFLAGS: 1020 if (ifp->if_flags & IFF_UP) { 1021 /* 1022 * If only the PROMISC or ALLMULTI flag changes, then 1023 * don't do a full re-init of the chip, just update 1024 * the Rx filter. 1025 */ 1026 if ((ifp->if_flags & IFF_RUNNING) && 1027 ((ifp->if_flags ^ sc->sc_if_flags) & 1028 (IFF_ALLMULTI | IFF_PROMISC)) != 0) { 1029 et_setmulti(sc); 1030 } else { 1031 if (!(ifp->if_flags & IFF_RUNNING)) 1032 et_init(ifp); 1033 } 1034 } else { 1035 if (ifp->if_flags & IFF_RUNNING) 1036 et_stop(sc); 1037 } 1038 sc->sc_if_flags = ifp->if_flags; 1039 break; 1040 1041 case SIOCSIFMEDIA: 1042 case SIOCGIFMEDIA: 1043 error = ifmedia_ioctl(ifp, ifr, &sc->sc_miibus.mii_media, cmd); 1044 break; 1045 1046 default: 1047 error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data); 1048 } 1049 1050 if (error == ENETRESET) { 1051 if (ifp->if_flags & IFF_RUNNING) 1052 et_setmulti(sc); 1053 error = 0; 1054 } 1055 1056 splx(s); 1057 return error; 1058 } 1059 1060 void 1061 et_start(struct ifnet *ifp) 1062 { 1063 struct et_softc *sc = ifp->if_softc; 1064 struct et_txbuf_data *tbd = &sc->sc_tx_data; 1065 int trans; 1066 struct mbuf *m; 1067 1068 if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd)) 1069 return; 1070 1071 trans = 0; 1072 for (;;) { 1073 m = ifq_dequeue(&ifp->if_snd); 1074 if (m == NULL) 1075 break; 1076 1077 if ((tbd->tbd_used + ET_NSEG_SPARE) > ET_TX_NDESC) { 1078 ifq_set_oactive(&ifp->if_snd); 1079 break; 1080 } 1081 1082 if (et_encap(sc, &m)) { 1083 ifp->if_oerrors++; 1084 ifq_set_oactive(&ifp->if_snd); 1085 break; 1086 } 1087 1088 trans = 1; 1089 1090 #if NBPFILTER > 0 1091 if (ifp->if_bpf != NULL) 1092 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 1093 #endif 1094 } 1095 1096 if (trans) { 1097 timeout_add_sec(&sc->sc_txtick, 1); 1098 ifp->if_timer = 5; 1099 } 1100 } 1101 1102 void 1103 et_watchdog(struct ifnet *ifp) 1104 { 1105 struct et_softc *sc = ifp->if_softc; 1106 printf("%s: watchdog timed out\n", sc->sc_dev.dv_xname); 1107 1108 et_init(ifp); 1109 et_start(ifp); 1110 } 1111 1112 int 1113 et_stop_rxdma(struct et_softc *sc) 1114 { 1115 CSR_WRITE_4(sc, ET_RXDMA_CTRL, 1116 ET_RXDMA_CTRL_HALT | ET_RXDMA_CTRL_RING1_ENABLE); 1117 1118 DELAY(5); 1119 if ((CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) == 0) { 1120 printf("%s: can't stop RX DMA engine\n", sc->sc_dev.dv_xname); 1121 return ETIMEDOUT; 1122 } 1123 return 0; 1124 } 1125 1126 int 1127 et_stop_txdma(struct et_softc *sc) 1128 { 1129 CSR_WRITE_4(sc, ET_TXDMA_CTRL, 1130 ET_TXDMA_CTRL_HALT | ET_TXDMA_CTRL_SINGLE_EPKT); 1131 return 0; 1132 } 1133 1134 void 1135 et_free_tx_ring(struct et_softc *sc) 1136 { 1137 struct et_txbuf_data *tbd = &sc->sc_tx_data; 1138 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring; 1139 int i; 1140 1141 for (i = 0; i < ET_TX_NDESC; ++i) { 1142 struct et_txbuf *tb = &tbd->tbd_buf[i]; 1143 1144 if (tb->tb_mbuf != NULL) { 1145 bus_dmamap_unload(sc->sc_dmat, tb->tb_dmap); 1146 m_freem(tb->tb_mbuf); 1147 tb->tb_mbuf = NULL; 1148 } 1149 } 1150 1151 bzero(tx_ring->tr_desc, ET_TX_RING_SIZE); 1152 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0, 1153 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1154 } 1155 1156 void 1157 et_free_rx_ring(struct et_softc *sc) 1158 { 1159 int n; 1160 1161 for (n = 0; n < ET_RX_NRING; ++n) { 1162 struct et_rxbuf_data *rbd = &sc->sc_rx_data[n]; 1163 struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[n]; 1164 int i; 1165 1166 for (i = 0; i < ET_RX_NDESC; ++i) { 1167 struct et_rxbuf *rb = &rbd->rbd_buf[i]; 1168 1169 if (rb->rb_mbuf != NULL) { 1170 bus_dmamap_unload(sc->sc_dmat, rb->rb_dmap); 1171 m_freem(rb->rb_mbuf); 1172 rb->rb_mbuf = NULL; 1173 } 1174 } 1175 1176 bzero(rx_ring->rr_desc, ET_RX_RING_SIZE); 1177 bus_dmamap_sync(sc->sc_dmat, rx_ring->rr_dmap, 0, 1178 rx_ring->rr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1179 } 1180 } 1181 1182 void 1183 et_setmulti(struct et_softc *sc) 1184 { 1185 struct arpcom *ac = &sc->sc_arpcom; 1186 struct ifnet *ifp = &ac->ac_if; 1187 uint32_t hash[4] = { 0, 0, 0, 0 }; 1188 uint32_t rxmac_ctrl, pktfilt; 1189 struct ether_multi *enm; 1190 struct ether_multistep step; 1191 int i, count; 1192 1193 pktfilt = CSR_READ_4(sc, ET_PKTFILT); 1194 rxmac_ctrl = CSR_READ_4(sc, ET_RXMAC_CTRL); 1195 1196 pktfilt &= ~(ET_PKTFILT_BCAST | ET_PKTFILT_MCAST | ET_PKTFILT_UCAST); 1197 if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) { 1198 rxmac_ctrl |= ET_RXMAC_CTRL_NO_PKTFILT; 1199 goto back; 1200 } 1201 1202 count = 0; 1203 ETHER_FIRST_MULTI(step, ac, enm); 1204 while (enm != NULL) { 1205 uint32_t *hp, h; 1206 1207 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN); 1208 h = (h & 0x3f800000) >> 23; 1209 1210 hp = &hash[0]; 1211 if (h >= 32 && h < 64) { 1212 h -= 32; 1213 hp = &hash[1]; 1214 } else if (h >= 64 && h < 96) { 1215 h -= 64; 1216 hp = &hash[2]; 1217 } else if (h >= 96) { 1218 h -= 96; 1219 hp = &hash[3]; 1220 } 1221 *hp |= (1 << h); 1222 1223 ++count; 1224 ETHER_NEXT_MULTI(step, enm); 1225 } 1226 1227 for (i = 0; i < 4; ++i) 1228 CSR_WRITE_4(sc, ET_MULTI_HASH + (i * 4), hash[i]); 1229 1230 if (count > 0) 1231 pktfilt |= ET_PKTFILT_MCAST; 1232 rxmac_ctrl &= ~ET_RXMAC_CTRL_NO_PKTFILT; 1233 back: 1234 CSR_WRITE_4(sc, ET_PKTFILT, pktfilt); 1235 CSR_WRITE_4(sc, ET_RXMAC_CTRL, rxmac_ctrl); 1236 } 1237 1238 int 1239 et_chip_init(struct et_softc *sc) 1240 { 1241 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1242 uint32_t rxq_end; 1243 int error; 1244 1245 /* 1246 * Split internal memory between TX and RX according to MTU 1247 */ 1248 if (ifp->if_hardmtu < 2048) 1249 rxq_end = 0x2bc; 1250 else if (ifp->if_hardmtu < 8192) 1251 rxq_end = 0x1ff; 1252 else 1253 rxq_end = 0x1b3; 1254 CSR_WRITE_4(sc, ET_RXQ_START, 0); 1255 CSR_WRITE_4(sc, ET_RXQ_END, rxq_end); 1256 CSR_WRITE_4(sc, ET_TXQ_START, rxq_end + 1); 1257 CSR_WRITE_4(sc, ET_TXQ_END, ET_INTERN_MEM_END); 1258 1259 /* No loopback */ 1260 CSR_WRITE_4(sc, ET_LOOPBACK, 0); 1261 1262 /* Clear MSI configure */ 1263 CSR_WRITE_4(sc, ET_MSI_CFG, 0); 1264 1265 /* Disable timer */ 1266 CSR_WRITE_4(sc, ET_TIMER, 0); 1267 1268 /* Initialize MAC */ 1269 et_init_mac(sc); 1270 1271 /* Enable memory controllers */ 1272 CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE); 1273 1274 /* Initialize RX MAC */ 1275 et_init_rxmac(sc); 1276 1277 /* Initialize TX MAC */ 1278 et_init_txmac(sc); 1279 1280 /* Initialize RX DMA engine */ 1281 error = et_init_rxdma(sc); 1282 if (error) 1283 return error; 1284 1285 /* Initialize TX DMA engine */ 1286 error = et_init_txdma(sc); 1287 if (error) 1288 return error; 1289 1290 return 0; 1291 } 1292 1293 int 1294 et_init_tx_ring(struct et_softc *sc) 1295 { 1296 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring; 1297 struct et_txstatus_data *txsd = &sc->sc_tx_status; 1298 struct et_txbuf_data *tbd = &sc->sc_tx_data; 1299 1300 bzero(tx_ring->tr_desc, ET_TX_RING_SIZE); 1301 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0, 1302 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1303 1304 tbd->tbd_start_index = 0; 1305 tbd->tbd_start_wrap = 0; 1306 tbd->tbd_used = 0; 1307 1308 bzero(txsd->txsd_status, sizeof(uint32_t)); 1309 bus_dmamap_sync(sc->sc_dmat, txsd->txsd_dmap, 0, 1310 txsd->txsd_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1311 return 0; 1312 } 1313 1314 int 1315 et_init_rx_ring(struct et_softc *sc) 1316 { 1317 struct et_rxstatus_data *rxsd = &sc->sc_rx_status; 1318 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring; 1319 int n; 1320 1321 for (n = 0; n < ET_RX_NRING; ++n) { 1322 struct et_rxbuf_data *rbd = &sc->sc_rx_data[n]; 1323 int i, error; 1324 1325 for (i = 0; i < ET_RX_NDESC; ++i) { 1326 error = rbd->rbd_newbuf(rbd, i, 1); 1327 if (error) { 1328 printf("%s: %d ring %d buf, newbuf failed: " 1329 "%d\n", sc->sc_dev.dv_xname, n, i, error); 1330 return error; 1331 } 1332 } 1333 } 1334 1335 bzero(rxsd->rxsd_status, sizeof(struct et_rxstatus)); 1336 bus_dmamap_sync(sc->sc_dmat, rxsd->rxsd_dmap, 0, 1337 rxsd->rxsd_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1338 1339 bzero(rxst_ring->rsr_stat, ET_RXSTAT_RING_SIZE); 1340 bus_dmamap_sync(sc->sc_dmat, rxst_ring->rsr_dmap, 0, 1341 rxst_ring->rsr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1342 1343 return 0; 1344 } 1345 1346 int 1347 et_init_rxdma(struct et_softc *sc) 1348 { 1349 struct et_rxstatus_data *rxsd = &sc->sc_rx_status; 1350 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring; 1351 struct et_rxdesc_ring *rx_ring; 1352 int error; 1353 1354 error = et_stop_rxdma(sc); 1355 if (error) { 1356 printf("%s: can't init RX DMA engine\n", sc->sc_dev.dv_xname); 1357 return error; 1358 } 1359 1360 /* 1361 * Install RX status 1362 */ 1363 CSR_WRITE_4(sc, ET_RX_STATUS_HI, ET_ADDR_HI(rxsd->rxsd_paddr)); 1364 CSR_WRITE_4(sc, ET_RX_STATUS_LO, ET_ADDR_LO(rxsd->rxsd_paddr)); 1365 1366 /* 1367 * Install RX stat ring 1368 */ 1369 CSR_WRITE_4(sc, ET_RXSTAT_HI, ET_ADDR_HI(rxst_ring->rsr_paddr)); 1370 CSR_WRITE_4(sc, ET_RXSTAT_LO, ET_ADDR_LO(rxst_ring->rsr_paddr)); 1371 CSR_WRITE_4(sc, ET_RXSTAT_CNT, ET_RX_NSTAT - 1); 1372 CSR_WRITE_4(sc, ET_RXSTAT_POS, 0); 1373 CSR_WRITE_4(sc, ET_RXSTAT_MINCNT, ((ET_RX_NSTAT * 15) / 100) - 1); 1374 1375 /* Match ET_RXSTAT_POS */ 1376 rxst_ring->rsr_index = 0; 1377 rxst_ring->rsr_wrap = 0; 1378 1379 /* 1380 * Install the 2nd RX descriptor ring 1381 */ 1382 rx_ring = &sc->sc_rx_ring[1]; 1383 CSR_WRITE_4(sc, ET_RX_RING1_HI, ET_ADDR_HI(rx_ring->rr_paddr)); 1384 CSR_WRITE_4(sc, ET_RX_RING1_LO, ET_ADDR_LO(rx_ring->rr_paddr)); 1385 CSR_WRITE_4(sc, ET_RX_RING1_CNT, ET_RX_NDESC - 1); 1386 CSR_WRITE_4(sc, ET_RX_RING1_POS, ET_RX_RING1_POS_WRAP); 1387 CSR_WRITE_4(sc, ET_RX_RING1_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1); 1388 1389 /* Match ET_RX_RING1_POS */ 1390 rx_ring->rr_index = 0; 1391 rx_ring->rr_wrap = 1; 1392 1393 /* 1394 * Install the 1st RX descriptor ring 1395 */ 1396 rx_ring = &sc->sc_rx_ring[0]; 1397 CSR_WRITE_4(sc, ET_RX_RING0_HI, ET_ADDR_HI(rx_ring->rr_paddr)); 1398 CSR_WRITE_4(sc, ET_RX_RING0_LO, ET_ADDR_LO(rx_ring->rr_paddr)); 1399 CSR_WRITE_4(sc, ET_RX_RING0_CNT, ET_RX_NDESC - 1); 1400 CSR_WRITE_4(sc, ET_RX_RING0_POS, ET_RX_RING0_POS_WRAP); 1401 CSR_WRITE_4(sc, ET_RX_RING0_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1); 1402 1403 /* Match ET_RX_RING0_POS */ 1404 rx_ring->rr_index = 0; 1405 rx_ring->rr_wrap = 1; 1406 1407 /* 1408 * RX intr moderation 1409 */ 1410 CSR_WRITE_4(sc, ET_RX_INTR_NPKTS, sc->sc_rx_intr_npkts); 1411 CSR_WRITE_4(sc, ET_RX_INTR_DELAY, sc->sc_rx_intr_delay); 1412 1413 return 0; 1414 } 1415 1416 int 1417 et_init_txdma(struct et_softc *sc) 1418 { 1419 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring; 1420 struct et_txstatus_data *txsd = &sc->sc_tx_status; 1421 int error; 1422 1423 error = et_stop_txdma(sc); 1424 if (error) { 1425 printf("%s: can't init TX DMA engine\n", sc->sc_dev.dv_xname); 1426 return error; 1427 } 1428 1429 /* 1430 * Install TX descriptor ring 1431 */ 1432 CSR_WRITE_4(sc, ET_TX_RING_HI, ET_ADDR_HI(tx_ring->tr_paddr)); 1433 CSR_WRITE_4(sc, ET_TX_RING_LO, ET_ADDR_LO(tx_ring->tr_paddr)); 1434 CSR_WRITE_4(sc, ET_TX_RING_CNT, ET_TX_NDESC - 1); 1435 1436 /* 1437 * Install TX status 1438 */ 1439 CSR_WRITE_4(sc, ET_TX_STATUS_HI, ET_ADDR_HI(txsd->txsd_paddr)); 1440 CSR_WRITE_4(sc, ET_TX_STATUS_LO, ET_ADDR_LO(txsd->txsd_paddr)); 1441 1442 CSR_WRITE_4(sc, ET_TX_READY_POS, 0); 1443 1444 /* Match ET_TX_READY_POS */ 1445 tx_ring->tr_ready_index = 0; 1446 tx_ring->tr_ready_wrap = 0; 1447 1448 return 0; 1449 } 1450 1451 void 1452 et_init_mac(struct et_softc *sc) 1453 { 1454 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1455 const uint8_t *eaddr = LLADDR(ifp->if_sadl); 1456 uint32_t val; 1457 1458 /* Reset MAC */ 1459 CSR_WRITE_4(sc, ET_MAC_CFG1, 1460 ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC | 1461 ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC | 1462 ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST); 1463 1464 /* 1465 * Setup inter packet gap 1466 */ 1467 val = __SHIFTIN(56, ET_IPG_NONB2B_1) | 1468 __SHIFTIN(88, ET_IPG_NONB2B_2) | 1469 __SHIFTIN(80, ET_IPG_MINIFG) | 1470 __SHIFTIN(96, ET_IPG_B2B); 1471 CSR_WRITE_4(sc, ET_IPG, val); 1472 1473 /* 1474 * Setup half duplex mode 1475 */ 1476 val = __SHIFTIN(10, ET_MAC_HDX_ALT_BEB_TRUNC) | 1477 __SHIFTIN(15, ET_MAC_HDX_REXMIT_MAX) | 1478 __SHIFTIN(55, ET_MAC_HDX_COLLWIN) | 1479 ET_MAC_HDX_EXC_DEFER; 1480 CSR_WRITE_4(sc, ET_MAC_HDX, val); 1481 1482 /* Clear MAC control */ 1483 CSR_WRITE_4(sc, ET_MAC_CTRL, 0); 1484 1485 /* Reset MII */ 1486 CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST); 1487 1488 /* 1489 * Set MAC address 1490 */ 1491 val = eaddr[2] | (eaddr[3] << 8) | (eaddr[4] << 16) | (eaddr[5] << 24); 1492 CSR_WRITE_4(sc, ET_MAC_ADDR1, val); 1493 val = (eaddr[0] << 16) | (eaddr[1] << 24); 1494 CSR_WRITE_4(sc, ET_MAC_ADDR2, val); 1495 1496 /* Set max frame length */ 1497 CSR_WRITE_4(sc, ET_MAX_FRMLEN, ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN); 1498 1499 /* Bring MAC out of reset state */ 1500 CSR_WRITE_4(sc, ET_MAC_CFG1, 0); 1501 } 1502 1503 void 1504 et_init_rxmac(struct et_softc *sc) 1505 { 1506 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1507 const uint8_t *eaddr = LLADDR(ifp->if_sadl); 1508 uint32_t val; 1509 int i; 1510 1511 /* Disable RX MAC and WOL */ 1512 CSR_WRITE_4(sc, ET_RXMAC_CTRL, ET_RXMAC_CTRL_WOL_DISABLE); 1513 1514 /* 1515 * Clear all WOL related registers 1516 */ 1517 for (i = 0; i < 3; ++i) 1518 CSR_WRITE_4(sc, ET_WOL_CRC + (i * 4), 0); 1519 for (i = 0; i < 20; ++i) 1520 CSR_WRITE_4(sc, ET_WOL_MASK + (i * 4), 0); 1521 1522 /* 1523 * Set WOL source address. XXX is this necessary? 1524 */ 1525 val = (eaddr[2] << 24) | (eaddr[3] << 16) | (eaddr[4] << 8) | eaddr[5]; 1526 CSR_WRITE_4(sc, ET_WOL_SA_LO, val); 1527 val = (eaddr[0] << 8) | eaddr[1]; 1528 CSR_WRITE_4(sc, ET_WOL_SA_HI, val); 1529 1530 /* Clear packet filters */ 1531 CSR_WRITE_4(sc, ET_PKTFILT, 0); 1532 1533 /* No ucast filtering */ 1534 CSR_WRITE_4(sc, ET_UCAST_FILTADDR1, 0); 1535 CSR_WRITE_4(sc, ET_UCAST_FILTADDR2, 0); 1536 CSR_WRITE_4(sc, ET_UCAST_FILTADDR3, 0); 1537 1538 if (ifp->if_hardmtu > 8192) { 1539 /* 1540 * In order to transmit jumbo packets greater than 8k, 1541 * the FIFO between RX MAC and RX DMA needs to be reduced 1542 * in size to (16k - MTU). In order to implement this, we 1543 * must use "cut through" mode in the RX MAC, which chops 1544 * packets down into segments which are (max_size * 16). 1545 * In this case we selected 256 bytes, since this is the 1546 * size of the PCI-Express TLP's that the 1310 uses. 1547 */ 1548 val = __SHIFTIN(16, ET_RXMAC_MC_SEGSZ_MAX) | 1549 ET_RXMAC_MC_SEGSZ_ENABLE; 1550 } else { 1551 val = 0; 1552 } 1553 CSR_WRITE_4(sc, ET_RXMAC_MC_SEGSZ, val); 1554 1555 CSR_WRITE_4(sc, ET_RXMAC_MC_WATERMARK, 0); 1556 1557 /* Initialize RX MAC management register */ 1558 CSR_WRITE_4(sc, ET_RXMAC_MGT, 0); 1559 1560 CSR_WRITE_4(sc, ET_RXMAC_SPACE_AVL, 0); 1561 1562 CSR_WRITE_4(sc, ET_RXMAC_MGT, 1563 ET_RXMAC_MGT_PASS_ECRC | 1564 ET_RXMAC_MGT_PASS_ELEN | 1565 ET_RXMAC_MGT_PASS_ETRUNC | 1566 ET_RXMAC_MGT_CHECK_PKT); 1567 1568 /* 1569 * Configure runt filtering (may not work on certain chip generation) 1570 */ 1571 val = __SHIFTIN(ETHER_MIN_LEN, ET_PKTFILT_MINLEN) | ET_PKTFILT_FRAG; 1572 CSR_WRITE_4(sc, ET_PKTFILT, val); 1573 1574 /* Enable RX MAC but leave WOL disabled */ 1575 CSR_WRITE_4(sc, ET_RXMAC_CTRL, 1576 ET_RXMAC_CTRL_WOL_DISABLE | ET_RXMAC_CTRL_ENABLE); 1577 1578 /* 1579 * Setup multicast hash and allmulti/promisc mode 1580 */ 1581 et_setmulti(sc); 1582 } 1583 1584 void 1585 et_init_txmac(struct et_softc *sc) 1586 { 1587 /* Disable TX MAC and FC(?) */ 1588 CSR_WRITE_4(sc, ET_TXMAC_CTRL, ET_TXMAC_CTRL_FC_DISABLE); 1589 1590 /* No flow control yet */ 1591 CSR_WRITE_4(sc, ET_TXMAC_FLOWCTRL, 0); 1592 1593 /* Enable TX MAC but leave FC(?) disabled */ 1594 CSR_WRITE_4(sc, ET_TXMAC_CTRL, 1595 ET_TXMAC_CTRL_ENABLE | ET_TXMAC_CTRL_FC_DISABLE); 1596 } 1597 1598 int 1599 et_start_rxdma(struct et_softc *sc) 1600 { 1601 uint32_t val = 0; 1602 1603 val |= __SHIFTIN(sc->sc_rx_data[0].rbd_bufsize, 1604 ET_RXDMA_CTRL_RING0_SIZE) | 1605 ET_RXDMA_CTRL_RING0_ENABLE; 1606 val |= __SHIFTIN(sc->sc_rx_data[1].rbd_bufsize, 1607 ET_RXDMA_CTRL_RING1_SIZE) | 1608 ET_RXDMA_CTRL_RING1_ENABLE; 1609 1610 CSR_WRITE_4(sc, ET_RXDMA_CTRL, val); 1611 1612 DELAY(5); 1613 1614 if (CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) { 1615 printf("%s: can't start RX DMA engine\n", sc->sc_dev.dv_xname); 1616 return ETIMEDOUT; 1617 } 1618 return 0; 1619 } 1620 1621 int 1622 et_start_txdma(struct et_softc *sc) 1623 { 1624 CSR_WRITE_4(sc, ET_TXDMA_CTRL, ET_TXDMA_CTRL_SINGLE_EPKT); 1625 return 0; 1626 } 1627 1628 int 1629 et_enable_txrx(struct et_softc *sc) 1630 { 1631 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1632 uint32_t val; 1633 int i; 1634 1635 val = CSR_READ_4(sc, ET_MAC_CFG1); 1636 val |= ET_MAC_CFG1_TXEN | ET_MAC_CFG1_RXEN; 1637 val &= ~(ET_MAC_CFG1_TXFLOW | ET_MAC_CFG1_RXFLOW | 1638 ET_MAC_CFG1_LOOPBACK); 1639 CSR_WRITE_4(sc, ET_MAC_CFG1, val); 1640 1641 et_ifmedia_upd(ifp); 1642 1643 #define NRETRY 100 1644 1645 for (i = 0; i < NRETRY; ++i) { 1646 val = CSR_READ_4(sc, ET_MAC_CFG1); 1647 if ((val & (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN)) == 1648 (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN)) 1649 break; 1650 1651 DELAY(10); 1652 } 1653 if (i == NRETRY) { 1654 printf("%s: can't enable RX/TX\n", sc->sc_dev.dv_xname); 1655 return ETIMEDOUT; 1656 } 1657 1658 #undef NRETRY 1659 return 0; 1660 } 1661 1662 void 1663 et_rxeof(struct et_softc *sc) 1664 { 1665 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1666 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 1667 struct et_rxstatus_data *rxsd = &sc->sc_rx_status; 1668 struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring; 1669 uint32_t rxs_stat_ring; 1670 int rxst_wrap, rxst_index; 1671 1672 bus_dmamap_sync(sc->sc_dmat, rxsd->rxsd_dmap, 0, 1673 rxsd->rxsd_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1674 bus_dmamap_sync(sc->sc_dmat, rxst_ring->rsr_dmap, 0, 1675 rxst_ring->rsr_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1676 1677 rxs_stat_ring = rxsd->rxsd_status->rxs_stat_ring; 1678 rxst_wrap = (rxs_stat_ring & ET_RXS_STATRING_WRAP) ? 1 : 0; 1679 rxst_index = __SHIFTOUT(rxs_stat_ring, ET_RXS_STATRING_INDEX); 1680 1681 while (rxst_index != rxst_ring->rsr_index || 1682 rxst_wrap != rxst_ring->rsr_wrap) { 1683 struct et_rxbuf_data *rbd; 1684 struct et_rxdesc_ring *rx_ring; 1685 struct et_rxstat *st; 1686 struct et_rxbuf *rb; 1687 struct mbuf *m; 1688 int buflen, buf_idx, ring_idx; 1689 uint32_t rxstat_pos, rxring_pos; 1690 1691 KKASSERT(rxst_ring->rsr_index < ET_RX_NSTAT); 1692 st = &rxst_ring->rsr_stat[rxst_ring->rsr_index]; 1693 1694 buflen = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_LEN); 1695 buf_idx = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_BUFIDX); 1696 ring_idx = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_RINGIDX); 1697 1698 if (++rxst_ring->rsr_index == ET_RX_NSTAT) { 1699 rxst_ring->rsr_index = 0; 1700 rxst_ring->rsr_wrap ^= 1; 1701 } 1702 rxstat_pos = __SHIFTIN(rxst_ring->rsr_index, 1703 ET_RXSTAT_POS_INDEX); 1704 if (rxst_ring->rsr_wrap) 1705 rxstat_pos |= ET_RXSTAT_POS_WRAP; 1706 CSR_WRITE_4(sc, ET_RXSTAT_POS, rxstat_pos); 1707 1708 if (ring_idx >= ET_RX_NRING) { 1709 ifp->if_ierrors++; 1710 printf("%s: invalid ring index %d\n", 1711 sc->sc_dev.dv_xname, ring_idx); 1712 continue; 1713 } 1714 if (buf_idx >= ET_RX_NDESC) { 1715 ifp->if_ierrors++; 1716 printf("%s: invalid buf index %d\n", 1717 sc->sc_dev.dv_xname, buf_idx); 1718 continue; 1719 } 1720 1721 rbd = &sc->sc_rx_data[ring_idx]; 1722 rb = &rbd->rbd_buf[buf_idx]; 1723 m = rb->rb_mbuf; 1724 bus_dmamap_sync(sc->sc_dmat, rb->rb_dmap, 0, 1725 rb->rb_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1726 1727 if (rbd->rbd_newbuf(rbd, buf_idx, 0) == 0) { 1728 if (buflen < ETHER_CRC_LEN) { 1729 m_freem(m); 1730 ifp->if_ierrors++; 1731 } else { 1732 m->m_pkthdr.len = m->m_len = buflen - 1733 ETHER_CRC_LEN; 1734 ml_enqueue(&ml, m); 1735 } 1736 } else { 1737 ifp->if_ierrors++; 1738 } 1739 1740 rx_ring = &sc->sc_rx_ring[ring_idx]; 1741 1742 if (buf_idx != rx_ring->rr_index) { 1743 printf("%s: WARNING!! ring %d, " 1744 "buf_idx %d, rr_idx %d\n", sc->sc_dev.dv_xname, 1745 ring_idx, buf_idx, rx_ring->rr_index); 1746 } 1747 1748 KKASSERT(rx_ring->rr_index < ET_RX_NDESC); 1749 if (++rx_ring->rr_index == ET_RX_NDESC) { 1750 rx_ring->rr_index = 0; 1751 rx_ring->rr_wrap ^= 1; 1752 } 1753 rxring_pos = __SHIFTIN(rx_ring->rr_index, ET_RX_RING_POS_INDEX); 1754 if (rx_ring->rr_wrap) 1755 rxring_pos |= ET_RX_RING_POS_WRAP; 1756 CSR_WRITE_4(sc, rx_ring->rr_posreg, rxring_pos); 1757 } 1758 1759 if_input(ifp, &ml); 1760 } 1761 1762 int 1763 et_encap(struct et_softc *sc, struct mbuf **m0) 1764 { 1765 struct mbuf *m = *m0; 1766 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring; 1767 struct et_txbuf_data *tbd = &sc->sc_tx_data; 1768 struct et_txdesc *td; 1769 bus_dmamap_t map; 1770 int error, maxsegs, first_idx, last_idx, i; 1771 uint32_t tx_ready_pos, last_td_ctrl2; 1772 1773 maxsegs = ET_TX_NDESC - tbd->tbd_used; 1774 if (maxsegs > ET_NSEG_MAX) 1775 maxsegs = ET_NSEG_MAX; 1776 KASSERT(maxsegs >= ET_NSEG_SPARE, 1777 ("not enough spare TX desc (%d)\n", maxsegs)); 1778 1779 KKASSERT(tx_ring->tr_ready_index < ET_TX_NDESC); 1780 first_idx = tx_ring->tr_ready_index; 1781 map = tbd->tbd_buf[first_idx].tb_dmap; 1782 1783 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m, 1784 BUS_DMA_NOWAIT); 1785 if (!error && map->dm_nsegs == 0) { 1786 bus_dmamap_unload(sc->sc_dmat, map); 1787 error = EFBIG; 1788 } 1789 if (error && error != EFBIG) { 1790 printf("%s: can't load TX mbuf", sc->sc_dev.dv_xname); 1791 goto back; 1792 } 1793 if (error) { /* error == EFBIG */ 1794 if (m_defrag(m, M_DONTWAIT)) { 1795 printf("%s: can't defrag TX mbuf\n", 1796 sc->sc_dev.dv_xname); 1797 error = ENOBUFS; 1798 goto back; 1799 } 1800 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m, 1801 BUS_DMA_NOWAIT); 1802 if (error || map->dm_nsegs == 0) { 1803 if (map->dm_nsegs == 0) { 1804 bus_dmamap_unload(sc->sc_dmat, map); 1805 error = EFBIG; 1806 } 1807 printf("%s: can't load defraged TX mbuf\n", 1808 sc->sc_dev.dv_xname); 1809 goto back; 1810 } 1811 } 1812 1813 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize, 1814 BUS_DMASYNC_PREWRITE); 1815 1816 last_td_ctrl2 = ET_TDCTRL2_LAST_FRAG; 1817 sc->sc_tx += map->dm_nsegs; 1818 if (sc->sc_tx / sc->sc_tx_intr_nsegs != sc->sc_tx_intr) { 1819 sc->sc_tx_intr = sc->sc_tx / sc->sc_tx_intr_nsegs; 1820 last_td_ctrl2 |= ET_TDCTRL2_INTR; 1821 } 1822 1823 last_idx = -1; 1824 for (i = 0; i < map->dm_nsegs; ++i) { 1825 int idx; 1826 1827 idx = (first_idx + i) % ET_TX_NDESC; 1828 td = &tx_ring->tr_desc[idx]; 1829 td->td_addr_hi = ET_ADDR_HI(map->dm_segs[i].ds_addr); 1830 td->td_addr_lo = ET_ADDR_LO(map->dm_segs[i].ds_addr); 1831 td->td_ctrl1 = 1832 __SHIFTIN(map->dm_segs[i].ds_len, ET_TDCTRL1_LEN); 1833 1834 if (i == map->dm_nsegs - 1) { /* Last frag */ 1835 td->td_ctrl2 = last_td_ctrl2; 1836 last_idx = idx; 1837 } 1838 1839 KKASSERT(tx_ring->tr_ready_index < ET_TX_NDESC); 1840 if (++tx_ring->tr_ready_index == ET_TX_NDESC) { 1841 tx_ring->tr_ready_index = 0; 1842 tx_ring->tr_ready_wrap ^= 1; 1843 } 1844 } 1845 td = &tx_ring->tr_desc[first_idx]; 1846 td->td_ctrl2 |= ET_TDCTRL2_FIRST_FRAG; /* First frag */ 1847 1848 KKASSERT(last_idx >= 0); 1849 tbd->tbd_buf[first_idx].tb_dmap = tbd->tbd_buf[last_idx].tb_dmap; 1850 tbd->tbd_buf[last_idx].tb_dmap = map; 1851 tbd->tbd_buf[last_idx].tb_mbuf = m; 1852 1853 tbd->tbd_used += map->dm_nsegs; 1854 KKASSERT(tbd->tbd_used <= ET_TX_NDESC); 1855 1856 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0, 1857 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1858 1859 1860 tx_ready_pos = __SHIFTIN(tx_ring->tr_ready_index, 1861 ET_TX_READY_POS_INDEX); 1862 if (tx_ring->tr_ready_wrap) 1863 tx_ready_pos |= ET_TX_READY_POS_WRAP; 1864 CSR_WRITE_4(sc, ET_TX_READY_POS, tx_ready_pos); 1865 1866 error = 0; 1867 back: 1868 if (error) { 1869 m_freem(m); 1870 *m0 = NULL; 1871 } 1872 return error; 1873 } 1874 1875 void 1876 et_txeof(struct et_softc *sc) 1877 { 1878 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1879 struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring; 1880 struct et_txbuf_data *tbd = &sc->sc_tx_data; 1881 uint32_t tx_done; 1882 int end, wrap; 1883 1884 if (tbd->tbd_used == 0) 1885 return; 1886 1887 tx_done = CSR_READ_4(sc, ET_TX_DONE_POS); 1888 end = __SHIFTOUT(tx_done, ET_TX_DONE_POS_INDEX); 1889 wrap = (tx_done & ET_TX_DONE_POS_WRAP) ? 1 : 0; 1890 1891 while (tbd->tbd_start_index != end || tbd->tbd_start_wrap != wrap) { 1892 struct et_txbuf *tb; 1893 1894 KKASSERT(tbd->tbd_start_index < ET_TX_NDESC); 1895 tb = &tbd->tbd_buf[tbd->tbd_start_index]; 1896 1897 bzero(&tx_ring->tr_desc[tbd->tbd_start_index], 1898 sizeof(struct et_txdesc)); 1899 bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0, 1900 tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 1901 1902 if (tb->tb_mbuf != NULL) { 1903 bus_dmamap_unload(sc->sc_dmat, tb->tb_dmap); 1904 m_freem(tb->tb_mbuf); 1905 tb->tb_mbuf = NULL; 1906 } 1907 1908 if (++tbd->tbd_start_index == ET_TX_NDESC) { 1909 tbd->tbd_start_index = 0; 1910 tbd->tbd_start_wrap ^= 1; 1911 } 1912 1913 KKASSERT(tbd->tbd_used > 0); 1914 tbd->tbd_used--; 1915 } 1916 1917 if (tbd->tbd_used == 0) { 1918 timeout_del(&sc->sc_txtick); 1919 ifp->if_timer = 0; 1920 } 1921 if (tbd->tbd_used + ET_NSEG_SPARE <= ET_TX_NDESC) 1922 ifq_clr_oactive(&ifp->if_snd); 1923 1924 et_start(ifp); 1925 } 1926 1927 void 1928 et_txtick(void *xsc) 1929 { 1930 struct et_softc *sc = xsc; 1931 int s; 1932 1933 s = splnet(); 1934 et_txeof(sc); 1935 splx(s); 1936 } 1937 1938 void 1939 et_tick(void *xsc) 1940 { 1941 struct et_softc *sc = xsc; 1942 int s; 1943 1944 s = splnet(); 1945 mii_tick(&sc->sc_miibus); 1946 timeout_add_sec(&sc->sc_tick, 1); 1947 splx(s); 1948 } 1949 1950 int 1951 et_newbuf_cluster(struct et_rxbuf_data *rbd, int buf_idx, int init) 1952 { 1953 return et_newbuf(rbd, buf_idx, init, MCLBYTES); 1954 } 1955 1956 int 1957 et_newbuf_hdr(struct et_rxbuf_data *rbd, int buf_idx, int init) 1958 { 1959 return et_newbuf(rbd, buf_idx, init, MHLEN); 1960 } 1961 1962 int 1963 et_newbuf(struct et_rxbuf_data *rbd, int buf_idx, int init, int len0) 1964 { 1965 struct et_softc *sc = rbd->rbd_softc; 1966 struct et_rxdesc_ring *rx_ring; 1967 struct et_rxdesc *desc; 1968 struct et_rxbuf *rb; 1969 struct mbuf *m; 1970 bus_dmamap_t dmap; 1971 int error, len; 1972 1973 KKASSERT(buf_idx < ET_RX_NDESC); 1974 rb = &rbd->rbd_buf[buf_idx]; 1975 1976 if (len0 >= MINCLSIZE) { 1977 MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA); 1978 if (m == NULL) 1979 return (ENOBUFS); 1980 MCLGET(m, init ? M_WAITOK : M_DONTWAIT); 1981 if ((m->m_flags & M_EXT) == 0) { 1982 m_freem(m); 1983 return (ENOBUFS); 1984 } 1985 len = MCLBYTES; 1986 } else { 1987 MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA); 1988 len = MHLEN; 1989 } 1990 1991 if (m == NULL) { 1992 error = ENOBUFS; 1993 1994 /* XXX for debug */ 1995 printf("%s: M_CLGET failed, size %d\n", sc->sc_dev.dv_xname, 1996 len0); 1997 if (init) { 1998 return error; 1999 } else { 2000 goto back; 2001 } 2002 } 2003 m->m_len = m->m_pkthdr.len = len; 2004 2005 /* 2006 * Try load RX mbuf into temporary DMA tag 2007 */ 2008 error = bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_mbuf_tmp_dmap, m, 2009 init ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT); 2010 if (error) { 2011 if (!error) { 2012 bus_dmamap_unload(sc->sc_dmat, sc->sc_mbuf_tmp_dmap); 2013 error = EFBIG; 2014 printf("%s: too many segments?!\n", 2015 sc->sc_dev.dv_xname); 2016 } 2017 m_freem(m); 2018 2019 /* XXX for debug */ 2020 printf("%s: can't load RX mbuf\n", sc->sc_dev.dv_xname); 2021 if (init) { 2022 return error; 2023 } else { 2024 goto back; 2025 } 2026 } 2027 2028 if (!init) 2029 bus_dmamap_unload(sc->sc_dmat, rb->rb_dmap); 2030 rb->rb_mbuf = m; 2031 2032 /* 2033 * Swap RX buf's DMA map with the loaded temporary one 2034 */ 2035 dmap = rb->rb_dmap; 2036 rb->rb_dmap = sc->sc_mbuf_tmp_dmap; 2037 rb->rb_paddr = rb->rb_dmap->dm_segs[0].ds_addr; 2038 sc->sc_mbuf_tmp_dmap = dmap; 2039 2040 error = 0; 2041 back: 2042 rx_ring = rbd->rbd_ring; 2043 desc = &rx_ring->rr_desc[buf_idx]; 2044 2045 desc->rd_addr_hi = ET_ADDR_HI(rb->rb_paddr); 2046 desc->rd_addr_lo = ET_ADDR_LO(rb->rb_paddr); 2047 desc->rd_ctrl = __SHIFTIN(buf_idx, ET_RDCTRL_BUFIDX); 2048 2049 bus_dmamap_sync(sc->sc_dmat, rx_ring->rr_dmap, 0, 2050 rx_ring->rr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE); 2051 return error; 2052 } 2053