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