OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [net/] [tulip.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/* tulip.c: A DEC 21040-family ethernet driver for Linux. */
2
/*
3
        Written 1994-1998 by Donald Becker.
4
 
5
        This software may be used and distributed according to the terms
6
        of the GNU Public License, incorporated herein by reference.
7
 
8
        This driver is for the Digital "Tulip" Ethernet adapter interface.
9
        It should work with most DEC 21*4*-based chips/ethercards, as well as
10
        with work-alike chips from Lite-On (PNIC) and Macronix (MXIC) and ASIX.
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
        Support and updates available at
17
        http://cesdis.gsfc.nasa.gov/linux/drivers/tulip.html
18
*/
19
 
20
#define SMP_CHECK
21
static const char version[] = "tulip.c:v0.90 10/20/98 becker@cesdis.gsfc.nasa.gov\n";
22
 
23
/* A few user-configurable values. */
24
 
25
/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
26
static int max_interrupt_work = 25;
27
 
28
#define MAX_UNITS 8
29
/* Used to pass the full-duplex flag, etc. */
30
static int full_duplex[MAX_UNITS] = {0, };
31
static int options[MAX_UNITS] = {0, };
32
static int mtu[MAX_UNITS] = {0, };                       /* Jumbo MTU for interfaces. */
33
 
34
/*  The possible media types that can be set in options[] are: */
35
static const char * const medianame[] = {
36
        "10baseT", "10base2", "AUI", "100baseTx",
37
        "10baseT-FD", "100baseTx-FD", "100baseT4", "100baseFx",
38
        "100baseFx-FD", "MII 10baseT", "MII 10baseT-FD", "MII",
39
        "10baseT(forced)", "MII 100baseTx", "MII 100baseTx-FD", "MII 100baseT4",
40
};
41
 
42
/* Set if the PCI BIOS detects the chips on a multiport board backwards. */
43
#ifdef REVERSE_PROBE_ORDER
44
static int reverse_probe = 1;
45
#else
46
static int reverse_probe = 0;
47
#endif
48
 
49
/* Keep the ring sizes a power of two for efficiency.
50
   Making the Tx ring too large decreases the effectiveness of channel
51
   bonding and packet priority.
52
   There are no ill effects from too-large receive rings. */
53
#define TX_RING_SIZE    16
54
#define RX_RING_SIZE    32
55
 
56
/* Set the copy breakpoint for the copy-only-tiny-buffer Rx structure. */
57
#ifdef __alpha__
58
static int rx_copybreak = 1518;
59
#else
60
static int rx_copybreak = 100;
61
#endif
62
 
63
/* Operational parameters that usually are not changed. */
64
/* Time in jiffies before concluding the transmitter is hung. */
65
#define TX_TIMEOUT  (4*HZ)
66
#define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
67
/* This is a mysterious value that can be written to CSR11 in the 21040 (only)
68
   to support a pre-NWay full-duplex signaling mechanism using short frames.
69
   No one knows what it should be, but if left at its default value some
70
   10base2(!) packets trigger a full-duplex-request interrupt. */
71
#define FULL_DUPLEX_MAGIC       0x6969
72
 
73
#include <linux/config.h>
74
#include <linux/version.h>
75
#ifdef MODULE
76
#ifdef MODVERSIONS
77
#include <linux/modversions.h>
78
#endif
79
#include <linux/module.h>
80
#else
81
#define MOD_INC_USE_COUNT
82
#define MOD_DEC_USE_COUNT
83
#endif
84
 
85
#include <linux/kernel.h>
86
#include <linux/sched.h>
87
#include <linux/string.h>
88
#include <linux/timer.h>
89
#include <linux/errno.h>
90
#include <linux/ioport.h>
91
#include <linux/malloc.h>
92
#include <linux/interrupt.h>
93
#include <linux/pci.h>
94
#include <linux/netdevice.h>
95
#include <linux/etherdevice.h>
96
#include <linux/skbuff.h>
97
#include <asm/processor.h>              /* Processor type for cache alignment. */
98
#include <asm/bitops.h>
99
#include <asm/io.h>
100
 
101
/* Kernel compatibility defines, some common to David Hind's PCMCIA package.
102
   This is only in the support-all-kernels source code. */
103
 
104
#if defined(MODULE) && LINUX_VERSION_CODE > 0x20115
105
MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");
106
MODULE_DESCRIPTION("Digital 21*4* Tulip ethernet driver");
107
MODULE_PARM(debug, "i");
108
MODULE_PARM(max_interrupt_work, "i");
109
MODULE_PARM(reverse_probe, "i");
110
MODULE_PARM(rx_copybreak, "i");
111
MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
112
MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
113
#endif
114
 
115
#define RUN_AT(x) (jiffies + (x))
116
 
117
#if (LINUX_VERSION_CODE >= 0x20100)
118
char kernel_version[] = UTS_RELEASE;
119
#endif
120
 
121
#if LINUX_VERSION_CODE < 0x20123
122
#define hard_smp_processor_id() smp_processor_id()
123
#define test_and_set_bit(val, addr) set_bit(val, addr)
124
#endif
125
#if LINUX_VERSION_CODE <= 0x20139
126
#define net_device_stats enet_statistics
127
#else
128
#define NETSTATS_VER2
129
#endif
130
#if LINUX_VERSION_CODE < 0x20155  ||  defined(CARDBUS)
131
/* Grrrr, the PCI code changed, but did not consider CardBus... */
132
#include <linux/bios32.h>
133
#define PCI_SUPPORT_VER1
134
#else
135
#define PCI_SUPPORT_VER2
136
#endif
137
#if LINUX_VERSION_CODE < 0x20159
138
#define dev_free_skb(skb) dev_kfree_skb(skb, FREE_WRITE);
139
#else
140
#define dev_free_skb(skb) dev_kfree_skb(skb);
141
#endif
142
 
143
#define tulip_debug debug
144
#ifdef TULIP_DEBUG
145
static int tulip_debug = TULIP_DEBUG;
146
#else
147
static int tulip_debug = 1;
148
#endif
149
 
150
/*
151
                                Theory of Operation
152
 
153
I. Board Compatibility
154
 
155
This device driver is designed for the DECchip "Tulip", Digital's
156
single-chip ethernet controllers for PCI.  Supported members of the family
157
are the 21040, 21041, 21140, 21140A, 21142, and 21143.  Similar work-alike
158
chips from Lite-On, Macronics, ASIX, Compex and other listed below are also
159
supported.
160
 
161
These chips are used on at least 140 unique PCI board designs.  The great
162
number of chips and board designs supported is the reason for the
163
driver size and complexity.  Almost of the increasing complexity is in the
164
board configuration and media selection code.  There is very little
165
increasing in the operational critical path length.
166
 
167
II. Board-specific settings
168
 
169
PCI bus devices are configured by the system at boot time, so no jumpers
170
need to be set on the board.  The system BIOS preferably should assign the
171
PCI INTA signal to an otherwise unused system IRQ line.
172
 
173
Some boards have EEPROMs tables with default media entry.  The factory default
174
is usually "autoselect".  This should only be overridden when using
175
transceiver connections without link beat e.g. 10base2 or AUI, or (rarely!)
176
for forcing full-duplex when used with old link partners that do not do
177
autonegotiation.
178
 
179
III. Driver operation
180
 
181
IIIa. Ring buffers
182
 
183
The Tulip can use either ring buffers or lists of Tx and Rx descriptors.
184
This driver uses statically allocated rings of Rx and Tx descriptors, set at
185
compile time by RX/TX_RING_SIZE.  This version of the driver allocates skbuffs
186
for the Rx ring buffers at open() time and passes the skb->data field to the
187
Tulip as receive data buffers.  When an incoming frame is less than
188
RX_COPYBREAK bytes long, a fresh skbuff is allocated and the frame is
189
copied to the new skbuff.  When the incoming frame is larger, the skbuff is
190
passed directly up the protocol stack and replaced by a newly allocated
191
skbuff.
192
 
193
The RX_COPYBREAK value is chosen to trade-off the memory wasted by
194
using a full-sized skbuff for small frames vs. the copying costs of larger
195
frames.  For small frames the copying cost is negligible (esp. considering
196
that we are pre-loading the cache with immediately useful header
197
information).  For large frames the copying cost is non-trivial, and the
198
larger copy might flush the cache of useful data.  A subtle aspect of this
199
choice is that the Tulip only receives into longword aligned buffers, thus
200
the IP header at offset 14 isn't longword aligned for further processing.
201
Copied frames are put into the new skbuff at an offset of "+2", thus copying
202
has the beneficial effect of aligning the IP header and preloading the
203
cache.
204
 
205
IIIC. Synchronization
206
The driver runs as two independent, single-threaded flows of control.  One
207
is the send-packet routine, which enforces single-threaded use by the
208
dev->tbusy flag.  The other thread is the interrupt handler, which is single
209
threaded by the hardware and other software.
210
 
211
The send packet thread has partial control over the Tx ring and 'dev->tbusy'
212
flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
213
queue slot is empty, it clears the tbusy flag when finished otherwise it sets
214
the 'tp->tx_full' flag.
215
 
216
The interrupt handler has exclusive control over the Rx ring and records stats
217
from the Tx ring.  (The Tx-done interrupt can't be selectively turned off, so
218
we can't avoid the interrupt overhead by having the Tx routine reap the Tx
219
stats.)  After reaping the stats, it marks the queue entry as empty by setting
220
the 'base' to zero.      Iff the 'tp->tx_full' flag is set, it clears both the
221
tx_full and tbusy flags.
222
 
223
IV. Notes
224
 
225
Thanks to Duke Kamstra of SMC for long ago providing an EtherPower board.
226
Greg LaPolla at Linksys provided PNIC and other Linksys boards.
227
Znyx provided a four-port card for testing.
228
 
229
IVb. References
230
 
231
http://cesdis.gsfc.nasa.gov/linux/misc/NWay.html
232
http://www.digital.com  (search for current 21*4* datasheets and "21X4 SROM")
233
http://www.national.com/pf/DP/DP83840.html
234
 
235
IVc. Errata
236
 
237
The old DEC databooks were light on details.
238
The 21040 databook claims that CSR13, CSR14, and CSR15 should each be the last
239
register of the set CSR12-15 written.  Hmmm, now how is that possible?
240
 
241
The DEC SROM format is very badly designed not precisely defined, leading to
242
part of the media selection junkheap below.  Some boards do not have EEPROM
243
media tables and need to be patched up.  Worse, other boards use the DEC
244
design kit media table when it isn't correct for their board.
245
 
246
We cannot use MII interrupts because there is no defined GPIO pin to attach
247
them.
248
 
249
*/
250
 
251
#ifndef PCI_VENDOR_ID_DEC               /* Now defined in linux/pci.h */
252
#define PCI_VENDOR_ID_DEC                       0x1011
253
#endif
254
 
255
static struct device *
256
tulip_probe1(int pci_bus, int pci_devfn, struct device *dev, long ioaddr,
257
                         int irq, int chip_idx, int board_idx);
258
 
259
/* This table drives the PCI probe routines.  It's mostly boilerplate in all
260
   of the drivers, and will likely be provided by some future kernel.
261
   Note the matching code -- the first table entry matchs all 56** cards but
262
   second only the 1234 card.
263
*/
264
enum pci_flags_bit {
265
        PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
266
        PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
267
};
268
#define PCI_ADDR0_IO (PCI_USES_IO|PCI_ADDR0)
269
 
270
struct pci_id_info {
271
        const char *name;
272
        u16     vendor_id, device_id, device_id_mask, flags;
273
        int io_size, min_latency;
274
        struct device *(*probe1)(int pci_bus, int pci_devfn, struct device *dev,
275
                                                         long ioaddr, int irq, int chip_idx, int fnd_cnt);
276
};
277
#ifndef CARDBUS
278
static struct pci_id_info pci_tbl[] = {
279
  { "Digital DC21040 Tulip",
280
        0x1011, 0x0002, 0xffff, PCI_ADDR0_IO, 128, 32, tulip_probe1 },
281
  { "Digital DC21041 Tulip",
282
        0x1011, 0x0014, 0xffff, PCI_ADDR0_IO, 128, 32, tulip_probe1 },
283
  { "Digital DS21140 Tulip",
284
        0x1011, 0x0009, 0xffff, PCI_ADDR0_IO, 128, 32, tulip_probe1 },
285
  { "Digital DS21143 Tulip",
286
        0x1011, 0x0019, 0xffff, PCI_ADDR0_IO, 128, 32, tulip_probe1 },
287
  { "Lite-On 82c168 PNIC",
288
        0x11AD, 0x0002, 0xffff, PCI_ADDR0_IO, 256, 32, tulip_probe1 },
289
  { "Macronix 98713 PMAC",
290
        0x10d9, 0x0512, 0xffff, PCI_ADDR0_IO, 256, 32, tulip_probe1 },
291
  { "Macronix 98715 PMAC",
292
        0x10d9, 0x0531, 0xffff, PCI_ADDR0_IO, 256, 32, tulip_probe1 },
293
  { "Macronix 98725 PMAC",
294
        0x10d9, 0x0531, 0xffff, PCI_ADDR0_IO, 256, 32, tulip_probe1 },
295
  { "ASIX AX88140",
296
        0x125B, 0x1400, 0xffff, PCI_ADDR0_IO, 128, 32, tulip_probe1 },
297
  {0},
298
};
299
#endif CARD_BUS
300
 
301
/* This table use during operation for capabilities and media timer. */
302
 
303
static void tulip_timer(unsigned long data);
304
static void t21142_timer(unsigned long data);
305
static void mxic_timer(unsigned long data);
306
static void pnic_timer(unsigned long data);
307
 
308
enum tbl_flag {
309
        HAS_MII=1, HAS_MEDIA_TABLE=2, CSR12_IN_SROM=4, ALWAYS_CHECK_MII=8,
310
        HAS_ACPI=0x10,
311
};
312
static struct tulip_chip_table {
313
        char *chip_name;
314
        int io_size;
315
        int valid_intrs;                        /* CSR7 interrupt enable settings */
316
        int flags;
317
        void (*media_timer)(unsigned long data);
318
} tulip_tbl[] = {
319
  { "Digital DC21040 Tulip", 128, 0x0001ebef, 0, tulip_timer },
320
  { "Digital DC21041 Tulip", 128, 0x0001ebef, HAS_MEDIA_TABLE, tulip_timer },
321
  { "Digital DS21140 Tulip", 128, 0x0001ebef,
322
        HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, tulip_timer },
323
  { "Digital DS21143 Tulip", 128, 0x0801fbff,
324
        HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII | HAS_ACPI, t21142_timer },
325
  { "Lite-On 82c168 PNIC", 256, 0x0001ebef,
326
        HAS_MII, pnic_timer },
327
  { "Macronix 98713 PMAC", 128, 0x0001ebef,
328
        HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, mxic_timer },
329
  { "Macronix 98715 PMAC", 256, 0x0001ebef,
330
        HAS_MEDIA_TABLE, mxic_timer },
331
  { "Macronix 98725 PMAC", 256, 0x0001ebef,
332
        HAS_MEDIA_TABLE, mxic_timer },
333
  { "ASIX AX88140", 128, 0x0001fbff,
334
        HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, tulip_timer },
335
  {0},
336
};
337
/* This matches the table above. */
338
enum chips { DC21040=0, DC21041=1, DC21140=2, DC21142=3, DC21143=3,
339
                         LC82C168, MX98713, MX98715, MX98725};
340
 
341
/* A full-duplex map for media types. */
342
enum MediaIs {MediaIsFD = 1, MediaAlwaysFD=2, MediaIsMII=4, MediaIsFx=8,
343
                  MediaIs100=16};
344
static const char media_cap[] =
345
{0,0,0,16,  3,19,16,24,  27,4,7,5, 0,20,23,20 };
346
/* 21041 transceiver register settings: 10-T, 10-2, AUI, 10-T, 10T-FD*/
347
static u16 t21041_csr13[] = { 0xEF05, 0xEF09, 0xEF09, 0xEF01, 0xEF09, };
348
static u16 t21041_csr14[] = { 0x7F3F, 0xF7FD, 0xF7FD, 0x7F3F, 0x7F3D, };
349
static u16 t21041_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
350
 
351
static u16 t21142_csr13[] = { 0x0001, 0x0009, 0x0009, 0x0000, 0x0001, };
352
static u16 t21142_csr14[] = { 0xFFFF, 0x0705, 0x0705, 0x0000, 0x7F3D, };
353
static u16 t21142_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
354
 
355
/* Offsets to the Command and Status Registers, "CSRs".  All accesses
356
   must be longword instructions and quadword aligned. */
357
enum tulip_offsets {
358
        CSR0=0,    CSR1=0x08, CSR2=0x10, CSR3=0x18, CSR4=0x20, CSR5=0x28,
359
        CSR6=0x30, CSR7=0x38, CSR8=0x40, CSR9=0x48, CSR10=0x50, CSR11=0x58,
360
        CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78 };
361
 
362
/* The bits in the CSR5 status registers, mostly interrupt sources. */
363
enum status_bits {
364
        TimerInt=0x800, TPLnkFail=0x1000, TPLnkPass=0x10,
365
        NormalIntr=0x10000, AbnormalIntr=0x8000,
366
        RxJabber=0x200, RxDied=0x100, RxNoBuf=0x80, RxIntr=0x40,
367
        TxFIFOUnderflow=0x20, TxJabber=0x08, TxNoBuf=0x04, TxDied=0x02, TxIntr=0x01,
368
};
369
 
370
/* The Tulip Rx and Tx buffer descriptors. */
371
struct tulip_rx_desc {
372
        s32 status;
373
        s32 length;
374
        u32 buffer1, buffer2;
375
};
376
 
377
struct tulip_tx_desc {
378
        s32 status;
379
        s32 length;
380
        u32 buffer1, buffer2;                           /* We use only buffer 1.  */
381
};
382
 
383
/* Ring-wrap flag in length field, use for last ring entry.
384
        0x01000000 means chain on buffer2 address,
385
        0x02000000 means use the ring start address in CSR2/3.
386
   Note: Some work-alike chips do not function correctly in chained mode.
387
*/
388
#define DESC_RING_WRAP 0x02000000
389
 
390
struct medialeaf {
391
        u8 type;
392
        u8 media;
393
        unsigned char *leafdata;
394
};
395
 
396
struct mediatable {
397
        u16 defaultmedia;
398
        u8 leafcount, csr12dir;                         /* General purpose pin directions. */
399
        unsigned has_mii:1, has_nonmii:1;
400
        struct medialeaf mleaf[0];
401
};
402
 
403
struct mediainfo {
404
        struct mediainfo *next;
405
        int info_type;
406
        int index;
407
        unsigned char *info;
408
};
409
 
410
struct tulip_private {
411
        char devname[8];                        /* Used only for kernel debugging. */
412
        const char *product_name;
413
        struct device *next_module;
414
        struct tulip_rx_desc rx_ring[RX_RING_SIZE];
415
        struct tulip_tx_desc tx_ring[TX_RING_SIZE];
416
        /* The saved address of a sent-in-place packet/buffer, for skfree(). */
417
        struct sk_buff* tx_skbuff[TX_RING_SIZE];
418
        /* The addresses of receive-in-place skbuffs. */
419
        struct sk_buff* rx_skbuff[RX_RING_SIZE];
420
        char *rx_buffs;                         /* Address of temporary Rx buffers. */
421
        u32 setup_frame[48];            /* Pseudo-Tx frame to init address table. */
422
        int chip_id;
423
        int revision;
424
        struct net_device_stats stats;
425
        struct timer_list timer;        /* Media selection timer. */
426
        int interrupt;                          /* In-interrupt flag. */
427
#ifdef SMP_CHECK
428
        int smp_proc_id;                        /* Which processor in IRQ handler. */
429
#endif
430
        unsigned int cur_rx, cur_tx;            /* The next free ring entry */
431
        unsigned int dirty_rx, dirty_tx;        /* The ring entries to be free()ed. */
432
        unsigned int tx_full:1;                         /* The Tx queue is full. */
433
        unsigned int full_duplex:1;                     /* Full-duplex operation requested. */
434
        unsigned int full_duplex_lock:1;
435
        unsigned int fake_addr:1;                       /* Multiport board faked address. */
436
        unsigned int default_port:4;            /* Last dev->if_port value. */
437
        unsigned int media2:4;                          /* Secondary monitored media port. */
438
        unsigned int medialock:1;                       /* Don't sense media type. */
439
        unsigned int mediasense:1;                      /* Media sensing in progress. */
440
        unsigned int csr6;                                      /* Current CSR6 control settings. */
441
        unsigned char eeprom[128];                      /* Serial EEPROM contents. */
442
        u16 to_advertise;                                       /* NWay capabilities advertised.  */
443
        u16 advertising[4];
444
        signed char phys[4], mii_cnt;           /* MII device addresses. */
445
        struct mediatable *mtable;
446
        int cur_index;                                          /* Current media index. */
447
        int saved_if_port;
448
        unsigned char pci_bus, pci_devfn;
449
        int pad0, pad1;                                         /* Used for 8-byte alignment */
450
};
451
 
452
static void parse_eeprom(struct device *dev);
453
static int read_eeprom(long ioaddr, int location);
454
static int mdio_read(struct device *dev, int phy_id, int location);
455
static void mdio_write(struct device *dev, int phy_id, int location, int value);
456
static void select_media(struct device *dev, int startup);
457
static int tulip_open(struct device *dev);
458
static void tulip_timer(unsigned long data);
459
static void tulip_tx_timeout(struct device *dev);
460
static void tulip_init_ring(struct device *dev);
461
static int tulip_start_xmit(struct sk_buff *skb, struct device *dev);
462
static int tulip_rx(struct device *dev);
463
static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
464
static int tulip_close(struct device *dev);
465
static struct net_device_stats *tulip_get_stats(struct device *dev);
466
#ifdef HAVE_PRIVATE_IOCTL
467
static int private_ioctl(struct device *dev, struct ifreq *rq, int cmd);
468
#endif
469
static void set_rx_mode(struct device *dev);
470
 
471
 
472
 
473
/* A list of all installed Tulip devices. */
474
static struct device *root_tulip_dev = NULL;
475
 
476
#ifndef CARDBUS
477
int tulip_probe(struct device *dev)
478
{
479
        int cards_found = 0;
480
        int pci_index = 0;
481
        unsigned char pci_bus, pci_device_fn;
482
 
483
        if ( ! pcibios_present())
484
                return -ENODEV;
485
 
486
        for (;pci_index < 0xff; pci_index++) {
487
                u16 vendor, device, pci_command, new_command;
488
                int chip_idx;
489
                int irq;
490
                long ioaddr;
491
 
492
                if (pcibios_find_class
493
                        (PCI_CLASS_NETWORK_ETHERNET << 8,
494
                         reverse_probe ? 0xfe - pci_index : pci_index,
495
                         &pci_bus, &pci_device_fn) != PCIBIOS_SUCCESSFUL)
496
                        if (reverse_probe)
497
                                continue;
498
                        else
499
                                break;
500
                pcibios_read_config_word(pci_bus, pci_device_fn,
501
                                                                 PCI_VENDOR_ID, &vendor);
502
                pcibios_read_config_word(pci_bus, pci_device_fn,
503
                                                                 PCI_DEVICE_ID, &device);
504
 
505
                for (chip_idx = 0; pci_tbl[chip_idx].vendor_id; chip_idx++)
506
                        if (vendor == pci_tbl[chip_idx].vendor_id
507
                                && (device & pci_tbl[chip_idx].device_id_mask) ==
508
                                pci_tbl[chip_idx].device_id)
509
                                break;
510
                if (pci_tbl[chip_idx].vendor_id == 0)
511
                        continue;
512
 
513
                {
514
#if defined(PCI_SUPPORT_VER2)
515
                        struct pci_dev *pdev = pci_find_slot(pci_bus, pci_device_fn);
516
                        ioaddr = pdev->base_address[0] & ~3;
517
                        irq = pdev->irq;
518
#else
519
                        u32 pci_ioaddr;
520
                        u8 pci_irq_line;
521
                        pcibios_read_config_dword(pci_bus, pci_device_fn,
522
                                                                          PCI_BASE_ADDRESS_0, &pci_ioaddr);
523
                        pcibios_read_config_byte(pci_bus, pci_device_fn,
524
                                                                         PCI_INTERRUPT_LINE, &pci_irq_line);
525
                        ioaddr = pci_ioaddr & ~3;
526
                        irq = pci_irq_line;
527
#endif
528
                }
529
 
530
                if (debug > 2)
531
                        printk(KERN_INFO "Found %s at PCI I/O address %#lx.\n",
532
                                   pci_tbl[chip_idx].name, ioaddr);
533
 
534
                if (check_region(ioaddr, pci_tbl[chip_idx].io_size))
535
                        continue;
536
 
537
                pcibios_read_config_word(pci_bus, pci_device_fn,
538
                                                                 PCI_COMMAND, &pci_command);
539
                new_command = pci_command | PCI_COMMAND_MASTER|PCI_COMMAND_IO;
540
                if (pci_command != new_command) {
541
                        printk(KERN_INFO "  The PCI BIOS has not enabled the"
542
                                   " device at %d/%d!  Updating PCI command %4.4x->%4.4x.\n",
543
                                   pci_bus, pci_device_fn, pci_command, new_command);
544
                        pcibios_write_config_word(pci_bus, pci_device_fn,
545
                                                                          PCI_COMMAND, new_command);
546
                }
547
 
548
                dev = pci_tbl[chip_idx].probe1(pci_bus, pci_device_fn, dev, ioaddr,
549
                                                                           irq, chip_idx, cards_found);
550
 
551
                /* Get and check the bus-master and latency values. */
552
                if (dev) {
553
                        u8 pci_latency;
554
                        pcibios_read_config_byte(pci_bus, pci_device_fn,
555
                                                                         PCI_LATENCY_TIMER, &pci_latency);
556
                        if (pci_latency < 10) {
557
                                printk(KERN_INFO "  PCI latency timer (CFLT) is "
558
                                           "unreasonably low at %d.  Setting to 64 clocks.\n",
559
                                           pci_latency);
560
                                pcibios_write_config_byte(pci_bus, pci_device_fn,
561
                                                                                  PCI_LATENCY_TIMER, 64);
562
                        }
563
                }
564
                dev = 0;
565
                cards_found++;
566
        }
567
 
568
        return cards_found ? 0 : -ENODEV;
569
}
570
#endif  /* not CARDBUS */
571
 
572
static struct device *tulip_probe1(int pci_bus, int pci_devfn,
573
                                                                   struct device *dev, long ioaddr, int irq,
574
                                                                   int chip_idx, int board_idx)
575
{
576
        static int did_version = 0;                      /* Already printed version info. */
577
        struct tulip_private *tp;
578
        /* See note below on the multiport cards. */
579
        static unsigned char last_phys_addr[6] = {0x00, 'L', 'i', 'n', 'u', 'x'};
580
        static int last_irq = 0;
581
        static int multiport_cnt = 0;            /* For four-port boards w/one EEPROM */
582
        int i;
583
        unsigned short sum;
584
 
585
        if (tulip_debug > 0  &&  did_version++ == 0)
586
                printk(KERN_INFO "%s", version);
587
 
588
        dev = init_etherdev(dev, 0);
589
 
590
        /* Bring the 21143 out of sleep mode.
591
           Caution: Snooze mode does not work with some boards! */
592
        if (tulip_tbl[chip_idx].flags & HAS_ACPI)
593
                pcibios_write_config_dword(pci_bus, pci_devfn, 0x40, 0x00000000);
594
 
595
        printk(KERN_INFO "%s: %s at %#3lx,",
596
                   dev->name, tulip_tbl[chip_idx].chip_name, ioaddr);
597
 
598
        /* Stop the chip's Tx and Rx processes. */
599
        outl(inl(ioaddr + CSR6) & ~0x2002, ioaddr + CSR6);
600
        /* Clear the missed-packet counter. */
601
        (volatile int)inl(ioaddr + CSR8);
602
 
603
        if (chip_idx == DC21041) {
604
                if (inl(ioaddr + CSR9) & 0x8000) {
605
                        printk(" 21040 compatible mode,");
606
                        chip_idx = DC21040;
607
                } else {
608
                        printk(" 21041 mode,");
609
                }
610
        }
611
 
612
        /* The station address ROM is read byte serially.  The register must
613
           be polled, waiting for the value to be read bit serially from the
614
           EEPROM.
615
           */
616
        sum = 0;
617
        if (chip_idx == DC21040) {
618
                outl(0, ioaddr + CSR9);          /* Reset the pointer with a dummy write. */
619
                for (i = 0; i < 6; i++) {
620
                        int value, boguscnt = 100000;
621
                        do
622
                                value = inl(ioaddr + CSR9);
623
                        while (value < 0  && --boguscnt > 0);
624
                        dev->dev_addr[i] = value;
625
                        sum += value & 0xff;
626
                }
627
        } else if (chip_idx == LC82C168) {
628
                for (i = 0; i < 3; i++) {
629
                        int value, boguscnt = 100000;
630
                        outl(0x600 | i, ioaddr + 0x98);
631
                        do
632
                                value = inl(ioaddr + CSR9);
633
                        while (value < 0  && --boguscnt > 0);
634
                        ((u16*)dev->dev_addr)[i] = value;
635
                        sum += value & 0xffff;
636
                }
637
        } else {        /* Must be a new chip, with a serial EEPROM interface. */
638
                /* We read the whole EEPROM, and sort it out later.  DEC has a
639
                   specification _Digital Semiconductor 21X4 Serial ROM Format_
640
                   but early vendor boards just put the address in the first six
641
                   EEPROM locations. */
642
                unsigned char ee_data[128];
643
                int sa_offset = 0;
644
 
645
                for (i = 0; i < sizeof(ee_data)/2; i++)
646
                        ((u16 *)ee_data)[i] = read_eeprom(ioaddr, i);
647
 
648
                /* Detect the simple EEPROM format by the duplicated station addr. */
649
                for (i = 0; i < 8; i ++)
650
                        if (ee_data[i] != ee_data[16+i])
651
                                sa_offset = 20;
652
                if (ee_data[0] == 0xff  &&  ee_data[1] == 0xff &&  ee_data[2] == 0) {
653
                        sa_offset = 2;          /* Grrr, damn Matrox boards. */
654
                        multiport_cnt = 4;
655
                }
656
                for (i = 0; i < 6; i ++) {
657
                        dev->dev_addr[i] = ee_data[i + sa_offset];
658
                        sum += ee_data[i + sa_offset];
659
                }
660
        }
661
        /* Lite-On boards have the address byte-swapped. */
662
        if (dev->dev_addr[0] == 0xA0  &&  dev->dev_addr[1] == 0x00)
663
                for (i = 0; i < 6; i+=2) {
664
                        char tmp = dev->dev_addr[i];
665
                        dev->dev_addr[i] = dev->dev_addr[i+1];
666
                        dev->dev_addr[i+1] = tmp;
667
                }
668
        /* On the Zynx 315 Etherarray and other multiport boards only the
669
           first Tulip has an EEPROM.
670
           The addresses of the subsequent ports are derived from the first.
671
           Many PCI BIOSes also incorrectly report the IRQ line, so we correct
672
           that here as well. */
673
        if (sum == 0  || sum == 6*0xff) {
674
                printk(" EEPROM not present,");
675
                for (i = 0; i < 5; i++)
676
                        dev->dev_addr[i] = last_phys_addr[i];
677
                dev->dev_addr[i] = last_phys_addr[i] + 1;
678
#if defined(__i386__)           /* This BIOS bug doesn't exist on Alphas. */
679
                irq = last_irq;
680
#endif
681
        }
682
 
683
        for (i = 0; i < 6; i++)
684
                printk(" %2.2x", last_phys_addr[i] = dev->dev_addr[i]);
685
        printk(", IRQ %d.\n", irq);
686
        last_irq = irq;
687
 
688
        /* We do a request_region() only to register /proc/ioports info. */
689
        /* Note that proper size is tulip_tbl[chip_idx].chip_name, but... */
690
        request_region(ioaddr, tulip_tbl[chip_idx].io_size, dev->name);
691
 
692
        dev->base_addr = ioaddr;
693
        dev->irq = irq;
694
 
695
        /* Make certain the data structures are quadword aligned. */
696
        tp = (void *)(((long)kmalloc(sizeof(*tp), GFP_KERNEL | GFP_DMA) + 7) & ~7);
697
        memset(tp, 0, sizeof(*tp));
698
        dev->priv = tp;
699
 
700
        tp->next_module = root_tulip_dev;
701
        root_tulip_dev = dev;
702
 
703
        tp->pci_bus = pci_bus;
704
        tp->pci_devfn = pci_devfn;
705
        tp->chip_id = chip_idx;
706
 
707
#ifdef TULIP_FULL_DUPLEX
708
        tp->full_duplex = 1;
709
        tp->full_duplex_lock = 1;
710
#endif
711
#ifdef TULIP_DEFAULT_MEDIA
712
        tp->default_port = TULIP_DEFAULT_MEDIA;
713
#endif
714
#ifdef TULIP_NO_MEDIA_SWITCH
715
        tp->medialock = 1;
716
#endif
717
 
718
        /* The lower four bits are the media type. */
719
        if (board_idx >= 0  &&  board_idx < MAX_UNITS) {
720
                tp->default_port = options[board_idx] & 15;
721
                if ((options[board_idx] & 0x90) || full_duplex[board_idx] > 0)
722
                        tp->full_duplex = 1;
723
                if (mtu[board_idx] > 0)
724
                        dev->mtu = mtu[board_idx];
725
        }
726
        if (dev->mem_start)
727
                tp->default_port = dev->mem_start;
728
        if (tp->default_port) {
729
                tp->medialock = 1;
730
                if (media_cap[tp->default_port] & MediaAlwaysFD)
731
                        tp->full_duplex = 1;
732
        }
733
        if (tp->full_duplex)
734
                tp->full_duplex_lock = 1;
735
 
736
        /* This is logically part of probe1(), but too complex to write inline. */
737
        if (tulip_tbl[chip_idx].flags & HAS_MEDIA_TABLE)
738
                parse_eeprom(dev);
739
 
740
        if (media_cap[tp->default_port] & MediaIsMII) {
741
                u16 media2advert[] = { 0x20, 0x40, 0x03e0, 0x60, 0x80, 0x100, 0x200 };
742
                tp->to_advertise = media2advert[tp->default_port - 9];
743
        } else
744
                tp->to_advertise = 0x03e1;
745
 
746
        if ((tulip_tbl[chip_idx].flags & ALWAYS_CHECK_MII) ||
747
                (tp->mtable  &&  tp->mtable->has_mii) ||
748
                ( ! tp->mtable  &&  (tulip_tbl[chip_idx].flags & HAS_MII))) {
749
                int phy, phy_idx;
750
                /* Clear undefined bits that confuse the MII interface. */
751
                if (tp->chip_id == DC21142)
752
                        outl(inl(ioaddr+CSR15) & ~(0x800037c0), ioaddr+CSR15);
753
                /* Find the connected MII xcvrs.
754
                   Doing this in open() would allow detecting external xcvrs later,
755
                   but takes much time. */
756
                for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(tp->phys);
757
                         phy++) {
758
                        int mii_status = mdio_read(dev, phy, 1);
759
                        if (mii_status != 0xffff  &&  mii_status != 0x0000) {
760
                                int mii_reg0 = mdio_read(dev, phy, 0);
761
                                int mii_advert = mdio_read(dev, phy, 4);
762
                                int reg4 = ((mii_status>>6) & tp->to_advertise) | 1;
763
                                tp->phys[phy_idx] = phy;
764
                                tp->advertising[phy_idx++] = reg4;
765
                                printk(KERN_INFO "%s:  MII transceiver #%d "
766
                                           "config %4.4x status %4.4x advertising %4.4x.\n",
767
                                           dev->name, phy, mii_reg0, mii_status, mii_advert);
768
                                /* Fixup for DLink with miswired PHY. */
769
                                if (mii_advert != reg4) {
770
                                        printk(KERN_DEBUG "%s:  Advertising %4.4x on PHY %d,"
771
                                                   " previously advertising %4.4x.\n",
772
                                                   dev->name, reg4, phy, mii_advert);
773
                                        mdio_write(dev, phy, 4, reg4);
774
                                }
775
                                /* Enable autonegotiation: some boards default to off. */
776
                                mdio_write(dev, phy, 0, mii_reg0 |
777
                                                   (tp->full_duplex ? 0x1100 : 0x1000) |
778
                                                   (media_cap[tp->default_port]&MediaIs100 ? 0x2000:0));
779
                        }
780
                }
781
                tp->mii_cnt = phy_idx;
782
                if (tp->mtable  &&  tp->mtable->has_mii  &&  phy_idx == 0) {
783
                        printk(KERN_INFO "%s: ***WARNING***: No MII transceiver found!\n",
784
                                   dev->name);
785
                        tp->phys[0] = 1;
786
                }
787
        }
788
 
789
        /* The Tulip-specific entries in the device structure. */
790
        dev->open = &tulip_open;
791
        dev->hard_start_xmit = &tulip_start_xmit;
792
        dev->stop = &tulip_close;
793
        dev->get_stats = &tulip_get_stats;
794
#ifdef HAVE_PRIVATE_IOCTL
795
        dev->do_ioctl = &private_ioctl;
796
#endif
797
#ifdef HAVE_MULTICAST
798
        dev->set_multicast_list = &set_rx_mode;
799
#endif
800
 
801
        /* Reset the xcvr interface and turn on heartbeat. */
802
        switch (chip_idx) {
803
        case DC21041:
804
                outl(0x00000000, ioaddr + CSR13);
805
                outl(0xFFFFFFFF, ioaddr + CSR14);
806
                outl(0x00000008, ioaddr + CSR15); /* Listen on AUI also. */
807
                outl(inl(ioaddr + CSR6) | 0x0200, ioaddr + CSR6);
808
                outl(0x0000EF05, ioaddr + CSR13);
809
                break;
810
        case DC21040:
811
                outl(0x00000000, ioaddr + CSR13);
812
                outl(0x00000004, ioaddr + CSR13);
813
                break;
814
        case DC21140: default:
815
                if (tp->mtable)
816
                        outl(tp->mtable->csr12dir | 0x100, ioaddr + CSR12);
817
                break;
818
        case DC21142:
819
                if (tp->mii_cnt) {
820
                        outl(0x82020000, ioaddr + CSR6);
821
                        outl(0x0000, ioaddr + CSR13);
822
                        outl(0x0000, ioaddr + CSR14);
823
                        outl(0x820E0000, ioaddr + CSR6);
824
                } else {
825
                        outl(0x82420200, ioaddr + CSR6);
826
                        outl(0x0001, ioaddr + CSR13);
827
                        outl(0x0003FFFF, ioaddr + CSR14);
828
                        outl(0x0008, ioaddr + CSR15);
829
                        outl(0x0001, ioaddr + CSR13);
830
                        outl(0x1301, ioaddr + CSR12); /* Start NWay. */
831
                }
832
                break;
833
        case LC82C168:
834
                if ( ! tp->mii_cnt) {
835
                        outl(0x00420000, ioaddr + CSR6);
836
                        outl(0x30, ioaddr + CSR12);
837
                        outl(0x0001F078, ioaddr + 0xB8);
838
                        outl(0x0201F078, ioaddr + 0xB8); /* Turn on autonegotiation. */
839
                }
840
                break;
841
        case MX98713: case MX98715: case MX98725:
842
                outl(0x00000000, ioaddr + CSR6);
843
                outl(0x000711C0, ioaddr + CSR14); /* Turn on NWay. */
844
                outl(0x00000001, ioaddr + CSR13);
845
                break;
846
        }
847
 
848
        return dev;
849
}
850
 
