1 /* $NetBSD: if_eg.c,v 1.38 1997/10/19 18:57:04 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1993 Dean Huxley <dean@fsa.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Dean Huxley. 18 * 4. The name of Dean Huxley may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /* 33 * Support for 3Com 3c505 Etherlink+ card. 34 */ 35 36 /* To do: 37 * - multicast 38 * - promiscuous 39 * - get rid of isa indirect stuff 40 */ 41 #include "bpfilter.h" 42 #include "rnd.h" 43 44 #include <sys/types.h> 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/mbuf.h> 48 #include <sys/socket.h> 49 #include <sys/ioctl.h> 50 #include <sys/errno.h> 51 #include <sys/syslog.h> 52 #include <sys/select.h> 53 #include <sys/device.h> 54 #if NRND > 0 55 #include <sys/rnd.h> 56 #endif 57 58 #include <net/if.h> 59 #include <net/if_dl.h> 60 #include <net/if_types.h> 61 62 #include <net/if_ether.h> 63 64 #ifdef INET 65 #include <netinet/in.h> 66 #include <netinet/in_systm.h> 67 #include <netinet/in_var.h> 68 #include <netinet/ip.h> 69 #include <netinet/if_inarp.h> 70 #endif 71 72 #ifdef NS 73 #include <netns/ns.h> 74 #include <netns/ns_if.h> 75 #endif 76 77 #if NBPFILTER > 0 78 #include <net/bpf.h> 79 #include <net/bpfdesc.h> 80 #endif 81 82 #include <machine/cpu.h> 83 #include <machine/intr.h> 84 #include <machine/bus.h> 85 86 #include <dev/isa/isavar.h> 87 #include <dev/isa/if_egreg.h> 88 #include <dev/isa/elink.h> 89 90 /* for debugging convenience */ 91 #ifdef EGDEBUG 92 #define DPRINTF(x) printf x 93 #else 94 #define DPRINTF(x) 95 #endif 96 97 #define ETHER_MIN_LEN 64 98 #define ETHER_MAX_LEN 1518 99 #define ETHER_ADDR_LEN 6 100 101 #define EG_INLEN 10 102 #define EG_BUFLEN 0x0670 103 104 /* 105 * Ethernet software status per interface. 106 */ 107 struct eg_softc { 108 struct device sc_dev; 109 void *sc_ih; 110 struct ethercom sc_ethercom; /* Ethernet common part */ 111 bus_space_tag_t sc_iot; /* bus space identifier */ 112 bus_space_handle_t sc_ioh; /* i/o handle */ 113 u_int8_t eg_rom_major; /* Cards ROM version (major number) */ 114 u_int8_t eg_rom_minor; /* Cards ROM version (minor number) */ 115 short eg_ram; /* Amount of RAM on the card */ 116 u_int8_t eg_pcb[64]; /* Primary Command Block buffer */ 117 u_int8_t eg_incount; /* Number of buffers currently used */ 118 caddr_t eg_inbuf; /* Incoming packet buffer */ 119 caddr_t eg_outbuf; /* Outgoing packet buffer */ 120 121 #if NRND > 0 122 rndsource_element_t rnd_source; 123 #endif 124 }; 125 126 int egprobe __P((struct device *, void *, void *)); 127 void egattach __P((struct device *, struct device *, void *)); 128 129 struct cfattach eg_ca = { 130 sizeof(struct eg_softc), egprobe, egattach 131 }; 132 133 struct cfdriver eg_cd = { 134 NULL, "eg", DV_IFNET 135 }; 136 137 int egintr __P((void *)); 138 void eginit __P((struct eg_softc *)); 139 int egioctl __P((struct ifnet *, u_long, caddr_t)); 140 void egrecv __P((struct eg_softc *)); 141 void egstart __P((struct ifnet *)); 142 void egwatchdog __P((struct ifnet *)); 143 void egreset __P((struct eg_softc *)); 144 void egread __P((struct eg_softc *, caddr_t, int)); 145 struct mbuf *egget __P((struct eg_softc *, caddr_t, int)); 146 void egstop __P((struct eg_softc *)); 147 148 static inline void egprintpcb __P((struct eg_softc *)); 149 static inline void egprintstat __P((u_char)); 150 static int egoutPCB __P((struct eg_softc *, u_int8_t)); 151 static int egreadPCBstat __P((struct eg_softc *, u_int8_t)); 152 static int egreadPCBready __P((struct eg_softc *)); 153 static int egwritePCB __P((struct eg_softc *)); 154 static int egreadPCB __P((struct eg_softc *)); 155 156 /* 157 * Support stuff 158 */ 159 160 static inline void 161 egprintpcb(sc) 162 struct eg_softc *sc; 163 { 164 int i; 165 166 for (i = 0; i < sc->eg_pcb[1] + 2; i++) 167 DPRINTF(("pcb[%2d] = %x\n", i, sc->eg_pcb[i])); 168 } 169 170 171 static inline void 172 egprintstat(b) 173 u_char b; 174 { 175 DPRINTF(("%s %s %s %s %s %s %s\n", 176 (b & EG_STAT_HCRE)?"HCRE":"", 177 (b & EG_STAT_ACRF)?"ACRF":"", 178 (b & EG_STAT_DIR )?"DIR ":"", 179 (b & EG_STAT_DONE)?"DONE":"", 180 (b & EG_STAT_ASF3)?"ASF3":"", 181 (b & EG_STAT_ASF2)?"ASF2":"", 182 (b & EG_STAT_ASF1)?"ASF1":"")); 183 } 184 185 static int 186 egoutPCB(sc, b) 187 struct eg_softc *sc; 188 u_int8_t b; 189 { 190 bus_space_tag_t iot = sc->sc_iot; 191 bus_space_handle_t ioh = sc->sc_ioh; 192 int i; 193 194 for (i=0; i < 4000; i++) { 195 if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HCRE) { 196 bus_space_write_1(iot, ioh, EG_COMMAND, b); 197 return 0; 198 } 199 delay(10); 200 } 201 DPRINTF(("egoutPCB failed\n")); 202 return 1; 203 } 204 205 static int 206 egreadPCBstat(sc, statb) 207 struct eg_softc *sc; 208 u_int8_t statb; 209 { 210 bus_space_tag_t iot = sc->sc_iot; 211 bus_space_handle_t ioh = sc->sc_ioh; 212 int i; 213 214 for (i=0; i < 5000; i++) { 215 if ((bus_space_read_1(iot, ioh, EG_STATUS) & 216 EG_PCB_STAT) != EG_PCB_NULL) 217 break; 218 delay(10); 219 } 220 if ((bus_space_read_1(iot, ioh, EG_STATUS) & EG_PCB_STAT) == statb) 221 return 0; 222 return 1; 223 } 224 225 static int 226 egreadPCBready(sc) 227 struct eg_softc *sc; 228 { 229 bus_space_tag_t iot = sc->sc_iot; 230 bus_space_handle_t ioh = sc->sc_ioh; 231 int i; 232 233 for (i=0; i < 10000; i++) { 234 if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF) 235 return 0; 236 delay(5); 237 } 238 DPRINTF(("PCB read not ready\n")); 239 return 1; 240 } 241 242 static int 243 egwritePCB(sc) 244 struct eg_softc *sc; 245 { 246 bus_space_tag_t iot = sc->sc_iot; 247 bus_space_handle_t ioh = sc->sc_ioh; 248 int i; 249 u_int8_t len; 250 251 bus_space_write_1(iot, ioh, EG_CONTROL, 252 (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_NULL); 253 254 len = sc->eg_pcb[1] + 2; 255 for (i = 0; i < len; i++) 256 egoutPCB(sc, sc->eg_pcb[i]); 257 258 for (i=0; i < 4000; i++) { 259 if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HCRE) 260 break; 261 delay(10); 262 } 263 264 bus_space_write_1(iot, ioh, EG_CONTROL, 265 (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_DONE); 266 267 egoutPCB(sc, len); 268 269 if (egreadPCBstat(sc, EG_PCB_ACCEPT)) 270 return 1; 271 return 0; 272 } 273 274 static int 275 egreadPCB(sc) 276 struct eg_softc *sc; 277 { 278 bus_space_tag_t iot = sc->sc_iot; 279 bus_space_handle_t ioh = sc->sc_ioh; 280 int i; 281 u_int8_t b; 282 283 bus_space_write_1(iot, ioh, EG_CONTROL, 284 (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_NULL); 285 286 bzero(sc->eg_pcb, sizeof(sc->eg_pcb)); 287 288 if (egreadPCBready(sc)) 289 return 1; 290 291 sc->eg_pcb[0] = bus_space_read_1(iot, ioh, EG_COMMAND); 292 293 if (egreadPCBready(sc)) 294 return 1; 295 296 sc->eg_pcb[1] = bus_space_read_1(iot, ioh, EG_COMMAND); 297 298 if (sc->eg_pcb[1] > 62) { 299 DPRINTF(("len %d too large\n", sc->eg_pcb[1])); 300 return 1; 301 } 302 303 for (i = 0; i < sc->eg_pcb[1]; i++) { 304 if (egreadPCBready(sc)) 305 return 1; 306 sc->eg_pcb[2+i] = bus_space_read_1(iot, ioh, EG_COMMAND); 307 } 308 if (egreadPCBready(sc)) 309 return 1; 310 if (egreadPCBstat(sc, EG_PCB_DONE)) 311 return 1; 312 if ((b = bus_space_read_1(iot, ioh, EG_COMMAND)) != sc->eg_pcb[1] + 2) { 313 DPRINTF(("%d != %d\n", b, sc->eg_pcb[1] + 2)); 314 return 1; 315 } 316 317 bus_space_write_1(iot, ioh, EG_CONTROL, 318 (bus_space_read_1(iot, ioh, EG_CONTROL) & 319 ~EG_PCB_STAT) | EG_PCB_ACCEPT); 320 321 return 0; 322 } 323 324 /* 325 * Real stuff 326 */ 327 328 int 329 egprobe(parent, match, aux) 330 struct device *parent; 331 void *match, *aux; 332 { 333 struct eg_softc *sc = match; 334 struct isa_attach_args *ia = aux; 335 bus_space_tag_t iot = ia->ia_iot; 336 bus_space_handle_t ioh; 337 int i, rval; 338 339 rval = 0; 340 341 if ((ia->ia_iobase & ~0x07f0) != 0) { 342 DPRINTF(("Weird iobase %x\n", ia->ia_iobase)); 343 return 0; 344 } 345 346 /* Disallow wildcarded i/o address. */ 347 if (ia->ia_iobase == ISACF_PORT_DEFAULT) 348 return (0); 349 350 /* Map i/o space. */ 351 if (bus_space_map(iot, ia->ia_iobase, 0x08, 0, &ioh)) { 352 DPRINTF(("egprobe: can't map i/o space in probe\n")); 353 return 0; 354 } 355 356 /* 357 * XXX Indirect brokenness. 358 */ 359 sc->sc_iot = iot; /* XXX */ 360 sc->sc_ioh = ioh; /* XXX */ 361 362 /* hard reset card */ 363 bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_RESET); 364 bus_space_write_1(iot, ioh, EG_CONTROL, 0); 365 for (i = 0; i < 5000; i++) { 366 delay(1000); 367 if ((bus_space_read_1(iot, ioh, EG_STATUS) & 368 EG_PCB_STAT) == EG_PCB_NULL) 369 break; 370 } 371 if ((bus_space_read_1(iot, ioh, EG_STATUS) & EG_PCB_STAT) != EG_PCB_NULL) { 372 DPRINTF(("egprobe: Reset failed\n")); 373 goto out; 374 } 375 sc->eg_pcb[0] = EG_CMD_GETINFO; /* Get Adapter Info */ 376 sc->eg_pcb[1] = 0; 377 if (egwritePCB(sc) != 0) 378 goto out; 379 380 if (egreadPCB(sc) != 0) { 381 egprintpcb(sc); 382 goto out; 383 } 384 385 if (sc->eg_pcb[0] != EG_RSP_GETINFO || /* Get Adapter Info Response */ 386 sc->eg_pcb[1] != 0x0a) { 387 egprintpcb(sc); 388 goto out; 389 } 390 sc->eg_rom_major = sc->eg_pcb[3]; 391 sc->eg_rom_minor = sc->eg_pcb[2]; 392 sc->eg_ram = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8); 393 394 ia->ia_iosize = 0x08; 395 ia->ia_msize = 0; 396 rval = 1; 397 398 out: 399 bus_space_unmap(iot, ioh, 0x08); 400 return rval; 401 } 402 403 void 404 egattach(parent, self, aux) 405 struct device *parent, *self; 406 void *aux; 407 { 408 struct eg_softc *sc = (void *)self; 409 struct isa_attach_args *ia = aux; 410 bus_space_tag_t iot = ia->ia_iot; 411 bus_space_handle_t ioh; 412 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 413 u_int8_t myaddr[ETHER_ADDR_LEN]; 414 415 printf("\n"); 416 417 /* Map i/o space. */ 418 if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) { 419 printf("%s: can't map i/o space\n", self->dv_xname); 420 return; 421 } 422 423 sc->sc_iot = iot; 424 sc->sc_ioh = ioh; 425 426 egstop(sc); 427 428 sc->eg_pcb[0] = EG_CMD_GETEADDR; /* Get Station address */ 429 sc->eg_pcb[1] = 0; 430 if (egwritePCB(sc) != 0) { 431 printf("%s: can't send Get Station Address\n", self->dv_xname); 432 return; 433 } 434 if (egreadPCB(sc) != 0) { 435 printf("%s: can't read station address\n", self->dv_xname); 436 egprintpcb(sc); 437 return; 438 } 439 440 /* check Get station address response */ 441 if (sc->eg_pcb[0] != EG_RSP_GETEADDR || sc->eg_pcb[1] != 0x06) { 442 printf("%s: card responded with garbage (1)\n", 443 self->dv_xname); 444 egprintpcb(sc); 445 return; 446 } 447 bcopy(&sc->eg_pcb[2], myaddr, ETHER_ADDR_LEN); 448 449 printf("%s: ROM v%d.%02d %dk address %s\n", self->dv_xname, 450 sc->eg_rom_major, sc->eg_rom_minor, sc->eg_ram, 451 ether_sprintf(myaddr)); 452 453 sc->eg_pcb[0] = EG_CMD_SETEADDR; /* Set station address */ 454 if (egwritePCB(sc) != 0) { 455 printf("%s: can't send Set Station Address\n", self->dv_xname); 456 return; 457 } 458 if (egreadPCB(sc) != 0) { 459 printf("%s: can't read Set Station Address status\n", 460 self->dv_xname); 461 egprintpcb(sc); 462 return; 463 } 464 if (sc->eg_pcb[0] != EG_RSP_SETEADDR || sc->eg_pcb[1] != 0x02 || 465 sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0) { 466 printf("%s: card responded with garbage (2)\n", 467 self->dv_xname); 468 egprintpcb(sc); 469 return; 470 } 471 472 /* Initialize ifnet structure. */ 473 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); 474 ifp->if_softc = sc; 475 ifp->if_start = egstart; 476 ifp->if_ioctl = egioctl; 477 ifp->if_watchdog = egwatchdog; 478 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS; 479 480 /* Now we can attach the interface. */ 481 if_attach(ifp); 482 ether_ifattach(ifp, myaddr); 483 484 #if NBPFILTER > 0 485 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); 486 #endif 487 488 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, 489 IPL_NET, egintr, sc); 490 491 #if NRND > 0 492 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET); 493 #endif 494 } 495 496 void 497 eginit(sc) 498 register struct eg_softc *sc; 499 { 500 register struct ifnet *ifp = &sc->sc_ethercom.ec_if; 501 bus_space_tag_t iot = sc->sc_iot; 502 bus_space_handle_t ioh = sc->sc_ioh; 503 504 /* soft reset the board */ 505 bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_FLSH); 506 delay(100); 507 bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_ATTN); 508 delay(100); 509 bus_space_write_1(iot, ioh, EG_CONTROL, 0); 510 delay(200); 511 512 sc->eg_pcb[0] = EG_CMD_CONFIG82586; /* Configure 82586 */ 513 sc->eg_pcb[1] = 2; 514 sc->eg_pcb[2] = 3; /* receive broadcast & multicast */ 515 sc->eg_pcb[3] = 0; 516 if (egwritePCB(sc) != 0) 517 printf("%s: can't send Configure 82586\n", 518 sc->sc_dev.dv_xname); 519 520 if (egreadPCB(sc) != 0) { 521 printf("%s: can't read Configure 82586 status\n", 522 sc->sc_dev.dv_xname); 523 egprintpcb(sc); 524 } else if (sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0) 525 printf("%s: configure card command failed\n", 526 sc->sc_dev.dv_xname); 527 528 if (sc->eg_inbuf == NULL) { 529 sc->eg_inbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT); 530 if (sc->eg_inbuf == NULL) { 531 printf("%s: can't allocate inbuf\n", 532 sc->sc_dev.dv_xname); 533 panic("eginit"); 534 } 535 } 536 sc->eg_incount = 0; 537 538 if (sc->eg_outbuf == NULL) { 539 sc->eg_outbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT); 540 if (sc->eg_outbuf == NULL) { 541 printf("%s: can't allocate outbuf\n", 542 sc->sc_dev.dv_xname); 543 panic("eginit"); 544 } 545 } 546 547 bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_CMDE); 548 549 sc->eg_incount = 0; 550 egrecv(sc); 551 552 /* Interface is now `running', with no output active. */ 553 ifp->if_flags |= IFF_RUNNING; 554 ifp->if_flags &= ~IFF_OACTIVE; 555 556 /* Attempt to start output, if any. */ 557 egstart(ifp); 558 } 559 560 void 561 egrecv(sc) 562 struct eg_softc *sc; 563 { 564 565 while (sc->eg_incount < EG_INLEN) { 566 sc->eg_pcb[0] = EG_CMD_RECVPACKET; 567 sc->eg_pcb[1] = 0x08; 568 sc->eg_pcb[2] = 0; /* address not used.. we send zero */ 569 sc->eg_pcb[3] = 0; 570 sc->eg_pcb[4] = 0; 571 sc->eg_pcb[5] = 0; 572 sc->eg_pcb[6] = EG_BUFLEN & 0xff; /* our buffer size */ 573 sc->eg_pcb[7] = (EG_BUFLEN >> 8) & 0xff; 574 sc->eg_pcb[8] = 0; /* timeout, 0 == none */ 575 sc->eg_pcb[9] = 0; 576 if (egwritePCB(sc) != 0) 577 break; 578 sc->eg_incount++; 579 } 580 } 581 582 void 583 egstart(ifp) 584 struct ifnet *ifp; 585 { 586 register struct eg_softc *sc = ifp->if_softc; 587 bus_space_tag_t iot = sc->sc_iot; 588 bus_space_handle_t ioh = sc->sc_ioh; 589 struct mbuf *m0, *m; 590 caddr_t buffer; 591 int len; 592 u_int16_t *ptr; 593 594 /* Don't transmit if interface is busy or not running */ 595 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 596 return; 597 598 loop: 599 /* Dequeue the next datagram. */ 600 IF_DEQUEUE(&ifp->if_snd, m0); 601 if (m0 == 0) 602 return; 603 604 ifp->if_flags |= IFF_OACTIVE; 605 606 /* We need to use m->m_pkthdr.len, so require the header */ 607 if ((m0->m_flags & M_PKTHDR) == 0) { 608 printf("%s: no header mbuf\n", sc->sc_dev.dv_xname); 609 panic("egstart"); 610 } 611 len = max(m0->m_pkthdr.len, ETHER_MIN_LEN); 612 613 #if NBPFILTER > 0 614 if (ifp->if_bpf) 615 bpf_mtap(ifp->if_bpf, m0); 616 #endif 617 618 sc->eg_pcb[0] = EG_CMD_SENDPACKET; 619 sc->eg_pcb[1] = 0x06; 620 sc->eg_pcb[2] = 0; /* address not used, we send zero */ 621 sc->eg_pcb[3] = 0; 622 sc->eg_pcb[4] = 0; 623 sc->eg_pcb[5] = 0; 624 sc->eg_pcb[6] = len; /* length of packet */ 625 sc->eg_pcb[7] = len >> 8; 626 if (egwritePCB(sc) != 0) { 627 printf("%s: can't send Send Packet command\n", 628 sc->sc_dev.dv_xname); 629 ifp->if_oerrors++; 630 ifp->if_flags &= ~IFF_OACTIVE; 631 m_freem(m0); 632 goto loop; 633 } 634 635 buffer = sc->eg_outbuf; 636 for (m = m0; m != 0; m = m->m_next) { 637 bcopy(mtod(m, caddr_t), buffer, m->m_len); 638 buffer += m->m_len; 639 } 640 641 /* set direction bit: host -> adapter */ 642 bus_space_write_1(iot, ioh, EG_CONTROL, 643 bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_CTL_DIR); 644 645 for (ptr = (u_int16_t *) sc->eg_outbuf; len > 0; len -= 2) { 646 bus_space_write_2(iot, ioh, EG_DATA, *ptr++); 647 while (!(bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HRDY)) 648 ; /* XXX need timeout here */ 649 } 650 651 m_freem(m0); 652 } 653 654 int 655 egintr(arg) 656 void *arg; 657 { 658 register struct eg_softc *sc = arg; 659 bus_space_tag_t iot = sc->sc_iot; 660 bus_space_handle_t ioh = sc->sc_ioh; 661 int i, len, serviced; 662 u_int16_t *ptr; 663 664 serviced = 0; 665 666 while (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF) { 667 egreadPCB(sc); 668 switch (sc->eg_pcb[0]) { 669 case EG_RSP_RECVPACKET: 670 len = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8); 671 672 /* Set direction bit : Adapter -> host */ 673 bus_space_write_1(iot, ioh, EG_CONTROL, 674 bus_space_read_1(iot, ioh, EG_CONTROL) | EG_CTL_DIR); 675 676 for (ptr = (u_int16_t *) sc->eg_inbuf; 677 len > 0; len -= 2) { 678 while (!(bus_space_read_1(iot, ioh, EG_STATUS) & 679 EG_STAT_HRDY)) 680 ; 681 *ptr++ = bus_space_read_2(iot, ioh, EG_DATA); 682 } 683 684 len = sc->eg_pcb[8] | (sc->eg_pcb[9] << 8); 685 egread(sc, sc->eg_inbuf, len); 686 687 sc->eg_incount--; 688 egrecv(sc); 689 serviced = 1; 690 break; 691 692 case EG_RSP_SENDPACKET: 693 if (sc->eg_pcb[6] || sc->eg_pcb[7]) { 694 DPRINTF(("%s: packet dropped\n", 695 sc->sc_dev.dv_xname)); 696 sc->sc_ethercom.ec_if.if_oerrors++; 697 } else 698 sc->sc_ethercom.ec_if.if_opackets++; 699 sc->sc_ethercom.ec_if.if_collisions += 700 sc->eg_pcb[8] & 0xf; 701 sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE; 702 egstart(&sc->sc_ethercom.ec_if); 703 serviced = 1; 704 break; 705 706 /* XXX byte-order and type-size bugs here... */ 707 case EG_RSP_GETSTATS: 708 DPRINTF(("%s: Card Statistics\n", 709 sc->sc_dev.dv_xname)); 710 bcopy(&sc->eg_pcb[2], &i, sizeof(i)); 711 DPRINTF(("Receive Packets %d\n", i)); 712 bcopy(&sc->eg_pcb[6], &i, sizeof(i)); 713 DPRINTF(("Transmit Packets %d\n", i)); 714 DPRINTF(("CRC errors %d\n", 715 *(short *) &sc->eg_pcb[10])); 716 DPRINTF(("alignment errors %d\n", 717 *(short *) &sc->eg_pcb[12])); 718 DPRINTF(("no resources errors %d\n", 719 *(short *) &sc->eg_pcb[14])); 720 DPRINTF(("overrun errors %d\n", 721 *(short *) &sc->eg_pcb[16])); 722 serviced = 1; 723 break; 724 725 default: 726 printf("%s: egintr: Unknown response %x??\n", 727 sc->sc_dev.dv_xname, sc->eg_pcb[0]); 728 egprintpcb(sc); 729 break; 730 } 731 732 #if NRND > 0 733 rnd_add_uint32(&sc->rnd_source, sc->eg_pcb[0]); 734 #endif 735 } 736 737 return serviced; 738 } 739 740 /* 741 * Pass a packet up to the higher levels. 742 */ 743 void 744 egread(sc, buf, len) 745 struct eg_softc *sc; 746 caddr_t buf; 747 int len; 748 { 749 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 750 struct mbuf *m; 751 struct ether_header *eh; 752 753 if (len <= sizeof(struct ether_header) || 754 len > ETHER_MAX_LEN) { 755 printf("%s: invalid packet size %d; dropping\n", 756 sc->sc_dev.dv_xname, len); 757 ifp->if_ierrors++; 758 return; 759 } 760 761 /* Pull packet off interface. */ 762 m = egget(sc, buf, len); 763 if (m == 0) { 764 ifp->if_ierrors++; 765 return; 766 } 767 768 ifp->if_ipackets++; 769 770 /* We assume the header fit entirely in one mbuf. */ 771 eh = mtod(m, struct ether_header *); 772 773 #if NBPFILTER > 0 774 /* 775 * Check if there's a BPF listener on this interface. 776 * If so, hand off the raw packet to BPF. 777 */ 778 if (ifp->if_bpf) { 779 bpf_mtap(ifp->if_bpf, m); 780 781 /* 782 * Note that the interface cannot be in promiscuous mode if 783 * there are no BPF listeners. And if we are in promiscuous 784 * mode, we have to check if this packet is really ours. 785 */ 786 if ((ifp->if_flags & IFF_PROMISC) && 787 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */ 788 bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl), 789 sizeof(eh->ether_dhost)) != 0) { 790 m_freem(m); 791 return; 792 } 793 } 794 #endif 795 796 /* We assume the header fit entirely in one mbuf. */ 797 m_adj(m, sizeof(struct ether_header)); 798 ether_input(ifp, eh, m); 799 } 800 801 /* 802 * convert buf into mbufs 803 */ 804 struct mbuf * 805 egget(sc, buf, totlen) 806 struct eg_softc *sc; 807 caddr_t buf; 808 int totlen; 809 { 810 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 811 struct mbuf *top, **mp, *m; 812 int len; 813 814 MGETHDR(m, M_DONTWAIT, MT_DATA); 815 if (m == 0) 816 return 0; 817 m->m_pkthdr.rcvif = ifp; 818 m->m_pkthdr.len = totlen; 819 len = MHLEN; 820 top = 0; 821 mp = ⊤ 822 823 while (totlen > 0) { 824 if (top) { 825 MGET(m, M_DONTWAIT, MT_DATA); 826 if (m == 0) { 827 m_freem(top); 828 return 0; 829 } 830 len = MLEN; 831 } 832 if (totlen >= MINCLSIZE) { 833 MCLGET(m, M_DONTWAIT); 834 if ((m->m_flags & M_EXT) == 0) { 835 m_free(m); 836 m_freem(top); 837 return 0; 838 } 839 len = MCLBYTES; 840 } 841 m->m_len = len = min(totlen, len); 842 bcopy((caddr_t)buf, mtod(m, caddr_t), len); 843 buf += len; 844 totlen -= len; 845 *mp = m; 846 mp = &m->m_next; 847 } 848 849 return top; 850 } 851 852 int 853 egioctl(ifp, cmd, data) 854 register struct ifnet *ifp; 855 u_long cmd; 856 caddr_t data; 857 { 858 struct eg_softc *sc = ifp->if_softc; 859 struct ifaddr *ifa = (struct ifaddr *)data; 860 int s, error = 0; 861 862 s = splnet(); 863 864 switch (cmd) { 865 866 case SIOCSIFADDR: 867 ifp->if_flags |= IFF_UP; 868 869 switch (ifa->ifa_addr->sa_family) { 870 #ifdef INET 871 case AF_INET: 872 eginit(sc); 873 arp_ifinit(ifp, ifa); 874 break; 875 #endif 876 #ifdef NS 877 case AF_NS: 878 { 879 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 880 881 if (ns_nullhost(*ina)) 882 ina->x_host = 883 *(union ns_host *)LLADDR(ifp->if_sadl); 884 else 885 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl), 886 ETHER_ADDR_LEN); 887 /* Set new address. */ 888 eginit(sc); 889 break; 890 } 891 #endif 892 default: 893 eginit(sc); 894 break; 895 } 896 break; 897 898 case SIOCSIFFLAGS: 899 if ((ifp->if_flags & IFF_UP) == 0 && 900 (ifp->if_flags & IFF_RUNNING) != 0) { 901 /* 902 * If interface is marked down and it is running, then 903 * stop it. 904 */ 905 egstop(sc); 906 ifp->if_flags &= ~IFF_RUNNING; 907 } else if ((ifp->if_flags & IFF_UP) != 0 && 908 (ifp->if_flags & IFF_RUNNING) == 0) { 909 /* 910 * If interface is marked up and it is stopped, then 911 * start it. 912 */ 913 eginit(sc); 914 } else { 915 sc->eg_pcb[0] = EG_CMD_GETSTATS; 916 sc->eg_pcb[1] = 0; 917 if (egwritePCB(sc) != 0) 918 DPRINTF(("write error\n")); 919 /* 920 * XXX deal with flags changes: 921 * IFF_MULTICAST, IFF_PROMISC, 922 * IFF_LINK0, IFF_LINK1, 923 */ 924 } 925 break; 926 927 default: 928 error = EINVAL; 929 break; 930 } 931 932 splx(s); 933 return error; 934 } 935 936 void 937 egreset(sc) 938 struct eg_softc *sc; 939 { 940 int s; 941 942 DPRINTF(("%s: egreset()\n", sc->sc_dev.dv_xname)); 943 s = splnet(); 944 egstop(sc); 945 eginit(sc); 946 splx(s); 947 } 948 949 void 950 egwatchdog(ifp) 951 struct ifnet *ifp; 952 { 953 struct eg_softc *sc = ifp->if_softc; 954 955 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname); 956 sc->sc_ethercom.ec_if.if_oerrors++; 957 958 egreset(sc); 959 } 960 961 void 962 egstop(sc) 963 register struct eg_softc *sc; 964 { 965 966 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EG_CONTROL, 0); 967 } 968