1 /* $OpenBSD: if_ste.c,v 1.13 2001/08/12 20:03:49 mickey Exp $ */ 2 /* 3 * Copyright (c) 1997, 1998, 1999 4 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Bill Paul. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * $FreeBSD: src/sys/pci/if_ste.c,v 1.14 1999/12/07 20:14:42 wpaul Exp $ 34 */ 35 36 #include "bpfilter.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/mbuf.h> 41 #include <sys/protosw.h> 42 #include <sys/socket.h> 43 #include <sys/ioctl.h> 44 #include <sys/errno.h> 45 #include <sys/malloc.h> 46 #include <sys/kernel.h> 47 #include <sys/timeout.h> 48 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/if_types.h> 52 53 #ifdef INET 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/in_var.h> 57 #include <netinet/ip.h> 58 #include <netinet/if_ether.h> 59 #endif 60 61 #include <net/if_media.h> 62 63 #if NBPFILTER > 0 64 #include <net/bpf.h> 65 #endif 66 67 #include <vm/vm.h> /* for vtophys */ 68 69 #include <sys/device.h> 70 71 #include <dev/mii/mii.h> 72 #include <dev/mii/miivar.h> 73 74 #include <dev/pci/pcireg.h> 75 #include <dev/pci/pcivar.h> 76 #include <dev/pci/pcidevs.h> 77 78 #define STE_USEIOSPACE 79 80 #include <dev/pci/if_stereg.h> 81 82 int ste_probe __P((struct device *, void *, void *)); 83 void ste_attach __P((struct device *, struct device *, void *)); 84 int ste_intr __P((void *)); 85 void ste_shutdown __P((void *)); 86 void ste_init __P((void *)); 87 void ste_rxeof __P((struct ste_softc *)); 88 void ste_txeoc __P((struct ste_softc *)); 89 void ste_txeof __P((struct ste_softc *)); 90 void ste_stats_update __P((void *)); 91 void ste_stop __P((struct ste_softc *)); 92 void ste_reset __P((struct ste_softc *)); 93 int ste_ioctl __P((struct ifnet *, u_long, caddr_t)); 94 int ste_encap __P((struct ste_softc *, struct ste_chain *, 95 struct mbuf *)); 96 void ste_start __P((struct ifnet *)); 97 void ste_watchdog __P((struct ifnet *)); 98 int ste_newbuf __P((struct ste_softc *, 99 struct ste_chain_onefrag *, 100 struct mbuf *)); 101 int ste_ifmedia_upd __P((struct ifnet *)); 102 void ste_ifmedia_sts __P((struct ifnet *, struct ifmediareq *)); 103 104 void ste_mii_sync __P((struct ste_softc *)); 105 void ste_mii_send __P((struct ste_softc *, u_int32_t, int)); 106 int ste_mii_readreg __P((struct ste_softc *, 107 struct ste_mii_frame *)); 108 int ste_mii_writereg __P((struct ste_softc *, 109 struct ste_mii_frame *)); 110 int ste_miibus_readreg __P((struct device *, int, int)); 111 void ste_miibus_writereg __P((struct device *, int, int, int)); 112 void ste_miibus_statchg __P((struct device *)); 113 114 int ste_eeprom_wait __P((struct ste_softc *)); 115 int ste_read_eeprom __P((struct ste_softc *, caddr_t, int, 116 int, int)); 117 void ste_wait __P((struct ste_softc *)); 118 u_int8_t ste_calchash __P((caddr_t)); 119 void ste_setmulti __P((struct ste_softc *)); 120 int ste_init_rx_list __P((struct ste_softc *)); 121 void ste_init_tx_list __P((struct ste_softc *)); 122 123 #define STE_SETBIT4(sc, reg, x) \ 124 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x) 125 126 #define STE_CLRBIT4(sc, reg, x) \ 127 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x) 128 129 #define STE_SETBIT2(sc, reg, x) \ 130 CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) | x) 131 132 #define STE_CLRBIT2(sc, reg, x) \ 133 CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) & ~x) 134 135 #define STE_SETBIT1(sc, reg, x) \ 136 CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) | x) 137 138 #define STE_CLRBIT1(sc, reg, x) \ 139 CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) & ~x) 140 141 142 #define MII_SET(x) STE_SETBIT1(sc, STE_PHYCTL, x) 143 #define MII_CLR(x) STE_CLRBIT1(sc, STE_PHYCTL, x) 144 145 /* 146 * Sync the PHYs by setting data bit and strobing the clock 32 times. 147 */ 148 void ste_mii_sync(sc) 149 struct ste_softc *sc; 150 { 151 register int i; 152 153 MII_SET(STE_PHYCTL_MDIR|STE_PHYCTL_MDATA); 154 155 for (i = 0; i < 32; i++) { 156 MII_SET(STE_PHYCTL_MCLK); 157 DELAY(1); 158 MII_CLR(STE_PHYCTL_MCLK); 159 DELAY(1); 160 } 161 162 return; 163 } 164 165 /* 166 * Clock a series of bits through the MII. 167 */ 168 void ste_mii_send(sc, bits, cnt) 169 struct ste_softc *sc; 170 u_int32_t bits; 171 int cnt; 172 { 173 int i; 174 175 MII_CLR(STE_PHYCTL_MCLK); 176 177 for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 178 if (bits & i) { 179 MII_SET(STE_PHYCTL_MDATA); 180 } else { 181 MII_CLR(STE_PHYCTL_MDATA); 182 } 183 DELAY(1); 184 MII_CLR(STE_PHYCTL_MCLK); 185 DELAY(1); 186 MII_SET(STE_PHYCTL_MCLK); 187 } 188 } 189 190 /* 191 * Read an PHY register through the MII. 192 */ 193 int ste_mii_readreg(sc, frame) 194 struct ste_softc *sc; 195 struct ste_mii_frame *frame; 196 197 { 198 int i, ack, s; 199 200 s = splimp(); 201 202 /* 203 * Set up frame for RX. 204 */ 205 frame->mii_stdelim = STE_MII_STARTDELIM; 206 frame->mii_opcode = STE_MII_READOP; 207 frame->mii_turnaround = 0; 208 frame->mii_data = 0; 209 210 CSR_WRITE_2(sc, STE_PHYCTL, 0); 211 /* 212 * Turn on data xmit. 213 */ 214 MII_SET(STE_PHYCTL_MDIR); 215 216 ste_mii_sync(sc); 217 218 /* 219 * Send command/address info. 220 */ 221 ste_mii_send(sc, frame->mii_stdelim, 2); 222 ste_mii_send(sc, frame->mii_opcode, 2); 223 ste_mii_send(sc, frame->mii_phyaddr, 5); 224 ste_mii_send(sc, frame->mii_regaddr, 5); 225 226 /* Turn off xmit. */ 227 MII_CLR(STE_PHYCTL_MDIR); 228 229 /* Idle bit */ 230 MII_CLR((STE_PHYCTL_MCLK|STE_PHYCTL_MDATA)); 231 DELAY(1); 232 MII_SET(STE_PHYCTL_MCLK); 233 DELAY(1); 234 235 /* Check for ack */ 236 MII_CLR(STE_PHYCTL_MCLK); 237 DELAY(1); 238 MII_SET(STE_PHYCTL_MCLK); 239 DELAY(1); 240 ack = CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA; 241 242 /* 243 * Now try reading data bits. If the ack failed, we still 244 * need to clock through 16 cycles to keep the PHY(s) in sync. 245 */ 246 if (ack) { 247 for(i = 0; i < 16; i++) { 248 MII_CLR(STE_PHYCTL_MCLK); 249 DELAY(1); 250 MII_SET(STE_PHYCTL_MCLK); 251 DELAY(1); 252 } 253 goto fail; 254 } 255 256 for (i = 0x8000; i; i >>= 1) { 257 MII_CLR(STE_PHYCTL_MCLK); 258 DELAY(1); 259 if (!ack) { 260 if (CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA) 261 frame->mii_data |= i; 262 DELAY(1); 263 } 264 MII_SET(STE_PHYCTL_MCLK); 265 DELAY(1); 266 } 267 268 fail: 269 270 MII_CLR(STE_PHYCTL_MCLK); 271 DELAY(1); 272 MII_SET(STE_PHYCTL_MCLK); 273 DELAY(1); 274 275 splx(s); 276 277 if (ack) 278 return(1); 279 return(0); 280 } 281 282 /* 283 * Write to a PHY register through the MII. 284 */ 285 int ste_mii_writereg(sc, frame) 286 struct ste_softc *sc; 287 struct ste_mii_frame *frame; 288 289 { 290 int s; 291 292 s = splimp(); 293 /* 294 * Set up frame for TX. 295 */ 296 297 frame->mii_stdelim = STE_MII_STARTDELIM; 298 frame->mii_opcode = STE_MII_WRITEOP; 299 frame->mii_turnaround = STE_MII_TURNAROUND; 300 301 /* 302 * Turn on data output. 303 */ 304 MII_SET(STE_PHYCTL_MDIR); 305 306 ste_mii_sync(sc); 307 308 ste_mii_send(sc, frame->mii_stdelim, 2); 309 ste_mii_send(sc, frame->mii_opcode, 2); 310 ste_mii_send(sc, frame->mii_phyaddr, 5); 311 ste_mii_send(sc, frame->mii_regaddr, 5); 312 ste_mii_send(sc, frame->mii_turnaround, 2); 313 ste_mii_send(sc, frame->mii_data, 16); 314 315 /* Idle bit. */ 316 MII_SET(STE_PHYCTL_MCLK); 317 DELAY(1); 318 MII_CLR(STE_PHYCTL_MCLK); 319 DELAY(1); 320 321 /* 322 * Turn off xmit. 323 */ 324 MII_CLR(STE_PHYCTL_MDIR); 325 326 splx(s); 327 328 return(0); 329 } 330 331 int ste_miibus_readreg(self, phy, reg) 332 struct device *self; 333 int phy, reg; 334 { 335 struct ste_softc *sc = (struct ste_softc *)self; 336 struct ste_mii_frame frame; 337 338 bzero((char *)&frame, sizeof(frame)); 339 340 frame.mii_phyaddr = phy; 341 frame.mii_regaddr = reg; 342 ste_mii_readreg(sc, &frame); 343 344 return(frame.mii_data); 345 } 346 347 void ste_miibus_writereg(self, phy, reg, data) 348 struct device *self; 349 int phy, reg, data; 350 { 351 struct ste_softc *sc = (struct ste_softc *)self; 352 struct ste_mii_frame frame; 353 354 bzero((char *)&frame, sizeof(frame)); 355 356 frame.mii_phyaddr = phy; 357 frame.mii_regaddr = reg; 358 frame.mii_data = data; 359 360 ste_mii_writereg(sc, &frame); 361 362 return; 363 } 364 365 void ste_miibus_statchg(self) 366 struct device *self; 367 { 368 struct ste_softc *sc = (struct ste_softc *)self; 369 struct mii_data *mii; 370 371 mii = &sc->sc_mii; 372 373 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 374 STE_SETBIT2(sc, STE_MACCTL0, STE_MACCTL0_FULLDUPLEX); 375 } else { 376 STE_CLRBIT2(sc, STE_MACCTL0, STE_MACCTL0_FULLDUPLEX); 377 } 378 379 return; 380 } 381 382 int ste_ifmedia_upd(ifp) 383 struct ifnet *ifp; 384 { 385 struct ste_softc *sc; 386 struct mii_data *mii; 387 388 sc = ifp->if_softc; 389 mii = &sc->sc_mii; 390 sc->ste_link = 0; 391 if (mii->mii_instance) { 392 struct mii_softc *miisc; 393 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 394 miisc = LIST_NEXT(miisc, mii_list)) 395 mii_phy_reset(miisc); 396 } 397 mii_mediachg(mii); 398 399 return(0); 400 } 401 402 void ste_ifmedia_sts(ifp, ifmr) 403 struct ifnet *ifp; 404 struct ifmediareq *ifmr; 405 { 406 struct ste_softc *sc; 407 struct mii_data *mii; 408 409 sc = ifp->if_softc; 410 mii = &sc->sc_mii; 411 412 mii_pollstat(mii); 413 ifmr->ifm_active = mii->mii_media_active; 414 ifmr->ifm_status = mii->mii_media_status; 415 416 return; 417 } 418 419 void ste_wait(sc) 420 struct ste_softc *sc; 421 { 422 register int i; 423 424 for (i = 0; i < STE_TIMEOUT; i++) { 425 if (!(CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_DMA_HALTINPROG)) 426 break; 427 } 428 429 if (i == STE_TIMEOUT) 430 printf("ste%d: command never completed!\n", sc->ste_unit); 431 432 return; 433 } 434 435 /* 436 * The EEPROM is slow: give it time to come ready after issuing 437 * it a command. 438 */ 439 int ste_eeprom_wait(sc) 440 struct ste_softc *sc; 441 { 442 int i; 443 444 DELAY(1000); 445 446 for (i = 0; i < 100; i++) { 447 if (CSR_READ_2(sc, STE_EEPROM_CTL) & STE_EECTL_BUSY) 448 DELAY(1000); 449 else 450 break; 451 } 452 453 if (i == 100) { 454 printf("ste%d: eeprom failed to come ready\n", sc->ste_unit); 455 return(1); 456 } 457 458 return(0); 459 } 460 461 /* 462 * Read a sequence of words from the EEPROM. Note that ethernet address 463 * data is stored in the EEPROM in network byte order. 464 */ 465 int ste_read_eeprom(sc, dest, off, cnt, swap) 466 struct ste_softc *sc; 467 caddr_t dest; 468 int off; 469 int cnt; 470 int swap; 471 { 472 int err = 0, i; 473 u_int16_t word = 0, *ptr; 474 475 if (ste_eeprom_wait(sc)) 476 return(1); 477 478 for (i = 0; i < cnt; i++) { 479 CSR_WRITE_2(sc, STE_EEPROM_CTL, STE_EEOPCODE_READ | (off + i)); 480 err = ste_eeprom_wait(sc); 481 if (err) 482 break; 483 word = CSR_READ_2(sc, STE_EEPROM_DATA); 484 ptr = (u_int16_t *)(dest + (i * 2)); 485 if (swap) 486 *ptr = ntohs(word); 487 else 488 *ptr = word; 489 } 490 491 return(err ? 1 : 0); 492 } 493 494 u_int8_t ste_calchash(addr) 495 caddr_t addr; 496 { 497 498 u_int32_t crc, carry; 499 int i, j; 500 u_int8_t c; 501 502 /* Compute CRC for the address value. */ 503 crc = 0xFFFFFFFF; /* initial value */ 504 505 for (i = 0; i < 6; i++) { 506 c = *(addr + i); 507 for (j = 0; j < 8; j++) { 508 carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01); 509 crc <<= 1; 510 c >>= 1; 511 if (carry) 512 crc = (crc ^ 0x04c11db6) | carry; 513 } 514 } 515 516 /* return the filter bit position */ 517 return(crc & 0x0000003F); 518 } 519 520 void ste_setmulti(sc) 521 struct ste_softc *sc; 522 { 523 struct ifnet *ifp; 524 struct arpcom *ac = &sc->arpcom; 525 struct ether_multi *enm; 526 struct ether_multistep step; 527 int h = 0; 528 u_int32_t hashes[2] = { 0, 0 }; 529 530 ifp = &sc->arpcom.ac_if; 531 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 532 STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI); 533 STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH); 534 return; 535 } 536 537 /* first, zot all the existing hash bits */ 538 CSR_WRITE_4(sc, STE_MAR0, 0); 539 CSR_WRITE_4(sc, STE_MAR1, 0); 540 541 /* now program new ones */ 542 ETHER_FIRST_MULTI(step, ac, enm); 543 while (enm != NULL) { 544 h = ste_calchash(enm->enm_addrlo); 545 if (h < 32) 546 hashes[0] |= (1 << h); 547 else 548 hashes[1] |= (1 << (h - 32)); 549 ETHER_NEXT_MULTI(step, enm); 550 } 551 552 CSR_WRITE_4(sc, STE_MAR0, hashes[0]); 553 CSR_WRITE_4(sc, STE_MAR1, hashes[1]); 554 STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI); 555 STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH); 556 557 return; 558 } 559 560 int ste_intr(xsc) 561 void *xsc; 562 { 563 struct ste_softc *sc; 564 struct ifnet *ifp; 565 u_int16_t status; 566 int claimed = 0; 567 568 sc = xsc; 569 ifp = &sc->arpcom.ac_if; 570 571 /* See if this is really our interrupt. */ 572 if (!(CSR_READ_2(sc, STE_ISR) & STE_ISR_INTLATCH)) 573 return claimed; 574 575 for (;;) { 576 status = CSR_READ_2(sc, STE_ISR_ACK); 577 578 if (!(status & STE_INTRS)) 579 break; 580 581 claimed = 1; 582 583 if (status & STE_ISR_RX_DMADONE) 584 ste_rxeof(sc); 585 586 if (status & STE_ISR_TX_DMADONE) 587 ste_txeof(sc); 588 589 if (status & STE_ISR_TX_DONE) 590 ste_txeoc(sc); 591 592 if (status & STE_ISR_STATS_OFLOW) { 593 timeout_del(&sc->sc_stats_tmo); 594 ste_stats_update(sc); 595 } 596 597 if (status & STE_ISR_HOSTERR) { 598 ste_reset(sc); 599 ste_init(sc); 600 } 601 } 602 603 /* Re-enable interrupts */ 604 CSR_WRITE_2(sc, STE_IMR, STE_INTRS); 605 606 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 607 ste_start(ifp); 608 609 return claimed; 610 } 611 612 /* 613 * A frame has been uploaded: pass the resulting mbuf chain up to 614 * the higher level protocols. 615 */ 616 void ste_rxeof(sc) 617 struct ste_softc *sc; 618 { 619 struct mbuf *m; 620 struct ifnet *ifp; 621 struct ste_chain_onefrag *cur_rx; 622 int total_len = 0; 623 u_int32_t rxstat; 624 625 ifp = &sc->arpcom.ac_if; 626 627 again: 628 629 while((rxstat = sc->ste_cdata.ste_rx_head->ste_ptr->ste_status)) { 630 cur_rx = sc->ste_cdata.ste_rx_head; 631 sc->ste_cdata.ste_rx_head = cur_rx->ste_next; 632 633 /* 634 * If an error occurs, update stats, clear the 635 * status word and leave the mbuf cluster in place: 636 * it should simply get re-used next time this descriptor 637 * comes up in the ring. 638 */ 639 if (rxstat & STE_RXSTAT_FRAME_ERR) { 640 ifp->if_ierrors++; 641 cur_rx->ste_ptr->ste_status = 0; 642 continue; 643 } 644 645 /* 646 * If there error bit was not set, the upload complete 647 * bit should be set which means we have a valid packet. 648 * If not, something truly strange has happened. 649 */ 650 if (!(rxstat & STE_RXSTAT_DMADONE)) { 651 printf("ste%d: bad receive status -- packet dropped", 652 sc->ste_unit); 653 ifp->if_ierrors++; 654 cur_rx->ste_ptr->ste_status = 0; 655 continue; 656 } 657 658 /* No errors; receive the packet. */ 659 m = cur_rx->ste_mbuf; 660 total_len = cur_rx->ste_ptr->ste_status & STE_RXSTAT_FRAMELEN; 661 662 /* 663 * Try to conjure up a new mbuf cluster. If that 664 * fails, it means we have an out of memory condition and 665 * should leave the buffer in place and continue. This will 666 * result in a lost packet, but there's little else we 667 * can do in this situation. 668 */ 669 if (ste_newbuf(sc, cur_rx, NULL) == ENOBUFS) { 670 ifp->if_ierrors++; 671 cur_rx->ste_ptr->ste_status = 0; 672 continue; 673 } 674 675 ifp->if_ipackets++; 676 m->m_pkthdr.rcvif = ifp; 677 m->m_pkthdr.len = m->m_len = total_len; 678 679 #if NBPFILTER > 0 680 if (ifp->if_bpf) 681 bpf_mtap(ifp->if_bpf, m); 682 #endif 683 684 /* pass it on. */ 685 ether_input_mbuf(ifp, m); 686 } 687 688 /* 689 * Handle the 'end of channel' condition. When the upload 690 * engine hits the end of the RX ring, it will stall. This 691 * is our cue to flush the RX ring, reload the uplist pointer 692 * register and unstall the engine. 693 * XXX This is actually a little goofy. With the ThunderLAN 694 * chip, you get an interrupt when the receiver hits the end 695 * of the receive ring, which tells you exactly when you 696 * you need to reload the ring pointer. Here we have to 697 * fake it. I'm mad at myself for not being clever enough 698 * to avoid the use of a goto here. 699 */ 700 if (CSR_READ_4(sc, STE_RX_DMALIST_PTR) == 0 || 701 CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_RXDMA_STOPPED) { 702 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL); 703 ste_wait(sc); 704 CSR_WRITE_4(sc, STE_RX_DMALIST_PTR, 705 vtophys(&sc->ste_ldata->ste_rx_list[0])); 706 sc->ste_cdata.ste_rx_head = &sc->ste_cdata.ste_rx_chain[0]; 707 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL); 708 goto again; 709 } 710 711 return; 712 } 713 714 void ste_txeoc(sc) 715 struct ste_softc *sc; 716 { 717 u_int8_t txstat; 718 struct ifnet *ifp; 719 720 ifp = &sc->arpcom.ac_if; 721 722 while ((txstat = CSR_READ_1(sc, STE_TX_STATUS)) & 723 STE_TXSTATUS_TXDONE) { 724 if (txstat & STE_TXSTATUS_UNDERRUN || 725 txstat & STE_TXSTATUS_EXCESSCOLLS || 726 txstat & STE_TXSTATUS_RECLAIMERR) { 727 ifp->if_oerrors++; 728 printf("ste%d: transmission error: %x\n", 729 sc->ste_unit, txstat); 730 731 ste_reset(sc); 732 ste_init(sc); 733 734 if (txstat & STE_TXSTATUS_UNDERRUN && 735 sc->ste_tx_thresh < STE_PACKET_SIZE) { 736 sc->ste_tx_thresh += STE_MIN_FRAMELEN; 737 printf("ste%d: tx underrun, increasing tx" 738 " start threshold to %d bytes\n", 739 sc->ste_unit, sc->ste_tx_thresh); 740 } 741 CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh); 742 CSR_WRITE_2(sc, STE_TX_RECLAIM_THRESH, 743 (STE_PACKET_SIZE >> 4)); 744 } 745 ste_init(sc); 746 CSR_WRITE_2(sc, STE_TX_STATUS, txstat); 747 } 748 749 return; 750 } 751 752 void ste_txeof(sc) 753 struct ste_softc *sc; 754 { 755 struct ste_chain *cur_tx = NULL; 756 struct ifnet *ifp; 757 int idx; 758 759 ifp = &sc->arpcom.ac_if; 760 761 idx = sc->ste_cdata.ste_tx_cons; 762 while(idx != sc->ste_cdata.ste_tx_prod) { 763 cur_tx = &sc->ste_cdata.ste_tx_chain[idx]; 764 765 if (!(cur_tx->ste_ptr->ste_ctl & STE_TXCTL_DMADONE)) 766 break; 767 768 if (cur_tx->ste_mbuf != NULL) { 769 m_freem(cur_tx->ste_mbuf); 770 cur_tx->ste_mbuf = NULL; 771 } 772 773 ifp->if_opackets++; 774 775 sc->ste_cdata.ste_tx_cnt--; 776 STE_INC(idx, STE_TX_LIST_CNT); 777 ifp->if_timer = 0; 778 } 779 780 sc->ste_cdata.ste_tx_cons = idx; 781 782 if (cur_tx != NULL) 783 ifp->if_flags &= ~IFF_OACTIVE; 784 785 return; 786 } 787 788 void ste_stats_update(xsc) 789 void *xsc; 790 { 791 struct ste_softc *sc; 792 struct ste_stats stats; 793 struct ifnet *ifp; 794 struct mii_data *mii; 795 int i, s; 796 u_int8_t *p; 797 798 s = splimp(); 799 800 sc = xsc; 801 ifp = &sc->arpcom.ac_if; 802 mii = &sc->sc_mii; 803 804 p = (u_int8_t *)&stats; 805 806 for (i = 0; i < sizeof(stats); i++) { 807 *p = CSR_READ_1(sc, STE_STATS + i); 808 p++; 809 } 810 811 ifp->if_collisions += stats.ste_single_colls + 812 stats.ste_multi_colls + stats.ste_late_colls; 813 814 mii_tick(mii); 815 if (!sc->ste_link) { 816 mii_pollstat(mii); 817 if (mii->mii_media_status & IFM_ACTIVE && 818 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) 819 sc->ste_link++; 820 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 821 ste_start(ifp); 822 } 823 824 timeout_add(&sc->sc_stats_tmo, hz); 825 splx(s); 826 827 return; 828 } 829 830 831 /* 832 * Probe for a Sundance ST201 chip. Check the PCI vendor and device 833 * IDs against our list and return a device name if we find a match. 834 */ 835 int ste_probe(parent, match, aux) 836 struct device *parent; 837 void *match, *aux; 838 { 839 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 840 841 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUNDANCE && 842 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUNDANCE_ST201) 843 return(1); 844 845 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_DLINK && 846 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_DLINK_550TX) 847 return(1); 848 849 return(0); 850 } 851 852 /* 853 * Attach the interface. Allocate softc structures, do ifmedia 854 * setup and ethernet/BPF attach. 855 */ 856 void ste_attach(parent, self, aux) 857 struct device *parent, *self; 858 void *aux; 859 { 860 int s; 861 const char *intrstr = NULL; 862 u_int32_t command; 863 struct ste_softc *sc = (struct ste_softc *)self; 864 struct pci_attach_args *pa = aux; 865 pci_chipset_tag_t pc = pa->pa_pc; 866 pci_intr_handle_t ih; 867 struct ifnet *ifp; 868 bus_addr_t iobase; 869 bus_size_t iosize; 870 871 s = splimp(); 872 sc->ste_unit = sc->sc_dev.dv_unit; 873 874 /* 875 * Handle power management nonsense. 876 */ 877 command = pci_conf_read(pc, pa->pa_tag, STE_PCI_CAPID) & 0x000000FF; 878 if (command == 0x01) { 879 880 command = pci_conf_read(pc, pa->pa_tag, STE_PCI_PWRMGMTCTRL); 881 if (command & STE_PSTATE_MASK) { 882 u_int32_t iobase, membase, irq; 883 884 /* Save important PCI config data. */ 885 iobase = pci_conf_read(pc, pa->pa_tag, STE_PCI_LOIO); 886 membase = pci_conf_read(pc, pa->pa_tag, STE_PCI_LOMEM); 887 irq = pci_conf_read(pc, pa->pa_tag, STE_PCI_INTLINE); 888 889 /* Reset the power state. */ 890 printf("ste%d: chip is in D%d power mode " 891 "-- setting to D0\n", sc->ste_unit, command & STE_PSTATE_MASK); 892 command &= 0xFFFFFFFC; 893 pci_conf_write(pc, pa->pa_tag, STE_PCI_PWRMGMTCTRL, command); 894 895 /* Restore PCI config data. */ 896 pci_conf_write(pc, pa->pa_tag, STE_PCI_LOIO, iobase); 897 pci_conf_write(pc, pa->pa_tag, STE_PCI_LOMEM, membase); 898 pci_conf_write(pc, pa->pa_tag, STE_PCI_INTLINE, irq); 899 } 900 } 901 902 /* 903 * Map control/status registers. 904 */ 905 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 906 command |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | 907 PCI_COMMAND_MASTER_ENABLE; 908 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command); 909 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 910 911 #ifdef STE_USEIOSPACE 912 if (!(command & PCI_COMMAND_IO_ENABLE)) { 913 printf(": failed to enable I/O ports\n"); 914 goto fail; 915 } 916 if (pci_io_find(pc, pa->pa_tag, STE_PCI_LOIO, &iobase, &iosize)) { 917 printf(": can't find I/O space\n"); 918 goto fail; 919 } 920 if (bus_space_map(pa->pa_iot, iobase, iosize, 0, &sc->ste_bhandle)) { 921 printf(": can't map I/O space\n"); 922 goto fail; 923 } 924 sc->ste_btag = pa->pa_iot; 925 #else 926 if (!(command & PCI_COMMAND_MEM_ENABLE)) { 927 printf(": failed to enable memory mapping\n"); 928 goto fail; 929 } 930 if (pci_mem_find(pc, pa->pa_tag, STE_PCI_LOMEM, &iobase, &iosize,NULL)){ 931 printf(": can't find mem space\n"); 932 goto fail; 933 } 934 if (bus_space_map(pa->pa_memt, iobase, iosize, 0, &sc->ste_bhandle)) { 935 printf(": can't map mem space\n"); 936 goto fail; 937 } 938 sc->ste_btag = pa->pa_memt; 939 #endif 940 941 /* Allocate interrupt */ 942 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline, 943 &ih)) { 944 printf(": couldn't map interrupt\n"); 945 goto fail; 946 } 947 intrstr = pci_intr_string(pc, ih); 948 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ste_intr, sc, 949 self->dv_xname); 950 if (sc->sc_ih == NULL) { 951 printf(": couldn't establish interrupt"); 952 if (intrstr != NULL) 953 printf(" at %s", intrstr); 954 printf("\n"); 955 goto fail; 956 } 957 printf(": %s", intrstr); 958 959 /* Reset the adapter. */ 960 ste_reset(sc); 961 962 /* 963 * Get station address from the EEPROM. 964 */ 965 ste_read_eeprom(sc,(caddr_t)&sc->arpcom.ac_enaddr,STE_EEADDR_NODE0,3,0); 966 967 printf(" address %s\n", ether_sprintf(sc->arpcom.ac_enaddr)); 968 969 sc->ste_ldata_ptr = malloc(sizeof(struct ste_list_data) + 8, 970 M_DEVBUF, M_NOWAIT); 971 if (sc->ste_ldata_ptr == NULL) { 972 printf("%s: no memory for list buffers!\n", sc->ste_unit); 973 goto fail; 974 } 975 976 sc->ste_ldata = (struct ste_list_data *)sc->ste_ldata_ptr; 977 bzero(sc->ste_ldata, sizeof(struct ste_list_data)); 978 979 ifp = &sc->arpcom.ac_if; 980 ifp->if_softc = sc; 981 ifp->if_mtu = ETHERMTU; 982 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 983 ifp->if_ioctl = ste_ioctl; 984 ifp->if_output = ether_output; 985 ifp->if_start = ste_start; 986 ifp->if_watchdog = ste_watchdog; 987 ifp->if_baudrate = 10000000; 988 IFQ_SET_MAXLEN(&ifp->if_snd, STE_TX_LIST_CNT - 1); 989 IFQ_SET_READY(&ifp->if_snd); 990 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); 991 992 sc->sc_mii.mii_ifp = ifp; 993 sc->sc_mii.mii_readreg = ste_miibus_readreg; 994 sc->sc_mii.mii_writereg = ste_miibus_writereg; 995 sc->sc_mii.mii_statchg = ste_miibus_statchg; 996 ifmedia_init(&sc->sc_mii.mii_media, 0, ste_ifmedia_upd,ste_ifmedia_sts); 997 mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 998 0); 999 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 1000 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 1001 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 1002 } else 1003 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 1004 1005 /* 1006 * Call MI attach routines. 1007 */ 1008 if_attach(ifp); 1009 ether_ifattach(ifp); 1010 1011 shutdownhook_establish(ste_shutdown, sc); 1012 1013 fail: 1014 splx(s); 1015 return; 1016 } 1017 1018 int ste_newbuf(sc, c, m) 1019 struct ste_softc *sc; 1020 struct ste_chain_onefrag *c; 1021 struct mbuf *m; 1022 { 1023 struct mbuf *m_new = NULL; 1024 1025 if (m == NULL) { 1026 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1027 if (m_new == NULL) { 1028 printf("ste%d: no memory for rx list -- " 1029 "packet dropped\n", sc->ste_unit); 1030 return(ENOBUFS); 1031 } 1032 MCLGET(m_new, M_DONTWAIT); 1033 if (!(m_new->m_flags & M_EXT)) { 1034 printf("ste%d: no memory for rx list -- " 1035 "packet dropped\n", sc->ste_unit); 1036 m_freem(m_new); 1037 return(ENOBUFS); 1038 } 1039 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 1040 } else { 1041 m_new = m; 1042 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 1043 m_new->m_data = m_new->m_ext.ext_buf; 1044 } 1045 1046 m_adj(m_new, ETHER_ALIGN); 1047 1048 c->ste_mbuf = m_new; 1049 c->ste_ptr->ste_status = 0; 1050 c->ste_ptr->ste_frag.ste_addr = vtophys(mtod(m_new, caddr_t)); 1051 c->ste_ptr->ste_frag.ste_len = 1536 | STE_FRAG_LAST; 1052 1053 return(0); 1054 } 1055 1056 int ste_init_rx_list(sc) 1057 struct ste_softc *sc; 1058 { 1059 struct ste_chain_data *cd; 1060 struct ste_list_data *ld; 1061 int i; 1062 1063 cd = &sc->ste_cdata; 1064 ld = sc->ste_ldata; 1065 1066 for (i = 0; i < STE_RX_LIST_CNT; i++) { 1067 cd->ste_rx_chain[i].ste_ptr = &ld->ste_rx_list[i]; 1068 if (ste_newbuf(sc, &cd->ste_rx_chain[i], NULL) == ENOBUFS) 1069 return(ENOBUFS); 1070 if (i == (STE_RX_LIST_CNT - 1)) { 1071 cd->ste_rx_chain[i].ste_next = 1072 &cd->ste_rx_chain[0]; 1073 ld->ste_rx_list[i].ste_next = 1074 vtophys(&ld->ste_rx_list[0]); 1075 } else { 1076 cd->ste_rx_chain[i].ste_next = 1077 &cd->ste_rx_chain[i + 1]; 1078 ld->ste_rx_list[i].ste_next = 1079 vtophys(&ld->ste_rx_list[i + 1]); 1080 } 1081 1082 } 1083 1084 cd->ste_rx_head = &cd->ste_rx_chain[0]; 1085 1086 return(0); 1087 } 1088 1089 void ste_init_tx_list(sc) 1090 struct ste_softc *sc; 1091 { 1092 struct ste_chain_data *cd; 1093 struct ste_list_data *ld; 1094 int i; 1095 1096 cd = &sc->ste_cdata; 1097 ld = sc->ste_ldata; 1098 for (i = 0; i < STE_TX_LIST_CNT; i++) { 1099 cd->ste_tx_chain[i].ste_ptr = &ld->ste_tx_list[i]; 1100 cd->ste_tx_chain[i].ste_phys = vtophys(&ld->ste_tx_list[i]); 1101 if (i == (STE_TX_LIST_CNT - 1)) 1102 cd->ste_tx_chain[i].ste_next = 1103 &cd->ste_tx_chain[0]; 1104 else 1105 cd->ste_tx_chain[i].ste_next = 1106 &cd->ste_tx_chain[i + 1]; 1107 if (i == 0) 1108 cd->ste_tx_chain[i].ste_prev = 1109 &cd->ste_tx_chain[STE_TX_LIST_CNT - 1]; 1110 else 1111 cd->ste_tx_chain[i].ste_prev = 1112 &cd->ste_tx_chain[i - 1]; 1113 } 1114 1115 bzero((char *)ld->ste_tx_list, 1116 sizeof(struct ste_desc) * STE_TX_LIST_CNT); 1117 1118 cd->ste_tx_prod = 0; 1119 cd->ste_tx_cons = 0; 1120 cd->ste_tx_cnt = 0; 1121 1122 return; 1123 } 1124 1125 void ste_init(xsc) 1126 void *xsc; 1127 { 1128 struct ste_softc *sc = (struct ste_softc *)xsc; 1129 struct ifnet *ifp = &sc->arpcom.ac_if; 1130 struct mii_data *mii; 1131 int i, s; 1132 1133 s = splimp(); 1134 1135 ste_stop(sc); 1136 1137 mii = &sc->sc_mii; 1138 1139 /* Init our MAC address */ 1140 for (i = 0; i < ETHER_ADDR_LEN; i++) { 1141 CSR_WRITE_1(sc, STE_PAR0 + i, sc->arpcom.ac_enaddr[i]); 1142 } 1143 1144 /* Init RX list */ 1145 if (ste_init_rx_list(sc) == ENOBUFS) { 1146 printf("ste%d: initialization failed: no " 1147 "memory for RX buffers\n", sc->ste_unit); 1148 ste_stop(sc); 1149 splx(s); 1150 return; 1151 } 1152 1153 /* Init TX descriptors */ 1154 ste_init_tx_list(sc); 1155 1156 /* Set the TX freethresh value */ 1157 CSR_WRITE_1(sc, STE_TX_DMABURST_THRESH, STE_PACKET_SIZE >> 8); 1158 1159 /* Set the TX start threshold for best performance. */ 1160 CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh); 1161 1162 /* Set the TX reclaim threshold. */ 1163 CSR_WRITE_1(sc, STE_TX_RECLAIM_THRESH, (STE_PACKET_SIZE >> 4)); 1164 1165 /* Set up the RX filter. */ 1166 CSR_WRITE_1(sc, STE_RX_MODE, STE_RXMODE_UNICAST); 1167 1168 /* If we want promiscuous mode, set the allframes bit. */ 1169 if (ifp->if_flags & IFF_PROMISC) { 1170 STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC); 1171 } else { 1172 STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC); 1173 } 1174 1175 /* Set capture broadcast bit to accept broadcast frames. */ 1176 if (ifp->if_flags & IFF_BROADCAST) { 1177 STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST); 1178 } else { 1179 STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST); 1180 } 1181 1182 ste_setmulti(sc); 1183 1184 /* Load the address of the RX list. */ 1185 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL); 1186 ste_wait(sc); 1187 CSR_WRITE_4(sc, STE_RX_DMALIST_PTR, 1188 vtophys(&sc->ste_ldata->ste_rx_list[0])); 1189 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL); 1190 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL); 1191 1192 /* Set TX polling interval */ 1193 CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 64); 1194 1195 /* Load address of the TX list */ 1196 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL); 1197 ste_wait(sc); 1198 CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 1199 vtophys(&sc->ste_ldata->ste_tx_list[0])); 1200 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL); 1201 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL); 1202 ste_wait(sc); 1203 1204 /* Enable receiver and transmitter */ 1205 CSR_WRITE_2(sc, STE_MACCTL0, 0); 1206 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_ENABLE); 1207 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_ENABLE); 1208 1209 /* Enable stats counters. */ 1210 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_ENABLE); 1211 1212 /* Enable interrupts. */ 1213 CSR_WRITE_2(sc, STE_ISR, 0xFFFF); 1214 CSR_WRITE_2(sc, STE_IMR, STE_INTRS); 1215 1216 ste_ifmedia_upd(ifp); 1217 1218 ifp->if_flags |= IFF_RUNNING; 1219 ifp->if_flags &= ~IFF_OACTIVE; 1220 1221 splx(s); 1222 1223 timeout_set(&sc->sc_stats_tmo, ste_stats_update, sc); 1224 timeout_add(&sc->sc_stats_tmo, hz); 1225 1226 return; 1227 } 1228 1229 void ste_stop(sc) 1230 struct ste_softc *sc; 1231 { 1232 int i; 1233 struct ifnet *ifp; 1234 1235 ifp = &sc->arpcom.ac_if; 1236 1237 timeout_del(&sc->sc_stats_tmo); 1238 1239 CSR_WRITE_2(sc, STE_IMR, 0); 1240 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_DISABLE); 1241 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_DISABLE); 1242 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_DISABLE); 1243 STE_SETBIT2(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL); 1244 STE_SETBIT2(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL); 1245 ste_wait(sc); 1246 1247 sc->ste_link = 0; 1248 1249 for (i = 0; i < STE_RX_LIST_CNT; i++) { 1250 if (sc->ste_cdata.ste_rx_chain[i].ste_mbuf != NULL) { 1251 m_freem(sc->ste_cdata.ste_rx_chain[i].ste_mbuf); 1252 sc->ste_cdata.ste_rx_chain[i].ste_mbuf = NULL; 1253 } 1254 } 1255 1256 for (i = 0; i < STE_TX_LIST_CNT; i++) { 1257 if (sc->ste_cdata.ste_tx_chain[i].ste_mbuf != NULL) { 1258 m_freem(sc->ste_cdata.ste_tx_chain[i].ste_mbuf); 1259 sc->ste_cdata.ste_tx_chain[i].ste_mbuf = NULL; 1260 } 1261 } 1262 1263 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE); 1264 1265 return; 1266 } 1267 1268 void ste_reset(sc) 1269 struct ste_softc *sc; 1270 { 1271 int i; 1272 1273 STE_SETBIT4(sc, STE_ASICCTL, 1274 STE_ASICCTL_GLOBAL_RESET|STE_ASICCTL_RX_RESET| 1275 STE_ASICCTL_TX_RESET|STE_ASICCTL_DMA_RESET| 1276 STE_ASICCTL_FIFO_RESET|STE_ASICCTL_NETWORK_RESET| 1277 STE_ASICCTL_AUTOINIT_RESET|STE_ASICCTL_HOST_RESET| 1278 STE_ASICCTL_EXTRESET_RESET); 1279 1280 DELAY(100000); 1281 1282 for (i = 0; i < STE_TIMEOUT; i++) { 1283 if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RESET_BUSY)) 1284 break; 1285 } 1286 1287 if (i == STE_TIMEOUT) 1288 printf("ste%d: global reset never completed\n", sc->ste_unit); 1289 1290 return; 1291 } 1292 1293 int ste_ioctl(ifp, command, data) 1294 struct ifnet *ifp; 1295 u_long command; 1296 caddr_t data; 1297 { 1298 struct ste_softc *sc = ifp->if_softc; 1299 struct ifreq *ifr = (struct ifreq *) data; 1300 struct ifaddr *ifa = (struct ifaddr *)data; 1301 struct mii_data *mii; 1302 int s, error = 0; 1303 1304 s = splimp(); 1305 1306 if ((error = ether_ioctl(ifp, &sc->arpcom, command, data)) > 0) { 1307 splx(s); 1308 return error; 1309 } 1310 1311 switch(command) { 1312 case SIOCSIFADDR: 1313 ifp->if_flags |= IFF_UP; 1314 switch (ifa->ifa_addr->sa_family) { 1315 case AF_INET: 1316 ste_init(sc); 1317 arp_ifinit(&sc->arpcom, ifa); 1318 break; 1319 default: 1320 ste_init(sc); 1321 break; 1322 } 1323 break; 1324 case SIOCSIFFLAGS: 1325 if (ifp->if_flags & IFF_UP) { 1326 if (ifp->if_flags & IFF_RUNNING && 1327 ifp->if_flags & IFF_PROMISC && 1328 !(sc->ste_if_flags & IFF_PROMISC)) { 1329 STE_SETBIT1(sc, STE_RX_MODE, 1330 STE_RXMODE_PROMISC); 1331 } else if (ifp->if_flags & IFF_RUNNING && 1332 !(ifp->if_flags & IFF_PROMISC) && 1333 sc->ste_if_flags & IFF_PROMISC) { 1334 STE_CLRBIT1(sc, STE_RX_MODE, 1335 STE_RXMODE_PROMISC); 1336 } else if (!(ifp->if_flags & IFF_RUNNING)) { 1337 sc->ste_tx_thresh = STE_MIN_FRAMELEN; 1338 ste_init(sc); 1339 } 1340 } else { 1341 if (ifp->if_flags & IFF_RUNNING) 1342 ste_stop(sc); 1343 } 1344 sc->ste_if_flags = ifp->if_flags; 1345 error = 0; 1346 break; 1347 case SIOCADDMULTI: 1348 case SIOCDELMULTI: 1349 error = (command == SIOCADDMULTI) ? 1350 ether_addmulti(ifr, &sc->arpcom) : 1351 ether_delmulti(ifr, &sc->arpcom); 1352 1353 if (error == ENETRESET) { 1354 /* 1355 * Multicast list has changed; set the hardware 1356 * filter accordingly. 1357 */ 1358 ste_setmulti(sc); 1359 error = 0; 1360 } 1361 break; 1362 case SIOCGIFMEDIA: 1363 case SIOCSIFMEDIA: 1364 mii = &sc->sc_mii; 1365 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1366 break; 1367 default: 1368 error = EINVAL; 1369 break; 1370 } 1371 1372 splx(s); 1373 1374 return(error); 1375 } 1376 1377 int ste_encap(sc, c, m_head) 1378 struct ste_softc *sc; 1379 struct ste_chain *c; 1380 struct mbuf *m_head; 1381 { 1382 int frag = 0; 1383 struct ste_frag *f = NULL; 1384 struct mbuf *m; 1385 struct ste_desc *d; 1386 int total_len = 0; 1387 1388 d = c->ste_ptr; 1389 d->ste_ctl = 0; 1390 d->ste_next = 0; 1391 1392 for (m = m_head, frag = 0; m != NULL; m = m->m_next) { 1393 if (m->m_len != 0) { 1394 if (frag == STE_MAXFRAGS) 1395 break; 1396 total_len += m->m_len; 1397 f = &c->ste_ptr->ste_frags[frag]; 1398 f->ste_addr = vtophys(mtod(m, vm_offset_t)); 1399 f->ste_len = m->m_len; 1400 frag++; 1401 } 1402 } 1403 1404 c->ste_mbuf = m_head; 1405 c->ste_ptr->ste_frags[frag - 1].ste_len |= STE_FRAG_LAST; 1406 c->ste_ptr->ste_ctl = total_len; 1407 1408 return(0); 1409 } 1410 1411 void ste_start(ifp) 1412 struct ifnet *ifp; 1413 { 1414 struct ste_softc *sc; 1415 struct mbuf *m_head = NULL; 1416 struct ste_chain *prev = NULL, *cur_tx = NULL, *start_tx; 1417 int idx; 1418 1419 sc = ifp->if_softc; 1420 1421 if (!sc->ste_link) 1422 return; 1423 1424 if (ifp->if_flags & IFF_OACTIVE) 1425 return; 1426 1427 idx = sc->ste_cdata.ste_tx_prod; 1428 start_tx = &sc->ste_cdata.ste_tx_chain[idx]; 1429 1430 while(sc->ste_cdata.ste_tx_chain[idx].ste_mbuf == NULL) { 1431 if ((STE_TX_LIST_CNT - sc->ste_cdata.ste_tx_cnt) < 3) { 1432 ifp->if_flags |= IFF_OACTIVE; 1433 break; 1434 } 1435 1436 IFQ_DEQUEUE(&ifp->if_snd, m_head); 1437 if (m_head == NULL) 1438 break; 1439 1440 cur_tx = &sc->ste_cdata.ste_tx_chain[idx]; 1441 1442 ste_encap(sc, cur_tx, m_head); 1443 1444 if (prev != NULL) 1445 prev->ste_ptr->ste_next = cur_tx->ste_phys; 1446 prev = cur_tx; 1447 1448 #if NBPFILTER > 0 1449 /* 1450 * If there's a BPF listener, bounce a copy of this frame 1451 * to him. 1452 */ 1453 if (ifp->if_bpf) 1454 bpf_mtap(ifp->if_bpf, cur_tx->ste_mbuf); 1455 #endif 1456 1457 STE_INC(idx, STE_TX_LIST_CNT); 1458 sc->ste_cdata.ste_tx_cnt++; 1459 } 1460 1461 if (cur_tx == NULL) 1462 return; 1463 1464 cur_tx->ste_ptr->ste_ctl |= STE_TXCTL_DMAINTR; 1465 1466 /* Start transmission */ 1467 sc->ste_cdata.ste_tx_prod = idx; 1468 start_tx->ste_prev->ste_ptr->ste_next = start_tx->ste_phys; 1469 1470 ifp->if_timer = 5; 1471 1472 return; 1473 } 1474 1475 void ste_watchdog(ifp) 1476 struct ifnet *ifp; 1477 { 1478 struct ste_softc *sc; 1479 1480 sc = ifp->if_softc; 1481 1482 ifp->if_oerrors++; 1483 printf("ste%d: watchdog timeout\n", sc->ste_unit); 1484 1485 ste_txeoc(sc); 1486 ste_txeof(sc); 1487 ste_rxeof(sc); 1488 ste_reset(sc); 1489 ste_init(sc); 1490 1491 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1492 ste_start(ifp); 1493 1494 return; 1495 } 1496 1497 void ste_shutdown(v) 1498 void *v; 1499 { 1500 struct ste_softc *sc = (struct ste_softc *)v; 1501 1502 ste_stop(sc); 1503 } 1504 1505 struct cfattach ste_ca = { 1506 sizeof(struct ste_softc), ste_probe, ste_attach 1507 }; 1508 1509 struct cfdriver ste_cd = { 1510 0, "ste", DV_IFNET 1511 }; 1512 1513