851
/* Serial EEPROM section. */
852
/* The main routine to parse the very complicated SROM structure.
853
   Search www.digital.com for "21X4 SROM" to get details.
854
   This code is very complex, and will require changes to support
855
   additional cards, so I'll be verbose about what is going on.
856
   */
857
 
858
/* Known cards that have old-style EEPROMs. */
859
static struct fixups {
860
  char *name;
861
  unsigned char addr0, addr1, addr2;
862
  u16 newtable[32];                             /* Max length below. */
863
} eeprom_fixups[] = {
864
  {"Asante", 0, 0, 0x94, {0x1e00, 0x0000, 0x0800, 0x0100, 0x018c,
865
                                                  0x0000, 0x0000, 0xe078, 0x0001, 0x0050, 0x0018 }},
866
  {"SMC9332DST", 0, 0, 0xC0, { 0x1e00, 0x0000, 0x0800, 0x021f,
867
                                                           0x0000, 0x009E, /* 10baseT */
868
                                                           0x0903, 0x006D, /* 100baseTx */ }},
869
  {"Cogent EM100", 0, 0, 0x92, { 0x1e00, 0x0000, 0x0800, 0x033f,
870
                                                                 0x0107, 0x8021, /* 100baseFx */
871
                                                                 0x0108, 0x8021, /* 100baseFx-FD */
872
                                                                 0x0103, 0x006D, /* 100baseTx */ }},
873
  {"Maxtech NX-110", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x0313,
874
                                                           0x1001, 0x009E, /* 10base2, CSR12 0x10*/
875
                                                           0x0000, 0x009E, /* 10baseT */
876
                                                           0x0303, 0x006D, /* 100baseTx, CSR12 0x03 */ }},
