[PATCH] s2io c99 warning fix
[sfrench/cifs-2.6.git] / drivers / net / s2io.c
1 /************************************************************************
2  * s2io.c: A Linux PCI-X Ethernet driver for Neterion 10GbE Server NIC
3  * Copyright(c) 2002-2005 Neterion Inc.
4
5  * This software may be used and distributed according to the terms of
6  * the GNU General Public License (GPL), incorporated herein by reference.
7  * Drivers based on or derived from this code fall under the GPL and must
8  * retain the authorship, copyright and license notice.  This file is not
9  * a complete program and may only be used when the entire operating
10  * system is licensed under the GPL.
11  * See the file COPYING in this distribution for more information.
12  *
13  * Credits:
14  * Jeff Garzik          : For pointing out the improper error condition
15  *                        check in the s2io_xmit routine and also some
16  *                        issues in the Tx watch dog function. Also for
17  *                        patiently answering all those innumerable
18  *                        questions regaring the 2.6 porting issues.
19  * Stephen Hemminger    : Providing proper 2.6 porting mechanism for some
20  *                        macros available only in 2.6 Kernel.
21  * Francois Romieu      : For pointing out all code part that were
22  *                        deprecated and also styling related comments.
23  * Grant Grundler       : For helping me get rid of some Architecture
24  *                        dependent code.
25  * Christopher Hellwig  : Some more 2.6 specific issues in the driver.
26  *
27  * The module loadable parameters that are supported by the driver and a brief
28  * explaination of all the variables.
29  * rx_ring_num : This can be used to program the number of receive rings used
30  * in the driver.
31  * rx_ring_sz: This defines the number of descriptors each ring can have. This
32  * is also an array of size 8.
33  * rx_ring_mode: This defines the operation mode of all 8 rings. The valid
34  *              values are 1, 2 and 3.
35  * tx_fifo_num: This defines the number of Tx FIFOs thats used int the driver.
36  * tx_fifo_len: This too is an array of 8. Each element defines the number of
37  * Tx descriptors that can be associated with each corresponding FIFO.
38  ************************************************************************/
39
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/errno.h>
44 #include <linux/ioport.h>
45 #include <linux/pci.h>
46 #include <linux/dma-mapping.h>
47 #include <linux/kernel.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/init.h>
52 #include <linux/delay.h>
53 #include <linux/stddef.h>
54 #include <linux/ioctl.h>
55 #include <linux/timex.h>
56 #include <linux/sched.h>
57 #include <linux/ethtool.h>
58 #include <linux/workqueue.h>
59 #include <linux/if_vlan.h>
60 #include <linux/ip.h>
61 #include <linux/tcp.h>
62 #include <net/tcp.h>
63
64 #include <asm/system.h>
65 #include <asm/uaccess.h>
66 #include <asm/io.h>
67 #include <asm/div64.h>
68
69 /* local include */
70 #include "s2io.h"
71 #include "s2io-regs.h"
72
73 #define DRV_VERSION "2.0.11.2"
74
75 /* S2io Driver name & version. */
76 static char s2io_driver_name[] = "Neterion";
77 static char s2io_driver_version[] = DRV_VERSION;
78
79 int rxd_size[4] = {32,48,48,64};
80 int rxd_count[4] = {127,85,85,63};
81
82 static inline int RXD_IS_UP2DT(RxD_t *rxdp)
83 {
84         int ret;
85
86         ret = ((!(rxdp->Control_1 & RXD_OWN_XENA)) &&
87                 (GET_RXD_MARKER(rxdp->Control_2) != THE_RXD_MARK));
88
89         return ret;
90 }
91
92 /*
93  * Cards with following subsystem_id have a link state indication
94  * problem, 600B, 600C, 600D, 640B, 640C and 640D.
95  * macro below identifies these cards given the subsystem_id.
96  */
97 #define CARDS_WITH_FAULTY_LINK_INDICATORS(dev_type, subid) \
98         (dev_type == XFRAME_I_DEVICE) ?                 \
99                 ((((subid >= 0x600B) && (subid <= 0x600D)) || \
100                  ((subid >= 0x640B) && (subid <= 0x640D))) ? 1 : 0) : 0
101
102 #define LINK_IS_UP(val64) (!(val64 & (ADAPTER_STATUS_RMAC_REMOTE_FAULT | \
103                                       ADAPTER_STATUS_RMAC_LOCAL_FAULT)))
104 #define TASKLET_IN_USE test_and_set_bit(0, (&sp->tasklet_status))
105 #define PANIC   1
106 #define LOW     2
107 static inline int rx_buffer_level(nic_t * sp, int rxb_size, int ring)
108 {
109         int level = 0;
110         mac_info_t *mac_control;
111
112         mac_control = &sp->mac_control;
113         if ((mac_control->rings[ring].pkt_cnt - rxb_size) > 16) {
114                 level = LOW;
115                 if (rxb_size <= rxd_count[sp->rxd_mode]) {
116                         level = PANIC;
117                 }
118         }
119
120         return level;
121 }
122
123 /* Ethtool related variables and Macros. */
124 static char s2io_gstrings[][ETH_GSTRING_LEN] = {
125         "Register test\t(offline)",
126         "Eeprom test\t(offline)",
127         "Link test\t(online)",
128         "RLDRAM test\t(offline)",
129         "BIST Test\t(offline)"
130 };
131
132 static char ethtool_stats_keys[][ETH_GSTRING_LEN] = {
133         {"tmac_frms"},
134         {"tmac_data_octets"},
135         {"tmac_drop_frms"},
136         {"tmac_mcst_frms"},
137         {"tmac_bcst_frms"},
138         {"tmac_pause_ctrl_frms"},
139         {"tmac_any_err_frms"},
140         {"tmac_vld_ip_octets"},
141         {"tmac_vld_ip"},
142         {"tmac_drop_ip"},
143         {"tmac_icmp"},
144         {"tmac_rst_tcp"},
145         {"tmac_tcp"},
146         {"tmac_udp"},
147         {"rmac_vld_frms"},
148         {"rmac_data_octets"},
149         {"rmac_fcs_err_frms"},
150         {"rmac_drop_frms"},
151         {"rmac_vld_mcst_frms"},
152         {"rmac_vld_bcst_frms"},
153         {"rmac_in_rng_len_err_frms"},
154         {"rmac_long_frms"},
155         {"rmac_pause_ctrl_frms"},
156         {"rmac_discarded_frms"},
157         {"rmac_usized_frms"},
158         {"rmac_osized_frms"},
159         {"rmac_frag_frms"},
160         {"rmac_jabber_frms"},
161         {"rmac_ip"},
162         {"rmac_ip_octets"},
163         {"rmac_hdr_err_ip"},
164         {"rmac_drop_ip"},
165         {"rmac_icmp"},
166         {"rmac_tcp"},
167         {"rmac_udp"},
168         {"rmac_err_drp_udp"},
169         {"rmac_pause_cnt"},
170         {"rmac_accepted_ip"},
171         {"rmac_err_tcp"},
172         {"\n DRIVER STATISTICS"},
173         {"single_bit_ecc_errs"},
174         {"double_bit_ecc_errs"},
175         ("lro_aggregated_pkts"),
176         ("lro_flush_both_count"),
177         ("lro_out_of_sequence_pkts"),
178         ("lro_flush_due_to_max_pkts"),
179         ("lro_avg_aggr_pkts"),
180 };
181
182 #define S2IO_STAT_LEN sizeof(ethtool_stats_keys)/ ETH_GSTRING_LEN
183 #define S2IO_STAT_STRINGS_LEN S2IO_STAT_LEN * ETH_GSTRING_LEN
184
185 #define S2IO_TEST_LEN   sizeof(s2io_gstrings) / ETH_GSTRING_LEN
186 #define S2IO_STRINGS_LEN        S2IO_TEST_LEN * ETH_GSTRING_LEN
187
188 #define S2IO_TIMER_CONF(timer, handle, arg, exp)                \
189                         init_timer(&timer);                     \
190                         timer.function = handle;                \
191                         timer.data = (unsigned long) arg;       \
192                         mod_timer(&timer, (jiffies + exp))      \
193
194 /* Add the vlan */
195 static void s2io_vlan_rx_register(struct net_device *dev,
196                                         struct vlan_group *grp)
197 {
198         nic_t *nic = dev->priv;
199         unsigned long flags;
200
201         spin_lock_irqsave(&nic->tx_lock, flags);
202         nic->vlgrp = grp;
203         spin_unlock_irqrestore(&nic->tx_lock, flags);
204 }
205
206 /* Unregister the vlan */
207 static void s2io_vlan_rx_kill_vid(struct net_device *dev, unsigned long vid)
208 {
209         nic_t *nic = dev->priv;
210         unsigned long flags;
211
212         spin_lock_irqsave(&nic->tx_lock, flags);
213         if (nic->vlgrp)
214                 nic->vlgrp->vlan_devices[vid] = NULL;
215         spin_unlock_irqrestore(&nic->tx_lock, flags);
216 }
217
218 /*
219  * Constants to be programmed into the Xena's registers, to configure
220  * the XAUI.
221  */
222
223 #define SWITCH_SIGN     0xA5A5A5A5A5A5A5A5ULL
224 #define END_SIGN        0x0
225
226 static u64 herc_act_dtx_cfg[] = {
227         /* Set address */
228         0x8000051536750000ULL, 0x80000515367500E0ULL,
229         /* Write data */
230         0x8000051536750004ULL, 0x80000515367500E4ULL,
231         /* Set address */
232         0x80010515003F0000ULL, 0x80010515003F00E0ULL,
233         /* Write data */
234         0x80010515003F0004ULL, 0x80010515003F00E4ULL,
235         /* Set address */
236         0x801205150D440000ULL, 0x801205150D4400E0ULL,
237         /* Write data */
238         0x801205150D440004ULL, 0x801205150D4400E4ULL,
239         /* Set address */
240         0x80020515F2100000ULL, 0x80020515F21000E0ULL,
241         /* Write data */
242         0x80020515F2100004ULL, 0x80020515F21000E4ULL,
243         /* Done */
244         END_SIGN
245 };
246
247 static u64 xena_mdio_cfg[] = {
248         /* Reset PMA PLL */
249         0xC001010000000000ULL, 0xC0010100000000E0ULL,
250         0xC0010100008000E4ULL,
251         /* Remove Reset from PMA PLL */
252         0xC001010000000000ULL, 0xC0010100000000E0ULL,
253         0xC0010100000000E4ULL,
254         END_SIGN
255 };
256
257 static u64 xena_dtx_cfg[] = {
258         0x8000051500000000ULL, 0x80000515000000E0ULL,
259         0x80000515D93500E4ULL, 0x8001051500000000ULL,
260         0x80010515000000E0ULL, 0x80010515001E00E4ULL,
261         0x8002051500000000ULL, 0x80020515000000E0ULL,
262         0x80020515F21000E4ULL,
263         /* Set PADLOOPBACKN */
264         0x8002051500000000ULL, 0x80020515000000E0ULL,
265         0x80020515B20000E4ULL, 0x8003051500000000ULL,
266         0x80030515000000E0ULL, 0x80030515B20000E4ULL,
267         0x8004051500000000ULL, 0x80040515000000E0ULL,
268         0x80040515B20000E4ULL, 0x8005051500000000ULL,
269         0x80050515000000E0ULL, 0x80050515B20000E4ULL,
270         SWITCH_SIGN,
271         /* Remove PADLOOPBACKN */
272         0x8002051500000000ULL, 0x80020515000000E0ULL,
273         0x80020515F20000E4ULL, 0x8003051500000000ULL,
274         0x80030515000000E0ULL, 0x80030515F20000E4ULL,
275         0x8004051500000000ULL, 0x80040515000000E0ULL,
276         0x80040515F20000E4ULL, 0x8005051500000000ULL,
277         0x80050515000000E0ULL, 0x80050515F20000E4ULL,
278         END_SIGN
279 };
280
281 /*
282  * Constants for Fixing the MacAddress problem seen mostly on
283  * Alpha machines.
284  */
285 static u64 fix_mac[] = {
286         0x0060000000000000ULL, 0x0060600000000000ULL,
287         0x0040600000000000ULL, 0x0000600000000000ULL,
288         0x0020600000000000ULL, 0x0060600000000000ULL,
289         0x0020600000000000ULL, 0x0060600000000000ULL,
290         0x0020600000000000ULL, 0x0060600000000000ULL,
291         0x0020600000000000ULL, 0x0060600000000000ULL,
292         0x0020600000000000ULL, 0x0060600000000000ULL,
293         0x0020600000000000ULL, 0x0060600000000000ULL,
294         0x0020600000000000ULL, 0x0060600000000000ULL,
295         0x0020600000000000ULL, 0x0060600000000000ULL,
296         0x0020600000000000ULL, 0x0060600000000000ULL,
297         0x0020600000000000ULL, 0x0060600000000000ULL,
298         0x0020600000000000ULL, 0x0000600000000000ULL,
299         0x0040600000000000ULL, 0x0060600000000000ULL,
300         END_SIGN
301 };
302
303 /* Module Loadable parameters. */
304 static unsigned int tx_fifo_num = 1;
305 static unsigned int tx_fifo_len[MAX_TX_FIFOS] =
306     {[0 ...(MAX_TX_FIFOS - 1)] = 0 };
307 static unsigned int rx_ring_num = 1;
308 static unsigned int rx_ring_sz[MAX_RX_RINGS] =
309     {[0 ...(MAX_RX_RINGS - 1)] = 0 };
310 static unsigned int rts_frm_len[MAX_RX_RINGS] =
311     {[0 ...(MAX_RX_RINGS - 1)] = 0 };
312 static unsigned int rx_ring_mode = 1;
313 static unsigned int use_continuous_tx_intrs = 1;
314 static unsigned int rmac_pause_time = 65535;
315 static unsigned int mc_pause_threshold_q0q3 = 187;
316 static unsigned int mc_pause_threshold_q4q7 = 187;
317 static unsigned int shared_splits;
318 static unsigned int tmac_util_period = 5;
319 static unsigned int rmac_util_period = 5;
320 static unsigned int bimodal = 0;
321 static unsigned int l3l4hdr_size = 128;
322 #ifndef CONFIG_S2IO_NAPI
323 static unsigned int indicate_max_pkts;
324 #endif
325 /* Frequency of Rx desc syncs expressed as power of 2 */
326 static unsigned int rxsync_frequency = 3;
327 /* Interrupt type. Values can be 0(INTA), 1(MSI), 2(MSI_X) */
328 static unsigned int intr_type = 0;
329 /* Large receive offload feature */
330 static unsigned int lro = 0;
331 /* Max pkts to be aggregated by LRO at one time. If not specified,
332  * aggregation happens until we hit max IP pkt size(64K)
333  */
334 static unsigned int lro_max_pkts = 0xFFFF;
335
336 /*
337  * S2IO device table.
338  * This table lists all the devices that this driver supports.
339  */
340 static struct pci_device_id s2io_tbl[] __devinitdata = {
341         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_S2IO_WIN,
342          PCI_ANY_ID, PCI_ANY_ID},
343         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_S2IO_UNI,
344          PCI_ANY_ID, PCI_ANY_ID},
345         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_HERC_WIN,
346          PCI_ANY_ID, PCI_ANY_ID},
347         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_HERC_UNI,
348          PCI_ANY_ID, PCI_ANY_ID},
349         {0,}
350 };
351
352 MODULE_DEVICE_TABLE(pci, s2io_tbl);
353
354 static struct pci_driver s2io_driver = {
355       .name = "S2IO",
356       .id_table = s2io_tbl,
357       .probe = s2io_init_nic,
358       .remove = __devexit_p(s2io_rem_nic),
359 };
360
361 /* A simplifier macro used both by init and free shared_mem Fns(). */
362 #define TXD_MEM_PAGE_CNT(len, per_each) ((len+per_each - 1) / per_each)
363
364 /**
365  * init_shared_mem - Allocation and Initialization of Memory
366  * @nic: Device private variable.
367  * Description: The function allocates all the memory areas shared
368  * between the NIC and the driver. This includes Tx descriptors,
369  * Rx descriptors and the statistics block.
370  */
371
372 static int init_shared_mem(struct s2io_nic *nic)
373 {
374         u32 size;
375         void *tmp_v_addr, *tmp_v_addr_next;
376         dma_addr_t tmp_p_addr, tmp_p_addr_next;
377         RxD_block_t *pre_rxd_blk = NULL;
378         int i, j, blk_cnt, rx_sz, tx_sz;
379         int lst_size, lst_per_page;
380         struct net_device *dev = nic->dev;
381         unsigned long tmp;
382         buffAdd_t *ba;
383
384         mac_info_t *mac_control;
385         struct config_param *config;
386
387         mac_control = &nic->mac_control;
388         config = &nic->config;
389
390
391         /* Allocation and initialization of TXDLs in FIOFs */
392         size = 0;
393         for (i = 0; i < config->tx_fifo_num; i++) {
394                 size += config->tx_cfg[i].fifo_len;
395         }
396         if (size > MAX_AVAILABLE_TXDS) {
397                 DBG_PRINT(ERR_DBG, "%s: Requested TxDs too high, ",
398                           __FUNCTION__);
399                 DBG_PRINT(ERR_DBG, "Requested: %d, max supported: 8192\n", size);
400                 return FAILURE;
401         }
402
403         lst_size = (sizeof(TxD_t) * config->max_txds);
404         tx_sz = lst_size * size;
405         lst_per_page = PAGE_SIZE / lst_size;
406
407         for (i = 0; i < config->tx_fifo_num; i++) {
408                 int fifo_len = config->tx_cfg[i].fifo_len;
409                 int list_holder_size = fifo_len * sizeof(list_info_hold_t);
410                 mac_control->fifos[i].list_info = kmalloc(list_holder_size,
411                                                           GFP_KERNEL);
412                 if (!mac_control->fifos[i].list_info) {
413                         DBG_PRINT(ERR_DBG,
414                                   "Malloc failed for list_info\n");
415                         return -ENOMEM;
416                 }
417                 memset(mac_control->fifos[i].list_info, 0, list_holder_size);
418         }
419         for (i = 0; i < config->tx_fifo_num; i++) {
420                 int page_num = TXD_MEM_PAGE_CNT(config->tx_cfg[i].fifo_len,
421                                                 lst_per_page);
422                 mac_control->fifos[i].tx_curr_put_info.offset = 0;
423                 mac_control->fifos[i].tx_curr_put_info.fifo_len =
424                     config->tx_cfg[i].fifo_len - 1;
425                 mac_control->fifos[i].tx_curr_get_info.offset = 0;
426                 mac_control->fifos[i].tx_curr_get_info.fifo_len =
427                     config->tx_cfg[i].fifo_len - 1;
428                 mac_control->fifos[i].fifo_no = i;
429                 mac_control->fifos[i].nic = nic;
430                 mac_control->fifos[i].max_txds = MAX_SKB_FRAGS + 2;
431
432                 for (j = 0; j < page_num; j++) {
433                         int k = 0;
434                         dma_addr_t tmp_p;
435                         void *tmp_v;
436                         tmp_v = pci_alloc_consistent(nic->pdev,
437                                                      PAGE_SIZE, &tmp_p);
438                         if (!tmp_v) {
439                                 DBG_PRINT(ERR_DBG,
440                                           "pci_alloc_consistent ");
441                                 DBG_PRINT(ERR_DBG, "failed for TxDL\n");
442                                 return -ENOMEM;
443                         }
444                         /* If we got a zero DMA address(can happen on
445                          * certain platforms like PPC), reallocate.
446                          * Store virtual address of page we don't want,
447                          * to be freed later.
448                          */
449                         if (!tmp_p) {
450                                 mac_control->zerodma_virt_addr = tmp_v;
451                                 DBG_PRINT(INIT_DBG, 
452                                 "%s: Zero DMA address for TxDL. ", dev->name);
453                                 DBG_PRINT(INIT_DBG, 
454                                 "Virtual address %p\n", tmp_v);
455                                 tmp_v = pci_alloc_consistent(nic->pdev,
456                                                      PAGE_SIZE, &tmp_p);
457                                 if (!tmp_v) {
458                                         DBG_PRINT(ERR_DBG,
459                                           "pci_alloc_consistent ");
460                                         DBG_PRINT(ERR_DBG, "failed for TxDL\n");
461                                         return -ENOMEM;
462                                 }
463                         }
464                         while (k < lst_per_page) {
465                                 int l = (j * lst_per_page) + k;
466                                 if (l == config->tx_cfg[i].fifo_len)
467                                         break;
468                                 mac_control->fifos[i].list_info[l].list_virt_addr =
469                                     tmp_v + (k * lst_size);
470                                 mac_control->fifos[i].list_info[l].list_phy_addr =
471                                     tmp_p + (k * lst_size);
472                                 k++;
473                         }
474                 }
475         }
476
477         nic->ufo_in_band_v = kmalloc((sizeof(u64) * size), GFP_KERNEL);
478         if (!nic->ufo_in_band_v)
479                 return -ENOMEM;
480
481         /* Allocation and initialization of RXDs in Rings */
482         size = 0;
483         for (i = 0; i < config->rx_ring_num; i++) {
484                 if (config->rx_cfg[i].num_rxd %
485                     (rxd_count[nic->rxd_mode] + 1)) {
486                         DBG_PRINT(ERR_DBG, "%s: RxD count of ", dev->name);
487                         DBG_PRINT(ERR_DBG, "Ring%d is not a multiple of ",
488                                   i);
489                         DBG_PRINT(ERR_DBG, "RxDs per Block");
490                         return FAILURE;
491                 }
492                 size += config->rx_cfg[i].num_rxd;
493                 mac_control->rings[i].block_count =
494                         config->rx_cfg[i].num_rxd /
495                         (rxd_count[nic->rxd_mode] + 1 );
496                 mac_control->rings[i].pkt_cnt = config->rx_cfg[i].num_rxd -
497                         mac_control->rings[i].block_count;
498         }
499         if (nic->rxd_mode == RXD_MODE_1)
500                 size = (size * (sizeof(RxD1_t)));
501         else
502                 size = (size * (sizeof(RxD3_t)));
503         rx_sz = size;
504
505         for (i = 0; i < config->rx_ring_num; i++) {
506                 mac_control->rings[i].rx_curr_get_info.block_index = 0;
507                 mac_control->rings[i].rx_curr_get_info.offset = 0;
508                 mac_control->rings[i].rx_curr_get_info.ring_len =
509                     config->rx_cfg[i].num_rxd - 1;
510                 mac_control->rings[i].rx_curr_put_info.block_index = 0;
511                 mac_control->rings[i].rx_curr_put_info.offset = 0;
512                 mac_control->rings[i].rx_curr_put_info.ring_len =
513                     config->rx_cfg[i].num_rxd - 1;
514                 mac_control->rings[i].nic = nic;
515                 mac_control->rings[i].ring_no = i;
516
517                 blk_cnt = config->rx_cfg[i].num_rxd /
518                                 (rxd_count[nic->rxd_mode] + 1);
519                 /*  Allocating all the Rx blocks */
520                 for (j = 0; j < blk_cnt; j++) {
521                         rx_block_info_t *rx_blocks;
522                         int l;
523
524                         rx_blocks = &mac_control->rings[i].rx_blocks[j];
525                         size = SIZE_OF_BLOCK; //size is always page size
526                         tmp_v_addr = pci_alloc_consistent(nic->pdev, size,
527                                                           &tmp_p_addr);
528                         if (tmp_v_addr == NULL) {
529                                 /*
530                                  * In case of failure, free_shared_mem()
531                                  * is called, which should free any
532                                  * memory that was alloced till the
533                                  * failure happened.
534                                  */
535                                 rx_blocks->block_virt_addr = tmp_v_addr;
536                                 return -ENOMEM;
537                         }
538                         memset(tmp_v_addr, 0, size);
539                         rx_blocks->block_virt_addr = tmp_v_addr;
540                         rx_blocks->block_dma_addr = tmp_p_addr;
541                         rx_blocks->rxds = kmalloc(sizeof(rxd_info_t)*
542                                                   rxd_count[nic->rxd_mode],
543                                                   GFP_KERNEL);
544                         for (l=0; l<rxd_count[nic->rxd_mode];l++) {
545                                 rx_blocks->rxds[l].virt_addr =
546                                         rx_blocks->block_virt_addr +
547                                         (rxd_size[nic->rxd_mode] * l);
548                                 rx_blocks->rxds[l].dma_addr =
549                                         rx_blocks->block_dma_addr +
550                                         (rxd_size[nic->rxd_mode] * l);
551                         }
552
553                         mac_control->rings[i].rx_blocks[j].block_virt_addr =
554                                 tmp_v_addr;
555                         mac_control->rings[i].rx_blocks[j].block_dma_addr =
556                                 tmp_p_addr;
557                 }
558                 /* Interlinking all Rx Blocks */
559                 for (j = 0; j < blk_cnt; j++) {
560                         tmp_v_addr =
561                                 mac_control->rings[i].rx_blocks[j].block_virt_addr;
562                         tmp_v_addr_next =
563                                 mac_control->rings[i].rx_blocks[(j + 1) %
564                                               blk_cnt].block_virt_addr;
565                         tmp_p_addr =
566                                 mac_control->rings[i].rx_blocks[j].block_dma_addr;
567                         tmp_p_addr_next =
568                                 mac_control->rings[i].rx_blocks[(j + 1) %
569                                               blk_cnt].block_dma_addr;
570
571                         pre_rxd_blk = (RxD_block_t *) tmp_v_addr;
572                         pre_rxd_blk->reserved_2_pNext_RxD_block =
573                             (unsigned long) tmp_v_addr_next;
574                         pre_rxd_blk->pNext_RxD_Blk_physical =
575                             (u64) tmp_p_addr_next;
576                 }
577         }
578         if (nic->rxd_mode >= RXD_MODE_3A) {
579                 /*
580                  * Allocation of Storages for buffer addresses in 2BUFF mode
581                  * and the buffers as well.
582                  */
583                 for (i = 0; i < config->rx_ring_num; i++) {
584                         blk_cnt = config->rx_cfg[i].num_rxd /
585                            (rxd_count[nic->rxd_mode]+ 1);
586                         mac_control->rings[i].ba =
587                                 kmalloc((sizeof(buffAdd_t *) * blk_cnt),
588                                      GFP_KERNEL);
589                         if (!mac_control->rings[i].ba)
590                                 return -ENOMEM;
591                         for (j = 0; j < blk_cnt; j++) {
592                                 int k = 0;
593                                 mac_control->rings[i].ba[j] =
594                                         kmalloc((sizeof(buffAdd_t) *
595                                                 (rxd_count[nic->rxd_mode] + 1)),
596                                                 GFP_KERNEL);
597                                 if (!mac_control->rings[i].ba[j])
598                                         return -ENOMEM;
599                                 while (k != rxd_count[nic->rxd_mode]) {
600                                         ba = &mac_control->rings[i].ba[j][k];
601
602                                         ba->ba_0_org = (void *) kmalloc
603                                             (BUF0_LEN + ALIGN_SIZE, GFP_KERNEL);
604                                         if (!ba->ba_0_org)
605                                                 return -ENOMEM;
606                                         tmp = (unsigned long)ba->ba_0_org;
607                                         tmp += ALIGN_SIZE;
608                                         tmp &= ~((unsigned long) ALIGN_SIZE);
609                                         ba->ba_0 = (void *) tmp;
610
611                                         ba->ba_1_org = (void *) kmalloc
612                                             (BUF1_LEN + ALIGN_SIZE, GFP_KERNEL);
613                                         if (!ba->ba_1_org)
614                                                 return -ENOMEM;
615                                         tmp = (unsigned long) ba->ba_1_org;
616                                         tmp += ALIGN_SIZE;
617                                         tmp &= ~((unsigned long) ALIGN_SIZE);
618                                         ba->ba_1 = (void *) tmp;
619                                         k++;
620                                 }
621                         }
622                 }
623         }
624
625         /* Allocation and initialization of Statistics block */
626         size = sizeof(StatInfo_t);
627         mac_control->stats_mem = pci_alloc_consistent
628             (nic->pdev, size, &mac_control->stats_mem_phy);
629
630         if (!mac_control->stats_mem) {
631                 /*
632                  * In case of failure, free_shared_mem() is called, which
633                  * should free any memory that was alloced till the
634                  * failure happened.
635                  */
636                 return -ENOMEM;
637         }
638         mac_control->stats_mem_sz = size;
639
640         tmp_v_addr = mac_control->stats_mem;
641         mac_control->stats_info = (StatInfo_t *) tmp_v_addr;
642         memset(tmp_v_addr, 0, size);
643         DBG_PRINT(INIT_DBG, "%s:Ring Mem PHY: 0x%llx\n", dev->name,
644                   (unsigned long long) tmp_p_addr);
645
646         return SUCCESS;
647 }
648
649 /**
650  * free_shared_mem - Free the allocated Memory
651  * @nic:  Device private variable.
652  * Description: This function is to free all memory locations allocated by
653  * the init_shared_mem() function and return it to the kernel.
654  */
655
656 static void free_shared_mem(struct s2io_nic *nic)
657 {
658         int i, j, blk_cnt, size;
659         void *tmp_v_addr;
660         dma_addr_t tmp_p_addr;
661         mac_info_t *mac_control;
662         struct config_param *config;
663         int lst_size, lst_per_page;
664         struct net_device *dev = nic->dev;
665
666         if (!nic)
667                 return;
668
669         mac_control = &nic->mac_control;
670         config = &nic->config;
671
672         lst_size = (sizeof(TxD_t) * config->max_txds);
673         lst_per_page = PAGE_SIZE / lst_size;
674
675         for (i = 0; i < config->tx_fifo_num; i++) {
676                 int page_num = TXD_MEM_PAGE_CNT(config->tx_cfg[i].fifo_len,
677                                                 lst_per_page);
678                 for (j = 0; j < page_num; j++) {
679                         int mem_blks = (j * lst_per_page);
680                         if (!mac_control->fifos[i].list_info)
681                                 return; 
682                         if (!mac_control->fifos[i].list_info[mem_blks].
683                                  list_virt_addr)
684                                 break;
685                         pci_free_consistent(nic->pdev, PAGE_SIZE,
686                                             mac_control->fifos[i].
687                                             list_info[mem_blks].
688                                             list_virt_addr,
689                                             mac_control->fifos[i].
690                                             list_info[mem_blks].
691                                             list_phy_addr);
692                 }
693                 /* If we got a zero DMA address during allocation,
694                  * free the page now
695                  */
696                 if (mac_control->zerodma_virt_addr) {
697                         pci_free_consistent(nic->pdev, PAGE_SIZE,
698                                             mac_control->zerodma_virt_addr,
699                                             (dma_addr_t)0);
700                         DBG_PRINT(INIT_DBG, 
701                                 "%s: Freeing TxDL with zero DMA addr. ",
702                                 dev->name);
703                         DBG_PRINT(INIT_DBG, "Virtual address %p\n",
704                                 mac_control->zerodma_virt_addr);
705                 }
706                 kfree(mac_control->fifos[i].list_info);
707         }
708
709         size = SIZE_OF_BLOCK;
710         for (i = 0; i < config->rx_ring_num; i++) {
711                 blk_cnt = mac_control->rings[i].block_count;
712                 for (j = 0; j < blk_cnt; j++) {
713                         tmp_v_addr = mac_control->rings[i].rx_blocks[j].
714                                 block_virt_addr;
715                         tmp_p_addr = mac_control->rings[i].rx_blocks[j].
716                                 block_dma_addr;
717                         if (tmp_v_addr == NULL)
718                                 break;
719                         pci_free_consistent(nic->pdev, size,
720                                             tmp_v_addr, tmp_p_addr);
721                         kfree(mac_control->rings[i].rx_blocks[j].rxds);
722                 }
723         }
724
725         if (nic->rxd_mode >= RXD_MODE_3A) {
726                 /* Freeing buffer storage addresses in 2BUFF mode. */
727                 for (i = 0; i < config->rx_ring_num; i++) {
728                         blk_cnt = config->rx_cfg[i].num_rxd /
729                             (rxd_count[nic->rxd_mode] + 1);
730                         for (j = 0; j < blk_cnt; j++) {
731                                 int k = 0;
732                                 if (!mac_control->rings[i].ba[j])
733                                         continue;
734                                 while (k != rxd_count[nic->rxd_mode]) {
735                                         buffAdd_t *ba =
736                                                 &mac_control->rings[i].ba[j][k];
737                                         kfree(ba->ba_0_org);
738                                         kfree(ba->ba_1_org);
739                                         k++;
740                                 }
741                                 kfree(mac_control->rings[i].ba[j]);
742                         }
743                         kfree(mac_control->rings[i].ba);
744                 }
745         }
746
747         if (mac_control->stats_mem) {
748                 pci_free_consistent(nic->pdev,
749                                     mac_control->stats_mem_sz,
750                                     mac_control->stats_mem,
751                                     mac_control->stats_mem_phy);
752         }
753         if (nic->ufo_in_band_v)
754                 kfree(nic->ufo_in_band_v);
755 }
756
757 /**
758  * s2io_verify_pci_mode -
759  */
760
761 static int s2io_verify_pci_mode(nic_t *nic)
762 {
763         XENA_dev_config_t __iomem *bar0 = nic->bar0;
764         register u64 val64 = 0;
765         int     mode;
766
767         val64 = readq(&bar0->pci_mode);
768         mode = (u8)GET_PCI_MODE(val64);
769
770         if ( val64 & PCI_MODE_UNKNOWN_MODE)
771                 return -1;      /* Unknown PCI mode */
772         return mode;
773 }
774
775
776 /**
777  * s2io_print_pci_mode -
778  */
779 static int s2io_print_pci_mode(nic_t *nic)
780 {
781         XENA_dev_config_t __iomem *bar0 = nic->bar0;
782         register u64 val64 = 0;
783         int     mode;
784         struct config_param *config = &nic->config;
785
786         val64 = readq(&bar0->pci_mode);
787         mode = (u8)GET_PCI_MODE(val64);
788
789         if ( val64 & PCI_MODE_UNKNOWN_MODE)
790                 return -1;      /* Unknown PCI mode */
791
792         if (val64 & PCI_MODE_32_BITS) {
793                 DBG_PRINT(ERR_DBG, "%s: Device is on 32 bit ", nic->dev->name);
794         } else {
795                 DBG_PRINT(ERR_DBG, "%s: Device is on 64 bit ", nic->dev->name);
796         }
797
798         switch(mode) {
799                 case PCI_MODE_PCI_33:
800                         DBG_PRINT(ERR_DBG, "33MHz PCI bus\n");
801                         config->bus_speed = 33;
802                         break;
803                 case PCI_MODE_PCI_66:
804                         DBG_PRINT(ERR_DBG, "66MHz PCI bus\n");
805                         config->bus_speed = 133;
806                         break;
807                 case PCI_MODE_PCIX_M1_66:
808                         DBG_PRINT(ERR_DBG, "66MHz PCIX(M1) bus\n");
809                         config->bus_speed = 133; /* Herc doubles the clock rate */
810                         break;
811                 case PCI_MODE_PCIX_M1_100:
812                         DBG_PRINT(ERR_DBG, "100MHz PCIX(M1) bus\n");
813                         config->bus_speed = 200;
814                         break;
815                 case PCI_MODE_PCIX_M1_133:
816                         DBG_PRINT(ERR_DBG, "133MHz PCIX(M1) bus\n");
817                         config->bus_speed = 266;
818                         break;
819                 case PCI_MODE_PCIX_M2_66:
820                         DBG_PRINT(ERR_DBG, "133MHz PCIX(M2) bus\n");
821                         config->bus_speed = 133;
822                         break;
823                 case PCI_MODE_PCIX_M2_100:
824                         DBG_PRINT(ERR_DBG, "200MHz PCIX(M2) bus\n");
825                         config->bus_speed = 200;
826                         break;
827                 case PCI_MODE_PCIX_M2_133:
828                         DBG_PRINT(ERR_DBG, "266MHz PCIX(M2) bus\n");
829                         config->bus_speed = 266;
830                         break;
831                 default:
832                         return -1;      /* Unsupported bus speed */
833         }
834
835         return mode;
836 }
837
838 /**
839  *  init_nic - Initialization of hardware
840  *  @nic: device peivate variable
841  *  Description: The function sequentially configures every block
842  *  of the H/W from their reset values.
843  *  Return Value:  SUCCESS on success and
844  *  '-1' on failure (endian settings incorrect).
845  */
846
847 static int init_nic(struct s2io_nic *nic)
848 {
849         XENA_dev_config_t __iomem *bar0 = nic->bar0;
850         struct net_device *dev = nic->dev;
851         register u64 val64 = 0;
852         void __iomem *add;
853         u32 time;
854         int i, j;
855         mac_info_t *mac_control;
856         struct config_param *config;
857         int mdio_cnt = 0, dtx_cnt = 0;
858         unsigned long long mem_share;
859         int mem_size;
860
861         mac_control = &nic->mac_control;
862         config = &nic->config;
863
864         /* to set the swapper controle on the card */
865         if(s2io_set_swapper(nic)) {
866                 DBG_PRINT(ERR_DBG,"ERROR: Setting Swapper failed\n");
867                 return -1;
868         }
869
870         /*
871          * Herc requires EOI to be removed from reset before XGXS, so..
872          */
873         if (nic->device_type & XFRAME_II_DEVICE) {
874                 val64 = 0xA500000000ULL;
875                 writeq(val64, &bar0->sw_reset);
876                 msleep(500);
877                 val64 = readq(&bar0->sw_reset);
878         }
879
880         /* Remove XGXS from reset state */
881         val64 = 0;
882         writeq(val64, &bar0->sw_reset);
883         msleep(500);
884         val64 = readq(&bar0->sw_reset);
885
886         /*  Enable Receiving broadcasts */
887         add = &bar0->mac_cfg;
888         val64 = readq(&bar0->mac_cfg);
889         val64 |= MAC_RMAC_BCAST_ENABLE;
890         writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
891         writel((u32) val64, add);
892         writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
893         writel((u32) (val64 >> 32), (add + 4));
894
895         /* Read registers in all blocks */
896         val64 = readq(&bar0->mac_int_mask);
897         val64 = readq(&bar0->mc_int_mask);
898         val64 = readq(&bar0->xgxs_int_mask);
899
900         /*  Set MTU */
901         val64 = dev->mtu;
902         writeq(vBIT(val64, 2, 14), &bar0->rmac_max_pyld_len);
903
904         /*
905          * Configuring the XAUI Interface of Xena.
906          * ***************************************
907          * To Configure the Xena's XAUI, one has to write a series
908          * of 64 bit values into two registers in a particular
909          * sequence. Hence a macro 'SWITCH_SIGN' has been defined
910          * which will be defined in the array of configuration values
911          * (xena_dtx_cfg & xena_mdio_cfg) at appropriate places
912          * to switch writing from one regsiter to another. We continue
913          * writing these values until we encounter the 'END_SIGN' macro.
914          * For example, After making a series of 21 writes into
915          * dtx_control register the 'SWITCH_SIGN' appears and hence we
916          * start writing into mdio_control until we encounter END_SIGN.
917          */
918         if (nic->device_type & XFRAME_II_DEVICE) {
919                 while (herc_act_dtx_cfg[dtx_cnt] != END_SIGN) {
920                         SPECIAL_REG_WRITE(herc_act_dtx_cfg[dtx_cnt],
921                                           &bar0->dtx_control, UF);
922                         if (dtx_cnt & 0x1)
923                                 msleep(1); /* Necessary!! */
924                         dtx_cnt++;
925                 }
926         } else {
927                 while (1) {
928                       dtx_cfg:
929                         while (xena_dtx_cfg[dtx_cnt] != END_SIGN) {
930                                 if (xena_dtx_cfg[dtx_cnt] == SWITCH_SIGN) {
931                                         dtx_cnt++;
932                                         goto mdio_cfg;
933                                 }
934                                 SPECIAL_REG_WRITE(xena_dtx_cfg[dtx_cnt],
935                                                   &bar0->dtx_control, UF);
936                                 val64 = readq(&bar0->dtx_control);
937                                 dtx_cnt++;
938                         }
939                       mdio_cfg:
940                         while (xena_mdio_cfg[mdio_cnt] != END_SIGN) {
941                                 if (xena_mdio_cfg[mdio_cnt] == SWITCH_SIGN) {
942                                         mdio_cnt++;
943                                         goto dtx_cfg;
944                                 }
945                                 SPECIAL_REG_WRITE(xena_mdio_cfg[mdio_cnt],
946                                                   &bar0->mdio_control, UF);
947                                 val64 = readq(&bar0->mdio_control);
948                                 mdio_cnt++;
949                         }
950                         if ((xena_dtx_cfg[dtx_cnt] == END_SIGN) &&
951                             (xena_mdio_cfg[mdio_cnt] == END_SIGN)) {
952                                 break;
953                         } else {
954                                 goto dtx_cfg;
955                         }
956                 }
957         }
958
959         /*  Tx DMA Initialization */
960         val64 = 0;
961         writeq(val64, &bar0->tx_fifo_partition_0);
962         writeq(val64, &bar0->tx_fifo_partition_1);
963         writeq(val64, &bar0->tx_fifo_partition_2);
964         writeq(val64, &bar0->tx_fifo_partition_3);
965
966
967         for (i = 0, j = 0; i < config->tx_fifo_num; i++) {
968                 val64 |=
969                     vBIT(config->tx_cfg[i].fifo_len - 1, ((i * 32) + 19),
970                          13) | vBIT(config->tx_cfg[i].fifo_priority,
971                                     ((i * 32) + 5), 3);
972
973                 if (i == (config->tx_fifo_num - 1)) {
974                         if (i % 2 == 0)
975                                 i++;
976                 }
977
978                 switch (i) {
979                 case 1:
980                         writeq(val64, &bar0->tx_fifo_partition_0);
981                         val64 = 0;
982                         break;
983                 case 3:
984                         writeq(val64, &bar0->tx_fifo_partition_1);
985                         val64 = 0;
986                         break;
987                 case 5:
988                         writeq(val64, &bar0->tx_fifo_partition_2);
989                         val64 = 0;
990                         break;
991                 case 7:
992                         writeq(val64, &bar0->tx_fifo_partition_3);
993                         break;
994                 }
995         }
996
997         /* Enable Tx FIFO partition 0. */
998         val64 = readq(&bar0->tx_fifo_partition_0);
999         val64 |= BIT(0);        /* To enable the FIFO partition. */
1000         writeq(val64, &bar0->tx_fifo_partition_0);
1001
1002         /*
1003          * Disable 4 PCCs for Xena1, 2 and 3 as per H/W bug
1004          * SXE-008 TRANSMIT DMA ARBITRATION ISSUE.
1005          */
1006         if ((nic->device_type == XFRAME_I_DEVICE) &&
1007                 (get_xena_rev_id(nic->pdev) < 4))
1008                 writeq(PCC_ENABLE_FOUR, &bar0->pcc_enable);
1009
1010         val64 = readq(&bar0->tx_fifo_partition_0);
1011         DBG_PRINT(INIT_DBG, "Fifo partition at: 0x%p is: 0x%llx\n",
1012                   &bar0->tx_fifo_partition_0, (unsigned long long) val64);
1013
1014         /*
1015          * Initialization of Tx_PA_CONFIG register to ignore packet
1016          * integrity checking.
1017          */
1018         val64 = readq(&bar0->tx_pa_cfg);
1019         val64 |= TX_PA_CFG_IGNORE_FRM_ERR | TX_PA_CFG_IGNORE_SNAP_OUI |
1020             TX_PA_CFG_IGNORE_LLC_CTRL | TX_PA_CFG_IGNORE_L2_ERR;
1021         writeq(val64, &bar0->tx_pa_cfg);
1022
1023         /* Rx DMA intialization. */
1024         val64 = 0;
1025         for (i = 0; i < config->rx_ring_num; i++) {
1026                 val64 |=
1027                     vBIT(config->rx_cfg[i].ring_priority, (5 + (i * 8)),
1028                          3);
1029         }
1030         writeq(val64, &bar0->rx_queue_priority);
1031
1032         /*
1033          * Allocating equal share of memory to all the
1034          * configured Rings.
1035          */
1036         val64 = 0;
1037         if (nic->device_type & XFRAME_II_DEVICE)
1038                 mem_size = 32;
1039         else
1040                 mem_size = 64;
1041
1042         for (i = 0; i < config->rx_ring_num; i++) {
1043                 switch (i) {
1044                 case 0:
1045                         mem_share = (mem_size / config->rx_ring_num +
1046                                      mem_size % config->rx_ring_num);
1047                         val64 |= RX_QUEUE_CFG_Q0_SZ(mem_share);
1048                         continue;
1049                 case 1:
1050                         mem_share = (mem_size / config->rx_ring_num);
1051                         val64 |= RX_QUEUE_CFG_Q1_SZ(mem_share);
1052                         continue;
1053                 case 2:
1054                         mem_share = (mem_size / config->rx_ring_num);
1055                         val64 |= RX_QUEUE_CFG_Q2_SZ(mem_share);
1056                         continue;
1057                 case 3:
1058                         mem_share = (mem_size / config->rx_ring_num);
1059                         val64 |= RX_QUEUE_CFG_Q3_SZ(mem_share);
1060                         continue;
1061                 case 4:
1062                         mem_share = (mem_size / config->rx_ring_num);
1063                         val64 |= RX_QUEUE_CFG_Q4_SZ(mem_share);
1064                         continue;
1065                 case 5:
1066                         mem_share = (mem_size / config->rx_ring_num);
1067                         val64 |= RX_QUEUE_CFG_Q5_SZ(mem_share);
1068                         continue;
1069                 case 6:
1070                         mem_share = (mem_size / config->rx_ring_num);
1071                         val64 |= RX_QUEUE_CFG_Q6_SZ(mem_share);
1072                         continue;
1073                 case 7:
1074                         mem_share = (mem_size / config->rx_ring_num);
1075                         val64 |= RX_QUEUE_CFG_Q7_SZ(mem_share);
1076                         continue;
1077                 }
1078         }
1079         writeq(val64, &bar0->rx_queue_cfg);
1080
1081         /*
1082          * Filling Tx round robin registers
1083          * as per the number of FIFOs
1084          */
1085         switch (config->tx_fifo_num) {
1086         case 1:
1087                 val64 = 0x0000000000000000ULL;
1088                 writeq(val64, &bar0->tx_w_round_robin_0);
1089                 writeq(val64, &bar0->tx_w_round_robin_1);
1090                 writeq(val64, &bar0->tx_w_round_robin_2);
1091                 writeq(val64, &bar0->tx_w_round_robin_3);
1092                 writeq(val64, &bar0->tx_w_round_robin_4);
1093                 break;
1094         case 2:
1095                 val64 = 0x0000010000010000ULL;
1096                 writeq(val64, &bar0->tx_w_round_robin_0);
1097                 val64 = 0x0100000100000100ULL;
1098                 writeq(val64, &bar0->tx_w_round_robin_1);
1099                 val64 = 0x0001000001000001ULL;
1100                 writeq(val64, &bar0->tx_w_round_robin_2);
1101                 val64 = 0x0000010000010000ULL;
1102                 writeq(val64, &bar0->tx_w_round_robin_3);
1103                 val64 = 0x0100000000000000ULL;
1104                 writeq(val64, &bar0->tx_w_round_robin_4);
1105                 break;
1106         case 3:
1107                 val64 = 0x0001000102000001ULL;
1108                 writeq(val64, &bar0->tx_w_round_robin_0);
1109                 val64 = 0x0001020000010001ULL;
1110                 writeq(val64, &bar0->tx_w_round_robin_1);
1111                 val64 = 0x0200000100010200ULL;
1112                 writeq(val64, &bar0->tx_w_round_robin_2);
1113                 val64 = 0x0001000102000001ULL;
1114                 writeq(val64, &bar0->tx_w_round_robin_3);
1115                 val64 = 0x0001020000000000ULL;
1116                 writeq(val64, &bar0->tx_w_round_robin_4);
1117                 break;
1118         case 4:
1119                 val64 = 0x0001020300010200ULL;
1120                 writeq(val64, &bar0->tx_w_round_robin_0);
1121                 val64 = 0x0100000102030001ULL;
1122                 writeq(val64, &bar0->tx_w_round_robin_1);
1123                 val64 = 0x0200010000010203ULL;
1124                 writeq(val64, &bar0->tx_w_round_robin_2);
1125                 val64 = 0x0001020001000001ULL;
1126                 writeq(val64, &bar0->tx_w_round_robin_3);
1127                 val64 = 0x0203000100000000ULL;
1128                 writeq(val64, &bar0->tx_w_round_robin_4);
1129                 break;
1130         case 5:
1131                 val64 = 0x0001000203000102ULL;
1132                 writeq(val64, &bar0->tx_w_round_robin_0);
1133                 val64 = 0x0001020001030004ULL;
1134                 writeq(val64, &bar0->tx_w_round_robin_1);
1135                 val64 = 0x0001000203000102ULL;
1136                 writeq(val64, &bar0->tx_w_round_robin_2);
1137                 val64 = 0x0001020001030004ULL;
1138                 writeq(val64, &bar0->tx_w_round_robin_3);
1139                 val64 = 0x0001000000000000ULL;
1140                 writeq(val64, &bar0->tx_w_round_robin_4);
1141                 break;
1142         case 6:
1143                 val64 = 0x0001020304000102ULL;
1144                 writeq(val64, &bar0->tx_w_round_robin_0);
1145                 val64 = 0x0304050001020001ULL;
1146                 writeq(val64, &bar0->tx_w_round_robin_1);
1147                 val64 = 0x0203000100000102ULL;
1148                 writeq(val64, &bar0->tx_w_round_robin_2);
1149                 val64 = 0x0304000102030405ULL;
1150                 writeq(val64, &bar0->tx_w_round_robin_3);
1151                 val64 = 0x0001000200000000ULL;
1152                 writeq(val64, &bar0->tx_w_round_robin_4);
1153                 break;
1154         case 7:
1155                 val64 = 0x0001020001020300ULL;
1156                 writeq(val64, &bar0->tx_w_round_robin_0);
1157                 val64 = 0x0102030400010203ULL;
1158                 writeq(val64, &bar0->tx_w_round_robin_1);
1159                 val64 = 0x0405060001020001ULL;
1160                 writeq(val64, &bar0->tx_w_round_robin_2);
1161                 val64 = 0x0304050000010200ULL;
1162                 writeq(val64, &bar0->tx_w_round_robin_3);
1163                 val64 = 0x0102030000000000ULL;
1164                 writeq(val64, &bar0->tx_w_round_robin_4);
1165                 break;
1166         case 8:
1167                 val64 = 0x0001020300040105ULL;
1168                 writeq(val64, &bar0->tx_w_round_robin_0);
1169                 val64 = 0x0200030106000204ULL;
1170                 writeq(val64, &bar0->tx_w_round_robin_1);
1171                 val64 = 0x0103000502010007ULL;
1172                 writeq(val64, &bar0->tx_w_round_robin_2);
1173                 val64 = 0x0304010002060500ULL;
1174                 writeq(val64, &bar0->tx_w_round_robin_3);
1175                 val64 = 0x0103020400000000ULL;
1176                 writeq(val64, &bar0->tx_w_round_robin_4);
1177                 break;
1178         }
1179
1180         /* Filling the Rx round robin registers as per the
1181          * number of Rings and steering based on QoS.
1182          */
1183         switch (config->rx_ring_num) {
1184         case 1:
1185                 val64 = 0x8080808080808080ULL;
1186                 writeq(val64, &bar0->rts_qos_steering);
1187                 break;
1188         case 2:
1189                 val64 = 0x0000010000010000ULL;
1190                 writeq(val64, &bar0->rx_w_round_robin_0);
1191                 val64 = 0x0100000100000100ULL;
1192                 writeq(val64, &bar0->rx_w_round_robin_1);
1193                 val64 = 0x0001000001000001ULL;
1194                 writeq(val64, &bar0->rx_w_round_robin_2);
1195                 val64 = 0x0000010000010000ULL;
1196                 writeq(val64, &bar0->rx_w_round_robin_3);
1197                 val64 = 0x0100000000000000ULL;
1198                 writeq(val64, &bar0->rx_w_round_robin_4);
1199
1200                 val64 = 0x8080808040404040ULL;
1201                 writeq(val64, &bar0->rts_qos_steering);
1202                 break;
1203         case 3:
1204                 val64 = 0x0001000102000001ULL;
1205                 writeq(val64, &bar0->rx_w_round_robin_0);
1206                 val64 = 0x0001020000010001ULL;
1207                 writeq(val64, &bar0->rx_w_round_robin_1);
1208                 val64 = 0x0200000100010200ULL;
1209                 writeq(val64, &bar0->rx_w_round_robin_2);
1210                 val64 = 0x0001000102000001ULL;
1211                 writeq(val64, &bar0->rx_w_round_robin_3);
1212                 val64 = 0x0001020000000000ULL;
1213                 writeq(val64, &bar0->rx_w_round_robin_4);
1214
1215                 val64 = 0x8080804040402020ULL;
1216                 writeq(val64, &bar0->rts_qos_steering);
1217                 break;
1218         case 4:
1219                 val64 = 0x0001020300010200ULL;
1220                 writeq(val64, &bar0->rx_w_round_robin_0);
1221                 val64 = 0x0100000102030001ULL;
1222                 writeq(val64, &bar0->rx_w_round_robin_1);
1223                 val64 = 0x0200010000010203ULL;
1224                 writeq(val64, &bar0->rx_w_round_robin_2);
1225                 val64 = 0x0001020001000001ULL;  
1226                 writeq(val64, &bar0->rx_w_round_robin_3);
1227                 val64 = 0x0203000100000000ULL;
1228                 writeq(val64, &bar0->rx_w_round_robin_4);
1229
1230                 val64 = 0x8080404020201010ULL;
1231                 writeq(val64, &bar0->rts_qos_steering);
1232                 break;
1233         case 5:
1234                 val64 = 0x0001000203000102ULL;
1235                 writeq(val64, &bar0->rx_w_round_robin_0);
1236                 val64 = 0x0001020001030004ULL;
1237                 writeq(val64, &bar0->rx_w_round_robin_1);
1238                 val64 = 0x0001000203000102ULL;
1239                 writeq(val64, &bar0->rx_w_round_robin_2);
1240                 val64 = 0x0001020001030004ULL;
1241                 writeq(val64, &bar0->rx_w_round_robin_3);
1242                 val64 = 0x0001000000000000ULL;
1243                 writeq(val64, &bar0->rx_w_round_robin_4);
1244
1245                 val64 = 0x8080404020201008ULL;
1246                 writeq(val64, &bar0->rts_qos_steering);
1247                 break;
1248         case 6:
1249                 val64 = 0x0001020304000102ULL;
1250                 writeq(val64, &bar0->rx_w_round_robin_0);
1251                 val64 = 0x0304050001020001ULL;
1252                 writeq(val64, &bar0->rx_w_round_robin_1);
1253                 val64 = 0x0203000100000102ULL;
1254                 writeq(val64, &bar0->rx_w_round_robin_2);
1255                 val64 = 0x0304000102030405ULL;
1256                 writeq(val64, &bar0->rx_w_round_robin_3);
1257                 val64 = 0x0001000200000000ULL;
1258                 writeq(val64, &bar0->rx_w_round_robin_4);
1259
1260                 val64 = 0x8080404020100804ULL;
1261                 writeq(val64, &bar0->rts_qos_steering);
1262                 break;
1263         case 7:
1264                 val64 = 0x0001020001020300ULL;
1265                 writeq(val64, &bar0->rx_w_round_robin_0);
1266                 val64 = 0x0102030400010203ULL;
1267                 writeq(val64, &bar0->rx_w_round_robin_1);
1268                 val64 = 0x0405060001020001ULL;
1269                 writeq(val64, &bar0->rx_w_round_robin_2);
1270                 val64 = 0x0304050000010200ULL;
1271                 writeq(val64, &bar0->rx_w_round_robin_3);
1272                 val64 = 0x0102030000000000ULL;
1273                 writeq(val64, &bar0->rx_w_round_robin_4);
1274
1275                 val64 = 0x8080402010080402ULL;
1276                 writeq(val64, &bar0->rts_qos_steering);
1277                 break;
1278         case 8:
1279                 val64 = 0x0001020300040105ULL;
1280                 writeq(val64, &bar0->rx_w_round_robin_0);
1281                 val64 = 0x0200030106000204ULL;
1282                 writeq(val64, &bar0->rx_w_round_robin_1);
1283                 val64 = 0x0103000502010007ULL;
1284                 writeq(val64, &bar0->rx_w_round_robin_2);
1285                 val64 = 0x0304010002060500ULL;
1286                 writeq(val64, &bar0->rx_w_round_robin_3);
1287                 val64 = 0x0103020400000000ULL;
1288                 writeq(val64, &bar0->rx_w_round_robin_4);
1289
1290                 val64 = 0x8040201008040201ULL;
1291                 writeq(val64, &bar0->rts_qos_steering);
1292                 break;
1293         }
1294
1295         /* UDP Fix */
1296         val64 = 0;
1297         for (i = 0; i < 8; i++)
1298                 writeq(val64, &bar0->rts_frm_len_n[i]);
1299
1300         /* Set the default rts frame length for the rings configured */
1301         val64 = MAC_RTS_FRM_LEN_SET(dev->mtu+22);
1302         for (i = 0 ; i < config->rx_ring_num ; i++)
1303                 writeq(val64, &bar0->rts_frm_len_n[i]);
1304
1305         /* Set the frame length for the configured rings
1306          * desired by the user
1307          */
1308         for (i = 0; i < config->rx_ring_num; i++) {
1309                 /* If rts_frm_len[i] == 0 then it is assumed that user not
1310                  * specified frame length steering.
1311                  * If the user provides the frame length then program
1312                  * the rts_frm_len register for those values or else
1313                  * leave it as it is.
1314                  */
1315                 if (rts_frm_len[i] != 0) {
1316                         writeq(MAC_RTS_FRM_LEN_SET(rts_frm_len[i]),
1317                                 &bar0->rts_frm_len_n[i]);
1318                 }
1319         }
1320
1321         /* Program statistics memory */
1322         writeq(mac_control->stats_mem_phy, &bar0->stat_addr);
1323
1324         if (nic->device_type == XFRAME_II_DEVICE) {
1325                 val64 = STAT_BC(0x320);
1326                 writeq(val64, &bar0->stat_byte_cnt);
1327         }
1328
1329         /*
1330          * Initializing the sampling rate for the device to calculate the
1331          * bandwidth utilization.
1332          */
1333         val64 = MAC_TX_LINK_UTIL_VAL(tmac_util_period) |
1334             MAC_RX_LINK_UTIL_VAL(rmac_util_period);
1335         writeq(val64, &bar0->mac_link_util);
1336
1337
1338         /*
1339          * Initializing the Transmit and Receive Traffic Interrupt
1340          * Scheme.
1341          */
1342         /*
1343          * TTI Initialization. Default Tx timer gets us about
1344          * 250 interrupts per sec. Continuous interrupts are enabled
1345          * by default.
1346          */
1347         if (nic->device_type == XFRAME_II_DEVICE) {
1348                 int count = (nic->config.bus_speed * 125)/2;
1349                 val64 = TTI_DATA1_MEM_TX_TIMER_VAL(count);
1350         } else {
1351
1352                 val64 = TTI_DATA1_MEM_TX_TIMER_VAL(0x2078);
1353         }
1354         val64 |= TTI_DATA1_MEM_TX_URNG_A(0xA) |
1355             TTI_DATA1_MEM_TX_URNG_B(0x10) |
1356             TTI_DATA1_MEM_TX_URNG_C(0x30) | TTI_DATA1_MEM_TX_TIMER_AC_EN;
1357                 if (use_continuous_tx_intrs)
1358                         val64 |= TTI_DATA1_MEM_TX_TIMER_CI_EN;
1359         writeq(val64, &bar0->tti_data1_mem);
1360
1361         val64 = TTI_DATA2_MEM_TX_UFC_A(0x10) |
1362             TTI_DATA2_MEM_TX_UFC_B(0x20) |
1363             TTI_DATA2_MEM_TX_UFC_C(0x70) | TTI_DATA2_MEM_TX_UFC_D(0x80);
1364         writeq(val64, &bar0->tti_data2_mem);
1365
1366         val64 = TTI_CMD_MEM_WE | TTI_CMD_MEM_STROBE_NEW_CMD;
1367         writeq(val64, &bar0->tti_command_mem);
1368
1369         /*
1370          * Once the operation completes, the Strobe bit of the command
1371          * register will be reset. We poll for this particular condition
1372          * We wait for a maximum of 500ms for the operation to complete,
1373          * if it's not complete by then we return error.
1374          */
1375         time = 0;
1376         while (TRUE) {
1377                 val64 = readq(&bar0->tti_command_mem);
1378                 if (!(val64 & TTI_CMD_MEM_STROBE_NEW_CMD)) {
1379                         break;
1380                 }
1381                 if (time > 10) {
1382                         DBG_PRINT(ERR_DBG, "%s: TTI init Failed\n",
1383                                   dev->name);
1384                         return -1;
1385                 }
1386                 msleep(50);
1387                 time++;
1388         }
1389
1390         if (nic->config.bimodal) {
1391                 int k = 0;
1392                 for (k = 0; k < config->rx_ring_num; k++) {
1393                         val64 = TTI_CMD_MEM_WE | TTI_CMD_MEM_STROBE_NEW_CMD;
1394                         val64 |= TTI_CMD_MEM_OFFSET(0x38+k);
1395                         writeq(val64, &bar0->tti_command_mem);
1396
1397                 /*
1398                  * Once the operation completes, the Strobe bit of the command
1399                  * register will be reset. We poll for this particular condition
1400                  * We wait for a maximum of 500ms for the operation to complete,
1401                  * if it's not complete by then we return error.
1402                 */
1403                         time = 0;
1404                         while (TRUE) {
1405                                 val64 = readq(&bar0->tti_command_mem);
1406                                 if (!(val64 & TTI_CMD_MEM_STROBE_NEW_CMD)) {
1407                                         break;
1408                                 }
1409                                 if (time > 10) {
1410                                         DBG_PRINT(ERR_DBG,
1411                                                 "%s: TTI init Failed\n",
1412                                         dev->name);
1413                                         return -1;
1414                                 }
1415                                 time++;
1416                                 msleep(50);
1417                         }
1418                 }
1419         } else {
1420
1421                 /* RTI Initialization */
1422                 if (nic->device_type == XFRAME_II_DEVICE) {
1423                         /*
1424                          * Programmed to generate Apprx 500 Intrs per
1425                          * second
1426                          */
1427                         int count = (nic->config.bus_speed * 125)/4;
1428                         val64 = RTI_DATA1_MEM_RX_TIMER_VAL(count);
1429                 } else {
1430                         val64 = RTI_DATA1_MEM_RX_TIMER_VAL(0xFFF);
1431                 }
1432                 val64 |= RTI_DATA1_MEM_RX_URNG_A(0xA) |
1433                     RTI_DATA1_MEM_RX_URNG_B(0x10) |
1434                     RTI_DATA1_MEM_RX_URNG_C(0x30) | RTI_DATA1_MEM_RX_TIMER_AC_EN;
1435
1436                 writeq(val64, &bar0->rti_data1_mem);
1437
1438                 val64 = RTI_DATA2_MEM_RX_UFC_A(0x1) |
1439                     RTI_DATA2_MEM_RX_UFC_B(0x2) ;
1440                 if (nic->intr_type == MSI_X)
1441                     val64 |= (RTI_DATA2_MEM_RX_UFC_C(0x20) | \
1442                                 RTI_DATA2_MEM_RX_UFC_D(0x40));
1443                 else
1444                     val64 |= (RTI_DATA2_MEM_RX_UFC_C(0x40) | \
1445                                 RTI_DATA2_MEM_RX_UFC_D(0x80));
1446                 writeq(val64, &bar0->rti_data2_mem);
1447
1448                 for (i = 0; i < config->rx_ring_num; i++) {
1449                         val64 = RTI_CMD_MEM_WE | RTI_CMD_MEM_STROBE_NEW_CMD
1450                                         | RTI_CMD_MEM_OFFSET(i);
1451                         writeq(val64, &bar0->rti_command_mem);
1452
1453                         /*
1454                          * Once the operation completes, the Strobe bit of the
1455                          * command register will be reset. We poll for this
1456                          * particular condition. We wait for a maximum of 500ms
1457                          * for the operation to complete, if it's not complete
1458                          * by then we return error.
1459                          */
1460                         time = 0;
1461                         while (TRUE) {
1462                                 val64 = readq(&bar0->rti_command_mem);
1463                                 if (!(val64 & RTI_CMD_MEM_STROBE_NEW_CMD)) {
1464                                         break;
1465                                 }
1466                                 if (time > 10) {
1467                                         DBG_PRINT(ERR_DBG, "%s: RTI init Failed\n",
1468                                                   dev->name);
1469                                         return -1;
1470                                 }
1471                                 time++;
1472                                 msleep(50);
1473                         }
1474                 }
1475         }
1476
1477         /*
1478          * Initializing proper values as Pause threshold into all
1479          * the 8 Queues on Rx side.
1480          */
1481         writeq(0xffbbffbbffbbffbbULL, &bar0->mc_pause_thresh_q0q3);
1482         writeq(0xffbbffbbffbbffbbULL, &bar0->mc_pause_thresh_q4q7);
1483
1484         /* Disable RMAC PAD STRIPPING */
1485         add = &bar0->mac_cfg;
1486         val64 = readq(&bar0->mac_cfg);
1487         val64 &= ~(MAC_CFG_RMAC_STRIP_PAD);
1488         writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
1489         writel((u32) (val64), add);
1490         writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
1491         writel((u32) (val64 >> 32), (add + 4));
1492         val64 = readq(&bar0->mac_cfg);
1493
1494         /* Enable FCS stripping by adapter */
1495         add = &bar0->mac_cfg;
1496         val64 = readq(&bar0->mac_cfg);
1497         val64 |= MAC_CFG_RMAC_STRIP_FCS;
1498         if (nic->device_type == XFRAME_II_DEVICE)
1499                 writeq(val64, &bar0->mac_cfg);
1500         else {
1501                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
1502                 writel((u32) (val64), add);
1503                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
1504                 writel((u32) (val64 >> 32), (add + 4));
1505         }
1506
1507         /*
1508          * Set the time value to be inserted in the pause frame
1509          * generated by xena.
1510          */
1511         val64 = readq(&bar0->rmac_pause_cfg);
1512         val64 &= ~(RMAC_PAUSE_HG_PTIME(0xffff));
1513         val64 |= RMAC_PAUSE_HG_PTIME(nic->mac_control.rmac_pause_time);
1514         writeq(val64, &bar0->rmac_pause_cfg);
1515
1516         /*
1517          * Set the Threshold Limit for Generating the pause frame
1518          * If the amount of data in any Queue exceeds ratio of
1519          * (mac_control.mc_pause_threshold_q0q3 or q4q7)/256
1520          * pause frame is generated
1521          */
1522         val64 = 0;
1523         for (i = 0; i < 4; i++) {
1524                 val64 |=
1525                     (((u64) 0xFF00 | nic->mac_control.
1526                       mc_pause_threshold_q0q3)
1527                      << (i * 2 * 8));
1528         }
1529         writeq(val64, &bar0->mc_pause_thresh_q0q3);
1530
1531         val64 = 0;
1532         for (i = 0; i < 4; i++) {
1533                 val64 |=
1534                     (((u64) 0xFF00 | nic->mac_control.
1535                       mc_pause_threshold_q4q7)
1536                      << (i * 2 * 8));
1537         }
1538         writeq(val64, &bar0->mc_pause_thresh_q4q7);
1539
1540         /*
1541          * TxDMA will stop Read request if the number of read split has
1542          * exceeded the limit pointed by shared_splits
1543          */
1544         val64 = readq(&bar0->pic_control);
1545         val64 |= PIC_CNTL_SHARED_SPLITS(shared_splits);
1546         writeq(val64, &bar0->pic_control);
1547
1548         /*
1549          * Programming the Herc to split every write transaction
1550          * that does not start on an ADB to reduce disconnects.
1551          */
1552         if (nic->device_type == XFRAME_II_DEVICE) {
1553                 val64 = WREQ_SPLIT_MASK_SET_MASK(255);
1554                 writeq(val64, &bar0->wreq_split_mask);
1555         }
1556
1557         /* Setting Link stability period to 64 ms */ 
1558         if (nic->device_type == XFRAME_II_DEVICE) {
1559                 val64 = MISC_LINK_STABILITY_PRD(3);
1560                 writeq(val64, &bar0->misc_control);
1561         }
1562
1563         return SUCCESS;
1564 }
1565 #define LINK_UP_DOWN_INTERRUPT          1
1566 #define MAC_RMAC_ERR_TIMER              2
1567
1568 static int s2io_link_fault_indication(nic_t *nic)
1569 {
1570         if (nic->intr_type != INTA)
1571                 return MAC_RMAC_ERR_TIMER;
1572         if (nic->device_type == XFRAME_II_DEVICE)
1573                 return LINK_UP_DOWN_INTERRUPT;
1574         else
1575                 return MAC_RMAC_ERR_TIMER;
1576 }
1577
1578 /**
1579  *  en_dis_able_nic_intrs - Enable or Disable the interrupts
1580  *  @nic: device private variable,
1581  *  @mask: A mask indicating which Intr block must be modified and,
1582  *  @flag: A flag indicating whether to enable or disable the Intrs.
1583  *  Description: This function will either disable or enable the interrupts
1584  *  depending on the flag argument. The mask argument can be used to
1585  *  enable/disable any Intr block.
1586  *  Return Value: NONE.
1587  */
1588
1589 static void en_dis_able_nic_intrs(struct s2io_nic *nic, u16 mask, int flag)
1590 {
1591         XENA_dev_config_t __iomem *bar0 = nic->bar0;
1592         register u64 val64 = 0, temp64 = 0;
1593
1594         /*  Top level interrupt classification */
1595         /*  PIC Interrupts */
1596         if ((mask & (TX_PIC_INTR | RX_PIC_INTR))) {
1597                 /*  Enable PIC Intrs in the general intr mask register */
1598                 val64 = TXPIC_INT_M | PIC_RX_INT_M;
1599                 if (flag == ENABLE_INTRS) {
1600                         temp64 = readq(&bar0->general_int_mask);
1601                         temp64 &= ~((u64) val64);
1602                         writeq(temp64, &bar0->general_int_mask);
1603                         /*
1604                          * If Hercules adapter enable GPIO otherwise
1605                          * disabled all PCIX, Flash, MDIO, IIC and GPIO
1606                          * interrupts for now.
1607                          * TODO
1608                          */
1609                         if (s2io_link_fault_indication(nic) ==
1610                                         LINK_UP_DOWN_INTERRUPT ) {
1611                                 temp64 = readq(&bar0->pic_int_mask);
1612                                 temp64 &= ~((u64) PIC_INT_GPIO);
1613                                 writeq(temp64, &bar0->pic_int_mask);
1614                                 temp64 = readq(&bar0->gpio_int_mask);
1615                                 temp64 &= ~((u64) GPIO_INT_MASK_LINK_UP);
1616                                 writeq(temp64, &bar0->gpio_int_mask);
1617                         } else {
1618                                 writeq(DISABLE_ALL_INTRS, &bar0->pic_int_mask);
1619                         }
1620                         /*
1621                          * No MSI Support is available presently, so TTI and
1622                          * RTI interrupts are also disabled.
1623                          */
1624                 } else if (flag == DISABLE_INTRS) {
1625                         /*
1626                          * Disable PIC Intrs in the general
1627                          * intr mask register
1628                          */
1629                         writeq(DISABLE_ALL_INTRS, &bar0->pic_int_mask);
1630                         temp64 = readq(&bar0->general_int_mask);
1631                         val64 |= temp64;
1632                         writeq(val64, &bar0->general_int_mask);
1633                 }
1634         }
1635
1636         /*  DMA Interrupts */
1637         /*  Enabling/Disabling Tx DMA interrupts */
1638         if (mask & TX_DMA_INTR) {
1639                 /* Enable TxDMA Intrs in the general intr mask register */
1640                 val64 = TXDMA_INT_M;
1641                 if (flag == ENABLE_INTRS) {
1642                         temp64 = readq(&bar0->general_int_mask);
1643                         temp64 &= ~((u64) val64);
1644                         writeq(temp64, &bar0->general_int_mask);
1645                         /*
1646                          * Keep all interrupts other than PFC interrupt
1647                          * and PCC interrupt disabled in DMA level.
1648                          */
1649                         val64 = DISABLE_ALL_INTRS & ~(TXDMA_PFC_INT_M |
1650                                                       TXDMA_PCC_INT_M);
1651                         writeq(val64, &bar0->txdma_int_mask);
1652                         /*
1653                          * Enable only the MISC error 1 interrupt in PFC block
1654                          */
1655                         val64 = DISABLE_ALL_INTRS & (~PFC_MISC_ERR_1);
1656                         writeq(val64, &bar0->pfc_err_mask);
1657                         /*
1658                          * Enable only the FB_ECC error interrupt in PCC block
1659                          */
1660                         val64 = DISABLE_ALL_INTRS & (~PCC_FB_ECC_ERR);
1661                         writeq(val64, &bar0->pcc_err_mask);
1662                 } else if (flag == DISABLE_INTRS) {
1663                         /*
1664                          * Disable TxDMA Intrs in the general intr mask
1665                          * register
1666                          */
1667                         writeq(DISABLE_ALL_INTRS, &bar0->txdma_int_mask);
1668                         writeq(DISABLE_ALL_INTRS, &bar0->pfc_err_mask);
1669                         temp64 = readq(&bar0->general_int_mask);
1670                         val64 |= temp64;
1671                         writeq(val64, &bar0->general_int_mask);
1672                 }
1673         }
1674
1675         /*  Enabling/Disabling Rx DMA interrupts */
1676         if (mask & RX_DMA_INTR) {
1677                 /*  Enable RxDMA Intrs in the general intr mask register */
1678                 val64 = RXDMA_INT_M;
1679                 if (flag == ENABLE_INTRS) {
1680                         temp64 = readq(&bar0->general_int_mask);
1681                         temp64 &= ~((u64) val64);
1682                         writeq(temp64, &bar0->general_int_mask);
1683                         /*
1684                          * All RxDMA block interrupts are disabled for now
1685                          * TODO
1686                          */
1687                         writeq(DISABLE_ALL_INTRS, &bar0->rxdma_int_mask);
1688                 } else if (flag == DISABLE_INTRS) {
1689                         /*
1690                          * Disable RxDMA Intrs in the general intr mask
1691                          * register
1692                          */
1693                         writeq(DISABLE_ALL_INTRS, &bar0->rxdma_int_mask);
1694                         temp64 = readq(&bar0->general_int_mask);
1695                         val64 |= temp64;
1696                         writeq(val64, &bar0->general_int_mask);
1697                 }
1698         }
1699
1700         /*  MAC Interrupts */
1701         /*  Enabling/Disabling MAC interrupts */
1702         if (mask & (TX_MAC_INTR | RX_MAC_INTR)) {
1703                 val64 = TXMAC_INT_M | RXMAC_INT_M;
1704                 if (flag == ENABLE_INTRS) {
1705                         temp64 = readq(&bar0->general_int_mask);
1706                         temp64 &= ~((u64) val64);
1707                         writeq(temp64, &bar0->general_int_mask);
1708                         /*
1709                          * All MAC block error interrupts are disabled for now
1710                          * TODO
1711                          */
1712                 } else if (flag == DISABLE_INTRS) {
1713                         /*
1714                          * Disable MAC Intrs in the general intr mask register
1715                          */
1716                         writeq(DISABLE_ALL_INTRS, &bar0->mac_int_mask);
1717                         writeq(DISABLE_ALL_INTRS,
1718                                &bar0->mac_rmac_err_mask);
1719
1720                         temp64 = readq(&bar0->general_int_mask);
1721                         val64 |= temp64;
1722                         writeq(val64, &bar0->general_int_mask);
1723                 }
1724         }
1725
1726         /*  XGXS Interrupts */
1727         if (mask & (TX_XGXS_INTR | RX_XGXS_INTR)) {
1728                 val64 = TXXGXS_INT_M | RXXGXS_INT_M;
1729                 if (flag == ENABLE_INTRS) {
1730                         temp64 = readq(&bar0->general_int_mask);
1731                         temp64 &= ~((u64) val64);
1732                         writeq(temp64, &bar0->general_int_mask);
1733                         /*
1734                          * All XGXS block error interrupts are disabled for now
1735                          * TODO
1736                          */
1737                         writeq(DISABLE_ALL_INTRS, &bar0->xgxs_int_mask);
1738                 } else if (flag == DISABLE_INTRS) {
1739                         /*
1740                          * Disable MC Intrs in the general intr mask register
1741                          */
1742                         writeq(DISABLE_ALL_INTRS, &bar0->xgxs_int_mask);
1743                         temp64 = readq(&bar0->general_int_mask);
1744                         val64 |= temp64;
1745                         writeq(val64, &bar0->general_int_mask);
1746                 }
1747         }
1748
1749         /*  Memory Controller(MC) interrupts */
1750         if (mask & MC_INTR) {
1751                 val64 = MC_INT_M;
1752                 if (flag == ENABLE_INTRS) {
1753                         temp64 = readq(&bar0->general_int_mask);
1754                         temp64 &= ~((u64) val64);
1755                         writeq(temp64, &bar0->general_int_mask);
1756                         /*
1757                          * Enable all MC Intrs.
1758                          */
1759                         writeq(0x0, &bar0->mc_int_mask);
1760                         writeq(0x0, &bar0->mc_err_mask);
1761                 } else if (flag == DISABLE_INTRS) {
1762                         /*
1763                          * Disable MC Intrs in the general intr mask register
1764                          */
1765                         writeq(DISABLE_ALL_INTRS, &bar0->mc_int_mask);
1766                         temp64 = readq(&bar0->general_int_mask);
1767                         val64 |= temp64;
1768                         writeq(val64, &bar0->general_int_mask);
1769                 }
1770         }
1771
1772
1773         /*  Tx traffic interrupts */
1774         if (mask & TX_TRAFFIC_INTR) {
1775                 val64 = TXTRAFFIC_INT_M;
1776                 if (flag == ENABLE_INTRS) {
1777                         temp64 = readq(&bar0->general_int_mask);
1778                         temp64 &= ~((u64) val64);
1779                         writeq(temp64, &bar0->general_int_mask);
1780                         /*
1781                          * Enable all the Tx side interrupts
1782                          * writing 0 Enables all 64 TX interrupt levels
1783                          */
1784                         writeq(0x0, &bar0->tx_traffic_mask);
1785                 } else if (flag == DISABLE_INTRS) {
1786                         /*
1787                          * Disable Tx Traffic Intrs in the general intr mask
1788                          * register.
1789                          */
1790                         writeq(DISABLE_ALL_INTRS, &bar0->tx_traffic_mask);
1791                         temp64 = readq(&bar0->general_int_mask);
1792                         val64 |= temp64;
1793                         writeq(val64, &bar0->general_int_mask);
1794                 }
1795         }
1796
1797         /*  Rx traffic interrupts */
1798         if (mask & RX_TRAFFIC_INTR) {
1799                 val64 = RXTRAFFIC_INT_M;
1800                 if (flag == ENABLE_INTRS) {
1801                         temp64 = readq(&bar0->general_int_mask);
1802                         temp64 &= ~((u64) val64);
1803                         writeq(temp64, &bar0->general_int_mask);
1804                         /* writing 0 Enables all 8 RX interrupt levels */
1805                         writeq(0x0, &bar0->rx_traffic_mask);
1806                 } else if (flag == DISABLE_INTRS) {
1807                         /*
1808                          * Disable Rx Traffic Intrs in the general intr mask
1809                          * register.
1810                          */
1811                         writeq(DISABLE_ALL_INTRS, &bar0->rx_traffic_mask);
1812                         temp64 = readq(&bar0->general_int_mask);
1813                         val64 |= temp64;
1814                         writeq(val64, &bar0->general_int_mask);
1815                 }
1816         }
1817 }
1818
1819 static int check_prc_pcc_state(u64 val64, int flag, int rev_id, int herc)
1820 {
1821         int ret = 0;
1822
1823         if (flag == FALSE) {
1824                 if ((!herc && (rev_id >= 4)) || herc) {
1825                         if (!(val64 & ADAPTER_STATUS_RMAC_PCC_IDLE) &&
1826                             ((val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ==
1827                              ADAPTER_STATUS_RC_PRC_QUIESCENT)) {
1828                                 ret = 1;
1829                         }
1830                 }else {
1831                         if (!(val64 & ADAPTER_STATUS_RMAC_PCC_FOUR_IDLE) &&
1832                             ((val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ==
1833                              ADAPTER_STATUS_RC_PRC_QUIESCENT)) {
1834                                 ret = 1;
1835                         }
1836                 }
1837         } else {
1838                 if ((!herc && (rev_id >= 4)) || herc) {
1839                         if (((val64 & ADAPTER_STATUS_RMAC_PCC_IDLE) ==
1840                              ADAPTER_STATUS_RMAC_PCC_IDLE) &&
1841                             (!(val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ||
1842                              ((val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ==
1843                               ADAPTER_STATUS_RC_PRC_QUIESCENT))) {
1844                                 ret = 1;
1845                         }
1846                 } else {
1847                         if (((val64 & ADAPTER_STATUS_RMAC_PCC_FOUR_IDLE) ==
1848                              ADAPTER_STATUS_RMAC_PCC_FOUR_IDLE) &&
1849                             (!(val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ||
1850                              ((val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ==
1851                               ADAPTER_STATUS_RC_PRC_QUIESCENT))) {
1852                                 ret = 1;
1853                         }
1854                 }
1855         }
1856
1857         return ret;
1858 }
1859 /**
1860  *  verify_xena_quiescence - Checks whether the H/W is ready
1861  *  @val64 :  Value read from adapter status register.
1862  *  @flag : indicates if the adapter enable bit was ever written once
1863  *  before.
1864  *  Description: Returns whether the H/W is ready to go or not. Depending
1865  *  on whether adapter enable bit was written or not the comparison
1866  *  differs and the calling function passes the input argument flag to
1867  *  indicate this.
1868  *  Return: 1 If xena is quiescence
1869  *          0 If Xena is not quiescence
1870  */
1871
1872 static int verify_xena_quiescence(nic_t *sp, u64 val64, int flag)
1873 {
1874         int ret = 0, herc;
1875         u64 tmp64 = ~((u64) val64);
1876         int rev_id = get_xena_rev_id(sp->pdev);
1877
1878         herc = (sp->device_type == XFRAME_II_DEVICE);
1879         if (!
1880             (tmp64 &
1881              (ADAPTER_STATUS_TDMA_READY | ADAPTER_STATUS_RDMA_READY |
1882               ADAPTER_STATUS_PFC_READY | ADAPTER_STATUS_TMAC_BUF_EMPTY |
1883               ADAPTER_STATUS_PIC_QUIESCENT | ADAPTER_STATUS_MC_DRAM_READY |
1884               ADAPTER_STATUS_MC_QUEUES_READY | ADAPTER_STATUS_M_PLL_LOCK |
1885               ADAPTER_STATUS_P_PLL_LOCK))) {
1886                 ret = check_prc_pcc_state(val64, flag, rev_id, herc);
1887         }
1888
1889         return ret;
1890 }
1891
1892 /**
1893  * fix_mac_address -  Fix for Mac addr problem on Alpha platforms
1894  * @sp: Pointer to device specifc structure
1895  * Description :
1896  * New procedure to clear mac address reading  problems on Alpha platforms
1897  *
1898  */
1899
1900 static void fix_mac_address(nic_t * sp)
1901 {
1902         XENA_dev_config_t __iomem *bar0 = sp->bar0;
1903         u64 val64;
1904         int i = 0;
1905
1906         while (fix_mac[i] != END_SIGN) {
1907                 writeq(fix_mac[i++], &bar0->gpio_control);
1908                 udelay(10);
1909                 val64 = readq(&bar0->gpio_control);
1910         }
1911 }
1912
1913 /**
1914  *  start_nic - Turns the device on
1915  *  @nic : device private variable.
1916  *  Description:
1917  *  This function actually turns the device on. Before this  function is
1918  *  called,all Registers are configured from their reset states
1919  *  and shared memory is allocated but the NIC is still quiescent. On
1920  *  calling this function, the device interrupts are cleared and the NIC is
1921  *  literally switched on by writing into the adapter control register.
1922  *  Return Value:
1923  *  SUCCESS on success and -1 on failure.
1924  */
1925
1926 static int start_nic(struct s2io_nic *nic)
1927 {
1928         XENA_dev_config_t __iomem *bar0 = nic->bar0;
1929         struct net_device *dev = nic->dev;
1930         register u64 val64 = 0;
1931         u16 interruptible;
1932         u16 subid, i;
1933         mac_info_t *mac_control;
1934         struct config_param *config;
1935
1936         mac_control = &nic->mac_control;
1937         config = &nic->config;
1938
1939         /*  PRC Initialization and configuration */
1940         for (i = 0; i < config->rx_ring_num; i++) {
1941                 writeq((u64) mac_control->rings[i].rx_blocks[0].block_dma_addr,
1942                        &bar0->prc_rxd0_n[i]);
1943
1944                 val64 = readq(&bar0->prc_ctrl_n[i]);
1945                 if (nic->config.bimodal)
1946                         val64 |= PRC_CTRL_BIMODAL_INTERRUPT;
1947                 if (nic->rxd_mode == RXD_MODE_1)
1948                         val64 |= PRC_CTRL_RC_ENABLED;
1949                 else
1950                         val64 |= PRC_CTRL_RC_ENABLED | PRC_CTRL_RING_MODE_3;
1951                 writeq(val64, &bar0->prc_ctrl_n[i]);
1952         }
1953
1954         if (nic->rxd_mode == RXD_MODE_3B) {
1955                 /* Enabling 2 buffer mode by writing into Rx_pa_cfg reg. */
1956                 val64 = readq(&bar0->rx_pa_cfg);
1957                 val64 |= RX_PA_CFG_IGNORE_L2_ERR;
1958                 writeq(val64, &bar0->rx_pa_cfg);
1959         }
1960
1961         /*
1962          * Enabling MC-RLDRAM. After enabling the device, we timeout
1963          * for around 100ms, which is approximately the time required
1964          * for the device to be ready for operation.
1965          */
1966         val64 = readq(&bar0->mc_rldram_mrs);
1967         val64 |= MC_RLDRAM_QUEUE_SIZE_ENABLE | MC_RLDRAM_MRS_ENABLE;
1968         SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_mrs, UF);
1969         val64 = readq(&bar0->mc_rldram_mrs);
1970
1971         msleep(100);    /* Delay by around 100 ms. */
1972
1973         /* Enabling ECC Protection. */
1974         val64 = readq(&bar0->adapter_control);
1975         val64 &= ~ADAPTER_ECC_EN;
1976         writeq(val64, &bar0->adapter_control);
1977
1978         /*
1979          * Clearing any possible Link state change interrupts that
1980          * could have popped up just before Enabling the card.
1981          */
1982         val64 = readq(&bar0->mac_rmac_err_reg);
1983         if (val64)
1984                 writeq(val64, &bar0->mac_rmac_err_reg);
1985
1986         /*
1987          * Verify if the device is ready to be enabled, if so enable
1988          * it.
1989          */
1990         val64 = readq(&bar0->adapter_status);
1991         if (!verify_xena_quiescence(nic, val64, nic->device_enabled_once)) {
1992                 DBG_PRINT(ERR_DBG, "%s: device is not ready, ", dev->name);
1993                 DBG_PRINT(ERR_DBG, "Adapter status reads: 0x%llx\n",
1994                           (unsigned long long) val64);
1995                 return FAILURE;
1996         }
1997
1998         /*  Enable select interrupts */
1999         if (nic->intr_type != INTA)
2000                 en_dis_able_nic_intrs(nic, ENA_ALL_INTRS, DISABLE_INTRS);
2001         else {
2002                 interruptible = TX_TRAFFIC_INTR | RX_TRAFFIC_INTR;
2003                 interruptible |= TX_PIC_INTR | RX_PIC_INTR;
2004                 interruptible |= TX_MAC_INTR | RX_MAC_INTR;
2005                 en_dis_able_nic_intrs(nic, interruptible, ENABLE_INTRS);
2006         }
2007
2008         /*
2009          * With some switches, link might be already up at this point.
2010          * Because of this weird behavior, when we enable laser,
2011          * we may not get link. We need to handle this. We cannot
2012          * figure out which switch is misbehaving. So we are forced to
2013          * make a global change.
2014          */
2015
2016         /* Enabling Laser. */
2017         val64 = readq(&bar0->adapter_control);
2018         val64 |= ADAPTER_EOI_TX_ON;
2019         writeq(val64, &bar0->adapter_control);
2020
2021         /* SXE-002: Initialize link and activity LED */
2022         subid = nic->pdev->subsystem_device;
2023         if (((subid & 0xFF) >= 0x07) &&
2024             (nic->device_type == XFRAME_I_DEVICE)) {
2025                 val64 = readq(&bar0->gpio_control);
2026                 val64 |= 0x0000800000000000ULL;
2027                 writeq(val64, &bar0->gpio_control);
2028                 val64 = 0x0411040400000000ULL;
2029                 writeq(val64, (void __iomem *)bar0 + 0x2700);
2030         }
2031
2032         /*
2033          * Don't see link state interrupts on certain switches, so
2034          * directly scheduling a link state task from here.
2035          */
2036         schedule_work(&nic->set_link_task);
2037
2038         return SUCCESS;
2039 }
2040 /**
2041  * s2io_txdl_getskb - Get the skb from txdl, unmap and return skb
2042  */
2043 static struct sk_buff *s2io_txdl_getskb(fifo_info_t *fifo_data, TxD_t *txdlp, int get_off)
2044 {
2045         nic_t *nic = fifo_data->nic;
2046         struct sk_buff *skb;
2047         TxD_t *txds;
2048         u16 j, frg_cnt;
2049
2050         txds = txdlp;
2051         if (txds->Host_Control == (u64)(long)nic->ufo_in_band_v) {
2052                 pci_unmap_single(nic->pdev, (dma_addr_t)
2053                         txds->Buffer_Pointer, sizeof(u64),
2054                         PCI_DMA_TODEVICE);
2055                 txds++;
2056         }
2057
2058         skb = (struct sk_buff *) ((unsigned long)
2059                         txds->Host_Control);
2060         if (!skb) {
2061                 memset(txdlp, 0, (sizeof(TxD_t) * fifo_data->max_txds));
2062                 return NULL;
2063         }
2064         pci_unmap_single(nic->pdev, (dma_addr_t)
2065                          txds->Buffer_Pointer,
2066                          skb->len - skb->data_len,
2067                          PCI_DMA_TODEVICE);
2068         frg_cnt = skb_shinfo(skb)->nr_frags;
2069         if (frg_cnt) {
2070                 txds++;
2071                 for (j = 0; j < frg_cnt; j++, txds++) {
2072                         skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
2073                         if (!txds->Buffer_Pointer)
2074                                 break;
2075                         pci_unmap_page(nic->pdev, (dma_addr_t) 
2076                                         txds->Buffer_Pointer,
2077                                        frag->size, PCI_DMA_TODEVICE);
2078                 }
2079         }
2080         txdlp->Host_Control = 0;
2081         return(skb);
2082 }
2083
2084 /**
2085  *  free_tx_buffers - Free all queued Tx buffers
2086  *  @nic : device private variable.
2087  *  Description:
2088  *  Free all queued Tx buffers.
2089  *  Return Value: void
2090 */
2091
2092 static void free_tx_buffers(struct s2io_nic *nic)
2093 {
2094         struct net_device *dev = nic->dev;
2095         struct sk_buff *skb;
2096         TxD_t *txdp;
2097         int i, j;
2098         mac_info_t *mac_control;
2099         struct config_param *config;
2100         int cnt = 0;
2101
2102         mac_control = &nic->mac_control;
2103         config = &nic->config;
2104
2105         for (i = 0; i < config->tx_fifo_num; i++) {
2106                 for (j = 0; j < config->tx_cfg[i].fifo_len - 1; j++) {
2107                         txdp = (TxD_t *) mac_control->fifos[i].list_info[j].
2108                             list_virt_addr;
2109                         skb = s2io_txdl_getskb(&mac_control->fifos[i], txdp, j);
2110                         if (skb) {
2111                                 dev_kfree_skb(skb);
2112                                 cnt++;
2113                         }
2114                 }
2115                 DBG_PRINT(INTR_DBG,
2116                           "%s:forcibly freeing %d skbs on FIFO%d\n",
2117                           dev->name, cnt, i);
2118                 mac_control->fifos[i].tx_curr_get_info.offset = 0;
2119                 mac_control->fifos[i].tx_curr_put_info.offset = 0;
2120         }
2121 }
2122
2123 /**
2124  *   stop_nic -  To stop the nic
2125  *   @nic ; device private variable.
2126  *   Description:
2127  *   This function does exactly the opposite of what the start_nic()
2128  *   function does. This function is called to stop the device.
2129  *   Return Value:
2130  *   void.
2131  */
2132
2133 static void stop_nic(struct s2io_nic *nic)
2134 {
2135         XENA_dev_config_t __iomem *bar0 = nic->bar0;
2136         register u64 val64 = 0;
2137         u16 interruptible, i;
2138         mac_info_t *mac_control;
2139         struct config_param *config;
2140
2141         mac_control = &nic->mac_control;
2142         config = &nic->config;
2143
2144         /*  Disable all interrupts */
2145         interruptible = TX_TRAFFIC_INTR | RX_TRAFFIC_INTR;
2146         interruptible |= TX_PIC_INTR | RX_PIC_INTR;
2147         interruptible |= TX_MAC_INTR | RX_MAC_INTR;
2148         en_dis_able_nic_intrs(nic, interruptible, DISABLE_INTRS);
2149
2150         /*  Disable PRCs */
2151         for (i = 0; i < config->rx_ring_num; i++) {
2152                 val64 = readq(&bar0->prc_ctrl_n[i]);
2153                 val64 &= ~((u64) PRC_CTRL_RC_ENABLED);
2154                 writeq(val64, &bar0->prc_ctrl_n[i]);
2155         }
2156 }
2157
2158 int fill_rxd_3buf(nic_t *nic, RxD_t *rxdp, struct sk_buff *skb)
2159 {
2160         struct net_device *dev = nic->dev;
2161         struct sk_buff *frag_list;
2162         void *tmp;
2163
2164         /* Buffer-1 receives L3/L4 headers */
2165         ((RxD3_t*)rxdp)->Buffer1_ptr = pci_map_single
2166                         (nic->pdev, skb->data, l3l4hdr_size + 4,
2167                         PCI_DMA_FROMDEVICE);
2168
2169         /* skb_shinfo(skb)->frag_list will have L4 data payload */
2170         skb_shinfo(skb)->frag_list = dev_alloc_skb(dev->mtu + ALIGN_SIZE);
2171         if (skb_shinfo(skb)->frag_list == NULL) {
2172                 DBG_PRINT(ERR_DBG, "%s: dev_alloc_skb failed\n ", dev->name);
2173                 return -ENOMEM ;
2174         }
2175         frag_list = skb_shinfo(skb)->frag_list;
2176         frag_list->next = NULL;
2177         tmp = (void *)ALIGN((long)frag_list->data, ALIGN_SIZE + 1);
2178         frag_list->data = tmp;
2179         frag_list->tail = tmp;
2180
2181         /* Buffer-2 receives L4 data payload */
2182         ((RxD3_t*)rxdp)->Buffer2_ptr = pci_map_single(nic->pdev,
2183                                 frag_list->data, dev->mtu,
2184                                 PCI_DMA_FROMDEVICE);
2185         rxdp->Control_2 |= SET_BUFFER1_SIZE_3(l3l4hdr_size + 4);
2186         rxdp->Control_2 |= SET_BUFFER2_SIZE_3(dev->mtu);
2187
2188         return SUCCESS;
2189 }
2190
2191 /**
2192  *  fill_rx_buffers - Allocates the Rx side skbs
2193  *  @nic:  device private variable
2194  *  @ring_no: ring number
2195  *  Description:
2196  *  The function allocates Rx side skbs and puts the physical
2197  *  address of these buffers into the RxD buffer pointers, so that the NIC
2198  *  can DMA the received frame into these locations.
2199  *  The NIC supports 3 receive modes, viz
2200  *  1. single buffer,
2201  *  2. three buffer and
2202  *  3. Five buffer modes.
2203  *  Each mode defines how many fragments the received frame will be split
2204  *  up into by the NIC. The frame is split into L3 header, L4 Header,
2205  *  L4 payload in three buffer mode and in 5 buffer mode, L4 payload itself
2206  *  is split into 3 fragments. As of now only single buffer mode is
2207  *  supported.
2208  *   Return Value:
2209  *  SUCCESS on success or an appropriate -ve value on failure.
2210  */
2211
2212 static int fill_rx_buffers(struct s2io_nic *nic, int ring_no)
2213 {
2214         struct net_device *dev = nic->dev;
2215         struct sk_buff *skb;
2216         RxD_t *rxdp;
2217         int off, off1, size, block_no, block_no1;
2218         u32 alloc_tab = 0;
2219         u32 alloc_cnt;
2220         mac_info_t *mac_control;
2221         struct config_param *config;
2222         u64 tmp;
2223         buffAdd_t *ba;
2224 #ifndef CONFIG_S2IO_NAPI
2225         unsigned long flags;
2226 #endif
2227         RxD_t *first_rxdp = NULL;
2228
2229         mac_control = &nic->mac_control;
2230         config = &nic->config;
2231         alloc_cnt = mac_control->rings[ring_no].pkt_cnt -
2232             atomic_read(&nic->rx_bufs_left[ring_no]);
2233
2234         while (alloc_tab < alloc_cnt) {
2235                 block_no = mac_control->rings[ring_no].rx_curr_put_info.
2236                     block_index;
2237                 block_no1 = mac_control->rings[ring_no].rx_curr_get_info.
2238                     block_index;
2239                 off = mac_control->rings[ring_no].rx_curr_put_info.offset;
2240                 off1 = mac_control->rings[ring_no].rx_curr_get_info.offset;
2241
2242                 rxdp = mac_control->rings[ring_no].
2243                                 rx_blocks[block_no].rxds[off].virt_addr;
2244
2245                 if ((block_no == block_no1) && (off == off1) &&
2246                                         (rxdp->Host_Control)) {
2247                         DBG_PRINT(INTR_DBG, "%s: Get and Put",
2248                                   dev->name);
2249                         DBG_PRINT(INTR_DBG, " info equated\n");
2250                         goto end;
2251                 }
2252                 if (off && (off == rxd_count[nic->rxd_mode])) {
2253                         mac_control->rings[ring_no].rx_curr_put_info.
2254                             block_index++;
2255                         if (mac_control->rings[ring_no].rx_curr_put_info.
2256                             block_index == mac_control->rings[ring_no].
2257                                         block_count)
2258                                 mac_control->rings[ring_no].rx_curr_put_info.
2259                                         block_index = 0;
2260                         block_no = mac_control->rings[ring_no].
2261                                         rx_curr_put_info.block_index;
2262                         if (off == rxd_count[nic->rxd_mode])
2263                                 off = 0;
2264                         mac_control->rings[ring_no].rx_curr_put_info.
2265                                 offset = off;
2266                         rxdp = mac_control->rings[ring_no].
2267                                 rx_blocks[block_no].block_virt_addr;
2268                         DBG_PRINT(INTR_DBG, "%s: Next block at: %p\n",
2269                                   dev->name, rxdp);
2270                 }
2271 #ifndef CONFIG_S2IO_NAPI
2272                 spin_lock_irqsave(&nic->put_lock, flags);
2273                 mac_control->rings[ring_no].put_pos =
2274                     (block_no * (rxd_count[nic->rxd_mode] + 1)) + off;
2275                 spin_unlock_irqrestore(&nic->put_lock, flags);
2276 #endif
2277                 if ((rxdp->Control_1 & RXD_OWN_XENA) &&
2278                         ((nic->rxd_mode >= RXD_MODE_3A) &&
2279                                 (rxdp->Control_2 & BIT(0)))) {
2280                         mac_control->rings[ring_no].rx_curr_put_info.
2281                                         offset = off;
2282                         goto end;
2283                 }
2284                 /* calculate size of skb based on ring mode */
2285                 size = dev->mtu + HEADER_ETHERNET_II_802_3_SIZE +
2286                                 HEADER_802_2_SIZE + HEADER_SNAP_SIZE;
2287                 if (nic->rxd_mode == RXD_MODE_1)
2288                         size += NET_IP_ALIGN;
2289                 else if (nic->rxd_mode == RXD_MODE_3B)
2290                         size = dev->mtu + ALIGN_SIZE + BUF0_LEN + 4;
2291                 else
2292                         size = l3l4hdr_size + ALIGN_SIZE + BUF0_LEN + 4;
2293
2294                 /* allocate skb */
2295                 skb = dev_alloc_skb(size);
2296                 if(!skb) {
2297                         DBG_PRINT(ERR_DBG, "%s: Out of ", dev->name);
2298                         DBG_PRINT(ERR_DBG, "memory to allocate SKBs\n");
2299                         if (first_rxdp) {
2300                                 wmb();
2301                                 first_rxdp->Control_1 |= RXD_OWN_XENA;
2302                         }
2303                         return -ENOMEM ;
2304                 }
2305                 if (nic->rxd_mode == RXD_MODE_1) {
2306                         /* 1 buffer mode - normal operation mode */
2307                         memset(rxdp, 0, sizeof(RxD1_t));
2308                         skb_reserve(skb, NET_IP_ALIGN);
2309                         ((RxD1_t*)rxdp)->Buffer0_ptr = pci_map_single
2310                             (nic->pdev, skb->data, size, PCI_DMA_FROMDEVICE);
2311                         rxdp->Control_2 &= (~MASK_BUFFER0_SIZE_1);
2312                         rxdp->Control_2 |= SET_BUFFER0_SIZE_1(size);
2313
2314                 } else if (nic->rxd_mode >= RXD_MODE_3A) {
2315                         /*
2316                          * 2 or 3 buffer mode -
2317                          * Both 2 buffer mode and 3 buffer mode provides 128
2318                          * byte aligned receive buffers.
2319                          *
2320                          * 3 buffer mode provides header separation where in
2321                          * skb->data will have L3/L4 headers where as
2322                          * skb_shinfo(skb)->frag_list will have the L4 data
2323                          * payload
2324                          */
2325
2326                         memset(rxdp, 0, sizeof(RxD3_t));
2327                         ba = &mac_control->rings[ring_no].ba[block_no][off];
2328                         skb_reserve(skb, BUF0_LEN);
2329                         tmp = (u64)(unsigned long) skb->data;
2330                         tmp += ALIGN_SIZE;
2331                         tmp &= ~ALIGN_SIZE;
2332                         skb->data = (void *) (unsigned long)tmp;
2333                         skb->tail = (void *) (unsigned long)tmp;
2334
2335                         ((RxD3_t*)rxdp)->Buffer0_ptr =
2336                             pci_map_single(nic->pdev, ba->ba_0, BUF0_LEN,
2337                                            PCI_DMA_FROMDEVICE);
2338                         rxdp->Control_2 = SET_BUFFER0_SIZE_3(BUF0_LEN);
2339                         if (nic->rxd_mode == RXD_MODE_3B) {
2340                                 /* Two buffer mode */
2341
2342                                 /*
2343                                  * Buffer2 will have L3/L4 header plus 
2344                                  * L4 payload
2345                                  */
2346                                 ((RxD3_t*)rxdp)->Buffer2_ptr = pci_map_single
2347                                 (nic->pdev, skb->data, dev->mtu + 4,
2348                                                 PCI_DMA_FROMDEVICE);
2349
2350                                 /* Buffer-1 will be dummy buffer not used */
2351                                 ((RxD3_t*)rxdp)->Buffer1_ptr =
2352                                 pci_map_single(nic->pdev, ba->ba_1, BUF1_LEN,
2353                                         PCI_DMA_FROMDEVICE);
2354                                 rxdp->Control_2 |= SET_BUFFER1_SIZE_3(1);
2355                                 rxdp->Control_2 |= SET_BUFFER2_SIZE_3
2356                                                                 (dev->mtu + 4);
2357                         } else {
2358                                 /* 3 buffer mode */
2359                                 if (fill_rxd_3buf(nic, rxdp, skb) == -ENOMEM) {
2360                                         dev_kfree_skb_irq(skb);
2361                                         if (first_rxdp) {
2362                                                 wmb();
2363                                                 first_rxdp->Control_1 |=
2364                                                         RXD_OWN_XENA;
2365                                         }
2366                                         return -ENOMEM ;
2367                                 }
2368                         }
2369                         rxdp->Control_2 |= BIT(0);
2370                 }
2371                 rxdp->Host_Control = (unsigned long) (skb);
2372                 if (alloc_tab & ((1 << rxsync_frequency) - 1))
2373                         rxdp->Control_1 |= RXD_OWN_XENA;
2374                 off++;
2375                 if (off == (rxd_count[nic->rxd_mode] + 1))
2376                         off = 0;
2377                 mac_control->rings[ring_no].rx_curr_put_info.offset = off;
2378
2379                 rxdp->Control_2 |= SET_RXD_MARKER;
2380                 if (!(alloc_tab & ((1 << rxsync_frequency) - 1))) {
2381                         if (first_rxdp) {
2382                                 wmb();
2383                                 first_rxdp->Control_1 |= RXD_OWN_XENA;
2384                         }
2385                         first_rxdp = rxdp;
2386                 }
2387                 atomic_inc(&nic->rx_bufs_left[ring_no]);
2388                 alloc_tab++;
2389         }
2390
2391       end:
2392         /* Transfer ownership of first descriptor to adapter just before
2393          * exiting. Before that, use memory barrier so that ownership
2394          * and other fields are seen by adapter correctly.
2395          */
2396         if (first_rxdp) {
2397                 wmb();
2398                 first_rxdp->Control_1 |= RXD_OWN_XENA;
2399         }
2400
2401         return SUCCESS;
2402 }
2403
2404 static void free_rxd_blk(struct s2io_nic *sp, int ring_no, int blk)
2405 {
2406         struct net_device *dev = sp->dev;
2407         int j;
2408         struct sk_buff *skb;
2409         RxD_t *rxdp;
2410         mac_info_t *mac_control;
2411         buffAdd_t *ba;
2412
2413         mac_control = &sp->mac_control;
2414         for (j = 0 ; j < rxd_count[sp->rxd_mode]; j++) {
2415                 rxdp = mac_control->rings[ring_no].
2416                                 rx_blocks[blk].rxds[j].virt_addr;
2417                 skb = (struct sk_buff *)
2418                         ((unsigned long) rxdp->Host_Control);
2419                 if (!skb) {
2420                         continue;
2421                 }
2422                 if (sp->rxd_mode == RXD_MODE_1) {
2423                         pci_unmap_single(sp->pdev, (dma_addr_t)
2424                                  ((RxD1_t*)rxdp)->Buffer0_ptr,
2425                                  dev->mtu +
2426                                  HEADER_ETHERNET_II_802_3_SIZE
2427                                  + HEADER_802_2_SIZE +
2428                                  HEADER_SNAP_SIZE,
2429                                  PCI_DMA_FROMDEVICE);
2430                         memset(rxdp, 0, sizeof(RxD1_t));
2431                 } else if(sp->rxd_mode == RXD_MODE_3B) {
2432                         ba = &mac_control->rings[ring_no].
2433                                 ba[blk][j];
2434                         pci_unmap_single(sp->pdev, (dma_addr_t)
2435                                  ((RxD3_t*)rxdp)->Buffer0_ptr,
2436                                  BUF0_LEN,
2437                                  PCI_DMA_FROMDEVICE);
2438                         pci_unmap_single(sp->pdev, (dma_addr_t)
2439                                  ((RxD3_t*)rxdp)->Buffer1_ptr,
2440                                  BUF1_LEN,
2441                                  PCI_DMA_FROMDEVICE);
2442                         pci_unmap_single(sp->pdev, (dma_addr_t)
2443                                  ((RxD3_t*)rxdp)->Buffer2_ptr,
2444                                  dev->mtu + 4,
2445                                  PCI_DMA_FROMDEVICE);
2446                         memset(rxdp, 0, sizeof(RxD3_t));
2447                 } else {
2448                         pci_unmap_single(sp->pdev, (dma_addr_t)
2449                                 ((RxD3_t*)rxdp)->Buffer0_ptr, BUF0_LEN,
2450                                 PCI_DMA_FROMDEVICE);
2451                         pci_unmap_single(sp->pdev, (dma_addr_t)
2452                                 ((RxD3_t*)rxdp)->Buffer1_ptr, 
2453                                 l3l4hdr_size + 4,
2454                                 PCI_DMA_FROMDEVICE);
2455                         pci_unmap_single(sp->pdev, (dma_addr_t)
2456                                 ((RxD3_t*)rxdp)->Buffer2_ptr, dev->mtu,
2457                                 PCI_DMA_FROMDEVICE);
2458                         memset(rxdp, 0, sizeof(RxD3_t));
2459                 }
2460                 dev_kfree_skb(skb);
2461                 atomic_dec(&sp->rx_bufs_left[ring_no]);
2462         }
2463 }
2464
2465 /**
2466  *  free_rx_buffers - Frees all Rx buffers
2467  *  @sp: device private variable.
2468  *  Description:
2469  *  This function will free all Rx buffers allocated by host.
2470  *  Return Value:
2471  *  NONE.
2472  */
2473
2474 static void free_rx_buffers(struct s2io_nic *sp)
2475 {
2476         struct net_device *dev = sp->dev;
2477         int i, blk = 0, buf_cnt = 0;
2478         mac_info_t *mac_control;
2479         struct config_param *config;
2480
2481         mac_control = &sp->mac_control;
2482         config = &sp->config;
2483
2484         for (i = 0; i < config->rx_ring_num; i++) {
2485                 for (blk = 0; blk < rx_ring_sz[i]; blk++)
2486                         free_rxd_blk(sp,i,blk);
2487
2488                 mac_control->rings[i].rx_curr_put_info.block_index = 0;
2489                 mac_control->rings[i].rx_curr_get_info.block_index = 0;
2490                 mac_control->rings[i].rx_curr_put_info.offset = 0;
2491                 mac_control->rings[i].rx_curr_get_info.offset = 0;
2492                 atomic_set(&sp->rx_bufs_left[i], 0);
2493                 DBG_PRINT(INIT_DBG, "%s:Freed 0x%x Rx Buffers on ring%d\n",
2494                           dev->name, buf_cnt, i);
2495         }
2496 }
2497
2498 /**
2499  * s2io_poll - Rx interrupt handler for NAPI support
2500  * @dev : pointer to the device structure.
2501  * @budget : The number of packets that were budgeted to be processed
2502  * during  one pass through the 'Poll" function.
2503  * Description:
2504  * Comes into picture only if NAPI support has been incorporated. It does
2505  * the same thing that rx_intr_handler does, but not in a interrupt context
2506  * also It will process only a given number of packets.
2507  * Return value:
2508  * 0 on success and 1 if there are No Rx packets to be processed.
2509  */
2510
2511 #if defined(CONFIG_S2IO_NAPI)
2512 static int s2io_poll(struct net_device *dev, int *budget)
2513 {
2514         nic_t *nic = dev->priv;
2515         int pkt_cnt = 0, org_pkts_to_process;
2516         mac_info_t *mac_control;
2517         struct config_param *config;
2518         XENA_dev_config_t __iomem *bar0 = nic->bar0;
2519         u64 val64;
2520         int i;
2521
2522         atomic_inc(&nic->isr_cnt);
2523         mac_control = &nic->mac_control;
2524         config = &nic->config;
2525
2526         nic->pkts_to_process = *budget;
2527         if (nic->pkts_to_process > dev->quota)
2528                 nic->pkts_to_process = dev->quota;
2529         org_pkts_to_process = nic->pkts_to_process;
2530
2531         val64 = readq(&bar0->rx_traffic_int);
2532         writeq(val64, &bar0->rx_traffic_int);
2533
2534         for (i = 0; i < config->rx_ring_num; i++) {
2535                 rx_intr_handler(&mac_control->rings[i]);
2536                 pkt_cnt = org_pkts_to_process - nic->pkts_to_process;
2537                 if (!nic->pkts_to_process) {
2538                         /* Quota for the current iteration has been met */
2539                         goto no_rx;
2540                 }
2541         }
2542         if (!pkt_cnt)
2543                 pkt_cnt = 1;
2544
2545         dev->quota -= pkt_cnt;
2546         *budget -= pkt_cnt;
2547         netif_rx_complete(dev);
2548
2549         for (i = 0; i < config->rx_ring_num; i++) {
2550                 if (fill_rx_buffers(nic, i) == -ENOMEM) {
2551                         DBG_PRINT(ERR_DBG, "%s:Out of memory", dev->name);
2552                         DBG_PRINT(ERR_DBG, " in Rx Poll!!\n");
2553                         break;
2554                 }
2555         }
2556         /* Re enable the Rx interrupts. */
2557         en_dis_able_nic_intrs(nic, RX_TRAFFIC_INTR, ENABLE_INTRS);
2558         atomic_dec(&nic->isr_cnt);
2559         return 0;
2560
2561 no_rx:
2562         dev->quota -= pkt_cnt;
2563         *budget -= pkt_cnt;
2564
2565         for (i = 0; i < config->rx_ring_num; i++) {
2566                 if (fill_rx_buffers(nic, i) == -ENOMEM) {
2567                         DBG_PRINT(ERR_DBG, "%s:Out of memory", dev->name);
2568                         DBG_PRINT(ERR_DBG, " in Rx Poll!!\n");
2569                         break;
2570                 }
2571         }
2572         atomic_dec(&nic->isr_cnt);
2573         return 1;
2574 }
2575 #endif
2576
2577 /**
2578  *  rx_intr_handler - Rx interrupt handler
2579  *  @nic: device private variable.
2580  *  Description:
2581  *  If the interrupt is because of a received frame or if the
2582  *  receive ring contains fresh as yet un-processed frames,this function is
2583  *  called. It picks out the RxD at which place the last Rx processing had
2584  *  stopped and sends the skb to the OSM's Rx handler and then increments
2585  *  the offset.
2586  *  Return Value:
2587  *  NONE.
2588  */
2589 static void rx_intr_handler(ring_info_t *ring_data)
2590 {
2591         nic_t *nic = ring_data->nic;
2592         struct net_device *dev = (struct net_device *) nic->dev;
2593         int get_block, put_block, put_offset;
2594         rx_curr_get_info_t get_info, put_info;
2595         RxD_t *rxdp;
2596         struct sk_buff *skb;
2597 #ifndef CONFIG_S2IO_NAPI
2598         int pkt_cnt = 0;
2599 #endif
2600         int i;
2601
2602         spin_lock(&nic->rx_lock);
2603         if (atomic_read(&nic->card_state) == CARD_DOWN) {
2604                 DBG_PRINT(INTR_DBG, "%s: %s going down for reset\n",
2605                           __FUNCTION__, dev->name);
2606                 spin_unlock(&nic->rx_lock);
2607                 return;
2608         }
2609
2610         get_info = ring_data->rx_curr_get_info;
2611         get_block = get_info.block_index;
2612         put_info = ring_data->rx_curr_put_info;
2613         put_block = put_info.block_index;
2614         rxdp = ring_data->rx_blocks[get_block].rxds[get_info.offset].virt_addr;
2615 #ifndef CONFIG_S2IO_NAPI
2616         spin_lock(&nic->put_lock);
2617         put_offset = ring_data->put_pos;
2618         spin_unlock(&nic->put_lock);
2619 #else
2620         put_offset = (put_block * (rxd_count[nic->rxd_mode] + 1)) +
2621                 put_info.offset;
2622 #endif
2623         while (RXD_IS_UP2DT(rxdp)) {
2624                 /* If your are next to put index then it's FIFO full condition */
2625                 if ((get_block == put_block) &&
2626                     (get_info.offset + 1) == put_info.offset) {
2627                         DBG_PRINT(ERR_DBG, "%s: Ring Full\n",dev->name);
2628                         break;
2629                 }
2630                 skb = (struct sk_buff *) ((unsigned long)rxdp->Host_Control);
2631                 if (skb == NULL) {
2632                         DBG_PRINT(ERR_DBG, "%s: The skb is ",
2633                                   dev->name);
2634                         DBG_PRINT(ERR_DBG, "Null in Rx Intr\n");
2635                         spin_unlock(&nic->rx_lock);
2636                         return;
2637                 }
2638                 if (nic->rxd_mode == RXD_MODE_1) {
2639                         pci_unmap_single(nic->pdev, (dma_addr_t)
2640                                  ((RxD1_t*)rxdp)->Buffer0_ptr,
2641                                  dev->mtu +
2642                                  HEADER_ETHERNET_II_802_3_SIZE +
2643                                  HEADER_802_2_SIZE +
2644                                  HEADER_SNAP_SIZE,
2645                                  PCI_DMA_FROMDEVICE);
2646                 } else if (nic->rxd_mode == RXD_MODE_3B) {
2647                         pci_unmap_single(nic->pdev, (dma_addr_t)
2648                                  ((RxD3_t*)rxdp)->Buffer0_ptr,
2649                                  BUF0_LEN, PCI_DMA_FROMDEVICE);
2650                         pci_unmap_single(nic->pdev, (dma_addr_t)
2651                                  ((RxD3_t*)rxdp)->Buffer1_ptr,
2652                                  BUF1_LEN, PCI_DMA_FROMDEVICE);
2653                         pci_unmap_single(nic->pdev, (dma_addr_t)
2654                                  ((RxD3_t*)rxdp)->Buffer2_ptr,
2655                                  dev->mtu + 4,
2656                                  PCI_DMA_FROMDEVICE);
2657                 } else {
2658                         pci_unmap_single(nic->pdev, (dma_addr_t)
2659                                          ((RxD3_t*)rxdp)->Buffer0_ptr, BUF0_LEN,
2660                                          PCI_DMA_FROMDEVICE);
2661                         pci_unmap_single(nic->pdev, (dma_addr_t)
2662                                          ((RxD3_t*)rxdp)->Buffer1_ptr,
2663                                          l3l4hdr_size + 4,
2664                                          PCI_DMA_FROMDEVICE);
2665                         pci_unmap_single(nic->pdev, (dma_addr_t)
2666                                          ((RxD3_t*)rxdp)->Buffer2_ptr,
2667                                          dev->mtu, PCI_DMA_FROMDEVICE);
2668                 }
2669                 rx_osm_handler(ring_data, rxdp);
2670                 get_info.offset++;
2671                 ring_data->rx_curr_get_info.offset = get_info.offset;
2672                 rxdp = ring_data->rx_blocks[get_block].
2673                                 rxds[get_info.offset].virt_addr;
2674                 if (get_info.offset == rxd_count[nic->rxd_mode]) {
2675                         get_info.offset = 0;
2676                         ring_data->rx_curr_get_info.offset = get_info.offset;
2677                         get_block++;
2678                         if (get_block == ring_data->block_count)
2679                                 get_block = 0;
2680                         ring_data->rx_curr_get_info.block_index = get_block;
2681                         rxdp = ring_data->rx_blocks[get_block].block_virt_addr;
2682                 }
2683
2684 #ifdef CONFIG_S2IO_NAPI
2685                 nic->pkts_to_process -= 1;
2686                 if (!nic->pkts_to_process)
2687                         break;
2688 #else
2689                 pkt_cnt++;
2690                 if ((indicate_max_pkts) && (pkt_cnt > indicate_max_pkts))
2691                         break;
2692 #endif
2693         }
2694         if (nic->lro) {
2695                 /* Clear all LRO sessions before exiting */
2696                 for (i=0; i<MAX_LRO_SESSIONS; i++) {
2697                         lro_t *lro = &nic->lro0_n[i];
2698                         if (lro->in_use) {
2699                                 update_L3L4_header(nic, lro);
2700                                 queue_rx_frame(lro->parent);
2701                                 clear_lro_session(lro);
2702                         }
2703                 }
2704         }
2705
2706         spin_unlock(&nic->rx_lock);
2707 }
2708
2709 /**
2710  *  tx_intr_handler - Transmit interrupt handler
2711  *  @nic : device private variable
2712  *  Description:
2713  *  If an interrupt was raised to indicate DMA complete of the
2714  *  Tx packet, this function is called. It identifies the last TxD
2715  *  whose buffer was freed and frees all skbs whose data have already
2716  *  DMA'ed into the NICs internal memory.
2717  *  Return Value:
2718  *  NONE
2719  */
2720
2721 static void tx_intr_handler(fifo_info_t *fifo_data)
2722 {
2723         nic_t *nic = fifo_data->nic;
2724         struct net_device *dev = (struct net_device *) nic->dev;
2725         tx_curr_get_info_t get_info, put_info;
2726         struct sk_buff *skb;
2727         TxD_t *txdlp;
2728
2729         get_info = fifo_data->tx_curr_get_info;
2730         put_info = fifo_data->tx_curr_put_info;
2731         txdlp = (TxD_t *) fifo_data->list_info[get_info.offset].
2732             list_virt_addr;
2733         while ((!(txdlp->Control_1 & TXD_LIST_OWN_XENA)) &&
2734                (get_info.offset != put_info.offset) &&
2735                (txdlp->Host_Control)) {
2736                 /* Check for TxD errors */
2737                 if (txdlp->Control_1 & TXD_T_CODE) {
2738                         unsigned long long err;
2739                         err = txdlp->Control_1 & TXD_T_CODE;
2740                         if ((err >> 48) == 0xA) {
2741                                 DBG_PRINT(TX_DBG, "TxD returned due \
2742 to loss of link\n");
2743                         }
2744                         else {
2745                                 DBG_PRINT(ERR_DBG, "***TxD error \
2746 %llx\n", err);
2747                         }
2748                 }
2749
2750                 skb = s2io_txdl_getskb(fifo_data, txdlp, get_info.offset);
2751                 if (skb == NULL) {
2752                         DBG_PRINT(ERR_DBG, "%s: Null skb ",
2753                         __FUNCTION__);
2754                         DBG_PRINT(ERR_DBG, "in Tx Free Intr\n");
2755                         return;
2756                 }
2757
2758                 /* Updating the statistics block */
2759                 nic->stats.tx_bytes += skb->len;
2760                 dev_kfree_skb_irq(skb);
2761
2762                 get_info.offset++;
2763                 get_info.offset %= get_info.fifo_len + 1;
2764                 txdlp = (TxD_t *) fifo_data->list_info
2765                     [get_info.offset].list_virt_addr;
2766                 fifo_data->tx_curr_get_info.offset =
2767                     get_info.offset;
2768         }
2769
2770         spin_lock(&nic->tx_lock);
2771         if (netif_queue_stopped(dev))
2772                 netif_wake_queue(dev);
2773         spin_unlock(&nic->tx_lock);
2774 }
2775
2776 /**
2777  *  alarm_intr_handler - Alarm Interrrupt handler
2778  *  @nic: device private variable
2779  *  Description: If the interrupt was neither because of Rx packet or Tx
2780  *  complete, this function is called. If the interrupt was to indicate
2781  *  a loss of link, the OSM link status handler is invoked for any other
2782  *  alarm interrupt the block that raised the interrupt is displayed
2783  *  and a H/W reset is issued.
2784  *  Return Value:
2785  *  NONE
2786 */
2787
2788 static void alarm_intr_handler(struct s2io_nic *nic)
2789 {
2790         struct net_device *dev = (struct net_device *) nic->dev;
2791         XENA_dev_config_t __iomem *bar0 = nic->bar0;
2792         register u64 val64 = 0, err_reg = 0;
2793
2794         /* Handling link status change error Intr */
2795         if (s2io_link_fault_indication(nic) == MAC_RMAC_ERR_TIMER) {
2796                 err_reg = readq(&bar0->mac_rmac_err_reg);
2797                 writeq(err_reg, &bar0->mac_rmac_err_reg);
2798                 if (err_reg & RMAC_LINK_STATE_CHANGE_INT) {
2799                         schedule_work(&nic->set_link_task);
2800                 }
2801         }
2802
2803         /* Handling Ecc errors */
2804         val64 = readq(&bar0->mc_err_reg);
2805         writeq(val64, &bar0->mc_err_reg);
2806         if (val64 & (MC_ERR_REG_ECC_ALL_SNG | MC_ERR_REG_ECC_ALL_DBL)) {
2807                 if (val64 & MC_ERR_REG_ECC_ALL_DBL) {
2808                         nic->mac_control.stats_info->sw_stat.
2809                                 double_ecc_errs++;
2810                         DBG_PRINT(INIT_DBG, "%s: Device indicates ",
2811                                   dev->name);
2812                         DBG_PRINT(INIT_DBG, "double ECC error!!\n");
2813                         if (nic->device_type != XFRAME_II_DEVICE) {
2814                                 /* Reset XframeI only if critical error */
2815                                 if (val64 & (MC_ERR_REG_MIRI_ECC_DB_ERR_0 |
2816                                              MC_ERR_REG_MIRI_ECC_DB_ERR_1)) {
2817                                         netif_stop_queue(dev);
2818                                         schedule_work(&nic->rst_timer_task);
2819                                 }
2820                         }
2821                 } else {
2822                         nic->mac_control.stats_info->sw_stat.
2823                                 single_ecc_errs++;
2824                 }
2825         }
2826
2827         /* In case of a serious error, the device will be Reset. */
2828         val64 = readq(&bar0->serr_source);
2829         if (val64 & SERR_SOURCE_ANY) {
2830                 DBG_PRINT(ERR_DBG, "%s: Device indicates ", dev->name);
2831                 DBG_PRINT(ERR_DBG, "serious error %llx!!\n", 
2832                           (unsigned long long)val64);
2833                 netif_stop_queue(dev);
2834                 schedule_work(&nic->rst_timer_task);
2835         }
2836
2837         /*
2838          * Also as mentioned in the latest Errata sheets if the PCC_FB_ECC
2839          * Error occurs, the adapter will be recycled by disabling the
2840          * adapter enable bit and enabling it again after the device
2841          * becomes Quiescent.
2842          */
2843         val64 = readq(&bar0->pcc_err_reg);
2844         writeq(val64, &bar0->pcc_err_reg);
2845         if (val64 & PCC_FB_ECC_DB_ERR) {
2846                 u64 ac = readq(&bar0->adapter_control);
2847                 ac &= ~(ADAPTER_CNTL_EN);
2848                 writeq(ac, &bar0->adapter_control);
2849                 ac = readq(&bar0->adapter_control);
2850                 schedule_work(&nic->set_link_task);
2851         }
2852
2853         /* Other type of interrupts are not being handled now,  TODO */
2854 }
2855
2856 /**
2857  *  wait_for_cmd_complete - waits for a command to complete.
2858  *  @sp : private member of the device structure, which is a pointer to the
2859  *  s2io_nic structure.
2860  *  Description: Function that waits for a command to Write into RMAC
2861  *  ADDR DATA registers to be completed and returns either success or
2862  *  error depending on whether the command was complete or not.
2863  *  Return value:
2864  *   SUCCESS on success and FAILURE on failure.
2865  */
2866
2867 static int wait_for_cmd_complete(nic_t * sp)
2868 {
2869         XENA_dev_config_t __iomem *bar0 = sp->bar0;
2870         int ret = FAILURE, cnt = 0;
2871         u64 val64;
2872
2873         while (TRUE) {
2874                 val64 = readq(&bar0->rmac_addr_cmd_mem);
2875                 if (!(val64 & RMAC_ADDR_CMD_MEM_STROBE_CMD_EXECUTING)) {
2876                         ret = SUCCESS;
2877                         break;
2878                 }
2879                 msleep(50);
2880                 if (cnt++ > 10)
2881                         break;
2882         }
2883
2884         return ret;
2885 }
2886
2887 /**
2888  *  s2io_reset - Resets the card.
2889  *  @sp : private member of the device structure.
2890  *  Description: Function to Reset the card. This function then also
2891  *  restores the previously saved PCI configuration space registers as
2892  *  the card reset also resets the configuration space.
2893  *  Return value:
2894  *  void.
2895  */
2896
2897 void s2io_reset(nic_t * sp)
2898 {
2899         XENA_dev_config_t __iomem *bar0 = sp->bar0;
2900         u64 val64;
2901         u16 subid, pci_cmd;
2902
2903         /* Back up  the PCI-X CMD reg, dont want to lose MMRBC, OST settings */
2904         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER, &(pci_cmd));
2905
2906         val64 = SW_RESET_ALL;
2907         writeq(val64, &bar0->sw_reset);
2908
2909         /*
2910          * At this stage, if the PCI write is indeed completed, the
2911          * card is reset and so is the PCI Config space of the device.
2912          * So a read cannot be issued at this stage on any of the
2913          * registers to ensure the write into "sw_reset" register
2914          * has gone through.
2915          * Question: Is there any system call that will explicitly force
2916          * all the write commands still pending on the bus to be pushed
2917          * through?
2918          * As of now I'am just giving a 250ms delay and hoping that the
2919          * PCI write to sw_reset register is done by this time.
2920          */
2921         msleep(250);
2922
2923         /* Restore the PCI state saved during initialization. */
2924         pci_restore_state(sp->pdev);
2925         pci_write_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
2926                                      pci_cmd);
2927         s2io_init_pci(sp);
2928
2929         msleep(250);
2930
2931         /* Set swapper to enable I/O register access */
2932         s2io_set_swapper(sp);
2933
2934         /* Restore the MSIX table entries from local variables */
2935         restore_xmsi_data(sp);
2936
2937         /* Clear certain PCI/PCI-X fields after reset */
2938         if (sp->device_type == XFRAME_II_DEVICE) {
2939                 /* Clear parity err detect bit */
2940                 pci_write_config_word(sp->pdev, PCI_STATUS, 0x8000);
2941
2942                 /* Clearing PCIX Ecc status register */
2943                 pci_write_config_dword(sp->pdev, 0x68, 0x7C);
2944
2945                 /* Clearing PCI_STATUS error reflected here */
2946                 writeq(BIT(62), &bar0->txpic_int_reg);
2947         }
2948
2949         /* Reset device statistics maintained by OS */
2950         memset(&sp->stats, 0, sizeof (struct net_device_stats));
2951
2952         /* SXE-002: Configure link and activity LED to turn it off */
2953         subid = sp->pdev->subsystem_device;
2954         if (((subid & 0xFF) >= 0x07) &&
2955             (sp->device_type == XFRAME_I_DEVICE)) {
2956                 val64 = readq(&bar0->gpio_control);
2957                 val64 |= 0x0000800000000000ULL;
2958                 writeq(val64, &bar0->gpio_control);
2959                 val64 = 0x0411040400000000ULL;
2960                 writeq(val64, (void __iomem *)bar0 + 0x2700);
2961         }
2962
2963         /*
2964          * Clear spurious ECC interrupts that would have occured on
2965          * XFRAME II cards after reset.
2966          */
2967         if (sp->device_type == XFRAME_II_DEVICE) {
2968                 val64 = readq(&bar0->pcc_err_reg);
2969                 writeq(val64, &bar0->pcc_err_reg);
2970         }
2971
2972         sp->device_enabled_once = FALSE;
2973 }
2974
2975 /**
2976  *  s2io_set_swapper - to set the swapper controle on the card
2977  *  @sp : private member of the device structure,
2978  *  pointer to the s2io_nic structure.
2979  *  Description: Function to set the swapper control on the card
2980  *  correctly depending on the 'endianness' of the system.
2981  *  Return value:
2982  *  SUCCESS on success and FAILURE on failure.
2983  */
2984
2985 int s2io_set_swapper(nic_t * sp)
2986 {
2987         struct net_device *dev = sp->dev;
2988         XENA_dev_config_t __iomem *bar0 = sp->bar0;
2989         u64 val64, valt, valr;
2990
2991         /*
2992          * Set proper endian settings and verify the same by reading
2993          * the PIF Feed-back register.
2994          */
2995
2996         val64 = readq(&bar0->pif_rd_swapper_fb);
2997         if (val64 != 0x0123456789ABCDEFULL) {
2998                 int i = 0;
2999                 u64 value[] = { 0xC30000C3C30000C3ULL,   /* FE=1, SE=1 */
3000                                 0x8100008181000081ULL,  /* FE=1, SE=0 */
3001                                 0x4200004242000042ULL,  /* FE=0, SE=1 */
3002                                 0};                     /* FE=0, SE=0 */
3003
3004                 while(i<4) {
3005                         writeq(value[i], &bar0->swapper_ctrl);
3006                         val64 = readq(&bar0->pif_rd_swapper_fb);
3007                         if (val64 == 0x0123456789ABCDEFULL)
3008                                 break;
3009                         i++;
3010                 }
3011                 if (i == 4) {
3012                         DBG_PRINT(ERR_DBG, "%s: Endian settings are wrong, ",
3013                                 dev->name);
3014                         DBG_PRINT(ERR_DBG, "feedback read %llx\n",
3015                                 (unsigned long long) val64);
3016                         return FAILURE;
3017                 }
3018                 valr = value[i];
3019         } else {
3020                 valr = readq(&bar0->swapper_ctrl);
3021         }
3022
3023         valt = 0x0123456789ABCDEFULL;
3024         writeq(valt, &bar0->xmsi_address);
3025         val64 = readq(&bar0->xmsi_address);
3026
3027         if(val64 != valt) {
3028                 int i = 0;
3029                 u64 value[] = { 0x00C3C30000C3C300ULL,  /* FE=1, SE=1 */
3030                                 0x0081810000818100ULL,  /* FE=1, SE=0 */
3031                                 0x0042420000424200ULL,  /* FE=0, SE=1 */
3032                                 0};                     /* FE=0, SE=0 */
3033
3034                 while(i<4) {
3035                         writeq((value[i] | valr), &bar0->swapper_ctrl);
3036                         writeq(valt, &bar0->xmsi_address);
3037                         val64 = readq(&bar0->xmsi_address);
3038                         if(val64 == valt)
3039                                 break;
3040                         i++;
3041                 }
3042                 if(i == 4) {
3043                         unsigned long long x = val64;
3044                         DBG_PRINT(ERR_DBG, "Write failed, Xmsi_addr ");
3045                         DBG_PRINT(ERR_DBG, "reads:0x%llx\n", x);
3046                         return FAILURE;
3047                 }
3048         }
3049         val64 = readq(&bar0->swapper_ctrl);
3050         val64 &= 0xFFFF000000000000ULL;
3051
3052 #ifdef  __BIG_ENDIAN
3053         /*
3054          * The device by default set to a big endian format, so a
3055          * big endian driver need not set anything.
3056          */
3057         val64 |= (SWAPPER_CTRL_TXP_FE |
3058                  SWAPPER_CTRL_TXP_SE |
3059                  SWAPPER_CTRL_TXD_R_FE |
3060                  SWAPPER_CTRL_TXD_W_FE |
3061                  SWAPPER_CTRL_TXF_R_FE |
3062                  SWAPPER_CTRL_RXD_R_FE |
3063                  SWAPPER_CTRL_RXD_W_FE |
3064                  SWAPPER_CTRL_RXF_W_FE |
3065                  SWAPPER_CTRL_XMSI_FE |
3066                  SWAPPER_CTRL_STATS_FE | SWAPPER_CTRL_STATS_SE);
3067         if (sp->intr_type == INTA)
3068                 val64 |= SWAPPER_CTRL_XMSI_SE;
3069         writeq(val64, &bar0->swapper_ctrl);
3070 #else
3071         /*
3072          * Initially we enable all bits to make it accessible by the
3073          * driver, then we selectively enable only those bits that
3074          * we want to set.
3075          */
3076         val64 |= (SWAPPER_CTRL_TXP_FE |
3077                  SWAPPER_CTRL_TXP_SE |
3078                  SWAPPER_CTRL_TXD_R_FE |
3079                  SWAPPER_CTRL_TXD_R_SE |
3080                  SWAPPER_CTRL_TXD_W_FE |
3081                  SWAPPER_CTRL_TXD_W_SE |
3082                  SWAPPER_CTRL_TXF_R_FE |
3083                  SWAPPER_CTRL_RXD_R_FE |
3084                  SWAPPER_CTRL_RXD_R_SE |
3085                  SWAPPER_CTRL_RXD_W_FE |
3086                  SWAPPER_CTRL_RXD_W_SE |
3087                  SWAPPER_CTRL_RXF_W_FE |
3088                  SWAPPER_CTRL_XMSI_FE |
3089                  SWAPPER_CTRL_STATS_FE | SWAPPER_CTRL_STATS_SE);
3090         if (sp->intr_type == INTA)
3091                 val64 |= SWAPPER_CTRL_XMSI_SE;
3092         writeq(val64, &bar0->swapper_ctrl);
3093 #endif
3094         val64 = readq(&bar0->swapper_ctrl);
3095
3096         /*
3097          * Verifying if endian settings are accurate by reading a
3098          * feedback register.
3099          */
3100         val64 = readq(&bar0->pif_rd_swapper_fb);
3101         if (val64 != 0x0123456789ABCDEFULL) {
3102                 /* Endian settings are incorrect, calls for another dekko. */
3103                 DBG_PRINT(ERR_DBG, "%s: Endian settings are wrong, ",
3104                           dev->name);
3105                 DBG_PRINT(ERR_DBG, "feedback read %llx\n",
3106                           (unsigned long long) val64);
3107                 return FAILURE;
3108         }
3109
3110         return SUCCESS;
3111 }
3112
3113 static int wait_for_msix_trans(nic_t *nic, int i)
3114 {
3115         XENA_dev_config_t __iomem *bar0 = nic->bar0;
3116         u64 val64;
3117         int ret = 0, cnt = 0;
3118
3119         do {
3120                 val64 = readq(&bar0->xmsi_access);
3121                 if (!(val64 & BIT(15)))
3122                         break;
3123                 mdelay(1);
3124                 cnt++;
3125         } while(cnt < 5);
3126         if (cnt == 5) {
3127                 DBG_PRINT(ERR_DBG, "XMSI # %d Access failed\n", i);
3128                 ret = 1;
3129         }
3130
3131         return ret;
3132 }
3133
3134 void restore_xmsi_data(nic_t *nic)
3135 {
3136         XENA_dev_config_t __iomem *bar0 = nic->bar0;
3137         u64 val64;
3138         int i;
3139
3140         for (i=0; i< MAX_REQUESTED_MSI_X; i++) {
3141                 writeq(nic->msix_info[i].addr, &bar0->xmsi_address);
3142                 writeq(nic->msix_info[i].data, &bar0->xmsi_data);
3143                 val64 = (BIT(7) | BIT(15) | vBIT(i, 26, 6));
3144                 writeq(val64, &bar0->xmsi_access);
3145                 if (wait_for_msix_trans(nic, i)) {
3146                         DBG_PRINT(ERR_DBG, "failed in %s\n", __FUNCTION__);
3147                         continue;
3148                 }
3149         }
3150 }
3151
3152 static void store_xmsi_data(nic_t *nic)
3153 {
3154         XENA_dev_config_t __iomem *bar0 = nic->bar0;
3155         u64 val64, addr, data;
3156         int i;
3157
3158         /* Store and display */
3159         for (i=0; i< MAX_REQUESTED_MSI_X; i++) {
3160                 val64 = (BIT(15) | vBIT(i, 26, 6));
3161                 writeq(val64, &bar0->xmsi_access);
3162                 if (wait_for_msix_trans(nic, i)) {
3163                         DBG_PRINT(ERR_DBG, "failed in %s\n", __FUNCTION__);
3164                         continue;
3165                 }
3166                 addr = readq(&bar0->xmsi_address);
3167                 data = readq(&bar0->xmsi_data);
3168                 if (addr && data) {
3169                         nic->msix_info[i].addr = addr;
3170                         nic->msix_info[i].data = data;
3171                 }
3172         }
3173 }
3174
3175 int s2io_enable_msi(nic_t *nic)
3176 {
3177         XENA_dev_config_t __iomem *bar0 = nic->bar0;
3178         u16 msi_ctrl, msg_val;
3179         struct config_param *config = &nic->config;
3180         struct net_device *dev = nic->dev;
3181         u64 val64, tx_mat, rx_mat;
3182         int i, err;
3183
3184         val64 = readq(&bar0->pic_control);
3185         val64 &= ~BIT(1);
3186         writeq(val64, &bar0->pic_control);
3187
3188         err = pci_enable_msi(nic->pdev);
3189         if (err) {
3190                 DBG_PRINT(ERR_DBG, "%s: enabling MSI failed\n",
3191                           nic->dev->name);
3192                 return err;
3193         }
3194
3195         /*
3196          * Enable MSI and use MSI-1 in stead of the standard MSI-0
3197          * for interrupt handling.
3198          */
3199         pci_read_config_word(nic->pdev, 0x4c, &msg_val);
3200         msg_val ^= 0x1;
3201         pci_write_config_word(nic->pdev, 0x4c, msg_val);
3202         pci_read_config_word(nic->pdev, 0x4c, &msg_val);
3203
3204         pci_read_config_word(nic->pdev, 0x42, &msi_ctrl);
3205         msi_ctrl |= 0x10;
3206         pci_write_config_word(nic->pdev, 0x42, msi_ctrl);
3207
3208         /* program MSI-1 into all usable Tx_Mat and Rx_Mat fields */
3209         tx_mat = readq(&bar0->tx_mat0_n[0]);
3210         for (i=0; i<config->tx_fifo_num; i++) {
3211                 tx_mat |= TX_MAT_SET(i, 1);
3212         }
3213         writeq(tx_mat, &bar0->tx_mat0_n[0]);
3214
3215         rx_mat = readq(&bar0->rx_mat);
3216         for (i=0; i<config->rx_ring_num; i++) {
3217                 rx_mat |= RX_MAT_SET(i, 1);
3218         }
3219         writeq(rx_mat, &bar0->rx_mat);
3220
3221         dev->irq = nic->pdev->irq;
3222         return 0;
3223 }
3224
3225 int s2io_enable_msi_x(nic_t *nic)
3226 {
3227         XENA_dev_config_t __iomem *bar0 = nic->bar0;
3228         u64 tx_mat, rx_mat;
3229         u16 msi_control; /* Temp variable */
3230         int ret, i, j, msix_indx = 1;
3231
3232         nic->entries = kmalloc(MAX_REQUESTED_MSI_X * sizeof(struct msix_entry),
3233                                GFP_KERNEL);
3234         if (nic->entries == NULL) {
3235                 DBG_PRINT(ERR_DBG, "%s: Memory allocation failed\n", __FUNCTION__);
3236                 return -ENOMEM;
3237         }
3238         memset(nic->entries, 0, MAX_REQUESTED_MSI_X * sizeof(struct msix_entry));
3239
3240         nic->s2io_entries =
3241                 kmalloc(MAX_REQUESTED_MSI_X * sizeof(struct s2io_msix_entry),
3242                                    GFP_KERNEL);
3243         if (nic->s2io_entries == NULL) {
3244                 DBG_PRINT(ERR_DBG, "%s: Memory allocation failed\n", __FUNCTION__);
3245                 kfree(nic->entries);
3246                 return -ENOMEM;
3247         }
3248         memset(nic->s2io_entries, 0,
3249                MAX_REQUESTED_MSI_X * sizeof(struct s2io_msix_entry));
3250
3251         for (i=0; i< MAX_REQUESTED_MSI_X; i++) {
3252                 nic->entries[i].entry = i;
3253                 nic->s2io_entries[i].entry = i;
3254                 nic->s2io_entries[i].arg = NULL;
3255                 nic->s2io_entries[i].in_use = 0;
3256         }
3257
3258         tx_mat = readq(&bar0->tx_mat0_n[0]);
3259         for (i=0; i<nic->config.tx_fifo_num; i++, msix_indx++) {
3260                 tx_mat |= TX_MAT_SET(i, msix_indx);
3261                 nic->s2io_entries[msix_indx].arg = &nic->mac_control.fifos[i];
3262                 nic->s2io_entries[msix_indx].type = MSIX_FIFO_TYPE;
3263                 nic->s2io_entries[msix_indx].in_use = MSIX_FLG;
3264         }
3265         writeq(tx_mat, &bar0->tx_mat0_n[0]);
3266
3267         if (!nic->config.bimodal) {
3268                 rx_mat = readq(&bar0->rx_mat);
3269                 for (j=0; j<nic->config.rx_ring_num; j++, msix_indx++) {
3270                         rx_mat |= RX_MAT_SET(j, msix_indx);
3271                         nic->s2io_entries[msix_indx].arg = &nic->mac_control.rings[j];
3272                         nic->s2io_entries[msix_indx].type = MSIX_RING_TYPE;
3273                         nic->s2io_entries[msix_indx].in_use = MSIX_FLG;
3274                 }
3275                 writeq(rx_mat, &bar0->rx_mat);
3276         } else {
3277                 tx_mat = readq(&bar0->tx_mat0_n[7]);
3278                 for (j=0; j<nic->config.rx_ring_num; j++, msix_indx++) {
3279                         tx_mat |= TX_MAT_SET(i, msix_indx);
3280                         nic->s2io_entries[msix_indx].arg = &nic->mac_control.rings[j];
3281                         nic->s2io_entries[msix_indx].type = MSIX_RING_TYPE;
3282                         nic->s2io_entries[msix_indx].in_use = MSIX_FLG;
3283                 }
3284                 writeq(tx_mat, &bar0->tx_mat0_n[7]);
3285         }
3286
3287         ret = pci_enable_msix(nic->pdev, nic->entries, MAX_REQUESTED_MSI_X);
3288         if (ret) {
3289                 DBG_PRINT(ERR_DBG, "%s: Enabling MSIX failed\n", nic->dev->name);
3290                 kfree(nic->entries);
3291                 kfree(nic->s2io_entries);
3292                 nic->entries = NULL;
3293                 nic->s2io_entries = NULL;
3294                 return -ENOMEM;
3295         }
3296
3297         /*
3298          * To enable MSI-X, MSI also needs to be enabled, due to a bug
3299          * in the herc NIC. (Temp change, needs to be removed later)
3300          */
3301         pci_read_config_word(nic->pdev, 0x42, &msi_control);
3302         msi_control |= 0x1; /* Enable MSI */
3303         pci_write_config_word(nic->pdev, 0x42, msi_control);
3304
3305         return 0;
3306 }
3307
3308 /* ********************************************************* *
3309  * Functions defined below concern the OS part of the driver *
3310  * ********************************************************* */
3311
3312 /**
3313  *  s2io_open - open entry point of the driver
3314  *  @dev : pointer to the device structure.
3315  *  Description:
3316  *  This function is the open entry point of the driver. It mainly calls a
3317  *  function to allocate Rx buffers and inserts them into the buffer
3318  *  descriptors and then enables the Rx part of the NIC.
3319  *  Return value:
3320  *  0 on success and an appropriate (-)ve integer as defined in errno.h
3321  *   file on failure.
3322  */
3323
3324 static int s2io_open(struct net_device *dev)
3325 {
3326         nic_t *sp = dev->priv;
3327         int err = 0;
3328         int i;
3329         u16 msi_control; /* Temp variable */
3330
3331         /*
3332          * Make sure you have link off by default every time
3333          * Nic is initialized
3334          */
3335         netif_carrier_off(dev);
3336         sp->last_link_state = 0;
3337
3338         /* Initialize H/W and enable interrupts */
3339         if (s2io_card_up(sp)) {
3340                 DBG_PRINT(ERR_DBG, "%s: H/W initialization failed\n",
3341                           dev->name);
3342                 err = -ENODEV;
3343                 goto hw_init_failed;
3344         }
3345
3346         /* Store the values of the MSIX table in the nic_t structure */
3347         store_xmsi_data(sp);
3348
3349         /* After proper initialization of H/W, register ISR */
3350         if (sp->intr_type == MSI) {
3351                 err = request_irq((int) sp->pdev->irq, s2io_msi_handle, 
3352                         SA_SHIRQ, sp->name, dev);
3353                 if (err) {
3354                         DBG_PRINT(ERR_DBG, "%s: MSI registration \
3355 failed\n", dev->name);
3356                         goto isr_registration_failed;
3357                 }
3358         }
3359         if (sp->intr_type == MSI_X) {
3360                 for (i=1; (sp->s2io_entries[i].in_use == MSIX_FLG); i++) {
3361                         if (sp->s2io_entries[i].type == MSIX_FIFO_TYPE) {
3362                                 sprintf(sp->desc1, "%s:MSI-X-%d-TX",
3363                                         dev->name, i);
3364                                 err = request_irq(sp->entries[i].vector,
3365                                           s2io_msix_fifo_handle, 0, sp->desc1,
3366                                           sp->s2io_entries[i].arg);
3367                                 DBG_PRINT(ERR_DBG, "%s @ 0x%llx\n", sp->desc1, 
3368                                     (unsigned long long)sp->msix_info[i].addr);
3369                         } else {
3370                                 sprintf(sp->desc2, "%s:MSI-X-%d-RX",
3371                                         dev->name, i);
3372                                 err = request_irq(sp->entries[i].vector,
3373                                           s2io_msix_ring_handle, 0, sp->desc2,
3374                                           sp->s2io_entries[i].arg);
3375                                 DBG_PRINT(ERR_DBG, "%s @ 0x%llx\n", sp->desc2, 
3376                                      (unsigned long long)sp->msix_info[i].addr);
3377                         }
3378                         if (err) {
3379                                 DBG_PRINT(ERR_DBG, "%s: MSI-X-%d registration \
3380 failed\n", dev->name, i);
3381                                 DBG_PRINT(ERR_DBG, "Returned: %d\n", err);
3382                                 goto isr_registration_failed;
3383                         }
3384                         sp->s2io_entries[i].in_use = MSIX_REGISTERED_SUCCESS;
3385                 }
3386         }
3387         if (sp->intr_type == INTA) {
3388                 err = request_irq((int) sp->pdev->irq, s2io_isr, SA_SHIRQ,
3389                                 sp->name, dev);
3390                 if (err) {
3391                         DBG_PRINT(ERR_DBG, "%s: ISR registration failed\n",
3392                                   dev->name);
3393                         goto isr_registration_failed;
3394                 }
3395         }
3396
3397         if (s2io_set_mac_addr(dev, dev->dev_addr) == FAILURE) {
3398                 DBG_PRINT(ERR_DBG, "Set Mac Address Failed\n");
3399                 err = -ENODEV;
3400                 goto setting_mac_address_failed;
3401         }
3402
3403         netif_start_queue(dev);
3404         return 0;
3405
3406 setting_mac_address_failed:
3407         if (sp->intr_type != MSI_X)
3408                 free_irq(sp->pdev->irq, dev);
3409 isr_registration_failed:
3410         del_timer_sync(&sp->alarm_timer);
3411         if (sp->intr_type == MSI_X) {
3412                 if (sp->device_type == XFRAME_II_DEVICE) {
3413                         for (i=1; (sp->s2io_entries[i].in_use == 
3414                                 MSIX_REGISTERED_SUCCESS); i++) {
3415                                 int vector = sp->entries[i].vector;
3416                                 void *arg = sp->s2io_entries[i].arg;
3417
3418                                 free_irq(vector, arg);
3419                         }
3420                         pci_disable_msix(sp->pdev);
3421
3422                         /* Temp */
3423                         pci_read_config_word(sp->pdev, 0x42, &msi_control);
3424                         msi_control &= 0xFFFE; /* Disable MSI */
3425                         pci_write_config_word(sp->pdev, 0x42, msi_control);
3426                 }
3427         }
3428         else if (sp->intr_type == MSI)
3429                 pci_disable_msi(sp->pdev);
3430         s2io_reset(sp);
3431 hw_init_failed:
3432         if (sp->intr_type == MSI_X) {
3433                 if (sp->entries)
3434                         kfree(sp->entries);
3435                 if (sp->s2io_entries)
3436                         kfree(sp->s2io_entries);
3437         }
3438         return err;
3439 }
3440
3441 /**
3442  *  s2io_close -close entry point of the driver
3443  *  @dev : device pointer.
3444  *  Description:
3445  *  This is the stop entry point of the driver. It needs to undo exactly
3446  *  whatever was done by the open entry point,thus it's usually referred to
3447  *  as the close function.Among other things this function mainly stops the
3448  *  Rx side of the NIC and frees all the Rx buffers in the Rx rings.
3449  *  Return value:
3450  *  0 on success and an appropriate (-)ve integer as defined in errno.h
3451  *  file on failure.
3452  */
3453
3454 static int s2io_close(struct net_device *dev)
3455 {
3456         nic_t *sp = dev->priv;
3457         int i;
3458         u16 msi_control;
3459
3460         flush_scheduled_work();
3461         netif_stop_queue(dev);
3462         /* Reset card, kill tasklet and free Tx and Rx buffers. */
3463         s2io_card_down(sp);
3464
3465         if (sp->intr_type == MSI_X) {
3466                 if (sp->device_type == XFRAME_II_DEVICE) {
3467                         for (i=1; (sp->s2io_entries[i].in_use == 
3468                                         MSIX_REGISTERED_SUCCESS); i++) {
3469                                 int vector = sp->entries[i].vector;
3470                                 void *arg = sp->s2io_entries[i].arg;
3471
3472                                 free_irq(vector, arg);
3473                         }
3474                         pci_read_config_word(sp->pdev, 0x42, &msi_control);
3475                         msi_control &= 0xFFFE; /* Disable MSI */
3476                         pci_write_config_word(sp->pdev, 0x42, msi_control);
3477
3478                         pci_disable_msix(sp->pdev);
3479                 }
3480         }
3481         else {
3482                 free_irq(sp->pdev->irq, dev);
3483                 if (sp->intr_type == MSI)
3484                         pci_disable_msi(sp->pdev);
3485         }       
3486         sp->device_close_flag = TRUE;   /* Device is shut down. */
3487         return 0;
3488 }
3489
3490 /**
3491  *  s2io_xmit - Tx entry point of te driver
3492  *  @skb : the socket buffer containing the Tx data.
3493  *  @dev : device pointer.
3494  *  Description :
3495  *  This function is the Tx entry point of the driver. S2IO NIC supports
3496  *  certain protocol assist features on Tx side, namely  CSO, S/G, LSO.
3497  *  NOTE: when device cant queue the pkt,just the trans_start variable will
3498  *  not be upadted.
3499  *  Return value:
3500  *  0 on success & 1 on failure.
3501  */
3502
3503 static int s2io_xmit(struct sk_buff *skb, struct net_device *dev)
3504 {
3505         nic_t *sp = dev->priv;
3506         u16 frg_cnt, frg_len, i, queue, queue_len, put_off, get_off;
3507         register u64 val64;
3508         TxD_t *txdp;
3509         TxFIFO_element_t __iomem *tx_fifo;
3510         unsigned long flags;
3511 #ifdef NETIF_F_TSO
3512         int mss;
3513 #endif
3514         u16 vlan_tag = 0;
3515         int vlan_priority = 0;
3516         mac_info_t *mac_control;
3517         struct config_param *config;
3518
3519         mac_control = &sp->mac_control;
3520         config = &sp->config;
3521
3522         DBG_PRINT(TX_DBG, "%s: In Neterion Tx routine\n", dev->name);
3523         spin_lock_irqsave(&sp->tx_lock, flags);
3524         if (atomic_read(&sp->card_state) == CARD_DOWN) {
3525                 DBG_PRINT(TX_DBG, "%s: Card going down for reset\n",
3526                           dev->name);
3527                 spin_unlock_irqrestore(&sp->tx_lock, flags);
3528                 dev_kfree_skb(skb);
3529                 return 0;
3530         }
3531
3532         queue = 0;
3533
3534         /* Get Fifo number to Transmit based on vlan priority */
3535         if (sp->vlgrp && vlan_tx_tag_present(skb)) {
3536                 vlan_tag = vlan_tx_tag_get(skb);
3537                 vlan_priority = vlan_tag >> 13;
3538                 queue = config->fifo_mapping[vlan_priority];
3539         }
3540
3541         put_off = (u16) mac_control->fifos[queue].tx_curr_put_info.offset;
3542         get_off = (u16) mac_control->fifos[queue].tx_curr_get_info.offset;
3543         txdp = (TxD_t *) mac_control->fifos[queue].list_info[put_off].
3544                 list_virt_addr;
3545
3546         queue_len = mac_control->fifos[queue].tx_curr_put_info.fifo_len + 1;
3547         /* Avoid "put" pointer going beyond "get" pointer */
3548         if (txdp->Host_Control || (((put_off + 1) % queue_len) == get_off)) {
3549                 DBG_PRINT(TX_DBG, "Error in xmit, No free TXDs.\n");
3550                 netif_stop_queue(dev);
3551                 dev_kfree_skb(skb);
3552                 spin_unlock_irqrestore(&sp->tx_lock, flags);
3553                 return 0;
3554         }
3555
3556         /* A buffer with no data will be dropped */
3557         if (!skb->len) {
3558                 DBG_PRINT(TX_DBG, "%s:Buffer has no data..\n", dev->name);
3559                 dev_kfree_skb(skb);
3560                 spin_unlock_irqrestore(&sp->tx_lock, flags);
3561                 return 0;
3562         }
3563
3564         txdp->Control_1 = 0;
3565         txdp->Control_2 = 0;
3566 #ifdef NETIF_F_TSO
3567         mss = skb_shinfo(skb)->tso_size;
3568         if (mss) {
3569                 txdp->Control_1 |= TXD_TCP_LSO_EN;
3570                 txdp->Control_1 |= TXD_TCP_LSO_MSS(mss);
3571         }
3572 #endif
3573         if (skb->ip_summed == CHECKSUM_HW) {
3574                 txdp->Control_2 |=
3575                     (TXD_TX_CKO_IPV4_EN | TXD_TX_CKO_TCP_EN |
3576                      TXD_TX_CKO_UDP_EN);
3577         }
3578         txdp->Control_1 |= TXD_GATHER_CODE_FIRST;
3579         txdp->Control_1 |= TXD_LIST_OWN_XENA;
3580         txdp->Control_2 |= config->tx_intr_type;
3581
3582         if (sp->vlgrp && vlan_tx_tag_present(skb)) {
3583                 txdp->Control_2 |= TXD_VLAN_ENABLE;
3584                 txdp->Control_2 |= TXD_VLAN_TAG(vlan_tag);
3585         }
3586
3587         frg_len = skb->len - skb->data_len;
3588         if (skb_shinfo(skb)->ufo_size) {
3589                 int ufo_size;
3590
3591                 ufo_size = skb_shinfo(skb)->ufo_size;
3592                 ufo_size &= ~7;
3593                 txdp->Control_1 |= TXD_UFO_EN;
3594                 txdp->Control_1 |= TXD_UFO_MSS(ufo_size);
3595                 txdp->Control_1 |= TXD_BUFFER0_SIZE(8);
3596 #ifdef __BIG_ENDIAN
3597                 sp->ufo_in_band_v[put_off] =
3598                                 (u64)skb_shinfo(skb)->ip6_frag_id;
3599 #else
3600                 sp->ufo_in_band_v[put_off] =
3601                                 (u64)skb_shinfo(skb)->ip6_frag_id << 32;
3602 #endif
3603                 txdp->Host_Control = (unsigned long)sp->ufo_in_band_v;
3604                 txdp->Buffer_Pointer = pci_map_single(sp->pdev,
3605                                         sp->ufo_in_band_v,
3606                                         sizeof(u64), PCI_DMA_TODEVICE);
3607                 txdp++;
3608                 txdp->Control_1 = 0;
3609                 txdp->Control_2 = 0;
3610         }
3611
3612         txdp->Buffer_Pointer = pci_map_single
3613             (sp->pdev, skb->data, frg_len, PCI_DMA_TODEVICE);
3614         txdp->Host_Control = (unsigned long) skb;
3615         txdp->Control_1 |= TXD_BUFFER0_SIZE(frg_len);
3616
3617         if (skb_shinfo(skb)->ufo_size)
3618                 txdp->Control_1 |= TXD_UFO_EN;
3619
3620         frg_cnt = skb_shinfo(skb)->nr_frags;
3621         /* For fragmented SKB. */
3622         for (i = 0; i < frg_cnt; i++) {
3623                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3624                 /* A '0' length fragment will be ignored */
3625                 if (!frag->size)
3626                         continue;
3627                 txdp++;
3628                 txdp->Buffer_Pointer = (u64) pci_map_page
3629                     (sp->pdev, frag->page, frag->page_offset,
3630                      frag->size, PCI_DMA_TODEVICE);
3631                 txdp->Control_1 = TXD_BUFFER0_SIZE(frag->size);
3632                 if (skb_shinfo(skb)->ufo_size)
3633                         txdp->Control_1 |= TXD_UFO_EN;
3634         }
3635         txdp->Control_1 |= TXD_GATHER_CODE_LAST;
3636
3637         if (skb_shinfo(skb)->ufo_size)
3638                 frg_cnt++; /* as Txd0 was used for inband header */
3639
3640         tx_fifo = mac_control->tx_FIFO_start[queue];
3641         val64 = mac_control->fifos[queue].list_info[put_off].list_phy_addr;
3642         writeq(val64, &tx_fifo->TxDL_Pointer);
3643
3644         val64 = (TX_FIFO_LAST_TXD_NUM(frg_cnt) | TX_FIFO_FIRST_LIST |
3645                  TX_FIFO_LAST_LIST);
3646
3647 #ifdef NETIF_F_TSO
3648         if (mss)
3649                 val64 |= TX_FIFO_SPECIAL_FUNC;
3650 #endif
3651         if (skb_shinfo(skb)->ufo_size)
3652                 val64 |= TX_FIFO_SPECIAL_FUNC;
3653         writeq(val64, &tx_fifo->List_Control);
3654
3655         mmiowb();
3656
3657         put_off++;
3658         put_off %= mac_control->fifos[queue].tx_curr_put_info.fifo_len + 1;
3659         mac_control->fifos[queue].tx_curr_put_info.offset = put_off;
3660
3661         /* Avoid "put" pointer going beyond "get" pointer */
3662         if (((put_off + 1) % queue_len) == get_off) {
3663                 DBG_PRINT(TX_DBG,
3664                           "No free TxDs for xmit, Put: 0x%x Get:0x%x\n",
3665                           put_off, get_off);
3666                 netif_stop_queue(dev);
3667         }
3668
3669         dev->trans_start = jiffies;
3670         spin_unlock_irqrestore(&sp->tx_lock, flags);
3671
3672         return 0;
3673 }
3674
3675 static void
3676 s2io_alarm_handle(unsigned long data)
3677 {
3678         nic_t *sp = (nic_t *)data;
3679
3680         alarm_intr_handler(sp);
3681         mod_timer(&sp->alarm_timer, jiffies + HZ / 2);
3682 }
3683
3684 static irqreturn_t
3685 s2io_msi_handle(int irq, void *dev_id, struct pt_regs *regs)
3686 {
3687         struct net_device *dev = (struct net_device *) dev_id;
3688         nic_t *sp = dev->priv;
3689         int i;
3690         int ret;
3691         mac_info_t *mac_control;
3692         struct config_param *config;
3693
3694         atomic_inc(&sp->isr_cnt);
3695         mac_control = &sp->mac_control;
3696         config = &sp->config;
3697         DBG_PRINT(INTR_DBG, "%s: MSI handler\n", __FUNCTION__);
3698
3699         /* If Intr is because of Rx Traffic */
3700         for (i = 0; i < config->rx_ring_num; i++)
3701                 rx_intr_handler(&mac_control->rings[i]);
3702
3703         /* If Intr is because of Tx Traffic */
3704         for (i = 0; i < config->tx_fifo_num; i++)
3705                 tx_intr_handler(&mac_control->fifos[i]);
3706
3707         /*
3708          * If the Rx buffer count is below the panic threshold then
3709          * reallocate the buffers from the interrupt handler itself,
3710          * else schedule a tasklet to reallocate the buffers.
3711          */
3712         for (i = 0; i < config->rx_ring_num; i++) {
3713                 if (!sp->lro) {
3714                         int rxb_size = atomic_read(&sp->rx_bufs_left[i]);
3715                         int level = rx_buffer_level(sp, rxb_size, i);
3716
3717                         if ((level == PANIC) && (!TASKLET_IN_USE)) {
3718                                 DBG_PRINT(INTR_DBG, "%s: Rx BD hit ", 
3719                                                         dev->name);
3720                                 DBG_PRINT(INTR_DBG, "PANIC levels\n");
3721                                 if ((ret = fill_rx_buffers(sp, i)) == -ENOMEM) {
3722                                         DBG_PRINT(ERR_DBG, "%s:Out of memory",
3723                                                   dev->name);
3724                                         DBG_PRINT(ERR_DBG, " in ISR!!\n");
3725                                         clear_bit(0, (&sp->tasklet_status));
3726                                         atomic_dec(&sp->isr_cnt);
3727                                         return IRQ_HANDLED;
3728                                 }
3729                                 clear_bit(0, (&sp->tasklet_status));
3730                         } else if (level == LOW) {
3731                                 tasklet_schedule(&sp->task);
3732                         }
3733                 }
3734                 else if (fill_rx_buffers(sp, i) == -ENOMEM) {
3735                                 DBG_PRINT(ERR_DBG, "%s:Out of memory",
3736                                                         dev->name);
3737                                 DBG_PRINT(ERR_DBG, " in Rx Intr!!\n");
3738                                 break;
3739                 }
3740         }
3741
3742         atomic_dec(&sp->isr_cnt);
3743         return IRQ_HANDLED;
3744 }
3745
3746 static irqreturn_t
3747 s2io_msix_ring_handle(int irq, void *dev_id, struct pt_regs *regs)
3748 {
3749         ring_info_t *ring = (ring_info_t *)dev_id;
3750         nic_t *sp = ring->nic;
3751         struct net_device *dev = (struct net_device *) dev_id;
3752         int rxb_size, level, rng_n;
3753
3754         atomic_inc(&sp->isr_cnt);
3755         rx_intr_handler(ring);
3756
3757         rng_n = ring->ring_no;
3758         if (!sp->lro) {
3759                 rxb_size = atomic_read(&sp->rx_bufs_left[rng_n]);
3760                 level = rx_buffer_level(sp, rxb_size, rng_n);
3761
3762                 if ((level == PANIC) && (!TASKLET_IN_USE)) {
3763                         int ret;
3764                         DBG_PRINT(INTR_DBG, "%s: Rx BD hit ", __FUNCTION__);
3765                         DBG_PRINT(INTR_DBG, "PANIC levels\n");
3766                         if ((ret = fill_rx_buffers(sp, rng_n)) == -ENOMEM) {
3767                                 DBG_PRINT(ERR_DBG, "Out of memory in %s",
3768                                           __FUNCTION__);
3769                                 clear_bit(0, (&sp->tasklet_status));
3770                                 return IRQ_HANDLED;
3771                         }
3772                         clear_bit(0, (&sp->tasklet_status));
3773                 } else if (level == LOW) {
3774                         tasklet_schedule(&sp->task);
3775                 }
3776         }
3777         else if (fill_rx_buffers(sp, rng_n) == -ENOMEM) {
3778                         DBG_PRINT(ERR_DBG, "%s:Out of memory", dev->name);
3779                         DBG_PRINT(ERR_DBG, " in Rx Intr!!\n");
3780         }
3781
3782         atomic_dec(&sp->isr_cnt);
3783
3784         return IRQ_HANDLED;
3785 }
3786
3787 static irqreturn_t
3788 s2io_msix_fifo_handle(int irq, void *dev_id, struct pt_regs *regs)
3789 {
3790         fifo_info_t *fifo = (fifo_info_t *)dev_id;
3791         nic_t *sp = fifo->nic;
3792
3793         atomic_inc(&sp->isr_cnt);
3794         tx_intr_handler(fifo);
3795         atomic_dec(&sp->isr_cnt);
3796         return IRQ_HANDLED;
3797 }
3798
3799 static void s2io_txpic_intr_handle(nic_t *sp)
3800 {
3801         XENA_dev_config_t __iomem *bar0 = sp->bar0;
3802         u64 val64;
3803
3804         val64 = readq(&bar0->pic_int_status);
3805         if (val64 & PIC_INT_GPIO) {
3806                 val64 = readq(&bar0->gpio_int_reg);
3807                 if ((val64 & GPIO_INT_REG_LINK_DOWN) &&
3808                     (val64 & GPIO_INT_REG_LINK_UP)) {
3809                         val64 |=  GPIO_INT_REG_LINK_DOWN;
3810                         val64 |= GPIO_INT_REG_LINK_UP;
3811                         writeq(val64, &bar0->gpio_int_reg);
3812                         goto masking;
3813                 }
3814
3815                 if (((sp->last_link_state == LINK_UP) &&
3816                         (val64 & GPIO_INT_REG_LINK_DOWN)) ||
3817                 ((sp->last_link_state == LINK_DOWN) &&
3818                 (val64 & GPIO_INT_REG_LINK_UP))) {
3819                         val64 = readq(&bar0->gpio_int_mask);
3820                         val64 |=  GPIO_INT_MASK_LINK_DOWN;
3821                         val64 |= GPIO_INT_MASK_LINK_UP;
3822                         writeq(val64, &bar0->gpio_int_mask);
3823                         s2io_set_link((unsigned long)sp);
3824                 }
3825 masking:
3826                 if (sp->last_link_state == LINK_UP) {
3827                         /*enable down interrupt */
3828                         val64 = readq(&bar0->gpio_int_mask);
3829                         /* unmasks link down intr */
3830                         val64 &=  ~GPIO_INT_MASK_LINK_DOWN;
3831                         /* masks link up intr */
3832                         val64 |= GPIO_INT_MASK_LINK_UP;
3833                         writeq(val64, &bar0->gpio_int_mask);
3834                 } else {
3835                         /*enable UP Interrupt */
3836                         val64 = readq(&bar0->gpio_int_mask);
3837                         /* unmasks link up interrupt */
3838                         val64 &= ~GPIO_INT_MASK_LINK_UP;
3839                         /* masks link down interrupt */
3840                         val64 |=  GPIO_INT_MASK_LINK_DOWN;
3841                         writeq(val64, &bar0->gpio_int_mask);
3842                 }
3843         }
3844 }
3845
3846 /**
3847  *  s2io_isr - ISR handler of the device .
3848  *  @irq: the irq of the device.
3849  *  @dev_id: a void pointer to the dev structure of the NIC.
3850  *  @pt_regs: pointer to the registers pushed on the stack.
3851  *  Description:  This function is the ISR handler of the device. It
3852  *  identifies the reason for the interrupt and calls the relevant
3853  *  service routines. As a contongency measure, this ISR allocates the
3854  *  recv buffers, if their numbers are below the panic value which is
3855  *  presently set to 25% of the original number of rcv buffers allocated.
3856  *  Return value:
3857  *   IRQ_HANDLED: will be returned if IRQ was handled by this routine
3858  *   IRQ_NONE: will be returned if interrupt is not from our device
3859  */
3860 static irqreturn_t s2io_isr(int irq, void *dev_id, struct pt_regs *regs)
3861 {
3862         struct net_device *dev = (struct net_device *) dev_id;
3863         nic_t *sp = dev->priv;
3864         XENA_dev_config_t __iomem *bar0 = sp->bar0;
3865         int i;
3866         u64 reason = 0, val64;
3867         mac_info_t *mac_control;
3868         struct config_param *config;
3869
3870         atomic_inc(&sp->isr_cnt);
3871         mac_control = &sp->mac_control;
3872         config = &sp->config;
3873
3874         /*
3875          * Identify the cause for interrupt and call the appropriate
3876          * interrupt handler. Causes for the interrupt could be;
3877          * 1. Rx of packet.
3878          * 2. Tx complete.
3879          * 3. Link down.
3880          * 4. Error in any functional blocks of the NIC.
3881          */
3882         reason = readq(&bar0->general_int_status);
3883
3884         if (!reason) {
3885                 /* The interrupt was not raised by Xena. */
3886                 atomic_dec(&sp->isr_cnt);
3887                 return IRQ_NONE;
3888         }
3889
3890 #ifdef CONFIG_S2IO_NAPI
3891         if (reason & GEN_INTR_RXTRAFFIC) {
3892                 if (netif_rx_schedule_prep(dev)) {
3893                         en_dis_able_nic_intrs(sp, RX_TRAFFIC_INTR,
3894                                               DISABLE_INTRS);
3895                         __netif_rx_schedule(dev);
3896                 }
3897         }
3898 #else
3899         /* If Intr is because of Rx Traffic */
3900         if (reason & GEN_INTR_RXTRAFFIC) {
3901                 /*
3902                  * rx_traffic_int reg is an R1 register, writing all 1's
3903                  * will ensure that the actual interrupt causing bit get's
3904                  * cleared and hence a read can be avoided.
3905                  */
3906                 val64 = 0xFFFFFFFFFFFFFFFFULL;
3907                 writeq(val64, &bar0->rx_traffic_int);
3908                 for (i = 0; i < config->rx_ring_num; i++) {
3909                         rx_intr_handler(&mac_control->rings[i]);
3910                 }
3911         }
3912 #endif
3913
3914         /* If Intr is because of Tx Traffic */
3915         if (reason & GEN_INTR_TXTRAFFIC) {
3916                 /*
3917                  * tx_traffic_int reg is an R1 register, writing all 1's
3918                  * will ensure that the actual interrupt causing bit get's
3919                  * cleared and hence a read can be avoided.
3920                  */
3921                 val64 = 0xFFFFFFFFFFFFFFFFULL;
3922                 writeq(val64, &bar0->tx_traffic_int);
3923
3924                 for (i = 0; i < config->tx_fifo_num; i++)
3925                         tx_intr_handler(&mac_control->fifos[i]);
3926         }
3927
3928         if (reason & GEN_INTR_TXPIC)
3929                 s2io_txpic_intr_handle(sp);
3930         /*
3931          * If the Rx buffer count is below the panic threshold then
3932          * reallocate the buffers from the interrupt handler itself,
3933          * else schedule a tasklet to reallocate the buffers.
3934          */
3935 #ifndef CONFIG_S2IO_NAPI
3936         for (i = 0; i < config->rx_ring_num; i++) {
3937                 if (!sp->lro) {
3938                         int ret;
3939                         int rxb_size = atomic_read(&sp->rx_bufs_left[i]);
3940                         int level = rx_buffer_level(sp, rxb_size, i);
3941
3942                         if ((level == PANIC) && (!TASKLET_IN_USE)) {
3943                                 DBG_PRINT(INTR_DBG, "%s: Rx BD hit ", 
3944                                                         dev->name);
3945                                 DBG_PRINT(INTR_DBG, "PANIC levels\n");
3946                                 if ((ret = fill_rx_buffers(sp, i)) == -ENOMEM) {
3947                                         DBG_PRINT(ERR_DBG, "%s:Out of memory",
3948                                                   dev->name);
3949                                         DBG_PRINT(ERR_DBG, " in ISR!!\n");
3950                                         clear_bit(0, (&sp->tasklet_status));
3951                                         atomic_dec(&sp->isr_cnt);
3952                                         return IRQ_HANDLED;
3953                                 }
3954                                 clear_bit(0, (&sp->tasklet_status));
3955                         } else if (level == LOW) {
3956                                 tasklet_schedule(&sp->task);
3957                         }
3958                 }
3959                 else if (fill_rx_buffers(sp, i) == -ENOMEM) {
3960                                 DBG_PRINT(ERR_DBG, "%s:Out of memory",
3961                                                         dev->name);
3962                                 DBG_PRINT(ERR_DBG, " in Rx intr!!\n");
3963                                 break;
3964                 }
3965         }
3966 #endif
3967
3968         atomic_dec(&sp->isr_cnt);
3969         return IRQ_HANDLED;
3970 }
3971
3972 /**
3973  * s2io_updt_stats -
3974  */
3975 static void s2io_updt_stats(nic_t *sp)
3976 {
3977         XENA_dev_config_t __iomem *bar0 = sp->bar0;
3978         u64 val64;
3979         int cnt = 0;
3980
3981         if (atomic_read(&sp->card_state) == CARD_UP) {
3982                 /* Apprx 30us on a 133 MHz bus */
3983                 val64 = SET_UPDT_CLICKS(10) |
3984                         STAT_CFG_ONE_SHOT_EN | STAT_CFG_STAT_EN;
3985                 writeq(val64, &bar0->stat_cfg);
3986                 do {
3987                         udelay(100);
3988                         val64 = readq(&bar0->stat_cfg);
3989                         if (!(val64 & BIT(0)))
3990                                 break;
3991                         cnt++;
3992                         if (cnt == 5)
3993                                 break; /* Updt failed */
3994                 } while(1);
3995         }
3996 }
3997
3998 /**
3999  *  s2io_get_stats - Updates the device statistics structure.
4000  *  @dev : pointer to the device structure.
4001  *  Description:
4002  *  This function updates the device statistics structure in the s2io_nic
4003  *  structure and returns a pointer to the same.
4004  *  Return value:
4005  *  pointer to the updated net_device_stats structure.
4006  */
4007
4008 static struct net_device_stats *s2io_get_stats(struct net_device *dev)
4009 {
4010         nic_t *sp = dev->priv;
4011         mac_info_t *mac_control;
4012         struct config_param *config;
4013
4014
4015         mac_control = &sp->mac_control;
4016         config = &sp->config;
4017
4018         /* Configure Stats for immediate updt */
4019         s2io_updt_stats(sp);
4020
4021         sp->stats.tx_packets =
4022                 le32_to_cpu(mac_control->stats_info->tmac_frms);
4023         sp->stats.tx_errors =
4024                 le32_to_cpu(mac_control->stats_info->tmac_any_err_frms);
4025         sp->stats.rx_errors =
4026                 le32_to_cpu(mac_control->stats_info->rmac_drop_frms);
4027         sp->stats.multicast =
4028                 le32_to_cpu(mac_control->stats_info->rmac_vld_mcst_frms);
4029         sp->stats.rx_length_errors =
4030                 le32_to_cpu(mac_control->stats_info->rmac_long_frms);
4031
4032         return (&sp->stats);
4033 }
4034
4035 /**
4036  *  s2io_set_multicast - entry point for multicast address enable/disable.
4037  *  @dev : pointer to the device structure
4038  *  Description:
4039  *  This function is a driver entry point which gets called by the kernel
4040  *  whenever multicast addresses must be enabled/disabled. This also gets
4041  *  called to set/reset promiscuous mode. Depending on the deivce flag, we
4042  *  determine, if multicast address must be enabled or if promiscuous mode
4043  *  is to be disabled etc.
4044  *  Return value:
4045  *  void.
4046  */
4047
4048 static void s2io_set_multicast(struct net_device *dev)
4049 {
4050         int i, j, prev_cnt;
4051         struct dev_mc_list *mclist;
4052         nic_t *sp = dev->priv;
4053         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4054         u64 val64 = 0, multi_mac = 0x010203040506ULL, mask =
4055             0xfeffffffffffULL;
4056         u64 dis_addr = 0xffffffffffffULL, mac_addr = 0;
4057         void __iomem *add;
4058
4059         if ((dev->flags & IFF_ALLMULTI) && (!sp->m_cast_flg)) {
4060                 /*  Enable all Multicast addresses */
4061                 writeq(RMAC_ADDR_DATA0_MEM_ADDR(multi_mac),
4062                        &bar0->rmac_addr_data0_mem);
4063                 writeq(RMAC_ADDR_DATA1_MEM_MASK(mask),
4064                        &bar0->rmac_addr_data1_mem);
4065                 val64 = RMAC_ADDR_CMD_MEM_WE |
4066                     RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
4067                     RMAC_ADDR_CMD_MEM_OFFSET(MAC_MC_ALL_MC_ADDR_OFFSET);
4068                 writeq(val64, &bar0->rmac_addr_cmd_mem);
4069                 /* Wait till command completes */
4070                 wait_for_cmd_complete(sp);
4071
4072                 sp->m_cast_flg = 1;
4073                 sp->all_multi_pos = MAC_MC_ALL_MC_ADDR_OFFSET;
4074         } else if ((dev->flags & IFF_ALLMULTI) && (sp->m_cast_flg)) {
4075                 /*  Disable all Multicast addresses */
4076                 writeq(RMAC_ADDR_DATA0_MEM_ADDR(dis_addr),
4077                        &bar0->rmac_addr_data0_mem);
4078                 writeq(RMAC_ADDR_DATA1_MEM_MASK(0x0),
4079                        &bar0->rmac_addr_data1_mem);
4080                 val64 = RMAC_ADDR_CMD_MEM_WE |
4081                     RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
4082                     RMAC_ADDR_CMD_MEM_OFFSET(sp->all_multi_pos);
4083                 writeq(val64, &bar0->rmac_addr_cmd_mem);
4084                 /* Wait till command completes */
4085                 wait_for_cmd_complete(sp);
4086
4087                 sp->m_cast_flg = 0;
4088                 sp->all_multi_pos = 0;
4089         }
4090
4091         if ((dev->flags & IFF_PROMISC) && (!sp->promisc_flg)) {
4092                 /*  Put the NIC into promiscuous mode */
4093                 add = &bar0->mac_cfg;
4094                 val64 = readq(&bar0->mac_cfg);
4095                 val64 |= MAC_CFG_RMAC_PROM_ENABLE;
4096
4097                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
4098                 writel((u32) val64, add);
4099                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
4100                 writel((u32) (val64 >> 32), (add + 4));
4101
4102                 val64 = readq(&bar0->mac_cfg);
4103                 sp->promisc_flg = 1;
4104                 DBG_PRINT(INFO_DBG, "%s: entered promiscuous mode\n",
4105                           dev->name);
4106         } else if (!(dev->flags & IFF_PROMISC) && (sp->promisc_flg)) {
4107                 /*  Remove the NIC from promiscuous mode */
4108                 add = &bar0->mac_cfg;
4109                 val64 = readq(&bar0->mac_cfg);
4110                 val64 &= ~MAC_CFG_RMAC_PROM_ENABLE;
4111
4112                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
4113                 writel((u32) val64, add);
4114                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
4115                 writel((u32) (val64 >> 32), (add + 4));
4116
4117                 val64 = readq(&bar0->mac_cfg);
4118                 sp->promisc_flg = 0;
4119                 DBG_PRINT(INFO_DBG, "%s: left promiscuous mode\n",
4120                           dev->name);
4121         }
4122
4123         /*  Update individual M_CAST address list */
4124         if ((!sp->m_cast_flg) && dev->mc_count) {
4125                 if (dev->mc_count >
4126                     (MAX_ADDRS_SUPPORTED - MAC_MC_ADDR_START_OFFSET - 1)) {
4127                         DBG_PRINT(ERR_DBG, "%s: No more Rx filters ",
4128                                   dev->name);
4129                         DBG_PRINT(ERR_DBG, "can be added, please enable ");
4130                         DBG_PRINT(ERR_DBG, "ALL_MULTI instead\n");
4131                         return;
4132                 }
4133
4134                 prev_cnt = sp->mc_addr_count;
4135                 sp->mc_addr_count = dev->mc_count;
4136
4137                 /* Clear out the previous list of Mc in the H/W. */
4138                 for (i = 0; i < prev_cnt; i++) {
4139                         writeq(RMAC_ADDR_DATA0_MEM_ADDR(dis_addr),
4140                                &bar0->rmac_addr_data0_mem);
4141                         writeq(RMAC_ADDR_DATA1_MEM_MASK(0ULL),
4142                                 &bar0->rmac_addr_data1_mem);
4143                         val64 = RMAC_ADDR_CMD_MEM_WE |
4144                             RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
4145                             RMAC_ADDR_CMD_MEM_OFFSET
4146                             (MAC_MC_ADDR_START_OFFSET + i);
4147                         writeq(val64, &bar0->rmac_addr_cmd_mem);
4148
4149                         /* Wait for command completes */
4150                         if (wait_for_cmd_complete(sp)) {
4151                                 DBG_PRINT(ERR_DBG, "%s: Adding ",
4152                                           dev->name);
4153                                 DBG_PRINT(ERR_DBG, "Multicasts failed\n");
4154                                 return;
4155                         }
4156                 }
4157
4158                 /* Create the new Rx filter list and update the same in H/W. */
4159                 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
4160                      i++, mclist = mclist->next) {
4161                         memcpy(sp->usr_addrs[i].addr, mclist->dmi_addr,
4162                                ETH_ALEN);
4163                         for (j = 0; j < ETH_ALEN; j++) {
4164                                 mac_addr |= mclist->dmi_addr[j];
4165                                 mac_addr <<= 8;
4166                         }
4167                         mac_addr >>= 8;
4168                         writeq(RMAC_ADDR_DATA0_MEM_ADDR(mac_addr),
4169                                &bar0->rmac_addr_data0_mem);
4170                         writeq(RMAC_ADDR_DATA1_MEM_MASK(0ULL),
4171                                 &bar0->rmac_addr_data1_mem);
4172                         val64 = RMAC_ADDR_CMD_MEM_WE |
4173                             RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
4174                             RMAC_ADDR_CMD_MEM_OFFSET
4175                             (i + MAC_MC_ADDR_START_OFFSET);
4176                         writeq(val64, &bar0->rmac_addr_cmd_mem);
4177
4178                         /* Wait for command completes */
4179                         if (wait_for_cmd_complete(sp)) {
4180                                 DBG_PRINT(ERR_DBG, "%s: Adding ",
4181                                           dev->name);
4182                                 DBG_PRINT(ERR_DBG, "Multicasts failed\n");
4183                                 return;
4184                         }
4185                 }
4186         }
4187 }
4188
4189 /**
4190  *  s2io_set_mac_addr - Programs the Xframe mac address
4191  *  @dev : pointer to the device structure.
4192  *  @addr: a uchar pointer to the new mac address which is to be set.
4193  *  Description : This procedure will program the Xframe to receive
4194  *  frames with new Mac Address
4195  *  Return value: SUCCESS on success and an appropriate (-)ve integer
4196  *  as defined in errno.h file on failure.
4197  */
4198
4199 int s2io_set_mac_addr(struct net_device *dev, u8 * addr)
4200 {
4201         nic_t *sp = dev->priv;
4202         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4203         register u64 val64, mac_addr = 0;
4204         int i;
4205
4206         /*
4207          * Set the new MAC address as the new unicast filter and reflect this
4208          * change on the device address registered with the OS. It will be
4209          * at offset 0.
4210          */
4211         for (i = 0; i < ETH_ALEN; i++) {
4212                 mac_addr <<= 8;
4213                 mac_addr |= addr[i];
4214         }
4215
4216         writeq(RMAC_ADDR_DATA0_MEM_ADDR(mac_addr),
4217                &bar0->rmac_addr_data0_mem);
4218
4219         val64 =
4220             RMAC_ADDR_CMD_MEM_WE | RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
4221             RMAC_ADDR_CMD_MEM_OFFSET(0);
4222         writeq(val64, &bar0->rmac_addr_cmd_mem);
4223         /* Wait till command completes */
4224         if (wait_for_cmd_complete(sp)) {
4225                 DBG_PRINT(ERR_DBG, "%s: set_mac_addr failed\n", dev->name);
4226                 return FAILURE;
4227         }
4228
4229         return SUCCESS;
4230 }
4231
4232 /**
4233  * s2io_ethtool_sset - Sets different link parameters.
4234  * @sp : private member of the device structure, which is a pointer to the  * s2io_nic structure.
4235  * @info: pointer to the structure with parameters given by ethtool to set
4236  * link information.
4237  * Description:
4238  * The function sets different link parameters provided by the user onto
4239  * the NIC.
4240  * Return value:
4241  * 0 on success.
4242 */
4243
4244 static int s2io_ethtool_sset(struct net_device *dev,
4245                              struct ethtool_cmd *info)
4246 {
4247         nic_t *sp = dev->priv;
4248         if ((info->autoneg == AUTONEG_ENABLE) ||
4249             (info->speed != SPEED_10000) || (info->duplex != DUPLEX_FULL))
4250                 return -EINVAL;
4251         else {
4252                 s2io_close(sp->dev);
4253                 s2io_open(sp->dev);
4254         }
4255
4256         return 0;
4257 }
4258
4259 /**
4260  * s2io_ethtol_gset - Return link specific information.
4261  * @sp : private member of the device structure, pointer to the
4262  *      s2io_nic structure.
4263  * @info : pointer to the structure with parameters given by ethtool
4264  * to return link information.
4265  * Description:
4266  * Returns link specific information like speed, duplex etc.. to ethtool.
4267  * Return value :
4268  * return 0 on success.
4269  */
4270
4271 static int s2io_ethtool_gset(struct net_device *dev, struct ethtool_cmd *info)
4272 {
4273         nic_t *sp = dev->priv;
4274         info->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
4275         info->advertising = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
4276         info->port = PORT_FIBRE;
4277         /* info->transceiver?? TODO */
4278
4279         if (netif_carrier_ok(sp->dev)) {
4280                 info->speed = 10000;
4281                 info->duplex = DUPLEX_FULL;
4282         } else {
4283                 info->speed = -1;
4284                 info->duplex = -1;
4285         }
4286
4287         info->autoneg = AUTONEG_DISABLE;
4288         return 0;
4289 }
4290
4291 /**
4292  * s2io_ethtool_gdrvinfo - Returns driver specific information.
4293  * @sp : private member of the device structure, which is a pointer to the
4294  * s2io_nic structure.
4295  * @info : pointer to the structure with parameters given by ethtool to
4296  * return driver information.
4297  * Description:
4298  * Returns driver specefic information like name, version etc.. to ethtool.
4299  * Return value:
4300  *  void
4301  */
4302
4303 static void s2io_ethtool_gdrvinfo(struct net_device *dev,
4304                                   struct ethtool_drvinfo *info)
4305 {
4306         nic_t *sp = dev->priv;
4307
4308         strncpy(info->driver, s2io_driver_name, sizeof(info->driver));
4309         strncpy(info->version, s2io_driver_version, sizeof(info->version));
4310         strncpy(info->fw_version, "", sizeof(info->fw_version));
4311         strncpy(info->bus_info, pci_name(sp->pdev), sizeof(info->bus_info));
4312         info->regdump_len = XENA_REG_SPACE;
4313         info->eedump_len = XENA_EEPROM_SPACE;
4314         info->testinfo_len = S2IO_TEST_LEN;
4315         info->n_stats = S2IO_STAT_LEN;
4316 }
4317
4318 /**
4319  *  s2io_ethtool_gregs - dumps the entire space of Xfame into the buffer.
4320  *  @sp: private member of the device structure, which is a pointer to the
4321  *  s2io_nic structure.
4322  *  @regs : pointer to the structure with parameters given by ethtool for
4323  *  dumping the registers.
4324  *  @reg_space: The input argumnet into which all the registers are dumped.
4325  *  Description:
4326  *  Dumps the entire register space of xFrame NIC into the user given
4327  *  buffer area.
4328  * Return value :
4329  * void .
4330 */
4331
4332 static void s2io_ethtool_gregs(struct net_device *dev,
4333                                struct ethtool_regs *regs, void *space)
4334 {
4335         int i;
4336         u64 reg;
4337         u8 *reg_space = (u8 *) space;
4338         nic_t *sp = dev->priv;
4339
4340         regs->len = XENA_REG_SPACE;
4341         regs->version = sp->pdev->subsystem_device;
4342
4343         for (i = 0; i < regs->len; i += 8) {
4344                 reg = readq(sp->bar0 + i);
4345                 memcpy((reg_space + i), &reg, 8);
4346         }
4347 }
4348
4349 /**
4350  *  s2io_phy_id  - timer function that alternates adapter LED.
4351  *  @data : address of the private member of the device structure, which
4352  *  is a pointer to the s2io_nic structure, provided as an u32.
4353  * Description: This is actually the timer function that alternates the
4354  * adapter LED bit of the adapter control bit to set/reset every time on
4355  * invocation. The timer is set for 1/2 a second, hence tha NIC blinks
4356  *  once every second.
4357 */
4358 static void s2io_phy_id(unsigned long data)
4359 {
4360         nic_t *sp = (nic_t *) data;
4361         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4362         u64 val64 = 0;
4363         u16 subid;
4364
4365         subid = sp->pdev->subsystem_device;
4366         if ((sp->device_type == XFRAME_II_DEVICE) ||
4367                    ((subid & 0xFF) >= 0x07)) {
4368                 val64 = readq(&bar0->gpio_control);
4369                 val64 ^= GPIO_CTRL_GPIO_0;
4370                 writeq(val64, &bar0->gpio_control);
4371         } else {
4372                 val64 = readq(&bar0->adapter_control);
4373                 val64 ^= ADAPTER_LED_ON;
4374                 writeq(val64, &bar0->adapter_control);
4375         }
4376
4377         mod_timer(&sp->id_timer, jiffies + HZ / 2);
4378 }
4379
4380 /**
4381  * s2io_ethtool_idnic - To physically identify the nic on the system.
4382  * @sp : private member of the device structure, which is a pointer to the
4383  * s2io_nic structure.
4384  * @id : pointer to the structure with identification parameters given by
4385  * ethtool.
4386  * Description: Used to physically identify the NIC on the system.
4387  * The Link LED will blink for a time specified by the user for
4388  * identification.
4389  * NOTE: The Link has to be Up to be able to blink the LED. Hence
4390  * identification is possible only if it's link is up.
4391  * Return value:
4392  * int , returns 0 on success
4393  */
4394
4395 static int s2io_ethtool_idnic(struct net_device *dev, u32 data)
4396 {
4397         u64 val64 = 0, last_gpio_ctrl_val;
4398         nic_t *sp = dev->priv;
4399         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4400         u16 subid;
4401
4402         subid = sp->pdev->subsystem_device;
4403         last_gpio_ctrl_val = readq(&bar0->gpio_control);
4404         if ((sp->device_type == XFRAME_I_DEVICE) &&
4405                 ((subid & 0xFF) < 0x07)) {
4406                 val64 = readq(&bar0->adapter_control);
4407                 if (!(val64 & ADAPTER_CNTL_EN)) {
4408                         printk(KERN_ERR
4409                                "Adapter Link down, cannot blink LED\n");
4410                         return -EFAULT;
4411                 }
4412         }
4413         if (sp->id_timer.function == NULL) {
4414                 init_timer(&sp->id_timer);
4415                 sp->id_timer.function = s2io_phy_id;
4416                 sp->id_timer.data = (unsigned long) sp;
4417         }
4418         mod_timer(&sp->id_timer, jiffies);
4419         if (data)
4420                 msleep_interruptible(data * HZ);
4421         else
4422                 msleep_interruptible(MAX_FLICKER_TIME);
4423         del_timer_sync(&sp->id_timer);
4424
4425         if (CARDS_WITH_FAULTY_LINK_INDICATORS(sp->device_type, subid)) {
4426                 writeq(last_gpio_ctrl_val, &bar0->gpio_control);
4427                 last_gpio_ctrl_val = readq(&bar0->gpio_control);
4428         }
4429
4430         return 0;
4431 }
4432
4433 /**
4434  * s2io_ethtool_getpause_data -Pause frame frame generation and reception.
4435  * @sp : private member of the device structure, which is a pointer to the
4436  *      s2io_nic structure.
4437  * @ep : pointer to the structure with pause parameters given by ethtool.
4438  * Description:
4439  * Returns the Pause frame generation and reception capability of the NIC.
4440  * Return value:
4441  *  void
4442  */
4443 static void s2io_ethtool_getpause_data(struct net_device *dev,
4444                                        struct ethtool_pauseparam *ep)
4445 {
4446         u64 val64;
4447         nic_t *sp = dev->priv;
4448         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4449
4450         val64 = readq(&bar0->rmac_pause_cfg);
4451         if (val64 & RMAC_PAUSE_GEN_ENABLE)
4452                 ep->tx_pause = TRUE;
4453         if (val64 & RMAC_PAUSE_RX_ENABLE)
4454                 ep->rx_pause = TRUE;
4455         ep->autoneg = FALSE;
4456 }
4457
4458 /**
4459  * s2io_ethtool_setpause_data -  set/reset pause frame generation.
4460  * @sp : private member of the device structure, which is a pointer to the
4461  *      s2io_nic structure.
4462  * @ep : pointer to the structure with pause parameters given by ethtool.
4463  * Description:
4464  * It can be used to set or reset Pause frame generation or reception
4465  * support of the NIC.
4466  * Return value:
4467  * int, returns 0 on Success
4468  */
4469
4470 static int s2io_ethtool_setpause_data(struct net_device *dev,
4471                                struct ethtool_pauseparam *ep)
4472 {
4473         u64 val64;
4474         nic_t *sp = dev->priv;
4475         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4476
4477         val64 = readq(&bar0->rmac_pause_cfg);
4478         if (ep->tx_pause)
4479                 val64 |= RMAC_PAUSE_GEN_ENABLE;
4480         else
4481                 val64 &= ~RMAC_PAUSE_GEN_ENABLE;
4482         if (ep->rx_pause)
4483                 val64 |= RMAC_PAUSE_RX_ENABLE;
4484         else
4485                 val64 &= ~RMAC_PAUSE_RX_ENABLE;
4486         writeq(val64, &bar0->rmac_pause_cfg);
4487         return 0;
4488 }
4489
4490 /**
4491  * read_eeprom - reads 4 bytes of data from user given offset.
4492  * @sp : private member of the device structure, which is a pointer to the
4493  *      s2io_nic structure.
4494  * @off : offset at which the data must be written
4495  * @data : Its an output parameter where the data read at the given
4496  *      offset is stored.
4497  * Description:
4498  * Will read 4 bytes of data from the user given offset and return the
4499  * read data.
4500  * NOTE: Will allow to read only part of the EEPROM visible through the
4501  *   I2C bus.
4502  * Return value:
4503  *  -1 on failure and 0 on success.
4504  */
4505
4506 #define S2IO_DEV_ID             5
4507 static int read_eeprom(nic_t * sp, int off, u64 * data)
4508 {
4509         int ret = -1;
4510         u32 exit_cnt = 0;
4511         u64 val64;
4512         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4513
4514         if (sp->device_type == XFRAME_I_DEVICE) {
4515                 val64 = I2C_CONTROL_DEV_ID(S2IO_DEV_ID) | I2C_CONTROL_ADDR(off) |
4516                     I2C_CONTROL_BYTE_CNT(0x3) | I2C_CONTROL_READ |
4517                     I2C_CONTROL_CNTL_START;
4518                 SPECIAL_REG_WRITE(val64, &bar0->i2c_control, LF);
4519
4520                 while (exit_cnt < 5) {
4521                         val64 = readq(&bar0->i2c_control);
4522                         if (I2C_CONTROL_CNTL_END(val64)) {
4523                                 *data = I2C_CONTROL_GET_DATA(val64);
4524                                 ret = 0;
4525                                 break;
4526                         }
4527                         msleep(50);
4528                         exit_cnt++;
4529                 }
4530         }
4531
4532         if (sp->device_type == XFRAME_II_DEVICE) {
4533                 val64 = SPI_CONTROL_KEY(0x9) | SPI_CONTROL_SEL1 |
4534                         SPI_CONTROL_BYTECNT(0x3) | 
4535                         SPI_CONTROL_CMD(0x3) | SPI_CONTROL_ADDR(off);
4536                 SPECIAL_REG_WRITE(val64, &bar0->spi_control, LF);
4537                 val64 |= SPI_CONTROL_REQ;
4538                 SPECIAL_REG_WRITE(val64, &bar0->spi_control, LF);
4539                 while (exit_cnt < 5) {
4540                         val64 = readq(&bar0->spi_control);
4541                         if (val64 & SPI_CONTROL_NACK) {
4542                                 ret = 1;
4543                                 break;
4544                         } else if (val64 & SPI_CONTROL_DONE) {
4545                                 *data = readq(&bar0->spi_data);
4546                                 *data &= 0xffffff;
4547                                 ret = 0;
4548                                 break;
4549                         }
4550                         msleep(50);
4551                         exit_cnt++;
4552                 }
4553         }
4554         return ret;
4555 }
4556
4557 /**
4558  *  write_eeprom - actually writes the relevant part of the data value.
4559  *  @sp : private member of the device structure, which is a pointer to the
4560  *       s2io_nic structure.
4561  *  @off : offset at which the data must be written
4562  *  @data : The data that is to be written
4563  *  @cnt : Number of bytes of the data that are actually to be written into
4564  *  the Eeprom. (max of 3)
4565  * Description:
4566  *  Actually writes the relevant part of the data value into the Eeprom
4567  *  through the I2C bus.
4568  * Return value:
4569  *  0 on success, -1 on failure.
4570  */
4571
4572 static int write_eeprom(nic_t * sp, int off, u64 data, int cnt)
4573 {
4574         int exit_cnt = 0, ret = -1;
4575         u64 val64;
4576         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4577
4578         if (sp->device_type == XFRAME_I_DEVICE) {
4579                 val64 = I2C_CONTROL_DEV_ID(S2IO_DEV_ID) | I2C_CONTROL_ADDR(off) |
4580                     I2C_CONTROL_BYTE_CNT(cnt) | I2C_CONTROL_SET_DATA((u32)data) |
4581                     I2C_CONTROL_CNTL_START;
4582                 SPECIAL_REG_WRITE(val64, &bar0->i2c_control, LF);
4583
4584                 while (exit_cnt < 5) {
4585                         val64 = readq(&bar0->i2c_control);
4586                         if (I2C_CONTROL_CNTL_END(val64)) {
4587                                 if (!(val64 & I2C_CONTROL_NACK))
4588                                         ret = 0;
4589                                 break;
4590                         }
4591                         msleep(50);
4592                         exit_cnt++;
4593                 }
4594         }
4595
4596         if (sp->device_type == XFRAME_II_DEVICE) {
4597                 int write_cnt = (cnt == 8) ? 0 : cnt;
4598                 writeq(SPI_DATA_WRITE(data,(cnt<<3)), &bar0->spi_data);
4599
4600                 val64 = SPI_CONTROL_KEY(0x9) | SPI_CONTROL_SEL1 |
4601                         SPI_CONTROL_BYTECNT(write_cnt) | 
4602                         SPI_CONTROL_CMD(0x2) | SPI_CONTROL_ADDR(off);
4603                 SPECIAL_REG_WRITE(val64, &bar0->spi_control, LF);
4604                 val64 |= SPI_CONTROL_REQ;
4605                 SPECIAL_REG_WRITE(val64, &bar0->spi_control, LF);
4606                 while (exit_cnt < 5) {
4607                         val64 = readq(&bar0->spi_control);
4608                         if (val64 & SPI_CONTROL_NACK) {
4609                                 ret = 1;
4610                                 break;
4611                         } else if (val64 & SPI_CONTROL_DONE) {
4612                                 ret = 0;
4613                                 break;
4614                         }
4615                         msleep(50);
4616                         exit_cnt++;
4617                 }
4618         }
4619         return ret;
4620 }
4621
4622 /**
4623  *  s2io_ethtool_geeprom  - reads the value stored in the Eeprom.
4624  *  @sp : private member of the device structure, which is a pointer to the *       s2io_nic structure.
4625  *  @eeprom : pointer to the user level structure provided by ethtool,
4626  *  containing all relevant information.
4627  *  @data_buf : user defined value to be written into Eeprom.
4628  *  Description: Reads the values stored in the Eeprom at given offset
4629  *  for a given length. Stores these values int the input argument data
4630  *  buffer 'data_buf' and returns these to the caller (ethtool.)
4631  *  Return value:
4632  *  int  0 on success
4633  */
4634
4635 static int s2io_ethtool_geeprom(struct net_device *dev,
4636                          struct ethtool_eeprom *eeprom, u8 * data_buf)
4637 {
4638         u32 i, valid;
4639         u64 data;
4640         nic_t *sp = dev->priv;
4641
4642         eeprom->magic = sp->pdev->vendor | (sp->pdev->device << 16);
4643
4644         if ((eeprom->offset + eeprom->len) > (XENA_EEPROM_SPACE))
4645                 eeprom->len = XENA_EEPROM_SPACE - eeprom->offset;
4646
4647         for (i = 0; i < eeprom->len; i += 4) {
4648                 if (read_eeprom(sp, (eeprom->offset + i), &data)) {
4649                         DBG_PRINT(ERR_DBG, "Read of EEPROM failed\n");
4650                         return -EFAULT;
4651                 }
4652                 valid = INV(data);
4653                 memcpy((data_buf + i), &valid, 4);
4654         }
4655         return 0;
4656 }
4657
4658 /**
4659  *  s2io_ethtool_seeprom - tries to write the user provided value in Eeprom
4660  *  @sp : private member of the device structure, which is a pointer to the
4661  *  s2io_nic structure.
4662  *  @eeprom : pointer to the user level structure provided by ethtool,
4663  *  containing all relevant information.
4664  *  @data_buf ; user defined value to be written into Eeprom.
4665  *  Description:
4666  *  Tries to write the user provided value in the Eeprom, at the offset
4667  *  given by the user.
4668  *  Return value:
4669  *  0 on success, -EFAULT on failure.
4670  */
4671
4672 static int s2io_ethtool_seeprom(struct net_device *dev,
4673                                 struct ethtool_eeprom *eeprom,
4674                                 u8 * data_buf)
4675 {
4676         int len = eeprom->len, cnt = 0;
4677         u64 valid = 0, data;
4678         nic_t *sp = dev->priv;
4679
4680         if (eeprom->magic != (sp->pdev->vendor | (sp->pdev->device << 16))) {
4681                 DBG_PRINT(ERR_DBG,
4682                           "ETHTOOL_WRITE_EEPROM Err: Magic value ");
4683                 DBG_PRINT(ERR_DBG, "is wrong, Its not 0x%x\n",
4684                           eeprom->magic);
4685                 return -EFAULT;
4686         }
4687
4688         while (len) {
4689                 data = (u32) data_buf[cnt] & 0x000000FF;
4690                 if (data) {
4691                         valid = (u32) (data << 24);
4692                 } else
4693                         valid = data;
4694
4695                 if (write_eeprom(sp, (eeprom->offset + cnt), valid, 0)) {
4696                         DBG_PRINT(ERR_DBG,
4697                                   "ETHTOOL_WRITE_EEPROM Err: Cannot ");
4698                         DBG_PRINT(ERR_DBG,
4699                                   "write into the specified offset\n");
4700                         return -EFAULT;
4701                 }
4702                 cnt++;
4703                 len--;
4704         }
4705
4706         return 0;
4707 }
4708
4709 /**
4710  * s2io_register_test - reads and writes into all clock domains.
4711  * @sp : private member of the device structure, which is a pointer to the
4712  * s2io_nic structure.
4713  * @data : variable that returns the result of each of the test conducted b
4714  * by the driver.
4715  * Description:
4716  * Read and write into all clock domains. The NIC has 3 clock domains,
4717  * see that registers in all the three regions are accessible.
4718  * Return value:
4719  * 0 on success.
4720  */
4721
4722 static int s2io_register_test(nic_t * sp, uint64_t * data)
4723 {
4724         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4725         u64 val64 = 0, exp_val;
4726         int fail = 0;
4727
4728         val64 = readq(&bar0->pif_rd_swapper_fb);
4729         if (val64 != 0x123456789abcdefULL) {
4730                 fail = 1;
4731                 DBG_PRINT(INFO_DBG, "Read Test level 1 fails\n");
4732         }
4733
4734         val64 = readq(&bar0->rmac_pause_cfg);
4735         if (val64 != 0xc000ffff00000000ULL) {
4736                 fail = 1;
4737                 DBG_PRINT(INFO_DBG, "Read Test level 2 fails\n");
4738         }
4739
4740         val64 = readq(&bar0->rx_queue_cfg);
4741         if (sp->device_type == XFRAME_II_DEVICE)
4742                 exp_val = 0x0404040404040404ULL;
4743         else
4744                 exp_val = 0x0808080808080808ULL;
4745         if (val64 != exp_val) {
4746                 fail = 1;
4747                 DBG_PRINT(INFO_DBG, "Read Test level 3 fails\n");
4748         }
4749
4750         val64 = readq(&bar0->xgxs_efifo_cfg);
4751         if (val64 != 0x000000001923141EULL) {
4752                 fail = 1;
4753                 DBG_PRINT(INFO_DBG, "Read Test level 4 fails\n");
4754         }
4755
4756         val64 = 0x5A5A5A5A5A5A5A5AULL;
4757         writeq(val64, &bar0->xmsi_data);
4758         val64 = readq(&bar0->xmsi_data);
4759         if (val64 != 0x5A5A5A5A5A5A5A5AULL) {
4760                 fail = 1;
4761                 DBG_PRINT(ERR_DBG, "Write Test level 1 fails\n");
4762         }
4763
4764         val64 = 0xA5A5A5A5A5A5A5A5ULL;
4765         writeq(val64, &bar0->xmsi_data);
4766         val64 = readq(&bar0->xmsi_data);
4767         if (val64 != 0xA5A5A5A5A5A5A5A5ULL) {
4768                 fail = 1;
4769                 DBG_PRINT(ERR_DBG, "Write Test level 2 fails\n");
4770         }
4771
4772         *data = fail;
4773         return fail;
4774 }
4775
4776 /**
4777  * s2io_eeprom_test - to verify that EEprom in the xena can be programmed.
4778  * @sp : private member of the device structure, which is a pointer to the
4779  * s2io_nic structure.
4780  * @data:variable that returns the result of each of the test conducted by
4781  * the driver.
4782  * Description:
4783  * Verify that EEPROM in the xena can be programmed using I2C_CONTROL
4784  * register.
4785  * Return value:
4786  * 0 on success.
4787  */
4788
4789 static int s2io_eeprom_test(nic_t * sp, uint64_t * data)
4790 {
4791         int fail = 0;
4792         u64 ret_data, org_4F0, org_7F0;
4793         u8 saved_4F0 = 0, saved_7F0 = 0;
4794         struct net_device *dev = sp->dev;
4795
4796         /* Test Write Error at offset 0 */
4797         /* Note that SPI interface allows write access to all areas
4798          * of EEPROM. Hence doing all negative testing only for Xframe I.
4799          */
4800         if (sp->device_type == XFRAME_I_DEVICE)
4801                 if (!write_eeprom(sp, 0, 0, 3))
4802                         fail = 1;
4803
4804         /* Save current values at offsets 0x4F0 and 0x7F0 */
4805         if (!read_eeprom(sp, 0x4F0, &org_4F0))
4806                 saved_4F0 = 1;
4807         if (!read_eeprom(sp, 0x7F0, &org_7F0))
4808                 saved_7F0 = 1;
4809
4810         /* Test Write at offset 4f0 */
4811         if (write_eeprom(sp, 0x4F0, 0x012345, 3))
4812                 fail = 1;
4813         if (read_eeprom(sp, 0x4F0, &ret_data))
4814                 fail = 1;
4815
4816         if (ret_data != 0x012345) {
4817                 DBG_PRINT(ERR_DBG, "%s: eeprom test error at offset 0x4F0. "
4818                         "Data written %llx Data read %llx\n",
4819                         dev->name, (unsigned long long)0x12345,
4820                         (unsigned long long)ret_data);
4821                 fail = 1;
4822         }
4823
4824         /* Reset the EEPROM data go FFFF */
4825         write_eeprom(sp, 0x4F0, 0xFFFFFF, 3);
4826
4827         /* Test Write Request Error at offset 0x7c */
4828         if (sp->device_type == XFRAME_I_DEVICE)
4829                 if (!write_eeprom(sp, 0x07C, 0, 3))
4830                         fail = 1;
4831
4832         /* Test Write Request at offset 0x7f0 */
4833         if (write_eeprom(sp, 0x7F0, 0x012345, 3))
4834                 fail = 1;
4835         if (read_eeprom(sp, 0x7F0, &ret_data))
4836                 fail = 1;
4837
4838         if (ret_data != 0x012345) {
4839                 DBG_PRINT(ERR_DBG, "%s: eeprom test error at offset 0x7F0. "
4840                         "Data written %llx Data read %llx\n",
4841                         dev->name, (unsigned long long)0x12345,
4842                         (unsigned long long)ret_data);
4843                 fail = 1;
4844         }
4845
4846         /* Reset the EEPROM data go FFFF */
4847         write_eeprom(sp, 0x7F0, 0xFFFFFF, 3);
4848
4849         if (sp->device_type == XFRAME_I_DEVICE) {
4850                 /* Test Write Error at offset 0x80 */
4851                 if (!write_eeprom(sp, 0x080, 0, 3))
4852                         fail = 1;
4853
4854                 /* Test Write Error at offset 0xfc */
4855                 if (!write_eeprom(sp, 0x0FC, 0, 3))
4856                         fail = 1;
4857
4858                 /* Test Write Error at offset 0x100 */
4859                 if (!write_eeprom(sp, 0x100, 0, 3))
4860                         fail = 1;
4861
4862                 /* Test Write Error at offset 4ec */
4863                 if (!write_eeprom(sp, 0x4EC, 0, 3))
4864                         fail = 1;
4865         }
4866
4867         /* Restore values at offsets 0x4F0 and 0x7F0 */
4868         if (saved_4F0)
4869                 write_eeprom(sp, 0x4F0, org_4F0, 3);
4870         if (saved_7F0)
4871                 write_eeprom(sp, 0x7F0, org_7F0, 3);
4872
4873         *data = fail;
4874         return fail;
4875 }
4876
4877 /**
4878  * s2io_bist_test - invokes the MemBist test of the card .
4879  * @sp : private member of the device structure, which is a pointer to the
4880  * s2io_nic structure.
4881  * @data:variable that returns the result of each of the test conducted by
4882  * the driver.
4883  * Description:
4884  * This invokes the MemBist test of the card. We give around
4885  * 2 secs time for the Test to complete. If it's still not complete
4886  * within this peiod, we consider that the test failed.
4887  * Return value:
4888  * 0 on success and -1 on failure.
4889  */
4890
4891 static int s2io_bist_test(nic_t * sp, uint64_t * data)
4892 {
4893         u8 bist = 0;
4894         int cnt = 0, ret = -1;
4895
4896         pci_read_config_byte(sp->pdev, PCI_BIST, &bist);
4897         bist |= PCI_BIST_START;
4898         pci_write_config_word(sp->pdev, PCI_BIST, bist);
4899
4900         while (cnt < 20) {
4901                 pci_read_config_byte(sp->pdev, PCI_BIST, &bist);
4902                 if (!(bist & PCI_BIST_START)) {
4903                         *data = (bist & PCI_BIST_CODE_MASK);
4904                         ret = 0;
4905                         break;
4906                 }
4907                 msleep(100);
4908                 cnt++;
4909         }
4910
4911         return ret;
4912 }
4913
4914 /**
4915  * s2io-link_test - verifies the link state of the nic
4916  * @sp ; private member of the device structure, which is a pointer to the
4917  * s2io_nic structure.
4918  * @data: variable that returns the result of each of the test conducted by
4919  * the driver.
4920  * Description:
4921  * The function verifies the link state of the NIC and updates the input
4922  * argument 'data' appropriately.
4923  * Return value:
4924  * 0 on success.
4925  */
4926
4927 static int s2io_link_test(nic_t * sp, uint64_t * data)
4928 {
4929         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4930         u64 val64;
4931
4932         val64 = readq(&bar0->adapter_status);
4933         if (val64 & ADAPTER_STATUS_RMAC_LOCAL_FAULT)
4934                 *data = 1;
4935
4936         return 0;
4937 }
4938
4939 /**
4940  * s2io_rldram_test - offline test for access to the RldRam chip on the NIC
4941  * @sp - private member of the device structure, which is a pointer to the
4942  * s2io_nic structure.
4943  * @data - variable that returns the result of each of the test
4944  * conducted by the driver.
4945  * Description:
4946  *  This is one of the offline test that tests the read and write
4947  *  access to the RldRam chip on the NIC.
4948  * Return value:
4949  *  0 on success.
4950  */
4951
4952 static int s2io_rldram_test(nic_t * sp, uint64_t * data)
4953 {
4954         XENA_dev_config_t __iomem *bar0 = sp->bar0;
4955         u64 val64;
4956         int cnt, iteration = 0, test_fail = 0;
4957
4958         val64 = readq(&bar0->adapter_control);
4959         val64 &= ~ADAPTER_ECC_EN;
4960         writeq(val64, &bar0->adapter_control);
4961
4962         val64 = readq(&bar0->mc_rldram_test_ctrl);
4963         val64 |= MC_RLDRAM_TEST_MODE;
4964         SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_test_ctrl, LF);
4965
4966         val64 = readq(&bar0->mc_rldram_mrs);
4967         val64 |= MC_RLDRAM_QUEUE_SIZE_ENABLE;
4968         SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_mrs, UF);
4969
4970         val64 |= MC_RLDRAM_MRS_ENABLE;
4971         SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_mrs, UF);
4972
4973         while (iteration < 2) {
4974                 val64 = 0x55555555aaaa0000ULL;
4975                 if (iteration == 1) {
4976                         val64 ^= 0xFFFFFFFFFFFF0000ULL;
4977                 }
4978                 writeq(val64, &bar0->mc_rldram_test_d0);
4979
4980                 val64 = 0xaaaa5a5555550000ULL;
4981                 if (iteration == 1) {
4982                         val64 ^= 0xFFFFFFFFFFFF0000ULL;
4983                 }
4984                 writeq(val64, &bar0->mc_rldram_test_d1);
4985
4986                 val64 = 0x55aaaaaaaa5a0000ULL;
4987                 if (iteration == 1) {
4988                         val64 ^= 0xFFFFFFFFFFFF0000ULL;
4989                 }
4990                 writeq(val64, &bar0->mc_rldram_test_d2);
4991
4992                 val64 = (u64) (0x0000003ffffe0100ULL);
4993                 writeq(val64, &bar0->mc_rldram_test_add);
4994
4995                 val64 = MC_RLDRAM_TEST_MODE | MC_RLDRAM_TEST_WRITE |
4996                         MC_RLDRAM_TEST_GO;
4997                 SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_test_ctrl, LF);
4998
4999                 for (cnt = 0; cnt < 5; cnt++) {
5000                         val64 = readq(&bar0->mc_rldram_test_ctrl);
5001                         if (val64 & MC_RLDRAM_TEST_DONE)
5002                                 break;
5003                         msleep(200);
5004                 }
5005
5006                 if (cnt == 5)
5007                         break;
5008
5009                 val64 = MC_RLDRAM_TEST_MODE | MC_RLDRAM_TEST_GO;
5010                 SPECIAL_REG_WRITE(val64, &bar0->mc_rldram_test_ctrl, LF);
5011
5012                 for (cnt = 0; cnt < 5; cnt++) {
5013                         val64 = readq(&bar0->mc_rldram_test_ctrl);
5014                         if (val64 & MC_RLDRAM_TEST_DONE)
5015                                 break;
5016                         msleep(500);
5017                 }
5018
5019                 if (cnt == 5)
5020                         break;
5021
5022                 val64 = readq(&bar0->mc_rldram_test_ctrl);
5023                 if (!(val64 & MC_RLDRAM_TEST_PASS))
5024                         test_fail = 1;
5025
5026                 iteration++;
5027         }
5028
5029         *data = test_fail;
5030
5031         /* Bring the adapter out of test mode */
5032         SPECIAL_REG_WRITE(0, &bar0->mc_rldram_test_ctrl, LF);
5033
5034         return test_fail;
5035 }
5036
5037 /**
5038  *  s2io_ethtool_test - conducts 6 tsets to determine the health of card.
5039  *  @sp : private member of the device structure, which is a pointer to the
5040  *  s2io_nic structure.
5041  *  @ethtest : pointer to a ethtool command specific structure that will be
5042  *  returned to the user.
5043  *  @data : variable that returns the result of each of the test
5044  * conducted by the driver.
5045  * Description:
5046  *  This function conducts 6 tests ( 4 offline and 2 online) to determine
5047  *  the health of the card.
5048  * Return value:
5049  *  void
5050  */
5051
5052 static void s2io_ethtool_test(struct net_device *dev,
5053                               struct ethtool_test *ethtest,
5054                               uint64_t * data)
5055 {
5056         nic_t *sp = dev->priv;
5057         int orig_state = netif_running(sp->dev);
5058
5059         if (ethtest->flags == ETH_TEST_FL_OFFLINE) {
5060                 /* Offline Tests. */
5061                 if (orig_state)
5062                         s2io_close(sp->dev);
5063
5064                 if (s2io_register_test(sp, &data[0]))
5065                         ethtest->flags |= ETH_TEST_FL_FAILED;
5066
5067                 s2io_reset(sp);
5068
5069                 if (s2io_rldram_test(sp, &data[3]))
5070                         ethtest->flags |= ETH_TEST_FL_FAILED;
5071
5072                 s2io_reset(sp);
5073
5074                 if (s2io_eeprom_test(sp, &data[1]))
5075                         ethtest->flags |= ETH_TEST_FL_FAILED;
5076
5077                 if (s2io_bist_test(sp, &data[4]))
5078                         ethtest->flags |= ETH_TEST_FL_FAILED;
5079
5080                 if (orig_state)
5081                         s2io_open(sp->dev);
5082
5083                 data[2] = 0;
5084         } else {
5085                 /* Online Tests. */
5086                 if (!orig_state) {
5087                         DBG_PRINT(ERR_DBG,
5088                                   "%s: is not up, cannot run test\n",
5089                                   dev->name);
5090                         data[0] = -1;
5091                         data[1] = -1;
5092                         data[2] = -1;
5093                         data[3] = -1;
5094                         data[4] = -1;
5095                 }
5096
5097                 if (s2io_link_test(sp, &data[2]))
5098                         ethtest->flags |= ETH_TEST_FL_FAILED;
5099
5100                 data[0] = 0;
5101                 data[1] = 0;
5102                 data[3] = 0;
5103                 data[4] = 0;
5104         }
5105 }
5106
5107 static void s2io_get_ethtool_stats(struct net_device *dev,
5108                                    struct ethtool_stats *estats,
5109                                    u64 * tmp_stats)
5110 {
5111         int i = 0;
5112         nic_t *sp = dev->priv;
5113         StatInfo_t *stat_info = sp->mac_control.stats_info;
5114         u64 tmp;
5115
5116         s2io_updt_stats(sp);
5117         tmp_stats[i++] =
5118                 (u64)le32_to_cpu(stat_info->tmac_frms_oflow) << 32  |
5119                 le32_to_cpu(stat_info->tmac_frms);
5120         tmp_stats[i++] =
5121                 (u64)le32_to_cpu(stat_info->tmac_data_octets_oflow) << 32 |
5122                 le32_to_cpu(stat_info->tmac_data_octets);
5123         tmp_stats[i++] = le64_to_cpu(stat_info->tmac_drop_frms);
5124         tmp_stats[i++] =
5125                 (u64)le32_to_cpu(stat_info->tmac_mcst_frms_oflow) << 32 |
5126                 le32_to_cpu(stat_info->tmac_mcst_frms);
5127         tmp_stats[i++] =
5128                 (u64)le32_to_cpu(stat_info->tmac_bcst_frms_oflow) << 32 |
5129                 le32_to_cpu(stat_info->tmac_bcst_frms);
5130         tmp_stats[i++] = le64_to_cpu(stat_info->tmac_pause_ctrl_frms);
5131         tmp_stats[i++] =
5132                 (u64)le32_to_cpu(stat_info->tmac_any_err_frms_oflow) << 32 |
5133                 le32_to_cpu(stat_info->tmac_any_err_frms);
5134         tmp_stats[i++] = le64_to_cpu(stat_info->tmac_vld_ip_octets);
5135         tmp_stats[i++] =
5136                 (u64)le32_to_cpu(stat_info->tmac_vld_ip_oflow) << 32 |
5137                 le32_to_cpu(stat_info->tmac_vld_ip);
5138         tmp_stats[i++] =
5139                 (u64)le32_to_cpu(stat_info->tmac_drop_ip_oflow) << 32 |
5140                 le32_to_cpu(stat_info->tmac_drop_ip);
5141         tmp_stats[i++] =
5142                 (u64)le32_to_cpu(stat_info->tmac_icmp_oflow) << 32 |
5143                 le32_to_cpu(stat_info->tmac_icmp);
5144         tmp_stats[i++] =
5145                 (u64)le32_to_cpu(stat_info->tmac_rst_tcp_oflow) << 32 |
5146                 le32_to_cpu(stat_info->tmac_rst_tcp);
5147         tmp_stats[i++] = le64_to_cpu(stat_info->tmac_tcp);
5148         tmp_stats[i++] = (u64)le32_to_cpu(stat_info->tmac_udp_oflow) << 32 |
5149                 le32_to_cpu(stat_info->tmac_udp);
5150         tmp_stats[i++] =
5151                 (u64)le32_to_cpu(stat_info->rmac_vld_frms_oflow) << 32 |
5152                 le32_to_cpu(stat_info->rmac_vld_frms);
5153         tmp_stats[i++] =
5154                 (u64)le32_to_cpu(stat_info->rmac_data_octets_oflow) << 32 |
5155                 le32_to_cpu(stat_info->rmac_data_octets);
5156         tmp_stats[i++] = le64_to_cpu(stat_info->rmac_fcs_err_frms);
5157         tmp_stats[i++] = le64_to_cpu(stat_info->rmac_drop_frms);
5158         tmp_stats[i++] =
5159                 (u64)le32_to_cpu(stat_info->rmac_vld_mcst_frms_oflow) << 32 |
5160                 le32_to_cpu(stat_info->rmac_vld_mcst_frms);
5161         tmp_stats[i++] =
5162                 (u64)le32_to_cpu(stat_info->rmac_vld_bcst_frms_oflow) << 32 |
5163                 le32_to_cpu(stat_info->rmac_vld_bcst_frms);
5164         tmp_stats[i++] = le32_to_cpu(stat_info->rmac_in_rng_len_err_frms);
5165         tmp_stats[i++] = le64_to_cpu(stat_info->rmac_long_frms);
5166         tmp_stats[i++] = le64_to_cpu(stat_info->rmac_pause_ctrl_frms);
5167         tmp_stats[i++] =
5168                 (u64)le32_to_cpu(stat_info->rmac_discarded_frms_oflow) << 32 |
5169                 le32_to_cpu(stat_info->rmac_discarded_frms);
5170         tmp_stats[i++] =
5171                 (u64)le32_to_cpu(stat_info->rmac_usized_frms_oflow) << 32 |
5172                 le32_to_cpu(stat_info->rmac_usized_frms);
5173         tmp_stats[i++] =
5174                 (u64)le32_to_cpu(stat_info->rmac_osized_frms_oflow) << 32 |
5175                 le32_to_cpu(stat_info->rmac_osized_frms);
5176         tmp_stats[i++] =
5177                 (u64)le32_to_cpu(stat_info->rmac_frag_frms_oflow) << 32 |
5178                 le32_to_cpu(stat_info->rmac_frag_frms);
5179         tmp_stats[i++] =
5180                 (u64)le32_to_cpu(stat_info->rmac_jabber_frms_oflow) << 32 |
5181                 le32_to_cpu(stat_info->rmac_jabber_frms);
5182         tmp_stats[i++] = (u64)le32_to_cpu(stat_info->rmac_ip_oflow) << 32 |
5183                 le32_to_cpu(stat_info->rmac_ip);
5184         tmp_stats[i++] = le64_to_cpu(stat_info->rmac_ip_octets);
5185         tmp_stats[i++] = le32_to_cpu(stat_info->rmac_hdr_err_ip);
5186         tmp_stats[i++] = (u64)le32_to_cpu(stat_info->rmac_drop_ip_oflow) << 32 |
5187                 le32_to_cpu(stat_info->rmac_drop_ip);
5188         tmp_stats[i++] = (u64)le32_to_cpu(stat_info->rmac_icmp_oflow) << 32 |
5189                 le32_to_cpu(stat_info->rmac_icmp);
5190         tmp_stats[i++] = le64_to_cpu(stat_info->rmac_tcp);
5191         tmp_stats[i++] = (u64)le32_to_cpu(stat_info->rmac_udp_oflow) << 32 |
5192                 le32_to_cpu(stat_info->rmac_udp);
5193         tmp_stats[i++] =
5194                 (u64)le32_to_cpu(stat_info->rmac_err_drp_udp_oflow) << 32 |
5195                 le32_to_cpu(stat_info->rmac_err_drp_udp);
5196         tmp_stats[i++] =
5197                 (u64)le32_to_cpu(stat_info->rmac_pause_cnt_oflow) << 32 |
5198                 le32_to_cpu(stat_info->rmac_pause_cnt);
5199         tmp_stats[i++] =
5200                 (u64)le32_to_cpu(stat_info->rmac_accepted_ip_oflow) << 32 |
5201                 le32_to_cpu(stat_info->rmac_accepted_ip);
5202         tmp_stats[i++] = le32_to_cpu(stat_info->rmac_err_tcp);
5203         tmp_stats[i++] = 0;
5204         tmp_stats[i++] = stat_info->sw_stat.single_ecc_errs;
5205         tmp_stats[i++] = stat_info->sw_stat.double_ecc_errs;
5206         tmp_stats[i++] = stat_info->sw_stat.clubbed_frms_cnt;
5207         tmp_stats[i++] = stat_info->sw_stat.sending_both;
5208         tmp_stats[i++] = stat_info->sw_stat.outof_sequence_pkts;
5209         tmp_stats[i++] = stat_info->sw_stat.flush_max_pkts;
5210         tmp = 0;
5211         if (stat_info->sw_stat.num_aggregations) {
5212                 tmp = stat_info->sw_stat.sum_avg_pkts_aggregated;
5213                 do_div(tmp, stat_info->sw_stat.num_aggregations);
5214         }
5215         tmp_stats[i++] = tmp;
5216 }
5217
5218 static int s2io_ethtool_get_regs_len(struct net_device *dev)
5219 {
5220         return (XENA_REG_SPACE);
5221 }
5222
5223
5224 static u32 s2io_ethtool_get_rx_csum(struct net_device * dev)
5225 {
5226         nic_t *sp = dev->priv;
5227
5228         return (sp->rx_csum);
5229 }
5230
5231 static int s2io_ethtool_set_rx_csum(struct net_device *dev, u32 data)
5232 {
5233         nic_t *sp = dev->priv;
5234
5235         if (data)
5236                 sp->rx_csum = 1;
5237         else
5238                 sp->rx_csum = 0;
5239
5240         return 0;
5241 }
5242
5243 static int s2io_get_eeprom_len(struct net_device *dev)
5244 {
5245         return (XENA_EEPROM_SPACE);
5246 }
5247
5248 static int s2io_ethtool_self_test_count(struct net_device *dev)
5249 {
5250         return (S2IO_TEST_LEN);
5251 }
5252
5253 static void s2io_ethtool_get_strings(struct net_device *dev,
5254                                      u32 stringset, u8 * data)
5255 {
5256         switch (stringset) {
5257         case ETH_SS_TEST:
5258                 memcpy(data, s2io_gstrings, S2IO_STRINGS_LEN);
5259                 break;
5260         case ETH_SS_STATS:
5261                 memcpy(data, &ethtool_stats_keys,
5262                        sizeof(ethtool_stats_keys));
5263         }
5264 }
5265 static int s2io_ethtool_get_stats_count(struct net_device *dev)
5266 {
5267         return (S2IO_STAT_LEN);
5268 }
5269
5270 static int s2io_ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
5271 {
5272         if (data)
5273                 dev->features |= NETIF_F_IP_CSUM;
5274         else
5275                 dev->features &= ~NETIF_F_IP_CSUM;
5276
5277         return 0;
5278 }
5279
5280
5281 static struct ethtool_ops netdev_ethtool_ops = {
5282         .get_settings = s2io_ethtool_gset,
5283         .set_settings = s2io_ethtool_sset,
5284         .get_drvinfo = s2io_ethtool_gdrvinfo,
5285         .get_regs_len = s2io_ethtool_get_regs_len,
5286         .get_regs = s2io_ethtool_gregs,
5287         .get_link = ethtool_op_get_link,
5288         .get_eeprom_len = s2io_get_eeprom_len,
5289         .get_eeprom = s2io_ethtool_geeprom,
5290         .set_eeprom = s2io_ethtool_seeprom,
5291         .get_pauseparam = s2io_ethtool_getpause_data,
5292         .set_pauseparam = s2io_ethtool_setpause_data,
5293         .get_rx_csum = s2io_ethtool_get_rx_csum,
5294         .set_rx_csum = s2io_ethtool_set_rx_csum,
5295         .get_tx_csum = ethtool_op_get_tx_csum,
5296         .set_tx_csum = s2io_ethtool_op_set_tx_csum,
5297         .get_sg = ethtool_op_get_sg,
5298         .set_sg = ethtool_op_set_sg,
5299 #ifdef NETIF_F_TSO
5300         .get_tso = ethtool_op_get_tso,
5301         .set_tso = ethtool_op_set_tso,
5302 #endif
5303         .get_ufo = ethtool_op_get_ufo,
5304         .set_ufo = ethtool_op_set_ufo,
5305         .self_test_count = s2io_ethtool_self_test_count,
5306         .self_test = s2io_ethtool_test,
5307         .get_strings = s2io_ethtool_get_strings,
5308         .phys_id = s2io_ethtool_idnic,
5309         .get_stats_count = s2io_ethtool_get_stats_count,
5310         .get_ethtool_stats = s2io_get_ethtool_stats
5311 };
5312
5313 /**
5314  *  s2io_ioctl - Entry point for the Ioctl
5315  *  @dev :  Device pointer.
5316  *  @ifr :  An IOCTL specefic structure, that can contain a pointer to
5317  *  a proprietary structure used to pass information to the driver.
5318  *  @cmd :  This is used to distinguish between the different commands that
5319  *  can be passed to the IOCTL functions.
5320  *  Description:
5321  *  Currently there are no special functionality supported in IOCTL, hence
5322  *  function always return EOPNOTSUPPORTED
5323  */
5324
5325 static int s2io_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5326 {
5327         return -EOPNOTSUPP;
5328 }
5329
5330 /**
5331  *  s2io_change_mtu - entry point to change MTU size for the device.
5332  *   @dev : device pointer.
5333  *   @new_mtu : the new MTU size for the device.
5334  *   Description: A driver entry point to change MTU size for the device.
5335  *   Before changing the MTU the device must be stopped.
5336  *  Return value:
5337  *   0 on success and an appropriate (-)ve integer as defined in errno.h
5338  *   file on failure.
5339  */
5340
5341 static int s2io_change_mtu(struct net_device *dev, int new_mtu)
5342 {
5343         nic_t *sp = dev->priv;
5344
5345         if ((new_mtu < MIN_MTU) || (new_mtu > S2IO_JUMBO_SIZE)) {
5346                 DBG_PRINT(ERR_DBG, "%s: MTU size is invalid.\n",
5347                           dev->name);
5348                 return -EPERM;
5349         }
5350
5351         dev->mtu = new_mtu;
5352         if (netif_running(dev)) {
5353                 s2io_card_down(sp);
5354                 netif_stop_queue(dev);
5355                 if (s2io_card_up(sp)) {
5356                         DBG_PRINT(ERR_DBG, "%s: Device bring up failed\n",
5357                                   __FUNCTION__);
5358                 }
5359                 if (netif_queue_stopped(dev))
5360                         netif_wake_queue(dev);
5361         } else { /* Device is down */
5362                 XENA_dev_config_t __iomem *bar0 = sp->bar0;
5363                 u64 val64 = new_mtu;
5364
5365                 writeq(vBIT(val64, 2, 14), &bar0->rmac_max_pyld_len);
5366         }
5367
5368         return 0;
5369 }
5370
5371 /**
5372  *  s2io_tasklet - Bottom half of the ISR.
5373  *  @dev_adr : address of the device structure in dma_addr_t format.
5374  *  Description:
5375  *  This is the tasklet or the bottom half of the ISR. This is
5376  *  an extension of the ISR which is scheduled by the scheduler to be run
5377  *  when the load on the CPU is low. All low priority tasks of the ISR can
5378  *  be pushed into the tasklet. For now the tasklet is used only to
5379  *  replenish the Rx buffers in the Rx buffer descriptors.
5380  *  Return value:
5381  *  void.
5382  */
5383
5384 static void s2io_tasklet(unsigned long dev_addr)
5385 {
5386         struct net_device *dev = (struct net_device *) dev_addr;
5387         nic_t *sp = dev->priv;
5388         int i, ret;
5389         mac_info_t *mac_control;
5390         struct config_param *config;
5391
5392         mac_control = &sp->mac_control;
5393         config = &sp->config;
5394
5395         if (!TASKLET_IN_USE) {
5396                 for (i = 0; i < config->rx_ring_num; i++) {
5397                         ret = fill_rx_buffers(sp, i);
5398                         if (ret == -ENOMEM) {
5399                                 DBG_PRINT(ERR_DBG, "%s: Out of ",
5400                                           dev->name);
5401                                 DBG_PRINT(ERR_DBG, "memory in tasklet\n");
5402                                 break;
5403                         } else if (ret == -EFILL) {
5404                                 DBG_PRINT(ERR_DBG,
5405                                           "%s: Rx Ring %d is full\n",
5406                                           dev->name, i);
5407                                 break;
5408                         }
5409                 }
5410                 clear_bit(0, (&sp->tasklet_status));
5411         }
5412 }
5413
5414 /**
5415  * s2io_set_link - Set the LInk status
5416  * @data: long pointer to device private structue
5417  * Description: Sets the link status for the adapter
5418  */
5419
5420 static void s2io_set_link(unsigned long data)
5421 {
5422         nic_t *nic = (nic_t *) data;
5423         struct net_device *dev = nic->dev;
5424         XENA_dev_config_t __iomem *bar0 = nic->bar0;
5425         register u64 val64;
5426         u16 subid;
5427
5428         if (test_and_set_bit(0, &(nic->link_state))) {
5429                 /* The card is being reset, no point doing anything */
5430                 return;
5431         }
5432
5433         subid = nic->pdev->subsystem_device;
5434         if (s2io_link_fault_indication(nic) == MAC_RMAC_ERR_TIMER) {
5435                 /*
5436                  * Allow a small delay for the NICs self initiated
5437                  * cleanup to complete.
5438                  */
5439                 msleep(100);
5440         }
5441
5442         val64 = readq(&bar0->adapter_status);
5443         if (verify_xena_quiescence(nic, val64, nic->device_enabled_once)) {
5444                 if (LINK_IS_UP(val64)) {
5445                         val64 = readq(&bar0->adapter_control);
5446                         val64 |= ADAPTER_CNTL_EN;
5447                         writeq(val64, &bar0->adapter_control);
5448                         if (CARDS_WITH_FAULTY_LINK_INDICATORS(nic->device_type,
5449                                                              subid)) {
5450                                 val64 = readq(&bar0->gpio_control);
5451                                 val64 |= GPIO_CTRL_GPIO_0;
5452                                 writeq(val64, &bar0->gpio_control);
5453                                 val64 = readq(&bar0->gpio_control);
5454                         } else {
5455                                 val64 |= ADAPTER_LED_ON;
5456                                 writeq(val64, &bar0->adapter_control);
5457                         }
5458                         if (s2io_link_fault_indication(nic) ==
5459                                                 MAC_RMAC_ERR_TIMER) {
5460                                 val64 = readq(&bar0->adapter_status);
5461                                 if (!LINK_IS_UP(val64)) {
5462                                         DBG_PRINT(ERR_DBG, "%s:", dev->name);
5463                                         DBG_PRINT(ERR_DBG, " Link down");
5464                                         DBG_PRINT(ERR_DBG, "after ");
5465                                         DBG_PRINT(ERR_DBG, "enabling ");
5466                                         DBG_PRINT(ERR_DBG, "device \n");
5467                                 }
5468                         }
5469                         if (nic->device_enabled_once == FALSE) {
5470                                 nic->device_enabled_once = TRUE;
5471                         }
5472                         s2io_link(nic, LINK_UP);
5473                 } else {
5474                         if (CARDS_WITH_FAULTY_LINK_INDICATORS(nic->device_type,
5475                                                               subid)) {
5476                                 val64 = readq(&bar0->gpio_control);
5477                                 val64 &= ~GPIO_CTRL_GPIO_0;
5478                                 writeq(val64, &bar0->gpio_control);
5479                                 val64 = readq(&bar0->gpio_control);
5480                         }
5481                         s2io_link(nic, LINK_DOWN);
5482                 }
5483         } else {                /* NIC is not Quiescent. */
5484                 DBG_PRINT(ERR_DBG, "%s: Error: ", dev->name);
5485                 DBG_PRINT(ERR_DBG, "device is not Quiescent\n");
5486                 netif_stop_queue(dev);
5487         }
5488         clear_bit(0, &(nic->link_state));
5489 }
5490
5491 static void s2io_card_down(nic_t * sp)
5492 {
5493         int cnt = 0;
5494         XENA_dev_config_t __iomem *bar0 = sp->bar0;
5495         unsigned long flags;
5496         register u64 val64 = 0;
5497
5498         del_timer_sync(&sp->alarm_timer);
5499         /* If s2io_set_link task is executing, wait till it completes. */
5500         while (test_and_set_bit(0, &(sp->link_state))) {
5501                 msleep(50);
5502         }
5503         atomic_set(&sp->card_state, CARD_DOWN);
5504
5505         /* disable Tx and Rx traffic on the NIC */
5506         stop_nic(sp);
5507
5508         /* Kill tasklet. */
5509         tasklet_kill(&sp->task);
5510
5511         /* Check if the device is Quiescent and then Reset the NIC */
5512         do {
5513                 val64 = readq(&bar0->adapter_status);
5514                 if (verify_xena_quiescence(sp, val64, sp->device_enabled_once)) {
5515                         break;
5516                 }
5517
5518                 msleep(50);
5519                 cnt++;
5520                 if (cnt == 10) {
5521                         DBG_PRINT(ERR_DBG,
5522                                   "s2io_close:Device not Quiescent ");
5523                         DBG_PRINT(ERR_DBG, "adaper status reads 0x%llx\n",
5524                                   (unsigned long long) val64);
5525                         break;
5526                 }
5527         } while (1);
5528         s2io_reset(sp);
5529
5530         /* Waiting till all Interrupt handlers are complete */
5531         cnt = 0;
5532         do {
5533                 msleep(10);
5534                 if (!atomic_read(&sp->isr_cnt))
5535                         break;
5536                 cnt++;
5537         } while(cnt < 5);
5538
5539         spin_lock_irqsave(&sp->tx_lock, flags);
5540         /* Free all Tx buffers */
5541         free_tx_buffers(sp);
5542         spin_unlock_irqrestore(&sp->tx_lock, flags);
5543
5544         /* Free all Rx buffers */
5545         spin_lock_irqsave(&sp->rx_lock, flags);
5546         free_rx_buffers(sp);
5547         spin_unlock_irqrestore(&sp->rx_lock, flags);
5548
5549         clear_bit(0, &(sp->link_state));
5550 }
5551
5552 static int s2io_card_up(nic_t * sp)
5553 {
5554         int i, ret = 0;
5555         mac_info_t *mac_control;
5556         struct config_param *config;
5557         struct net_device *dev = (struct net_device *) sp->dev;
5558
5559         /* Initialize the H/W I/O registers */
5560         if (init_nic(sp) != 0) {
5561                 DBG_PRINT(ERR_DBG, "%s: H/W initialization failed\n",
5562                           dev->name);
5563                 return -ENODEV;
5564         }
5565
5566         if (sp->intr_type == MSI)
5567                 ret = s2io_enable_msi(sp);
5568         else if (sp->intr_type == MSI_X)
5569                 ret = s2io_enable_msi_x(sp);
5570         if (ret) {
5571                 DBG_PRINT(ERR_DBG, "%s: Defaulting to INTA\n", dev->name);
5572                 sp->intr_type = INTA;
5573         }
5574
5575         /*
5576          * Initializing the Rx buffers. For now we are considering only 1
5577          * Rx ring and initializing buffers into 30 Rx blocks
5578          */
5579         mac_control = &sp->mac_control;
5580         config = &sp->config;
5581
5582         for (i = 0; i < config->rx_ring_num; i++) {
5583                 if ((ret = fill_rx_buffers(sp, i))) {
5584                         DBG_PRINT(ERR_DBG, "%s: Out of memory in Open\n",
5585                                   dev->name);
5586                         s2io_reset(sp);
5587                         free_rx_buffers(sp);
5588                         return -ENOMEM;
5589                 }
5590                 DBG_PRINT(INFO_DBG, "Buf in ring:%d is %d:\n", i,
5591                           atomic_read(&sp->rx_bufs_left[i]));
5592         }
5593
5594         /* Setting its receive mode */
5595         s2io_set_multicast(dev);
5596
5597         if (sp->lro) {
5598                 /* Initialize max aggregatable pkts based on MTU */
5599                 sp->lro_max_aggr_per_sess = ((1<<16) - 1) / dev->mtu;
5600                 /* Check if we can use(if specified) user provided value */
5601                 if (lro_max_pkts < sp->lro_max_aggr_per_sess)
5602                         sp->lro_max_aggr_per_sess = lro_max_pkts;
5603         }
5604
5605         /* Enable tasklet for the device */
5606         tasklet_init(&sp->task, s2io_tasklet, (unsigned long) dev);
5607
5608         /* Enable Rx Traffic and interrupts on the NIC */
5609         if (start_nic(sp)) {
5610                 DBG_PRINT(ERR_DBG, "%s: Starting NIC failed\n", dev->name);
5611                 tasklet_kill(&sp->task);
5612                 s2io_reset(sp);
5613                 free_irq(dev->irq, dev);
5614                 free_rx_buffers(sp);
5615                 return -ENODEV;
5616         }
5617
5618         S2IO_TIMER_CONF(sp->alarm_timer, s2io_alarm_handle, sp, (HZ/2));
5619
5620         atomic_set(&sp->card_state, CARD_UP);
5621         return 0;
5622 }
5623
5624 /**
5625  * s2io_restart_nic - Resets the NIC.
5626  * @data : long pointer to the device private structure
5627  * Description:
5628  * This function is scheduled to be run by the s2io_tx_watchdog
5629  * function after 0.5 secs to reset the NIC. The idea is to reduce
5630  * the run time of the watch dog routine which is run holding a
5631  * spin lock.
5632  */
5633
5634 static void s2io_restart_nic(unsigned long data)
5635 {
5636         struct net_device *dev = (struct net_device *) data;
5637         nic_t *sp = dev->priv;
5638
5639         s2io_card_down(sp);
5640         if (s2io_card_up(sp)) {
5641                 DBG_PRINT(ERR_DBG, "%s: Device bring up failed\n",
5642                           dev->name);
5643         }
5644         netif_wake_queue(dev);
5645         DBG_PRINT(ERR_DBG, "%s: was reset by Tx watchdog timer\n",
5646                   dev->name);
5647
5648 }
5649
5650 /**
5651  *  s2io_tx_watchdog - Watchdog for transmit side.
5652  *  @dev : Pointer to net device structure
5653  *  Description:
5654  *  This function is triggered if the Tx Queue is stopped
5655  *  for a pre-defined amount of time when the Interface is still up.
5656  *  If the Interface is jammed in such a situation, the hardware is
5657  *  reset (by s2io_close) and restarted again (by s2io_open) to
5658  *  overcome any problem that might have been caused in the hardware.
5659  *  Return value:
5660  *  void
5661  */
5662
5663 static void s2io_tx_watchdog(struct net_device *dev)
5664 {
5665         nic_t *sp = dev->priv;
5666
5667         if (netif_carrier_ok(dev)) {
5668                 schedule_work(&sp->rst_timer_task);
5669         }
5670 }
5671
5672 /**
5673  *   rx_osm_handler - To perform some OS related operations on SKB.
5674  *   @sp: private member of the device structure,pointer to s2io_nic structure.
5675  *   @skb : the socket buffer pointer.
5676  *   @len : length of the packet
5677  *   @cksum : FCS checksum of the frame.
5678  *   @ring_no : the ring from which this RxD was extracted.
5679  *   Description:
5680  *   This function is called by the Tx interrupt serivce routine to perform
5681  *   some OS related operations on the SKB before passing it to the upper
5682  *   layers. It mainly checks if the checksum is OK, if so adds it to the
5683  *   SKBs cksum variable, increments the Rx packet count and passes the SKB
5684  *   to the upper layer. If the checksum is wrong, it increments the Rx
5685  *   packet error count, frees the SKB and returns error.
5686  *   Return value:
5687  *   SUCCESS on success and -1 on failure.
5688  */
5689 static int rx_osm_handler(ring_info_t *ring_data, RxD_t * rxdp)
5690 {
5691         nic_t *sp = ring_data->nic;
5692         struct net_device *dev = (struct net_device *) sp->dev;
5693         struct sk_buff *skb = (struct sk_buff *)
5694                 ((unsigned long) rxdp->Host_Control);
5695         int ring_no = ring_data->ring_no;
5696         u16 l3_csum, l4_csum;
5697         lro_t *lro;
5698
5699         skb->dev = dev;
5700         if (rxdp->Control_1 & RXD_T_CODE) {
5701                 unsigned long long err = rxdp->Control_1 & RXD_T_CODE;
5702                 DBG_PRINT(ERR_DBG, "%s: Rx error Value: 0x%llx\n",
5703                           dev->name, err);
5704                 dev_kfree_skb(skb);
5705                 sp->stats.rx_crc_errors++;
5706                 atomic_dec(&sp->rx_bufs_left[ring_no]);
5707                 rxdp->Host_Control = 0;
5708                 return 0;
5709         }
5710
5711         /* Updating statistics */
5712         rxdp->Host_Control = 0;
5713         sp->rx_pkt_count++;
5714         sp->stats.rx_packets++;
5715         if (sp->rxd_mode == RXD_MODE_1) {
5716                 int len = RXD_GET_BUFFER0_SIZE_1(rxdp->Control_2);
5717
5718                 sp->stats.rx_bytes += len;
5719                 skb_put(skb, len);
5720
5721         } else if (sp->rxd_mode >= RXD_MODE_3A) {
5722                 int get_block = ring_data->rx_curr_get_info.block_index;
5723                 int get_off = ring_data->rx_curr_get_info.offset;
5724                 int buf0_len = RXD_GET_BUFFER0_SIZE_3(rxdp->Control_2);
5725                 int buf2_len = RXD_GET_BUFFER2_SIZE_3(rxdp->Control_2);
5726                 unsigned char *buff = skb_push(skb, buf0_len);
5727
5728                 buffAdd_t *ba = &ring_data->ba[get_block][get_off];
5729                 sp->stats.rx_bytes += buf0_len + buf2_len;
5730                 memcpy(buff, ba->ba_0, buf0_len);
5731
5732                 if (sp->rxd_mode == RXD_MODE_3A) {
5733                         int buf1_len = RXD_GET_BUFFER1_SIZE_3(rxdp->Control_2);
5734
5735                         skb_put(skb, buf1_len);
5736                         skb->len += buf2_len;
5737                         skb->data_len += buf2_len;
5738                         skb->truesize += buf2_len;
5739                         skb_put(skb_shinfo(skb)->frag_list, buf2_len);
5740                         sp->stats.rx_bytes += buf1_len;
5741
5742                 } else
5743                         skb_put(skb, buf2_len);
5744         }
5745
5746         if ((rxdp->Control_1 & TCP_OR_UDP_FRAME) && ((!sp->lro) ||
5747             (sp->lro && (!(rxdp->Control_1 & RXD_FRAME_IP_FRAG)))) &&
5748             (sp->rx_csum)) {
5749                 l3_csum = RXD_GET_L3_CKSUM(rxdp->Control_1);
5750                 l4_csum = RXD_GET_L4_CKSUM(rxdp->Control_1);
5751                 if ((l3_csum == L3_CKSUM_OK) && (l4_csum == L4_CKSUM_OK)) {
5752                         /*
5753                          * NIC verifies if the Checksum of the received
5754                          * frame is Ok or not and accordingly returns
5755                          * a flag in the RxD.
5756                          */
5757                         skb->ip_summed = CHECKSUM_UNNECESSARY;
5758                         if (sp->lro) {
5759                                 u32 tcp_len;
5760                                 u8 *tcp;
5761                                 int ret = 0;
5762
5763                                 ret = s2io_club_tcp_session(skb->data, &tcp,
5764                                                 &tcp_len, &lro, rxdp, sp);
5765                                 switch (ret) {
5766                                         case 3: /* Begin anew */
5767                                                 lro->parent = skb;
5768                                                 goto aggregate;
5769                                         case 1: /* Aggregate */
5770                                         {
5771                                                 lro_append_pkt(sp, lro,
5772                                                         skb, tcp_len);
5773                                                 goto aggregate;
5774                                         }
5775                                         case 4: /* Flush session */
5776                                         {
5777                                                 lro_append_pkt(sp, lro,
5778                                                         skb, tcp_len);
5779                                                 queue_rx_frame(lro->parent);
5780                                                 clear_lro_session(lro);
5781                                                 sp->mac_control.stats_info->
5782                                                     sw_stat.flush_max_pkts++;
5783                                                 goto aggregate;
5784                                         }
5785                                         case 2: /* Flush both */
5786                                                 lro->parent->data_len =
5787                                                         lro->frags_len;
5788                                                 sp->mac_control.stats_info->
5789                                                      sw_stat.sending_both++;
5790                                                 queue_rx_frame(lro->parent);
5791                                                 clear_lro_session(lro);
5792                                                 goto send_up;
5793                                         case 0: /* sessions exceeded */
5794                                         case 5: /*
5795                                                  * First pkt in session not
5796                                                  * L3/L4 aggregatable
5797                                                  */
5798                                                 break;
5799                                         default:
5800                                                 DBG_PRINT(ERR_DBG,
5801                                                         "%s: Samadhana!!\n",
5802                                                          __FUNCTION__);
5803                                                 BUG();
5804                                 }
5805                         }
5806                 } else {
5807                         /*
5808                          * Packet with erroneous checksum, let the
5809                          * upper layers deal with it.
5810                          */
5811                         skb->ip_summed = CHECKSUM_NONE;
5812                 }
5813         } else {
5814                 skb->ip_summed = CHECKSUM_NONE;
5815         }
5816
5817         if (!sp->lro) {
5818                 skb->protocol = eth_type_trans(skb, dev);
5819 #ifdef CONFIG_S2IO_NAPI
5820                 if (sp->vlgrp && RXD_GET_VLAN_TAG(rxdp->Control_2)) {
5821                         /* Queueing the vlan frame to the upper layer */
5822                         vlan_hwaccel_receive_skb(skb, sp->vlgrp,
5823                                 RXD_GET_VLAN_TAG(rxdp->Control_2));
5824                 } else {
5825                         netif_receive_skb(skb);
5826                 }
5827 #else
5828                 if (sp->vlgrp && RXD_GET_VLAN_TAG(rxdp->Control_2)) {
5829                         /* Queueing the vlan frame to the upper layer */
5830                         vlan_hwaccel_rx(skb, sp->vlgrp,
5831                                 RXD_GET_VLAN_TAG(rxdp->Control_2));
5832                 } else {
5833                         netif_rx(skb);
5834                 }
5835 #endif
5836         } else {
5837 send_up:
5838                 queue_rx_frame(skb);
5839         }               
5840         dev->last_rx = jiffies;
5841 aggregate:
5842         atomic_dec(&sp->rx_bufs_left[ring_no]);
5843         return SUCCESS;
5844 }
5845
5846 /**
5847  *  s2io_link - stops/starts the Tx queue.
5848  *  @sp : private member of the device structure, which is a pointer to the
5849  *  s2io_nic structure.
5850  *  @link : inidicates whether link is UP/DOWN.
5851  *  Description:
5852  *  This function stops/starts the Tx queue depending on whether the link
5853  *  status of the NIC is is down or up. This is called by the Alarm
5854  *  interrupt handler whenever a link change interrupt comes up.
5855  *  Return value:
5856  *  void.
5857  */
5858
5859 void s2io_link(nic_t * sp, int link)
5860 {
5861         struct net_device *dev = (struct net_device *) sp->dev;
5862
5863         if (link != sp->last_link_state) {
5864                 if (link == LINK_DOWN) {
5865                         DBG_PRINT(ERR_DBG, "%s: Link down\n", dev->name);
5866                         netif_carrier_off(dev);
5867                 } else {
5868                         DBG_PRINT(ERR_DBG, "%s: Link Up\n", dev->name);
5869                         netif_carrier_on(dev);
5870                 }
5871         }
5872         sp->last_link_state = link;
5873 }
5874
5875 /**
5876  *  get_xena_rev_id - to identify revision ID of xena.
5877  *  @pdev : PCI Dev structure
5878  *  Description:
5879  *  Function to identify the Revision ID of xena.
5880  *  Return value:
5881  *  returns the revision ID of the device.
5882  */
5883
5884 int get_xena_rev_id(struct pci_dev *pdev)
5885 {
5886         u8 id = 0;
5887         int ret;
5888         ret = pci_read_config_byte(pdev, PCI_REVISION_ID, (u8 *) & id);
5889         return id;
5890 }
5891
5892 /**
5893  *  s2io_init_pci -Initialization of PCI and PCI-X configuration registers .
5894  *  @sp : private member of the device structure, which is a pointer to the
5895  *  s2io_nic structure.
5896  *  Description:
5897  *  This function initializes a few of the PCI and PCI-X configuration registers
5898  *  with recommended values.
5899  *  Return value:
5900  *  void
5901  */
5902
5903 static void s2io_init_pci(nic_t * sp)
5904 {
5905         u16 pci_cmd = 0, pcix_cmd = 0;
5906
5907         /* Enable Data Parity Error Recovery in PCI-X command register. */
5908         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
5909                              &(pcix_cmd));
5910         pci_write_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
5911                               (pcix_cmd | 1));
5912         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
5913                              &(pcix_cmd));
5914
5915         /* Set the PErr Response bit in PCI command register. */
5916         pci_read_config_word(sp->pdev, PCI_COMMAND, &pci_cmd);
5917         pci_write_config_word(sp->pdev, PCI_COMMAND,
5918                               (pci_cmd | PCI_COMMAND_PARITY));
5919         pci_read_config_word(sp->pdev, PCI_COMMAND, &pci_cmd);
5920
5921         /* Forcibly disabling relaxed ordering capability of the card. */
5922         pcix_cmd &= 0xfffd;
5923         pci_write_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
5924                               pcix_cmd);
5925         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
5926                              &(pcix_cmd));
5927 }
5928
5929 MODULE_AUTHOR("Raghavendra Koushik <raghavendra.koushik@neterion.com>");
5930 MODULE_LICENSE("GPL");
5931 MODULE_VERSION(DRV_VERSION);
5932
5933 module_param(tx_fifo_num, int, 0);
5934 module_param(rx_ring_num, int, 0);
5935 module_param(rx_ring_mode, int, 0);
5936 module_param_array(tx_fifo_len, uint, NULL, 0);
5937 module_param_array(rx_ring_sz, uint, NULL, 0);
5938 module_param_array(rts_frm_len, uint, NULL, 0);
5939 module_param(use_continuous_tx_intrs, int, 1);
5940 module_param(rmac_pause_time, int, 0);
5941 module_param(mc_pause_threshold_q0q3, int, 0);
5942 module_param(mc_pause_threshold_q4q7, int, 0);
5943 module_param(shared_splits, int, 0);
5944 module_param(tmac_util_period, int, 0);
5945 module_param(rmac_util_period, int, 0);
5946 module_param(bimodal, bool, 0);
5947 module_param(l3l4hdr_size, int , 0);
5948 #ifndef CONFIG_S2IO_NAPI
5949 module_param(indicate_max_pkts, int, 0);
5950 #endif
5951 module_param(rxsync_frequency, int, 0);
5952 module_param(intr_type, int, 0);
5953 module_param(lro, int, 0);
5954 module_param(lro_max_pkts, int, 0);
5955
5956 /**
5957  *  s2io_init_nic - Initialization of the adapter .
5958  *  @pdev : structure containing the PCI related information of the device.
5959  *  @pre: List of PCI devices supported by the driver listed in s2io_tbl.
5960  *  Description:
5961  *  The function initializes an adapter identified by the pci_dec structure.
5962  *  All OS related initialization including memory and device structure and
5963  *  initlaization of the device private variable is done. Also the swapper
5964  *  control register is initialized to enable read and write into the I/O
5965  *  registers of the device.
5966  *  Return value:
5967  *  returns 0 on success and negative on failure.
5968  */
5969
5970 static int __devinit
5971 s2io_init_nic(struct pci_dev *pdev, const struct pci_device_id *pre)
5972 {
5973         nic_t *sp;
5974         struct net_device *dev;
5975         int i, j, ret;
5976         int dma_flag = FALSE;
5977         u32 mac_up, mac_down;
5978         u64 val64 = 0, tmp64 = 0;
5979         XENA_dev_config_t __iomem *bar0 = NULL;
5980         u16 subid;
5981         mac_info_t *mac_control;
5982         struct config_param *config;
5983         int mode;
5984         u8 dev_intr_type = intr_type;
5985
5986 #ifdef CONFIG_S2IO_NAPI
5987         if (dev_intr_type != INTA) {
5988                 DBG_PRINT(ERR_DBG, "NAPI cannot be enabled when MSI/MSI-X \
5989 is enabled. Defaulting to INTA\n");
5990                 dev_intr_type = INTA;
5991         }
5992         else
5993                 DBG_PRINT(ERR_DBG, "NAPI support has been enabled\n");
5994 #endif
5995
5996         if ((ret = pci_enable_device(pdev))) {
5997                 DBG_PRINT(ERR_DBG,
5998                           "s2io_init_nic: pci_enable_device failed\n");
5999                 return ret;
6000         }
6001
6002         if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
6003                 DBG_PRINT(INIT_DBG, "s2io_init_nic: Using 64bit DMA\n");
6004                 dma_flag = TRUE;
6005                 if (pci_set_consistent_dma_mask
6006                     (pdev, DMA_64BIT_MASK)) {
6007                         DBG_PRINT(ERR_DBG,
6008                                   "Unable to obtain 64bit DMA for \
6009                                         consistent allocations\n");
6010                         pci_disable_device(pdev);
6011                         return -ENOMEM;
6012                 }
6013         } else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
6014                 DBG_PRINT(INIT_DBG, "s2io_init_nic: Using 32bit DMA\n");
6015         } else {
6016                 pci_disable_device(pdev);
6017                 return -ENOMEM;
6018         }
6019
6020         if ((dev_intr_type == MSI_X) && 
6021                         ((pdev->device != PCI_DEVICE_ID_HERC_WIN) &&
6022                         (pdev->device != PCI_DEVICE_ID_HERC_UNI))) {
6023                 DBG_PRINT(ERR_DBG, "Xframe I does not support MSI_X. \
6024 Defaulting to INTA\n");
6025                 dev_intr_type = INTA;
6026         }
6027         if (dev_intr_type != MSI_X) {
6028                 if (pci_request_regions(pdev, s2io_driver_name)) {
6029                         DBG_PRINT(ERR_DBG, "Request Regions failed\n"),
6030                             pci_disable_device(pdev);
6031                         return -ENODEV;
6032                 }
6033         }
6034         else {
6035                 if (!(request_mem_region(pci_resource_start(pdev, 0),
6036                          pci_resource_len(pdev, 0), s2io_driver_name))) {
6037                         DBG_PRINT(ERR_DBG, "bar0 Request Regions failed\n");
6038                         pci_disable_device(pdev);
6039                         return -ENODEV;
6040                 }
6041                 if (!(request_mem_region(pci_resource_start(pdev, 2),
6042                          pci_resource_len(pdev, 2), s2io_driver_name))) {
6043                         DBG_PRINT(ERR_DBG, "bar1 Request Regions failed\n");
6044                         release_mem_region(pci_resource_start(pdev, 0),
6045                                    pci_resource_len(pdev, 0));
6046                         pci_disable_device(pdev);
6047                         return -ENODEV;
6048                 }
6049         }
6050
6051         dev = alloc_etherdev(sizeof(nic_t));
6052         if (dev == NULL) {
6053                 DBG_PRINT(ERR_DBG, "Device allocation failed\n");
6054                 pci_disable_device(pdev);
6055                 pci_release_regions(pdev);
6056                 return -ENODEV;
6057         }
6058
6059         pci_set_master(pdev);
6060         pci_set_drvdata(pdev, dev);
6061         SET_MODULE_OWNER(dev);
6062         SET_NETDEV_DEV(dev, &pdev->dev);
6063
6064         /*  Private member variable initialized to s2io NIC structure */
6065         sp = dev->priv;
6066         memset(sp, 0, sizeof(nic_t));
6067         sp->dev = dev;
6068         sp->pdev = pdev;
6069         sp->high_dma_flag = dma_flag;
6070         sp->device_enabled_once = FALSE;
6071         if (rx_ring_mode == 1)
6072                 sp->rxd_mode = RXD_MODE_1;
6073         if (rx_ring_mode == 2)
6074                 sp->rxd_mode = RXD_MODE_3B;
6075         if (rx_ring_mode == 3)
6076                 sp->rxd_mode = RXD_MODE_3A;
6077
6078         sp->intr_type = dev_intr_type;
6079
6080         if ((pdev->device == PCI_DEVICE_ID_HERC_WIN) ||
6081                 (pdev->device == PCI_DEVICE_ID_HERC_UNI))
6082                 sp->device_type = XFRAME_II_DEVICE;
6083         else
6084                 sp->device_type = XFRAME_I_DEVICE;
6085
6086         sp->lro = lro;
6087                 
6088         /* Initialize some PCI/PCI-X fields of the NIC. */
6089         s2io_init_pci(sp);
6090
6091         /*
6092          * Setting the device configuration parameters.
6093          * Most of these parameters can be specified by the user during
6094          * module insertion as they are module loadable parameters. If
6095          * these parameters are not not specified during load time, they
6096          * are initialized with default values.
6097          */
6098         mac_control = &sp->mac_control;
6099         config = &sp->config;
6100
6101         /* Tx side parameters. */
6102         if (tx_fifo_len[0] == 0)
6103                 tx_fifo_len[0] = DEFAULT_FIFO_LEN; /* Default value. */
6104         config->tx_fifo_num = tx_fifo_num;
6105         for (i = 0; i < MAX_TX_FIFOS; i++) {
6106                 config->tx_cfg[i].fifo_len = tx_fifo_len[i];
6107                 config->tx_cfg[i].fifo_priority = i;
6108         }
6109
6110         /* mapping the QoS priority to the configured fifos */
6111         for (i = 0; i < MAX_TX_FIFOS; i++)
6112                 config->fifo_mapping[i] = fifo_map[config->tx_fifo_num][i];
6113
6114         config->tx_intr_type = TXD_INT_TYPE_UTILZ;
6115         for (i = 0; i < config->tx_fifo_num; i++) {
6116                 config->tx_cfg[i].f_no_snoop =
6117                     (NO_SNOOP_TXD | NO_SNOOP_TXD_BUFFER);
6118                 if (config->tx_cfg[i].fifo_len < 65) {
6119                         config->tx_intr_type = TXD_INT_TYPE_PER_LIST;
6120                         break;
6121                 }
6122         }
6123         /* + 2 because one Txd for skb->data and one Txd for UFO */
6124         config->max_txds = MAX_SKB_FRAGS + 2;
6125
6126         /* Rx side parameters. */
6127         if (rx_ring_sz[0] == 0)
6128                 rx_ring_sz[0] = SMALL_BLK_CNT; /* Default value. */
6129         config->rx_ring_num = rx_ring_num;
6130         for (i = 0; i < MAX_RX_RINGS; i++) {
6131                 config->rx_cfg[i].num_rxd = rx_ring_sz[i] *
6132                     (rxd_count[sp->rxd_mode] + 1);
6133                 config->rx_cfg[i].ring_priority = i;
6134         }
6135
6136         for (i = 0; i < rx_ring_num; i++) {
6137                 config->rx_cfg[i].ring_org = RING_ORG_BUFF1;
6138                 config->rx_cfg[i].f_no_snoop =
6139                     (NO_SNOOP_RXD | NO_SNOOP_RXD_BUFFER);
6140         }
6141
6142         /*  Setting Mac Control parameters */
6143         mac_control->rmac_pause_time = rmac_pause_time;
6144         mac_control->mc_pause_threshold_q0q3 = mc_pause_threshold_q0q3;
6145         mac_control->mc_pause_threshold_q4q7 = mc_pause_threshold_q4q7;
6146
6147
6148         /* Initialize Ring buffer parameters. */
6149         for (i = 0; i < config->rx_ring_num; i++)
6150                 atomic_set(&sp->rx_bufs_left[i], 0);
6151
6152         /* Initialize the number of ISRs currently running */
6153         atomic_set(&sp->isr_cnt, 0);
6154
6155         /*  initialize the shared memory used by the NIC and the host */
6156         if (init_shared_mem(sp)) {
6157                 DBG_PRINT(ERR_DBG, "%s: Memory allocation failed\n",
6158                           __FUNCTION__);
6159                 ret = -ENOMEM;
6160                 goto mem_alloc_failed;
6161         }
6162
6163         sp->bar0 = ioremap(pci_resource_start(pdev, 0),
6164                                      pci_resource_len(pdev, 0));
6165         if (!sp->bar0) {
6166                 DBG_PRINT(ERR_DBG, "%s: S2IO: cannot remap io mem1\n",
6167                           dev->name);
6168                 ret = -ENOMEM;
6169                 goto bar0_remap_failed;
6170         }
6171
6172         sp->bar1 = ioremap(pci_resource_start(pdev, 2),
6173                                      pci_resource_len(pdev, 2));
6174         if (!sp->bar1) {
6175                 DBG_PRINT(ERR_DBG, "%s: S2IO: cannot remap io mem2\n",
6176                           dev->name);
6177                 ret = -ENOMEM;
6178                 goto bar1_remap_failed;
6179         }
6180
6181         dev->irq = pdev->irq;
6182         dev->base_addr = (unsigned long) sp->bar0;
6183
6184         /* Initializing the BAR1 address as the start of the FIFO pointer. */
6185         for (j = 0; j < MAX_TX_FIFOS; j++) {
6186                 mac_control->tx_FIFO_start[j] = (TxFIFO_element_t __iomem *)
6187                     (sp->bar1 + (j * 0x00020000));
6188         }
6189
6190         /*  Driver entry points */
6191         dev->open = &s2io_open;
6192         dev->stop = &s2io_close;
6193         dev->hard_start_xmit = &s2io_xmit;
6194         dev->get_stats = &s2io_get_stats;
6195         dev->set_multicast_list = &s2io_set_multicast;
6196         dev->do_ioctl = &s2io_ioctl;
6197         dev->change_mtu = &s2io_change_mtu;
6198         SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
6199         dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6200         dev->vlan_rx_register = s2io_vlan_rx_register;
6201         dev->vlan_rx_kill_vid = (void *)s2io_vlan_rx_kill_vid;
6202
6203         /*
6204          * will use eth_mac_addr() for  dev->set_mac_address
6205          * mac address will be set every time dev->open() is called
6206          */
6207 #if defined(CONFIG_S2IO_NAPI)
6208         dev->poll = s2io_poll;
6209         dev->weight = 32;
6210 #endif
6211
6212         dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
6213         if (sp->high_dma_flag == TRUE)
6214                 dev->features |= NETIF_F_HIGHDMA;
6215 #ifdef NETIF_F_TSO
6216         dev->features |= NETIF_F_TSO;
6217 #endif
6218         if (sp->device_type & XFRAME_II_DEVICE) {
6219                 dev->features |= NETIF_F_UFO;
6220                 dev->features |= NETIF_F_HW_CSUM;
6221         }
6222
6223         dev->tx_timeout = &s2io_tx_watchdog;
6224         dev->watchdog_timeo = WATCH_DOG_TIMEOUT;
6225         INIT_WORK(&sp->rst_timer_task,
6226                   (void (*)(void *)) s2io_restart_nic, dev);
6227         INIT_WORK(&sp->set_link_task,
6228                   (void (*)(void *)) s2io_set_link, sp);
6229
6230         pci_save_state(sp->pdev);
6231
6232         /* Setting swapper control on the NIC, for proper reset operation */
6233         if (s2io_set_swapper(sp)) {
6234                 DBG_PRINT(ERR_DBG, "%s:swapper settings are wrong\n",
6235                           dev->name);
6236                 ret = -EAGAIN;
6237                 goto set_swap_failed;
6238         }
6239
6240         /* Verify if the Herc works on the slot its placed into */
6241         if (sp->device_type & XFRAME_II_DEVICE) {
6242                 mode = s2io_verify_pci_mode(sp);
6243                 if (mode < 0) {
6244                         DBG_PRINT(ERR_DBG, "%s: ", __FUNCTION__);
6245                         DBG_PRINT(ERR_DBG, " Unsupported PCI bus mode\n");
6246                         ret = -EBADSLT;
6247                         goto set_swap_failed;
6248                 }
6249         }
6250
6251         /* Not needed for Herc */
6252         if (sp->device_type & XFRAME_I_DEVICE) {
6253                 /*
6254                  * Fix for all "FFs" MAC address problems observed on
6255                  * Alpha platforms
6256                  */
6257                 fix_mac_address(sp);
6258                 s2io_reset(sp);
6259         }
6260
6261         /*
6262          * MAC address initialization.
6263          * For now only one mac address will be read and used.
6264          */
6265         bar0 = sp->bar0;
6266         val64 = RMAC_ADDR_CMD_MEM_RD | RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
6267             RMAC_ADDR_CMD_MEM_OFFSET(0 + MAC_MAC_ADDR_START_OFFSET);
6268         writeq(val64, &bar0->rmac_addr_cmd_mem);
6269         wait_for_cmd_complete(sp);
6270
6271         tmp64 = readq(&bar0->rmac_addr_data0_mem);
6272         mac_down = (u32) tmp64;
6273         mac_up = (u32) (tmp64 >> 32);
6274
6275         memset(sp->def_mac_addr[0].mac_addr, 0, sizeof(ETH_ALEN));
6276
6277         sp->def_mac_addr[0].mac_addr[3] = (u8) (mac_up);
6278         sp->def_mac_addr[0].mac_addr[2] = (u8) (mac_up >> 8);
6279         sp->def_mac_addr[0].mac_addr[1] = (u8) (mac_up >> 16);
6280         sp->def_mac_addr[0].mac_addr[0] = (u8) (mac_up >> 24);
6281         sp->def_mac_addr[0].mac_addr[5] = (u8) (mac_down >> 16);
6282         sp->def_mac_addr[0].mac_addr[4] = (u8) (mac_down >> 24);
6283
6284         /*  Set the factory defined MAC address initially   */
6285         dev->addr_len = ETH_ALEN;
6286         memcpy(dev->dev_addr, sp->def_mac_addr, ETH_ALEN);
6287
6288         /*
6289          * Initialize the tasklet status and link state flags
6290          * and the card state parameter
6291          */
6292         atomic_set(&(sp->card_state), 0);
6293         sp->tasklet_status = 0;
6294         sp->link_state = 0;
6295
6296         /* Initialize spinlocks */
6297         spin_lock_init(&sp->tx_lock);
6298 #ifndef CONFIG_S2IO_NAPI
6299         spin_lock_init(&sp->put_lock);
6300 #endif
6301         spin_lock_init(&sp->rx_lock);
6302
6303         /*
6304          * SXE-002: Configure link and activity LED to init state
6305          * on driver load.
6306          */
6307         subid = sp->pdev->subsystem_device;
6308         if ((subid & 0xFF) >= 0x07) {
6309                 val64 = readq(&bar0->gpio_control);
6310                 val64 |= 0x0000800000000000ULL;
6311                 writeq(val64, &bar0->gpio_control);
6312                 val64 = 0x0411040400000000ULL;
6313                 writeq(val64, (void __iomem *) bar0 + 0x2700);
6314                 val64 = readq(&bar0->gpio_control);
6315         }
6316
6317         sp->rx_csum = 1;        /* Rx chksum verify enabled by default */
6318
6319         if (register_netdev(dev)) {
6320                 DBG_PRINT(ERR_DBG, "Device registration failed\n");
6321                 ret = -ENODEV;
6322                 goto register_failed;
6323         }
6324
6325         if (sp->device_type & XFRAME_II_DEVICE) {
6326                 DBG_PRINT(ERR_DBG, "%s: Neterion Xframe II 10GbE adapter ",
6327                           dev->name);
6328                 DBG_PRINT(ERR_DBG, "(rev %d), Version %s",
6329                                 get_xena_rev_id(sp->pdev),
6330                                 s2io_driver_version);
6331                 switch(sp->intr_type) {
6332                         case INTA:
6333                                 DBG_PRINT(ERR_DBG, ", Intr type INTA");
6334                                 break;
6335                         case MSI:
6336                                 DBG_PRINT(ERR_DBG, ", Intr type MSI");
6337                                 break;
6338                         case MSI_X:
6339                                 DBG_PRINT(ERR_DBG, ", Intr type MSI-X");
6340                                 break;
6341                 }
6342
6343                 DBG_PRINT(ERR_DBG, "\nCopyright(c) 2002-2005 Neterion Inc.\n");
6344                 DBG_PRINT(ERR_DBG, "MAC ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n",
6345                           sp->def_mac_addr[0].mac_addr[0],
6346                           sp->def_mac_addr[0].mac_addr[1],
6347                           sp->def_mac_addr[0].mac_addr[2],
6348                           sp->def_mac_addr[0].mac_addr[3],
6349                           sp->def_mac_addr[0].mac_addr[4],
6350                           sp->def_mac_addr[0].mac_addr[5]);
6351                 mode = s2io_print_pci_mode(sp);
6352                 if (mode < 0) {
6353                         DBG_PRINT(ERR_DBG, " Unsupported PCI bus mode ");
6354                         ret = -EBADSLT;
6355                         goto set_swap_failed;
6356                 }
6357         } else {
6358                 DBG_PRINT(ERR_DBG, "%s: Neterion Xframe I 10GbE adapter ",
6359                           dev->name);
6360                 DBG_PRINT(ERR_DBG, "(rev %d), Version %s",
6361                                         get_xena_rev_id(sp->pdev),
6362                                         s2io_driver_version);
6363                 switch(sp->intr_type) {
6364                         case INTA:
6365                                 DBG_PRINT(ERR_DBG, ", Intr type INTA");
6366                                 break;
6367                         case MSI:
6368                                 DBG_PRINT(ERR_DBG, ", Intr type MSI");
6369                                 break;
6370                         case MSI_X:
6371                                 DBG_PRINT(ERR_DBG, ", Intr type MSI-X");
6372                                 break;
6373                 }
6374                 DBG_PRINT(ERR_DBG, "\nCopyright(c) 2002-2005 Neterion Inc.\n");
6375                 DBG_PRINT(ERR_DBG, "MAC ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n",
6376                           sp->def_mac_addr[0].mac_addr[0],
6377                           sp->def_mac_addr[0].mac_addr[1],
6378                           sp->def_mac_addr[0].mac_addr[2],
6379                           sp->def_mac_addr[0].mac_addr[3],
6380                           sp->def_mac_addr[0].mac_addr[4],
6381                           sp->def_mac_addr[0].mac_addr[5]);
6382         }
6383         if (sp->rxd_mode == RXD_MODE_3B)
6384                 DBG_PRINT(ERR_DBG, "%s: 2-Buffer mode support has been "
6385                           "enabled\n",dev->name);
6386         if (sp->rxd_mode == RXD_MODE_3A)
6387                 DBG_PRINT(ERR_DBG, "%s: 3-Buffer mode support has been "
6388                           "enabled\n",dev->name);
6389
6390         if (sp->lro)
6391                 DBG_PRINT(ERR_DBG, "%s: Large receive offload enabled\n",
6392                         dev->name);
6393
6394         /* Initialize device name */
6395         strcpy(sp->name, dev->name);
6396         if (sp->device_type & XFRAME_II_DEVICE)
6397                 strcat(sp->name, ": Neterion Xframe II 10GbE adapter");
6398         else
6399                 strcat(sp->name, ": Neterion Xframe I 10GbE adapter");
6400
6401         /* Initialize bimodal Interrupts */
6402         sp->config.bimodal = bimodal;
6403         if (!(sp->device_type & XFRAME_II_DEVICE) && bimodal) {
6404                 sp->config.bimodal = 0;
6405                 DBG_PRINT(ERR_DBG,"%s:Bimodal intr not supported by Xframe I\n",
6406                         dev->name);
6407         }
6408
6409         /*
6410          * Make Link state as off at this point, when the Link change
6411          * interrupt comes the state will be automatically changed to
6412          * the right state.
6413          */
6414         netif_carrier_off(dev);
6415
6416         return 0;
6417
6418       register_failed:
6419       set_swap_failed:
6420         iounmap(sp->bar1);
6421       bar1_remap_failed:
6422         iounmap(sp->bar0);
6423       bar0_remap_failed:
6424       mem_alloc_failed:
6425         free_shared_mem(sp);
6426         pci_disable_device(pdev);
6427         if (dev_intr_type != MSI_X)
6428                 pci_release_regions(pdev);
6429         else {
6430                 release_mem_region(pci_resource_start(pdev, 0),
6431                         pci_resource_len(pdev, 0));
6432                 release_mem_region(pci_resource_start(pdev, 2),
6433                         pci_resource_len(pdev, 2));
6434         }
6435         pci_set_drvdata(pdev, NULL);
6436         free_netdev(dev);
6437
6438         return ret;
6439 }
6440
6441 /**
6442  * s2io_rem_nic - Free the PCI device
6443  * @pdev: structure containing the PCI related information of the device.
6444  * Description: This function is called by the Pci subsystem to release a
6445  * PCI device and free up all resource held up by the device. This could
6446  * be in response to a Hot plug event or when the driver is to be removed
6447  * from memory.
6448  */
6449
6450 static void __devexit s2io_rem_nic(struct pci_dev *pdev)
6451 {
6452         struct net_device *dev =
6453             (struct net_device *) pci_get_drvdata(pdev);
6454         nic_t *sp;
6455
6456         if (dev == NULL) {
6457                 DBG_PRINT(ERR_DBG, "Driver Data is NULL!!\n");
6458                 return;
6459         }
6460
6461         sp = dev->priv;
6462         unregister_netdev(dev);
6463
6464         free_shared_mem(sp);
6465         iounmap(sp->bar0);
6466         iounmap(sp->bar1);
6467         pci_disable_device(pdev);
6468         if (sp->intr_type != MSI_X)
6469                 pci_release_regions(pdev);
6470         else {
6471                 release_mem_region(pci_resource_start(pdev, 0),
6472                         pci_resource_len(pdev, 0));
6473                 release_mem_region(pci_resource_start(pdev, 2),
6474                         pci_resource_len(pdev, 2));
6475         }
6476         pci_set_drvdata(pdev, NULL);
6477         free_netdev(dev);
6478 }
6479
6480 /**
6481  * s2io_starter - Entry point for the driver
6482  * Description: This function is the entry point for the driver. It verifies
6483  * the module loadable parameters and initializes PCI configuration space.
6484  */
6485
6486 int __init s2io_starter(void)
6487 {
6488         return pci_module_init(&s2io_driver);
6489 }
6490
6491 /**
6492  * s2io_closer - Cleanup routine for the driver
6493  * Description: This function is the cleanup routine for the driver. It unregist * ers the driver.
6494  */
6495
6496 void s2io_closer(void)
6497 {
6498         pci_unregister_driver(&s2io_driver);
6499         DBG_PRINT(INIT_DBG, "cleanup done\n");
6500 }
6501
6502 module_init(s2io_starter);
6503 module_exit(s2io_closer);
6504
6505 static int check_L2_lro_capable(u8 *buffer, struct iphdr **ip, 
6506                 struct tcphdr **tcp, RxD_t *rxdp)
6507 {
6508         int ip_off;
6509         u8 l2_type = (u8)((rxdp->Control_1 >> 37) & 0x7), ip_len;
6510
6511         if (!(rxdp->Control_1 & RXD_FRAME_PROTO_TCP)) {
6512                 DBG_PRINT(INIT_DBG,"%s: Non-TCP frames not supported for LRO\n",
6513                           __FUNCTION__);
6514                 return -1;
6515         }
6516
6517         /* TODO:
6518          * By default the VLAN field in the MAC is stripped by the card, if this
6519          * feature is turned off in rx_pa_cfg register, then the ip_off field
6520          * has to be shifted by a further 2 bytes
6521          */
6522         switch (l2_type) {
6523                 case 0: /* DIX type */
6524                 case 4: /* DIX type with VLAN */
6525                         ip_off = HEADER_ETHERNET_II_802_3_SIZE;
6526                         break;
6527                 /* LLC, SNAP etc are considered non-mergeable */
6528                 default:
6529                         return -1;
6530         }
6531
6532         *ip = (struct iphdr *)((u8 *)buffer + ip_off);
6533         ip_len = (u8)((*ip)->ihl);
6534         ip_len <<= 2;
6535         *tcp = (struct tcphdr *)((unsigned long)*ip + ip_len);
6536
6537         return 0;
6538 }
6539
6540 static int check_for_socket_match(lro_t *lro, struct iphdr *ip,
6541                                   struct tcphdr *tcp)
6542 {
6543         DBG_PRINT(INFO_DBG,"%s: Been here...\n", __FUNCTION__);
6544         if ((lro->iph->saddr != ip->saddr) || (lro->iph->daddr != ip->daddr) ||
6545            (lro->tcph->source != tcp->source) || (lro->tcph->dest != tcp->dest))
6546                 return -1;
6547         return 0;
6548 }
6549
6550 static inline int get_l4_pyld_length(struct iphdr *ip, struct tcphdr *tcp)
6551 {
6552         return(ntohs(ip->tot_len) - (ip->ihl << 2) - (tcp->doff << 2));
6553 }
6554
6555 static void initiate_new_session(lro_t *lro, u8 *l2h,
6556                      struct iphdr *ip, struct tcphdr *tcp, u32 tcp_pyld_len)
6557 {
6558         DBG_PRINT(INFO_DBG,"%s: Been here...\n", __FUNCTION__);
6559         lro->l2h = l2h;
6560         lro->iph = ip;
6561         lro->tcph = tcp;
6562         lro->tcp_next_seq = tcp_pyld_len + ntohl(tcp->seq);
6563         lro->tcp_ack = ntohl(tcp->ack_seq);
6564         lro->sg_num = 1;
6565         lro->total_len = ntohs(ip->tot_len);
6566         lro->frags_len = 0;
6567         /* 
6568          * check if we saw TCP timestamp. Other consistency checks have
6569          * already been done.
6570          */
6571         if (tcp->doff == 8) {
6572                 u32 *ptr;
6573                 ptr = (u32 *)(tcp+1);
6574                 lro->saw_ts = 1;
6575                 lro->cur_tsval = *(ptr+1);
6576                 lro->cur_tsecr = *(ptr+2);
6577         }
6578         lro->in_use = 1;
6579 }
6580
6581 static void update_L3L4_header(nic_t *sp, lro_t *lro)
6582 {
6583         struct iphdr *ip = lro->iph;
6584         struct tcphdr *tcp = lro->tcph;
6585         u16 nchk;
6586         StatInfo_t *statinfo = sp->mac_control.stats_info;
6587         DBG_PRINT(INFO_DBG,"%s: Been here...\n", __FUNCTION__);
6588
6589         /* Update L3 header */
6590         ip->tot_len = htons(lro->total_len);
6591         ip->check = 0;
6592         nchk = ip_fast_csum((u8 *)lro->iph, ip->ihl);
6593         ip->check = nchk;
6594
6595         /* Update L4 header */
6596         tcp->ack_seq = lro->tcp_ack;
6597         tcp->window = lro->window;
6598
6599         /* Update tsecr field if this session has timestamps enabled */
6600         if (lro->saw_ts) {
6601                 u32 *ptr = (u32 *)(tcp + 1);
6602                 *(ptr+2) = lro->cur_tsecr;
6603         }
6604
6605         /* Update counters required for calculation of
6606          * average no. of packets aggregated.
6607          */
6608         statinfo->sw_stat.sum_avg_pkts_aggregated += lro->sg_num;
6609         statinfo->sw_stat.num_aggregations++;
6610 }
6611
6612 static void aggregate_new_rx(lro_t *lro, struct iphdr *ip,
6613                 struct tcphdr *tcp, u32 l4_pyld)
6614 {
6615         DBG_PRINT(INFO_DBG,"%s: Been here...\n", __FUNCTION__);
6616         lro->total_len += l4_pyld;
6617         lro->frags_len += l4_pyld;
6618         lro->tcp_next_seq += l4_pyld;
6619         lro->sg_num++;
6620
6621         /* Update ack seq no. and window ad(from this pkt) in LRO object */
6622         lro->tcp_ack = tcp->ack_seq;
6623         lro->window = tcp->window;
6624         
6625         if (lro->saw_ts) {
6626                 u32 *ptr;
6627                 /* Update tsecr and tsval from this packet */
6628                 ptr = (u32 *) (tcp + 1);
6629                 lro->cur_tsval = *(ptr + 1); 
6630                 lro->cur_tsecr = *(ptr + 2);
6631         }
6632 }
6633
6634 static int verify_l3_l4_lro_capable(lro_t *l_lro, struct iphdr *ip,
6635                                     struct tcphdr *tcp, u32 tcp_pyld_len)
6636 {
6637         u8 *ptr;
6638
6639         DBG_PRINT(INFO_DBG,"%s: Been here...\n", __FUNCTION__);
6640
6641         if (!tcp_pyld_len) {
6642                 /* Runt frame or a pure ack */
6643                 return -1;
6644         }
6645
6646         if (ip->ihl != 5) /* IP has options */
6647                 return -1;
6648
6649         if (tcp->urg || tcp->psh || tcp->rst || tcp->syn || tcp->fin ||
6650                                                                 !tcp->ack) {
6651                 /*
6652                  * Currently recognize only the ack control word and
6653                  * any other control field being set would result in
6654                  * flushing the LRO session
6655                  */
6656                 return -1;
6657         }
6658
6659         /* 
6660          * Allow only one TCP timestamp option. Don't aggregate if
6661          * any other options are detected.
6662          */
6663         if (tcp->doff != 5 && tcp->doff != 8)
6664                 return -1;
6665
6666         if (tcp->doff == 8) {
6667                 ptr = (u8 *)(tcp + 1);  
6668                 while (*ptr == TCPOPT_NOP)
6669                         ptr++;
6670                 if (*ptr != TCPOPT_TIMESTAMP || *(ptr+1) != TCPOLEN_TIMESTAMP)
6671                         return -1;
6672
6673                 /* Ensure timestamp value increases monotonically */
6674                 if (l_lro)
6675                         if (l_lro->cur_tsval > *((u32 *)(ptr+2)))
6676                                 return -1;
6677
6678                 /* timestamp echo reply should be non-zero */
6679                 if (*((u32 *)(ptr+6)) == 0) 
6680                         return -1;
6681         }
6682
6683         return 0;
6684 }
6685
6686 static int
6687 s2io_club_tcp_session(u8 *buffer, u8 **tcp, u32 *tcp_len, lro_t **lro,
6688                       RxD_t *rxdp, nic_t *sp)
6689 {
6690         struct iphdr *ip;
6691         struct tcphdr *tcph;
6692         int ret = 0, i;
6693
6694         if (!(ret = check_L2_lro_capable(buffer, &ip, (struct tcphdr **)tcp,
6695                                          rxdp))) {
6696                 DBG_PRINT(INFO_DBG,"IP Saddr: %x Daddr: %x\n",
6697                           ip->saddr, ip->daddr);
6698         } else {
6699                 return ret;
6700         }
6701
6702         tcph = (struct tcphdr *)*tcp;
6703         *tcp_len = get_l4_pyld_length(ip, tcph);
6704         for (i=0; i<MAX_LRO_SESSIONS; i++) {
6705                 lro_t *l_lro = &sp->lro0_n[i];
6706                 if (l_lro->in_use) {
6707                         if (check_for_socket_match(l_lro, ip, tcph))
6708                                 continue;
6709                         /* Sock pair matched */
6710                         *lro = l_lro;
6711
6712                         if ((*lro)->tcp_next_seq != ntohl(tcph->seq)) {
6713                                 DBG_PRINT(INFO_DBG, "%s:Out of order. expected "
6714                                           "0x%x, actual 0x%x\n", __FUNCTION__,
6715                                           (*lro)->tcp_next_seq,
6716                                           ntohl(tcph->seq));
6717
6718                                 sp->mac_control.stats_info->
6719                                    sw_stat.outof_sequence_pkts++;
6720                                 ret = 2;
6721                                 break;
6722                         }
6723
6724                         if (!verify_l3_l4_lro_capable(l_lro, ip, tcph,*tcp_len))
6725                                 ret = 1; /* Aggregate */
6726                         else
6727                                 ret = 2; /* Flush both */
6728                         break;
6729                 }
6730         }
6731
6732         if (ret == 0) {
6733                 /* Before searching for available LRO objects,
6734                  * check if the pkt is L3/L4 aggregatable. If not
6735                  * don't create new LRO session. Just send this
6736                  * packet up.
6737                  */
6738                 if (verify_l3_l4_lro_capable(NULL, ip, tcph, *tcp_len)) {
6739                         return 5;
6740                 }
6741
6742                 for (i=0; i<MAX_LRO_SESSIONS; i++) {
6743                         lro_t *l_lro = &sp->lro0_n[i];
6744                         if (!(l_lro->in_use)) {
6745                                 *lro = l_lro;
6746                                 ret = 3; /* Begin anew */
6747                                 break;
6748                         }
6749                 }
6750         }
6751
6752         if (ret == 0) { /* sessions exceeded */
6753                 DBG_PRINT(INFO_DBG,"%s:All LRO sessions already in use\n",
6754                           __FUNCTION__);
6755                 *lro = NULL;
6756                 return ret;
6757         }
6758
6759         switch (ret) {
6760                 case 3:
6761                         initiate_new_session(*lro, buffer, ip, tcph, *tcp_len);
6762                         break;
6763                 case 2:
6764                         update_L3L4_header(sp, *lro);
6765                         break;
6766                 case 1:
6767                         aggregate_new_rx(*lro, ip, tcph, *tcp_len);
6768                         if ((*lro)->sg_num == sp->lro_max_aggr_per_sess) {
6769                                 update_L3L4_header(sp, *lro);
6770                                 ret = 4; /* Flush the LRO */
6771                         }
6772                         break;
6773                 default:
6774                         DBG_PRINT(ERR_DBG,"%s:Dont know, can't say!!\n",
6775                                 __FUNCTION__);
6776                         break;
6777         }
6778
6779         return ret;
6780 }
6781
6782 static void clear_lro_session(lro_t *lro)
6783 {
6784         static u16 lro_struct_size = sizeof(lro_t);
6785
6786         memset(lro, 0, lro_struct_size);
6787 }
6788
6789 static void queue_rx_frame(struct sk_buff *skb)
6790 {
6791         struct net_device *dev = skb->dev;
6792
6793         skb->protocol = eth_type_trans(skb, dev);
6794 #ifdef CONFIG_S2IO_NAPI
6795         netif_receive_skb(skb);
6796 #else
6797         netif_rx(skb);
6798 #endif
6799 }
6800
6801 static void lro_append_pkt(nic_t *sp, lro_t *lro, struct sk_buff *skb,
6802                            u32 tcp_len)
6803 {
6804         struct sk_buff *tmp, *first = lro->parent;
6805
6806         first->len += tcp_len;
6807         first->data_len = lro->frags_len;
6808         skb_pull(skb, (skb->len - tcp_len));
6809         if ((tmp = skb_shinfo(first)->frag_list)) {
6810                 while (tmp->next)
6811                         tmp = tmp->next;
6812                 tmp->next = skb;
6813         }
6814         else
6815                 skb_shinfo(first)->frag_list = skb;
6816         sp->mac_control.stats_info->sw_stat.clubbed_frms_cnt++;
6817         return;
6818 }