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