877
  {"Accton EN1207", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x031F,
878
                                                        0x1B01, 0x0000, /* 10base2,   CSR12 0x1B */
879
                                                        0x1B03, 0x006D, /* 100baseTx, CSR12 0x1B */
880
                                                        0x0B00, 0x009E, /* 10baseT,   CSR12 0x0B */
881
   }},
882
  {0, 0, 0, 0, {}}};
883
 
884
static const char * block_name[] = {"21140 non-MII", "21140 MII PHY",
885
 "21142 Serial PHY", "21142 MII PHY", "21143 SYM PHY", "21143 reset method"};
886
 
887
#define EEPROM_SIZE 128
888
#if defined(__i386__)
889
#define get_u16(ptr) (*(u16 *)(ptr))
890
#else
891
#define get_u16(ptr) (((u8*)(ptr))[0] + (((u8*)(ptr))[1]<<8))
892
#endif
893
 
894
static void parse_eeprom(struct device *dev)
895
{
896
        /* The last media info list parsed, for multiport boards.  */
897
        static struct mediatable *last_mediatable = NULL;
898
        static unsigned char *last_ee_data = NULL;
899
        static int controller_index = 0;
900
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
901
        long ioaddr = dev->base_addr;
902
        unsigned char *ee_data = tp->eeprom;
903
        int i;
904
 
905
        tp->mtable = 0;
906
        for (i = 0; i < EEPROM_SIZE/2; i++)
907
                ((u16 *)ee_data)[i] = read_eeprom(ioaddr, i);
908
 
909
        /* Detect an old-style (SA only) EEPROM layout:
910
           memcmp(eedata, eedata+16, 8). */
911
        for (i = 0; i < 8; i ++)
912
                if (ee_data[i] != ee_data[16+i])
913
                        break;
914
        if (i >= 8) {
915
                if (ee_data[0] == 0xff) {
916
                        if (last_mediatable) {
917
                                controller_index++;
918
                                printk(KERN_INFO "%s:  Controller %d of multiport board.\n",
919
                                           dev->name, controller_index);
920
                                tp->mtable = last_mediatable;
921
                                ee_data = last_ee_data;
922
                                goto subsequent_board;
923
                        } else
924
                                printk(KERN_INFO "%s:  Missing EEPROM, this interface may "
925
                                           "not work correctly!\n",
926
                           dev->name);
927
                        return;
928
                }
929
          /* Do a fix-up based on the vendor half of the station address prefix. */
930
          for (i = 0; eeprom_fixups[i].name; i++) {
931
                if (dev->dev_addr[0] == eeprom_fixups[i].addr0
932
                        &&  dev->dev_addr[1] == eeprom_fixups[i].addr1
933
                        &&  dev->dev_addr[2] == eeprom_fixups[i].addr2) {
934
                  if (dev->dev_addr[2] == 0xE8  &&  ee_data[0x1a] == 0x55)
935
                          i++;                  /* An Accton EN1207, not an outlaw Maxtech. */
936
                  memcpy(ee_data + 26, eeprom_fixups[i].newtable,
937
                                 sizeof(eeprom_fixups[i].newtable));
938
                  printk(KERN_INFO "%s: Old format EEPROM on '%s' board.  Using"
939
                                 " substitute media control info.\n",
940
                                 dev->name, eeprom_fixups[i].name);
941
                  break;
942
                }
943
          }
944
          if (eeprom_fixups[i].name == NULL) { /* No fixup found. */
945
                printk(KERN_INFO "%s: Old style EEPROM -- no media selection information.\n",
946
                           dev->name);
947
                return;
948
          }
949
        }
950
        if (tulip_debug > 1) {
951
          printk(KERN_DEBUG "read_eeprom:");
952
          for (i = 0; i < 64; i++) {
953
                printk("%s%4.4x", (i & 7) == 0 ? "\n" KERN_DEBUG : " ",
954
                           read_eeprom(ioaddr, i));
955
          }
956
          printk("\n");
957
        }
958
 
959
        controller_index = 0;
960
        if (ee_data[19] > 1) {          /* Multiport board. */
961
                last_ee_data = ee_data;
962
        }
963
subsequent_board:
964
 
965
        if (ee_data[27] == 0) {          /* No valid media table. */
966
        } else if (tp->chip_id == DC21041) {
967
                unsigned char *p = (void *)ee_data + ee_data[27 + controller_index*3];
968
                short media;
969
                int count;
970
 
971
                media = get_u16(p);
972
                p += 2;
973
                count = *p++;
974
 
975
                printk(KERN_INFO "%s:21041 Media information at %d, default media "
976
                           "%4.4x (%s).\n", dev->name, ee_data[27], media,
977
                           media & 0x0800 ? "Autosense" : medianame[media & 15]);
978
                for (i = 0; i < count; i++) {
979
                        unsigned char media_code = *p++;
980
                        u16 csrvals[3];
981
                        int idx;
982
                        for (idx = 0; idx < 3; idx++) {
983
                                csrvals[idx] = get_u16(p);
984
                                p += 2;
985
                        }
986
                        if (media_code & 0x40) {
987
                                printk(KERN_INFO "%s:  21041 media %2.2x (%s),"
988
                                           " csr13 %4.4x csr14 %4.4x csr15 %4.4x.\n",
989
                                           dev->name, media_code & 15, medianame[media_code & 15],
990
                                           csrvals[0], csrvals[1], csrvals[2]);
991
                        } else
992
                                printk(KERN_INFO "%s:  21041 media #%d, %s.\n",
993
                                           dev->name, media_code & 15, medianame[media_code & 15]);
994
                }
995
        } else {
996
                unsigned char *p = (void *)ee_data + ee_data[27];
997
                unsigned char csr12dir = 0;
998
                int count;
999
                struct mediatable *mtable;
1000
                u16 media = get_u16(p);
1001
 
1002
                p += 2;
1003
                if (tulip_tbl[tp->chip_id].flags & CSR12_IN_SROM)
1004
                        csr12dir = *p++;
1005
                count = *p++;
1006
                mtable = (struct mediatable *)
1007
                        kmalloc(sizeof(struct mediatable) + count*sizeof(struct medialeaf),
1008
                                        GFP_KERNEL);
1009
                if (mtable == NULL)
1010
                        return;                         /* Horrible, impossible failure. */
1011
                last_mediatable = tp->mtable = mtable;
1012
                mtable->defaultmedia = media;
1013
                mtable->leafcount = count;
1014
                mtable->csr12dir = csr12dir;
1015
                mtable->has_nonmii = mtable->has_mii = 0;
1016
 
1017
                printk(KERN_INFO "%s:  EEPROM default media type %s.\n", dev->name,
1018
                           media & 0x0800 ? "Autosense" : medianame[media & 15]);
1019
                for (i = 0; i < count; i++) {
1020
                        struct medialeaf *leaf = &mtable->mleaf[i];
1021
 
1022
                        if ((p[0] & 0x80) == 0) { /* 21140 Compact block. */
1023
                                leaf->type = 0;
1024
                                leaf->media = p[0] & 0x3f;
1025
                                leaf->leafdata = p;
1026
                                if ((p[2] & 0x61) == 0x01)      /* Bogus, but Znyx boards do it. */
1027
                                        mtable->has_mii = 1;
1028
                                p += 4;
1029
                        } else {
1030
                                leaf->type = p[1];
1031
                                if (p[1] & 1) {
1032
                                        mtable->has_mii = 1;
1033
                                        leaf->media = 11;
1034
                                } else {
1035
                                        mtable->has_nonmii = 1;
1036
                                        leaf->media = p[2] & 0x0f;
1037
                                }
1038
                                leaf->leafdata = p + 2;
1039
                                p += (p[0] & 0x3f) + 1;
1040
                        }
1041
                        if (tulip_debug > 1  &&  leaf->media == 11) {
1042
                                unsigned char *bp = leaf->leafdata;
1043
                                printk(KERN_INFO "%s:  MII interface PHY %d, setup/reset "
1044
                                           "sequences %d/%d long, capabilities %2.2x %2.2x.\n",
1045
                                           dev->name, bp[0], bp[1], bp[1 + bp[1]*2],
1046
                                           bp[5 + bp[2 + bp[1]*2]*2], bp[4 + bp[2 + bp[1]*2]*2]);
1047
                        }
1048
                        printk(KERN_INFO "%s:  Index #%d - Media %s (#%d) described "
1049
                                   "by a %s (%d) block.\n",
1050
                                   dev->name, i, medianame[leaf->media], leaf->media,
1051
                                   block_name[leaf->type], leaf->type);
1052
                }
1053
        }
1054
}
1055
/* Reading a serial EEPROM is a "bit" grungy, but we work our way through:->.*/
1056
 
1057
/*  EEPROM_Ctrl bits. */
1058
#define EE_SHIFT_CLK    0x02    /* EEPROM shift clock. */
1059
#define EE_CS                   0x01    /* EEPROM chip select. */
1060
#define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
1061
#define EE_WRITE_0              0x01
1062
#define EE_WRITE_1              0x05
1063
#define EE_DATA_READ    0x08    /* EEPROM chip data out. */
1064
#define EE_ENB                  (0x4800 | EE_CS)
1065
 
1066
/* Delay between EEPROM clock transitions.
1067
   Even at 33Mhz current PCI implementations don't overrun the EEPROM clock.
1068
   We add a bus turn-around to insure that this remains true. */
1069
#define eeprom_delay()  inl(ee_addr)
1070
 
1071
/* The EEPROM commands include the alway-set leading bit. */
1072
#define EE_WRITE_CMD    (5 << 6)
1073
#define EE_READ_CMD             (6 << 6)
1074
#define EE_ERASE_CMD    (7 << 6)
1075
 
