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 10baseT (i.e. non-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         const char *if_port;
 565 
 566         dev->if_port &= 3;
 567         switch (dev->if_port)
 568         {
 569         case TULIP_10TP_PORT:
 570                 if_port = "10baseT";
 571                 break;
 572         case TULIP_100TP_PORT:
 573         /* TULIP_AUI_PORT is the same as TULIP_100TP_PORT. */
 574                 if_port = "100baseT/AUI";
 575                 break;
 576         case TULIP_BNC_PORT:
 577                 if_port = "BNC";
 578                 break;
 579         default:
 580                 if_port = "unknown type";
 581                 break;
 582         }
 583         printk("%s: enabling %s port.\n", dev->name, if_port);
 584         /* Set the full duplex match frame. */
 585         tio_write(FULL_DUPLEX_MAGIC, CSR11);
 586         tio_write(TSIAC_RESET, CSR13);
 587         /* Reset the serial interface */
 588         tio_write((dev->if_port ? TSIAC_NO10TP: 0) | TSIAC_C21040, CSR13);
 589 }
 590 
 591 static void
 592 generic21041_select(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 593 {
 594         int ioaddr = dev->base_addr;
 595         u32 tsiac = TSIAC_C21041;
 596         u32 tsiax = TSIAX_10TP;
 597         u32 tsiag = TSIAG_10TP;
 598 
 599         printk("%s: enabling ", dev->name);
 600         switch(dev->if_port) {
 601         case TULIP_AUI_PORT:
 602                 tsiac |= TSIAC_NO10TP;
 603                 tsiax = TSIAX_NO10TP;
 604                 tsiag = TSIAG_AUI;
 605                 printk("AUI");
 606                 break;
 607         case TULIP_BNC_PORT:
 608                 tsiac |= TSIAC_NO10TP;
 609                 tsiax = TSIAX_NO10TP;
 610                 tsiag = TSIAG_BNC;
 611                 printk("BNC");
 612                 break;
 613         default:
 614                 dev->if_port = TULIP_10TP_PORT;
 615                 printk("10TP");
 616                 break;
 617         }
 618         tio_sia_write(ioaddr, tsiac, tsiax, tsiag);
 619         printk(" port.\n");
 620 }
 621 
 622 static void
 623 auto21140_select(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 624 {
 625         int ioaddr = dev->base_addr;
 626         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 627 
 628         /* kick port */
 629         tio_write(TPOLL_TRIGGER, CSR1);
 630         tio_write(TINTR_ENABLE, CSR7);
 631         tio_write(TCMOD_AUTO|TCMOD_TRxSTART, CSR6);
 632         dev->if_port = !(tio_read(CSR12) & TGEPR_FORCEALED);
 633         printk("%s: probed %s port.\n",
 634                    dev->name, dev->if_port ? "100baseTx" : "10baseT");
 635         tio_write((dev->if_port ? TGEPR_FORCE100: 0)
 636                           | (tp->full_duplex ? 0:TGEPR_HALFDUPLEX), CSR12);     
 637         tio_write(TINTR_DISABLE, CSR7);
 638         tio_read(CSR8) & 0xffff;
 639         tio_write(TCMOD_AUTO, CSR6);
 640 }
 641 
 642 static void
 643 generic21140_select(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 644 {
 645         int ioaddr = dev->base_addr, csr6;
 646         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 647 
 648         dev->if_port &= 1;
 649         csr6 = tio_read(CSR6) &
 650                 ~(TCMOD_10TP|TCMOD_100TP|TCMOD_TRxSTART|TCMOD_SCRM);
 651 
 652         /* Stop the transmit process. */
 653         tio_write(csr6 | TCMOD_RxSTART, CSR6);
 654         printk("%s: enabling %s port.\n",
 655                    dev->name, dev->if_port ? "100baseTx" : "10baseT");
 656         tio_write((dev->if_port ? TCMOD_100TP: TCMOD_10TP)
 657                           | TCMOD_TRxSTART | TCMOD_TH128 | csr6, CSR6);
 658         tio_write((dev->if_port ? TGEPR_FORCE100: 0)
 659                           | (tp->full_duplex ? 0:TGEPR_HALFDUPLEX), CSR12);
 660 }
 661 
 662 static int
 663 tulip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 664 {
 665         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 666         int ioaddr = dev->base_addr;
 667 
 668         /* Reset the chip, holding bit 0 set at least 10 PCI cycles. */
 669         tio_write(tio_read(CSR0)|TBMOD_RESET, CSR0);
 670 /*      tio_write(TBMOD_RESERVED|TBMOD_RESET, CSR0);*/
 671         SLOW_DOWN_IO;
 672         /* Deassert reset.  Set 8 longword cache alignment, 8 longword burst.
 673            -> Set 32 longword cache alignment, unlimited longword burst ?
 674            Wait the specified 50 PCI cycles after a reset by initializing
 675            Tx and Rx queues and the address filter list. */
 676         tio_write(tio_read(CSR0)|TBMOD_ALIGN32|TBMOD_BURST0, CSR0);
 677 /*      tio_write(TBMOD_RESERVED|TBMOD_ALIGN32|TBMOD_BURST0, CSR0);*/
 678 
 679         if (irq2dev_map[dev->irq] != NULL
 680                 || (irq2dev_map[dev->irq] = dev) == NULL
 681                 || dev->irq == 0
 682 #if LINUX_VERSION_CODE < 0x10346
 683                 || request_irq(dev->irq, &tulip_interrupt, 0, tp->signature)) {
 684 #else
 685                 || request_irq(dev->irq, (void *)&tulip_interrupt, 0,
 686                                            tp->signature, dev)) {
 687 #endif
 688                 return -EAGAIN;
 689         }
 690 
 691 /*
 692         if (tulip_debug > 1)
 693                 printk("%s: tulip_open() irq %d.\n", dev->name, dev->irq);
 694 */
 695 
 696         tulip_init_ring(dev);
 697 
 698         /* Fill the whole address filter table with our physical address. */
 699         { 
 700                 unsigned short *eaddrs = (unsigned short *)dev->dev_addr;
 701                 int *setup_frm = tp->setup_frame, i;
 702 
 703                 /* You must add the broadcast address when doing perfect filtering! */
 704                 *setup_frm++ = 0xffff;
 705                 *setup_frm++ = 0xffff;
 706                 *setup_frm++ = 0xffff;
 707                 /* Fill the rest of the accept table with our physical address. */
 708                 for (i = 1; i < 16; i++) {
 709                         *setup_frm++ = eaddrs[0];
 710                         *setup_frm++ = eaddrs[1];
 711                         *setup_frm++ = eaddrs[2];
 712                 }
 713                 /* Put the setup frame on the Tx list. */
 714                 tp->tx_ring[0].length = 0x08000000 | 192;
 715                 tp->tx_ring[0].buffer1 = virt_to_bus(tp->setup_frame);
 716                 tp->tx_ring[0].buffer2 = 0;
 717                 tp->tx_ring[0].status = TRING_OWN;
 718 
 719                 tp->cur_tx++, tp->dirty_tx++;
 720         }
 721 
 722         tio_write(virt_to_bus(tp->rx_ring), CSR3);
 723         tio_write(virt_to_bus(tp->tx_ring), CSR4);
 724 
 725         dev->if_port = TULIP_PORT;
 726         if (tp->port_select) tp->port_select(dev);
 727         /* Start the chip's Tx and Rx processes. */
 728         tio_write(tio_read(CSR6) | TCMOD_TRxSTART
 729                           | (tp->full_duplex ? TCMOD_FULLDUPLEX:0), CSR6);
 730 
 731         /* Trigger an immediate transmit demand to process the setup frame. */
 732         tio_write(TPOLL_TRIGGER, CSR1);
 733 
 734         dev->tbusy = 0;
 735         dev->interrupt = 0;
 736         dev->start = 1;
 737 
 738         /* Enable interrupts by setting the interrupt mask. */
 739         tio_write(TINTR_ENABLE, CSR7);
 740 
 741         MOD_INC_USE_COUNT;
 742         return 0;
 743 }
 744 
 745 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
 746 static void
 747 tulip_init_ring(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 748 {
 749         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 750         int i;
 751 
 752         tp->tx_full = 0;
 753         tp->cur_rx = tp->cur_tx = 0;
 754         tp->dirty_rx = tp->dirty_tx = 0;
 755 
 756         for (i = 0; i < RX_RING_SIZE; i++) {
 757                 tp->rx_ring[i].status = TRING_OWN;
 758                 tp->rx_ring[i].length = PKT_BUF_SZ;
 759                 tp->rx_ring[i].buffer1 = virt_to_bus(tp->rx_buffs[i]);
 760                 tp->rx_ring[i].buffer2 = virt_to_bus(&tp->rx_ring[i+1]);
 761         }
 762         /* Mark the last entry as wrapping the ring. */ 
 763         tp->rx_ring[i-1].length = PKT_BUF_SZ | 0x02000000;
 764         tp->rx_ring[i-1].buffer2 = virt_to_bus(&tp->rx_ring[0]);
 765 
 766         /* The Tx buffer descriptor is filled in as needed, but we
 767            do need to clear the ownership bit. */
 768         for (i = 0; i < TX_RING_SIZE; i++) {
 769                 tp->tx_ring[i].status = 0x00000000;
 770         }
 771 }
 772 
 773 static int
 774 tulip_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 775 {
 776         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 777         int ioaddr = dev->base_addr;
 778         int entry;
 779 
 780         /* Transmitter timeout, serious problems. */
 781         if (dev->tbusy) {
 782                 int tickssofar = jiffies - dev->trans_start;
 783                 int i;
 784                 if (tickssofar < 40) return(1);
 785                 if (tp->port_select
 786                         && (!tp->port_error || tp->port_error(dev))) {
 787                         dev->if_port ++;
 788                         tp->port_select(dev);
 789                         dev->trans_start = jiffies;
 790                         return(0);
 791                 }
 792                 printk("%s: transmit timed out, status %8.8x,"
 793                            "SIA %8.8x %8.8x %8.8x %8.8x, resetting...\n",
 794                            dev->name, tio_read(CSR5), tio_read(CSR12),
 795                            tio_read(CSR13), tio_read(CSR14), tio_read(CSR15));
 796                 printk("  Rx ring %8.8x: ", (int)tp->rx_ring);
 797                 for (i = 0; i < RX_RING_SIZE; i++)
 798                         printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
 799                 printk("\n  Tx ring %8.8x: ", (int)tp->tx_ring);
 800                 for (i = 0; i < TX_RING_SIZE; i++)
 801                         printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
 802                 printk("\n");
 803 
 804                 tp->stats.tx_errors++;
 805                 /* Perhaps we should reinitialize the hardware here. */
 806                 dev->if_port = 0;
 807                 tio_write(TSIAC_CONFIG, CSR13);
 808                 /* Start the chip's Tx and Rx processes . */
 809                 tio_write(TCMOD_10TP | TCMOD_TRxSTART, CSR6);
 810                 /* Trigger an immediate transmit demand. */
 811                 tio_write(TPOLL_TRIGGER, CSR1);
 812 
 813                 dev->tbusy=0;
 814                 dev->trans_start = jiffies;
 815                 return(0);
 816         }
 817 
 818         if (skb == NULL || skb->len <= 0) {
 819                 printk("%s: Obsolete driver layer request made: skbuff==NULL.\n",
 820                            dev->name);
 821                 dev_tint(dev);
 822                 return(0);
 823         }
 824 
 825         /* Block a timer-based transmit from overlapping.  This could better be
 826            done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
 827            If this ever occurs the queue layer is doing something evil! */
 828         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 829                 printk("%s: Transmitter access conflict.\n", dev->name);
 830                 return 1;
 831         }
 832 
 833         /* Caution: the write order is important here, set the base address
 834            with the "ownership" bits last. */
 835 
 836         /* Calculate the next Tx descriptor entry. */
 837         entry = tp->cur_tx % TX_RING_SIZE;
 838 
 839         tp->tx_full = 1;
 840         tp->tx_skbuff[entry] = skb;
 841         tp->tx_ring[entry].length = skb->len |
 842                 (entry == TX_RING_SIZE-1 ? 0xe2000000 : 0xe0000000);
 843         tp->tx_ring[entry].buffer1 = virt_to_bus(skb->data);
 844         tp->tx_ring[entry].buffer2 = 0;
 845         tp->tx_ring[entry].status = TRING_OWN;  /* Pass ownership to the chip. */
 846 
 847         tp->cur_tx++;
 848 
 849         /* Trigger an immediate transmit demand. */
 850         tio_write(TPOLL_TRIGGER, CSR1);
 851 
 852         dev->trans_start = jiffies;
 853 
 854         return(0);
 855 }
 856 
 857 /* The interrupt handler does all of the Rx thread work and cleans up
 858    after the Tx thread. */
 859 static void tulip_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 860 {
 861         struct device *dev = (struct device *)(irq2dev_map[irq]);
 862         struct tulip_private *lp;
 863         int csr5, ioaddr, boguscnt=10;
 864 
 865         if (dev == NULL) {
 866                 printk ("tulip_interrupt(): irq %d for unknown device.\n", irq);
 867                 return;
 868         }
 869 
 870         ioaddr = dev->base_addr;
 871         lp = (struct tulip_private *)dev->priv;
 872         if (dev->interrupt)
 873                 printk("%s: Re-entering the interrupt handler.\n", dev->name);
 874 
 875         dev->interrupt = 1;
 876 
 877         do {
 878                 csr5 = tio_read(CSR5);
 879                 /* Acknowledge all of the current interrupt sources ASAP. */
 880                 tio_write(csr5 & TSTAT_CLEARINTR, CSR5);
 881 
 882                 /* check interrupt ? */
 883                 if ((csr5 & (TSTAT_NORINTR|TSTAT_ABNINTR)) == 0) break;
 884 
 885                 if (csr5 & TSTAT_RxINTR)                        /* Rx interrupt */
 886                         tulip_rx(dev);
 887 
 888                 if (csr5 & TSTAT_TxINTR) {              /* Tx-done interrupt */
 889                         int dirty_tx = lp->dirty_tx;
 890 
 891                         while (dirty_tx < lp->cur_tx) {
 892                                 int entry = dirty_tx % TX_RING_SIZE;
 893                                 int status = lp->tx_ring[entry].status;
 894 
 895                                 if (status < 0)
 896                                         break;                  /* It still hasn't been Txed */
 897 
 898                                 if (status & TRING_ERROR) {
 899                                         /* There was an major error, log it. */
 900                                         lp->stats.tx_errors++;
 901                                         if (status & TRING_TxABORT) lp->stats.tx_aborted_errors++;
 902                                         if (status & TRING_TxCARR) lp->stats.tx_carrier_errors++;
 903                                         if (status & TRING_TxWINDOW) lp->stats.tx_window_errors++;
 904                                         if (status & TRING_TxFIFO) lp->stats.tx_fifo_errors++;
 905                                         if ((status & TRING_TxHEARTBEAT) && !lp->full_duplex)
 906                                                 lp->stats.tx_heartbeat_errors++;
 907 #ifdef ETHER_STATS
 908                                         if (status & 0x0100) lp->stats.collisions16++;
 909 #endif
 910                                 } else {
 911 #ifdef ETHER_STATS
 912                                         if (status & 0x0001) lp->stats.tx_deferred++;
 913 #endif
 914                                         lp->stats.collisions += (status >> 3) & 15;
 915                                         lp->stats.tx_packets++;
 916                                 }
 917 
 918                                 /* Free the original skb. */
 919                                 dev_kfree_skb(lp->tx_skbuff[entry], FREE_WRITE);
 920                                 dirty_tx++;
 921                         }
 922 
 923 #ifndef final_version
 924                         if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
 925                                 printk("out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
 926                                            dirty_tx, lp->cur_tx, lp->tx_full);
 927                                 dirty_tx += TX_RING_SIZE;
 928                         }
 929 #endif
 930 
 931                         if (lp->tx_full && dev->tbusy
 932                                 && dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
 933                                 /* The ring is no longer full, clear tbusy. */
 934                                 lp->tx_full = 0;
 935                                 dev->tbusy = 0;
 936                                 mark_bh(NET_BH);
 937                         }
 938 
 939                         lp->dirty_tx = dirty_tx;
 940                 }
 941 
 942                 /* Log errors. */
 943                 if (csr5 & TSTAT_ABNINTR) {     /* Abnormal error summary bit. */
 944                         if (csr5 & TSTAT_TxTOUT) lp->stats.tx_errors++; /* Tx babble. */
 945                         if (csr5 & TSTAT_RxMISSED) {            /* Missed a Rx frame. */
 946                                 lp->stats.rx_errors++;
 947                                 lp->stats.rx_missed_errors += tio_read(CSR8) & 0xffff;
 948                         }
 949                         if (csr5 & TSTAT_TEXPIRED) {
 950                                 printk("%s: Something Wicked happened! %8.8x.\n",
 951                                            dev->name, csr5);
 952                                 /* Hmmmmm, it's not clear what to do here. */
 953                         }
 954                 }
 955                 if (--boguscnt < 0) {
 956                         printk("%s: Too much work at interrupt, csr5=0x%8.8x.\n",
 957                                    dev->name, csr5);
 958                         /* Clear all interrupt sources. */
 959                         tio_write(TSTAT_CLEARINTR, CSR5);
 960                         break;
 961                 }
 962         } while (1);
 963 
 964         /* Special code for testing *only*. */
 965         {
 966                 static int stopit = 10;
 967                 if (dev->start == 0  &&  --stopit < 0) {
 968                         printk("%s: Emergency stop, looping startup interrupt.\n",
 969                                    dev->name);
 970 #if LINUX_VERSION_CODE < 0x10346
 971                         free_irq(irq);
 972 #else
 973                         free_irq(irq, dev);
 974 #endif
 975                 }
 976         }
 977 
 978         dev->interrupt = 0;
 979         return;
 980 }
 981 
 982 static int
 983 tulip_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 984 {
 985         struct tulip_private *lp = (struct tulip_private *)dev->priv;
 986         int entry = lp->cur_rx % RX_RING_SIZE;
 987         int i;
 988 
 989         /* If we own the next entry, it's a new packet. Send it up. */
 990         while (lp->rx_ring[entry].status >= 0) {
 991                 int status = lp->rx_ring[entry].status;
 992 
 993                 if ((status & TRING_RxDESCMASK) != TRING_RxDESCMASK) {
 994                         printk("%s: Ethernet frame spanned multiple buffers,"
 995                                    "status %8.8x!\n", dev->name, status);
 996                 } else if (status & TRING_ERROR) {
 997                         /* There was a fatal error. */
 998                         lp->stats.rx_errors++; /* end of a packet.*/
 999                         if (status & TRING_RxLENGTH) lp->stats.rx_length_errors++;
1000                         if (status & TRING_RxFRAME) lp->stats.rx_frame_errors++;
1001                         if (status & TRING_RxCRC) lp->stats.rx_crc_errors++;
1002                         if (status & TRING_RxFIFO) lp->stats.rx_fifo_errors++;
1003                 } else {
1004                         /* Malloc up new buffer, compatible with net-2e. */
1005                         short pkt_len = lp->rx_ring[entry].status >> 16;
1006                         struct sk_buff *skb;
1007 
1008 #if LINUX_VERSION_CODE < 0x10300
1009                         skb = alloc_skb(pkt_len, GFP_ATOMIC);
1010 #else
1011                         skb = dev_alloc_skb(pkt_len + 2);
1012 #endif
1013                         if (skb == NULL) {
1014                                 printk("%s: Memory squeeze, deferring packet.\n",
1015                                            dev->name);
1016                                 /* Check that at least two ring entries are free.
1017                                    If not, free one and mark stats->rx_dropped++. */
1018                                 for (i=0; i < RX_RING_SIZE; i++)
1019                                         if (lp->rx_ring[(entry+i) % RX_RING_SIZE].status < 0)
1020                                                 break;
1021 
1022                                 if (i > RX_RING_SIZE -2) {
1023                                         lp->stats.rx_dropped++;
1024                                         lp->rx_ring[entry].status = TRING_OWN;
1025                                         lp->cur_rx++;
1026                                 }
1027                                 break;
1028                         }
1029                         skb->dev = dev;
1030 #if LINUX_VERSION_CODE < 0x10300
1031                         skb->len = pkt_len;
1032                         memcpy(skb->data, bus_to_virt(lp->rx_ring[entry].buffer1),
1033                                    pkt_len);
1034 #else
1035                         skb_reserve(skb, 2);
1036                         memcpy(skb_put(skb, pkt_len),
1037                                    bus_to_virt(lp->rx_ring[entry].buffer1), pkt_len);
1038                         /* Needed for 1.3.x */
1039                         skb->protocol = eth_type_trans(skb,dev);
1040 #endif
1041                         netif_rx(skb);
1042                         lp->stats.rx_packets++;
1043                 }
1044 
1045                 lp->rx_ring[entry].status = TRING_OWN;
1046                 entry = (++lp->cur_rx) % RX_RING_SIZE;
1047         }
1048         return(0);
1049 }
1050 
1051 static int
1052 tulip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1053 {
1054         int ioaddr = dev->base_addr;
1055         struct tulip_private *tp = (struct tulip_private *)dev->priv;
1056 
1057         dev->start = 0;
1058         dev->tbusy = 1;
1059 
1060         /* Disable interrupts by clearing the interrupt mask. */
1061         tio_write(TINTR_DISABLE, CSR7);
1062         /* Stop the chip's Tx and Rx processes. */
1063         tio_write(tio_read(CSR6) & ~(TCMOD_TRxSTART), CSR6);
1064         /* Leave the card in 10baseT state. */
1065         tio_write(TSIAC_CONFIG, CSR13);
1066 
1067         tp->stats.rx_missed_errors += tio_read(CSR8) & 0xffff;
1068 
1069         tio_write(0, CSR13);
1070 /*      tio_write(0, CSR8);     wake up chip ? */
1071 
1072 #if LINUX_VERSION_CODE < 0x10346
1073         free_irq(dev->irq);
1074 #else
1075         free_irq(dev->irq, dev);
1076 #endif
1077         irq2dev_map[dev->irq] = 0;
1078 
1079         MOD_DEC_USE_COUNT;
1080         return(0);
1081 }
1082 
1083 static struct enet_statistics *
1084 tulip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1085 {
1086         struct tulip_private *tp = (struct tulip_private *)dev->priv;
1087         short ioaddr = dev->base_addr;
1088 
1089         tp->stats.rx_missed_errors += tio_read(CSR8) & 0xffff;
1090 
1091         return(&tp->stats);
1092 }
1093 
1094 /*
1095  *      Set or clear the multicast filter for this adaptor.
1096  */
1097 
1098 #if LINUX_VERSION_CODE < 0x10300
1099 static void set_multicast_list(struct device *dev, int num_addrs,
     /* [previous][next][first][last][top][bottom][index][help] */
1100                                                            void *addrs)
1101 #else
1102 static void set_multicast_list(struct device *dev)
1103 #endif
1104 {
1105         short ioaddr = dev->base_addr;
1106         int csr6 = tio_read(CSR6) & ~(TCMOD_MODEMASK|TCMOD_FILTERMASK);
1107 
1108         if (dev->flags&IFF_PROMISC) 
1109         {                       /* Set promiscuous. why ALLMULTI ? */
1110                 tio_write(csr6 | TCMOD_PROMISC | TCMOD_ALLMCAST, CSR6);
1111                 /* Log any net taps. */
1112                 printk("%s: Promiscuous mode enabled.\n", dev->name);
1113         }
1114         else if (dev->mc_count > 15 || (dev->flags&IFF_ALLMULTI)) 
1115         {
1116                 /* Too many to filter perfectly -- accept all multicasts. */
1117                 tio_write(csr6 | TCMOD_ALLMCAST, CSR6);
1118         }
1119         else
1120         {
1121                 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1122                 struct dev_mc_list *dmi=dev->mc_list;
1123                 int *setup_frm = tp->setup_frame;
1124                 unsigned short *eaddrs;
1125                 int i;
1126 
1127                 /* We have <= 15 addresses that we can use the wonderful
1128                    16 address perfect filtering of the Tulip.  Note that only
1129                    the low shortword of setup_frame[] is valid. */
1130                 tio_write(csr6 | 0x0000, CSR6);
1131                 i=0;
1132                 while(dmi) 
1133                 {
1134                         eaddrs=(unsigned short *)dmi->dmi_addr;
1135                         dmi=dmi->next;
1136                         i++;
1137                         *setup_frm++ = *eaddrs++;
1138                         *setup_frm++ = *eaddrs++;
1139                         *setup_frm++ = *eaddrs++;
1140                 }
1141                 /* Fill the rest of the table with our physical address. */
1142                 eaddrs = (unsigned short *)dev->dev_addr;
1143                 do {
1144                         *setup_frm++ = eaddrs[0];
1145                         *setup_frm++ = eaddrs[1];
1146                         *setup_frm++ = eaddrs[2];
1147                 } while (++i < 16);
1148 
1149                 /* Now add this frame to the Tx list. */
1150         }
1151 }
1152 
1153 #if 0
1154 static int
1155 set_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1156 {
1157         int i;
1158         struct sockaddr *sa=(struct sockaddr *)addr;
1159         if (dev->start)
1160                 return -EBUSY;
1161         printk("%s: Setting MAC address to ", dev->name);
1162         for (i = 0; i < ETH_ALEN - 1; i++)
1163                 printk("%2.2x:", dev->dev_addr[i] = sa->sa_data[i]);
1164         printk("%2.2x.\n", dev->dev_addr[i] = sa->sa_data[i]);
1165         return 0;
1166 }
1167 #endif
1168 
1169 static struct device *tulip_alloc(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1170 {
1171         struct tulip_private *tp;
1172         char *buff;
1173 #ifndef MODULE
1174         int alloc_size;
1175 #endif
1176         if (!dev || dev->priv) {
1177                 struct device *olddev = dev;
1178 
1179                 alloc_size = sizeof(struct device)
1180                         + sizeof(struct tulip_private)
1181                         + ETHNAMSIZ;
1182                 alloc_size = ROUND_UP(alloc_size, 8);
1183 
1184                 buff = (char *)kmalloc(alloc_size, GFP_KERNEL);
1185                 dev = (struct device *)buff;
1186                 if (dev == NULL) {
1187                         printk("tulip_alloc: kmalloc failed.\n");
1188                         return(NULL);
1189                 }
1190                 tp = (struct tulip_private *)(buff + sizeof(struct device));
1191                 memset(buff, 0, alloc_size);
1192                 dev->priv = (void *)tp;
1193                 dev->name = (char *)(buff + sizeof(struct device)
1194                                                          + sizeof(struct tulip_private));
1195                 if (olddev) {
1196                         dev->next = olddev->next;
1197                         olddev->next = dev;
1198                 }
1199         } else {
1200                 alloc_size = ROUND_UP(sizeof(struct tulip_private), 8);
1201                 tp = (struct tulip_private *)kmalloc(alloc_size, GFP_KERNEL);
1202                 memset((void *)tp, 0, alloc_size);
1203                 dev->priv = (void *)tp;
1204         }
1205         return(dev);
1206 }
1207 
1208 int
1209 tulip_hwinit(struct device *dev, int ioaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1210                          int irq, int device_id)
1211 {
1212         /* See note below on the Znyx 315 etherarray. */
1213         unsigned char last_phys_addr[6] = {0x00, 'L', 'i', 'n', 'u', 'x'};
1214         char detect_mesg[80], *mesgp=detect_mesg, *card_name=NULL;
1215         struct tulip_private *tp = (struct tulip_private *)dev->priv;
1216         int i;
1217         unsigned short sum, bitsum;
1218 
1219         if (check_region(ioaddr, TULIP_TOTAL_SIZE) != 0) {
1220                 printk("tulip_alloc: region already allocated at %#3x.\n",
1221                            ioaddr);
1222                 return(-1);
1223         }
1224 
1225         mesgp += sprintf(mesgp, "(DEC 21%d4%d Tulip",
1226                                          device_id == PCI_DEVICE_ID_DEC_TULIP_FAST,
1227                                          device_id == PCI_DEVICE_ID_DEC_TULIP_PLUS);
1228 
1229         /* Stop the chip's Tx and Rx processes. */
1230         tio_write(tio_read(CSR6) & ~TCMOD_TRxSTART, CSR6);
1231         /* Clear the missed-packet counter. */
1232         tio_read(CSR8) & 0xffff;
1233 
1234         if (device_id == PCI_DEVICE_ID_DEC_TULIP_PLUS
1235             && (tio_read(CSR9) & 0x8000)) {
1236                 mesgp += sprintf(mesgp, "treat as 21040 ");
1237             device_id = PCI_DEVICE_ID_DEC_TULIP;
1238         }
1239         
1240         /* The station address ROM is read byte serially.  The register must
1241            be polled, waiting for the value to be read bit serially from the
1242            EEPROM.
1243            */
1244         sum = 0;
1245         if (device_id == PCI_DEVICE_ID_DEC_TULIP) {
1246                 tio_write(0, CSR9);
1247             /* Reset the pointer with a dummy write. */
1248             bitsum = 0xff;
1249             for (i = 0; i < 6; i++) {
1250                         int value, boguscnt = 100000;
1251                         do
1252                                 value = tio_read(CSR9);
1253                         while (value < 0  && --boguscnt > 0);
1254                         dev->dev_addr[i] = value;
1255                         sum += value;
1256                         bitsum &= value;
1257             }
1258         } else {
1259             /* Must be a 21140/21041, with a serial EEPROM interface. */
1260             struct eeprom eep;
1261             u_char *addr;
1262 
1263             if (read_eeprom(ioaddr, &eep) < 0) {
1264                         addr = eep.ng_addr;/* broken EEPROM structure */
1265             } else {
1266                         addr = eep.ok_addr;/* DEC EtherWorks */
1267             }
1268             for (i = 0; i < ETH_ALEN; i++) {
1269                         sum += addr[i];
1270                         dev->dev_addr[i] = addr[i];
1271             }
1272         }
1273         /* Make certain the data structures are quadword aligned. */
1274 
1275         mesgp += sprintf(mesgp, ") at %#3x, ", ioaddr);
1276 
1277         /* On the Zynx 315 etherarray boards only the first Tulip has an EEPROM.
1278            The addresses of the subsequent ports are derived from the first. */
1279         if (sum == 0) {
1280                 for (i = 0; i < ETH_ALEN - 1; i++)
1281                         dev->dev_addr[i] = last_phys_addr[i];
1282                 dev->dev_addr[i] = last_phys_addr[i] + 1;
1283         }
1284         for (i = 0; i < ETH_ALEN - 1; i++)
1285                 mesgp += sprintf(mesgp, "%2.2x:",
1286                                                  last_phys_addr[i] = dev->dev_addr[i]);
1287         mesgp += sprintf(mesgp, "%2.2x, IRQ %d\n",
1288                                          last_phys_addr[i] = dev->dev_addr[i], irq);
1289 
1290         card_name = card_type(tp, device_id,
1291                                                   htonl((*(int*)dev->dev_addr) & 0xFFFFFF));
1292 
1293         /* We do a request_region() only to register /proc/ioports info. */
1294         request_region(ioaddr, TULIP_TOTAL_SIZE, tp->signature);
1295 
1296         dev->base_addr = ioaddr;
1297         dev->irq = irq;
1298 
1299 #ifdef TULIP_FULL_DUPLEX
1300         tp->full_duplex = 1;
1301 #endif
1302 
1303         /* The Tulip-specific entries in the device structure. */
1304         dev->open = &tulip_open;
1305         dev->hard_start_xmit = &tulip_start_xmit;
1306         dev->stop = &tulip_close;
1307         dev->get_stats = &tulip_get_stats;
1308         dev->set_multicast_list = &set_multicast_list;
1309 #if 0
1310         dev->set_mac_address = &set_mac_address;
1311 #endif
1312 
1313 #ifdef  MODULE
1314     ether_setup(dev);
1315 #else
1316     init_etherdev(dev, 0);
1317 #endif
1318 
1319         printk("%s: %s %s", dev->name, card_name, detect_mesg);
1320 
1321         /* Reset the xcvr interface and turn on heartbeat. */
1322         tio_write(TSIAC_RESET, CSR13);
1323         tio_write(TSIAC_CONFIG, CSR13);
1324 
1325         return(0);
1326 }
1327 
1328 int tulip_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1329 {
1330         static u_short probed_irqs=0;
1331         u_char pci_bus, pci_device_fn, pci_latency, pci_irq;
1332         u_int pci_ioaddr;
1333         u_short pci_command;
1334         u_long pci_chips[] = {
1335                 PCI_DEVICE_ID_DEC_TULIP,
1336                 PCI_DEVICE_ID_DEC_TULIP_FAST,
1337                 PCI_DEVICE_ID_DEC_TULIP_PLUS,
1338                 PCI_DEVICE_ID_NONE
1339         };
1340         int num=0, cno;
1341         int pci_index;
1342 
1343     if (!pcibios_present()) return(-ENODEV);
1344 
1345         for (pci_index = 0; pci_index < 8; pci_index++) {
1346                 /* Search for the PCI_DEVICE_ID_DEV_TULIP* chips */
1347                 for (cno = 0; pci_chips[cno] != PCI_DEVICE_ID_NONE; cno ++)
1348                         if (pcibios_find_device(PCI_VENDOR_ID_DEC,
1349                                                                         pci_chips[cno],
1350                                                                         pci_index, &pci_bus,
1351                                                                         &pci_device_fn) == 0) {
1352                                 /* get IRQ */
1353                                 pcibios_read_config_byte(pci_bus, pci_device_fn,
1354                                                                                  PCI_INTERRUPT_LINE, &pci_irq);
1355                                 if (probed_irqs & (1 << pci_irq)) continue;
1356                                 /* get IO address */
1357                                 pcibios_read_config_dword(pci_bus, pci_device_fn,
1358                                                                                   PCI_BASE_ADDRESS_0,
1359                                                                                   &pci_ioaddr);
1360                                 /* Remove I/O space marker in bit 0. */
1361                                 pci_ioaddr &= ~3;
1362 #ifdef  MODULE
1363                                 /* compare requested IRQ/IO address */
1364                                 if (dev && dev->irq && dev->base_addr &&
1365                                         (dev->irq != pci_irq
1366                                          || dev->base_addr != pci_ioaddr)) continue;
1367 #else
1368                                 if ((dev = tulip_alloc(dev)) == NULL) break;
1369 #endif
1370                                 if (!probed_irqs) printk(version);
1371                                 probed_irqs |= (1 << pci_irq);
1372                                 
1373                                 /* Get and check the bus-master and latency values. */
1374                                 pcibios_read_config_word(pci_bus, pci_device_fn,
1375                                                                                  PCI_COMMAND, &pci_command);
1376                                 if ( ! (pci_command & PCI_COMMAND_MASTER)) {
1377                                         printk("  PCI Master Bit has not been set!"
1378                                                    " Setting...\n");
1379                                         pci_command |= PCI_COMMAND_MASTER;
1380                                         pcibios_write_config_word(pci_bus, pci_device_fn,
1381                                                                                           PCI_COMMAND, pci_command);
1382                                 }
1383                                 pcibios_read_config_byte(pci_bus, pci_device_fn,
1384                                                                                  PCI_LATENCY_TIMER,
1385                                                                                  &pci_latency);
1386                                 if (pci_latency < 10) {
1387                                         printk("  PCI latency timer (CFLT) is"
1388                                                    " unreasonably low at %d."
1389                                                    "  Setting to 100 clocks.\n", pci_latency);
1390                                         pcibios_write_config_byte(pci_bus, pci_device_fn,
1391                                                                                           PCI_LATENCY_TIMER, 100);
1392                                 }
1393                                 if (tulip_hwinit(dev, pci_ioaddr, pci_irq,
1394                                                                  pci_chips[cno]) < 0) continue;
1395                                 num ++;
1396 #if defined(MODULE) || defined(TULIP_ONLY_ONE)
1397                                 return(0);
1398 #endif
1399                 }
1400         }
1401         return(num > 0 ? 0: -ENODEV);
1402 }
1403 
1404 #ifdef MODULE
1405 static int io = 0xfc00;
1406 static int irq = 9;
1407 
1408 static struct device *mod_dev;
1409 
1410 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1411 {
1412         if ((mod_dev = tulip_alloc(0)) == NULL) return(-EIO);
1413 
1414         mod_dev->base_addr = io;
1415         mod_dev->irq = irq;
1416         mod_dev->init = &tulip_probe;
1417 
1418         if (register_netdev(mod_dev)) {
1419                 printk("tulip: register_netdev() returned non-zero.\n");
1420                 kfree_s(mod_dev, alloc_size);
1421                 return -EIO;
1422         }
1423         return(0);
1424 }
1425 
1426 void
1427 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1428 {
1429         release_region(mod_dev->base_addr, TULIP_TOTAL_SIZE);
1430         unregister_netdev(mod_dev);
1431         kfree_s(mod_dev, alloc_size);
1432 }
1433 
1434 #endif /* MODULE */
1435 
1436 
1437 /*
1438  * Local variables:
1439  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c tulip.c"
1440  *  c-indent-level: 4
1441  *  tab-width: 4
1442  * End:
1443  */

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