1 /* 2 * Copyright (c) 1995, David Greenman 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by David Greenman. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id: if_fxp.c,v 1.10 1996/01/26 09:29:28 phk Exp $ 33 */ 34 35 /* 36 * Intel EtherExpress Pro/100B PCI Fast Ethernet driver 37 */ 38 39 #include "bpfilter.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/ioctl.h> 44 #include <sys/mbuf.h> 45 #include <sys/malloc.h> 46 #include <sys/kernel.h> 47 #include <sys/devconf.h> 48 #include <sys/syslog.h> 49 50 #include <net/if.h> 51 #include <net/if_dl.h> 52 #include <net/if_types.h> 53 54 #ifdef INET 55 #include <netinet/in.h> 56 #include <netinet/in_systm.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip.h> 59 #include <netinet/if_ether.h> 60 #endif 61 62 #ifdef IPX 63 #include <netipx/ipx.h> 64 #include <netipx/ipx_if.h> 65 #endif 66 67 #ifdef NS 68 #include <netns/ns.h> 69 #include <netns/ns_if.h> 70 #endif 71 72 #if NBPFILTER > 0 73 #include <net/bpf.h> 74 #include <net/bpfdesc.h> 75 #endif 76 77 #include <vm/vm.h> /* for vtophys */ 78 #include <vm/vm_param.h> /* for vtophys */ 79 #include <vm/pmap.h> /* for vtophys */ 80 #include <machine/clock.h> /* for DELAY */ 81 82 #include <pci/pcivar.h> 83 #include <pci/if_fxpreg.h> 84 85 struct fxp_softc { 86 struct arpcom arpcom; /* per-interface network data */ 87 struct fxp_csr *csr; /* control/status registers */ 88 struct fxp_cb_tx *cbl_base; /* base of TxCB list */ 89 struct fxp_cb_tx *cbl_first; /* first active TxCB in list */ 90 struct fxp_cb_tx *cbl_last; /* last active TxCB in list */ 91 struct mbuf *rfa_headm; /* first mbuf in receive frame area */ 92 struct mbuf *rfa_tailm; /* last mbuf in receive frame area */ 93 struct fxp_stats *fxp_stats; /* Pointer to interface stats */ 94 int tx_queued; /* # of active TxCB's */ 95 int promisc_mode; /* promiscuous mode enabled */ 96 }; 97 98 #include "fxp.h" 99 static struct fxp_softc *fxp_sc[NFXP]; /* XXX Yuck */ 100 101 static u_long fxp_count; 102 103 /* 104 * Template for default configuration parameters. 105 * See struct fxp_cb_config for the bit definitions. 106 */ 107 static u_char fxp_cb_config_template[] = { 108 0x0, 0x0, /* cb_status */ 109 0x80, 0x2, /* cb_command */ 110 0xff, 0xff, 0xff, 0xff, /* link_addr */ 111 0x16, /* 0 */ 112 0x8, /* 1 */ 113 0x0, /* 2 */ 114 0x0, /* 3 */ 115 0x0, /* 4 */ 116 0x80, /* 5 */ 117 0xb2, /* 6 */ 118 0x3, /* 7 */ 119 0x1, /* 8 */ 120 0x0, /* 9 */ 121 0x26, /* 10 */ 122 0x0, /* 11 */ 123 0x60, /* 12 */ 124 0x0, /* 13 */ 125 0xf2, /* 14 */ 126 0x48, /* 15 */ 127 0x0, /* 16 */ 128 0x40, /* 17 */ 129 0xf3, /* 18 */ 130 0x0, /* 19 */ 131 0x3f, /* 20 */ 132 0x5, /* 21 */ 133 0x0, 0x0 134 }; 135 136 static inline int fxp_scb_wait __P((struct fxp_csr *)); 137 static char *fxp_probe __P((pcici_t, pcidi_t)); 138 static void fxp_attach __P((pcici_t, int)); 139 static int fxp_shutdown __P((struct kern_devconf *, int)); 140 static void fxp_intr __P((void *)); 141 static void fxp_start __P((struct ifnet *)); 142 static int fxp_ioctl __P((struct ifnet *, int, caddr_t)); 143 static void fxp_init __P((struct ifnet *)); 144 static void fxp_stop __P((struct fxp_softc *)); 145 static void fxp_watchdog __P((struct ifnet *)); 146 static void fxp_get_macaddr __P((struct fxp_softc *)); 147 static int fxp_add_rfabuf __P((struct fxp_softc *, struct mbuf *)); 148 149 timeout_t fxp_stats_update; 150 151 static struct pci_device fxp_device = { 152 "fxp", 153 fxp_probe, 154 fxp_attach, 155 &fxp_count, 156 fxp_shutdown 157 }; 158 DATA_SET(pcidevice_set, fxp_device); 159 160 /* 161 * Number of transmit control blocks. This determines the number 162 * of transmit buffers that can be chained in the CB list. 163 * This must be a power of two. 164 */ 165 #define FXP_NTXCB 64 166 167 /* 168 * TxCB list index mask. This is used to do list wrap-around. 169 */ 170 #define FXP_TXCB_MASK (FXP_NTXCB - 1) 171 172 /* 173 * Number of DMA segments in a TxCB. Note that this is carefully 174 * chosen to make the total struct size an even power of two. It's 175 * critical that no TxCB be split across a page boundry since 176 * no attempt is made to allocate physically contiguous memory. 177 * 178 * XXX - don't forget to change the hard-coded constant in the 179 * fxp_cb_tx struct (defined in if_fxpreg.h), too! 180 */ 181 #define FXP_NTXSEG 29 182 183 /* 184 * Number of receive frame area buffers. These are large so chose 185 * wisely. 186 */ 187 #define FXP_NRFABUFS 32 188 189 /* 190 * Wait for the previous command to be accepted (but not necessarily 191 * completed). 192 */ 193 static inline int 194 fxp_scb_wait(csr) 195 struct fxp_csr *csr; 196 { 197 int i = 10000; 198 199 while ((csr->scb_command & FXP_SCB_COMMAND_MASK) && --i); 200 return (i); 201 } 202 203 /* 204 * Return identification string if this is device is ours. 205 */ 206 static char * 207 fxp_probe(config_id, device_id) 208 pcici_t config_id; 209 pcidi_t device_id; 210 { 211 if (((device_id & 0xffff) == FXP_VENDORID_INTEL) && 212 ((device_id >> 16) & 0xffff) == FXP_DEVICEID_i82557) 213 return ("Intel EtherExpress Pro/100B Fast Ethernet"); 214 215 return NULL; 216 } 217 218 /* 219 * Allocate data structures and attach the device. 220 */ 221 static void 222 fxp_attach(config_id, unit) 223 pcici_t config_id; 224 int unit; 225 { 226 struct fxp_softc *sc; 227 struct ifnet *ifp; 228 vm_offset_t pbase; 229 int s, i; 230 231 sc = malloc(sizeof(struct fxp_softc), M_DEVBUF, M_NOWAIT); 232 if (sc == NULL) 233 return; 234 bzero(sc, sizeof(struct fxp_softc)); 235 236 s = splimp(); 237 238 /* 239 * Map control/status registers. 240 */ 241 if (!pci_map_mem(config_id, FXP_PCI_MMBA, 242 (vm_offset_t *)&sc->csr, &pbase)) { 243 printf("fxp%d: couldn't map memory\n", unit); 244 goto fail; 245 } 246 247 /* 248 * Issue a software reset. 249 */ 250 sc->csr->port = 0; 251 DELAY(10); 252 253 /* 254 * Allocate our interrupt. 255 */ 256 if (!pci_map_int(config_id, fxp_intr, sc, &net_imask)) { 257 printf("fxp%d: couldn't map interrupt\n", unit); 258 goto fail; 259 } 260 261 sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB, 262 M_DEVBUF, M_NOWAIT); 263 if (sc->cbl_base == NULL) 264 goto malloc_fail; 265 266 sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF, M_NOWAIT); 267 if (sc->fxp_stats == NULL) 268 goto malloc_fail; 269 bzero(sc->fxp_stats, sizeof(struct fxp_stats)); 270 271 /* 272 * Pre-allocate our receive buffers. 273 */ 274 for (i = 0; i < FXP_NRFABUFS; i++) { 275 if (fxp_add_rfabuf(sc, NULL) != 0) { 276 goto malloc_fail; 277 } 278 } 279 280 fxp_sc[unit] = sc; 281 282 ifp = &sc->arpcom.ac_if; 283 ifp->if_softc = sc; 284 ifp->if_unit = unit; 285 ifp->if_name = "fxp"; 286 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 287 ifp->if_ioctl = fxp_ioctl; 288 ifp->if_output = ether_output; 289 ifp->if_start = fxp_start; 290 ifp->if_watchdog = fxp_watchdog; 291 292 fxp_get_macaddr(sc); 293 printf("fxp%d: Ethernet address %6D\n", unit, 294 sc->arpcom.ac_enaddr, ":"); 295 296 /* 297 * Attach the interface. 298 */ 299 if_attach(ifp); 300 ether_ifattach(ifp); 301 302 #if NBPFILTER > 0 303 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 304 #endif 305 splx(s); 306 return; 307 308 malloc_fail: 309 printf("fxp%d: Failed to malloc memory\n", unit); 310 (void) pci_unmap_int(config_id); 311 if (sc && sc->cbl_base) 312 free(sc->cbl_base, M_DEVBUF); 313 if (sc && sc->fxp_stats) 314 free(sc->fxp_stats, M_DEVBUF); 315 /* frees entire chain */ 316 if (sc && sc->rfa_headm) 317 m_freem(sc->rfa_headm); 318 fail: 319 if (sc) 320 free(sc, M_DEVBUF); 321 splx(s); 322 } 323 324 /* 325 * Read station (MAC) address from serial EEPROM. Basically, you 326 * manually shift in the read opcode (one bit at a time) and then 327 * shift in the address, and then you shift out the data (all of 328 * this one bit at a time). The word size is 16 bits, so you have 329 * to provide the address for every 16 bits of data. The MAC address 330 * is in the first 3 words (6 bytes total). 331 */ 332 static void 333 fxp_get_macaddr(sc) 334 struct fxp_softc *sc; 335 { 336 struct fxp_csr *csr; 337 u_short reg, *data; 338 int i, x; 339 340 csr = sc->csr; 341 data = (u_short *)sc->arpcom.ac_enaddr; 342 343 for (i = 0; i < 3; i++) { 344 csr->eeprom_control = FXP_EEPROM_EECS; 345 /* 346 * Shift in read opcode. 347 */ 348 for (x = 3; x > 0; x--) { 349 if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) { 350 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 351 } else { 352 reg = FXP_EEPROM_EECS; 353 } 354 csr->eeprom_control = reg; 355 csr->eeprom_control = reg | FXP_EEPROM_EESK; 356 DELAY(1); 357 csr->eeprom_control = reg; 358 DELAY(1); 359 } 360 /* 361 * Shift in address. 362 */ 363 for (x = 6; x > 0; x--) { 364 if (i & (1 << (x - 1))) { 365 reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI; 366 } else { 367 reg = FXP_EEPROM_EECS; 368 } 369 csr->eeprom_control = reg; 370 csr->eeprom_control = reg | FXP_EEPROM_EESK; 371 DELAY(1); 372 csr->eeprom_control = reg; 373 DELAY(1); 374 } 375 reg = FXP_EEPROM_EECS; 376 data[i] = 0; 377 /* 378 * Shift out data. 379 */ 380 for (x = 16; x > 0; x--) { 381 csr->eeprom_control = reg | FXP_EEPROM_EESK; 382 DELAY(1); 383 if (csr->eeprom_control & FXP_EEPROM_EEDO) 384 data[i] |= (1 << (x - 1)); 385 csr->eeprom_control = reg; 386 DELAY(1); 387 } 388 csr->eeprom_control = 0; 389 DELAY(1); 390 } 391 } 392 393 /* 394 * Device shutdown routine. Usually called at system shutdown. The 395 * main purpose of this routine is to shut off receiver DMA so that 396 * kernel memory doesn't get clobbered during warmboot. 397 */ 398 static int 399 fxp_shutdown(kdc, force) 400 struct kern_devconf *kdc; 401 int force; 402 { 403 struct fxp_softc *sc = fxp_sc[kdc->kdc_unit]; 404 405 fxp_stop(sc); 406 407 (void) dev_detach(kdc); 408 return 0; 409 } 410 411 /* 412 * Start packet transmission on the interface. 413 */ 414 static void 415 fxp_start(ifp) 416 struct ifnet *ifp; 417 { 418 struct fxp_softc *sc = ifp->if_softc; 419 struct fxp_csr *csr = sc->csr; 420 struct fxp_cb_tx *txp; 421 struct mbuf *m, *mb_head; 422 int segment; 423 424 txloop: 425 /* 426 * See if a TxCB is available. If not, indicate this to the 427 * outside world and exit. 428 */ 429 if (sc->tx_queued >= FXP_NTXCB) { 430 ifp->if_flags |= IFF_OACTIVE; 431 return; 432 } 433 /* 434 * Grab a packet to transmit. 435 */ 436 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, mb_head); 437 if (mb_head == NULL) { 438 /* 439 * No more packets to send. 440 */ 441 return; 442 } 443 444 /* 445 * Get pointer to next available (unused) descriptor. 446 */ 447 txp = sc->cbl_last->next; 448 449 /* 450 * Go through each of the mbufs in the chain and initialize 451 * the transmit buffers descriptors with the physical address 452 * and size of the mbuf. 453 */ 454 tbdinit: 455 for (m = mb_head, segment = 0; m != NULL; m = m->m_next) { 456 if (m->m_len != 0) { 457 if (segment == FXP_NTXSEG) 458 break; 459 txp->tbd[segment].tb_addr = 460 vtophys(mtod(m, vm_offset_t)); 461 txp->tbd[segment].tb_size = m->m_len; 462 segment++; 463 } 464 } 465 if (m != NULL && segment == FXP_NTXSEG) { 466 struct mbuf *mn; 467 468 /* 469 * We ran out of segments. We have to recopy this mbuf 470 * chain first. 471 */ 472 MGETHDR(mn, M_DONTWAIT, MT_DATA); 473 if (mn == NULL) { 474 m_freem(mb_head); 475 return; 476 } 477 if (mb_head->m_pkthdr.len > MHLEN) { 478 MCLGET(mn, M_DONTWAIT); 479 if ((mn->m_flags & M_EXT) == 0) { 480 m_freem(mn); 481 m_freem(mb_head); 482 return; 483 } 484 } 485 m_copydata(mb_head, 0, mb_head->m_pkthdr.len, mtod(mn, caddr_t)); 486 mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len; 487 m_freem(mb_head); 488 mb_head = mn; 489 goto tbdinit; 490 } 491 492 txp->tbd_number = segment; 493 494 /* 495 * Finish the initialization of this TxCB. 496 */ 497 txp->cb_status = 0; 498 txp->cb_command = 499 FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S; 500 txp->tx_threshold = 16; /* bytes*8 */ 501 txp->mb_head = mb_head; 502 503 /* 504 * Advance the end-of-list forward. 505 */ 506 sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S; 507 sc->cbl_last = txp; 508 509 /* 510 * If no packets were previously queued then advance the first 511 * pointer to this TxCB. 512 */ 513 if (sc->tx_queued++ == 0) { 514 sc->cbl_first = txp; 515 } 516 517 if (!fxp_scb_wait(csr)) { 518 /* 519 * Hmmm, card has gone out to lunch 520 */ 521 fxp_init(ifp); 522 goto txloop; 523 } 524 525 /* 526 * Resume transmission if suspended. 527 */ 528 csr->scb_command = FXP_SCB_COMMAND_CU_RESUME; 529 530 #if NBPFILTER > 0 531 /* 532 * Pass packet to bpf if there is a listener. 533 */ 534 if (ifp->if_bpf != NULL) 535 bpf_mtap(ifp, mb_head); 536 #endif 537 /* 538 * Set a 5 second timer just in case we don't hear from the 539 * card again. 540 */ 541 ifp->if_timer = 5; 542 543 goto txloop; 544 } 545 546 /* 547 * Process interface interrupts. Returns 1 if the interrupt 548 * was handled, 0 if it wasn't. 549 */ 550 static void 551 fxp_intr(arg) 552 void *arg; 553 { 554 struct fxp_softc *sc = arg; 555 struct fxp_csr *csr = sc->csr; 556 struct ifnet *ifp = &sc->arpcom.ac_if; 557 u_char statack; 558 559 while ((statack = csr->scb_statack) != 0) { 560 /* 561 * First ACK all the interrupts in this pass. 562 */ 563 csr->scb_statack = statack; 564 565 /* 566 * Free any finished transmit mbuf chains. 567 */ 568 if (statack & FXP_SCB_STATACK_CNA) { 569 struct fxp_cb_tx *txp; 570 571 for (txp = sc->cbl_first; 572 (txp->cb_status & FXP_CB_STATUS_C) && 573 txp->mb_head != NULL; 574 txp = txp->next) { 575 m_freem(txp->mb_head); 576 txp->mb_head = NULL; 577 sc->tx_queued--; 578 } 579 sc->cbl_first = txp; 580 /* 581 * We unconditionally clear IFF_OACTIVE since it 582 * doesn't hurt to do so even if the tx queue is 583 * still full - it will just get set again in 584 * fxp_start(). If we get a CNA interrupt, it is 585 * (almost?) certain that we've freed up space for 586 * at least one more packet. 587 */ 588 ifp->if_flags &= ~IFF_OACTIVE; 589 /* 590 * Clear watchdog timer. It may or may not be set 591 * again in fxp_start(). 592 */ 593 ifp->if_timer = 0; 594 fxp_start(ifp); 595 } 596 /* 597 * Process receiver interrupts. If a no-resource (RNR) 598 * condition exists, get whatever packets we can and 599 * re-start the receiver. 600 */ 601 if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) { 602 struct mbuf *m; 603 struct fxp_rfa *rfa; 604 rcvloop: 605 m = sc->rfa_headm; 606 rfa = (struct fxp_rfa *)m->m_ext.ext_buf; 607 608 if (rfa->rfa_status & FXP_RFA_STATUS_C) { 609 /* 610 * Remove first packet from the chain. 611 */ 612 sc->rfa_headm = m->m_next; 613 m->m_next = NULL; 614 615 /* 616 * Add a new buffer to the receive chain. If this 617 * fails, the old buffer is recycled instead. 618 */ 619 if (fxp_add_rfabuf(sc, m) == 0) { 620 struct ether_header *eh; 621 u_short total_len; 622 623 total_len = rfa->actual_size & (MCLBYTES - 1); 624 m->m_pkthdr.rcvif = ifp; 625 m->m_pkthdr.len = m->m_len = total_len - 626 sizeof(struct ether_header); 627 eh = mtod(m, struct ether_header *); 628 #if NBPFILTER > 0 629 if (ifp->if_bpf != NULL) { 630 bpf_tap(ifp, mtod(m, caddr_t), total_len); 631 /* 632 * Only pass this packet up if it is for us. 633 */ 634 if ((ifp->if_flags & IFF_PROMISC) && 635 (rfa->rfa_status & FXP_RFA_STATUS_IAMATCH) && 636 (eh->ether_dhost[0] & 1) == 0) { 637 m_freem(m); 638 goto rcvloop; 639 } 640 } 641 #endif 642 m->m_data += sizeof(struct ether_header); 643 ether_input(ifp, eh, m); 644 } 645 goto rcvloop; 646 } 647 if (statack & FXP_SCB_STATACK_RNR) { 648 struct fxp_csr *csr = sc->csr; 649 650 (void) fxp_scb_wait(csr); 651 csr->scb_general = vtophys(sc->rfa_headm->m_ext.ext_buf); 652 csr->scb_command = FXP_SCB_COMMAND_RU_START; 653 } 654 } 655 } 656 } 657 658 /* 659 * Update packet in/out/collision statistics. The i82557 doesn't 660 * allow you to access these counters without doing a fairly 661 * expensive DMA to get _all_ of the statistics it maintains, so 662 * we do this operation here only once per second. The statistics 663 * counters in the kernel are updated from the previous dump-stats 664 * DMA and then a new dump-stats DMA is started. The on-chip 665 * counters are zeroed when the DMA completes. If we can't start 666 * the DMA immediately, we don't wait - we just prepare to read 667 * them again next time. 668 */ 669 void 670 fxp_stats_update(arg) 671 void *arg; 672 { 673 struct fxp_softc *sc = arg; 674 struct ifnet *ifp = &sc->arpcom.ac_if; 675 struct fxp_stats *sp = sc->fxp_stats; 676 677 ifp->if_opackets += sp->tx_good; 678 ifp->if_collisions += sp->tx_total_collisions; 679 ifp->if_ipackets += sp->rx_good; 680 ifp->if_ierrors += 681 sp->rx_crc_errors + 682 sp->rx_alignment_errors + 683 sp->rx_rnr_errors + 684 sp->rx_overrun_errors + 685 sp->rx_shortframes; 686 /* 687 * If there is no pending command, start another stats 688 * dump. Otherwise punt for now. 689 */ 690 if ((sc->csr->scb_command & FXP_SCB_COMMAND_MASK) == 0) { 691 /* 692 * Start another stats dump. By waiting for it to be 693 * accepted, we avoid having to do splhigh locking when 694 * writing scb_command in other parts of the driver. 695 */ 696 sc->csr->scb_command = FXP_SCB_COMMAND_CU_DUMPRESET; 697 (void) fxp_scb_wait(sc->csr); 698 } else { 699 /* 700 * A previous command is still waiting to be accepted. 701 * Just zero our copy of the stats and wait for the 702 * next timer event to update them. 703 */ 704 sp->tx_good = 0; 705 sp->tx_total_collisions = 0; 706 707 sp->rx_good = 0; 708 sp->rx_crc_errors = 0; 709 sp->rx_alignment_errors = 0; 710 sp->rx_rnr_errors = 0; 711 sp->rx_overrun_errors = 0; 712 sp->rx_shortframes = 0;; 713 } 714 /* 715 * Schedule another timeout one second from now. 716 */ 717 timeout(fxp_stats_update, sc, hz); 718 } 719 720 /* 721 * Stop the interface. Cancels the statistics updater and resets 722 * the interface. 723 */ 724 static void 725 fxp_stop(sc) 726 struct fxp_softc *sc; 727 { 728 struct ifnet *ifp = &sc->arpcom.ac_if; 729 struct fxp_cb_tx *txp; 730 int i; 731 732 /* 733 * Cancel stats updater. 734 */ 735 untimeout(fxp_stats_update, sc); 736 737 /* 738 * Issue software reset 739 */ 740 sc->csr->port = 0; 741 DELAY(10); 742 743 /* 744 * Release any xmit buffers. 745 */ 746 for (txp = sc->cbl_first; txp != NULL && txp->mb_head != NULL; 747 txp = txp->next) { 748 m_freem(txp->mb_head); 749 txp->mb_head = NULL; 750 } 751 sc->tx_queued = 0; 752 753 /* 754 * Free all the receive buffers then reallocate/reinitialize 755 */ 756 if (sc->rfa_headm != NULL) 757 m_freem(sc->rfa_headm); 758 sc->rfa_headm = NULL; 759 sc->rfa_tailm = NULL; 760 for (i = 0; i < FXP_NRFABUFS; i++) { 761 if (fxp_add_rfabuf(sc, NULL) != 0) { 762 /* 763 * This "can't happen" - we're at splimp() 764 * and we just freed all the buffers we need 765 * above. 766 */ 767 panic("fxp_stop: no buffers!"); 768 } 769 } 770 771 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 772 ifp->if_timer = 0; 773 } 774 775 /* 776 * Watchdog/transmission transmit timeout handler. Called when a 777 * transmission is started on the interface, but no interrupt is 778 * received before the timeout. This usually indicates that the 779 * card has wedged for some reason. 780 */ 781 static void 782 fxp_watchdog(ifp) 783 struct ifnet *ifp; 784 { 785 log(LOG_ERR, "fxp%d: device timeout\n", ifp->if_unit); 786 ifp->if_oerrors++; 787 788 fxp_init(ifp); 789 } 790 791 static void 792 fxp_init(ifp) 793 struct ifnet *ifp; 794 { 795 struct fxp_softc *sc = ifp->if_softc; 796 struct fxp_cb_config *cbp; 797 struct fxp_cb_ias *cb_ias; 798 struct fxp_cb_tx *txp; 799 struct fxp_csr *csr = sc->csr; 800 int i, s, mcast, prm; 801 802 s = splimp(); 803 /* 804 * Cancel any pending I/O 805 */ 806 fxp_stop(sc); 807 808 prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0; 809 sc->promisc_mode = prm; 810 /* 811 * Sleeze out here and enable reception of all multicasts if 812 * multicasts are enabled. Ideally, we'd program the multicast 813 * address filter to only accept specific multicasts. 814 */ 815 mcast = (ifp->if_flags & (IFF_MULTICAST|IFF_ALLMULTI)) ? 1 : 0; 816 817 /* 818 * Initialize base of CBL and RFA memory. Loading with zero 819 * sets it up for regular linear addressing. 820 */ 821 csr->scb_general = 0; 822 csr->scb_command = FXP_SCB_COMMAND_CU_BASE; 823 824 (void) fxp_scb_wait(csr); 825 csr->scb_command = FXP_SCB_COMMAND_RU_BASE; 826 827 /* 828 * Initialize base of dump-stats buffer. 829 */ 830 (void) fxp_scb_wait(csr); 831 csr->scb_general = vtophys(sc->fxp_stats); 832 csr->scb_command = FXP_SCB_COMMAND_CU_DUMP_ADR; 833 834 /* 835 * We temporarily use memory that contains the TxCB list to 836 * construct the config CB. The TxCB list memory is rebuilt 837 * later. 838 */ 839 cbp = (struct fxp_cb_config *) sc->cbl_base; 840 841 /* 842 * This bcopy is kind of disgusting, but there are a bunch of must be 843 * zero and must be one bits in this structure and this is the easiest 844 * way to initialize them all to proper values. 845 */ 846 bcopy(fxp_cb_config_template, cbp, sizeof(struct fxp_cb_config)); 847 848 cbp->cb_status = 0; 849 cbp->cb_command = FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL; 850 cbp->link_addr = -1; /* (no) next command */ 851 cbp->byte_count = 22; /* (22) bytes to config */ 852 cbp->rx_fifo_limit = 8; /* rx fifo threshold */ 853 cbp->tx_fifo_limit = 0; /* tx fifo threshold */ 854 cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */ 855 cbp->rx_dma_bytecount = 0; /* (no) rx DMA max */ 856 cbp->tx_dma_bytecount = 0; /* (no) tx DMA max */ 857 cbp->dma_bce = 1; /* (enable) dma max counters */ 858 cbp->late_scb = 0; /* (don't) defer SCB update */ 859 cbp->tno_int = 0; /* (disable) tx not okay interrupt */ 860 cbp->ci_int = 0; /* (do) interrupt on CU not active */ 861 cbp->save_bf = prm; /* save bad frames */ 862 cbp->disc_short_rx = !prm; /* discard short packets */ 863 cbp->underrun_retry = 1; /* retry mode (1) on DMA underrun */ 864 cbp->mediatype = 1; /* (MII) interface mode */ 865 cbp->nsai = 1; /* (don't) disable source addr insert */ 866 cbp->preamble_length = 2; /* (7 byte) preamble */ 867 cbp->loopback = 0; /* (don't) loopback */ 868 cbp->linear_priority = 0; /* (normal CSMA/CD operation) */ 869 cbp->linear_pri_mode = 0; /* (wait after xmit only) */ 870 cbp->interfrm_spacing = 6; /* (96 bits of) interframe spacing */ 871 cbp->promiscuous = prm; /* promiscuous mode */ 872 cbp->bcast_disable = 0; /* (don't) disable broadcasts */ 873 cbp->crscdt = 0; /* (CRS only) */ 874 cbp->stripping = !prm; /* truncate rx packet to byte count */ 875 cbp->padding = 1; /* (do) pad short tx packets */ 876 cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */ 877 cbp->force_fdx = 0; /* (don't) force full duplex */ 878 cbp->fdx_pin_en = 1; /* (enable) FDX# pin */ 879 cbp->multi_ia = 0; /* (don't) accept multiple IAs */ 880 cbp->mc_all = mcast; /* accept all multicasts */ 881 882 /* 883 * Start the config command/DMA. 884 */ 885 (void) fxp_scb_wait(csr); 886 csr->scb_general = vtophys(cbp); 887 csr->scb_command = FXP_SCB_COMMAND_CU_START; 888 /* ...and wait for it to complete. */ 889 while (!(cbp->cb_status & FXP_CB_STATUS_C)); 890 891 /* 892 * Now initialize the station address. Temporarily use the TxCB 893 * memory area like we did above for the config CB. 894 */ 895 cb_ias = (struct fxp_cb_ias *) sc->cbl_base; 896 cb_ias->cb_status = 0; 897 cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL; 898 cb_ias->link_addr = -1; 899 bcopy(sc->arpcom.ac_enaddr, (void *)cb_ias->macaddr, 900 sizeof(sc->arpcom.ac_enaddr)); 901 902 /* 903 * Start the IAS (Individual Address Setup) command/DMA. 904 */ 905 (void) fxp_scb_wait(csr); 906 csr->scb_command = FXP_SCB_COMMAND_CU_START; 907 /* ...and wait for it to complete. */ 908 while (!(cb_ias->cb_status & FXP_CB_STATUS_C)); 909 910 /* 911 * Initialize transmit control block (TxCB) list. 912 */ 913 914 txp = sc->cbl_base; 915 bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB); 916 for (i = 0; i < FXP_NTXCB; i++) { 917 txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK; 918 txp[i].cb_command = FXP_CB_COMMAND_NOP; 919 txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK]); 920 txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]); 921 txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK]; 922 } 923 /* 924 * Set the stop flag on the first TxCB and start the control 925 * unit. It will execute the NOP and then suspend. 926 */ 927 txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S; 928 sc->cbl_first = sc->cbl_last = txp; 929 sc->tx_queued = 0; 930 931 (void) fxp_scb_wait(csr); 932 csr->scb_command = FXP_SCB_COMMAND_CU_START; 933 934 /* 935 * Initialize receiver buffer area - RFA. 936 */ 937 (void) fxp_scb_wait(csr); 938 csr->scb_general = vtophys(sc->rfa_headm->m_ext.ext_buf); 939 csr->scb_command = FXP_SCB_COMMAND_RU_START; 940 941 ifp->if_flags |= IFF_RUNNING; 942 ifp->if_flags &= ~IFF_OACTIVE; 943 splx(s); 944 945 /* 946 * Start stats updater. 947 */ 948 timeout(fxp_stats_update, sc, hz); 949 } 950 951 /* 952 * Add a buffer to the end of the RFA buffer list. 953 * Return 0 if successful, 1 for failure. A failure results in 954 * adding the 'oldm' (if non-NULL) on to the end of the list - 955 * tossing out it's old contents and recycling it. 956 * The RFA struct is stuck at the beginning of mbuf cluster and the 957 * data pointer is fixed up to point just past it. 958 */ 959 static int 960 fxp_add_rfabuf(sc, oldm) 961 struct fxp_softc *sc; 962 struct mbuf *oldm; 963 { 964 struct mbuf *m; 965 struct fxp_rfa *rfa, *p_rfa; 966 967 MGETHDR(m, M_DONTWAIT, MT_DATA); 968 if (m != NULL) { 969 MCLGET(m, M_DONTWAIT); 970 if ((m->m_flags & M_EXT) == 0) { 971 m_freem(m); 972 m = oldm; 973 } 974 } else { 975 m = oldm; 976 } 977 if (m == NULL) 978 return 1; 979 rfa = mtod(m, struct fxp_rfa *); 980 rfa->rfa_status = 0; 981 rfa->rfa_control = FXP_RFA_CONTROL_EL; 982 rfa->link_addr = -1; 983 rfa->rbd_addr = -1; 984 rfa->actual_size = 0; 985 rfa->size = MCLBYTES - sizeof(struct fxp_rfa); 986 m->m_data += sizeof(struct fxp_rfa); 987 /* 988 * If there are other buffers already on the list, attach this 989 * one to the end by fixing up the tail to point to this one. 990 */ 991 if (sc->rfa_headm != NULL) { 992 p_rfa = (struct fxp_rfa *) sc->rfa_tailm->m_ext.ext_buf; 993 sc->rfa_tailm->m_next = m; 994 p_rfa->link_addr = vtophys(rfa); 995 p_rfa->rfa_control &= ~FXP_RFA_CONTROL_EL; 996 } else { 997 sc->rfa_headm = m; 998 } 999 sc->rfa_tailm = m; 1000 1001 return (m == oldm); 1002 } 1003 1004 static int 1005 fxp_ioctl(ifp, command, data) 1006 struct ifnet *ifp; 1007 int command; 1008 caddr_t data; 1009 { 1010 struct ifaddr *ifa = (struct ifaddr *) data; 1011 struct fxp_softc *sc = ifp->if_softc; 1012 struct ifreq *ifr = (struct ifreq *) data; 1013 int s, error = 0; 1014 1015 s = splimp(); 1016 1017 switch (command) { 1018 1019 case SIOCSIFADDR: 1020 ifp->if_flags |= IFF_UP; 1021 1022 switch (ifa->ifa_addr->sa_family) { 1023 #ifdef INET 1024 case AF_INET: 1025 fxp_init(ifp); /* before arpwhohas */ 1026 arp_ifinit((struct arpcom *)ifp, ifa); 1027 break; 1028 #endif 1029 #ifdef IPX 1030 /* 1031 * XXX - This code is probably wrong 1032 */ 1033 case AF_IPX: 1034 { 1035 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 1036 1037 if (ipx_nullhost(*ina)) 1038 ina->x_host = 1039 *(union ipx_host *) (sc->arpcom.ac_enaddr); 1040 else { 1041 bcopy((caddr_t) ina->x_host.c_host, 1042 (caddr_t) sc->arpcom.ac_enaddr, 1043 sizeof(sc->arpcom.ac_enaddr)); 1044 } 1045 1046 /* 1047 * Set new address 1048 */ 1049 fxp_init(ifp); 1050 break; 1051 } 1052 #endif 1053 #ifdef NS 1054 /* 1055 * XXX - This code is probably wrong 1056 */ 1057 case AF_NS: 1058 { 1059 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 1060 1061 if (ns_nullhost(*ina)) 1062 ina->x_host = 1063 *(union ns_host *) (sc->arpcom.ac_enaddr); 1064 else { 1065 bcopy((caddr_t) ina->x_host.c_host, 1066 (caddr_t) sc->arpcom.ac_enaddr, 1067 sizeof(sc->arpcom.ac_enaddr)); 1068 } 1069 1070 /* 1071 * Set new address 1072 */ 1073 fxp_init(ifp); 1074 break; 1075 } 1076 #endif 1077 default: 1078 fxp_init(ifp); 1079 break; 1080 } 1081 break; 1082 1083 case SIOCGIFADDR: 1084 { 1085 struct sockaddr *sa; 1086 1087 sa = (struct sockaddr *) & ifr->ifr_data; 1088 bcopy((caddr_t) sc->arpcom.ac_enaddr, 1089 (caddr_t) sa->sa_data, sizeof(sc->arpcom.ac_enaddr)); 1090 } 1091 break; 1092 1093 case SIOCSIFFLAGS: 1094 1095 /* 1096 * If interface is marked up and not running, then start it. 1097 * If it is marked down and running, stop it. 1098 * XXX If it's up then re-initialize it. This is so flags 1099 * such as IFF_PROMISC are handled. 1100 */ 1101 if (ifp->if_flags & IFF_UP) { 1102 fxp_init(ifp); 1103 } else { 1104 if (ifp->if_flags & IFF_RUNNING) 1105 fxp_stop(sc); 1106 } 1107 break; 1108 1109 case SIOCADDMULTI: 1110 case SIOCDELMULTI: 1111 /* 1112 * Update out multicast list. 1113 */ 1114 error = (command == SIOCADDMULTI) ? 1115 ether_addmulti(ifr, &sc->arpcom) : 1116 ether_delmulti(ifr, &sc->arpcom); 1117 1118 if (error == ENETRESET) { 1119 /* 1120 * Multicast list has changed; set the hardware filter 1121 * accordingly. 1122 */ 1123 fxp_init(ifp); 1124 1125 error = 0; 1126 } 1127 break; 1128 1129 case SIOCSIFMTU: 1130 /* 1131 * Set the interface MTU. 1132 */ 1133 if (ifr->ifr_mtu > ETHERMTU) { 1134 error = EINVAL; 1135 } else { 1136 ifp->if_mtu = ifr->ifr_mtu; 1137 } 1138 break; 1139 1140 default: 1141 error = EINVAL; 1142 } 1143 (void) splx(s); 1144 return (error); 1145 } 1146