ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / tulip / tulip_core.c
1 /* tulip_core.c: A DEC 21x4x-family ethernet driver for Linux. */
2
3 /*
4         Maintained by Jeff Garzik <jgarzik@pobox.com>
5         Copyright 2000,2001  The Linux Kernel Team
6         Written/copyright 1994-2001 by Donald Becker.
7
8         This software may be used and distributed according to the terms
9         of the GNU General Public License, incorporated herein by reference.
10
11         Please refer to Documentation/DocBook/tulip-user.{pdf,ps,html}
12         for more information on this driver, or visit the project
13         Web page at http://sourceforge.net/projects/tulip/
14
15 */
16
17 #include <linux/config.h>
18
19 #define DRV_NAME        "tulip"
20 #ifdef CONFIG_TULIP_NAPI
21 #define DRV_VERSION    "1.1.13-NAPI" /* Keep at least for test */
22 #else
23 #define DRV_VERSION     "1.1.13"
24 #endif
25 #define DRV_RELDATE     "May 11, 2002"
26
27
28 #include <linux/module.h>
29 #include "tulip.h"
30 #include <linux/pci.h>
31 #include <linux/init.h>
32 #include <linux/etherdevice.h>
33 #include <linux/delay.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/crc32.h>
37 #include <asm/unaligned.h>
38 #include <asm/uaccess.h>
39
40 #ifdef __sparc__
41 #include <asm/pbm.h>
42 #endif
43
44 static char version[] __devinitdata =
45         "Linux Tulip driver version " DRV_VERSION " (" DRV_RELDATE ")\n";
46
47
48 /* A few user-configurable values. */
49
50 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
51 static unsigned int max_interrupt_work = 25;
52
53 #define MAX_UNITS 8
54 /* Used to pass the full-duplex flag, etc. */
55 static int full_duplex[MAX_UNITS];
56 static int options[MAX_UNITS];
57 static int mtu[MAX_UNITS];                      /* Jumbo MTU for interfaces. */
58
59 /*  The possible media types that can be set in options[] are: */
60 const char * const medianame[32] = {
61         "10baseT", "10base2", "AUI", "100baseTx",
62         "10baseT-FDX", "100baseTx-FDX", "100baseT4", "100baseFx",
63         "100baseFx-FDX", "MII 10baseT", "MII 10baseT-FDX", "MII",
64         "10baseT(forced)", "MII 100baseTx", "MII 100baseTx-FDX", "MII 100baseT4",
65         "MII 100baseFx-HDX", "MII 100baseFx-FDX", "Home-PNA 1Mbps", "Invalid-19",
66         "","","","", "","","","",  "","","","Transceiver reset",
67 };
68
69 /* Set the copy breakpoint for the copy-only-tiny-buffer Rx structure. */
70 #if defined(__alpha__) || defined(__arm__) || defined(__hppa__) \
71         || defined(__sparc_) || defined(__ia64__) \
72         || defined(__sh__) || defined(__mips__)
73 static int rx_copybreak = 1518;
74 #else
75 static int rx_copybreak = 100;
76 #endif
77
78 /*
79   Set the bus performance register.
80         Typical: Set 16 longword cache alignment, no burst limit.
81         Cache alignment bits 15:14           Burst length 13:8
82                 0000    No alignment  0x00000000 unlimited              0800 8 longwords
83                 4000    8  longwords            0100 1 longword         1000 16 longwords
84                 8000    16 longwords            0200 2 longwords        2000 32 longwords
85                 C000    32  longwords           0400 4 longwords
86         Warning: many older 486 systems are broken and require setting 0x00A04800
87            8 longword cache alignment, 8 longword burst.
88         ToDo: Non-Intel setting could be better.
89 */
90
91 #if defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
92 static int csr0 = 0x01A00000 | 0xE000;
93 #elif defined(__i386__) || defined(__powerpc__)
94 static int csr0 = 0x01A00000 | 0x8000;
95 #elif defined(__sparc__) || defined(__hppa__)
96 /* The UltraSparc PCI controllers will disconnect at every 64-byte
97  * crossing anyways so it makes no sense to tell Tulip to burst
98  * any more than that.
99  */
100 static int csr0 = 0x01A00000 | 0x9000;
101 #elif defined(__arm__) || defined(__sh__)
102 static int csr0 = 0x01A00000 | 0x4800;
103 #elif defined(__mips__)
104 static int csr0 = 0x00200000 | 0x4000;
105 #else
106 #warning Processor architecture undefined!
107 static int csr0 = 0x00A00000 | 0x4800;
108 #endif
109
110 /* Operational parameters that usually are not changed. */
111 /* Time in jiffies before concluding the transmitter is hung. */
112 #define TX_TIMEOUT  (4*HZ)
113
114
115 MODULE_AUTHOR("The Linux Kernel Team");
116 MODULE_DESCRIPTION("Digital 21*4* Tulip ethernet driver");
117 MODULE_LICENSE("GPL");
118 MODULE_PARM(tulip_debug, "i");
119 MODULE_PARM(max_interrupt_work, "i");
120 MODULE_PARM(rx_copybreak, "i");
121 MODULE_PARM(csr0, "i");
122 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
123 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
124
125 #define PFX DRV_NAME ": "
126
127 #ifdef TULIP_DEBUG
128 int tulip_debug = TULIP_DEBUG;
129 #else
130 int tulip_debug = 1;
131 #endif
132
133
134
135 /*
136  * This table use during operation for capabilities and media timer.
137  *
138  * It is indexed via the values in 'enum chips'
139  */
140
141 struct tulip_chip_table tulip_tbl[] = {
142   { }, /* placeholder for array, slot unused currently */
143   { }, /* placeholder for array, slot unused currently */
144
145   /* DC21140 */
146   { "Digital DS21140 Tulip", 128, 0x0001ebef,
147         HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM | HAS_PCI_MWI, tulip_timer },
148
149   /* DC21142, DC21143 */
150   { "Digital DS21143 Tulip", 128, 0x0801fbff,
151         HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII | HAS_ACPI | HAS_NWAY
152         | HAS_INTR_MITIGATION | HAS_PCI_MWI, t21142_timer },
153
154   /* LC82C168 */
155   { "Lite-On 82c168 PNIC", 256, 0x0001fbef,
156         HAS_MII | HAS_PNICNWAY, pnic_timer },
157
158   /* MX98713 */
159   { "Macronix 98713 PMAC", 128, 0x0001ebef,
160         HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, mxic_timer },
161
162   /* MX98715 */
163   { "Macronix 98715 PMAC", 256, 0x0001ebef,
164         HAS_MEDIA_TABLE, mxic_timer },
165
166   /* MX98725 */
167   { "Macronix 98725 PMAC", 256, 0x0001ebef,
168         HAS_MEDIA_TABLE, mxic_timer },
169
170   /* AX88140 */
171   { "ASIX AX88140", 128, 0x0001fbff,
172         HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM | MC_HASH_ONLY
173         | IS_ASIX, tulip_timer },
174
175   /* PNIC2 */
176   { "Lite-On PNIC-II", 256, 0x0801fbff,
177         HAS_MII | HAS_NWAY | HAS_8023X | HAS_PCI_MWI, pnic2_timer },
178
179   /* COMET */
180   { "ADMtek Comet", 256, 0x0001abef,
181         HAS_MII | MC_HASH_ONLY | COMET_MAC_ADDR, comet_timer },
182
183   /* COMPEX9881 */
184   { "Compex 9881 PMAC", 128, 0x0001ebef,
185         HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM, mxic_timer },
186
187   /* I21145 */
188   { "Intel DS21145 Tulip", 128, 0x0801fbff,
189         HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII | HAS_ACPI
190         | HAS_NWAY | HAS_PCI_MWI, t21142_timer },
191
192   /* DM910X */
193   { "Davicom DM9102/DM9102A", 128, 0x0001ebef,
194         HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM | HAS_ACPI,
195         tulip_timer },
196
197   /* RS7112 */
198   { "Conexant LANfinity", 256, 0x0001ebef,
199         HAS_MII | HAS_ACPI, tulip_timer },
200 };
201
202
203 static struct pci_device_id tulip_pci_tbl[] = {
204         { 0x1011, 0x0009, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21140 },
205         { 0x1011, 0x0019, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DC21143 },
206         { 0x11AD, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, LC82C168 },
207         { 0x10d9, 0x0512, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98713 },
208         { 0x10d9, 0x0531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98715 },
209 /*      { 0x10d9, 0x0531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98725 },*/
210         { 0x125B, 0x1400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AX88140 },
211         { 0x11AD, 0xc115, PCI_ANY_ID, PCI_ANY_ID, 0, 0, PNIC2 },
212         { 0x1317, 0x0981, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
213         { 0x1317, 0x0985, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
214         { 0x1317, 0x1985, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
215         { 0x1317, 0x9511, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
216         { 0x13D1, 0xAB02, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
217         { 0x13D1, 0xAB03, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
218         { 0x13D1, 0xAB08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
219         { 0x104A, 0x0981, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
220         { 0x104A, 0x2774, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
221         { 0x1259, 0xa120, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
222         { 0x11F6, 0x9881, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMPEX9881 },
223         { 0x8086, 0x0039, PCI_ANY_ID, PCI_ANY_ID, 0, 0, I21145 },
224         { 0x1282, 0x9100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DM910X },
225         { 0x1282, 0x9102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DM910X },
226         { 0x1113, 0x1216, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
227         { 0x1113, 0x1217, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MX98715 },
228         { 0x1113, 0x9511, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
229         { 0x14f1, 0x1803, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CONEXANT },
230         { 0x1186, 0x1561, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
231         { 0x1626, 0x8410, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
232         { 0x1737, 0xAB09, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
233         { 0x1737, 0xAB08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
234         { 0x17B3, 0xAB08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET },
235         { 0x10b9, 0x5261, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DM910X },       /* ALi 1563 integrated ethernet */
236         { 0x10b7, 0x9300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, COMET }, /* 3Com 3CSOHO100B-TX */
237         { } /* terminate list */
238 };
239 MODULE_DEVICE_TABLE(pci, tulip_pci_tbl);
240
241
242 /* A full-duplex map for media types. */
243 const char tulip_media_cap[32] =
244 {0,0,0,16,  3,19,16,24,  27,4,7,5, 0,20,23,20,  28,31,0,0, };
245
246 static void tulip_tx_timeout(struct net_device *dev);
247 static void tulip_init_ring(struct net_device *dev);
248 static int tulip_start_xmit(struct sk_buff *skb, struct net_device *dev);
249 static int tulip_open(struct net_device *dev);
250 static int tulip_close(struct net_device *dev);
251 static void tulip_up(struct net_device *dev);
252 static void tulip_down(struct net_device *dev);
253 static struct net_device_stats *tulip_get_stats(struct net_device *dev);
254 static int private_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
255 static void set_rx_mode(struct net_device *dev);
256 #ifdef CONFIG_NET_POLL_CONTROLLER
257 static void poll_tulip(struct net_device *dev);
258 #endif
259
260 static void tulip_set_power_state (struct tulip_private *tp,
261                                    int sleep, int snooze)
262 {
263         if (tp->flags & HAS_ACPI) {
264                 u32 tmp, newtmp;
265                 pci_read_config_dword (tp->pdev, CFDD, &tmp);
266                 newtmp = tmp & ~(CFDD_Sleep | CFDD_Snooze);
267                 if (sleep)
268                         newtmp |= CFDD_Sleep;
269                 else if (snooze)
270                         newtmp |= CFDD_Snooze;
271                 if (tmp != newtmp)
272                         pci_write_config_dword (tp->pdev, CFDD, newtmp);
273         }
274
275 }
276
277
278 static void tulip_up(struct net_device *dev)
279 {
280         struct tulip_private *tp = netdev_priv(dev);
281         long ioaddr = dev->base_addr;
282         int next_tick = 3*HZ;
283         int i;
284
285         /* Wake the chip from sleep/snooze mode. */
286         tulip_set_power_state (tp, 0, 0);
287
288         /* On some chip revs we must set the MII/SYM port before the reset!? */
289         if (tp->mii_cnt  ||  (tp->mtable  &&  tp->mtable->has_mii))
290                 outl(0x00040000, ioaddr + CSR6);
291
292         /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
293         outl(0x00000001, ioaddr + CSR0);
294         udelay(100);
295
296         /* Deassert reset.
297            Wait the specified 50 PCI cycles after a reset by initializing
298            Tx and Rx queues and the address filter list. */
299         outl(tp->csr0, ioaddr + CSR0);
300         udelay(100);
301
302         if (tulip_debug > 1)
303                 printk(KERN_DEBUG "%s: tulip_up(), irq==%d.\n", dev->name, dev->irq);
304
305         outl(tp->rx_ring_dma, ioaddr + CSR3);
306         outl(tp->tx_ring_dma, ioaddr + CSR4);
307         tp->cur_rx = tp->cur_tx = 0;
308         tp->dirty_rx = tp->dirty_tx = 0;
309
310         if (tp->flags & MC_HASH_ONLY) {
311                 u32 addr_low = le32_to_cpu(get_unaligned((u32 *)dev->dev_addr));
312                 u32 addr_high = le16_to_cpu(get_unaligned((u16 *)(dev->dev_addr+4)));
313                 if (tp->chip_id == AX88140) {
314                         outl(0, ioaddr + CSR13);
315                         outl(addr_low,  ioaddr + CSR14);
316                         outl(1, ioaddr + CSR13);
317                         outl(addr_high, ioaddr + CSR14);
318                 } else if (tp->flags & COMET_MAC_ADDR) {
319                         outl(addr_low,  ioaddr + 0xA4);
320                         outl(addr_high, ioaddr + 0xA8);
321                         outl(0, ioaddr + 0xAC);
322                         outl(0, ioaddr + 0xB0);
323                 }
324         } else {
325                 /* This is set_rx_mode(), but without starting the transmitter. */
326                 u16 *eaddrs = (u16 *)dev->dev_addr;
327                 u16 *setup_frm = &tp->setup_frame[15*6];
328                 dma_addr_t mapping;
329
330                 /* 21140 bug: you must add the broadcast address. */
331                 memset(tp->setup_frame, 0xff, sizeof(tp->setup_frame));
332                 /* Fill the final entry of the table with our physical address. */
333                 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
334                 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
335                 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
336
337                 mapping = pci_map_single(tp->pdev, tp->setup_frame,
338                                          sizeof(tp->setup_frame),
339                                          PCI_DMA_TODEVICE);
340                 tp->tx_buffers[tp->cur_tx].skb = NULL;
341                 tp->tx_buffers[tp->cur_tx].mapping = mapping;
342
343                 /* Put the setup frame on the Tx list. */
344                 tp->tx_ring[tp->cur_tx].length = cpu_to_le32(0x08000000 | 192);
345                 tp->tx_ring[tp->cur_tx].buffer1 = cpu_to_le32(mapping);
346                 tp->tx_ring[tp->cur_tx].status = cpu_to_le32(DescOwned);
347
348                 tp->cur_tx++;
349         }
350
351         tp->saved_if_port = dev->if_port;
352         if (dev->if_port == 0)
353                 dev->if_port = tp->default_port;
354
355         /* Allow selecting a default media. */
356         i = 0;
357         if (tp->mtable == NULL)
358                 goto media_picked;
359         if (dev->if_port) {
360                 int looking_for = tulip_media_cap[dev->if_port] & MediaIsMII ? 11 :
361                         (dev->if_port == 12 ? 0 : dev->if_port);
362                 for (i = 0; i < tp->mtable->leafcount; i++)
363                         if (tp->mtable->mleaf[i].media == looking_for) {
364                                 printk(KERN_INFO "%s: Using user-specified media %s.\n",
365                                            dev->name, medianame[dev->if_port]);
366                                 goto media_picked;
367                         }
368         }
369         if ((tp->mtable->defaultmedia & 0x0800) == 0) {
370                 int looking_for = tp->mtable->defaultmedia & MEDIA_MASK;
371                 for (i = 0; i < tp->mtable->leafcount; i++)
372                         if (tp->mtable->mleaf[i].media == looking_for) {
373                                 printk(KERN_INFO "%s: Using EEPROM-set media %s.\n",
374                                            dev->name, medianame[looking_for]);
375                                 goto media_picked;
376                         }
377         }
378         /* Start sensing first non-full-duplex media. */
379         for (i = tp->mtable->leafcount - 1;
380                  (tulip_media_cap[tp->mtable->mleaf[i].media] & MediaAlwaysFD) && i > 0; i--)
381                 ;
382 media_picked:
383
384         tp->csr6 = 0;
385         tp->cur_index = i;
386         tp->nwayset = 0;
387
388         if (dev->if_port) {
389                 if (tp->chip_id == DC21143  &&
390                     (tulip_media_cap[dev->if_port] & MediaIsMII)) {
391                         /* We must reset the media CSRs when we force-select MII mode. */
392                         outl(0x0000, ioaddr + CSR13);
393                         outl(0x0000, ioaddr + CSR14);
394                         outl(0x0008, ioaddr + CSR15);
395                 }
396                 tulip_select_media(dev, 1);
397         } else if (tp->chip_id == DC21142) {
398                 if (tp->mii_cnt) {
399                         tulip_select_media(dev, 1);
400                         if (tulip_debug > 1)
401                                 printk(KERN_INFO "%s: Using MII transceiver %d, status "
402                                            "%4.4x.\n",
403                                            dev->name, tp->phys[0], tulip_mdio_read(dev, tp->phys[0], 1));
404                         outl(csr6_mask_defstate, ioaddr + CSR6);
405                         tp->csr6 = csr6_mask_hdcap;
406                         dev->if_port = 11;
407                         outl(0x0000, ioaddr + CSR13);
408                         outl(0x0000, ioaddr + CSR14);
409                 } else
410                         t21142_start_nway(dev);
411         } else if (tp->chip_id == PNIC2) {
412                 /* for initial startup advertise 10/100 Full and Half */
413                 tp->sym_advertise = 0x01E0;
414                 /* enable autonegotiate end interrupt */
415                 outl(inl(ioaddr+CSR5)| 0x00008010, ioaddr + CSR5);
416                 outl(inl(ioaddr+CSR7)| 0x00008010, ioaddr + CSR7);
417                 pnic2_start_nway(dev);
418         } else if (tp->chip_id == LC82C168  &&  ! tp->medialock) {
419                 if (tp->mii_cnt) {
420                         dev->if_port = 11;
421                         tp->csr6 = 0x814C0000 | (tp->full_duplex ? 0x0200 : 0);
422                         outl(0x0001, ioaddr + CSR15);
423                 } else if (inl(ioaddr + CSR5) & TPLnkPass)
424                         pnic_do_nway(dev);
425                 else {
426                         /* Start with 10mbps to do autonegotiation. */
427                         outl(0x32, ioaddr + CSR12);
428                         tp->csr6 = 0x00420000;
429                         outl(0x0001B078, ioaddr + 0xB8);
430                         outl(0x0201B078, ioaddr + 0xB8);
431                         next_tick = 1*HZ;
432                 }
433         } else if ((tp->chip_id == MX98713 || tp->chip_id == COMPEX9881)
434                            && ! tp->medialock) {
435                 dev->if_port = 0;
436                 tp->csr6 = 0x01880000 | (tp->full_duplex ? 0x0200 : 0);
437                 outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
438         } else if (tp->chip_id == MX98715 || tp->chip_id == MX98725) {
439                 /* Provided by BOLO, Macronix - 12/10/1998. */
440                 dev->if_port = 0;
441                 tp->csr6 = 0x01a80200;
442                 outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
443                 outl(0x11000 | inw(ioaddr + 0xa0), ioaddr + 0xa0);
444         } else if (tp->chip_id == COMET || tp->chip_id == CONEXANT) {
445                 /* Enable automatic Tx underrun recovery. */
446                 outl(inl(ioaddr + 0x88) | 1, ioaddr + 0x88);
447                 dev->if_port = tp->mii_cnt ? 11 : 0;
448                 tp->csr6 = 0x00040000;
449         } else if (tp->chip_id == AX88140) {
450                 tp->csr6 = tp->mii_cnt ? 0x00040100 : 0x00000100;
451         } else
452                 tulip_select_media(dev, 1);
453
454         /* Start the chip's Tx to process setup frame. */
455         tulip_stop_rxtx(tp);
456         barrier();
457         udelay(5);
458         outl(tp->csr6 | TxOn, ioaddr + CSR6);
459
460         /* Enable interrupts by setting the interrupt mask. */
461         outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR5);
462         outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
463         tulip_start_rxtx(tp);
464         outl(0, ioaddr + CSR2);         /* Rx poll demand */
465
466         if (tulip_debug > 2) {
467                 printk(KERN_DEBUG "%s: Done tulip_up(), CSR0 %8.8x, CSR5 %8.8x CSR6 %8.8x.\n",
468                            dev->name, inl(ioaddr + CSR0), inl(ioaddr + CSR5),
469                            inl(ioaddr + CSR6));
470         }
471
472         /* Set the timer to switch to check for link beat and perhaps switch
473            to an alternate media type. */
474         tp->timer.expires = RUN_AT(next_tick);
475         add_timer(&tp->timer);
476 #ifdef CONFIG_TULIP_NAPI
477         init_timer(&tp->oom_timer);
478         tp->oom_timer.data = (unsigned long)dev;
479         tp->oom_timer.function = oom_timer;
480 #endif
481 }
482
483 static int
484 tulip_open(struct net_device *dev)
485 {
486         int retval;
487
488         if ((retval = request_irq(dev->irq, &tulip_interrupt, SA_SHIRQ, dev->name, dev)))
489                 return retval;
490
491         tulip_init_ring (dev);
492
493         tulip_up (dev);
494
495         netif_start_queue (dev);
496
497         return 0;
498 }
499
500
501 static void tulip_tx_timeout(struct net_device *dev)
502 {
503         struct tulip_private *tp = netdev_priv(dev);
504         long ioaddr = dev->base_addr;
505         unsigned long flags;
506
507         spin_lock_irqsave (&tp->lock, flags);
508
509         if (tulip_media_cap[dev->if_port] & MediaIsMII) {
510                 /* Do nothing -- the media monitor should handle this. */
511                 if (tulip_debug > 1)
512                         printk(KERN_WARNING "%s: Transmit timeout using MII device.\n",
513                                    dev->name);
514         } else if (tp->chip_id == DC21140 || tp->chip_id == DC21142
515                            || tp->chip_id == MX98713 || tp->chip_id == COMPEX9881
516                            || tp->chip_id == DM910X) {
517                 printk(KERN_WARNING "%s: 21140 transmit timed out, status %8.8x, "
518                            "SIA %8.8x %8.8x %8.8x %8.8x, resetting...\n",
519                            dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12),
520                            inl(ioaddr + CSR13), inl(ioaddr + CSR14), inl(ioaddr + CSR15));
521                 if ( ! tp->medialock  &&  tp->mtable) {
522                         do
523                                 --tp->cur_index;
524                         while (tp->cur_index >= 0
525                                    && (tulip_media_cap[tp->mtable->mleaf[tp->cur_index].media]
526                                            & MediaIsFD));
527                         if (--tp->cur_index < 0) {
528                                 /* We start again, but should instead look for default. */
529                                 tp->cur_index = tp->mtable->leafcount - 1;
530                         }
531                         tulip_select_media(dev, 0);
532                         printk(KERN_WARNING "%s: transmit timed out, switching to %s "
533                                    "media.\n", dev->name, medianame[dev->if_port]);
534                 }
535         } else if (tp->chip_id == PNIC2) {
536                 printk(KERN_WARNING "%s: PNIC2 transmit timed out, status %8.8x, "
537                        "CSR6/7 %8.8x / %8.8x CSR12 %8.8x, resetting...\n",
538                        dev->name, (int)inl(ioaddr + CSR5), (int)inl(ioaddr + CSR6),
539                        (int)inl(ioaddr + CSR7), (int)inl(ioaddr + CSR12));
540         } else {
541                 printk(KERN_WARNING "%s: Transmit timed out, status %8.8x, CSR12 "
542                            "%8.8x, resetting...\n",
543                            dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12));
544                 dev->if_port = 0;
545         }
546
547 #if defined(way_too_many_messages)
548         if (tulip_debug > 3) {
549                 int i;
550                 for (i = 0; i < RX_RING_SIZE; i++) {
551                         u8 *buf = (u8 *)(tp->rx_ring[i].buffer1);
552                         int j;
553                         printk(KERN_DEBUG "%2d: %8.8x %8.8x %8.8x %8.8x  "
554                                    "%2.2x %2.2x %2.2x.\n",
555                                    i, (unsigned int)tp->rx_ring[i].status,
556                                    (unsigned int)tp->rx_ring[i].length,
557                                    (unsigned int)tp->rx_ring[i].buffer1,
558                                    (unsigned int)tp->rx_ring[i].buffer2,
559                                    buf[0], buf[1], buf[2]);
560                         for (j = 0; buf[j] != 0xee && j < 1600; j++)
561                                 if (j < 100) printk(" %2.2x", buf[j]);
562                         printk(" j=%d.\n", j);
563                 }
564                 printk(KERN_DEBUG "  Rx ring %8.8x: ", (int)tp->rx_ring);
565                 for (i = 0; i < RX_RING_SIZE; i++)
566                         printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
567                 printk("\n" KERN_DEBUG "  Tx ring %8.8x: ", (int)tp->tx_ring);
568                 for (i = 0; i < TX_RING_SIZE; i++)
569                         printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
570                 printk("\n");
571         }
572 #endif
573
574         /* Stop and restart the chip's Tx processes . */
575
576         tulip_restart_rxtx(tp);
577         /* Trigger an immediate transmit demand. */
578         outl(0, ioaddr + CSR1);
579
580         tp->stats.tx_errors++;
581
582         spin_unlock_irqrestore (&tp->lock, flags);
583         dev->trans_start = jiffies;
584         netif_wake_queue (dev);
585 }
586
587
588 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
589 static void tulip_init_ring(struct net_device *dev)
590 {
591         struct tulip_private *tp = netdev_priv(dev);
592         int i;
593
594         tp->susp_rx = 0;
595         tp->ttimer = 0;
596         tp->nir = 0;
597
598         for (i = 0; i < RX_RING_SIZE; i++) {
599                 tp->rx_ring[i].status = 0x00000000;
600                 tp->rx_ring[i].length = cpu_to_le32(PKT_BUF_SZ);
601                 tp->rx_ring[i].buffer2 = cpu_to_le32(tp->rx_ring_dma + sizeof(struct tulip_rx_desc) * (i + 1));
602                 tp->rx_buffers[i].skb = NULL;
603                 tp->rx_buffers[i].mapping = 0;
604         }
605         /* Mark the last entry as wrapping the ring. */
606         tp->rx_ring[i-1].length = cpu_to_le32(PKT_BUF_SZ | DESC_RING_WRAP);
607         tp->rx_ring[i-1].buffer2 = cpu_to_le32(tp->rx_ring_dma);
608
609         for (i = 0; i < RX_RING_SIZE; i++) {
610                 dma_addr_t mapping;
611
612                 /* Note the receive buffer must be longword aligned.
613                    dev_alloc_skb() provides 16 byte alignment.  But do *not*
614                    use skb_reserve() to align the IP header! */
615                 struct sk_buff *skb = dev_alloc_skb(PKT_BUF_SZ);
616                 tp->rx_buffers[i].skb = skb;
617                 if (skb == NULL)
618                         break;
619                 mapping = pci_map_single(tp->pdev, skb->tail,
620                                          PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
621                 tp->rx_buffers[i].mapping = mapping;
622                 skb->dev = dev;                 /* Mark as being used by this device. */
623                 tp->rx_ring[i].status = cpu_to_le32(DescOwned); /* Owned by Tulip chip */
624                 tp->rx_ring[i].buffer1 = cpu_to_le32(mapping);
625         }
626         tp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
627
628         /* The Tx buffer descriptor is filled in as needed, but we
629            do need to clear the ownership bit. */
630         for (i = 0; i < TX_RING_SIZE; i++) {
631                 tp->tx_buffers[i].skb = NULL;
632                 tp->tx_buffers[i].mapping = 0;
633                 tp->tx_ring[i].status = 0x00000000;
634                 tp->tx_ring[i].buffer2 = cpu_to_le32(tp->tx_ring_dma + sizeof(struct tulip_tx_desc) * (i + 1));
635         }
636         tp->tx_ring[i-1].buffer2 = cpu_to_le32(tp->tx_ring_dma);
637 }
638
639 static int
640 tulip_start_xmit(struct sk_buff *skb, struct net_device *dev)
641 {
642         struct tulip_private *tp = netdev_priv(dev);
643         int entry;
644         u32 flag;
645         dma_addr_t mapping;
646
647         spin_lock_irq(&tp->lock);
648
649         /* Calculate the next Tx descriptor entry. */
650         entry = tp->cur_tx % TX_RING_SIZE;
651
652         tp->tx_buffers[entry].skb = skb;
653         mapping = pci_map_single(tp->pdev, skb->data,
654                                  skb->len, PCI_DMA_TODEVICE);
655         tp->tx_buffers[entry].mapping = mapping;
656         tp->tx_ring[entry].buffer1 = cpu_to_le32(mapping);
657
658         if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE/2) {/* Typical path */
659                 flag = 0x60000000; /* No interrupt */
660         } else if (tp->cur_tx - tp->dirty_tx == TX_RING_SIZE/2) {
661                 flag = 0xe0000000; /* Tx-done intr. */
662         } else if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE - 2) {
663                 flag = 0x60000000; /* No Tx-done intr. */
664         } else {                /* Leave room for set_rx_mode() to fill entries. */
665                 flag = 0xe0000000; /* Tx-done intr. */
666                 netif_stop_queue(dev);
667         }
668         if (entry == TX_RING_SIZE-1)
669                 flag = 0xe0000000 | DESC_RING_WRAP;
670
671         tp->tx_ring[entry].length = cpu_to_le32(skb->len | flag);
672         /* if we were using Transmit Automatic Polling, we would need a
673          * wmb() here. */
674         tp->tx_ring[entry].status = cpu_to_le32(DescOwned);
675         wmb();
676
677         tp->cur_tx++;
678
679         /* Trigger an immediate transmit demand. */
680         outl(0, dev->base_addr + CSR1);
681
682         spin_unlock_irq(&tp->lock);
683
684         dev->trans_start = jiffies;
685
686         return 0;
687 }
688
689 static void tulip_clean_tx_ring(struct tulip_private *tp)
690 {
691         unsigned int dirty_tx;
692
693         for (dirty_tx = tp->dirty_tx ; tp->cur_tx - dirty_tx > 0;
694                 dirty_tx++) {
695                 int entry = dirty_tx % TX_RING_SIZE;
696                 int status = le32_to_cpu(tp->tx_ring[entry].status);
697
698                 if (status < 0) {
699                         tp->stats.tx_errors++;  /* It wasn't Txed */
700                         tp->tx_ring[entry].status = 0;
701                 }
702
703                 /* Check for Tx filter setup frames. */
704                 if (tp->tx_buffers[entry].skb == NULL) {
705                         /* test because dummy frames not mapped */
706                         if (tp->tx_buffers[entry].mapping)
707                                 pci_unmap_single(tp->pdev,
708                                         tp->tx_buffers[entry].mapping,
709                                         sizeof(tp->setup_frame),
710                                         PCI_DMA_TODEVICE);
711                         continue;
712                 }
713
714                 pci_unmap_single(tp->pdev, tp->tx_buffers[entry].mapping,
715                                 tp->tx_buffers[entry].skb->len,
716                                 PCI_DMA_TODEVICE);
717
718                 /* Free the original skb. */
719                 dev_kfree_skb_irq(tp->tx_buffers[entry].skb);
720                 tp->tx_buffers[entry].skb = NULL;
721                 tp->tx_buffers[entry].mapping = 0;
722         }
723 }
724
725 static void tulip_down (struct net_device *dev)
726 {
727         long ioaddr = dev->base_addr;
728         struct tulip_private *tp = netdev_priv(dev);
729         unsigned long flags;
730
731         del_timer_sync (&tp->timer);
732 #ifdef CONFIG_TULIP_NAPI
733         del_timer_sync (&tp->oom_timer);
734 #endif
735         spin_lock_irqsave (&tp->lock, flags);
736
737         /* Disable interrupts by clearing the interrupt mask. */
738         outl (0x00000000, ioaddr + CSR7);
739
740         /* Stop the Tx and Rx processes. */
741         tulip_stop_rxtx(tp);
742
743         /* prepare receive buffers */
744         tulip_refill_rx(dev);
745
746         /* release any unconsumed transmit buffers */
747         tulip_clean_tx_ring(tp);
748
749         if (inl (ioaddr + CSR6) != 0xffffffff)
750                 tp->stats.rx_missed_errors += inl (ioaddr + CSR8) & 0xffff;
751
752         spin_unlock_irqrestore (&tp->lock, flags);
753
754         init_timer(&tp->timer);
755         tp->timer.data = (unsigned long)dev;
756         tp->timer.function = tulip_tbl[tp->chip_id].media_timer;
757
758         dev->if_port = tp->saved_if_port;
759
760         /* Leave the driver in snooze, not sleep, mode. */
761         tulip_set_power_state (tp, 0, 1);
762 }
763
764
765 static int tulip_close (struct net_device *dev)
766 {
767         long ioaddr = dev->base_addr;
768         struct tulip_private *tp = netdev_priv(dev);
769         int i;
770
771         netif_stop_queue (dev);
772
773         tulip_down (dev);
774
775         if (tulip_debug > 1)
776                 printk (KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
777                         dev->name, inl (ioaddr + CSR5));
778
779         free_irq (dev->irq, dev);
780
781         /* Free all the skbuffs in the Rx queue. */
782         for (i = 0; i < RX_RING_SIZE; i++) {
783                 struct sk_buff *skb = tp->rx_buffers[i].skb;
784                 dma_addr_t mapping = tp->rx_buffers[i].mapping;
785
786                 tp->rx_buffers[i].skb = NULL;
787                 tp->rx_buffers[i].mapping = 0;
788
789                 tp->rx_ring[i].status = 0;      /* Not owned by Tulip chip. */
790                 tp->rx_ring[i].length = 0;
791                 tp->rx_ring[i].buffer1 = 0xBADF00D0;    /* An invalid address. */
792                 if (skb) {
793                         pci_unmap_single(tp->pdev, mapping, PKT_BUF_SZ,
794                                          PCI_DMA_FROMDEVICE);
795                         dev_kfree_skb (skb);
796                 }
797         }
798         for (i = 0; i < TX_RING_SIZE; i++) {
799                 struct sk_buff *skb = tp->tx_buffers[i].skb;
800
801                 if (skb != NULL) {
802                         pci_unmap_single(tp->pdev, tp->tx_buffers[i].mapping,
803                                          skb->len, PCI_DMA_TODEVICE);
804                         dev_kfree_skb (skb);
805                 }
806                 tp->tx_buffers[i].skb = NULL;
807                 tp->tx_buffers[i].mapping = 0;
808         }
809
810         return 0;
811 }
812
813 static struct net_device_stats *tulip_get_stats(struct net_device *dev)
814 {
815         struct tulip_private *tp = netdev_priv(dev);
816         long ioaddr = dev->base_addr;
817
818         if (netif_running(dev)) {
819                 unsigned long flags;
820
821                 spin_lock_irqsave (&tp->lock, flags);
822
823                 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
824
825                 spin_unlock_irqrestore(&tp->lock, flags);
826         }
827
828         return &tp->stats;
829 }
830
831
832 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
833 {
834         struct tulip_private *np = netdev_priv(dev);
835         u32 ethcmd;
836
837         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
838                 return -EFAULT;
839
840         switch (ethcmd) {
841         case ETHTOOL_GDRVINFO: {
842                 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
843                 strcpy(info.driver, DRV_NAME);
844                 strcpy(info.version, DRV_VERSION);
845                 strcpy(info.bus_info, pci_name(np->pdev));
846                 if (copy_to_user(useraddr, &info, sizeof(info)))
847                         return -EFAULT;
848                 return 0;
849         }
850
851         }
852
853         return -EOPNOTSUPP;
854 }
855
856 /* Provide ioctl() calls to examine the MII xcvr state. */
857 static int private_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
858 {
859         struct tulip_private *tp = netdev_priv(dev);
860         long ioaddr = dev->base_addr;
861         struct mii_ioctl_data *data = (struct mii_ioctl_data *) & rq->ifr_data;
862         const unsigned int phy_idx = 0;
863         int phy = tp->phys[phy_idx] & 0x1f;
864         unsigned int regnum = data->reg_num;
865
866         switch (cmd) {
867         case SIOCETHTOOL:
868                 return netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
869
870         case SIOCGMIIPHY:               /* Get address of MII PHY in use. */
871                 if (tp->mii_cnt)
872                         data->phy_id = phy;
873                 else if (tp->flags & HAS_NWAY)
874                         data->phy_id = 32;
875                 else if (tp->chip_id == COMET)
876                         data->phy_id = 1;
877                 else
878                         return -ENODEV;
879
880         case SIOCGMIIREG:               /* Read MII PHY register. */
881                 if (data->phy_id == 32 && (tp->flags & HAS_NWAY)) {
882                         int csr12 = inl (ioaddr + CSR12);
883                         int csr14 = inl (ioaddr + CSR14);
884                         switch (regnum) {
885                         case 0:
886                                 if (((csr14<<5) & 0x1000) ||
887                                         (dev->if_port == 5 && tp->nwayset))
888                                         data->val_out = 0x1000;
889                                 else
890                                         data->val_out = (tulip_media_cap[dev->if_port]&MediaIs100 ? 0x2000 : 0)
891                                                 | (tulip_media_cap[dev->if_port]&MediaIsFD ? 0x0100 : 0);
892                                 break;
893                         case 1:
894                                 data->val_out =
895                                         0x1848 +
896                                         ((csr12&0x7000) == 0x5000 ? 0x20 : 0) +
897                                         ((csr12&0x06) == 6 ? 0 : 4);
898                                 data->val_out |= 0x6048;
899                                 break;
900                         case 4:
901                                 /* Advertised value, bogus 10baseTx-FD value from CSR6. */
902                                 data->val_out =
903                                         ((inl(ioaddr + CSR6) >> 3) & 0x0040) +
904                                         ((csr14 >> 1) & 0x20) + 1;
905                                 data->val_out |= ((csr14 >> 9) & 0x03C0);
906                                 break;
907                         case 5: data->val_out = tp->lpar; break;
908                         default: data->val_out = 0; break;
909                         }
910                 } else {
911                         data->val_out = tulip_mdio_read (dev, data->phy_id & 0x1f, regnum);
912                 }
913                 return 0;
914
915         case SIOCSMIIREG:               /* Write MII PHY register. */
916                 if (!capable (CAP_NET_ADMIN))
917                         return -EPERM;
918                 if (regnum & ~0x1f)
919                         return -EINVAL;
920                 if (data->phy_id == phy) {
921                         u16 value = data->val_in;
922                         switch (regnum) {
923                         case 0: /* Check for autonegotiation on or reset. */
924                                 tp->full_duplex_lock = (value & 0x9000) ? 0 : 1;
925                                 if (tp->full_duplex_lock)
926                                         tp->full_duplex = (value & 0x0100) ? 1 : 0;
927                                 break;
928                         case 4:
929                                 tp->advertising[phy_idx] =
930                                 tp->mii_advertise = data->val_in;
931                                 break;
932                         }
933                 }
934                 if (data->phy_id == 32 && (tp->flags & HAS_NWAY)) {
935                         u16 value = data->val_in;
936                         if (regnum == 0) {
937                           if ((value & 0x1200) == 0x1200) {
938                             if (tp->chip_id == PNIC2) {
939                                    pnic2_start_nway (dev);
940                             } else {
941                                    t21142_start_nway (dev);
942                             }
943                           }
944                         } else if (regnum == 4)
945                                 tp->sym_advertise = value;
946                 } else {
947                         tulip_mdio_write (dev, data->phy_id & 0x1f, regnum, data->val_in);
948                 }
949                 return 0;
950         default:
951                 return -EOPNOTSUPP;
952         }
953
954         return -EOPNOTSUPP;
955 }
956
957
958 /* Set or clear the multicast filter for this adaptor.
959    Note that we only use exclusion around actually queueing the
960    new frame, not around filling tp->setup_frame.  This is non-deterministic
961    when re-entered but still correct. */
962
963 #undef set_bit_le
964 #define set_bit_le(i,p) do { ((char *)(p))[(i)/8] |= (1<<((i)%8)); } while(0)
965
966 static void build_setup_frame_hash(u16 *setup_frm, struct net_device *dev)
967 {
968         struct tulip_private *tp = netdev_priv(dev);
969         u16 hash_table[32];
970         struct dev_mc_list *mclist;
971         int i;
972         u16 *eaddrs;
973
974         memset(hash_table, 0, sizeof(hash_table));
975         set_bit_le(255, hash_table);                    /* Broadcast entry */
976         /* This should work on big-endian machines as well. */
977         for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
978              i++, mclist = mclist->next) {
979                 int index = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff;
980
981                 set_bit_le(index, hash_table);
982
983         }
984         for (i = 0; i < 32; i++) {
985                 *setup_frm++ = hash_table[i];
986                 *setup_frm++ = hash_table[i];
987         }
988         setup_frm = &tp->setup_frame[13*6];
989
990         /* Fill the final entry with our physical address. */
991         eaddrs = (u16 *)dev->dev_addr;
992         *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
993         *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
994         *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
995 }
996
997 static void build_setup_frame_perfect(u16 *setup_frm, struct net_device *dev)
998 {
999         struct tulip_private *tp = netdev_priv(dev);
1000         struct dev_mc_list *mclist;
1001         int i;
1002         u16 *eaddrs;
1003
1004         /* We have <= 14 addresses so we can use the wonderful
1005            16 address perfect filtering of the Tulip. */
1006         for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
1007              i++, mclist = mclist->next) {
1008                 eaddrs = (u16 *)mclist->dmi_addr;
1009                 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
1010                 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
1011                 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
1012         }
1013         /* Fill the unused entries with the broadcast address. */
1014         memset(setup_frm, 0xff, (15-i)*12);
1015         setup_frm = &tp->setup_frame[15*6];
1016
1017         /* Fill the final entry with our physical address. */
1018         eaddrs = (u16 *)dev->dev_addr;
1019         *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
1020         *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
1021         *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
1022 }
1023
1024
1025 static void set_rx_mode(struct net_device *dev)
1026 {
1027         struct tulip_private *tp = netdev_priv(dev);
1028         long ioaddr = dev->base_addr;
1029         int csr6;
1030
1031         csr6 = inl(ioaddr + CSR6) & ~0x00D5;
1032
1033         tp->csr6 &= ~0x00D5;
1034         if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
1035                 tp->csr6 |= AcceptAllMulticast | AcceptAllPhys;
1036                 csr6 |= AcceptAllMulticast | AcceptAllPhys;
1037                 /* Unconditionally log net taps. */
1038                 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1039         } else if ((dev->mc_count > 1000)  ||  (dev->flags & IFF_ALLMULTI)) {
1040                 /* Too many to filter well -- accept all multicasts. */
1041                 tp->csr6 |= AcceptAllMulticast;
1042                 csr6 |= AcceptAllMulticast;
1043         } else  if (tp->flags & MC_HASH_ONLY) {
1044                 /* Some work-alikes have only a 64-entry hash filter table. */
1045                 /* Should verify correctness on big-endian/__powerpc__ */
1046                 struct dev_mc_list *mclist;
1047                 int i;
1048                 if (dev->mc_count > 64) {               /* Arbitrary non-effective limit. */
1049                         tp->csr6 |= AcceptAllMulticast;
1050                         csr6 |= AcceptAllMulticast;
1051                 } else {
1052                         u32 mc_filter[2] = {0, 0};               /* Multicast hash filter */
1053                         int filterbit;
1054                         for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1055                                  i++, mclist = mclist->next) {
1056                                 if (tp->flags & COMET_MAC_ADDR)
1057                                         filterbit = ether_crc_le(ETH_ALEN, mclist->dmi_addr);
1058                                 else
1059                                         filterbit = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
1060                                 filterbit &= 0x3f;
1061                                 mc_filter[filterbit >> 5] |= cpu_to_le32(1 << (filterbit & 31));
1062                                 if (tulip_debug > 2) {
1063                                         printk(KERN_INFO "%s: Added filter for %2.2x:%2.2x:%2.2x:"
1064                                                    "%2.2x:%2.2x:%2.2x  %8.8x bit %d.\n", dev->name,
1065                                                    mclist->dmi_addr[0], mclist->dmi_addr[1],
1066                                                    mclist->dmi_addr[2], mclist->dmi_addr[3],
1067                                                    mclist->dmi_addr[4], mclist->dmi_addr[5],
1068                                                    ether_crc(ETH_ALEN, mclist->dmi_addr), filterbit);
1069                                 }
1070                         }
1071                         if (mc_filter[0] == tp->mc_filter[0]  &&
1072                                 mc_filter[1] == tp->mc_filter[1])
1073                                 ;                               /* No change. */
1074                         else if (tp->flags & IS_ASIX) {
1075                                 outl(2, ioaddr + CSR13);
1076                                 outl(mc_filter[0], ioaddr + CSR14);
1077                                 outl(3, ioaddr + CSR13);
1078                                 outl(mc_filter[1], ioaddr + CSR14);
1079                         } else if (tp->flags & COMET_MAC_ADDR) {
1080                                 outl(mc_filter[0], ioaddr + 0xAC);
1081                                 outl(mc_filter[1], ioaddr + 0xB0);
1082                         }
1083                         tp->mc_filter[0] = mc_filter[0];
1084                         tp->mc_filter[1] = mc_filter[1];
1085                 }
1086         } else {
1087                 unsigned long flags;
1088                 u32 tx_flags = 0x08000000 | 192;
1089
1090                 /* Note that only the low-address shortword of setup_frame is valid!
1091                    The values are doubled for big-endian architectures. */
1092                 if (dev->mc_count > 14) { /* Must use a multicast hash table. */
1093                         build_setup_frame_hash(tp->setup_frame, dev);
1094                         tx_flags = 0x08400000 | 192;
1095                 } else {
1096                         build_setup_frame_perfect(tp->setup_frame, dev);
1097                 }
1098
1099                 spin_lock_irqsave(&tp->lock, flags);
1100
1101                 if (tp->cur_tx - tp->dirty_tx > TX_RING_SIZE - 2) {
1102                         /* Same setup recently queued, we need not add it. */
1103                 } else {
1104                         unsigned int entry;
1105                         int dummy = -1;
1106
1107                         /* Now add this frame to the Tx list. */
1108
1109                         entry = tp->cur_tx++ % TX_RING_SIZE;
1110
1111                         if (entry != 0) {
1112                                 /* Avoid a chip errata by prefixing a dummy entry. */
1113                                 tp->tx_buffers[entry].skb = NULL;
1114                                 tp->tx_buffers[entry].mapping = 0;
1115                                 tp->tx_ring[entry].length =
1116                                         (entry == TX_RING_SIZE-1) ? cpu_to_le32(DESC_RING_WRAP) : 0;
1117                                 tp->tx_ring[entry].buffer1 = 0;
1118                                 /* Must set DescOwned later to avoid race with chip */
1119                                 dummy = entry;
1120                                 entry = tp->cur_tx++ % TX_RING_SIZE;
1121                         }
1122
1123                         tp->tx_buffers[entry].skb = NULL;
1124                         tp->tx_buffers[entry].mapping =
1125                                 pci_map_single(tp->pdev, tp->setup_frame,
1126                                                sizeof(tp->setup_frame),
1127                                                PCI_DMA_TODEVICE);
1128                         /* Put the setup frame on the Tx list. */
1129                         if (entry == TX_RING_SIZE-1)
1130                                 tx_flags |= DESC_RING_WRAP;             /* Wrap ring. */
1131                         tp->tx_ring[entry].length = cpu_to_le32(tx_flags);
1132                         tp->tx_ring[entry].buffer1 =
1133                                 cpu_to_le32(tp->tx_buffers[entry].mapping);
1134                         tp->tx_ring[entry].status = cpu_to_le32(DescOwned);
1135                         if (dummy >= 0)
1136                                 tp->tx_ring[dummy].status = cpu_to_le32(DescOwned);
1137                         if (tp->cur_tx - tp->dirty_tx >= TX_RING_SIZE - 2)
1138                                 netif_stop_queue(dev);
1139
1140                         /* Trigger an immediate transmit demand. */
1141                         outl(0, ioaddr + CSR1);
1142                 }
1143
1144                 spin_unlock_irqrestore(&tp->lock, flags);
1145         }
1146
1147         outl(csr6, ioaddr + CSR6);
1148 }
1149
1150 #ifdef CONFIG_TULIP_MWI
1151 static void __devinit tulip_mwi_config (struct pci_dev *pdev,
1152                                         struct net_device *dev)
1153 {
1154         struct tulip_private *tp = netdev_priv(dev);
1155         u8 cache;
1156         u16 pci_command;
1157         u32 csr0;
1158
1159         if (tulip_debug > 3)
1160                 printk(KERN_DEBUG "%s: tulip_mwi_config()\n", pci_name(pdev));
1161
1162         tp->csr0 = csr0 = 0;
1163
1164         /* if we have any cache line size at all, we can do MRM */
1165         csr0 |= MRM;
1166
1167         /* ...and barring hardware bugs, MWI */
1168         if (!(tp->chip_id == DC21143 && tp->revision == 65))
1169                 csr0 |= MWI;
1170
1171         /* set or disable MWI in the standard PCI command bit.
1172          * Check for the case where  mwi is desired but not available
1173          */
1174         if (csr0 & MWI) pci_set_mwi(pdev);
1175         else            pci_clear_mwi(pdev);
1176
1177         /* read result from hardware (in case bit refused to enable) */
1178         pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
1179         if ((csr0 & MWI) && (!(pci_command & PCI_COMMAND_INVALIDATE)))
1180                 csr0 &= ~MWI;
1181
1182         /* if cache line size hardwired to zero, no MWI */
1183         pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache);
1184         if ((csr0 & MWI) && (cache == 0)) {
1185                 csr0 &= ~MWI;
1186                 pci_clear_mwi(pdev);
1187         }
1188
1189         /* assign per-cacheline-size cache alignment and
1190          * burst length values
1191          */
1192         switch (cache) {
1193         case 8:
1194                 csr0 |= MRL | (1 << CALShift) | (16 << BurstLenShift);
1195                 break;
1196         case 16:
1197                 csr0 |= MRL | (2 << CALShift) | (16 << BurstLenShift);
1198                 break;
1199         case 32:
1200                 csr0 |= MRL | (3 << CALShift) | (32 << BurstLenShift);
1201                 break;
1202         default:
1203                 cache = 0;
1204                 break;
1205         }
1206
1207         /* if we have a good cache line size, we by now have a good
1208          * csr0, so save it and exit
1209          */
1210         if (cache)
1211                 goto out;
1212
1213         /* we don't have a good csr0 or cache line size, disable MWI */
1214         if (csr0 & MWI) {
1215                 pci_clear_mwi(pdev);
1216                 csr0 &= ~MWI;
1217         }
1218
1219         /* sane defaults for burst length and cache alignment
1220          * originally from de4x5 driver
1221          */
1222         csr0 |= (8 << BurstLenShift) | (1 << CALShift);
1223
1224 out:
1225         tp->csr0 = csr0;
1226         if (tulip_debug > 2)
1227                 printk(KERN_DEBUG "%s: MWI config cacheline=%d, csr0=%08x\n",
1228                        pci_name(pdev), cache, csr0);
1229 }
1230 #endif
1231
1232 static int __devinit tulip_init_one (struct pci_dev *pdev,
1233                                      const struct pci_device_id *ent)
1234 {
1235         struct tulip_private *tp;
1236         /* See note below on the multiport cards. */
1237         static unsigned char last_phys_addr[6] = {0x00, 'L', 'i', 'n', 'u', 'x'};
1238         static int last_irq;
1239         static int multiport_cnt;       /* For four-port boards w/one EEPROM */
1240         u8 chip_rev;
1241         int i, irq;
1242         unsigned short sum;
1243         unsigned char *ee_data;
1244         struct net_device *dev;
1245         long ioaddr;
1246         static int board_idx = -1;
1247         int chip_idx = ent->driver_data;
1248         unsigned int eeprom_missing = 0;
1249         unsigned int force_csr0 = 0;
1250
1251 #ifndef MODULE
1252         static int did_version;         /* Already printed version info. */
1253         if (tulip_debug > 0  &&  did_version++ == 0)
1254                 printk (KERN_INFO "%s", version);
1255 #endif
1256
1257         board_idx++;
1258
1259         /*
1260          *      Lan media wire a tulip chip to a wan interface. Needs a very
1261          *      different driver (lmc driver)
1262          */
1263
1264         if (pdev->subsystem_vendor == PCI_VENDOR_ID_LMC) {
1265                 printk (KERN_ERR PFX "skipping LMC card.\n");
1266                 return -ENODEV;
1267         }
1268
1269         /*
1270          *      Early DM9100's need software CRC and the DMFE driver
1271          */
1272
1273         if (pdev->vendor == 0x1282 && pdev->device == 0x9100)
1274         {
1275                 u32 dev_rev;
1276                 /* Read Chip revision */
1277                 pci_read_config_dword(pdev, PCI_REVISION_ID, &dev_rev);
1278                 if(dev_rev < 0x02000030)
1279                 {
1280                         printk(KERN_ERR PFX "skipping early DM9100 with Crc bug (use dmfe)\n");
1281                         return -ENODEV;
1282                 }
1283         }
1284
1285         /*
1286          *      Looks for early PCI chipsets where people report hangs
1287          *      without the workarounds being on.
1288          */
1289
1290         /* Intel Saturn. Switch to 8 long words burst, 8 long word cache aligned
1291            Aries might need this too. The Saturn errata are not pretty reading but
1292            thankfully it's an old 486 chipset.
1293         */
1294
1295         if (pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82424, NULL)) {
1296                 csr0 = MRL | MRM | (8 << BurstLenShift) | (1 << CALShift);
1297                 force_csr0 = 1;
1298         }
1299         /* The dreaded SiS496 486 chipset. Same workaround as above. */
1300         if (pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_496, NULL)) {
1301                 csr0 = MRL | MRM | (8 << BurstLenShift) | (1 << CALShift);
1302                 force_csr0 = 1;
1303         }
1304
1305         /* bugfix: the ASIX must have a burst limit or horrible things happen. */
1306         if (chip_idx == AX88140) {
1307                 if ((csr0 & 0x3f00) == 0)
1308                         csr0 |= 0x2000;
1309         }
1310
1311         /* PNIC doesn't have MWI/MRL/MRM... */
1312         if (chip_idx == LC82C168)
1313                 csr0 &= ~0xfff10000; /* zero reserved bits 31:20, 16 */
1314
1315         /* DM9102A has troubles with MRM & clear reserved bits 24:22, 20, 16, 7:1 */
1316         if ((pdev->vendor == 0x1282 && pdev->device == 0x9102)
1317                 || (pdev->vendor == 0x10b9 && pdev->device == 0x5261))
1318                 csr0 &= ~0x01f100ff;
1319
1320 #if defined(__sparc__)
1321         /* DM9102A needs 32-dword alignment/burst length on sparc - chip bug? */
1322         if ((pdev->vendor == 0x1282 && pdev->device == 0x9102)
1323                 || (pdev->vendor == 0x10b9 && pdev->device == 0x5261))
1324                 csr0 = (csr0 & ~0xff00) | 0xe000;
1325 #endif
1326
1327         /*
1328          *      And back to business
1329          */
1330
1331         i = pci_enable_device(pdev);
1332         if (i) {
1333                 printk (KERN_ERR PFX
1334                         "Cannot enable tulip board #%d, aborting\n",
1335                         board_idx);
1336                 return i;
1337         }
1338
1339         ioaddr = pci_resource_start (pdev, 0);
1340         irq = pdev->irq;
1341
1342         /* alloc_etherdev ensures aligned and zeroed private structures */
1343         dev = alloc_etherdev (sizeof (*tp));
1344         if (!dev) {
1345                 printk (KERN_ERR PFX "ether device alloc failed, aborting\n");
1346                 return -ENOMEM;
1347         }
1348
1349         SET_MODULE_OWNER(dev);
1350         SET_NETDEV_DEV(dev, &pdev->dev);
1351         if (pci_resource_len (pdev, 0) < tulip_tbl[chip_idx].io_size) {
1352                 printk (KERN_ERR PFX "%s: I/O region (0x%lx@0x%lx) too small, "
1353                         "aborting\n", pci_name(pdev),
1354                         pci_resource_len (pdev, 0),
1355                         pci_resource_start (pdev, 0));
1356                 goto err_out_free_netdev;
1357         }
1358
1359         /* grab all resources from both PIO and MMIO regions, as we
1360          * don't want anyone else messing around with our hardware */
1361         if (pci_request_regions (pdev, "tulip"))
1362                 goto err_out_free_netdev;
1363
1364 #ifndef USE_IO_OPS
1365         ioaddr = (unsigned long) ioremap (pci_resource_start (pdev, 1),
1366                                           tulip_tbl[chip_idx].io_size);
1367         if (!ioaddr)
1368                 goto err_out_free_res;
1369 #endif
1370
1371         pci_read_config_byte (pdev, PCI_REVISION_ID, &chip_rev);
1372
1373         /*
1374          * initialize private data structure 'tp'
1375          * it is zeroed and aligned in alloc_etherdev
1376          */
1377         tp = netdev_priv(dev);
1378
1379         tp->rx_ring = pci_alloc_consistent(pdev,
1380                                            sizeof(struct tulip_rx_desc) * RX_RING_SIZE +
1381                                            sizeof(struct tulip_tx_desc) * TX_RING_SIZE,
1382                                            &tp->rx_ring_dma);
1383         if (!tp->rx_ring)
1384                 goto err_out_mtable;
1385         tp->tx_ring = (struct tulip_tx_desc *)(tp->rx_ring + RX_RING_SIZE);
1386         tp->tx_ring_dma = tp->rx_ring_dma + sizeof(struct tulip_rx_desc) * RX_RING_SIZE;
1387
1388         tp->chip_id = chip_idx;
1389         tp->flags = tulip_tbl[chip_idx].flags;
1390         tp->pdev = pdev;
1391         tp->base_addr = ioaddr;
1392         tp->revision = chip_rev;
1393         tp->csr0 = csr0;
1394         spin_lock_init(&tp->lock);
1395         spin_lock_init(&tp->mii_lock);
1396         init_timer(&tp->timer);
1397         tp->timer.data = (unsigned long)dev;
1398         tp->timer.function = tulip_tbl[tp->chip_id].media_timer;
1399
1400         dev->base_addr = ioaddr;
1401
1402 #ifdef CONFIG_TULIP_MWI
1403         if (!force_csr0 && (tp->flags & HAS_PCI_MWI))
1404                 tulip_mwi_config (pdev, dev);
1405 #else
1406         /* MWI is broken for DC21143 rev 65... */
1407         if (chip_idx == DC21143 && chip_rev == 65)
1408                 tp->csr0 &= ~MWI;
1409 #endif
1410
1411         /* Stop the chip's Tx and Rx processes. */
1412         tulip_stop_rxtx(tp);
1413
1414         pci_set_master(pdev);
1415
1416         /* Clear the missed-packet counter. */
1417         inl(ioaddr + CSR8);
1418
1419         /* The station address ROM is read byte serially.  The register must
1420            be polled, waiting for the value to be read bit serially from the
1421            EEPROM.
1422            */
1423         ee_data = tp->eeprom;
1424         sum = 0;
1425         if (chip_idx == LC82C168) {
1426                 for (i = 0; i < 3; i++) {
1427                         int value, boguscnt = 100000;
1428                         outl(0x600 | i, ioaddr + 0x98);
1429                         do
1430                                 value = inl(ioaddr + CSR9);
1431                         while (value < 0  && --boguscnt > 0);
1432                         put_unaligned(le16_to_cpu(value), ((u16*)dev->dev_addr) + i);
1433                         sum += value & 0xffff;
1434                 }
1435         } else if (chip_idx == COMET) {
1436                 /* No need to read the EEPROM. */
1437                 put_unaligned(cpu_to_le32(inl(ioaddr + 0xA4)), (u32 *)dev->dev_addr);
1438                 put_unaligned(cpu_to_le16(inl(ioaddr + 0xA8)), (u16 *)(dev->dev_addr + 4));
1439                 for (i = 0; i < 6; i ++)
1440                         sum += dev->dev_addr[i];
1441         } else {
1442                 /* A serial EEPROM interface, we read now and sort it out later. */
1443                 int sa_offset = 0;
1444                 int ee_addr_size = tulip_read_eeprom(ioaddr, 0xff, 8) & 0x40000 ? 8 : 6;
1445
1446                 for (i = 0; i < sizeof(tp->eeprom)/2; i++)
1447                         ((u16 *)ee_data)[i] =
1448                                 le16_to_cpu(tulip_read_eeprom(ioaddr, i, ee_addr_size));
1449
1450                 /* DEC now has a specification (see Notes) but early board makers
1451                    just put the address in the first EEPROM locations. */
1452                 /* This does  memcmp(ee_data, ee_data+16, 8) */
1453                 for (i = 0; i < 8; i ++)
1454                         if (ee_data[i] != ee_data[16+i])
1455                                 sa_offset = 20;
1456                 if (chip_idx == CONEXANT) {
1457                         /* Check that the tuple type and length is correct. */
1458                         if (ee_data[0x198] == 0x04  &&  ee_data[0x199] == 6)
1459                                 sa_offset = 0x19A;
1460                 } else if (ee_data[0] == 0xff  &&  ee_data[1] == 0xff &&
1461                                    ee_data[2] == 0) {
1462                         sa_offset = 2;          /* Grrr, damn Matrox boards. */
1463                         multiport_cnt = 4;
1464                 }
1465 #ifdef CONFIG_DDB5476
1466                 if ((pdev->bus->number == 0) && (PCI_SLOT(pdev->devfn) == 6)) {
1467                         /* DDB5476 MAC address in first EEPROM locations. */
1468                        sa_offset = 0;
1469                        /* No media table either */
1470                        tp->flags &= ~HAS_MEDIA_TABLE;
1471                }
1472 #endif
1473 #ifdef CONFIG_DDB5477
1474                if ((pdev->bus->number == 0) && (PCI_SLOT(pdev->devfn) == 4)) {
1475                        /* DDB5477 MAC address in first EEPROM locations. */
1476                        sa_offset = 0;
1477                        /* No media table either */
1478                        tp->flags &= ~HAS_MEDIA_TABLE;
1479                }
1480 #endif
1481 #ifdef CONFIG_MIPS_COBALT
1482                if ((pdev->bus->number == 0) && 
1483                    ((PCI_SLOT(pdev->devfn) == 7) ||
1484                     (PCI_SLOT(pdev->devfn) == 12))) {
1485                        /* Cobalt MAC address in first EEPROM locations. */
1486                        sa_offset = 0;
1487                        /* No media table either */
1488                        tp->flags &= ~HAS_MEDIA_TABLE;
1489                }
1490 #endif
1491 #ifdef __hppa__
1492                 /* 3x5 HSC (J3514A) has a broken srom */
1493                 if(ee_data[0] == 0x61 && ee_data[1] == 0x10) {
1494                         /* pci_vendor_id and subsystem_id are swapped */
1495                         ee_data[0] = ee_data[2];
1496                         ee_data[1] = ee_data[3];
1497                         ee_data[2] = 0x61;
1498                         ee_data[3] = 0x10;
1499
1500                         /* srom need to be byte-swaped and shifted up 1 word.  
1501                          * This shift needs to happen at the end of the MAC
1502                          * first because of the 2 byte overlap.
1503                          */
1504                         for(i = 4; i >= 0; i -= 2) {
1505                                 ee_data[17 + i + 3] = ee_data[17 + i];
1506                                 ee_data[16 + i + 5] = ee_data[16 + i];
1507                         }
1508                 }
1509 #endif
1510                 for (i = 0; i < 6; i ++) {
1511                         dev->dev_addr[i] = ee_data[i + sa_offset];
1512                         sum += ee_data[i + sa_offset];
1513                 }
1514         }
1515         /* Lite-On boards have the address byte-swapped. */
1516         if ((dev->dev_addr[0] == 0xA0  ||  dev->dev_addr[0] == 0xC0)
1517                 &&  dev->dev_addr[1] == 0x00)
1518                 for (i = 0; i < 6; i+=2) {
1519                         char tmp = dev->dev_addr[i];
1520                         dev->dev_addr[i] = dev->dev_addr[i+1];
1521                         dev->dev_addr[i+1] = tmp;
1522                 }
1523         /* On the Zynx 315 Etherarray and other multiport boards only the
1524            first Tulip has an EEPROM.
1525            On Sparc systems the mac address is held in the OBP property
1526            "local-mac-address".
1527            The addresses of the subsequent ports are derived from the first.
1528            Many PCI BIOSes also incorrectly report the IRQ line, so we correct
1529            that here as well. */
1530         if (sum == 0  || sum == 6*0xff) {
1531 #if defined(__sparc__)
1532                 struct pcidev_cookie *pcp = pdev->sysdata;
1533 #endif
1534                 eeprom_missing = 1;
1535                 for (i = 0; i < 5; i++)
1536                         dev->dev_addr[i] = last_phys_addr[i];
1537                 dev->dev_addr[i] = last_phys_addr[i] + 1;
1538 #if defined(__sparc__)
1539                 if ((pcp != NULL) && prom_getproplen(pcp->prom_node,
1540                         "local-mac-address") == 6) {
1541                         prom_getproperty(pcp->prom_node, "local-mac-address",
1542                             dev->dev_addr, 6);
1543                 }
1544 #endif
1545 #if defined(__i386__)           /* Patch up x86 BIOS bug. */
1546                 if (last_irq)
1547                         irq = last_irq;
1548 #endif
1549         }
1550
1551         for (i = 0; i < 6; i++)
1552                 last_phys_addr[i] = dev->dev_addr[i];
1553         last_irq = irq;
1554         dev->irq = irq;
1555
1556         /* The lower four bits are the media type. */
1557         if (board_idx >= 0  &&  board_idx < MAX_UNITS) {
1558                 if (options[board_idx] & MEDIA_MASK)
1559                         tp->default_port = options[board_idx] & MEDIA_MASK;
1560                 if ((options[board_idx] & FullDuplex) || full_duplex[board_idx] > 0)
1561                         tp->full_duplex = 1;
1562                 if (mtu[board_idx] > 0)
1563                         dev->mtu = mtu[board_idx];
1564         }
1565         if (dev->mem_start & MEDIA_MASK)
1566                 tp->default_port = dev->mem_start & MEDIA_MASK;
1567         if (tp->default_port) {
1568                 printk(KERN_INFO "tulip%d: Transceiver selection forced to %s.\n",
1569                        board_idx, medianame[tp->default_port & MEDIA_MASK]);
1570                 tp->medialock = 1;
1571                 if (tulip_media_cap[tp->default_port] & MediaAlwaysFD)
1572                         tp->full_duplex = 1;
1573         }
1574         if (tp->full_duplex)
1575                 tp->full_duplex_lock = 1;
1576
1577         if (tulip_media_cap[tp->default_port] & MediaIsMII) {
1578                 u16 media2advert[] = { 0x20, 0x40, 0x03e0, 0x60, 0x80, 0x100, 0x200 };
1579                 tp->mii_advertise = media2advert[tp->default_port - 9];
1580                 tp->mii_advertise |= (tp->flags & HAS_8023X); /* Matching bits! */
1581         }
1582
1583         if (tp->flags & HAS_MEDIA_TABLE) {
1584                 sprintf(dev->name, "tulip%d", board_idx);       /* hack */
1585                 tulip_parse_eeprom(dev);
1586                 strcpy(dev->name, "eth%d");                     /* un-hack */
1587         }
1588
1589         if ((tp->flags & ALWAYS_CHECK_MII) ||
1590                 (tp->mtable  &&  tp->mtable->has_mii) ||
1591                 ( ! tp->mtable  &&  (tp->flags & HAS_MII))) {
1592                 if (tp->mtable  &&  tp->mtable->has_mii) {
1593                         for (i = 0; i < tp->mtable->leafcount; i++)
1594                                 if (tp->mtable->mleaf[i].media == 11) {
1595                                         tp->cur_index = i;
1596                                         tp->saved_if_port = dev->if_port;
1597                                         tulip_select_media(dev, 2);
1598                                         dev->if_port = tp->saved_if_port;
1599                                         break;
1600                                 }
1601                 }
1602
1603                 /* Find the connected MII xcvrs.
1604                    Doing this in open() would allow detecting external xcvrs
1605                    later, but takes much time. */
1606                 tulip_find_mii (dev, board_idx);
1607         }
1608
1609         /* The Tulip-specific entries in the device structure. */
1610         dev->open = tulip_open;
1611         dev->hard_start_xmit = tulip_start_xmit;
1612         dev->tx_timeout = tulip_tx_timeout;
1613         dev->watchdog_timeo = TX_TIMEOUT;
1614 #ifdef CONFIG_TULIP_NAPI
1615         dev->poll = tulip_poll;
1616         dev->weight = 16;
1617 #endif
1618         dev->stop = tulip_close;
1619         dev->get_stats = tulip_get_stats;
1620         dev->do_ioctl = private_ioctl;
1621         dev->set_multicast_list = set_rx_mode;
1622 #ifdef CONFIG_NET_POLL_CONTROLLER
1623         dev->poll_controller = &poll_tulip;
1624 #endif
1625
1626         if (register_netdev(dev))
1627                 goto err_out_free_ring;
1628
1629         printk(KERN_INFO "%s: %s rev %d at %#3lx,",
1630                dev->name, tulip_tbl[chip_idx].chip_name, chip_rev, ioaddr);
1631         pci_set_drvdata(pdev, dev);
1632
1633         if (eeprom_missing)
1634                 printk(" EEPROM not present,");
1635         for (i = 0; i < 6; i++)
1636                 printk("%c%2.2X", i ? ':' : ' ', dev->dev_addr[i]);
1637         printk(", IRQ %d.\n", irq);
1638
1639         if (tp->chip_id == PNIC2)
1640                 tp->link_change = pnic2_lnk_change;
1641         else if (tp->flags & HAS_NWAY)
1642                 tp->link_change = t21142_lnk_change;
1643         else if (tp->flags & HAS_PNICNWAY)
1644                 tp->link_change = pnic_lnk_change;
1645
1646         /* Reset the xcvr interface and turn on heartbeat. */
1647         switch (chip_idx) {
1648         case DC21140:
1649         case DM910X:
1650         default:
1651                 if (tp->mtable)
1652                         outl(tp->mtable->csr12dir | 0x100, ioaddr + CSR12);
1653                 break;
1654         case DC21142:
1655                 if (tp->mii_cnt  ||  tulip_media_cap[dev->if_port] & MediaIsMII) {
1656                         outl(csr6_mask_defstate, ioaddr + CSR6);
1657                         outl(0x0000, ioaddr + CSR13);
1658                         outl(0x0000, ioaddr + CSR14);
1659                         outl(csr6_mask_hdcap, ioaddr + CSR6);
1660                 } else
1661                         t21142_start_nway(dev);
1662                 break;
1663         case PNIC2:
1664                 /* just do a reset for sanity sake */
1665                 outl(0x0000, ioaddr + CSR13);
1666                 outl(0x0000, ioaddr + CSR14);
1667                 break;
1668         case LC82C168:
1669                 if ( ! tp->mii_cnt) {
1670                         tp->nway = 1;
1671                         tp->nwayset = 0;
1672                         outl(csr6_ttm | csr6_ca, ioaddr + CSR6);
1673                         outl(0x30, ioaddr + CSR12);
1674                         outl(0x0001F078, ioaddr + CSR6);
1675                         outl(0x0201F078, ioaddr + CSR6); /* Turn on autonegotiation. */
1676                 }
1677                 break;
1678         case MX98713:
1679         case COMPEX9881:
1680                 outl(0x00000000, ioaddr + CSR6);
1681                 outl(0x000711C0, ioaddr + CSR14); /* Turn on NWay. */
1682                 outl(0x00000001, ioaddr + CSR13);
1683                 break;
1684         case MX98715:
1685         case MX98725:
1686                 outl(0x01a80000, ioaddr + CSR6);
1687                 outl(0xFFFFFFFF, ioaddr + CSR14);
1688                 outl(0x00001000, ioaddr + CSR12);
1689                 break;
1690         case COMET:
1691                 /* No initialization necessary. */
1692                 break;
1693         }
1694
1695         /* put the chip in snooze mode until opened */
1696         tulip_set_power_state (tp, 0, 1);
1697
1698         return 0;
1699
1700 err_out_free_ring:
1701         pci_free_consistent (pdev,
1702                              sizeof (struct tulip_rx_desc) * RX_RING_SIZE +
1703                              sizeof (struct tulip_tx_desc) * TX_RING_SIZE,
1704                              tp->rx_ring, tp->rx_ring_dma);
1705
1706 err_out_mtable:
1707         if (tp->mtable)
1708                 kfree (tp->mtable);
1709 #ifndef USE_IO_OPS
1710         iounmap((void *)ioaddr);
1711
1712 err_out_free_res:
1713 #endif
1714         pci_release_regions (pdev);
1715
1716 err_out_free_netdev:
1717         free_netdev (dev);
1718         return -ENODEV;
1719 }
1720
1721
1722 #ifdef CONFIG_PM
1723
1724 static int tulip_suspend (struct pci_dev *pdev, u32 state)
1725 {
1726         struct net_device *dev = pci_get_drvdata(pdev);
1727
1728         if (dev && netif_running (dev) && netif_device_present (dev)) {
1729                 netif_device_detach (dev);
1730                 tulip_down (dev);
1731                 /* pci_power_off(pdev, -1); */
1732         }
1733         return 0;
1734 }
1735
1736
1737 static int tulip_resume(struct pci_dev *pdev)
1738 {
1739         struct net_device *dev = pci_get_drvdata(pdev);
1740
1741         if (dev && netif_running (dev) && !netif_device_present (dev)) {
1742 #if 1
1743                 pci_enable_device (pdev);
1744 #endif
1745                 /* pci_power_on(pdev); */
1746                 tulip_up (dev);
1747                 netif_device_attach (dev);
1748         }
1749         return 0;
1750 }
1751
1752 #endif /* CONFIG_PM */
1753
1754
1755 static void __devexit tulip_remove_one (struct pci_dev *pdev)
1756 {
1757         struct net_device *dev = pci_get_drvdata (pdev);
1758         struct tulip_private *tp;
1759
1760         if (!dev)
1761                 return;
1762
1763         tp = netdev_priv(dev);
1764         unregister_netdev(dev);
1765         pci_free_consistent (pdev,
1766                              sizeof (struct tulip_rx_desc) * RX_RING_SIZE +
1767                              sizeof (struct tulip_tx_desc) * TX_RING_SIZE,
1768                              tp->rx_ring, tp->rx_ring_dma);
1769         if (tp->mtable)
1770                 kfree (tp->mtable);
1771 #ifndef USE_IO_OPS
1772         iounmap((void *)dev->base_addr);
1773 #endif
1774         free_netdev (dev);
1775         pci_release_regions (pdev);
1776         pci_set_drvdata (pdev, NULL);
1777
1778         /* pci_power_off (pdev, -1); */
1779 }
1780
1781 #ifdef CONFIG_NET_POLL_CONTROLLER
1782 /*
1783  * Polling 'interrupt' - used by things like netconsole to send skbs
1784  * without having to re-enable interrupts. It's not called while
1785  * the interrupt routine is executing.
1786  */
1787
1788 static void poll_tulip (struct net_device *dev)
1789 {
1790         /* disable_irq here is not very nice, but with the lockless
1791            interrupt handler we have no other choice. */
1792         disable_irq(dev->irq);
1793         tulip_interrupt (dev->irq, dev, NULL);
1794         enable_irq(dev->irq);
1795 }
1796 #endif
1797
1798 static struct pci_driver tulip_driver = {
1799         .name           = DRV_NAME,
1800         .id_table       = tulip_pci_tbl,
1801         .probe          = tulip_init_one,
1802         .remove         = __devexit_p(tulip_remove_one),
1803 #ifdef CONFIG_PM
1804         .suspend        = tulip_suspend,
1805         .resume         = tulip_resume,
1806 #endif /* CONFIG_PM */
1807 };
1808
1809
1810 static int __init tulip_init (void)
1811 {
1812 #ifdef MODULE
1813         printk (KERN_INFO "%s", version);
1814 #endif
1815
1816         /* copy module parms into globals */
1817         tulip_rx_copybreak = rx_copybreak;
1818         tulip_max_interrupt_work = max_interrupt_work;
1819
1820         /* probe for and init boards */
1821         return pci_module_init (&tulip_driver);
1822 }
1823
1824
1825 static void __exit tulip_cleanup (void)
1826 {
1827         pci_unregister_driver (&tulip_driver);
1828 }
1829
1830
1831 module_init(tulip_init);
1832 module_exit(tulip_cleanup);