VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / acenic.c
1 /*
2  * acenic.c: Linux driver for the Alteon AceNIC Gigabit Ethernet card
3  *           and other Tigon based cards.
4  *
5  * Copyright 1998-2002 by Jes Sorensen, <jes@trained-monkey.org>.
6  *
7  * Thanks to Alteon and 3Com for providing hardware and documentation
8  * enabling me to write this driver.
9  *
10  * A mailing list for discussing the use of this driver has been
11  * setup, please subscribe to the lists if you have any questions
12  * about the driver. Send mail to linux-acenic-help@sunsite.auc.dk to
13  * see how to subscribe.
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * Additional credits:
21  *   Pete Wyckoff <wyckoff@ca.sandia.gov>: Initial Linux/Alpha and trace
22  *       dump support. The trace dump support has not been
23  *       integrated yet however.
24  *   Troy Benjegerdes: Big Endian (PPC) patches.
25  *   Nate Stahl: Better out of memory handling and stats support.
26  *   Aman Singla: Nasty race between interrupt handler and tx code dealing
27  *                with 'testing the tx_ret_csm and setting tx_full'
28  *   David S. Miller <davem@redhat.com>: conversion to new PCI dma mapping
29  *                                       infrastructure and Sparc support
30  *   Pierrick Pinasseau (CERN): For lending me an Ultra 5 to test the
31  *                              driver under Linux/Sparc64
32  *   Matt Domsch <Matt_Domsch@dell.com>: Detect Alteon 1000baseT cards
33  *                                       ETHTOOL_GDRVINFO support
34  *   Chip Salzenberg <chip@valinux.com>: Fix race condition between tx
35  *                                       handler and close() cleanup.
36  *   Ken Aaker <kdaaker@rchland.vnet.ibm.com>: Correct check for whether
37  *                                       memory mapped IO is enabled to
38  *                                       make the driver work on RS/6000.
39  *   Takayoshi Kouchi <kouchi@hpc.bs1.fc.nec.co.jp>: Identifying problem
40  *                                       where the driver would disable
41  *                                       bus master mode if it had to disable
42  *                                       write and invalidate.
43  *   Stephen Hack <stephen_hack@hp.com>: Fixed ace_set_mac_addr for little
44  *                                       endian systems.
45  *   Val Henson <vhenson@esscom.com>:    Reset Jumbo skb producer and
46  *                                       rx producer index when
47  *                                       flushing the Jumbo ring.
48  *   Hans Grobler <grobh@sun.ac.za>:     Memory leak fixes in the
49  *                                       driver init path.
50  *   Grant Grundler <grundler@cup.hp.com>: PCI write posting fixes.
51  */
52
53 #include <linux/config.h>
54 #include <linux/module.h>
55 #include <linux/version.h>
56 #include <linux/types.h>
57 #include <linux/errno.h>
58 #include <linux/ioport.h>
59 #include <linux/pci.h>
60 #include <linux/kernel.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
64 #include <linux/init.h>
65 #include <linux/delay.h>
66 #include <linux/mm.h>
67 #include <linux/highmem.h>
68 #include <linux/sockios.h>
69
70 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
71 #include <linux/if_vlan.h>
72 #endif
73
74 #ifdef SIOCETHTOOL
75 #include <linux/ethtool.h>
76 #endif
77
78 #include <net/sock.h>
79 #include <net/ip.h>
80
81 #include <asm/system.h>
82 #include <asm/io.h>
83 #include <asm/irq.h>
84 #include <asm/byteorder.h>
85 #include <asm/uaccess.h>
86
87
88 #define DRV_NAME "acenic"
89
90 #undef INDEX_DEBUG
91
92 #ifdef CONFIG_ACENIC_OMIT_TIGON_I
93 #define ACE_IS_TIGON_I(ap)      0
94 #define ACE_TX_RING_ENTRIES(ap) MAX_TX_RING_ENTRIES
95 #else
96 #define ACE_IS_TIGON_I(ap)      (ap->version == 1)
97 #define ACE_TX_RING_ENTRIES(ap) ap->tx_ring_entries
98 #endif
99
100 #ifndef PCI_VENDOR_ID_ALTEON
101 #define PCI_VENDOR_ID_ALTEON            0x12ae  
102 #endif
103 #ifndef PCI_DEVICE_ID_ALTEON_ACENIC_FIBRE
104 #define PCI_DEVICE_ID_ALTEON_ACENIC_FIBRE  0x0001
105 #define PCI_DEVICE_ID_ALTEON_ACENIC_COPPER 0x0002
106 #endif
107 #ifndef PCI_DEVICE_ID_3COM_3C985
108 #define PCI_DEVICE_ID_3COM_3C985        0x0001
109 #endif
110 #ifndef PCI_VENDOR_ID_NETGEAR
111 #define PCI_VENDOR_ID_NETGEAR           0x1385
112 #define PCI_DEVICE_ID_NETGEAR_GA620     0x620a
113 #endif
114 #ifndef PCI_DEVICE_ID_NETGEAR_GA620T
115 #define PCI_DEVICE_ID_NETGEAR_GA620T    0x630a
116 #endif
117
118
119 /*
120  * Farallon used the DEC vendor ID by mistake and they seem not
121  * to care - stinky!
122  */
123 #ifndef PCI_DEVICE_ID_FARALLON_PN9000SX
124 #define PCI_DEVICE_ID_FARALLON_PN9000SX 0x1a
125 #endif
126 #ifndef PCI_DEVICE_ID_FARALLON_PN9100T
127 #define PCI_DEVICE_ID_FARALLON_PN9100T  0xfa
128 #endif
129 #ifndef PCI_VENDOR_ID_SGI
130 #define PCI_VENDOR_ID_SGI               0x10a9
131 #endif
132 #ifndef PCI_DEVICE_ID_SGI_ACENIC
133 #define PCI_DEVICE_ID_SGI_ACENIC        0x0009
134 #endif
135
136 static struct pci_device_id acenic_pci_tbl[] = {
137         { PCI_VENDOR_ID_ALTEON, PCI_DEVICE_ID_ALTEON_ACENIC_FIBRE,
138           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
139         { PCI_VENDOR_ID_ALTEON, PCI_DEVICE_ID_ALTEON_ACENIC_COPPER,
140           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
141         { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C985,
142           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
143         { PCI_VENDOR_ID_NETGEAR, PCI_DEVICE_ID_NETGEAR_GA620,
144           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
145         { PCI_VENDOR_ID_NETGEAR, PCI_DEVICE_ID_NETGEAR_GA620T,
146           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
147         /*
148          * Farallon used the DEC vendor ID on their cards incorrectly,
149          * then later Alteon's ID.
150          */
151         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_FARALLON_PN9000SX,
152           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
153         { PCI_VENDOR_ID_ALTEON, PCI_DEVICE_ID_FARALLON_PN9100T,
154           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
155         { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_ACENIC,
156           PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
157         { }
158 };
159 MODULE_DEVICE_TABLE(pci, acenic_pci_tbl);
160
161 #ifndef SET_NETDEV_DEV
162 #define SET_NETDEV_DEV(net, pdev)       do{} while(0)
163 #endif
164
165 #if LINUX_VERSION_CODE >= 0x2051c
166 #define ace_sync_irq(irq)       synchronize_irq(irq)
167 #else
168 #define ace_sync_irq(irq)       synchronize_irq()
169 #endif
170
171 #ifndef offset_in_page
172 #define offset_in_page(ptr)     ((unsigned long)(ptr) & ~PAGE_MASK)
173 #endif
174
175 #define ACE_MAX_MOD_PARMS       8
176 #define BOARD_IDX_STATIC        0
177 #define BOARD_IDX_OVERFLOW      -1
178
179 #if (defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)) && \
180         defined(NETIF_F_HW_VLAN_RX)
181 #define ACENIC_DO_VLAN          1
182 #define ACE_RCB_VLAN_FLAG       RCB_FLG_VLAN_ASSIST
183 #else
184 #define ACENIC_DO_VLAN          0
185 #define ACE_RCB_VLAN_FLAG       0
186 #endif
187
188 #include "acenic.h"
189
190 /*
191  * These must be defined before the firmware is included.
192  */
193 #define MAX_TEXT_LEN    96*1024
194 #define MAX_RODATA_LEN  8*1024
195 #define MAX_DATA_LEN    2*1024
196
197 #include "acenic_firmware.h"
198
199 #ifndef tigon2FwReleaseLocal
200 #define tigon2FwReleaseLocal 0
201 #endif
202
203 /*
204  * This driver currently supports Tigon I and Tigon II based cards
205  * including the Alteon AceNIC, the 3Com 3C985[B] and NetGear
206  * GA620. The driver should also work on the SGI, DEC and Farallon
207  * versions of the card, however I have not been able to test that
208  * myself.
209  *
210  * This card is really neat, it supports receive hardware checksumming
211  * and jumbo frames (up to 9000 bytes) and does a lot of work in the
212  * firmware. Also the programming interface is quite neat, except for
213  * the parts dealing with the i2c eeprom on the card ;-)
214  *
215  * Using jumbo frames:
216  *
217  * To enable jumbo frames, simply specify an mtu between 1500 and 9000
218  * bytes to ifconfig. Jumbo frames can be enabled or disabled at any time
219  * by running `ifconfig eth<X> mtu <MTU>' with <X> being the Ethernet
220  * interface number and <MTU> being the MTU value.
221  *
222  * Module parameters:
223  *
224  * When compiled as a loadable module, the driver allows for a number
225  * of module parameters to be specified. The driver supports the
226  * following module parameters:
227  *
228  *  trace=<val> - Firmware trace level. This requires special traced
229  *                firmware to replace the firmware supplied with
230  *                the driver - for debugging purposes only.
231  *
232  *  link=<val>  - Link state. Normally you want to use the default link
233  *                parameters set by the driver. This can be used to
234  *                override these in case your switch doesn't negotiate
235  *                the link properly. Valid values are:
236  *         0x0001 - Force half duplex link.
237  *         0x0002 - Do not negotiate line speed with the other end.
238  *         0x0010 - 10Mbit/sec link.
239  *         0x0020 - 100Mbit/sec link.
240  *         0x0040 - 1000Mbit/sec link.
241  *         0x0100 - Do not negotiate flow control.
242  *         0x0200 - Enable RX flow control Y
243  *         0x0400 - Enable TX flow control Y (Tigon II NICs only).
244  *                Default value is 0x0270, ie. enable link+flow
245  *                control negotiation. Negotiating the highest
246  *                possible link speed with RX flow control enabled.
247  *
248  *                When disabling link speed negotiation, only one link
249  *                speed is allowed to be specified!
250  *
251  *  tx_coal_tick=<val> - number of coalescing clock ticks (us) allowed
252  *                to wait for more packets to arive before
253  *                interrupting the host, from the time the first
254  *                packet arrives.
255  *
256  *  rx_coal_tick=<val> - number of coalescing clock ticks (us) allowed
257  *                to wait for more packets to arive in the transmit ring,
258  *                before interrupting the host, after transmitting the
259  *                first packet in the ring.
260  *
261  *  max_tx_desc=<val> - maximum number of transmit descriptors
262  *                (packets) transmitted before interrupting the host.
263  *
264  *  max_rx_desc=<val> - maximum number of receive descriptors
265  *                (packets) received before interrupting the host.
266  *
267  *  tx_ratio=<val> - 7 bit value (0 - 63) specifying the split in 64th
268  *                increments of the NIC's on board memory to be used for
269  *                transmit and receive buffers. For the 1MB NIC app. 800KB
270  *                is available, on the 1/2MB NIC app. 300KB is available.
271  *                68KB will always be available as a minimum for both
272  *                directions. The default value is a 50/50 split.
273  *  dis_pci_mem_inval=<val> - disable PCI memory write and invalidate
274  *                operations, default (1) is to always disable this as
275  *                that is what Alteon does on NT. I have not been able
276  *                to measure any real performance differences with
277  *                this on my systems. Set <val>=0 if you want to
278  *                enable these operations.
279  *
280  * If you use more than one NIC, specify the parameters for the
281  * individual NICs with a comma, ie. trace=0,0x00001fff,0 you want to
282  * run tracing on NIC #2 but not on NIC #1 and #3.
283  *
284  * TODO:
285  *
286  * - Proper multicast support.
287  * - NIC dump support.
288  * - More tuning parameters.
289  *
290  * The mini ring is not used under Linux and I am not sure it makes sense
291  * to actually use it.
292  *
293  * New interrupt handler strategy:
294  *
295  * The old interrupt handler worked using the traditional method of
296  * replacing an skbuff with a new one when a packet arrives. However
297  * the rx rings do not need to contain a static number of buffer
298  * descriptors, thus it makes sense to move the memory allocation out
299  * of the main interrupt handler and do it in a bottom half handler
300  * and only allocate new buffers when the number of buffers in the
301  * ring is below a certain threshold. In order to avoid starving the
302  * NIC under heavy load it is however necessary to force allocation
303  * when hitting a minimum threshold. The strategy for alloction is as
304  * follows:
305  *
306  *     RX_LOW_BUF_THRES    - allocate buffers in the bottom half
307  *     RX_PANIC_LOW_THRES  - we are very low on buffers, allocate
308  *                           the buffers in the interrupt handler
309  *     RX_RING_THRES       - maximum number of buffers in the rx ring
310  *     RX_MINI_THRES       - maximum number of buffers in the mini ring
311  *     RX_JUMBO_THRES      - maximum number of buffers in the jumbo ring
312  *
313  * One advantagous side effect of this allocation approach is that the
314  * entire rx processing can be done without holding any spin lock
315  * since the rx rings and registers are totally independent of the tx
316  * ring and its registers.  This of course includes the kmalloc's of
317  * new skb's. Thus start_xmit can run in parallel with rx processing
318  * and the memory allocation on SMP systems.
319  *
320  * Note that running the skb reallocation in a bottom half opens up
321  * another can of races which needs to be handled properly. In
322  * particular it can happen that the interrupt handler tries to run
323  * the reallocation while the bottom half is either running on another
324  * CPU or was interrupted on the same CPU. To get around this the
325  * driver uses bitops to prevent the reallocation routines from being
326  * reentered.
327  *
328  * TX handling can also be done without holding any spin lock, wheee
329  * this is fun! since tx_ret_csm is only written to by the interrupt
330  * handler. The case to be aware of is when shutting down the device
331  * and cleaning up where it is necessary to make sure that
332  * start_xmit() is not running while this is happening. Well DaveM
333  * informs me that this case is already protected against ... bye bye
334  * Mr. Spin Lock, it was nice to know you.
335  *
336  * TX interrupts are now partly disabled so the NIC will only generate
337  * TX interrupts for the number of coal ticks, not for the number of
338  * TX packets in the queue. This should reduce the number of TX only,
339  * ie. when no RX processing is done, interrupts seen.
340  */
341
342 /*
343  * Threshold values for RX buffer allocation - the low water marks for
344  * when to start refilling the rings are set to 75% of the ring
345  * sizes. It seems to make sense to refill the rings entirely from the
346  * intrrupt handler once it gets below the panic threshold, that way
347  * we don't risk that the refilling is moved to another CPU when the
348  * one running the interrupt handler just got the slab code hot in its
349  * cache.
350  */
351 #define RX_RING_SIZE            72
352 #define RX_MINI_SIZE            64
353 #define RX_JUMBO_SIZE           48
354
355 #define RX_PANIC_STD_THRES      16
356 #define RX_PANIC_STD_REFILL     (3*RX_PANIC_STD_THRES)/2
357 #define RX_LOW_STD_THRES        (3*RX_RING_SIZE)/4
358 #define RX_PANIC_MINI_THRES     12
359 #define RX_PANIC_MINI_REFILL    (3*RX_PANIC_MINI_THRES)/2
360 #define RX_LOW_MINI_THRES       (3*RX_MINI_SIZE)/4
361 #define RX_PANIC_JUMBO_THRES    6
362 #define RX_PANIC_JUMBO_REFILL   (3*RX_PANIC_JUMBO_THRES)/2
363 #define RX_LOW_JUMBO_THRES      (3*RX_JUMBO_SIZE)/4
364
365
366 /*
367  * Size of the mini ring entries, basically these just should be big
368  * enough to take TCP ACKs
369  */
370 #define ACE_MINI_SIZE           100
371
372 #define ACE_MINI_BUFSIZE        ACE_MINI_SIZE
373 #define ACE_STD_BUFSIZE         (ACE_STD_MTU + ETH_HLEN + 4)
374 #define ACE_JUMBO_BUFSIZE       (ACE_JUMBO_MTU + ETH_HLEN + 4)
375
376 /*
377  * There seems to be a magic difference in the effect between 995 and 996
378  * but little difference between 900 and 995 ... no idea why.
379  *
380  * There is now a default set of tuning parameters which is set, depending
381  * on whether or not the user enables Jumbo frames. It's assumed that if
382  * Jumbo frames are enabled, the user wants optimal tuning for that case.
383  */
384 #define DEF_TX_COAL             400 /* 996 */
385 #define DEF_TX_MAX_DESC         60  /* was 40 */
386 #define DEF_RX_COAL             120 /* 1000 */
387 #define DEF_RX_MAX_DESC         25
388 #define DEF_TX_RATIO            21 /* 24 */
389
390 #define DEF_JUMBO_TX_COAL       20
391 #define DEF_JUMBO_TX_MAX_DESC   60
392 #define DEF_JUMBO_RX_COAL       30
393 #define DEF_JUMBO_RX_MAX_DESC   6
394 #define DEF_JUMBO_TX_RATIO      21
395
396 #if tigon2FwReleaseLocal < 20001118
397 /*
398  * Standard firmware and early modifications duplicate
399  * IRQ load without this flag (coal timer is never reset).
400  * Note that with this flag tx_coal should be less than
401  * time to xmit full tx ring.
402  * 400usec is not so bad for tx ring size of 128.
403  */
404 #define TX_COAL_INTS_ONLY       1       /* worth it */
405 #else
406 /*
407  * With modified firmware, this is not necessary, but still useful.
408  */
409 #define TX_COAL_INTS_ONLY       1
410 #endif
411
412 #define DEF_TRACE               0
413 #define DEF_STAT                (2 * TICKS_PER_SEC)
414
415
416 static int link[ACE_MAX_MOD_PARMS];
417 static int trace[ACE_MAX_MOD_PARMS];
418 static int tx_coal_tick[ACE_MAX_MOD_PARMS];
419 static int rx_coal_tick[ACE_MAX_MOD_PARMS];
420 static int max_tx_desc[ACE_MAX_MOD_PARMS];
421 static int max_rx_desc[ACE_MAX_MOD_PARMS];
422 static int tx_ratio[ACE_MAX_MOD_PARMS];
423 static int dis_pci_mem_inval[ACE_MAX_MOD_PARMS] = {1, 1, 1, 1, 1, 1, 1, 1};
424
425 MODULE_AUTHOR("Jes Sorensen <jes@trained-monkey.org>");
426 MODULE_LICENSE("GPL");
427 MODULE_DESCRIPTION("AceNIC/3C985/GA620 Gigabit Ethernet driver");
428 MODULE_PARM(link, "1-" __MODULE_STRING(8) "i");
429 MODULE_PARM(trace, "1-" __MODULE_STRING(8) "i");
430 MODULE_PARM(tx_coal_tick, "1-" __MODULE_STRING(8) "i");
431 MODULE_PARM(max_tx_desc, "1-" __MODULE_STRING(8) "i");
432 MODULE_PARM(rx_coal_tick, "1-" __MODULE_STRING(8) "i");
433 MODULE_PARM(max_rx_desc, "1-" __MODULE_STRING(8) "i");
434 MODULE_PARM(tx_ratio, "1-" __MODULE_STRING(8) "i");
435 MODULE_PARM_DESC(link, "AceNIC/3C985/NetGear link state");
436 MODULE_PARM_DESC(trace, "AceNIC/3C985/NetGear firmware trace level");
437 MODULE_PARM_DESC(tx_coal_tick, "AceNIC/3C985/GA620 max clock ticks to wait from first tx descriptor arrives");
438 MODULE_PARM_DESC(max_tx_desc, "AceNIC/3C985/GA620 max number of transmit descriptors to wait");
439 MODULE_PARM_DESC(rx_coal_tick, "AceNIC/3C985/GA620 max clock ticks to wait from first rx descriptor arrives");
440 MODULE_PARM_DESC(max_rx_desc, "AceNIC/3C985/GA620 max number of receive descriptors to wait");
441 MODULE_PARM_DESC(tx_ratio, "AceNIC/3C985/GA620 ratio of NIC memory used for TX/RX descriptors (range 0-63)");
442
443
444 static char version[] __initdata = 
445   "acenic.c: v0.92 08/05/2002  Jes Sorensen, linux-acenic@SunSITE.dk\n"
446   "                            http://home.cern.ch/~jes/gige/acenic.html\n";
447
448 static int ace_get_settings(struct net_device *, struct ethtool_cmd *);
449 static int ace_set_settings(struct net_device *, struct ethtool_cmd *);
450 static void ace_get_drvinfo(struct net_device *, struct ethtool_drvinfo *);
451
452 static struct ethtool_ops ace_ethtool_ops = {
453         .get_settings = ace_get_settings,
454         .set_settings = ace_set_settings,
455         .get_drvinfo = ace_get_drvinfo,
456 };
457
458 static int __devinit acenic_probe_one(struct pci_dev *pdev,
459                 const struct pci_device_id *id)
460 {
461         struct net_device *dev;
462         struct ace_private *ap;
463         static int boards_found;
464
465         dev = alloc_etherdev(sizeof(struct ace_private));
466         if (dev == NULL) {
467                 printk(KERN_ERR "acenic: Unable to allocate "
468                        "net_device structure!\n");
469                 return -ENOMEM;
470         }
471
472         SET_MODULE_OWNER(dev);
473         SET_NETDEV_DEV(dev, &pdev->dev);
474
475         ap = dev->priv;
476         ap->pdev = pdev;
477
478         dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
479 #if ACENIC_DO_VLAN
480         dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
481         dev->vlan_rx_register = ace_vlan_rx_register;
482         dev->vlan_rx_kill_vid = ace_vlan_rx_kill_vid;
483 #endif
484         if (1) {
485                 static void ace_watchdog(struct net_device *dev);
486                 dev->tx_timeout = &ace_watchdog;
487                 dev->watchdog_timeo = 5*HZ;
488         }
489
490         dev->open = &ace_open;
491         dev->stop = &ace_close;
492         dev->hard_start_xmit = &ace_start_xmit;
493         dev->get_stats = &ace_get_stats;
494         dev->set_multicast_list = &ace_set_multicast_list;
495         SET_ETHTOOL_OPS(dev, &ace_ethtool_ops);
496         dev->set_mac_address = &ace_set_mac_addr;
497         dev->change_mtu = &ace_change_mtu;
498
499         /* we only display this string ONCE */
500         if (!boards_found)
501                 printk(version);
502
503         if (pci_enable_device(pdev))
504                 goto fail_free_netdev;
505
506         /*
507          * Enable master mode before we start playing with the
508          * pci_command word since pci_set_master() will modify
509          * it.
510          */
511         pci_set_master(pdev);
512
513         pci_read_config_word(pdev, PCI_COMMAND, &ap->pci_command);
514
515         /* OpenFirmware on Mac's does not set this - DOH.. */ 
516         if (!(ap->pci_command & PCI_COMMAND_MEMORY)) {
517                 printk(KERN_INFO "%s: Enabling PCI Memory Mapped "
518                        "access - was not enabled by BIOS/Firmware\n",
519                        dev->name);
520                 ap->pci_command = ap->pci_command | PCI_COMMAND_MEMORY;
521                 pci_write_config_word(ap->pdev, PCI_COMMAND,
522                                       ap->pci_command);
523                 wmb();
524         }
525
526         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ap->pci_latency);
527         if (ap->pci_latency <= 0x40) {
528                 ap->pci_latency = 0x40;
529                 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ap->pci_latency);
530         }
531
532         /*
533          * Remap the regs into kernel space - this is abuse of
534          * dev->base_addr since it was means for I/O port
535          * addresses but who gives a damn.
536          */
537         dev->base_addr = pci_resource_start(pdev, 0);
538         ap->regs = (struct ace_regs *)ioremap(dev->base_addr, 0x4000);
539         if (!ap->regs) {
540                 printk(KERN_ERR "%s:  Unable to map I/O register, "
541                        "AceNIC %i will be disabled.\n",
542                        dev->name, boards_found);
543                 goto fail_free_netdev;
544         }
545
546         switch(pdev->vendor) {
547         case PCI_VENDOR_ID_ALTEON:
548                 if (pdev->device == PCI_DEVICE_ID_FARALLON_PN9100T) {
549                         strncpy(ap->name, "Farallon PN9100-T "
550                                 "Gigabit Ethernet", sizeof (ap->name));
551                         printk(KERN_INFO "%s: Farallon PN9100-T ",
552                                dev->name);
553                 } else {
554                         strncpy(ap->name, "AceNIC Gigabit Ethernet",
555                                 sizeof (ap->name));
556                         printk(KERN_INFO "%s: Alteon AceNIC ",
557                                dev->name);
558                 }
559                 break;
560         case PCI_VENDOR_ID_3COM:
561                 strncpy(ap->name, "3Com 3C985 Gigabit Ethernet",
562                         sizeof (ap->name));
563                 printk(KERN_INFO "%s: 3Com 3C985 ", dev->name);
564                 break;
565         case PCI_VENDOR_ID_NETGEAR:
566                 strncpy(ap->name, "NetGear GA620 Gigabit Ethernet",
567                         sizeof (ap->name));
568                 printk(KERN_INFO "%s: NetGear GA620 ", dev->name);
569                 break;
570         case PCI_VENDOR_ID_DEC:
571                 if (pdev->device == PCI_DEVICE_ID_FARALLON_PN9000SX) {
572                         strncpy(ap->name, "Farallon PN9000-SX "
573                                 "Gigabit Ethernet", sizeof (ap->name));
574                         printk(KERN_INFO "%s: Farallon PN9000-SX ",
575                                dev->name);
576                         break;
577                 }
578         case PCI_VENDOR_ID_SGI:
579                 strncpy(ap->name, "SGI AceNIC Gigabit Ethernet",
580                         sizeof (ap->name));
581                 printk(KERN_INFO "%s: SGI AceNIC ", dev->name);
582                 break;
583         default:
584                 strncpy(ap->name, "Unknown AceNIC based Gigabit "
585                         "Ethernet", sizeof (ap->name));
586                 printk(KERN_INFO "%s: Unknown AceNIC ", dev->name);
587                 break;
588         }
589
590         ap->name [sizeof (ap->name) - 1] = '\0';
591         printk("Gigabit Ethernet at 0x%08lx, ", dev->base_addr);
592 #ifdef __sparc__
593         printk("irq %s\n", __irq_itoa(pdev->irq));
594 #else
595         printk("irq %i\n", pdev->irq);
596 #endif
597
598 #ifdef CONFIG_ACENIC_OMIT_TIGON_I
599         if ((readl(&ap->regs->HostCtrl) >> 28) == 4) {
600                 printk(KERN_ERR "%s: Driver compiled without Tigon I"
601                        " support - NIC disabled\n", dev->name);
602                 goto fail_uninit;
603         }
604 #endif
605
606         if (ace_allocate_descriptors(dev))
607                 goto fail_free_netdev;
608
609 #ifdef MODULE
610         if (boards_found >= ACE_MAX_MOD_PARMS)
611                 ap->board_idx = BOARD_IDX_OVERFLOW;
612         else
613                 ap->board_idx = boards_found;
614 #else
615         ap->board_idx = BOARD_IDX_STATIC;
616 #endif
617
618         if (ace_init(dev))
619                 goto fail_free_netdev;
620
621         if (register_netdev(dev)) {
622                 printk(KERN_ERR "acenic: device registration failed\n");
623                 goto fail_uninit;
624         }
625
626         if (ap->pci_using_dac)
627                 dev->features |= NETIF_F_HIGHDMA;
628
629         pci_set_drvdata(pdev, dev);
630
631         boards_found++;
632         return 0;
633
634  fail_uninit:
635         ace_init_cleanup(dev);
636  fail_free_netdev:
637         free_netdev(dev);
638         return -ENODEV;
639 }
640
641 static void __devexit acenic_remove_one(struct pci_dev *pdev)
642 {
643         struct net_device *dev = pci_get_drvdata(pdev);
644         struct ace_private *ap = dev->priv;
645         struct ace_regs *regs = ap->regs;
646         short i;
647
648         unregister_netdev(dev);
649
650         writel(readl(&regs->CpuCtrl) | CPU_HALT, &regs->CpuCtrl);
651         if (ap->version >= 2)
652                 writel(readl(&regs->CpuBCtrl) | CPU_HALT, &regs->CpuBCtrl);
653         
654         /*
655          * This clears any pending interrupts
656          */
657         writel(1, &regs->Mb0Lo);
658         readl(&regs->CpuCtrl);  /* flush */
659
660         /*
661          * Make sure no other CPUs are processing interrupts
662          * on the card before the buffers are being released.
663          * Otherwise one might experience some `interesting'
664          * effects.
665          *
666          * Then release the RX buffers - jumbo buffers were
667          * already released in ace_close().
668          */
669         ace_sync_irq(dev->irq);
670
671         for (i = 0; i < RX_STD_RING_ENTRIES; i++) {
672                 struct sk_buff *skb = ap->skb->rx_std_skbuff[i].skb;
673
674                 if (skb) {
675                         struct ring_info *ringp;
676                         dma_addr_t mapping;
677
678                         ringp = &ap->skb->rx_std_skbuff[i];
679                         mapping = pci_unmap_addr(ringp, mapping);
680                         pci_unmap_page(ap->pdev, mapping,
681                                        ACE_STD_BUFSIZE,
682                                        PCI_DMA_FROMDEVICE);
683
684                         ap->rx_std_ring[i].size = 0;
685                         ap->skb->rx_std_skbuff[i].skb = NULL;
686                         dev_kfree_skb(skb);
687                 }
688         }
689
690         if (ap->version >= 2) {
691                 for (i = 0; i < RX_MINI_RING_ENTRIES; i++) {
692                         struct sk_buff *skb = ap->skb->rx_mini_skbuff[i].skb;
693
694                         if (skb) {
695                                 struct ring_info *ringp;
696                                 dma_addr_t mapping;
697
698                                 ringp = &ap->skb->rx_mini_skbuff[i];
699                                 mapping = pci_unmap_addr(ringp,mapping);
700                                 pci_unmap_page(ap->pdev, mapping,
701                                                ACE_MINI_BUFSIZE,
702                                                PCI_DMA_FROMDEVICE);
703
704                                 ap->rx_mini_ring[i].size = 0;
705                                 ap->skb->rx_mini_skbuff[i].skb = NULL;
706                                 dev_kfree_skb(skb);
707                         }
708                 }
709         }
710
711         for (i = 0; i < RX_JUMBO_RING_ENTRIES; i++) {
712                 struct sk_buff *skb = ap->skb->rx_jumbo_skbuff[i].skb;
713                 if (skb) {
714                         struct ring_info *ringp;
715                         dma_addr_t mapping;
716
717                         ringp = &ap->skb->rx_jumbo_skbuff[i];
718                         mapping = pci_unmap_addr(ringp, mapping);
719                         pci_unmap_page(ap->pdev, mapping,
720                                        ACE_JUMBO_BUFSIZE,
721                                        PCI_DMA_FROMDEVICE);
722
723                         ap->rx_jumbo_ring[i].size = 0;
724                         ap->skb->rx_jumbo_skbuff[i].skb = NULL;
725                         dev_kfree_skb(skb);
726                 }
727         }
728
729         ace_init_cleanup(dev);
730         free_netdev(dev);
731 }
732
733 static struct pci_driver acenic_pci_driver = {
734         .name           = "acenic",
735         .id_table       = acenic_pci_tbl,
736         .probe          = acenic_probe_one,
737         .remove         = __devexit_p(acenic_remove_one),
738 };
739
740 static int __init acenic_init(void)
741 {
742         return pci_module_init(&acenic_pci_driver);
743 }
744
745 static void __exit acenic_exit(void)
746 {
747         pci_unregister_driver(&acenic_pci_driver);
748 }
749
750 module_init(acenic_init);
751 module_exit(acenic_exit);
752
753 static void ace_free_descriptors(struct net_device *dev)
754 {
755         struct ace_private *ap = dev->priv;
756         int size;
757
758         if (ap->rx_std_ring != NULL) {
759                 size = (sizeof(struct rx_desc) *
760                         (RX_STD_RING_ENTRIES +
761                          RX_JUMBO_RING_ENTRIES +
762                          RX_MINI_RING_ENTRIES +
763                          RX_RETURN_RING_ENTRIES));
764                 pci_free_consistent(ap->pdev, size, ap->rx_std_ring,
765                                     ap->rx_ring_base_dma);
766                 ap->rx_std_ring = NULL;
767                 ap->rx_jumbo_ring = NULL;
768                 ap->rx_mini_ring = NULL;
769                 ap->rx_return_ring = NULL;
770         }
771         if (ap->evt_ring != NULL) {
772                 size = (sizeof(struct event) * EVT_RING_ENTRIES);
773                 pci_free_consistent(ap->pdev, size, ap->evt_ring,
774                                     ap->evt_ring_dma);
775                 ap->evt_ring = NULL;
776         }
777         if (ap->tx_ring != NULL && !ACE_IS_TIGON_I(ap)) {
778                 size = (sizeof(struct tx_desc) * MAX_TX_RING_ENTRIES);
779                 pci_free_consistent(ap->pdev, size, ap->tx_ring,
780                                     ap->tx_ring_dma);
781         }
782         ap->tx_ring = NULL;
783
784         if (ap->evt_prd != NULL) {
785                 pci_free_consistent(ap->pdev, sizeof(u32),
786                                     (void *)ap->evt_prd, ap->evt_prd_dma);
787                 ap->evt_prd = NULL;
788         }
789         if (ap->rx_ret_prd != NULL) {
790                 pci_free_consistent(ap->pdev, sizeof(u32),
791                                     (void *)ap->rx_ret_prd,
792                                     ap->rx_ret_prd_dma);
793                 ap->rx_ret_prd = NULL;
794         }
795         if (ap->tx_csm != NULL) {
796                 pci_free_consistent(ap->pdev, sizeof(u32),
797                                     (void *)ap->tx_csm, ap->tx_csm_dma);
798                 ap->tx_csm = NULL;
799         }
800 }
801
802
803 static int ace_allocate_descriptors(struct net_device *dev)
804 {
805         struct ace_private *ap = dev->priv;
806         int size;
807
808         size = (sizeof(struct rx_desc) *
809                 (RX_STD_RING_ENTRIES +
810                  RX_JUMBO_RING_ENTRIES +
811                  RX_MINI_RING_ENTRIES +
812                  RX_RETURN_RING_ENTRIES));
813
814         ap->rx_std_ring = pci_alloc_consistent(ap->pdev, size,
815                                                &ap->rx_ring_base_dma);
816         if (ap->rx_std_ring == NULL)
817                 goto fail;
818
819         ap->rx_jumbo_ring = ap->rx_std_ring + RX_STD_RING_ENTRIES;
820         ap->rx_mini_ring = ap->rx_jumbo_ring + RX_JUMBO_RING_ENTRIES;
821         ap->rx_return_ring = ap->rx_mini_ring + RX_MINI_RING_ENTRIES;
822
823         size = (sizeof(struct event) * EVT_RING_ENTRIES);
824
825         ap->evt_ring = pci_alloc_consistent(ap->pdev, size, &ap->evt_ring_dma);
826
827         if (ap->evt_ring == NULL)
828                 goto fail;
829
830         /*
831          * Only allocate a host TX ring for the Tigon II, the Tigon I
832          * has to use PCI registers for this ;-(
833          */
834         if (!ACE_IS_TIGON_I(ap)) {
835                 size = (sizeof(struct tx_desc) * MAX_TX_RING_ENTRIES);
836
837                 ap->tx_ring = pci_alloc_consistent(ap->pdev, size,
838                                                    &ap->tx_ring_dma);
839
840                 if (ap->tx_ring == NULL)
841                         goto fail;
842         }
843
844         ap->evt_prd = pci_alloc_consistent(ap->pdev, sizeof(u32),
845                                            &ap->evt_prd_dma);
846         if (ap->evt_prd == NULL)
847                 goto fail;
848
849         ap->rx_ret_prd = pci_alloc_consistent(ap->pdev, sizeof(u32),
850                                               &ap->rx_ret_prd_dma);
851         if (ap->rx_ret_prd == NULL)
852                 goto fail;
853
854         ap->tx_csm = pci_alloc_consistent(ap->pdev, sizeof(u32),
855                                           &ap->tx_csm_dma);
856         if (ap->tx_csm == NULL)
857                 goto fail;
858
859         return 0;
860
861 fail:
862         /* Clean up. */
863         ace_init_cleanup(dev);
864         return 1;
865 }
866
867
868 /*
869  * Generic cleanup handling data allocated during init. Used when the
870  * module is unloaded or if an error occurs during initialization
871  */
872 static void ace_init_cleanup(struct net_device *dev)
873 {
874         struct ace_private *ap;
875
876         ap = dev->priv;
877
878         ace_free_descriptors(dev);
879
880         if (ap->info)
881                 pci_free_consistent(ap->pdev, sizeof(struct ace_info),
882                                     ap->info, ap->info_dma);
883         if (ap->skb)
884                 kfree(ap->skb);
885         if (ap->trace_buf)
886                 kfree(ap->trace_buf);
887
888         if (dev->irq)
889                 free_irq(dev->irq, dev);
890
891         iounmap(ap->regs);
892 }
893
894
895 /*
896  * Commands are considered to be slow.
897  */
898 static inline void ace_issue_cmd(struct ace_regs *regs, struct cmd *cmd)
899 {
900         u32 idx;
901
902         idx = readl(&regs->CmdPrd);
903
904         writel(*(u32 *)(cmd), &regs->CmdRng[idx]);
905         idx = (idx + 1) % CMD_RING_ENTRIES;
906
907         writel(idx, &regs->CmdPrd);
908 }
909
910
911 static int __init ace_init(struct net_device *dev)
912 {
913         struct ace_private *ap;
914         struct ace_regs *regs;
915         struct ace_info *info = NULL;
916         struct pci_dev *pdev;
917         unsigned long myjif;
918         u64 tmp_ptr;
919         u32 tig_ver, mac1, mac2, tmp, pci_state;
920         int board_idx, ecode = 0;
921         short i;
922         unsigned char cache_size;
923
924         ap = dev->priv;
925         regs = ap->regs;
926
927         board_idx = ap->board_idx;
928
929         /*
930          * aman@sgi.com - its useful to do a NIC reset here to
931          * address the `Firmware not running' problem subsequent
932          * to any crashes involving the NIC
933          */
934         writel(HW_RESET | (HW_RESET << 24), &regs->HostCtrl);
935         readl(&regs->HostCtrl);         /* PCI write posting */
936         udelay(5);
937
938         /*
939          * Don't access any other registers before this point!
940          */
941 #ifdef __BIG_ENDIAN
942         /*
943          * This will most likely need BYTE_SWAP once we switch
944          * to using __raw_writel()
945          */
946         writel((WORD_SWAP | CLR_INT | ((WORD_SWAP | CLR_INT) << 24)),
947                &regs->HostCtrl);
948 #else
949         writel((CLR_INT | WORD_SWAP | ((CLR_INT | WORD_SWAP) << 24)),
950                &regs->HostCtrl);
951 #endif
952         readl(&regs->HostCtrl);         /* PCI write posting */
953
954         /*
955          * Stop the NIC CPU and clear pending interrupts
956          */
957         writel(readl(&regs->CpuCtrl) | CPU_HALT, &regs->CpuCtrl);
958         readl(&regs->CpuCtrl);          /* PCI write posting */
959         writel(0, &regs->Mb0Lo);
960
961         tig_ver = readl(&regs->HostCtrl) >> 28;
962
963         switch(tig_ver){
964 #ifndef CONFIG_ACENIC_OMIT_TIGON_I
965         case 4:
966         case 5:
967                 printk(KERN_INFO "  Tigon I  (Rev. %i), Firmware: %i.%i.%i, ",
968                        tig_ver, tigonFwReleaseMajor, tigonFwReleaseMinor,
969                        tigonFwReleaseFix);
970                 writel(0, &regs->LocalCtrl);
971                 ap->version = 1;
972                 ap->tx_ring_entries = TIGON_I_TX_RING_ENTRIES;
973                 break;
974 #endif
975         case 6:
976                 printk(KERN_INFO "  Tigon II (Rev. %i), Firmware: %i.%i.%i, ",
977                        tig_ver, tigon2FwReleaseMajor, tigon2FwReleaseMinor,
978                        tigon2FwReleaseFix);
979                 writel(readl(&regs->CpuBCtrl) | CPU_HALT, &regs->CpuBCtrl);
980                 readl(&regs->CpuBCtrl);         /* PCI write posting */
981                 /*
982                  * The SRAM bank size does _not_ indicate the amount
983                  * of memory on the card, it controls the _bank_ size!
984                  * Ie. a 1MB AceNIC will have two banks of 512KB.
985                  */
986                 writel(SRAM_BANK_512K, &regs->LocalCtrl);
987                 writel(SYNC_SRAM_TIMING, &regs->MiscCfg);
988                 ap->version = 2;
989                 ap->tx_ring_entries = MAX_TX_RING_ENTRIES;
990                 break;
991         default:
992                 printk(KERN_WARNING "  Unsupported Tigon version detected "
993                        "(%i)\n", tig_ver);
994                 ecode = -ENODEV;
995                 goto init_error;
996         }
997
998         /*
999          * ModeStat _must_ be set after the SRAM settings as this change
1000          * seems to corrupt the ModeStat and possible other registers.
1001          * The SRAM settings survive resets and setting it to the same
1002          * value a second time works as well. This is what caused the
1003          * `Firmware not running' problem on the Tigon II.
1004          */
1005 #ifdef __BIG_ENDIAN
1006         writel(ACE_BYTE_SWAP_DMA | ACE_WARN | ACE_FATAL | ACE_BYTE_SWAP_BD |
1007                ACE_WORD_SWAP_BD | ACE_NO_JUMBO_FRAG, &regs->ModeStat);
1008 #else
1009         writel(ACE_BYTE_SWAP_DMA | ACE_WARN | ACE_FATAL |
1010                ACE_WORD_SWAP_BD | ACE_NO_JUMBO_FRAG, &regs->ModeStat);
1011 #endif
1012         readl(&regs->ModeStat);         /* PCI write posting */
1013
1014         mac1 = 0;
1015         for(i = 0; i < 4; i++) {
1016                 mac1 = mac1 << 8;
1017                 tmp = read_eeprom_byte(dev, 0x8c+i);
1018                 if (tmp < 0) {
1019                         ecode = -EIO;
1020                         goto init_error;
1021                 } else
1022                         mac1 |= (tmp & 0xff);
1023         }
1024         mac2 = 0;
1025         for(i = 4; i < 8; i++) {
1026                 mac2 = mac2 << 8;
1027                 tmp = read_eeprom_byte(dev, 0x8c+i);
1028                 if (tmp < 0) {
1029                         ecode = -EIO;
1030                         goto init_error;
1031                 } else
1032                         mac2 |= (tmp & 0xff);
1033         }
1034
1035         writel(mac1, &regs->MacAddrHi);
1036         writel(mac2, &regs->MacAddrLo);
1037
1038         printk("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
1039                (mac1 >> 8) & 0xff, mac1 & 0xff, (mac2 >> 24) &0xff,
1040                (mac2 >> 16) & 0xff, (mac2 >> 8) & 0xff, mac2 & 0xff);
1041
1042         dev->dev_addr[0] = (mac1 >> 8) & 0xff;
1043         dev->dev_addr[1] = mac1 & 0xff;
1044         dev->dev_addr[2] = (mac2 >> 24) & 0xff;
1045         dev->dev_addr[3] = (mac2 >> 16) & 0xff;
1046         dev->dev_addr[4] = (mac2 >> 8) & 0xff;
1047         dev->dev_addr[5] = mac2 & 0xff;
1048
1049         /*
1050          * Looks like this is necessary to deal with on all architectures,
1051          * even this %$#%$# N440BX Intel based thing doesn't get it right.
1052          * Ie. having two NICs in the machine, one will have the cache
1053          * line set at boot time, the other will not.
1054          */
1055         pdev = ap->pdev;
1056         pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_size);
1057         cache_size <<= 2;
1058         if (cache_size != SMP_CACHE_BYTES) {
1059                 printk(KERN_INFO "  PCI cache line size set incorrectly "
1060                        "(%i bytes) by BIOS/FW, ", cache_size);
1061                 if (cache_size > SMP_CACHE_BYTES)
1062                         printk("expecting %i\n", SMP_CACHE_BYTES);
1063                 else {
1064                         printk("correcting to %i\n", SMP_CACHE_BYTES);
1065                         pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
1066                                               SMP_CACHE_BYTES >> 2);
1067                 }
1068         }
1069
1070         pci_state = readl(&regs->PciState);
1071         printk(KERN_INFO "  PCI bus width: %i bits, speed: %iMHz, "
1072                "latency: %i clks\n",
1073                 (pci_state & PCI_32BIT) ? 32 : 64,
1074                 (pci_state & PCI_66MHZ) ? 66 : 33, 
1075                 ap->pci_latency);
1076
1077         /*
1078          * Set the max DMA transfer size. Seems that for most systems
1079          * the performance is better when no MAX parameter is
1080          * set. However for systems enabling PCI write and invalidate,
1081          * DMA writes must be set to the L1 cache line size to get
1082          * optimal performance.
1083          *
1084          * The default is now to turn the PCI write and invalidate off
1085          * - that is what Alteon does for NT.
1086          */
1087         tmp = READ_CMD_MEM | WRITE_CMD_MEM;
1088         if (ap->version >= 2) {
1089                 tmp |= (MEM_READ_MULTIPLE | (pci_state & PCI_66MHZ));
1090                 /*
1091                  * Tuning parameters only supported for 8 cards
1092                  */
1093                 if (board_idx == BOARD_IDX_OVERFLOW ||
1094                     dis_pci_mem_inval[board_idx]) {
1095                         if (ap->pci_command & PCI_COMMAND_INVALIDATE) {
1096                                 ap->pci_command &= ~PCI_COMMAND_INVALIDATE;
1097                                 pci_write_config_word(pdev, PCI_COMMAND,
1098                                                       ap->pci_command);
1099                                 printk(KERN_INFO "  Disabling PCI memory "
1100                                        "write and invalidate\n");
1101                         }
1102                 } else if (ap->pci_command & PCI_COMMAND_INVALIDATE) {
1103                         printk(KERN_INFO "  PCI memory write & invalidate "
1104                                "enabled by BIOS, enabling counter measures\n");
1105
1106                         switch(SMP_CACHE_BYTES) {
1107                         case 16:
1108                                 tmp |= DMA_WRITE_MAX_16;
1109                                 break;
1110                         case 32:
1111                                 tmp |= DMA_WRITE_MAX_32;
1112                                 break;
1113                         case 64:
1114                                 tmp |= DMA_WRITE_MAX_64;
1115                                 break;
1116                         case 128:
1117                                 tmp |= DMA_WRITE_MAX_128;
1118                                 break;
1119                         default:
1120                                 printk(KERN_INFO "  Cache line size %i not "
1121                                        "supported, PCI write and invalidate "
1122                                        "disabled\n", SMP_CACHE_BYTES);
1123                                 ap->pci_command &= ~PCI_COMMAND_INVALIDATE;
1124                                 pci_write_config_word(pdev, PCI_COMMAND,
1125                                                       ap->pci_command);
1126                         }
1127                 }
1128         }
1129
1130 #ifdef __sparc__
1131         /*
1132          * On this platform, we know what the best dma settings
1133          * are.  We use 64-byte maximum bursts, because if we
1134          * burst larger than the cache line size (or even cross
1135          * a 64byte boundary in a single burst) the UltraSparc
1136          * PCI controller will disconnect at 64-byte multiples.
1137          *
1138          * Read-multiple will be properly enabled above, and when
1139          * set will give the PCI controller proper hints about
1140          * prefetching.
1141          */
1142         tmp &= ~DMA_READ_WRITE_MASK;
1143         tmp |= DMA_READ_MAX_64;
1144         tmp |= DMA_WRITE_MAX_64;
1145 #endif
1146 #ifdef __alpha__
1147         tmp &= ~DMA_READ_WRITE_MASK;
1148         tmp |= DMA_READ_MAX_128;
1149         /*
1150          * All the docs say MUST NOT. Well, I did.
1151          * Nothing terrible happens, if we load wrong size.
1152          * Bit w&i still works better!
1153          */
1154         tmp |= DMA_WRITE_MAX_128;
1155 #endif
1156         writel(tmp, &regs->PciState);
1157
1158 #if 0
1159         /*
1160          * The Host PCI bus controller driver has to set FBB.
1161          * If all devices on that PCI bus support FBB, then the controller
1162          * can enable FBB support in the Host PCI Bus controller (or on
1163          * the PCI-PCI bridge if that applies).
1164          * -ggg
1165          */
1166         /*
1167          * I have received reports from people having problems when this
1168          * bit is enabled.
1169          */
1170         if (!(ap->pci_command & PCI_COMMAND_FAST_BACK)) {
1171                 printk(KERN_INFO "  Enabling PCI Fast Back to Back\n");
1172                 ap->pci_command |= PCI_COMMAND_FAST_BACK;
1173                 pci_write_config_word(pdev, PCI_COMMAND, ap->pci_command);
1174         }
1175 #endif
1176                 
1177         /*
1178          * Configure DMA attributes.
1179          */
1180         if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL)) {
1181                 ap->pci_using_dac = 1;
1182         } else if (!pci_set_dma_mask(pdev, 0xffffffffULL)) {
1183                 ap->pci_using_dac = 0;
1184         } else {
1185                 ecode = -ENODEV;
1186                 goto init_error;
1187         }
1188
1189         /*
1190          * Initialize the generic info block and the command+event rings
1191          * and the control blocks for the transmit and receive rings
1192          * as they need to be setup once and for all.
1193          */
1194         if (!(info = pci_alloc_consistent(ap->pdev, sizeof(struct ace_info),
1195                                           &ap->info_dma))) {
1196                 ecode = -EAGAIN;
1197                 goto init_error;
1198         }
1199         ap->info = info;
1200
1201         /*
1202          * Get the memory for the skb rings.
1203          */
1204         if (!(ap->skb = kmalloc(sizeof(struct ace_skb), GFP_KERNEL))) {
1205                 ecode = -EAGAIN;
1206                 goto init_error;
1207         }
1208
1209         ecode = request_irq(pdev->irq, ace_interrupt, SA_SHIRQ,
1210                             DRV_NAME, dev);
1211         if (ecode) {
1212                 printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
1213                        DRV_NAME, pdev->irq);
1214                 goto init_error;
1215         } else
1216                 dev->irq = pdev->irq;
1217
1218 #ifdef INDEX_DEBUG
1219         spin_lock_init(&ap->debug_lock);
1220         ap->last_tx = ACE_TX_RING_ENTRIES(ap) - 1;
1221         ap->last_std_rx = 0;
1222         ap->last_mini_rx = 0;
1223 #endif
1224
1225         memset(ap->info, 0, sizeof(struct ace_info));
1226         memset(ap->skb, 0, sizeof(struct ace_skb));
1227
1228         ace_load_firmware(dev);
1229         ap->fw_running = 0;
1230
1231         tmp_ptr = ap->info_dma;
1232         writel(tmp_ptr >> 32, &regs->InfoPtrHi);
1233         writel(tmp_ptr & 0xffffffff, &regs->InfoPtrLo);
1234
1235         memset(ap->evt_ring, 0, EVT_RING_ENTRIES * sizeof(struct event));
1236
1237         set_aceaddr(&info->evt_ctrl.rngptr, ap->evt_ring_dma);
1238         info->evt_ctrl.flags = 0;
1239
1240         *(ap->evt_prd) = 0;
1241         wmb();
1242         set_aceaddr(&info->evt_prd_ptr, ap->evt_prd_dma);
1243         writel(0, &regs->EvtCsm);
1244
1245         set_aceaddr(&info->cmd_ctrl.rngptr, 0x100);
1246         info->cmd_ctrl.flags = 0;
1247         info->cmd_ctrl.max_len = 0;
1248
1249         for (i = 0; i < CMD_RING_ENTRIES; i++)
1250                 writel(0, &regs->CmdRng[i]);
1251
1252         writel(0, &regs->CmdPrd);
1253         writel(0, &regs->CmdCsm);
1254
1255         tmp_ptr = ap->info_dma;
1256         tmp_ptr += (unsigned long) &(((struct ace_info *)0)->s.stats);
1257         set_aceaddr(&info->stats2_ptr, (dma_addr_t) tmp_ptr);
1258
1259         set_aceaddr(&info->rx_std_ctrl.rngptr, ap->rx_ring_base_dma);
1260         info->rx_std_ctrl.max_len = ACE_STD_BUFSIZE;
1261         info->rx_std_ctrl.flags =
1262           RCB_FLG_TCP_UDP_SUM | RCB_FLG_NO_PSEUDO_HDR | ACE_RCB_VLAN_FLAG;
1263
1264         memset(ap->rx_std_ring, 0,
1265                RX_STD_RING_ENTRIES * sizeof(struct rx_desc));
1266
1267         for (i = 0; i < RX_STD_RING_ENTRIES; i++)
1268                 ap->rx_std_ring[i].flags = BD_FLG_TCP_UDP_SUM;
1269
1270         ap->rx_std_skbprd = 0;
1271         atomic_set(&ap->cur_rx_bufs, 0);
1272
1273         set_aceaddr(&info->rx_jumbo_ctrl.rngptr,
1274                     (ap->rx_ring_base_dma +
1275                      (sizeof(struct rx_desc) * RX_STD_RING_ENTRIES)));
1276         info->rx_jumbo_ctrl.max_len = 0;
1277         info->rx_jumbo_ctrl.flags =
1278           RCB_FLG_TCP_UDP_SUM | RCB_FLG_NO_PSEUDO_HDR | ACE_RCB_VLAN_FLAG;
1279
1280         memset(ap->rx_jumbo_ring, 0,
1281                RX_JUMBO_RING_ENTRIES * sizeof(struct rx_desc));
1282
1283         for (i = 0; i < RX_JUMBO_RING_ENTRIES; i++)
1284                 ap->rx_jumbo_ring[i].flags = BD_FLG_TCP_UDP_SUM | BD_FLG_JUMBO;
1285
1286         ap->rx_jumbo_skbprd = 0;
1287         atomic_set(&ap->cur_jumbo_bufs, 0);
1288
1289         memset(ap->rx_mini_ring, 0,
1290                RX_MINI_RING_ENTRIES * sizeof(struct rx_desc));
1291
1292         if (ap->version >= 2) {
1293                 set_aceaddr(&info->rx_mini_ctrl.rngptr,
1294                             (ap->rx_ring_base_dma +
1295                              (sizeof(struct rx_desc) *
1296                               (RX_STD_RING_ENTRIES +
1297                                RX_JUMBO_RING_ENTRIES))));
1298                 info->rx_mini_ctrl.max_len = ACE_MINI_SIZE;
1299                 info->rx_mini_ctrl.flags = 
1300                   RCB_FLG_TCP_UDP_SUM|RCB_FLG_NO_PSEUDO_HDR|ACE_RCB_VLAN_FLAG;
1301
1302                 for (i = 0; i < RX_MINI_RING_ENTRIES; i++)
1303                         ap->rx_mini_ring[i].flags =
1304                                 BD_FLG_TCP_UDP_SUM | BD_FLG_MINI;
1305         } else {
1306                 set_aceaddr(&info->rx_mini_ctrl.rngptr, 0);
1307                 info->rx_mini_ctrl.flags = RCB_FLG_RNG_DISABLE;
1308                 info->rx_mini_ctrl.max_len = 0;
1309         }
1310
1311         ap->rx_mini_skbprd = 0;
1312         atomic_set(&ap->cur_mini_bufs, 0);
1313
1314         set_aceaddr(&info->rx_return_ctrl.rngptr,
1315                     (ap->rx_ring_base_dma +
1316                      (sizeof(struct rx_desc) *
1317                       (RX_STD_RING_ENTRIES +
1318                        RX_JUMBO_RING_ENTRIES +
1319                        RX_MINI_RING_ENTRIES))));
1320         info->rx_return_ctrl.flags = 0;
1321         info->rx_return_ctrl.max_len = RX_RETURN_RING_ENTRIES;
1322
1323         memset(ap->rx_return_ring, 0,
1324                RX_RETURN_RING_ENTRIES * sizeof(struct rx_desc));
1325
1326         set_aceaddr(&info->rx_ret_prd_ptr, ap->rx_ret_prd_dma);
1327         *(ap->rx_ret_prd) = 0;
1328
1329         writel(TX_RING_BASE, &regs->WinBase);
1330
1331         if (ACE_IS_TIGON_I(ap)) {
1332                 ap->tx_ring = (struct tx_desc *)regs->Window;
1333                 for (i = 0; i < (TIGON_I_TX_RING_ENTRIES * 
1334                                  sizeof(struct tx_desc) / 4); i++) {
1335                         writel(0, (unsigned long)ap->tx_ring + i * 4);
1336                 }
1337
1338                 set_aceaddr(&info->tx_ctrl.rngptr, TX_RING_BASE);
1339         } else {
1340                 memset(ap->tx_ring, 0,
1341                        MAX_TX_RING_ENTRIES * sizeof(struct tx_desc));
1342
1343                 set_aceaddr(&info->tx_ctrl.rngptr, ap->tx_ring_dma);
1344         }
1345
1346         info->tx_ctrl.max_len = ACE_TX_RING_ENTRIES(ap);
1347         tmp = RCB_FLG_TCP_UDP_SUM | RCB_FLG_NO_PSEUDO_HDR | ACE_RCB_VLAN_FLAG;
1348
1349         /*
1350          * The Tigon I does not like having the TX ring in host memory ;-(
1351          */
1352         if (!ACE_IS_TIGON_I(ap))
1353                 tmp |= RCB_FLG_TX_HOST_RING;
1354 #if TX_COAL_INTS_ONLY
1355         tmp |= RCB_FLG_COAL_INT_ONLY;
1356 #endif
1357         info->tx_ctrl.flags = tmp;
1358
1359         set_aceaddr(&info->tx_csm_ptr, ap->tx_csm_dma);
1360
1361         /*
1362          * Potential item for tuning parameter
1363          */
1364 #if 0 /* NO */
1365         writel(DMA_THRESH_16W, &regs->DmaReadCfg);
1366         writel(DMA_THRESH_16W, &regs->DmaWriteCfg);
1367 #else
1368         writel(DMA_THRESH_8W, &regs->DmaReadCfg);
1369         writel(DMA_THRESH_8W, &regs->DmaWriteCfg);
1370 #endif
1371
1372         writel(0, &regs->MaskInt);
1373         writel(1, &regs->IfIdx);
1374 #if 0
1375         /*
1376          * McKinley boxes do not like us fiddling with AssistState
1377          * this early
1378          */
1379         writel(1, &regs->AssistState);
1380 #endif
1381
1382         writel(DEF_STAT, &regs->TuneStatTicks);
1383         writel(DEF_TRACE, &regs->TuneTrace);
1384
1385         ace_set_rxtx_parms(dev, 0);
1386
1387         if (board_idx == BOARD_IDX_OVERFLOW) {
1388                 printk(KERN_WARNING "%s: more than %i NICs detected, "
1389                        "ignoring module parameters!\n",
1390                        dev->name, ACE_MAX_MOD_PARMS);
1391         } else if (board_idx >= 0) {
1392                 if (tx_coal_tick[board_idx])
1393                         writel(tx_coal_tick[board_idx],
1394                                &regs->TuneTxCoalTicks);
1395                 if (max_tx_desc[board_idx])
1396                         writel(max_tx_desc[board_idx], &regs->TuneMaxTxDesc);
1397
1398                 if (rx_coal_tick[board_idx])
1399                         writel(rx_coal_tick[board_idx],
1400                                &regs->TuneRxCoalTicks);
1401                 if (max_rx_desc[board_idx])
1402                         writel(max_rx_desc[board_idx], &regs->TuneMaxRxDesc);
1403
1404                 if (trace[board_idx])
1405                         writel(trace[board_idx], &regs->TuneTrace);
1406
1407                 if ((tx_ratio[board_idx] > 0) && (tx_ratio[board_idx] < 64))
1408                         writel(tx_ratio[board_idx], &regs->TxBufRat);
1409         }
1410
1411         /*
1412          * Default link parameters
1413          */
1414         tmp = LNK_ENABLE | LNK_FULL_DUPLEX | LNK_1000MB | LNK_100MB |
1415                 LNK_10MB | LNK_RX_FLOW_CTL_Y | LNK_NEG_FCTL | LNK_NEGOTIATE;
1416         if(ap->version >= 2)
1417                 tmp |= LNK_TX_FLOW_CTL_Y;
1418
1419         /*
1420          * Override link default parameters
1421          */
1422         if ((board_idx >= 0) && link[board_idx]) {
1423                 int option = link[board_idx];
1424
1425                 tmp = LNK_ENABLE;
1426
1427                 if (option & 0x01) {
1428                         printk(KERN_INFO "%s: Setting half duplex link\n",
1429                                dev->name);
1430                         tmp &= ~LNK_FULL_DUPLEX;
1431                 }
1432                 if (option & 0x02)
1433                         tmp &= ~LNK_NEGOTIATE;
1434                 if (option & 0x10)
1435                         tmp |= LNK_10MB;
1436                 if (option & 0x20)
1437                         tmp |= LNK_100MB;
1438                 if (option & 0x40)
1439                         tmp |= LNK_1000MB;
1440                 if ((option & 0x70) == 0) {
1441                         printk(KERN_WARNING "%s: No media speed specified, "
1442                                "forcing auto negotiation\n", dev->name);
1443                         tmp |= LNK_NEGOTIATE | LNK_1000MB |
1444                                 LNK_100MB | LNK_10MB;
1445                 }
1446                 if ((option & 0x100) == 0)
1447                         tmp |= LNK_NEG_FCTL;
1448                 else
1449                         printk(KERN_INFO "%s: Disabling flow control "
1450                                "negotiation\n", dev->name);
1451                 if (option & 0x200)
1452                         tmp |= LNK_RX_FLOW_CTL_Y;
1453                 if ((option & 0x400) && (ap->version >= 2)) {
1454                         printk(KERN_INFO "%s: Enabling TX flow control\n",
1455                                dev->name);
1456                         tmp |= LNK_TX_FLOW_CTL_Y;
1457                 }
1458         }
1459
1460         ap->link = tmp;
1461         writel(tmp, &regs->TuneLink);
1462         if (ap->version >= 2)
1463                 writel(tmp, &regs->TuneFastLink);
1464
1465         if (ACE_IS_TIGON_I(ap))
1466                 writel(tigonFwStartAddr, &regs->Pc);
1467         if (ap->version == 2)
1468                 writel(tigon2FwStartAddr, &regs->Pc);
1469
1470         writel(0, &regs->Mb0Lo);
1471
1472         /*
1473          * Set tx_csm before we start receiving interrupts, otherwise
1474          * the interrupt handler might think it is supposed to process
1475          * tx ints before we are up and running, which may cause a null
1476          * pointer access in the int handler.
1477          */
1478         ap->cur_rx = 0;
1479         ap->tx_prd = *(ap->tx_csm) = ap->tx_ret_csm = 0;
1480
1481         wmb();
1482         ace_set_txprd(regs, ap, 0);
1483         writel(0, &regs->RxRetCsm);
1484
1485         /*
1486          * Zero the stats before starting the interface
1487          */
1488         memset(&ap->stats, 0, sizeof(ap->stats));
1489
1490        /*
1491         * Enable DMA engine now.
1492         * If we do this sooner, Mckinley box pukes.
1493         * I assume it's because Tigon II DMA engine wants to check
1494         * *something* even before the CPU is started.
1495         */
1496        writel(1, &regs->AssistState);  /* enable DMA */
1497
1498         /*
1499          * Start the NIC CPU
1500          */
1501         writel(readl(&regs->CpuCtrl) & ~(CPU_HALT|CPU_TRACE), &regs->CpuCtrl);
1502         readl(&regs->CpuCtrl);
1503
1504         /*
1505          * Wait for the firmware to spin up - max 3 seconds.
1506          */
1507         myjif = jiffies + 3 * HZ;
1508         while (time_before(jiffies, myjif) && !ap->fw_running)
1509                 cpu_relax();
1510
1511         if (!ap->fw_running) {
1512                 printk(KERN_ERR "%s: Firmware NOT running!\n", dev->name);
1513
1514                 ace_dump_trace(ap);
1515                 writel(readl(&regs->CpuCtrl) | CPU_HALT, &regs->CpuCtrl);
1516                 readl(&regs->CpuCtrl);
1517
1518                 /* aman@sgi.com - account for badly behaving firmware/NIC:
1519                  * - have observed that the NIC may continue to generate
1520                  *   interrupts for some reason; attempt to stop it - halt
1521                  *   second CPU for Tigon II cards, and also clear Mb0
1522                  * - if we're a module, we'll fail to load if this was
1523                  *   the only GbE card in the system => if the kernel does
1524                  *   see an interrupt from the NIC, code to handle it is
1525                  *   gone and OOps! - so free_irq also
1526                  */
1527                 if (ap->version >= 2)
1528                         writel(readl(&regs->CpuBCtrl) | CPU_HALT,
1529                                &regs->CpuBCtrl);
1530                 writel(0, &regs->Mb0Lo);
1531                 readl(&regs->Mb0Lo);
1532
1533                 ecode = -EBUSY;
1534                 goto init_error;
1535         }
1536
1537         /*
1538          * We load the ring here as there seem to be no way to tell the
1539          * firmware to wipe the ring without re-initializing it.
1540          */
1541         if (!test_and_set_bit(0, &ap->std_refill_busy))
1542                 ace_load_std_rx_ring(ap, RX_RING_SIZE);
1543         else
1544                 printk(KERN_ERR "%s: Someone is busy refilling the RX ring\n",
1545                        dev->name);
1546         if (ap->version >= 2) {
1547                 if (!test_and_set_bit(0, &ap->mini_refill_busy))
1548                         ace_load_mini_rx_ring(ap, RX_MINI_SIZE);
1549                 else
1550                         printk(KERN_ERR "%s: Someone is busy refilling "
1551                                "the RX mini ring\n", dev->name);
1552         }
1553         return 0;
1554
1555  init_error:
1556         ace_init_cleanup(dev);
1557         return ecode;
1558 }
1559
1560
1561 static void ace_set_rxtx_parms(struct net_device *dev, int jumbo)
1562 {
1563         struct ace_private *ap;
1564         struct ace_regs *regs;
1565         int board_idx;
1566
1567         ap = dev->priv;
1568         regs = ap->regs;
1569
1570         board_idx = ap->board_idx;
1571
1572         if (board_idx >= 0) {
1573                 if (!jumbo) {
1574                         if (!tx_coal_tick[board_idx])
1575                                 writel(DEF_TX_COAL, &regs->TuneTxCoalTicks);
1576                         if (!max_tx_desc[board_idx])
1577                                 writel(DEF_TX_MAX_DESC, &regs->TuneMaxTxDesc);
1578                         if (!rx_coal_tick[board_idx])
1579                                 writel(DEF_RX_COAL, &regs->TuneRxCoalTicks);
1580                         if (!max_rx_desc[board_idx])
1581                                 writel(DEF_RX_MAX_DESC, &regs->TuneMaxRxDesc);
1582                         if (!tx_ratio[board_idx])
1583                                 writel(DEF_TX_RATIO, &regs->TxBufRat);
1584                 } else {
1585                         if (!tx_coal_tick[board_idx])
1586                                 writel(DEF_JUMBO_TX_COAL,
1587                                        &regs->TuneTxCoalTicks);
1588                         if (!max_tx_desc[board_idx])
1589                                 writel(DEF_JUMBO_TX_MAX_DESC,
1590                                        &regs->TuneMaxTxDesc);
1591                         if (!rx_coal_tick[board_idx])
1592                                 writel(DEF_JUMBO_RX_COAL,
1593                                        &regs->TuneRxCoalTicks);
1594                         if (!max_rx_desc[board_idx])
1595                                 writel(DEF_JUMBO_RX_MAX_DESC,
1596                                        &regs->TuneMaxRxDesc);
1597                         if (!tx_ratio[board_idx])
1598                                 writel(DEF_JUMBO_TX_RATIO, &regs->TxBufRat);
1599                 }
1600         }
1601 }
1602
1603
1604 static void ace_watchdog(struct net_device *data)
1605 {
1606         struct net_device *dev = data;
1607         struct ace_private *ap = dev->priv;
1608         struct ace_regs *regs = ap->regs;
1609
1610         /*
1611          * We haven't received a stats update event for more than 2.5
1612          * seconds and there is data in the transmit queue, thus we
1613          * asume the card is stuck.
1614          */
1615         if (*ap->tx_csm != ap->tx_ret_csm) {
1616                 printk(KERN_WARNING "%s: Transmitter is stuck, %08x\n",
1617                        dev->name, (unsigned int)readl(&regs->HostCtrl));
1618                 /* This can happen due to ieee flow control. */
1619         } else {
1620                 printk(KERN_DEBUG "%s: BUG... transmitter died. Kicking it.\n",
1621                        dev->name);
1622 #if 0
1623                 netif_wake_queue(dev);
1624 #endif
1625         }
1626 }
1627
1628
1629 static void ace_tasklet(unsigned long dev)
1630 {
1631         struct ace_private *ap = ((struct net_device *)dev)->priv;
1632         int cur_size;
1633
1634         cur_size = atomic_read(&ap->cur_rx_bufs);
1635         if ((cur_size < RX_LOW_STD_THRES) &&
1636             !test_and_set_bit(0, &ap->std_refill_busy)) {
1637 #ifdef DEBUG
1638                 printk("refilling buffers (current %i)\n", cur_size);
1639 #endif
1640                 ace_load_std_rx_ring(ap, RX_RING_SIZE - cur_size);
1641         }
1642
1643         if (ap->version >= 2) {
1644                 cur_size = atomic_read(&ap->cur_mini_bufs);
1645                 if ((cur_size < RX_LOW_MINI_THRES) &&
1646                     !test_and_set_bit(0, &ap->mini_refill_busy)) {
1647 #ifdef DEBUG
1648                         printk("refilling mini buffers (current %i)\n",
1649                                cur_size);
1650 #endif
1651                         ace_load_mini_rx_ring(ap, RX_MINI_SIZE - cur_size);
1652                 }
1653         }
1654
1655         cur_size = atomic_read(&ap->cur_jumbo_bufs);
1656         if (ap->jumbo && (cur_size < RX_LOW_JUMBO_THRES) &&
1657             !test_and_set_bit(0, &ap->jumbo_refill_busy)) {
1658 #ifdef DEBUG
1659                 printk("refilling jumbo buffers (current %i)\n", cur_size);
1660 #endif
1661                 ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE - cur_size);
1662         }
1663         ap->tasklet_pending = 0;
1664 }
1665
1666
1667 /*
1668  * Copy the contents of the NIC's trace buffer to kernel memory.
1669  */
1670 static void ace_dump_trace(struct ace_private *ap)
1671 {
1672 #if 0
1673         if (!ap->trace_buf)
1674                 if (!(ap->trace_buf = kmalloc(ACE_TRACE_SIZE, GFP_KERNEL)))
1675                     return;
1676 #endif
1677 }
1678
1679
1680 /*
1681  * Load the standard rx ring.
1682  *
1683  * Loading rings is safe without holding the spin lock since this is
1684  * done only before the device is enabled, thus no interrupts are
1685  * generated and by the interrupt handler/tasklet handler.
1686  */
1687 static void ace_load_std_rx_ring(struct ace_private *ap, int nr_bufs)
1688 {
1689         struct ace_regs *regs;
1690         short i, idx;
1691
1692         regs = ap->regs;
1693
1694         prefetchw(&ap->cur_rx_bufs);
1695
1696         idx = ap->rx_std_skbprd;
1697
1698         for (i = 0; i < nr_bufs; i++) {
1699                 struct sk_buff *skb;
1700                 struct rx_desc *rd;
1701                 dma_addr_t mapping;
1702
1703                 skb = alloc_skb(ACE_STD_BUFSIZE + NET_IP_ALIGN, GFP_ATOMIC);
1704                 if (!skb)
1705                         break;
1706
1707                 skb_reserve(skb, NET_IP_ALIGN);
1708                 mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
1709                                        offset_in_page(skb->data),
1710                                        ACE_STD_BUFSIZE,
1711                                        PCI_DMA_FROMDEVICE);
1712                 ap->skb->rx_std_skbuff[idx].skb = skb;
1713                 pci_unmap_addr_set(&ap->skb->rx_std_skbuff[idx],
1714                                    mapping, mapping);
1715
1716                 rd = &ap->rx_std_ring[idx];
1717                 set_aceaddr(&rd->addr, mapping);
1718                 rd->size = ACE_STD_BUFSIZE;
1719                 rd->idx = idx;
1720                 idx = (idx + 1) % RX_STD_RING_ENTRIES;
1721         }
1722
1723         if (!i)
1724                 goto error_out;
1725
1726         atomic_add(i, &ap->cur_rx_bufs);
1727         ap->rx_std_skbprd = idx;
1728
1729         if (ACE_IS_TIGON_I(ap)) {
1730                 struct cmd cmd;
1731                 cmd.evt = C_SET_RX_PRD_IDX;
1732                 cmd.code = 0;
1733                 cmd.idx = ap->rx_std_skbprd;
1734                 ace_issue_cmd(regs, &cmd);
1735         } else {
1736                 writel(idx, &regs->RxStdPrd);
1737                 wmb();
1738         }
1739
1740  out:
1741         clear_bit(0, &ap->std_refill_busy);
1742         return;
1743
1744  error_out:
1745         printk(KERN_INFO "Out of memory when allocating "
1746                "standard receive buffers\n");
1747         goto out;
1748 }
1749
1750
1751 static void ace_load_mini_rx_ring(struct ace_private *ap, int nr_bufs)
1752 {
1753         struct ace_regs *regs;
1754         short i, idx;
1755
1756         regs = ap->regs;
1757
1758         prefetchw(&ap->cur_mini_bufs);
1759
1760         idx = ap->rx_mini_skbprd;
1761         for (i = 0; i < nr_bufs; i++) {
1762                 struct sk_buff *skb;
1763                 struct rx_desc *rd;
1764                 dma_addr_t mapping;
1765
1766                 skb = alloc_skb(ACE_MINI_BUFSIZE + NET_IP_ALIGN, GFP_ATOMIC);
1767                 if (!skb)
1768                         break;
1769
1770                 skb_reserve(skb, NET_IP_ALIGN);
1771                 mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
1772                                        offset_in_page(skb->data),
1773                                        ACE_MINI_BUFSIZE,
1774                                        PCI_DMA_FROMDEVICE);
1775                 ap->skb->rx_mini_skbuff[idx].skb = skb;
1776                 pci_unmap_addr_set(&ap->skb->rx_mini_skbuff[idx],
1777                                    mapping, mapping);
1778
1779                 rd = &ap->rx_mini_ring[idx];
1780                 set_aceaddr(&rd->addr, mapping);
1781                 rd->size = ACE_MINI_BUFSIZE;
1782                 rd->idx = idx;
1783                 idx = (idx + 1) % RX_MINI_RING_ENTRIES;
1784         }
1785
1786         if (!i)
1787                 goto error_out;
1788
1789         atomic_add(i, &ap->cur_mini_bufs);
1790
1791         ap->rx_mini_skbprd = idx;
1792
1793         writel(idx, &regs->RxMiniPrd);
1794         wmb();
1795
1796  out:
1797         clear_bit(0, &ap->mini_refill_busy);
1798         return;
1799  error_out:
1800         printk(KERN_INFO "Out of memory when allocating "
1801                "mini receive buffers\n");
1802         goto out;
1803 }
1804
1805
1806 /*
1807  * Load the jumbo rx ring, this may happen at any time if the MTU
1808  * is changed to a value > 1500.
1809  */
1810 static void ace_load_jumbo_rx_ring(struct ace_private *ap, int nr_bufs)
1811 {
1812         struct ace_regs *regs;
1813         short i, idx;
1814
1815         regs = ap->regs;
1816
1817         idx = ap->rx_jumbo_skbprd;
1818
1819         for (i = 0; i < nr_bufs; i++) {
1820                 struct sk_buff *skb;
1821                 struct rx_desc *rd;
1822                 dma_addr_t mapping;
1823
1824                 skb = alloc_skb(ACE_JUMBO_BUFSIZE + NET_IP_ALIGN, GFP_ATOMIC);
1825                 if (!skb)
1826                         break;
1827
1828                 skb_reserve(skb, NET_IP_ALIGN);
1829                 mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
1830                                        offset_in_page(skb->data),
1831                                        ACE_JUMBO_BUFSIZE,
1832                                        PCI_DMA_FROMDEVICE);
1833                 ap->skb->rx_jumbo_skbuff[idx].skb = skb;
1834                 pci_unmap_addr_set(&ap->skb->rx_jumbo_skbuff[idx],
1835                                    mapping, mapping);
1836
1837                 rd = &ap->rx_jumbo_ring[idx];
1838                 set_aceaddr(&rd->addr, mapping);
1839                 rd->size = ACE_JUMBO_BUFSIZE;
1840                 rd->idx = idx;
1841                 idx = (idx + 1) % RX_JUMBO_RING_ENTRIES;
1842         }
1843
1844         if (!i)
1845                 goto error_out;
1846
1847         atomic_add(i, &ap->cur_jumbo_bufs);
1848         ap->rx_jumbo_skbprd = idx;
1849
1850         if (ACE_IS_TIGON_I(ap)) {
1851                 struct cmd cmd;
1852                 cmd.evt = C_SET_RX_JUMBO_PRD_IDX;
1853                 cmd.code = 0;
1854                 cmd.idx = ap->rx_jumbo_skbprd;
1855                 ace_issue_cmd(regs, &cmd);
1856         } else {
1857                 writel(idx, &regs->RxJumboPrd);
1858                 wmb();
1859         }
1860
1861  out:
1862         clear_bit(0, &ap->jumbo_refill_busy);
1863         return;
1864  error_out:
1865         if (net_ratelimit())
1866                 printk(KERN_INFO "Out of memory when allocating "
1867                        "jumbo receive buffers\n");
1868         goto out;
1869 }
1870
1871
1872 /*
1873  * All events are considered to be slow (RX/TX ints do not generate
1874  * events) and are handled here, outside the main interrupt handler,
1875  * to reduce the size of the handler.
1876  */
1877 static u32 ace_handle_event(struct net_device *dev, u32 evtcsm, u32 evtprd)
1878 {
1879         struct ace_private *ap;
1880
1881         ap = dev->priv;
1882
1883         while (evtcsm != evtprd) {
1884                 switch (ap->evt_ring[evtcsm].evt) {
1885                 case E_FW_RUNNING:
1886                         printk(KERN_INFO "%s: Firmware up and running\n",
1887                                dev->name);
1888                         ap->fw_running = 1;
1889                         wmb();
1890                         break;
1891                 case E_STATS_UPDATED:
1892                         break;
1893                 case E_LNK_STATE:
1894                 {
1895                         u16 code = ap->evt_ring[evtcsm].code;
1896                         switch (code) {
1897                         case E_C_LINK_UP:
1898                         {
1899                                 u32 state = readl(&ap->regs->GigLnkState);
1900                                 printk(KERN_WARNING "%s: Optical link UP "
1901                                        "(%s Duplex, Flow Control: %s%s)\n",
1902                                        dev->name,
1903                                        state & LNK_FULL_DUPLEX ? "Full":"Half",
1904                                        state & LNK_TX_FLOW_CTL_Y ? "TX " : "",
1905                                        state & LNK_RX_FLOW_CTL_Y ? "RX" : "");
1906                                 break;
1907                         }
1908                         case E_C_LINK_DOWN:
1909                                 printk(KERN_WARNING "%s: Optical link DOWN\n",
1910                                        dev->name);
1911                                 break;
1912                         case E_C_LINK_10_100:
1913                                 printk(KERN_WARNING "%s: 10/100BaseT link "
1914                                        "UP\n", dev->name);
1915                                 break;
1916                         default:
1917                                 printk(KERN_ERR "%s: Unknown optical link "
1918                                        "state %02x\n", dev->name, code);
1919                         }
1920                         break;
1921                 }
1922                 case E_ERROR:
1923                         switch(ap->evt_ring[evtcsm].code) {
1924                         case E_C_ERR_INVAL_CMD:
1925                                 printk(KERN_ERR "%s: invalid command error\n",
1926                                        dev->name);
1927                                 break;
1928                         case E_C_ERR_UNIMP_CMD:
1929                                 printk(KERN_ERR "%s: unimplemented command "
1930                                        "error\n", dev->name);
1931                                 break;
1932                         case E_C_ERR_BAD_CFG:
1933                                 printk(KERN_ERR "%s: bad config error\n",
1934                                        dev->name);
1935                                 break;
1936                         default:
1937                                 printk(KERN_ERR "%s: unknown error %02x\n",
1938                                        dev->name, ap->evt_ring[evtcsm].code);
1939                         }
1940                         break;
1941                 case E_RESET_JUMBO_RNG:
1942                 {
1943                         int i;
1944                         for (i = 0; i < RX_JUMBO_RING_ENTRIES; i++) {
1945                                 if (ap->skb->rx_jumbo_skbuff[i].skb) {
1946                                         ap->rx_jumbo_ring[i].size = 0;
1947                                         set_aceaddr(&ap->rx_jumbo_ring[i].addr, 0);
1948                                         dev_kfree_skb(ap->skb->rx_jumbo_skbuff[i].skb);
1949                                         ap->skb->rx_jumbo_skbuff[i].skb = NULL;
1950                                 }
1951                         }
1952
1953                         if (ACE_IS_TIGON_I(ap)) {
1954                                 struct cmd cmd;
1955                                 cmd.evt = C_SET_RX_JUMBO_PRD_IDX;
1956                                 cmd.code = 0;
1957                                 cmd.idx = 0;
1958                                 ace_issue_cmd(ap->regs, &cmd);
1959                         } else {
1960                                 writel(0, &((ap->regs)->RxJumboPrd));
1961                                 wmb();
1962                         }
1963
1964                         ap->jumbo = 0;
1965                         ap->rx_jumbo_skbprd = 0;
1966                         printk(KERN_INFO "%s: Jumbo ring flushed\n",
1967                                dev->name);
1968                         clear_bit(0, &ap->jumbo_refill_busy);
1969                         break;
1970                 }
1971                 default:
1972                         printk(KERN_ERR "%s: Unhandled event 0x%02x\n",
1973                                dev->name, ap->evt_ring[evtcsm].evt);
1974                 }
1975                 evtcsm = (evtcsm + 1) % EVT_RING_ENTRIES;
1976         }
1977
1978         return evtcsm;
1979 }
1980
1981
1982 static void ace_rx_int(struct net_device *dev, u32 rxretprd, u32 rxretcsm)
1983 {
1984         struct ace_private *ap = dev->priv;
1985         u32 idx;
1986         int mini_count = 0, std_count = 0;
1987
1988         idx = rxretcsm;
1989
1990         prefetchw(&ap->cur_rx_bufs);
1991         prefetchw(&ap->cur_mini_bufs);
1992         
1993         while (idx != rxretprd) {
1994                 struct ring_info *rip;
1995                 struct sk_buff *skb;
1996                 struct rx_desc *rxdesc, *retdesc;
1997                 u32 skbidx;
1998                 int bd_flags, desc_type, mapsize;
1999                 u16 csum;
2000
2001
2002                 /* make sure the rx descriptor isn't read before rxretprd */
2003                 if (idx == rxretcsm) 
2004                         rmb();
2005
2006                 retdesc = &ap->rx_return_ring[idx];
2007                 skbidx = retdesc->idx;
2008                 bd_flags = retdesc->flags;
2009                 desc_type = bd_flags & (BD_FLG_JUMBO | BD_FLG_MINI);
2010
2011                 switch(desc_type) {
2012                         /*
2013                          * Normal frames do not have any flags set
2014                          *
2015                          * Mini and normal frames arrive frequently,
2016                          * so use a local counter to avoid doing
2017                          * atomic operations for each packet arriving.
2018                          */
2019                 case 0:
2020                         rip = &ap->skb->rx_std_skbuff[skbidx];
2021                         mapsize = ACE_STD_BUFSIZE;
2022                         rxdesc = &ap->rx_std_ring[skbidx];
2023                         std_count++;
2024                         break;
2025                 case BD_FLG_JUMBO:
2026                         rip = &ap->skb->rx_jumbo_skbuff[skbidx];
2027                         mapsize = ACE_JUMBO_BUFSIZE;
2028                         rxdesc = &ap->rx_jumbo_ring[skbidx];
2029                         atomic_dec(&ap->cur_jumbo_bufs);
2030                         break;
2031                 case BD_FLG_MINI:
2032                         rip = &ap->skb->rx_mini_skbuff[skbidx];
2033                         mapsize = ACE_MINI_BUFSIZE;
2034                         rxdesc = &ap->rx_mini_ring[skbidx];
2035                         mini_count++; 
2036                         break;
2037                 default:
2038                         printk(KERN_INFO "%s: unknown frame type (0x%02x) "
2039                                "returned by NIC\n", dev->name,
2040                                retdesc->flags);
2041                         goto error;
2042                 }
2043
2044                 skb = rip->skb;
2045                 rip->skb = NULL;
2046                 pci_unmap_page(ap->pdev,
2047                                pci_unmap_addr(rip, mapping),
2048                                mapsize,
2049                                PCI_DMA_FROMDEVICE);
2050                 skb_put(skb, retdesc->size);
2051
2052                 /*
2053                  * Fly baby, fly!
2054                  */
2055                 csum = retdesc->tcp_udp_csum;
2056
2057                 skb->dev = dev;
2058                 skb->protocol = eth_type_trans(skb, dev);
2059
2060                 /*
2061                  * Instead of forcing the poor tigon mips cpu to calculate
2062                  * pseudo hdr checksum, we do this ourselves.
2063                  */
2064                 if (bd_flags & BD_FLG_TCP_UDP_SUM) {
2065                         skb->csum = htons(csum);
2066                         skb->ip_summed = CHECKSUM_HW;
2067                 } else {
2068                         skb->ip_summed = CHECKSUM_NONE;
2069                 }
2070
2071                 /* send it up */
2072 #if ACENIC_DO_VLAN
2073                 if (ap->vlgrp && (bd_flags & BD_FLG_VLAN_TAG)) {
2074                         vlan_hwaccel_rx(skb, ap->vlgrp, retdesc->vlan);
2075                 } else
2076 #endif
2077                         netif_rx(skb);
2078
2079                 dev->last_rx = jiffies;
2080                 ap->stats.rx_packets++;
2081                 ap->stats.rx_bytes += retdesc->size;
2082
2083                 idx = (idx + 1) % RX_RETURN_RING_ENTRIES;
2084         }
2085
2086         atomic_sub(std_count, &ap->cur_rx_bufs);
2087         if (!ACE_IS_TIGON_I(ap))
2088                 atomic_sub(mini_count, &ap->cur_mini_bufs);
2089
2090  out:
2091         /*
2092          * According to the documentation RxRetCsm is obsolete with
2093          * the 12.3.x Firmware - my Tigon I NICs seem to disagree!
2094          */
2095         if (ACE_IS_TIGON_I(ap)) {
2096                 struct ace_regs *regs = ap->regs;
2097                 writel(idx, &regs->RxRetCsm);
2098         }
2099         ap->cur_rx = idx;
2100
2101         return;
2102  error:
2103         idx = rxretprd;
2104         goto out;
2105 }
2106
2107
2108 static inline void ace_tx_int(struct net_device *dev,
2109                               u32 txcsm, u32 idx)
2110 {
2111         struct ace_private *ap = dev->priv;
2112
2113         do {
2114                 struct sk_buff *skb;
2115                 dma_addr_t mapping;
2116                 struct tx_ring_info *info;
2117
2118                 info = ap->skb->tx_skbuff + idx;
2119                 skb = info->skb;
2120                 mapping = pci_unmap_addr(info, mapping);
2121
2122                 if (mapping) {
2123                         pci_unmap_page(ap->pdev, mapping,
2124                                        pci_unmap_len(info, maplen),
2125                                        PCI_DMA_TODEVICE);
2126                         pci_unmap_addr_set(info, mapping, 0);
2127                 }
2128
2129                 if (skb) {
2130                         ap->stats.tx_packets++;
2131                         ap->stats.tx_bytes += skb->len;
2132                         dev_kfree_skb_irq(skb);
2133                         info->skb = NULL;
2134                 }
2135
2136                 idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
2137         } while (idx != txcsm);
2138
2139         if (netif_queue_stopped(dev))
2140                 netif_wake_queue(dev);
2141
2142         wmb();
2143         ap->tx_ret_csm = txcsm;
2144
2145         /* So... tx_ret_csm is advanced _after_ check for device wakeup.
2146          *
2147          * We could try to make it before. In this case we would get
2148          * the following race condition: hard_start_xmit on other cpu
2149          * enters after we advanced tx_ret_csm and fills space,
2150          * which we have just freed, so that we make illegal device wakeup.
2151          * There is no good way to workaround this (at entry
2152          * to ace_start_xmit detects this condition and prevents
2153          * ring corruption, but it is not a good workaround.)
2154          *
2155          * When tx_ret_csm is advanced after, we wake up device _only_
2156          * if we really have some space in ring (though the core doing
2157          * hard_start_xmit can see full ring for some period and has to
2158          * synchronize.) Superb.
2159          * BUT! We get another subtle race condition. hard_start_xmit
2160          * may think that ring is full between wakeup and advancing
2161          * tx_ret_csm and will stop device instantly! It is not so bad.
2162          * We are guaranteed that there is something in ring, so that
2163          * the next irq will resume transmission. To speedup this we could
2164          * mark descriptor, which closes ring with BD_FLG_COAL_NOW
2165          * (see ace_start_xmit).
2166          *
2167          * Well, this dilemma exists in all lock-free devices.
2168          * We, following scheme used in drivers by Donald Becker,
2169          * select the least dangerous.
2170          *                                                      --ANK
2171          */
2172 }
2173
2174
2175 static irqreturn_t ace_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
2176 {
2177         struct ace_private *ap;
2178         struct ace_regs *regs;
2179         struct net_device *dev = (struct net_device *)dev_id;
2180         u32 idx;
2181         u32 txcsm, rxretcsm, rxretprd;
2182         u32 evtcsm, evtprd;
2183
2184         ap = dev->priv;
2185         regs = ap->regs;
2186
2187         /*
2188          * In case of PCI shared interrupts or spurious interrupts,
2189          * we want to make sure it is actually our interrupt before
2190          * spending any time in here.
2191          */
2192         if (!(readl(&regs->HostCtrl) & IN_INT))
2193                 return IRQ_NONE;
2194
2195         /*
2196          * ACK intr now. Otherwise we will lose updates to rx_ret_prd,
2197          * which happened _after_ rxretprd = *ap->rx_ret_prd; but before
2198          * writel(0, &regs->Mb0Lo).
2199          *
2200          * "IRQ avoidance" recommended in docs applies to IRQs served
2201          * threads and it is wrong even for that case.
2202          */
2203         writel(0, &regs->Mb0Lo);
2204         readl(&regs->Mb0Lo);
2205
2206         /*
2207          * There is no conflict between transmit handling in
2208          * start_xmit and receive processing, thus there is no reason
2209          * to take a spin lock for RX handling. Wait until we start
2210          * working on the other stuff - hey we don't need a spin lock
2211          * anymore.
2212          */
2213         rxretprd = *ap->rx_ret_prd;
2214         rxretcsm = ap->cur_rx;
2215
2216         if (rxretprd != rxretcsm)
2217                 ace_rx_int(dev, rxretprd, rxretcsm);
2218
2219         txcsm = *ap->tx_csm;
2220         idx = ap->tx_ret_csm;
2221
2222         if (txcsm != idx) {
2223                 /*
2224                  * If each skb takes only one descriptor this check degenerates
2225                  * to identity, because new space has just been opened.
2226                  * But if skbs are fragmented we must check that this index
2227                  * update releases enough of space, otherwise we just
2228                  * wait for device to make more work.
2229                  */
2230                 if (!tx_ring_full(ap, txcsm, ap->tx_prd))
2231                         ace_tx_int(dev, txcsm, idx);
2232         }
2233
2234         evtcsm = readl(&regs->EvtCsm);
2235         evtprd = *ap->evt_prd;
2236
2237         if (evtcsm != evtprd) {
2238                 evtcsm = ace_handle_event(dev, evtcsm, evtprd);
2239                 writel(evtcsm, &regs->EvtCsm);
2240         }
2241
2242         /*
2243          * This has to go last in the interrupt handler and run with
2244          * the spin lock released ... what lock?
2245          */
2246         if (netif_running(dev)) {
2247                 int cur_size;
2248                 int run_tasklet = 0;
2249
2250                 cur_size = atomic_read(&ap->cur_rx_bufs);
2251                 if (cur_size < RX_LOW_STD_THRES) {
2252                         if ((cur_size < RX_PANIC_STD_THRES) &&
2253                             !test_and_set_bit(0, &ap->std_refill_busy)) {
2254 #ifdef DEBUG
2255                                 printk("low on std buffers %i\n", cur_size);
2256 #endif
2257                                 ace_load_std_rx_ring(ap,
2258                                                      RX_RING_SIZE - cur_size);
2259                         } else
2260                                 run_tasklet = 1;
2261                 }
2262
2263                 if (!ACE_IS_TIGON_I(ap)) {
2264                         cur_size = atomic_read(&ap->cur_mini_bufs);
2265                         if (cur_size < RX_LOW_MINI_THRES) {
2266                                 if ((cur_size < RX_PANIC_MINI_THRES) &&
2267                                     !test_and_set_bit(0,
2268                                                       &ap->mini_refill_busy)) {
2269 #ifdef DEBUG
2270                                         printk("low on mini buffers %i\n",
2271                                                cur_size);
2272 #endif
2273                                         ace_load_mini_rx_ring(ap, RX_MINI_SIZE - cur_size);
2274                                 } else
2275                                         run_tasklet = 1;
2276                         }
2277                 }
2278
2279                 if (ap->jumbo) {
2280                         cur_size = atomic_read(&ap->cur_jumbo_bufs);
2281                         if (cur_size < RX_LOW_JUMBO_THRES) {
2282                                 if ((cur_size < RX_PANIC_JUMBO_THRES) &&
2283                                     !test_and_set_bit(0,
2284                                                       &ap->jumbo_refill_busy)){
2285 #ifdef DEBUG
2286                                         printk("low on jumbo buffers %i\n",
2287                                                cur_size);
2288 #endif
2289                                         ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE - cur_size);
2290                                 } else
2291                                         run_tasklet = 1;
2292                         }
2293                 }
2294                 if (run_tasklet && !ap->tasklet_pending) {
2295                         ap->tasklet_pending = 1;
2296                         tasklet_schedule(&ap->ace_tasklet);
2297                 }
2298         }
2299
2300         return IRQ_HANDLED;
2301 }
2302
2303
2304 #if ACENIC_DO_VLAN
2305 static void ace_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
2306 {
2307         struct ace_private *ap = dev->priv;
2308         unsigned long flags;
2309
2310         local_irq_save(flags);
2311         ace_mask_irq(dev);
2312
2313         ap->vlgrp = grp;
2314
2315         ace_unmask_irq(dev);
2316         local_irq_restore(flags);
2317 }
2318
2319
2320 static void ace_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2321 {
2322         struct ace_private *ap = dev->priv;
2323         unsigned long flags;
2324
2325         local_irq_save(flags);
2326         ace_mask_irq(dev);
2327
2328         if (ap->vlgrp)
2329                 ap->vlgrp->vlan_devices[vid] = NULL;
2330
2331         ace_unmask_irq(dev);
2332         local_irq_restore(flags);
2333 }
2334 #endif /* ACENIC_DO_VLAN */
2335
2336
2337 static int ace_open(struct net_device *dev)
2338 {
2339         struct ace_private *ap;
2340         struct ace_regs *regs;
2341         struct cmd cmd;
2342
2343         ap = dev->priv;
2344         regs = ap->regs;
2345
2346         if (!(ap->fw_running)) {
2347                 printk(KERN_WARNING "%s: Firmware not running!\n", dev->name);
2348                 return -EBUSY;
2349         }
2350
2351         writel(dev->mtu + ETH_HLEN + 4, &regs->IfMtu);
2352
2353         cmd.evt = C_CLEAR_STATS;
2354         cmd.code = 0;
2355         cmd.idx = 0;
2356         ace_issue_cmd(regs, &cmd);
2357
2358         cmd.evt = C_HOST_STATE;
2359         cmd.code = C_C_STACK_UP;
2360         cmd.idx = 0;
2361         ace_issue_cmd(regs, &cmd);
2362
2363         if (ap->jumbo &&
2364             !test_and_set_bit(0, &ap->jumbo_refill_busy))
2365                 ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE);
2366
2367         if (dev->flags & IFF_PROMISC) {
2368                 cmd.evt = C_SET_PROMISC_MODE;
2369                 cmd.code = C_C_PROMISC_ENABLE;
2370                 cmd.idx = 0;
2371                 ace_issue_cmd(regs, &cmd);
2372
2373                 ap->promisc = 1;
2374         }else
2375                 ap->promisc = 0;
2376         ap->mcast_all = 0;
2377
2378 #if 0
2379         cmd.evt = C_LNK_NEGOTIATION;
2380         cmd.code = 0;
2381         cmd.idx = 0;
2382         ace_issue_cmd(regs, &cmd);
2383 #endif
2384
2385         netif_start_queue(dev);
2386
2387         /*
2388          * Setup the bottom half rx ring refill handler
2389          */
2390         tasklet_init(&ap->ace_tasklet, ace_tasklet, (unsigned long)dev);
2391         return 0;
2392 }
2393
2394
2395 static int ace_close(struct net_device *dev)
2396 {
2397         struct ace_private *ap;
2398         struct ace_regs *regs;
2399         struct cmd cmd;
2400         unsigned long flags;
2401         short i;
2402
2403         /*
2404          * Without (or before) releasing irq and stopping hardware, this
2405          * is an absolute non-sense, by the way. It will be reset instantly
2406          * by the first irq.
2407          */
2408         netif_stop_queue(dev);
2409
2410         ap = dev->priv;
2411         regs = ap->regs;
2412
2413         if (ap->promisc) {
2414                 cmd.evt = C_SET_PROMISC_MODE;
2415                 cmd.code = C_C_PROMISC_DISABLE;
2416                 cmd.idx = 0;
2417                 ace_issue_cmd(regs, &cmd);
2418                 ap->promisc = 0;
2419         }
2420
2421         cmd.evt = C_HOST_STATE;
2422         cmd.code = C_C_STACK_DOWN;
2423         cmd.idx = 0;
2424         ace_issue_cmd(regs, &cmd);
2425
2426         tasklet_kill(&ap->ace_tasklet);
2427
2428         /*
2429          * Make sure one CPU is not processing packets while
2430          * buffers are being released by another.
2431          */
2432
2433         local_irq_save(flags);
2434         ace_mask_irq(dev);
2435
2436         for (i = 0; i < ACE_TX_RING_ENTRIES(ap); i++) {
2437                 struct sk_buff *skb;
2438                 dma_addr_t mapping;
2439                 struct tx_ring_info *info;
2440
2441                 info = ap->skb->tx_skbuff + i;
2442                 skb = info->skb;
2443                 mapping = pci_unmap_addr(info, mapping);
2444
2445                 if (mapping) {
2446                         if (ACE_IS_TIGON_I(ap)) {
2447                                 writel(0, &ap->tx_ring[i].addr.addrhi);
2448                                 writel(0, &ap->tx_ring[i].addr.addrlo);
2449                                 writel(0, &ap->tx_ring[i].flagsize);
2450                         } else
2451                                 memset(ap->tx_ring + i, 0,
2452                                        sizeof(struct tx_desc));
2453                         pci_unmap_page(ap->pdev, mapping,
2454                                        pci_unmap_len(info, maplen),
2455                                        PCI_DMA_TODEVICE);
2456                         pci_unmap_addr_set(info, mapping, 0);
2457                 }
2458                 if (skb) {
2459                         dev_kfree_skb(skb);
2460                         info->skb = NULL;
2461                 }
2462         }
2463
2464         if (ap->jumbo) {
2465                 cmd.evt = C_RESET_JUMBO_RNG;
2466                 cmd.code = 0;
2467                 cmd.idx = 0;
2468                 ace_issue_cmd(regs, &cmd);
2469         }
2470
2471         ace_unmask_irq(dev);
2472         local_irq_restore(flags);
2473
2474         return 0;
2475 }
2476
2477
2478 static inline dma_addr_t
2479 ace_map_tx_skb(struct ace_private *ap, struct sk_buff *skb,
2480                struct sk_buff *tail, u32 idx)
2481 {
2482         dma_addr_t mapping;
2483         struct tx_ring_info *info;
2484
2485         mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
2486                                offset_in_page(skb->data),
2487                                skb->len, PCI_DMA_TODEVICE);
2488
2489         info = ap->skb->tx_skbuff + idx;
2490         info->skb = tail;
2491         pci_unmap_addr_set(info, mapping, mapping);
2492         pci_unmap_len_set(info, maplen, skb->len);
2493         return mapping;
2494 }
2495
2496
2497 static inline void
2498 ace_load_tx_bd(struct ace_private *ap, struct tx_desc *desc, u64 addr,
2499                u32 flagsize, u32 vlan_tag)
2500 {
2501 #if !USE_TX_COAL_NOW
2502         flagsize &= ~BD_FLG_COAL_NOW;
2503 #endif
2504
2505         if (ACE_IS_TIGON_I(ap)) {
2506                 writel(addr >> 32, &desc->addr.addrhi);
2507                 writel(addr & 0xffffffff, &desc->addr.addrlo);
2508                 writel(flagsize, &desc->flagsize);
2509 #if ACENIC_DO_VLAN
2510                 writel(vlan_tag, &desc->vlanres);
2511 #endif
2512         } else {
2513                 desc->addr.addrhi = addr >> 32;
2514                 desc->addr.addrlo = addr;
2515                 desc->flagsize = flagsize;
2516 #if ACENIC_DO_VLAN
2517                 desc->vlanres = vlan_tag;
2518 #endif
2519         }
2520 }
2521
2522
2523 static int ace_start_xmit(struct sk_buff *skb, struct net_device *dev)
2524 {
2525         struct ace_private *ap = dev->priv;
2526         struct ace_regs *regs = ap->regs;
2527         struct tx_desc *desc;
2528         u32 idx, flagsize;
2529
2530 restart:
2531         idx = ap->tx_prd;
2532
2533         if (tx_ring_full(ap, ap->tx_ret_csm, idx))
2534                 goto overflow;
2535
2536 #if MAX_SKB_FRAGS
2537         if (!skb_shinfo(skb)->nr_frags)
2538 #endif
2539         {
2540                 dma_addr_t mapping;
2541                 u32 vlan_tag = 0;
2542
2543                 mapping = ace_map_tx_skb(ap, skb, skb, idx);
2544                 flagsize = (skb->len << 16) | (BD_FLG_END);
2545                 if (skb->ip_summed == CHECKSUM_HW)
2546                         flagsize |= BD_FLG_TCP_UDP_SUM;
2547 #if ACENIC_DO_VLAN
2548                 if (vlan_tx_tag_present(skb)) {
2549                         flagsize |= BD_FLG_VLAN_TAG;
2550                         vlan_tag = vlan_tx_tag_get(skb);
2551                 }
2552 #endif
2553                 desc = ap->tx_ring + idx;
2554                 idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
2555
2556                 /* Look at ace_tx_int for explanations. */
2557                 if (tx_ring_full(ap, ap->tx_ret_csm, idx))
2558                         flagsize |= BD_FLG_COAL_NOW;
2559
2560                 ace_load_tx_bd(ap, desc, mapping, flagsize, vlan_tag);
2561         }
2562 #if MAX_SKB_FRAGS
2563         else {
2564                 dma_addr_t mapping;
2565                 u32 vlan_tag = 0;
2566                 int i, len = 0;
2567
2568                 mapping = ace_map_tx_skb(ap, skb, NULL, idx);
2569                 flagsize = (skb_headlen(skb) << 16);
2570                 if (skb->ip_summed == CHECKSUM_HW)
2571                         flagsize |= BD_FLG_TCP_UDP_SUM;
2572 #if ACENIC_DO_VLAN
2573                 if (vlan_tx_tag_present(skb)) {
2574                         flagsize |= BD_FLG_VLAN_TAG;
2575                         vlan_tag = vlan_tx_tag_get(skb);
2576                 }
2577 #endif
2578
2579                 ace_load_tx_bd(ap, ap->tx_ring + idx, mapping, flagsize, vlan_tag);
2580
2581                 idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
2582
2583                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2584                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2585                         struct tx_ring_info *info;
2586
2587                         len += frag->size;
2588                         info = ap->skb->tx_skbuff + idx;
2589                         desc = ap->tx_ring + idx;
2590
2591                         mapping = pci_map_page(ap->pdev, frag->page,
2592                                                frag->page_offset, frag->size,
2593                                                PCI_DMA_TODEVICE);
2594
2595                         flagsize = (frag->size << 16);
2596                         if (skb->ip_summed == CHECKSUM_HW)
2597                                 flagsize |= BD_FLG_TCP_UDP_SUM;
2598                         idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
2599
2600                         if (i == skb_shinfo(skb)->nr_frags - 1) {
2601                                 flagsize |= BD_FLG_END;
2602                                 if (tx_ring_full(ap, ap->tx_ret_csm, idx))
2603                                         flagsize |= BD_FLG_COAL_NOW;
2604
2605                                 /*
2606                                  * Only the last fragment frees
2607                                  * the skb!
2608                                  */
2609                                 info->skb = skb;
2610                         } else {
2611                                 info->skb = NULL;
2612                         }
2613                         pci_unmap_addr_set(info, mapping, mapping);
2614                         pci_unmap_len_set(info, maplen, frag->size);
2615                         ace_load_tx_bd(ap, desc, mapping, flagsize, vlan_tag);
2616                 }
2617         }
2618 #endif
2619
2620         wmb();
2621         ap->tx_prd = idx;
2622         ace_set_txprd(regs, ap, idx);
2623
2624         if (flagsize & BD_FLG_COAL_NOW) {
2625                 netif_stop_queue(dev);
2626
2627                 /*
2628                  * A TX-descriptor producer (an IRQ) might have gotten
2629                  * inbetween, making the ring free again. Since xmit is
2630                  * serialized, this is the only situation we have to
2631                  * re-test.
2632                  */
2633                 if (!tx_ring_full(ap, ap->tx_ret_csm, idx))
2634                         netif_wake_queue(dev);
2635         }
2636
2637         dev->trans_start = jiffies;
2638         return 0;
2639
2640 overflow:
2641         /*
2642          * This race condition is unavoidable with lock-free drivers.
2643          * We wake up the queue _before_ tx_prd is advanced, so that we can
2644          * enter hard_start_xmit too early, while tx ring still looks closed.
2645          * This happens ~1-4 times per 100000 packets, so that we can allow
2646          * to loop syncing to other CPU. Probably, we need an additional
2647          * wmb() in ace_tx_intr as well.
2648          *
2649          * Note that this race is relieved by reserving one more entry
2650          * in tx ring than it is necessary (see original non-SG driver).
2651          * However, with SG we need to reserve 2*MAX_SKB_FRAGS+1, which
2652          * is already overkill.
2653          *
2654          * Alternative is to return with 1 not throttling queue. In this
2655          * case loop becomes longer, no more useful effects.
2656          */
2657         barrier();
2658         goto restart;
2659 }
2660
2661
2662 static int ace_change_mtu(struct net_device *dev, int new_mtu)
2663 {
2664         struct ace_private *ap = dev->priv;
2665         struct ace_regs *regs = ap->regs;
2666
2667         if (new_mtu > ACE_JUMBO_MTU)
2668                 return -EINVAL;
2669
2670         writel(new_mtu + ETH_HLEN + 4, &regs->IfMtu);
2671         dev->mtu = new_mtu;
2672
2673         if (new_mtu > ACE_STD_MTU) {
2674                 if (!(ap->jumbo)) {
2675                         printk(KERN_INFO "%s: Enabling Jumbo frame "
2676                                "support\n", dev->name);
2677                         ap->jumbo = 1;
2678                         if (!test_and_set_bit(0, &ap->jumbo_refill_busy))
2679                                 ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE);
2680                         ace_set_rxtx_parms(dev, 1);
2681                 }
2682         } else {
2683                 while (test_and_set_bit(0, &ap->jumbo_refill_busy));
2684                 ace_sync_irq(dev->irq);
2685                 ace_set_rxtx_parms(dev, 0);
2686                 if (ap->jumbo) {
2687                         struct cmd cmd;
2688
2689                         cmd.evt = C_RESET_JUMBO_RNG;
2690                         cmd.code = 0;
2691                         cmd.idx = 0;
2692                         ace_issue_cmd(regs, &cmd);
2693                 }
2694         }
2695
2696         return 0;
2697 }
2698
2699 static int ace_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2700 {
2701         struct ace_private *ap = dev->priv;
2702         struct ace_regs *regs = ap->regs;
2703         u32 link;
2704
2705         memset(ecmd, 0, sizeof(struct ethtool_cmd));
2706         ecmd->supported =
2707                 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2708                  SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2709                  SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full |
2710                  SUPPORTED_Autoneg | SUPPORTED_FIBRE);
2711
2712         ecmd->port = PORT_FIBRE;
2713         ecmd->transceiver = XCVR_INTERNAL;
2714
2715         link = readl(&regs->GigLnkState);
2716         if (link & LNK_1000MB)
2717                 ecmd->speed = SPEED_1000;
2718         else {
2719                 link = readl(&regs->FastLnkState);
2720                 if (link & LNK_100MB)
2721                         ecmd->speed = SPEED_100;
2722                 else if (link & LNK_10MB)
2723                         ecmd->speed = SPEED_10;
2724                 else
2725                         ecmd->speed = 0;
2726         }
2727         if (link & LNK_FULL_DUPLEX)
2728                 ecmd->duplex = DUPLEX_FULL;
2729         else
2730                 ecmd->duplex = DUPLEX_HALF;
2731
2732         if (link & LNK_NEGOTIATE)
2733                 ecmd->autoneg = AUTONEG_ENABLE;
2734         else
2735                 ecmd->autoneg = AUTONEG_DISABLE;
2736
2737 #if 0
2738         /*
2739          * Current struct ethtool_cmd is insufficient
2740          */
2741         ecmd->trace = readl(&regs->TuneTrace);
2742
2743         ecmd->txcoal = readl(&regs->TuneTxCoalTicks);
2744         ecmd->rxcoal = readl(&regs->TuneRxCoalTicks);
2745 #endif
2746         ecmd->maxtxpkt = readl(&regs->TuneMaxTxDesc);
2747         ecmd->maxrxpkt = readl(&regs->TuneMaxRxDesc);
2748
2749         return 0;
2750 }
2751
2752 static int ace_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2753 {
2754         struct ace_private *ap = dev->priv;
2755         struct ace_regs *regs = ap->regs;
2756         u32 link, speed;
2757
2758         link = readl(&regs->GigLnkState);
2759         if (link & LNK_1000MB)
2760                 speed = SPEED_1000;
2761         else {
2762                 link = readl(&regs->FastLnkState);
2763                 if (link & LNK_100MB)
2764                         speed = SPEED_100;
2765                 else if (link & LNK_10MB)
2766                         speed = SPEED_10;
2767                 else
2768                         speed = SPEED_100;
2769         }
2770
2771         link = LNK_ENABLE | LNK_1000MB | LNK_100MB | LNK_10MB |
2772                 LNK_RX_FLOW_CTL_Y | LNK_NEG_FCTL;
2773         if (!ACE_IS_TIGON_I(ap))
2774                 link |= LNK_TX_FLOW_CTL_Y;
2775         if (ecmd->autoneg == AUTONEG_ENABLE)
2776                 link |= LNK_NEGOTIATE;
2777         if (ecmd->speed != speed) {
2778                 link &= ~(LNK_1000MB | LNK_100MB | LNK_10MB);
2779                 switch (speed) {
2780                 case SPEED_1000:
2781                         link |= LNK_1000MB;
2782                         break;
2783                 case SPEED_100:
2784                         link |= LNK_100MB;
2785                         break;
2786                 case SPEED_10:
2787                         link |= LNK_10MB;
2788                         break;
2789                 }
2790         }
2791
2792         if (ecmd->duplex == DUPLEX_FULL)
2793                 link |= LNK_FULL_DUPLEX;
2794
2795         if (link != ap->link) {
2796                 struct cmd cmd;
2797                 printk(KERN_INFO "%s: Renegotiating link state\n",
2798                        dev->name);
2799
2800                 ap->link = link;
2801                 writel(link, &regs->TuneLink);
2802                 if (!ACE_IS_TIGON_I(ap))
2803                         writel(link, &regs->TuneFastLink);
2804                 wmb();
2805
2806                 cmd.evt = C_LNK_NEGOTIATION;
2807                 cmd.code = 0;
2808                 cmd.idx = 0;
2809                 ace_issue_cmd(regs, &cmd);
2810         }
2811         return 0;
2812 }
2813
2814 static void ace_get_drvinfo(struct net_device *dev, 
2815                             struct ethtool_drvinfo *info)
2816 {
2817         struct ace_private *ap = dev->priv;
2818
2819         strlcpy(info->driver, "acenic", sizeof(info->driver));
2820         snprintf(info->version, sizeof(info->version), "%i.%i.%i", 
2821                 tigonFwReleaseMajor, tigonFwReleaseMinor,
2822                 tigonFwReleaseFix);
2823
2824         if (ap->pdev)
2825                 strlcpy(info->bus_info, pci_name(ap->pdev), 
2826                         sizeof(info->bus_info));
2827
2828 }
2829
2830 /*
2831  * Set the hardware MAC address.
2832  */
2833 static int ace_set_mac_addr(struct net_device *dev, void *p)
2834 {
2835         struct sockaddr *addr=p;
2836         struct ace_regs *regs;
2837         u8 *da;
2838         struct cmd cmd;
2839
2840         if(netif_running(dev))
2841                 return -EBUSY;
2842
2843         memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
2844
2845         da = (u8 *)dev->dev_addr;
2846
2847         regs = ((struct ace_private *)dev->priv)->regs;
2848         writel(da[0] << 8 | da[1], &regs->MacAddrHi);
2849         writel((da[2] << 24) | (da[3] << 16) | (da[4] << 8) | da[5],
2850                &regs->MacAddrLo);
2851
2852         cmd.evt = C_SET_MAC_ADDR;
2853         cmd.code = 0;
2854         cmd.idx = 0;
2855         ace_issue_cmd(regs, &cmd);
2856
2857         return 0;
2858 }
2859
2860
2861 static void ace_set_multicast_list(struct net_device *dev)
2862 {
2863         struct ace_private *ap = dev->priv;
2864         struct ace_regs *regs = ap->regs;
2865         struct cmd cmd;
2866
2867         if ((dev->flags & IFF_ALLMULTI) && !(ap->mcast_all)) {
2868                 cmd.evt = C_SET_MULTICAST_MODE;
2869                 cmd.code = C_C_MCAST_ENABLE;
2870                 cmd.idx = 0;
2871                 ace_issue_cmd(regs, &cmd);
2872                 ap->mcast_all = 1;
2873         } else if (ap->mcast_all) {
2874                 cmd.evt = C_SET_MULTICAST_MODE;
2875                 cmd.code = C_C_MCAST_DISABLE;
2876                 cmd.idx = 0;
2877                 ace_issue_cmd(regs, &cmd);
2878                 ap->mcast_all = 0;
2879         }
2880
2881         if ((dev->flags & IFF_PROMISC) && !(ap->promisc)) {
2882                 cmd.evt = C_SET_PROMISC_MODE;
2883                 cmd.code = C_C_PROMISC_ENABLE;
2884                 cmd.idx = 0;
2885                 ace_issue_cmd(regs, &cmd);
2886                 ap->promisc = 1;
2887         }else if (!(dev->flags & IFF_PROMISC) && (ap->promisc)) {
2888                 cmd.evt = C_SET_PROMISC_MODE;
2889                 cmd.code = C_C_PROMISC_DISABLE;
2890                 cmd.idx = 0;
2891                 ace_issue_cmd(regs, &cmd);
2892                 ap->promisc = 0;
2893         }
2894
2895         /*
2896          * For the time being multicast relies on the upper layers
2897          * filtering it properly. The Firmware does not allow one to
2898          * set the entire multicast list at a time and keeping track of
2899          * it here is going to be messy.
2900          */
2901         if ((dev->mc_count) && !(ap->mcast_all)) {
2902                 cmd.evt = C_SET_MULTICAST_MODE;
2903                 cmd.code = C_C_MCAST_ENABLE;
2904                 cmd.idx = 0;
2905                 ace_issue_cmd(regs, &cmd);
2906         }else if (!ap->mcast_all) {
2907                 cmd.evt = C_SET_MULTICAST_MODE;
2908                 cmd.code = C_C_MCAST_DISABLE;
2909                 cmd.idx = 0;
2910                 ace_issue_cmd(regs, &cmd);
2911         }
2912 }
2913
2914
2915 static struct net_device_stats *ace_get_stats(struct net_device *dev)
2916 {
2917         struct ace_private *ap = dev->priv;
2918         struct ace_mac_stats *mac_stats =
2919                 (struct ace_mac_stats *)ap->regs->Stats;
2920
2921         ap->stats.rx_missed_errors = readl(&mac_stats->drop_space);
2922         ap->stats.multicast = readl(&mac_stats->kept_mc);
2923         ap->stats.collisions = readl(&mac_stats->coll);
2924
2925         return &ap->stats;
2926 }
2927
2928
2929 static void __init ace_copy(struct ace_regs *regs, void *src,
2930                             u32 dest, int size)
2931 {
2932         unsigned long tdest;
2933         u32 *wsrc;
2934         short tsize, i;
2935
2936         if (size <= 0)
2937                 return;
2938
2939         while (size > 0) {
2940                 tsize = min_t(u32, ((~dest & (ACE_WINDOW_SIZE - 1)) + 1),
2941                             min_t(u32, size, ACE_WINDOW_SIZE));
2942                 tdest = (unsigned long)&regs->Window +
2943                         (dest & (ACE_WINDOW_SIZE - 1));
2944                 writel(dest & ~(ACE_WINDOW_SIZE - 1), &regs->WinBase);
2945                 /*
2946                  * This requires byte swapping on big endian, however
2947                  * writel does that for us
2948                  */
2949                 wsrc = src;
2950                 for (i = 0; i < (tsize / 4); i++) {
2951                         writel(wsrc[i], tdest + i*4);
2952                 }
2953                 dest += tsize;
2954                 src += tsize;
2955                 size -= tsize;
2956         }
2957
2958         return;
2959 }
2960
2961
2962 static void __init ace_clear(struct ace_regs *regs, u32 dest, int size)
2963 {
2964         unsigned long tdest;
2965         short tsize = 0, i;
2966
2967         if (size <= 0)
2968                 return;
2969
2970         while (size > 0) {
2971                 tsize = min_t(u32, ((~dest & (ACE_WINDOW_SIZE - 1)) + 1),
2972                                 min_t(u32, size, ACE_WINDOW_SIZE));
2973                 tdest = (unsigned long)&regs->Window +
2974                         (dest & (ACE_WINDOW_SIZE - 1));
2975                 writel(dest & ~(ACE_WINDOW_SIZE - 1), &regs->WinBase);
2976
2977                 for (i = 0; i < (tsize / 4); i++) {
2978                         writel(0, tdest + i*4);
2979                 }
2980
2981                 dest += tsize;
2982                 size -= tsize;
2983         }
2984
2985         return;
2986 }
2987
2988
2989 /*
2990  * Download the firmware into the SRAM on the NIC
2991  *
2992  * This operation requires the NIC to be halted and is performed with
2993  * interrupts disabled and with the spinlock hold.
2994  */
2995 int __init ace_load_firmware(struct net_device *dev)
2996 {
2997         struct ace_private *ap;
2998         struct ace_regs *regs;
2999
3000         ap = dev->priv;
3001         regs = ap->regs;
3002
3003         if (!(readl(&regs->CpuCtrl) & CPU_HALTED)) {
3004                 printk(KERN_ERR "%s: trying to download firmware while the "
3005                        "CPU is running!\n", dev->name);
3006                 return -EFAULT;
3007         }
3008
3009         /*
3010          * Do not try to clear more than 512KB or we end up seeing
3011          * funny things on NICs with only 512KB SRAM
3012          */
3013         ace_clear(regs, 0x2000, 0x80000-0x2000);
3014         if (ACE_IS_TIGON_I(ap)) {
3015                 ace_copy(regs, tigonFwText, tigonFwTextAddr, tigonFwTextLen);
3016                 ace_copy(regs, tigonFwData, tigonFwDataAddr, tigonFwDataLen);
3017                 ace_copy(regs, tigonFwRodata, tigonFwRodataAddr,
3018                          tigonFwRodataLen);
3019                 ace_clear(regs, tigonFwBssAddr, tigonFwBssLen);
3020                 ace_clear(regs, tigonFwSbssAddr, tigonFwSbssLen);
3021         }else if (ap->version == 2) {
3022                 ace_clear(regs, tigon2FwBssAddr, tigon2FwBssLen);
3023                 ace_clear(regs, tigon2FwSbssAddr, tigon2FwSbssLen);
3024                 ace_copy(regs, tigon2FwText, tigon2FwTextAddr,tigon2FwTextLen);
3025                 ace_copy(regs, tigon2FwRodata, tigon2FwRodataAddr,
3026                          tigon2FwRodataLen);
3027                 ace_copy(regs, tigon2FwData, tigon2FwDataAddr,tigon2FwDataLen);
3028         }
3029
3030         return 0;
3031 }
3032
3033
3034 /*
3035  * The eeprom on the AceNIC is an Atmel i2c EEPROM.
3036  *
3037  * Accessing the EEPROM is `interesting' to say the least - don't read
3038  * this code right after dinner.
3039  *
3040  * This is all about black magic and bit-banging the device .... I
3041  * wonder in what hospital they have put the guy who designed the i2c
3042  * specs.
3043  *
3044  * Oh yes, this is only the beginning!
3045  *
3046  * Thanks to Stevarino Webinski for helping tracking down the bugs in the
3047  * code i2c readout code by beta testing all my hacks.
3048  */
3049 static void __init eeprom_start(struct ace_regs *regs)
3050 {
3051         u32 local;
3052
3053         readl(&regs->LocalCtrl);
3054         udelay(ACE_SHORT_DELAY);
3055         local = readl(&regs->LocalCtrl);
3056         local |= EEPROM_DATA_OUT | EEPROM_WRITE_ENABLE;
3057         writel(local, &regs->LocalCtrl);
3058         readl(&regs->LocalCtrl);
3059         mb();
3060         udelay(ACE_SHORT_DELAY);
3061         local |= EEPROM_CLK_OUT;
3062         writel(local, &regs->LocalCtrl);
3063         readl(&regs->LocalCtrl);
3064         mb();
3065         udelay(ACE_SHORT_DELAY);
3066         local &= ~EEPROM_DATA_OUT;
3067         writel(local, &regs->LocalCtrl);
3068         readl(&regs->LocalCtrl);
3069         mb();
3070         udelay(ACE_SHORT_DELAY);
3071         local &= ~EEPROM_CLK_OUT;
3072         writel(local, &regs->LocalCtrl);
3073         readl(&regs->LocalCtrl);
3074         mb();
3075 }
3076
3077
3078 static void __init eeprom_prep(struct ace_regs *regs, u8 magic)
3079 {
3080         short i;
3081         u32 local;
3082
3083         udelay(ACE_SHORT_DELAY);
3084         local = readl(&regs->LocalCtrl);
3085         local &= ~EEPROM_DATA_OUT;
3086         local |= EEPROM_WRITE_ENABLE;
3087         writel(local, &regs->LocalCtrl);
3088         readl(&regs->LocalCtrl);
3089         mb();
3090
3091         for (i = 0; i < 8; i++, magic <<= 1) {
3092                 udelay(ACE_SHORT_DELAY);
3093                 if (magic & 0x80) 
3094                         local |= EEPROM_DATA_OUT;
3095                 else
3096                         local &= ~EEPROM_DATA_OUT;
3097                 writel(local, &regs->LocalCtrl);
3098                 readl(&regs->LocalCtrl);
3099                 mb();
3100
3101                 udelay(ACE_SHORT_DELAY);
3102                 local |= EEPROM_CLK_OUT;
3103                 writel(local, &regs->LocalCtrl);
3104                 readl(&regs->LocalCtrl);
3105                 mb();
3106                 udelay(ACE_SHORT_DELAY);
3107                 local &= ~(EEPROM_CLK_OUT | EEPROM_DATA_OUT);
3108                 writel(local, &regs->LocalCtrl);
3109                 readl(&regs->LocalCtrl);
3110                 mb();
3111         }
3112 }
3113
3114
3115 static int __init eeprom_check_ack(struct ace_regs *regs)
3116 {
3117         int state;
3118         u32 local;
3119
3120         local = readl(&regs->LocalCtrl);
3121         local &= ~EEPROM_WRITE_ENABLE;
3122         writel(local, &regs->LocalCtrl);
3123         readl(&regs->LocalCtrl);
3124         mb();
3125         udelay(ACE_LONG_DELAY);
3126         local |= EEPROM_CLK_OUT;
3127         writel(local, &regs->LocalCtrl);
3128         readl(&regs->LocalCtrl);
3129         mb();
3130         udelay(ACE_SHORT_DELAY);
3131         /* sample data in middle of high clk */
3132         state = (readl(&regs->LocalCtrl) & EEPROM_DATA_IN) != 0;
3133         udelay(ACE_SHORT_DELAY);
3134         mb();
3135         writel(readl(&regs->LocalCtrl) & ~EEPROM_CLK_OUT, &regs->LocalCtrl);
3136         readl(&regs->LocalCtrl);
3137         mb();
3138
3139         return state;
3140 }
3141
3142
3143 static void __init eeprom_stop(struct ace_regs *regs)
3144 {
3145         u32 local;
3146
3147         udelay(ACE_SHORT_DELAY);
3148         local = readl(&regs->LocalCtrl);
3149         local |= EEPROM_WRITE_ENABLE;
3150         writel(local, &regs->LocalCtrl);
3151         readl(&regs->LocalCtrl);
3152         mb();
3153         udelay(ACE_SHORT_DELAY);
3154         local &= ~EEPROM_DATA_OUT;
3155         writel(local, &regs->LocalCtrl);
3156         readl(&regs->LocalCtrl);
3157         mb();
3158         udelay(ACE_SHORT_DELAY);
3159         local |= EEPROM_CLK_OUT;
3160         writel(local, &regs->LocalCtrl);
3161         readl(&regs->LocalCtrl);
3162         mb();
3163         udelay(ACE_SHORT_DELAY);
3164         local |= EEPROM_DATA_OUT;
3165         writel(local, &regs->LocalCtrl);
3166         readl(&regs->LocalCtrl);
3167         mb();
3168         udelay(ACE_LONG_DELAY);
3169         local &= ~EEPROM_CLK_OUT;
3170         writel(local, &regs->LocalCtrl);
3171         mb();
3172 }
3173
3174
3175 /*
3176  * Read a whole byte from the EEPROM.
3177  */
3178 static int __init read_eeprom_byte(struct net_device *dev,
3179                                    unsigned long offset)
3180 {
3181         struct ace_regs *regs;
3182         unsigned long flags;
3183         u32 local;
3184         int result = 0;
3185         short i;
3186
3187         if (!dev) {
3188                 printk(KERN_ERR "No device!\n");
3189                 result = -ENODEV;
3190                 goto eeprom_read_error;
3191         }
3192
3193         regs = ((struct ace_private *)dev->priv)->regs;
3194
3195         /*
3196          * Don't take interrupts on this CPU will bit banging
3197          * the %#%#@$ I2C device
3198          */
3199         local_irq_save(flags);
3200
3201         eeprom_start(regs);
3202
3203         eeprom_prep(regs, EEPROM_WRITE_SELECT);
3204         if (eeprom_check_ack(regs)) {
3205                 local_irq_restore(flags);
3206                 printk(KERN_ERR "%s: Unable to sync eeprom\n", dev->name);
3207                 result = -EIO;
3208                 goto eeprom_read_error;
3209         }
3210
3211         eeprom_prep(regs, (offset >> 8) & 0xff);
3212         if (eeprom_check_ack(regs)) {
3213                 local_irq_restore(flags);
3214                 printk(KERN_ERR "%s: Unable to set address byte 0\n",
3215                        dev->name);
3216                 result = -EIO;
3217                 goto eeprom_read_error;
3218         }
3219
3220         eeprom_prep(regs, offset & 0xff);
3221         if (eeprom_check_ack(regs)) {
3222                 local_irq_restore(flags);
3223                 printk(KERN_ERR "%s: Unable to set address byte 1\n",
3224                        dev->name);
3225                 result = -EIO;
3226                 goto eeprom_read_error;
3227         }
3228
3229         eeprom_start(regs);
3230         eeprom_prep(regs, EEPROM_READ_SELECT);
3231         if (eeprom_check_ack(regs)) {
3232                 local_irq_restore(flags);
3233                 printk(KERN_ERR "%s: Unable to set READ_SELECT\n",
3234                        dev->name);
3235                 result = -EIO;
3236                 goto eeprom_read_error;
3237         }
3238
3239         for (i = 0; i < 8; i++) {
3240                 local = readl(&regs->LocalCtrl);
3241                 local &= ~EEPROM_WRITE_ENABLE;
3242                 writel(local, &regs->LocalCtrl);
3243                 readl(&regs->LocalCtrl);
3244                 udelay(ACE_LONG_DELAY);
3245                 mb();
3246                 local |= EEPROM_CLK_OUT;
3247                 writel(local, &regs->LocalCtrl);
3248                 readl(&regs->LocalCtrl);
3249                 mb();
3250                 udelay(ACE_SHORT_DELAY);
3251                 /* sample data mid high clk */
3252                 result = (result << 1) |
3253                         ((readl(&regs->LocalCtrl) & EEPROM_DATA_IN) != 0);
3254                 udelay(ACE_SHORT_DELAY);
3255                 mb();
3256                 local = readl(&regs->LocalCtrl);
3257                 local &= ~EEPROM_CLK_OUT;
3258                 writel(local, &regs->LocalCtrl);
3259                 readl(&regs->LocalCtrl);
3260                 udelay(ACE_SHORT_DELAY);
3261                 mb();
3262                 if (i == 7) {
3263                         local |= EEPROM_WRITE_ENABLE;
3264                         writel(local, &regs->LocalCtrl);
3265                         readl(&regs->LocalCtrl);
3266                         mb();
3267                         udelay(ACE_SHORT_DELAY);
3268                 }
3269         }
3270
3271         local |= EEPROM_DATA_OUT;
3272         writel(local, &regs->LocalCtrl);
3273         readl(&regs->LocalCtrl);
3274         mb();
3275         udelay(ACE_SHORT_DELAY);
3276         writel(readl(&regs->LocalCtrl) | EEPROM_CLK_OUT, &regs->LocalCtrl);
3277         readl(&regs->LocalCtrl);
3278         udelay(ACE_LONG_DELAY);
3279         writel(readl(&regs->LocalCtrl) & ~EEPROM_CLK_OUT, &regs->LocalCtrl);
3280         readl(&regs->LocalCtrl);
3281         mb();
3282         udelay(ACE_SHORT_DELAY);
3283         eeprom_stop(regs);
3284
3285         local_irq_restore(flags);
3286  out:
3287         return result;
3288
3289  eeprom_read_error:
3290         printk(KERN_ERR "%s: Unable to read eeprom byte 0x%02lx\n",
3291                dev->name, offset);
3292         goto out;
3293 }
3294
3295
3296 /*
3297  * Local variables:
3298  * compile-command: "gcc -D__SMP__ -D__KERNEL__ -DMODULE -I../../include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -fno-strength-reduce -DMODVERSIONS -include ../../include/linux/modversions.h   -c -o acenic.o acenic.c"
3299  * End:
3300  */