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