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