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