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