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