1076
static int read_eeprom(long ioaddr, int location)
1077
{
1078
        int i;
1079
        unsigned short retval = 0;
1080
        long ee_addr = ioaddr + CSR9;
1081
        int read_cmd = location | EE_READ_CMD;
1082
 
1083
        outl(EE_ENB & ~EE_CS, ee_addr);
1084
        outl(EE_ENB, ee_addr);
1085
 
1086
        /* Shift the read command bits out. */
1087
        for (i = 10; i >= 0; i--) {
1088
                short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1089
                outl(EE_ENB | dataval, ee_addr);
1090
                eeprom_delay();
1091
                outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1092
                eeprom_delay();
1093
        }
1094
        outl(EE_ENB, ee_addr);
1095
 
1096
        for (i = 16; i > 0; i--) {
1097
                outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
1098
                eeprom_delay();
1099
                retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1100
                outl(EE_ENB, ee_addr);
1101
                eeprom_delay();
1102
        }
1103
 
1104
        /* Terminate the EEPROM access. */
1105
        outl(EE_ENB & ~EE_CS, ee_addr);
1106
        return retval;
1107
}
1108
 
1109
/* MII transceiver control section.
1110
   Read and write the MII registers using software-generated serial
1111
   MDIO protocol.  See the MII specifications or DP83840A data sheet
1112
   for details. */
1113
 
1114
/* The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
1115
   met by back-to-back PCI I/O cycles, but we insert a delay to avoid
1116
   "overclocking" issues or future 66Mhz PCI. */
1117
#define mdio_delay() inl(mdio_addr)
1118
 
1119
/* Read and write the MII registers using software-generated serial
1120
   MDIO protocol.  It is just different enough from the EEPROM protocol
1121
   to not share code.  The maxium data clock rate is 2.5 Mhz. */
1122
#define MDIO_SHIFT_CLK  0x10000
1123
#define MDIO_DATA_WRITE0 0x00000
1124
#define MDIO_DATA_WRITE1 0x20000
1125
#define MDIO_ENB                0x00000         /* Ignore the 0x02000 databook setting. */
1126
#define MDIO_ENB_IN             0x40000
1127
#define MDIO_DATA_READ  0x80000
1128
 
