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

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