if_de.c revision 8876
1/*- 2 * Copyright (c) 1994, 1995 Matt Thomas (matt@lkg.dec.com) 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, this list of conditions and the following disclaimer. 10 * 2. The name of the author may not be used to endorse or promote products 11 * derived from this software withough specific prior written permission 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * 24 * $Id: if_de.c,v 1.27 1995/05/26 02:02:44 davidg Exp $ 25 * 26 */ 27 28/* 29 * DEC DC21040 PCI Ethernet Controller 30 * 31 * Written by Matt Thomas 32 * BPF support code stolen directly from if_ec.c 33 * 34 * This driver supports the DEC DE435 or any other PCI 35 * board which support DC21040 or DC21140 (mostly). 36 */ 37 38#if defined(__FreeBSD__) 39#include "de.h" 40#endif 41#if NDE > 0 || !defined(__FreeBSD__) 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/mbuf.h> 46#include <sys/protosw.h> 47#include <sys/socket.h> 48#include <sys/ioctl.h> 49#include <sys/errno.h> 50#include <sys/malloc.h> 51#include <sys/kernel.h> 52#include <sys/proc.h> /* only for declaration of wakeup() used by vm.h */ 53#if defined(__FreeBSD__) 54#include <sys/devconf.h> 55#include <machine/clock.h> 56#elif defined(__bsdi__) || defined(__NetBSD__) 57#include <sys/device.h> 58#endif 59 60#include <net/if.h> 61#include <net/if_types.h> 62#include <net/if_dl.h> 63#include <net/route.h> 64 65#include "bpfilter.h" 66#if NBPFILTER > 0 67#include <net/bpf.h> 68#include <net/bpfdesc.h> 69#endif 70 71#ifdef INET 72#include <netinet/in.h> 73#include <netinet/in_systm.h> 74#include <netinet/in_var.h> 75#include <netinet/ip.h> 76#include <netinet/if_ether.h> 77#endif 78 79#ifdef NS 80#include <netns/ns.h> 81#include <netns/ns_if.h> 82#endif 83 84#include <vm/vm.h> 85#include <vm/vm_kern.h> 86#include <vm/vm_param.h> 87 88#if defined(__FreeBSD__) 89#include <pci.h> 90#if NPCI > 0 91#include <pci/pcivar.h> 92#include <pci/dc21040.h> 93#endif 94#endif 95 96#if defined(__bsdi__) 97#include <i386/pci/pci.h> 98#include <i386/pci/dc21040.h> 99#include <i386/isa/isa.h> 100#include <i386/isa/icu.h> 101#include <i386/isa/dma.h> 102#include <i386/isa/isavar.h> 103#endif 104 105#if defined(__NetBSD__) 106#include <dev/pci/pcivar.h> 107#include <dev/ic/dc21040.h> 108#endif 109 110/* 111 * This module supports 112 * the DEC DC21040 PCI Ethernet Controller. 113 * the DEC DC21140 PCI Fast Ethernet Controller. 114 */ 115 116typedef struct { 117 unsigned long addr; 118 unsigned long length; 119} tulip_addrvec_t; 120 121typedef struct { 122 tulip_desc_t *ri_first; 123 tulip_desc_t *ri_last; 124 tulip_desc_t *ri_nextin; 125 tulip_desc_t *ri_nextout; 126 int ri_max; 127 int ri_free; 128} tulip_ringinfo_t; 129 130typedef struct { 131 volatile tulip_uint32_t *csr_busmode; /* CSR0 */ 132 volatile tulip_uint32_t *csr_txpoll; /* CSR1 */ 133 volatile tulip_uint32_t *csr_rxpoll; /* CSR2 */ 134 volatile tulip_uint32_t *csr_rxlist; /* CSR3 */ 135 volatile tulip_uint32_t *csr_txlist; /* CSR4 */ 136 volatile tulip_uint32_t *csr_status; /* CSR5 */ 137 volatile tulip_uint32_t *csr_command; /* CSR6 */ 138 volatile tulip_uint32_t *csr_intr; /* CSR7 */ 139 volatile tulip_uint32_t *csr_missed_frame; /* CSR8 */ 140 141 /* DC21040 specific registers */ 142 143 volatile tulip_sint32_t *csr_enetrom; /* CSR9 */ 144 volatile tulip_uint32_t *csr_reserved; /* CSR10 */ 145 volatile tulip_uint32_t *csr_full_duplex; /* CSR11 */ 146 volatile tulip_uint32_t *csr_sia_status; /* CSR12 */ 147 volatile tulip_uint32_t *csr_sia_connectivity; /* CSR13 */ 148 volatile tulip_uint32_t *csr_sia_tx_rx; /* CSR14 */ 149 volatile tulip_uint32_t *csr_sia_general; /* CSR15 */ 150 151 /* DC21140/DC21041 specific registers */ 152 153 volatile tulip_uint32_t *csr_srom_mii; /* CSR9 */ 154 volatile tulip_uint32_t *csr_gp_timer; /* CSR11 */ 155 volatile tulip_uint32_t *csr_gp; /* CSR12 */ 156 volatile tulip_uint32_t *csr_watchdog; /* CSR15 */ 157} tulip_regfile_t; 158 159/* 160 * macros to read and write CSRs. Note that the "0 +" in 161 * READ_CSR is to prevent the macro from being an lvalue 162 * and WRITE_CSR shouldn't be assigned from. 163 */ 164#define TULIP_READ_CSR(sc, csr) (0 + *(sc)->tulip_csrs.csr) 165#ifndef __alpha__ 166#define TULIP_WRITE_CSR(sc, csr, val) \ 167 ((void)(*(sc)->tulip_csrs.csr = (val))) 168#else 169#define TULIP_WRITE_CSR(sc, csr, val) \ 170 ((void)(*(sc)->tulip_csrs.csr = (val), MB())) 171#endif 172 173/* 174 * The DC21040 has a stupid restriction in that the receive 175 * buffers must be longword aligned. But since Ethernet 176 * headers are not a multiple of longwords in size this forces 177 * the data to non-longword aligned. Since IP requires the 178 * data to be longword aligned, we need to copy it after it has 179 * been DMA'ed in our memory. 180 * 181 * Since we have to copy it anyways, we might as well as allocate 182 * dedicated receive space for the input. This allows to use a 183 * small receive buffer size and more ring entries to be able to 184 * better keep with a flood of tiny Ethernet packets. 185 * 186 * The receive space MUST ALWAYS be a multiple of the page size. 187 * And the number of receive descriptors multiplied by the size 188 * of the receive buffers must equal the recevive space. This 189 * is so that we can manipulate the page tables so that even if a 190 * packet wraps around the end of the receive space, we can 191 * treat it as virtually contiguous. 192 * 193 * The above used to be true (the stupid restriction is still true) 194 * but we gone to directly DMA'ing into MBUFs because with 100Mb 195 * cards the copying is just too much of a hit. 196 */ 197#if defined(__alpha__) 198#define TULIP_COPY_RXDATA 1 199#endif 200 201 202#define TULIP_RXDESCS 16 203#define TULIP_TXDESCS 128 204#define TULIP_RXQ_TARGET 8 205 206typedef enum { 207 TULIP_DC21040_GENERIC, 208 TULIP_DC21140_DEC_EB, 209 TULIP_DC21140_DEC_DE500, 210 TULIP_DC21140_COGENT_EM100 211} tulip_board_t; 212 213typedef struct _tulip_softc_t tulip_softc_t; 214 215typedef struct { 216 tulip_board_t bd_type; 217 const char *bd_description; 218 int (*bd_media_probe)(tulip_softc_t *sc); 219 void (*bd_media_select)(tulip_softc_t *sc); 220} tulip_boardsw_t; 221 222typedef enum { TULIP_DC21040, TULIP_DC21140, TULIP_DC21041 } tulip_chipid_t; 223 224struct _tulip_softc_t { 225#if defined(__bsdi__) 226 struct device tulip_dev; /* base device */ 227 struct isadev tulip_id; /* ISA device */ 228 struct intrhand tulip_ih; /* intrrupt vectoring */ 229 struct atshutdown tulip_ats; /* shutdown routine */ 230#endif 231#if defined(__NetBSD__) 232 struct device tulip_dev; /* base device */ 233 void *tulip_ih; /* intrrupt vectoring */ 234 /* XXX no shutdown routine? */ 235#endif 236 struct arpcom tulip_ac; 237 tulip_regfile_t tulip_csrs; 238 unsigned tulip_flags; 239#define TULIP_WANTSETUP 0x01 240#define TULIP_WANTHASH 0x02 241#define TULIP_DOINGSETUP 0x04 242#define TULIP_ALTPHYS 0x08 /* use AUI */ 243 unsigned char tulip_rombuf[128]; 244 tulip_uint32_t tulip_setupbuf[192/sizeof(tulip_uint32_t)]; 245 tulip_uint32_t tulip_setupdata[192/sizeof(tulip_uint32_t)]; 246 tulip_uint32_t tulip_intrmask; 247 tulip_uint32_t tulip_cmdmode; 248 tulip_uint32_t tulip_revinfo; 249 tulip_chipid_t tulip_chipid; 250 const tulip_boardsw_t *tulip_boardsw; 251 struct ifqueue tulip_txq; 252 struct ifqueue tulip_rxq; 253 tulip_ringinfo_t tulip_rxinfo; 254 tulip_ringinfo_t tulip_txinfo; 255}; 256 257#ifndef IFF_ALTPHYS 258#define IFF_ALTPHYS IFF_LINK0 /* In case it isn't defined */ 259#endif 260static const char *tulip_chipdescs[] = { 261 "DC21040 [10Mb/s]", 262 "DC21140 [10-100Mb/s]", 263 "DC21041 [10Mb/s]" 264}; 265 266#if defined(__FreeBSD__) 267typedef void ifnet_ret_t; 268typedef int ioctl_cmd_t; 269tulip_softc_t *tulips[NDE]; 270#define TULIP_UNIT_TO_SOFTC(unit) (tulips[unit]) 271#endif 272#if defined(__bsdi__) 273typedef int ifnet_ret_t; 274typedef int ioctl_cmd_t; 275extern struct cfdriver decd; 276#define TULIP_UNIT_TO_SOFTC(unit) ((tulip_softc_t *) decd.cd_devs[unit]) 277#endif 278#if defined(__NetBSD__) 279typedef void ifnet_ret_t; 280typedef u_long ioctl_cmd_t; 281extern struct cfdriver decd; 282#define TULIP_UNIT_TO_SOFTC(unit) ((tulip_softc_t *) decd.cd_devs[unit]) 283#endif 284 285#define tulip_if tulip_ac.ac_if 286#define tulip_unit tulip_ac.ac_if.if_unit 287#define tulip_name tulip_ac.ac_if.if_name 288#define tulip_bpf tulip_ac.ac_if.if_bpf 289#define tulip_hwaddr tulip_ac.ac_enaddr 290 291#define TULIP_CRC32_POLY 0xEDB88320UL /* CRC-32 Poly -- Little Endian */ 292#define TULIP_CHECK_RXCRC 0 293#define TULIP_MAX_TXSEG 30 294 295#define TULIP_ADDREQUAL(a1, a2) \ 296 (((u_short *)a1)[0] == ((u_short *)a2)[0] \ 297 && ((u_short *)a1)[1] == ((u_short *)a2)[1] \ 298 && ((u_short *)a1)[2] == ((u_short *)a2)[2]) 299#define TULIP_ADDRBRDCST(a1) \ 300 (((u_short *)a1)[0] == 0xFFFFU \ 301 && ((u_short *)a1)[1] == 0xFFFFU \ 302 && ((u_short *)a1)[2] == 0xFFFFU) 303 304static ifnet_ret_t tulip_start(struct ifnet *ifp); 305static void tulip_rx_intr(tulip_softc_t *sc); 306static void tulip_addr_filter(tulip_softc_t *sc); 307 308 309static int 310tulip_dc21040_media_probe( 311 tulip_softc_t * const sc) 312{ 313 int cnt; 314 315 TULIP_WRITE_CSR(sc, csr_sia_connectivity, 0); 316 TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET); 317 for (cnt = 0; cnt < 2400; cnt++) { 318 if ((TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0) 319 break; 320 DELAY(1000); 321 } 322 return (TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) != 0; 323} 324 325static void 326tulip_dc21040_media_select( 327 tulip_softc_t * const sc) 328{ 329 sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT; 330 TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET); 331 if (sc->tulip_if.if_flags & IFF_ALTPHYS) { 332 if ((sc->tulip_flags & TULIP_ALTPHYS) == 0) 333 printf("%s%d: enabling Thinwire/AUI port\n", 334 sc->tulip_if.if_name, sc->tulip_if.if_unit); 335 TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_AUI); 336 sc->tulip_flags |= TULIP_ALTPHYS; 337 } else { 338 if (sc->tulip_flags & TULIP_ALTPHYS) 339 printf("%s%d: enabling 10baseT/UTP port\n", 340 sc->tulip_if.if_name, sc->tulip_if.if_unit); 341 TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET); 342 sc->tulip_flags &= ~TULIP_ALTPHYS; 343 } 344} 345 346static const tulip_boardsw_t tulip_dc21040_boardsw = { 347 TULIP_DC21040_GENERIC, 348 "", 349 tulip_dc21040_media_probe, 350 tulip_dc21040_media_select 351}; 352 353static int 354tulip_dc21140_evalboard_media_probe( 355 tulip_softc_t * const sc) 356{ 357 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_PINS); 358 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_INIT); 359 TULIP_WRITE_CSR(sc, csr_command, 360 TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT | 361 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE); 362 TULIP_WRITE_CSR(sc, csr_command, 363 TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL); 364 DELAY(1000000); 365 return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_EB_OK100) != 0; 366} 367 368static void 369tulip_dc21140_evalboard_media_select( 370 tulip_softc_t * const sc) 371{ 372 sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE; 373 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_PINS); 374 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_INIT); 375 if (sc->tulip_if.if_flags & IFF_ALTPHYS) { 376 if ((sc->tulip_flags & TULIP_ALTPHYS) == 0) 377 printf("%s%d: enabling 100baseTX UTP port\n", 378 sc->tulip_if.if_name, sc->tulip_if.if_unit); 379 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT 380 |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER; 381 sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL; 382 sc->tulip_flags |= TULIP_ALTPHYS; 383 } else { 384 if (sc->tulip_flags & TULIP_ALTPHYS) 385 printf("%s%d: enabling 10baseT UTP port\n", 386 sc->tulip_if.if_name, sc->tulip_if.if_unit); 387 sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT 388 |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER); 389 sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL; 390 sc->tulip_flags &= ~TULIP_ALTPHYS; 391 } 392} 393 394static const tulip_boardsw_t tulip_dc21140_eb_boardsw = { 395 TULIP_DC21140_DEC_EB, 396 "", 397 tulip_dc21140_evalboard_media_probe, 398 tulip_dc21140_evalboard_media_select 399}; 400 401 402static int 403tulip_dc21140_cogent_em100_media_probe( 404 tulip_softc_t * const sc) 405{ 406 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_PINS); 407 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_INIT); 408 TULIP_WRITE_CSR(sc, csr_command, 409 TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT | 410 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE); 411 TULIP_WRITE_CSR(sc, csr_command, 412 TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL); 413 return 1; 414} 415 416static void 417tulip_dc21140_cogent_em100_media_select( 418 tulip_softc_t * const sc) 419{ 420 sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE; 421 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_PINS); 422 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_INIT); 423 if ((sc->tulip_flags & TULIP_ALTPHYS) == 0) 424 printf("%s%d: enabling 100baseTX UTP port\n", 425 sc->tulip_if.if_name, sc->tulip_if.if_unit); 426 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT 427 |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER; 428 sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL; 429 sc->tulip_flags |= TULIP_ALTPHYS; 430} 431 432static const tulip_boardsw_t tulip_dc21140_cogent_em100_boardsw = { 433 TULIP_DC21140_COGENT_EM100, 434 "Cogent EM100", 435 tulip_dc21140_cogent_em100_media_probe, 436 tulip_dc21140_cogent_em100_media_select 437}; 438 439static int 440tulip_dc21140_de500_media_probe( 441 tulip_softc_t * const sc) 442{ 443 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_PINS); 444 DELAY(1000); 445 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_HALFDUPLEX); 446 if ((TULIP_READ_CSR(sc, csr_gp) & 447 (TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10)) != 448 (TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10)) 449 return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_DE500_NOTOK_100) != 0; 450 TULIP_WRITE_CSR(sc, csr_gp, 451 TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100); 452 TULIP_WRITE_CSR(sc, csr_command, 453 TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT | 454 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE); 455 TULIP_WRITE_CSR(sc, csr_command, 456 TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL); 457 DELAY(1000000); 458 return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_DE500_NOTOK_100) != 0; 459} 460 461static void 462tulip_dc21140_de500_media_select( 463 tulip_softc_t * const sc) 464{ 465 sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE; 466 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_PINS); 467 if (sc->tulip_if.if_flags & IFF_ALTPHYS) { 468 if ((sc->tulip_flags & TULIP_ALTPHYS) == 0) 469 printf("%s%d: enabling 100baseTX UTP port\n", 470 sc->tulip_if.if_name, sc->tulip_if.if_unit); 471 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT 472 |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER; 473 sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL; 474 sc->tulip_flags |= TULIP_ALTPHYS; 475 TULIP_WRITE_CSR(sc, csr_gp, 476 TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100); 477 } else { 478 if (sc->tulip_flags & TULIP_ALTPHYS) 479 printf("%s%d: enabling 10baseT UTP port\n", 480 sc->tulip_if.if_name, sc->tulip_if.if_unit); 481 sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT 482 |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER); 483 sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL; 484 sc->tulip_flags &= ~TULIP_ALTPHYS; 485 TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_HALFDUPLEX); 486 } 487} 488 489static const tulip_boardsw_t tulip_dc21140_de500_boardsw = { 490 TULIP_DC21140_DEC_DE500, "Digital DE500 ", 491 tulip_dc21140_de500_media_probe, 492 tulip_dc21140_de500_media_select 493}; 494 495static void 496tulip_reset( 497 tulip_softc_t * const sc) 498{ 499 tulip_ringinfo_t *ri; 500 tulip_desc_t *di; 501 502 TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET); 503 DELAY(10); /* Wait 10 microsends (actually 50 PCI cycles but at 504 33MHz that comes to two microseconds but wait a 505 bit longer anyways) */ 506 507 (*sc->tulip_boardsw->bd_media_select)(sc); 508 509 TULIP_WRITE_CSR(sc, csr_txlist, vtophys(&sc->tulip_txinfo.ri_first[0])); 510 TULIP_WRITE_CSR(sc, csr_rxlist, vtophys(&sc->tulip_rxinfo.ri_first[0])); 511 TULIP_WRITE_CSR(sc, csr_intr, 0); 512 TULIP_WRITE_CSR(sc, csr_busmode, 513 TULIP_BUSMODE_BURSTLEN_8LW|TULIP_BUSMODE_CACHE_ALIGN8 514 |(BYTE_ORDER != LITTLE_ENDIAN ? TULIP_BUSMODE_BIGENDIAN : 0)); 515 516 sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS; 517 /* 518 * Free all the mbufs that were on the transmit ring. 519 */ 520 for (;;) { 521 struct mbuf *m; 522 IF_DEQUEUE(&sc->tulip_txq, m); 523 if (m == NULL) 524 break; 525 m_freem(m); 526 } 527 528 ri = &sc->tulip_txinfo; 529 ri->ri_nextin = ri->ri_nextout = ri->ri_first; 530 ri->ri_free = ri->ri_max; 531 for (di = ri->ri_first; di < ri->ri_last; di++) 532 di->d_status = 0; 533 534 /* 535 * We need to collect all the mbufs were on the 536 * receive ring before we reinit it either to put 537 * them back on or to know if we have to allocate 538 * more. 539 */ 540 ri = &sc->tulip_rxinfo; 541 ri->ri_nextin = ri->ri_nextout = ri->ri_first; 542 ri->ri_free = ri->ri_max; 543 for (di = ri->ri_first; di < ri->ri_last; di++) { 544 di->d_status = 0; 545 di->d_length1 = 0; di->d_addr1 = 0; 546 di->d_length2 = 0; di->d_addr2 = 0; 547 } 548 for (;;) { 549 struct mbuf *m; 550 IF_DEQUEUE(&sc->tulip_rxq, m); 551 if (m == NULL) 552 break; 553 m_freem(m); 554 } 555 556 sc->tulip_intrmask = TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR 557 |TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED 558 |TULIP_STS_TXBABBLE|TULIP_STS_LINKFAIL|TULIP_STS_RXSTOPPED; 559 sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP); 560 tulip_addr_filter(sc); 561} 562 563static void 564tulip_init( 565 tulip_softc_t * const sc) 566{ 567 if (sc->tulip_if.if_flags & IFF_UP) { 568 sc->tulip_if.if_flags |= IFF_RUNNING; 569 if (sc->tulip_if.if_flags & IFF_PROMISC) { 570 sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS; 571 } else { 572 sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS; 573 if (sc->tulip_if.if_flags & IFF_ALLMULTI) { 574 sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI; 575 } else { 576 sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI; 577 } 578 } 579 sc->tulip_cmdmode |= TULIP_CMD_TXRUN; 580 if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) { 581 tulip_rx_intr(sc); 582 sc->tulip_cmdmode |= TULIP_CMD_RXRUN; 583 sc->tulip_intrmask |= TULIP_STS_RXSTOPPED; 584 } else { 585 sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED; 586 tulip_start(&sc->tulip_if); 587 } 588 sc->tulip_cmdmode |= TULIP_CMD_THRSHLD160; 589 TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask); 590 TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode); 591 } else { 592 tulip_reset(sc); 593 sc->tulip_if.if_flags &= ~IFF_RUNNING; 594 } 595} 596 597 598#if TULIP_CHECK_RXCRC 599static unsigned 600tulip_crc32( 601 u_char *addr, 602 int len) 603{ 604 unsigned int crc = 0xFFFFFFFF; 605 static unsigned int crctbl[256]; 606 int idx; 607 static int done; 608 /* 609 * initialize the multicast address CRC table 610 */ 611 for (idx = 0; !done && idx < 256; idx++) { 612 unsigned int tmp = idx; 613 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 614 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 615 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 616 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 617 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 618 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 619 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 620 tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0); /* XOR */ 621 crctbl[idx] = tmp; 622 } 623 done = 1; 624 625 while (len-- > 0) 626 crc = (crc >> 8) ^ crctbl[*addr++] ^ crctbl[crc & 0xFF]; 627 628 return crc; 629} 630#endif 631 632static void 633tulip_rx_intr( 634 tulip_softc_t * const sc) 635{ 636 tulip_ringinfo_t * const ri = &sc->tulip_rxinfo; 637 struct ifnet * const ifp = &sc->tulip_if; 638 639 for (;;) { 640 struct ether_header eh; 641 tulip_desc_t *eop = ri->ri_nextin; 642 int total_len = 0; 643 struct mbuf *m = NULL; 644 int accept = 0; 645 646 if (sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET) 647 goto queue_mbuf; 648 649 if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER) 650 break; 651 652 total_len = ((eop->d_status >> 16) & 0x7FF) - 4; 653 IF_DEQUEUE(&sc->tulip_rxq, m); 654 if ((eop->d_status & TULIP_DSTS_ERRSUM) == 0) { 655 656#if TULIP_CHECK_RXCRC 657 unsigned crc = tulip_crc32(mtod(m, unsigned char *), total_len); 658 if (~crc != *((unsigned *) &bufaddr[total_len])) { 659 printf("%s%d: bad rx crc: %08x [rx] != %08x\n", 660 sc->tulip_name, sc->tulip_unit, 661 *((unsigned *) &bufaddr[total_len]), ~crc); 662 goto next; 663 } 664#endif 665 eh = *mtod(m, struct ether_header *); 666#if NBPFILTER > 0 667 if (sc->tulip_bpf != NULL) 668 bpf_tap(sc->tulip_bpf, mtod(m, caddr_t), total_len); 669#endif 670 if ((sc->tulip_if.if_flags & IFF_PROMISC) 671 && (eh.ether_dhost[0] & 1) == 0 672 && !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr)) 673 goto next; 674 accept = 1; 675 total_len -= sizeof(struct ether_header); 676 } else { 677 ifp->if_ierrors++; 678 } 679 next: 680 ifp->if_ipackets++; 681 if (++ri->ri_nextin == ri->ri_last) 682 ri->ri_nextin = ri->ri_first; 683 queue_mbuf: 684 /* 685 * Either we are priming the TULIP with mbufs (m == NULL) 686 * or we are about to accept an mbuf for the upper layers 687 * so we need to allocate an mbuf to replace it. If we 688 * can't replace, then count it as an input error and reuse 689 * the mbuf. 690 */ 691 if (accept || m == NULL) { 692 struct mbuf *m0; 693 MGETHDR(m0, M_DONTWAIT, MT_DATA); 694 if (m0 != NULL) { 695#if defined(TULIP_COPY_RXDATA) 696 if (!accept || total_len >= MHLEN) { 697#endif 698 MCLGET(m0, M_DONTWAIT); 699 if ((m0->m_flags & M_EXT) == 0) { 700 m_freem(m0); 701 m0 = NULL; 702 } 703#if defined(TULIP_COPY_RXDATA) 704 } 705#endif 706 } 707 if (accept) { 708 if (m0 != NULL) { 709#if defined(__bsdi__) 710 eh.ether_type = ntohs(eh.ether_type); 711#endif 712#if !defined(TULIP_COPY_RXDATA) 713 m->m_data += sizeof(struct ether_header); 714 m->m_len = m->m_pkthdr.len = total_len; 715 m->m_pkthdr.rcvif = ifp; 716 ether_input(ifp, &eh, m); 717 m = m0; 718#else 719 bcopy(mtod(m, caddr_t) + sizeof(struct ether_header), 720 mtod(m0, caddr_t), total_len); 721 m0->m_len = m0->m_pkthdr.len = total_len; 722 m0->m_pkthdr.rcvif = ifp; 723 ether_input(ifp, &eh, m0); 724#endif 725 } else { 726 ifp->if_ierrors++; 727 } 728 } else { 729 m = m0; 730 } 731 } 732 if (m == NULL) 733 break; 734 /* 735 * Now give the buffer to the TULIP and save in our 736 * receive queue. 737 */ 738 ri->ri_nextout->d_length1 = MCLBYTES - 4; 739 ri->ri_nextout->d_addr1 = vtophys(mtod(m, caddr_t)); 740 ri->ri_nextout->d_status = TULIP_DSTS_OWNER; 741 if (++ri->ri_nextout == ri->ri_last) 742 ri->ri_nextout = ri->ri_first; 743 IF_ENQUEUE(&sc->tulip_rxq, m); 744 } 745} 746 747static int 748tulip_tx_intr( 749 tulip_softc_t * const sc) 750{ 751 tulip_ringinfo_t * const ri = &sc->tulip_txinfo; 752 struct mbuf *m; 753 int xmits = 0; 754 755 while (ri->ri_free < ri->ri_max) { 756 if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER) 757 break; 758 759 if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxLASTSEG) { 760 if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxSETUPPKT) { 761 /* 762 * We've just finished processing a setup packet. 763 * Mark that we can finished it. If there's not 764 * another pending, startup the TULIP receiver. 765 * Make sure we ack the RXSTOPPED so we won't get 766 * an abormal interrupt indication. 767 */ 768 sc->tulip_flags &= ~TULIP_DOINGSETUP; 769 if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) { 770 tulip_rx_intr(sc); 771 sc->tulip_cmdmode |= TULIP_CMD_RXRUN; 772 sc->tulip_intrmask |= TULIP_STS_RXSTOPPED; 773 TULIP_WRITE_CSR(sc, csr_status, TULIP_STS_RXSTOPPED); 774 TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode); 775 TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask); 776 } 777 } else { 778 IF_DEQUEUE(&sc->tulip_txq, m); 779 m_freem(m); 780 sc->tulip_if.if_collisions += 781 (ri->ri_nextin->d_status & TULIP_DSTS_TxCOLLMASK) 782 >> TULIP_DSTS_V_TxCOLLCNT; 783 if (ri->ri_nextin->d_status & TULIP_DSTS_ERRSUM) 784 sc->tulip_if.if_oerrors++; 785 xmits++; 786 } 787 } 788 789 if (++ri->ri_nextin == ri->ri_last) 790 ri->ri_nextin = ri->ri_first; 791 ri->ri_free++; 792 sc->tulip_if.if_flags &= ~IFF_OACTIVE; 793 } 794 sc->tulip_if.if_opackets += xmits; 795 return xmits; 796} 797 798static ifnet_ret_t 799tulip_start( 800 struct ifnet * const ifp) 801{ 802 tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(ifp->if_unit); 803 struct ifqueue * const ifq = &ifp->if_snd; 804 tulip_ringinfo_t * const ri = &sc->tulip_txinfo; 805 struct mbuf *m, *m0; 806 807 if ((ifp->if_flags & IFF_RUNNING) == 0) 808 return; 809 810 for (;;) { 811 tulip_desc_t *eop, *nextout; 812 int segcnt, free, recopy; 813 tulip_uint32_t d_status; 814 815 if (sc->tulip_flags & TULIP_WANTSETUP) { 816 if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) { 817 ifp->if_flags |= IFF_OACTIVE; 818 return; 819 } 820 bcopy(sc->tulip_setupdata, sc->tulip_setupbuf, 821 sizeof(sc->tulip_setupbuf)); 822 sc->tulip_flags &= ~TULIP_WANTSETUP; 823 sc->tulip_flags |= TULIP_DOINGSETUP; 824 ri->ri_free--; 825 ri->ri_nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN; 826 ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG 827 |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR; 828 if (sc->tulip_flags & TULIP_WANTHASH) 829 ri->ri_nextout->d_flag |= TULIP_DFLAG_TxHASHFILT; 830 ri->ri_nextout->d_length1 = sizeof(sc->tulip_setupbuf); 831 ri->ri_nextout->d_addr1 = vtophys(sc->tulip_setupbuf); 832 ri->ri_nextout->d_length2 = 0; 833 ri->ri_nextout->d_addr2 = 0; 834 ri->ri_nextout->d_status = TULIP_DSTS_OWNER; 835 TULIP_WRITE_CSR(sc, csr_txpoll, 1); 836 /* 837 * Advance the ring for the next transmit packet. 838 */ 839 if (++ri->ri_nextout == ri->ri_last) 840 ri->ri_nextout = ri->ri_first; 841 /* 842 * Make sure the next descriptor is owned by us since it 843 * may have been set up above if we ran out of room in the 844 * ring. 845 */ 846 ri->ri_nextout->d_status = 0; 847 } 848 849 IF_DEQUEUE(ifq, m); 850 if (m == NULL) 851 break; 852 853 /* 854 * Now we try to fill in our transmit descriptors. This is 855 * a bit reminiscent of going on the Ark two by two 856 * since each descriptor for the TULIP can describe 857 * two buffers. So we advance through packet filling 858 * each of the two entries at a time to to fill each 859 * descriptor. Clear the first and last segment bits 860 * in each descriptor (actually just clear everything 861 * but the end-of-ring or chain bits) to make sure 862 * we don't get messed up by previously sent packets. 863 * 864 * We may fail to put the entire packet on the ring if 865 * there is either not enough ring entries free or if the 866 * packet has more than MAX_TXSEG segments. In the former 867 * case we will just wait for the ring to empty. In the 868 * latter case we have to recopy. 869 */ 870 d_status = 0; 871 recopy = 0; 872 eop = nextout = ri->ri_nextout; 873 m0 = m; 874 segcnt = 0; 875 free = ri->ri_free; 876 do { 877 int len = m0->m_len; 878 caddr_t addr = mtod(m0, caddr_t); 879 unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1)); 880 881 while (len > 0) { 882 unsigned slen = min(len, clsize); 883 884 segcnt++; 885 if (segcnt > TULIP_MAX_TXSEG) { 886 recopy = 1; 887 m0 = NULL; /* to break out of outside loop */ 888 break; 889 } 890 if (segcnt & 1) { 891 if (--free == 0) { 892 /* 893 * There's no more room but since nothing 894 * has been committed at this point, just 895 * show output is active, put back the 896 * mbuf and return. 897 */ 898 ifp->if_flags |= IFF_OACTIVE; 899 IF_PREPEND(ifq, m); 900 return; 901 } 902 eop = nextout; 903 if (++nextout == ri->ri_last) 904 nextout = ri->ri_first; 905 eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN; 906 eop->d_status = d_status; 907 eop->d_addr1 = vtophys(addr); eop->d_length1 = slen; 908 } else { 909 /* 910 * Fill in second half of descriptor 911 */ 912 eop->d_addr2 = vtophys(addr); eop->d_length2 = slen; 913 } 914 d_status = TULIP_DSTS_OWNER; 915 len -= slen; 916 addr += slen; 917 clsize = CLBYTES; 918 } 919 } while ((m0 = m0->m_next) != NULL); 920 921 /* 922 * The packet exceeds the number of transmit buffer 923 * entries that we can use for one packet, so we have 924 * recopy it into one mbuf and then try again. 925 */ 926 if (recopy) { 927 MGETHDR(m0, M_DONTWAIT, MT_DATA); 928 if (m0 != NULL) { 929 if (m->m_pkthdr.len > MHLEN) { 930 MCLGET(m0, M_DONTWAIT); 931 if ((m0->m_flags & M_EXT) == 0) { 932 m_freem(m); 933 m_freem(m0); 934 continue; 935 } 936 } 937 m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t)); 938 m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len; 939 IF_PREPEND(ifq, m0); 940 } 941 m_freem(m); 942 continue; 943 } 944 945 /* 946 * The descriptors have been filled in. Now get ready 947 * to transmit. 948 */ 949#if NBPFILTER > 0 950 if (sc->tulip_bpf != NULL) 951 bpf_mtap(sc->tulip_bpf, m); 952#endif 953 IF_ENQUEUE(&sc->tulip_txq, m); 954 955 /* 956 * Make sure the next descriptor after this packet is owned 957 * by us since it may have been set up above if we ran out 958 * of room in the ring. 959 */ 960 nextout->d_status = 0; 961 962 /* 963 * If we only used the first segment of the last descriptor, 964 * make sure the second segment will not be used. 965 */ 966 if (segcnt & 1) { 967 eop->d_addr2 = 0; 968 eop->d_length2 = 0; 969 } 970 971 /* 972 * Mark the last and first segments, indicate we want a transmit 973 * complete interrupt, give the descriptors to the TULIP, and tell 974 * it to transmit! 975 */ 976 eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR; 977 978 /* 979 * Note that ri->ri_nextout is still the start of the packet 980 * and until we set the OWNER bit, we can still back out of 981 * everything we have done. 982 */ 983 ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG; 984 ri->ri_nextout->d_status = TULIP_DSTS_OWNER; 985 986 /* 987 * This advances the ring for us. 988 */ 989 ri->ri_nextout = nextout; 990 ri->ri_free = free; 991 992 TULIP_WRITE_CSR(sc, csr_txpoll, 1); 993 } 994 if (m != NULL) { 995 ifp->if_flags |= IFF_OACTIVE; 996 IF_PREPEND(ifq, m); 997 } 998} 999 1000static int 1001tulip_intr( 1002 void *arg) 1003{ 1004 tulip_softc_t * const sc = (tulip_softc_t *) arg; 1005 tulip_uint32_t csr; 1006 int progress=0; 1007 1008 while ((csr = TULIP_READ_CSR(sc, csr_status)) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR)) { 1009 progress = 1; 1010 TULIP_WRITE_CSR(sc, csr_status, csr & sc->tulip_intrmask); 1011 1012 if (csr & TULIP_STS_SYSERROR) { 1013 if ((csr & TULIP_STS_ERRORMASK) == TULIP_STS_ERR_PARITY) { 1014 tulip_reset(sc); 1015 tulip_init(sc); 1016 break; 1017 } 1018 } 1019 if (csr & TULIP_STS_ABNRMLINTR) { 1020 printf("%s%d: abnormal interrupt: 0x%05x [0x%05x]\n", 1021 sc->tulip_name, sc->tulip_unit, csr, csr & sc->tulip_intrmask); 1022 TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode); 1023 } 1024 if (csr & TULIP_STS_RXINTR) 1025 tulip_rx_intr(sc); 1026 if (sc->tulip_txinfo.ri_free < sc->tulip_txinfo.ri_max) { 1027 tulip_tx_intr(sc); 1028 tulip_start(&sc->tulip_if); 1029 } 1030 } 1031 return (progress); 1032} 1033 1034/* 1035 * 1036 */ 1037 1038static void 1039tulip_delay_300ns( 1040 tulip_softc_t * const sc) 1041{ 1042 TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode); 1043 TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode); 1044 1045 TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode); 1046 TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode); 1047 1048 TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode); 1049 TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode); 1050} 1051 1052#define EMIT do { TULIP_WRITE_CSR(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0) 1053 1054static void 1055tulip_idle_srom( 1056 tulip_softc_t * const sc) 1057{ 1058 unsigned bit, csr; 1059 1060 csr = SROMSEL | SROMRD; EMIT; 1061 csr ^= SROMCS; EMIT; 1062 csr ^= SROMCLKON; EMIT; 1063 1064 /* 1065 * Write 25 cycles of 0 which will force the SROM to be idle. 1066 */ 1067 for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) { 1068 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */ 1069 csr ^= SROMCLKON; EMIT; /* clock high; data valid */ 1070 } 1071 csr ^= SROMCLKOFF; EMIT; 1072 csr ^= SROMCS; EMIT; EMIT; 1073 csr = 0; EMIT; 1074} 1075 1076 1077static void 1078tulip_read_srom( 1079 tulip_softc_t * const sc) 1080{ 1081 int idx; 1082 const unsigned bitwidth = SROM_BITWIDTH; 1083 const unsigned cmdmask = (SROMCMD_RD << bitwidth); 1084 const unsigned msb = 1 << (bitwidth + 3 - 1); 1085 unsigned lastidx = (1 << bitwidth) - 1; 1086 1087 tulip_idle_srom(sc); 1088 1089 for (idx = 0; idx <= lastidx; idx++) { 1090 unsigned lastbit, data, bits, bit, csr; 1091 csr = SROMSEL | SROMRD; EMIT; 1092 csr ^= SROMCSON; EMIT; 1093 csr ^= SROMCLKON; EMIT; 1094 1095 lastbit = 0; 1096 for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) { 1097 const unsigned thisbit = bits & msb; 1098 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */ 1099 if (thisbit != lastbit) { 1100 csr ^= SROMDOUT; EMIT; /* clock low; invert data */ 1101 } 1102 csr ^= SROMCLKON; EMIT; /* clock high; data valid */ 1103 lastbit = thisbit; 1104 } 1105 csr ^= SROMCLKOFF; EMIT; 1106 1107 for (data = 0, bits = 0; bits < 16; bits++) { 1108 data <<= 1; 1109 csr ^= SROMCLKON; EMIT; /* clock high; data valid */ 1110 data |= TULIP_READ_CSR(sc, csr_srom_mii) & SROMDIN ? 1 : 0; 1111 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */ 1112 } 1113 sc->tulip_rombuf[idx*2] = data & 0xFF; 1114 sc->tulip_rombuf[idx*2+1] = data >> 8; 1115 csr = SROMSEL | SROMRD; EMIT; 1116 csr = 0; EMIT; 1117 } 1118} 1119 1120#define tulip_mchash(mca) (tulip_crc32(mca, 6) & 0x1FF) 1121#define tulip_srom_crcok(databuf) ( \ 1122 (tulip_crc32(databuf, 126) & 0xFFFF) == \ 1123 ((databuf)[126] | ((databuf)[127] << 8))) 1124 1125static unsigned 1126tulip_crc32( 1127 const unsigned char *databuf, 1128 size_t datalen) 1129{ 1130 u_int idx, bit, data, crc = 0xFFFFFFFFUL; 1131 1132 for (idx = 0; idx < datalen; idx++) 1133 for (data = *databuf++, bit = 0; bit < 8; bit++, data >>= 1) 1134 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? TULIP_CRC32_POLY : 0); 1135 return crc; 1136} 1137 1138 1139/* 1140 * This is the standard method of reading the DEC Address ROMS. 1141 */ 1142static int 1143tulip_read_macaddr( 1144 tulip_softc_t *sc) 1145{ 1146 int cksum, rom_cksum, idx; 1147 tulip_sint32_t csr; 1148 unsigned char tmpbuf[8]; 1149 static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA }; 1150 1151 if (sc->tulip_chipid == TULIP_DC21040) { 1152 TULIP_WRITE_CSR(sc, csr_enetrom, 1); 1153 sc->tulip_boardsw = &tulip_dc21040_boardsw; 1154 for (idx = 0; idx < 32; idx++) { 1155 int cnt = 0; 1156 while ((csr = TULIP_READ_CSR(sc, csr_enetrom)) < 0 && cnt < 10000) 1157 cnt++; 1158 sc->tulip_rombuf[idx] = csr & 0xFF; 1159 } 1160 } else { 1161 /* 1162 * Assume all DC21140 board are compatible with the 1163 * DEC 10/100 evaluation board. Not really valid but ... 1164 */ 1165 if (sc->tulip_chipid == TULIP_DC21140) 1166 sc->tulip_boardsw = &tulip_dc21140_eb_boardsw; 1167 tulip_read_srom(sc); 1168 if (tulip_srom_crcok(sc->tulip_rombuf)) { 1169 /* 1170 * New SROM format. Copy out the Ethernet address. 1171 * If it contains a DE500-XA string, then it must be 1172 * a DE500-XA. 1173 */ 1174 bcopy(sc->tulip_rombuf + 20, sc->tulip_hwaddr, 6); 1175 if (bcmp(sc->tulip_rombuf + 29, "DE500-XA", 8) == 0) 1176 sc->tulip_boardsw = &tulip_dc21140_de500_boardsw; 1177 if (sc->tulip_boardsw == NULL) 1178 return -6; 1179 return 0; 1180 } 1181 } 1182 1183 1184 if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) { 1185 /* 1186 * Some folks don't use the standard ethernet rom format 1187 * but instead just put the address in the first 6 bytes 1188 * of the rom and let the rest be all 0xffs. (Can we say 1189 * ZNYX???) 1190 */ 1191 for (idx = 6; idx < 32; idx++) { 1192 if (sc->tulip_rombuf[idx] != 0xFF) 1193 return -4; 1194 } 1195 /* 1196 * Make sure the address is not multicast or locally assigned 1197 * that the OUI is not 00-00-00. 1198 */ 1199 if ((sc->tulip_rombuf[0] & 3) != 0) 1200 return -4; 1201 if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0 1202 && sc->tulip_rombuf[2] == 0) 1203 return -4; 1204 bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6); 1205 return 0; 1206 } 1207 if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0) 1208 return -3; 1209 1210 tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14]; 1211 tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12]; 1212 tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10]; 1213 tmpbuf[6] = sc->tulip_rombuf[9]; tmpbuf[7] = sc->tulip_rombuf[8]; 1214 if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0) 1215 return -2; 1216 1217 bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6); 1218 1219 cksum = *(u_short *) &sc->tulip_hwaddr[0]; 1220 cksum *= 2; 1221 if (cksum > 65535) cksum -= 65535; 1222 cksum += *(u_short *) &sc->tulip_hwaddr[2]; 1223 if (cksum > 65535) cksum -= 65535; 1224 cksum *= 2; 1225 if (cksum > 65535) cksum -= 65535; 1226 cksum += *(u_short *) &sc->tulip_hwaddr[4]; 1227 if (cksum >= 65535) cksum -= 65535; 1228 1229 rom_cksum = *(u_short *) &sc->tulip_rombuf[6]; 1230 1231 if (cksum != rom_cksum) 1232 return -1; 1233 1234 if (sc->tulip_chipid == TULIP_DC21140) { 1235 if (sc->tulip_hwaddr[0] == TULIP_OUI_COGENT_0 1236 && sc->tulip_hwaddr[1] == TULIP_OUI_COGENT_1 1237 && sc->tulip_hwaddr[2] == TULIP_OUI_COGENT_2) { 1238 if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100_ID) 1239 sc->tulip_boardsw = &tulip_dc21140_cogent_em100_boardsw; 1240 } 1241 } 1242 1243 return 0; 1244} 1245 1246static void 1247tulip_addr_filter( 1248 tulip_softc_t * const sc) 1249{ 1250 tulip_uint32_t *sp = sc->tulip_setupdata; 1251 struct ether_multistep step; 1252 struct ether_multi *enm; 1253 int i; 1254 1255 sc->tulip_flags &= ~TULIP_WANTHASH; 1256 sc->tulip_flags |= TULIP_WANTSETUP; 1257 sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN; 1258 sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED; 1259 if (sc->tulip_ac.ac_multicnt > 14) { 1260 unsigned hash; 1261 /* 1262 * If we have more than 14 multicasts, we have 1263 * go into hash perfect mode (512 bit multicast 1264 * hash and one perfect hardware). 1265 */ 1266 1267 bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata)); 1268 hash = tulip_mchash(etherbroadcastaddr); 1269 sp[hash >> 4] |= 1 << (hash & 0xF); 1270 ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm); 1271 while (enm != NULL) { 1272 hash = tulip_mchash(enm->enm_addrlo); 1273 sp[hash >> 4] |= 1 << (hash & 0xF); 1274 ETHER_NEXT_MULTI(step, enm); 1275 } 1276 sc->tulip_flags |= TULIP_WANTHASH; 1277 sp[39] = ((u_short *) sc->tulip_ac.ac_enaddr)[0]; 1278 sp[40] = ((u_short *) sc->tulip_ac.ac_enaddr)[1]; 1279 sp[41] = ((u_short *) sc->tulip_ac.ac_enaddr)[2]; 1280 } else { 1281 /* 1282 * Else can get perfect filtering for 16 addresses. 1283 */ 1284 i = 0; 1285 ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm); 1286 for (; enm != NULL; i++) { 1287 *sp++ = ((u_short *) enm->enm_addrlo)[0]; 1288 *sp++ = ((u_short *) enm->enm_addrlo)[1]; 1289 *sp++ = ((u_short *) enm->enm_addrlo)[2]; 1290 ETHER_NEXT_MULTI(step, enm); 1291 } 1292 /* 1293 * If an IP address is enabled, turn on broadcast 1294 */ 1295 if (sc->tulip_ac.ac_ipaddr.s_addr != 0) { 1296 i++; 1297 *sp++ = 0xFFFF; 1298 *sp++ = 0xFFFF; 1299 *sp++ = 0xFFFF; 1300 } 1301 /* 1302 * Pad the rest with our hardware address 1303 */ 1304 for (; i < 16; i++) { 1305 *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[0]; 1306 *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[1]; 1307 *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[2]; 1308 } 1309 } 1310} 1311 1312/*extern void arp_ifinit(struct arpcom *, struct ifaddr*);*/ 1313 1314static int 1315tulip_ioctl( 1316 struct ifnet * const ifp, 1317 ioctl_cmd_t cmd, 1318 caddr_t data) 1319{ 1320 tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(ifp->if_unit); 1321 struct ifaddr *ifa = (struct ifaddr *)data; 1322 struct ifreq *ifr = (struct ifreq *) data; 1323 int s, error = 0; 1324 1325 s = splimp(); 1326 1327 switch (cmd) { 1328 case SIOCSIFADDR: { 1329 1330 ifp->if_flags |= IFF_UP; 1331 switch(ifa->ifa_addr->sa_family) { 1332#ifdef INET 1333 case AF_INET: { 1334 ((struct arpcom *)ifp)->ac_ipaddr = IA_SIN(ifa)->sin_addr; 1335 tulip_addr_filter(sc); /* reset multicast filtering */ 1336 tulip_init(sc); 1337#if defined(__FreeBSD__) || defined(__NetBSD__) 1338 arp_ifinit((struct arpcom *)ifp, ifa); 1339#elif defined(__bsdi__) 1340 arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr); 1341#endif 1342 break; 1343 } 1344#endif /* INET */ 1345 1346#ifdef NS 1347 /* 1348 * This magic copied from if_is.c; I don't use XNS, 1349 * so I have no way of telling if this actually 1350 * works or not. 1351 */ 1352 case AF_NS: { 1353 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 1354 if (ns_nullhost(*ina)) { 1355 ina->x_host = *(union ns_host *)(sc->tulip_ac.ac_enaddr); 1356 } else { 1357 ifp->if_flags &= ~IFF_RUNNING; 1358 bcopy((caddr_t)ina->x_host.c_host, 1359 (caddr_t)sc->tulip_ac.ac_enaddr, 1360 sizeof sc->tulip_ac.ac_enaddr); 1361 } 1362 1363 tulip_init(sc); 1364 break; 1365 } 1366#endif /* NS */ 1367 1368 default: { 1369 tulip_init(sc); 1370 break; 1371 } 1372 } 1373 break; 1374 } 1375 1376 case SIOCSIFFLAGS: { 1377 /* 1378 * Changing the connection forces a reset. 1379 */ 1380 if (sc->tulip_flags & TULIP_ALTPHYS) { 1381 if ((ifp->if_flags & IFF_ALTPHYS) == 0) 1382 tulip_reset(sc); 1383 } else { 1384 if (ifp->if_flags & IFF_ALTPHYS) 1385 tulip_reset(sc); 1386 } 1387 tulip_init(sc); 1388 break; 1389 } 1390 1391 case SIOCADDMULTI: 1392 case SIOCDELMULTI: { 1393 /* 1394 * Update multicast listeners 1395 */ 1396 if (cmd == SIOCADDMULTI) 1397 error = ether_addmulti(ifr, &sc->tulip_ac); 1398 else 1399 error = ether_delmulti(ifr, &sc->tulip_ac); 1400 1401 if (error == ENETRESET) { 1402 tulip_addr_filter(sc); /* reset multicast filtering */ 1403 tulip_init(sc); 1404 error = 0; 1405 } 1406 break; 1407 } 1408#if defined(SIOCSIFMTU) 1409#if !defined(ifr_mtu) 1410#define ifr_mtu ifr_metric 1411#endif 1412 case SIOCSIFMTU: 1413 /* 1414 * Set the interface MTU. 1415 */ 1416 if (ifr->ifr_mtu > ETHERMTU) { 1417 error = EINVAL; 1418 } else { 1419 ifp->if_mtu = ifr->ifr_mtu; 1420 } 1421 break; 1422#endif 1423 1424 default: { 1425 error = EINVAL; 1426 break; 1427 } 1428 } 1429 1430 splx(s); 1431 return error; 1432} 1433 1434static void 1435tulip_attach( 1436 tulip_softc_t * const sc) 1437{ 1438 struct ifnet * const ifp = &sc->tulip_if; 1439 1440 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST; 1441 ifp->if_ioctl = tulip_ioctl; 1442 ifp->if_output = ether_output; 1443 ifp->if_start = tulip_start; 1444 1445#ifdef __FreeBSD__ 1446 printf("%s%d", sc->tulip_name, sc->tulip_unit); 1447#endif 1448 printf(": %s%s pass %d.%d Ethernet address %s\n", 1449 sc->tulip_boardsw->bd_description, 1450 tulip_chipdescs[sc->tulip_chipid], 1451 (sc->tulip_revinfo & 0xF0) >> 4, 1452 sc->tulip_revinfo & 0x0F, 1453 ether_sprintf(sc->tulip_hwaddr)); 1454 1455 if ((*sc->tulip_boardsw->bd_media_probe)(sc)) { 1456 ifp->if_flags |= IFF_ALTPHYS; 1457 } else { 1458 sc->tulip_flags |= TULIP_ALTPHYS; 1459 } 1460 1461 tulip_reset(sc); 1462 1463 if_attach(ifp); 1464#if defined(__NetBSD__) 1465 ether_ifattach(ifp); 1466#endif 1467 1468#if NBPFILTER > 0 1469 bpfattach(&sc->tulip_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); 1470#endif 1471} 1472 1473static void 1474tulip_initcsrs( 1475 tulip_softc_t * const sc, 1476 volatile tulip_uint32_t *va_csrs, 1477 size_t csr_size) 1478{ 1479 sc->tulip_csrs.csr_busmode = va_csrs + 0 * csr_size; 1480 sc->tulip_csrs.csr_txpoll = va_csrs + 1 * csr_size; 1481 sc->tulip_csrs.csr_rxpoll = va_csrs + 2 * csr_size; 1482 sc->tulip_csrs.csr_rxlist = va_csrs + 3 * csr_size; 1483 sc->tulip_csrs.csr_txlist = va_csrs + 4 * csr_size; 1484 sc->tulip_csrs.csr_status = va_csrs + 5 * csr_size; 1485 sc->tulip_csrs.csr_command = va_csrs + 6 * csr_size; 1486 sc->tulip_csrs.csr_intr = va_csrs + 7 * csr_size; 1487 sc->tulip_csrs.csr_missed_frame = va_csrs + 8 * csr_size; 1488 if (sc->tulip_chipid == TULIP_DC21040) { 1489 sc->tulip_csrs.csr_enetrom = (tulip_sint32_t *) va_csrs + 9 * csr_size; 1490 sc->tulip_csrs.csr_reserved = va_csrs + 10 * csr_size; 1491 sc->tulip_csrs.csr_full_duplex = va_csrs + 11 * csr_size; 1492 sc->tulip_csrs.csr_sia_status = va_csrs + 12 * csr_size; 1493 sc->tulip_csrs.csr_sia_connectivity = va_csrs + 13 * csr_size; 1494 sc->tulip_csrs.csr_sia_tx_rx = va_csrs + 14 * csr_size; 1495 sc->tulip_csrs.csr_sia_general = va_csrs + 15 * csr_size; 1496 } else if (sc->tulip_chipid == TULIP_DC21140 || sc->tulip_chipid == TULIP_DC21041) { 1497 sc->tulip_csrs.csr_srom_mii = va_csrs + 9 * csr_size; 1498 sc->tulip_csrs.csr_gp_timer = va_csrs + 11 * csr_size; 1499 sc->tulip_csrs.csr_gp = va_csrs + 12 * csr_size; 1500 sc->tulip_csrs.csr_watchdog = va_csrs + 15 * csr_size; 1501 } 1502} 1503 1504static void 1505tulip_initring( 1506 tulip_softc_t * const sc, 1507 tulip_ringinfo_t * const ri, 1508 tulip_desc_t *descs, 1509 int ndescs) 1510{ 1511 ri->ri_max = ndescs; 1512 ri->ri_first = descs; 1513 ri->ri_last = ri->ri_first + ri->ri_max; 1514 bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max); 1515 ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING; 1516} 1517 1518/* 1519 * This is the PCI configuration support. Since the DC21040 is available 1520 * on both EISA and PCI boards, one must be careful in how defines the 1521 * DC21040 in the config file. 1522 */ 1523 1524#define PCI_CFID 0x00 /* Configuration ID */ 1525#define PCI_CFCS 0x04 /* Configurtion Command/Status */ 1526#define PCI_CFRV 0x08 /* Configuration Revision */ 1527#define PCI_CFLT 0x0c /* Configuration Latency Timer */ 1528#define PCI_CBIO 0x10 /* Configuration Base IO Address */ 1529#define PCI_CBMA 0x14 /* Configuration Base Memory Address */ 1530#define PCI_CFIT 0x3c /* Configuration Interrupt */ 1531#define PCI_CFDA 0x40 /* Configuration Driver Area */ 1532 1533#if defined(__alpha__) 1534#define TULIP_PCI_CSRSIZE (256 / sizeof(tulip_uint32_t)) 1535#define TULIP_PCI_CSROFFSET (24 / sizeof(tulip_uint32_t)) 1536#elif defined(__i386__) 1537#define TULIP_PCI_CSRSIZE (8 / sizeof(tulip_uint32_t)) 1538#define TULIP_PCI_CSROFFSET 0 1539#endif 1540 1541#if defined(__FreeBSD__) 1542 1543#define TULIP_PCI_ATTACH_ARGS pcici_t config_id, int unit 1544 1545static int 1546tulip_pci_shutdown( 1547 struct kern_devconf * const kdc, 1548 int force) 1549{ 1550 if (kdc->kdc_unit < NDE) { 1551 tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(kdc->kdc_unit); 1552 TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET); 1553 DELAY(10); /* Wait 10 microsends (actually 50 PCI cycles but at 1554 33MHz that comes to two microseconds but wait a 1555 bit longer anyways) */ 1556 } 1557 (void) dev_detach(kdc); 1558 return 0; 1559} 1560 1561static char* 1562tulip_pci_probe( 1563 pcici_t config_id, 1564 pcidi_t device_id) 1565{ 1566 if (device_id == 0x00021011ul) 1567 return "Digital DC21040 Ethernet"; 1568 if (device_id == 0x00141011ul) 1569 return "Digital DC21041 Ethernet"; 1570 if (device_id == 0x00091011ul) 1571 return "Digital DC21140 Fast Ethernet"; 1572 return NULL; 1573} 1574 1575static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS); 1576static u_long tulip_pci_count; 1577 1578struct pci_device dedevice = { 1579 "de", 1580 tulip_pci_probe, 1581 tulip_pci_attach, 1582 &tulip_pci_count, 1583 tulip_pci_shutdown, 1584}; 1585 1586DATA_SET (pcidevice_set, dedevice); 1587#endif /* __FreeBSD__ */ 1588 1589#if defined(__bsdi__) 1590#define TULIP_PCI_ATTACH_ARGS struct device *parent, struct device *self, void *aux 1591 1592static void 1593tulip_pci_shutdown( 1594 void *arg) 1595{ 1596 tulip_softc_t * const sc = (tulip_softc_t *) arg; 1597 TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET); 1598 DELAY(10); /* Wait 10 microsends (actually 50 PCI cycles but at 1599 33MHz that comes to two microseconds but wait a 1600 bit longer anyways) */ 1601} 1602 1603static int 1604tulip_pci_match( 1605 pci_devaddr_t *pa) 1606{ 1607 int irq; 1608 unsigned id; 1609 1610 id = pci_inl(pa, PCI_VENDOR_ID); 1611 if ((id & 0xFFFF) != 0x1011) 1612 return 0; 1613 id >>= 16; 1614 if (id != 2 && id != 9 && id != 0x14) 1615 return 0; 1616 irq = pci_inl(pa, PCI_I_PIN) & 0xFF; 1617 if (irq == 0 || irq >= 16) 1618 return 0; 1619 1620 return 1; 1621} 1622 1623static int 1624tulip_pci_probe( 1625 struct device *parent, 1626 struct cfdata *cf, 1627 void *aux) 1628{ 1629 struct isa_attach_args * const ia = (struct isa_attach_args *) aux; 1630 unsigned irq; 1631 pci_devaddr_t *pa; 1632 1633 pa = pci_scan(tulip_pci_match); 1634 if (pa == NULL) 1635 return 0; 1636 1637 irq = (1 << (pci_inl(pa, PCI_I_PIN) & 0xFF)); 1638 1639 if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) { 1640 printf("fpa%d: error: desired IRQ of %d does not match device's actual IRQ of %d,\n", 1641 cf->cf_unit, 1642 ffs(ia->ia_irq) - 1, ffs(irq) - 1); 1643 return 0; 1644 } 1645 if (ia->ia_irq == IRQUNK) { 1646 if ((irq = isa_irqalloc(irq)) == 0) 1647 return 0; 1648 ia->ia_irq = irq; 1649 } 1650 1651 /* PCI bus masters don't use host DMA channels */ 1652 ia->ia_drq = DRQNONE; 1653 1654 /* Get the memory base address; assume the BIOS set it up correctly */ 1655 ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7); 1656 pci_outl(pa, PCI_CBMA, 0xFFFFFFFF); 1657 ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1; 1658 pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr); 1659 1660 /* Disable I/O space access */ 1661 pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1); 1662 ia->ia_iobase = 0; 1663 ia->ia_iosize = 0; 1664 1665 ia->ia_aux = (void *) pa; 1666 return 1; 1667} 1668 1669static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS); 1670 1671struct cfdriver decd = { 1672 0, "de", tulip_pci_probe, tulip_pci_attach, DV_IFNET, sizeof(tulip_softc_t) 1673}; 1674 1675#endif /* __bsdi__ */ 1676 1677#if defined(__NetBSD__) 1678#define TULIP_PCI_ATTACH_ARGS struct device *parent, struct device *self, void *aux 1679 1680#if 0 /* XXX! */ 1681static void 1682tulip_pci_shutdown( 1683 void *arg) 1684{ 1685 tulip_softc_t * const sc = (tulip_softc_t *) arg; 1686 TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET); 1687 DELAY(10); /* Wait 10 microsends (actually 50 PCI cycles but at 1688 33MHz that comes to two microseconds but wait a 1689 bit longer anyways) */ 1690} 1691#endif 1692 1693static int 1694tulip_pci_probe( 1695 struct device *parent, 1696 void *match, 1697 void *aux) 1698{ 1699 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 1700 1701 if (pa->pa_id == 0x00021011ul 1702 || pa->pa_id == 0x00141011ul 1703 || pa->pa_id == 0x00091011ul) 1704 return 1; 1705 1706 return 0; 1707} 1708 1709static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS); 1710 1711struct cfdriver decd = { 1712 0, "de", tulip_pci_probe, tulip_pci_attach, DV_IFNET, sizeof(tulip_softc_t) 1713}; 1714 1715#endif /* __NetBSD__ */ 1716 1717static void 1718tulip_pci_attach( 1719 TULIP_PCI_ATTACH_ARGS) 1720{ 1721#if defined(__FreeBSD__) 1722 tulip_softc_t *sc; 1723#endif 1724#if defined(__bsdi__) 1725 tulip_softc_t * const sc = (tulip_softc_t *) self; 1726 struct isa_attach_args * const ia = (struct isa_attach_args *) aux; 1727 pci_devaddr_t *pa = (pci_devaddr_t *) ia->ia_aux; 1728 int unit = sc->tulip_dev.dv_unit; 1729#endif 1730#if defined(__NetBSD__) 1731 tulip_softc_t * const sc = (tulip_softc_t *) self; 1732 struct pci_attach_args * const pa = (struct pci_attach_args *) aux; 1733 int unit = sc->tulip_dev.dv_unit; 1734#endif 1735 int retval, idx, revinfo, id; 1736 vm_offset_t va_csrs, pa_csrs; 1737 tulip_desc_t *rxdescs, *txdescs; 1738 tulip_chipid_t chipid; 1739 1740#if defined(__FreeBSD__) 1741 if (unit >= NDE) { 1742 printf("de%d: not configured; kernel is built for only %d device%s.\n", 1743 unit, NDE, NDE == 1 ? "" : "s"); 1744 return; 1745 } 1746#endif 1747 1748#if defined(__FreeBSD__) 1749 revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF; 1750 id = pci_conf_read(config_id, PCI_CFID); 1751#endif 1752#if defined(__bsdi__) 1753 revinfo = pci_inl(pa, PCI_CFRV) & 0xFF; 1754 id = pci_inl(pa, PCI_CFID); 1755#endif 1756#if defined(__NetBSD__) 1757 revinfo = pci_conf_read(pa->pa_tag, PCI_CFRV) & 0xFF; 1758 id = pa->pa_id; 1759#endif 1760 1761 if (id == 0x00021011ul) chipid = TULIP_DC21040; 1762 else if (id == 0x00091011) chipid = TULIP_DC21140; 1763 else if (id == 0x00141011) chipid = TULIP_DC21041; 1764 else return; 1765 1766 if (chipid == TULIP_DC21040 && revinfo < 0x20) { 1767#ifdef __FreeBSD__ 1768 printf("de%d", unit); 1769#endif 1770 printf(": not configured; DC21040 pass 2.0 required (%d.%d found)\n", 1771 revinfo >> 4, revinfo & 0x0f); 1772 return; 1773 } else if (chipid == TULIP_DC21140 && revinfo < 0x11) { 1774#ifdef __FreeBSD__ 1775 printf("de%d", unit); 1776#endif 1777 printf(": not configured; DC21140 pass 1.1 required (%d.%d found)\n", 1778 revinfo >> 4, revinfo & 0x0f); 1779 return; 1780 } 1781 1782#if defined(__FreeBSD__) 1783 sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT); 1784 if (sc == NULL) 1785 return; 1786 bzero(sc, sizeof(*sc)); /* Zero out the softc*/ 1787#endif 1788 1789 rxdescs = (tulip_desc_t *) 1790 malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT); 1791 if (rxdescs == NULL) { 1792#if defined(__FreeBSD__) 1793 free((caddr_t) sc, M_DEVBUF); 1794#endif 1795 return; 1796 } 1797 1798 txdescs = (tulip_desc_t *) 1799 malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT); 1800 if (txdescs == NULL) { 1801 free((caddr_t) rxdescs, M_DEVBUF); 1802#if defined(__FreeBSD__) 1803 free((caddr_t) sc, M_DEVBUF); 1804#endif 1805 return; 1806 } 1807 1808 sc->tulip_chipid = chipid; 1809 sc->tulip_unit = unit; 1810 sc->tulip_name = "de"; 1811#if defined(__FreeBSD__) 1812 retval = pci_map_mem(config_id, PCI_CBMA, &va_csrs, &pa_csrs); 1813 if (!retval) { 1814 free((caddr_t) txdescs, M_DEVBUF); 1815 free((caddr_t) rxdescs, M_DEVBUF); 1816 free((caddr_t) sc, M_DEVBUF); 1817 return; 1818 } 1819 tulips[unit] = sc; 1820#endif 1821#if defined(__bsdi__) 1822 va_csrs = (vm_offset_t) mapphys((vm_offset_t) ia->ia_maddr, ia->ia_msize); 1823#endif 1824#if defined(__NetBSD__) 1825 if (pci_map_mem(pa->pa_tag, PCI_CBMA, &va_csrs, &pa_csrs)) { 1826 free((caddr_t) txdescs, M_DEVBUF); 1827 free((caddr_t) rxdescs, M_DEVBUF); 1828 return; 1829 } 1830#endif 1831 sc->tulip_revinfo = revinfo; 1832 tulip_initcsrs(sc, ((volatile tulip_uint32_t *) va_csrs) + TULIP_PCI_CSROFFSET, 1833 TULIP_PCI_CSRSIZE); 1834 tulip_initring(sc, &sc->tulip_rxinfo, rxdescs, TULIP_RXDESCS); 1835 tulip_initring(sc, &sc->tulip_txinfo, txdescs, TULIP_TXDESCS); 1836 if ((retval = tulip_read_macaddr(sc)) < 0) { 1837#ifdef __FreeBSD__ 1838 printf("%s%d", sc->tulip_name, sc->tulip_unit); 1839#endif 1840 printf(": can't read ENET ROM (why=%d) (", retval); 1841 for (idx = 0; idx < 32; idx++) 1842 printf("%02x", sc->tulip_rombuf[idx]); 1843 printf("\n"); 1844 printf("%s%d: %s%s pass %d.%d Ethernet address %s\n", 1845 sc->tulip_name, sc->tulip_unit, 1846 (sc->tulip_boardsw != NULL ? sc->tulip_boardsw->bd_description : ""), 1847 tulip_chipdescs[sc->tulip_chipid], 1848 (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F, 1849 "unknown"); 1850 } else { 1851 tulip_reset(sc); 1852 tulip_attach(sc); 1853#if defined(__NetBSD__) 1854 sc->tulip_ih = pci_map_int(pa->pa_tag, PCI_IPL_NET, tulip_intr, sc); 1855 if (sc->tulip_ih == NULL) { 1856 printf("%s%d: couldn't map interrupt\n", 1857 sc->tulip_name, sc->tulip_unit); 1858 return; 1859 } 1860#endif 1861#if defined(__FreeBSD__) 1862 pci_map_int (config_id, tulip_intr, (void*) sc, &net_imask); 1863#endif 1864#if defined(__bsdi__) 1865 isa_establish(&sc->tulip_id, &sc->tulip_dev); 1866 1867 sc->tulip_ih.ih_fun = tulip_intr; 1868 sc->tulip_ih.ih_arg = (void *)sc; 1869 intr_establish(ia->ia_irq, &sc->tulip_ih, DV_NET); 1870 1871 sc->tulip_ats.func = tulip_pci_shutdown; 1872 sc->tulip_ats.arg = (void *) sc; 1873 atshutdown(&sc->tulip_ats, ATSH_ADD); 1874#endif 1875 } 1876} 1877#endif /* NDE > 0 */ 1878