1129
static int mdio_read(struct device *dev, int phy_id, int location)
1130
{
1131
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1132
        int i;
1133
        int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
1134
        int retval = 0;
1135
        long mdio_addr = dev->base_addr + CSR9;
1136
 
1137
        if (tp->chip_id == LC82C168) {
1138
                long ioaddr = dev->base_addr;
1139
                int i = 1000;
1140
                outl(0x60020000 + (phy_id<<23) + (location<<18), ioaddr + 0xA0);
1141
                inl(ioaddr + 0xA0);
1142
                inl(ioaddr + 0xA0);
1143
                while (--i > 0)
1144
                        if ( ! ((retval = inl(ioaddr + 0xA0)) & 0x80000000))
1145
                                return retval & 0xffff;
1146
                return 0xffff;
1147
        }
1148
 
1149
        /* Establish sync by sending at least 32 logic ones. */
1150
        for (i = 32; i >= 0; i--) {
1151
                outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
1152
                mdio_delay();
1153
                outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
1154
                mdio_delay();
1155
        }
1156
        /* Shift the read command bits out. */
1157
        for (i = 15; i >= 0; i--) {
1158
                int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
1159
 
1160
                outl(MDIO_ENB | dataval, mdio_addr);
1161
                mdio_delay();
1162
                outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
1163
                mdio_delay();
1164
        }
1165
        /* Read the two transition, 16 data, and wire-idle bits. */
1166
        for (i = 19; i > 0; i--) {
1167
                outl(MDIO_ENB_IN, mdio_addr);
1168
                mdio_delay();
1169
                retval = (retval << 1) | ((inl(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
1170
                outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
1171
                mdio_delay();
1172
        }
1173
        return (retval>>1) & 0xffff;
1174
}
1175
 
1176
static void mdio_write(struct device *dev, int phy_id, int location, int value)
1177
{
1178
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1179
        int i;
1180
        int cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
1181
        long mdio_addr = dev->base_addr + CSR9;
1182
 
1183
        if (tp->chip_id == LC82C168) {
1184
                long ioaddr = dev->base_addr;
1185
                int i = 1000;
1186
                outl(cmd, ioaddr + 0xA0);
1187
                do
1188
                        if ( ! (inl(ioaddr + 0xA0) & 0x80000000))
1189
                                break;
1190
                while (--i > 0);
1191
                return;
1192
        }
1193
 
1194
        /* Establish sync by sending 32 logic ones. */
1195
        for (i = 32; i >= 0; i--) {
1196
                outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
1197
                mdio_delay();
1198
                outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
1199
                mdio_delay();
1200
        }
1201
        /* Shift the command bits out. */
1202
        for (i = 31; i >= 0; i--) {
1203
                int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
1204
                outl(MDIO_ENB | dataval, mdio_addr);
1205
                mdio_delay();
1206
                outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
1207
                mdio_delay();
1208
        }
1209
        /* Clear out extra bits. */
1210
        for (i = 2; i > 0; i--) {
1211
                outl(MDIO_ENB_IN, mdio_addr);
1212
                mdio_delay();
1213
                outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
1214
                mdio_delay();
1215
        }
1216
        return;
1217
}
1218
 
1219
 
1220
static int
1221
tulip_open(struct device *dev)
1222
{
1223
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1224
        long ioaddr = dev->base_addr;
1225
        int i;
1226
 
1227
        /* On some chip revs we must set the MII/SYM port before the reset!? */
1228
        if (tp->mii_cnt  ||  (tp->mtable  &&  tp->mtable->has_mii))
1229
                outl(0x00040000, ioaddr + CSR6);
1230
 
1231
        /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
1232
        outl(0x00000001, ioaddr + CSR0);
1233
 
1234
        if (request_irq(dev->irq, &tulip_interrupt, SA_SHIRQ, dev->name, dev))
1235
                return -EAGAIN;
1236
        MOD_INC_USE_COUNT;
1237
 
1238
        /* Deassert reset.
1239
           486: Set 8 longword cache alignment, 8 longword burst.
1240
           586: Set 16 longword cache alignment, no burst limit.
1241
           Cache alignment bits 15:14        Burst length 13:8
1242
                0000    No alignment  0x00000000 unlimited              0800 8 longwords
1243
                4000    8  longwords            0100 1 longword         1000 16 longwords
1244
                8000    16 longwords            0200 2 longwords        2000 32 longwords
1245
                C000    32  longwords           0400 4 longwords
1246
           Wait the specified 50 PCI cycles after a reset by initializing
1247
           Tx and Rx queues and the address filter list. */
1248
#if defined(__alpha__)
1249
        /* ToDo: Alpha setting could be better. */
1250
        outl(0x01A00000 | 0xE000, ioaddr + CSR0);
1251
#elif defined(__powerpc__)
1252
        outl(0x01A00080 | 0x8000, ioaddr + CSR0);
1253
#elif defined(__i386__)
1254
#if defined(MODULE)
1255
        /* When a module we don't have 'x86' to check. */
1256
        outl(0x01A00000 | 0x4800, ioaddr + CSR0);
1257
#else
1258
#if (LINUX_VERSION_CODE > 0x2014c)
1259
#define x86 boot_cpu_data.x86
1260
#endif
1261
        outl(0x01A00000 | (x86 <= 4 ? 0x4800 : 0x8000), ioaddr + CSR0);
1262
        if (x86 <= 4)
1263
                printk(KERN_INFO "%s: This is a 386/486 PCI system, setting cache "
1264
                           "alignment to %x.\n", dev->name,
1265
                           0x01A00000 | (x86 <= 4 ? 0x4800 : 0x8000));
1266
#endif
1267
#else
1268
        outl(0x01A00000 | 0x4800, ioaddr + CSR0);
1269
#warning Processor architecture undefined!
1270
#endif
1271
 
1272
        if (tulip_debug > 1)
1273
                printk(KERN_DEBUG "%s: tulip_open() irq %d.\n", dev->name, dev->irq);
1274
 
1275
        tulip_init_ring(dev);
1276
 
1277
        /* This is set_rx_mode(), but without starting the transmitter. */
1278
        /* Fill the whole address filter table with our physical address. */
1279
        {
1280
                u16 *eaddrs = (u16 *)dev->dev_addr;
1281
                u32 *setup_frm = tp->setup_frame, i;
1282
 
1283
                /* You must add the broadcast address when doing perfect filtering! */
1284
                *setup_frm++ = 0xffff;
1285
                *setup_frm++ = 0xffff;
1286
                *setup_frm++ = 0xffff;
1287
                /* Fill the rest of the accept table with our physical address. */
1288
                for (i = 1; i < 16; i++) {
1289
                        *setup_frm++ = eaddrs[0];
1290
                        *setup_frm++ = eaddrs[1];
1291
                        *setup_frm++ = eaddrs[2];
1292
                }
1293
                /* Put the setup frame on the Tx list. */
1294
                tp->tx_ring[0].length = 0x08000000 | 192;
1295
                tp->tx_ring[0].buffer1 = virt_to_bus(tp->setup_frame);
1296
                tp->tx_ring[0].status = 0x80000000;
1297
 
1298
                tp->cur_tx++;
1299
        }
1300
 
1301
        outl(virt_to_bus(tp->rx_ring), ioaddr + CSR3);
1302
        outl(virt_to_bus(tp->tx_ring), ioaddr + CSR4);
1303
 
1304
        tp->saved_if_port = dev->if_port;
1305
        if (dev->if_port == 0)
1306
                dev->if_port = tp->default_port;
1307
        if (tp->chip_id == DC21041  &&  dev->if_port > 4)
1308
                /* Invalid: Select initial TP, autosense, autonegotiate.  */
1309
                dev->if_port = 4;
1310
 
1311
        /* Allow selecting a default media. */
1312
        i = 0;
1313
        if (tp->mtable == NULL)
1314
                goto media_picked;
1315
        if (dev->if_port) {
1316
                int looking_for = media_cap[dev->if_port] & MediaIsMII ? 11 :
1317
                        (dev->if_port == 12 ? 0 : dev->if_port);
1318
                for (i = 0; i < tp->mtable->leafcount; i++)
1319
                        if (tp->mtable->mleaf[i].media == looking_for) {
1320
                                printk(KERN_INFO "%s: Using user-specified media %s.\n",
1321
                                           dev->name, medianame[dev->if_port]);
1322
                                goto media_picked;
1323
                        }
1324
        }
1325
        if ((tp->mtable->defaultmedia & 0x0800) == 0)
1326
                for (i = 0; i < tp->mtable->leafcount; i++)
1327
                  if (tp->mtable->mleaf[i].media == (tp->mtable->defaultmedia & 15)) {
1328
                        printk(KERN_INFO "%s: Using EEPROM-set media %s.\n",
1329
                                   dev->name, medianame[tp->mtable->mleaf[i].media]);
1330
                        goto media_picked;
1331
                  }
1332
        /* Start sensing first non-full-duplex media. */
1333
        for (i = tp->mtable->leafcount - 1;
1334
                 (media_cap[tp->mtable->mleaf[i].media] & MediaAlwaysFD) && i > 0; i--)
1335
          ;
1336
media_picked:
1337
 
1338
        tp->csr6 = 0;
1339
        tp->cur_index = i;
1340
        if (dev->if_port == 0  &&  tp->chip_id == DC21142) {
1341
                if (tp->mii_cnt) {
1342
                        if (tulip_debug > 1)
1343
                                printk(KERN_INFO "%s: Using MII transceiver %d, status "
1344
                                           "%4.4x.\n",
1345
                                           dev->name, tp->phys[0], mdio_read(dev, tp->phys[0], 1));
1346
                        select_media(dev, 1);
1347
                        outl(0x82020000, ioaddr + CSR6);
1348
                        tp->csr6 = 0x820E0000;
1349
                        dev->if_port = 11;
1350
                        outl(0x0000, ioaddr + CSR13);
1351
                        outl(0x0000, ioaddr + CSR14);
1352
                        outl(0x0008, ioaddr + CSR15);
1353
                } else {
1354
                        if (tulip_debug > 1)
1355
                                printk(KERN_INFO "%s: Using default 21143 media sense.\n",
1356
                                           dev->name);
1357
                        tp->csr6 = 0x82420200;
1358
                        outl(0x0003FFFF, ioaddr + CSR14);
1359
                        outl(0x0008, ioaddr + CSR15);
1360
                        outl(0x0001, ioaddr + CSR13);
1361
                        outl(0x1301, ioaddr + CSR12);
1362
                }
1363
        } else if (tp->chip_id == LC82C168  &&  tp->mii_cnt && ! tp->medialock) {
1364
                dev->if_port = 11;
1365
                tp->csr6 = 0x816C0000 | (tp->full_duplex ? 0x0200 : 0);
1366
                outl(0x0001, ioaddr + CSR15);
1367
        } else if (tp->chip_id == MX98713 && ! tp->medialock) {
1368
                dev->if_port = 0;
1369
                tp->csr6 = 0x01a80000 | (tp->full_duplex ? 0x0200 : 0);
1370
                outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
1371
        } else
1372
                select_media(dev, 1);
1373
 
1374
        /* Start the chip's Tx to process setup frame. */
1375
        outl(tp->csr6, ioaddr + CSR6);
1376
        outl(tp->csr6 | 0x2000, ioaddr + CSR6);
1377
 
1378
        dev->tbusy = 0;
1379
        tp->interrupt = 0;
1380
        dev->start = 1;
1381
 
1382
        /* Enable interrupts by setting the interrupt mask. */
1383
        outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR5);
1384
        outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
1385
        outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1386
        outl(0, ioaddr + CSR2);          /* Rx poll demand */
1387
 
1388
        if (tulip_debug > 2) {
1389
                printk(KERN_DEBUG "%s: Done tulip_open(), CSR0 %8.8x, CSR5 %8.8x CSR6 %8.8x.\n",
1390
                           dev->name, inl(ioaddr + CSR0), inl(ioaddr + CSR5),
1391
                           inl(ioaddr + CSR6));
1392
        }
1393
        /* Set the timer to switch to check for link beat and perhaps switch
1394
           to an alternate media type. */
1395
        init_timer(&tp->timer);
1396
        tp->timer.expires = RUN_AT(5*HZ);
1397
        tp->timer.data = (unsigned long)dev;
1398
        tp->timer.function = tulip_tbl[tp->chip_id].media_timer;
1399
        add_timer(&tp->timer);
1400
 
1401
        return 0;
1402
}
1403
 
1404
/* Set up the transceiver control registers for the selected media type. */
1405
static void select_media(struct device *dev, int startup)
1406
{
1407
        long ioaddr = dev->base_addr;
1408
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1409
        struct mediatable *mtable = tp->mtable;
1410
        u32 new_csr6;
1411
        int check_mii =0, i;
1412
 
1413
        if (mtable) {
1414
                struct medialeaf *mleaf = &mtable->mleaf[tp->cur_index];
1415
                unsigned char *p = mleaf->leafdata;
1416
                switch (mleaf->type) {
1417
                case 0:                                  /* 21140 non-MII xcvr. */
1418
                        if (tulip_debug > 1)
1419
                                printk(KERN_DEBUG "%s: Using a 21140 non-MII transceiver"
1420
                                           " with control setting %2.2x.\n",
1421
                                           dev->name, p[1]);
1422
                        dev->if_port = p[0];
1423
                        if (startup)
1424
                                outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
1425
                        outl(p[1], ioaddr + CSR12);
1426
                        new_csr6 = 0x02000000 | ((p[2] & 0x71) << 18);
1427
                        break;
1428
                case 2: case 4: {
1429
                        u16 setup[3];
1430
                        for (i = 0; i < 3; i++)
1431
                                setup[i] = get_u16(&p[i*2 + 1]);
1432
 
1433
                        dev->if_port = p[0] & 15;
1434
                        if (tulip_debug > 1)
1435
                                printk(KERN_DEBUG "%s: 21143 non-MII %s transceiver control %4.4x/%4.4x.\n",
1436
                                           dev->name, medianame[dev->if_port], setup[0], setup[1]);
1437
                        if (p[0] & 0x40) {       /* SIA (CSR13-15) setup values are provided. */
1438
                                outl(0, ioaddr + CSR13);
1439
                                outl(setup[1], ioaddr + CSR14);
1440
                                outl(setup[2], ioaddr + CSR15);
1441
                                outl(setup[0], ioaddr + CSR13);
1442
                                for (i = 0; i < 3; i++)                  /* Re-fill setup[]  */
1443
                                        setup[i] = get_u16(&p[i*2 + 7]);
1444
                        } else if (dev->if_port <= 4) {
1445
                                outl(0, ioaddr + CSR13);
1446
                                outl(t21142_csr14[dev->if_port], ioaddr + CSR14);
1447
                                outl(t21142_csr15[dev->if_port], ioaddr + CSR15);
1448
                                outl(t21142_csr13[dev->if_port], ioaddr + CSR13);
1449
                        } else {
1450
                                outl(0, ioaddr + CSR14);
1451
                                outl(8, ioaddr + CSR15);
1452
                                outl(0, ioaddr + CSR13);
1453
                        }
1454
                        outl(setup[0]<<16, ioaddr + CSR15);      /* Direction */
1455
                        outl(setup[1]<<16, ioaddr + CSR15);     /* Data */
1456
                        if (mleaf->type == 4)
1457
                                new_csr6 = 0x82020000 | ((setup[2] & 0x71) << 18);
1458
                        else
1459
                                new_csr6 = 0x82420000;
1460
                        break;
1461
                }
1462
                case 1: case 3: {
1463
                        int phy_num = p[0];
1464
                        int init_length = p[1];
1465
                        u16 *misc_info;
1466
                        u16 to_advertise;
1467
 
1468
                        dev->if_port = 11;
1469
                        check_mii = 1;
1470
                        new_csr6 = 0x020E0000;
1471
                        if (mleaf->type == 3) { /* 21142 */
1472
                                u16 *init_sequence = (u16*)(p+2);
1473
                                u16 *reset_sequence = &((u16*)(p+3))[init_length];
1474
                                int reset_length = p[2 + init_length*2];
1475
                                misc_info = reset_sequence + reset_length;
1476
                                if (startup)
1477
                                        for (i = 0; i < reset_length; i++)
1478
                                                outl(get_u16(&reset_sequence[i]) << 16, ioaddr + CSR15);
1479
                                for (i = 0; i < init_length; i++)
1480
                                        outl(get_u16(&init_sequence[i]) << 16, ioaddr + CSR15);
1481
                        } else {
1482
                                u8 *init_sequence = p + 2;
1483
                                u8 *reset_sequence = p + 3 + init_length;
1484
                                int reset_length = p[2 + init_length];
1485
                                misc_info = (u16*)(reset_sequence + reset_length);
1486
                                if (startup) {
1487
                                        outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
1488
                                        for (i = 0; i < reset_length; i++)
1489
                                                outl(reset_sequence[i], ioaddr + CSR12);
1490
                                }
1491
                                for (i = 0; i < init_length; i++)
1492
                                        outl(init_sequence[i], ioaddr + CSR12);
1493
                        }
1494
                        to_advertise = (get_u16(&misc_info[1]) & tp->to_advertise) | 1;
1495
                        tp->advertising[phy_num] = to_advertise;
1496
                        if (tulip_debug > 1 || 1)
1497
                                printk(KERN_DEBUG "%s:  Advertising %4.4x on PHY %d (%d).\n",
1498
                                           dev->name, to_advertise, phy_num, tp->phys[phy_num]);
1499
                        /* Bogus: put in by a committee?  */
1500
                        mdio_write(dev, tp->phys[phy_num], 4, to_advertise);
1501
                        break;
1502
                }
1503
                default:
1504
                  new_csr6 = 0x020E0000;
1505
                }
1506
                if (tulip_debug > 1)
1507
                        printk(KERN_DEBUG "%s: Using media type %s, CSR12 is %2.2x.\n",
1508
                                   dev->name, medianame[dev->if_port],
1509
                                   inl(ioaddr + CSR12) & 0xff);
1510
        } else if (tp->chip_id == DC21041) {
1511
                if (tulip_debug > 1)
1512
                        printk(KERN_DEBUG "%s: 21041 using media %s, CSR12 is %4.4x.\n",
1513
                                   dev->name, medianame[dev->if_port & 15],
1514
                                   inl(ioaddr + CSR12) & 0xffff);
1515
                outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
1516
                outl(t21041_csr14[dev->if_port], ioaddr + CSR14);
1517
                outl(t21041_csr15[dev->if_port], ioaddr + CSR15);
1518
                outl(t21041_csr13[dev->if_port], ioaddr + CSR13);
1519
                new_csr6 = 0x80020000;
1520
        } else if (tp->chip_id == LC82C168) {
1521
                if (startup && ! tp->medialock)
1522
                        dev->if_port = tp->mii_cnt ? 11 : 0;
1523
                if (tulip_debug > 1)
1524
                        printk(KERN_DEBUG "%s: PNIC PHY status is %3.3x, CSR12 %4.4x,"
1525
                                   " media %s.\n",
1526
                                   dev->name, inl(ioaddr + 0xB8), inl(ioaddr + CSR12),
1527
                                   medianame[dev->if_port]);
1528
                if (tp->mii_cnt) {
1529
                        new_csr6 = 0x812C0000;
1530
                        outl(0x0001, ioaddr + CSR15);
1531
                        outl(0x0201B07A, ioaddr + 0xB8);
1532
                } else if (startup) {
1533
                        /* Start with 10mbps to do autonegotiation. */
1534
                        outl(0x32, ioaddr + CSR12);
1535
                        new_csr6 = 0x00420000;
1536
                        outl(0x0001B078, ioaddr + 0xB8);
1537
                        outl(0x0201B078, ioaddr + 0xB8);
1538
                } else if (dev->if_port == 3  ||  dev->if_port == 5) {
1539
                        outl(0x33, ioaddr + CSR12);
1540
                        new_csr6 = 0x01860000;
1541
                        if (startup)
1542
                                outl(0x0201F868, ioaddr + 0xB8); /* Trigger autonegotiation. */
1543
                        else
1544
                                outl(0x1F868, ioaddr + 0xB8);
1545
                } else {
1546
                        outl(0x32, ioaddr + CSR12);
1547
                        new_csr6 = 0x00420000;
1548
                        outl(0x1F078, ioaddr + 0xB8);
1549
                }
1550
        } else if (tp->chip_id == DC21040) {                                    /* 21040 */
1551
                /* Turn on the xcvr interface. */
1552
                int csr12 = inl(ioaddr + CSR12);
1553
                if (tulip_debug > 1)
1554
                        printk(KERN_DEBUG "%s: 21040 media type is %s, CSR12 is %2.2x.\n",
1555
                                   dev->name, dev->if_port ? "AUI" : "10baseT", csr12);
1556
                new_csr6 = (dev->if_port ? 0x01860000 : 0x00420000);
1557
                /* Set the full duplux match frame. */
1558
                outl(FULL_DUPLEX_MAGIC, ioaddr + CSR11);
1559
                outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
1560
                outl(dev->if_port ? 0x0000000C : 0x00000004, ioaddr + CSR13);
1561
        } else {                                        /* Unknown chip type with no media table. */
1562
                if (tp->default_port == 0)
1563
                        if (tp->mii_cnt) {
1564
                                dev->if_port = 11;
1565
                        } else
1566
                                dev->if_port = 3;
1567
                if (media_cap[dev->if_port] & MediaIsMII) {
1568
                        new_csr6 = 0x020E0000;
1569
                } else if (media_cap[dev->if_port] & MediaIsFx) {
1570
                        new_csr6 = 0x028600000;
1571
                } else
1572
                        new_csr6 = 0x038600000;
1573
                if (tulip_debug > 1)
1574
                        printk(KERN_DEBUG "%s: No media description table, assuming "
1575
                                   "%s transceiver, CSR12 %2.2x.\n",
1576
                                   dev->name, medianame[dev->if_port],
1577
                                   inl(ioaddr + CSR12));
1578
        }
1579
 
1580
        tp->csr6 = new_csr6 | (tp->csr6 & 0xfdff) | (tp->full_duplex ? 0x0200 : 0);
1581
        return;
1582
}
1583
 
1584
static void tulip_timer(unsigned long data)
1585
{
1586
        struct device *dev = (struct device *)data;
1587
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1588
        long ioaddr = dev->base_addr;
1589
        u32 csr12 = inl(ioaddr + CSR12);
1590
        int next_tick = 0;
1591
 
1592
        if (tulip_debug > 3) {
1593
                printk(KERN_DEBUG "%s: Media selection tick, status %8.8x mode %8.8x "
1594
                           "SIA %8.8x %8.8x %8.8x %8.8x.\n",
1595
                           dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR6),
1596
                           csr12, inl(ioaddr + CSR13),
1597
                           inl(ioaddr + CSR14), inl(ioaddr + CSR15));
1598
        }
1599
        switch (tp->chip_id) {
1600
        case DC21040:
1601
                if (csr12 & 0x0002) { /* Network error */
1602
                        printk(KERN_INFO "%s: No 10baseT link beat found, switching to %s media.\n",
1603
                                   dev->name, dev->if_port ? "10baseT" : "AUI");
1604
                        dev->if_port ^= 1;
1605
                        outl(dev->if_port ? 0x0000000C : 0x00000004, ioaddr + CSR13);
1606
                        dev->trans_start = jiffies;
1607
                }
1608
                break;
1609
        case DC21041:
1610
                if (tulip_debug > 2)
1611
                        printk(KERN_DEBUG "%s: 21041 media tick  CSR12 %8.8x.\n",
1612
                                   dev->name, csr12);
1613
                switch (dev->if_port) {
1614
                case 0: case 3: case 4:
1615
                  if (csr12 & 0x0004) { /*LnkFail */
1616
                        /* 10baseT is dead.  Check for activity on alternate port. */
1617
                        tp->mediasense = 1;
1618
                        if (csr12 & 0x0200)
1619
                                dev->if_port = 2;
1620
                        else
1621
                                dev->if_port = 1;
1622
                        printk(KERN_INFO "%s: No 21041 10baseT link beat, Media switched to %s.\n",
1623
                                   dev->name, medianame[dev->if_port]);
1624
                        outl(0, ioaddr + CSR13); /* Reset */
1625
                        outl(t21041_csr14[dev->if_port], ioaddr + CSR14);
1626
                        outl(t21041_csr15[dev->if_port], ioaddr + CSR15);
1627
                        outl(t21041_csr13[dev->if_port], ioaddr + CSR13);
1628
                        next_tick = 10*HZ;                      /* 2.4 sec. */
1629
                  } else
1630
                        next_tick = 30*HZ;
1631
                  break;
1632
                case 1:                                 /* 10base2 */
1633
                case 2:                                 /* AUI */
1634
                  if (csr12 & 0x0100) {
1635
                        next_tick = (30*HZ);                    /* 30 sec. */
1636
                        tp->mediasense = 0;
1637
                  } else if ((csr12 & 0x0004) == 0) {
1638
                        printk(KERN_INFO "%s: 21041 media switched to 10baseT.\n", dev->name);
1639
                        dev->if_port = 0;
1640
                        select_media(dev, 0);
1641
                        next_tick = (24*HZ)/10;                         /* 2.4 sec. */
1642
                  } else if (tp->mediasense || (csr12 & 0x0002)) {
1643
                        dev->if_port = 3 - dev->if_port; /* Swap ports. */
1644
                        select_media(dev, 0);
1645
                        next_tick = 20*HZ;
1646
                  } else {
1647
                        next_tick = 20*HZ;
1648
                  }
1649
                  break;
1650
                }
1651
                break;
1652
        case DC21140:  case DC21142: case MX98713: default: {
1653
                struct medialeaf *mleaf;
1654
                unsigned char *p;
1655
                if (tp->mtable == NULL) {       /* No EEPROM info, use generic code. */
1656
                        /* Not much that can be done.
1657
                           Assume this a generic MII or SYM transceiver. */
1658
                        next_tick = 60*HZ;
1659
                        if (tulip_debug > 2)
1660
                                printk(KERN_DEBUG "%s: network media monitor CSR6 %8.8x "
1661
                                           "CSR12 0x%2.2x.\n",
1662
                                           dev->name, inl(ioaddr + CSR6), csr12 & 0xff);
1663
                        break;
1664
                }
1665
                mleaf = &tp->mtable->mleaf[tp->cur_index];
1666
                p = mleaf->leafdata;
1667
                switch (mleaf->type) {
1668
                case 0: case 4: {
1669
                        /* Type 0 serial or 4 SYM transceiver.  Check the link beat bit. */
1670
                        int offset = mleaf->type == 4 ? 5 : 2;
1671
                        s8 bitnum = p[offset];
1672
                        if (p[offset+1] & 0x80) {
1673
                                if (tulip_debug > 1)
1674
                                        printk(KERN_DEBUG"%s: Transceiver monitor tick "
1675
                                                   "CSR12=%#2.2x, no media sense.\n",
1676
                                                   dev->name, csr12);
1677
                                if (mleaf->type == 4) {
1678
                                        if (mleaf->media == 3 && (csr12 & 0x02))
1679
                                                goto select_next_media;
1680
                                }
1681
                                break;
1682
                        }
1683
                        if (tulip_debug > 2)
1684
                                printk(KERN_DEBUG "%s: Transceiver monitor tick: CSR12=%#2.2x"
1685
                                           " bit %d is %d, expecting %d.\n",
1686
                                           dev->name, csr12, (bitnum >> 1) & 7,
1687
                                           (csr12 & (1 << ((bitnum >> 1) & 7))) != 0,
1688
                                           (bitnum >= 0));
1689
                        /* Check that the specified bit has the proper value. */
1690
                        if ((bitnum < 0) !=
1691
                                ((csr12 & (1 << ((bitnum >> 1) & 7))) != 0)) {
1692
                                if (tulip_debug > 1)
1693
                                        printk(KERN_DEBUG "%s: Link beat detected for %s.\n", dev->name,
1694
                                                   medianame[mleaf->media]);
1695
                                if ((p[2] & 0x61) == 0x01)      /* Bogus Znyx board. */
1696
                                        goto actually_mii;
1697
                                break;
1698
                        }
1699
                        if (tp->medialock)
1700
                                break;
1701
          select_next_media:
1702
                        if (--tp->cur_index < 0) {
1703
                                /* We start again, but should instead look for default. */
1704
                                tp->cur_index = tp->mtable->leafcount - 1;
1705
                        }
1706
                        dev->if_port = tp->mtable->mleaf[tp->cur_index].media;
1707
                        if (media_cap[dev->if_port] & MediaIsFD)
1708
                                goto select_next_media; /* Skip FD entries. */
1709
                        if (tulip_debug > 1)
1710
                                printk(KERN_DEBUG "%s: No link beat on media %s,"
1711
                                           " trying transceiver type %s.\n",
1712
                                           dev->name, medianame[mleaf->media & 15],
1713
                                           medianame[tp->mtable->mleaf[tp->cur_index].media]);
1714
                        select_media(dev, 0);
1715
                        /* Restart the transmit process. */
1716
                        outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1717
                        outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1718
                        next_tick = (24*HZ)/10;
1719
                        break;
1720
                }
1721
                case 1:  case 3: {              /* 21140, 21142 MII */
1722
                        int mii_reg1, mii_reg5;
1723
                actually_mii:
1724
                        mii_reg1 = mdio_read(dev, tp->phys[0], 1);
1725
                        mii_reg5 = mdio_read(dev, tp->phys[0], 5);
1726
                        if (tulip_debug > 1)
1727
                                printk(KERN_INFO "%s: MII status %4.4x, Link partner report "
1728
                                           "%4.4x, CSR12 %2.2x, %cD.\n",
1729
                                           dev->name, mii_reg1, mii_reg5, csr12,
1730
                                           tp->full_duplex ? 'F' : 'H');
1731
                        if (mii_reg1 != 0xffff  &&  (mii_reg1 & 0x0004) == 0) {
1732
                                int new_reg1 = mdio_read(dev, tp->phys[0], 1);
1733
                                if ((new_reg1 & 0x0004) == 0) {
1734
                                        printk(KERN_INFO "%s: No link beat on the MII interface,"
1735
                                                   " status then %4.4x now %4.4x.\n",
1736
                                                   dev->name, mii_reg1, new_reg1);
1737
                                        if (tp->mtable  &&  tp->mtable->has_nonmii)
1738
                                                goto select_next_media;
1739
                                }
1740
                        }
1741
                        if (mii_reg5 == 0xffff  ||  mii_reg5 == 0x0000)
1742
                                ;                               /* No MII device or no link partner report */
1743
                        else if (tp->full_duplex_lock)
1744
                                ;
1745
                        else {
1746
                                int negotiated = mii_reg5 & tp->advertising[0];
1747
                                int duplex = ((negotiated & 0x0100) != 0
1748
                                                          || (negotiated & 0x00C0) == 0x0040);
1749
                                /* 100baseTx-FD  or  10T-FD, but not 100-HD */
1750
                                if (tp->full_duplex != duplex) {
1751
                                        tp->full_duplex = duplex;
1752
                                        if (tp->full_duplex)
1753
                                                tp->csr6 |= 0x0200;
1754
                                        else
1755
                                                tp->csr6 &= ~0x0200;
1756
                                        outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1757
                                        outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1758
                                        if (tulip_debug > 0) /* Gurppp, should be >1 */
1759
                                                printk(KERN_INFO "%s: Setting %s-duplex based on MII"
1760
                                                           " Xcvr #%d parter capability of %4.4x.\n",
1761
                                                           dev->name, tp->full_duplex ? "full" : "half",
1762
                                                           tp->phys[0], mii_reg5);
1763
                                }
1764
                        }
1765
                        next_tick = 60*HZ;
1766
                        break;
1767
                }
1768
                case 2:                                 /* 21142 serial block has no link beat. */
1769
                default:
1770
                        break;
1771
                }
1772
        }
1773
        break;
1774
        }
1775
        if (next_tick) {
1776
                tp->timer.expires = RUN_AT(next_tick);
1777
                add_timer(&tp->timer);
1778
        }
1779
}
1780
 
1781
/* Handle the 21143 uniquely: do autoselect with NWay, not the EEPROM list
1782
   of available transceivers.  */
1783
static void t21142_timer(unsigned long data)
1784
{
1785
        struct device *dev = (struct device *)data;
1786
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1787
        long ioaddr = dev->base_addr;
1788
        int csr12 = inl(ioaddr + CSR12);
1789
        int next_tick = 60*HZ;
1790
        int new_csr6 = 0;
1791
 
1792
        if (tulip_debug > 1)
1793
                printk(KERN_INFO"%s: 21143 negotiation status %8.8x, %s.\n",
1794
                           dev->name, csr12, medianame[dev->if_port]);
1795
        if (media_cap[dev->if_port] & MediaIsMII) {
1796
                int mii_reg1 = mdio_read(dev, tp->phys[0], 1);
1797
                int mii_reg5 = mdio_read(dev, tp->phys[0], 5);
1798
                if (tulip_debug > 1)
1799
                        printk(KERN_INFO "%s: MII status %4.4x, Link partner report "
1800
                                   "%4.4x, CSR6 %x.\n",
1801
                                   dev->name, mii_reg1, mii_reg5, inl(ioaddr + CSR6));
1802
                if (mii_reg1 != 0xffff  &&  (mii_reg1 & 0x0004) == 0) {
1803
                        int new_reg1 = mdio_read(dev, tp->phys[0], 1);
1804
                        if ((new_reg1 & 0x0004) == 0) {
1805
                                printk(KERN_INFO "%s: No link beat on the MII interface,"
1806
                                           " status then %4.4x now %4.4x.\n",
1807
                                           dev->name, mii_reg1, new_reg1);
1808
                        }
1809
                        if (tp->full_duplex_lock)
1810
                                ;
1811
                        else {
1812
                                int negotiated = mii_reg5 & tp->advertising[0];
1813
                                int duplex = ((negotiated & 0x0100) != 0
1814
                                                          || (negotiated & 0x00C0) == 0x0040);
1815
                                /* 100baseTx-FD  or  10T-FD, but not 100-HD */
1816
                                if (tp->full_duplex != duplex) {
1817
                                        tp->full_duplex = duplex;
1818
                                        if (tp->full_duplex)
1819
                                                tp->csr6 |= 0x0200;
1820
                                        else
1821
                                                tp->csr6 &= ~0x0200;
1822
                                        outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1823
                                        outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1824
                                        if (tulip_debug > 0)
1825
                                                printk(KERN_INFO "%s: Setting %s-duplex based on MII"
1826
                                                           " Xcvr #%d parter capability of %4.4x.\n",
1827
                                                           dev->name, tp->full_duplex ? "full" : "half",
1828
                                                           tp->phys[0], mii_reg5);
1829
                                }
1830
                        }
1831
                        next_tick = 60*HZ;
1832
                }
1833
        } else if (dev->if_port == 3) {
1834
                if (csr12 & 2) {                /* No 100mbps link beat, revert to 10mbps. */
1835
                        new_csr6 = 0x82420200;
1836
                        outl(new_csr6, ioaddr + CSR6);
1837
                        outl(0x0000, ioaddr + CSR13);
1838
                        outl(0x0003FFFF, ioaddr + CSR14);
1839
                        outl(0x0008, ioaddr + CSR15);
1840
                        outl(0x0001, ioaddr + CSR13);
1841
                        outl(0x1301, ioaddr + CSR12); /* Start NWay. */
1842
                }
1843
        } else if ((csr12 & 0x7000) != 0x5000) {
1844
                /* Negotiation failed.  Search media types. */
1845
                if (tulip_debug > 1)
1846
                        printk(KERN_INFO"%s: 21143 negotiation failed, status %8.8x.\n",
1847
                                   dev->name, csr12);
1848
                if (!(csr12 & 4)) {             /* 10mbps link beat good. */
1849
                        new_csr6 = 0x82420000;
1850
                        dev->if_port = 0;
1851
                        outl(0, ioaddr + CSR13);
1852
                        outl(0x0003FFFF, ioaddr + CSR14);
1853
                        outl(t21142_csr15[dev->if_port], ioaddr + CSR15);
1854
                        outl(t21142_csr13[dev->if_port], ioaddr + CSR13);
1855
                } else if (csr12 & 0x100) {
1856
                        new_csr6 = 0x82420200;
1857
                        dev->if_port = 2;
1858
                        outl(0, ioaddr + CSR13);
1859
                        outl(0x0003FFFF, ioaddr + CSR14);
1860
                        outl(0x0008, ioaddr + CSR15);
1861
                        outl(0x0001, ioaddr + CSR13);
1862
                } else {
1863
                        /* Select 100mbps port to check for link beat. */
1864
                        new_csr6 = 0x83860000;
1865
                        dev->if_port = 3;
1866
                        outl(0, ioaddr + CSR13);
1867
                        outl(0x0003FF7F, ioaddr + CSR14);
1868
                        outl(8, ioaddr + CSR15);
1869
                        outl(1, ioaddr + CSR13);
1870
                }
1871
                if (tulip_debug > 1)
1872
                        printk(KERN_INFO"%s: Testing new 21143 media %s.\n",
1873
                                   dev->name, medianame[dev->if_port]);
1874
                if (new_csr6 != (tp->csr6 & ~0x00D5)) {
1875
                        tp->csr6 &= 0x00D5;
1876
                        tp->csr6 |= new_csr6;
1877
                        outl(0x0301, ioaddr + CSR12);
1878
                        outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1879
                        outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1880
                }
1881
        }
1882
        tp->timer.expires = RUN_AT(next_tick);
1883
        add_timer(&tp->timer);
1884
}
1885
 
1886
static void t21142_lnk_change( struct device *dev)
1887
{
1888
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1889
        long ioaddr = dev->base_addr;
1890
        int csr12 = inl(ioaddr + CSR12);
1891
 
1892
        if (tulip_debug > 1)
1893
                printk(KERN_INFO"%s: 21143 link status interrupt %8.8x, CSR5 %x.\n",
1894
                           dev->name, csr12, inl(ioaddr + CSR5));
1895
 
1896
        if ((csr12 & 0x7000) == 0x5000) {
1897
                if (csr12 & 0x01800000) {
1898
                        /* Switch to 100mbps mode. */
1899
                        outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1900
                        if (csr12 & 0x01000000) {
1901
                                dev->if_port = 5;
1902
                                tp->csr6 = 0x83860200;
1903
                        } else {
1904
                                dev->if_port = 3;
1905
                                tp->csr6 = 0x83860000;
1906
                        }
1907
                        outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1908
                } /* Else 10baseT-FD is handled automatically. */
1909
        } else if (dev->if_port == 3) {
1910
                if (!(csr12 & 2))
1911
                        printk(KERN_INFO"%s: 21143 100baseTx link beat good.\n",
1912
                                   dev->name);
1913
                else
1914
                        dev->if_port = 0;
1915
        } else if (dev->if_port == 0) {
1916
                if (!(csr12 & 4))
1917
                        printk(KERN_INFO"%s: 21143 10baseT link beat good.\n",
1918
                                   dev->name);
1919
        } else if (!(csr12 & 4)) {              /* 10mbps link beat good. */
1920
                        printk(KERN_INFO"%s: 21143 10mpbs sensed media.\n",
1921
                                   dev->name);
1922
                        dev->if_port = 0;
1923
        } else  {               /* 100mbps link beat good. */
1924
                printk(KERN_INFO"%s: 21143 100baseTx sensed media.\n",
1925
                           dev->name);
1926
                dev->if_port = 3;
1927
                tp->csr6 = 0x83860000;
1928
                outl(0x0003FF7F, ioaddr + CSR14);
1929
                outl(0x0301, ioaddr + CSR12);
1930
                outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1931
                outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1932
        }
1933
}
1934
 
1935
static void mxic_timer(unsigned long data)
1936
{
1937
        struct device *dev = (struct device *)data;
1938
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1939
        long ioaddr = dev->base_addr;
1940
        int next_tick = 60*HZ;
1941
 
1942
        if (tulip_debug > 3) {
1943
                printk(KERN_INFO"%s: MXIC negotiation status %8.8x.\n", dev->name,
1944
                           inl(ioaddr + CSR12));
1945
        }
1946
        if (next_tick) {
1947
                tp->timer.expires = RUN_AT(next_tick);
1948
                add_timer(&tp->timer);
1949
        }
1950
}
1951
 
1952
static void pnic_timer(unsigned long data)
1953
{
1954
        struct device *dev = (struct device *)data;
1955
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
1956
        long ioaddr = dev->base_addr;
1957
        int csr12 = inl(ioaddr + CSR12);
1958
        int next_tick = 60*HZ;
1959
        int new_csr6 = tp->csr6 & ~0x40C40200;
1960
 
1961
        if (media_cap[dev->if_port] & MediaIsMII) {
1962
                int negotiated = mdio_read(dev, tp->phys[0], 5) & tp->advertising[0];
1963
 
1964
                if (tulip_debug > 1)
1965
                        printk(KERN_DEBUG "%s: LC82C168 negotiated capability %8.8x, "
1966
                                   "CSR5 %8.8x.\n",
1967
                                   dev->name, negotiated, inl(ioaddr + CSR5));
1968
 
1969
                if (negotiated & 0x0380)                                /* 10 vs 100mbps */
1970
                        new_csr6 |= 0x812E0000;
1971
                else
1972
                        new_csr6 |= 0x816E0000;
1973
                if (((negotiated & 0x0300) == 0x0100)                   /* Duplex */
1974
                        || (negotiated & 0x00C0) == 0x0040
1975
                        || tp->full_duplex_lock) {
1976
                        tp->full_duplex = 1;
1977
                        new_csr6 |= 0x0200;
1978
                }
1979
                if (tulip_debug > 1)
1980
                        printk(KERN_DEBUG "%s: LC82C168 MII PHY status %4.4x, Link "
1981
                                   "partner report %4.4x, csr6 %8.8x/%8.8x.\n",
1982
                           dev->name, mdio_read(dev, tp->phys[0], 1), negotiated,
1983
                                   tp->csr6, inl(ioaddr + CSR6));
1984
        } else {
1985
                int phy_reg = inl(ioaddr + 0xB8);
1986
                int csr5 = inl(ioaddr + CSR5);
1987
 
1988
                if (tulip_debug > 1)
1989
                        printk(KERN_DEBUG "%s: LC82C168 phy status %8.8x, CSR5 %8.8x.\n",
1990
                                   dev->name, phy_reg, csr5);
1991
 
1992
                if (phy_reg & 0x04000000) {     /* Remote link fault */
1993
                        /*outl(0x0201F078, ioaddr + 0xB8);*/
1994
                        next_tick = 3*HZ;
1995
                }
1996
                if (inl(ioaddr + CSR5) & TPLnkFail) { /* 100baseTx link beat */
1997
                        if (tulip_debug > 1)
1998
                                printk(KERN_DEBUG "%s: %s link beat failed, CSR12 %4.4x, "
1999
                                           "CSR5 %8.8x, PHY %3.3x.\n",
2000
                                           dev->name, medianame[dev->if_port], csr12,
2001
                                           inl(ioaddr + CSR5), inl(ioaddr + 0xB8));
2002
                        if (tp->medialock) {
2003
                        } else if (dev->if_port == 0) {
2004
                                dev->if_port = 3;
2005
                                outl(0x33, ioaddr + CSR12);
2006
                                new_csr6 = 0x01860000;
2007
                                outl(0x1F868, ioaddr + 0xB8);
2008
                        } else {
2009
                                dev->if_port = 0;
2010
                                outl(0x32, ioaddr + CSR12);
2011
                                new_csr6 = 0x00420000;
2012
                                outl(0x1F078, ioaddr + 0xB8);
2013
                        }
2014
                        new_csr6 |= (tp->csr6 & 0xfdff);
2015
                        next_tick = 3*HZ;
2016
                } else
2017
                        new_csr6 = tp->csr6;
2018
                if (tp->full_duplex_lock  ||  (phy_reg & 0x30000000) != 0) {
2019
                        tp->full_duplex = 1;
2020
                        new_csr6 |= 0x00000200;
2021
                }
2022
        }
2023
        if (tp->csr6 != new_csr6) {
2024
                tp->csr6 = new_csr6;
2025
                outl(tp->csr6 | 0x0002, ioaddr + CSR6); /* Restart Tx */
2026
                outl(tp->csr6 | 0x2002, ioaddr + CSR6);
2027
                dev->trans_start = jiffies;
2028
                if (tulip_debug > 0) /* Gurppp, should be >1 */
2029
                        printk(KERN_INFO "%s: Changing PNIC configuration to %s-duplex, "
2030
                                   "CSR6 %8.8x.\n",
2031
                                   dev->name, tp->full_duplex ? "full" : "half", new_csr6);
2032
        }
2033
        tp->timer.expires = RUN_AT(next_tick);
2034
        add_timer(&tp->timer);
2035
}
2036
 
2037
static void tulip_tx_timeout(struct device *dev)
2038
{
2039
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2040
        long ioaddr = dev->base_addr;
2041
 
2042
        if (media_cap[dev->if_port] & MediaIsMII) {
2043
                /* Do nothing -- the media monitor should handle this. */
2044
                if (tulip_debug > 1)
2045
                        printk(KERN_WARNING "%s: Transmit timeout using MII device.\n",
2046
                                   dev->name);
2047
        } else if (tp->chip_id == DC21040) {
2048
                if ( !tp->medialock  &&  inl(ioaddr + CSR12) & 0x0002) {
2049
                        dev->if_port ^= 1;
2050
                        printk(KERN_INFO "%s: transmit timed out, switching to "
2051
                                   "%s10baseT media.\n",
2052
                                   dev->name, dev->if_port ? "non" : "");
2053
                        outl(dev->if_port ? 0x0000000C : 0x00000004, ioaddr + CSR13);
2054
                }
2055
                dev->trans_start = jiffies;
2056
                return;
2057
        } else if (tp->chip_id == DC21041) {
2058
                int csr12 = inl(ioaddr + CSR12);
2059
 
2060
                printk(KERN_WARNING "%s: 21041 transmit timed out, status %8.8x, "
2061
                           "CSR12 %8.8x, CSR13 %8.8x, CSR14 %8.8x, resetting...\n",
2062
                           dev->name, inl(ioaddr + CSR5), csr12,
2063
                           inl(ioaddr + CSR13), inl(ioaddr + CSR14));
2064
                tp->mediasense = 1;
2065
                if ( ! tp->medialock) {
2066
                        if (dev->if_port == 1 || dev->if_port == 2)
2067
                                if (csr12 & 0x0004) {
2068
                                        dev->if_port = 2 - dev->if_port;
2069
                                } else
2070
                                        dev->if_port = 0;
2071
                        else
2072
                                dev->if_port = 1;
2073
                        select_media(dev, 0);
2074
                }
2075
        } else if (tp->chip_id == DC21140 || tp->chip_id == DC21142
2076
                           || tp->chip_id == MX98713) {
2077
                printk(KERN_WARNING "%s: 21140 transmit timed out, status %8.8x, "
2078
                           "SIA %8.8x %8.8x %8.8x %8.8x, resetting...\n",
2079
                           dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12),
2080
                           inl(ioaddr + CSR13), inl(ioaddr + CSR14), inl(ioaddr + CSR15));
2081
                if ( ! tp->medialock  &&  tp->mtable) {
2082
                        if (--tp->cur_index < 0) {
2083
                                /* We start again, but should instead look for default. */
2084
                                tp->cur_index = tp->mtable->leafcount - 1;
2085
                        }
2086
                        select_media(dev, 0);
2087
                        printk(KERN_WARNING "%s: transmit timed out, switching to %s "
2088
                                   "media.\n", dev->name, medianame[dev->if_port]);
2089
                }
2090
        } else {
2091
                printk(KERN_WARNING "%s: Transmit timed out, status %8.8x, CSR12 "
2092
                           "%8.8x, resetting...\n",
2093
                           dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12));
2094
                dev->if_port = 0;
2095
        }
