1 /* $OpenBSD: if_el.c,v 1.16 2001/06/27 06:34:45 kjc Exp $ */ 2 /* $NetBSD: if_el.c,v 1.39 1996/05/12 23:52:32 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1994, Matthew E. Kimmel. Permission is hereby granted 6 * to use, copy, modify and distribute this software provided that both 7 * the copyright notice and this permission notice appear in all copies 8 * of the software, derivative works or modified versions, and any 9 * portions thereof. 10 */ 11 12 /* 13 * 3COM Etherlink 3C501 device driver 14 */ 15 16 /* 17 * Bugs/possible improvements: 18 * - Does not currently support DMA 19 * - Does not currently support multicasts 20 */ 21 22 #include "bpfilter.h" 23 24 #include <sys/param.h> 25 #include <sys/systm.h> 26 #include <sys/errno.h> 27 #include <sys/ioctl.h> 28 #include <sys/mbuf.h> 29 #include <sys/socket.h> 30 #include <sys/syslog.h> 31 #include <sys/device.h> 32 33 #include <net/if.h> 34 #include <net/if_dl.h> 35 #include <net/if_types.h> 36 37 #ifdef INET 38 #include <netinet/in.h> 39 #include <netinet/in_systm.h> 40 #include <netinet/in_var.h> 41 #include <netinet/ip.h> 42 #include <netinet/if_ether.h> 43 #endif 44 45 #if NBPFILTER > 0 46 #include <net/bpf.h> 47 #include <net/bpfdesc.h> 48 #endif 49 50 #include <machine/cpu.h> 51 #include <machine/intr.h> 52 #include <machine/pio.h> 53 54 #include <dev/isa/isavar.h> 55 #include <dev/isa/if_elreg.h> 56 57 /* for debugging convenience */ 58 #ifdef EL_DEBUG 59 #define dprintf(x) printf x 60 #else 61 #define dprintf(x) 62 #endif 63 64 /* 65 * per-line info and status 66 */ 67 struct el_softc { 68 struct device sc_dev; 69 void *sc_ih; 70 71 struct arpcom sc_arpcom; /* ethernet common */ 72 int sc_iobase; /* base I/O addr */ 73 }; 74 75 /* 76 * prototypes 77 */ 78 int elintr __P((void *)); 79 void elinit __P((struct el_softc *)); 80 int elioctl __P((struct ifnet *, u_long, caddr_t)); 81 void elstart __P((struct ifnet *)); 82 void elwatchdog __P((struct ifnet *)); 83 void elreset __P((struct el_softc *)); 84 void elstop __P((struct el_softc *)); 85 static int el_xmit __P((struct el_softc *)); 86 void elread __P((struct el_softc *, int)); 87 struct mbuf *elget __P((struct el_softc *sc, int)); 88 static inline void el_hardreset __P((struct el_softc *)); 89 90 int elprobe __P((struct device *, void *, void *)); 91 void elattach __P((struct device *, struct device *, void *)); 92 93 struct cfattach el_ca = { 94 sizeof(struct el_softc), elprobe, elattach 95 }; 96 97 struct cfdriver el_cd = { 98 NULL, "el", DV_IFNET 99 }; 100 101 /* 102 * Probe routine. 103 * 104 * See if the card is there and at the right place. 105 * (XXX - cgd -- needs help) 106 */ 107 int 108 elprobe(parent, match, aux) 109 struct device *parent; 110 void *match, *aux; 111 { 112 struct el_softc *sc = match; 113 struct isa_attach_args *ia = aux; 114 int iobase = ia->ia_iobase; 115 u_char station_addr[ETHER_ADDR_LEN]; 116 int i; 117 118 /* First check the base. */ 119 if (iobase < 0x280 || iobase > 0x3f0) 120 return 0; 121 122 /* Grab some info for our structure. */ 123 sc->sc_iobase = iobase; 124 125 /* 126 * Now attempt to grab the station address from the PROM and see if it 127 * contains the 3com vendor code. 128 */ 129 dprintf(("Probing 3c501 at 0x%x...\n", iobase)); 130 131 /* Reset the board. */ 132 dprintf(("Resetting board...\n")); 133 outb(iobase+EL_AC, EL_AC_RESET); 134 delay(5); 135 outb(iobase+EL_AC, 0); 136 137 /* Now read the address. */ 138 dprintf(("Reading station address...\n")); 139 for (i = 0; i < ETHER_ADDR_LEN; i++) { 140 outb(iobase+EL_GPBL, i); 141 station_addr[i] = inb(iobase+EL_EAW); 142 } 143 dprintf(("Address is %s\n", ether_sprintf(station_addr))); 144 145 /* 146 * If the vendor code is ok, return a 1. We'll assume that whoever 147 * configured this system is right about the IRQ. 148 */ 149 if (station_addr[0] != 0x02 || station_addr[1] != 0x60 || 150 station_addr[2] != 0x8c) { 151 dprintf(("Bad vendor code.\n")); 152 return 0; 153 } 154 155 dprintf(("Vendor code ok.\n")); 156 /* Copy the station address into the arpcom structure. */ 157 bcopy(station_addr, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN); 158 159 ia->ia_iosize = 4; /* XXX */ 160 ia->ia_msize = 0; 161 return 1; 162 } 163 164 /* 165 * Attach the interface to the kernel data structures. By the time this is 166 * called, we know that the card exists at the given I/O address. We still 167 * assume that the IRQ given is correct. 168 */ 169 void 170 elattach(parent, self, aux) 171 struct device *parent, *self; 172 void *aux; 173 { 174 struct el_softc *sc = (void *)self; 175 struct isa_attach_args *ia = aux; 176 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 177 178 dprintf(("Attaching %s...\n", sc->sc_dev.dv_xname)); 179 180 /* Stop the board. */ 181 elstop(sc); 182 183 /* Initialize ifnet structure. */ 184 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); 185 ifp->if_softc = sc; 186 ifp->if_start = elstart; 187 ifp->if_ioctl = elioctl; 188 ifp->if_watchdog = elwatchdog; 189 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS; 190 IFQ_SET_READY(&ifp->if_snd); 191 192 /* Now we can attach the interface. */ 193 dprintf(("Attaching interface...\n")); 194 if_attach(ifp); 195 ether_ifattach(ifp); 196 197 /* Print out some information for the user. */ 198 printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr)); 199 200 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, 201 IPL_NET, elintr, sc, sc->sc_dev.dv_xname); 202 203 dprintf(("elattach() finished.\n")); 204 } 205 206 /* 207 * Reset interface. 208 */ 209 void 210 elreset(sc) 211 struct el_softc *sc; 212 { 213 int s; 214 215 dprintf(("elreset()\n")); 216 s = splnet(); 217 elstop(sc); 218 elinit(sc); 219 splx(s); 220 } 221 222 /* 223 * Stop interface. 224 */ 225 void 226 elstop(sc) 227 struct el_softc *sc; 228 { 229 230 outb(sc->sc_iobase+EL_AC, 0); 231 } 232 233 /* 234 * Do a hardware reset of the board, and upload the ethernet address again in 235 * case the board forgets. 236 */ 237 static inline void 238 el_hardreset(sc) 239 struct el_softc *sc; 240 { 241 int iobase = sc->sc_iobase; 242 int i; 243 244 outb(iobase+EL_AC, EL_AC_RESET); 245 delay(5); 246 outb(iobase+EL_AC, 0); 247 248 for (i = 0; i < ETHER_ADDR_LEN; i++) 249 outb(iobase+i, sc->sc_arpcom.ac_enaddr[i]); 250 } 251 252 /* 253 * Initialize interface. 254 */ 255 void 256 elinit(sc) 257 struct el_softc *sc; 258 { 259 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 260 int iobase = sc->sc_iobase; 261 262 /* First, reset the board. */ 263 el_hardreset(sc); 264 265 /* Configure rx. */ 266 dprintf(("Configuring rx...\n")); 267 if (ifp->if_flags & IFF_PROMISC) 268 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_PROMISC); 269 else 270 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_ABROAD); 271 outb(iobase+EL_RBC, 0); 272 273 /* Configure TX. */ 274 dprintf(("Configuring tx...\n")); 275 outb(iobase+EL_TXC, 0); 276 277 /* Start reception. */ 278 dprintf(("Starting reception...\n")); 279 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX); 280 281 /* Set flags appropriately. */ 282 ifp->if_flags |= IFF_RUNNING; 283 ifp->if_flags &= ~IFF_OACTIVE; 284 285 /* And start output. */ 286 elstart(ifp); 287 } 288 289 /* 290 * Start output on interface. Get datagrams from the queue and output them, 291 * giving the receiver a chance between datagrams. Call only from splnet or 292 * interrupt level! 293 */ 294 void 295 elstart(ifp) 296 struct ifnet *ifp; 297 { 298 struct el_softc *sc = ifp->if_softc; 299 int iobase = sc->sc_iobase; 300 struct mbuf *m, *m0; 301 int s, i, off, retries; 302 303 dprintf(("elstart()...\n")); 304 s = splnet(); 305 306 /* Don't do anything if output is active. */ 307 if ((ifp->if_flags & IFF_OACTIVE) != 0) { 308 splx(s); 309 return; 310 } 311 312 ifp->if_flags |= IFF_OACTIVE; 313 314 /* 315 * The main loop. They warned me against endless loops, but would I 316 * listen? NOOO.... 317 */ 318 for (;;) { 319 /* Dequeue the next datagram. */ 320 IFQ_DEQUEUE(&ifp->if_snd, m0); 321 322 /* If there's nothing to send, return. */ 323 if (m0 == 0) 324 break; 325 326 #if NBPFILTER > 0 327 /* Give the packet to the bpf, if any. */ 328 if (ifp->if_bpf) 329 bpf_mtap(ifp->if_bpf, m0); 330 #endif 331 332 /* Disable the receiver. */ 333 outb(iobase+EL_AC, EL_AC_HOST); 334 outb(iobase+EL_RBC, 0); 335 336 /* Transfer datagram to board. */ 337 dprintf(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len)); 338 off = EL_BUFSIZ - max(m0->m_pkthdr.len, ETHER_MIN_LEN); 339 outb(iobase+EL_GPBL, off); 340 outb(iobase+EL_GPBH, off >> 8); 341 342 /* Copy the datagram to the buffer. */ 343 for (m = m0; m != 0; m = m->m_next) 344 outsb(iobase+EL_BUF, mtod(m, caddr_t), m->m_len); 345 346 m_freem(m0); 347 348 /* Now transmit the datagram. */ 349 retries = 0; 350 for (;;) { 351 outb(iobase+EL_GPBL, off); 352 outb(iobase+EL_GPBH, off >> 8); 353 if (el_xmit(sc)) { 354 ifp->if_oerrors++; 355 break; 356 } 357 /* Check out status. */ 358 i = inb(iobase+EL_TXS); 359 dprintf(("tx status=0x%x\n", i)); 360 if ((i & EL_TXS_READY) == 0) { 361 dprintf(("el: err txs=%x\n", i)); 362 if (i & (EL_TXS_COLL | EL_TXS_COLL16)) { 363 ifp->if_collisions++; 364 if ((i & EL_TXC_DCOLL16) == 0 && 365 retries < 15) { 366 retries++; 367 outb(iobase+EL_AC, EL_AC_HOST); 368 } 369 } else { 370 ifp->if_oerrors++; 371 break; 372 } 373 } else { 374 ifp->if_opackets++; 375 break; 376 } 377 } 378 379 /* 380 * Now give the card a chance to receive. 381 * Gotta love 3c501s... 382 */ 383 (void)inb(iobase+EL_AS); 384 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX); 385 splx(s); 386 /* Interrupt here. */ 387 s = splnet(); 388 } 389 390 (void)inb(iobase+EL_AS); 391 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX); 392 ifp->if_flags &= ~IFF_OACTIVE; 393 splx(s); 394 } 395 396 /* 397 * This function actually attempts to transmit a datagram downloaded to the 398 * board. Call at splnet or interrupt, after downloading data! Returns 0 on 399 * success, non-0 on failure. 400 */ 401 static int 402 el_xmit(sc) 403 struct el_softc *sc; 404 { 405 int iobase = sc->sc_iobase; 406 int i; 407 408 /* 409 * XXX 410 * This busy-waits for the tx completion. Can we get an interrupt 411 * instead? 412 */ 413 414 dprintf(("el: xmit...")); 415 outb(iobase+EL_AC, EL_AC_TXFRX); 416 i = 20000; 417 while ((inb(iobase+EL_AS) & EL_AS_TXBUSY) && (i > 0)) 418 i--; 419 if (i == 0) { 420 dprintf(("tx not ready\n")); 421 return -1; 422 } 423 dprintf(("%d cycles.\n", 20000 - i)); 424 return 0; 425 } 426 427 /* 428 * Controller interrupt. 429 */ 430 int 431 elintr(arg) 432 void *arg; 433 { 434 register struct el_softc *sc = arg; 435 int iobase = sc->sc_iobase; 436 int rxstat, len; 437 438 dprintf(("elintr: ")); 439 440 /* Check board status. */ 441 if ((inb(iobase+EL_AS) & EL_AS_RXBUSY) != 0) { 442 (void)inb(iobase+EL_RXC); 443 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX); 444 return 0; 445 } 446 447 for (;;) { 448 rxstat = inb(iobase+EL_RXS); 449 if (rxstat & EL_RXS_STALE) 450 break; 451 452 /* If there's an overflow, reinit the board. */ 453 if ((rxstat & EL_RXS_NOFLOW) == 0) { 454 dprintf(("overflow.\n")); 455 el_hardreset(sc); 456 /* Put board back into receive mode. */ 457 if (sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) 458 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_PROMISC); 459 else 460 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_ABROAD); 461 (void)inb(iobase+EL_AS); 462 outb(iobase+EL_RBC, 0); 463 break; 464 } 465 466 /* Incoming packet. */ 467 len = inb(iobase+EL_RBL); 468 len |= inb(iobase+EL_RBH) << 8; 469 dprintf(("receive len=%d rxstat=%x ", len, rxstat)); 470 outb(iobase+EL_AC, EL_AC_HOST); 471 472 /* Pass data up to upper levels. */ 473 elread(sc, len); 474 475 /* Is there another packet? */ 476 if ((inb(iobase+EL_AS) & EL_AS_RXBUSY) != 0) 477 break; 478 479 dprintf(("<rescan> ")); 480 } 481 482 (void)inb(iobase+EL_RXC); 483 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX); 484 return 1; 485 } 486 487 /* 488 * Pass a packet to the higher levels. 489 */ 490 void 491 elread(sc, len) 492 register struct el_softc *sc; 493 int len; 494 { 495 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 496 struct mbuf *m; 497 498 if (len <= sizeof(struct ether_header) || 499 len > ETHER_MAX_LEN) { 500 printf("%s: invalid packet size %d; dropping\n", 501 sc->sc_dev.dv_xname, len); 502 ifp->if_ierrors++; 503 return; 504 } 505 506 /* Pull packet off interface. */ 507 m = elget(sc, len); 508 if (m == 0) { 509 ifp->if_ierrors++; 510 return; 511 } 512 513 ifp->if_ipackets++; 514 515 #if NBPFILTER > 0 516 /* 517 * Check if there's a BPF listener on this interface. 518 * If so, hand off the raw packet to BPF. 519 */ 520 if (ifp->if_bpf) 521 bpf_mtap(ifp->if_bpf, m); 522 #endif 523 524 ether_input_mbuf(ifp, m); 525 } 526 527 /* 528 * Pull read data off a interface. Len is length of data, with local net 529 * header stripped. We copy the data into mbufs. When full cluster sized 530 * units are present we copy into clusters. 531 */ 532 struct mbuf * 533 elget(sc, totlen) 534 struct el_softc *sc; 535 int totlen; 536 { 537 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 538 int iobase = sc->sc_iobase; 539 struct mbuf *top, **mp, *m; 540 int len; 541 542 MGETHDR(m, M_DONTWAIT, MT_DATA); 543 if (m == 0) 544 return 0; 545 m->m_pkthdr.rcvif = ifp; 546 m->m_pkthdr.len = totlen; 547 len = MHLEN; 548 top = 0; 549 mp = ⊤ 550 551 outb(iobase+EL_GPBL, 0); 552 outb(iobase+EL_GPBH, 0); 553 554 while (totlen > 0) { 555 if (top) { 556 MGET(m, M_DONTWAIT, MT_DATA); 557 if (m == 0) { 558 m_freem(top); 559 return 0; 560 } 561 len = MLEN; 562 } 563 if (totlen >= MINCLSIZE) { 564 MCLGET(m, M_DONTWAIT); 565 if (m->m_flags & M_EXT) 566 len = MCLBYTES; 567 } 568 m->m_len = len = min(totlen, len); 569 insb(iobase+EL_BUF, mtod(m, caddr_t), len); 570 totlen -= len; 571 *mp = m; 572 mp = &m->m_next; 573 } 574 575 outb(iobase+EL_RBC, 0); 576 outb(iobase+EL_AC, EL_AC_RX); 577 578 return top; 579 } 580 581 /* 582 * Process an ioctl request. This code needs some work - it looks pretty ugly. 583 */ 584 int 585 elioctl(ifp, cmd, data) 586 register struct ifnet *ifp; 587 u_long cmd; 588 caddr_t data; 589 { 590 struct el_softc *sc = ifp->if_softc; 591 struct ifaddr *ifa = (struct ifaddr *)data; 592 int s, error = 0; 593 594 s = splnet(); 595 596 if ((error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data)) > 0) { 597 splx(s); 598 return error; 599 } 600 601 switch (cmd) { 602 603 case SIOCSIFADDR: 604 ifp->if_flags |= IFF_UP; 605 606 switch (ifa->ifa_addr->sa_family) { 607 #ifdef INET 608 case AF_INET: 609 elinit(sc); 610 arp_ifinit(&sc->sc_arpcom, ifa); 611 break; 612 #endif 613 default: 614 elinit(sc); 615 break; 616 } 617 break; 618 619 case SIOCSIFFLAGS: 620 if ((ifp->if_flags & IFF_UP) == 0 && 621 (ifp->if_flags & IFF_RUNNING) != 0) { 622 /* 623 * If interface is marked down and it is running, then 624 * stop it. 625 */ 626 elstop(sc); 627 ifp->if_flags &= ~IFF_RUNNING; 628 } else if ((ifp->if_flags & IFF_UP) != 0 && 629 (ifp->if_flags & IFF_RUNNING) == 0) { 630 /* 631 * If interface is marked up and it is stopped, then 632 * start it. 633 */ 634 elinit(sc); 635 } else { 636 /* 637 * Some other important flag might have changed, so 638 * reset. 639 */ 640 elreset(sc); 641 } 642 break; 643 644 default: 645 error = EINVAL; 646 break; 647 } 648 649 splx(s); 650 return error; 651 } 652 653 /* 654 * Device timeout routine. 655 */ 656 void 657 elwatchdog(ifp) 658 struct ifnet *ifp; 659 { 660 struct el_softc *sc = ifp->if_softc; 661 662 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname); 663 sc->sc_arpcom.ac_if.if_oerrors++; 664 665 elreset(sc); 666 } 667