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