1 /* 2 * Device driver for National Semiconductor DS8390 based ethernet adapters. 3 * 4 * Based on original ISA bus driver by David Greenman, 29-April-1993 5 * 6 * Copyright (C) 1993, David Greenman. This software may be used, modified, 7 * copied, distributed, and sold, in both source and binary form provided 8 * that the above copyright and these terms are retained. Under no 9 * circumstances is the author responsible for the proper functioning 10 * of this software, nor does the author assume any responsibility 11 * for damages incurred with its use. 12 * 13 * Adapted for MacBSD by Brad Parker <brad@fcr.com> 14 * 15 * Currently supports: 16 * Apples NB Ethernet card 17 * Interlan A310 Nubus Ethernet card 18 * Cayman Systems GatorCard 19 */ 20 21 /* 22 * $Id: if_ae.c,v 1.1 1993/11/29 00:32:43 briggs Exp $ 23 */ 24 25 /* 26 * Modification history 27 * 28 * $Log: if_ae.c,v $ 29 * Revision 1.1 1993/11/29 00:32:43 briggs 30 * Update to current work in progress. This includes an update to 31 * use config.new. 32 * Numerous updates to console so it works better on the SE/30 screen. 33 * Some nice changes from Brad Parker for handling NuBUS and an ethernet 34 * driver that I haven't worked on, yet. 35 * 36 * 37 */ 38 39 #include "ae.h" 40 #if NAE > 0 41 /* bpfilter included here in case it is needed in future net includes */ 42 #include "bpfilter.h" 43 44 #include "param.h" 45 #include "systm.h" 46 #include "errno.h" 47 #include "ioctl.h" 48 #include "mbuf.h" 49 #include "socket.h" 50 #include "syslog.h" 51 52 #include "net/if.h" 53 #include "net/if_dl.h" 54 #include "net/if_types.h" 55 #include "net/netisr.h" 56 57 #ifdef INET 58 #include "netinet/in.h" 59 #include "netinet/in_systm.h" 60 #include "netinet/in_var.h" 61 #include "netinet/ip.h" 62 #include "netinet/if_ether.h" 63 #endif 64 65 #ifdef NS 66 #include "netns/ns.h" 67 #include "netns/ns_if.h" 68 #endif 69 70 #if NBPFILTER > 0 71 #include "net/bpf.h" 72 #include "net/bpfdesc.h" 73 #endif 74 75 #include "device.h" 76 #include "if_aereg.h" 77 78 /* For backwards compatibility */ 79 #ifndef IFF_ALTPHYS 80 #define IFF_ALTPHYS IFF_LLC0 81 #endif 82 83 /* 84 * ae_softc: per line info and status 85 */ 86 struct ae_softc { 87 struct arpcom arpcom; /* ethernet common */ 88 89 char *type_str; /* pointer to type string */ 90 u_char vendor; /* interface vendor */ 91 u_char type; /* interface type code */ 92 u_char slot; /* slot (0-f) */ 93 #define APPLE_CARD(sc) ((sc)->vendor == AE_VENDOR_APPLE) 94 #define REG_MAP(sc, reg) (APPLE_CARD(sc) ? (0x0f-(reg))<<2 : (reg)<<2) 95 #define NIC_GET(sc, reg) ((sc)->nic_addr[REG_MAP(sc, reg)]) 96 #define NIC_PUT(sc, reg, val) ((sc)->nic_addr[REG_MAP(sc, reg)] = (val)) 97 volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */ 98 caddr_t rom_addr; /* on board prom address */ 99 caddr_t smem_start; /* shared memory start address */ 100 caddr_t smem_end; /* shared memory end address */ 101 u_long smem_size; /* total shared memory size */ 102 caddr_t smem_ring; /* start of RX ring-buffer (in smem) */ 103 104 caddr_t bpf; /* BPF "magic cookie" */ 105 106 u_char xmit_busy; /* transmitter is busy */ 107 u_char txb_cnt; /* Number of transmit buffers */ 108 u_char txb_next; /* Pointer to next buffer ready to xmit */ 109 u_short txb_next_len; /* next xmit buffer length */ 110 u_char data_buffered; /* data has been buffered in interface memory */ 111 u_char tx_page_start; /* first page of TX buffer area */ 112 113 u_char rec_page_start; /* first page of RX ring-buffer */ 114 u_char rec_page_stop; /* last page of RX ring-buffer */ 115 u_char next_packet; /* pointer to next unread RX packet */ 116 } ae_softc[NAE]; 117 118 void ae_find(); 119 int ae_attach(), ae_init(), aeintr(), ae_ioctl(), ae_probe(), 120 ae_start(), ae_reset(), ae_watchdog(); 121 122 static void ae_stop(); 123 static inline void ae_rint(); 124 static inline void ae_xmit(); 125 static inline char *ae_ring_copy(); 126 127 extern int ether_output(); 128 129 /* 130 struct isa_driver eddriver = { 131 ae_probe, 132 ae_attach, 133 "ae" 134 }; 135 */ 136 137 #define ETHER_MIN_LEN 64 138 #define ETHER_MAX_LEN 1518 139 #define ETHER_ADDR_LEN 6 140 #define ETHER_HDR_SIZE 14 141 142 extern struct nubus_hw nubus_table[MAXSLOTS]; 143 char ae_name[] = "8390 Nubus Ethernet card"; 144 static char zero = 0; 145 static u_char ones = 0xff; 146 147 148 void 149 ae_find(struct macdriver *md) 150 { 151 int slot; 152 153 for (slot = 0; slot < MAXSLOTS; slot++) 154 { 155 register struct nubus_hw *nu = &nubus_table[slot]; 156 157 if (!nu->found || nu->claimed) 158 continue; 159 160 if (nu->Slot.type == NUBUS_NETWORK) { 161 printf("ae: found network card; slot %d, type 0x%x\n", slot, nu->Slot.type); 162 163 if (ae_probe(md, slot)) { 164 if (!md->hwfound) { 165 md->hwfound = 1; 166 md->name = ae_name; 167 } 168 nu->claimed = 1; 169 ae_attach(md); 170 } 171 172 break; 173 } 174 } 175 } 176 177 int 178 ae_probe(struct macdriver *md, int slot) 179 { 180 register struct nubus_hw *nu = &nubus_table[slot]; 181 struct ae_softc *sc = &ae_softc[md->unit]; 182 int i, memsize; 183 int flags = 0; 184 185 /* 186 * Try to determine what type of card this is... 187 */ 188 sc->vendor == AE_VENDOR_APPLE; 189 190 /* see if it's an Interlan/GatorCard */ 191 sc->rom_addr = nu->addr + GC_ROM_OFFSET; 192 if (sc->rom_addr[0x18] == 0x0 && 193 sc->rom_addr[0x1c] == 0x55) { 194 sc->vendor = AE_VENDOR_INTERLAN; 195 printf("found interlan card in slot %x\n", slot); 196 } 197 198 sc->type = 0; 199 sc->slot = slot; 200 201 switch (sc->vendor) { 202 case AE_VENDOR_INTERLAN: 203 sc->nic_addr = nu->addr + GC_NIC_OFFSET; 204 sc->rom_addr = nu->addr + GC_ROM_OFFSET; 205 sc->smem_start = nu->addr + GC_DATA_OFFSET; 206 sc->type_str = "Interlan"; 207 memsize = 8192; 208 209 /* reset the NIC chip */ 210 *((caddr_t)nu->addr + GC_RESET_OFFSET) = (char)zero; 211 212 /* Get station address from on-board ROM */ 213 for (i = 0; i < ETHER_ADDR_LEN; ++i) 214 sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*4); 215 break; 216 217 case AE_VENDOR_APPLE: 218 default: 219 sc->nic_addr = nu->addr + AE_NIC_OFFSET; 220 sc->rom_addr = nu->addr + AE_ROM_OFFSET; 221 sc->smem_start = nu->addr + AE_DATA_OFFSET; 222 sc->type_str = "Apple"; 223 memsize = 8192; 224 225 /* Get station address from on-board ROM */ 226 for (i = 0; i < ETHER_ADDR_LEN; ++i) 227 sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2); 228 break; 229 } 230 231 /* 232 * allocate one xmit buffer if < 16k, two buffers otherwise 233 */ 234 if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING)) { 235 sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE); 236 sc->txb_cnt = 1; 237 sc->rec_page_start = AE_TXBUF_SIZE; 238 } else { 239 sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE * 2); 240 sc->txb_cnt = 2; 241 sc->rec_page_start = AE_TXBUF_SIZE * 2; 242 } 243 244 sc->smem_size = memsize; 245 sc->smem_end = sc->smem_start + memsize; 246 sc->rec_page_stop = memsize / AE_PAGE_SIZE; 247 sc->tx_page_start = 0; 248 249 /* 250 * Now zero memory and verify that it is clear 251 */ 252 bzero(sc->smem_start, memsize); 253 254 for (i = 0; i < memsize; ++i) 255 if (sc->smem_start[i]) { 256 printf("ae%d: failed to clear shared memory at %x\n", 257 md->unit, sc->smem_start + i); 258 259 return(0); 260 } 261 262 #ifdef DEBUG_PRINT 263 printf("nic_addr %x, rom_addr %x\n", 264 sc->nic_addr, sc->rom_addr); 265 printf("smem_size %d\n", sc->smem_size); 266 printf("smem_start %x, smem_ring %x, smem_end %x\n", 267 sc->smem_start, sc->smem_ring, sc->smem_end); 268 printf("phys address %02x:%02x:%02x:%02x:%02x:%02x\n", 269 sc->arpcom.ac_enaddr[0], 270 sc->arpcom.ac_enaddr[1], 271 sc->arpcom.ac_enaddr[2], 272 sc->arpcom.ac_enaddr[3], 273 sc->arpcom.ac_enaddr[4], 274 sc->arpcom.ac_enaddr[5]); 275 #endif 276 277 return(1); 278 } 279 280 /* 281 * Install interface into kernel networking data structures 282 */ 283 int 284 ae_attach(struct macdriver *md) 285 { 286 struct ae_softc *sc = &ae_softc[md->unit]; 287 struct ifnet *ifp = &sc->arpcom.ac_if; 288 struct ifaddr *ifa; 289 struct sockaddr_dl *sdl; 290 291 /* 292 * Set interface to stopped condition (reset) 293 */ 294 ae_stop(md->unit); 295 296 /* 297 * Initialize ifnet structure 298 */ 299 ifp->if_unit = md->unit; 300 ifp->if_name = "ae"; 301 ifp->if_mtu = ETHERMTU; 302 ifp->if_init = ae_init; 303 ifp->if_output = ether_output; 304 ifp->if_start = ae_start; 305 ifp->if_ioctl = ae_ioctl; 306 ifp->if_reset = ae_reset; 307 ifp->if_watchdog = ae_watchdog; 308 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS); 309 310 /* 311 * Attach the interface 312 */ 313 if_attach(ifp); 314 315 /* 316 * Search down the ifa address list looking for the AF_LINK type entry 317 */ 318 ifa = ifp->if_addrlist; 319 while ((ifa != 0) && (ifa->ifa_addr != 0) && 320 (ifa->ifa_addr->sa_family != AF_LINK)) 321 ifa = ifa->ifa_next; 322 /* 323 * If we find an AF_LINK type entry we fill in the hardware address. 324 * This is useful for netstat(1) to keep track of which interface 325 * is which. 326 */ 327 if ((ifa != 0) && (ifa->ifa_addr != 0)) { 328 /* 329 * Fill in the link-level address for this interface 330 */ 331 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 332 sdl->sdl_type = IFT_ETHER; 333 sdl->sdl_alen = ETHER_ADDR_LEN; 334 sdl->sdl_slen = 0; 335 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN); 336 } 337 338 /* 339 * Print additional info when attached 340 */ 341 printf("ae%d: address %s, ", md->unit, 342 ether_sprintf(sc->arpcom.ac_enaddr)); 343 344 if (sc->type_str && (*sc->type_str != 0)) 345 printf("type %s ", sc->type_str); 346 else 347 printf("type unknown (0x%x) ", sc->type); 348 349 printf("\n"); 350 351 /* 352 * If BPF is in the kernel, call the attach for it 353 */ 354 #if NBPFILTER > 0 355 bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); 356 #endif 357 358 } 359 360 /* 361 * Reset interface. 362 */ 363 int 364 ae_reset(unit) 365 int unit; 366 { 367 int s; 368 369 s = splnet(); 370 371 /* 372 * Stop interface and re-initialize. 373 */ 374 ae_stop(unit); 375 ae_init(unit); 376 377 (void) splx(s); 378 } 379 380 /* 381 * Take interface offline. 382 */ 383 void 384 ae_stop(unit) 385 int unit; 386 { 387 struct ae_softc *sc = &ae_softc[unit]; 388 int n = 5000; 389 390 /* 391 * Stop everything on the interface, and select page 0 registers. 392 */ 393 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP); 394 395 /* 396 * Wait for interface to enter stopped state, but limit # of checks 397 * to 'n' (about 5ms). It shouldn't even take 5us on modern 398 * DS8390's, but just in case it's an old one. 399 */ 400 while (((NIC_GET(sc, AE_P0_ISR) & AE_ISR_RST) == 0) && --n); 401 } 402 403 /* 404 * Device timeout/watchdog routine. Entered if the device neglects to 405 * generate an interrupt after a transmit has been started on it. 406 */ 407 int 408 ae_watchdog(unit) 409 int unit; 410 { 411 log(LOG_ERR, "ae%d: device timeout\n", unit); 412 { 413 struct ae_softc *sc = &ae_softc[unit]; 414 printf("cr %x, isr %x\n", NIC_GET(sc, AE_P0_CR), NIC_GET(sc, AE_P0_ISR)); 415 via_dump(); 416 if (NIC_GET(sc, AE_P0_ISR)) { 417 aeintr(0); 418 return; 419 } 420 } 421 ae_reset(unit); 422 } 423 424 /* 425 * Initialize device. 426 */ 427 ae_init(unit) 428 int unit; 429 { 430 struct ae_softc *sc = &ae_softc[unit]; 431 struct ifnet *ifp = &sc->arpcom.ac_if; 432 int i, s; 433 u_char command; 434 435 436 /* address not known */ 437 if (ifp->if_addrlist == (struct ifaddr *)0) return; 438 439 /* 440 * Initialize the NIC in the exact order outlined in the NS manual. 441 * This init procedure is "mandatory"...don't change what or when 442 * things happen. 443 */ 444 s = splnet(); 445 446 /* reset transmitter flags */ 447 sc->data_buffered = 0; 448 sc->xmit_busy = 0; 449 sc->arpcom.ac_if.if_timer = 0; 450 451 sc->txb_next = 0; 452 453 /* This variable is used below - don't move this assignment */ 454 sc->next_packet = sc->rec_page_start + 1; 455 456 #ifdef DEBUG_PRINT 457 printf("page_start %d, page_stop %d, next %d\n", 458 sc->rec_page_start, sc->rec_page_stop, sc->next_packet); 459 #endif 460 461 /* 462 * Set interface for page 0, Remote DMA complete, Stopped 463 */ 464 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP); 465 466 /* 467 * Set FIFO threshold to 4, No auto-init Remote DMA, Burst mode, 468 * byte order=80x86, word-wide DMA xfers, 469 */ 470 NIC_PUT(sc, AE_P0_DCR, AE_DCR_FT1|AE_DCR_BMS|AE_DCR_WTS); 471 472 /* 473 * Clear Remote Byte Count Registers 474 */ 475 NIC_PUT(sc, AE_P0_RBCR0, zero); 476 NIC_PUT(sc, AE_P0_RBCR1, zero); 477 478 /* 479 * Enable reception of broadcast packets 480 */ 481 NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB); 482 483 /* 484 * Place NIC in internal loopback mode 485 */ 486 NIC_PUT(sc, AE_P0_TCR, AE_TCR_LB0); 487 488 /* 489 * Initialize transmit/receive (ring-buffer) Page Start 490 */ 491 NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start); 492 NIC_PUT(sc, AE_P0_PSTART, sc->rec_page_start); 493 494 /* 495 * Initialize Receiver (ring-buffer) Page Stop and Boundry 496 */ 497 NIC_PUT(sc, AE_P0_PSTOP, sc->rec_page_stop); 498 NIC_PUT(sc, AE_P0_BNRY, sc->rec_page_start); 499 500 /* 501 * Clear all interrupts. A '1' in each bit position clears the 502 * corresponding flag. 503 */ 504 NIC_PUT(sc, AE_P0_ISR, ones); 505 506 /* 507 * Enable the following interrupts: receive/transmit complete, 508 * receive/transmit error, and Receiver OverWrite. 509 * 510 * Counter overflow and Remote DMA complete are *not* enabled. 511 */ 512 NIC_PUT(sc, AE_P0_IMR, 513 AE_IMR_PRXE|AE_IMR_PTXE|AE_IMR_RXEE|AE_IMR_TXEE|AE_IMR_OVWE); 514 515 /* 516 * Program Command Register for page 1 517 */ 518 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STP); 519 520 /* 521 * Copy out our station address 522 */ 523 for (i = 0; i < ETHER_ADDR_LEN; ++i) 524 NIC_PUT(sc, AE_P1_PAR0 + i, sc->arpcom.ac_enaddr[i]); 525 526 #if NBPFILTER > 0 527 /* 528 * Initialize multicast address hashing registers to accept 529 * all multicasts (only used when in promiscuous mode) 530 */ 531 for (i = 0; i < 8; ++i) 532 NIC_PUT(sc, AE_P1_MAR0 + i, 0xff); 533 #endif 534 535 /* 536 * Set Current Page pointer to next_packet (initialized above) 537 */ 538 NIC_PUT(sc, AE_P1_CURR, sc->next_packet); 539 540 /* 541 * Set Command Register for page 0, Remote DMA complete, 542 * and interface Start. 543 */ 544 NIC_PUT(sc, AE_P1_CR, AE_CR_RD2|AE_CR_STA); 545 546 /* 547 * Take interface out of loopback 548 */ 549 NIC_PUT(sc, AE_P0_TCR, zero); 550 551 /* 552 * Set 'running' flag, and clear output active flag. 553 */ 554 ifp->if_flags |= IFF_RUNNING; 555 ifp->if_flags &= ~IFF_OACTIVE; 556 557 /* */ 558 add_nubus_intr(sc->slot, aeintr, sc - ae_softc); 559 560 /* 561 * ...and attempt to start output 562 */ 563 ae_start(ifp); 564 565 (void) splx(s); 566 } 567 568 /* 569 * This routine actually starts the transmission on the interface 570 */ 571 static inline void ae_xmit(ifp) 572 struct ifnet *ifp; 573 { 574 struct ae_softc *sc = &ae_softc[ifp->if_unit]; 575 u_short len = sc->txb_next_len; 576 577 /* 578 * Set NIC for page 0 register access 579 */ 580 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA); 581 582 /* 583 * Set TX buffer start page 584 */ 585 NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start + 586 sc->txb_next * AE_TXBUF_SIZE); 587 588 /* 589 * Set TX length 590 */ 591 NIC_PUT(sc, AE_P0_TBCR0, len & 0xff); 592 NIC_PUT(sc, AE_P0_TBCR1, len >> 8); 593 594 /* 595 * Set page 0, Remote DMA complete, Transmit Packet, and *Start* 596 */ 597 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_TXP|AE_CR_STA); 598 599 sc->xmit_busy = 1; 600 sc->data_buffered = 0; 601 602 /* 603 * Switch buffers if we are doing double-buffered transmits 604 */ 605 if ((sc->txb_next == 0) && (sc->txb_cnt > 1)) 606 sc->txb_next = 1; 607 else 608 sc->txb_next = 0; 609 610 /* 611 * Set a timer just in case we never hear from the board again 612 */ 613 ifp->if_timer = 2; 614 } 615 616 /* 617 * Start output on interface. 618 * We make two assumptions here: 619 * 1) that the current priority is set to splnet _before_ this code 620 * is called *and* is returned to the appropriate priority after 621 * return 622 * 2) that the IFF_OACTIVE flag is checked before this code is called 623 * (i.e. that the output part of the interface is idle) 624 */ 625 int 626 ae_start(ifp) 627 struct ifnet *ifp; 628 { 629 struct ae_softc *sc = &ae_softc[ifp->if_unit]; 630 struct mbuf *m0, *m; 631 caddr_t buffer; 632 int len; 633 634 outloop: 635 /* 636 * See if there is room to send more data (i.e. one or both of the 637 * buffers is empty). 638 */ 639 if (sc->data_buffered) 640 if (sc->xmit_busy) { 641 /* 642 * No room. Indicate this to the outside world 643 * and exit. 644 */ 645 ifp->if_flags |= IFF_OACTIVE; 646 return; 647 } else { 648 /* 649 * Data is buffered, but we're not transmitting, so 650 * start the xmit on the buffered data. 651 * Note that ae_xmit() resets the data_buffered flag 652 * before returning. 653 */ 654 ae_xmit(ifp); 655 } 656 657 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m); 658 if (m == 0) { 659 /* 660 * The following isn't pretty; we are using the !OACTIVE flag to 661 * indicate to the outside world that we can accept an additional 662 * packet rather than that the transmitter is _actually_ 663 * active. Indeed, the transmitter may be active, but if we haven't 664 * filled the secondary buffer with data then we still want to 665 * accept more. 666 * Note that it isn't necessary to test the data_buffered flag - 667 * we wouldn't have tried to de-queue the packet in the first place 668 * if it was set. 669 */ 670 ifp->if_flags &= ~IFF_OACTIVE; 671 return; 672 } 673 674 /* 675 * Copy the mbuf chain into the transmit buffer 676 */ 677 buffer = sc->smem_start + (sc->txb_next * AE_TXBUF_SIZE * AE_PAGE_SIZE); 678 len = 0; 679 for (m0 = m; m != 0; m = m->m_next) { 680 /*printf("ae: copy %d bytes @ %x\n", m->m_len, buffer);*/ 681 bcopy(mtod(m, caddr_t), buffer, m->m_len); 682 buffer += m->m_len; 683 len += m->m_len; 684 } 685 if (len & 1) len++; 686 687 sc->txb_next_len = MAX(len, ETHER_MIN_LEN); 688 689 if (sc->txb_cnt > 1) 690 /* 691 * only set 'buffered' flag if doing multiple buffers 692 */ 693 sc->data_buffered = 1; 694 695 if (sc->xmit_busy == 0) 696 ae_xmit(ifp); 697 /* 698 * If there is BPF support in the configuration, tap off here. 699 * The following has support for converting trailer packets 700 * back to normal. 701 */ 702 #if NBPFILTER > 0 703 if (sc->bpf) { 704 u_short etype; 705 int off, datasize, resid; 706 struct ether_header *eh; 707 struct trailer_header { 708 u_short ether_type; 709 u_short ether_residual; 710 } trailer_header; 711 char ether_packet[ETHER_MAX_LEN]; 712 char *ep; 713 714 ep = ether_packet; 715 716 /* 717 * We handle trailers below: 718 * Copy ether header first, then residual data, 719 * then data. Put all this in a temporary buffer 720 * 'ether_packet' and send off to bpf. Since the 721 * system has generated this packet, we assume 722 * that all of the offsets in the packet are 723 * correct; if they're not, the system will almost 724 * certainly crash in m_copydata. 725 * We make no assumptions about how the data is 726 * arranged in the mbuf chain (i.e. how much 727 * data is in each mbuf, if mbuf clusters are 728 * used, etc.), which is why we use m_copydata 729 * to get the ether header rather than assume 730 * that this is located in the first mbuf. 731 */ 732 /* copy ether header */ 733 m_copydata(m0, 0, sizeof(struct ether_header), ep); 734 eh = (struct ether_header *) ep; 735 ep += sizeof(struct ether_header); 736 etype = ntohs(eh->ether_type); 737 if (etype >= ETHERTYPE_TRAIL && 738 etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) { 739 datasize = ((etype - ETHERTYPE_TRAIL) << 9); 740 off = datasize + sizeof(struct ether_header); 741 742 /* copy trailer_header into a data structure */ 743 m_copydata(m0, off, sizeof(struct trailer_header), 744 &trailer_header.ether_type); 745 746 /* copy residual data */ 747 m_copydata(m0, off+sizeof(struct trailer_header), 748 resid = ntohs(trailer_header.ether_residual) - 749 sizeof(struct trailer_header), ep); 750 ep += resid; 751 752 /* copy data */ 753 m_copydata(m0, sizeof(struct ether_header), 754 datasize, ep); 755 ep += datasize; 756 757 /* restore original ether packet type */ 758 eh->ether_type = trailer_header.ether_type; 759 760 bpf_tap(sc->bpf, ether_packet, ep - ether_packet); 761 } else 762 bpf_mtap(sc->bpf, m0); 763 } 764 #endif 765 766 m_freem(m0); 767 768 /* 769 * If we are doing double-buffering, a buffer might be free to 770 * fill with another packet, so loop back to the top. 771 */ 772 if (sc->txb_cnt > 1) 773 goto outloop; 774 else { 775 ifp->if_flags |= IFF_OACTIVE; 776 return; 777 } 778 } 779 780 /* 781 * Ethernet interface receiver interrupt. 782 */ 783 static inline void 784 ae_rint(unit) 785 int unit; 786 { 787 register struct ae_softc *sc = &ae_softc[unit]; 788 u_char boundry, current; 789 u_short len; 790 struct ae_ring *packet_ptr; 791 792 /* 793 * Set NIC to page 1 registers to get 'current' pointer 794 */ 795 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA); 796 797 /* 798 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e. 799 * it points to where new data has been buffered. The 'CURR' 800 * (current) register points to the logical end of the ring-buffer 801 * - i.e. it points to where additional new data will be added. 802 * We loop here until the logical beginning equals the logical 803 * end (or in other words, until the ring-buffer is empty). 804 */ 805 while (sc->next_packet != NIC_GET(sc, AE_P1_CURR)) { 806 807 /* get pointer to this buffer header structure */ 808 packet_ptr = (struct ae_ring *)(sc->smem_ring + 809 (sc->next_packet - sc->rec_page_start) * AE_PAGE_SIZE); 810 811 /* 812 * The byte count includes the FCS - Frame Check Sequence (a 813 * 32 bit CRC). 814 */ 815 len = packet_ptr->count[0] | (packet_ptr->count[1] << 8); 816 if ((len >= ETHER_MIN_LEN) && (len <= ETHER_MAX_LEN)) { 817 /* 818 * Go get packet. len - 4 removes CRC from length. 819 * (packet_ptr + 1) points to data just after the packet ring 820 * header (+4 bytes) 821 */ 822 ae_get_packet(sc, (caddr_t)(packet_ptr + 1), len - 4); 823 ++sc->arpcom.ac_if.if_ipackets; 824 } else { 825 /* 826 * Really BAD...probably indicates that the ring pointers 827 * are corrupted. Also seen on early rev chips under 828 * high load - the byte order of the length gets switched. 829 */ 830 log(LOG_ERR, 831 "ae%d: shared memory corrupt - invalid packet length %d\n", 832 unit, len); 833 ae_reset(unit); 834 return; 835 } 836 837 /* 838 * Update next packet pointer 839 */ 840 sc->next_packet = packet_ptr->next_packet; 841 842 /* 843 * Update NIC boundry pointer - being careful to keep it 844 * one buffer behind. (as recommended by NS databook) 845 */ 846 boundry = sc->next_packet - 1; 847 if (boundry < sc->rec_page_start) 848 boundry = sc->rec_page_stop - 1; 849 850 /* 851 * Set NIC to page 0 registers to update boundry register 852 */ 853 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA); 854 855 NIC_PUT(sc, AE_P0_BNRY, boundry); 856 857 /* 858 * Set NIC to page 1 registers before looping to top (prepare to 859 * get 'CURR' current pointer) 860 */ 861 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA); 862 } 863 } 864 865 /* 866 * Ethernet interface interrupt processor 867 */ 868 int 869 aeintr(unit) 870 int unit; 871 { 872 struct ae_softc *sc = &ae_softc[unit]; 873 u_char isr; 874 875 /* 876 * Set NIC to page 0 registers 877 */ 878 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA); 879 880 /* 881 * loop until there are no more new interrupts 882 */ 883 while (isr = NIC_GET(sc, AE_P0_ISR)) { 884 885 /* 886 * reset all the bits that we are 'acknowledging' 887 * by writing a '1' to each bit position that was set 888 * (writing a '1' *clears* the bit) 889 */ 890 NIC_PUT(sc, AE_P0_ISR, isr); 891 892 /* 893 * Handle transmitter interrupts. Handle these first 894 * because the receiver will reset the board under 895 * some conditions. 896 */ 897 if (isr & (AE_ISR_PTX|AE_ISR_TXE)) { 898 u_char collisions = NIC_GET(sc, AE_P0_NCR); 899 900 /* 901 * Check for transmit error. If a TX completed with an 902 * error, we end up throwing the packet away. Really 903 * the only error that is possible is excessive 904 * collisions, and in this case it is best to allow the 905 * automatic mechanisms of TCP to backoff the flow. Of 906 * course, with UDP we're screwed, but this is expected 907 * when a network is heavily loaded. 908 */ 909 if (isr & AE_ISR_TXE) { 910 911 /* 912 * Excessive collisions (16) 913 */ 914 if ((NIC_GET(sc, AE_P0_TSR) & AE_TSR_ABT) 915 && (collisions == 0)) { 916 /* 917 * When collisions total 16, the 918 * P0_NCR will indicate 0, and the 919 * TSR_ABT is set. 920 */ 921 collisions = 16; 922 } 923 924 /* 925 * update output errors counter 926 */ 927 ++sc->arpcom.ac_if.if_oerrors; 928 } else { 929 /* 930 * Update total number of successfully 931 * transmitted packets. 932 */ 933 ++sc->arpcom.ac_if.if_opackets; 934 } 935 936 /* 937 * reset tx busy and output active flags 938 */ 939 sc->xmit_busy = 0; 940 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE; 941 942 /* 943 * clear watchdog timer 944 */ 945 sc->arpcom.ac_if.if_timer = 0; 946 947 /* 948 * Add in total number of collisions on last 949 * transmission. 950 */ 951 sc->arpcom.ac_if.if_collisions += collisions; 952 953 /* 954 * If data is ready to transmit, start it transmitting, 955 * otherwise defer until after handling receiver 956 */ 957 if (sc->data_buffered) 958 ae_xmit(&sc->arpcom.ac_if); 959 } 960 961 /* 962 * Handle receiver interrupts 963 */ 964 if (isr & (AE_ISR_PRX|AE_ISR_RXE|AE_ISR_OVW)) { 965 /* 966 * Overwrite warning. In order to make sure that a lockup 967 * of the local DMA hasn't occurred, we reset and 968 * re-init the NIC. The NSC manual suggests only a 969 * partial reset/re-init is necessary - but some 970 * chips seem to want more. The DMA lockup has been 971 * seen only with early rev chips - Methinks this 972 * bug was fixed in later revs. -DG 973 */ 974 if (isr & AE_ISR_OVW) { 975 ++sc->arpcom.ac_if.if_ierrors; 976 log(LOG_WARNING, 977 "ae%d: warning - receiver ring buffer overrun\n", 978 unit); 979 /* 980 * Stop/reset/re-init NIC 981 */ 982 ae_reset(unit); 983 } else { 984 985 /* 986 * Receiver Error. One or more of: CRC error, frame 987 * alignment error FIFO overrun, or missed packet. 988 */ 989 if (isr & AE_ISR_RXE) { 990 ++sc->arpcom.ac_if.if_ierrors; 991 #ifdef AE_DEBUG 992 printf("ae%d: receive error %x\n", unit, 993 NIC_GET(sc, AE_P0_RSR)); 994 #endif 995 } 996 997 /* 998 * Go get the packet(s) 999 * XXX - Doing this on an error is dubious 1000 * because there shouldn't be any data to 1001 * get (we've configured the interface to 1002 * not accept packets with errors). 1003 */ 1004 ae_rint (unit); 1005 } 1006 } 1007 1008 /* 1009 * If it looks like the transmitter can take more data, 1010 * attempt to start output on the interface. 1011 * This is done after handling the receiver to 1012 * give the receiver priority. 1013 */ 1014 if ((sc->arpcom.ac_if.if_flags & IFF_OACTIVE) == 0) 1015 ae_start(&sc->arpcom.ac_if); 1016 1017 /* 1018 * return NIC CR to standard state: page 0, remote DMA complete, 1019 * start (toggling the TXP bit off, even if was just set 1020 * in the transmit routine, is *okay* - it is 'edge' 1021 * triggered from low to high) 1022 */ 1023 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA); 1024 1025 /* 1026 * If the Network Talley Counters overflow, read them to 1027 * reset them. It appears that old 8390's won't 1028 * clear the ISR flag otherwise - resulting in an 1029 * infinite loop. 1030 */ 1031 if (isr & AE_ISR_CNT) { 1032 (void) NIC_GET(sc, AE_P0_CNTR0); 1033 (void) NIC_GET(sc, AE_P0_CNTR1); 1034 (void) NIC_GET(sc, AE_P0_CNTR2); 1035 } 1036 } 1037 } 1038 1039 /* 1040 * Process an ioctl request. This code needs some work - it looks 1041 * pretty ugly. 1042 */ 1043 int 1044 ae_ioctl(ifp, command, data) 1045 register struct ifnet *ifp; 1046 int command; 1047 caddr_t data; 1048 { 1049 register struct ifaddr *ifa = (struct ifaddr *)data; 1050 struct ae_softc *sc = &ae_softc[ifp->if_unit]; 1051 struct ifreq *ifr = (struct ifreq *)data; 1052 int s, error = 0; 1053 1054 s = splnet(); 1055 1056 switch (command) { 1057 1058 case SIOCSIFADDR: 1059 ifp->if_flags |= IFF_UP; 1060 1061 switch (ifa->ifa_addr->sa_family) { 1062 #ifdef INET 1063 case AF_INET: 1064 ae_init(ifp->if_unit); /* before arpwhohas */ 1065 /* 1066 * See if another station has *our* IP address. 1067 * i.e.: There is an address conflict! If a 1068 * conflict exists, a message is sent to the 1069 * console. 1070 */ 1071 ((struct arpcom *)ifp)->ac_ipaddr = 1072 IA_SIN(ifa)->sin_addr; 1073 arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr); 1074 break; 1075 #endif 1076 #ifdef NS 1077 /* 1078 * XXX - This code is probably wrong 1079 */ 1080 case AF_NS: 1081 { 1082 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 1083 1084 if (ns_nullhost(*ina)) 1085 ina->x_host = 1086 *(union ns_host *)(sc->arpcom.ac_enaddr); 1087 else { 1088 /* 1089 * 1090 */ 1091 bcopy((caddr_t)ina->x_host.c_host, 1092 (caddr_t)sc->arpcom.ac_enaddr, 1093 sizeof(sc->arpcom.ac_enaddr)); 1094 } 1095 /* 1096 * Set new address 1097 */ 1098 ae_init(ifp->if_unit); 1099 break; 1100 } 1101 #endif 1102 default: 1103 ae_init(ifp->if_unit); 1104 break; 1105 } 1106 break; 1107 1108 case SIOCSIFFLAGS: 1109 /* 1110 * If interface is marked down and it is running, then stop it 1111 */ 1112 if (((ifp->if_flags & IFF_UP) == 0) && 1113 (ifp->if_flags & IFF_RUNNING)) { 1114 ae_stop(ifp->if_unit); 1115 ifp->if_flags &= ~IFF_RUNNING; 1116 } else { 1117 /* 1118 * If interface is marked up and it is stopped, then start it 1119 */ 1120 if ((ifp->if_flags & IFF_UP) && 1121 ((ifp->if_flags & IFF_RUNNING) == 0)) 1122 ae_init(ifp->if_unit); 1123 } 1124 #if NBPFILTER > 0 1125 if (ifp->if_flags & IFF_PROMISC) { 1126 /* 1127 * Set promiscuous mode on interface. 1128 * XXX - for multicasts to work, we would need to 1129 * write 1's in all bits of multicast 1130 * hashing array. For now we assume that 1131 * this was done in ae_init(). 1132 */ 1133 NIC_PUT(sc, AE_P0_RCR, 1134 AE_RCR_PRO|AE_RCR_AM|AE_RCR_AB); 1135 } else { 1136 /* 1137 * XXX - for multicasts to work, we would need to 1138 * rewrite the multicast hashing array with the 1139 * proper hash (would have been destroyed above). 1140 */ 1141 NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB); 1142 } 1143 #endif 1144 break; 1145 1146 default: 1147 error = EINVAL; 1148 } 1149 (void) splx(s); 1150 return (error); 1151 } 1152 1153 /* 1154 * Macro to calculate a new address within shared memory when given an offset 1155 * from an address, taking into account ring-wrap. 1156 */ 1157 #define ringoffset(sc, start, off, type) \ 1158 ((type)( ((caddr_t)(start)+(off) >= (sc)->smem_end) ? \ 1159 (((caddr_t)(start)+(off))) - (sc)->smem_end \ 1160 + (sc)->smem_ring: \ 1161 ((caddr_t)(start)+(off)) )) 1162 1163 /* 1164 * Retreive packet from shared memory and send to the next level up via 1165 * ether_input(). If there is a BPF listener, give a copy to BPF, too. 1166 */ 1167 ae_get_packet(sc, buf, len) 1168 struct ae_softc *sc; 1169 char *buf; 1170 u_short len; 1171 { 1172 struct ether_header *eh; 1173 struct mbuf *m, *head, *ae_ring_to_mbuf(); 1174 u_short off; 1175 int resid; 1176 u_short etype; 1177 struct trailer_header { 1178 u_short trail_type; 1179 u_short trail_residual; 1180 } trailer_header; 1181 1182 /* Allocate a header mbuf */ 1183 MGETHDR(m, M_DONTWAIT, MT_DATA); 1184 if (m == 0) 1185 goto bad; 1186 m->m_pkthdr.rcvif = &sc->arpcom.ac_if; 1187 m->m_pkthdr.len = len; 1188 m->m_len = 0; 1189 head = m; 1190 1191 eh = (struct ether_header *)buf; 1192 1193 /* The following sillines is to make NFS happy */ 1194 #define EROUND ((sizeof(struct ether_header) + 3) & ~3) 1195 #define EOFF (EROUND - sizeof(struct ether_header)) 1196 1197 /* 1198 * The following assumes there is room for 1199 * the ether header in the header mbuf 1200 */ 1201 head->m_data += EOFF; 1202 bcopy(buf, mtod(head, caddr_t), sizeof(struct ether_header)); 1203 buf += sizeof(struct ether_header); 1204 head->m_len += sizeof(struct ether_header); 1205 len -= sizeof(struct ether_header); 1206 1207 etype = ntohs((u_short)eh->ether_type); 1208 1209 /* 1210 * Deal with trailer protocol: 1211 * If trailer protocol, calculate the datasize as 'off', 1212 * which is also the offset to the trailer header. 1213 * Set resid to the amount of packet data following the 1214 * trailer header. 1215 * Finally, copy residual data into mbuf chain. 1216 */ 1217 if (etype >= ETHERTYPE_TRAIL && 1218 etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) { 1219 1220 off = (etype - ETHERTYPE_TRAIL) << 9; 1221 if ((off + sizeof(struct trailer_header)) > len) 1222 goto bad; /* insanity */ 1223 1224 eh->ether_type = *ringoffset(sc, buf, off, u_short *); 1225 resid = ntohs(*ringoffset(sc, buf, off+2, u_short *)); 1226 1227 if ((off + resid) > len) goto bad; /* insanity */ 1228 1229 resid -= sizeof(struct trailer_header); 1230 if (resid < 0) goto bad; /* insanity */ 1231 1232 m = ae_ring_to_mbuf(sc, ringoffset(sc, buf, off+4, char *), head, resid); 1233 if (m == 0) goto bad; 1234 1235 len = off; 1236 head->m_pkthdr.len -= 4; /* subtract trailer header */ 1237 } 1238 1239 /* 1240 * Pull packet off interface. Or if this was a trailer packet, 1241 * the data portion is appended. 1242 */ 1243 m = ae_ring_to_mbuf(sc, buf, m, len); 1244 if (m == 0) goto bad; 1245 1246 #if NBPFILTER > 0 1247 /* 1248 * Check if there's a BPF listener on this interface. 1249 * If so, hand off the raw packet to bpf. 1250 */ 1251 if (sc->bpf) { 1252 bpf_mtap(sc->bpf, head); 1253 1254 /* 1255 * Note that the interface cannot be in promiscuous mode if 1256 * there are no BPF listeners. And if we are in promiscuous 1257 * mode, we have to check if this packet is really ours. 1258 * 1259 * XXX This test does not support multicasts. 1260 */ 1261 if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) && 1262 bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, 1263 sizeof(eh->ether_dhost)) != 0 && 1264 bcmp(eh->ether_dhost, etherbroadcastaddr, 1265 sizeof(eh->ether_dhost)) != 0) { 1266 1267 m_freem(head); 1268 return; 1269 } 1270 } 1271 #endif 1272 1273 /* 1274 * Fix up data start offset in mbuf to point past ether header 1275 */ 1276 m_adj(head, sizeof(struct ether_header)); 1277 1278 /* 1279 * silly ether_input routine needs 'type' in host byte order 1280 */ 1281 eh->ether_type = ntohs(eh->ether_type); 1282 1283 ether_input(&sc->arpcom.ac_if, eh, head); 1284 return; 1285 1286 bad: if (head) 1287 m_freem(head); 1288 return; 1289 } 1290 1291 /* 1292 * Supporting routines 1293 */ 1294 1295 /* 1296 * Given a source and destination address, copy 'amount' of a packet from 1297 * the ring buffer into a linear destination buffer. Takes into account 1298 * ring-wrap. 1299 */ 1300 static inline char * 1301 ae_ring_copy(sc,src,dst,amount) 1302 struct ae_softc *sc; 1303 char *src; 1304 char *dst; 1305 u_short amount; 1306 { 1307 u_short tmp_amount; 1308 1309 /* does copy wrap to lower addr in ring buffer? */ 1310 if (src + amount > sc->smem_end) { 1311 tmp_amount = sc->smem_end - src; 1312 bcopy(src, dst, tmp_amount); /* copy amount up to end of smem */ 1313 amount -= tmp_amount; 1314 src = sc->smem_ring; 1315 dst += tmp_amount; 1316 } 1317 1318 bcopy(src, dst, amount); 1319 1320 return(src + amount); 1321 } 1322 1323 /* 1324 * Copy data from receive buffer to end of mbuf chain 1325 * allocate additional mbufs as needed. return pointer 1326 * to last mbuf in chain. 1327 * sc = ed info (softc) 1328 * src = pointer in ed ring buffer 1329 * dst = pointer to last mbuf in mbuf chain to copy to 1330 * amount = amount of data to copy 1331 */ 1332 struct mbuf * 1333 ae_ring_to_mbuf(sc,src,dst,total_len) 1334 struct ae_softc *sc; 1335 char *src; 1336 struct mbuf *dst; 1337 u_short total_len; 1338 { 1339 register struct mbuf *m = dst; 1340 1341 while (total_len) { 1342 register u_short amount = min(total_len, M_TRAILINGSPACE(m)); 1343 1344 if (amount == 0) { /* no more data in this mbuf, alloc another */ 1345 /* 1346 * If there is enough data for an mbuf cluster, attempt 1347 * to allocate one of those, otherwise, a regular 1348 * mbuf will do. 1349 * Note that a regular mbuf is always required, even if 1350 * we get a cluster - getting a cluster does not 1351 * allocate any mbufs, and one is needed to assign 1352 * the cluster to. The mbuf that has a cluster 1353 * extension can not be used to contain data - only 1354 * the cluster can contain data. 1355 */ 1356 dst = m; 1357 MGET(m, M_DONTWAIT, MT_DATA); 1358 if (m == 0) 1359 return (0); 1360 1361 if (total_len >= MINCLSIZE) 1362 MCLGET(m, M_DONTWAIT); 1363 1364 m->m_len = 0; 1365 dst->m_next = m; 1366 amount = min(total_len, M_TRAILINGSPACE(m)); 1367 } 1368 1369 src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len, amount); 1370 1371 m->m_len += amount; 1372 total_len -= amount; 1373 1374 } 1375 return (m); 1376 } 1377 #endif 1378 1379