o Disable the debug output that Kanoj disabled that I enabled
[linux-2.6/linux-mips.git] / drivers / net / pcmcia / tulip_cb.c
blob38dcdc0c1173c9f3a76a7093425d58afb7a084ac
1 /* tulip.c: A DEC 21040-family ethernet driver for Linux. */
2 /*
3 Written/copyright 1994-1999 by Donald Becker.
5 This software may be used and distributed according to the terms
6 of the GNU Public License, incorporated herein by reference.
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.
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
16 Support and updates available at
17 http://cesdis.gsfc.nasa.gov/linux/drivers/tulip.html
20 #define SMP_CHECK
21 static const char version[] = "tulip.c:v0.91 4/14/99 becker@cesdis.gsfc.nasa.gov (modified by danilo@cs.uni-magdeburg.de for XIRCOM CBE, fixed by Doug Ledford)\n";
23 /* A few user-configurable values. */
25 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
26 static int max_interrupt_work = 25;
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. */
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",
42 /* Keep the ring sizes a power of two for efficiency.
43 Making the Tx ring too large decreases the effectiveness of channel
44 bonding and packet priority.
45 There are no ill effects from too-large receive rings. */
46 #define TX_RING_SIZE 16
47 #define RX_RING_SIZE 32
49 /* Set the copy breakpoint for the copy-only-tiny-buffer Rx structure. */
50 #ifdef __alpha__
51 static int rx_copybreak = 1518;
52 #else
53 static int rx_copybreak = 100;
54 #endif
57 Set the bus performance register.
58 Typical: Set 16 longword cache alignment, no burst limit.
59 Cache alignment bits 15:14 Burst length 13:8
60 0000 No alignment 0x00000000 unlimited 0800 8 longwords
61 4000 8 longwords 0100 1 longword 1000 16 longwords
62 8000 16 longwords 0200 2 longwords 2000 32 longwords
63 C000 32 longwords 0400 4 longwords
64 Warning: many older 486 systems are broken and require setting 0x00A04800
65 8 longword cache alignment, 8 longword burst.
66 ToDo: Non-Intel setting could be better.
69 #if defined(__alpha__)
70 static int csr0 = 0x01A00000 | 0xE000;
71 #elif defined(__powerpc__)
72 static int csr0 = 0x01B00000 | 0x8000;
73 #elif defined(__sparc__)
74 static int csr0 = 0x01B00080 | 0x8000;
75 #elif defined(__i386__)
76 static int csr0 = 0x01A00000 | 0x8000;
77 #else
78 #warning Processor architecture undefined!
79 static int csr0 = 0x00A00000 | 0x4800;
80 #endif
82 /* Operational parameters that usually are not changed. */
83 /* Time in jiffies before concluding the transmitter is hung. */
84 #define TX_TIMEOUT (4*HZ)
85 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
86 /* This is a mysterious value that can be written to CSR11 in the 21040 (only)
87 to support a pre-NWay full-duplex signaling mechanism using short frames.
88 No one knows what it should be, but if left at its default value some
89 10base2(!) packets trigger a full-duplex-request interrupt. */
90 #define FULL_DUPLEX_MAGIC 0x6969
92 #if !defined(__OPTIMIZE__) || !defined(__KERNEL__)
93 #warning You must compile this file with the correct options!
94 #warning See the last lines of the source file.
95 #error You must compile this driver with "-O".
96 #endif
98 #include <linux/version.h>
99 #include <linux/module.h>
100 #include <linux/kernel.h>
101 #include <linux/sched.h>
102 #include <linux/string.h>
103 #include <linux/timer.h>
104 #include <linux/errno.h>
105 #include <linux/ioport.h>
106 #include <linux/malloc.h>
107 #include <linux/interrupt.h>
108 #include <linux/pci.h>
109 #include <linux/netdevice.h>
110 #include <linux/etherdevice.h>
111 #include <linux/skbuff.h>
112 #include <linux/delay.h>
113 #include <linux/init.h>
114 #include <asm/processor.h> /* Processor type for cache alignment. */
115 #include <asm/bitops.h>
116 #include <asm/io.h>
117 #include <asm/unaligned.h>
119 /* Kernel compatibility defines, some common to David Hinds' PCMCIA package.
120 This is only in the support-all-kernels source code. */
122 MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");
123 MODULE_DESCRIPTION("Digital 21*4* Tulip ethernet driver");
124 MODULE_PARM(debug, "i");
125 MODULE_PARM(max_interrupt_work, "i");
126 MODULE_PARM(reverse_probe, "i");
127 MODULE_PARM(rx_copybreak, "i");
128 MODULE_PARM(csr0, "i");
129 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
130 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
132 #define RUN_AT(x) (jiffies + (x))
134 #define tulip_debug debug
135 #ifdef TULIP_DEBUG
136 static int tulip_debug = TULIP_DEBUG;
137 #else
138 static int tulip_debug = 1;
139 #endif
142 Theory of Operation
144 I. Board Compatibility
146 This device driver is designed for the DECchip "Tulip", Digital's
147 single-chip ethernet controllers for PCI. Supported members of the family
148 are the 21040, 21041, 21140, 21140A, 21142, and 21143. Similar work-alike
149 chips from Lite-On, Macronics, ASIX, Compex and other listed below are also
150 supported.
152 These chips are used on at least 140 unique PCI board designs. The great
153 number of chips and board designs supported is the reason for the
154 driver size and complexity. Almost of the increasing complexity is in the
155 board configuration and media selection code. There is very little
156 increasing in the operational critical path length.
158 II. Board-specific settings
160 PCI bus devices are configured by the system at boot time, so no jumpers
161 need to be set on the board. The system BIOS preferably should assign the
162 PCI INTA signal to an otherwise unused system IRQ line.
164 Some boards have EEPROMs tables with default media entry. The factory default
165 is usually "autoselect". This should only be overridden when using
166 transceiver connections without link beat e.g. 10base2 or AUI, or (rarely!)
167 for forcing full-duplex when used with old link partners that do not do
168 autonegotiation.
170 III. Driver operation
172 IIIa. Ring buffers
174 The Tulip can use either ring buffers or lists of Tx and Rx descriptors.
175 This driver uses statically allocated rings of Rx and Tx descriptors, set at
176 compile time by RX/TX_RING_SIZE. This version of the driver allocates skbuffs
177 for the Rx ring buffers at open() time and passes the skb->data field to the
178 Tulip as receive data buffers. When an incoming frame is less than
179 RX_COPYBREAK bytes long, a fresh skbuff is allocated and the frame is
180 copied to the new skbuff. When the incoming frame is larger, the skbuff is
181 passed directly up the protocol stack and replaced by a newly allocated
182 skbuff.
184 The RX_COPYBREAK value is chosen to trade-off the memory wasted by
185 using a full-sized skbuff for small frames vs. the copying costs of larger
186 frames. For small frames the copying cost is negligible (esp. considering
187 that we are pre-loading the cache with immediately useful header
188 information). For large frames the copying cost is non-trivial, and the
189 larger copy might flush the cache of useful data. A subtle aspect of this
190 choice is that the Tulip only receives into longword aligned buffers, thus
191 the IP header at offset 14 isn't longword aligned for further processing.
192 Copied frames are put into the new skbuff at an offset of "+2", thus copying
193 has the beneficial effect of aligning the IP header and preloading the
194 cache.
196 IIIC. Synchronization
197 The driver runs as two independent, single-threaded flows of control. One
198 is the send-packet routine, which enforces single-threaded use by the
199 dev->tbusy flag. The other thread is the interrupt handler, which is single
200 threaded by the hardware and other software.
202 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
203 flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
204 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
205 the 'tp->tx_full' flag.
207 The interrupt handler has exclusive control over the Rx ring and records stats
208 from the Tx ring. (The Tx-done interrupt can't be selectively turned off, so
209 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
210 stats.) After reaping the stats, it marks the queue entry as empty by setting
211 the 'base' to zero. Iff the 'tp->tx_full' flag is set, it clears both the
212 tx_full and tbusy flags.
214 IV. Notes
216 Thanks to Duke Kamstra of SMC for long ago providing an EtherPower board.
217 Greg LaPolla at Linksys provided PNIC and other Linksys boards.
218 Znyx provided a four-port card for testing.
220 IVb. References
222 http://cesdis.gsfc.nasa.gov/linux/misc/NWay.html
223 http://www.digital.com (search for current 21*4* datasheets and "21X4 SROM")
224 http://www.national.com/pf/DP/DP83840A.html
225 http://www.asix.com.tw/pmac.htm
226 http://www.admtek.com.tw/
228 IVc. Errata
230 The old DEC databooks were light on details.
231 The 21040 databook claims that CSR13, CSR14, and CSR15 should each be the last
232 register of the set CSR12-15 written. Hmmm, now how is that possible?
234 The DEC SROM format is very badly designed not precisely defined, leading to
235 part of the media selection junkheap below. Some boards do not have EEPROM
236 media tables and need to be patched up. Worse, other boards use the DEC
237 design kit media table when it isn't correct for their board.
239 We cannot use MII interrupts because there is no defined GPIO pin to attach
240 them. The MII transceiver status is polled using an kernel timer.
244 /* This table use during operation for capabilities and media timer. */
246 static void tulip_timer(unsigned long data);
247 static void t21142_timer(unsigned long data);
248 static void mxic_timer(unsigned long data);
249 static void pnic_timer(unsigned long data);
250 static void comet_timer(unsigned long data);
252 enum tbl_flag {
253 HAS_MII=1, HAS_MEDIA_TABLE=2, CSR12_IN_SROM=4, ALWAYS_CHECK_MII=8,
254 HAS_ACPI=0x10, MC_HASH_ONLY=0x20, /* Hash-only multicast filter. */
255 HAS_NWAY143=0x40, /* Uses 21143-like internal NWay. */
257 static struct tulip_chip_table {
258 char *chip_name;
259 int io_size;
260 int valid_intrs; /* CSR7 interrupt enable settings */
261 int flags;
262 void (*media_timer)(unsigned long data);
263 } tulip_tbl[] = {
264 { "Digital DC21040 Tulip", 128, 0x0001ebef, 0, tulip_timer },
265 { "Digital DC21041 Tulip", 128, 0x0001ebef, HAS_MEDIA_TABLE, tulip_timer },
266 { "Digital DS21140 Tulip", 128, 0x0001ebef,
267 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, tulip_timer },
268 { "Digital DS21143 Tulip", 128, 0x0801fbff,
269 HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII | HAS_ACPI | HAS_NWAY143, t21142_timer },
270 { "Lite-On 82c168 PNIC", 256, 0x0001ebef,
271 HAS_MII, pnic_timer },
272 { "Macronix 98713 PMAC", 128, 0x0001ebef,
273 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, mxic_timer },
274 { "Macronix 98715 PMAC", 256, 0x0001ebef,
275 HAS_MEDIA_TABLE, mxic_timer },
276 { "Macronix 98725 PMAC", 256, 0x0001ebef,
277 HAS_MEDIA_TABLE, mxic_timer },
278 { "ASIX AX88140", 128, 0x0001fbff,
279 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM | MC_HASH_ONLY, tulip_timer },
280 { "Lite-On PNIC-II", 256, 0x0001ebef,
281 HAS_MII | HAS_NWAY143, pnic_timer },
282 { "ADMtek Comet", 256, 0x0001abef,
283 MC_HASH_ONLY, comet_timer },
284 { "Compex 9881 PMAC", 128, 0x0001ebef,
285 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, mxic_timer },
286 { "Xircom Cardbus Adapter (DEC 21143 compatible mode)", 128, 0x0801fbff,
287 HAS_MII | HAS_ACPI, tulip_timer },
288 {0},
290 /* This matches the table above. Note 21142 == 21143. */
291 enum chips {
292 DC21040=0, DC21041=1, DC21140=2, DC21142=3, DC21143=3,
293 LC82C168, MX98713, MX98715, MX98725, AX88140, PNIC2, COMET, COMPEX9881,
294 X3201_3,
297 /* A full-duplex map for media types. */
298 enum MediaIs {
299 MediaIsFD = 1, MediaAlwaysFD=2, MediaIsMII=4, MediaIsFx=8,
300 MediaIs100=16};
301 static const char media_cap[] =
302 {0,0,0,16, 3,19,16,24, 27,4,7,5, 0,20,23,20 };
303 static u8 t21040_csr13[] = {2,0x0C,8,4, 4,0,0,0, 0,0,0,0, 4,0,0,0};
304 /* 21041 transceiver register settings: 10-T, 10-2, AUI, 10-T, 10T-FD*/
305 static u16 t21041_csr13[] = { 0xEF05, 0xEF09, 0xEF09, 0xEF01, 0xEF09, };
306 static u16 t21041_csr14[] = { 0x7F3F, 0xF7FD, 0xF7FD, 0x7F3F, 0x7F3D, };
307 static u16 t21041_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
309 static u16 t21142_csr13[] = { 0x0001, 0x0009, 0x0009, 0x0000, 0x0001, };
310 static u16 t21142_csr14[] = { 0xFFFF, 0x0705, 0x0705, 0x0000, 0x7F3D, };
311 static u16 t21142_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
313 /* Offsets to the Command and Status Registers, "CSRs". All accesses
314 must be longword instructions and quadword aligned. */
315 enum tulip_offsets {
316 CSR0=0, CSR1=0x08, CSR2=0x10, CSR3=0x18, CSR4=0x20, CSR5=0x28,
317 CSR6=0x30, CSR7=0x38, CSR8=0x40, CSR9=0x48, CSR10=0x50, CSR11=0x58,
318 CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78 };
320 /* The bits in the CSR5 status registers, mostly interrupt sources. */
321 enum status_bits {
322 TimerInt=0x800, TPLnkFail=0x1000, TPLnkPass=0x10,
323 NormalIntr=0x10000, AbnormalIntr=0x8000,
324 RxJabber=0x200, RxDied=0x100, RxNoBuf=0x80, RxIntr=0x40,
325 TxFIFOUnderflow=0x20, TxJabber=0x08, TxNoBuf=0x04, TxDied=0x02, TxIntr=0x01,
328 /* The Tulip Rx and Tx buffer descriptors. */
329 struct tulip_rx_desc {
330 s32 status;
331 s32 length;
332 u32 buffer1, buffer2;
335 struct tulip_tx_desc {
336 s32 status;
337 s32 length;
338 u32 buffer1, buffer2; /* We use only buffer 1. */
341 enum desc_status_bits {
342 DescOwned=0x80000000, RxDescFatalErr=0x8000, RxWholePkt=0x0300,
345 /* Ring-wrap flag in length field, use for last ring entry.
346 0x01000000 means chain on buffer2 address,
347 0x02000000 means use the ring start address in CSR2/3.
348 Note: Some work-alike chips do not function correctly in chained mode.
349 The ASIX chip works only in chained mode.
350 Thus we indicates ring mode, but always write the 'next' field for
351 chained mode as well.
353 #define DESC_RING_WRAP 0x02000000
355 #ifdef CARDBUS
356 #define EEPROM_ADDRLEN (chip_rev == 65 ? 8 : 6)
357 #else
358 #define EEPROM_ADDRLEN 6
359 #endif
360 #define EEPROM_SIZE 128 /* 2 << EEPROM_ADDRLEN */
362 struct medialeaf {
363 u8 type;
364 u8 media;
365 unsigned char *leafdata;
368 struct mediatable {
369 u16 defaultmedia;
370 u8 leafcount, csr12dir; /* General purpose pin directions. */
371 unsigned has_mii:1, has_nonmii:1, has_reset:6;
372 u32 csr15dir, csr15val; /* 21143 NWay setting. */
373 struct medialeaf mleaf[0];
376 struct mediainfo {
377 struct mediainfo *next;
378 int info_type;
379 int index;
380 unsigned char *info;
383 struct tulip_private {
384 char devname[8]; /* Used only for kernel debugging. */
385 const char *product_name;
386 struct tulip_rx_desc rx_ring[RX_RING_SIZE];
387 struct tulip_tx_desc tx_ring[TX_RING_SIZE];
388 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
389 struct sk_buff* tx_skbuff[TX_RING_SIZE];
390 #ifdef CARDBUS
391 /* The X3201-3 requires double word aligned tx bufs */
392 struct sk_buff* tx_aligned_skbuff[TX_RING_SIZE];
393 #endif
394 /* The addresses of receive-in-place skbuffs. */
395 struct sk_buff* rx_skbuff[RX_RING_SIZE];
396 char *rx_buffs; /* Address of temporary Rx buffers. */
397 u8 setup_buf[96*sizeof(u16) + 7];
398 u16 *setup_frame; /* Pseudo-Tx frame to init address table. */
399 int chip_id;
400 int revision;
401 struct net_device_stats stats;
402 struct timer_list timer; /* Media selection timer. */
403 int interrupt; /* In-interrupt flag. */
404 unsigned int cur_rx, cur_tx; /* The next free ring entry */
405 unsigned int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
406 unsigned int tx_full:1; /* The Tx queue is full. */
407 unsigned int full_duplex:1; /* Full-duplex operation requested. */
408 unsigned int full_duplex_lock:1;
409 unsigned int fake_addr:1; /* Multiport board faked address. */
410 unsigned int default_port:4; /* Last dev->if_port value. */
411 unsigned int media2:4; /* Secondary monitored media port. */
412 unsigned int medialock:1; /* Don't sense media type. */
413 unsigned int mediasense:1; /* Media sensing in progress. */
414 unsigned int nway:1, nwayset:1; /* 21143 internal NWay. */
415 unsigned int open:1;
416 unsigned int csr0; /* CSR0 setting. */
417 unsigned int csr6; /* Current CSR6 control settings. */
418 unsigned char eeprom[EEPROM_SIZE]; /* Serial EEPROM contents. */
419 u16 to_advertise; /* NWay capabilities advertised. */
420 u16 lpar; /* 21143 Link partner ability. */
421 u16 advertising[4];
422 signed char phys[4], mii_cnt; /* MII device addresses. */
423 struct mediatable *mtable;
424 int cur_index; /* Current media index. */
425 int saved_if_port;
426 struct pci_dev *pdev;
427 spinlock_t lock;
428 int pad0, pad1; /* Used for 8-byte alignment */
431 static void parse_eeprom(struct net_device *dev);
432 static int read_eeprom(long ioaddr, int location, int addr_len);
433 static int mdio_read(struct net_device *dev, int phy_id, int location);
434 static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
435 static void select_media(struct net_device *dev, int startup);
436 static void tulip_up(struct net_device *dev);
437 static void tulip_down(struct net_device *dev);
438 static int tulip_open(struct net_device *dev);
439 static void tulip_timer(unsigned long data);
440 static void t21142_start_nway(struct net_device *dev);
441 static void tulip_tx_timeout(struct net_device *dev);
442 static void tulip_init_ring(struct net_device *dev);
443 static int tulip_start_xmit(struct sk_buff *skb, struct net_device *dev);
444 static int tulip_rx(struct net_device *dev);
445 static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
446 static int tulip_close(struct net_device *dev);
447 static struct net_device_stats *tulip_get_stats(struct net_device *dev);
448 #ifdef HAVE_PRIVATE_IOCTL
449 static int private_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
450 #endif
451 static void set_rx_mode(struct net_device *dev);
453 /* The Xircom cards are picky about when certain bits in CSR6 can be
454 manipulated. Keith Owens <kaos@ocs.com.au>. */
456 static void outl_CSR6 (u32 newcsr6, long ioaddr, int chip_idx)
458 const int strict_bits = 0x0060e202;
459 int csr5, csr5_22_20, csr5_19_17, currcsr6, attempts = 200;
460 long flags;
461 save_flags(flags);
462 cli();
463 if (chip_idx != X3201_3) {
464 outl(newcsr6, ioaddr + CSR6);
465 restore_flags(flags);
466 return;
468 newcsr6 &= 0x726cfeca; /* mask out the reserved CSR6 bits that always */
469 /* read 0 on the Xircom cards */
470 newcsr6 |= 0x320c0000; /* or in the reserved bits that always read 1 */
471 currcsr6 = inl(ioaddr + CSR6);
472 if (((newcsr6 & strict_bits) == (currcsr6 & strict_bits)) ||
473 ((currcsr6 & ~0x2002) == 0)) {
474 outl(newcsr6, ioaddr + CSR6); /* safe */
475 restore_flags(flags);
476 return;
478 /* make sure the transmitter and receiver are stopped first */
479 currcsr6 &= ~0x2002;
480 while (1) {
481 csr5 = inl(ioaddr + CSR5);
482 if (csr5 == 0xffffffff)
483 break; /* cannot read csr5, card removed? */
484 csr5_22_20 = csr5 & 0x700000;
485 csr5_19_17 = csr5 & 0x0e0000;
486 if ((csr5_22_20 == 0 || csr5_22_20 == 0x600000) &&
487 (csr5_19_17 == 0 || csr5_19_17 == 0x80000 || csr5_19_17 == 0xc0000))
488 break; /* both are stopped or suspended */
489 if (!--attempts) {
490 printk(KERN_INFO "tulip.c: outl_CSR6 too many attempts,"
491 "csr5=0x%08x\n", csr5);
492 outl(newcsr6, ioaddr + CSR6); /* unsafe but do it anyway */
493 restore_flags(flags);
494 return;
496 outl(currcsr6, ioaddr + CSR6);
497 udelay(1);
499 /* now it is safe to change csr6 */
500 outl(newcsr6, ioaddr + CSR6);
501 restore_flags(flags);
504 static struct net_device *tulip_probe1(struct pci_dev *pdev,
505 struct net_device *dev, long ioaddr, int irq,
506 int chip_idx, int board_idx)
508 static int did_version = 0; /* Already printed version info. */
509 struct tulip_private *tp;
510 /* See note below on the multiport cards. */
511 static unsigned char last_phys_addr[6] = {0x00, 'L', 'i', 'n', 'u', 'x'};
512 static int last_irq = 0;
513 static int multiport_cnt = 0; /* For four-port boards w/one EEPROM */
514 u8 chip_rev;
515 int i;
516 unsigned short sum;
518 if (tulip_debug > 0 && did_version++ == 0)
519 printk(KERN_INFO "%s", version);
521 dev = init_etherdev(dev, 0);
523 pci_read_config_byte(pdev, PCI_REVISION_ID, &chip_rev);
524 /* Bring the 21143 out of sleep mode.
525 Caution: Snooze mode does not work with some boards! */
526 if (tulip_tbl[chip_idx].flags & HAS_ACPI)
527 pci_write_config_dword(pdev, 0x40, 0x00000000);
529 printk(KERN_INFO "%s: %s rev %d at %#3lx,",
530 dev->name, tulip_tbl[chip_idx].chip_name, chip_rev, ioaddr);
532 /* Stop the chip's Tx and Rx processes. */
533 outl_CSR6(inl(ioaddr + CSR6) & ~0x2002, ioaddr, chip_idx);
534 /* Clear the missed-packet counter. */
535 (volatile int)inl(ioaddr + CSR8);
537 if (chip_idx == DC21041) {
538 if (inl(ioaddr + CSR9) & 0x8000) {
539 printk(" 21040 compatible mode,");
540 chip_idx = DC21040;
541 } else {
542 printk(" 21041 mode,");
546 /* The station address ROM is read byte serially. The register must
547 be polled, waiting for the value to be read bit serially from the
548 EEPROM.
550 sum = 0;
551 if (chip_idx == DC21040) {
552 outl(0, ioaddr + CSR9); /* Reset the pointer with a dummy write. */
553 for (i = 0; i < 6; i++) {
554 int value, boguscnt = 100000;
556 value = inl(ioaddr + CSR9);
557 while (value < 0 && --boguscnt > 0);
558 dev->dev_addr[i] = value;
559 sum += value & 0xff;
561 } else if (chip_idx == LC82C168) {
562 for (i = 0; i < 3; i++) {
563 int value, boguscnt = 100000;
564 outl(0x600 | i, ioaddr + 0x98);
566 value = inl(ioaddr + CSR9);
567 while (value < 0 && --boguscnt > 0);
568 put_unaligned(le16_to_cpu(value), ((u16*)dev->dev_addr) + i);
569 sum += value & 0xffff;
571 } else if (chip_idx == COMET) {
572 /* No need to read the EEPROM. */
573 put_unaligned(inl(ioaddr + 0xA4), (u32 *)dev->dev_addr);
574 put_unaligned(inl(ioaddr + 0xA8), (u16 *)(dev->dev_addr + 4));
575 for (i = 0; i < 6; i ++)
576 sum += dev->dev_addr[i];
577 } else if (chip_idx == X3201_3) {
578 /* Xircom has its address stored in the CIS
579 * we access it through the boot rom interface for now
580 * this might not work, as the CIS is not parsed but I
581 * (danilo) use the offset I found on my card's CIS !!!
583 * Doug Ledford: I changed this routine around so that it
584 * walks the CIS memory space, parsing the config items, and
585 * finds the proper lan_node_id tuple and uses the data
586 * stored there.
588 unsigned char j, tuple, link, data_id, data_count;
589 outl(1<<12, ioaddr + CSR9); /* enable boot rom access */
590 for (i = 0x100; i < 0x1f7; i += link+2) {
591 outl(i, ioaddr + CSR10);
592 tuple = inl(ioaddr + CSR9) & 0xff;
593 outl(i + 1, ioaddr + CSR10);
594 link = inl(ioaddr + CSR9) & 0xff;
595 outl(i + 2, ioaddr + CSR10);
596 data_id = inl(ioaddr + CSR9) & 0xff;
597 outl(i + 3, ioaddr + CSR10);
598 data_count = inl(ioaddr + CSR9) & 0xff;
599 if ( (tuple == 0x22) &&
600 (data_id == 0x04) && (data_count == 0x06) ) {
602 * This is it. We have the data we want.
604 for (j = 0; j < 6; j++) {
605 outl(i + j + 4, ioaddr + CSR10);
606 dev->dev_addr[j] = inl(ioaddr + CSR9) & 0xff;
608 break;
609 } else if (link == 0) {
610 break;
613 sum = 1; // to make check below fail!
614 } else { /* Must be a new chip, with a serial EEPROM interface. */
615 /* We read the whole EEPROM, and sort it out later. DEC has a
616 specification _Digital Semiconductor 21X4 Serial ROM Format_
617 but early vendor boards just put the address in the first six
618 EEPROM locations. */
619 unsigned char ee_data[EEPROM_SIZE];
620 int sa_offset = 0;
622 for (i = 0; i < sizeof(ee_data)/2; i++)
623 ((u16 *)ee_data)[i] =
624 le16_to_cpu(read_eeprom(ioaddr, i, EEPROM_ADDRLEN));
626 /* Detect the simple EEPROM format by the duplicated station addr. */
627 for (i = 0; i < 8; i ++)
628 if (ee_data[i] != ee_data[16+i])
629 sa_offset = 20;
630 if (ee_data[0] == 0xff && ee_data[1] == 0xff && ee_data[2] == 0) {
631 sa_offset = 2; /* Grrr, damn Matrox boards. */
632 multiport_cnt = 4;
634 for (i = 0; i < 6; i ++) {
635 dev->dev_addr[i] = ee_data[i + sa_offset];
636 sum += ee_data[i + sa_offset];
639 /* Lite-On boards have the address byte-swapped. */
640 if ((dev->dev_addr[0] == 0xA0 || dev->dev_addr[0] == 0xC0)
641 && dev->dev_addr[1] == 0x00)
642 for (i = 0; i < 6; i+=2) {
643 char tmp = dev->dev_addr[i];
644 dev->dev_addr[i] = dev->dev_addr[i+1];
645 dev->dev_addr[i+1] = tmp;
647 /* On the Zynx 315 Etherarray and other multiport boards only the
648 first Tulip has an EEPROM.
649 The addresses of the subsequent ports are derived from the first.
650 Many PCI BIOSes also incorrectly report the IRQ line, so we correct
651 that here as well. */
652 if (sum == 0 || sum == 6*0xff) {
653 printk(" EEPROM not present,");
654 for (i = 0; i < 5; i++)
655 dev->dev_addr[i] = last_phys_addr[i];
656 dev->dev_addr[i] = last_phys_addr[i] + 1;
657 #if defined(__i386__) /* Patch up x86 BIOS bug. */
658 if (last_irq)
659 irq = last_irq;
660 #endif
663 for (i = 0; i < 6; i++)
664 printk("%c%2.2X", i ? ':' : ' ', last_phys_addr[i] = dev->dev_addr[i]);
665 printk(", IRQ %d.\n", irq);
666 last_irq = irq;
668 /* We do a request_region() only to register /proc/ioports info. */
669 /* Note that proper size is tulip_tbl[chip_idx].chip_name, but... */
670 request_region(ioaddr, tulip_tbl[chip_idx].io_size, dev->name);
672 dev->base_addr = ioaddr;
673 dev->irq = irq;
675 /* Make certain the data structures are quadword aligned. */
676 tp = (void *)(((long)kmalloc(sizeof(*tp), GFP_KERNEL | GFP_DMA) + 7) & ~7);
677 memset(tp, 0, sizeof(*tp));
678 dev->priv = tp;
680 tp->lock = SPIN_LOCK_UNLOCKED;
681 tp->pdev = pdev;
682 tp->chip_id = chip_idx;
683 tp->revision = chip_rev;
684 tp->csr0 = csr0;
685 tp->setup_frame = (u16 *)(((unsigned long)tp->setup_buf + 7) & ~7);
687 /* BugFixes: The 21143-TD hangs with PCI Write-and-Invalidate cycles.
688 And the ASIX must have a burst limit or horrible things happen. */
689 if ( (chip_idx == DC21143 && chip_rev == 65) ||
690 (chip_idx == X3201_3) )
691 tp->csr0 &= ~0x01000000;
692 else if (chip_idx == AX88140)
693 tp->csr0 |= 0x2000;
695 #ifdef TULIP_FULL_DUPLEX
696 tp->full_duplex = 1;
697 tp->full_duplex_lock = 1;
698 #endif
699 #ifdef TULIP_DEFAULT_MEDIA
700 tp->default_port = TULIP_DEFAULT_MEDIA;
701 #endif
702 #ifdef TULIP_NO_MEDIA_SWITCH
703 tp->medialock = 1;
704 #endif
706 /* The lower four bits are the media type. */
707 if (board_idx >= 0 && board_idx < MAX_UNITS) {
708 tp->default_port = options[board_idx] & 15;
709 if ((options[board_idx] & 0x90) || full_duplex[board_idx] > 0)
710 tp->full_duplex = 1;
711 if (mtu[board_idx] > 0)
712 dev->mtu = mtu[board_idx];
714 if (dev->mem_start)
715 tp->default_port = dev->mem_start;
716 if (tp->default_port) {
717 tp->medialock = 1;
718 if (media_cap[tp->default_port] & MediaAlwaysFD)
719 tp->full_duplex = 1;
721 if (tp->full_duplex)
722 tp->full_duplex_lock = 1;
724 /* This is logically part of probe1(), but too complex to write inline. */
725 if (tulip_tbl[chip_idx].flags & HAS_MEDIA_TABLE)
726 parse_eeprom(dev);
728 if (media_cap[tp->default_port] & MediaIsMII) {
729 u16 media2advert[] = { 0x20, 0x40, 0x03e0, 0x60, 0x80, 0x100, 0x200 };
730 tp->to_advertise = media2advert[tp->default_port - 9];
731 } else
732 tp->to_advertise = 0x03e1;
734 if ((tulip_tbl[chip_idx].flags & ALWAYS_CHECK_MII) ||
735 (tp->mtable && tp->mtable->has_mii) ||
736 ( ! tp->mtable && (tulip_tbl[chip_idx].flags & HAS_MII))) {
737 int phy, phy_idx;
738 if (tp->mtable && tp->mtable->has_mii) {
739 for (i = 0; i < tp->mtable->leafcount; i++)
740 if (tp->mtable->mleaf[i].media == 11) {
741 tp->cur_index = i;
742 tp->saved_if_port = dev->if_port;
743 select_media(dev, 1);
744 dev->if_port = tp->saved_if_port;
745 break;
748 /* Find the connected MII xcvrs.
749 Doing this in open() would allow detecting external xcvrs later,
750 but takes much time. */
751 for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(tp->phys);
752 phy++) {
753 int mii_status = mdio_read(dev, phy, 1);
754 if ((mii_status & 0x8301) == 0x8001 ||
755 ((mii_status & 0x8000) == 0 && (mii_status & 0x7800) != 0)) {
756 int mii_reg0 = mdio_read(dev, phy, 0);
757 int mii_advert = mdio_read(dev, phy, 4);
758 int reg4 = ((mii_status>>6) & tp->to_advertise) | 1;
759 tp->phys[phy_idx] = phy;
760 tp->advertising[phy_idx++] = reg4;
761 printk(KERN_INFO "%s: MII transceiver #%d "
762 "config %4.4x status %4.4x advertising %4.4x.\n",
763 dev->name, phy, mii_reg0, mii_status, mii_advert);
764 /* Fixup for DLink with miswired PHY. */
765 if (mii_advert != reg4) {
766 printk(KERN_DEBUG "%s: Advertising %4.4x on PHY %d,"
767 " previously advertising %4.4x.\n",
768 dev->name, reg4, phy, mii_advert);
769 mdio_write(dev, phy, 4, reg4);
771 /* Enable autonegotiation: some boards default to off. */
772 mdio_write(dev, phy, 0, mii_reg0 |
773 (tp->full_duplex ? 0x1100 : 0x1000) |
774 (media_cap[tp->default_port]&MediaIs100 ? 0x2000:0));
777 tp->mii_cnt = phy_idx;
778 if (tp->mtable && tp->mtable->has_mii && phy_idx == 0) {
779 printk(KERN_INFO "%s: ***WARNING***: No MII transceiver found!\n",
780 dev->name);
781 tp->phys[0] = 1;
785 /* The Tulip-specific entries in the device structure. */
786 dev->open = &tulip_open;
787 dev->hard_start_xmit = &tulip_start_xmit;
788 dev->stop = &tulip_close;
789 dev->get_stats = &tulip_get_stats;
790 #ifdef HAVE_PRIVATE_IOCTL
791 dev->do_ioctl = &private_ioctl;
792 #endif
793 #ifdef HAVE_MULTICAST
794 dev->set_multicast_list = &set_rx_mode;
795 #endif
796 dev->tx_timeout = tulip_tx_timeout;
797 dev->watchdog_timeo = TX_TIMEOUT;
799 /* Reset the xcvr interface and turn on heartbeat. */
800 switch (chip_idx) {
801 case DC21041:
802 outl(0x00000000, ioaddr + CSR13);
803 outl(0xFFFFFFFF, ioaddr + CSR14);
804 outl(0x00000008, ioaddr + CSR15); /* Listen on AUI also. */
805 outl_CSR6(inl(ioaddr + CSR6) | 0x0200, ioaddr, chip_idx);
806 outl(0x0000EF05, ioaddr + CSR13);
807 break;
808 case DC21040:
809 outl(0x00000000, ioaddr + CSR13);
810 outl(0x00000004, ioaddr + CSR13);
811 break;
812 case DC21140: default:
813 if (tp->mtable)
814 outl(tp->mtable->csr12dir | 0x100, ioaddr + CSR12);
815 break;
816 case DC21142:
817 case PNIC2:
818 if (tp->mii_cnt || media_cap[dev->if_port] & MediaIsMII) {
819 outl_CSR6(0x82020000, ioaddr, chip_idx);
820 outl(0x0000, ioaddr + CSR13);
821 outl(0x0000, ioaddr + CSR14);
822 outl_CSR6(0x820E0000, ioaddr, chip_idx);
823 } else {
824 outl_CSR6(0x82420200, ioaddr, chip_idx);
825 outl(0x0001, ioaddr + CSR13);
826 outl(0x0003FFFF, ioaddr + CSR14);
827 outl(0x0008, ioaddr + CSR15);
828 outl(0x0001, ioaddr + CSR13);
829 outl(0x1301, ioaddr + CSR12); /* Start NWay. */
831 break;
832 case X3201_3:
833 outl(0x0008, ioaddr + CSR15);
834 udelay(5); /* The delays are Xircom recommended to give the
835 * chipset time to reset the actual hardware
836 * on the PCMCIA card
838 outl(0xa8050000, ioaddr + CSR15);
839 udelay(5);
840 outl(0xa00f0000, ioaddr + CSR15);
841 udelay(5);
842 outl_CSR6(0x32000200, ioaddr, chip_idx);
843 break;
844 case LC82C168:
845 if ( ! tp->mii_cnt) {
846 outl_CSR6(0x00420000, ioaddr, chip_idx);
847 outl(0x30, ioaddr + CSR12);
848 outl(0x0001F078, ioaddr + 0xB8);
849 outl(0x0201F078, ioaddr + 0xB8); /* Turn on autonegotiation. */
851 break;
852 case MX98713: case COMPEX9881:
853 outl_CSR6(0x00000000, ioaddr, chip_idx);
854 outl(0x000711C0, ioaddr + CSR14); /* Turn on NWay. */
855 outl(0x00000001, ioaddr + CSR13);
856 break;
857 case MX98715: case MX98725:
858 outl_CSR6(0x01a80000, ioaddr, chip_idx);
859 outl(0xFFFFFFFF, ioaddr + CSR14);
860 outl(0x00001000, ioaddr + CSR12);
861 break;
862 case COMET:
863 /* No initialization necessary. */
864 break;
867 return dev;
870 /* Serial EEPROM section. */
871 /* The main routine to parse the very complicated SROM structure.
872 Search www.digital.com for "21X4 SROM" to get details.
873 This code is very complex, and will require changes to support
874 additional cards, so I'll be verbose about what is going on.
877 /* Known cards that have old-style EEPROMs. */
878 static struct fixups {
879 char *name;
880 unsigned char addr0, addr1, addr2;
881 u16 newtable[32]; /* Max length below. */
882 } eeprom_fixups[] = {
883 {"Asante", 0, 0, 0x94, {0x1e00, 0x0000, 0x0800, 0x0100, 0x018c,
884 0x0000, 0x0000, 0xe078, 0x0001, 0x0050, 0x0018 }},
885 {"SMC9332DST", 0, 0, 0xC0, { 0x1e00, 0x0000, 0x0800, 0x021f,
886 0x0000, 0x009E, /* 10baseT */
887 0x0903, 0x006D, /* 100baseTx */ }},
888 {"Cogent EM100", 0, 0, 0x92, { 0x1e00, 0x0000, 0x0800, 0x033f,
889 0x0107, 0x8021, /* 100baseFx */
890 0x0108, 0x8021, /* 100baseFx-FD */
891 0x0103, 0x006D, /* 100baseTx */ }},
892 {"Maxtech NX-110", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x0313,
893 0x1001, 0x009E, /* 10base2, CSR12 0x10*/
894 0x0000, 0x009E, /* 10baseT */
895 0x0303, 0x006D, /* 100baseTx, CSR12 0x03 */ }},
896 {"Accton EN1207", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x031F,
897 0x1B01, 0x0000, /* 10base2, CSR12 0x1B */
898 0x1B03, 0x006D, /* 100baseTx, CSR12 0x1B */
899 0x0B00, 0x009E, /* 10baseT, CSR12 0x0B */
901 {0, 0, 0, 0, {}}};
903 static const char * block_name[] = {"21140 non-MII", "21140 MII PHY",
904 "21142 Serial PHY", "21142 MII PHY", "21143 SYM PHY", "21143 reset method"};
906 #if defined(__i386__) /* AKA get_unaligned() */
907 #define get_u16(ptr) (*(u16 *)(ptr))
908 #else
909 #define get_u16(ptr) (((u8*)(ptr))[0] + (((u8*)(ptr))[1]<<8))
910 #endif
912 static void parse_eeprom(struct net_device *dev)
914 /* The last media info list parsed, for multiport boards. */
915 static struct mediatable *last_mediatable = NULL;
916 static unsigned char *last_ee_data = NULL;
917 static int controller_index = 0;
918 struct tulip_private *tp = (struct tulip_private *)dev->priv;
919 long ioaddr = dev->base_addr;
920 unsigned char *ee_data = tp->eeprom;
921 int i;
922 #ifdef CARDBUS
923 int chip_rev = tp->revision;
924 #endif
926 tp->mtable = 0;
927 for (i = 0; i < EEPROM_SIZE/2; i++)
928 ((u16 *)ee_data)[i] =
929 le16_to_cpu(read_eeprom(ioaddr, i, EEPROM_ADDRLEN));
931 /* Detect an old-style (SA only) EEPROM layout:
932 memcmp(eedata, eedata+16, 8). */
933 for (i = 0; i < 8; i ++)
934 if (ee_data[i] != ee_data[16+i])
935 break;
936 if (i >= 8) {
937 if (ee_data[0] == 0xff) {
938 if (last_mediatable) {
939 controller_index++;
940 printk(KERN_INFO "%s: Controller %d of multiport board.\n",
941 dev->name, controller_index);
942 tp->mtable = last_mediatable;
943 ee_data = last_ee_data;
944 goto subsequent_board;
945 } else
946 printk(KERN_INFO "%s: Missing EEPROM, this interface may "
947 "not work correctly!\n",
948 dev->name);
949 return;
951 /* Do a fix-up based on the vendor half of the station address prefix. */
952 for (i = 0; eeprom_fixups[i].name; i++) {
953 if (dev->dev_addr[0] == eeprom_fixups[i].addr0
954 && dev->dev_addr[1] == eeprom_fixups[i].addr1
955 && dev->dev_addr[2] == eeprom_fixups[i].addr2) {
956 if (dev->dev_addr[2] == 0xE8 && ee_data[0x1a] == 0x55)
957 i++; /* An Accton EN1207, not an outlaw Maxtech. */
958 memcpy(ee_data + 26, eeprom_fixups[i].newtable,
959 sizeof(eeprom_fixups[i].newtable));
960 printk(KERN_INFO "%s: Old format EEPROM on '%s' board. Using"
961 " substitute media control info.\n",
962 dev->name, eeprom_fixups[i].name);
963 break;
966 if (eeprom_fixups[i].name == NULL) { /* No fixup found. */
967 printk(KERN_INFO "%s: Old style EEPROM with no media selection "
968 "information.\n",
969 dev->name);
970 return;
974 controller_index = 0;
975 if (ee_data[19] > 1) { /* Multiport board. */
976 last_ee_data = ee_data;
978 subsequent_board:
980 if (ee_data[27] == 0) { /* No valid media table. */
981 } else if (tp->chip_id == DC21041) {
982 unsigned char *p = (void *)ee_data + ee_data[27 + controller_index*3];
983 short media;
984 int count;
986 media = get_u16(p);
987 p += 2;
988 count = *p++;
990 printk(KERN_INFO "%s:21041 Media information at %d, default media "
991 "%4.4x (%s).\n", dev->name, ee_data[27], media,
992 media & 0x0800 ? "Autosense" : medianame[media & 15]);
993 for (i = 0; i < count; i++) {
994 unsigned char media_code = *p++;
995 u16 csrvals[3];
996 int idx;
997 for (idx = 0; idx < 3; idx++) {
998 csrvals[idx] = get_u16(p);
999 p += 2;
1001 if (media_code & 0x40) {
1002 printk(KERN_INFO "%s: 21041 media %2.2x (%s),"
1003 " csr13 %4.4x csr14 %4.4x csr15 %4.4x.\n",
1004 dev->name, media_code & 15, medianame[media_code & 15],
1005 csrvals[0], csrvals[1], csrvals[2]);
1006 } else
1007 printk(KERN_INFO "%s: 21041 media #%d, %s.\n",
1008 dev->name, media_code & 15, medianame[media_code & 15]);
1010 } else {
1011 unsigned char *p = (void *)ee_data + ee_data[27];
1012 unsigned char csr12dir = 0;
1013 int count;
1014 struct mediatable *mtable;
1015 u16 media = get_u16(p);
1017 p += 2;
1018 if (tulip_tbl[tp->chip_id].flags & CSR12_IN_SROM)
1019 csr12dir = *p++;
1020 count = *p++;
1021 mtable = (struct mediatable *)
1022 kmalloc(sizeof(struct mediatable) + count*sizeof(struct medialeaf),
1023 GFP_KERNEL);
1024 if (mtable == NULL)
1025 return; /* Horrible, impossible failure. */
1026 last_mediatable = tp->mtable = mtable;
1027 mtable->defaultmedia = media;
1028 mtable->leafcount = count;
1029 mtable->csr12dir = csr12dir;
1030 mtable->has_nonmii = mtable->has_mii = mtable->has_reset = 0;
1031 mtable->csr15dir = mtable->csr15val = 0;
1033 printk(KERN_INFO "%s: EEPROM default media type %s.\n", dev->name,
1034 media & 0x0800 ? "Autosense" : medianame[media & 15]);
1035 for (i = 0; i < count; i++) {
1036 struct medialeaf *leaf = &mtable->mleaf[i];
1038 if ((p[0] & 0x80) == 0) { /* 21140 Compact block. */
1039 leaf->type = 0;
1040 leaf->media = p[0] & 0x3f;
1041 leaf->leafdata = p;
1042 if ((p[2] & 0x61) == 0x01) /* Bogus, but Znyx boards do it. */
1043 mtable->has_mii = 1;
1044 p += 4;
1045 } else {
1046 leaf->type = p[1];
1047 if (p[1] == 0x05) {
1048 mtable->has_reset = i;
1049 leaf->media = p[2] & 0x0f;
1050 } else if (p[1] & 1) {
1051 mtable->has_mii = 1;
1052 leaf->media = 11;
1053 } else {
1054 mtable->has_nonmii = 1;
1055 leaf->media = p[2] & 0x0f;
1056 if (p[1] == 2) {
1057 if (leaf->media == 0) {
1058 mtable->csr15dir = get_unaligned((u16*)&p[3])<<16;
1059 mtable->csr15val = get_unaligned((u16*)&p[5])<<16;
1060 } else if (leaf->media == 0x40) {
1061 u32 base15 = get_unaligned((u16*)&p[7]);
1062 mtable->csr15dir =
1063 (get_unaligned((u16*)&p[9])<<16) + base15;
1064 mtable->csr15val =
1065 (get_unaligned((u16*)&p[11])<<16) + base15;
1069 leaf->leafdata = p + 2;
1070 p += (p[0] & 0x3f) + 1;
1072 if (tulip_debug > 1 && leaf->media == 11) {
1073 unsigned char *bp = leaf->leafdata;
1074 printk(KERN_INFO "%s: MII interface PHY %d, setup/reset "
1075 "sequences %d/%d long, capabilities %2.2x %2.2x.\n",
1076 dev->name, bp[0], bp[1], bp[1 + bp[1]*2],
1077 bp[5 + bp[2 + bp[1]*2]*2], bp[4 + bp[2 + bp[1]*2]*2]);
1079 printk(KERN_INFO "%s: Index #%d - Media %s (#%d) described "
1080 "by a %s (%d) block.\n",
1081 dev->name, i, medianame[leaf->media], leaf->media,
1082 block_name[leaf->type], leaf->type);
1086 /* Reading a serial EEPROM is a "bit" grungy, but we work our way through:->.*/
1088 /* EEPROM_Ctrl bits. */
1089 #define EE_SHIFT_CLK 0x02 /* EEPROM shift clock. */
1090 #define EE_CS 0x01 /* EEPROM chip select. */
1091 #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
1092 #define EE_WRITE_0 0x01
1093 #define EE_WRITE_1 0x05
1094 #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
1095 #define EE_ENB (0x4800 | EE_CS)
1097 /* Delay between EEPROM clock transitions.
1098 Even at 33Mhz current PCI implementations don't overrun the EEPROM clock.
1099 We add a bus turn-around to insure that this remains true. */
1100 #define eeprom_delay() inl(ee_addr)
1102 /* The EEPROM commands include the alway-set leading bit. */
1103 #define EE_WRITE_CMD (5 << addr_len)
1104 #define EE_READ_CMD (6 << addr_len)
1105 #define EE_ERASE_CMD (7 << addr_len)
1107 static int read_eeprom(long ioaddr, int location, int addr_len)
1109 int i;
1110 unsigned short retval = 0;
1111 long ee_addr = ioaddr + CSR9;
1112 int read_cmd = location | EE_READ_CMD;
1114 outl(EE_ENB & ~EE_CS, ee_addr);
1115 outl(EE_ENB, ee_addr);
1117 /* Shift the read command bits out. */
1118 for (i = 4 + addr_len; i >= 0; i--) {
1119 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1120 outl(EE_ENB | dataval, ee_addr);
1121 eeprom_delay();
1122 outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1123 eeprom_delay();
1125 outl(EE_ENB, ee_addr);
1127 for (i = 16; i > 0; i--) {
1128 outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
1129 eeprom_delay();
1130 retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1131 outl(EE_ENB, ee_addr);
1132 eeprom_delay();
1135 /* Terminate the EEPROM access. */
1136 outl(EE_ENB & ~EE_CS, ee_addr);
1137 return retval;
1140 /* MII transceiver control section.
1141 Read and write the MII registers using software-generated serial
1142 MDIO protocol. See the MII specifications or DP83840A data sheet
1143 for details. */
1145 /* The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
1146 met by back-to-back PCI I/O cycles, but we insert a delay to avoid
1147 "overclocking" issues or future 66Mhz PCI. */
1148 #define mdio_delay() inl(mdio_addr)
1150 /* Read and write the MII registers using software-generated serial
1151 MDIO protocol. It is just different enough from the EEPROM protocol
1152 to not share code. The maxium data clock rate is 2.5 Mhz. */
1153 #define MDIO_SHIFT_CLK 0x10000
1154 #define MDIO_DATA_WRITE0 0x00000
1155 #define MDIO_DATA_WRITE1 0x20000
1156 #define MDIO_ENB 0x00000 /* Ignore the 0x02000 databook setting. */
1157 #define MDIO_ENB_IN 0x40000
1158 #define MDIO_DATA_READ 0x80000
1160 static int mdio_read(struct net_device *dev, int phy_id, int location)
1162 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1163 int i;
1164 int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
1165 int retval = 0;
1166 long ioaddr = dev->base_addr;
1167 long mdio_addr = ioaddr + CSR9;
1169 if (tp->chip_id == LC82C168) {
1170 int i = 1000;
1171 outl(0x60020000 + (phy_id<<23) + (location<<18), ioaddr + 0xA0);
1172 inl(ioaddr + 0xA0);
1173 inl(ioaddr + 0xA0);
1174 while (--i > 0)
1175 if ( ! ((retval = inl(ioaddr + 0xA0)) & 0x80000000))
1176 return retval & 0xffff;
1177 return 0xffff;
1180 if (tp->chip_id == COMET) {
1181 if (phy_id == 1) {
1182 if (location < 7)
1183 return inl(ioaddr + 0xB4 + (location<<2));
1184 else if (location == 17)
1185 return inl(ioaddr + 0xD0);
1186 else if (location >= 29 && location <= 31)
1187 return inl(ioaddr + 0xD4 + ((location-29)<<2));
1189 return 0xffff;
1192 /* Establish sync by sending at least 32 logic ones. */
1193 for (i = 32; i >= 0; i--) {
1194 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
1195 mdio_delay();
1196 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
1197 mdio_delay();
1199 /* Shift the read command bits out. */
1200 for (i = 15; i >= 0; i--) {
1201 int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
1203 outl(MDIO_ENB | dataval, mdio_addr);
1204 mdio_delay();
1205 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
1206 mdio_delay();
1208 /* Read the two transition, 16 data, and wire-idle bits. */
1209 for (i = 19; i > 0; i--) {
1210 outl(MDIO_ENB_IN, mdio_addr);
1211 mdio_delay();
1212 retval = (retval << 1) | ((inl(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
1213 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
1214 mdio_delay();
1216 return (retval>>1) & 0xffff;
1219 static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
1221 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1222 int i;
1223 int cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
1224 long ioaddr = dev->base_addr;
1225 long mdio_addr = ioaddr + CSR9;
1227 if (tp->chip_id == LC82C168) {
1228 int i = 1000;
1229 outl(cmd, ioaddr + 0xA0);
1231 if ( ! (inl(ioaddr + 0xA0) & 0x80000000))
1232 break;
1233 while (--i > 0);
1234 return;
1237 if (tp->chip_id == COMET) {
1238 if (phy_id != 1)
1239 return;
1240 if (location < 7)
1241 outl(value, ioaddr + 0xB4 + (location<<2));
1242 else if (location == 17)
1243 outl(value, ioaddr + 0xD0);
1244 else if (location >= 29 && location <= 31)
1245 outl(value, ioaddr + 0xD4 + ((location-29)<<2));
1246 return;
1249 /* Establish sync by sending 32 logic ones. */
1250 for (i = 32; i >= 0; i--) {
1251 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
1252 mdio_delay();
1253 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
1254 mdio_delay();
1256 /* Shift the command bits out. */
1257 for (i = 31; i >= 0; i--) {
1258 int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
1259 outl(MDIO_ENB | dataval, mdio_addr);
1260 mdio_delay();
1261 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
1262 mdio_delay();
1264 /* Clear out extra bits. */
1265 for (i = 2; i > 0; i--) {
1266 outl(MDIO_ENB_IN, mdio_addr);
1267 mdio_delay();
1268 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
1269 mdio_delay();
1271 return;
1274 static void
1275 tulip_up(struct net_device *dev)
1277 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1278 long ioaddr = dev->base_addr;
1279 int i;
1281 /* On some chip revs we must set the MII/SYM port before the reset!? */
1282 if (tp->mii_cnt || (tp->mtable && tp->mtable->has_mii))
1283 outl_CSR6(0x00040000, ioaddr, tp->chip_id);
1285 /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
1286 outl(0x00000001, ioaddr + CSR0);
1288 /* Deassert reset. */
1289 outl(tp->csr0, ioaddr + CSR0);
1290 udelay(2);
1292 if (tulip_tbl[tp->chip_id].flags & HAS_ACPI)
1293 pci_write_config_dword(tp->pdev, 0x40, 0x00000000);
1295 /* Clear the tx ring */
1296 for (i = 0; i < TX_RING_SIZE; i++) {
1297 tp->tx_skbuff[i] = 0;
1298 tp->tx_ring[i].status = 0x00000000;
1301 if (tulip_debug > 1)
1302 printk(KERN_DEBUG "%s: tulip_open() irq %d.\n", dev->name, dev->irq);
1304 if (tulip_tbl[tp->chip_id].flags & MC_HASH_ONLY) {
1305 u32 addr_low = cpu_to_le32(get_unaligned((u32 *)dev->dev_addr));
1306 u32 addr_high = cpu_to_le32(get_unaligned((u16 *)(dev->dev_addr+4)));
1307 if (tp->chip_id == AX88140) {
1308 outl(0, ioaddr + CSR13);
1309 outl(addr_low, ioaddr + CSR14);
1310 outl(1, ioaddr + CSR13);
1311 outl(addr_high, ioaddr + CSR14);
1312 } else if (tp->chip_id == COMET) {
1313 outl(addr_low, ioaddr + 0xA4);
1314 outl(addr_high, ioaddr + 0xA8);
1315 outl(0, ioaddr + 0xAC);
1316 outl(0, ioaddr + 0xB0);
1318 } else if (tp->chip_id != X3201_3) {
1319 /* This is set_rx_mode(), but without starting the transmitter. */
1320 u16 *eaddrs = (u16 *)dev->dev_addr;
1321 u16 *setup_frm = &tp->setup_frame[15*6];
1323 /* 21140 bug: you must add the broadcast address. */
1324 memset(tp->setup_frame, 0xff, 96*sizeof(u16));
1325 /* Fill the final entry of the table with our physical address. */
1326 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
1327 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
1328 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
1329 /* Put the setup frame on the Tx list. */
1330 tp->tx_ring[0].length = 0x08000000 | 192;
1331 tp->tx_ring[0].buffer1 = virt_to_bus(tp->setup_frame);
1332 tp->tx_ring[0].status = DescOwned;
1334 tp->cur_tx++;
1335 } else { /* X3201_3 */
1336 u16 *eaddrs = (u16 *)dev->dev_addr;
1337 u16 *setup_frm = &tp->setup_frame[0*6];
1339 /* fill the table with the broadcast address */
1340 memset(tp->setup_frame, 0xff, 96*sizeof(u16));
1341 /* re-fill the first 14 table entries with our address */
1342 for(i=0; i<14; i++) {
1343 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
1344 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
1345 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
1348 /* Put the setup frame on the Tx list. */
1349 tp->tx_ring[0].length = 0x08000000 | 192;
1350 /* Lie about the address of our setup frame to make the */
1351 /* chip happy */
1352 tp->tx_ring[0].buffer1 = (virt_to_bus(tp->setup_frame) + 4);
1353 tp->tx_ring[0].status = DescOwned;
1355 tp->cur_tx++;
1357 outl(virt_to_bus(tp->rx_ring), ioaddr + CSR3);
1358 outl(virt_to_bus(tp->tx_ring), ioaddr + CSR4);
1360 tp->saved_if_port = dev->if_port;
1361 if (dev->if_port == 0)
1362 dev->if_port = tp->default_port;
1363 if (tp->chip_id == DC21041 && dev->if_port > 4)
1364 /* Invalid: Select initial TP, autosense, autonegotiate. */
1365 dev->if_port = 4;
1367 /* Allow selecting a default media. */
1368 i = 0;
1369 if (tp->mtable == NULL)
1370 goto media_picked;
1371 if (dev->if_port) {
1372 int looking_for = media_cap[dev->if_port] & MediaIsMII ? 11 :
1373 (dev->if_port == 12 ? 0 : dev->if_port);
1374 for (i = 0; i < tp->mtable->leafcount; i++)
1375 if (tp->mtable->mleaf[i].media == looking_for) {
1376 printk(KERN_INFO "%s: Using user-specified media %s.\n",
1377 dev->name, medianame[dev->if_port]);
1378 goto media_picked;
1381 if ((tp->mtable->defaultmedia & 0x0800) == 0) {
1382 int looking_for = tp->mtable->defaultmedia & 15;
1383 for (i = 0; i < tp->mtable->leafcount; i++)
1384 if (tp->mtable->mleaf[i].media == looking_for) {
1385 printk(KERN_INFO "%s: Using EEPROM-set media %s.\n",
1386 dev->name, medianame[looking_for]);
1387 goto media_picked;
1390 /* Start sensing first non-full-duplex media. */
1391 for (i = tp->mtable->leafcount - 1;
1392 (media_cap[tp->mtable->mleaf[i].media] & MediaAlwaysFD) && i > 0; i--)
1394 media_picked:
1396 tp->csr6 = 0;
1397 tp->cur_index = i;
1398 if (dev->if_port == 0 && tp->chip_id == DC21142) {
1399 if (tp->mii_cnt) {
1400 select_media(dev, 1);
1401 if (tulip_debug > 1)
1402 printk(KERN_INFO "%s: Using MII transceiver %d, status "
1403 "%4.4x.\n",
1404 dev->name, tp->phys[0], mdio_read(dev, tp->phys[0], 1));
1405 outl_CSR6(0x82020000, ioaddr, tp->chip_id);
1406 tp->csr6 = 0x820E0000;
1407 dev->if_port = 11;
1408 outl(0x0000, ioaddr + CSR13);
1409 outl(0x0000, ioaddr + CSR14);
1410 } else
1411 t21142_start_nway(dev);
1412 } else if ((tp->chip_id == LC82C168 || tp->chip_id == PNIC2)
1413 && tp->mii_cnt && ! tp->medialock) {
1414 dev->if_port = 11;
1415 tp->csr6 = 0x814C0000 | (tp->full_duplex ? 0x0200 : 0);
1416 outl(0x0001, ioaddr + CSR15);
1417 } else if ((tp->chip_id == MX98713 || tp->chip_id == COMPEX9881)
1418 && ! tp->medialock) {
1419 dev->if_port = 0;
1420 tp->csr6 = 0x01880000 | (tp->full_duplex ? 0x0200 : 0);
1421 outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
1422 } else if (tp->chip_id == MX98715 || tp->chip_id == MX98725) {
1423 /* Provided by BOLO, Macronix - 12/10/1998. */
1424 dev->if_port = 0;
1425 tp->csr6 = 0x01880200;
1426 outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
1427 outl(0x11000 | inw(ioaddr + 0xa0), ioaddr + 0xa0);
1428 } else if (tp->chip_id == DC21143 &&
1429 media_cap[dev->if_port] & MediaIsMII) {
1430 /* We must reset the media CSRs when we force-select MII mode. */
1431 outl(0x0000, ioaddr + CSR13);
1432 outl(0x0000, ioaddr + CSR14);
1433 outl(0x0008, ioaddr + CSR15);
1434 } else if (tp->chip_id == X3201_3) {
1435 outl(0x0008, ioaddr + CSR15);
1436 udelay(5);
1437 outl(0xa8050000, ioaddr + CSR15);
1438 udelay(5);
1439 outl(0xa00f0000, ioaddr + CSR15);
1440 udelay(5);
1441 tp->csr6 = 0x32400000;
1442 } else if (tp->chip_id == COMET) {
1443 dev->if_port = 0;
1444 tp->csr6 = 0x00040000;
1445 } else
1446 select_media(dev, 1);
1448 /* Start the chip's Tx to process setup frame. */
1449 outl_CSR6(tp->csr6, ioaddr, tp->chip_id);
1450 outl_CSR6(tp->csr6 | 0x2000, ioaddr, tp->chip_id);
1452 /* Enable interrupts by setting the interrupt mask. */
1453 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR5);
1454 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
1455 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
1456 outl(0, ioaddr + CSR2); /* Rx poll demand */
1458 netif_start_queue (dev);
1460 if (tulip_debug > 2) {
1461 printk(KERN_DEBUG "%s: Done tulip_open(), CSR0 %8.8x, CSR5 %8.8x CSR6 %8.8x.\n",
1462 dev->name, inl(ioaddr + CSR0), inl(ioaddr + CSR5),
1463 inl(ioaddr + CSR6));
1465 /* Set the timer to switch to check for link beat and perhaps switch
1466 to an alternate media type. */
1467 init_timer(&tp->timer);
1468 tp->timer.expires = RUN_AT(5*HZ);
1469 tp->timer.data = (unsigned long)dev;
1470 tp->timer.function = tulip_tbl[tp->chip_id].media_timer;
1471 add_timer(&tp->timer);
1474 static int
1475 tulip_open(struct net_device *dev)
1477 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1479 if (request_irq(dev->irq, &tulip_interrupt, SA_SHIRQ, dev->name, dev))
1480 return -EAGAIN;
1482 tulip_init_ring(dev);
1484 tulip_up(dev);
1485 tp->open = 1;
1486 MOD_INC_USE_COUNT;
1488 return 0;
1491 /* Set up the transceiver control registers for the selected media type. */
1492 static void select_media(struct net_device *dev, int startup)
1494 long ioaddr = dev->base_addr;
1495 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1496 struct mediatable *mtable = tp->mtable;
1497 u32 new_csr6;
1498 int i;
1500 if (mtable) {
1501 struct medialeaf *mleaf = &mtable->mleaf[tp->cur_index];
1502 unsigned char *p = mleaf->leafdata;
1503 switch (mleaf->type) {
1504 case 0: /* 21140 non-MII xcvr. */
1505 if (tulip_debug > 1)
1506 printk(KERN_DEBUG "%s: Using a 21140 non-MII transceiver"
1507 " with control setting %2.2x.\n",
1508 dev->name, p[1]);
1509 dev->if_port = p[0];
1510 if (startup)
1511 outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
1512 outl(p[1], ioaddr + CSR12);
1513 new_csr6 = 0x02000000 | ((p[2] & 0x71) << 18);
1514 break;
1515 case 2: case 4: {
1516 u16 setup[5];
1517 u32 csr13val, csr14val, csr15dir, csr15val;
1518 for (i = 0; i < 5; i++)
1519 setup[i] = get_u16(&p[i*2 + 1]);
1521 dev->if_port = p[0] & 15;
1522 if (media_cap[dev->if_port] & MediaAlwaysFD)
1523 tp->full_duplex = 1;
1525 if (startup && mtable->has_reset) {
1526 struct medialeaf *rleaf = &mtable->mleaf[mtable->has_reset];
1527 unsigned char *rst = rleaf->leafdata;
1528 if (tulip_debug > 1)
1529 printk(KERN_DEBUG "%s: Resetting the transceiver.\n",
1530 dev->name);
1531 for (i = 0; i < rst[0]; i++)
1532 outl(get_u16(rst + 1 + (i<<1)) << 16, ioaddr + CSR15);
1534 if (tulip_debug > 1)
1535 printk(KERN_DEBUG "%s: 21143 non-MII %s transceiver control "
1536 "%4.4x/%4.4x.\n",
1537 dev->name, medianame[dev->if_port], setup[0], setup[1]);
1538 if (p[0] & 0x40) { /* SIA (CSR13-15) setup values are provided. */
1539 csr13val = setup[0];
1540 csr14val = setup[1];
1541 csr15dir = (setup[3]<<16) | setup[2];
1542 csr15val = (setup[4]<<16) | setup[2];
1543 outl(0, ioaddr + CSR13);
1544 outl(csr14val, ioaddr + CSR14);
1545 outl(csr15dir, ioaddr + CSR15); /* Direction */
1546 outl(csr15val, ioaddr + CSR15); /* Data */
1547 outl(csr13val, ioaddr + CSR13);
1548 } else {
1549 csr13val = 1;
1550 csr14val = 0x0003FF7F;
1551 csr15dir = (setup[0]<<16) | 0x0008;
1552 csr15val = (setup[1]<<16) | 0x0008;
1553 if (dev->if_port <= 4)
1554 csr14val = t21142_csr14[dev->if_port];
1555 if (startup) {
1556 outl(0, ioaddr + CSR13);
1557 outl(csr14val, ioaddr + CSR14);
1559 outl(csr15dir, ioaddr + CSR15); /* Direction */
1560 outl(csr15val, ioaddr + CSR15); /* Data */
1561 if (startup) outl(csr13val, ioaddr + CSR13);
1563 if (tulip_debug > 1)
1564 printk(KERN_DEBUG "%s: Setting CSR15 to %8.8x/%8.8x.\n",
1565 dev->name, csr15dir, csr15val);
1566 if (mleaf->type == 4)
1567 new_csr6 = 0x82020000 | ((setup[2] & 0x71) << 18);
1568 else
1569 new_csr6 = 0x82420000;
1570 break;
1572 case 1: case 3: {
1573 int phy_num = p[0];
1574 int init_length = p[1];
1575 u16 *misc_info;
1576 u16 to_advertise;
1578 dev->if_port = 11;
1579 new_csr6 = 0x020E0000;
1580 if (mleaf->type == 3) { /* 21142 */
1581 u16 *init_sequence = (u16*)(p+2);
1582 u16 *reset_sequence = &((u16*)(p+3))[init_length];
1583 int reset_length = p[2 + init_length*2];
1584 misc_info = reset_sequence + reset_length;
1585 if (startup)
1586 for (i = 0; i < reset_length; i++)
1587 outl(get_u16(&reset_sequence[i]) << 16, ioaddr + CSR15);
1588 for (i = 0; i < init_length; i++)
1589 outl(get_u16(&init_sequence[i]) << 16, ioaddr + CSR15);
1590 } else {
1591 u8 *init_sequence = p + 2;
1592 u8 *reset_sequence = p + 3 + init_length;
1593 int reset_length = p[2 + init_length];
1594 misc_info = (u16*)(reset_sequence + reset_length);
1595 if (startup) {
1596 outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
1597 for (i = 0; i < reset_length; i++)
1598 outl(reset_sequence[i], ioaddr + CSR12);
1600 for (i = 0; i < init_length; i++)
1601 outl(init_sequence[i], ioaddr + CSR12);
1603 to_advertise = (get_u16(&misc_info[1]) & tp->to_advertise) | 1;
1604 tp->advertising[phy_num] = to_advertise;
1605 if (tulip_debug > 1)
1606 printk(KERN_DEBUG "%s: Advertising %4.4x on PHY %d (%d).\n",
1607 dev->name, to_advertise, phy_num, tp->phys[phy_num]);
1608 /* Bogus: put in by a committee? */
1609 mdio_write(dev, tp->phys[phy_num], 4, to_advertise);
1610 break;
1612 default:
1613 printk(KERN_DEBUG "%s: Invalid media table selection %d.\n",
1614 dev->name, mleaf->type);
1615 new_csr6 = 0x020E0000;
1617 if (tulip_debug > 1)
1618 printk(KERN_DEBUG "%s: Using media type %s, CSR12 is %2.2x.\n",
1619 dev->name, medianame[dev->if_port],
1620 inl(ioaddr + CSR12) & 0xff);
1621 } else if (tp->chip_id == DC21041) {
1622 if (tulip_debug > 1)
1623 printk(KERN_DEBUG "%s: 21041 using media %s, CSR12 is %4.4x.\n",
1624 dev->name, medianame[dev->if_port & 15],
1625 inl(ioaddr + CSR12) & 0xffff);
1626 outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
1627 outl(t21041_csr14[dev->if_port], ioaddr + CSR14);
1628 outl(t21041_csr15[dev->if_port], ioaddr + CSR15);
1629 outl(t21041_csr13[dev->if_port], ioaddr + CSR13);
1630 new_csr6 = 0x80020000;
1631 } else if (tp->chip_id == LC82C168 || tp->chip_id == PNIC2) {
1632 if (startup && ! tp->medialock)
1633 dev->if_port = tp->mii_cnt ? 11 : 0;
1634 if (tulip_debug > 1)
1635 printk(KERN_DEBUG "%s: PNIC PHY status is %3.3x, CSR12 %4.4x,"
1636 " media %s.\n",
1637 dev->name, inl(ioaddr + 0xB8), inl(ioaddr + CSR12),
1638 medianame[dev->if_port]);
1639 if (tp->mii_cnt) {
1640 new_csr6 = 0x810C0000;
1641 outl(0x0001, ioaddr + CSR15);
1642 outl(0x0201B07A, ioaddr + 0xB8);
1643 } else if (startup) {
1644 /* Start with 10mbps to do autonegotiation. */
1645 outl(0x32, ioaddr + CSR12);
1646 new_csr6 = 0x00420000;
1647 outl(0x0001B078, ioaddr + 0xB8);
1648 outl(0x0201B078, ioaddr + 0xB8);
1649 } else if (dev->if_port == 3 || dev->if_port == 5) {
1650 outl(0x33, ioaddr + CSR12);
1651 new_csr6 = 0x01860000;
1652 if (startup)
1653 outl(0x0201F868, ioaddr + 0xB8); /* Trigger autonegotiation. */
1654 else
1655 outl(0x1F868, ioaddr + 0xB8);
1656 } else {
1657 outl(0x32, ioaddr + CSR12);
1658 new_csr6 = 0x00420000;
1659 outl(0x1F078, ioaddr + 0xB8);
1661 } else if (tp->chip_id == DC21040) { /* 21040 */
1662 /* Turn on the xcvr interface. */
1663 int csr12 = inl(ioaddr + CSR12);
1664 if (tulip_debug > 1)
1665 printk(KERN_DEBUG "%s: 21040 media type is %s, CSR12 is %2.2x.\n",
1666 dev->name, medianame[dev->if_port], csr12);
1667 if (media_cap[dev->if_port] & MediaAlwaysFD)
1668 tp->full_duplex = 1;
1669 new_csr6 = 0x20000;
1670 /* Set the full duplux match frame. */
1671 outl(FULL_DUPLEX_MAGIC, ioaddr + CSR11);
1672 outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
1673 if (t21040_csr13[dev->if_port] & 8) {
1674 outl(0x0705, ioaddr + CSR14);
1675 outl(0x0006, ioaddr + CSR15);
1676 } else {
1677 outl(0xffff, ioaddr + CSR14);
1678 outl(0x0000, ioaddr + CSR15);
1680 outl(0x8f01 | t21040_csr13[dev->if_port], ioaddr + CSR13);
1681 } else if (tp->chip_id == X3201_3) { /* Xircom */
1682 if (tp->default_port == 0)
1683 dev->if_port = tp->mii_cnt ? 11 : 3;
1684 /* Someone is on crack, the Xircom only does MII, no Fx */
1685 /* if (media_cap[dev->if_port] & MediaIsMII) {
1686 new_csr6 = 0x020E0000;
1687 } else if (media_cap[dev->if_port] & MediaIsFx) {
1688 new_csr6 = 0x028600000;
1689 } else
1690 new_csr6 = 0x038600000;*/
1691 new_csr6 = 0x324c0000;
1692 if (tulip_debug > 1)
1693 printk(KERN_DEBUG "%s: Xircom CardBus Adapter: "
1694 "%s transceiver, CSR12 %2.2x.\n",
1695 dev->name, medianame[dev->if_port],
1696 inl(ioaddr + CSR12));
1697 } else { /* Unknown chip type with no media table. */
1698 if (tp->default_port == 0)
1699 dev->if_port = tp->mii_cnt ? 11 : 3;
1700 if (media_cap[dev->if_port] & MediaIsMII) {
1701 new_csr6 = 0x020E0000;
1702 } else if (media_cap[dev->if_port] & MediaIsFx) {
1703 new_csr6 = 0x028600000;
1704 } else
1705 new_csr6 = 0x038600000;
1706 if (tulip_debug > 1)
1707 printk(KERN_DEBUG "%s: No media description table, assuming "
1708 "%s transceiver, CSR12 %2.2x.\n",
1709 dev->name, medianame[dev->if_port],
1710 inl(ioaddr + CSR12));
1713 tp->csr6 = new_csr6 | (tp->csr6 & 0xfdff) | (tp->full_duplex ? 0x0200 : 0);
1714 return;
1718 Check the MII negotiated duplex, and change the CSR6 setting if
1719 required.
1720 Return 0 if everything is OK.
1721 Return < 0 if the transceiver is missing or has no link beat.
1723 static int check_duplex(struct net_device *dev)
1725 long ioaddr = dev->base_addr;
1726 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1727 int mii_reg1, mii_reg5, negotiated, duplex;
1729 if (tp->full_duplex_lock)
1730 return 0;
1731 mii_reg1 = mdio_read(dev, tp->phys[0], 1);
1732 mii_reg5 = mdio_read(dev, tp->phys[0], 5);
1733 if (tulip_debug > 1)
1734 printk(KERN_INFO "%s: MII status %4.4x, Link partner report "
1735 "%4.4x.\n", dev->name, mii_reg1, mii_reg5);
1736 if (mii_reg1 == 0xffff)
1737 return -2;
1738 if ((mii_reg1 & 0x0004) == 0) {
1739 int new_reg1 = mdio_read(dev, tp->phys[0], 1);
1740 if ((new_reg1 & 0x0004) == 0) {
1741 if (tulip_debug > 1)
1742 printk(KERN_INFO "%s: No link beat on the MII interface,"
1743 " status %4.4x.\n", dev->name, new_reg1);
1744 return -1;
1747 negotiated = mii_reg5 & tp->advertising[0];
1748 duplex = ((negotiated & 0x0300) == 0x0100
1749 || (negotiated & 0x00C0) == 0x0040);
1750 /* 100baseTx-FD or 10T-FD, but not 100-HD */
1751 if (tp->full_duplex != duplex) {
1752 tp->full_duplex = duplex;
1753 if (tp->full_duplex) tp->csr6 |= 0x0200;
1754 else tp->csr6 &= ~0x0200;
1755 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
1756 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
1757 if (tulip_debug > 0)
1758 printk(KERN_INFO "%s: Setting %s-duplex based on MII"
1759 "#%d link partner capability of %4.4x.\n",
1760 dev->name, tp->full_duplex ? "full" : "half",
1761 tp->phys[0], mii_reg5);
1763 return 0;
1766 static void tulip_timer(unsigned long data)
1768 struct net_device *dev = (struct net_device *)data;
1769 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1770 long ioaddr = dev->base_addr;
1771 u32 csr12 = inl(ioaddr + CSR12);
1772 int next_tick = 2*HZ;
1774 if (tulip_debug > 2) {
1775 printk(KERN_DEBUG "%s: Media selection tick, status %8.8x mode %8.8x "
1776 "SIA %8.8x %8.8x %8.8x %8.8x.\n",
1777 dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR6),
1778 csr12, inl(ioaddr + CSR13),
1779 inl(ioaddr + CSR14), inl(ioaddr + CSR15));
1781 switch (tp->chip_id) {
1782 case DC21040:
1783 if (!tp->medialock && csr12 & 0x0002) { /* Network error */
1784 printk(KERN_INFO "%s: No link beat found.\n",
1785 dev->name);
1786 dev->if_port = (dev->if_port == 2 ? 0 : 2);
1787 select_media(dev, 0);
1788 dev->trans_start = jiffies;
1790 break;
1791 case DC21041:
1792 if (tulip_debug > 2)
1793 printk(KERN_DEBUG "%s: 21041 media tick CSR12 %8.8x.\n",
1794 dev->name, csr12);
1795 switch (dev->if_port) {
1796 case 0: case 3: case 4:
1797 if (csr12 & 0x0004) { /*LnkFail */
1798 /* 10baseT is dead. Check for activity on alternate port. */
1799 tp->mediasense = 1;
1800 if (csr12 & 0x0200)
1801 dev->if_port = 2;
1802 else
1803 dev->if_port = 1;
1804 printk(KERN_INFO "%s: No 21041 10baseT link beat, Media switched to %s.\n",
1805 dev->name, medianame[dev->if_port]);
1806 outl(0, ioaddr + CSR13); /* Reset */
1807 outl(t21041_csr14[dev->if_port], ioaddr + CSR14);
1808 outl(t21041_csr15[dev->if_port], ioaddr + CSR15);
1809 outl(t21041_csr13[dev->if_port], ioaddr + CSR13);
1810 next_tick = 10*HZ; /* 2.4 sec. */
1811 } else
1812 next_tick = 30*HZ;
1813 break;
1814 case 1: /* 10base2 */
1815 case 2: /* AUI */
1816 if (csr12 & 0x0100) {
1817 next_tick = (30*HZ); /* 30 sec. */
1818 tp->mediasense = 0;
1819 } else if ((csr12 & 0x0004) == 0) {
1820 printk(KERN_INFO "%s: 21041 media switched to 10baseT.\n",
1821 dev->name);
1822 dev->if_port = 0;
1823 select_media(dev, 0);
1824 next_tick = (24*HZ)/10; /* 2.4 sec. */
1825 } else if (tp->mediasense || (csr12 & 0x0002)) {
1826 dev->if_port = 3 - dev->if_port; /* Swap ports. */
1827 select_media(dev, 0);
1828 next_tick = 20*HZ;
1829 } else {
1830 next_tick = 20*HZ;
1832 break;
1834 break;
1835 case DC21140: case DC21142: case MX98713: case COMPEX9881: default: {
1836 struct medialeaf *mleaf;
1837 unsigned char *p;
1838 if (tp->mtable == NULL) { /* No EEPROM info, use generic code. */
1839 /* Not much that can be done.
1840 Assume this a generic MII or SYM transceiver. */
1841 next_tick = 60*HZ;
1842 if (tulip_debug > 2)
1843 printk(KERN_DEBUG "%s: network media monitor CSR6 %8.8x "
1844 "CSR12 0x%2.2x.\n",
1845 dev->name, inl(ioaddr + CSR6), csr12 & 0xff);
1846 break;
1848 mleaf = &tp->mtable->mleaf[tp->cur_index];
1849 p = mleaf->leafdata;
1850 switch (mleaf->type) {
1851 case 0: case 4: {
1852 /* Type 0 serial or 4 SYM transceiver. Check the link beat bit. */
1853 int offset = mleaf->type == 4 ? 5 : 2;
1854 s8 bitnum = p[offset];
1855 if (p[offset+1] & 0x80) {
1856 if (tulip_debug > 1)
1857 printk(KERN_DEBUG"%s: Transceiver monitor tick "
1858 "CSR12=%#2.2x, no media sense.\n",
1859 dev->name, csr12);
1860 if (mleaf->type == 4) {
1861 if (mleaf->media == 3 && (csr12 & 0x02))
1862 goto select_next_media;
1864 break;
1866 if (tulip_debug > 2)
1867 printk(KERN_DEBUG "%s: Transceiver monitor tick: CSR12=%#2.2x"
1868 " bit %d is %d, expecting %d.\n",
1869 dev->name, csr12, (bitnum >> 1) & 7,
1870 (csr12 & (1 << ((bitnum >> 1) & 7))) != 0,
1871 (bitnum >= 0));
1872 /* Check that the specified bit has the proper value. */
1873 if ((bitnum < 0) !=
1874 ((csr12 & (1 << ((bitnum >> 1) & 7))) != 0)) {
1875 if (tulip_debug > 1)
1876 printk(KERN_DEBUG "%s: Link beat detected for %s.\n", dev->name,
1877 medianame[mleaf->media]);
1878 if ((p[2] & 0x61) == 0x01) /* Bogus Znyx board. */
1879 goto actually_mii;
1880 break;
1882 if (tp->medialock)
1883 break;
1884 select_next_media:
1885 if (--tp->cur_index < 0) {
1886 /* We start again, but should instead look for default. */
1887 tp->cur_index = tp->mtable->leafcount - 1;
1889 dev->if_port = tp->mtable->mleaf[tp->cur_index].media;
1890 if (media_cap[dev->if_port] & MediaIsFD)
1891 goto select_next_media; /* Skip FD entries. */
1892 if (tulip_debug > 1)
1893 printk(KERN_DEBUG "%s: No link beat on media %s,"
1894 " trying transceiver type %s.\n",
1895 dev->name, medianame[mleaf->media & 15],
1896 medianame[tp->mtable->mleaf[tp->cur_index].media]);
1897 select_media(dev, 0);
1898 /* Restart the transmit process. */
1899 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
1900 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
1901 next_tick = (24*HZ)/10;
1902 break;
1904 case 1: case 3: /* 21140, 21142 MII */
1905 actually_mii:
1906 check_duplex(dev);
1907 next_tick = 60*HZ;
1908 break;
1909 case 2: /* 21142 serial block has no link beat. */
1910 default:
1911 break;
1914 break;
1916 tp->timer.expires = RUN_AT(next_tick);
1917 add_timer(&tp->timer);
1920 /* Handle the 21143 uniquely: do autoselect with NWay, not the EEPROM list
1921 of available transceivers. */
1922 static void t21142_timer(unsigned long data)
1924 struct net_device *dev = (struct net_device *)data;
1925 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1926 long ioaddr = dev->base_addr;
1927 int csr12 = inl(ioaddr + CSR12);
1928 int next_tick = 60*HZ;
1929 int new_csr6 = 0;
1931 if ((tulip_debug > 2) && !(media_cap[dev->if_port] & MediaIsMII))
1932 printk(KERN_INFO"%s: 21143 negotiation status %8.8x, %s.\n",
1933 dev->name, csr12, medianame[dev->if_port]);
1934 if (media_cap[dev->if_port] & MediaIsMII) {
1935 check_duplex(dev);
1936 next_tick = 60*HZ;
1937 } else if (tp->nwayset) {
1938 /* Don't screw up a negotiated session! */
1939 if (tulip_debug > 1)
1940 printk(KERN_INFO"%s: Using NWay-set %s media, csr12 %8.8x.\n",
1941 dev->name, medianame[dev->if_port], csr12);
1942 } else if (tp->medialock) {
1944 } else if (dev->if_port == 3) {
1945 if (csr12 & 2) { /* No 100mbps link beat, revert to 10mbps. */
1946 if (tulip_debug > 1)
1947 printk(KERN_INFO"%s: No 21143 100baseTx link beat, %8.8x, "
1948 "trying NWay.\n", dev->name, csr12);
1949 t21142_start_nway(dev);
1950 next_tick = 3*HZ;
1952 } else if (((csr12 & 0x7000) != 0x5000)
1953 && tp->chip_id != X3201_3) {
1954 /* Negotiation failed. Search media types. */
1955 if (tulip_debug > 1)
1956 printk(KERN_INFO"%s: 21143 negotiation failed, status %8.8x.\n",
1957 dev->name, csr12);
1958 if (!(csr12 & 4)) { /* 10mbps link beat good. */
1959 new_csr6 = 0x82420000;
1960 dev->if_port = 0;
1961 outl(0, ioaddr + CSR13);
1962 outl(0x0003FFFF, ioaddr + CSR14);
1963 outw(t21142_csr15[dev->if_port], ioaddr + CSR15);
1964 outl(t21142_csr13[dev->if_port], ioaddr + CSR13);
1965 } else {
1966 /* Select 100mbps port to check for link beat. */
1967 new_csr6 = 0x83860000;
1968 dev->if_port = 3;
1969 outl(0, ioaddr + CSR13);
1970 outl(0x0003FF7F, ioaddr + CSR14);
1971 outw(8, ioaddr + CSR15);
1972 outl(1, ioaddr + CSR13);
1974 if (tulip_debug > 1)
1975 printk(KERN_INFO"%s: Testing new 21143 media %s.\n",
1976 dev->name, medianame[dev->if_port]);
1977 if (new_csr6 != (tp->csr6 & ~0x00D5)) {
1978 tp->csr6 &= 0x00D5;
1979 tp->csr6 |= new_csr6;
1980 outl(0x0301, ioaddr + CSR12);
1981 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
1982 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
1984 next_tick = 3*HZ;
1986 if (tp->cur_tx - tp->dirty_tx > 0 &&
1987 jiffies - dev->trans_start > TX_TIMEOUT) {
1988 printk(KERN_WARNING "%s: Tx hung, %d vs. %d.\n",
1989 dev->name, tp->cur_tx, tp->dirty_tx);
1990 tulip_tx_timeout(dev);
1993 tp->timer.expires = RUN_AT(next_tick);
1994 add_timer(&tp->timer);
1997 static void t21142_start_nway(struct net_device *dev)
1999 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2000 long ioaddr = dev->base_addr;
2001 int csr14 = ((tp->to_advertise & 0x0180) << 9) |
2002 ((tp->to_advertise&0x0020)<<1) | 0xffbf;
2004 dev->if_port = 0;
2005 tp->nway = tp->mediasense = 1;
2006 tp->nwayset = tp->lpar = 0;
2007 if (debug > 1)
2008 printk(KERN_DEBUG "%s: Restarting 21143 autonegotiation, %8.8x.\n",
2009 dev->name, csr14);
2010 outl(0x0001, ioaddr + CSR13);
2011 outl(csr14, ioaddr + CSR14);
2012 tp->csr6 = 0x82420000 | (tp->to_advertise & 0x0040 ? 0x0200 : 0);
2013 outl_CSR6(tp->csr6, ioaddr, tp->chip_id);
2014 if (tp->mtable && tp->mtable->csr15dir) {
2015 outl(tp->mtable->csr15dir, ioaddr + CSR15);
2016 outl(tp->mtable->csr15val, ioaddr + CSR15);
2017 } else
2018 outw(0x0008, ioaddr + CSR15);
2019 outl(0x1301, ioaddr + CSR12); /* Trigger NWAY. */
2022 static void t21142_lnk_change(struct net_device *dev, int csr5)
2024 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2025 long ioaddr = dev->base_addr;
2026 int csr12 = inl(ioaddr + CSR12);
2028 if (tulip_debug > 1)
2029 printk(KERN_INFO"%s: 21143 link status interrupt %8.8x, CSR5 %x, "
2030 "%8.8x.\n", dev->name, csr12, csr5, inl(ioaddr + CSR14));
2032 /* If NWay finished and we have a negotiated partner capability. */
2033 if (tp->nway && !tp->nwayset && (csr12 & 0x7000) == 0x5000) {
2034 int setup_done = 0;
2035 tp->lpar = csr12 >> 16;
2036 tp->nwayset = 1;
2037 if (csr12 & 0x01000000) dev->if_port = 5;
2038 else if (csr12 & 0x00800000) dev->if_port = 3;
2039 else if (csr12 & 0x00400000) dev->if_port = 4;
2040 else if (csr12 & 0x00200000) dev->if_port = 0;
2041 else {
2042 tp->nwayset = 0;
2043 if ( ! (csr12 & 2)) dev->if_port = 3;
2044 else if ( ! (csr12 & 4)) dev->if_port = 0;
2046 tp->full_duplex = (media_cap[tp->default_port] & MediaAlwaysFD) ? 1:0;
2048 if (tulip_debug > 1) {
2049 if (tp->nwayset)
2050 printk(KERN_INFO "%s: Switching to %s based on link partner "
2051 "advertisement %4.4x.\n",
2052 dev->name, medianame[dev->if_port], tp->lpar);
2053 else
2054 printk(KERN_INFO "%s: Switching to %s based on link beat "
2055 "status of %4.4x.\n",
2056 dev->name, medianame[dev->if_port], csr12);
2059 if (tp->mtable) {
2060 int i;
2061 for (i = 0; i < tp->mtable->leafcount; i++)
2062 if (tp->mtable->mleaf[i].media == dev->if_port) {
2063 tp->cur_index = i;
2064 select_media(dev, 0);
2065 setup_done = 1;
2066 break;
2069 if ( ! setup_done) {
2070 tp->csr6 = dev->if_port & 1 ? 0x83860000 : 0x82420000;
2071 if (tp->full_duplex)
2072 tp->csr6 |= 0x0200;
2073 outw(0x0000, ioaddr + CSR13);
2074 outw(0x0000, ioaddr + CSR14);
2076 outl_CSR6(tp->csr6 | 0x0000, ioaddr, tp->chip_id);
2077 if (debug > 2)
2078 printk(KERN_DEBUG "%s: Restarting Tx and Rx, CSR5 is %8.8x.\n",
2079 dev->name, inl(ioaddr + CSR5));
2080 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
2081 } else if ((tp->nwayset && (csr5 & 0x08000000)
2082 && (dev->if_port == 3 || dev->if_port == 5)
2083 && (csr12 & 2) == 2) ||
2084 (tp->nway && (csr5 & (TPLnkFail)))) {
2085 /* Link blew? Maybe restart NWay. */
2086 del_timer(&tp->timer);
2087 t21142_start_nway(dev);
2088 tp->timer.expires = RUN_AT(3*HZ);
2089 add_timer(&tp->timer);
2090 } else if (dev->if_port == 3 || dev->if_port == 5) {
2091 if (tulip_debug > 1)
2092 printk(KERN_INFO"%s: 21143 %s link beat %s.\n",
2093 dev->name, medianame[dev->if_port],
2094 (csr12 & 2) ? "failed" : "good");
2095 if ((csr12 & 2) && ! tp->medialock) {
2096 del_timer(&tp->timer);
2097 t21142_start_nway(dev);
2098 tp->timer.expires = RUN_AT(3*HZ);
2099 add_timer(&tp->timer);
2101 } else if (dev->if_port == 0 || dev->if_port == 4) {
2102 if ((csr12 & 4) == 0)
2103 printk(KERN_INFO"%s: 21143 10baseT link beat good.\n",
2104 dev->name);
2105 } else if (!(csr12 & 4)) { /* 10mbps link beat good. */
2106 if (tulip_debug)
2107 printk(KERN_INFO"%s: 21143 10mbps sensed media.\n",
2108 dev->name);
2109 dev->if_port = 0;
2110 } else if (tp->nwayset) {
2111 if (tulip_debug)
2112 printk(KERN_INFO"%s: 21143 using NWay-set %s, csr6 %8.8x.\n",
2113 dev->name, medianame[dev->if_port], tp->csr6);
2114 } else { /* 100mbps link beat good. */
2115 if (tulip_debug)
2116 printk(KERN_INFO"%s: 21143 100baseTx sensed media.\n",
2117 dev->name);
2118 dev->if_port = 3;
2119 tp->csr6 = 0x83860000;
2120 outl(0x0003FF7F, ioaddr + CSR14);
2121 outl(0x0301, ioaddr + CSR12);
2122 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
2123 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
2127 static void mxic_timer(unsigned long data)
2129 struct net_device *dev = (struct net_device *)data;
2130 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2131 long ioaddr = dev->base_addr;
2132 int next_tick = 60*HZ;
2134 if (tulip_debug > 3) {
2135 printk(KERN_INFO"%s: MXIC negotiation status %8.8x.\n", dev->name,
2136 inl(ioaddr + CSR12));
2138 if (next_tick) {
2139 tp->timer.expires = RUN_AT(next_tick);
2140 add_timer(&tp->timer);
2144 static void pnic_timer(unsigned long data)
2146 struct net_device *dev = (struct net_device *)data;
2147 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2148 long ioaddr = dev->base_addr;
2149 int csr12 = inl(ioaddr + CSR12);
2150 int next_tick = 60*HZ;
2151 int new_csr6 = tp->csr6 & ~0x40C40200;
2153 if (media_cap[dev->if_port] & MediaIsMII) {
2154 int negotiated = mdio_read(dev, tp->phys[0], 5) & tp->advertising[0];
2156 if (tulip_debug > 1)
2157 printk(KERN_DEBUG "%s: PNIC negotiated capability %8.8x, "
2158 "CSR5 %8.8x.\n",
2159 dev->name, negotiated, inl(ioaddr + CSR5));
2161 if (negotiated & 0x0380) /* 10 vs 100mbps */
2162 new_csr6 |= 0x810E0000;
2163 else
2164 new_csr6 |= 0x814E0000;
2165 if (((negotiated & 0x0300) == 0x0100) /* Duplex */
2166 || (negotiated & 0x00C0) == 0x0040
2167 || tp->full_duplex_lock) {
2168 tp->full_duplex = 1;
2169 new_csr6 |= 0x0200;
2171 if (tulip_debug > 1)
2172 printk(KERN_DEBUG "%s: PNIC MII PHY status %4.4x, Link "
2173 "partner report %4.4x, csr6 %8.8x/%8.8x.\n",
2174 dev->name, mdio_read(dev, tp->phys[0], 1), negotiated,
2175 tp->csr6, inl(ioaddr + CSR6));
2176 } else {
2177 int phy_reg = inl(ioaddr + 0xB8);
2178 int csr5 = inl(ioaddr + CSR5);
2180 if (tulip_debug > 1)
2181 printk(KERN_DEBUG "%s: PNIC PHY status %8.8x, CSR5 %8.8x.\n",
2182 dev->name, phy_reg, csr5);
2184 if (phy_reg & 0x04000000) { /* Remote link fault */
2185 /*outl(0x0201F078, ioaddr + 0xB8);*/
2186 next_tick = 3*HZ;
2188 if (inl(ioaddr + CSR5) & TPLnkFail) { /* 100baseTx link beat */
2189 if (tulip_debug > 1)
2190 printk(KERN_DEBUG "%s: %s link beat failed, CSR12 %4.4x, "
2191 "CSR5 %8.8x, PHY %3.3x.\n",
2192 dev->name, medianame[dev->if_port], csr12,
2193 inl(ioaddr + CSR5), inl(ioaddr + 0xB8));
2194 if (tp->medialock) {
2195 } else if (dev->if_port == 0) {
2196 dev->if_port = 3;
2197 outl(0x33, ioaddr + CSR12);
2198 new_csr6 = 0x01860000;
2199 outl(0x1F868, ioaddr + 0xB8);
2200 } else {
2201 dev->if_port = 0;
2202 outl(0x32, ioaddr + CSR12);
2203 new_csr6 = 0x00420000;
2204 outl(0x1F078, ioaddr + 0xB8);
2206 new_csr6 |= (tp->csr6 & 0xfdff);
2207 next_tick = 3*HZ;
2208 } else
2209 new_csr6 = tp->csr6;
2210 if (tp->full_duplex_lock || (phy_reg & 0x30000000) != 0) {
2211 tp->full_duplex = 1;
2212 new_csr6 |= 0x00000200;
2215 if (tp->csr6 != new_csr6) {
2216 tp->csr6 = new_csr6;
2217 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id); /* Restart Tx */
2218 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
2219 dev->trans_start = jiffies;
2220 if (tulip_debug > 1)
2221 printk(KERN_INFO "%s: Changing PNIC configuration to %s-duplex, "
2222 "CSR6 %8.8x.\n",
2223 dev->name, tp->full_duplex ? "full" : "half", new_csr6);
2225 tp->timer.expires = RUN_AT(next_tick);
2226 add_timer(&tp->timer);
2229 static void comet_timer(unsigned long data)
2231 struct net_device *dev = (struct net_device *)data;
2232 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2233 long ioaddr = dev->base_addr;
2234 int next_tick = 60*HZ;
2236 if (tulip_debug > 1)
2237 printk(KERN_DEBUG "%s: Comet link status %4.4x partner capability "
2238 "%4.4x.\n",
2239 dev->name, inl(ioaddr + 0xB8), inl(ioaddr + 0xC8));
2240 tp->timer.expires = RUN_AT(next_tick);
2241 add_timer(&tp->timer);
2244 static void tulip_tx_timeout(struct net_device *dev)
2246 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2247 long ioaddr = dev->base_addr;
2249 if (media_cap[dev->if_port] & MediaIsMII) {
2250 /* Do nothing -- the media monitor should handle this. */
2251 if (tulip_debug > 1)
2252 printk(KERN_WARNING "%s: Transmit timeout using MII device.\n",
2253 dev->name);
2254 } else if (tp->chip_id == DC21040) {
2255 if ( !tp->medialock && inl(ioaddr + CSR12) & 0x0002) {
2256 dev->if_port = (dev->if_port == 2 ? 0 : 2);
2257 printk(KERN_INFO "%s: transmit timed out, switching to "
2258 "%s.\n",
2259 dev->name, medianame[dev->if_port]);
2260 select_media(dev, 0);
2262 dev->trans_start = jiffies;
2263 return;
2264 } else if (tp->chip_id == DC21041) {
2265 int csr12 = inl(ioaddr + CSR12);
2267 printk(KERN_WARNING "%s: 21041 transmit timed out, status %8.8x, "
2268 "CSR12 %8.8x, CSR13 %8.8x, CSR14 %8.8x, resetting...\n",
2269 dev->name, inl(ioaddr + CSR5), csr12,
2270 inl(ioaddr + CSR13), inl(ioaddr + CSR14));
2271 tp->mediasense = 1;
2272 if ( ! tp->medialock) {
2273 if (dev->if_port == 1 || dev->if_port == 2)
2274 if (csr12 & 0x0004) {
2275 dev->if_port = 2 - dev->if_port;
2276 } else
2277 dev->if_port = 0;
2278 else
2279 dev->if_port = 1;
2280 select_media(dev, 0);
2282 } else if (tp->chip_id == DC21140 || tp->chip_id == DC21142
2283 || tp->chip_id == MX98713 || tp->chip_id == COMPEX9881) {
2284 printk(KERN_WARNING "%s: 21140 transmit timed out, status %8.8x, "
2285 "SIA %8.8x %8.8x %8.8x %8.8x, resetting...\n",
2286 dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12),
2287 inl(ioaddr + CSR13), inl(ioaddr + CSR14), inl(ioaddr + CSR15));
2288 if ( ! tp->medialock && tp->mtable) {
2290 --tp->cur_index;
2291 while (tp->cur_index >= 0
2292 && (media_cap[tp->mtable->mleaf[tp->cur_index].media]
2293 & MediaIsFD));
2294 if (--tp->cur_index < 0) {
2295 /* We start again, but should instead look for default. */
2296 tp->cur_index = tp->mtable->leafcount - 1;
2298 select_media(dev, 0);
2299 printk(KERN_WARNING "%s: transmit timed out, switching to %s "
2300 "media.\n", dev->name, medianame[dev->if_port]);
2302 } else {
2303 printk(KERN_WARNING "%s: Transmit timed out, status %8.8x, CSR12 "
2304 "%8.8x, resetting...\n",
2305 dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12));
2306 dev->if_port = 0;
2309 #if defined(way_too_many_messages)
2310 if (tulip_debug > 3) {
2311 int i;
2312 for (i = 0; i < RX_RING_SIZE; i++) {
2313 u8 *buf = (u8 *)(tp->rx_ring[i].buffer1);
2314 int j;
2315 printk(KERN_DEBUG "%2d: %8.8x %8.8x %8.8x %8.8x "
2316 "%2.2x %2.2x %2.2x.\n",
2317 i, (unsigned int)tp->rx_ring[i].status,
2318 (unsigned int)tp->rx_ring[i].length,
2319 (unsigned int)tp->rx_ring[i].buffer1,
2320 (unsigned int)tp->rx_ring[i].buffer2,
2321 buf[0], buf[1], buf[2]);
2322 for (j = 0; buf[j] != 0xee && j < 1600; j++)
2323 if (j < 100) printk(" %2.2x", buf[j]);
2324 printk(" j=%d.\n", j);
2326 printk(KERN_DEBUG " Rx ring %8.8x: ", (int)tp->rx_ring);
2327 for (i = 0; i < RX_RING_SIZE; i++)
2328 printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
2329 printk("\n" KERN_DEBUG " Tx ring %8.8x: ", (int)tp->tx_ring);
2330 for (i = 0; i < TX_RING_SIZE; i++)
2331 printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
2332 printk("\n");
2334 #endif
2336 /* Stop and restart the chip's Tx processes . */
2337 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
2338 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
2339 /* Trigger an immediate transmit demand. */
2340 outl(0, ioaddr + CSR1);
2342 dev->trans_start = jiffies;
2343 netif_wake_queue (dev);
2344 tp->stats.tx_errors++;
2347 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
2348 static void tulip_init_ring(struct net_device *dev)
2350 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2351 int i;
2353 tp->tx_full = 0;
2354 tp->cur_rx = tp->cur_tx = 0;
2355 tp->dirty_rx = tp->dirty_tx = 0;
2357 for (i = 0; i < RX_RING_SIZE; i++) {
2358 tp->rx_ring[i].status = 0x00000000;
2359 tp->rx_ring[i].length = PKT_BUF_SZ;
2360 tp->rx_ring[i].buffer2 = virt_to_bus(&tp->rx_ring[i+1]);
2361 tp->rx_skbuff[i] = NULL;
2363 /* Mark the last entry as wrapping the ring. */
2364 tp->rx_ring[i-1].length = PKT_BUF_SZ | DESC_RING_WRAP;
2365 tp->rx_ring[i-1].buffer2 = virt_to_bus(&tp->rx_ring[0]);
2367 for (i = 0; i < RX_RING_SIZE; i++) {
2368 /* Note the receive buffer must be longword aligned.
2369 dev_alloc_skb() provides 16 byte alignment. But do *not*
2370 use skb_reserve() to align the IP header! */
2371 struct sk_buff *skb = dev_alloc_skb(PKT_BUF_SZ);
2372 tp->rx_skbuff[i] = skb;
2373 if (skb == NULL)
2374 break;
2375 skb->dev = dev; /* Mark as being used by this device. */
2376 tp->rx_ring[i].status = DescOwned; /* Owned by Tulip chip */
2377 tp->rx_ring[i].buffer1 = virt_to_bus(skb->tail);
2379 tp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
2381 /* The Tx buffer descriptor is filled in as needed, but we
2382 do need to clear the ownership bit. */
2383 for (i = 0; i < TX_RING_SIZE; i++) {
2384 tp->tx_skbuff[i] = 0;
2385 tp->tx_ring[i].status = 0x00000000;
2386 tp->tx_ring[i].buffer2 = virt_to_bus(&tp->tx_ring[i+1]);
2387 #ifdef CARDBUS
2388 if (tp->chip_id == X3201_3)
2389 tp->tx_aligned_skbuff[i] = dev_alloc_skb(PKT_BUF_SZ);
2390 #endif CARDBUS
2392 tp->tx_ring[i-1].buffer2 = virt_to_bus(&tp->tx_ring[0]);
2395 static int
2396 tulip_start_xmit(struct sk_buff *skb, struct net_device *dev)
2398 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2399 int entry;
2400 u32 flag;
2402 /* Caution: the write order is important here, set the base address
2403 with the "ownership" bits last. */
2405 /* Calculate the next Tx descriptor entry. */
2406 entry = tp->cur_tx % TX_RING_SIZE;
2408 tp->tx_skbuff[entry] = skb;
2409 #ifdef CARDBUS
2410 if (tp->chip_id == X3201_3) {
2411 memcpy(tp->tx_aligned_skbuff[entry]->data,skb->data,skb->len);
2412 tp->tx_ring[entry].buffer1 = virt_to_bus(tp->tx_aligned_skbuff[entry]->data);
2413 } else
2414 #endif
2415 tp->tx_ring[entry].buffer1 = virt_to_bus(skb->data);
2417 if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE/2) {/* Typical path */
2418 flag = 0x60000000; /* No interrupt */
2419 } else if (tp->cur_tx - tp->dirty_tx == TX_RING_SIZE/2) {
2420 flag = 0xe0000000; /* Tx-done intr. */
2421 } else if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE - 2) {
2422 flag = 0x60000000; /* No Tx-done intr. */
2423 } else {
2424 /* Leave room for set_rx_mode() to fill entries. */
2425 flag = 0xe0000000; /* Tx-done intr. */
2426 tp->tx_full = 1;
2428 if (entry == TX_RING_SIZE-1)
2429 flag |= 0xe0000000 | DESC_RING_WRAP;
2431 tp->tx_ring[entry].length = skb->len | flag;
2432 tp->tx_ring[entry].status = DescOwned; /* Pass ownership to the chip. */
2433 tp->cur_tx++;
2434 if (tp->tx_full)
2435 netif_stop_queue (dev);
2436 else
2437 netif_wake_queue (dev);
2439 /* Trigger an immediate transmit demand. */
2440 outl(0, dev->base_addr + CSR1);
2442 dev->trans_start = jiffies;
2444 return 0;
2447 /* The interrupt handler does all of the Rx thread work and cleans up
2448 after the Tx thread. */
2449 static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
2451 struct net_device *dev = (struct net_device *)dev_instance;
2452 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2453 long ioaddr = dev->base_addr;
2454 int csr5, work_budget = max_interrupt_work;
2456 spin_lock (&tp->lock);
2458 do {
2459 csr5 = inl(ioaddr + CSR5);
2460 /* Acknowledge all of the current interrupt sources ASAP. */
2461 outl(csr5 & 0x0001ffff, ioaddr + CSR5);
2463 if (tulip_debug > 4)
2464 printk(KERN_DEBUG "%s: interrupt csr5=%#8.8x new csr5=%#8.8x.\n",
2465 dev->name, csr5, inl(dev->base_addr + CSR5));
2467 if (csr5 == 0xffffffff)
2468 break; /* all bits set, assume PCMCIA card removed */
2470 if ((csr5 & (NormalIntr|AbnormalIntr)) == 0)
2471 break;
2473 if (csr5 & (RxIntr | RxNoBuf))
2474 work_budget -= tulip_rx(dev);
2476 if (csr5 & (TxNoBuf | TxDied | TxIntr)) {
2477 unsigned int dirty_tx;
2479 for (dirty_tx = tp->dirty_tx; tp->cur_tx - dirty_tx > 0;
2480 dirty_tx++) {
2481 int entry = dirty_tx % TX_RING_SIZE;
2482 int status = tp->tx_ring[entry].status;
2484 if (status < 0)
2485 break; /* It still hasn't been Txed */
2486 /* Check for Rx filter setup frames. */
2487 if (tp->tx_skbuff[entry] == NULL)
2488 continue;
2490 if (status & 0x8000) {
2491 /* There was an major error, log it. */
2492 #ifndef final_version
2493 if (tulip_debug > 1)
2494 printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
2495 dev->name, status);
2496 #endif
2497 tp->stats.tx_errors++;
2498 if (status & 0x4104) tp->stats.tx_aborted_errors++;
2499 if (status & 0x0C00) tp->stats.tx_carrier_errors++;
2500 if (status & 0x0200) tp->stats.tx_window_errors++;
2501 if (status & 0x0002) tp->stats.tx_fifo_errors++;
2502 if ((status & 0x0080) && tp->full_duplex == 0)
2503 tp->stats.tx_heartbeat_errors++;
2504 #ifdef ETHER_STATS
2505 if (status & 0x0100) tp->stats.collisions16++;
2506 #endif
2507 } else {
2508 #ifdef ETHER_STATS
2509 if (status & 0x0001) tp->stats.tx_deferred++;
2510 #endif
2511 tp->stats.tx_bytes += tp->tx_ring[entry].length & 0x7ff;
2512 tp->stats.collisions += (status >> 3) & 15;
2513 tp->stats.tx_packets++;
2516 /* Free the original skb. */
2517 dev_kfree_skb_irq(tp->tx_skbuff[entry]);
2518 tp->tx_skbuff[entry] = 0;
2521 #ifndef final_version
2522 if (tp->cur_tx - dirty_tx > TX_RING_SIZE) {
2523 printk(KERN_ERR "%s: Out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
2524 dev->name, dirty_tx, tp->cur_tx, tp->tx_full);
2525 dirty_tx += TX_RING_SIZE;
2527 #endif
2529 if (tp->tx_full &&
2530 tp->cur_tx - dirty_tx < TX_RING_SIZE - 2)
2531 /* The ring is no longer full */
2532 tp->tx_full = 0;
2534 if (tp->tx_full)
2535 netif_stop_queue (dev);
2536 else
2537 netif_wake_queue (dev);
2539 tp->dirty_tx = dirty_tx;
2540 if (csr5 & TxDied) {
2541 if (tulip_debug > 2)
2542 printk(KERN_WARNING "%s: The transmitter stopped."
2543 " CSR5 is %x, CSR6 %x, new CSR6 %x.\n",
2544 dev->name, csr5, inl(ioaddr + CSR6), tp->csr6);
2545 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
2546 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
2550 /* Log errors. */
2551 if (csr5 & AbnormalIntr) { /* Abnormal error summary bit. */
2552 if (csr5 == 0xffffffff)
2553 break;
2554 if (csr5 & TxJabber) tp->stats.tx_errors++;
2555 if (csr5 & TxFIFOUnderflow) {
2556 if ((tp->csr6 & 0xC000) != 0xC000)
2557 tp->csr6 += 0x4000; /* Bump up the Tx threshold */
2558 else
2559 tp->csr6 |= 0x00200000; /* Store-n-forward. */
2560 /* Restart the transmit process. */
2561 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
2562 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
2564 if (csr5 & RxDied) { /* Missed a Rx frame. */
2565 tp->stats.rx_errors++;
2566 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2567 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
2569 if (csr5 & TimerInt) {
2570 if (tulip_debug > 2)
2571 printk(KERN_ERR "%s: Re-enabling interrupts, %8.8x.\n",
2572 dev->name, csr5);
2573 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
2575 if (csr5 & (TPLnkPass | TPLnkFail | 0x08000000)) {
2576 if ( tp->chip_id == DC21142)
2577 t21142_lnk_change(dev, csr5);
2579 /* Clear all error sources, included undocumented ones! */
2580 outl(0x0800f7ba, ioaddr + CSR5);
2582 if (--work_budget < 0) {
2583 if (tulip_debug > 1)
2584 printk(KERN_WARNING "%s: Too much work during an interrupt, "
2585 "csr5=0x%8.8x.\n", dev->name, csr5);
2586 /* Acknowledge all interrupt sources. */
2587 outl(0x8001ffff, ioaddr + CSR5);
2588 #ifdef notdef
2589 /* Clear all but standard interrupt sources. */
2590 outl((~csr5) & 0x0001ebef, ioaddr + CSR7);
2591 #endif
2592 break;
2594 } while (1);
2596 if (tulip_debug > 3)
2597 printk(KERN_DEBUG "%s: exiting interrupt, csr5=%#4.4x.\n",
2598 dev->name, inl(ioaddr + CSR5));
2600 spin_unlock (&tp->lock);
2603 static int
2604 tulip_rx(struct net_device *dev)
2606 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2607 int entry = tp->cur_rx % RX_RING_SIZE;
2608 int rx_work_limit = tp->dirty_rx + RX_RING_SIZE - tp->cur_rx;
2609 int work_done = 0;
2611 if (tulip_debug > 4)
2612 printk(KERN_DEBUG " In tulip_rx(), entry %d %8.8x.\n", entry,
2613 tp->rx_ring[entry].status);
2614 /* If we own the next entry, it's a new packet. Send it up. */
2615 while (tp->rx_ring[entry].status >= 0) {
2616 s32 status = tp->rx_ring[entry].status;
2618 if (tulip_debug > 5)
2619 printk(KERN_DEBUG " In tulip_rx(), entry %d %8.8x.\n", entry,
2620 tp->rx_ring[entry].status);
2621 if (--rx_work_limit < 0)
2622 break;
2623 if ((status & 0x38008300) != 0x0300) {
2624 if ((status & 0x38000300) != 0x0300) {
2625 /* Ingore earlier buffers. */
2626 if ((status & 0xffff) != 0x7fff) {
2627 if (tulip_debug > 1)
2628 printk(KERN_WARNING "%s: Oversized Ethernet frame "
2629 "spanned multiple buffers, status %8.8x!\n",
2630 dev->name, status);
2631 tp->stats.rx_length_errors++;
2633 } else if (status & RxDescFatalErr) {
2634 /* There was a fatal error. */
2635 if (tulip_debug > 2)
2636 printk(KERN_DEBUG "%s: Receive error, Rx status %8.8x.\n",
2637 dev->name, status);
2638 tp->stats.rx_errors++; /* end of a packet.*/
2639 if (status & 0x0890) tp->stats.rx_length_errors++;
2640 if (status & 0x0004) tp->stats.rx_frame_errors++;
2641 if (status & 0x0002) tp->stats.rx_crc_errors++;
2642 if (status & 0x0001) tp->stats.rx_fifo_errors++;
2644 } else {
2645 /* Omit the four octet CRC from the length. */
2646 short pkt_len = ((status >> 16) & 0x7ff) - 4;
2647 struct sk_buff *skb;
2649 #ifndef final_version
2650 if (pkt_len > 1518) {
2651 printk(KERN_WARNING "%s: Bogus packet size of %d (%#x).\n",
2652 dev->name, pkt_len, pkt_len);
2653 pkt_len = 1518;
2654 tp->stats.rx_length_errors++;
2656 #endif
2657 /* Check if the packet is long enough to accept without copying
2658 to a minimally-sized skbuff. */
2659 if (pkt_len < rx_copybreak
2660 && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
2661 skb->dev = dev;
2662 skb_reserve(skb, 2); /* 16 byte align the IP header */
2663 #if ! defined(__alpha__)
2664 eth_copy_and_sum(skb, bus_to_virt(tp->rx_ring[entry].buffer1),
2665 pkt_len, 0);
2666 skb_put(skb, pkt_len);
2667 #else
2668 memcpy(skb_put(skb, pkt_len),
2669 bus_to_virt(tp->rx_ring[entry].buffer1), pkt_len);
2670 #endif
2671 work_done++;
2672 } else { /* Pass up the skb already on the Rx ring. */
2673 char *temp = skb_put(skb = tp->rx_skbuff[entry], pkt_len);
2674 tp->rx_skbuff[entry] = NULL;
2675 #ifndef final_version
2676 if (bus_to_virt(tp->rx_ring[entry].buffer1) != temp)
2677 printk(KERN_ERR "%s: Internal fault: The skbuff addresses "
2678 "do not match in tulip_rx: %p vs. %p / %p.\n",
2679 dev->name, bus_to_virt(tp->rx_ring[entry].buffer1),
2680 skb->head, temp);
2681 #endif
2683 skb->protocol = eth_type_trans(skb, dev);
2684 netif_rx(skb);
2685 dev->last_rx = jiffies;
2686 tp->stats.rx_packets++;
2687 tp->stats.rx_bytes += pkt_len;
2689 entry = (++tp->cur_rx) % RX_RING_SIZE;
2692 /* Refill the Rx ring buffers. */
2693 for (; tp->cur_rx - tp->dirty_rx > 0; tp->dirty_rx++) {
2694 entry = tp->dirty_rx % RX_RING_SIZE;
2695 if (tp->rx_skbuff[entry] == NULL) {
2696 struct sk_buff *skb;
2697 skb = tp->rx_skbuff[entry] = dev_alloc_skb(PKT_BUF_SZ);
2698 if (skb == NULL)
2699 break;
2700 skb->dev = dev; /* Mark as being used by this device. */
2701 tp->rx_ring[entry].buffer1 = virt_to_bus(skb->tail);
2702 work_done++;
2704 tp->rx_ring[entry].status = DescOwned;
2707 return work_done;
2710 static void
2711 tulip_down(struct net_device *dev)
2713 long ioaddr = dev->base_addr;
2714 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2716 /* Disable interrupts by clearing the interrupt mask. */
2717 outl(0x00000000, ioaddr + CSR7);
2718 /* Stop the chip's Tx and Rx processes. */
2719 outl_CSR6(inl(ioaddr + CSR6) & ~0x2002, ioaddr, tp->chip_id);
2720 /* 21040 -- Leave the card in 10baseT state. */
2721 if (tp->chip_id == DC21040)
2722 outl(0x00000004, ioaddr + CSR13);
2724 if (inl(ioaddr + CSR6) != 0xffffffff)
2725 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2727 dev->if_port = tp->saved_if_port;
2730 static int
2731 tulip_close(struct net_device *dev)
2733 long ioaddr = dev->base_addr;
2734 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2735 int i;
2737 if (tulip_debug > 1)
2738 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
2739 dev->name, inl(ioaddr + CSR5));
2741 netif_stop_queue(dev);
2743 if (netif_device_present(dev))
2744 tulip_down(dev);
2746 del_timer(&tp->timer);
2748 free_irq(dev->irq, dev);
2750 /* Free all the skbuffs in the Rx queue. */
2751 for (i = 0; i < RX_RING_SIZE; i++) {
2752 struct sk_buff *skb = tp->rx_skbuff[i];
2753 tp->rx_skbuff[i] = 0;
2754 tp->rx_ring[i].status = 0; /* Not owned by Tulip chip. */
2755 tp->rx_ring[i].length = 0;
2756 tp->rx_ring[i].buffer1 = 0xBADF00D0; /* An invalid address. */
2757 if (skb) {
2758 dev_kfree_skb(skb);
2761 for (i = 0; i < TX_RING_SIZE; i++) {
2762 if (tp->tx_skbuff[i])
2763 dev_kfree_skb(tp->tx_skbuff[i]);
2764 tp->tx_skbuff[i] = 0;
2767 MOD_DEC_USE_COUNT;
2768 tp->open = 0;
2769 return 0;
2772 static struct net_device_stats *tulip_get_stats(struct net_device *dev)
2774 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2775 long ioaddr = dev->base_addr;
2777 if (netif_device_present(dev))
2778 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2780 return &tp->stats;
2783 #ifdef HAVE_PRIVATE_IOCTL
2784 /* Provide ioctl() calls to examine the MII xcvr state. */
2785 static int private_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2787 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2788 long ioaddr = dev->base_addr;
2789 u16 *data = (u16 *)&rq->ifr_data;
2790 int phy = tp->phys[0] & 0x1f;
2791 long flags;
2793 switch(cmd) {
2794 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
2795 if (tp->mii_cnt)
2796 data[0] = phy;
2797 else if (tp->chip_id == DC21142) /* 21142 pseudo-MII */
2798 data[0] = 32;
2799 else if (tp->chip_id == PNIC2)
2800 data[0] = 32;
2801 else if (tp->chip_id == COMET)
2802 data[0] = 1;
2803 else
2804 return -ENODEV;
2805 return 0;
2806 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
2807 if (data[0] == 32 &&
2808 (tp->chip_id == DC21142 || tp->chip_id == PNIC2)) {
2809 int csr12 = inl(ioaddr + CSR12);
2810 int csr14 = inl(ioaddr + CSR14);
2811 switch (data[1]) {
2812 case 0: {
2813 data[3] = (csr14<<5) & 0x1000;
2814 break; }
2815 case 1:
2816 data[3] = 0x7848 + ((csr12&0x7000) == 0x5000 ? 0x20 : 0)
2817 + (csr12&0x06 ? 0x04 : 0);
2818 break;
2819 case 4: {
2820 data[3] = ((csr14>>9)&0x0380) +
2821 ((inl(ioaddr + CSR6)>>3)&0x0040) +((csr14>>1)&0x20) + 1;
2822 break;
2824 case 5: data[3] = csr12 >> 16; break;
2825 default: data[3] = 0; break;
2827 } else {
2828 save_flags(flags);
2829 cli();
2830 data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
2831 restore_flags(flags);
2833 return 0;
2834 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
2835 #if defined(CAP_NET_ADMIN)
2836 if (!capable(CAP_NET_ADMIN))
2837 return -EPERM;
2838 #else
2839 if (!suser())
2840 return -EPERM;
2841 #endif
2842 if (data[0] == 32 && tp->chip_id == DC21142) {
2843 if (data[1] == 5)
2844 tp->to_advertise = data[2];
2845 } else {
2846 save_flags(flags);
2847 cli();
2848 mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
2849 restore_flags(flags);
2851 return 0;
2852 default:
2853 return -EOPNOTSUPP;
2856 return -EOPNOTSUPP;
2858 #endif /* HAVE_PRIVATE_IOCTL */
2860 /* Set or clear the multicast filter for this adaptor.
2861 Note that we only use exclusion around actually queueing the
2862 new frame, not around filling tp->setup_frame. This is non-deterministic
2863 when re-entered but still correct. */
2865 /* The little-endian AUTODIN32 ethernet CRC calculation.
2866 N.B. Do not use for bulk data, use a table-based routine instead.
2867 This is common code and should be moved to net/core/crc.c */
2868 static unsigned const ethernet_polynomial_le = 0xedb88320U;
2869 static inline u32 ether_crc_le(int length, unsigned char *data)
2871 u32 crc = 0xffffffff; /* Initial value. */
2872 while(--length >= 0) {
2873 unsigned char current_octet = *data++;
2874 int bit;
2875 for (bit = 8; --bit >= 0; current_octet >>= 1) {
2876 if ((crc ^ current_octet) & 1) {
2877 crc >>= 1;
2878 crc ^= ethernet_polynomial_le;
2879 } else
2880 crc >>= 1;
2883 return crc;
2885 static unsigned const ethernet_polynomial = 0x04c11db7U;
2886 static inline u32 ether_crc(int length, unsigned char *data)
2888 int crc = -1;
2890 while(--length >= 0) {
2891 unsigned char current_octet = *data++;
2892 int bit;
2893 for (bit = 0; bit < 8; bit++, current_octet >>= 1)
2894 crc = (crc << 1) ^
2895 ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
2897 return crc;
2900 static void set_rx_mode(struct net_device *dev)
2902 long ioaddr = dev->base_addr;
2903 int csr6 = inl(ioaddr + CSR6) & ~0x00D5;
2904 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2906 tp->csr6 &= ~0x00D5;
2907 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2908 tp->csr6 |= 0x00C0;
2909 csr6 |= 0x00C0;
2910 /* Unconditionally log net taps. */
2911 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
2912 } else if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
2913 /* Too many to filter well -- accept all multicasts. */
2914 tp->csr6 |= 0x0080;
2915 csr6 |= 0x0080;
2916 } else if (tulip_tbl[tp->chip_id].flags & MC_HASH_ONLY) {
2917 /* Some work-alikes have only a 64-entry hash filter table. */
2918 /* Should verify correctness on big-endian/__powerpc__ */
2919 struct dev_mc_list *mclist;
2920 int i;
2921 u32 mc_filter[2]; /* Multicast hash filter */
2922 if (dev->mc_count > 64) { /* Arbitrary non-effective limit. */
2923 tp->csr6 |= 0x0080;
2924 csr6 |= 0x0080;
2925 } else {
2926 mc_filter[1] = mc_filter[0] = 0;
2927 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2928 i++, mclist = mclist->next)
2929 set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr)>>26, mc_filter);
2930 if (tp->chip_id == AX88140) {
2931 outl(2, ioaddr + CSR13);
2932 outl(mc_filter[0], ioaddr + CSR14);
2933 outl(3, ioaddr + CSR13);
2934 outl(mc_filter[1], ioaddr + CSR14);
2935 } else if (tp->chip_id == COMET) { /* Has a simple hash filter. */
2936 outl(mc_filter[0], ioaddr + 0xAC);
2937 outl(mc_filter[1], ioaddr + 0xB0);
2940 } else {
2941 u16 *eaddrs, *setup_frm = tp->setup_frame;
2942 struct dev_mc_list *mclist;
2943 u32 tx_flags = 0x08000000 | 192;
2944 int i;
2946 /* Note that only the low-address shortword of setup_frame is valid!
2947 The values are doubled for big-endian architectures. */
2948 if ((dev->mc_count > 14) || ((dev->mc_count > 6) && (tp->chip_id == X3201_3))) { /* Must use a multicast hash table. */
2949 u16 hash_table[32];
2950 tx_flags = 0x08400000 | 192; /* Use hash filter. */
2951 memset(hash_table, 0, sizeof(hash_table));
2952 set_bit(255, hash_table); /* Broadcast entry */
2953 /* This should work on big-endian machines as well. */
2954 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2955 i++, mclist = mclist->next)
2956 set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff,
2957 hash_table);
2958 for (i = 0; i < 32; i++) {
2959 *setup_frm++ = hash_table[i];
2960 *setup_frm++ = hash_table[i];
2962 setup_frm = &tp->setup_frame[13*6];
2963 } else if(tp->chip_id != X3201_3) {
2964 /* We have <= 14 addresses so we can use the wonderful
2965 16 address perfect filtering of the Tulip. */
2966 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
2967 i++, mclist = mclist->next) {
2968 eaddrs = (u16 *)mclist->dmi_addr;
2969 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
2970 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
2971 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
2973 /* Fill the unused entries with the broadcast address. */
2974 memset(setup_frm, 0xff, (15-i)*12);
2975 setup_frm = &tp->setup_frame[15*6];
2976 } else {
2977 /* fill the first two table entries with our address */
2978 eaddrs = (u16 *)dev->dev_addr;
2979 for(i=0; i<2; i++) {
2980 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
2981 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
2982 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
2984 /* Double fill each entry to accomodate chips that */
2985 /* don't like to parse these correctly */
2986 for (i=0, mclist=dev->mc_list; i<dev->mc_count;
2987 i++, mclist=mclist->next) {
2988 eaddrs = (u16 *)mclist->dmi_addr;
2989 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
2990 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
2991 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
2992 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
2993 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
2994 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
2996 i=((i+1)*2);
2997 /* Fill the unused entries with the broadcast address. */
2998 memset(setup_frm, 0xff, (15-i)*12);
2999 setup_frm = &tp->setup_frame[15*6];
3002 /* Fill the final entry with our physical address. */
3003 eaddrs = (u16 *)dev->dev_addr;
3004 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
3005 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
3006 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
3007 /* Now add this frame to the Tx list. */
3008 if (tp->cur_tx - tp->dirty_tx > TX_RING_SIZE - 2) {
3009 /* Same setup recently queued, we need not add it. */
3010 } else {
3011 unsigned long flags;
3012 unsigned int entry, dummy = -1;
3014 save_flags(flags); cli();
3015 entry = tp->cur_tx++ % TX_RING_SIZE;
3017 if (entry != 0) {
3018 /* Avoid a chip errata by prefixing a dummy entry. */
3019 tp->tx_skbuff[entry] = 0;
3020 tp->tx_ring[entry].length =
3021 (entry == TX_RING_SIZE-1) ? DESC_RING_WRAP : 0;
3022 tp->tx_ring[entry].buffer1 = 0;
3023 /* race with chip, set DescOwned later */
3024 dummy = entry;
3025 entry = tp->cur_tx++ % TX_RING_SIZE;
3028 tp->tx_skbuff[entry] = 0;
3029 /* Put the setup frame on the Tx list. */
3030 if (entry == TX_RING_SIZE-1)
3031 tx_flags |= DESC_RING_WRAP; /* Wrap ring. */
3032 tp->tx_ring[entry].length = tx_flags;
3033 if(tp->chip_id == X3201_3)
3034 tp->tx_ring[entry].buffer1 = (virt_to_bus(tp->setup_frame) + 4);
3035 else
3036 tp->tx_ring[entry].buffer1 = virt_to_bus(tp->setup_frame);
3037 tp->tx_ring[entry].status = DescOwned;
3038 if (tp->cur_tx - tp->dirty_tx >= TX_RING_SIZE - 2) {
3039 tp->tx_full = 1;
3040 netif_stop_queue (dev);
3042 if (dummy >= 0)
3043 tp->tx_ring[dummy].status = DescOwned;
3044 restore_flags(flags);
3045 /* Trigger an immediate transmit demand. */
3046 outl(0, ioaddr + CSR1);
3049 outl_CSR6(csr6 | 0x0000, ioaddr, tp->chip_id);
3052 static const struct pci_device_id tulip_pci_table[] __devinitdata = {
3053 { 0x1011, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21040 },
3054 { 0x1011, 0x0014, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21041 },
3055 { 0x1011, 0x0009, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21140 },
3056 { 0x1011, 0x0019, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21142 },
3057 { 0x11AD, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, LC82C168 },
3058 { 0x10d9, 0x0512, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98713 },
3059 { 0x10d9, 0x0531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98715 },
3060 { 0x10d9, 0x0531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98725 },
3061 { 0x125B, 0x1400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AX88140 },
3062 { 0x11AD, 0xc115, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PNIC2 },
3063 { 0x1317, 0x0981, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
3064 { 0x11F6, 0x9881, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMPEX9881 },
3065 { 0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, X3201_3 },
3066 {0},
3069 MODULE_DEVICE_TABLE(pci, tulip_pci_table);
3071 static int __devinit tulip_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3073 struct net_device *dev;
3074 static int board_idx = 0;
3076 printk(KERN_INFO "tulip_attach(%s)\n", pdev->slot_name);
3078 pci_enable_device (pdev);
3079 pci_set_master (pdev);
3080 dev = tulip_probe1(pdev, NULL,
3081 pci_resource_start (pdev, 0), pdev->irq,
3082 id->driver_data, board_idx++);
3083 if (dev) {
3084 pdev->driver_data = dev;
3085 return 0;
3087 return -ENODEV;
3090 static void tulip_suspend(struct pci_dev *pdev)
3092 struct net_device *dev = pdev->driver_data;
3093 struct tulip_private *tp = (struct tulip_private *)dev->priv;
3094 printk(KERN_INFO "tulip_suspend(%s)\n", dev->name);
3095 if (tp->open) tulip_down(dev);
3098 static void tulip_resume(struct pci_dev *pdev)
3100 struct net_device *dev = pdev->driver_data;
3101 struct tulip_private *tp = (struct tulip_private *)dev->priv;
3102 printk(KERN_INFO "tulip_resume(%s)\n", dev->name);
3103 if (tp->open) tulip_up(dev);
3106 static void __devexit tulip_remove(struct pci_dev *pdev)
3108 struct net_device *dev = pdev->driver_data;
3109 struct tulip_private *tp = (struct tulip_private *)dev->priv;
3111 printk(KERN_INFO "tulip_detach(%s)\n", dev->name);
3112 unregister_netdev(dev);
3113 kfree(dev);
3114 kfree(tp);
3117 static struct pci_driver tulip_ops = {
3118 name: "tulip_cb",
3119 id_table: tulip_pci_table,
3120 probe: tulip_pci_probe,
3121 remove: tulip_remove,
3122 suspend: tulip_suspend,
3123 resume: tulip_resume
3126 static int __init tulip_init(void)
3128 pci_register_driver(&tulip_ops);
3129 return 0;
3132 static __exit void tulip_exit(void)
3134 pci_unregister_driver(&tulip_ops);
3137 module_init(tulip_init)
3138 module_exit(tulip_exit)
3142 * Local variables:
3143 * SMP-compile-command: "gcc -D__SMP__ -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c tulip.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
3144 * compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c tulip.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
3145 * 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.9/include/"
3146 * c-indent-level: 4
3147 * c-basic-offset: 4
3148 * tab-width: 4
3149 * End: