Import 2.3.47pre1
[davej-history.git] / drivers / net / tulip.c
bloba20d34ceca0316e6d1613f43bd3f6bd463c1d167
1 /* tulip.c: A DEC 21040-family ethernet driver for Linux. */
2 /*
3 Copyright 2000 The Linux Kernel Team
4 Written/copyright 1994-1999 by Donald Becker.
6 This software may be used and distributed according to the terms
7 of the GNU Public License, incorporated herein by reference.
9 This driver is for the Digital "Tulip" Ethernet adapter interface.
10 It should work with most DEC 21*4*-based chips/ethercards, as well as
11 with work-alike chips from Lite-On (PNIC) and Macronix (MXIC) and ASIX.
13 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
14 Center of Excellence in Space Data and Information Sciences
15 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
17 Additional information available at
18 http://cesdis.gsfc.nasa.gov/linux/drivers/tulip.html
20 For this specific driver variant please use linux-kernel for
21 bug reports.
24 Theory of Operation
26 I. Board Compatibility
28 This device driver is designed for the DECchip "Tulip", Digital's
29 single-chip ethernet controllers for PCI. Supported members of the family
30 are the 21040, 21041, 21140, 21140A, 21142, and 21143. Similar work-alike
31 chips from Lite-On, Macronics, ASIX, Compex and other listed below are also
32 supported.
34 These chips are used on at least 140 unique PCI board designs. The great
35 number of chips and board designs supported is the reason for the
36 driver size and complexity. Almost of the increasing complexity is in the
37 board configuration and media selection code. There is very little
38 increasing in the operational critical path length.
40 II. Board-specific settings
42 PCI bus devices are configured by the system at boot time, so no jumpers
43 need to be set on the board. The system BIOS preferably should assign the
44 PCI INTA signal to an otherwise unused system IRQ line.
46 Some boards have EEPROMs tables with default media entry. The factory default
47 is usually "autoselect". This should only be overridden when using
48 transceiver connections without link beat e.g. 10base2 or AUI, or (rarely!)
49 for forcing full-duplex when used with old link partners that do not do
50 autonegotiation.
52 III. Driver operation
54 IIIa. Ring buffers
56 The Tulip can use either ring buffers or lists of Tx and Rx descriptors.
57 This driver uses statically allocated rings of Rx and Tx descriptors, set at
58 compile time by RX/TX_RING_SIZE. This version of the driver allocates skbuffs
59 for the Rx ring buffers at open() time and passes the skb->data field to the
60 Tulip as receive data buffers. When an incoming frame is less than
61 RX_COPYBREAK bytes long, a fresh skbuff is allocated and the frame is
62 copied to the new skbuff. When the incoming frame is larger, the skbuff is
63 passed directly up the protocol stack and replaced by a newly allocated
64 skbuff.
66 The RX_COPYBREAK value is chosen to trade-off the memory wasted by
67 using a full-sized skbuff for small frames vs. the copying costs of larger
68 frames. For small frames the copying cost is negligible (esp. considering
69 that we are pre-loading the cache with immediately useful header
70 information). For large frames the copying cost is non-trivial, and the
71 larger copy might flush the cache of useful data. A subtle aspect of this
72 choice is that the Tulip only receives into longword aligned buffers, thus
73 the IP header at offset 14 isn't longword aligned for further processing.
74 Copied frames are put into the new skbuff at an offset of "+2", thus copying
75 has the beneficial effect of aligning the IP header and preloading the
76 cache.
78 IIIC. Synchronization
79 The driver runs as two independent, single-threaded flows of control. One
80 is the send-packet routine, which enforces single-threaded use by the
81 dev->tbusy flag. The other thread is the interrupt handler, which is single
82 threaded by the hardware and other software.
84 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
85 flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
86 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
87 the 'tp->tx_full' flag.
89 The interrupt handler has exclusive control over the Rx ring and records stats
90 from the Tx ring. (The Tx-done interrupt can't be selectively turned off, so
91 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
92 stats.) After reaping the stats, it marks the queue entry as empty by setting
93 the 'base' to zero. Iff the 'tp->tx_full' flag is set, it clears both the
94 tx_full and tbusy flags.
96 IV. Notes
98 Thanks to Duke Kamstra of SMC for long ago providing an EtherPower board.
99 Greg LaPolla at Linksys provided PNIC and other Linksys boards.
100 Znyx provided a four-port card for testing.
102 IVb. References
104 http://cesdis.gsfc.nasa.gov/linux/misc/NWay.html
105 http://www.digital.com (search for current 21*4* datasheets and "21X4 SROM")
106 http://www.national.com/pf/DP/DP83840A.html
107 http://www.asix.com.tw/pmac.htm
108 http://www.admtek.com.tw/
110 IVc. Errata
112 The old DEC databooks were light on details.
113 The 21040 databook claims that CSR13, CSR14, and CSR15 should each be the last
114 register of the set CSR12-15 written. Hmmm, now how is that possible?
116 The DEC SROM format is very badly designed not precisely defined, leading to
117 part of the media selection junkheap below. Some boards do not have EEPROM
118 media tables and need to be patched up. Worse, other boards use the DEC
119 design kit media table when it isn't correct for their board.
121 We cannot use MII interrupts because there is no defined GPIO pin to attach
122 them. The MII transceiver status is polled using an kernel timer.
126 static const char version[] = "Linux Tulip driver version 0.9.2 (Feb 15, 2000)\n";
128 #include <linux/module.h>
129 #include <linux/kernel.h>
130 #include <linux/sched.h>
131 #include <linux/string.h>
132 #include <linux/timer.h>
133 #include <linux/ioport.h>
134 #include <linux/malloc.h>
135 #include <linux/interrupt.h>
136 #include <linux/pci.h>
137 #include <linux/netdevice.h>
138 #include <linux/etherdevice.h>
139 #include <linux/skbuff.h>
140 #include <linux/init.h>
141 #include <asm/processor.h> /* Processor type for cache alignment. */
142 #include <asm/bitops.h>
143 #include <asm/io.h>
144 #include <asm/unaligned.h>
148 /* A few user-configurable values. */
150 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
151 static int max_interrupt_work = 25;
153 #define MAX_UNITS 8
154 /* Used to pass the full-duplex flag, etc. */
155 static int full_duplex[MAX_UNITS] = {0, };
156 static int options[MAX_UNITS] = {0, };
157 static int mtu[MAX_UNITS] = {0, }; /* Jumbo MTU for interfaces. */
159 /* The possible media types that can be set in options[] are: */
160 static const char * const medianame[] = {
161 "10baseT", "10base2", "AUI", "100baseTx",
162 "10baseT-FD", "100baseTx-FD", "100baseT4", "100baseFx",
163 "100baseFx-FD", "MII 10baseT", "MII 10baseT-FD", "MII",
164 "10baseT(forced)", "MII 100baseTx", "MII 100baseTx-FD", "MII 100baseT4",
167 /* Set if the PCI BIOS detects the chips on a multiport board backwards. */
168 #ifdef REVERSE_PROBE_ORDER
169 static int reverse_probe = 1;
170 #else
171 static int reverse_probe = 0;
172 #endif
174 /* Keep the ring sizes a power of two for efficiency.
175 Making the Tx ring too large decreases the effectiveness of channel
176 bonding and packet priority.
177 There are no ill effects from too-large receive rings. */
178 #define TX_RING_SIZE 16
179 #define RX_RING_SIZE 32
181 /* Set the copy breakpoint for the copy-only-tiny-buffer Rx structure. */
182 #ifdef __alpha__
183 static int rx_copybreak = 1518;
184 #else
185 static int rx_copybreak = 100;
186 #endif
189 Set the bus performance register.
190 Typical: Set 16 longword cache alignment, no burst limit.
191 Cache alignment bits 15:14 Burst length 13:8
192 0000 No alignment 0x00000000 unlimited 0800 8 longwords
193 4000 8 longwords 0100 1 longword 1000 16 longwords
194 8000 16 longwords 0200 2 longwords 2000 32 longwords
195 C000 32 longwords 0400 4 longwords
196 Warning: many older 486 systems are broken and require setting 0x00A04800
197 8 longword cache alignment, 8 longword burst.
198 ToDo: Non-Intel setting could be better.
201 #if defined(__alpha__)
202 static int csr0 = 0x01A00000 | 0xE000;
203 #elif defined(__i386__) || defined(__powerpc__) || defined(__sparc__)
204 static int csr0 = 0x01A00000 | 0x8000;
205 #else
206 #warning Processor architecture undefined!
207 static int csr0 = 0x00A00000 | 0x4800;
208 #endif
210 /* Operational parameters that usually are not changed. */
211 /* Time in jiffies before concluding the transmitter is hung. */
212 #define TX_TIMEOUT (4*HZ)
213 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
214 /* This is a mysterious value that can be written to CSR11 in the 21040 (only)
215 to support a pre-NWay full-duplex signaling mechanism using short frames.
216 No one knows what it should be, but if left at its default value some
217 10base2(!) packets trigger a full-duplex-request interrupt. */
218 #define FULL_DUPLEX_MAGIC 0x6969
221 /* Kernel compatibility defines, some common to David Hind's PCMCIA package.
222 This is only in the support-all-kernels source code. */
224 MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");
225 MODULE_DESCRIPTION("Digital 21*4* Tulip ethernet driver");
226 MODULE_PARM(debug, "i");
227 MODULE_PARM(max_interrupt_work, "i");
228 MODULE_PARM(reverse_probe, "i");
229 MODULE_PARM(rx_copybreak, "i");
230 MODULE_PARM(csr0, "i");
231 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
232 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
234 #define TULIP_MODULE_NAME "tulip"
235 #define PFX TULIP_MODULE_NAME ": "
237 #define RUN_AT(x) (jiffies + (x))
239 /* Condensed operations for readability. */
240 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
241 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
243 #define tulip_debug debug
244 #ifdef TULIP_DEBUG
245 static int tulip_debug = TULIP_DEBUG;
246 #else
247 static int tulip_debug = 1;
248 #endif
252 /* This table use during operation for capabilities and media timer. */
254 static void tulip_timer(unsigned long data);
255 static void t21142_timer(unsigned long data);
256 static void mxic_timer(unsigned long data);
257 static void pnic_timer(unsigned long data);
258 static void comet_timer(unsigned long data);
260 enum tbl_flag {
261 HAS_MII=1, HAS_MEDIA_TABLE=2, CSR12_IN_SROM=4, ALWAYS_CHECK_MII=8,
262 HAS_PWRDWN=0x10, MC_HASH_ONLY=0x20, /* Hash-only multicast filter. */
263 HAS_PNICNWAY=0x80, HAS_NWAY143=0x40, /* Uses internal NWay xcvr. */
264 HAS_8023X=0x100,
266 static struct tulip_chip_table {
267 char *chip_name;
268 int io_size;
269 int valid_intrs; /* CSR7 interrupt enable settings */
270 int flags;
271 void (*media_timer)(unsigned long data);
272 } tulip_tbl[] = {
273 { "Digital DC21040 Tulip", 128, 0x0001ebef, 0, tulip_timer },
274 { "Digital DC21041 Tulip", 128, 0x0001ebff, HAS_MEDIA_TABLE, tulip_timer },
275 { "Digital DS21140 Tulip", 128, 0x0001ebef,
276 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, tulip_timer },
277 { "Digital DS21143 Tulip", 128, 0x0801fbff,
278 HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII | HAS_PWRDWN | HAS_NWAY143,
279 t21142_timer },
280 { "Lite-On 82c168 PNIC", 256, 0x0001ebef,
281 HAS_MII | HAS_PNICNWAY, pnic_timer },
282 { "Macronix 98713 PMAC", 128, 0x0001ebef,
283 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, mxic_timer },
284 { "Macronix 98715 PMAC", 256, 0x0001ebef,
285 HAS_MEDIA_TABLE, mxic_timer },
286 { "Macronix 98725 PMAC", 256, 0x0001ebef,
287 HAS_MEDIA_TABLE, mxic_timer },
288 { "ASIX AX88140", 128, 0x0001fbff,
289 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM | MC_HASH_ONLY, tulip_timer },
290 { "Lite-On PNIC-II", 256, 0x0801fbff,
291 HAS_MII | HAS_NWAY143 | HAS_8023X, t21142_timer },
292 { "ADMtek Comet", 256, 0x0001abef,
293 MC_HASH_ONLY, comet_timer },
294 { "Compex 9881 PMAC", 128, 0x0001ebef,
295 HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, mxic_timer },
296 { "Intel DS21145 Tulip", 128, 0x0801fbff,
297 HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII | HAS_PWRDWN | HAS_NWAY143,
298 t21142_timer },
299 { "Xircom tulip work-alike", 128, 0x0801fbff,
300 HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII | HAS_PWRDWN | HAS_NWAY143,
301 t21142_timer },
302 {0},
304 /* This matches the table above. Note 21142 == 21143. */
305 enum chips {
306 DC21040=0,
307 DC21041=1,
308 DC21140=2,
309 DC21142=3, DC21143=3,
310 LC82C168,
311 MX98713,
312 MX98715,
313 MX98725,
314 AX88140,
315 PNIC2,
316 COMET,
317 COMPEX9881,
318 I21145,
319 XIRCLONE,
323 static struct pci_device_id tulip_pci_tbl[] __devinitdata = {
324 { 0x1011, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21040 },
325 { 0x1011, 0x0014, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21041 },
326 { 0x1011, 0x0009, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21140 },
327 { 0x1011, 0x0019, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21143 },
328 { 0x11AD, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, LC82C168 },
329 { 0x10d9, 0x0512, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98713 },
330 { 0x10d9, 0x0531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98715 },
331 { 0x10d9, 0x0531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98725 },
332 { 0x125B, 0x1400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AX88140 },
333 { 0x11AD, 0xc115, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PNIC2 },
334 { 0x1317, 0x0981, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
335 { 0x11F6, 0x9881, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMPEX9881 },
336 { 0x8086, 0x0039, PCI_ANY_ID, PCI_ANY_ID, 0, 0, I21145 },
337 { 0x115d, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, XIRCLONE },
338 {0},
340 MODULE_DEVICE_TABLE(pci,tulip_pci_tbl);
343 /* A full-duplex map for media types. */
344 enum MediaIs {
345 MediaIsFD = 1, MediaAlwaysFD=2, MediaIsMII=4, MediaIsFx=8,
346 MediaIs100=16};
347 static const char media_cap[] =
348 {0,0,0,16, 3,19,16,24, 27,4,7,5, 0,20,23,20 };
349 static u8 t21040_csr13[] = {2,0x0C,8,4, 4,0,0,0, 0,0,0,0, 4,0,0,0};
350 /* 21041 transceiver register settings: 10-T, 10-2, AUI, 10-T, 10T-FD*/
351 static u16 t21041_csr13[] = { 0xEF01, 0xEF09, 0xEF09, 0xEF01, 0xEF09, };
352 static u16 t21041_csr14[] = { 0xFFFF, 0xF7FD, 0xF7FD, 0x7F3F, 0x7F3D, };
353 static u16 t21041_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
355 static u16 t21142_csr13[] = { 0x0001, 0x0009, 0x0009, 0x0000, 0x0001, };
356 static u16 t21142_csr14[] = { 0xFFFF, 0x0705, 0x0705, 0x0000, 0x7F3D, };
357 static u16 t21142_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
359 /* Offsets to the Command and Status Registers, "CSRs". All accesses
360 must be longword instructions and quadword aligned. */
361 enum tulip_offsets {
362 CSR0=0, CSR1=0x08, CSR2=0x10, CSR3=0x18, CSR4=0x20, CSR5=0x28,
363 CSR6=0x30, CSR7=0x38, CSR8=0x40, CSR9=0x48, CSR10=0x50, CSR11=0x58,
364 CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78 };
366 /* The bits in the CSR5 status registers, mostly interrupt sources. */
367 enum status_bits {
368 TimerInt=0x800, SytemError=0x2000, TPLnkFail=0x1000, TPLnkPass=0x10,
369 NormalIntr=0x10000, AbnormalIntr=0x8000,
370 RxJabber=0x200, RxDied=0x100, RxNoBuf=0x80, RxIntr=0x40,
371 TxFIFOUnderflow=0x20, TxJabber=0x08, TxNoBuf=0x04, TxDied=0x02, TxIntr=0x01,
374 /* The Tulip Rx and Tx buffer descriptors. */
375 struct tulip_rx_desc {
376 s32 status;
377 s32 length;
378 u32 buffer1, buffer2;
381 struct tulip_tx_desc {
382 s32 status;
383 s32 length;
384 u32 buffer1, buffer2; /* We use only buffer 1. */
387 enum desc_status_bits {
388 DescOwned=0x80000000, RxDescFatalErr=0x8000, RxWholePkt=0x0300,
391 /* Ring-wrap flag in length field, use for last ring entry.
392 0x01000000 means chain on buffer2 address,
393 0x02000000 means use the ring start address in CSR2/3.
394 Note: Some work-alike chips do not function correctly in chained mode.
395 The ASIX chip works only in chained mode.
396 Thus we indicates ring mode, but always write the 'next' field for
397 chained mode as well.
399 #define DESC_RING_WRAP 0x02000000
401 #define EEPROM_SIZE 128 /* 2 << EEPROM_ADDRLEN */
403 struct medialeaf {
404 u8 type;
405 u8 media;
406 unsigned char *leafdata;
409 struct mediatable {
410 u16 defaultmedia;
411 u8 leafcount, csr12dir; /* General purpose pin directions. */
412 unsigned has_mii:1, has_nonmii:1, has_reset:6;
413 u32 csr15dir, csr15val; /* 21143 NWay setting. */
414 struct medialeaf mleaf[0];
417 struct mediainfo {
418 struct mediainfo *next;
419 int info_type;
420 int index;
421 unsigned char *info;
424 struct tulip_private {
425 char devname[8]; /* Used only for kernel debugging. */
426 const char *product_name;
427 struct net_device *next_module;
428 struct tulip_rx_desc rx_ring[RX_RING_SIZE];
429 struct tulip_tx_desc tx_ring[TX_RING_SIZE];
430 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
431 struct sk_buff* tx_skbuff[TX_RING_SIZE];
432 /* The addresses of receive-in-place skbuffs. */
433 struct sk_buff* rx_skbuff[RX_RING_SIZE];
434 char *rx_buffs; /* Address of temporary Rx buffers. */
435 u16 setup_frame[96]; /* Pseudo-Tx frame to init address table. */
436 int chip_id;
437 int revision;
438 int flags;
439 struct net_device_stats stats;
440 struct timer_list timer; /* Media selection timer. */
441 spinlock_t tx_lock;
442 unsigned int cur_rx, cur_tx; /* The next free ring entry */
443 unsigned int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
444 unsigned int tx_full:1; /* The Tx queue is full. */
445 unsigned int full_duplex:1; /* Full-duplex operation requested. */
446 unsigned int full_duplex_lock:1;
447 unsigned int fake_addr:1; /* Multiport board faked address. */
448 unsigned int default_port:4; /* Last dev->if_port value. */
449 unsigned int media2:4; /* Secondary monitored media port. */
450 unsigned int medialock:1; /* Don't sense media type. */
451 unsigned int mediasense:1; /* Media sensing in progress. */
452 unsigned int nway:1, nwayset:1; /* 21143 internal NWay. */
453 unsigned int csr0; /* CSR0 setting. */
454 unsigned int csr6; /* Current CSR6 control settings. */
455 unsigned char eeprom[EEPROM_SIZE]; /* Serial EEPROM contents. */
456 void (*link_change)(struct net_device *dev, int csr5);
457 u16 to_advertise; /* NWay capabilities advertised. */
458 u16 lpar; /* 21143 Link partner ability. */
459 u16 advertising[4];
460 signed char phys[4], mii_cnt; /* MII device addresses. */
461 struct mediatable *mtable;
462 int cur_index; /* Current media index. */
463 int saved_if_port;
464 struct pci_dev *pdev;
465 int ttimer;
466 int susp_rx;
467 unsigned long nir;
468 int pad0, pad1; /* Used for 8-byte alignment */
471 static void parse_eeprom(struct net_device *dev);
472 static int read_eeprom(long ioaddr, int location, int addr_len);
473 static int mdio_read(struct net_device *dev, int phy_id, int location);
474 static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
475 static void select_media(struct net_device *dev, int startup);
476 static int tulip_open(struct net_device *dev);
477 /* Chip-specific media selection (timer functions prototyped above). */
478 static void t21142_lnk_change(struct net_device *dev, int csr5);
479 static void t21142_start_nway(struct net_device *dev);
480 static void pnic_lnk_change(struct net_device *dev, int csr5);
481 static void pnic_do_nway(struct net_device *dev);
483 static void tulip_tx_timeout(struct net_device *dev);
484 static void tulip_init_ring(struct net_device *dev);
485 static int tulip_start_xmit(struct sk_buff *skb, struct net_device *dev);
486 static int tulip_refill_rx(struct net_device *dev);
487 static int tulip_rx(struct net_device *dev);
488 static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
489 static int tulip_close(struct net_device *dev);
490 static struct net_device_stats *tulip_get_stats(struct net_device *dev);
491 static int private_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
492 static void set_rx_mode(struct net_device *dev);
495 /* Serial EEPROM section. */
496 /* The main routine to parse the very complicated SROM structure.
497 Search www.digital.com for "21X4 SROM" to get details.
498 This code is very complex, and will require changes to support
499 additional cards, so I'll be verbose about what is going on.
502 /* Known cards that have old-style EEPROMs. */
503 static struct fixups {
504 char *name;
505 unsigned char addr0, addr1, addr2;
506 u16 newtable[32]; /* Max length below. */
507 } eeprom_fixups[] = {
508 {"Asante", 0, 0, 0x94, {0x1e00, 0x0000, 0x0800, 0x0100, 0x018c,
509 0x0000, 0x0000, 0xe078, 0x0001, 0x0050, 0x0018 }},
510 {"SMC9332DST", 0, 0, 0xC0, { 0x1e00, 0x0000, 0x0800, 0x041f,
511 0x0000, 0x009E, /* 10baseT */
512 0x0004, 0x009E, /* 10baseT-FD */
513 0x0903, 0x006D, /* 100baseTx */
514 0x0905, 0x006D, /* 100baseTx-FD */ }},
515 {"Cogent EM100", 0, 0, 0x92, { 0x1e00, 0x0000, 0x0800, 0x063f,
516 0x0107, 0x8021, /* 100baseFx */
517 0x0108, 0x8021, /* 100baseFx-FD */
518 0x0100, 0x009E, /* 10baseT */
519 0x0104, 0x009E, /* 10baseT-FD */
520 0x0103, 0x006D, /* 100baseTx */
521 0x0105, 0x006D, /* 100baseTx-FD */ }},
522 {"Maxtech NX-110", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x0513,
523 0x1001, 0x009E, /* 10base2, CSR12 0x10*/
524 0x0000, 0x009E, /* 10baseT */
525 0x0004, 0x009E, /* 10baseT-FD */
526 0x0303, 0x006D, /* 100baseTx, CSR12 0x03 */
527 0x0305, 0x006D, /* 100baseTx-FD CSR12 0x03 */}},
528 {"Accton EN1207", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x051F,
529 0x1B01, 0x0000, /* 10base2, CSR12 0x1B */
530 0x0B00, 0x009E, /* 10baseT, CSR12 0x0B */
531 0x0B04, 0x009E, /* 10baseT-FD,CSR12 0x0B */
532 0x1B03, 0x006D, /* 100baseTx, CSR12 0x1B */
533 0x1B05, 0x006D, /* 100baseTx-FD CSR12 0x1B */
535 {0, 0, 0, 0, {}}};
537 static const char * block_name[] = {"21140 non-MII", "21140 MII PHY",
538 "21142 Serial PHY", "21142 MII PHY", "21143 SYM PHY", "21143 reset method"};
540 #if defined(__i386__) /* AKA get_unaligned() */
541 #define get_u16(ptr) (*(u16 *)(ptr))
542 #else
543 #define get_u16(ptr) (((u8*)(ptr))[0] + (((u8*)(ptr))[1]<<8))
544 #endif
546 static void parse_eeprom(struct net_device *dev)
548 /* The last media info list parsed, for multiport boards. */
549 static struct mediatable *last_mediatable = NULL;
550 static unsigned char *last_ee_data = NULL;
551 static int controller_index = 0;
552 struct tulip_private *tp = (struct tulip_private *)dev->priv;
553 unsigned char *ee_data = tp->eeprom;
554 int i;
556 tp->mtable = 0;
557 /* Detect an old-style (SA only) EEPROM layout:
558 memcmp(eedata, eedata+16, 8). */
559 for (i = 0; i < 8; i ++)
560 if (ee_data[i] != ee_data[16+i])
561 break;
562 if (i >= 8) {
563 if (ee_data[0] == 0xff) {
564 if (last_mediatable) {
565 controller_index++;
566 printk(KERN_INFO "%s: Controller %d of multiport board.\n",
567 dev->name, controller_index);
568 tp->mtable = last_mediatable;
569 ee_data = last_ee_data;
570 goto subsequent_board;
571 } else
572 printk(KERN_INFO "%s: Missing EEPROM, this interface may "
573 "not work correctly!\n",
574 dev->name);
575 return;
577 /* Do a fix-up based on the vendor half of the station address prefix. */
578 for (i = 0; eeprom_fixups[i].name; i++) {
579 if (dev->dev_addr[0] == eeprom_fixups[i].addr0
580 && dev->dev_addr[1] == eeprom_fixups[i].addr1
581 && dev->dev_addr[2] == eeprom_fixups[i].addr2) {
582 if (dev->dev_addr[2] == 0xE8 && ee_data[0x1a] == 0x55)
583 i++; /* An Accton EN1207, not an outlaw Maxtech. */
584 memcpy(ee_data + 26, eeprom_fixups[i].newtable,
585 sizeof(eeprom_fixups[i].newtable));
586 printk(KERN_INFO "%s: Old format EEPROM on '%s' board. Using"
587 " substitute media control info.\n",
588 dev->name, eeprom_fixups[i].name);
589 break;
592 if (eeprom_fixups[i].name == NULL) { /* No fixup found. */
593 printk(KERN_INFO "%s: Old style EEPROM with no media selection "
594 "information.\n",
595 dev->name);
596 return;
600 controller_index = 0;
601 if (ee_data[19] > 1) { /* Multiport board. */
602 last_ee_data = ee_data;
604 subsequent_board:
606 if (ee_data[27] == 0) { /* No valid media table. */
607 } else if (tp->chip_id == DC21041) {
608 unsigned char *p = (void *)ee_data + ee_data[27 + controller_index*3];
609 int media = get_u16(p);
610 int count = p[2];
611 p += 3;
613 printk(KERN_INFO "%s: 21041 Media table, default media %4.4x (%s).\n",
614 dev->name, media,
615 media & 0x0800 ? "Autosense" : medianame[media & 15]);
616 for (i = 0; i < count; i++) {
617 unsigned char media_code = *p++;
618 if (media_code & 0x40)
619 p += 6;
620 printk(KERN_INFO "%s: 21041 media #%d, %s.\n",
621 dev->name, media_code & 15, medianame[media_code & 15]);
623 } else {
624 unsigned char *p = (void *)ee_data + ee_data[27];
625 unsigned char csr12dir = 0;
626 int count, new_advertise = 0;
627 struct mediatable *mtable;
628 u16 media = get_u16(p);
630 p += 2;
631 if (tp->flags & CSR12_IN_SROM)
632 csr12dir = *p++;
633 count = *p++;
634 mtable = (struct mediatable *)
635 kmalloc(sizeof(struct mediatable) + count*sizeof(struct medialeaf),
636 GFP_KERNEL);
637 if (mtable == NULL)
638 return; /* Horrible, impossible failure. */
639 last_mediatable = tp->mtable = mtable;
640 mtable->defaultmedia = media;
641 mtable->leafcount = count;
642 mtable->csr12dir = csr12dir;
643 mtable->has_nonmii = mtable->has_mii = mtable->has_reset = 0;
644 mtable->csr15dir = mtable->csr15val = 0;
646 printk(KERN_INFO "%s: EEPROM default media type %s.\n", dev->name,
647 media & 0x0800 ? "Autosense" : medianame[media & 15]);
648 for (i = 0; i < count; i++) {
649 struct medialeaf *leaf = &mtable->mleaf[i];
651 if ((p[0] & 0x80) == 0) { /* 21140 Compact block. */
652 leaf->type = 0;
653 leaf->media = p[0] & 0x3f;
654 leaf->leafdata = p;
655 if ((p[2] & 0x61) == 0x01) /* Bogus, but Znyx boards do it. */
656 mtable->has_mii = 1;
657 p += 4;
658 } else {
659 leaf->type = p[1];
660 if (p[1] == 0x05) {
661 mtable->has_reset = i;
662 leaf->media = p[2] & 0x0f;
663 } else if (p[1] & 1) {
664 mtable->has_mii = 1;
665 leaf->media = 11;
666 } else {
667 mtable->has_nonmii = 1;
668 leaf->media = p[2] & 0x0f;
669 switch (leaf->media) {
670 case 0: new_advertise |= 0x0020; break;
671 case 4: new_advertise |= 0x0040; break;
672 case 3: new_advertise |= 0x0080; break;
673 case 5: new_advertise |= 0x0100; break;
674 case 6: new_advertise |= 0x0200; break;
676 if (p[1] == 2 && leaf->media == 0) {
677 if (p[2] & 0x40) {
678 u32 base15 = get_unaligned((u16*)&p[7]);
679 mtable->csr15dir =
680 (get_unaligned((u16*)&p[9])<<16) + base15;
681 mtable->csr15val =
682 (get_unaligned((u16*)&p[11])<<16) + base15;
683 } else {
684 mtable->csr15dir = get_unaligned((u16*)&p[3])<<16;
685 mtable->csr15val = get_unaligned((u16*)&p[5])<<16;
689 leaf->leafdata = p + 2;
690 p += (p[0] & 0x3f) + 1;
692 if (tulip_debug > 1 && leaf->media == 11) {
693 unsigned char *bp = leaf->leafdata;
694 printk(KERN_INFO "%s: MII interface PHY %d, setup/reset "
695 "sequences %d/%d long, capabilities %2.2x %2.2x.\n",
696 dev->name, bp[0], bp[1], bp[2 + bp[1]*2],
697 bp[5 + bp[2 + bp[1]*2]*2], bp[4 + bp[2 + bp[1]*2]*2]);
699 printk(KERN_INFO "%s: Index #%d - Media %s (#%d) described "
700 "by a %s (%d) block.\n",
701 dev->name, i, medianame[leaf->media], leaf->media,
702 block_name[leaf->type], leaf->type);
704 if (new_advertise)
705 tp->to_advertise = new_advertise;
708 /* Reading a serial EEPROM is a "bit" grungy, but we work our way through:->.*/
710 /* EEPROM_Ctrl bits. */
711 #define EE_SHIFT_CLK 0x02 /* EEPROM shift clock. */
712 #define EE_CS 0x01 /* EEPROM chip select. */
713 #define EE_DATA_WRITE 0x04 /* Data from the Tulip to EEPROM. */
714 #define EE_WRITE_0 0x01
715 #define EE_WRITE_1 0x05
716 #define EE_DATA_READ 0x08 /* Data from the EEPROM chip. */
717 #define EE_ENB (0x4800 | EE_CS)
719 /* Delay between EEPROM clock transitions.
720 Even at 33Mhz current PCI implementations don't overrun the EEPROM clock.
721 We add a bus turn-around to insure that this remains true. */
722 #define eeprom_delay() inl(ee_addr)
724 /* The EEPROM commands include the alway-set leading bit. */
725 #define EE_READ_CMD (6)
727 /* Note: this routine returns extra data bits for size detection. */
728 static int read_eeprom(long ioaddr, int location, int addr_len)
730 int i;
731 unsigned retval = 0;
732 long ee_addr = ioaddr + CSR9;
733 int read_cmd = location | (EE_READ_CMD << addr_len);
735 outl(EE_ENB & ~EE_CS, ee_addr);
736 outl(EE_ENB, ee_addr);
738 /* Shift the read command bits out. */
739 for (i = 4 + addr_len; i >= 0; i--) {
740 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
741 outl(EE_ENB | dataval, ee_addr);
742 eeprom_delay();
743 outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
744 eeprom_delay();
745 retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
747 outl(EE_ENB, ee_addr);
749 for (i = 16; i > 0; i--) {
750 outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
751 eeprom_delay();
752 retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
753 outl(EE_ENB, ee_addr);
754 eeprom_delay();
757 /* Terminate the EEPROM access. */
758 outl(EE_ENB & ~EE_CS, ee_addr);
759 return retval;
762 /* MII transceiver control section.
763 Read and write the MII registers using software-generated serial
764 MDIO protocol. See the MII specifications or DP83840A data sheet
765 for details. */
767 /* The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
768 met by back-to-back PCI I/O cycles, but we insert a delay to avoid
769 "overclocking" issues or future 66Mhz PCI. */
770 #define mdio_delay() inl(mdio_addr)
772 /* Read and write the MII registers using software-generated serial
773 MDIO protocol. It is just different enough from the EEPROM protocol
774 to not share code. The maxium data clock rate is 2.5 Mhz. */
775 #define MDIO_SHIFT_CLK 0x10000
776 #define MDIO_DATA_WRITE0 0x00000
777 #define MDIO_DATA_WRITE1 0x20000
778 #define MDIO_ENB 0x00000 /* Ignore the 0x02000 databook setting. */
779 #define MDIO_ENB_IN 0x40000
780 #define MDIO_DATA_READ 0x80000
782 static int mdio_read(struct net_device *dev, int phy_id, int location)
784 struct tulip_private *tp = (struct tulip_private *)dev->priv;
785 int i;
786 int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
787 int retval = 0;
788 long ioaddr = dev->base_addr;
789 long mdio_addr = ioaddr + CSR9;
791 if (tp->chip_id == LC82C168) {
792 int i = 1000;
793 outl(0x60020000 + (phy_id<<23) + (location<<18), ioaddr + 0xA0);
794 inl(ioaddr + 0xA0);
795 inl(ioaddr + 0xA0);
796 while (--i > 0)
797 if ( ! ((retval = inl(ioaddr + 0xA0)) & 0x80000000))
798 return retval & 0xffff;
799 return 0xffff;
802 if (tp->chip_id == COMET) {
803 if (phy_id == 1) {
804 if (location < 7)
805 return inl(ioaddr + 0xB4 + (location<<2));
806 else if (location == 17)
807 return inl(ioaddr + 0xD0);
808 else if (location >= 29 && location <= 31)
809 return inl(ioaddr + 0xD4 + ((location-29)<<2));
811 return 0xffff;
814 /* Establish sync by sending at least 32 logic ones. */
815 for (i = 32; i >= 0; i--) {
816 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
817 mdio_delay();
818 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
819 mdio_delay();
821 /* Shift the read command bits out. */
822 for (i = 15; i >= 0; i--) {
823 int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
825 outl(MDIO_ENB | dataval, mdio_addr);
826 mdio_delay();
827 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
828 mdio_delay();
830 /* Read the two transition, 16 data, and wire-idle bits. */
831 for (i = 19; i > 0; i--) {
832 outl(MDIO_ENB_IN, mdio_addr);
833 mdio_delay();
834 retval = (retval << 1) | ((inl(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
835 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
836 mdio_delay();
838 return (retval>>1) & 0xffff;
841 static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
843 struct tulip_private *tp = (struct tulip_private *)dev->priv;
844 int i;
845 int cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
846 long ioaddr = dev->base_addr;
847 long mdio_addr = ioaddr + CSR9;
849 if (tp->chip_id == LC82C168) {
850 int i = 1000;
851 outl(cmd, ioaddr + 0xA0);
853 if ( ! (inl(ioaddr + 0xA0) & 0x80000000))
854 break;
855 while (--i > 0);
856 return;
859 if (tp->chip_id == COMET) {
860 if (phy_id != 1)
861 return;
862 if (location < 7)
863 outl(value, ioaddr + 0xB4 + (location<<2));
864 else if (location == 17)
865 outl(value, ioaddr + 0xD0);
866 else if (location >= 29 && location <= 31)
867 outl(value, ioaddr + 0xD4 + ((location-29)<<2));
868 return;
871 /* Establish sync by sending 32 logic ones. */
872 for (i = 32; i >= 0; i--) {
873 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
874 mdio_delay();
875 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
876 mdio_delay();
878 /* Shift the command bits out. */
879 for (i = 31; i >= 0; i--) {
880 int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
881 outl(MDIO_ENB | dataval, mdio_addr);
882 mdio_delay();
883 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
884 mdio_delay();
886 /* Clear out extra bits. */
887 for (i = 2; i > 0; i--) {
888 outl(MDIO_ENB_IN, mdio_addr);
889 mdio_delay();
890 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
891 mdio_delay();
893 return;
897 static int
898 tulip_open(struct net_device *dev)
900 struct tulip_private *tp = (struct tulip_private *)dev->priv;
901 long ioaddr = dev->base_addr;
902 int next_tick = 3*HZ;
903 int i;
905 /* Wake the chip from sleep/snooze mode. */
906 if (tp->flags & HAS_PWRDWN)
907 pci_write_config_dword(tp->pdev, 0x40, 0);
909 /* On some chip revs we must set the MII/SYM port before the reset!? */
910 if (tp->mii_cnt || (tp->mtable && tp->mtable->has_mii))
911 outl(0x00040000, ioaddr + CSR6);
913 /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
914 outl(0x00000001, ioaddr + CSR0);
916 if (request_irq(dev->irq, &tulip_interrupt, SA_SHIRQ, dev->name, dev))
917 return -EAGAIN;
919 /* Deassert reset.
920 Wait the specified 50 PCI cycles after a reset by initializing
921 Tx and Rx queues and the address filter list. */
922 outl(tp->csr0, ioaddr + CSR0);
924 if (tulip_debug > 1)
925 printk(KERN_DEBUG "%s: tulip_open() irq %d.\n", dev->name, dev->irq);
927 MOD_INC_USE_COUNT;
929 spin_lock_init(&tp->tx_lock);
930 tulip_init_ring(dev);
932 #if 0
933 if (tp->chip_id == PNIC2) {
934 u32 addr_low = cpu_to_le32(get_unaligned((u32 *)dev->dev_addr));
935 u32 addr_high = cpu_to_le16(get_unaligned((u16 *)(dev->dev_addr+4)));
936 addr_high = (dev->dev_addr[4]<<8) + (dev->dev_addr[5]<<0);
937 outl((dev->dev_addr[0]<<8) + dev->dev_addr[1] +
938 (dev->dev_addr[2]<<24) + (dev->dev_addr[3]<<16),
939 ioaddr + 0xB0);
940 outl(addr_high + (addr_high<<16), ioaddr + 0xB8);
942 #endif
943 if (tp->flags & MC_HASH_ONLY) {
944 u32 addr_low = cpu_to_le32(get_unaligned((u32 *)dev->dev_addr));
945 u32 addr_high = cpu_to_le32(get_unaligned((u16 *)(dev->dev_addr+4)));
946 if (tp->chip_id == AX88140) {
947 outl(0, ioaddr + CSR13);
948 outl(addr_low, ioaddr + CSR14);
949 outl(1, ioaddr + CSR13);
950 outl(addr_high, ioaddr + CSR14);
951 } else if (tp->chip_id == COMET) {
952 outl(addr_low, ioaddr + 0xA4);
953 outl(addr_high, ioaddr + 0xA8);
954 outl(0, ioaddr + 0xAC);
955 outl(0, ioaddr + 0xB0);
957 } else {
958 /* This is set_rx_mode(), but without starting the transmitter. */
959 u16 *eaddrs = (u16 *)dev->dev_addr;
960 u16 *setup_frm = &tp->setup_frame[15*6];
962 /* 21140 bug: you must add the broadcast address. */
963 memset(tp->setup_frame, 0xff, sizeof(tp->setup_frame));
964 /* Fill the final entry of the table with our physical address. */
965 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
966 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
967 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
968 /* Put the setup frame on the Tx list. */
969 tp->tx_ring[0].length = cpu_to_le32(0x08000000 | 192);
970 tp->tx_ring[0].buffer1 = virt_to_le32desc(tp->setup_frame);
971 tp->tx_ring[0].status = cpu_to_le32(DescOwned);
973 tp->cur_tx++;
976 outl(virt_to_bus(tp->rx_ring), ioaddr + CSR3);
977 outl(virt_to_bus(tp->tx_ring), ioaddr + CSR4);
979 tp->saved_if_port = dev->if_port;
980 if (dev->if_port == 0)
981 dev->if_port = tp->default_port;
983 /* Allow selecting a default media. */
984 i = 0;
985 if (tp->mtable == NULL)
986 goto media_picked;
987 if (dev->if_port) {
988 int looking_for = media_cap[dev->if_port] & MediaIsMII ? 11 :
989 (dev->if_port == 12 ? 0 : dev->if_port);
990 for (i = 0; i < tp->mtable->leafcount; i++)
991 if (tp->mtable->mleaf[i].media == looking_for) {
992 printk(KERN_INFO "%s: Using user-specified media %s.\n",
993 dev->name, medianame[dev->if_port]);
994 goto media_picked;
997 if ((tp->mtable->defaultmedia & 0x0800) == 0) {
998 int looking_for = tp->mtable->defaultmedia & 15;
999 for (i = 0; i < tp->mtable->leafcount; i++)
1000 if (tp->mtable->mleaf[i].media == looking_for) {
1001 printk(KERN_INFO "%s: Using EEPROM-set media %s.\n",
1002 dev->name, medianame[looking_for]);
1003 goto media_picked;
1006 /* Start sensing first non-full-duplex media. */
1007 for (i = tp->mtable->leafcount - 1;
1008 (media_cap[tp->mtable->mleaf[i].media] & MediaAlwaysFD) && i > 0; i--)
1010 media_picked:
1012 tp->csr6 = 0;
1013 tp->cur_index = i;
1014 tp->nwayset = 0;
1015 if (dev->if_port == 0 && tp->chip_id == DC21041) {
1016 tp->nway = 1;
1018 if (dev->if_port == 0 && tp->chip_id == DC21142) {
1019 if (tp->mii_cnt) {
1020 select_media(dev, 1);
1021 if (tulip_debug > 1)
1022 printk(KERN_INFO "%s: Using MII transceiver %d, status "
1023 "%4.4x.\n",
1024 dev->name, tp->phys[0], mdio_read(dev, tp->phys[0], 1));
1025 outl(0x82020000, ioaddr + CSR6);
1026 tp->csr6 = 0x820E0000;
1027 dev->if_port = 11;
1028 outl(0x0000, ioaddr + CSR13);
1029 outl(0x0000, ioaddr + CSR14);
1030 } else
1031 t21142_start_nway(dev);
1032 } else if (tp->chip_id == PNIC2) {
1033 t21142_start_nway(dev);
1034 } else if (tp->chip_id == LC82C168 && ! tp->medialock) {
1035 if (tp->mii_cnt) {
1036 dev->if_port = 11;
1037 tp->csr6 = 0x814C0000 | (tp->full_duplex ? 0x0200 : 0);
1038 outl(0x0001, ioaddr + CSR15);
1039 } else if (inl(ioaddr + CSR5) & TPLnkPass)
1040 pnic_do_nway(dev);
1041 else {
1042 /* Start with 10mbps to do autonegotiation. */
1043 outl(0x32, ioaddr + CSR12);
1044 tp->csr6 = 0x00420000;
1045 outl(0x0001B078, ioaddr + 0xB8);
1046 outl(0x0201B078, ioaddr + 0xB8);
1047 next_tick = 1*HZ;
1049 } else if ((tp->chip_id == MX98713 || tp->chip_id == COMPEX9881)
1050 && ! tp->medialock) {
1051 dev->if_port = 0;
1052 tp->csr6 = 0x01880000 | (tp->full_duplex ? 0x0200 : 0);
1053 outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
1054 } else if (tp->chip_id == MX98715 || tp->chip_id == MX98725) {
1055 /* Provided by BOLO, Macronix - 12/10/1998. */
1056 dev->if_port = 0;
1057 tp->csr6 = 0x01a80200;
1058 outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
1059 outl(0x11000 | inw(ioaddr + 0xa0), ioaddr + 0xa0);
1060 } else if (tp->chip_id == DC21143 &&
1061 media_cap[dev->if_port] & MediaIsMII) {
1062 /* We must reset the media CSRs when we force-select MII mode. */
1063 outl(0x0000, ioaddr + CSR13);
1064 outl(0x0000, ioaddr + CSR14);
1065 outl(0x0008, ioaddr + CSR15);
1066 } else if (tp->chip_id == COMET) {
1067 dev->if_port = 0;
1068 tp->csr6 = 0x00040000;
1069 } else if (tp->chip_id == AX88140) {
1070 tp->csr6 = tp->mii_cnt ? 0x00040100 : 0x00000100;
1071 } else
1072 select_media(dev, 1);
1074 /* Start the chip's Tx to process setup frame. */
1075 outl(tp->csr6, ioaddr + CSR6);
1076 outl(tp->csr6 | 0x2000, ioaddr + CSR6);
1078 /* Enable interrupts by setting the interrupt mask. */
1079 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR5);
1080 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
1081 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1082 outl(0, ioaddr + CSR2); /* Rx poll demand */
1084 if (tulip_debug > 2) {
1085 printk(KERN_DEBUG "%s: Done tulip_open(), CSR0 %8.8x, CSR5 %8.8x CSR6 %8.8x.\n",
1086 dev->name, inl(ioaddr + CSR0), inl(ioaddr + CSR5),
1087 inl(ioaddr + CSR6));
1089 /* Set the timer to switch to check for link beat and perhaps switch
1090 to an alternate media type. */
1091 init_timer(&tp->timer);
1092 tp->timer.expires = RUN_AT(next_tick);
1093 tp->timer.data = (unsigned long)dev;
1094 tp->timer.function = tulip_tbl[tp->chip_id].media_timer;
1095 add_timer(&tp->timer);
1097 netif_start_queue(dev);
1099 return 0;
1102 /* Set up the transceiver control registers for the selected media type. */
1103 static void select_media(struct net_device *dev, int startup)
1105 long ioaddr = dev->base_addr;
1106 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1107 struct mediatable *mtable = tp->mtable;
1108 u32 new_csr6;
1109 int i;
1111 if (mtable) {
1112 struct medialeaf *mleaf = &mtable->mleaf[tp->cur_index];
1113 unsigned char *p = mleaf->leafdata;
1114 switch (mleaf->type) {
1115 case 0: /* 21140 non-MII xcvr. */
1116 if (tulip_debug > 1)
1117 printk(KERN_DEBUG "%s: Using a 21140 non-MII transceiver"
1118 " with control setting %2.2x.\n",
1119 dev->name, p[1]);
1120 dev->if_port = p[0];
1121 if (startup)
1122 outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
1123 outl(p[1], ioaddr + CSR12);
1124 new_csr6 = 0x02000000 | ((p[2] & 0x71) << 18);
1125 break;
1126 case 2: case 4: {
1127 u16 setup[5];
1128 u32 csr13val, csr14val, csr15dir, csr15val;
1129 for (i = 0; i < 5; i++)
1130 setup[i] = get_u16(&p[i*2 + 1]);
1132 dev->if_port = p[0] & 15;
1133 if (media_cap[dev->if_port] & MediaAlwaysFD)
1134 tp->full_duplex = 1;
1136 if (startup && mtable->has_reset) {
1137 struct medialeaf *rleaf = &mtable->mleaf[mtable->has_reset];
1138 unsigned char *rst = rleaf->leafdata;
1139 if (tulip_debug > 1)
1140 printk(KERN_DEBUG "%s: Resetting the transceiver.\n",
1141 dev->name);
1142 for (i = 0; i < rst[0]; i++)
1143 outl(get_u16(rst + 1 + (i<<1)) << 16, ioaddr + CSR15);
1145 if (tulip_debug > 1)
1146 printk(KERN_DEBUG "%s: 21143 non-MII %s transceiver control "
1147 "%4.4x/%4.4x.\n",
1148 dev->name, medianame[dev->if_port], setup[0], setup[1]);
1149 if (p[0] & 0x40) { /* SIA (CSR13-15) setup values are provided. */
1150 csr13val = setup[0];
1151 csr14val = setup[1];
1152 csr15dir = (setup[3]<<16) | setup[2];
1153 csr15val = (setup[4]<<16) | setup[2];
1154 outl(0, ioaddr + CSR13);
1155 outl(csr14val, ioaddr + CSR14);
1156 outl(csr15dir, ioaddr + CSR15); /* Direction */
1157 outl(csr15val, ioaddr + CSR15); /* Data */
1158 outl(csr13val, ioaddr + CSR13);
1159 } else {
1160 csr13val = 1;
1161 csr14val = 0x0003FF7F;
1162 csr15dir = (setup[0]<<16) | 0x0008;
1163 csr15val = (setup[1]<<16) | 0x0008;
1164 if (dev->if_port <= 4)
1165 csr14val = t21142_csr14[dev->if_port];
1166 if (startup) {
1167 outl(0, ioaddr + CSR13);
1168 outl(csr14val, ioaddr + CSR14);
1170 outl(csr15dir, ioaddr + CSR15); /* Direction */
1171 outl(csr15val, ioaddr + CSR15); /* Data */
1172 if (startup) outl(csr13val, ioaddr + CSR13);
1174 if (tulip_debug > 1)
1175 printk(KERN_DEBUG "%s: Setting CSR15 to %8.8x/%8.8x.\n",
1176 dev->name, csr15dir, csr15val);
1177 if (mleaf->type == 4)
1178 new_csr6 = 0x82020000 | ((setup[2] & 0x71) << 18);
1179 else
1180 new_csr6 = 0x82420000;
1181 break;
1183 case 1: case 3: {
1184 int phy_num = p[0];
1185 int init_length = p[1];
1186 u16 *misc_info;
1187 u16 to_advertise;
1189 dev->if_port = 11;
1190 new_csr6 = 0x020E0000;
1191 if (mleaf->type == 3) { /* 21142 */
1192 u16 *init_sequence = (u16*)(p+2);
1193 u16 *reset_sequence = &((u16*)(p+3))[init_length];
1194 int reset_length = p[2 + init_length*2];
1195 misc_info = reset_sequence + reset_length;
1196 if (startup)
1197 for (i = 0; i < reset_length; i++)
1198 outl(get_u16(&reset_sequence[i]) << 16, ioaddr + CSR15);
1199 for (i = 0; i < init_length; i++)
1200 outl(get_u16(&init_sequence[i]) << 16, ioaddr + CSR15);
1201 } else {
1202 u8 *init_sequence = p + 2;
1203 u8 *reset_sequence = p + 3 + init_length;
1204 int reset_length = p[2 + init_length];
1205 misc_info = (u16*)(reset_sequence + reset_length);
1206 if (startup) {
1207 outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
1208 for (i = 0; i < reset_length; i++)
1209 outl(reset_sequence[i], ioaddr + CSR12);
1211 for (i = 0; i < init_length; i++)
1212 outl(init_sequence[i], ioaddr + CSR12);
1214 to_advertise = (get_u16(&misc_info[1]) & tp->to_advertise) | 1;
1215 tp->advertising[phy_num] = to_advertise;
1216 if (tulip_debug > 1)
1217 printk(KERN_DEBUG "%s: Advertising %4.4x on PHY %d (%d).\n",
1218 dev->name, to_advertise, phy_num, tp->phys[phy_num]);
1219 /* Bogus: put in by a committee? */
1220 mdio_write(dev, tp->phys[phy_num], 4, to_advertise);
1221 break;
1223 default:
1224 printk(KERN_DEBUG "%s: Invalid media table selection %d.\n",
1225 dev->name, mleaf->type);
1226 new_csr6 = 0x020E0000;
1228 if (tulip_debug > 1)
1229 printk(KERN_DEBUG "%s: Using media type %s, CSR12 is %2.2x.\n",
1230 dev->name, medianame[dev->if_port],
1231 inl(ioaddr + CSR12) & 0xff);
1232 } else if (tp->chip_id == DC21041) {
1233 int port = dev->if_port <= 4 ? dev->if_port : 0;
1234 if (tulip_debug > 1)
1235 printk(KERN_DEBUG "%s: 21041 using media %s, CSR12 is %4.4x.\n",
1236 dev->name, medianame[port == 3 ? 12: port],
1237 inl(ioaddr + CSR12));
1238 outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
1239 outl(t21041_csr14[port], ioaddr + CSR14);
1240 outl(t21041_csr15[port], ioaddr + CSR15);
1241 outl(t21041_csr13[port], ioaddr + CSR13);
1242 new_csr6 = 0x80020000;
1243 } else if (tp->chip_id == LC82C168) {
1244 if (startup && ! tp->medialock)
1245 dev->if_port = tp->mii_cnt ? 11 : 0;
1246 if (tulip_debug > 1)
1247 printk(KERN_DEBUG "%s: PNIC PHY status is %3.3x, media %s.\n",
1248 dev->name, inl(ioaddr + 0xB8), medianame[dev->if_port]);
1249 if (tp->mii_cnt) {
1250 new_csr6 = 0x810C0000;
1251 outl(0x0001, ioaddr + CSR15);
1252 outl(0x0201B07A, ioaddr + 0xB8);
1253 } else if (startup) {
1254 /* Start with 10mbps to do autonegotiation. */
1255 outl(0x32, ioaddr + CSR12);
1256 new_csr6 = 0x00420000;
1257 outl(0x0001B078, ioaddr + 0xB8);
1258 outl(0x0201B078, ioaddr + 0xB8);
1259 } else if (dev->if_port == 3 || dev->if_port == 5) {
1260 outl(0x33, ioaddr + CSR12);
1261 new_csr6 = 0x01860000;
1262 /* Trigger autonegotiation. */
1263 outl(startup ? 0x0201F868 : 0x0001F868, ioaddr + 0xB8);
1264 } else {
1265 outl(0x32, ioaddr + CSR12);
1266 new_csr6 = 0x00420000;
1267 outl(0x1F078, ioaddr + 0xB8);
1269 } else if (tp->chip_id == DC21040) { /* 21040 */
1270 /* Turn on the xcvr interface. */
1271 int csr12 = inl(ioaddr + CSR12);
1272 if (tulip_debug > 1)
1273 printk(KERN_DEBUG "%s: 21040 media type is %s, CSR12 is %2.2x.\n",
1274 dev->name, medianame[dev->if_port], csr12);
1275 if (media_cap[dev->if_port] & MediaAlwaysFD)
1276 tp->full_duplex = 1;
1277 new_csr6 = 0x20000;
1278 /* Set the full duplux match frame. */
1279 outl(FULL_DUPLEX_MAGIC, ioaddr + CSR11);
1280 outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
1281 if (t21040_csr13[dev->if_port] & 8) {
1282 outl(0x0705, ioaddr + CSR14);
1283 outl(0x0006, ioaddr + CSR15);
1284 } else {
1285 outl(0xffff, ioaddr + CSR14);
1286 outl(0x0000, ioaddr + CSR15);
1288 outl(0x8f01 | t21040_csr13[dev->if_port], ioaddr + CSR13);
1289 } else { /* Unknown chip type with no media table. */
1290 if (tp->default_port == 0)
1291 dev->if_port = tp->mii_cnt ? 11 : 3;
1292 if (media_cap[dev->if_port] & MediaIsMII) {
1293 new_csr6 = 0x020E0000;
1294 } else if (media_cap[dev->if_port] & MediaIsFx) {
1295 new_csr6 = 0x028600000;
1296 } else
1297 new_csr6 = 0x038600000;
1298 if (tulip_debug > 1)
1299 printk(KERN_DEBUG "%s: No media description table, assuming "
1300 "%s transceiver, CSR12 %2.2x.\n",
1301 dev->name, medianame[dev->if_port],
1302 inl(ioaddr + CSR12));
1305 tp->csr6 = new_csr6 | (tp->csr6 & 0xfdff) | (tp->full_duplex ? 0x0200 : 0);
1306 return;
1310 Check the MII negotiated duplex, and change the CSR6 setting if
1311 required.
1312 Return 0 if everything is OK.
1313 Return < 0 if the transceiver is missing or has no link beat.
1315 static int check_duplex(struct net_device *dev)
1317 long ioaddr = dev->base_addr;
1318 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1319 int mii_reg1, mii_reg5, negotiated, duplex;
1321 if (tp->full_duplex_lock)
1322 return 0;
1323 mii_reg1 = mdio_read(dev, tp->phys[0], 1);
1324 mii_reg5 = mdio_read(dev, tp->phys[0], 5);
1325 if (tulip_debug > 1)
1326 printk(KERN_INFO "%s: MII status %4.4x, Link partner report "
1327 "%4.4x.\n", dev->name, mii_reg1, mii_reg5);
1328 if (mii_reg1 == 0xffff)
1329 return -2;
1330 if ((mii_reg1 & 0x0004) == 0) {
1331 int new_reg1 = mdio_read(dev, tp->phys[0], 1);
1332 if ((new_reg1 & 0x0004) == 0) {
1333 if (tulip_debug > 1)
1334 printk(KERN_INFO "%s: No link beat on the MII interface,"
1335 " status %4.4x.\n", dev->name, new_reg1);
1336 return -1;
1339 negotiated = mii_reg5 & tp->advertising[0];
1340 duplex = ((negotiated & 0x0300) == 0x0100
1341 || (negotiated & 0x00C0) == 0x0040);
1342 /* 100baseTx-FD or 10T-FD, but not 100-HD */
1343 if (tp->full_duplex != duplex) {
1344 tp->full_duplex = duplex;
1345 if (negotiated & 0x038) /* 100mbps. */
1346 tp->csr6 &= ~0x00400000;
1347 if (tp->full_duplex) tp->csr6 |= 0x0200;
1348 else tp->csr6 &= ~0x0200;
1349 outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1350 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1351 if (tulip_debug > 0)
1352 printk(KERN_INFO "%s: Setting %s-duplex based on MII"
1353 "#%d link partner capability of %4.4x.\n",
1354 dev->name, tp->full_duplex ? "full" : "half",
1355 tp->phys[0], mii_reg5);
1356 return 1;
1358 return 0;
1361 static void tulip_timer(unsigned long data)
1363 struct net_device *dev = (struct net_device *)data;
1364 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1365 long ioaddr = dev->base_addr;
1366 u32 csr12 = inl(ioaddr + CSR12);
1367 int next_tick = 2*HZ;
1369 if (tulip_debug > 2) {
1370 printk(KERN_DEBUG "%s: Media selection tick, %s, status %8.8x mode"
1371 " %8.8x SIA %8.8x %8.8x %8.8x %8.8x.\n",
1372 dev->name, medianame[dev->if_port], inl(ioaddr + CSR5),
1373 inl(ioaddr + CSR6), csr12, inl(ioaddr + CSR13),
1374 inl(ioaddr + CSR14), inl(ioaddr + CSR15));
1376 switch (tp->chip_id) {
1377 case DC21040:
1378 if (!tp->medialock && csr12 & 0x0002) { /* Network error */
1379 printk(KERN_INFO "%s: No link beat found.\n",
1380 dev->name);
1381 dev->if_port = (dev->if_port == 2 ? 0 : 2);
1382 select_media(dev, 0);
1383 dev->trans_start = jiffies;
1385 break;
1386 case DC21041:
1387 if (tulip_debug > 2)
1388 printk(KERN_DEBUG "%s: 21041 media tick CSR12 %8.8x.\n",
1389 dev->name, csr12);
1390 if (tp->medialock) break;
1391 switch (dev->if_port) {
1392 case 0: case 3: case 4:
1393 if (csr12 & 0x0004) { /*LnkFail */
1394 /* 10baseT is dead. Check for activity on alternate port. */
1395 tp->mediasense = 1;
1396 if (csr12 & 0x0200)
1397 dev->if_port = 2;
1398 else
1399 dev->if_port = 1;
1400 printk(KERN_INFO "%s: No 21041 10baseT link beat, Media switched to %s.\n",
1401 dev->name, medianame[dev->if_port]);
1402 outl(0, ioaddr + CSR13); /* Reset */
1403 outl(t21041_csr14[dev->if_port], ioaddr + CSR14);
1404 outl(t21041_csr15[dev->if_port], ioaddr + CSR15);
1405 outl(t21041_csr13[dev->if_port], ioaddr + CSR13);
1406 next_tick = 10*HZ; /* 2.4 sec. */
1407 } else
1408 next_tick = 30*HZ;
1409 break;
1410 case 1: /* 10base2 */
1411 case 2: /* AUI */
1412 if (csr12 & 0x0100) {
1413 next_tick = (30*HZ); /* 30 sec. */
1414 tp->mediasense = 0;
1415 } else if ((csr12 & 0x0004) == 0) {
1416 printk(KERN_INFO "%s: 21041 media switched to 10baseT.\n",
1417 dev->name);
1418 dev->if_port = 0;
1419 select_media(dev, 0);
1420 next_tick = (24*HZ)/10; /* 2.4 sec. */
1421 } else if (tp->mediasense || (csr12 & 0x0002)) {
1422 dev->if_port = 3 - dev->if_port; /* Swap ports. */
1423 select_media(dev, 0);
1424 next_tick = 20*HZ;
1425 } else {
1426 next_tick = 20*HZ;
1428 break;
1430 break;
1431 case DC21140: case DC21142: case MX98713: case COMPEX9881: default: {
1432 struct medialeaf *mleaf;
1433 unsigned char *p;
1434 if (tp->mtable == NULL) { /* No EEPROM info, use generic code. */
1435 /* Not much that can be done.
1436 Assume this a generic MII or SYM transceiver. */
1437 next_tick = 60*HZ;
1438 if (tulip_debug > 2)
1439 printk(KERN_DEBUG "%s: network media monitor CSR6 %8.8x "
1440 "CSR12 0x%2.2x.\n",
1441 dev->name, inl(ioaddr + CSR6), csr12 & 0xff);
1442 break;
1444 mleaf = &tp->mtable->mleaf[tp->cur_index];
1445 p = mleaf->leafdata;
1446 switch (mleaf->type) {
1447 case 0: case 4: {
1448 /* Type 0 serial or 4 SYM transceiver. Check the link beat bit. */
1449 int offset = mleaf->type == 4 ? 5 : 2;
1450 s8 bitnum = p[offset];
1451 if (p[offset+1] & 0x80) {
1452 if (tulip_debug > 1)
1453 printk(KERN_DEBUG"%s: Transceiver monitor tick "
1454 "CSR12=%#2.2x, no media sense.\n",
1455 dev->name, csr12);
1456 if (mleaf->type == 4) {
1457 if (mleaf->media == 3 && (csr12 & 0x02))
1458 goto select_next_media;
1460 break;
1462 if (tulip_debug > 2)
1463 printk(KERN_DEBUG "%s: Transceiver monitor tick: CSR12=%#2.2x"
1464 " bit %d is %d, expecting %d.\n",
1465 dev->name, csr12, (bitnum >> 1) & 7,
1466 (csr12 & (1 << ((bitnum >> 1) & 7))) != 0,
1467 (bitnum >= 0));
1468 /* Check that the specified bit has the proper value. */
1469 if ((bitnum < 0) !=
1470 ((csr12 & (1 << ((bitnum >> 1) & 7))) != 0)) {
1471 if (tulip_debug > 1)
1472 printk(KERN_DEBUG "%s: Link beat detected for %s.\n", dev->name,
1473 medianame[mleaf->media]);
1474 if ((p[2] & 0x61) == 0x01) /* Bogus Znyx board. */
1475 goto actually_mii;
1476 break;
1478 if (tp->medialock)
1479 break;
1480 select_next_media:
1481 if (--tp->cur_index < 0) {
1482 /* We start again, but should instead look for default. */
1483 tp->cur_index = tp->mtable->leafcount - 1;
1485 dev->if_port = tp->mtable->mleaf[tp->cur_index].media;
1486 if (media_cap[dev->if_port] & MediaIsFD)
1487 goto select_next_media; /* Skip FD entries. */
1488 if (tulip_debug > 1)
1489 printk(KERN_DEBUG "%s: No link beat on media %s,"
1490 " trying transceiver type %s.\n",
1491 dev->name, medianame[mleaf->media & 15],
1492 medianame[tp->mtable->mleaf[tp->cur_index].media]);
1493 select_media(dev, 0);
1494 /* Restart the transmit process. */
1495 outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1496 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1497 next_tick = (24*HZ)/10;
1498 break;
1500 case 1: case 3: /* 21140, 21142 MII */
1501 actually_mii:
1502 check_duplex(dev);
1503 next_tick = 60*HZ;
1504 break;
1505 case 2: /* 21142 serial block has no link beat. */
1506 default:
1507 break;
1510 break;
1512 tp->timer.expires = RUN_AT(next_tick);
1513 add_timer(&tp->timer);
1516 /* Handle the 21143 uniquely: do autoselect with NWay, not the EEPROM list
1517 of available transceivers. */
1518 static void t21142_timer(unsigned long data)
1520 struct net_device *dev = (struct net_device *)data;
1521 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1522 long ioaddr = dev->base_addr;
1523 int csr12 = inl(ioaddr + CSR12);
1524 int next_tick = 60*HZ;
1525 int new_csr6 = 0;
1527 if (tulip_debug > 2)
1528 printk(KERN_INFO"%s: 21143 negotiation status %8.8x, %s.\n",
1529 dev->name, csr12, medianame[dev->if_port]);
1530 if (media_cap[dev->if_port] & MediaIsMII) {
1531 check_duplex(dev);
1532 next_tick = 60*HZ;
1533 } else if (tp->nwayset) {
1534 /* Don't screw up a negotiated session! */
1535 if (tulip_debug > 1)
1536 printk(KERN_INFO"%s: Using NWay-set %s media, csr12 %8.8x.\n",
1537 dev->name, medianame[dev->if_port], csr12);
1538 } else if (tp->medialock) {
1540 } else if (dev->if_port == 3) {
1541 if (csr12 & 2) { /* No 100mbps link beat, revert to 10mbps. */
1542 if (tulip_debug > 1)
1543 printk(KERN_INFO"%s: No 21143 100baseTx link beat, %8.8x, "
1544 "trying NWay.\n", dev->name, csr12);
1545 t21142_start_nway(dev);
1546 next_tick = 3*HZ;
1548 } else if ((csr12 & 0x7000) != 0x5000) {
1549 /* Negotiation failed. Search media types. */
1550 if (tulip_debug > 1)
1551 printk(KERN_INFO"%s: 21143 negotiation failed, status %8.8x.\n",
1552 dev->name, csr12);
1553 if (!(csr12 & 4)) { /* 10mbps link beat good. */
1554 new_csr6 = 0x82420000;
1555 dev->if_port = 0;
1556 outl(0, ioaddr + CSR13);
1557 outl(0x0003FFFF, ioaddr + CSR14);
1558 outw(t21142_csr15[dev->if_port], ioaddr + CSR15);
1559 outl(t21142_csr13[dev->if_port], ioaddr + CSR13);
1560 } else {
1561 /* Select 100mbps port to check for link beat. */
1562 new_csr6 = 0x83860000;
1563 dev->if_port = 3;
1564 outl(0, ioaddr + CSR13);
1565 outl(0x0003FF7F, ioaddr + CSR14);
1566 outw(8, ioaddr + CSR15);
1567 outl(1, ioaddr + CSR13);
1569 if (tulip_debug > 1)
1570 printk(KERN_INFO"%s: Testing new 21143 media %s.\n",
1571 dev->name, medianame[dev->if_port]);
1572 if (new_csr6 != (tp->csr6 & ~0x00D5)) {
1573 tp->csr6 &= 0x00D5;
1574 tp->csr6 |= new_csr6;
1575 outl(0x0301, ioaddr + CSR12);
1576 outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1577 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1579 next_tick = 3*HZ;
1582 tp->timer.expires = RUN_AT(next_tick);
1583 add_timer(&tp->timer);
1586 static void t21142_start_nway(struct net_device *dev)
1588 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1589 long ioaddr = dev->base_addr;
1590 int csr14 = ((tp->to_advertise & 0x0780) << 9) |
1591 ((tp->to_advertise&0x0020)<<1) | 0xffbf;
1593 dev->if_port = 0;
1594 tp->nway = tp->mediasense = 1;
1595 tp->nwayset = tp->lpar = 0;
1596 if (debug > 1)
1597 printk(KERN_DEBUG "%s: Restarting 21143 autonegotiation, %8.8x.\n",
1598 dev->name, csr14);
1599 outl(0x0001, ioaddr + CSR13);
1600 outl(csr14, ioaddr + CSR14);
1601 tp->csr6 = 0x82420000 | (tp->to_advertise & 0x0040 ? 0x0200 : 0);
1602 outl(tp->csr6, ioaddr + CSR6);
1603 if (tp->mtable && tp->mtable->csr15dir) {
1604 outl(tp->mtable->csr15dir, ioaddr + CSR15);
1605 outl(tp->mtable->csr15val, ioaddr + CSR15);
1606 } else
1607 outw(0x0008, ioaddr + CSR15);
1608 outl(0x1301, ioaddr + CSR12); /* Trigger NWAY. */
1611 static void t21142_lnk_change(struct net_device *dev, int csr5)
1613 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1614 long ioaddr = dev->base_addr;
1615 int csr12 = inl(ioaddr + CSR12);
1617 if (tulip_debug > 1)
1618 printk(KERN_INFO"%s: 21143 link status interrupt %8.8x, CSR5 %x, "
1619 "%8.8x.\n", dev->name, csr12, csr5, inl(ioaddr + CSR14));
1621 /* If NWay finished and we have a negotiated partner capability. */
1622 if (tp->nway && !tp->nwayset && (csr12 & 0x7000) == 0x5000) {
1623 int setup_done = 0;
1624 int negotiated = tp->to_advertise & (csr12 >> 16);
1625 tp->lpar = csr12 >> 16;
1626 tp->nwayset = 1;
1627 if (negotiated & 0x0100) dev->if_port = 5;
1628 else if (negotiated & 0x0080) dev->if_port = 3;
1629 else if (negotiated & 0x0040) dev->if_port = 4;
1630 else if (negotiated & 0x0020) dev->if_port = 0;
1631 else {
1632 tp->nwayset = 0;
1633 if ((csr12 & 2) == 0 && (tp->to_advertise & 0x0180))
1634 dev->if_port = 3;
1636 tp->full_duplex = (media_cap[dev->if_port] & MediaAlwaysFD) ? 1:0;
1638 if (tulip_debug > 1) {
1639 if (tp->nwayset)
1640 printk(KERN_INFO "%s: Switching to %s based on link "
1641 "negotiation %4.4x & %4.4x = %4.4x.\n",
1642 dev->name, medianame[dev->if_port], tp->to_advertise,
1643 tp->lpar, negotiated);
1644 else
1645 printk(KERN_INFO "%s: Autonegotiation failed, using %s,"
1646 " link beat status %4.4x.\n",
1647 dev->name, medianame[dev->if_port], csr12);
1650 if (tp->mtable) {
1651 int i;
1652 for (i = 0; i < tp->mtable->leafcount; i++)
1653 if (tp->mtable->mleaf[i].media == dev->if_port) {
1654 tp->cur_index = i;
1655 select_media(dev, 0);
1656 setup_done = 1;
1657 break;
1660 if ( ! setup_done) {
1661 tp->csr6 = dev->if_port & 1 ? 0x83860000 : 0x82420000;
1662 if (tp->full_duplex)
1663 tp->csr6 |= 0x0200;
1664 outl(1, ioaddr + CSR13);
1666 #if 0 /* Restart shouldn't be needed. */
1667 outl(tp->csr6 | 0x0000, ioaddr + CSR6);
1668 if (debug > 2)
1669 printk(KERN_DEBUG "%s: Restarting Tx and Rx, CSR5 is %8.8x.\n",
1670 dev->name, inl(ioaddr + CSR5));
1671 #endif
1672 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1673 if (debug > 2)
1674 printk(KERN_DEBUG "%s: Setting CSR6 %8.8x/%x CSR12 %8.8x.\n",
1675 dev->name, tp->csr6, inl(ioaddr + CSR6),
1676 inl(ioaddr + CSR12));
1677 } else if ((tp->nwayset && (csr5 & 0x08000000)
1678 && (dev->if_port == 3 || dev->if_port == 5)
1679 && (csr12 & 2) == 2) ||
1680 (tp->nway && (csr5 & (TPLnkFail)))) {
1681 /* Link blew? Maybe restart NWay. */
1682 del_timer(&tp->timer);
1683 t21142_start_nway(dev);
1684 tp->timer.expires = RUN_AT(3*HZ);
1685 add_timer(&tp->timer);
1686 } else if (dev->if_port == 3 || dev->if_port == 5) {
1687 if (tulip_debug > 1)
1688 printk(KERN_INFO"%s: 21143 %s link beat %s.\n",
1689 dev->name, medianame[dev->if_port],
1690 (csr12 & 2) ? "failed" : "good");
1691 if ((csr12 & 2) && ! tp->medialock) {
1692 del_timer(&tp->timer);
1693 t21142_start_nway(dev);
1694 tp->timer.expires = RUN_AT(3*HZ);
1695 add_timer(&tp->timer);
1697 } else if (dev->if_port == 0 || dev->if_port == 4) {
1698 if ((csr12 & 4) == 0)
1699 printk(KERN_INFO"%s: 21143 10baseT link beat good.\n",
1700 dev->name);
1701 } else if (!(csr12 & 4)) { /* 10mbps link beat good. */
1702 if (tulip_debug)
1703 printk(KERN_INFO"%s: 21143 10mbps sensed media.\n",
1704 dev->name);
1705 dev->if_port = 0;
1706 } else if (tp->nwayset) {
1707 if (tulip_debug)
1708 printk(KERN_INFO"%s: 21143 using NWay-set %s, csr6 %8.8x.\n",
1709 dev->name, medianame[dev->if_port], tp->csr6);
1710 } else { /* 100mbps link beat good. */
1711 if (tulip_debug)
1712 printk(KERN_INFO"%s: 21143 100baseTx sensed media.\n",
1713 dev->name);
1714 dev->if_port = 3;
1715 tp->csr6 = 0x83860000;
1716 outl(0x0003FF7F, ioaddr + CSR14);
1717 outl(0x0301, ioaddr + CSR12);
1718 outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1719 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1723 static void mxic_timer(unsigned long data)
1725 struct net_device *dev = (struct net_device *)data;
1726 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1727 long ioaddr = dev->base_addr;
1728 int next_tick = 60*HZ;
1730 if (tulip_debug > 3) {
1731 printk(KERN_INFO"%s: MXIC negotiation status %8.8x.\n", dev->name,
1732 inl(ioaddr + CSR12));
1734 if (next_tick) {
1735 tp->timer.expires = RUN_AT(next_tick);
1736 add_timer(&tp->timer);
1740 static void pnic_do_nway(struct net_device *dev)
1742 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1743 long ioaddr = dev->base_addr;
1744 u32 phy_reg = inl(ioaddr + 0xB8);
1745 u32 new_csr6 = tp->csr6 & ~0x40C40200;
1747 if (phy_reg & 0x78000000) { /* Ignore baseT4 */
1748 if (phy_reg & 0x20000000) dev->if_port = 5;
1749 else if (phy_reg & 0x40000000) dev->if_port = 3;
1750 else if (phy_reg & 0x10000000) dev->if_port = 4;
1751 else if (phy_reg & 0x08000000) dev->if_port = 0;
1752 tp->nwayset = 1;
1753 new_csr6 = (dev->if_port & 1) ? 0x01860000 : 0x00420000;
1754 outl(0x32 | (dev->if_port & 1), ioaddr + CSR12);
1755 if (dev->if_port & 1)
1756 outl(0x1F868, ioaddr + 0xB8);
1757 if (phy_reg & 0x30000000) {
1758 tp->full_duplex = 1;
1759 new_csr6 |= 0x00000200;
1761 if (tulip_debug > 1)
1762 printk(KERN_DEBUG "%s: PNIC autonegotiated status %8.8x, %s.\n",
1763 dev->name, phy_reg, medianame[dev->if_port]);
1764 if (tp->csr6 != new_csr6) {
1765 tp->csr6 = new_csr6;
1766 outl(tp->csr6 | 0x0002, ioaddr + CSR6); /* Restart Tx */
1767 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1768 dev->trans_start = jiffies;
1772 static void pnic_lnk_change(struct net_device *dev, int csr5)
1774 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1775 long ioaddr = dev->base_addr;
1776 int phy_reg = inl(ioaddr + 0xB8);
1778 if (tulip_debug > 1)
1779 printk(KERN_DEBUG "%s: PNIC link changed state %8.8x, CSR5 %8.8x.\n",
1780 dev->name, phy_reg, csr5);
1781 if (inl(ioaddr + CSR5) & TPLnkFail) {
1782 outl((inl(ioaddr + CSR7) & ~TPLnkFail) | TPLnkPass, ioaddr + CSR7);
1783 if (! tp->nwayset || jiffies - dev->trans_start > 1*HZ) {
1784 tp->csr6 = 0x00420000 | (tp->csr6 & 0x0000fdff);
1785 outl(tp->csr6, ioaddr + CSR6);
1786 outl(0x30, ioaddr + CSR12);
1787 outl(0x0201F078, ioaddr + 0xB8); /* Turn on autonegotiation. */
1788 dev->trans_start = jiffies;
1790 } else if (inl(ioaddr + CSR5) & TPLnkPass) {
1791 pnic_do_nway(dev);
1792 outl((inl(ioaddr + CSR7) & ~TPLnkPass) | TPLnkFail, ioaddr + CSR7);
1795 static void pnic_timer(unsigned long data)
1797 struct net_device *dev = (struct net_device *)data;
1798 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1799 long ioaddr = dev->base_addr;
1800 int next_tick = 60*HZ;
1802 if (media_cap[dev->if_port] & MediaIsMII) {
1803 if (check_duplex(dev) > 0)
1804 next_tick = 3*HZ;
1805 } else {
1806 int csr12 = inl(ioaddr + CSR12);
1807 int new_csr6 = tp->csr6 & ~0x40C40200;
1808 int phy_reg = inl(ioaddr + 0xB8);
1809 int csr5 = inl(ioaddr + CSR5);
1811 if (tulip_debug > 1)
1812 printk(KERN_DEBUG "%s: PNIC timer PHY status %8.8x, %s "
1813 "CSR5 %8.8x.\n",
1814 dev->name, phy_reg, medianame[dev->if_port], csr5);
1815 if (phy_reg & 0x04000000) { /* Remote link fault */
1816 outl(0x0201F078, ioaddr + 0xB8);
1817 next_tick = 1*HZ;
1818 tp->nwayset = 0;
1819 } else if (phy_reg & 0x78000000) { /* Ignore baseT4 */
1820 pnic_do_nway(dev);
1821 next_tick = 60*HZ;
1822 } else if (csr5 & TPLnkFail) { /* 100baseTx link beat */
1823 if (tulip_debug > 1)
1824 printk(KERN_DEBUG "%s: %s link beat failed, CSR12 %4.4x, "
1825 "CSR5 %8.8x, PHY %3.3x.\n",
1826 dev->name, medianame[dev->if_port], csr12,
1827 inl(ioaddr + CSR5), inl(ioaddr + 0xB8));
1828 next_tick = 3*HZ;
1829 if (tp->medialock) {
1830 } else if (tp->nwayset && (dev->if_port & 1)) {
1831 next_tick = 1*HZ;
1832 } else if (dev->if_port == 0) {
1833 dev->if_port = 3;
1834 outl(0x33, ioaddr + CSR12);
1835 new_csr6 = 0x01860000;
1836 outl(0x1F868, ioaddr + 0xB8);
1837 } else {
1838 dev->if_port = 0;
1839 outl(0x32, ioaddr + CSR12);
1840 new_csr6 = 0x00420000;
1841 outl(0x1F078, ioaddr + 0xB8);
1843 if (tp->csr6 != new_csr6) {
1844 tp->csr6 = new_csr6;
1845 outl(tp->csr6 | 0x0002, ioaddr + CSR6); /* Restart Tx */
1846 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1847 dev->trans_start = jiffies;
1848 if (tulip_debug > 1)
1849 printk(KERN_INFO "%s: Changing PNIC configuration to %s "
1850 "%s-duplex, CSR6 %8.8x.\n",
1851 dev->name, medianame[dev->if_port],
1852 tp->full_duplex ? "full" : "half", new_csr6);
1856 tp->timer.expires = RUN_AT(next_tick);
1857 add_timer(&tp->timer);
1860 static void comet_timer(unsigned long data)
1862 struct net_device *dev = (struct net_device *)data;
1863 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1864 long ioaddr = dev->base_addr;
1865 int next_tick = 60*HZ;
1867 if (tulip_debug > 1)
1868 printk(KERN_DEBUG "%s: Comet link status %4.4x partner capability "
1869 "%4.4x.\n",
1870 dev->name, inl(ioaddr + 0xB8), inl(ioaddr + 0xC8));
1871 tp->timer.expires = RUN_AT(next_tick);
1872 add_timer(&tp->timer);
1875 static void tulip_tx_timeout(struct net_device *dev)
1877 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1878 long ioaddr = dev->base_addr;
1880 if (media_cap[dev->if_port] & MediaIsMII) {
1881 /* Do nothing -- the media monitor should handle this. */
1882 if (tulip_debug > 1)
1883 printk(KERN_WARNING "%s: Transmit timeout using MII device.\n",
1884 dev->name);
1885 } else if (tp->chip_id == DC21040) {
1886 if ( !tp->medialock && inl(ioaddr + CSR12) & 0x0002) {
1887 dev->if_port = (dev->if_port == 2 ? 0 : 2);
1888 printk(KERN_INFO "%s: transmit timed out, switching to "
1889 "%s.\n",
1890 dev->name, medianame[dev->if_port]);
1891 select_media(dev, 0);
1893 dev->trans_start = jiffies;
1894 return;
1895 } else if (tp->chip_id == DC21041) {
1896 int csr12 = inl(ioaddr + CSR12);
1898 printk(KERN_WARNING "%s: 21041 transmit timed out, status %8.8x, "
1899 "CSR12 %8.8x, CSR13 %8.8x, CSR14 %8.8x, resetting...\n",
1900 dev->name, inl(ioaddr + CSR5), csr12,
1901 inl(ioaddr + CSR13), inl(ioaddr + CSR14));
1902 tp->mediasense = 1;
1903 if ( ! tp->medialock) {
1904 if (dev->if_port == 1 || dev->if_port == 2)
1905 if (csr12 & 0x0004) {
1906 dev->if_port = 2 - dev->if_port;
1907 } else
1908 dev->if_port = 0;
1909 else
1910 dev->if_port = 1;
1911 select_media(dev, 0);
1913 } else if (tp->chip_id == DC21140 || tp->chip_id == DC21142
1914 || tp->chip_id == MX98713 || tp->chip_id == COMPEX9881) {
1915 printk(KERN_WARNING "%s: 21140 transmit timed out, status %8.8x, "
1916 "SIA %8.8x %8.8x %8.8x %8.8x, resetting...\n",
1917 dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12),
1918 inl(ioaddr + CSR13), inl(ioaddr + CSR14), inl(ioaddr + CSR15));
1919 if ( ! tp->medialock && tp->mtable) {
1921 --tp->cur_index;
1922 while (tp->cur_index >= 0
1923 && (media_cap[tp->mtable->mleaf[tp->cur_index].media]
1924 & MediaIsFD));
1925 if (--tp->cur_index < 0) {
1926 /* We start again, but should instead look for default. */
1927 tp->cur_index = tp->mtable->leafcount - 1;
1929 select_media(dev, 0);
1930 printk(KERN_WARNING "%s: transmit timed out, switching to %s "
1931 "media.\n", dev->name, medianame[dev->if_port]);
1933 } else {
1934 printk(KERN_WARNING "%s: Transmit timed out, status %8.8x, CSR12 "
1935 "%8.8x, resetting...\n",
1936 dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12));
1937 dev->if_port = 0;
1940 #if defined(way_too_many_messages)
1941 if (tulip_debug > 3) {
1942 int i;
1943 for (i = 0; i < RX_RING_SIZE; i++) {
1944 u8 *buf = (u8 *)(tp->rx_ring[i].buffer1);
1945 int j;
1946 printk(KERN_DEBUG "%2d: %8.8x %8.8x %8.8x %8.8x "
1947 "%2.2x %2.2x %2.2x.\n",
1948 i, (unsigned int)tp->rx_ring[i].status,
1949 (unsigned int)tp->rx_ring[i].length,
1950 (unsigned int)tp->rx_ring[i].buffer1,
1951 (unsigned int)tp->rx_ring[i].buffer2,
1952 buf[0], buf[1], buf[2]);
1953 for (j = 0; buf[j] != 0xee && j < 1600; j++)
1954 if (j < 100) printk(" %2.2x", buf[j]);
1955 printk(" j=%d.\n", j);
1957 printk(KERN_DEBUG " Rx ring %8.8x: ", (int)tp->rx_ring);
1958 for (i = 0; i < RX_RING_SIZE; i++)
1959 printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
1960 printk("\n" KERN_DEBUG " Tx ring %8.8x: ", (int)tp->tx_ring);
1961 for (i = 0; i < TX_RING_SIZE; i++)
1962 printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
1963 printk("\n");
1965 #endif
1967 /* Stop and restart the chip's Tx processes . */
1968 outl(tp->csr6 | 0x0002, ioaddr + CSR6);
1969 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
1970 /* Trigger an immediate transmit demand. */
1971 outl(0, ioaddr + CSR1);
1973 dev->trans_start = jiffies;
1974 tp->stats.tx_errors++;
1975 return;
1979 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1980 static void tulip_init_ring(struct net_device *dev)
1982 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1983 int i;
1985 tp->tx_full = 0;
1986 tp->cur_rx = tp->cur_tx = 0;
1987 tp->dirty_rx = tp->dirty_tx = 0;
1988 tp->susp_rx = 0;
1989 tp->ttimer = 0;
1990 tp->nir = 0;
1992 for (i = 0; i < RX_RING_SIZE; i++) {
1993 tp->rx_ring[i].status = 0x00000000;
1994 tp->rx_ring[i].length = cpu_to_le32(PKT_BUF_SZ);
1995 tp->rx_ring[i].buffer2 = virt_to_le32desc(&tp->rx_ring[i+1]);
1996 tp->rx_skbuff[i] = NULL;
1998 /* Mark the last entry as wrapping the ring. */
1999 tp->rx_ring[i-1].length = cpu_to_le32(PKT_BUF_SZ | DESC_RING_WRAP);
2000 tp->rx_ring[i-1].buffer2 = virt_to_le32desc(&tp->rx_ring[0]);
2002 for (i = 0; i < RX_RING_SIZE; i++) {
2003 /* Note the receive buffer must be longword aligned.
2004 dev_alloc_skb() provides 16 byte alignment. But do *not*
2005 use skb_reserve() to align the IP header! */
2006 struct sk_buff *skb = dev_alloc_skb(PKT_BUF_SZ);
2007 tp->rx_skbuff[i] = skb;
2008 if (skb == NULL)
2009 break;
2010 skb->dev = dev; /* Mark as being used by this device. */
2011 tp->rx_ring[i].status = cpu_to_le32(DescOwned); /* Owned by Tulip chip */
2012 tp->rx_ring[i].buffer1 = virt_to_le32desc(skb->tail);
2014 tp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
2016 /* The Tx buffer descriptor is filled in as needed, but we
2017 do need to clear the ownership bit. */
2018 for (i = 0; i < TX_RING_SIZE; i++) {
2019 tp->tx_skbuff[i] = 0;
2020 tp->tx_ring[i].status = 0x00000000;
2021 tp->tx_ring[i].buffer2 = virt_to_le32desc(&tp->tx_ring[i+1]);
2023 tp->tx_ring[i-1].buffer2 = virt_to_le32desc(&tp->tx_ring[0]);
2026 static int
2027 tulip_start_xmit(struct sk_buff *skb, struct net_device *dev)
2029 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2030 int entry;
2031 u32 flag;
2032 unsigned long cpuflags;
2034 /* Caution: the write order is important here, set the field
2035 with the ownership bits last. */
2037 spin_lock_irqsave(&tp->tx_lock, cpuflags);
2039 /* Calculate the next Tx descriptor entry. */
2040 entry = tp->cur_tx % TX_RING_SIZE;
2042 tp->tx_skbuff[entry] = skb;
2043 tp->tx_ring[entry].buffer1 = virt_to_le32desc(skb->data);
2045 if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE/2) {/* Typical path */
2046 flag = 0x60000000; /* No interrupt */
2047 } else if (tp->cur_tx - tp->dirty_tx == TX_RING_SIZE/2) {
2048 flag = 0xe0000000; /* Tx-done intr. */
2049 } else if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE - 2) {
2050 flag = 0x60000000; /* No Tx-done intr. */
2051 } else { /* Leave room for set_rx_mode() to fill entries. */
2052 tp->tx_full = 1;
2053 flag = 0xe0000000; /* Tx-done intr. */
2054 netif_stop_queue(dev);
2056 if (entry == TX_RING_SIZE-1)
2057 flag = 0xe0000000 | DESC_RING_WRAP;
2059 tp->tx_ring[entry].length = cpu_to_le32(skb->len | flag);
2060 tp->tx_ring[entry].status = cpu_to_le32(DescOwned);
2061 tp->cur_tx++;
2062 spin_unlock_irqrestore(&tp->tx_lock, cpuflags);
2064 /* Trigger an immediate transmit demand. */
2065 outl(0, dev->base_addr + CSR1);
2067 dev->trans_start = jiffies;
2069 return 0;
2072 /* The interrupt handler does all of the Rx thread work and cleans up
2073 after the Tx thread. */
2074 static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
2076 struct net_device *dev = (struct net_device *)dev_instance;
2077 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2078 long ioaddr = dev->base_addr;
2079 int csr5;
2080 int entry;
2081 int missed;
2082 int rx = 0;
2083 int tx = 0;
2084 int oi = 0;
2085 int maxrx = RX_RING_SIZE;
2086 int maxtx = TX_RING_SIZE;
2087 int maxoi = TX_RING_SIZE;
2089 tp->nir++;
2091 do {
2092 csr5 = inl(ioaddr + CSR5);
2093 /* Acknowledge all of the current interrupt sources ASAP. */
2094 outl(csr5 & 0x0001ffff, ioaddr + CSR5);
2096 if (tulip_debug > 4)
2097 printk(KERN_DEBUG "%s: interrupt csr5=%#8.8x new csr5=%#8.8x.\n",
2098 dev->name, csr5, inl(dev->base_addr + CSR5));
2100 if ((csr5 & (NormalIntr|AbnormalIntr)) == 0)
2101 break;
2103 if (csr5 & (RxIntr | RxNoBuf)) {
2104 rx += tulip_rx(dev);
2105 tulip_refill_rx(dev);
2108 if (csr5 & (TxNoBuf | TxDied | TxIntr | TimerInt)) {
2109 unsigned int dirty_tx;
2111 spin_lock(&tp->tx_lock);
2113 for (dirty_tx = tp->dirty_tx; tp->cur_tx - dirty_tx > 0;
2114 dirty_tx++) {
2115 int entry = dirty_tx % TX_RING_SIZE;
2116 int status = le32_to_cpu(tp->tx_ring[entry].status);
2118 if (status < 0)
2119 break; /* It still has not been Txed */
2120 /* Check for Rx filter setup frames. */
2121 if (tp->tx_skbuff[entry] == NULL)
2122 continue;
2124 if (status & 0x8000) {
2125 /* There was an major error, log it. */
2126 #ifndef final_version
2127 if (tulip_debug > 1)
2128 printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
2129 dev->name, status);
2130 #endif
2131 tp->stats.tx_errors++;
2132 if (status & 0x4104) tp->stats.tx_aborted_errors++;
2133 if (status & 0x0C00) tp->stats.tx_carrier_errors++;
2134 if (status & 0x0200) tp->stats.tx_window_errors++;
2135 if (status & 0x0002) tp->stats.tx_fifo_errors++;
2136 if ((status & 0x0080) && tp->full_duplex == 0)
2137 tp->stats.tx_heartbeat_errors++;
2138 #ifdef ETHER_STATS
2139 if (status & 0x0100) tp->stats.collisions16++;
2140 #endif
2141 } else {
2142 #ifdef ETHER_STATS
2143 if (status & 0x0001) tp->stats.tx_deferred++;
2144 #endif
2145 tp->stats.tx_bytes += tp->tx_skbuff[entry]->len;
2146 tp->stats.collisions += (status >> 3) & 15;
2147 tp->stats.tx_packets++;
2150 /* Free the original skb. */
2151 dev_kfree_skb_irq(tp->tx_skbuff[entry]);
2152 tp->tx_skbuff[entry] = 0;
2153 tx++;
2156 #ifndef final_version
2157 if (tp->cur_tx - dirty_tx > TX_RING_SIZE) {
2158 printk(KERN_ERR "%s: Out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
2159 dev->name, dirty_tx, tp->cur_tx, tp->tx_full);
2160 dirty_tx += TX_RING_SIZE;
2162 #endif
2164 if (tp->tx_full && tp->cur_tx - dirty_tx < TX_RING_SIZE - 2) {
2165 /* The ring is no longer full, clear tbusy. */
2166 tp->tx_full = 0;
2167 netif_wake_queue(dev);
2170 tp->dirty_tx = dirty_tx;
2171 if (csr5 & TxDied) {
2172 if (tulip_debug > 2)
2173 printk(KERN_WARNING "%s: The transmitter stopped."
2174 " CSR5 is %x, CSR6 %x, new CSR6 %x.\n",
2175 dev->name, csr5, inl(ioaddr + CSR6), tp->csr6);
2176 outl(tp->csr6 | 0x0002, ioaddr + CSR6);
2177 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
2179 spin_unlock(&tp->tx_lock);
2182 /* Log errors. */
2183 if (csr5 & AbnormalIntr) { /* Abnormal error summary bit. */
2184 if (csr5 == 0xffffffff)
2185 break;
2186 if (csr5 & TxJabber) tp->stats.tx_errors++;
2187 if (csr5 & TxFIFOUnderflow) {
2188 if ((tp->csr6 & 0xC000) != 0xC000)
2189 tp->csr6 += 0x4000; /* Bump up the Tx threshold */
2190 else
2191 tp->csr6 |= 0x00200000; /* Store-n-forward. */
2192 /* Restart the transmit process. */
2193 outl(tp->csr6 | 0x0002, ioaddr + CSR6);
2194 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
2195 outl(0, ioaddr + CSR1);
2197 if (csr5 & RxDied) { /* Missed a Rx frame. */
2198 tp->stats.rx_errors++;
2199 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2200 outl(tp->csr6 | 0x2002, ioaddr + CSR6);
2202 if (csr5 & (TPLnkPass | TPLnkFail | 0x08000000)) {
2203 if (tp->link_change)
2204 (tp->link_change)(dev, csr5);
2206 if (csr5 & SytemError) {
2207 printk(KERN_ERR "%s: (%lu) System Error occured\n", dev->name, tp->nir);
2209 /* Clear all error sources, included undocumented ones! */
2210 outl(0x0800f7ba, ioaddr + CSR5);
2211 oi++;
2213 if (csr5 & TimerInt) {
2214 #if 0
2215 if (tulip_debug > 2)
2216 printk(KERN_ERR "%s: Re-enabling interrupts, %8.8x.\n",
2217 dev->name, csr5);
2218 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
2219 #endif
2220 tp->ttimer = 0;
2221 oi++;
2223 if (tx > maxtx || rx > maxrx || oi > maxoi) {
2224 if (tulip_debug > 1)
2225 printk(KERN_WARNING "%s: Too much work during an interrupt, "
2226 "csr5=0x%8.8x. (%lu) (%d,%d,%d)\n", dev->name, csr5, tp->nir, tx, rx, oi);
2227 /* Acknowledge all interrupt sources. */
2228 #if 0
2229 /* Clear all interrupting sources, set timer to re-enable. */
2230 outl(((~csr5) & 0x0001ebef) | NormalIntr | AbnormalIntr | TimerInt,
2231 ioaddr + CSR7);
2232 outl(12, ioaddr + CSR11);
2233 tp->ttimer = 1;
2234 #endif
2235 break;
2237 } while (1);
2239 tulip_refill_rx(dev);
2241 /* check if we card is in suspend mode */
2242 entry = tp->dirty_rx % RX_RING_SIZE;
2243 if (tp->rx_skbuff[entry] == NULL) {
2244 if (tulip_debug > 1)
2245 printk(KERN_WARNING "%s: in rx suspend mode: (%lu) (tp->cur_rx = %u, ttimer = %d, rx = %d) go/stay in suspend mode\n", dev->name, tp->nir, tp->cur_rx, tp->ttimer, rx);
2246 if (tp->ttimer == 0 || (inl(ioaddr + CSR11) & 0xffff) == 0) {
2247 if (tulip_debug > 1)
2248 printk(KERN_WARNING "%s: in rx suspend mode: (%lu) set timer\n", dev->name, tp->nir);
2249 outl(tulip_tbl[tp->chip_id].valid_intrs | TimerInt,
2250 ioaddr + CSR7);
2251 outl(TimerInt, ioaddr + CSR5);
2252 outl(12, ioaddr + CSR11);
2253 tp->ttimer = 1;
2257 if ((missed = inl(ioaddr + CSR8) & 0x1ffff)) {
2258 tp->stats.rx_dropped += missed & 0x10000 ? 0x10000 : missed;
2261 if (tulip_debug > 4)
2262 printk(KERN_DEBUG "%s: exiting interrupt, csr5=%#4.4x.\n",
2263 dev->name, inl(ioaddr + CSR5));
2267 static int tulip_refill_rx(struct net_device *dev)
2269 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2270 int entry;
2271 int refilled = 0;
2273 /* Refill the Rx ring buffers. */
2274 for (; tp->cur_rx - tp->dirty_rx > 0; tp->dirty_rx++) {
2275 entry = tp->dirty_rx % RX_RING_SIZE;
2276 if (tp->rx_skbuff[entry] == NULL) {
2277 struct sk_buff *skb;
2278 skb = tp->rx_skbuff[entry] = dev_alloc_skb(PKT_BUF_SZ);
2279 if (skb == NULL)
2280 break;
2281 skb->dev = dev; /* Mark as being used by this device. */
2282 tp->rx_ring[entry].buffer1 = virt_to_le32desc(skb->tail);
2283 refilled++;
2285 tp->rx_ring[entry].status = cpu_to_le32(DescOwned);
2287 return refilled;
2290 static int tulip_rx(struct net_device *dev)
2292 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2293 int entry = tp->cur_rx % RX_RING_SIZE;
2294 int rx_work_limit = tp->dirty_rx + RX_RING_SIZE - tp->cur_rx;
2295 int received = 0;
2297 if (tulip_debug > 4)
2298 printk(KERN_DEBUG " In tulip_rx(), entry %d %8.8x.\n", entry,
2299 tp->rx_ring[entry].status);
2300 /* If we own the next entry, it is a new packet. Send it up. */
2301 while ( ! (tp->rx_ring[entry].status & cpu_to_le32(DescOwned))) {
2302 s32 status = le32_to_cpu(tp->rx_ring[entry].status);
2304 if (tulip_debug > 5)
2305 printk(KERN_DEBUG "%s: In tulip_rx(), entry %d %8.8x.\n",
2306 dev->name, entry, status);
2307 if (--rx_work_limit < 0)
2308 break;
2309 if ((status & 0x38008300) != 0x0300) {
2310 if ((status & 0x38000300) != 0x0300) {
2311 /* Ingore earlier buffers. */
2312 if ((status & 0xffff) != 0x7fff) {
2313 if (tulip_debug > 1)
2314 printk(KERN_WARNING "%s: Oversized Ethernet frame "
2315 "spanned multiple buffers, status %8.8x!\n",
2316 dev->name, status);
2317 tp->stats.rx_length_errors++;
2319 } else if (status & RxDescFatalErr) {
2320 /* There was a fatal error. */
2321 if (tulip_debug > 2)
2322 printk(KERN_DEBUG "%s: Receive error, Rx status %8.8x.\n",
2323 dev->name, status);
2324 tp->stats.rx_errors++; /* end of a packet.*/
2325 if (status & 0x0890) tp->stats.rx_length_errors++;
2326 if (status & 0x0004) tp->stats.rx_frame_errors++;
2327 if (status & 0x0002) tp->stats.rx_crc_errors++;
2328 if (status & 0x0001) tp->stats.rx_fifo_errors++;
2330 } else {
2331 /* Omit the four octet CRC from the length. */
2332 short pkt_len = ((status >> 16) & 0x7ff) - 4;
2333 struct sk_buff *skb;
2335 #ifndef final_version
2336 if (pkt_len > 1518) {
2337 printk(KERN_WARNING "%s: Bogus packet size of %d (%#x).\n",
2338 dev->name, pkt_len, pkt_len);
2339 pkt_len = 1518;
2340 tp->stats.rx_length_errors++;
2342 #endif
2343 /* Check if the packet is long enough to accept without copying
2344 to a minimally-sized skbuff. */
2345 if (pkt_len < rx_copybreak
2346 && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
2347 skb->dev = dev;
2348 skb_reserve(skb, 2); /* 16 byte align the IP header */
2349 #if ! defined(__alpha__)
2350 eth_copy_and_sum(skb, tp->rx_skbuff[entry]->tail, pkt_len, 0);
2351 skb_put(skb, pkt_len);
2352 #else
2353 memcpy(skb_put(skb, pkt_len), tp->rx_skbuff[entry]->tail,
2354 pkt_len);
2355 #endif
2356 } else { /* Pass up the skb already on the Rx ring. */
2357 char *temp = skb_put(skb = tp->rx_skbuff[entry], pkt_len);
2358 tp->rx_skbuff[entry] = NULL;
2359 #ifndef final_version
2360 if (le32desc_to_virt(tp->rx_ring[entry].buffer1) != temp)
2361 printk(KERN_ERR "%s: Internal fault: The skbuff addresses "
2362 "do not match in tulip_rx: %p vs. %p / %p.\n",
2363 dev->name,
2364 le32desc_to_virt(tp->rx_ring[entry].buffer1),
2365 skb->head, temp);
2366 #endif
2368 skb->protocol = eth_type_trans(skb, dev);
2369 netif_rx(skb);
2370 dev->last_rx = jiffies;
2371 tp->stats.rx_packets++;
2372 tp->stats.rx_bytes += pkt_len;
2374 received++;
2375 entry = (++tp->cur_rx) % RX_RING_SIZE;
2378 return received;
2381 static int
2382 tulip_close(struct net_device *dev)
2384 long ioaddr = dev->base_addr;
2385 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2386 int i;
2388 netif_stop_queue(dev);
2390 if (tulip_debug > 1)
2391 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
2392 dev->name, inl(ioaddr + CSR5));
2394 /* Disable interrupts by clearing the interrupt mask. */
2395 outl(0x00000000, ioaddr + CSR7);
2396 /* Stop the Tx and Rx processes. */
2397 outl(inl(ioaddr + CSR6) & ~0x2002, ioaddr + CSR6);
2398 /* 21040 -- Leave the card in 10baseT state. */
2399 if (tp->chip_id == DC21040)
2400 outl(0x00000004, ioaddr + CSR13);
2402 if (inl(ioaddr + CSR6) != 0xffffffff)
2403 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2405 del_timer(&tp->timer);
2407 free_irq(dev->irq, dev);
2409 dev->if_port = tp->saved_if_port;
2411 /* Free all the skbuffs in the Rx queue. */
2412 for (i = 0; i < RX_RING_SIZE; i++) {
2413 struct sk_buff *skb = tp->rx_skbuff[i];
2414 tp->rx_skbuff[i] = 0;
2415 tp->rx_ring[i].status = 0; /* Not owned by Tulip chip. */
2416 tp->rx_ring[i].length = 0;
2417 tp->rx_ring[i].buffer1 = 0xBADF00D0; /* An invalid address. */
2418 if (skb) {
2419 dev_kfree_skb(skb);
2422 for (i = 0; i < TX_RING_SIZE; i++) {
2423 if (tp->tx_skbuff[i])
2424 dev_kfree_skb(tp->tx_skbuff[i]);
2425 tp->tx_skbuff[i] = 0;
2428 /* Leave the driver in snooze, not sleep, mode. */
2429 if (tp->flags & HAS_PWRDWN)
2430 pci_write_config_dword(tp->pdev, 0x40, 0x40000000);
2432 MOD_DEC_USE_COUNT;
2434 return 0;
2437 static struct enet_statistics *
2438 tulip_get_stats(struct net_device *dev)
2440 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2441 long ioaddr = dev->base_addr;
2443 if (netif_running(dev))
2444 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
2446 return &tp->stats;
2450 /* Provide ioctl() calls to examine the MII xcvr state. */
2451 static int private_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2453 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2454 long ioaddr = dev->base_addr;
2455 u16 *data = (u16 *)&rq->ifr_data;
2456 int phy = tp->phys[0] & 0x1f;
2457 long flags;
2459 switch(cmd) {
2460 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
2461 if (tp->mii_cnt)
2462 data[0] = phy;
2463 else if (tp->flags & HAS_NWAY143)
2464 data[0] = 32;
2465 else if (tp->chip_id == COMET)
2466 data[0] = 1;
2467 else
2468 return -ENODEV;
2469 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
2470 if (data[0] == 32 && (tp->flags & HAS_NWAY143)) {
2471 int csr12 = inl(ioaddr + CSR12);
2472 int csr14 = inl(ioaddr + CSR14);
2473 switch (data[1]) {
2474 case 0: {
2475 data[3] = (csr14<<5) & 0x1000;
2476 break; }
2477 case 1:
2478 data[3] = 0x7848 + ((csr12&0x7000) == 0x5000 ? 0x20 : 0)
2479 + (csr12&0x06 ? 0x04 : 0);
2480 break;
2481 case 4: {
2482 data[3] = ((csr14>>9)&0x07C0) +
2483 ((inl(ioaddr + CSR6)>>3)&0x0040) + ((csr14>>1)&0x20) + 1;
2484 break;
2486 case 5: data[3] = csr12 >> 16; break;
2487 default: data[3] = 0; break;
2489 } else {
2490 save_flags(flags);
2491 cli();
2492 data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
2493 restore_flags(flags);
2495 return 0;
2496 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
2497 if (!capable(CAP_NET_ADMIN))
2498 return -EPERM;
2499 if (data[0] == 32 && (tp->flags & HAS_NWAY143)) {
2500 if (data[1] == 5)
2501 tp->to_advertise = data[2];
2502 } else {
2503 save_flags(flags);
2504 cli();
2505 mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
2506 restore_flags(flags);
2508 return 0;
2509 default:
2510 return -EOPNOTSUPP;
2513 return -EOPNOTSUPP;
2517 /* Set or clear the multicast filter for this adaptor.
2518 Note that we only use exclusion around actually queueing the
2519 new frame, not around filling tp->setup_frame. This is non-deterministic
2520 when re-entered but still correct. */
2522 /* The little-endian AUTODIN32 ethernet CRC calculation.
2523 N.B. Do not use for bulk data, use a table-based routine instead.
2524 This is common code and should be moved to net/core/crc.c */
2525 static unsigned const ethernet_polynomial_le = 0xedb88320U;
2526 static inline u32 ether_crc_le(int length, unsigned char *data)
2528 u32 crc = 0xffffffff; /* Initial value. */
2529 while(--length >= 0) {
2530 unsigned char current_octet = *data++;
2531 int bit;
2532 for (bit = 8; --bit >= 0; current_octet >>= 1) {
2533 if ((crc ^ current_octet) & 1) {
2534 crc >>= 1;
2535 crc ^= ethernet_polynomial_le;
2536 } else
2537 crc >>= 1;
2540 return crc;
2542 static unsigned const ethernet_polynomial = 0x04c11db7U;
2543 static inline u32 ether_crc(int length, unsigned char *data)
2545 int crc = -1;
2547 while(--length >= 0) {
2548 unsigned char current_octet = *data++;
2549 int bit;
2550 for (bit = 0; bit < 8; bit++, current_octet >>= 1)
2551 crc = (crc << 1) ^
2552 ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
2554 return crc;
2557 static void set_rx_mode(struct net_device *dev)
2559 struct tulip_private *tp = (struct tulip_private *)dev->priv;
2560 long ioaddr = dev->base_addr;
2561 int csr6 = inl(ioaddr + CSR6) & ~0x00D5;
2562 unsigned long cpuflags;
2564 tp->csr6 &= ~0x00D5;
2565 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2566 tp->csr6 |= 0x00C0;
2567 csr6 |= 0x00C0;
2568 /* Unconditionally log net taps. */
2569 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
2570 } else if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
2571 /* Too many to filter well -- accept all multicasts. */
2572 tp->csr6 |= 0x0080;
2573 csr6 |= 0x0080;
2574 } else if (tp->flags & MC_HASH_ONLY) {
2575 /* Some work-alikes have only a 64-entry hash filter table. */
2576 /* Should verify correctness on big-endian/__powerpc__ */
2577 struct dev_mc_list *mclist;
2578 int i;
2579 u32 mc_filter[2]; /* Multicast hash filter */
2580 if (dev->mc_count > 64) { /* Arbitrary non-effective limit. */
2581 tp->csr6 |= 0x0080;
2582 csr6 |= 0x0080;
2583 } else {
2584 mc_filter[1] = mc_filter[0] = 0;
2585 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2586 i++, mclist = mclist->next)
2587 set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr)>>26, mc_filter);
2588 if (tp->chip_id == AX88140) {
2589 outl(2, ioaddr + CSR13);
2590 outl(mc_filter[0], ioaddr + CSR14);
2591 outl(3, ioaddr + CSR13);
2592 outl(mc_filter[1], ioaddr + CSR14);
2593 } else if (tp->chip_id == COMET) { /* Has a simple hash filter. */
2594 outl(mc_filter[0], ioaddr + 0xAC);
2595 outl(mc_filter[1], ioaddr + 0xB0);
2598 } else {
2599 u16 *eaddrs, *setup_frm = tp->setup_frame;
2600 struct dev_mc_list *mclist;
2601 u32 tx_flags = 0x08000000 | 192;
2602 int i;
2604 /* Note that only the low-address shortword of setup_frame is valid!
2605 The values are doubled for big-endian architectures. */
2606 if (dev->mc_count > 14) { /* Must use a multicast hash table. */
2607 u16 hash_table[32];
2608 tx_flags = 0x08400000 | 192; /* Use hash filter. */
2609 memset(hash_table, 0, sizeof(hash_table));
2610 set_bit(255, hash_table); /* Broadcast entry */
2611 /* This should work on big-endian machines as well. */
2612 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2613 i++, mclist = mclist->next)
2614 set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff,
2615 hash_table);
2616 for (i = 0; i < 32; i++)
2617 *setup_frm++ = *setup_frm++ = hash_table[i];
2618 setup_frm = &tp->setup_frame[13*6];
2619 } else {
2620 /* We have <= 14 addresses so we can use the wonderful
2621 16 address perfect filtering of the Tulip. */
2622 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
2623 i++, mclist = mclist->next) {
2624 eaddrs = (u16 *)mclist->dmi_addr;
2625 *setup_frm++ = *setup_frm++ = *eaddrs++;
2626 *setup_frm++ = *setup_frm++ = *eaddrs++;
2627 *setup_frm++ = *setup_frm++ = *eaddrs++;
2629 /* Fill the unused entries with the broadcast address. */
2630 memset(setup_frm, 0xff, (15-i)*12);
2631 setup_frm = &tp->setup_frame[15*6];
2633 /* Fill the final entry with our physical address. */
2634 eaddrs = (u16 *)dev->dev_addr;
2635 *setup_frm++ = *setup_frm++ = eaddrs[0];
2636 *setup_frm++ = *setup_frm++ = eaddrs[1];
2637 *setup_frm++ = *setup_frm++ = eaddrs[2];
2638 /* Now add this frame to the Tx list. */
2639 spin_lock_irqsave(&tp->tx_lock, cpuflags);
2640 if (tp->cur_tx - tp->dirty_tx > TX_RING_SIZE - 2) {
2641 /* Same setup recently queued, we need not add it. */
2642 } else {
2643 unsigned long flags;
2644 unsigned int entry;
2646 save_flags(flags); cli();
2647 entry = tp->cur_tx++ % TX_RING_SIZE;
2649 if (entry != 0) {
2650 /* Avoid a chip errata by prefixing a dummy entry. */
2651 tp->tx_skbuff[entry] = 0;
2652 tp->tx_ring[entry].length =
2653 (entry == TX_RING_SIZE-1) ? cpu_to_le32(DESC_RING_WRAP) : 0;
2654 tp->tx_ring[entry].buffer1 = 0;
2655 tp->tx_ring[entry].status = cpu_to_le32(DescOwned);
2656 entry = tp->cur_tx++ % TX_RING_SIZE;
2659 tp->tx_skbuff[entry] = 0;
2660 /* Put the setup frame on the Tx list. */
2661 if (entry == TX_RING_SIZE-1)
2662 tx_flags |= DESC_RING_WRAP; /* Wrap ring. */
2663 tp->tx_ring[entry].length = cpu_to_le32(tx_flags);
2664 tp->tx_ring[entry].buffer1 = virt_to_le32desc(tp->setup_frame);
2665 tp->tx_ring[entry].status = cpu_to_le32(DescOwned);
2666 if (tp->cur_tx - tp->dirty_tx >= TX_RING_SIZE - 2) {
2667 netif_stop_queue(dev);
2668 tp->tx_full = 1;
2670 spin_unlock_irqrestore(&tp->tx_lock, cpuflags);
2671 /* Trigger an immediate transmit demand. */
2672 outl(0, ioaddr + CSR1);
2675 outl(csr6 | 0x0000, ioaddr + CSR6);
2679 static int __devinit tulip_init_one (struct pci_dev *pdev,
2680 const struct pci_device_id *ent)
2682 static int did_version = 0; /* Already printed version info. */
2683 struct tulip_private *tp;
2684 /* See note below on the multiport cards. */
2685 static unsigned char last_phys_addr[6] = {0x00, 'L', 'i', 'n', 'u', 'x'};
2686 static int last_irq = 0;
2687 static int multiport_cnt = 0; /* For four-port boards w/one EEPROM */
2688 u8 chip_rev;
2689 int i, irq;
2690 unsigned short sum;
2691 u8 ee_data[EEPROM_SIZE];
2692 struct net_device *dev;
2693 long ioaddr;
2694 static int board_idx = -1;
2695 int chip_idx = ent->driver_data;
2697 board_idx++;
2699 if (tulip_debug > 0 && did_version++ == 0)
2700 printk(KERN_INFO "%s", version);
2702 if( pdev->subsystem_vendor == 0x1376 ){
2703 printk(KERN_ERR "tulip: skipping LMC card.\n");
2704 return -ENODEV;
2707 ioaddr = pci_resource_start (pdev, 0);
2708 irq = pdev->irq;
2710 /* Make certain the data structures are quadword aligned. */
2711 dev = init_etherdev (NULL, sizeof (*tp));
2712 if (!dev)
2713 return -ENOMEM;
2715 /* We do a request_region() only to register /proc/ioports info. */
2716 /* Note that proper size is tulip_tbl[chip_idx].chip_name, but... */
2717 if (!request_region (ioaddr, tulip_tbl[chip_idx].io_size, dev->name))
2718 goto err_out_free_netdev;
2720 pci_enable_device (pdev);
2722 tp = dev->priv;
2723 memset(tp, 0, sizeof(*tp));
2725 pci_read_config_byte (pdev, PCI_REVISION_ID, &chip_rev);
2727 /* Bring the 21041/21143 out of sleep mode.
2728 Caution: Snooze mode does not work with some boards! */
2729 if (tulip_tbl[chip_idx].flags & HAS_PWRDWN)
2730 pci_write_config_dword(pdev, 0x40, 0x00000000);
2732 printk(KERN_INFO "%s: %s rev %d at %#3lx,",
2733 dev->name, tulip_tbl[chip_idx].chip_name, chip_rev, ioaddr);
2735 /* Stop the chip's Tx and Rx processes. */
2736 outl(inl(ioaddr + CSR6) & ~0x2002, ioaddr + CSR6);
2737 /* Clear the missed-packet counter. */
2738 (volatile int)inl(ioaddr + CSR8);
2740 if (chip_idx == DC21041 && inl(ioaddr + CSR9) & 0x8000) {
2741 printk(" 21040 compatible mode,");
2742 chip_idx = DC21040;
2745 /* The station address ROM is read byte serially. The register must
2746 be polled, waiting for the value to be read bit serially from the
2747 EEPROM.
2749 sum = 0;
2750 if (chip_idx == DC21040) {
2751 outl(0, ioaddr + CSR9); /* Reset the pointer with a dummy write. */
2752 for (i = 0; i < 6; i++) {
2753 int value, boguscnt = 100000;
2755 value = inl(ioaddr + CSR9);
2756 while (value < 0 && --boguscnt > 0);
2757 dev->dev_addr[i] = value;
2758 sum += value & 0xff;
2760 } else if (chip_idx == LC82C168) {
2761 for (i = 0; i < 3; i++) {
2762 int value, boguscnt = 100000;
2763 outl(0x600 | i, ioaddr + 0x98);
2765 value = inl(ioaddr + CSR9);
2766 while (value < 0 && --boguscnt > 0);
2767 put_unaligned(le16_to_cpu(value), ((u16*)dev->dev_addr) + i);
2768 sum += value & 0xffff;
2770 } else if (chip_idx == COMET) {
2771 /* No need to read the EEPROM. */
2772 put_unaligned(inl(ioaddr + 0xA4), (u32 *)dev->dev_addr);
2773 put_unaligned(inl(ioaddr + 0xA8), (u16 *)(dev->dev_addr + 4));
2774 for (i = 0; i < 6; i ++)
2775 sum += dev->dev_addr[i];
2776 } else {
2777 /* A serial EEPROM interface, we read now and sort it out later. */
2778 int sa_offset = 0;
2779 int ee_addr_size = read_eeprom(ioaddr, 0xff, 8) & 0x40000 ? 8 : 6;
2781 for (i = 0; i < sizeof(ee_data)/2; i++)
2782 ((u16 *)ee_data)[i] =
2783 le16_to_cpu(read_eeprom(ioaddr, i, ee_addr_size));
2785 /* DEC now has a specification (see Notes) but early board makers
2786 just put the address in the first EEPROM locations. */
2787 /* This does memcmp(eedata, eedata+16, 8) */
2788 for (i = 0; i < 8; i ++)
2789 if (ee_data[i] != ee_data[16+i])
2790 sa_offset = 20;
2791 if (ee_data[0] == 0xff && ee_data[1] == 0xff && ee_data[2] == 0) {
2792 sa_offset = 2; /* Grrr, damn Matrox boards. */
2793 multiport_cnt = 4;
2795 for (i = 0; i < 6; i ++) {
2796 dev->dev_addr[i] = ee_data[i + sa_offset];
2797 sum += ee_data[i + sa_offset];
2800 /* Lite-On boards have the address byte-swapped. */
2801 if ((dev->dev_addr[0] == 0xA0 || dev->dev_addr[0] == 0xC0)
2802 && dev->dev_addr[1] == 0x00)
2803 for (i = 0; i < 6; i+=2) {
2804 char tmp = dev->dev_addr[i];
2805 dev->dev_addr[i] = dev->dev_addr[i+1];
2806 dev->dev_addr[i+1] = tmp;
2808 /* On the Zynx 315 Etherarray and other multiport boards only the
2809 first Tulip has an EEPROM.
2810 The addresses of the subsequent ports are derived from the first.
2811 Many PCI BIOSes also incorrectly report the IRQ line, so we correct
2812 that here as well. */
2813 if (sum == 0 || sum == 6*0xff) {
2814 printk(" EEPROM not present,");
2815 for (i = 0; i < 5; i++)
2816 dev->dev_addr[i] = last_phys_addr[i];
2817 dev->dev_addr[i] = last_phys_addr[i] + 1;
2818 #if defined(__i386__) /* Patch up x86 BIOS bug. */
2819 if (last_irq)
2820 irq = last_irq;
2821 #endif
2824 for (i = 0; i < 6; i++)
2825 printk("%c%2.2X", i ? ':' : ' ', last_phys_addr[i] = dev->dev_addr[i]);
2826 printk(", IRQ %d.\n", irq);
2827 last_irq = irq;
2829 pdev->driver_data = dev;
2830 dev->base_addr = ioaddr;
2831 dev->irq = irq;
2833 tp->chip_id = chip_idx;
2834 tp->revision = chip_rev;
2835 tp->flags = tulip_tbl[chip_idx].flags;
2836 tp->csr0 = csr0;
2837 tp->pdev = pdev;
2839 /* BugFixes: The 21143-TD hangs with PCI Write-and-Invalidate cycles.
2840 And the ASIX must have a burst limit or horrible things happen. */
2841 if (chip_idx == DC21143 && chip_rev == 65)
2842 tp->csr0 &= ~0x01000000;
2843 else if (chip_idx == AX88140)
2844 tp->csr0 |= 0x2000;
2846 #ifdef TULIP_FULL_DUPLEX
2847 tp->full_duplex = 1;
2848 tp->full_duplex_lock = 1;
2849 #endif
2850 #ifdef TULIP_DEFAULT_MEDIA
2851 tp->default_port = TULIP_DEFAULT_MEDIA;
2852 #endif
2853 #ifdef TULIP_NO_MEDIA_SWITCH
2854 tp->medialock = 1;
2855 #endif
2857 /* The lower four bits are the media type. */
2858 if (board_idx >= 0 && board_idx < MAX_UNITS) {
2859 tp->default_port = options[board_idx] & 15;
2860 if ((options[board_idx] & 0x90) || full_duplex[board_idx] > 0)
2861 tp->full_duplex = 1;
2862 if (mtu[board_idx] > 0)
2863 dev->mtu = mtu[board_idx];
2865 if (dev->mem_start)
2866 tp->default_port = dev->mem_start;
2867 if (tp->default_port) {
2868 tp->medialock = 1;
2869 if (media_cap[tp->default_port] & MediaAlwaysFD)
2870 tp->full_duplex = 1;
2872 if (tp->full_duplex)
2873 tp->full_duplex_lock = 1;
2875 if (media_cap[tp->default_port] & MediaIsMII) {
2876 u16 media2advert[] = { 0x20, 0x40, 0x03e0, 0x60, 0x80, 0x100, 0x200 };
2877 tp->to_advertise = media2advert[tp->default_port - 9];
2878 } else if (tp->flags & HAS_8023X)
2879 tp->to_advertise = 0x05e1;
2880 else
2881 tp->to_advertise = 0x01e1;
2883 /* This is logically part of probe1(), but too complex to write inline. */
2884 if (tp->flags & HAS_MEDIA_TABLE) {
2885 memcpy(tp->eeprom, ee_data, sizeof(tp->eeprom));
2886 parse_eeprom(dev);
2889 if ((tp->flags & ALWAYS_CHECK_MII) ||
2890 (tp->mtable && tp->mtable->has_mii) ||
2891 ( ! tp->mtable && (tp->flags & HAS_MII))) {
2892 int phy, phy_idx;
2893 if (tp->mtable && tp->mtable->has_mii) {
2894 for (i = 0; i < tp->mtable->leafcount; i++)
2895 if (tp->mtable->mleaf[i].media == 11) {
2896 tp->cur_index = i;
2897 tp->saved_if_port = dev->if_port;
2898 select_media(dev, 1);
2899 dev->if_port = tp->saved_if_port;
2900 break;
2903 /* Find the connected MII xcvrs.
2904 Doing this in open() would allow detecting external xcvrs later,
2905 but takes much time. */
2906 for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(tp->phys);
2907 phy++) {
2908 int mii_status = mdio_read(dev, phy, 1);
2909 if ((mii_status & 0x8301) == 0x8001 ||
2910 ((mii_status & 0x8000) == 0 && (mii_status & 0x7800) != 0)) {
2911 int mii_reg0 = mdio_read(dev, phy, 0);
2912 int mii_advert = mdio_read(dev, phy, 4);
2913 int reg4 = ((mii_status>>6) & tp->to_advertise) | 1;
2914 tp->phys[phy_idx] = phy;
2915 tp->advertising[phy_idx++] = reg4;
2916 printk(KERN_INFO "%s: MII transceiver #%d "
2917 "config %4.4x status %4.4x advertising %4.4x.\n",
2918 dev->name, phy, mii_reg0, mii_status, mii_advert);
2919 /* Fixup for DLink with miswired PHY. */
2920 if (mii_advert != reg4) {
2921 printk(KERN_DEBUG "%s: Advertising %4.4x on PHY %d,"
2922 " previously advertising %4.4x.\n",
2923 dev->name, reg4, phy, mii_advert);
2924 printk(KERN_DEBUG "%s: Advertising %4.4x (to advertise"
2925 " is %4.4x).\n",
2926 dev->name, reg4, tp->to_advertise);
2927 mdio_write(dev, phy, 4, reg4);
2929 /* Enable autonegotiation: some boards default to off. */
2930 mdio_write(dev, phy, 0, mii_reg0 |
2931 (tp->full_duplex ? 0x1100 : 0x1000) |
2932 (media_cap[tp->default_port]&MediaIs100 ? 0x2000:0));
2935 tp->mii_cnt = phy_idx;
2936 if (tp->mtable && tp->mtable->has_mii && phy_idx == 0) {
2937 printk(KERN_INFO "%s: ***WARNING***: No MII transceiver found!\n",
2938 dev->name);
2939 tp->phys[0] = 1;
2943 /* The Tulip-specific entries in the device structure. */
2944 dev->open = tulip_open;
2945 dev->hard_start_xmit = tulip_start_xmit;
2946 dev->tx_timeout = tulip_tx_timeout;
2947 dev->watchdog_timeo = TX_TIMEOUT;
2948 dev->stop = tulip_close;
2949 dev->get_stats = tulip_get_stats;
2950 dev->do_ioctl = private_ioctl;
2951 dev->set_multicast_list = set_rx_mode;
2953 if ((tp->flags & HAS_NWAY143) || tp->chip_id == DC21041)
2954 tp->link_change = t21142_lnk_change;
2955 else if (tp->flags & HAS_PNICNWAY)
2956 tp->link_change = pnic_lnk_change;
2958 /* Reset the xcvr interface and turn on heartbeat. */
2959 switch (chip_idx) {
2960 case DC21041:
2961 tp->to_advertise = 0x0061;
2962 outl(0x00000000, ioaddr + CSR13);
2963 outl(0xFFFFFFFF, ioaddr + CSR14);
2964 outl(0x00000008, ioaddr + CSR15); /* Listen on AUI also. */
2965 outl(inl(ioaddr + CSR6) | 0x0200, ioaddr + CSR6);
2966 outl(0x0000EF05, ioaddr + CSR13);
2967 break;
2968 case DC21040:
2969 outl(0x00000000, ioaddr + CSR13);
2970 outl(0x00000004, ioaddr + CSR13);
2971 break;
2972 case DC21140: default:
2973 if (tp->mtable)
2974 outl(tp->mtable->csr12dir | 0x100, ioaddr + CSR12);
2975 break;
2976 case DC21142:
2977 case PNIC2:
2978 if (tp->mii_cnt || media_cap[dev->if_port] & MediaIsMII) {
2979 outl(0x82020000, ioaddr + CSR6);
2980 outl(0x0000, ioaddr + CSR13);
2981 outl(0x0000, ioaddr + CSR14);
2982 outl(0x820E0000, ioaddr + CSR6);
2983 } else
2984 t21142_start_nway(dev);
2985 break;
2986 case LC82C168:
2987 if ( ! tp->mii_cnt) {
2988 tp->nway = 1;
2989 tp->nwayset = 0;
2990 outl(0x00420000, ioaddr + CSR6);
2991 outl(0x30, ioaddr + CSR12);
2992 outl(0x0001F078, ioaddr + 0xB8);
2993 outl(0x0201F078, ioaddr + 0xB8); /* Turn on autonegotiation. */
2995 break;
2996 case MX98713: case COMPEX9881:
2997 outl(0x00000000, ioaddr + CSR6);
2998 outl(0x000711C0, ioaddr + CSR14); /* Turn on NWay. */
2999 outl(0x00000001, ioaddr + CSR13);
3000 break;
3001 case MX98715: case MX98725:
3002 outl(0x01a80000, ioaddr + CSR6);
3003 outl(0xFFFFFFFF, ioaddr + CSR14);
3004 outl(0x00001000, ioaddr + CSR12);
3005 break;
3006 case COMET:
3007 /* No initialization necessary. */
3008 break;
3011 if (tulip_tbl[chip_idx].flags & HAS_PWRDWN)
3012 pci_write_config_dword(pdev, 0x40, 0x40000000);
3014 return 0;
3016 err_out_free_netdev:
3017 unregister_netdev (dev);
3018 kfree (dev);
3019 return -ENODEV;
3023 static void tulip_suspend (struct pci_dev *pdev)
3025 struct net_device *dev = pdev->driver_data;
3027 if (dev) {
3028 long ioaddr = dev->base_addr;
3029 struct tulip_private *tp = (struct tulip_private *)dev->priv;
3030 int csr6 = inl(ioaddr + CSR6);
3031 /* Disable interrupts, stop the chip, gather stats. */
3032 if (csr6 != 0xffffffff) {
3033 outl(0x00000000, ioaddr + CSR7);
3034 outl(csr6 & ~0x2002, ioaddr + CSR6);
3035 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
3037 tulip_close(dev);
3038 /* Put the 21143 into sleep mode. */
3039 pci_write_config_dword(pdev, 0x40,0x80000000);
3044 static void tulip_resume (struct pci_dev *pdev)
3046 struct net_device *dev = pdev->driver_data;
3048 if (dev) {
3049 pci_write_config_dword(pdev, 0x40, 0x0000);
3050 tulip_open(dev);
3055 static void __devexit tulip_remove_one (struct pci_dev *pdev)
3057 struct net_device *dev = pdev->driver_data;
3059 if (dev) {
3060 struct tulip_private *tp = (struct tulip_private *)dev->priv;
3061 unregister_netdev(dev);
3062 release_region(dev->base_addr,
3063 tulip_tbl[tp->chip_id].io_size);
3064 kfree(dev);
3069 static struct pci_driver tulip_driver = {
3070 name: TULIP_MODULE_NAME,
3071 id_table: tulip_pci_tbl,
3072 probe: tulip_init_one,
3073 remove: tulip_remove_one,
3074 suspend: tulip_suspend,
3075 resume: tulip_resume,
3079 static int __init tulip_init (void)
3081 return pci_register_driver (&tulip_driver) > 0 ? 0 : -ENODEV;
3085 static void __exit tulip_cleanup (void)
3087 pci_unregister_driver (&tulip_driver);
3091 module_init(tulip_init);
3092 module_exit(tulip_cleanup);