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