2096
 
2097
#ifdef way_too_many_messages
2098
        printk("  Rx ring %8.8x: ", (int)tp->rx_ring);
2099
        for (i = 0; i < RX_RING_SIZE; i++)
2100
                printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
2101
        printk("\n  Tx ring %8.8x: ", (int)tp->tx_ring);
2102
        for (i = 0; i < TX_RING_SIZE; i++)
2103
                printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
2104
        printk("\n");
2105
#endif
2106
 
2107
        /* Stop and restart the chip's Tx processes . */
2108
        outl(tp->csr6 | 0x0002, ioaddr + CSR6);
2109
        outl(tp->csr6 | 0x2002, ioaddr + CSR6);
2110
        /* Trigger an immediate transmit demand. */
2111
        outl(0, ioaddr + CSR1);
2112
 
2113
        dev->trans_start = jiffies;
2114
        tp->stats.tx_errors++;
2115
        return;
2116
}
2117
 
2118
 
2119
/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
2120
static void tulip_init_ring(struct device *dev)
2121
{
2122
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2123
        int i;
2124
 
2125
        tp->tx_full = 0;
2126
        tp->cur_rx = tp->cur_tx = 0;
2127
        tp->dirty_rx = tp->dirty_tx = 0;
2128
 
2129
        for (i = 0; i < RX_RING_SIZE; i++) {
2130
                tp->rx_ring[i].status = 0x80000000;     /* Owned by Tulip chip */
2131
                tp->rx_ring[i].length = PKT_BUF_SZ;
2132
                {
2133
                        /* Note the receive buffer must be longword aligned.
2134
                           dev_alloc_skb() provides 16 byte alignment.  But do *not*
2135
                           use skb_reserve() to align the IP header! */
2136
                        struct sk_buff *skb;
2137
                        skb = dev_alloc_skb(PKT_BUF_SZ);
2138
                        tp->rx_skbuff[i] = skb;
2139
                        if (skb == NULL)
2140
                                break;                  /* Bad news!  */
2141
                        skb->dev = dev;                 /* Mark as being used by this device. */
2142
#if LINUX_VERSION_CODE > 0x10300
2143
                        tp->rx_ring[i].buffer1 = virt_to_bus(skb->tail);
2144
#else
2145
                        tp->rx_ring[i].buffer1 = virt_to_bus(skb->data);
2146
#endif
2147
                }
2148
                tp->rx_ring[i].buffer2 = virt_to_bus(&tp->rx_ring[i+1]);
2149
        }
2150
        /* Mark the last entry as wrapping the ring. */
2151
        tp->rx_ring[i-1].length = PKT_BUF_SZ | DESC_RING_WRAP;
2152
        tp->rx_ring[i-1].buffer2 = virt_to_bus(&tp->rx_ring[0]);
2153
 
2154
        /* The Tx buffer descriptor is filled in as needed, but we
2155
           do need to clear the ownership bit. */
2156
        for (i = 0; i < TX_RING_SIZE; i++) {
2157
                tp->tx_skbuff[i] = 0;
2158
                tp->tx_ring[i].status = 0x00000000;
2159
                tp->tx_ring[i].buffer2 = virt_to_bus(&tp->tx_ring[i+1]);
2160
        }
2161
        tp->tx_ring[i-1].buffer2 = virt_to_bus(&tp->tx_ring[0]);
2162
}
2163
 
