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