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