2164
static int
2165
tulip_start_xmit(struct sk_buff *skb, struct device *dev)
2166
{
2167
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2168
        int entry;
2169
        u32 flag;
2170
 
2171
        /* Block a timer-based transmit from overlapping.  This could better be
2172
           done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
2173
        if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
2174
                if (jiffies - dev->trans_start < TX_TIMEOUT)
2175
                        return 1;
2176
                tulip_tx_timeout(dev);
2177
                return 1;
2178
        }
2179
 
2180
        /* Caution: the write order is important here, set the base address
2181
           with the "ownership" bits last. */
2182
 
2183
        /* Calculate the next Tx descriptor entry. */
2184
        entry = tp->cur_tx % TX_RING_SIZE;
2185
 
2186
        tp->tx_skbuff[entry] = skb;
2187
        tp->tx_ring[entry].buffer1 = virt_to_bus(skb->data);
2188
 
2189
        if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE/2) {/* Typical path */
2190
          flag = 0x60000000; /* No interrupt */
2191
          dev->tbusy = 0;
2192
        } else if (tp->cur_tx - tp->dirty_tx == TX_RING_SIZE/2) {
2193
          flag = 0xe0000000; /* Tx-done intr. */
2194
          dev->tbusy = 0;
2195
        } else if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE - 2) {
2196
          flag = 0x60000000; /* No Tx-done intr. */
2197
          dev->tbusy = 0;
2198
        } else {
2199
          /* Leave room for set_rx_mode() to fill entries. */
2200
          flag = 0xe0000000; /* Tx-done intr. */
2201
          tp->tx_full = 1;
2202
        }
2203
        if (entry == TX_RING_SIZE-1)
2204
                flag |= 0xe0000000 | DESC_RING_WRAP;
2205
 
2206
        tp->tx_ring[entry].length = skb->len | flag;
2207
        tp->tx_ring[entry].status = 0x80000000; /* Pass ownership to the chip. */
2208
        tp->cur_tx++;
2209
        /* Trigger an immediate transmit demand. */
2210
        outl(0, dev->base_addr + CSR1);
2211
 
2212
        dev->trans_start = jiffies;
2213
 
2214
        return 0;
2215
}
2216
 
2217
/* The interrupt handler does all of the Rx thread work and cleans up
2218
   after the Tx thread. */
2219
static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
2220
{
2221
        struct device *dev = (struct device *)dev_instance;
2222
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2223
        long ioaddr = dev->base_addr;
2224
        int csr5, work_budget = max_interrupt_work;
2225
 
2226
#if defined(__i386__) && defined(SMP_CHECK)   /* && defined(__SMP__) */
2227
        if (test_and_set_bit(0, (void*)&dev->interrupt)) {
2228
                printk(KERN_ERR "%s: Re-entering the interrupt handler with proc %d,"
2229
                           " proc %d already handling.\n", dev->name,
2230
                           tp->smp_proc_id, hard_smp_processor_id());
2231
                dev->interrupt = 0;
2232
                return;
2233
        } else
2234
                tp->smp_proc_id = hard_smp_processor_id();
2235
#else
2236
        if (dev->interrupt) {
2237
                printk(KERN_ERR "%s: Re-entering the interrupt handler.\n", dev->name);
2238
                return;
2239
        }
2240
        dev->interrupt = 1;
2241
#endif
2242
 
2243
        do {
2244
                csr5 = inl(ioaddr + CSR5);
2245
                /* Acknowledge all of the current interrupt sources ASAP. */
2246
                outl(csr5 & 0x0001ffff, ioaddr + CSR5);
2247
 
2248
                if (tulip_debug > 4)
2249
                        printk(KERN_DEBUG "%s: interrupt  csr5=%#8.8x new csr5=%#8.8x.\n",
2250
                                   dev->name, csr5, inl(dev->base_addr + CSR5));
2251
 
2252
                if ((csr5 & (NormalIntr|AbnormalIntr)) == 0)
2253
                        break;
2254
 
2255
                if (csr5 & (RxIntr | RxNoBuf))
2256
                        work_budget -= tulip_rx(dev);
2257
 
2258
                if (csr5 & (TxNoBuf | TxDied | TxIntr)) {
2259
                        unsigned int dirty_tx;
2260
 
2261
                        for (dirty_tx = tp->dirty_tx; tp->cur_tx - dirty_tx > 0;
2262
                                 dirty_tx++) {
2263
                                int entry = dirty_tx % TX_RING_SIZE;
2264
                                int status = tp->tx_ring[entry].status;
2265
 
2266
                                if (status < 0)
2267
                                        break;                  /* It still hasn't been Txed */
2268
                                /* Check for Rx filter setup frames. */
2269
                                if (tp->tx_skbuff[entry] == NULL)
2270
                                  continue;
2271
 
2272
                                if (status & 0x8000) {
2273
                                        /* There was an major error, log it. */
2274
#ifndef final_version
2275
                                        if (tulip_debug > 1)
2276
                                                printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
2277
                                                           dev->name, status);
2278
#endif
2279
                                        tp->stats.tx_errors++;
2280
                                        if (status & 0x4104) tp->stats.tx_aborted_errors++;
2281
                                        if (status & 0x0C00) tp->stats.tx_carrier_errors++;
2282
                                        if (status & 0x0200) tp->stats.tx_window_errors++;
2283
                                        if (status & 0x0002) tp->stats.tx_fifo_errors++;
2284
                                        if ((status & 0x0080) && tp->full_duplex == 0)
2285
                                                tp->stats.tx_heartbeat_errors++;
2286
#ifdef ETHER_STATS
2287
                                        if (status & 0x0100) tp->stats.collisions16++;
2288
#endif
2289
                                } else {
2290
#ifdef ETHER_STATS
2291
                                        if (status & 0x0001) tp->stats.tx_deferred++;
2292
#endif
2293
#if LINUX_VERSION_CODE > 0x20127
2294
                                        tp->stats.tx_bytes += tp->tx_ring[entry].length & 0x7ff;
2295
#endif
2296
                                        tp->stats.collisions += (status >> 3) & 15;
2297
                                        tp->stats.tx_packets++;
2298
                                }
2299
 
2300
                                /* Free the original skb. */
2301
                                dev_free_skb(tp->tx_skbuff[entry]);
2302
                                tp->tx_skbuff[entry] = 0;
2303
                        }
2304
 
2305
#ifndef final_version
2306
                        if (tp->cur_tx - dirty_tx > TX_RING_SIZE) {
2307
                                printk(KERN_ERR "%s: Out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
2308
                                           dev->name, dirty_tx, tp->cur_tx, tp->tx_full);
2309
                                dirty_tx += TX_RING_SIZE;
2310
                        }
2311
#endif
2312
 
2313
                        if (tp->tx_full && dev->tbusy
2314
                                && tp->cur_tx - dirty_tx  < TX_RING_SIZE - 2) {
2315
                                /* The ring is no longer full, clear tbusy. */
2316
                                tp->tx_full = 0;
2317
                                dev->tbusy = 0;
2318
                                mark_bh(NET_BH);
2319
                        }
2320
 
2321
                        tp->dirty_tx = dirty_tx;
2322
                        if (csr5 & TxDied) {
2323
                                if (tulip_debug)
2324
                                        printk(KERN_WARNING "%s: The transmitter stopped!"
2325
                                                   "  CSR5 is %x, CSR6 %x.\n",
2326
                                                   dev->name, csr5, inl(ioaddr + CSR6));
2327
                                outl(tp->csr6 | 0x0002, ioaddr + CSR6);
2328
                                outl(tp->csr6 | 0x2002, ioaddr + CSR6);
2329
                        }
2330
                }
2331
 
2332
                /* Log errors. */
2333
                if (csr5 & AbnormalIntr) {      /* Abnormal error summary bit. */
2334
                        if (csr5 == 0xffffffff)
2335
                                break;
2336
                        if (csr5 & TxJabber) tp->stats.tx_errors++;
2337
                        if (csr5 & TxFIFOUnderflow) {
2338
                                if ((tp->csr6 & 0xC000) != 0xC000)
2339
                                        tp->csr6 += 0x4000;     /* Bump up the Tx threshold */
2340
                                else
2341
                                        tp->csr6 |= 0x00200000;  /* Store-n-forward. */
2342
                                /* Restart the transmit process. */
2343
                                outl(tp->csr6 | 0x0002, ioaddr + CSR6);
2344
                                outl(tp->csr6 | 0x2002, ioaddr + CSR6);
2345
                        }
2346
                        if (csr5 & RxDied) {            /* Missed a Rx frame. */
2347
                                tp->stats.rx_errors++;
2348
                                tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2349
                        }
2350
                        if (csr5 & TimerInt) {
2351
                                printk(KERN_ERR "%s: Something Wicked happened! %8.8x.\n",
2352
                                           dev->name, csr5);
2353
                                /* Hmmmmm, it's not clear what to do here. */
2354
                        }
2355
                        if (csr5 & (TPLnkPass | TPLnkFail | 0x08000000)
2356
                                && tp->chip_id == DC21142) {
2357
                                if (tulip_debug > 1)
2358
                                        printk(KERN_INFO"%s: 21143 link change, CSR5 = %8.8x.\n",
2359
                                                   dev->name, csr5);
2360
                                t21142_lnk_change(dev);
2361
                        }
2362
                        /* Clear all error sources, included undocumented ones! */
2363
                        outl(0x0800f7ba, ioaddr + CSR5);
2364
                }
2365
                if (--work_budget < 0) {
2366
                        if (tulip_debug > 1)
2367
                                printk(KERN_WARNING "%s: Too much work during an interrupt, "
2368
                                           "csr5=0x%8.8x.\n", dev->name, csr5);
2369
                        /* Acknowledge all interrupt sources. */
2370
                        outl(0x8001ffff, ioaddr + CSR5);
2371
#ifdef notdef
2372
                        /* Clear all but standard interrupt sources. */
2373
                        outl((~csr5) & 0x0001ebef, ioaddr + CSR7);
2374
#endif
2375
                        break;
2376
                }
2377
        } while (1);
2378
 
2379
        if (tulip_debug > 3)
2380
                printk(KERN_DEBUG "%s: exiting interrupt, csr5=%#4.4x.\n",
2381
                           dev->name, inl(ioaddr + CSR5));
2382
 
2383
#if defined(__i386__)
2384
        clear_bit(0, (void*)&dev->interrupt);
2385
#else
2386
        dev->interrupt = 0;
2387
#endif
2388
        return;
2389
}
2390
 
2391
static int
2392
tulip_rx(struct device *dev)
2393
{
2394
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2395
        int entry = tp->cur_rx % RX_RING_SIZE;
2396
        int rx_work_limit = tp->dirty_rx + RX_RING_SIZE - tp->cur_rx;
2397
        int work_done = 0;
2398
 
2399
        if (tulip_debug > 4)
2400
                printk(KERN_DEBUG " In tulip_rx(), entry %d %8.8x.\n", entry,
2401
                           tp->rx_ring[entry].status);
2402
        /* If we own the next entry, it's a new packet. Send it up. */
2403
        while (tp->rx_ring[entry].status >= 0) {
2404
                s32 status = tp->rx_ring[entry].status;
2405
 
2406
                if (--rx_work_limit < 0)
2407
                        break;
2408
                if ((status & 0x38008300) != 0x0300) {
2409
                        if ((status & 0x38000300) != 0x0300) {
2410
                                /* Ingore earlier buffers. */
2411
                                if ((status & 0xffff) != 0x7fff) {
2412
                                        if (tulip_debug > 1)
2413
                                                printk(KERN_WARNING "%s: Oversized Ethernet frame "
2414
                                                           "spanned multiple buffers, status %8.8x!\n",
2415
                                                           dev->name, status);
2416
                                        tp->stats.rx_length_errors++;
2417
                                }
2418
                        } else if (status & 0x8000) {
2419
                                /* There was a fatal error. */
2420
                                if (tulip_debug > 2)
2421
                                        printk(KERN_DEBUG "%s: Receive error, Rx status %8.8x.\n",
2422
                                                   dev->name, status);
2423
                                tp->stats.rx_errors++; /* end of a packet.*/
2424
                                if (status & 0x0890) tp->stats.rx_length_errors++;
2425
                                if (status & 0x0004) tp->stats.rx_frame_errors++;
2426
                                if (status & 0x0002) tp->stats.rx_crc_errors++;
2427
                                if (status & 0x0001) tp->stats.rx_fifo_errors++;
2428
                        }
2429
                } else {
2430
                        /* Omit the four octet CRC from the length. */
2431
                        short pkt_len = ((status >> 16) & 0x7ff) - 4;
2432
                        struct sk_buff *skb;
2433
 
2434
#ifndef final_version
2435
                        if (pkt_len > 1518) {
2436
                                printk("%s: Bogus packet size of %d (%#x).\n",
2437
                                           dev->name, pkt_len, pkt_len);
2438
                                pkt_len = 1518;
2439
                                tp->stats.rx_length_errors++;
2440
                        }
2441
#endif
2442
                        /* Check if the packet is long enough to accept without copying
2443
                           to a minimally-sized skbuff. */
2444
                        if (pkt_len < rx_copybreak
2445
                                && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
2446
                                skb->dev = dev;
2447
                                skb_reserve(skb, 2);    /* 16 byte align the IP header */
2448
#if ! defined(__alpha__)
2449
                                eth_copy_and_sum(skb, bus_to_virt(tp->rx_ring[entry].buffer1),
2450
                                                                 pkt_len, 0);
2451
                                skb_put(skb, pkt_len);
2452
#else
2453
                                memcpy(skb_put(skb, pkt_len),
2454
                                           bus_to_virt(tp->rx_ring[entry].buffer1), pkt_len);
2455
#endif
2456
                                work_done++;
2457
                        } else {        /* Pass up the skb already on the Rx ring. */
2458
                                char *temp = skb_put(skb = tp->rx_skbuff[entry], pkt_len);
2459
                                tp->rx_skbuff[entry] = NULL;
2460
#ifndef final_version
2461
                                if (bus_to_virt(tp->rx_ring[entry].buffer1) != temp)
2462
                                        printk(KERN_ERR "%s: Internal fault: The skbuff addresses "
2463
                                                   "do not match in tulip_rx: %p vs. %p / %p.\n",
2464
                                                   dev->name, bus_to_virt(tp->rx_ring[entry].buffer1),
2465
                                                   skb->head, temp);
2466
#endif
2467
                        }
2468
                        skb->protocol = eth_type_trans(skb, dev);
2469
                        netif_rx(skb);
2470
                        dev->last_rx = jiffies;
2471
                        tp->stats.rx_packets++;
2472
#if LINUX_VERSION_CODE > 0x20127
2473
                        tp->stats.rx_bytes += pkt_len;
2474
#endif
2475
                }
2476
                entry = (++tp->cur_rx) % RX_RING_SIZE;
2477
        }
2478
 
2479
        /* Refill the Rx ring buffers. */
2480
        for (; tp->cur_rx - tp->dirty_rx > 0; tp->dirty_rx++) {
2481
                entry = tp->dirty_rx % RX_RING_SIZE;
2482
                if (tp->rx_skbuff[entry] == NULL) {
2483
                        struct sk_buff *skb;
2484
                        skb = tp->rx_skbuff[entry] = dev_alloc_skb(PKT_BUF_SZ);
2485
                        if (skb == NULL)
2486
                                break;
2487
                        skb->dev = dev;                 /* Mark as being used by this device. */
2488
                        tp->rx_ring[entry].buffer1 = virt_to_bus(skb->tail);
2489
                        work_done++;
2490
                }
2491
                tp->rx_ring[entry].status = 0x80000000;
2492
        }
2493
 
2494
        return work_done;
2495
}
2496
 
