1 /* $NetBSD: dp8390.c,v 1.3 1997/04/30 19:16:23 scottr Exp $ */ 2 3 /* 4 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet 5 * adapters. 6 * 7 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved. 8 * 9 * Copyright (C) 1993, David Greenman. This software may be used, modified, 10 * copied, distributed, and sold, in both source and binary form provided that 11 * the above copyright and these terms are retained. Under no circumstances is 12 * the author responsible for the proper functioning of this software, nor does 13 * the author assume any responsibility for damages incurred with its use. 14 */ 15 16 #include "bpfilter.h" 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/errno.h> 22 #include <sys/ioctl.h> 23 #include <sys/mbuf.h> 24 #include <sys/socket.h> 25 #include <sys/syslog.h> 26 27 #include <net/if.h> 28 #include <net/if_dl.h> 29 #include <net/if_types.h> 30 #include <net/if_ether.h> 31 32 #ifdef INET 33 #include <netinet/in.h> 34 #include <netinet/in_systm.h> 35 #include <netinet/in_var.h> 36 #include <netinet/ip.h> 37 #include <netinet/if_inarp.h> 38 #endif 39 40 #ifdef NS 41 #include <netns/ns.h> 42 #include <netns/ns_if.h> 43 #endif 44 45 #if NBPFILTER > 0 46 #include <net/bpf.h> 47 #include <net/bpfdesc.h> 48 #endif 49 50 #include <machine/bus.h> 51 52 #include <dev/ic/dp8390reg.h> 53 #include <dev/ic/dp8390var.h> 54 55 #ifdef DEBUG 56 #define __inline__ /* XXX for debugging porpoises */ 57 #endif 58 59 static __inline__ void dp8390_xmit __P((struct dp8390_softc *)); 60 61 static __inline__ void dp8390_read_hdr __P((struct dp8390_softc *, 62 int, struct dp8390_ring *)); 63 static __inline__ int dp8390_ring_copy __P((struct dp8390_softc *, 64 int, caddr_t, u_short)); 65 static __inline__ int dp8390_write_mbuf __P((struct dp8390_softc *, 66 struct mbuf *, int)); 67 68 static int dp8390_test_mem __P((struct dp8390_softc *)); 69 70 #define ETHER_MIN_LEN 64 71 #define ETHER_MAX_LEN 1518 72 #define ETHER_ADDR_LEN 6 73 74 /* 75 * Do bus-independent setup. 76 */ 77 int 78 dp8390_config(sc) 79 struct dp8390_softc *sc; 80 { 81 struct ifnet *ifp = &sc->sc_ec.ec_if; 82 int rv; 83 84 rv = 1; 85 86 if (!sc->test_mem) 87 sc->test_mem = dp8390_test_mem; 88 89 /* Allocate one xmit buffer if < 16k, two buffers otherwise. */ 90 if ((sc->mem_size < 16384) || 91 (sc->sc_flags & DP8390_NO_MULTI_BUFFERING)) 92 sc->txb_cnt = 1; 93 else 94 sc->txb_cnt = 2; 95 96 sc->tx_page_start = 0; 97 sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE; 98 sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT); 99 sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT); 100 sc->mem_end = sc->mem_start + sc->mem_size; 101 102 /* Now zero memory and verify that it is clear. */ 103 if ((*sc->test_mem)(sc)) 104 goto out; 105 106 /* Set interface to stopped condition (reset). */ 107 dp8390_stop(sc); 108 109 /* Initialize ifnet structure. */ 110 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); 111 ifp->if_softc = sc; 112 ifp->if_start = dp8390_start; 113 ifp->if_ioctl = dp8390_ioctl; 114 if (!ifp->if_watchdog) 115 ifp->if_watchdog = dp8390_watchdog; 116 ifp->if_flags = 117 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; 118 119 /* Attach the interface. */ 120 if_attach(ifp); 121 ether_ifattach(ifp, sc->sc_enaddr); 122 #if NBPFILTER > 0 123 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); 124 #endif 125 126 /* Print additional info when attached. */ 127 printf(": address %s, ", ether_sprintf(sc->sc_enaddr)); 128 129 if (sc->type_str) 130 printf("type %s ", sc->type_str); 131 else 132 printf("type unknown (0x%x) ", sc->type); 133 134 rv = 0; 135 out: 136 return (rv); 137 } 138 139 /* 140 * Reset interface. 141 */ 142 void 143 dp8390_reset(sc) 144 struct dp8390_softc *sc; 145 { 146 int s; 147 148 s = splnet(); 149 dp8390_stop(sc); 150 dp8390_init(sc); 151 splx(s); 152 } 153 154 /* 155 * Take interface offline. 156 */ 157 void 158 dp8390_stop(sc) 159 struct dp8390_softc *sc; 160 { 161 bus_space_tag_t regt = sc->sc_regt; 162 bus_space_handle_t regh = sc->sc_regh; 163 int n = 5000; 164 165 /* Stop everything on the interface, and select page 0 registers. */ 166 NIC_PUT(regt, regh, ED_P0_CR, 167 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP); 168 169 /* 170 * Wait for interface to enter stopped state, but limit # of checks to 171 * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but 172 * just in case it's an old one. 173 */ 174 while (((NIC_GET(regt, regh, 175 ED_P0_ISR) & ED_ISR_RST) == 0) && --n) 176 ; 177 } 178 179 /* 180 * Device timeout/watchdog routine. Entered if the device neglects to generate 181 * an interrupt after a transmit has been started on it. 182 */ 183 184 void 185 dp8390_watchdog(ifp) 186 struct ifnet *ifp; 187 { 188 struct dp8390_softc *sc = ifp->if_softc; 189 190 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname); 191 ++sc->sc_ec.ec_if.if_oerrors; 192 193 dp8390_reset(sc); 194 } 195 196 /* 197 * Initialize device. 198 */ 199 void 200 dp8390_init(sc) 201 struct dp8390_softc *sc; 202 { 203 bus_space_tag_t regt = sc->sc_regt; 204 bus_space_handle_t regh = sc->sc_regh; 205 struct ifnet *ifp = &sc->sc_ec.ec_if; 206 u_int8_t mcaf[8]; 207 int i; 208 209 /* 210 * Initialize the NIC in the exact order outlined in the NS manual. 211 * This init procedure is "mandatory"...don't change what or when 212 * things happen. 213 */ 214 215 /* Reset transmitter flags. */ 216 ifp->if_timer = 0; 217 218 sc->txb_inuse = 0; 219 sc->txb_new = 0; 220 sc->txb_next_tx = 0; 221 222 /* Set interface for page 0, remote DMA complete, stopped. */ 223 NIC_PUT(regt, regh, ED_P0_CR, 224 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP); 225 226 if (sc->dcr_reg & ED_DCR_LS) { 227 NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg); 228 } else { 229 /* 230 * Set FIFO threshold to 8, No auto-init Remote DMA, byte 231 * order=80x86, byte-wide DMA xfers, 232 */ 233 NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS); 234 } 235 236 /* Clear remote byte count registers. */ 237 NIC_PUT(regt, regh, ED_P0_RBCR0, 0); 238 NIC_PUT(regt, regh, ED_P0_RBCR1, 0); 239 240 /* Tell RCR to do nothing for now. */ 241 NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON); 242 243 /* Place NIC in internal loopback mode. */ 244 NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0); 245 246 /* Set lower bits of byte addressable framing to 0. */ 247 if (sc->is790) 248 NIC_PUT(regt, regh, 0x09, 0); 249 250 /* Initialize receive buffer ring. */ 251 NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start); 252 NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start); 253 NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop); 254 255 /* 256 * Clear all interrupts. A '1' in each bit position clears the 257 * corresponding flag. 258 */ 259 NIC_PUT(regt, regh, ED_P0_ISR, 0xff); 260 261 /* 262 * Enable the following interrupts: receive/transmit complete, 263 * receive/transmit error, and Receiver OverWrite. 264 * 265 * Counter overflow and Remote DMA complete are *not* enabled. 266 */ 267 NIC_PUT(regt, regh, ED_P0_IMR, 268 ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE | 269 ED_IMR_OVWE); 270 271 /* Program command register for page 1. */ 272 NIC_PUT(regt, regh, ED_P0_CR, 273 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP); 274 275 /* Copy out our station address. */ 276 for (i = 0; i < ETHER_ADDR_LEN; ++i) 277 NIC_PUT(regt, regh, ED_P1_PAR0 + i, 278 LLADDR(ifp->if_sadl)[i]); 279 280 /* Set multicast filter on chip. */ 281 dp8390_getmcaf(&sc->sc_ec, mcaf); 282 for (i = 0; i < 8; i++) 283 NIC_PUT(regt, regh, ED_P1_MAR0 + i, mcaf[i]); 284 285 /* 286 * Set current page pointer to one page after the boundary pointer, as 287 * recommended in the National manual. 288 */ 289 sc->next_packet = sc->rec_page_start + 1; 290 NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet); 291 292 /* Program command register for page 0. */ 293 NIC_PUT(regt, regh, ED_P1_CR, 294 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP); 295 296 i = ED_RCR_AB | ED_RCR_AM; 297 if (ifp->if_flags & IFF_PROMISC) { 298 /* 299 * Set promiscuous mode. Multicast filter was set earlier so 300 * that we should receive all multicast packets. 301 */ 302 i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP; 303 } 304 NIC_PUT(regt, regh, ED_P0_RCR, i); 305 306 /* Take interface out of loopback. */ 307 NIC_PUT(regt, regh, ED_P0_TCR, 0); 308 309 /* Do any card-specific initialization, if applicable. */ 310 if (sc->init_card) 311 (*sc->init_card)(sc); 312 313 /* Fire up the interface. */ 314 NIC_PUT(regt, regh, ED_P0_CR, 315 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA); 316 317 /* Set 'running' flag, and clear output active flag. */ 318 ifp->if_flags |= IFF_RUNNING; 319 ifp->if_flags &= ~IFF_OACTIVE; 320 321 /* ...and attempt to start output. */ 322 dp8390_start(ifp); 323 } 324 325 /* 326 * This routine actually starts the transmission on the interface. 327 */ 328 static __inline__ void 329 dp8390_xmit(sc) 330 struct dp8390_softc *sc; 331 { 332 bus_space_tag_t regt = sc->sc_regt; 333 bus_space_handle_t regh = sc->sc_regh; 334 struct ifnet *ifp = &sc->sc_ec.ec_if; 335 u_short len; 336 337 len = sc->txb_len[sc->txb_next_tx]; 338 339 /* Set NIC for page 0 register access. */ 340 NIC_PUT(regt, regh, ED_P0_CR, 341 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA); 342 343 /* Set TX buffer start page. */ 344 NIC_PUT(regt, regh, ED_P0_TPSR, sc->tx_page_start + 345 sc->txb_next_tx * ED_TXBUF_SIZE); 346 347 /* Set TX length. */ 348 NIC_PUT(regt, regh, ED_P0_TBCR0, len); 349 NIC_PUT(regt, regh, ED_P0_TBCR1, len >> 8); 350 351 /* Set page 0, remote DMA complete, transmit packet, and *start*. */ 352 NIC_PUT(regt, regh, ED_P0_CR, 353 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA); 354 355 /* Point to next transmit buffer slot and wrap if necessary. */ 356 sc->txb_next_tx++; 357 if (sc->txb_next_tx == sc->txb_cnt) 358 sc->txb_next_tx = 0; 359 360 /* Set a timer just in case we never hear from the board again. */ 361 ifp->if_timer = 2; 362 } 363 364 /* 365 * Start output on interface. 366 * We make two assumptions here: 367 * 1) that the current priority is set to splnet _before_ this code 368 * is called *and* is returned to the appropriate priority after 369 * return 370 * 2) that the IFF_OACTIVE flag is checked before this code is called 371 * (i.e. that the output part of the interface is idle) 372 */ 373 void 374 dp8390_start(ifp) 375 struct ifnet *ifp; 376 { 377 struct dp8390_softc *sc = ifp->if_softc; 378 struct mbuf *m0; 379 int buffer; 380 int len; 381 382 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 383 return; 384 385 outloop: 386 /* See if there is room to put another packet in the buffer. */ 387 if (sc->txb_inuse == sc->txb_cnt) { 388 /* No room. Indicate this to the outside world and exit. */ 389 ifp->if_flags |= IFF_OACTIVE; 390 return; 391 } 392 IF_DEQUEUE(&ifp->if_snd, m0); 393 if (m0 == 0) 394 return; 395 396 /* We need to use m->m_pkthdr.len, so require the header */ 397 if ((m0->m_flags & M_PKTHDR) == 0) 398 panic("dp8390_start: no header mbuf"); 399 400 #if NBPFILTER > 0 401 /* Tap off here if there is a BPF listener. */ 402 if (ifp->if_bpf) 403 bpf_mtap(ifp->if_bpf, m0); 404 #endif 405 406 /* txb_new points to next open buffer slot. */ 407 buffer = sc->mem_start + 408 ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT); 409 410 if (sc->write_mbuf) 411 len = (*sc->write_mbuf)(sc, m0, buffer); 412 else 413 len = dp8390_write_mbuf(sc, m0, buffer); 414 415 m_freem(m0); 416 sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN); 417 418 /* Start the first packet transmitting. */ 419 if (sc->txb_inuse == 0) 420 dp8390_xmit(sc); 421 422 /* Point to next buffer slot and wrap if necessary. */ 423 if (++sc->txb_new == sc->txb_cnt) 424 sc->txb_new = 0; 425 426 sc->txb_inuse++; 427 428 /* Loop back to the top to possibly buffer more packets. */ 429 goto outloop; 430 } 431 432 /* 433 * Ethernet interface receiver interrupt. 434 */ 435 void 436 dp8390_rint(sc) 437 struct dp8390_softc *sc; 438 { 439 bus_space_tag_t regt = sc->sc_regt; 440 bus_space_handle_t regh = sc->sc_regh; 441 struct dp8390_ring packet_hdr; 442 int packet_ptr; 443 u_short len; 444 u_char boundary, current; 445 u_char nlen; 446 447 loop: 448 /* Set NIC to page 1 registers to get 'current' pointer. */ 449 NIC_PUT(regt, regh, ED_P0_CR, 450 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA); 451 452 /* 453 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e. 454 * it points to where new data has been buffered. The 'CURR' (current) 455 * register points to the logical end of the ring-buffer - i.e. it 456 * points to where additional new data will be added. We loop here 457 * until the logical beginning equals the logical end (or in other 458 * words, until the ring-buffer is empty). 459 */ 460 current = NIC_GET(regt, regh, ED_P1_CURR); 461 if (sc->next_packet == current) 462 return; 463 464 /* Set NIC to page 0 registers to update boundary register. */ 465 NIC_PUT(regt, regh, ED_P1_CR, 466 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA); 467 468 do { 469 /* Get pointer to this buffer's header structure. */ 470 packet_ptr = sc->mem_ring + 471 ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT); 472 473 if (sc->read_hdr) 474 (*sc->read_hdr)(sc, packet_ptr, &packet_hdr); 475 else 476 dp8390_read_hdr(sc, packet_ptr, &packet_hdr); 477 len = packet_hdr.count; 478 479 /* 480 * Try do deal with old, buggy chips that sometimes duplicate 481 * the low byte of the length into the high byte. We do this 482 * by simply ignoring the high byte of the length and always 483 * recalculating it. 484 * 485 * NOTE: sc->next_packet is pointing at the current packet. 486 */ 487 if (packet_hdr.next_packet >= sc->next_packet) 488 nlen = (packet_hdr.next_packet - sc->next_packet); 489 else 490 nlen = ((packet_hdr.next_packet - sc->rec_page_start) + 491 (sc->rec_page_stop - sc->next_packet)); 492 --nlen; 493 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE) 494 --nlen; 495 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT); 496 #ifdef DIAGNOSTIC 497 if (len != packet_hdr.count) { 498 printf("%s: length does not match " 499 "next packet pointer\n", sc->sc_dev.dv_xname); 500 printf("%s: len %04x nlen %04x start %02x " 501 "first %02x curr %02x next %02x stop %02x\n", 502 sc->sc_dev.dv_xname, packet_hdr.count, len, 503 sc->rec_page_start, sc->next_packet, current, 504 packet_hdr.next_packet, sc->rec_page_stop); 505 } 506 #endif 507 508 /* 509 * Be fairly liberal about what we allow as a "reasonable" 510 * length so that a [crufty] packet will make it to BPF (and 511 * can thus be analyzed). Note that all that is really 512 * important is that we have a length that will fit into one 513 * mbuf cluster or less; the upper layer protocols can then 514 * figure out the length from their own length field(s). 515 */ 516 if (len <= MCLBYTES && 517 packet_hdr.next_packet >= sc->rec_page_start && 518 packet_hdr.next_packet < sc->rec_page_stop) { 519 /* Go get packet. */ 520 dp8390_read(sc, 521 packet_ptr + sizeof(struct dp8390_ring), 522 len - sizeof(struct dp8390_ring)); 523 ++sc->sc_ec.ec_if.if_ipackets; 524 } else { 525 /* Really BAD. The ring pointers are corrupted. */ 526 log(LOG_ERR, "%s: NIC memory corrupt - " 527 "invalid packet length %d\n", 528 sc->sc_dev.dv_xname, len); 529 ++sc->sc_ec.ec_if.if_ierrors; 530 dp8390_reset(sc); 531 return; 532 } 533 534 /* Update next packet pointer. */ 535 sc->next_packet = packet_hdr.next_packet; 536 537 /* 538 * Update NIC boundary pointer - being careful to keep it one 539 * buffer behind (as recommended by NS databook). 540 */ 541 boundary = sc->next_packet - 1; 542 if (boundary < sc->rec_page_start) 543 boundary = sc->rec_page_stop - 1; 544 NIC_PUT(regt, regh, ED_P0_BNRY, boundary); 545 } while (sc->next_packet != current); 546 547 goto loop; 548 } 549 550 /* Ethernet interface interrupt processor. */ 551 void 552 dp8390_intr(arg, slot) 553 void *arg; 554 int slot; 555 { 556 struct dp8390_softc *sc = (struct dp8390_softc *)arg; 557 bus_space_tag_t regt = sc->sc_regt; 558 bus_space_handle_t regh = sc->sc_regh; 559 struct ifnet *ifp = &sc->sc_ec.ec_if; 560 u_char isr; 561 562 /* Set NIC to page 0 registers. */ 563 NIC_PUT(regt, regh, ED_P0_CR, 564 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA); 565 566 isr = NIC_GET(regt, regh, ED_P0_ISR); 567 if (!isr) 568 return; 569 570 /* Loop until there are no more new interrupts. */ 571 for (;;) { 572 /* 573 * Reset all the bits that we are 'acknowledging' by writing a 574 * '1' to each bit position that was set. 575 * (Writing a '1' *clears* the bit.) 576 */ 577 NIC_PUT(regt, regh, ED_P0_ISR, isr); 578 579 /* 580 * Handle transmitter interrupts. Handle these first because 581 * the receiver will reset the board under some conditions. 582 */ 583 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) { 584 u_char collisions = NIC_GET(regt, regh, 585 ED_P0_NCR) & 0x0f; 586 587 /* 588 * Check for transmit error. If a TX completed with an 589 * error, we end up throwing the packet away. Really 590 * the only error that is possible is excessive 591 * collisions, and in this case it is best to allow the 592 * automatic mechanisms of TCP to backoff the flow. Of 593 * course, with UDP we're screwed, but this is expected 594 * when a network is heavily loaded. 595 */ 596 (void)NIC_GET(regt, regh, ED_P0_TSR); 597 if (isr & ED_ISR_TXE) { 598 /* 599 * Excessive collisions (16). 600 */ 601 if ((NIC_GET(regt, regh, ED_P0_TSR) 602 & ED_TSR_ABT) && (collisions == 0)) { 603 /* 604 * When collisions total 16, the P0_NCR 605 * will indicate 0, and the TSR_ABT is 606 * set. 607 */ 608 collisions = 16; 609 } 610 611 /* Update output errors counter. */ 612 ++ifp->if_oerrors; 613 } else { 614 /* 615 * Update total number of successfully 616 * transmitted packets. 617 */ 618 ++ifp->if_opackets; 619 } 620 621 /* Done with the buffer. */ 622 sc->txb_inuse--; 623 624 /* Clear watchdog timer. */ 625 ifp->if_timer = 0; 626 ifp->if_flags &= ~IFF_OACTIVE; 627 628 /* 629 * Add in total number of collisions on last 630 * transmission. 631 */ 632 ifp->if_collisions += collisions; 633 634 /* 635 * Decrement buffer in-use count if not zero (can only 636 * be zero if a transmitter interrupt occured while not 637 * actually transmitting). 638 * If data is ready to transmit, start it transmitting, 639 * otherwise defer until after handling receiver. 640 */ 641 if (sc->txb_inuse > 0) 642 dp8390_xmit(sc); 643 } 644 645 /* Handle receiver interrupts. */ 646 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) { 647 /* 648 * Overwrite warning. In order to make sure that a 649 * lockup of the local DMA hasn't occurred, we reset 650 * and re-init the NIC. The NSC manual suggests only a 651 * partial reset/re-init is necessary - but some chips 652 * seem to want more. The DMA lockup has been seen 653 * only with early rev chips - Methinks this bug was 654 * fixed in later revs. -DG 655 */ 656 if (isr & ED_ISR_OVW) { 657 ++ifp->if_ierrors; 658 #ifdef DIAGNOSTIC 659 log(LOG_WARNING, "%s: warning - receiver " 660 "ring buffer overrun\n", 661 sc->sc_dev.dv_xname); 662 #endif 663 /* Stop/reset/re-init NIC. */ 664 dp8390_reset(sc); 665 } else { 666 /* 667 * Receiver Error. One or more of: CRC error, 668 * frame alignment error FIFO overrun, or 669 * missed packet. 670 */ 671 if (isr & ED_ISR_RXE) { 672 ++ifp->if_ierrors; 673 #ifdef DEBUG 674 printf("%s: receive error %x\n", 675 sc->sc_dev.dv_xname, 676 NIC_GET(regt, regh, 677 ED_P0_RSR)); 678 #endif 679 } 680 681 /* 682 * Go get the packet(s) 683 * XXX - Doing this on an error is dubious 684 * because there shouldn't be any data to get 685 * (we've configured the interface to not 686 * accept packets with errors). 687 */ 688 if (sc->recv_int) 689 (*sc->recv_int)(sc); 690 else 691 dp8390_rint(sc); 692 } 693 } 694 695 /* 696 * If it looks like the transmitter can take more data, attempt 697 * to start output on the interface. This is done after 698 * handling the receiver to give the receiver priority. 699 */ 700 dp8390_start(ifp); 701 702 /* 703 * Return NIC CR to standard state: page 0, remote DMA 704 * complete, start (toggling the TXP bit off, even if was just 705 * set in the transmit routine, is *okay* - it is 'edge' 706 * triggered from low to high). 707 */ 708 NIC_PUT(regt, regh, ED_P0_CR, 709 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA); 710 711 /* 712 * If the Network Talley Counters overflow, read them to reset 713 * them. It appears that old 8390's won't clear the ISR flag 714 * otherwise - resulting in an infinite loop. 715 */ 716 if (isr & ED_ISR_CNT) { 717 (void)NIC_GET(regt, regh, ED_P0_CNTR0); 718 (void)NIC_GET(regt, regh, ED_P0_CNTR1); 719 (void)NIC_GET(regt, regh, ED_P0_CNTR2); 720 } 721 722 isr = NIC_GET(regt, regh, ED_P0_ISR); 723 if (!isr) 724 return; 725 } 726 } 727 728 /* 729 * Process an ioctl request. This code needs some work - it looks pretty ugly. 730 */ 731 int 732 dp8390_ioctl(ifp, cmd, data) 733 struct ifnet *ifp; 734 u_long cmd; 735 caddr_t data; 736 { 737 struct dp8390_softc *sc = ifp->if_softc; 738 struct ifaddr *ifa = (struct ifaddr *) data; 739 struct ifreq *ifr = (struct ifreq *) data; 740 int s, error = 0; 741 742 s = splnet(); 743 744 switch (cmd) { 745 746 case SIOCSIFADDR: 747 ifp->if_flags |= IFF_UP; 748 749 switch (ifa->ifa_addr->sa_family) { 750 #ifdef INET 751 case AF_INET: 752 dp8390_init(sc); 753 arp_ifinit(ifp, ifa); 754 break; 755 #endif 756 #ifdef NS 757 /* XXX - This code is probably wrong. */ 758 case AF_NS: 759 { 760 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 761 762 if (ns_nullhost(*ina)) 763 ina->x_host = 764 *(union ns_host *)LLADDR(ifp->if_sadl); 765 else 766 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl), 767 ETHER_ADDR_LEN); 768 /* Set new address. */ 769 dp8390_init(sc); 770 break; 771 } 772 #endif 773 default: 774 dp8390_init(sc); 775 break; 776 } 777 break; 778 779 case SIOCSIFFLAGS: 780 if ((ifp->if_flags & IFF_UP) == 0 && 781 (ifp->if_flags & IFF_RUNNING) != 0) { 782 /* 783 * If interface is marked down and it is running, then 784 * stop it. 785 */ 786 dp8390_stop(sc); 787 ifp->if_flags &= ~IFF_RUNNING; 788 } else if ((ifp->if_flags & IFF_UP) != 0 && 789 (ifp->if_flags & IFF_RUNNING) == 0) { 790 /* 791 * If interface is marked up and it is stopped, then 792 * start it. 793 */ 794 dp8390_init(sc); 795 } else { 796 /* 797 * Reset the interface to pick up changes in any other 798 * flags that affect hardware registers. 799 */ 800 dp8390_stop(sc); 801 dp8390_init(sc); 802 } 803 break; 804 805 case SIOCADDMULTI: 806 case SIOCDELMULTI: 807 /* Update our multicast list. */ 808 error = (cmd == SIOCADDMULTI) ? 809 ether_addmulti(ifr, &sc->sc_ec) : 810 ether_delmulti(ifr, &sc->sc_ec); 811 812 if (error == ENETRESET) { 813 /* 814 * Multicast list has changed; set the hardware filter 815 * accordingly. 816 */ 817 dp8390_stop(sc); /* XXX for ds_setmcaf? */ 818 dp8390_init(sc); 819 error = 0; 820 } 821 break; 822 823 default: 824 error = EINVAL; 825 break; 826 } 827 828 splx(s); 829 return (error); 830 } 831 832 /* 833 * Retrieve packet from buffer memory and send to the next level up via 834 * ether_input(). If there is a BPF listener, give a copy to BPF, too. 835 */ 836 void 837 dp8390_read(sc, buf, len) 838 struct dp8390_softc *sc; 839 int buf; 840 u_short len; 841 { 842 struct ifnet *ifp = &sc->sc_ec.ec_if; 843 struct mbuf *m; 844 struct ether_header *eh; 845 846 /* Pull packet off interface. */ 847 m = dp8390_get(sc, buf, len); 848 if (m == 0) { 849 ifp->if_ierrors++; 850 return; 851 } 852 853 ifp->if_ipackets++; 854 855 /* We assume that the header fits entirely in one mbuf. */ 856 eh = mtod(m, struct ether_header *); 857 858 #if NBPFILTER > 0 859 /* 860 * Check if there's a BPF listener on this interface. 861 * If so, hand off the raw packet to bpf. 862 */ 863 if (ifp->if_bpf) { 864 bpf_mtap(ifp->if_bpf, m); 865 866 /* 867 * Note that the interface cannot be in promiscuous mode if 868 * there are no BPF listeners. And if we are in promiscuous 869 * mode, we have to check if this packet is really ours. 870 */ 871 if ((ifp->if_flags & IFF_PROMISC) && 872 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */ 873 bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl), 874 sizeof(eh->ether_dhost)) != 0) { 875 m_freem(m); 876 return; 877 } 878 } 879 #endif 880 881 /* Fix up data start offset in mbuf to point past ether header. */ 882 m_adj(m, sizeof(struct ether_header)); 883 ether_input(ifp, eh, m); 884 } 885 886 887 /* 888 * Supporting routines. 889 */ 890 891 /* 892 * Compute the multicast address filter from the list of multicast addresses we 893 * need to listen to. 894 */ 895 void 896 dp8390_getmcaf(ec, af) 897 struct ethercom *ec; 898 u_int8_t *af; 899 { 900 struct ifnet *ifp = &ec->ec_if; 901 struct ether_multi *enm; 902 u_int8_t *cp, c; 903 u_int32_t crc; 904 int i, len; 905 struct ether_multistep step; 906 907 /* 908 * Set up multicast address filter by passing all multicast addresses 909 * through a crc generator, and then using the high order 6 bits as an 910 * index into the 64 bit logical address filter. The high order bit 911 * selects the word, while the rest of the bits select the bit within 912 * the word. 913 */ 914 915 if (ifp->if_flags & IFF_PROMISC) { 916 ifp->if_flags |= IFF_ALLMULTI; 917 for (i = 0; i < 8; i++) 918 af[i] = 0xff; 919 return; 920 } 921 for (i = 0; i < 8; i++) 922 af[i] = 0; 923 ETHER_FIRST_MULTI(step, ec, enm); 924 while (enm != NULL) { 925 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 926 sizeof(enm->enm_addrlo)) != 0) { 927 /* 928 * We must listen to a range of multicast addresses. 929 * For now, just accept all multicasts, rather than 930 * trying to set only those filter bits needed to match 931 * the range. (At this time, the only use of address 932 * ranges is for IP multicast routing, for which the 933 * range is big enough to require all bits set.) 934 */ 935 ifp->if_flags |= IFF_ALLMULTI; 936 for (i = 0; i < 8; i++) 937 af[i] = 0xff; 938 return; 939 } 940 cp = enm->enm_addrlo; 941 crc = 0xffffffff; 942 for (len = sizeof(enm->enm_addrlo); --len >= 0;) { 943 c = *cp++; 944 for (i = 8; --i >= 0;) { 945 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) { 946 crc <<= 1; 947 crc ^= 0x04c11db6 | 1; 948 } else 949 crc <<= 1; 950 c >>= 1; 951 } 952 } 953 /* Just want the 6 most significant bits. */ 954 crc >>= 26; 955 956 /* Turn on the corresponding bit in the filter. */ 957 af[crc >> 3] |= 1 << (crc & 0x7); 958 959 ETHER_NEXT_MULTI(step, enm); 960 } 961 ifp->if_flags &= ~IFF_ALLMULTI; 962 } 963 964 /* 965 * Copy data from receive buffer to end of mbuf chain allocate additional mbufs 966 * as needed. Return pointer to last mbuf in chain. 967 * sc = dp8390 info (softc) 968 * src = pointer in dp8390 ring buffer 969 * dst = pointer to last mbuf in mbuf chain to copy to 970 * amount = amount of data to copy 971 */ 972 struct mbuf * 973 dp8390_get(sc, src, total_len) 974 struct dp8390_softc *sc; 975 int src; 976 u_short total_len; 977 { 978 struct ifnet *ifp = &sc->sc_ec.ec_if; 979 struct mbuf *top, **mp, *m; 980 u_short len; 981 982 MGETHDR(m, M_DONTWAIT, MT_DATA); 983 if (m == 0) 984 return 0; 985 m->m_pkthdr.rcvif = ifp; 986 m->m_pkthdr.len = total_len; 987 len = MHLEN; 988 top = 0; 989 mp = ⊤ 990 991 while (total_len > 0) { 992 if (top) { 993 MGET(m, M_DONTWAIT, MT_DATA); 994 if (m == 0) { 995 m_freem(top); 996 return 0; 997 } 998 len = MLEN; 999 } 1000 if (total_len >= MINCLSIZE) { 1001 MCLGET(m, M_DONTWAIT); 1002 if ((m->m_flags & M_EXT) == 0) { 1003 m_freem(m); 1004 m_freem(top); 1005 return 0; 1006 } 1007 len = MCLBYTES; 1008 } 1009 m->m_len = len = min(total_len, len); 1010 if (sc->ring_copy) 1011 src = (*sc->ring_copy)(sc, src, mtod(m, caddr_t), len); 1012 else 1013 src = dp8390_ring_copy(sc, src, mtod(m, caddr_t), len); 1014 total_len -= len; 1015 *mp = m; 1016 mp = &m->m_next; 1017 } 1018 1019 return top; 1020 } 1021 1022 1023 /* 1024 * Default driver support functions. 1025 * 1026 * NOTE: all support functions assume 8-bit shared memory. 1027 */ 1028 /* 1029 * Zero NIC buffer memory and verify that it is clear. 1030 */ 1031 static int 1032 dp8390_test_mem(sc) 1033 struct dp8390_softc *sc; 1034 { 1035 bus_space_tag_t buft = sc->sc_buft; 1036 bus_space_handle_t bufh = sc->sc_bufh; 1037 int i; 1038 1039 bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size); 1040 1041 for (i = 0; i < sc->mem_size; ++i) { 1042 if (bus_space_read_1(buft, bufh, sc->mem_start + i)) { 1043 printf(": failed to clear NIC buffer at offset %x - " 1044 "check configuration\n", (sc->mem_start + i)); 1045 return 1; 1046 } 1047 } 1048 1049 return 0; 1050 } 1051 1052 /* 1053 * Read a packet header from the ring, given the source offset. 1054 */ 1055 static __inline__ void 1056 dp8390_read_hdr(sc, src, hdrp) 1057 struct dp8390_softc *sc; 1058 int src; 1059 struct dp8390_ring *hdrp; 1060 { 1061 bus_space_tag_t buft = sc->sc_buft; 1062 bus_space_handle_t bufh = sc->sc_bufh; 1063 1064 /* 1065 * The byte count includes a 4 byte header that was added by 1066 * the NIC. 1067 */ 1068 hdrp->rsr = bus_space_read_1(buft, bufh, src); 1069 hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1); 1070 hdrp->count = bus_space_read_1(buft, bufh, src + 2) | 1071 (bus_space_read_1(buft, bufh, src + 3) << 8); 1072 } 1073 1074 /* 1075 * Copy `amount' bytes from a packet in the ring buffer to a linear 1076 * destination buffer, given a source offset and destination address. 1077 * Takes into account ring-wrap. 1078 */ 1079 static __inline__ int 1080 dp8390_ring_copy(sc, src, dst, amount) 1081 struct dp8390_softc *sc; 1082 int src; 1083 caddr_t dst; 1084 u_short amount; 1085 { 1086 bus_space_tag_t buft = sc->sc_buft; 1087 bus_space_handle_t bufh = sc->sc_bufh; 1088 u_short tmp_amount; 1089 1090 /* Does copy wrap to lower addr in ring buffer? */ 1091 if (src + amount > sc->mem_end) { 1092 tmp_amount = sc->mem_end - src; 1093 1094 /* Copy amount up to end of NIC memory. */ 1095 bus_space_read_region_1(buft, bufh, src, dst, tmp_amount); 1096 1097 amount -= tmp_amount; 1098 src = sc->mem_ring; 1099 dst += tmp_amount; 1100 } 1101 bus_space_read_region_1(buft, bufh, src, dst, amount); 1102 1103 return (src + amount); 1104 } 1105 1106 /* 1107 * Copy a packet from an mbuf to the transmit buffer on the card. 1108 * 1109 * Currently uses an extra buffer/extra memory copy, unless the whole 1110 * packet fits in one mbuf. 1111 */ 1112 static __inline__ int 1113 dp8390_write_mbuf(sc, m, buf) 1114 struct dp8390_softc *sc; 1115 struct mbuf *m; 1116 int buf; 1117 { 1118 bus_space_tag_t buft = sc->sc_buft; 1119 bus_space_handle_t bufh = sc->sc_bufh; 1120 u_char *data; 1121 int len, totlen = 0; 1122 1123 for (; m ; m = m->m_next) { 1124 data = mtod(m, u_char *); 1125 len = m->m_len; 1126 if (len > 0) { 1127 totlen += len; 1128 bus_space_write_region_1(buft, bufh, buf, data, len); 1129 } 1130 } 1131 1132 return (totlen); 1133 } 1134