root/drivers/net/tulip.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. tio_sia_write
  2. card_type
  3. read_eeprom
  4. generic21040_error
  5. generic21041_error
  6. generic21040_select
  7. generic21041_select
  8. auto21140_select
  9. generic21140_select
  10. tulip_open
  11. tulip_init_ring
  12. tulip_start_xmit
  13. tulip_interrupt
  14. tulip_rx
  15. tulip_close
  16. tulip_get_stats
  17. set_multicast_list
  18. set_mac_address
  19. tulip_alloc
  20. tulip_hwinit
  21. tulip_probe
  22. init_module
  23. cleanup_module

   1 /* tulip.c: A DEC 21040 ethernet driver for linux. */
   2 /*
   3    NOTICE: this version works with kernels 1.1.82 and later only!
   4         Written 1994,1995 by Donald Becker.
   5 
   6         This software may be used and distributed according to the terms
   7         of the GNU Public License, incorporated herein by reference.
   8 
   9         This driver is for the SMC EtherPower PCI ethernet adapter.
  10         It should work with most other DEC 21*40-based ethercards.
  11 
  12         The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
  13         Center of Excellence in Space Data and Information Sciences
  14            Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
  15 */
  16 
  17 static char *version =
  18 "tulip.c:v0.10 8/11/95 becker@cesdis.gsfc.nasa.gov\n"
  19 "        +0.68 3/09/96 "
  20 "http://www.dsl.tutics.tut.ac.jp/~manabe/linux/tulip.html\n";
  21 
  22 /* A few user-configurable values. */
  23 
  24 /* Default to using non-10baseT (i.e. AUI/10base2/100baseT port) port. */
  25 #define TULIP_10TP_PORT         0
  26 #define TULIP_100TP_PORT        1
  27 #define TULIP_AUI_PORT          1
  28 #define TULIP_BNC_PORT          2
  29 
  30 #ifndef TULIP_PORT
  31 #define TULIP_PORT                      TULIP_10TP_PORT
  32 #endif
  33 
  34 /* Define to force full-duplex operation on all Tulip interfaces. */
  35 /* #define  TULIP_FULL_DUPLEX 1 */
  36 
  37 /* Define to probe only first detected device */
  38 /* #define      TULIP_ONLY_ONE 1 */
  39 
  40 #include <linux/config.h>
  41 
  42 #if defined(MODULE) && defined(CONFIG_MODVERSIONS)
  43 #define MODVERSIONS
  44 #include <linux/modversions.h>
  45 #endif
  46 
  47 #include <linux/version.h>
  48 
  49 #if LINUX_VERSION_CODE < 0x10300
  50 /* i.e. version 1.2.x */
  51 #define virt_to_bus(address)    (unsigned long)(address)
  52 #define bus_to_virt(address)    (void *)(address)
  53 #define PCI_DEVICE_ID_DEC_TULIP_PLUS    0x0014
  54 #ifdef  MODULE
  55 #include <linux/module.h>
  56 char kernel_version[] = UTS_RELEASE;
  57 #else
  58 #undef  MOD_INC_USE_COUNT
  59 #undef  MOD_DEC_USE_COUNT
  60 #define MOD_INC_USE_COUNT
  61 #define MOD_DEC_USE_COUNT
  62 #endif
  63 #else
  64 /* i.e. version 1.3.x */
  65 #include <linux/module.h>
  66 #endif
  67 
  68 #include <linux/kernel.h>
  69 #include <linux/sched.h>
  70 #include <linux/string.h>
  71 #include <linux/ptrace.h>
  72 #include <linux/errno.h>
  73 #include <linux/ioport.h>
  74 #include <linux/malloc.h>
  75 #include <linux/interrupt.h>
  76 #include <linux/pci.h>
  77 #include <linux/bios32.h>
  78 #include <asm/byteorder.h>
  79 #include <asm/bitops.h>
  80 #include <asm/io.h>
  81 #include <asm/dma.h>
  82 
  83 #include <linux/netdevice.h>
  84 #include <linux/etherdevice.h>
  85 #include <linux/skbuff.h>
  86 
  87 /* The total size is unusually large: The 21040 aligns each of its 16
  88    longword-wide registers on a quadword boundary. */
  89 #define TULIP_TOTAL_SIZE 0x80
  90 
  91 #ifdef TULIP_DEBUG
  92 int tulip_debug = TULIP_DEBUG;
  93 #else
  94 int tulip_debug = 3;
  95 #endif
  96 
  97 /*
  98                                 Theory of Operation
  99 
 100 I. Board Compatibility
 101 
 102 This device driver is designed for the DECchip 21040 "Tulip", Digital's
 103 single-chip ethernet controller for PCI, as used on the SMC EtherPower
 104 ethernet adapter.  It also works with boards based the 21041 (new/experimental)
 105 and 21140 (10/100mbps).
 106 
 107 
 108 II. Board-specific settings
 109 
 110 PCI bus devices are configured by the system at boot time, so no jumpers
 111 need to be set on the board.  The system BIOS should be set to assign the
 112 PCI INTA signal to an otherwise unused system IRQ line.  While it's
 113 physically possible to shared PCI interrupt lines, the kernel doesn't
 114 support it. 
 115 
 116 III. Driver operation
 117 
 118 IIIa. Ring buffers
 119 The Tulip can use either ring buffers or lists of Tx and Rx descriptors.
 120 The current driver uses a statically allocated Rx ring of descriptors and
 121 buffers, and a list of the Tx buffers.
 122 
 123 IIIC. Synchronization
 124 The driver runs as two independent, single-threaded flows of control.  One
 125 is the send-packet routine, which enforces single-threaded use by the
 126 dev->tbusy flag.  The other thread is the interrupt handler, which is single
 127 threaded by the hardware and other software.
 128 
 129 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
 130 flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
 131 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
 132 the 'tp->tx_full' flag.
 133 
 134 The interrupt handler has exclusive control over the Rx ring and records stats
 135 from the Tx ring.  (The Tx-done interrupt can't be selectively turned off, so
 136 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
 137 stats.)  After reaping the stats, it marks the queue entry as empty by setting
 138 the 'base' to zero.      Iff the 'tp->tx_full' flag is set, it clears both the
 139 tx_full and tbusy flags.
 140 
 141 IV. Notes
 142 
 143 Thanks to Duke Kamstra of SMC for providing an EtherPower board.
 144 
 145 The DEC databook doesn't document which Rx filter settings accept broadcast
 146 packets.  Nor does it document how to configure the part to configure the
 147 serial subsystem for normal (vs. loopback) operation or how to have it
 148 autoswitch between internal 10baseT, SIA and AUI transceivers.
 149 
 150 The databook claims that CSR13, CSR14, and CSR15 should each be the last
 151 register of the set CSR12-15 written.   Hmmm, now how is that possible?
 152 */
 153 
 154 
 155 /* A few values that may be tweaked. */
 156 /* Keep the ring sizes a power of two for efficiency. */
 157 #define TX_RING_SIZE    4
 158 #define RX_RING_SIZE    4
 159 #define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
 160 
 161 /* This is a mysterious value that can be written to CSR11 in the 21040
 162    to detect a full-duplex frame.  No one knows what it should be, but if
 163    left at its default value some 10base2(!) packets trigger a
 164    full-duplex-request interrupt. */
 165 #define FULL_DUPLEX_MAGIC       0x6969
 166 
 167 /* The rest of these values should never change. */
 168 
 169 #define PCI_DEVICE_ID_NONE      0xFFFF
 170 #define ETHNAMSIZ       8
 171 #define ROUND_UP(size, n)       ((size + n - 1) & ~(n - 1))
 172 
 173 /* Offsets to the Command and Status Registers, "CSRs".  All accesses
 174    must be longword instructions and quadword aligned. */
 175 enum tulip_offsets {
 176                                 /* 21040                21041           21140           */
 177         CSR0=0,         /* BUS mode                                                             */
 178     CSR1=0x08,  /* TX poll demand                                               */
 179         CSR2=0x10,      /* RX poll demand                                               */
 180         CSR3=0x18,      /* RX ring base addr                                    */
 181         CSR4=0x20,      /* TX ring base addr                                    */
 182         CSR5=0x28,      /* Status                                                               */
 183         CSR6=0x30,      /* Command mode                                                 */
 184         CSR7=0x38,      /* Interrupt Mask                                               */
 185         CSR8=0x40,      /* Missed frame counter                                 */
 186         CSR9=0x48,      /* Eth.addrROM  SROM mii        SROM mii        */
 187         CSR10=0x50,     /* Diagn.               boot ROM        -                       */
 188         CSR11=0x58,     /* Full duplex  G.P. timer      G.P. timer      */
 189         CSR12=0x60,     /* SIA status                           G.P.            */
 190         CSR13=0x68,     /* SIA connectivity                     -                       */
 191         CSR14=0x70,     /* SIA TX/RX                            -                       */
 192         CSR15=0x78      /* SIA general                          watchdog        */
 193 };
 194 
 195 /* description of CSR0 bus mode register */
 196 #define TBMOD_RESERVED          0xfff80000      /* I don't know */
 197 #define TBMOD_RESET                     0x00000001
 198 #define TBMOD_BIGENDIAN         0x00000080
 199 /*
 200            Cache alignment bits 15:14        Burst length 13:8
 201         0000    No alignment  0x00000000 unlimited              0800 8 longwords
 202                 4000    8  longwords            0100 1 longword         1000 16 longwords
 203                 8000    16 longwords            0200 2 longwords        2000 32 longwords
 204                 C000    32  longwords           0400 4 longwords
 205 */
 206 #define TBMOD_ALIGN0            0x00000000      /* no cache alignment */
 207 #define TBMOD_ALIGN8            0x00004000      /* 8 longwords */
 208 #define TBMOD_ALIGN16           0x00008000
 209 #define TBMOD_ALIGN32           (TBMOD_ALIGN8|TBMOD_ALIGN16)
 210 #define TBMOD_BURST0            0x00000000      /* unlimited=rx buffer size */
 211 #define TBMOD_BURST1            0x00000100      /* 1 longwords */
 212 #define TBMOD_BURST2            0x00000200
 213 #define TBMOD_BURST4            0x00000400
 214 #define TBMOD_BURST8            0x00000800
 215 #define TBMOD_BURST16           0x00001000
 216 #define TBMOD_BURST32           0x00002000
 217 
 218 /* description of CSR1 Tx poll demand register */
 219 /* description of CSR2 Rx poll demand register */
 220 #define TPOLL_START                     0x00000001      /* ? */
 221 #define TPOLL_TRIGGER           0x00000000      /* ? */
 222 
 223 /* description of CSR5 status register from de4x5.h */
 224 #define TSTAT_BUSERROR          0x03800000
 225 #define TSTAT_SYSERROR          0x00002000
 226 #define TSTAT_TxSTAT            0x00700000
 227 #define TSTAT_RxSTAT            0x000e0000
 228 #define TSTAT_LKFAIL            0x00001000
 229 #define TSTAT_NORINTR           0x00010000      /* Normal interrupt */
 230 #define TSTAT_ABNINTR           0x00008000      /* Abnormal interrupt */
 231 #define TSTAT_RxMISSED          0x00000100      /* Rx frame missed */
 232 #define TSTAT_RxUNABL           0x00000080
 233 #define TSTAT_RxINTR            0x00000040
 234 #define TSTAT_LKPASS            0x00000010
 235 #define TSTAT_TEXPIRED          0x00000800      /* Timer Expired */
 236 #define TSTAT_TxTOUT            0x00000008
 237 #define TSTAT_TxUNABL           0x00000004
 238 #define TSTAT_TxINTR            0x00000001
 239 #define TSTAT_CLEARINTR         0x0001ffff      /* clear all interrupt sources */
 240 
 241 /* description of CSR6 command mode register */
 242 #define TCMOD_SCRM                      0x01000000      /* scrambler mode */
 243 #define TCMOD_PCS                       0x00800000      /* PCS function */
 244 #define TCMOD_TxTHMODE          0x00400000      /* Tx threshold mode */
 245 #define TCMOD_SW100TP           0x00040000  /* 21140: 100MB */
 246 #define TCMOD_CAPTURE           0x00020000      /* capture effect */
 247 #define TCMOD_FULLDUPLEX        0x00000200
 248 #define TCMOD_TH128                     0x00008000      /* 10 - 128 bytes threshold */
 249 #define TCMOD_TxSTART           0x00002000
 250 #define TCMOD_RxSTART           0x00000002
 251 #define TCMOD_ALLMCAST          0x00000080      /* pass all multicast */
 252 #define TCMOD_PROMISC           0x00000040      /* promisc */
 253 #define TCMOD_BOFFCOUNTER       0x00000020      /* backoff counter */
 254 #define TCMOD_INVFILTER         0x00000010      /* invert filtering */
 255 #define TCMOD_HONLYFILTER       0x00000004      /* hash only filtering */
 256 #define TCMOD_HPFILTER          0x00000001      /* hash/perfect Rx filtering */
 257 #define TCMOD_MODEMASK          (TCMOD_ALLMCAST|TCMOD_PROMISC)
 258 #define TCMOD_FILTERMASK        (TCMOD_HONLYFILTER|TCMOD_HPFILTER|TCMOD_INVFILTER)
 259 #define TCMOD_TRxSTART          (TCMOD_TxSTART|TCMOD_RxSTART)
 260 #define TCMOD_BASE                      (TCMOD_CAPTURE|TCMOD_BOFFCOUNTER)
 261 #define TCMOD_10TP                      (TCMOD_TxTHMODE|TCMOD_BASE)
 262 #define TCMOD_100TP                     (TCMOD_SCRM|TCMOD_PCS|TCMOD_SW100TP|TCMOD_BASE)
 263 #define TCMOD_AUTO                      (TCMOD_SW100TP|TCMOD_TH128|TCMOD_10TP)
 264 
 265 /* description of CSR7 interrupt mask register */
 266 #define TINTR_ENABLE            0xFFFFFFFF
 267 #define TINTR_DISABLE           0x00000000
 268 
 269 /* description of CSR12 SIA status(2104x)/GP(21140) register */
 270 #define TSIAS_CONERROR          0x00000002      /* connection error */
 271 #define TSIAS_LNKERROR          0x00000004      /* link error */
 272 #define TGEPR_LK10NG            0x00000080      /* 10Mbps N.G. (R) */
 273 #define TGEPR_LK100NG           0x00000040      /* 100Mbps N.G. (R) */
 274 #define TGEPR_DETECT            0x00000020      /* detect signal (R) */
 275 #define TGEPR_HALFDUPLEX        0x00000008      /* half duplex (W) */
 276 #define TGEPR_PHYLOOPBACK       0x00000004      /* PHY loopback (W) */
 277 #define TGEPR_FORCEALED         0x00000002      /* force activity LED on (W) */
 278 #define TGEPR_FORCE100          0x00000001      /* force 100Mbps mode */
 279 
 280 /* description of CSR13 SIA connectivity register */
 281 #define TSIAC_OUTEN                     0x0000e000      /* 21041: Output enable */
 282 #define TSIAC_SELED                     0x00000f00      /* 21041: AUI or TP with LEDs */
 283 #define TSIAC_INEN                      0x00001000      /* 21041: Input enable */
 284 #define TSIAC_NO10TP            0x00000008      /* 10baseT(0) or not(1) */
 285 #define TSIAC_CONFIG            0x00000004      /* Configuration */
 286 #define TSIAC_SWRESET           0x00000001      /* 21041: software reset */
 287 #define TSIAC_RESET                     0x00000000      /* reset */
 288 #define TSIAC_C21041            (TSIAC_OUTEN|TSIAC_SELED|TSIAC_SWRESET)
 289 #define TSIAC_C21040            TSIAC_CONFIG
 290 
 291 /* description of CSR14 SIA TX/RX register */
 292 #define TSIAX_NO10TP            0x0000f73d
 293 #define TSIAX_10TP                      0x0000ff3f
 294 
 295 /* description of CSR15 SIA general register */
 296 #define TSIAG_SWBNCAUI          0x00000008 /* BNC(0) or AUI(1) */
 297 #define TSIAG_BNC                       0x00000006
 298 #define TSIAG_AUI                       (TSIAG_BNC|TSIAG_SWBNCAUI)
 299 #define TSIAG_10TP                      0x00000000
 300 
 301 /* description of rx_ring.status */
 302 #define TRING_OWN                       0x80000000      /* Owned by chip */
 303 #define TRING_CLEAR                     0x00000000      /* clear */
 304 #define TRING_ERROR                     0x00008000      /* error summary */
 305 #define TRING_ETxTO                     0x00004000      /* Tx time out */
 306 #define TRING_ELCOLL            0x00000200      /* late collision */
 307 #define TRING_EFCOLL            0x00000100      /* fatal collision */
 308 #define TRING_ELCARR            0x00000800      /* carrier lost */
 309 #define TRING_ENCARR            0x00000400      /* no carrier */
 310 #define TRING_ENOHB                     0x00000080      /* heartbeat fail */
 311 #define TRING_ELINK                     0x00000004      /* link fail */
 312 #define TRING_EUFLOW            0x00000002      /* underflow */
 313 
 314 #define TRING_ELEN                      0x00004000      /* length error */
 315 #define TRING_FDESC                     0x00000200      /* first descriptor */
 316 #define TRING_LDESC                     0x00000100      /* last descriptor */
 317 #define TRING_ERUNT                     0x00000800      /* runt frame */
 318 #define TRING_ELONG                     0x00000080      /* frame too long */
 319 #define TRING_EWATCHDOG         0x00000010      /* receive watchdog */
 320 #define TRING_EDRBIT            0x00000004      /* dribble bit */
 321 #define TRING_ECRC                      0x00000002      /* CRC error */
 322 #define TRING_EOVERFLOW         0x00000001      /* overflow */
 323 
 324 #define TRING_RxDESCMASK        (TRING_FDESC|TRING_LDESC)
 325 #define TRING_RxLENGTH          (TRING_ERUNT|TRING_ELONG|TRING_EWATCHDOG)
 326 #define TRING_RxFRAME           (TRING_EDRBIT)
 327 #define TRING_RxCRC                     (TRING_ECRC)
 328 #define TRING_RxFIFO            (TRING_EOVERFLOW)
 329 #define TRING_TxABORT           (TRING_ETxTO|TRING_EFCOLL|TRING_ELINK)
 330 #define TRING_TxCARR            (TRING_ELCARR|TRING_ENCARR)
 331 #define TRING_TxWINDOW          (TRING_ELCOLL)
 332 #define TRING_TxFIFO            (TRING_EUFLOW)
 333 #define TRING_TxHEARTBEAT       (TRING_ENOHB)
 334 /* The Tulip Rx and Tx buffer descriptors. */
 335 struct tulip_rx_desc {
 336         s32 status;
 337         s32 length;
 338         u32 buffer1, buffer2;                   /* We use only buffer 1.  */
 339 };
 340 
 341 struct tulip_tx_desc {
 342         s32 status;
 343         s32 length;
 344         u32 buffer1, buffer2;                   /* We use only buffer 1.  */
 345 };
 346 
 347 struct tulip_private {
 348         struct tulip_rx_desc rx_ring[RX_RING_SIZE];
 349         struct tulip_tx_desc tx_ring[TX_RING_SIZE];
 350         /* The saved address of a sent-in-place packet/buffer, for skfree(). */
 351         struct sk_buff* tx_skbuff[TX_RING_SIZE];
 352         char rx_buffs[RX_RING_SIZE][PKT_BUF_SZ];
 353         /* temporary Rx buffers. */
 354         struct enet_statistics stats;
 355         int setup_frame[48];    /* Pseudo-Tx frame to init address table. */
 356         void (*port_select)(struct device *dev);
 357         int (*port_error)(struct device *dev);
 358         char *signature;
 359         unsigned int cur_rx, cur_tx;            /* The next free ring entry */
 360         unsigned int dirty_rx, dirty_tx;        /* The ring entries to be free()ed. */
 361         unsigned int tx_full:1;                         /* The Tx queue is full. */
 362         unsigned int full_duplex:1;                     /* Full-duplex operation requested. */
 363 };
 364 
 365 struct eeprom {
 366     union {
 367                 struct { /* broken EEPROM structure */
 368                         u_char addr[ETH_ALEN];
 369                 } ng;
 370                 struct { /* DEC EtherWorks
 371                                         and other cards which have correct eeprom structure */
 372                         u_char dum1[20];
 373                         u_char addr[ETH_ALEN];
 374                 } ok;
 375     } hw;
 376 #define ng_addr hw.ng.addr
 377 #define ok_addr hw.ok.addr
 378 #define EE_SIGNLEN      14              /* should be 102 ? */
 379         u_char sign[EE_SIGNLEN];
 380 };
 381 
 382 static int read_eeprom(int ioaddr, struct eeprom *eepp);
 383 static int tulip_open(struct device *dev);
 384 static void tulip_init_ring(struct device *dev);
 385 static int tulip_start_xmit(struct sk_buff *skb, struct device *dev);
 386 static int tulip_rx(struct device *dev);
 387 static void tulip_interrupt(int irq, struct pt_regs *regs);
 388 static int tulip_close(struct device *dev);
 389 static struct enet_statistics *tulip_get_stats(struct device *dev);
 390 static struct device *tulip_alloc(struct device *dev);
 391 #if LINUX_VERSION_CODE < 0x10300
 392 static void set_multicast_list(struct device *dev, int num_addrs,
 393                                                            void *addrs);
 394 #else
 395 static void set_multicast_list(struct device *dev);
 396 #endif
 397 
 398 #define generic21140_error      NULL
 399 static void generic21040_select(struct device *dev);
 400 static void generic21140_select(struct device *dev);
 401 static void generic21041_select(struct device *dev);
 402 static void auto21140_select(struct device *dev);
 403 static int generic21040_error(struct device *dev);
 404 static int generic21041_error(struct device *dev);
 405 
 406 static struct {
 407         void (*port_select)(struct device *dev);
 408         int (*port_error)(struct device *dev);
 409         unsigned int vendor_id, device_id;
 410         char *signature;
 411         unsigned int port_auto:1;
 412 } cardVendor[] = {
 413         {generic21140_select, generic21140_error,
 414                  0x0000c000, PCI_DEVICE_ID_DEC_TULIP_FAST, "smc9332", 0},
 415         {generic21041_select, generic21041_error,
 416                  0x0000c000, PCI_DEVICE_ID_DEC_TULIP_PLUS, "smc8432", 0},
 417         {generic21040_select, generic21040_error,
 418                  0x0000c000, PCI_DEVICE_ID_DEC_TULIP, "old smc8432", 0},
 419         {auto21140_select, generic21140_error,
 420                  0x0000f400, PCI_DEVICE_ID_DEC_TULIP_FAST, "LA100PCI", 1},
 421         {generic21140_select, generic21140_error,
 422                  0x0000f800, PCI_DEVICE_ID_DEC_TULIP_FAST, "DE500", 0},
 423         {generic21041_select, generic21041_error,
 424                  0x0000f800, PCI_DEVICE_ID_DEC_TULIP_PLUS, "DE450", 0},
 425         {generic21040_select, generic21040_error,
 426                  0x0000f800, PCI_DEVICE_ID_DEC_TULIP, "DE43x", 0},
 427         {generic21040_select, generic21040_error,
 428                  0x0040c700, PCI_DEVICE_ID_DEC_TULIP, "EN9400", 0},
 429         {generic21040_select, generic21040_error,
 430                  0x00c09500, PCI_DEVICE_ID_DEC_TULIP, "ZNYX312", 0},
 431         {generic21040_select, generic21040_error,
 432                  0, PCI_DEVICE_ID_DEC_TULIP, "21040", 0},
 433         {generic21140_select, generic21140_error,
 434                  0, PCI_DEVICE_ID_DEC_TULIP_FAST, "21140", 0},
 435         {generic21041_select, generic21041_error,
 436                  0, PCI_DEVICE_ID_DEC_TULIP_PLUS, "21041", 0},
 437         {NULL, NULL, 0, 0, "Unknown", 0}
 438 };
 439 
 440 
 441 /* Serial EEPROM section.
 442    A "bit" grungy, but we work our way through bit-by-bit :->. */
 443 /*  EEPROM_Ctrl bits. */
 444 #define EE_SHIFT_CLK    0x02    /* EEPROM shift clock. */
 445 #define EE_CS                   0x01    /* EEPROM chip select. */
 446 #define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
 447 #define EE_WRITE_0              0x01
 448 #define EE_WRITE_1              0x05
 449 #define EE_DATA_READ    0x08    /* EEPROM chip data out. */
 450 #define EE_ENB                  (0x4800 | EE_CS)
 451 
 452 /* Delay between EEPROM clock transitions.
 453    This is a "nasty" timing loop, but PC compatible machines are *supposed*
 454    to delay an ISA compatible period for the SLOW_DOWN_IO macro.  */
 455 
 456 #define eeprom_delay(nanosec)\
 457         do { int _i = 3; while (--_i > 0) { __SLOW_DOWN_IO; }} while (0)
 458 
 459 /* The EEPROM commands include the alway-set leading bit. */
 460 #define EE_WRITE_CMD    (5 << 6)
 461 #define EE_READ_CMD             (6 << 6)
 462 #define EE_ERASE_CMD    (7 << 6)
 463 
 464 #ifdef MODULE
 465 static u_long alloc_size;
 466 #endif
 467 
 468 #ifdef  __i386__
 469 #define tio_write(val, port)    outl(val, ioaddr + port)
 470 #define tio_read(port)                  inl(ioaddr + port)
 471 #endif
 472 
 473 static void inline
 474 tio_sia_write(u32 ioaddr, u32 val13, u32 val14, u32 val15)
     /* [previous][next][first][last][top][bottom][index][help] */
 475 {
 476         tio_write(0,CSR13);
 477         tio_write(val15,CSR15);
 478         tio_write(val14,CSR14);
 479         tio_write(val13,CSR13);
 480 }
 481 
 482 static char *
 483 card_type(struct tulip_private *tp, int device_id, int vendor_id)
     /* [previous][next][first][last][top][bottom][index][help] */
 484 {
 485         int n;
 486 
 487         for (n = 0; cardVendor[n].device_id; n ++)
 488                 if (cardVendor[n].device_id == device_id
 489                         && (cardVendor[n].vendor_id == vendor_id
 490                                 || cardVendor[n].vendor_id == 0)) break;
 491         tp->port_select = cardVendor[n].port_select;
 492         tp->port_error = cardVendor[n].port_error;
 493         tp->signature = cardVendor[n].signature;
 494         return(cardVendor[n].signature);
 495 }
 496 
 497 static int
 498 read_eeprom(int ioaddr, struct eeprom *eepp)
     /* [previous][next][first][last][top][bottom][index][help] */
 499 {
 500     int i, n;
 501     unsigned short val = 0;
 502     int read_cmd = EE_READ_CMD;
 503     u_char *p=(u_char *)eepp;
 504 
 505         for (n = 0; n < sizeof(struct eeprom) / 2; n ++, read_cmd ++) {
 506                 tio_write(EE_ENB & ~EE_CS, CSR9);
 507                 tio_write(EE_ENB, CSR9);
 508 
 509                 /* Shift the read command bits out. */
 510                 for (i = 10; i >= 0; i--) {
 511                         short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
 512                         tio_write(EE_ENB | dataval, CSR9);
 513                         eeprom_delay(100);
 514                         tio_write(EE_ENB | dataval | EE_SHIFT_CLK, CSR9);
 515                         eeprom_delay(150);
 516                         tio_write(EE_ENB | dataval, CSR9);
 517                         /* Finish EEPROM a clock tick. */
 518                         eeprom_delay(250);
 519                 }
 520                 tio_write(EE_ENB, CSR9);
 521 
 522                 for (i = 16; i > 0; i--) {
 523                         tio_write(EE_ENB | EE_SHIFT_CLK, CSR9);
 524                         eeprom_delay(100);
 525                         val = (val << 1)
 526                                 | ((tio_read(CSR9) & EE_DATA_READ) ? 1 : 0);
 527                         tio_write(EE_ENB, CSR9);
 528                         eeprom_delay(100);
 529                 }
 530 
 531                 /* Terminate the EEPROM access. */
 532                 tio_write(EE_ENB & ~EE_CS, CSR9);
 533                 *p ++ = val;
 534                 *p ++ = val >> 8;
 535     }
 536         /* broken eeprom ? */
 537         p = (u_char *)eepp;
 538         for (i = 0; i < 8; i ++)
 539                 if (p[i] != p[15 - i] || p[i] != p[16 + i]) return(0);
 540         return(-1); /* broken */
 541 }
 542 
 543 /* Is this correct ? */
 544 static int
 545 generic21040_error(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 546 {
 547         int ioaddr = dev->base_addr;
 548 
 549         return(tio_read(CSR12) & TSIAS_CONERROR);
 550 }
 551 
 552 static int
 553 generic21041_error(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 554 {
 555         int ioaddr = dev->base_addr;
 556 
 557         return(tio_read(CSR12) & TSIAS_LNKERROR);
 558 }
 559 
 560 static void
 561 generic21040_select(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 562 {
 563         int ioaddr = dev->base_addr;
 564 
 565         dev->if_port &= 3;
 566         printk("%s: enabling %s port.\n",
 567                    dev->name, dev->if_port ?  "AUI":"10baseT");
 568         /* Set the full duplex match frame. */
 569         tio_write(FULL_DUPLEX_MAGIC, CSR11);
 570         tio_write(TSIAC_RESET, CSR13);
 571         /* Reset the serial interface */
 572         tio_write((dev->if_port ? TSIAC_NO10TP: 0) | TSIAC_C21040, CSR13);
 573 }
 574 
 575 static void
 576 generic21041_select(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 577 {
 578         int ioaddr = dev->base_addr;
 579         u32 tsiac = TSIAC_C21041;
 580         u32 tsiax = TSIAX_10TP;
 581         u32 tsiag = TSIAG_10TP;
 582 
 583         printk("%s: enabling ", dev->name);
 584         switch(dev->if_port) {
 585         case TULIP_AUI_PORT:
 586                 tsiac |= TSIAC_NO10TP;
 587                 tsiax = TSIAX_NO10TP;
 588                 tsiag = TSIAG_AUI;
 589                 printk("AUI");
 590                 break;
 591         case TULIP_BNC_PORT:
 592                 tsiac |= TSIAC_NO10TP;
 593                 tsiax = TSIAX_NO10TP;
 594                 tsiag = TSIAG_BNC;
 595                 printk("BNC");
 596                 break;
 597         default:
 598                 dev->if_port = TULIP_10TP_PORT;
 599                 printk("10TP");
 600                 break;
 601         }
 602         tio_sia_write(ioaddr, tsiac, tsiax, tsiag);
 603         printk(" port.\n");
 604 }
 605 
 606 static void
 607 auto21140_select(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 608 {
 609         int ioaddr = dev->base_addr;
 610         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 611 
 612         /* kick port */
 613         tio_write(TPOLL_TRIGGER, CSR1);
 614         tio_write(TINTR_ENABLE, CSR7);
 615         tio_write(TCMOD_AUTO|TCMOD_TRxSTART, CSR6);
 616         dev->if_port = !(tio_read(CSR12) & TGEPR_FORCEALED);
 617         printk("%s: probed %s port.\n",
 618                    dev->name, dev->if_port ? "100baseTx" : "10baseT");
 619         tio_write((dev->if_port ? TGEPR_FORCE100: 0)
 620                           | (tp->full_duplex ? 0:TGEPR_HALFDUPLEX), CSR12);     
 621         tio_write(TINTR_DISABLE, CSR7);
 622         tio_read(CSR8) & 0xffff;
 623         tio_write(TCMOD_AUTO, CSR6);
 624 }
 625 
 626 static void
 627 generic21140_select(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 628 {
 629         int ioaddr = dev->base_addr, csr6;
 630         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 631 
 632         dev->if_port &= 1;
 633         csr6 = tio_read(CSR6) &
 634                 ~(TCMOD_10TP|TCMOD_100TP|TCMOD_TRxSTART|TCMOD_SCRM);
 635 
 636         /* Stop the transmit process. */
 637         tio_write(csr6 | TCMOD_RxSTART, CSR6);
 638         printk("%s: enabling %s port.\n",
 639                    dev->name, dev->if_port ? "100baseTx" : "10baseT");
 640         tio_write((dev->if_port ? TCMOD_100TP: TCMOD_10TP)
 641                           | TCMOD_TRxSTART | TCMOD_TH128 | csr6, CSR6);
 642         tio_write((dev->if_port ? TGEPR_FORCE100: 0)
 643                           | (tp->full_duplex ? 0:TGEPR_HALFDUPLEX), CSR12);
 644 }
 645 
 646 static int
 647 tulip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 648 {
 649         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 650         int ioaddr = dev->base_addr;
 651 
 652         /* Reset the chip, holding bit 0 set at least 10 PCI cycles. */
 653         tio_write(tio_read(CSR0)|TBMOD_RESET, CSR0);
 654 /*      tio_write(TBMOD_RESERVED|TBMOD_RESET, CSR0);*/
 655         SLOW_DOWN_IO;
 656         /* Deassert reset.  Set 8 longword cache alignment, 8 longword burst.
 657            -> Set 32 longword cache alignment, unlimited longword burst ?
 658            Wait the specified 50 PCI cycles after a reset by initializing
 659            Tx and Rx queues and the address filter list. */
 660         tio_write(tio_read(CSR0)|TBMOD_ALIGN32|TBMOD_BURST0, CSR0);
 661 /*      tio_write(TBMOD_RESERVED|TBMOD_ALIGN32|TBMOD_BURST0, CSR0);*/
 662 
 663         if (irq2dev_map[dev->irq] != NULL
 664                 || (irq2dev_map[dev->irq] = dev) == NULL
 665                 || dev->irq == 0
 666 #if LINUX_VERSION_CODE < 0x10346
 667                 || request_irq(dev->irq, &tulip_interrupt, 0, tp->signature)) {
 668 #else
 669                 || request_irq(dev->irq, (void *)&tulip_interrupt, 0,
 670                                            tp->signature, dev)) {
 671 #endif
 672                 return -EAGAIN;
 673         }
 674 
 675 /*
 676         if (tulip_debug > 1)
 677                 printk("%s: tulip_open() irq %d.\n", dev->name, dev->irq);
 678 */
 679 
 680         tulip_init_ring(dev);
 681 
 682         /* Fill the whole address filter table with our physical address. */
 683         { 
 684                 unsigned short *eaddrs = (unsigned short *)dev->dev_addr;
 685                 int *setup_frm = tp->setup_frame, i;
 686 
 687                 /* You must add the broadcast address when doing perfect filtering! */
 688                 *setup_frm++ = 0xffff;
 689                 *setup_frm++ = 0xffff;
 690                 *setup_frm++ = 0xffff;
 691                 /* Fill the rest of the accept table with our physical address. */
 692                 for (i = 1; i < 16; i++) {
 693                         *setup_frm++ = eaddrs[0];
 694                         *setup_frm++ = eaddrs[1];
 695                         *setup_frm++ = eaddrs[2];
 696                 }
 697                 /* Put the setup frame on the Tx list. */
 698                 tp->tx_ring[0].length = 0x08000000 | 192;
 699                 tp->tx_ring[0].buffer1 = virt_to_bus(tp->setup_frame);
 700                 tp->tx_ring[0].buffer2 = 0;
 701                 tp->tx_ring[0].status = TRING_OWN;
 702 
 703                 tp->cur_tx++, tp->dirty_tx++;
 704         }
 705 
 706         tio_write(virt_to_bus(tp->rx_ring), CSR3);
 707         tio_write(virt_to_bus(tp->tx_ring), CSR4);
 708 
 709         dev->if_port = TULIP_PORT;
 710         if (tp->port_select) tp->port_select(dev);
 711         /* Start the chip's Tx and Rx processes. */
 712         tio_write(tio_read(CSR6) | TCMOD_TRxSTART
 713                           | (tp->full_duplex ? TCMOD_FULLDUPLEX:0), CSR6);
 714 
 715         /* Trigger an immediate transmit demand to process the setup frame. */
 716         tio_write(TPOLL_TRIGGER, CSR1);
 717 
 718         dev->tbusy = 0;
 719         dev->interrupt = 0;
 720         dev->start = 1;
 721 
 722         /* Enable interrupts by setting the interrupt mask. */
 723         tio_write(TINTR_ENABLE, CSR7);
 724 
 725         MOD_INC_USE_COUNT;
 726         return 0;
 727 }
 728 
 729 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
 730 static void
 731 tulip_init_ring(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 732 {
 733         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 734         int i;
 735 
 736         tp->tx_full = 0;
 737         tp->cur_rx = tp->cur_tx = 0;
 738         tp->dirty_rx = tp->dirty_tx = 0;
 739 
 740         for (i = 0; i < RX_RING_SIZE; i++) {
 741                 tp->rx_ring[i].status = TRING_OWN;
 742                 tp->rx_ring[i].length = PKT_BUF_SZ;
 743                 tp->rx_ring[i].buffer1 = virt_to_bus(tp->rx_buffs[i]);
 744                 tp->rx_ring[i].buffer2 = virt_to_bus(&tp->rx_ring[i+1]);
 745         }
 746         /* Mark the last entry as wrapping the ring. */ 
 747         tp->rx_ring[i-1].length = PKT_BUF_SZ | 0x02000000;
 748         tp->rx_ring[i-1].buffer2 = virt_to_bus(&tp->rx_ring[0]);
 749 
 750         /* The Tx buffer descriptor is filled in as needed, but we
 751            do need to clear the ownership bit. */
 752         for (i = 0; i < TX_RING_SIZE; i++) {
 753                 tp->tx_ring[i].status = 0x00000000;
 754         }
 755 }
 756 
 757 static int
 758 tulip_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 759 {
 760         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 761         int ioaddr = dev->base_addr;
 762         int entry;
 763 
 764         /* Transmitter timeout, serious problems. */
 765         if (dev->tbusy) {
 766                 int tickssofar = jiffies - dev->trans_start;
 767                 int i;
 768                 if (tickssofar < 40) return(1);
 769                 if (tp->port_select
 770                         && (!tp->port_error || tp->port_error(dev))) {
 771                         dev->if_port ++;
 772                         tp->port_select(dev);
 773                         dev->trans_start = jiffies;
 774                         return(0);
 775                 }
 776                 printk("%s: transmit timed out, status %8.8x,"
 777                            "SIA %8.8x %8.8x %8.8x %8.8x, resetting...\n",
 778                            dev->name, tio_read(CSR5), tio_read(CSR12),
 779                            tio_read(CSR13), tio_read(CSR14), tio_read(CSR15));
 780                 printk("  Rx ring %8.8x: ", (int)tp->rx_ring);
 781                 for (i = 0; i < RX_RING_SIZE; i++)
 782                         printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
 783                 printk("\n  Tx ring %8.8x: ", (int)tp->tx_ring);
 784                 for (i = 0; i < TX_RING_SIZE; i++)
 785                         printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
 786                 printk("\n");
 787 
 788                 tp->stats.tx_errors++;
 789                 /* Perhaps we should reinitialize the hardware here. */
 790                 dev->if_port = 0;
 791                 tio_write(TSIAC_CONFIG, CSR13);
 792                 /* Start the chip's Tx and Rx processes . */
 793                 tio_write(TCMOD_10TP | TCMOD_TRxSTART, CSR6);
 794                 /* Trigger an immediate transmit demand. */
 795                 tio_write(TPOLL_TRIGGER, CSR1);
 796 
 797                 dev->tbusy=0;
 798                 dev->trans_start = jiffies;
 799                 return(0);
 800         }
 801 
 802         if (skb == NULL || skb->len <= 0) {
 803                 printk("%s: Obsolete driver layer request made: skbuff==NULL.\n",
 804                            dev->name);
 805                 dev_tint(dev);
 806                 return(0);
 807         }
 808 
 809         /* Block a timer-based transmit from overlapping.  This could better be
 810            done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
 811            If this ever occurs the queue layer is doing something evil! */
 812         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 813                 printk("%s: Transmitter access conflict.\n", dev->name);
 814                 return 1;
 815         }
 816 
 817         /* Caution: the write order is important here, set the base address
 818            with the "ownership" bits last. */
 819 
 820         /* Calculate the next Tx descriptor entry. */
 821         entry = tp->cur_tx % TX_RING_SIZE;
 822 
 823         tp->tx_full = 1;
 824         tp->tx_skbuff[entry] = skb;
 825         tp->tx_ring[entry].length = skb->len |
 826                 (entry == TX_RING_SIZE-1 ? 0xe2000000 : 0xe0000000);
 827         tp->tx_ring[entry].buffer1 = virt_to_bus(skb->data);
 828         tp->tx_ring[entry].buffer2 = 0;
 829         tp->tx_ring[entry].status = TRING_OWN;  /* Pass ownership to the chip. */
 830 
 831         tp->cur_tx++;
 832 
 833         /* Trigger an immediate transmit demand. */
 834         tio_write(TPOLL_TRIGGER, CSR1);
 835 
 836         dev->trans_start = jiffies;
 837 
 838         return(0);
 839 }
 840 
 841 /* The interrupt handler does all of the Rx thread work and cleans up
 842    after the Tx thread. */
 843 static void tulip_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 844 {
 845         struct device *dev = (struct device *)(irq2dev_map[irq]);
 846         struct tulip_private *lp;
 847         int csr5, ioaddr, boguscnt=10;
 848 
 849         if (dev == NULL) {
 850                 printk ("tulip_interrupt(): irq %d for unknown device.\n", irq);
 851                 return;
 852         }
 853 
 854         ioaddr = dev->base_addr;
 855         lp = (struct tulip_private *)dev->priv;
 856         if (dev->interrupt)
 857                 printk("%s: Re-entering the interrupt handler.\n", dev->name);
 858 
 859         dev->interrupt = 1;
 860 
 861         do {
 862                 csr5 = tio_read(CSR5);
 863                 /* Acknowledge all of the current interrupt sources ASAP. */
 864                 tio_write(csr5 & TSTAT_CLEARINTR, CSR5);
 865 
 866                 /* check interrupt ? */
 867                 if ((csr5 & (TSTAT_NORINTR|TSTAT_ABNINTR)) == 0) break;
 868 
 869                 if (csr5 & TSTAT_RxINTR)                        /* Rx interrupt */
 870                         tulip_rx(dev);
 871 
 872                 if (csr5 & TSTAT_TxINTR) {              /* Tx-done interrupt */
 873                         int dirty_tx = lp->dirty_tx;
 874 
 875                         while (dirty_tx < lp->cur_tx) {
 876                                 int entry = dirty_tx % TX_RING_SIZE;
 877                                 int status = lp->tx_ring[entry].status;
 878 
 879                                 if (status < 0)
 880                                         break;                  /* It still hasn't been Txed */
 881 
 882                                 if (status & TRING_ERROR) {
 883                                         /* There was an major error, log it. */
 884                                         lp->stats.tx_errors++;
 885                                         if (status & TRING_TxABORT) lp->stats.tx_aborted_errors++;
 886                                         if (status & TRING_TxCARR) lp->stats.tx_carrier_errors++;
 887                                         if (status & TRING_TxWINDOW) lp->stats.tx_window_errors++;
 888                                         if (status & TRING_TxFIFO) lp->stats.tx_fifo_errors++;
 889                                         if ((status & TRING_TxHEARTBEAT) && !lp->full_duplex)
 890                                                 lp->stats.tx_heartbeat_errors++;
 891 #ifdef ETHER_STATS
 892                                         if (status & 0x0100) lp->stats.collisions16++;
 893 #endif
 894                                 } else {
 895 #ifdef ETHER_STATS
 896                                         if (status & 0x0001) lp->stats.tx_deferred++;
 897 #endif
 898                                         lp->stats.collisions += (status >> 3) & 15;
 899                                         lp->stats.tx_packets++;
 900                                 }
 901 
 902                                 /* Free the original skb. */
 903                                 dev_kfree_skb(lp->tx_skbuff[entry], FREE_WRITE);
 904                                 dirty_tx++;
 905                         }
 906 
 907 #ifndef final_version
 908                         if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
 909                                 printk("out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
 910                                            dirty_tx, lp->cur_tx, lp->tx_full);
 911                                 dirty_tx += TX_RING_SIZE;
 912                         }
 913 #endif
 914 
 915                         if (lp->tx_full && dev->tbusy
 916                                 && dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
 917                                 /* The ring is no longer full, clear tbusy. */
 918                                 lp->tx_full = 0;
 919                                 dev->tbusy = 0;
 920                                 mark_bh(NET_BH);
 921                         }
 922 
 923                         lp->dirty_tx = dirty_tx;
 924                 }
 925 
 926                 /* Log errors. */
 927                 if (csr5 & TSTAT_ABNINTR) {     /* Abnormal error summary bit. */
 928                         if (csr5 & TSTAT_TxTOUT) lp->stats.tx_errors++; /* Tx babble. */
 929                         if (csr5 & TSTAT_RxMISSED) {            /* Missed a Rx frame. */
 930                                 lp->stats.rx_errors++;
 931                                 lp->stats.rx_missed_errors += tio_read(CSR8) & 0xffff;
 932                         }
 933                         if (csr5 & TSTAT_TEXPIRED) {
 934                                 printk("%s: Something Wicked happened! %8.8x.\n",
 935                                            dev->name, csr5);
 936                                 /* Hmmmmm, it's not clear what to do here. */
 937                         }
 938                 }
 939                 if (--boguscnt < 0) {
 940                         printk("%s: Too much work at interrupt, csr5=0x%8.8x.\n",
 941                                    dev->name, csr5);
 942                         /* Clear all interrupt sources. */
 943                         tio_write(TSTAT_CLEARINTR, CSR5);
 944                         break;
 945                 }
 946         } while (1);
 947 
 948         /* Special code for testing *only*. */
 949         {
 950                 static int stopit = 10;
 951                 if (dev->start == 0  &&  --stopit < 0) {
 952                         printk("%s: Emergency stop, looping startup interrupt.\n",
 953                                    dev->name);
 954 #if LINUX_VERSION_CODE < 0x10346
 955                         free_irq(irq);
 956 #else
 957                         free_irq(irq, dev);
 958 #endif
 959                 }
 960         }
 961 
 962         dev->interrupt = 0;
 963         return;
 964 }
 965 
 966 static int
 967 tulip_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 968 {
 969         struct tulip_private *lp = (struct tulip_private *)dev->priv;
 970         int entry = lp->cur_rx % RX_RING_SIZE;
 971         int i;
 972 
 973         /* If we own the next entry, it's a new packet. Send it up. */
 974         while (lp->rx_ring[entry].status >= 0) {
 975                 int status = lp->rx_ring[entry].status;
 976 
 977                 if ((status & TRING_RxDESCMASK) != TRING_RxDESCMASK) {
 978                         printk("%s: Ethernet frame spanned multiple buffers,"
 979                                    "status %8.8x!\n", dev->name, status);
 980                 } else if (status & TRING_ERROR) {
 981                         /* There was a fatal error. */
 982                         lp->stats.rx_errors++; /* end of a packet.*/
 983                         if (status & TRING_RxLENGTH) lp->stats.rx_length_errors++;
 984                         if (status & TRING_RxFRAME) lp->stats.rx_frame_errors++;
 985                         if (status & TRING_RxCRC) lp->stats.rx_crc_errors++;
 986                         if (status & TRING_RxFIFO) lp->stats.rx_fifo_errors++;
 987                 } else {
 988                         /* Malloc up new buffer, compatible with net-2e. */
 989                         short pkt_len = lp->rx_ring[entry].status >> 16;
 990                         struct sk_buff *skb;
 991 
 992 #if LINUX_VERSION_CODE < 0x10300
 993                         skb = alloc_skb(pkt_len, GFP_ATOMIC);
 994 #else
 995                         skb = dev_alloc_skb(pkt_len + 2);
 996 #endif
 997                         if (skb == NULL) {
 998                                 printk("%s: Memory squeeze, deferring packet.\n",
 999                                            dev->name);
1000                                 /* Check that at least two ring entries are free.
1001                                    If not, free one and mark stats->rx_dropped++. */
1002                                 for (i=0; i < RX_RING_SIZE; i++)
1003                                         if (lp->rx_ring[(entry+i) % RX_RING_SIZE].status < 0)
1004                                                 break;
1005 
1006                                 if (i > RX_RING_SIZE -2) {
1007                                         lp->stats.rx_dropped++;
1008                                         lp->rx_ring[entry].status = TRING_OWN;
1009                                         lp->cur_rx++;
1010                                 }
1011                                 break;
1012                         }
1013                         skb->dev = dev;
1014 #if LINUX_VERSION_CODE < 0x10300
1015                         skb->len = pkt_len;
1016                         memcpy(skb->data, bus_to_virt(lp->rx_ring[entry].buffer1),
1017                                    pkt_len);
1018 #else
1019                         skb_reserve(skb, 2);
1020                         memcpy(skb_put(skb, pkt_len),
1021                                    bus_to_virt(lp->rx_ring[entry].buffer1), pkt_len);
1022                         /* Needed for 1.3.x */
1023                         skb->protocol = eth_type_trans(skb,dev);
1024 #endif
1025                         netif_rx(skb);
1026                         lp->stats.rx_packets++;
1027                 }
1028 
1029                 lp->rx_ring[entry].status = TRING_OWN;
1030                 entry = (++lp->cur_rx) % RX_RING_SIZE;
1031         }
1032         return(0);
1033 }
1034 
1035 static int
1036 tulip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1037 {
1038         int ioaddr = dev->base_addr;
1039         struct tulip_private *tp = (struct tulip_private *)dev->priv;
1040 
1041         dev->start = 0;
1042         dev->tbusy = 1;
1043 
1044         /* Disable interrupts by clearing the interrupt mask. */
1045         tio_write(TINTR_DISABLE, CSR7);
1046         /* Stop the chip's Tx and Rx processes. */
1047         tio_write(tio_read(CSR6) & ~(TCMOD_TRxSTART), CSR6);
1048         /* Leave the card in 10baseT state. */
1049         tio_write(TSIAC_CONFIG, CSR13);
1050 
1051         tp->stats.rx_missed_errors += tio_read(CSR8) & 0xffff;
1052 
1053         tio_write(0, CSR13);
1054 /*      tio_write(0, CSR8);     wake up chip ? */
1055 
1056 #if LINUX_VERSION_CODE < 0x10346
1057         free_irq(dev->irq);
1058 #else
1059         free_irq(dev->irq, dev);
1060 #endif
1061         irq2dev_map[dev->irq] = 0;
1062 
1063         MOD_DEC_USE_COUNT;
1064         return(0);
1065 }
1066 
1067 static struct enet_statistics *
1068 tulip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1069 {
1070         struct tulip_private *tp = (struct tulip_private *)dev->priv;
1071         short ioaddr = dev->base_addr;
1072 
1073         tp->stats.rx_missed_errors += tio_read(CSR8) & 0xffff;
1074 
1075         return(&tp->stats);
1076 }
1077 
1078 /*
1079  *      Set or clear the multicast filter for this adaptor.
1080  */
1081 
1082 #if LINUX_VERSION_CODE < 0x10300
1083 static void set_multicast_list(struct device *dev, int num_addrs,
     /* [previous][next][first][last][top][bottom][index][help] */
1084                                                            void *addrs)
1085 #else
1086 static void set_multicast_list(struct device *dev)
1087 #endif
1088 {
1089         short ioaddr = dev->base_addr;
1090         int csr6 = tio_read(CSR6) & ~(TCMOD_MODEMASK|TCMOD_FILTERMASK);
1091 
1092         if (dev->flags&IFF_PROMISC) 
1093         {                       /* Set promiscuous. why ALLMULTI ? */
1094                 tio_write(csr6 | TCMOD_PROMISC | TCMOD_ALLMCAST, CSR6);
1095                 /* Log any net taps. */
1096                 printk("%s: Promiscuous mode enabled.\n", dev->name);
1097         }
1098         else if (dev->mc_count > 15 || (dev->flags&IFF_ALLMULTI)) 
1099         {
1100                 /* Too many to filter perfectly -- accept all multicasts. */
1101                 tio_write(csr6 | TCMOD_ALLMCAST, CSR6);
1102         }
1103         else
1104         {
1105                 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1106                 struct dev_mc_list *dmi=dev->mc_list;
1107                 int *setup_frm = tp->setup_frame;
1108                 unsigned short *eaddrs;
1109                 int i;
1110 
1111                 /* We have <= 15 addresses that we can use the wonderful
1112                    16 address perfect filtering of the Tulip.  Note that only
1113                    the low shortword of setup_frame[] is valid. */
1114                 tio_write(csr6 | 0x0000, CSR6);
1115                 i=0;
1116                 while(dmi) 
1117                 {
1118                         eaddrs=(unsigned short *)dmi->dmi_addr;
1119                         dmi=dmi->next;
1120                         i++;
1121                         *setup_frm++ = *eaddrs++;
1122                         *setup_frm++ = *eaddrs++;
1123                         *setup_frm++ = *eaddrs++;
1124                 }
1125                 /* Fill the rest of the table with our physical address. */
1126                 eaddrs = (unsigned short *)dev->dev_addr;
1127                 do {
1128                         *setup_frm++ = eaddrs[0];
1129                         *setup_frm++ = eaddrs[1];
1130                         *setup_frm++ = eaddrs[2];
1131                 } while (++i < 16);
1132 
1133                 /* Now add this frame to the Tx list. */
1134         }
1135 }
1136 
1137 #if 0
1138 static int
1139 set_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1140 {
1141         int i;
1142         struct sockaddr *sa=(struct sockaddr *)addr;
1143         if (dev->start)
1144                 return -EBUSY;
1145         printk("%s: Setting MAC address to ", dev->name);
1146         for (i = 0; i < ETH_ALEN - 1; i++)
1147                 printk("%2.2x:", dev->dev_addr[i] = sa->sa_data[i]);
1148         printk("%2.2x.\n", dev->dev_addr[i] = sa->sa_data[i]);
1149         return 0;
1150 }
1151 #endif
1152 
1153 static struct device *tulip_alloc(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1154 {
1155         struct tulip_private *tp;
1156         char *buff;
1157 #ifndef MODULE
1158         int alloc_size;
1159 #endif
1160         if (!dev || dev->priv) {
1161                 struct device *olddev = dev;
1162 
1163                 alloc_size = sizeof(struct device)
1164                         + sizeof(struct tulip_private)
1165                         + ETHNAMSIZ;
1166                 alloc_size = ROUND_UP(alloc_size, 8);
1167 
1168                 buff = (char *)kmalloc(alloc_size, GFP_KERNEL);
1169                 dev = (struct device *)buff;
1170                 if (dev == NULL) {
1171                         printk("tulip_alloc: kmalloc failed.\n");
1172                         return(NULL);
1173                 }
1174                 tp = (struct tulip_private *)(buff + sizeof(struct device));
1175                 memset(buff, 0, alloc_size);
1176                 dev->priv = (void *)tp;
1177                 dev->name = (char *)(buff + sizeof(struct device)
1178                                                          + sizeof(struct tulip_private));
1179                 if (olddev) {
1180                         dev->next = olddev->next;
1181                         olddev->next = dev;
1182                 }
1183         } else {
1184                 alloc_size = ROUND_UP(sizeof(struct tulip_private), 8);
1185                 tp = (struct tulip_private *)kmalloc(alloc_size, GFP_KERNEL);
1186                 memset((void *)tp, 0, alloc_size);
1187                 dev->priv = (void *)tp;
1188         }
1189         return(dev);
1190 }
1191 
1192 int
1193 tulip_hwinit(struct device *dev, int ioaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1194                          int irq, int device_id)
1195 {
1196         /* See note below on the Znyx 315 etherarray. */
1197         unsigned char last_phys_addr[6] = {0x00, 'L', 'i', 'n', 'u', 'x'};
1198         char detect_mesg[80], *mesgp=detect_mesg, *card_name=NULL;
1199         struct tulip_private *tp = (struct tulip_private *)dev->priv;
1200         int i;
1201         unsigned short sum, bitsum;
1202 
1203         if (check_region(ioaddr, TULIP_TOTAL_SIZE) != 0) {
1204                 printk("tulip_alloc: region already allocated at %#3x.\n",
1205                            ioaddr);
1206                 return(-1);
1207         }
1208 
1209         mesgp += sprintf(mesgp, "(DEC 21%d4%d Tulip",
1210                                          device_id == PCI_DEVICE_ID_DEC_TULIP_FAST,
1211                                          device_id == PCI_DEVICE_ID_DEC_TULIP_PLUS);
1212 
1213         /* Stop the chip's Tx and Rx processes. */
1214         tio_write(tio_read(CSR6) & ~TCMOD_TRxSTART, CSR6);
1215         /* Clear the missed-packet counter. */
1216         tio_read(CSR8) & 0xffff;
1217 
1218         if (device_id == PCI_DEVICE_ID_DEC_TULIP_PLUS
1219             && (tio_read(CSR9) & 0x8000)) {
1220                 mesgp += sprintf(mesgp, "treat as 21040 ");
1221             device_id = PCI_DEVICE_ID_DEC_TULIP;
1222         }
1223         
1224         /* The station address ROM is read byte serially.  The register must
1225            be polled, waiting for the value to be read bit serially from the
1226            EEPROM.
1227            */
1228         sum = 0;
1229         if (device_id == PCI_DEVICE_ID_DEC_TULIP) {
1230                 tio_write(0, CSR9);
1231             /* Reset the pointer with a dummy write. */
1232             bitsum = 0xff;
1233             for (i = 0; i < 6; i++) {
1234                         int value, boguscnt = 100000;
1235                         do
1236                                 value = tio_read(CSR9);
1237                         while (value < 0  && --boguscnt > 0);
1238                         dev->dev_addr[i] = value;
1239                         sum += value;
1240                         bitsum &= value;
1241             }
1242         } else {
1243             /* Must be a 21140/21041, with a serial EEPROM interface. */
1244             struct eeprom eep;
1245             u_char *addr;
1246 
1247             if (read_eeprom(ioaddr, &eep) < 0) {
1248                         addr = eep.ng_addr;/* broken EEPROM structure */
1249             } else {
1250                         addr = eep.ok_addr;/* DEC EtherWorks */
1251             }
1252             for (i = 0; i < ETH_ALEN; i++) {
1253                         sum += addr[i];
1254                         dev->dev_addr[i] = addr[i];
1255             }
1256         }
1257         /* Make certain the data structures are quadword aligned. */
1258 
1259         mesgp += sprintf(mesgp, ") at %#3x, ", ioaddr);
1260 
1261         /* On the Zynx 315 etherarray boards only the first Tulip has an EEPROM.
1262            The addresses of the subsequent ports are derived from the first. */
1263         if (sum == 0) {
1264                 for (i = 0; i < ETH_ALEN - 1; i++)
1265                         dev->dev_addr[i] = last_phys_addr[i];
1266                 dev->dev_addr[i] = last_phys_addr[i] + 1;
1267         }
1268         for (i = 0; i < ETH_ALEN - 1; i++)
1269                 mesgp += sprintf(mesgp, "%2.2x:",
1270                                                  last_phys_addr[i] = dev->dev_addr[i]);
1271         mesgp += sprintf(mesgp, "%2.2x, IRQ %d\n",
1272                                          last_phys_addr[i] = dev->dev_addr[i], irq);
1273 
1274         card_name = card_type(tp, device_id,
1275                                                   htonl((*(int*)dev->dev_addr) & 0xFFFFFF));
1276 
1277         /* We do a request_region() only to register /proc/ioports info. */
1278         request_region(ioaddr, TULIP_TOTAL_SIZE, tp->signature);
1279 
1280         dev->base_addr = ioaddr;
1281         dev->irq = irq;
1282 
1283 #ifdef TULIP_FULL_DUPLEX
1284         tp->full_duplex = 1;
1285 #endif
1286 
1287         /* The Tulip-specific entries in the device structure. */
1288         dev->open = &tulip_open;
1289         dev->hard_start_xmit = &tulip_start_xmit;
1290         dev->stop = &tulip_close;
1291         dev->get_stats = &tulip_get_stats;
1292         dev->set_multicast_list = &set_multicast_list;
1293 #if 0
1294         dev->set_mac_address = &set_mac_address;
1295 #endif
1296 
1297 #ifdef  MODULE
1298     ether_setup(dev);
1299 #else
1300     init_etherdev(dev, 0);
1301 #endif
1302 
1303         printk("%s: %s %s", dev->name, card_name, detect_mesg);
1304 
1305         /* Reset the xcvr interface and turn on heartbeat. */
1306         tio_write(TSIAC_RESET, CSR13);
1307         tio_write(TSIAC_CONFIG, CSR13);
1308 
1309         return(0);
1310 }
1311 
1312 int tulip_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1313 {
1314         static u_short probed_irqs=0;
1315         u_char pci_bus, pci_device_fn, pci_latency, pci_irq;
1316         u_int pci_ioaddr;
1317         u_short pci_command;
1318         u_long pci_chips[] = {
1319                 PCI_DEVICE_ID_DEC_TULIP,
1320                 PCI_DEVICE_ID_DEC_TULIP_FAST,
1321                 PCI_DEVICE_ID_DEC_TULIP_PLUS,
1322                 PCI_DEVICE_ID_NONE
1323         };
1324         int num=0, cno;
1325         int pci_index;
1326 
1327     if (!pcibios_present()) return(-ENODEV);
1328 
1329         for (pci_index = 0; pci_index < 8; pci_index++) {
1330                 /* Search for the PCI_DEVICE_ID_DEV_TULIP* chips */
1331                 for (cno = 0; pci_chips[cno] != PCI_DEVICE_ID_NONE; cno ++)
1332                         if (pcibios_find_device(PCI_VENDOR_ID_DEC,
1333                                                                         pci_chips[cno],
1334                                                                         pci_index, &pci_bus,
1335                                                                         &pci_device_fn) == 0) {
1336                                 /* get IRQ */
1337                                 pcibios_read_config_byte(pci_bus, pci_device_fn,
1338                                                                                  PCI_INTERRUPT_LINE, &pci_irq);
1339                                 if (probed_irqs & (1 << pci_irq)) continue;
1340                                 /* get IO address */
1341                                 pcibios_read_config_dword(pci_bus, pci_device_fn,
1342                                                                                   PCI_BASE_ADDRESS_0,
1343                                                                                   &pci_ioaddr);
1344                                 /* Remove I/O space marker in bit 0. */
1345                                 pci_ioaddr &= ~3;
1346 #ifdef  MODULE
1347                                 /* compare requested IRQ/IO address */
1348                                 if (dev && dev->irq && dev->base_addr &&
1349                                         (dev->irq != pci_irq
1350                                          || dev->base_addr != pci_ioaddr)) continue;
1351 #else
1352                                 if ((dev = tulip_alloc(dev)) == NULL) break;
1353 #endif
1354                                 if (!probed_irqs) printk(version);
1355                                 probed_irqs |= (1 << pci_irq);
1356                                 
1357                                 /* Get and check the bus-master and latency values. */
1358                                 pcibios_read_config_word(pci_bus, pci_device_fn,
1359                                                                                  PCI_COMMAND, &pci_command);
1360                                 if ( ! (pci_command & PCI_COMMAND_MASTER)) {
1361                                         printk("  PCI Master Bit has not been set!"
1362                                                    " Setting...\n");
1363                                         pci_command |= PCI_COMMAND_MASTER;
1364                                         pcibios_write_config_word(pci_bus, pci_device_fn,
1365                                                                                           PCI_COMMAND, pci_command);
1366                                 }
1367                                 pcibios_read_config_byte(pci_bus, pci_device_fn,
1368                                                                                  PCI_LATENCY_TIMER,
1369                                                                                  &pci_latency);
1370                                 if (pci_latency < 10) {
1371                                         printk("  PCI latency timer (CFLT) is"
1372                                                    " unreasonably low at %d."
1373                                                    "  Setting to 100 clocks.\n", pci_latency);
1374                                         pcibios_write_config_byte(pci_bus, pci_device_fn,
1375                                                                                           PCI_LATENCY_TIMER, 100);
1376                                 }
1377                                 if (tulip_hwinit(dev, pci_ioaddr, pci_irq,
1378                                                                  pci_chips[cno]) < 0) continue;
1379                                 num ++;
1380 #if defined(MODULE) || defined(TULIP_ONLY_ONE)
1381                                 return(0);
1382 #endif
1383                 }
1384         }
1385         return(num > 0 ? 0: -ENODEV);
1386 }
1387 
1388 #ifdef MODULE
1389 static int io = 0xfc00;
1390 static int irq = 9;
1391 
1392 static struct device *mod_dev;
1393 
1394 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1395 {
1396         if ((mod_dev = tulip_alloc(0)) == NULL) return(-EIO);
1397 
1398         mod_dev->base_addr = io;
1399         mod_dev->irq = irq;
1400         mod_dev->init = &tulip_probe;
1401 
1402         if (register_netdev(mod_dev)) {
1403                 printk("tulip: register_netdev() returned non-zero.\n");
1404                 kfree_s(mod_dev, alloc_size);
1405                 return -EIO;
1406         }
1407         return(0);
1408 }
1409 
1410 void
1411 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1412 {
1413         release_region(mod_dev->base_addr, TULIP_TOTAL_SIZE);
1414         unregister_netdev(mod_dev);
1415         kfree_s(mod_dev, alloc_size);
1416 }
1417 
1418 #endif /* MODULE */
1419 
1420 
1421 /*
1422  * Local variables:
1423  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c tulip.c"
1424  *  c-indent-level: 4
1425  *  tab-width: 4
1426  * End:
1427  */

/* [previous][next][first][last][top][bottom][index][help] */