2497
static int
2498
tulip_close(struct device *dev)
2499
{
2500
        long ioaddr = dev->base_addr;
2501
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2502
        int i;
2503
 
2504
        dev->start = 0;
2505
        dev->tbusy = 1;
2506
 
2507
        if (tulip_debug > 1)
2508
                printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
2509
                           dev->name, inl(ioaddr + CSR5));
2510
 
2511
        /* Disable interrupts by clearing the interrupt mask. */
2512
        outl(0x00000000, ioaddr + CSR7);
2513
        /* Stop the chip's Tx and Rx processes. */
2514
        outl(inl(ioaddr + CSR6) & ~0x2002, ioaddr + CSR6);
2515
        /* 21040 -- Leave the card in 10baseT state. */
2516
        if (tp->chip_id == DC21040)
2517
                outl(0x00000004, ioaddr + CSR13);
2518
 
2519
        if (inl(ioaddr + CSR6) != 0xffffffff)
2520
                tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2521
 
2522
        del_timer(&tp->timer);
2523
 
2524
        free_irq(dev->irq, dev);
2525
 
2526
        dev->if_port = tp->saved_if_port;
2527
 
2528
        /* Free all the skbuffs in the Rx queue. */
2529
        for (i = 0; i < RX_RING_SIZE; i++) {
2530
                struct sk_buff *skb = tp->rx_skbuff[i];
2531
                tp->rx_skbuff[i] = 0;
2532
                tp->rx_ring[i].status = 0;               /* Not owned by Tulip chip. */
2533
                tp->rx_ring[i].length = 0;
2534
                tp->rx_ring[i].buffer1 = 0xBADF00D0; /* An invalid address. */
2535
                if (skb) {
2536
#if LINUX_VERSION_CODE < 0x20100
2537
                        skb->free = 1;
2538
#endif
2539
                        dev_free_skb(skb);
2540
                }
2541
        }
2542
        for (i = 0; i < TX_RING_SIZE; i++) {
2543
                if (tp->tx_skbuff[i])
2544
                        dev_free_skb(tp->tx_skbuff[i]);
2545
                tp->tx_skbuff[i] = 0;
2546
        }
2547
 
2548
 
2549
        MOD_DEC_USE_COUNT;
2550
 
2551
        return 0;
2552
}
2553
 
2554
static struct net_device_stats *tulip_get_stats(struct device *dev)
2555
{
2556
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2557
        long ioaddr = dev->base_addr;
2558
 
2559
        if (dev->start)
2560
                tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2561
 
2562
        return &tp->stats;
2563
}
2564
 
2565
#ifdef HAVE_PRIVATE_IOCTL
2566
/* Provide ioctl() calls to examine the MII xcvr state. */
2567
static int private_ioctl(struct device *dev, struct ifreq *rq, int cmd)
2568
{
2569
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2570
        long ioaddr = dev->base_addr;
2571
        u16 *data = (u16 *)&rq->ifr_data;
2572
        int phy = tp->phys[0] & 0x1f;
2573
        long flags;
2574
 
2575
        switch(cmd) {
2576
        case SIOCDEVPRIVATE:            /* Get the address of the PHY in use. */
2577
                if (tp->mii_cnt)
2578
                        data[0] = phy;
2579
                else if (tp->chip_id == DC21142)
2580
                        data[0] = 32;
2581
                else
2582
                        return -ENODEV;
2583
                return 0;
2584
        case SIOCDEVPRIVATE+1:          /* Read the specified MII register. */
2585
                if (data[0] == 32) {  /* 21142 pseudo-MII */
2586
                        int csr12 = inl(ioaddr + CSR12);
2587
                        int csr14 = inl(ioaddr + CSR14);
2588
                        switch (data[1]) {
2589
                        case 0: {
2590
                                data[3] = ((csr14<<13)&0x4000) + ((csr14<<5)&0x1000);
2591
                                break; }
2592
                        case 1:
2593
                                data[3] = 0x7848 + ((csr12&0x7000) == 0x5000 ? 0x20 : 0)
2594
                                        + (csr12&0x06 ? 0x04 : 0);
2595
                                break;
2596
                        case 4: {
2597
                                data[3] = ((csr14>>9)&0x0380) +
2598
                                        ((inl(ioaddr + CSR6)>>3)&0x0040) +((csr14>>1)&0x20) + 1;
2599
                                break;
2600
                        }
2601
                        case 5: data[3] = csr12 >> 16; break;
2602
                        default: data[3] = 0; break;
2603
                        }
2604
                } else {
2605
                        save_flags(flags);
2606
                        cli();
2607
                        data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
2608
                        restore_flags(flags);
2609
                }
2610
                return 0;
2611
        case SIOCDEVPRIVATE+2:          /* Write the specified MII register */
2612
                if (!suser())
2613
                        return -EPERM;
2614
                if (data[0] == 32) {  /* 21142 pseudo-MII */
2615
                        if (data[1] == 5)
2616
                                tp->advertising[tp->mii_cnt] = data[2];
2617
                } else {
2618
                        save_flags(flags);
2619
                        cli();
2620
                        mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
2621
                        restore_flags(flags);
2622
                }
2623
                return 0;
2624
        default:
2625
                return -EOPNOTSUPP;
2626
        }
2627
 
2628
        return -EOPNOTSUPP;
2629
}
2630
#endif  /* HAVE_PRIVATE_IOCTL */
2631
 
2632
/* Set or clear the multicast filter for this adaptor.
2633
   Note that we only use exclusion around actually queueing the
2634
   new frame, not around filling tp->setup_frame.  This is non-deterministic
2635
   when re-entered but still correct. */
2636
 
2637
/* The little-endian AUTODIN32 ethernet CRC calculation.
2638
   N.B. Do not use for bulk data, use a table-based routine instead.
2639
   This is common code and should be moved to net/core/crc.c */
2640
static unsigned const ethernet_polynomial_le = 0xedb88320U;
2641
static inline unsigned ether_crc_le(int length, unsigned char *data)
2642
{
2643
        unsigned int crc = 0xffffffff;  /* Initial value. */
2644
        while(--length >= 0) {
2645
                unsigned char current_octet = *data++;
2646
                int bit;
2647
                for (bit = 8; --bit >= 0; current_octet >>= 1) {
2648
                        if ((crc ^ current_octet) & 1) {
2649
                                crc >>= 1;
2650
                                crc ^= ethernet_polynomial_le;
2651
                        } else
2652
                                crc >>= 1;
2653
                }
2654
        }
2655
        return crc;
2656
}
2657
 
2658
static void set_rx_mode(struct device *dev)
2659
{
2660
        long ioaddr = dev->base_addr;
2661
        int csr6 = inl(ioaddr + CSR6) & ~0x00D5;
2662
        struct tulip_private *tp = (struct tulip_private *)dev->priv;
2663
 
2664
        tp->csr6 &= ~0x00D5;
2665
        if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
2666
                outl(csr6 | 0x00C0, ioaddr + CSR6);
2667
                /* Unconditionally log net taps. */
2668
                printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
2669
                tp->csr6 |= 0xC0;
2670
        } else if ((dev->mc_count > 1000)  ||  (dev->flags & IFF_ALLMULTI)) {
2671
                /* Too many to filter perfectly -- accept all multicasts. */
2672
                outl(csr6 | 0x0080, ioaddr + CSR6);
2673
                tp->csr6 |= 0x80;
2674
        } else {
2675
                u32 *setup_frm = tp->setup_frame;
2676
                struct dev_mc_list *mclist;
2677
                u16 *eaddrs;
2678
                u32 tx_flags;
2679
                int i;
2680
 
2681
                if (dev->mc_count > 14) { /* Must use a multicast hash table. */
2682
                        u16 hash_table[32];
2683
                        memset(hash_table, 0, sizeof(hash_table));
2684
                        /* This should work on big-endian machines as well. */
2685
                        for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2686
                                 i++, mclist = mclist->next)
2687
                                set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff,
2688
                                                hash_table);
2689
                        /* Copy the hash table to the setup frame.
2690
                           NOTE that only the LOW SHORTWORD of setup_frame[] is valid! */
2691
                        for (i = 0; i < 32; i++)
2692
                                *setup_frm++ = hash_table[i];
2693
                        setup_frm += 7;
2694
                        tx_flags = 0x08400000 | 192;
2695
                        /* Too clever: i > 15 for fall-though. */
2696
                } else {
2697
                        /* We have <= 15 addresses so we can use the wonderful
2698
                           16 address perfect filtering of the Tulip. */
2699
                        for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
2700
                                 i++, mclist = mclist->next) {
2701
                                /* Note that only the low shortword of setup_frame[] is valid!
2702
                                   This code may require tweaking for non-x86 architectures! */
2703
                                eaddrs = (u16 *)mclist->dmi_addr;
2704
                                *setup_frm++ = *eaddrs++;
2705
                                *setup_frm++ = *eaddrs++;
2706
                                *setup_frm++ = *eaddrs++;
2707
                        }
2708
                        /* Fill the rest of the table with our physical address.
2709
                           Once again, only the low shortword or setup_frame[] is valid! */
2710
                        *setup_frm++ = 0xffff;
2711
                        *setup_frm++ = 0xffff;
2712
                        *setup_frm++ = 0xffff;
2713
                        tx_flags = 0x08000000 | 192;
2714
                }
2715
                eaddrs = (u16 *)dev->dev_addr;
2716
                do {
2717
                        *setup_frm++ = eaddrs[0];
2718
                        *setup_frm++ = eaddrs[1];
2719
                        *setup_frm++ = eaddrs[2];
2720
                } while (++i < 15);
2721
                /* Now add this frame to the Tx list. */
2722
                if (tp->cur_tx - tp->dirty_tx > TX_RING_SIZE - 2) {
2723
                        /* Same setup recently queued, we need not add it. */
2724
                } else {
2725
                        unsigned long flags;
2726
                        unsigned int entry;
2727
 
2728
                        save_flags(flags); cli();
2729
                        entry = tp->cur_tx++ % TX_RING_SIZE;
2730
 
2731
                        if (entry != 0) {
2732
                                /* Avoid a chip errata by prefixing a dummy entry. */
2733
                                tp->tx_skbuff[entry] = 0;
2734
                                tp->tx_ring[entry].length =
2735
                                        (entry == TX_RING_SIZE-1) ? DESC_RING_WRAP : 0;
2736
                                tp->tx_ring[entry].buffer1 = 0;
2737
                                tp->tx_ring[entry].status = 0x80000000;
2738
                                entry = tp->cur_tx++ % TX_RING_SIZE;
2739
                        }
2740
 
2741
                        tp->tx_skbuff[entry] = 0;
2742
                        /* Put the setup frame on the Tx list. */
2743
                        if (entry == TX_RING_SIZE-1)
2744
                                tx_flags |= DESC_RING_WRAP;             /* Wrap ring. */
2745
                        tp->tx_ring[entry].length = tx_flags;
2746
                        tp->tx_ring[entry].buffer1 = virt_to_bus(tp->setup_frame);
2747
                        tp->tx_ring[entry].status = 0x80000000;
2748
                        if (tp->cur_tx - tp->dirty_tx >= TX_RING_SIZE - 2) {
2749
                                dev->tbusy = 1;
2750
                                tp->tx_full = 1;
2751
                        }
2752
                        restore_flags(flags);
2753
                        /* Trigger an immediate transmit demand. */
2754
                        outl(0, ioaddr + CSR1);
2755
                }
2756
                outl(csr6 | 0x0000, ioaddr + CSR6);
2757
        }
2758
}
2759
 
2760
#ifdef CARDBUS
2761
 
2762
#include <pcmcia/driver_ops.h>
2763
 
2764
static dev_node_t *tulip_attach(dev_locator_t *loc)
2765
{
2766
        struct device *dev;
2767
        u16 dev_id;
2768
        u32 io;
2769
        u8 bus, devfn, irq;
2770
 
2771
        if (loc->bus != LOC_PCI) return NULL;
2772
        bus = loc->b.pci.bus; devfn = loc->b.pci.devfn;
2773
        printk(KERN_INFO "tulip_attach(bus %d, function %d)\n", bus, devfn);
2774
        pcibios_read_config_dword(bus, devfn, PCI_BASE_ADDRESS_0, &io);
2775
        pcibios_read_config_word(bus, devfn, PCI_DEVICE_ID, &dev_id);
2776
        pcibios_read_config_byte(bus, devfn, PCI_INTERRUPT_LINE, &irq);
2777
        dev = tulip_probe1(bus, devfn, NULL, io & ~3, irq, DC21142, -1);
2778
        if (dev) {
2779
                dev_node_t *node = kmalloc(sizeof(dev_node_t), GFP_KERNEL);
2780
                strcpy(node->dev_name, dev->name);
2781
                node->major = node->minor = 0;
2782
                node->next = NULL;
2783
                MOD_INC_USE_COUNT;
2784
                return node;
2785
        }
2786
        return NULL;
2787
}
2788
 
2789
static void tulip_suspend(dev_node_t *node)
2790
{
2791
        struct device **devp, **next;
2792
        printk(KERN_INFO "tulip_suspend(%s)\n", node->dev_name);
2793
        for (devp = &root_tulip_dev; *devp; devp = next) {
2794
                next = &((struct tulip_private *)(*devp)->priv)->next_module;
2795
                if (strcmp((*devp)->name, node->dev_name) == 0) break;
2796
        }
2797
        if (*devp) {
2798
                struct tulip_private *tp = (struct tulip_private *)(*devp)->priv;
2799
                tulip_close(*devp);
2800
                /* Put the chip into sleep mode. */
2801
                pcibios_write_config_dword(tp->pci_bus,tp->pci_devfn, 0x40,0x80000000);
2802
        }
2803
}
2804
 
2805
static void tulip_resume(dev_node_t *node)
2806
{
2807
        struct device **devp, **next;
2808
        printk(KERN_INFO "tulip_resume(%s)\n", node->dev_name);
2809
        for (devp = &root_tulip_dev; *devp; devp = next) {
2810
                next = &((struct tulip_private *)(*devp)->priv)->next_module;
2811
                if (strcmp((*devp)->name, node->dev_name) == 0) break;
2812
        }
2813
        if (*devp) {
2814
                struct tulip_private *tp = (struct tulip_private *)(*devp)->priv;
2815
                pcibios_write_config_dword(tp->pci_bus, tp->pci_devfn, 0x40, 0x0000);
2816
                tulip_open(*devp);
2817
        }
2818
}
2819
 
2820
static void tulip_detach(dev_node_t *node)
2821
{
2822
        struct device **devp, **next;
2823
        printk(KERN_INFO "tulip_detach(%s)\n", node->dev_name);
2824
        for (devp = &root_tulip_dev; *devp; devp = next) {
2825
                next = &((struct tulip_private *)(*devp)->priv)->next_module;
2826
                if (strcmp((*devp)->name, node->dev_name) == 0) break;
2827
        }
2828
        if (*devp) {
2829
                unregister_netdev(*devp);
2830
                kfree(*devp);
2831
                *devp = *next;
2832
                kfree(node);
2833
                MOD_DEC_USE_COUNT;
2834
        }
2835
}
2836
 
2837
struct driver_operations tulip_ops = {
2838
        "tulip_cb", tulip_attach, tulip_suspend, tulip_resume, tulip_detach
2839
};
2840
 
2841
#endif  /* Cardbus support */
2842
 
2843
 
2844
#ifdef MODULE
2845
int init_module(void)
2846
{
2847
#ifdef CARDBUS
2848
        reverse_probe = 0;                       /* Not used. */
2849
        register_driver(&tulip_ops);
2850
        return 0;
2851
#else
2852
        return tulip_probe(NULL);
2853
#endif
2854
}
2855
 
2856
void cleanup_module(void)
2857
{
2858
        struct device *next_dev;
2859
 
2860
#ifdef CARDBUS
2861
        unregister_driver(&tulip_ops);
2862
#endif
2863
 
2864
        /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
2865
        while (root_tulip_dev) {
2866
                struct tulip_private *tp = (struct tulip_private *)root_tulip_dev->priv;
2867
                next_dev = tp->next_module;
2868
                unregister_netdev(root_tulip_dev);
2869
                release_region(root_tulip_dev->base_addr,
2870
                                           tulip_tbl[tp->chip_id].io_size);
2871
                kfree(root_tulip_dev);
2872
                root_tulip_dev = next_dev;
2873
        }
2874
}
2875
 
2876
#endif  /* MODULE */
2877
 
2878
/*
2879
 * Local variables:
2880
 *  SMP-compile-command: "gcc -D__SMP__ -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c tulip.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
2881
 *  compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c tulip.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
2882
 *  cardbus-compile-command: "gcc -DCARDBUS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c tulip.c -o tulip_cb.o -I/usr/src/pcmcia-cs-3.0.5/include/"
2883
 *  c-indent-level: 4
2884
 *  c-basic-offset: 4
2885
 *  tab-width: 4
2886
 * End:
2887
 */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.