1 /* $NetBSD: dp8390.c,v 1.95 2019/05/29 10:07:29 msaitoh 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.95 2019/05/29 10:07:29 msaitoh 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 ++sc->sc_ec.ec_if.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 ++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 uint8_t collisions = 657 NIC_GET(regt, regh, ED_P0_NCR) & 0x0f; 658 659 /* 660 * Check for transmit error. If a TX completed with an 661 * error, we end up throwing the packet away. Really 662 * the only error that is possible is excessive 663 * collisions, and in this case it is best to allow the 664 * automatic mechanisms of TCP to backoff the flow. Of 665 * course, with UDP we're screwed, but this is expected 666 * when a network is heavily loaded. 667 */ 668 if ((isr & ED_ISR_TXE) != 0) { 669 /* Excessive collisions (16). */ 670 if ((NIC_GET(regt, regh, ED_P0_TSR) 671 & ED_TSR_ABT) && (collisions == 0)) { 672 /* 673 * When collisions total 16, the P0_NCR 674 * will indicate 0, and the TSR_ABT is 675 * set. 676 */ 677 collisions = 16; 678 } 679 680 /* Update output errors counter. */ 681 ++ifp->if_oerrors; 682 } else { 683 /* 684 * Throw away the non-error status bits. 685 * 686 * XXX 687 * It may be useful to detect loss of carrier 688 * and late collisions here. 689 */ 690 (void)NIC_GET(regt, regh, ED_P0_TSR); 691 692 /* 693 * Update total number of successfully 694 * transmitted packets. 695 */ 696 ++ifp->if_opackets; 697 } 698 699 /* Clear watchdog timer. */ 700 ifp->if_timer = 0; 701 ifp->if_flags &= ~IFF_OACTIVE; 702 703 /* 704 * Add in total number of collisions on last 705 * transmission. 706 */ 707 ifp->if_collisions += collisions; 708 709 /* 710 * Decrement buffer in-use count if not zero (can only 711 * be zero if a transmitter interrupt occurred while 712 * not actually transmitting). 713 * If data is ready to transmit, start it transmitting, 714 * otherwise defer until after handling receiver. 715 */ 716 if (--sc->txb_inuse != 0) 717 dp8390_xmit(sc); 718 } 719 720 /* Handle receiver interrupts. */ 721 if ((isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) != 0) { 722 /* 723 * Overwrite warning. In order to make sure that a 724 * lockup of the local DMA hasn't occurred, we reset 725 * and re-init the NIC. The NSC manual suggests only a 726 * partial reset/re-init is necessary - but some chips 727 * seem to want more. The DMA lockup has been seen 728 * only with early rev chips - Methinks this bug was 729 * fixed in later revs. -DG 730 */ 731 if ((isr & ED_ISR_OVW) != 0) { 732 ++ifp->if_ierrors; 733 #ifdef DIAGNOSTIC 734 log(LOG_WARNING, "%s: warning - receiver " 735 "ring buffer overrun\n", 736 device_xname(sc->sc_dev)); 737 #endif 738 /* Stop/reset/re-init NIC. */ 739 dp8390_reset(sc); 740 } else { 741 /* 742 * Receiver Error. One or more of: CRC error, 743 * frame alignment error FIFO overrun, or 744 * missed packet. 745 */ 746 if ((isr & ED_ISR_RXE) != 0) { 747 ++ifp->if_ierrors; 748 #ifdef DEBUG 749 if (dp8390_debug) { 750 printf("%s: receive error %x\n", 751 device_xname(sc->sc_dev), 752 NIC_GET(regt, regh, 753 ED_P0_RSR)); 754 } 755 #endif 756 } 757 758 /* 759 * Go get the packet(s) 760 * XXX - Doing this on an error is dubious 761 * because there shouldn't be any data to get 762 * (we've configured the interface to not 763 * accept packets with errors). 764 */ 765 (*sc->recv_int)(sc); 766 } 767 } 768 769 /* 770 * If it looks like the transmitter can take more data, attempt 771 * to start output on the interface. This is done after 772 * handling the receiver to give the receiver priority. 773 */ 774 if_schedule_deferred_start(ifp); 775 776 /* 777 * Return NIC CR to standard state: page 0, remote DMA 778 * complete, start (toggling the TXP bit off, even if was just 779 * set in the transmit routine, is *okay* - it is 'edge' 780 * triggered from low to high). 781 */ 782 NIC_BARRIER(regt, regh); 783 NIC_PUT(regt, regh, ED_P0_CR, 784 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA); 785 NIC_BARRIER(regt, regh); 786 787 /* 788 * If the Network Talley Counters overflow, read them to reset 789 * them. It appears that old 8390's won't clear the ISR flag 790 * otherwise - resulting in an infinite loop. 791 */ 792 if ((isr & ED_ISR_CNT) != 0) { 793 (void)NIC_GET(regt, regh, ED_P0_CNTR0); 794 (void)NIC_GET(regt, regh, ED_P0_CNTR1); 795 (void)NIC_GET(regt, regh, ED_P0_CNTR2); 796 } 797 798 isr = NIC_GET(regt, regh, ED_P0_ISR); 799 if (isr == 0) 800 goto out; 801 } 802 803 out: 804 rnd_add_uint32(&sc->rnd_source, rndisr); 805 return 1; 806 } 807 808 /* 809 * Process an ioctl request. This code needs some work - it looks pretty ugly. 810 */ 811 int 812 dp8390_ioctl(struct ifnet *ifp, u_long cmd, void *data) 813 { 814 struct dp8390_softc *sc = ifp->if_softc; 815 struct ifaddr *ifa = data; 816 int s, error = 0; 817 818 s = splnet(); 819 820 switch (cmd) { 821 822 case SIOCINITIFADDR: 823 if ((error = dp8390_enable(sc)) != 0) 824 break; 825 ifp->if_flags |= IFF_UP; 826 827 dp8390_init(sc); 828 switch (ifa->ifa_addr->sa_family) { 829 #ifdef INET 830 case AF_INET: 831 arp_ifinit(ifp, ifa); 832 break; 833 #endif 834 default: 835 break; 836 } 837 break; 838 839 case SIOCSIFFLAGS: 840 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 841 break; 842 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { 843 case IFF_RUNNING: 844 /* 845 * If interface is marked down and it is running, then 846 * stop it. 847 */ 848 dp8390_stop(sc); 849 ifp->if_flags &= ~IFF_RUNNING; 850 dp8390_disable(sc); 851 break; 852 case IFF_UP: 853 /* 854 * If interface is marked up and it is stopped, then 855 * start it. 856 */ 857 if ((error = dp8390_enable(sc)) != 0) 858 break; 859 dp8390_init(sc); 860 break; 861 case IFF_UP | IFF_RUNNING: 862 /* 863 * Reset the interface to pick up changes in any other 864 * flags that affect hardware registers. 865 */ 866 dp8390_stop(sc); 867 dp8390_init(sc); 868 break; 869 default: 870 break; 871 } 872 break; 873 874 case SIOCADDMULTI: 875 case SIOCDELMULTI: 876 if (sc->sc_enabled == 0) { 877 error = EIO; 878 break; 879 } 880 881 /* Update our multicast list. */ 882 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 883 /* 884 * Multicast list has changed; set the hardware filter 885 * accordingly. 886 */ 887 if (ifp->if_flags & IFF_RUNNING) { 888 dp8390_stop(sc); /* XXX for ds_setmcaf? */ 889 dp8390_init(sc); 890 } 891 error = 0; 892 } 893 break; 894 895 default: 896 error = ether_ioctl(ifp, cmd, data); 897 break; 898 } 899 900 splx(s); 901 return error; 902 } 903 904 /* 905 * Retrieve packet from buffer memory and send to the next level up via 906 * ether_input(). If there is a BPF listener, give a copy to BPF, too. 907 */ 908 void 909 dp8390_read(struct dp8390_softc *sc, int buf, u_short len) 910 { 911 struct ifnet *ifp = &sc->sc_ec.ec_if; 912 struct mbuf *m; 913 914 /* Pull packet off interface. */ 915 m = dp8390_get(sc, buf, len); 916 if (m == NULL) { 917 ifp->if_ierrors++; 918 return; 919 } 920 921 if_percpuq_enqueue(ifp->if_percpuq, m); 922 } 923 924 925 /* 926 * Supporting routines. 927 */ 928 929 /* 930 * Compute the multicast address filter from the list of multicast addresses we 931 * need to listen to. 932 */ 933 void 934 dp8390_getmcaf(struct ethercom *ec, uint8_t *af) 935 { 936 struct ifnet *ifp = &ec->ec_if; 937 struct ether_multi *enm; 938 uint32_t crc; 939 int i; 940 struct ether_multistep step; 941 942 /* 943 * Set up multicast address filter by passing all multicast addresses 944 * through a crc generator, and then using the high order 6 bits as an 945 * index into the 64 bit logical address filter. The high order bit 946 * selects the word, while the rest of the bits select the bit within 947 * the word. 948 */ 949 950 if (ifp->if_flags & IFF_PROMISC) { 951 ifp->if_flags |= IFF_ALLMULTI; 952 for (i = 0; i < 8; i++) 953 af[i] = 0xff; 954 return; 955 } 956 for (i = 0; i < 8; i++) 957 af[i] = 0; 958 ETHER_LOCK(ec); 959 ETHER_FIRST_MULTI(step, ec, enm); 960 while (enm != NULL) { 961 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 962 sizeof(enm->enm_addrlo)) != 0) { 963 /* 964 * We must listen to a range of multicast addresses. 965 * For now, just accept all multicasts, rather than 966 * trying to set only those filter bits needed to match 967 * the range. (At this time, the only use of address 968 * ranges is for IP multicast routing, for which the 969 * range is big enough to require all bits set.) 970 */ 971 ifp->if_flags |= IFF_ALLMULTI; 972 for (i = 0; i < 8; i++) 973 af[i] = 0xff; 974 ETHER_UNLOCK(ec); 975 return; 976 } 977 978 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN); 979 980 /* Just want the 6 most significant bits. */ 981 crc >>= 26; 982 983 /* Turn on the corresponding bit in the filter. */ 984 af[crc >> 3] |= 1 << (crc & 0x7); 985 986 ETHER_NEXT_MULTI(step, enm); 987 } 988 ETHER_UNLOCK(ec); 989 ifp->if_flags &= ~IFF_ALLMULTI; 990 } 991 992 /* 993 * Copy data from receive buffer to a new mbuf chain allocating mbufs 994 * as needed. Return pointer to first mbuf in chain. 995 * sc = dp8390 info (softc) 996 * src = pointer in dp8390 ring buffer 997 * total_len = amount of data to copy 998 */ 999 struct mbuf * 1000 dp8390_get(struct dp8390_softc *sc, int src, u_short total_len) 1001 { 1002 struct ifnet *ifp = &sc->sc_ec.ec_if; 1003 struct mbuf *m, *m0, *newm; 1004 u_short len; 1005 1006 MGETHDR(m0, M_DONTWAIT, MT_DATA); 1007 if (m0 == NULL) 1008 return NULL; 1009 m_set_rcvif(m0, ifp); 1010 m0->m_pkthdr.len = total_len; 1011 len = MHLEN; 1012 m = m0; 1013 1014 while (total_len > 0) { 1015 if (total_len >= MINCLSIZE) { 1016 MCLGET(m, M_DONTWAIT); 1017 if ((m->m_flags & M_EXT) == 0) 1018 goto bad; 1019 len = MCLBYTES; 1020 } 1021 1022 /* Make sure the data after the Ethernet header is aligned. */ 1023 if (m == m0) { 1024 char *newdata = (char *) 1025 ALIGN(m->m_data + sizeof(struct ether_header)) - 1026 sizeof(struct ether_header); 1027 len -= newdata - m->m_data; 1028 m->m_data = newdata; 1029 } 1030 1031 m->m_len = len = uimin(total_len, len); 1032 src = (*sc->ring_copy)(sc, src, mtod(m, void *), len); 1033 1034 total_len -= len; 1035 if (total_len > 0) { 1036 MGET(newm, M_DONTWAIT, MT_DATA); 1037 if (newm == NULL) 1038 goto bad; 1039 len = MLEN; 1040 m = m->m_next = newm; 1041 } 1042 } 1043 1044 return m0; 1045 1046 bad: 1047 m_freem(m0); 1048 return NULL; 1049 } 1050 1051 1052 /* 1053 * Default driver support functions. 1054 * 1055 * NOTE: all support functions assume 8-bit shared memory. 1056 */ 1057 /* 1058 * Zero NIC buffer memory and verify that it is clear. 1059 */ 1060 static int 1061 dp8390_test_mem(struct dp8390_softc *sc) 1062 { 1063 bus_space_tag_t buft = sc->sc_buft; 1064 bus_space_handle_t bufh = sc->sc_bufh; 1065 int i; 1066 1067 bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size); 1068 1069 for (i = 0; i < sc->mem_size; ++i) { 1070 if (bus_space_read_1(buft, bufh, sc->mem_start + i)) { 1071 printf(": failed to clear NIC buffer at offset %x - " 1072 "check configuration\n", (sc->mem_start + i)); 1073 return 1; 1074 } 1075 } 1076 1077 return 0; 1078 } 1079 1080 /* 1081 * Read a packet header from the ring, given the source offset. 1082 */ 1083 static void 1084 dp8390_read_hdr(struct dp8390_softc *sc, int src, struct dp8390_ring *hdrp) 1085 { 1086 bus_space_tag_t buft = sc->sc_buft; 1087 bus_space_handle_t bufh = sc->sc_bufh; 1088 1089 /* 1090 * The byte count includes a 4 byte header that was added by 1091 * the NIC. 1092 */ 1093 hdrp->rsr = bus_space_read_1(buft, bufh, src); 1094 hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1); 1095 hdrp->count = bus_space_read_1(buft, bufh, src + 2) | 1096 (bus_space_read_1(buft, bufh, src + 3) << 8); 1097 } 1098 1099 /* 1100 * Copy `amount' bytes from a packet in the ring buffer to a linear 1101 * destination buffer, given a source offset and destination address. 1102 * Takes into account ring-wrap. 1103 */ 1104 static int 1105 dp8390_ring_copy(struct dp8390_softc *sc, int src, void *dst, u_short amount) 1106 { 1107 bus_space_tag_t buft = sc->sc_buft; 1108 bus_space_handle_t bufh = sc->sc_bufh; 1109 u_short tmp_amount; 1110 1111 /* Does copy wrap to lower addr in ring buffer? */ 1112 if (src + amount > sc->mem_end) { 1113 tmp_amount = sc->mem_end - src; 1114 1115 /* Copy amount up to end of NIC memory. */ 1116 bus_space_read_region_1(buft, bufh, src, dst, tmp_amount); 1117 1118 amount -= tmp_amount; 1119 src = sc->mem_ring; 1120 dst = (char *)dst + tmp_amount; 1121 } 1122 bus_space_read_region_1(buft, bufh, src, dst, amount); 1123 1124 return src + amount; 1125 } 1126 1127 /* 1128 * Copy a packet from an mbuf to the transmit buffer on the card. 1129 * 1130 * Currently uses an extra buffer/extra memory copy, unless the whole 1131 * packet fits in one mbuf. 1132 */ 1133 static int 1134 dp8390_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf) 1135 { 1136 bus_space_tag_t buft = sc->sc_buft; 1137 bus_space_handle_t bufh = sc->sc_bufh; 1138 uint8_t *data; 1139 int len, totlen = 0; 1140 1141 for (; m ; m = m->m_next) { 1142 data = mtod(m, uint8_t *); 1143 len = m->m_len; 1144 if (len > 0) { 1145 bus_space_write_region_1(buft, bufh, buf, data, len); 1146 totlen += len; 1147 buf += len; 1148 } 1149 } 1150 if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) { 1151 bus_space_set_region_1(buft, bufh, buf, 0, 1152 ETHER_MIN_LEN - ETHER_CRC_LEN - totlen); 1153 totlen = ETHER_MIN_LEN - ETHER_CRC_LEN; 1154 } 1155 return totlen; 1156 } 1157 1158 /* 1159 * Enable power on the interface. 1160 */ 1161 int 1162 dp8390_enable(struct dp8390_softc *sc) 1163 { 1164 1165 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) { 1166 if ((*sc->sc_enable)(sc) != 0) { 1167 aprint_error_dev(sc->sc_dev, 1168 "device enable failed\n"); 1169 return EIO; 1170 } 1171 } 1172 1173 sc->sc_enabled = 1; 1174 return 0; 1175 } 1176 1177 /* 1178 * Disable power on the interface. 1179 */ 1180 void 1181 dp8390_disable(struct dp8390_softc *sc) 1182 { 1183 1184 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) { 1185 (*sc->sc_disable)(sc); 1186 sc->sc_enabled = 0; 1187 } 1188 } 1189 1190 int 1191 dp8390_activate(device_t self, enum devact act) 1192 { 1193 struct dp8390_softc *sc = device_private(self); 1194 1195 switch (act) { 1196 case DVACT_DEACTIVATE: 1197 if_deactivate(&sc->sc_ec.ec_if); 1198 return 0; 1199 default: 1200 return EOPNOTSUPP; 1201 } 1202 } 1203 1204 int 1205 dp8390_detach(struct dp8390_softc *sc, int flags) 1206 { 1207 struct ifnet *ifp = &sc->sc_ec.ec_if; 1208 1209 /* Succeed now if there's no work to do. */ 1210 if ((sc->sc_flags & DP8390_ATTACHED) == 0) 1211 return 0; 1212 1213 /* dp8390_disable() checks sc->sc_enabled */ 1214 dp8390_disable(sc); 1215 1216 if (sc->sc_media_fini != NULL) 1217 (*sc->sc_media_fini)(sc); 1218 1219 /* Delete all remaining media. */ 1220 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY); 1221 1222 rnd_detach_source(&sc->rnd_source); 1223 ether_ifdetach(ifp); 1224 if_detach(ifp); 1225 1226 return 0; 1227 } 1228