ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / s2io.c
1 /************************************************************************
2  * s2io.c: A Linux PCI-X Ethernet driver for S2IO 10GbE Server NIC
3  * Copyright(c) 2002-2005 S2IO Technologies
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  * ring_num : This can be used to program the number of receive rings used 
30  * in the driver.                                       
31  * frame_len: This is an array of size 8. Using this we can set the maximum 
32  * size of the received frame that can be steered into the corrsponding 
33  * receive ring.
34  * ring_len: This defines the number of descriptors each ring can have. This 
35  * is also an array of size 8.
36  * fifo_num: This defines the number of Tx FIFOs thats used int the driver.
37  * fifo_len: This too is an array of 8. Each element defines the number of 
38  * Tx descriptors that can be associated with each corresponding FIFO.
39  * latency_timer: This input is programmed into the Latency timer register
40  * in PCI Configuration space.
41  ************************************************************************/
42
43 #include<linux/config.h>
44 #include<linux/module.h>
45 #include<linux/types.h>
46 #include<linux/errno.h>
47 #include<linux/ioport.h>
48 #include<linux/pci.h>
49 #include<linux/kernel.h>
50 #include<linux/netdevice.h>
51 #include<linux/etherdevice.h>
52 #include<linux/skbuff.h>
53 #include<linux/init.h>
54 #include<linux/delay.h>
55 #include<linux/stddef.h>
56 #include<linux/ioctl.h>
57 #include<linux/timex.h>
58 #include<linux/sched.h>
59 #include<linux/ethtool.h>
60 #include<asm/system.h>
61 #include<asm/uaccess.h>
62 #include<linux/version.h>
63 #include<asm/io.h>
64 #include<linux/workqueue.h>
65
66 /* local include */
67 #include "s2io.h"
68 #include "s2io-regs.h"
69
70 /* S2io Driver name & version. */
71 static char s2io_driver_name[] = "s2io";
72 static char s2io_driver_version[] = "Version 1.0";
73
74 #define LINK_IS_UP(val64) (!(val64 & (ADAPTER_STATUS_RMAC_REMOTE_FAULT | \
75                                       ADAPTER_STATUS_RMAC_LOCAL_FAULT)))
76 #define TASKLET_IN_USE test_and_set_bit(0, \
77                                 (unsigned long *)(&sp->tasklet_status))
78 #define PANIC   1
79 #define LOW     2
80 static inline int rx_buffer_level(nic_t * sp, int rxb_size, int ring)
81 {
82         int level = 0;
83         if ((sp->pkt_cnt[ring] - rxb_size) > 128) {
84                 level = LOW;
85                 if (rxb_size < sp->pkt_cnt[ring] / 8)
86                         level = PANIC;
87         }
88
89         return level;
90 }
91
92 /* Ethtool related variables and Macros. */
93 static char s2io_gstrings[][ETH_GSTRING_LEN] = {
94         "Register test\t(offline)",
95         "Eeprom test\t(offline)",
96         "Link test\t(online)",
97         "RLDRAM test\t(offline)",
98         "BIST Test\t(offline)"
99 };
100
101 static char ethtool_stats_keys[][ETH_GSTRING_LEN] = {
102         {"tmac_frms"},
103         {"tmac_data_octets"},
104         {"tmac_drop_frms"},
105         {"tmac_mcst_frms"},
106         {"tmac_bcst_frms"},
107         {"tmac_pause_ctrl_frms"},
108         {"tmac_any_err_frms"},
109         {"tmac_vld_ip_octets"},
110         {"tmac_vld_ip"},
111         {"tmac_drop_ip"},
112         {"tmac_icmp"},
113         {"tmac_rst_tcp"},
114         {"tmac_tcp"},
115         {"tmac_udp"},
116         {"rmac_vld_frms"},
117         {"rmac_data_octets"},
118         {"rmac_fcs_err_frms"},
119         {"rmac_drop_frms"},
120         {"rmac_vld_mcst_frms"},
121         {"rmac_vld_bcst_frms"},
122         {"rmac_in_rng_len_err_frms"},
123         {"rmac_long_frms"},
124         {"rmac_pause_ctrl_frms"},
125         {"rmac_discarded_frms"},
126         {"rmac_usized_frms"},
127         {"rmac_osized_frms"},
128         {"rmac_frag_frms"},
129         {"rmac_jabber_frms"},
130         {"rmac_ip"},
131         {"rmac_ip_octets"},
132         {"rmac_hdr_err_ip"},
133         {"rmac_drop_ip"},
134         {"rmac_icmp"},
135         {"rmac_tcp"},
136         {"rmac_udp"},
137         {"rmac_err_drp_udp"},
138         {"rmac_pause_cnt"},
139         {"rmac_accepted_ip"},
140         {"rmac_err_tcp"},
141 };
142
143 #define S2IO_STAT_LEN sizeof(ethtool_stats_keys)/ ETH_GSTRING_LEN
144 #define S2IO_STAT_STRINGS_LEN S2IO_STAT_LEN * ETH_GSTRING_LEN
145
146 #define S2IO_TEST_LEN   sizeof(s2io_gstrings) / ETH_GSTRING_LEN
147 #define S2IO_STRINGS_LEN        S2IO_TEST_LEN * ETH_GSTRING_LEN
148
149
150 /* Constants to be programmed into the Xena's registers to configure
151  * the XAUI.
152  */
153
154 #define SWITCH_SIGN     0xA5A5A5A5A5A5A5A5ULL
155 #define END_SIGN        0x0
156
157 static u64 default_mdio_cfg[] = {
158         /* Reset PMA PLL */
159         0xC001010000000000ULL, 0xC0010100000000E0ULL,
160         0xC0010100008000E4ULL,
161         /* Remove Reset from PMA PLL */
162         0xC001010000000000ULL, 0xC0010100000000E0ULL,
163         0xC0010100000000E4ULL,
164         END_SIGN
165 };
166
167 static u64 default_dtx_cfg[] = {
168         0x8000051500000000ULL, 0x80000515000000E0ULL,
169         0x80000515D93500E4ULL, 0x8001051500000000ULL,
170         0x80010515000000E0ULL, 0x80010515001E00E4ULL,
171         0x8002051500000000ULL, 0x80020515000000E0ULL,
172         0x80020515F21000E4ULL,
173         /* Set PADLOOPBACKN */
174         0x8002051500000000ULL, 0x80020515000000E0ULL,
175         0x80020515B20000E4ULL, 0x8003051500000000ULL,
176         0x80030515000000E0ULL, 0x80030515B20000E4ULL,
177         0x8004051500000000ULL, 0x80040515000000E0ULL,
178         0x80040515B20000E4ULL, 0x8005051500000000ULL,
179         0x80050515000000E0ULL, 0x80050515B20000E4ULL,
180         SWITCH_SIGN,
181         /* Remove PADLOOPBACKN */
182         0x8002051500000000ULL, 0x80020515000000E0ULL,
183         0x80020515F20000E4ULL, 0x8003051500000000ULL,
184         0x80030515000000E0ULL, 0x80030515F20000E4ULL,
185         0x8004051500000000ULL, 0x80040515000000E0ULL,
186         0x80040515F20000E4ULL, 0x8005051500000000ULL,
187         0x80050515000000E0ULL, 0x80050515F20000E4ULL,
188         END_SIGN
189 };
190
191 /* Constants for Fixing the MacAddress problem seen mostly on
192  * Alpha machines.
193  */
194 static u64 fix_mac[] = {
195         0x0060000000000000ULL, 0x0060600000000000ULL,
196         0x0040600000000000ULL, 0x0000600000000000ULL,
197         0x0020600000000000ULL, 0x0060600000000000ULL,
198         0x0020600000000000ULL, 0x0060600000000000ULL,
199         0x0020600000000000ULL, 0x0060600000000000ULL,
200         0x0020600000000000ULL, 0x0060600000000000ULL,
201         0x0020600000000000ULL, 0x0060600000000000ULL,
202         0x0020600000000000ULL, 0x0060600000000000ULL,
203         0x0020600000000000ULL, 0x0060600000000000ULL,
204         0x0020600000000000ULL, 0x0060600000000000ULL,
205         0x0020600000000000ULL, 0x0060600000000000ULL,
206         0x0020600000000000ULL, 0x0060600000000000ULL,
207         0x0020600000000000ULL, 0x0000600000000000ULL,
208         0x0040600000000000ULL, 0x0060600000000000ULL,
209         END_SIGN
210 };
211
212
213 /* Module Loadable parameters. */
214 static u32 ring_num;
215 static u32 frame_len[MAX_RX_RINGS];
216 static u32 ring_len[MAX_RX_RINGS];
217 static u32 fifo_num;
218 static u32 fifo_len[MAX_TX_FIFOS];
219 static u32 rx_prio;
220 static u32 tx_prio;
221 static u8 latency_timer = 0;
222
223 /* 
224  * S2IO device table.
225  * This table lists all the devices that this driver supports. 
226  */
227 static struct pci_device_id s2io_tbl[] __devinitdata = {
228         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_S2IO_WIN,
229          PCI_ANY_ID, PCI_ANY_ID},
230         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_S2IO_UNI,
231          PCI_ANY_ID, PCI_ANY_ID},
232         {0,}
233 };
234
235 MODULE_DEVICE_TABLE(pci, s2io_tbl);
236
237 static struct pci_driver s2io_driver = {
238       name:"S2IO",
239       id_table:s2io_tbl,
240       probe:s2io_init_nic,
241       remove:s2io_rem_nic,
242 };
243
244 /*  
245  *  Input Arguments: 
246  *  Device private variable.
247  *  Return Value: 
248  *  SUCCESS on success and an appropriate -ve value on failure.
249  *  Description: 
250  *  The function allocates the all memory areas shared 
251  *  between the NIC and the driver. This includes Tx descriptors, 
252  *  Rx descriptors and the statistics block.
253  */
254 static int initSharedMem(struct s2io_nic *nic)
255 {
256         u32 size;
257         void *tmp_v_addr, *tmp_v_addr_next;
258         dma_addr_t tmp_p_addr, tmp_p_addr_next;
259         RxD_block_t *pre_rxd_blk = NULL;
260         int i, j, blk_cnt;
261         struct net_device *dev = nic->dev;
262
263         mac_info_t *mac_control;
264         struct config_param *config;
265
266         mac_control = &nic->mac_control;
267         config = &nic->config;
268
269
270         /* Allocation and initialization of TXDLs in FIOFs */
271         size = 0;
272         for (i = 0; i < config->TxFIFONum; i++) {
273                 size += config->TxCfg[i].FifoLen;
274         }
275         if (size > MAX_AVAILABLE_TXDS) {
276                 DBG_PRINT(ERR_DBG, "%s: Total number of Tx FIFOs ",
277                           dev->name);
278                 DBG_PRINT(ERR_DBG, "exceeds the maximum value ");
279                 DBG_PRINT(ERR_DBG, "that can be used\n");
280                 return FAILURE;
281         }
282         size *= (sizeof(TxD_t) * config->MaxTxDs);
283
284         mac_control->txd_list_mem = pci_alloc_consistent
285             (nic->pdev, size, &mac_control->txd_list_mem_phy);
286         if (!mac_control->txd_list_mem) {
287                 return -ENOMEM;
288         }
289         mac_control->txd_list_mem_sz = size;
290
291         tmp_v_addr = mac_control->txd_list_mem;
292         tmp_p_addr = mac_control->txd_list_mem_phy;
293         memset(tmp_v_addr, 0, size);
294
295         DBG_PRINT(INIT_DBG, "%s:List Mem PHY: 0x%llx\n", dev->name,
296                   (unsigned long long) tmp_p_addr);
297
298         for (i = 0; i < config->TxFIFONum; i++) {
299                 mac_control->txdl_start_phy[i] = tmp_p_addr;
300                 mac_control->txdl_start[i] = (TxD_t *) tmp_v_addr;
301                 mac_control->tx_curr_put_info[i].offset = 0;
302                 mac_control->tx_curr_put_info[i].fifo_len =
303                     config->TxCfg[i].FifoLen - 1;
304                 mac_control->tx_curr_get_info[i].offset = 0;
305                 mac_control->tx_curr_get_info[i].fifo_len =
306                     config->TxCfg[i].FifoLen - 1;
307
308                 tmp_p_addr +=
309                     (config->TxCfg[i].FifoLen * (sizeof(TxD_t)) *
310                      config->MaxTxDs);
311                 tmp_v_addr +=
312                     (config->TxCfg[i].FifoLen * (sizeof(TxD_t)) *
313                      config->MaxTxDs);
314         }
315
316         /* Allocation and initialization of RXDs in Rings */
317         size = 0;
318         for (i = 0; i < config->RxRingNum; i++) {
319                 if (config->RxCfg[i].NumRxd % (MAX_RXDS_PER_BLOCK + 1)) {
320                         DBG_PRINT(ERR_DBG, "%s: RxD count of ", dev->name);
321                         DBG_PRINT(ERR_DBG, "Ring%d is not a multiple of ",
322                                   i);
323                         DBG_PRINT(ERR_DBG, "RxDs per Block");
324                         return FAILURE;
325                 }
326                 size += config->RxCfg[i].NumRxd;
327                 nic->block_count[i] =
328                     config->RxCfg[i].NumRxd / (MAX_RXDS_PER_BLOCK + 1);
329                 nic->pkt_cnt[i] =
330                     config->RxCfg[i].NumRxd - nic->block_count[i];
331         }
332         size = (size * (sizeof(RxD_t)));
333         mac_control->rxd_ring_mem_sz = size;
334
335         for (i = 0; i < config->RxRingNum; i++) {
336                 mac_control->rx_curr_get_info[i].block_index = 0;
337                 mac_control->rx_curr_get_info[i].offset = 0;
338                 mac_control->rx_curr_get_info[i].ring_len =
339                     config->RxCfg[i].NumRxd - 1;
340                 mac_control->rx_curr_put_info[i].block_index = 0;
341                 mac_control->rx_curr_put_info[i].offset = 0;
342                 mac_control->rx_curr_put_info[i].ring_len =
343                     config->RxCfg[i].NumRxd - 1;
344                 blk_cnt =
345                     config->RxCfg[i].NumRxd / (MAX_RXDS_PER_BLOCK + 1);
346                 /*  Allocating all the Rx blocks */
347                 for (j = 0; j < blk_cnt; j++) {
348                         size = (MAX_RXDS_PER_BLOCK + 1) * (sizeof(RxD_t));
349                         tmp_v_addr = pci_alloc_consistent(nic->pdev, size,
350                                                           &tmp_p_addr);
351                         if (tmp_v_addr == NULL) {
352                                 /* In case of failure, freeSharedMem() 
353                                  * is called, which should free any 
354                                  * memory that was alloced till the 
355                                  * failure happened.
356                                  */
357                                 nic->rx_blocks[i][j].block_virt_addr =
358                                     tmp_v_addr;
359                                 return -ENOMEM;
360                         }
361                         memset(tmp_v_addr, 0, size);
362                         nic->rx_blocks[i][j].block_virt_addr = tmp_v_addr;
363                         nic->rx_blocks[i][j].block_dma_addr = tmp_p_addr;
364                 }
365                 /* Interlinking all Rx Blocks */
366                 for (j = 0; j < blk_cnt; j++) {
367                         tmp_v_addr = nic->rx_blocks[i][j].block_virt_addr;
368                         tmp_v_addr_next =
369                             nic->rx_blocks[i][(j + 1) %
370                                               blk_cnt].block_virt_addr;
371                         tmp_p_addr = nic->rx_blocks[i][j].block_dma_addr;
372                         tmp_p_addr_next =
373                             nic->rx_blocks[i][(j + 1) %
374                                               blk_cnt].block_dma_addr;
375
376                         pre_rxd_blk = (RxD_block_t *) tmp_v_addr;
377                         pre_rxd_blk->reserved_1 = END_OF_BLOCK; /* last RxD 
378                                                                  * marker.
379                                                                  */
380                         pre_rxd_blk->reserved_2_pNext_RxD_block =
381                             (unsigned long) tmp_v_addr_next;
382                         pre_rxd_blk->pNext_RxD_Blk_physical =
383                             (u64) tmp_p_addr_next;
384                 }
385         }
386
387         /* Allocation and initialization of Statistics block */
388         size = sizeof(StatInfo_t);
389         mac_control->stats_mem = pci_alloc_consistent
390             (nic->pdev, size, &mac_control->stats_mem_phy);
391
392         if (!mac_control->stats_mem) {
393                 /* In case of failure, freeSharedMem() is called, which 
394                  * should free any memory that was alloced till the 
395                  * failure happened.
396                  */
397                 return -ENOMEM;
398         }
399         mac_control->stats_mem_sz = size;
400
401         tmp_v_addr = mac_control->stats_mem;
402         mac_control->StatsInfo = (StatInfo_t *) tmp_v_addr;
403         memset(tmp_v_addr, 0, size);
404
405         DBG_PRINT(INIT_DBG, "%s:Ring Mem PHY: 0x%llx\n", dev->name,
406                   (unsigned long long) tmp_p_addr);
407
408         return SUCCESS;
409 }
410
411 /*  
412  *  Input Arguments: 
413  *  Device peivate variable.
414  *  Return Value: 
415  *  NONE
416  *  Description: 
417  *  This function is to free all memory locations allocated by
418  *  the initSharedMem() function and return it to the kernel.
419  */
420 static void freeSharedMem(struct s2io_nic *nic)
421 {
422         int i, j, blk_cnt, size;
423         void *tmp_v_addr;
424         dma_addr_t tmp_p_addr;
425         mac_info_t *mac_control;
426         struct config_param *config;
427
428
429         if (!nic)
430                 return;
431
432         mac_control = &nic->mac_control;
433         config = &nic->config;
434
435         if (mac_control->txd_list_mem) {
436                 pci_free_consistent(nic->pdev,
437                                     mac_control->txd_list_mem_sz,
438                                     mac_control->txd_list_mem,
439                                     mac_control->txd_list_mem_phy);
440         }
441
442         size = (MAX_RXDS_PER_BLOCK + 1) * (sizeof(RxD_t));
443         for (i = 0; i < config->RxRingNum; i++) {
444                 blk_cnt = nic->block_count[i];
445                 for (j = 0; j < blk_cnt; j++) {
446                         tmp_v_addr = nic->rx_blocks[i][j].block_virt_addr;
447                         tmp_p_addr = nic->rx_blocks[i][j].block_dma_addr;
448                         if (tmp_v_addr == NULL)
449                                 break;
450                         pci_free_consistent(nic->pdev, size,
451                                             tmp_v_addr, tmp_p_addr);
452                 }
453         }
454
455         if (mac_control->stats_mem) {
456                 pci_free_consistent(nic->pdev,
457                                     mac_control->stats_mem_sz,
458                                     mac_control->stats_mem,
459                                     mac_control->stats_mem_phy);
460         }
461 }
462
463 /*  
464  *  Input Arguments: 
465  *  device peivate variable
466  *  Return Value: 
467  *  SUCCESS on success and '-1' on failure (endian settings incorrect).
468  *  Description: 
469  *  The function sequentially configures every block 
470  *  of the H/W from their reset values. 
471  */
472 static int initNic(struct s2io_nic *nic)
473 {
474         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
475         struct net_device *dev = nic->dev;
476         register u64 val64 = 0;
477         void *add;
478         u32 time;
479         int i, j;
480         mac_info_t *mac_control;
481         struct config_param *config;
482         int mdio_cnt = 0, dtx_cnt = 0;
483         unsigned long long print_var, mem_share;
484
485         mac_control = &nic->mac_control;
486         config = &nic->config;
487
488         /*  Set proper endian settings and verify the same by 
489          *  reading the PIF Feed-back register.
490          */
491 #ifdef  __BIG_ENDIAN
492         /* The device by default set to a big endian format, so 
493          * a big endian driver need not set anything.
494          */
495         writeq(0xffffffffffffffffULL, &bar0->swapper_ctrl);
496         val64 = (SWAPPER_CTRL_PIF_R_FE |
497                  SWAPPER_CTRL_PIF_R_SE |
498                  SWAPPER_CTRL_PIF_W_FE |
499                  SWAPPER_CTRL_PIF_W_SE |
500                  SWAPPER_CTRL_TXP_FE |
501                  SWAPPER_CTRL_TXP_SE |
502                  SWAPPER_CTRL_TXD_R_FE |
503                  SWAPPER_CTRL_TXD_W_FE |
504                  SWAPPER_CTRL_TXF_R_FE |
505                  SWAPPER_CTRL_RXD_R_FE |
506                  SWAPPER_CTRL_RXD_W_FE |
507                  SWAPPER_CTRL_RXF_W_FE |
508                  SWAPPER_CTRL_XMSI_FE |
509                  SWAPPER_CTRL_XMSI_SE |
510                  SWAPPER_CTRL_STATS_FE | SWAPPER_CTRL_STATS_SE);
511         writeq(val64, &bar0->swapper_ctrl);
512 #else
513         /* Initially we enable all bits to make it accessible by 
514          * the driver, then we selectively enable only those bits 
515          * that we want to set.
516          */
517         writeq(0xffffffffffffffffULL, &bar0->swapper_ctrl);
518         val64 = (SWAPPER_CTRL_PIF_R_FE |
519                  SWAPPER_CTRL_PIF_R_SE |
520                  SWAPPER_CTRL_PIF_W_FE |
521                  SWAPPER_CTRL_PIF_W_SE |
522                  SWAPPER_CTRL_TXP_FE |
523                  SWAPPER_CTRL_TXP_SE |
524                  SWAPPER_CTRL_TXD_R_FE |
525                  SWAPPER_CTRL_TXD_R_SE |
526                  SWAPPER_CTRL_TXD_W_FE |
527                  SWAPPER_CTRL_TXD_W_SE |
528                  SWAPPER_CTRL_TXF_R_FE |
529                  SWAPPER_CTRL_RXD_R_FE |
530                  SWAPPER_CTRL_RXD_R_SE |
531                  SWAPPER_CTRL_RXD_W_FE |
532                  SWAPPER_CTRL_RXD_W_SE |
533                  SWAPPER_CTRL_RXF_W_FE |
534                  SWAPPER_CTRL_XMSI_FE |
535                  SWAPPER_CTRL_XMSI_SE |
536                  SWAPPER_CTRL_STATS_FE | SWAPPER_CTRL_STATS_SE);
537         writeq(val64, &bar0->swapper_ctrl);
538 #endif
539
540         /* Verifying if endian settings are accurate by reading 
541          * a feedback register.
542          */
543         val64 = readq(&bar0->pif_rd_swapper_fb);
544         if (val64 != 0x0123456789ABCDEFULL) {
545                 /* Endian settings are incorrect, calls for another dekko. */
546                 print_var = (unsigned long long) val64;
547                 DBG_PRINT(INIT_DBG, "%s: Endian settings are wrong",
548                           dev->name);
549                 DBG_PRINT(ERR_DBG, ", feedback read %llx\n", print_var);
550
551                 return FAILURE;
552         }
553
554         /* Remove XGXS from reset state */
555         val64 = 0;
556         writeq(val64, &bar0->sw_reset);
557         val64 = readq(&bar0->sw_reset);
558         set_current_state(TASK_UNINTERRUPTIBLE);
559         schedule_timeout(HZ / 2);
560
561         /*  Enable Receiving broadcasts */
562         val64 = readq(&bar0->mac_cfg);
563         val64 |= MAC_RMAC_BCAST_ENABLE;
564         writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
565         writeq(val64, &bar0->mac_cfg);
566
567         /* Read registers in all blocks */
568         val64 = readq(&bar0->mac_int_mask);
569         val64 = readq(&bar0->mc_int_mask);
570         val64 = readq(&bar0->xgxs_int_mask);
571
572         /*  Set MTU */
573         val64 = dev->mtu;
574         writeq(vBIT(val64, 2, 14), &bar0->rmac_max_pyld_len);
575
576         /* Configuring the XAUI Interface of Xena. 
577          *****************************************
578          * To Configure the Xena's XAUI, one has to write a series 
579          * of 64 bit values into two registers in a particular 
580          * sequence. Hence a macro 'SWITCH_SIGN' has been defined 
581          * which will be defined in the array of configuration values 
582          * (default_dtx_cfg & default_mdio_cfg) at appropriate places 
583          * to switch writing from one regsiter to another. We continue 
584          * writing these values until we encounter the 'END_SIGN' macro.
585          * For example, After making a series of 21 writes into 
586          * dtx_control register the 'SWITCH_SIGN' appears and hence we 
587          * start writing into mdio_control until we encounter END_SIGN.
588          */
589         while (1) {
590               dtx_cfg:
591                 while (default_dtx_cfg[dtx_cnt] != END_SIGN) {
592                         if (default_dtx_cfg[dtx_cnt] == SWITCH_SIGN) {
593                                 dtx_cnt++;
594                                 goto mdio_cfg;
595                         }
596                         writeq(default_dtx_cfg[dtx_cnt],
597                                &bar0->dtx_control);
598                         val64 = readq(&bar0->dtx_control);
599                         dtx_cnt++;
600                 }
601               mdio_cfg:
602                 while (default_mdio_cfg[mdio_cnt] != END_SIGN) {
603                         if (default_mdio_cfg[mdio_cnt] == SWITCH_SIGN) {
604                                 mdio_cnt++;
605                                 goto dtx_cfg;
606                         }
607                         writeq(default_mdio_cfg[mdio_cnt],
608                                &bar0->mdio_control);
609                         val64 = readq(&bar0->mdio_control);
610                         mdio_cnt++;
611                 }
612                 if ((default_dtx_cfg[dtx_cnt] == END_SIGN) &&
613                     (default_mdio_cfg[mdio_cnt] == END_SIGN)) {
614                         break;
615                 } else {
616                         goto dtx_cfg;
617                 }
618         }
619
620         /*  Tx DMA Initialization */
621         val64 = 0;
622         writeq(val64, &bar0->tx_fifo_partition_0);
623         writeq(val64, &bar0->tx_fifo_partition_1);
624         writeq(val64, &bar0->tx_fifo_partition_2);
625         writeq(val64, &bar0->tx_fifo_partition_3);
626
627
628         for (i = 0, j = 0; i < config->TxFIFONum; i++) {
629                 val64 |=
630                     vBIT(config->TxCfg[i].FifoLen - 1, ((i * 32) + 19),
631                          13) | vBIT(config->TxCfg[i].FifoPriority,
632                                     ((i * 32) + 5), 3);
633
634                 if (i == (config->TxFIFONum - 1)) {
635                         if (i % 2 == 0)
636                                 i++;
637                 }
638
639                 switch (i) {
640                 case 1:
641                         writeq(val64, &bar0->tx_fifo_partition_0);
642                         val64 = 0;
643                         break;
644                 case 3:
645                         writeq(val64, &bar0->tx_fifo_partition_1);
646                         val64 = 0;
647                         break;
648                 case 5:
649                         writeq(val64, &bar0->tx_fifo_partition_2);
650                         val64 = 0;
651                         break;
652                 case 7:
653                         writeq(val64, &bar0->tx_fifo_partition_3);
654                         break;
655                 }
656         }
657
658         /* Enable Tx FIFO partition 0. */
659         val64 = readq(&bar0->tx_fifo_partition_0);
660         val64 |= BIT(0);        /* To enable the FIFO partition. */
661         writeq(val64, &bar0->tx_fifo_partition_0);
662
663         val64 = readq(&bar0->tx_fifo_partition_0);
664         DBG_PRINT(INIT_DBG, "Fifo partition at: 0x%p is: 0x%llx\n",
665                   &bar0->tx_fifo_partition_0, (unsigned long long) val64);
666
667         /* 
668          * Initialization of Tx_PA_CONFIG register to ignore packet 
669          * integrity checking.
670          */
671         val64 = readq(&bar0->tx_pa_cfg);
672         val64 |= TX_PA_CFG_IGNORE_FRM_ERR | TX_PA_CFG_IGNORE_SNAP_OUI |
673             TX_PA_CFG_IGNORE_LLC_CTRL | TX_PA_CFG_IGNORE_L2_ERR;
674         writeq(val64, &bar0->tx_pa_cfg);
675
676         /* Rx DMA intialization. */
677         val64 = 0;
678         for (i = 0; i < config->RxRingNum; i++) {
679                 val64 |=
680                     vBIT(config->RxCfg[i].RingPriority, (5 + (i * 8)), 3);
681         }
682         writeq(val64, &bar0->rx_queue_priority);
683
684         /* Allocating equal share of memory to all the configured 
685          * Rings.
686          */
687         val64 = 0;
688         for (i = 0; i < config->RxRingNum; i++) {
689                 switch (i) {
690                 case 0:
691                         mem_share = (64 / config->RxRingNum +
692                                      64 % config->RxRingNum);
693                         val64 |= RX_QUEUE_CFG_Q0_SZ(mem_share);
694                         continue;
695                 case 1:
696                         mem_share = (64 / config->RxRingNum);
697                         val64 |= RX_QUEUE_CFG_Q1_SZ(mem_share);
698                         continue;
699                 case 2:
700                         mem_share = (64 / config->RxRingNum);
701                         val64 |= RX_QUEUE_CFG_Q2_SZ(mem_share);
702                         continue;
703                 case 3:
704                         mem_share = (64 / config->RxRingNum);
705                         val64 |= RX_QUEUE_CFG_Q3_SZ(mem_share);
706                         continue;
707                 case 4:
708                         mem_share = (64 / config->RxRingNum);
709                         val64 |= RX_QUEUE_CFG_Q4_SZ(mem_share);
710                         continue;
711                 case 5:
712                         mem_share = (64 / config->RxRingNum);
713                         val64 |= RX_QUEUE_CFG_Q5_SZ(mem_share);
714                         continue;
715                 case 6:
716                         mem_share = (64 / config->RxRingNum);
717                         val64 |= RX_QUEUE_CFG_Q6_SZ(mem_share);
718                         continue;
719                 case 7:
720                         mem_share = (64 / config->RxRingNum);
721                         val64 |= RX_QUEUE_CFG_Q7_SZ(mem_share);
722                         continue;
723                 }
724         }
725         writeq(val64, &bar0->rx_queue_cfg);
726
727         /* Initializing the Tx round robin registers to 0.
728          * Filling Tx and Rx round robin registers as per the 
729          * number of FIFOs and Rings is still TODO.
730          */
731         writeq(0, &bar0->tx_w_round_robin_0);
732         writeq(0, &bar0->tx_w_round_robin_1);
733         writeq(0, &bar0->tx_w_round_robin_2);
734         writeq(0, &bar0->tx_w_round_robin_3);
735         writeq(0, &bar0->tx_w_round_robin_4);
736
737         /* Disable Rx steering. Hard coding all packets be steered to
738          * Queue 0 for now. 
739          * TODO*/
740         if (rx_prio) {
741                 u64 def = 0x8000000000000000ULL, tmp;
742                 for (i = 0; i < MAX_RX_RINGS; i++) {
743                         tmp = (u64) (def >> (i % config->RxRingNum));
744                         val64 |= (u64) (tmp >> (i * 8));
745                 }
746                 writeq(val64, &bar0->rts_qos_steering);
747         } else {
748                 val64 = 0x8080808080808080ULL;
749                 writeq(val64, &bar0->rts_qos_steering);
750         }
751
752         /* UDP Fix */
753         val64 = 0;
754         for (i = 1; i < 8; i++)
755                 writeq(val64, &bar0->rts_frm_len_n[i]);
756
757         /* Set rts_frm_len register for fifo 0 */
758         writeq(MAC_RTS_FRM_LEN_SET(dev->mtu + 22),
759                &bar0->rts_frm_len_n[0]);
760
761         /* Enable statistics */
762         writeq(mac_control->stats_mem_phy, &bar0->stat_addr);
763         val64 = SET_UPDT_PERIOD(8) | STAT_CFG_STAT_RO | STAT_CFG_STAT_EN;
764         writeq(val64, &bar0->stat_cfg);
765
766         /* Initializing the sampling rate for the device to calculate the
767          * bandwidth utilization.
768          */
769         val64 = MAC_TX_LINK_UTIL_VAL(0x5) | MAC_RX_LINK_UTIL_VAL(0x5);
770         writeq(val64, &bar0->mac_link_util);
771
772
773         /* Initializing the Transmit and Receive Traffic Interrupt 
774          * Scheme.
775          */
776         /* TTI Initialization */
777         val64 = TTI_DATA1_MEM_TX_TIMER_VAL(0xFFF) |
778             TTI_DATA1_MEM_TX_URNG_A(0xA) | TTI_DATA1_MEM_TX_URNG_B(0x10) |
779             TTI_DATA1_MEM_TX_URNG_C(0x30) | TTI_DATA1_MEM_TX_TIMER_AC_EN;
780         writeq(val64, &bar0->tti_data1_mem);
781
782         val64 =
783             TTI_DATA2_MEM_TX_UFC_A(0x10) | TTI_DATA2_MEM_TX_UFC_B(0x20) |
784             TTI_DATA2_MEM_TX_UFC_C(0x40) | TTI_DATA2_MEM_TX_UFC_D(0x80);
785         writeq(val64, &bar0->tti_data2_mem);
786
787         val64 = TTI_CMD_MEM_WE | TTI_CMD_MEM_STROBE_NEW_CMD;
788         writeq(val64, &bar0->tti_command_mem);
789
790         /* Once the operation completes, the Strobe bit of the command
791          * register will be reset. We poll for this particular condition
792          * We wait for a maximum of 500ms for the operation to complete,
793          * if it's not complete by then we return error.
794          */
795         time = 0;
796         while (TRUE) {
797                 val64 = readq(&bar0->tti_command_mem);
798                 if (!(val64 & TTI_CMD_MEM_STROBE_NEW_CMD)) {
799                         break;
800                 }
801                 if (time > 10) {
802                         DBG_PRINT(ERR_DBG, "%s: TTI init Failed\n",
803                                   dev->name);
804                         return -1;
805                 }
806                 set_current_state(TASK_UNINTERRUPTIBLE);
807                 schedule_timeout(HZ / 20);
808                 time++;
809         }
810
811         /* RTI Initialization */
812         val64 = RTI_DATA1_MEM_RX_TIMER_VAL(0xFFF) |
813             RTI_DATA1_MEM_RX_URNG_A(0xA) | RTI_DATA1_MEM_RX_URNG_B(0x10) |
814             RTI_DATA1_MEM_RX_URNG_C(0x30) | RTI_DATA1_MEM_RX_TIMER_AC_EN;
815         writeq(val64, &bar0->rti_data1_mem);
816
817         val64 = RTI_DATA2_MEM_RX_UFC_A(0x1) | RTI_DATA2_MEM_RX_UFC_B(0x2) |
818             RTI_DATA2_MEM_RX_UFC_C(0x40) | RTI_DATA2_MEM_RX_UFC_D(0x80);
819         writeq(val64, &bar0->rti_data2_mem);
820
821         val64 = RTI_CMD_MEM_WE | RTI_CMD_MEM_STROBE_NEW_CMD;
822         writeq(val64, &bar0->rti_command_mem);
823
824         /* Once the operation completes, the Strobe bit of the command
825          * register will be reset. We poll for this particular condition
826          * We wait for a maximum of 500ms for the operation to complete,
827          * if it's not complete by then we return error.
828          */
829         time = 0;
830         while (TRUE) {
831                 val64 = readq(&bar0->rti_command_mem);
832                 if (!(val64 & TTI_CMD_MEM_STROBE_NEW_CMD)) {
833                         break;
834                 }
835                 if (time > 10) {
836                         DBG_PRINT(ERR_DBG, "%s: RTI init Failed\n",
837                                   dev->name);
838                         return -1;
839                 }
840                 time++;
841                 set_current_state(TASK_UNINTERRUPTIBLE);
842                 schedule_timeout(HZ / 20);
843         }
844
845         /* Initializing proper values as Pause threshold into all 
846          * the 8 Queues on Rx side.
847          */
848         writeq(0xffbbffbbffbbffbbULL, &bar0->mc_pause_thresh_q0q3);
849         writeq(0xffbbffbbffbbffbbULL, &bar0->mc_pause_thresh_q4q7);
850
851         /* Disable RMAC PAD STRIPPING */
852         add = (void *) &bar0->mac_cfg;
853         val64 = readq(&bar0->mac_cfg);
854         val64 &= ~(MAC_CFG_RMAC_STRIP_PAD);
855         writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
856         writel((u32) (val64), add);
857         writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
858         writel((u32) (val64 >> 32), (add + 4));
859         val64 = readq(&bar0->mac_cfg);
860
861         return SUCCESS;
862 }
863
864 /*  
865  *  Input Arguments: 
866  *  device private variable,
867  *  A mask indicating which Intr block must be modified and,
868  *  A flag indicating whether to enable or disable the Intrs.
869  *  Return Value: 
870  *  NONE.
871  *  Description: 
872  *  This function will either disable or enable the interrupts 
873  *  depending on the flag argument. The mask argument can be used to 
874  *  enable/disable any Intr block. 
875  */
876 static void en_dis_able_NicIntrs(struct s2io_nic *nic, u16 mask, int flag)
877 {
878         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
879         register u64 val64 = 0, temp64 = 0;
880
881         /*  Top level interrupt classification */
882         /*  PIC Interrupts */
883         if ((mask & (TX_PIC_INTR | RX_PIC_INTR))) {
884                 /*  Enable PIC Intrs in the general intr mask register */
885                 val64 = TXPIC_INT_M | PIC_RX_INT_M;
886                 if (flag == ENABLE_INTRS) {
887                         temp64 = readq(&bar0->general_int_mask);
888                         temp64 &= ~((u64) val64);
889                         writeq(temp64, &bar0->general_int_mask);
890                         /*  Disabled all PCIX, Flash, MDIO, IIC and GPIO
891                          *  interrupts for now. 
892                          * TODO */
893                         writeq(DISABLE_ALL_INTRS, &bar0->pic_int_mask);
894                         /*  No MSI Support is available presently, so TTI and
895                          * RTI interrupts are also disabled.
896                          */
897                 } else if (flag == DISABLE_INTRS) {
898                         /*  Disable PIC Intrs in the general intr mask register 
899                          */
900                         writeq(DISABLE_ALL_INTRS, &bar0->pic_int_mask);
901                         temp64 = readq(&bar0->general_int_mask);
902                         val64 |= temp64;
903                         writeq(val64, &bar0->general_int_mask);
904                 }
905         }
906
907         /*  DMA Interrupts */
908         /*  Enabling/Disabling Tx DMA interrupts */
909         if (mask & TX_DMA_INTR) {
910                 /*  Enable TxDMA Intrs in the general intr mask register */
911                 val64 = TXDMA_INT_M;
912                 if (flag == ENABLE_INTRS) {
913                         temp64 = readq(&bar0->general_int_mask);
914                         temp64 &= ~((u64) val64);
915                         writeq(temp64, &bar0->general_int_mask);
916                         /* Disable all interrupts other than PFC interrupt in 
917                          * DMA level.
918                          */
919                         val64 = DISABLE_ALL_INTRS & (~TXDMA_PFC_INT_M);
920                         writeq(val64, &bar0->txdma_int_mask);
921                         /* Enable only the MISC error 1 interrupt in PFC block 
922                          */
923                         val64 = DISABLE_ALL_INTRS & (~PFC_MISC_ERR_1);
924                         writeq(val64, &bar0->pfc_err_mask);
925                 } else if (flag == DISABLE_INTRS) {
926                         /*  Disable TxDMA Intrs in the general intr mask 
927                          *  register */
928                         writeq(DISABLE_ALL_INTRS, &bar0->txdma_int_mask);
929                         writeq(DISABLE_ALL_INTRS, &bar0->pfc_err_mask);
930                         temp64 = readq(&bar0->general_int_mask);
931                         val64 |= temp64;
932                         writeq(val64, &bar0->general_int_mask);
933                 }
934         }
935
936         /*  Enabling/Disabling Rx DMA interrupts */
937         if (mask & RX_DMA_INTR) {
938                 /*  Enable RxDMA Intrs in the general intr mask register */
939                 val64 = RXDMA_INT_M;
940                 if (flag == ENABLE_INTRS) {
941                         temp64 = readq(&bar0->general_int_mask);
942                         temp64 &= ~((u64) val64);
943                         writeq(temp64, &bar0->general_int_mask);
944                         /* All RxDMA block interrupts are disabled for now 
945                          * TODO */
946                         writeq(DISABLE_ALL_INTRS, &bar0->rxdma_int_mask);
947                 } else if (flag == DISABLE_INTRS) {
948                         /*  Disable RxDMA Intrs in the general intr mask 
949                          *  register */
950                         writeq(DISABLE_ALL_INTRS, &bar0->rxdma_int_mask);
951                         temp64 = readq(&bar0->general_int_mask);
952                         val64 |= temp64;
953                         writeq(val64, &bar0->general_int_mask);
954                 }
955         }
956
957         /*  MAC Interrupts */
958         /*  Enabling/Disabling MAC interrupts */
959         if (mask & (TX_MAC_INTR | RX_MAC_INTR)) {
960                 val64 = TXMAC_INT_M | RXMAC_INT_M;
961                 if (flag == ENABLE_INTRS) {
962                         temp64 = readq(&bar0->general_int_mask);
963                         temp64 &= ~((u64) val64);
964                         writeq(temp64, &bar0->general_int_mask);
965                         /* All MAC block error interrupts are disabled for now 
966                          * except the link status change interrupt.
967                          * TODO*/
968                         val64 = MAC_INT_STATUS_RMAC_INT;
969                         temp64 = readq(&bar0->mac_int_mask);
970                         temp64 &= ~((u64) val64);
971                         writeq(temp64, &bar0->mac_int_mask);
972
973                         val64 = readq(&bar0->mac_rmac_err_mask);
974                         val64 &= ~((u64) RMAC_LINK_STATE_CHANGE_INT);
975                         writeq(val64, &bar0->mac_rmac_err_mask);
976                 } else if (flag == DISABLE_INTRS) {
977                         /*  Disable MAC Intrs in the general intr mask register 
978                          */
979                         writeq(DISABLE_ALL_INTRS, &bar0->mac_int_mask);
980                         writeq(DISABLE_ALL_INTRS,
981                                &bar0->mac_rmac_err_mask);
982
983                         temp64 = readq(&bar0->general_int_mask);
984                         val64 |= temp64;
985                         writeq(val64, &bar0->general_int_mask);
986                 }
987         }
988
989         /*  XGXS Interrupts */
990         if (mask & (TX_XGXS_INTR | RX_XGXS_INTR)) {
991                 val64 = TXXGXS_INT_M | RXXGXS_INT_M;
992                 if (flag == ENABLE_INTRS) {
993                         temp64 = readq(&bar0->general_int_mask);
994                         temp64 &= ~((u64) val64);
995                         writeq(temp64, &bar0->general_int_mask);
996                         /* All XGXS block error interrupts are disabled for now
997                          *  TODO */
998                         writeq(DISABLE_ALL_INTRS, &bar0->xgxs_int_mask);
999                 } else if (flag == DISABLE_INTRS) {
1000                         /*  Disable MC Intrs in the general intr mask register 
1001                          */
1002                         writeq(DISABLE_ALL_INTRS, &bar0->xgxs_int_mask);
1003                         temp64 = readq(&bar0->general_int_mask);
1004                         val64 |= temp64;
1005                         writeq(val64, &bar0->general_int_mask);
1006                 }
1007         }
1008
1009         /*  Memory Controller(MC) interrupts */
1010         if (mask & MC_INTR) {
1011                 val64 = MC_INT_M;
1012                 if (flag == ENABLE_INTRS) {
1013                         temp64 = readq(&bar0->general_int_mask);
1014                         temp64 &= ~((u64) val64);
1015                         writeq(temp64, &bar0->general_int_mask);
1016                         /* All MC block error interrupts are disabled for now
1017                          * TODO */
1018                         writeq(DISABLE_ALL_INTRS, &bar0->mc_int_mask);
1019                 } else if (flag == DISABLE_INTRS) {
1020                         /*  Disable MC Intrs in the general intr mask register
1021                          */
1022                         writeq(DISABLE_ALL_INTRS, &bar0->mc_int_mask);
1023                         temp64 = readq(&bar0->general_int_mask);
1024                         val64 |= temp64;
1025                         writeq(val64, &bar0->general_int_mask);
1026                 }
1027         }
1028
1029
1030         /*  Tx traffic interrupts */
1031         if (mask & TX_TRAFFIC_INTR) {
1032                 val64 = TXTRAFFIC_INT_M;
1033                 if (flag == ENABLE_INTRS) {
1034                         temp64 = readq(&bar0->general_int_mask);
1035                         temp64 &= ~((u64) val64);
1036                         writeq(temp64, &bar0->general_int_mask);
1037                         /* Enable all the Tx side interrupts */
1038                         writeq(0x0, &bar0->tx_traffic_mask);    /* '0' Enables 
1039                                                                  * all 64 TX 
1040                                                                  * interrupt 
1041                                                                  * levels.
1042                                                                  */
1043                 } else if (flag == DISABLE_INTRS) {
1044                         /*  Disable Tx Traffic Intrs in the general intr mask 
1045                          *  register.
1046                          */
1047                         writeq(DISABLE_ALL_INTRS, &bar0->tx_traffic_mask);
1048                         temp64 = readq(&bar0->general_int_mask);
1049                         val64 |= temp64;
1050                         writeq(val64, &bar0->general_int_mask);
1051                 }
1052         }
1053
1054         /*  Rx traffic interrupts */
1055         if (mask & RX_TRAFFIC_INTR) {
1056                 val64 = RXTRAFFIC_INT_M;
1057                 if (flag == ENABLE_INTRS) {
1058                         temp64 = readq(&bar0->general_int_mask);
1059                         temp64 &= ~((u64) val64);
1060                         writeq(temp64, &bar0->general_int_mask);
1061                         writeq(0x0, &bar0->rx_traffic_mask);    /* '0' Enables 
1062                                                                  * all 8 RX 
1063                                                                  * interrupt 
1064                                                                  * levels.
1065                                                                  */
1066                 } else if (flag == DISABLE_INTRS) {
1067                         /*  Disable Rx Traffic Intrs in the general intr mask 
1068                          *  register.
1069                          */
1070                         writeq(DISABLE_ALL_INTRS, &bar0->rx_traffic_mask);
1071                         temp64 = readq(&bar0->general_int_mask);
1072                         val64 |= temp64;
1073                         writeq(val64, &bar0->general_int_mask);
1074                 }
1075         }
1076 }
1077
1078 /*  
1079  *  Input Arguments: 
1080  *   val64 - Value read from adapter status register.
1081  *   flag - indicates if the adapter enable bit was ever written once before.
1082  *  Return Value: 
1083  *   void.
1084  *  Description: 
1085  *   Returns whether the H/W is ready to go or not. Depending on whether 
1086  *   adapter enable bit was written or not the comparison differs and the 
1087  *   calling function passes the input argument flag to indicate this.
1088  */
1089 static int verify_xena_quiescence(u64 val64, int flag)
1090 {
1091         int ret = 0;
1092         u64 tmp64 = ~((u64) val64);
1093
1094         if (!
1095             (tmp64 &
1096              (ADAPTER_STATUS_TDMA_READY | ADAPTER_STATUS_RDMA_READY |
1097               ADAPTER_STATUS_PFC_READY | ADAPTER_STATUS_TMAC_BUF_EMPTY |
1098               ADAPTER_STATUS_PIC_QUIESCENT | ADAPTER_STATUS_MC_DRAM_READY |
1099               ADAPTER_STATUS_MC_QUEUES_READY | ADAPTER_STATUS_M_PLL_LOCK |
1100               ADAPTER_STATUS_P_PLL_LOCK))) {
1101                 if (flag == FALSE) {
1102                         if (!(val64 & ADAPTER_STATUS_RMAC_PCC_IDLE) &&
1103                             ((val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ==
1104                              ADAPTER_STATUS_RC_PRC_QUIESCENT)) {
1105
1106                                 ret = 1;
1107
1108                         }
1109                 } else {
1110                         if (((val64 & ADAPTER_STATUS_RMAC_PCC_IDLE) ==
1111                              ADAPTER_STATUS_RMAC_PCC_IDLE) &&
1112                             (!(val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ||
1113                              ((val64 & ADAPTER_STATUS_RC_PRC_QUIESCENT) ==
1114                               ADAPTER_STATUS_RC_PRC_QUIESCENT))) {
1115
1116                                 ret = 1;
1117
1118                         }
1119                 }
1120         }
1121
1122         return ret;
1123 }
1124
1125 /* 
1126  * New procedure to clear mac address reading  problems on Alpha platforms
1127  *
1128  */
1129 void FixMacAddress(nic_t * sp)
1130 {
1131         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
1132         u64 val64;
1133         int i = 0;
1134
1135         while (fix_mac[i] != END_SIGN) {
1136                 writeq(fix_mac[i++], &bar0->gpio_control);
1137                 val64 = readq(&bar0->gpio_control);
1138         }
1139 }
1140
1141 /*  
1142  *  Input Arguments: 
1143  *  device private variable.
1144  *  Return Value: 
1145  *  SUCCESS on success and -1 on failure.
1146  *  Description: 
1147  *  This function actually turns the device on. Before this 
1148  *  function is called, all Registers are configured from their reset states 
1149  *  and shared memory is allocated but the NIC is still quiescent. On 
1150  *  calling this function, the device interrupts are cleared and the NIC is
1151  *  literally switched on by writing into the adapter control register.
1152  */
1153 static int startNic(struct s2io_nic *nic)
1154 {
1155         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
1156         struct net_device *dev = nic->dev;
1157         register u64 val64 = 0;
1158         u16 interruptible, i;
1159         u16 subid;
1160         mac_info_t *mac_control;
1161         struct config_param *config;
1162
1163         mac_control = &nic->mac_control;
1164         config = &nic->config;
1165
1166         /*  PRC Initialization and configuration */
1167         for (i = 0; i < config->RxRingNum; i++) {
1168                 writeq((u64) nic->rx_blocks[i][0].block_dma_addr,
1169                        &bar0->prc_rxd0_n[i]);
1170
1171                 val64 = readq(&bar0->prc_ctrl_n[i]);
1172                 val64 |= PRC_CTRL_RC_ENABLED;
1173                 writeq(val64, &bar0->prc_ctrl_n[i]);
1174         }
1175
1176         /* Enabling MC-RLDRAM. After enabling the device, we timeout
1177          * for around 100ms, which is approximately the time required
1178          * for the device to be ready for operation.
1179          */
1180         val64 = readq(&bar0->mc_rldram_mrs);
1181         val64 |= MC_RLDRAM_QUEUE_SIZE_ENABLE | MC_RLDRAM_MRS_ENABLE;
1182         writeq(val64, &bar0->mc_rldram_mrs);
1183         val64 = readq(&bar0->mc_rldram_mrs);
1184
1185         set_current_state(TASK_UNINTERRUPTIBLE);
1186         schedule_timeout(HZ / 10);      /* Delay by around 100 ms. */
1187
1188         /* Enabling ECC Protection. */
1189         val64 = readq(&bar0->adapter_control);
1190         val64 &= ~ADAPTER_ECC_EN;
1191         writeq(val64, &bar0->adapter_control);
1192
1193         /* Clearing any possible Link state change interrupts that 
1194          * could have popped up just before Enabling the card.
1195          */
1196         val64 = readq(&bar0->mac_rmac_err_reg);
1197         if (val64)
1198                 writeq(val64, &bar0->mac_rmac_err_reg);
1199
1200         /* Verify if the device is ready to be enabled, if so enable 
1201          * it.
1202          */
1203         val64 = readq(&bar0->adapter_status);
1204         if (!verify_xena_quiescence(val64, nic->device_enabled_once)) {
1205                 DBG_PRINT(ERR_DBG, "%s: device is not ready, ", dev->name);
1206                 DBG_PRINT(ERR_DBG, "Adapter status reads: 0x%llx\n",
1207                           (unsigned long long) val64);
1208                 return FAILURE;
1209         }
1210
1211         /*  Enable select interrupts */
1212         interruptible = TX_TRAFFIC_INTR | RX_TRAFFIC_INTR | TX_MAC_INTR |
1213             RX_MAC_INTR;
1214         en_dis_able_NicIntrs(nic, interruptible, ENABLE_INTRS);
1215
1216         /* With some switches, link might be already up at this point.
1217          * Because of this weird behavior, when we enable laser, 
1218          * we may not get link. We need to handle this. We cannot 
1219          * figure out which switch is misbehaving. So we are forced to 
1220          * make a global change. 
1221          */
1222
1223         /* Enabling Laser. */
1224         val64 = readq(&bar0->adapter_control);
1225         val64 |= ADAPTER_EOI_TX_ON;
1226         writeq(val64, &bar0->adapter_control);
1227
1228         /* SXE-002: Initialize link and activity LED */
1229         subid = nic->pdev->subsystem_device;
1230         if ((subid & 0xFF) >= 0x07) {
1231                 val64 = readq(&bar0->gpio_control);
1232                 val64 |= 0x0000800000000000ULL;
1233                 writeq(val64, &bar0->gpio_control);
1234                 val64 = 0x0411040400000000ULL;
1235                 writeq(val64, (void *) ((u8 *) bar0 + 0x2700));
1236         }
1237
1238         /* 
1239          * Here we are performing soft reset on XGXS to 
1240          * force link down. Since link is already up, we will get
1241          * link state change interrupt after this reset
1242          */
1243         writeq(0x8007051500000000ULL, &bar0->dtx_control);
1244         val64 = readq(&bar0->dtx_control);
1245         writeq(0x80070515000000E0ULL, &bar0->dtx_control);
1246         val64 = readq(&bar0->dtx_control);
1247         writeq(0x80070515001F00E4ULL, &bar0->dtx_control);
1248         val64 = readq(&bar0->dtx_control);
1249
1250         return SUCCESS;
1251 }
1252
1253 /*  
1254  *  Input Arguments: 
1255  *   nic - device private variable.
1256  *  Return Value: 
1257  *   void.
1258  *  Description: 
1259  *   Free all queued Tx buffers.
1260  */
1261 void freeTxBuffers(struct s2io_nic *nic)
1262 {
1263         struct net_device *dev = nic->dev;
1264         struct sk_buff *skb;
1265         TxD_t *txdp;
1266         int i, j;
1267 #if DEBUG_ON
1268         int cnt = 0;
1269 #endif
1270         mac_info_t *mac_control;
1271         struct config_param *config;
1272
1273         mac_control = &nic->mac_control;
1274         config = &nic->config;
1275
1276         for (i = 0; i < config->TxFIFONum; i++) {
1277                 for (j = 0; j < config->TxCfg[i].FifoLen - 1; j++) {
1278                         txdp = mac_control->txdl_start[i] +
1279                             (config->MaxTxDs * j);
1280
1281                         if (!(txdp->Control_1 & TXD_LIST_OWN_XENA)) {
1282                                 /* If owned by host, ignore */
1283                                 continue;
1284                         }
1285                         skb =
1286                             (struct sk_buff *) ((unsigned long) txdp->
1287                                                 Host_Control);
1288                         if (skb == NULL) {
1289                                 DBG_PRINT(ERR_DBG, "%s: NULL skb ",
1290                                           dev->name);
1291                                 DBG_PRINT(ERR_DBG, "in Tx Int\n");
1292                                 return;
1293                         }
1294 #if DEBUG_ON
1295                         cnt++;
1296 #endif
1297                         dev_kfree_skb(skb);
1298                         memset(txdp, 0, sizeof(TxD_t));
1299                 }
1300 #if DEBUG_ON
1301                 DBG_PRINT(INTR_DBG,
1302                           "%s:forcibly freeing %d skbs on FIFO%d\n",
1303                           dev->name, cnt, i);
1304 #endif
1305         }
1306 }
1307
1308 /*  
1309  *  Input Arguments: 
1310  *   nic - device private variable.
1311  *  Return Value: 
1312  *   void.
1313  *  Description: 
1314  *   This function does exactly the opposite of what the startNic() 
1315  *   function does. This function is called to stop 
1316  *   the device.
1317  */
1318 static void stopNic(struct s2io_nic *nic)
1319 {
1320         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
1321         register u64 val64 = 0;
1322         u16 interruptible, i;
1323         mac_info_t *mac_control;
1324         struct config_param *config;
1325
1326         mac_control = &nic->mac_control;
1327         config = &nic->config;
1328
1329 /*  Disable all interrupts */
1330         interruptible = TX_TRAFFIC_INTR | RX_TRAFFIC_INTR | TX_MAC_INTR |
1331             RX_MAC_INTR;
1332         en_dis_able_NicIntrs(nic, interruptible, DISABLE_INTRS);
1333
1334 /*  Disable PRCs */
1335         for (i = 0; i < config->RxRingNum; i++) {
1336                 val64 = readq(&bar0->prc_ctrl_n[i]);
1337                 val64 &= ~((u64) PRC_CTRL_RC_ENABLED);
1338                 writeq(val64, &bar0->prc_ctrl_n[i]);
1339         }
1340 }
1341
1342 /*  
1343  *  Input Arguments: 
1344  *  device private variable
1345  *  Return Value: 
1346  *  SUCCESS on success or an appropriate -ve value on failure.
1347  *  Description: 
1348  *  The function allocates Rx side skbs and puts the physical
1349  *  address of these buffers into the RxD buffer pointers, so that the NIC
1350  *  can DMA the received frame into these locations.
1351  *  The NIC supports 3 receive modes, viz
1352  *  1. single buffer,
1353  *  2. three buffer and
1354  *  3. Five buffer modes.
1355  *  Each mode defines how many fragments the received frame will be split 
1356  *  up into by the NIC. The frame is split into L3 header, L4 Header, 
1357  *  L4 payload in three buffer mode and in 5 buffer mode, L4 payload itself 
1358  *  is split into 3 fragments. As of now only single buffer mode is supported.
1359  */
1360 int fill_rx_buffers(struct s2io_nic *nic, int ring_no)
1361 {
1362         struct net_device *dev = nic->dev;
1363         struct sk_buff *skb;
1364         RxD_t *rxdp;
1365         int off, off1, size, block_no, block_no1;
1366         int offset, offset1;
1367         u32 alloc_tab = 0;
1368         u32 alloc_cnt = nic->pkt_cnt[ring_no] -
1369             atomic_read(&nic->rx_bufs_left[ring_no]);
1370         mac_info_t *mac_control;
1371         struct config_param *config;
1372
1373         mac_control = &nic->mac_control;
1374         config = &nic->config;
1375
1376         if (frame_len[ring_no]) {
1377                 if (frame_len[ring_no] > dev->mtu)
1378                         dev->mtu = frame_len[ring_no];
1379                 size = frame_len[ring_no] + HEADER_ETHERNET_II_802_3_SIZE +
1380                     HEADER_802_2_SIZE + HEADER_SNAP_SIZE;
1381         } else {
1382                 size = dev->mtu + HEADER_ETHERNET_II_802_3_SIZE +
1383                     HEADER_802_2_SIZE + HEADER_SNAP_SIZE;
1384         }
1385
1386         while (alloc_tab < alloc_cnt) {
1387                 block_no = mac_control->rx_curr_put_info[ring_no].
1388                     block_index;
1389                 block_no1 = mac_control->rx_curr_get_info[ring_no].
1390                     block_index;
1391                 off = mac_control->rx_curr_put_info[ring_no].offset;
1392                 off1 = mac_control->rx_curr_get_info[ring_no].offset;
1393                 offset = block_no * (MAX_RXDS_PER_BLOCK + 1) + off;
1394                 offset1 = block_no1 * (MAX_RXDS_PER_BLOCK + 1) + off1;
1395
1396                 rxdp = nic->rx_blocks[ring_no][block_no].
1397                     block_virt_addr + off;
1398                 if ((offset == offset1) && (rxdp->Host_Control)) {
1399                         DBG_PRINT(INTR_DBG, "%s: Get and Put", dev->name);
1400                         DBG_PRINT(INTR_DBG, " info equated\n");
1401                         goto end;
1402                 }
1403
1404                 if (rxdp->Control_1 == END_OF_BLOCK) {
1405                         mac_control->rx_curr_put_info[ring_no].
1406                             block_index++;
1407                         mac_control->rx_curr_put_info[ring_no].
1408                             block_index %= nic->block_count[ring_no];
1409                         block_no = mac_control->rx_curr_put_info
1410                             [ring_no].block_index;
1411                         off++;
1412                         off %= (MAX_RXDS_PER_BLOCK + 1);
1413                         mac_control->rx_curr_put_info[ring_no].offset =
1414                             off;
1415                         /*rxdp = nic->rx_blocks[ring_no][block_no].
1416                            block_virt_addr + off; */
1417                         rxdp = (RxD_t *) ((unsigned long) rxdp->Control_2);
1418                         DBG_PRINT(INTR_DBG, "%s: Next block at: %p\n",
1419                                   dev->name, rxdp);
1420                 }
1421
1422                 if (rxdp->Control_1 & RXD_OWN_XENA) {
1423                         mac_control->rx_curr_put_info[ring_no].
1424                             offset = off;
1425                         goto end;
1426                 }
1427
1428                 skb = dev_alloc_skb(size + HEADER_ALIGN_LAYER_3);
1429                 if (!skb) {
1430                         DBG_PRINT(ERR_DBG, "%s: Out of ", dev->name);
1431                         DBG_PRINT(ERR_DBG, "memory to allocate SKBs\n");
1432                         return -ENOMEM;
1433                 }
1434                 skb_reserve(skb, HEADER_ALIGN_LAYER_3);
1435                 memset(rxdp, 0, sizeof(RxD_t));
1436                 rxdp->Buffer0_ptr = pci_map_single
1437                     (nic->pdev, skb->data, size, PCI_DMA_FROMDEVICE);
1438                 rxdp->Control_2 &= (~MASK_BUFFER0_SIZE);
1439                 rxdp->Control_2 |= SET_BUFFER0_SIZE(size);
1440                 rxdp->Host_Control = (unsigned long) (skb);
1441                 rxdp->Control_1 |= RXD_OWN_XENA;
1442                 off++;
1443                 off %= (MAX_RXDS_PER_BLOCK + 1);
1444                 mac_control->rx_curr_put_info[ring_no].offset = off;
1445                 atomic_inc(&nic->rx_bufs_left[ring_no]);
1446                 alloc_tab++;
1447         }
1448
1449       end:
1450         return SUCCESS;
1451 }
1452
1453 /*  
1454  *  Input Arguments: 
1455  *  device private variable.
1456  *  Return Value: 
1457  *  NONE.
1458  *  Description: 
1459  *  This function will free all Rx buffers allocated by host.
1460  */
1461 static void freeRxBuffers(struct s2io_nic *sp)
1462 {
1463         struct net_device *dev = sp->dev;
1464         int i, j, blk = 0, off, buf_cnt = 0;
1465         RxD_t *rxdp;
1466         struct sk_buff *skb;
1467         mac_info_t *mac_control;
1468         struct config_param *config;
1469
1470         mac_control = &sp->mac_control;
1471         config = &sp->config;
1472
1473         for (i = 0; i < config->RxRingNum; i++) {
1474                 for (j = 0, blk = 0; j < config->RxCfg[i].NumRxd; j++) {
1475                         off = j % (MAX_RXDS_PER_BLOCK + 1);
1476                         rxdp = sp->rx_blocks[i][blk].block_virt_addr + off;
1477
1478                         if (rxdp->Control_1 == END_OF_BLOCK) {
1479                                 rxdp =
1480                                     (RxD_t *) ((unsigned long) rxdp->
1481                                                Control_2);
1482                                 j++;
1483                                 blk++;
1484                         }
1485
1486                         skb =
1487                             (struct sk_buff *) ((unsigned long) rxdp->
1488                                                 Host_Control);
1489                         if (skb) {
1490                                 pci_unmap_single(sp->pdev, (dma_addr_t)
1491                                                  rxdp->Buffer0_ptr,
1492                                                  dev->mtu +
1493                                                  HEADER_ETHERNET_II_802_3_SIZE
1494                                                  + HEADER_802_2_SIZE +
1495                                                  HEADER_SNAP_SIZE,
1496                                                  PCI_DMA_FROMDEVICE);
1497                                 dev_kfree_skb(skb);
1498                                 atomic_dec(&sp->rx_bufs_left[i]);
1499                                 buf_cnt++;
1500                         }
1501                         memset(rxdp, 0, sizeof(RxD_t));
1502                 }
1503                 mac_control->rx_curr_put_info[i].block_index = 0;
1504                 mac_control->rx_curr_get_info[i].block_index = 0;
1505                 mac_control->rx_curr_put_info[i].offset = 0;
1506                 mac_control->rx_curr_get_info[i].offset = 0;
1507                 atomic_set(&sp->rx_bufs_left[i], 0);
1508                 DBG_PRINT(INIT_DBG, "%s:Freed 0x%x Rx Buffers on ring%d\n",
1509                           dev->name, buf_cnt, i);
1510         }
1511 }
1512
1513 /*
1514  *  Input Argument: 
1515  *   dev - pointer to the device structure.
1516  *   budget - The number of packets that were budgeted to be processed during
1517  *   one pass through the 'Poll" function.
1518  *  Return value:
1519  *   0 on success and 1 if there are No Rx packets to be processed.
1520  *  Description:
1521  *   Comes into picture only if NAPI support has been incorporated. It does
1522  *   the same thing that rxIntrHandler does, but not in a interrupt context
1523  *   also It will process only a given number of packets.
1524  */
1525 #ifdef CONFIG_S2IO_NAPI
1526 static int s2io_poll(struct net_device *dev, int *budget)
1527 {
1528         nic_t *nic = dev->priv;
1529         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
1530         int pkts_to_process = *budget, pkt_cnt = 0;
1531         register u64 val64 = 0;
1532         rx_curr_get_info_t offset_info;
1533         int i, block_no;
1534         u16 val16, cksum;
1535         struct sk_buff *skb;
1536         RxD_t *rxdp;
1537         mac_info_t *mac_control;
1538         struct config_param *config;
1539
1540         mac_control = &nic->mac_control;
1541         config = &nic->config;
1542
1543         if (pkts_to_process > dev->quota)
1544                 pkts_to_process = dev->quota;
1545
1546         val64 = readq(&bar0->rx_traffic_int);
1547         writeq(val64, &bar0->rx_traffic_int);
1548
1549         for (i = 0; i < config->RxRingNum; i++) {
1550                 if (--pkts_to_process < 0) {
1551                         goto no_rx;
1552                 }
1553                 offset_info = mac_control->rx_curr_get_info[i];
1554                 block_no = offset_info.block_index;
1555                 rxdp = nic->rx_blocks[i][block_no].block_virt_addr +
1556                     offset_info.offset;
1557                 while (!(rxdp->Control_1 & RXD_OWN_XENA)) {
1558                         if (rxdp->Control_1 == END_OF_BLOCK) {
1559                                 rxdp =
1560                                     (RxD_t *) ((unsigned long) rxdp->
1561                                                Control_2);
1562                                 offset_info.offset++;
1563                                 offset_info.offset %=
1564                                     (MAX_RXDS_PER_BLOCK + 1);
1565                                 block_no++;
1566                                 block_no %= nic->block_count[i];
1567                                 mac_control->rx_curr_get_info[i].
1568                                     offset = offset_info.offset;
1569                                 mac_control->rx_curr_get_info[i].
1570                                     block_index = block_no;
1571                                 continue;
1572                         }
1573                         skb =
1574                             (struct sk_buff *) ((unsigned long) rxdp->
1575                                                 Host_Control);
1576                         if (skb == NULL) {
1577                                 DBG_PRINT(ERR_DBG, "%s: The skb is ",
1578                                           dev->name);
1579                                 DBG_PRINT(ERR_DBG, "Null in Rx Intr\n");
1580                                 return 0;
1581                         }
1582                         val64 = RXD_GET_BUFFER0_SIZE(rxdp->Control_2);
1583                         val16 = (u16) (val64 >> 48);
1584                         cksum = RXD_GET_L4_CKSUM(rxdp->Control_1);
1585                         pci_unmap_single(nic->pdev, (dma_addr_t)
1586                                          rxdp->Buffer0_ptr,
1587                                          dev->mtu +
1588                                          HEADER_ETHERNET_II_802_3_SIZE +
1589                                          HEADER_802_2_SIZE +
1590                                          HEADER_SNAP_SIZE,
1591                                          PCI_DMA_FROMDEVICE);
1592                         rxOsmHandler(nic, val16, rxdp, i);
1593                         pkt_cnt++;
1594                         offset_info.offset++;
1595                         offset_info.offset %= (MAX_RXDS_PER_BLOCK + 1);
1596                         rxdp =
1597                             nic->rx_blocks[i][block_no].block_virt_addr +
1598                             offset_info.offset;
1599                         mac_control->rx_curr_get_info[i].offset =
1600                             offset_info.offset;
1601                 }
1602         }
1603         if (!pkt_cnt)
1604                 pkt_cnt = 1;
1605
1606         for (i = 0; i < config->RxRingNum; i++)
1607                 fill_rx_buffers(nic, i);
1608
1609         dev->quota -= pkt_cnt;
1610         *budget -= pkt_cnt;
1611         netif_rx_complete(dev);
1612
1613 /* Re enable the Rx interrupts. */
1614         en_dis_able_NicIntrs(nic, RX_TRAFFIC_INTR, ENABLE_INTRS);
1615         return 0;
1616
1617       no_rx:
1618         for (i = 0; i < config->RxRingNum; i++)
1619                 fill_rx_buffers(nic, i);
1620         dev->quota -= pkt_cnt;
1621         *budget -= pkt_cnt;
1622         return 1;
1623 }
1624 #else
1625 /*  
1626  *  Input Arguments: 
1627  *  device private variable.
1628  *  Return Value: 
1629  *  NONE.
1630  *  Description: 
1631  * If the interrupt is because of a received frame or if the 
1632  *  receive ring contains fresh as yet un-processed frames, this function is
1633  *  called. It picks out the RxD at which place the last Rx processing had 
1634  *  stopped and sends the skb to the OSM's Rx handler and then increments 
1635  *  the offset.
1636  */
1637 static void rxIntrHandler(struct s2io_nic *nic)
1638 {
1639         struct net_device *dev = (struct net_device *) nic->dev;
1640         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
1641         rx_curr_get_info_t offset_info;
1642         RxD_t *rxdp;
1643         struct sk_buff *skb;
1644         u16 val16, cksum;
1645         register u64 val64 = 0;
1646         int i, block_no;
1647         mac_info_t *mac_control;
1648         struct config_param *config;
1649
1650         mac_control = &nic->mac_control;
1651         config = &nic->config;
1652
1653 #if DEBUG_ON
1654         nic->rxint_cnt++;
1655 #endif
1656
1657 /* rx_traffic_int reg is an R1 register, hence we read and write back 
1658  * the samevalue in the register to clear it.
1659  */
1660         val64 = readq(&bar0->rx_traffic_int);
1661         writeq(val64, &bar0->rx_traffic_int);
1662
1663         for (i = 0; i < config->RxRingNum; i++) {
1664                 offset_info = mac_control->rx_curr_get_info[i];
1665                 block_no = offset_info.block_index;
1666                 rxdp = nic->rx_blocks[i][block_no].block_virt_addr +
1667                     offset_info.offset;
1668                 while (!(rxdp->Control_1 & RXD_OWN_XENA)) {
1669                         if (rxdp->Control_1 == END_OF_BLOCK) {
1670                                 rxdp = (RxD_t *) ((unsigned long)
1671                                                   rxdp->Control_2);
1672                                 offset_info.offset++;
1673                                 offset_info.offset %=
1674                                     (MAX_RXDS_PER_BLOCK + 1);
1675                                 block_no++;
1676                                 block_no %= nic->block_count[i];
1677                                 mac_control->rx_curr_get_info[i].
1678                                     offset = offset_info.offset;
1679                                 mac_control->rx_curr_get_info[i].
1680                                     block_index = block_no;
1681                                 continue;
1682                         }
1683                         skb = (struct sk_buff *) ((unsigned long)
1684                                                   rxdp->Host_Control);
1685                         if (skb == NULL) {
1686                                 DBG_PRINT(ERR_DBG, "%s: The skb is ",
1687                                           dev->name);
1688                                 DBG_PRINT(ERR_DBG, "Null in Rx Intr\n");
1689                                 return;
1690                         }
1691                         val64 = RXD_GET_BUFFER0_SIZE(rxdp->Control_2);
1692                         val16 = (u16) (val64 >> 48);
1693                         cksum = RXD_GET_L4_CKSUM(rxdp->Control_1);
1694                         pci_unmap_single(nic->pdev, (dma_addr_t)
1695                                          rxdp->Buffer0_ptr,
1696                                          dev->mtu +
1697                                          HEADER_ETHERNET_II_802_3_SIZE +
1698                                          HEADER_802_2_SIZE +
1699                                          HEADER_SNAP_SIZE,
1700                                          PCI_DMA_FROMDEVICE);
1701                         rxOsmHandler(nic, val16, rxdp, i);
1702                         offset_info.offset++;
1703                         offset_info.offset %= (MAX_RXDS_PER_BLOCK + 1);
1704                         rxdp =
1705                             nic->rx_blocks[i][block_no].block_virt_addr +
1706                             offset_info.offset;
1707                         mac_control->rx_curr_get_info[i].offset =
1708                             offset_info.offset;
1709                 }
1710         }
1711 }
1712 #endif
1713
1714 /*  
1715  *  Input Arguments: 
1716  *  device private variable
1717  *  Return Value: 
1718  *  NONE
1719  *  Description: 
1720  *  If an interrupt was raised to indicate DMA complete of the 
1721  *  Tx packet, this function is called. It identifies the last TxD whose buffer
1722  *  was freed and frees all skbs whose data have already DMA'ed into the NICs
1723  *  internal memory.
1724  */
1725 static void txIntrHandler(struct s2io_nic *nic)
1726 {
1727         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
1728         struct net_device *dev = (struct net_device *) nic->dev;
1729         tx_curr_get_info_t offset_info, offset_info1;
1730         struct sk_buff *skb;
1731         TxD_t *txdlp;
1732         register u64 val64 = 0;
1733         int i;
1734         u16 j, frg_cnt;
1735         mac_info_t *mac_control;
1736         struct config_param *config;
1737 #if DEBUG_ON
1738         int cnt = 0;
1739         nic->txint_cnt++;
1740 #endif
1741
1742         mac_control = &nic->mac_control;
1743         config = &nic->config;
1744
1745         /* tx_traffic_int reg is an R1 register, hence we read and write 
1746          * back the samevalue in the register to clear it.
1747          */
1748         val64 = readq(&bar0->tx_traffic_int);
1749         writeq(val64, &bar0->tx_traffic_int);
1750
1751         for (i = 0; i < config->TxFIFONum; i++) {
1752                 offset_info = mac_control->tx_curr_get_info[i];
1753                 offset_info1 = mac_control->tx_curr_put_info[i];
1754                 txdlp = mac_control->txdl_start[i] +
1755                     (config->MaxTxDs * offset_info.offset);
1756                 while ((!(txdlp->Control_1 & TXD_LIST_OWN_XENA)) &&
1757                        (offset_info.offset != offset_info1.offset) &&
1758                        (txdlp->Host_Control)) {
1759                         /* Check for TxD errors */
1760                         if (txdlp->Control_1 & TXD_T_CODE) {
1761                                 unsigned long long err;
1762                                 err = txdlp->Control_1 & TXD_T_CODE;
1763                                 DBG_PRINT(ERR_DBG, "***TxD error %llx\n",
1764                                           err);
1765                         }
1766
1767                         skb = (struct sk_buff *) ((unsigned long)
1768                                                   txdlp->Host_Control);
1769                         if (skb == NULL) {
1770                                 DBG_PRINT(ERR_DBG, "%s: Null skb ",
1771                                           dev->name);
1772                                 DBG_PRINT(ERR_DBG, "in Tx Free Intr\n");
1773                                 return;
1774                         }
1775                         nic->tx_pkt_count++;
1776
1777                         frg_cnt = skb_shinfo(skb)->nr_frags;
1778
1779                         /*  For unfragmented skb */
1780                         pci_unmap_single(nic->pdev, (dma_addr_t)
1781                                          txdlp->Buffer_Pointer,
1782                                          skb->len - skb->data_len,
1783                                          PCI_DMA_TODEVICE);
1784                         if (frg_cnt) {
1785                                 TxD_t *temp = txdlp;
1786                                 txdlp++;
1787                                 for (j = 0; j < frg_cnt; j++, txdlp++) {
1788                                         skb_frag_t *frag =
1789                                             &skb_shinfo(skb)->frags[j];
1790                                         pci_unmap_page(nic->pdev,
1791                                                        (dma_addr_t)
1792                                                        txdlp->
1793                                                        Buffer_Pointer,
1794                                                        frag->size,
1795                                                        PCI_DMA_TODEVICE);
1796                                 }
1797                                 txdlp = temp;
1798                         }
1799                         memset(txdlp, 0,
1800                                (sizeof(TxD_t) * config->MaxTxDs));
1801
1802                         /* Updating the statistics block */
1803                         nic->stats.tx_packets++;
1804                         nic->stats.tx_bytes += skb->len;
1805 #if DEBUG_ON
1806                         nic->txpkt_bytes += skb->len;
1807                         cnt++;
1808 #endif
1809                         dev_kfree_skb_irq(skb);
1810
1811                         offset_info.offset++;
1812                         offset_info.offset %= offset_info.fifo_len + 1;
1813                         txdlp = mac_control->txdl_start[i] +
1814                             (config->MaxTxDs * offset_info.offset);
1815                         mac_control->tx_curr_get_info[i].offset =
1816                             offset_info.offset;
1817                 }
1818 #if DEBUG_ON
1819                 DBG_PRINT(INTR_DBG, "%s: freed %d Tx Pkts\n", dev->name,
1820                           cnt);
1821 #endif
1822         }
1823
1824         spin_lock(&nic->tx_lock);
1825         if (netif_queue_stopped(dev))
1826                 netif_wake_queue(dev);
1827         spin_unlock(&nic->tx_lock);
1828 }
1829
1830 /*  
1831  *  Input Arguments: 
1832  *  device private variable
1833  *  Return Value: 
1834  *  NONE
1835  *  Description: 
1836  *  If the interrupt was neither because of Rx packet or Tx 
1837  *  complete, this function is called. If the interrupt was to indicate a loss
1838  *  of link, the OSM link status handler is invoked for any other alarm 
1839  *  interrupt the block that raised the interrupt is displayed and a H/W reset 
1840  *  is issued.
1841  */
1842 static void alarmIntrHandler(struct s2io_nic *nic)
1843 {
1844         struct net_device *dev = (struct net_device *) nic->dev;
1845         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
1846         register u64 val64 = 0, err_reg = 0;
1847
1848
1849         /* Handling link status change error Intr */
1850         err_reg = readq(&bar0->mac_rmac_err_reg);
1851         if (err_reg & RMAC_LINK_STATE_CHANGE_INT) {
1852                 schedule_work(&nic->set_link_task);
1853         }
1854
1855         /* Handling SERR errors by stopping device Xmit queue and forcing 
1856          * a H/W reset.
1857          */
1858         val64 = readq(&bar0->serr_source);
1859         if (val64 & SERR_SOURCE_ANY) {
1860                 DBG_PRINT(ERR_DBG, "%s: Device indicates ", dev->name);
1861                 DBG_PRINT(ERR_DBG, "serious error!!\n");
1862                 netif_stop_queue(dev);
1863         }
1864 /* Other type of interrupts are not being handled now,  TODO*/
1865 }
1866
1867 /*
1868  *  Input Argument: 
1869  *  sp - private member of the device structure, which is a pointer to the 
1870  *      s2io_nic structure.
1871  *  Return value:
1872  *   SUCCESS on success and FAILURE on failure.
1873  *  Description:
1874  *   Function that waits for a command to Write into RMAC ADDR DATA registers 
1875  *   to be completed and returns either success or error depending on whether 
1876  *   the command was complete or not. 
1877  */
1878 int waitForCmdComplete(nic_t * sp)
1879 {
1880         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
1881         int ret = FAILURE, cnt = 0;
1882         u64 val64;
1883
1884         while (TRUE) {
1885                 val64 =
1886                     RMAC_ADDR_CMD_MEM_RD | RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD
1887                     | RMAC_ADDR_CMD_MEM_OFFSET(0);
1888                 writeq(val64, &bar0->rmac_addr_cmd_mem);
1889                 val64 = readq(&bar0->rmac_addr_cmd_mem);
1890                 if (!val64) {
1891                         ret = SUCCESS;
1892                         break;
1893                 }
1894                 set_current_state(TASK_UNINTERRUPTIBLE);
1895                 schedule_timeout(HZ / 20);
1896                 if (cnt++ > 10)
1897                         break;
1898         }
1899
1900         return ret;
1901 }
1902
1903 /*
1904  *  Input Argument: 
1905  *  sp - private member of the device structure, which is a pointer to the 
1906  *      s2io_nic structure.
1907  *  Return value:
1908  *   void.
1909  *  Description:
1910  *   Function to Reset the card. This function then also restores the previously
1911  *   saved PCI configuration space registers as the card reset also resets the
1912  *   Configration space.
1913  */
1914 void s2io_reset(nic_t * sp)
1915 {
1916         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
1917         u64 val64;
1918         u16 subid;
1919
1920         val64 = SW_RESET_ALL;
1921         writeq(val64, &bar0->sw_reset);
1922
1923         /* At this stage, if the PCI write is indeed completed, the 
1924          * card is reset and so is the PCI Config space of the device. 
1925          * So a read cannot be issued at this stage on any of the 
1926          * registers to ensure the write into "sw_reset" register
1927          * has gone through.
1928          * Question: Is there any system call that will explicitly force
1929          * all the write commands still pending on the bus to be pushed
1930          * through?
1931          * As of now I'am just giving a 250ms delay and hoping that the
1932          * PCI write to sw_reset register is done by this time.
1933          */
1934         set_current_state(TASK_UNINTERRUPTIBLE);
1935         schedule_timeout(HZ / 4);
1936
1937         /* Restore the PCI state saved during initializarion. */
1938         pci_restore_state(sp->pdev, sp->config_space);
1939         s2io_init_pci(sp);
1940
1941         set_current_state(TASK_UNINTERRUPTIBLE);
1942         schedule_timeout(HZ / 4);
1943
1944         /* SXE-002: Configure link and activity LED to turn it off */
1945         subid = sp->pdev->subsystem_device;
1946         if ((subid & 0xFF) >= 0x07) {
1947                 val64 = readq(&bar0->gpio_control);
1948                 val64 |= 0x0000800000000000ULL;
1949                 writeq(val64, &bar0->gpio_control);
1950                 val64 = 0x0411040400000000ULL;
1951                 writeq(val64, (void *) ((u8 *) bar0 + 0x2700));
1952         }
1953
1954         sp->device_enabled_once = FALSE;
1955 }
1956
1957 /*
1958  *  Input Argument: 
1959  *  sp - private member of the device structure, which is a pointer to the 
1960  *      s2io_nic structure.
1961  *  Return value:
1962  *  SUCCESS on success and FAILURE on failure.
1963  *  Description:
1964  * Function to set the swapper control on the card correctly depending on the
1965  * 'endianness' of the system.
1966  */
1967 int s2io_set_swapper(nic_t * sp)
1968 {
1969         struct net_device *dev = sp->dev;
1970         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
1971         u64 val64;
1972
1973 /*  Set proper endian settings and verify the same by reading the PIF 
1974  *  Feed-back register.
1975  */
1976 #ifdef  __BIG_ENDIAN
1977 /* The device by default set to a big endian format, so a big endian 
1978  * driver need not set anything.
1979  */
1980         writeq(0xffffffffffffffffULL, &bar0->swapper_ctrl);
1981         val64 = (SWAPPER_CTRL_PIF_R_FE |
1982                  SWAPPER_CTRL_PIF_R_SE |
1983                  SWAPPER_CTRL_PIF_W_FE |
1984                  SWAPPER_CTRL_PIF_W_SE |
1985                  SWAPPER_CTRL_TXP_FE |
1986                  SWAPPER_CTRL_TXP_SE |
1987                  SWAPPER_CTRL_TXD_R_FE |
1988                  SWAPPER_CTRL_TXD_W_FE |
1989                  SWAPPER_CTRL_TXF_R_FE |
1990                  SWAPPER_CTRL_RXD_R_FE |
1991                  SWAPPER_CTRL_RXD_W_FE |
1992                  SWAPPER_CTRL_RXF_W_FE |
1993                  SWAPPER_CTRL_XMSI_FE |
1994                  SWAPPER_CTRL_XMSI_SE |
1995                  SWAPPER_CTRL_STATS_FE | SWAPPER_CTRL_STATS_SE);
1996         writeq(val64, &bar0->swapper_ctrl);
1997 #else
1998 /* Initially we enable all bits to make it accessible by the driver,
1999  * then we selectively enable only those bits that we want to set.
2000  */
2001         writeq(0xffffffffffffffffULL, &bar0->swapper_ctrl);
2002         val64 = (SWAPPER_CTRL_PIF_R_FE |
2003                  SWAPPER_CTRL_PIF_R_SE |
2004                  SWAPPER_CTRL_PIF_W_FE |
2005                  SWAPPER_CTRL_PIF_W_SE |
2006                  SWAPPER_CTRL_TXP_FE |
2007                  SWAPPER_CTRL_TXP_SE |
2008                  SWAPPER_CTRL_TXD_R_FE |
2009                  SWAPPER_CTRL_TXD_R_SE |
2010                  SWAPPER_CTRL_TXD_W_FE |
2011                  SWAPPER_CTRL_TXD_W_SE |
2012                  SWAPPER_CTRL_TXF_R_FE |
2013                  SWAPPER_CTRL_RXD_R_FE |
2014                  SWAPPER_CTRL_RXD_R_SE |
2015                  SWAPPER_CTRL_RXD_W_FE |
2016                  SWAPPER_CTRL_RXD_W_SE |
2017                  SWAPPER_CTRL_RXF_W_FE |
2018                  SWAPPER_CTRL_XMSI_FE |
2019                  SWAPPER_CTRL_XMSI_SE |
2020                  SWAPPER_CTRL_STATS_FE | SWAPPER_CTRL_STATS_SE);
2021         writeq(val64, &bar0->swapper_ctrl);
2022 #endif
2023
2024 /*  Verifying if endian settings are accurate by reading a feedback
2025  *  register.
2026  */
2027         val64 = readq(&bar0->pif_rd_swapper_fb);
2028         if (val64 != 0x0123456789ABCDEFULL) {
2029                 /* Endian settings are incorrect, calls for another dekko. */
2030                 DBG_PRINT(ERR_DBG, "%s: Endian settings are wrong, ",
2031                           dev->name);
2032                 DBG_PRINT(ERR_DBG, "feedback read %llx\n",
2033                           (unsigned long long) val64);
2034                 return FAILURE;
2035         }
2036
2037         return SUCCESS;
2038 }
2039
2040 /* ********************************************************* *
2041  * Functions defined below concern the OS part of the driver *
2042  * ********************************************************* */
2043
2044 /*
2045  *  Input Argument: 
2046  *  dev - pointer to the device structure.
2047  *  Return value:
2048  *  '0' on success and an appropriate (-)ve integer as defined in errno.h
2049  *   file on failure.
2050  *  Description:
2051  *  This function is the open entry point of the driver. It mainly calls a
2052  *  function to allocate Rx buffers and inserts them into the buffer
2053  *  descriptors and then enables the Rx part of the NIC. 
2054  */
2055 int s2io_open(struct net_device *dev)
2056 {
2057         nic_t *sp = dev->priv;
2058         int i, ret = 0, err = 0;
2059         mac_info_t *mac_control;
2060         struct config_param *config;
2061
2062
2063 /* Make sure you have link off by default every time Nic is initialized*/
2064         netif_carrier_off(dev);
2065         sp->last_link_state = LINK_DOWN;
2066
2067 /*  Initialize the H/W I/O registers */
2068         if (initNic(sp) != 0) {
2069                 DBG_PRINT(ERR_DBG, "%s: H/W initialization failed\n",
2070                           dev->name);
2071                 return -ENODEV;
2072         }
2073
2074 /*  After proper initialization of H/W, register ISR */
2075         err =
2076             request_irq((int) sp->irq, s2io_isr, SA_SHIRQ, sp->name, dev);
2077         if (err) {
2078                 s2io_reset(sp);
2079                 DBG_PRINT(ERR_DBG, "%s: ISR registration failed\n",
2080                           dev->name);
2081                 return err;
2082         }
2083         if (s2io_set_mac_addr(dev, dev->dev_addr) == FAILURE) {
2084                 DBG_PRINT(ERR_DBG, "Set Mac Address Failed\n");
2085                 s2io_reset(sp);
2086                 return -ENODEV;
2087         }
2088
2089
2090 /*  Setting its receive mode */
2091         s2io_set_multicast(dev);
2092
2093 /*  Initializing the Rx buffers. For now we are considering only 1 Rx ring
2094  * and initializing buffers into 1016 RxDs or 8 Rx blocks
2095  */
2096         mac_control = &sp->mac_control;
2097         config = &sp->config;
2098
2099         for (i = 0; i < config->RxRingNum; i++) {
2100                 if ((ret = fill_rx_buffers(sp, i))) {
2101                         DBG_PRINT(ERR_DBG, "%s: Out of memory in Open\n",
2102                                   dev->name);
2103                         s2io_reset(sp);
2104                         free_irq(dev->irq, dev);
2105                         freeRxBuffers(sp);
2106                         return -ENOMEM;
2107                 }
2108                 DBG_PRINT(INFO_DBG, "Buf in ring:%d is %d:\n", i,
2109                           atomic_read(&sp->rx_bufs_left[i]));
2110         }
2111
2112 /*  Enable tasklet for the device */
2113         tasklet_init(&sp->task, s2io_tasklet, (unsigned long) dev);
2114
2115 /*  Enable Rx Traffic and interrupts on the NIC */
2116         if (startNic(sp)) {
2117                 DBG_PRINT(ERR_DBG, "%s: Starting NIC failed\n", dev->name);
2118                 tasklet_kill(&sp->task);
2119                 s2io_reset(sp);
2120                 free_irq(dev->irq, dev);
2121                 freeRxBuffers(sp);
2122                 return -ENODEV;
2123         }
2124
2125         sp->device_close_flag = FALSE;  /* Device is up and running. */
2126         netif_start_queue(dev);
2127
2128         return 0;
2129 }
2130
2131 /*
2132  *  Input Argument/s: 
2133  *  dev - device pointer.
2134  *  Return value:
2135  *  '0' on success and an appropriate (-)ve integer as defined in errno.h
2136  *  file on failure.
2137  *  Description:
2138  *  This is the stop entry point of the driver. It needs to undo exactly
2139  *  whatever was done by the open entry point, thus it's usually referred to
2140  *  as the close function. Among other things this function mainly stops the
2141  *  Rx side of the NIC and frees all the Rx buffers in the Rx rings.
2142  */
2143 int s2io_close(struct net_device *dev)
2144 {
2145         nic_t *sp = dev->priv;
2146         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2147         register u64 val64 = 0;
2148         u16 cnt = 0;
2149
2150         spin_lock(&sp->isr_lock);
2151         netif_stop_queue(dev);
2152
2153 /* disable Tx and Rx traffic on the NIC */
2154         stopNic(sp);
2155
2156         spin_unlock(&sp->isr_lock);
2157
2158 /* If the device tasklet is running, wait till its done before killing it */
2159         while (atomic_read(&(sp->tasklet_status))) {
2160                 set_current_state(TASK_UNINTERRUPTIBLE);
2161                 schedule_timeout(HZ / 10);
2162         }
2163         tasklet_kill(&sp->task);
2164
2165 /* Check if the device is Quiescent and then Reset the NIC */
2166         do {
2167                 val64 = readq(&bar0->adapter_status);
2168                 if (verify_xena_quiescence(val64, sp->device_enabled_once)) {
2169                         break;
2170                 }
2171
2172                 set_current_state(TASK_UNINTERRUPTIBLE);
2173                 schedule_timeout(HZ / 20);
2174                 cnt++;
2175                 if (cnt == 10) {
2176                         DBG_PRINT(ERR_DBG,
2177                                   "s2io_close:Device not Quiescent ");
2178                         DBG_PRINT(ERR_DBG, "adaper status reads 0x%llx\n",
2179                                   (unsigned long long) val64);
2180                         break;
2181                 }
2182         } while (1);
2183         s2io_reset(sp);
2184
2185 /*  Free the Registered IRQ */
2186         free_irq(dev->irq, dev);
2187
2188 /* Free all Tx Buffers waiting for transmission */
2189         freeTxBuffers(sp);
2190
2191 /*  Free all Rx buffers allocated by host */
2192         freeRxBuffers(sp);
2193
2194         sp->device_close_flag = TRUE;   /* Device is shut down. */
2195
2196         return 0;
2197 }
2198
2199 /*
2200  *  Input Argument/s: 
2201  *  skb - the socket buffer containing the Tx data.
2202  *  dev - device pointer.
2203  *  Return value:
2204  *  '0' on success & 1 on failure. 
2205  *  NOTE: when device cant queue the pkt, just the trans_start variable will
2206  *  not be upadted.
2207  *  Description:
2208  *  This function is the Tx entry point of the driver. S2IO NIC supports
2209  *  certain protocol assist features on Tx side, namely  CSO, S/G, LSO.
2210  */
2211 int s2io_xmit(struct sk_buff *skb, struct net_device *dev)
2212 {
2213         nic_t *sp = dev->priv;
2214         u16 off, txd_len, frg_cnt, frg_len, i, queue, off1, queue_len;
2215         register u64 val64;
2216         TxD_t *txdp;
2217         TxFIFO_element_t *tx_fifo;
2218         unsigned long flags;
2219 #ifdef NETIF_F_TSO
2220         int mss;
2221 #endif
2222         mac_info_t *mac_control;
2223         struct config_param *config;
2224
2225         mac_control = &sp->mac_control;
2226         config = &sp->config;
2227
2228         DBG_PRINT(TX_DBG, "%s: In S2IO Tx routine\n", dev->name);
2229
2230         spin_lock_irqsave(&sp->tx_lock, flags);
2231         queue = 0;
2232         /* Multi FIFO Tx is disabled for now. */
2233         if (!queue && tx_prio) {
2234                 u8 x = (skb->data)[5];
2235                 queue = x % config->TxFIFONum;
2236         }
2237
2238
2239         off = (u16) mac_control->tx_curr_put_info[queue].offset;
2240         off1 = (u16) mac_control->tx_curr_get_info[queue].offset;
2241         txd_len = mac_control->txdl_len;
2242         txdp = mac_control->txdl_start[queue] + (config->MaxTxDs * off);
2243
2244         queue_len = mac_control->tx_curr_put_info[queue].fifo_len + 1;
2245         /* Avoid "put" pointer going beyond "get" pointer */
2246         if (txdp->Host_Control || (((off + 1) % queue_len) == off1)) {
2247                 DBG_PRINT(ERR_DBG, "Error in xmit, No free TXDs.\n");
2248                 netif_stop_queue(dev);
2249                 dev_kfree_skb(skb);
2250                 spin_unlock_irqrestore(&sp->tx_lock, flags);
2251                 return 0;
2252         }
2253
2254 #ifdef NETIF_F_TSO
2255         mss = skb_shinfo(skb)->tso_size;
2256         if (mss) {
2257                 txdp->Control_1 |= TXD_TCP_LSO_EN;
2258                 txdp->Control_1 |= TXD_TCP_LSO_MSS(mss);
2259         }
2260 #endif
2261
2262         frg_cnt = skb_shinfo(skb)->nr_frags;
2263         frg_len = skb->len - skb->data_len;
2264
2265         txdp->Host_Control = (unsigned long) skb;
2266         txdp->Buffer_Pointer = pci_map_single
2267             (sp->pdev, skb->data, frg_len, PCI_DMA_TODEVICE);
2268         if (skb->ip_summed == CHECKSUM_HW) {
2269                 txdp->Control_2 |=
2270                     (TXD_TX_CKO_IPV4_EN | TXD_TX_CKO_TCP_EN |
2271                      TXD_TX_CKO_UDP_EN);
2272         }
2273
2274         txdp->Control_2 |= config->TxIntrType;
2275
2276         txdp->Control_1 |= (TXD_BUFFER0_SIZE(frg_len) |
2277                             TXD_GATHER_CODE_FIRST);
2278         txdp->Control_1 |= TXD_LIST_OWN_XENA;
2279
2280         /* For fragmented SKB. */
2281         for (i = 0; i < frg_cnt; i++) {
2282                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2283                 txdp++;
2284                 txdp->Buffer_Pointer = (u64) pci_map_page
2285                     (sp->pdev, frag->page, frag->page_offset,
2286                      frag->size, PCI_DMA_TODEVICE);
2287                 txdp->Control_1 |= TXD_BUFFER0_SIZE(frag->size);
2288         }
2289         txdp->Control_1 |= TXD_GATHER_CODE_LAST;
2290
2291         tx_fifo = mac_control->tx_FIFO_start[queue];
2292         val64 = (mac_control->txdl_start_phy[queue] +
2293                  (sizeof(TxD_t) * txd_len * off));
2294         writeq(val64, &tx_fifo->TxDL_Pointer);
2295
2296         val64 = (TX_FIFO_LAST_TXD_NUM(frg_cnt) | TX_FIFO_FIRST_LIST |
2297                  TX_FIFO_LAST_LIST);
2298 #ifdef NETIF_F_TSO
2299         if (mss)
2300                 val64 |= TX_FIFO_SPECIAL_FUNC;
2301 #endif
2302         writeq(val64, &tx_fifo->List_Control);
2303
2304         off++;
2305         off %= mac_control->tx_curr_put_info[queue].fifo_len + 1;
2306         mac_control->tx_curr_put_info[queue].offset = off;
2307
2308         /* Avoid "put" pointer going beyond "get" pointer */
2309         if (((off + 1) % queue_len) == off1) {
2310                 DBG_PRINT(TX_DBG, 
2311                   "No free TxDs for xmit, Put: 0x%x Get:0x%x\n",
2312                   off, off1);
2313                 netif_stop_queue(dev);
2314         }
2315
2316         dev->trans_start = jiffies;
2317         spin_unlock_irqrestore(&sp->tx_lock, flags);
2318
2319         return 0;
2320 }
2321
2322 /*
2323  *  Input Argument/s: 
2324  *  irq: the irq of the device.
2325  *  dev_id: a void pointer to the dev structure of the NIC.
2326  *  ptregs: pointer to the registers pushed on the stack.
2327  *  Return value:
2328  *  void.
2329  *  Description:
2330  *  This function is the ISR handler of the device. It identifies the reason 
2331  *  for the interrupt and calls the relevant service routines.
2332  *  As a contongency measure, this ISR allocates the recv buffers, if their 
2333  *  numbers are below the panic value which is presently set to 25% of the
2334  *  original number of rcv buffers allocated.
2335  */
2336
2337 static irqreturn_t s2io_isr(int irq, void *dev_id, struct pt_regs *regs)
2338 {
2339         struct net_device *dev = (struct net_device *) dev_id;
2340         nic_t *sp = dev->priv;
2341         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2342 #ifndef CONFIG_S2IO_NAPI
2343         int i, ret;
2344 #endif
2345         u64 reason = 0, general_mask = 0;
2346         mac_info_t *mac_control;
2347         struct config_param *config;
2348
2349         mac_control = &sp->mac_control;
2350         config = &sp->config;
2351
2352         spin_lock(&sp->isr_lock);
2353
2354         /* Identify the cause for interrupt and call the appropriate
2355          * interrupt handler. Causes for the interrupt could be;
2356          * 1. Rx of packet.
2357          * 2. Tx complete.
2358          * 3. Link down.
2359          * 4. Error in any functional blocks of the NIC. 
2360          */
2361         reason = readq(&bar0->general_int_status);
2362
2363         if (!reason) {
2364                 /* The interrupt was not raised by Xena. */
2365                 spin_unlock(&sp->isr_lock);
2366                 return IRQ_NONE;
2367         }
2368         /* Mask the interrupts on the NIC */
2369         general_mask = readq(&bar0->general_int_mask);
2370         writeq(0xFFFFFFFFFFFFFFFFULL, &bar0->general_int_mask);
2371
2372 #if DEBUG_ON
2373         sp->int_cnt++;
2374 #endif
2375
2376         /* If Intr is because of Tx Traffic */
2377         if (reason & GEN_INTR_TXTRAFFIC) {
2378                 txIntrHandler(sp);
2379         }
2380
2381         /* If Intr is because of an error */
2382         if (reason & (GEN_ERROR_INTR))
2383                 alarmIntrHandler(sp);
2384
2385 #ifdef CONFIG_S2IO_NAPI
2386         if (reason & GEN_INTR_RXTRAFFIC) {
2387                 if (netif_rx_schedule_prep(dev)) {
2388                         en_dis_able_NicIntrs(sp, RX_TRAFFIC_INTR,
2389                                              DISABLE_INTRS);
2390                         /* We retake the snap shot of the general interrupt 
2391                          * register.
2392                          */
2393                         general_mask = readq(&bar0->general_int_mask);
2394                         __netif_rx_schedule(dev);
2395                 }
2396         }
2397 #else
2398         /* If Intr is because of Rx Traffic */
2399         if (reason & GEN_INTR_RXTRAFFIC) {
2400                 rxIntrHandler(sp);
2401         }
2402 #endif
2403
2404 /* If the Rx buffer count is below the panic threshold then reallocate the
2405  * buffers from the interrupt handler itself, else schedule a tasklet to 
2406  * reallocate the buffers.
2407  */
2408 #if 1
2409         {
2410         int i;
2411
2412         for (i = 0; i < config->RxRingNum; i++) {
2413                 int rxb_size = atomic_read(&sp->rx_bufs_left[i]);
2414                 int level = rx_buffer_level(sp, rxb_size, i);
2415
2416                 if ((level == PANIC) && (!TASKLET_IN_USE)) {
2417                         int ret;
2418
2419                         DBG_PRINT(ERR_DBG, "%s: Rx BD hit ", dev->name);
2420                         DBG_PRINT(ERR_DBG, "PANIC levels\n");
2421                         if ((ret = fill_rx_buffers(sp, i)) == -ENOMEM) {
2422                                 DBG_PRINT(ERR_DBG, "%s:Out of memory",
2423                                           dev->name);
2424                                 DBG_PRINT(ERR_DBG, " in ISR!!\n");
2425                                 writeq(general_mask,
2426                                        &bar0->general_int_mask);
2427                                 spin_unlock(&sp->isr_lock);
2428                                 return IRQ_HANDLED;
2429                         }
2430                         clear_bit(0,
2431                                   (unsigned long *) (&sp->tasklet_status));
2432                 } else if ((level == LOW)
2433                            && (!atomic_read(&sp->tasklet_status))) {
2434                         tasklet_schedule(&sp->task);
2435                 }
2436
2437         }
2438
2439         }
2440 #else
2441         tasklet_schedule(&sp->task);
2442 #endif
2443
2444         /* Unmask all the previously enabled interrupts on the NIC */
2445         writeq(general_mask, &bar0->general_int_mask);
2446
2447         spin_unlock(&sp->isr_lock);
2448         return IRQ_HANDLED;
2449 }
2450
2451 /*
2452  *  Input Argument/s: 
2453  *  dev - pointer to the device structure.
2454  *  Return value:
2455  *  pointer to the updated net_device_stats structure.
2456  *  Description:
2457  *  This function updates the device statistics structure in the s2io_nic 
2458  *  structure and returns a pointer to the same.
2459  */
2460 struct net_device_stats *s2io_get_stats(struct net_device *dev)
2461 {
2462         nic_t *sp = dev->priv;
2463         mac_info_t *mac_control;
2464         struct config_param *config;
2465
2466         mac_control = &sp->mac_control;
2467         config = &sp->config;
2468
2469         sp->stats.tx_errors = mac_control->StatsInfo->tmac_any_err_frms;
2470         sp->stats.rx_errors = mac_control->StatsInfo->rmac_drop_frms;
2471         sp->stats.multicast = mac_control->StatsInfo->rmac_vld_mcst_frms;
2472         sp->stats.rx_length_errors =
2473             mac_control->StatsInfo->rmac_long_frms;
2474
2475         return (&sp->stats);
2476 }
2477
2478 /*
2479  *  Input Argument/s: 
2480  *  dev - pointer to the device structure
2481  *  Return value:
2482  *  void.
2483  *  Description:
2484  *  This function is a driver entry point which gets called by the kernel 
2485  *  whenever multicast addresses must be enabled/disabled. This also gets 
2486  *  called to set/reset promiscuous mode. Depending on the deivce flag, we
2487  *  determine, if multicast address must be enabled or if promiscuous mode
2488  *  is to be disabled etc.
2489  */
2490 static void s2io_set_multicast(struct net_device *dev)
2491 {
2492         int i, j, prev_cnt;
2493         struct dev_mc_list *mclist;
2494         nic_t *sp = dev->priv;
2495         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2496         u64 val64 = 0, multi_mac = 0x010203040506ULL, mask =
2497             0xfeffffffffffULL;
2498         u64 dis_addr = 0xffffffffffffULL, mac_addr = 0;
2499         void *add;
2500
2501         if ((dev->flags & IFF_ALLMULTI) && (!sp->m_cast_flg)) {
2502                 /*  Enable all Multicast addresses */
2503                 writeq(RMAC_ADDR_DATA0_MEM_ADDR(multi_mac),
2504                        &bar0->rmac_addr_data0_mem);
2505                 writeq(RMAC_ADDR_DATA1_MEM_MASK(mask),
2506                        &bar0->rmac_addr_data1_mem);
2507                 val64 = RMAC_ADDR_CMD_MEM_WE |
2508                     RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
2509                     RMAC_ADDR_CMD_MEM_OFFSET(MAC_MC_ALL_MC_ADDR_OFFSET);
2510                 writeq(val64, &bar0->rmac_addr_cmd_mem);
2511                 /* Wait till command completes */
2512                 waitForCmdComplete(sp);
2513
2514                 sp->m_cast_flg = 1;
2515                 sp->all_multi_pos = MAC_MC_ALL_MC_ADDR_OFFSET;
2516         } else if ((dev->flags & IFF_ALLMULTI) && (sp->m_cast_flg)) {
2517                 /*  Disable all Multicast addresses */
2518                 writeq(RMAC_ADDR_DATA0_MEM_ADDR(dis_addr),
2519                        &bar0->rmac_addr_data0_mem);
2520                 val64 = RMAC_ADDR_CMD_MEM_WE |
2521                     RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
2522                     RMAC_ADDR_CMD_MEM_OFFSET(sp->all_multi_pos);
2523                 writeq(val64, &bar0->rmac_addr_cmd_mem);
2524                 /* Wait till command completes */
2525                 waitForCmdComplete(sp);
2526
2527                 sp->m_cast_flg = 0;
2528                 sp->all_multi_pos = 0;
2529         }
2530
2531         if ((dev->flags & IFF_PROMISC) && (!sp->promisc_flg)) {
2532                 /*  Put the NIC into promiscuous mode */
2533                 add = (void *) &bar0->mac_cfg;
2534                 val64 = readq(&bar0->mac_cfg);
2535                 val64 |= MAC_CFG_RMAC_PROM_ENABLE;
2536
2537                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
2538                 writel((u32) val64, add);
2539                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
2540                 writel((u32) (val64 >> 32), (add + 4));
2541
2542                 val64 = readq(&bar0->mac_cfg);
2543                 sp->promisc_flg = 1;
2544                 DBG_PRINT(ERR_DBG, "%s: entered promiscuous mode\n",
2545                           dev->name);
2546         } else if (!(dev->flags & IFF_PROMISC) && (sp->promisc_flg)) {
2547                 /*  Remove the NIC from promiscuous mode */
2548                 add = (void *) &bar0->mac_cfg;
2549                 val64 = readq(&bar0->mac_cfg);
2550                 val64 &= ~MAC_CFG_RMAC_PROM_ENABLE;
2551
2552                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
2553                 writel((u32) val64, add);
2554                 writeq(RMAC_CFG_KEY(0x4C0D), &bar0->rmac_cfg_key);
2555                 writel((u32) (val64 >> 32), (add + 4));
2556
2557                 val64 = readq(&bar0->mac_cfg);
2558                 sp->promisc_flg = 0;
2559                 DBG_PRINT(ERR_DBG, "%s: left promiscuous mode\n",
2560                           dev->name);
2561         }
2562
2563         /*  Update individual M_CAST address list */
2564         if ((!sp->m_cast_flg) && dev->mc_count) {
2565                 if (dev->mc_count >
2566                     (MAX_ADDRS_SUPPORTED - MAC_MC_ADDR_START_OFFSET - 1)) {
2567                         DBG_PRINT(ERR_DBG, "%s: No more Rx filters ",
2568                                   dev->name);
2569                         DBG_PRINT(ERR_DBG, "can be added, please enable ");
2570                         DBG_PRINT(ERR_DBG, "ALL_MULTI instead\n");
2571                         return;
2572                 }
2573
2574                 prev_cnt = sp->mc_addr_count;
2575                 sp->mc_addr_count = dev->mc_count;
2576
2577                 /* Clear out the previous list of Mc in the H/W. */
2578                 for (i = 0; i < prev_cnt; i++) {
2579                         writeq(RMAC_ADDR_DATA0_MEM_ADDR(dis_addr),
2580                                &bar0->rmac_addr_data0_mem);
2581                         val64 = RMAC_ADDR_CMD_MEM_WE |
2582                             RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
2583                             RMAC_ADDR_CMD_MEM_OFFSET
2584                             (MAC_MC_ADDR_START_OFFSET + i);
2585                         writeq(val64, &bar0->rmac_addr_cmd_mem);
2586
2587                         /* Wait for command completes */
2588                         if (waitForCmdComplete(sp)) {
2589                                 DBG_PRINT(ERR_DBG, "%s: Adding ",
2590                                           dev->name);
2591                                 DBG_PRINT(ERR_DBG, "Multicasts failed\n");
2592                                 return;
2593                         }
2594                 }
2595
2596                 /* Create the new Rx filter list and update the same in H/W. */
2597                 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
2598                      i++, mclist = mclist->next) {
2599                         memcpy(sp->usr_addrs[i].addr, mclist->dmi_addr,
2600                                ETH_ALEN);
2601                         for (j = 0; j < ETH_ALEN; j++) {
2602                                 mac_addr |= mclist->dmi_addr[j];
2603                                 mac_addr <<= 8;
2604                         }
2605                         writeq(RMAC_ADDR_DATA0_MEM_ADDR(mac_addr),
2606                                &bar0->rmac_addr_data0_mem);
2607
2608                         val64 = RMAC_ADDR_CMD_MEM_WE |
2609                             RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
2610                             RMAC_ADDR_CMD_MEM_OFFSET
2611                             (i + MAC_MC_ADDR_START_OFFSET);
2612                         writeq(val64, &bar0->rmac_addr_cmd_mem);
2613
2614                         /* Wait for command completes */
2615                         if (waitForCmdComplete(sp)) {
2616                                 DBG_PRINT(ERR_DBG, "%s: Adding ",
2617                                           dev->name);
2618                                 DBG_PRINT(ERR_DBG, "Multicasts failed\n");
2619                                 return;
2620                         }
2621                 }
2622         }
2623 }
2624
2625 /*
2626  *  Input Argument/s: 
2627  *  dev - pointer to the device structure.
2628  *  new_mac - a uchar pointer to the new mac address which is to be set.
2629  *  Return value:
2630  *  SUCCESS on success and an appropriate (-)ve integer as defined in errno.h
2631  *  file on failure.
2632  *  Description:
2633  *  This procedure will program the Xframe to receive frames with new
2634  *  Mac Address
2635  */
2636 int s2io_set_mac_addr(struct net_device *dev, u8 * addr)
2637 {
2638         nic_t *sp = dev->priv;
2639         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2640         register u64 val64, mac_addr = 0;
2641         int i;
2642
2643         /* 
2644          * Set the new MAC address as the new unicast filter and reflect this
2645          * change on the device address registered with the OS. It will be
2646          * at offset 0. 
2647          */
2648         for (i = 0; i < ETH_ALEN; i++) {
2649                 mac_addr <<= 8;
2650                 mac_addr |= addr[i];
2651         }
2652
2653         writeq(RMAC_ADDR_DATA0_MEM_ADDR(mac_addr),
2654                &bar0->rmac_addr_data0_mem);
2655
2656         val64 =
2657             RMAC_ADDR_CMD_MEM_WE | RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
2658             RMAC_ADDR_CMD_MEM_OFFSET(0);
2659         writeq(val64, &bar0->rmac_addr_cmd_mem);
2660         /* Wait till command completes */
2661         if (waitForCmdComplete(sp)) {
2662                 DBG_PRINT(ERR_DBG, "%s: set_mac_addr failed\n", dev->name);
2663                 return FAILURE;
2664         }
2665
2666         return SUCCESS;
2667 }
2668
2669 /*
2670  * Input Argument/s: 
2671  *  sp - private member of the device structure, which is a pointer to the 
2672  *      s2io_nic structure.
2673  *  info - pointer to the structure with parameters given by ethtool to set
2674  *  link information.
2675  * Return value:
2676  *  0 on success.
2677  * Description:
2678  *  The function sets different link parameters provided by the user onto 
2679  *  the NIC.
2680  */
2681 static int s2io_ethtool_sset(struct net_device *dev,
2682                              struct ethtool_cmd *info)
2683 {
2684         nic_t *sp = dev->priv;
2685         if ((info->autoneg == AUTONEG_ENABLE) ||
2686             (info->speed != SPEED_10000) || (info->duplex != DUPLEX_FULL))
2687                 return -EINVAL;
2688         else {
2689                 s2io_close(sp->dev);
2690                 s2io_open(sp->dev);
2691         }
2692
2693         return 0;
2694 }
2695
2696 /*
2697  * Input Argument/s: 
2698  *  sp - private member of the device structure, which is a pointer to the 
2699  *      s2io_nic structure.
2700  *  info - pointer to the structure with parameters given by ethtool to return
2701  *  link information.
2702  * Return value:
2703  *  void
2704  * Description:
2705  *  Returns link specefic information like speed, duplex etc.. to ethtool.
2706  */
2707 int s2io_ethtool_gset(struct net_device *dev, struct ethtool_cmd *info)
2708 {
2709         nic_t *sp = dev->priv;
2710         info->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
2711         info->advertising = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
2712         info->port = PORT_FIBRE;
2713         /* info->transceiver?? TODO */
2714
2715         if (netif_carrier_ok(sp->dev)) {
2716                 info->speed = 10000;
2717                 info->duplex = DUPLEX_FULL;
2718         } else {
2719                 info->speed = -1;
2720                 info->duplex = -1;
2721         }
2722
2723         info->autoneg = AUTONEG_DISABLE;
2724         return 0;
2725 }
2726
2727 /*
2728  * Input Argument/s: 
2729  *  sp - private member of the device structure, which is a pointer to the 
2730  *      s2io_nic structure.
2731  *  info - pointer to the structure with parameters given by ethtool to return
2732  *  driver information.
2733  * Return value:
2734  *  void
2735  * Description:
2736  *  Returns driver specefic information like name, version etc.. to ethtool.
2737  */
2738 static void s2io_ethtool_gdrvinfo(struct net_device *dev,
2739                                   struct ethtool_drvinfo *info)
2740 {
2741         nic_t *sp = dev->priv;
2742
2743         strncpy(info->driver, s2io_driver_name, sizeof(s2io_driver_name));
2744         strncpy(info->version, s2io_driver_version,
2745                 sizeof(s2io_driver_version));
2746         strncpy(info->fw_version, "", 32);
2747         strncpy(info->bus_info, sp->pdev->slot_name, 32);
2748         info->regdump_len = XENA_REG_SPACE;
2749         info->eedump_len = XENA_EEPROM_SPACE;
2750         info->testinfo_len = S2IO_TEST_LEN;
2751         info->n_stats = S2IO_STAT_LEN;
2752 }
2753
2754 /*
2755  * Input Argument/s: 
2756  *  sp - private member of the device structure, which is a pointer to the 
2757  *      s2io_nic structure.
2758  *  regs - pointer to the structure with parameters given by ethtool for 
2759  *  dumping the registers.
2760  *  reg_space - The input argumnet into which all the registers are dumped.
2761  * Return value:
2762  *  void
2763  * Description:
2764  *  Dumps the entire register space of xFrame NIC into the user given buffer 
2765  *  area.
2766  */
2767 static void s2io_ethtool_gregs(struct net_device *dev,
2768                                struct ethtool_regs *regs, void *space)
2769 {
2770         int i;
2771         u64 reg;
2772         u8 *reg_space = (u8 *) space;
2773         nic_t *sp = dev->priv;
2774
2775         regs->len = XENA_REG_SPACE;
2776         regs->version = sp->pdev->subsystem_device;
2777
2778         for (i = 0; i < regs->len; i += 8) {
2779                 reg = readq((void *) (sp->bar0 + i));
2780                 memcpy((reg_space + i), &reg, 8);
2781         }
2782 }
2783
2784 /*
2785  * Input Argument/s: 
2786  *  data - address of the private member of the device structure, which 
2787  *  is a pointer to the s2io_nic structure, provided as an u32.
2788  * Return value:
2789  *  void
2790  * Description:
2791  *  This is actually the timer function that alternates the adapter LED bit
2792  *  of the adapter control bit to set/reset every time on invocation.
2793  *  The timer is set for 1/2 a second, hence tha NIC blinks once every second.
2794  */
2795 static void s2io_phy_id(unsigned long data)
2796 {
2797         nic_t *sp = (nic_t *) data;
2798         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2799         u64 val64 = 0;
2800         u16 subid;
2801
2802         subid = sp->pdev->subsystem_device;
2803         if ((subid & 0xFF) >= 0x07) {
2804                 val64 = readq(&bar0->gpio_control);
2805                 val64 ^= GPIO_CTRL_GPIO_0;
2806                 writeq(val64, &bar0->gpio_control);
2807         } else {
2808                 val64 = readq(&bar0->adapter_control);
2809                 val64 ^= ADAPTER_LED_ON;
2810                 writeq(val64, &bar0->adapter_control);
2811         }
2812
2813         mod_timer(&sp->id_timer, jiffies + HZ / 2);
2814 }
2815
2816 /*
2817  * Input Argument/s: 
2818  *  sp - private member of the device structure, which is a pointer to the 
2819  *      s2io_nic structure.
2820  *  id - pointer to the structure with identification parameters given by 
2821  *  ethtool.
2822  * Return value:
2823  *  int , returns '0' on success
2824  * Description:
2825  *  Used to physically identify the NIC on the system. The Link LED will blink
2826  *  for a time specified by the user for identification.
2827  *  NOTE: The Link has to be Up to be able to blink the LED. Hence 
2828  *  identification is possible only if it's link is up.
2829  */
2830 static int s2io_ethtool_idnic(struct net_device *dev, u32 data)
2831 {
2832         u64 val64 = 0;
2833         nic_t *sp = dev->priv;
2834         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2835         u16 subid;
2836
2837         subid = sp->pdev->subsystem_device;
2838         if ((subid & 0xFF) < 0x07) {
2839                 val64 = readq(&bar0->adapter_control);
2840                 if (!(val64 & ADAPTER_CNTL_EN)) {
2841                         printk(KERN_ERR
2842                                "Adapter Link down, cannot blink LED\n");
2843                         return -EFAULT;
2844                 }
2845         }
2846         if (sp->id_timer.function == NULL) {
2847                 init_timer(&sp->id_timer);
2848                 sp->id_timer.function = s2io_phy_id;
2849                 sp->id_timer.data = (unsigned long) sp;
2850         }
2851         mod_timer(&sp->id_timer, jiffies);
2852         set_current_state(TASK_INTERRUPTIBLE);
2853         if (data)
2854                 schedule_timeout(data * HZ);
2855         else
2856                 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
2857         del_timer_sync(&sp->id_timer);
2858
2859         return 0;
2860 }
2861
2862 /*
2863  * Input Argument/s: 
2864  *  sp - private member of the device structure, which is a pointer to the 
2865  *      s2io_nic structure.
2866  *  ep - pointer to the structure with pause parameters given by ethtool.
2867  * Return value:
2868  *  void
2869  * Description:
2870  *  Returns the Pause frame generation and reception capability of the NIC.
2871  */
2872 static void s2io_ethtool_getpause_data(struct net_device *dev,
2873                                        struct ethtool_pauseparam *ep)
2874 {
2875         u64 val64;
2876         nic_t *sp = dev->priv;
2877         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2878
2879         val64 = readq(&bar0->rmac_pause_cfg);
2880         if (val64 & RMAC_PAUSE_GEN_ENABLE)
2881                 ep->tx_pause = TRUE;
2882         if (val64 & RMAC_PAUSE_RX_ENABLE)
2883                 ep->rx_pause = TRUE;
2884         ep->autoneg = FALSE;
2885 }
2886
2887 /*
2888  * Input Argument/s: 
2889  * sp - private member of the device structure, which is a pointer to the 
2890  *      s2io_nic structure.
2891  * ep - pointer to the structure with pause parameters given by ethtool.
2892  * Return value:
2893  * int, returns '0' on Success
2894  * Description:
2895  * It can be used to set or reset Pause frame generation or reception support 
2896  * of the NIC.
2897  */
2898 int s2io_ethtool_setpause_data(struct net_device *dev,
2899                                struct ethtool_pauseparam *ep)
2900 {
2901         u64 val64;
2902         nic_t *sp = dev->priv;
2903         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2904
2905         val64 = readq(&bar0->rmac_pause_cfg);
2906         if (ep->tx_pause)
2907                 val64 |= RMAC_PAUSE_GEN_ENABLE;
2908         else
2909                 val64 &= ~RMAC_PAUSE_GEN_ENABLE;
2910         if (ep->rx_pause)
2911                 val64 |= RMAC_PAUSE_RX_ENABLE;
2912         else
2913                 val64 &= ~RMAC_PAUSE_RX_ENABLE;
2914         writeq(val64, &bar0->rmac_pause_cfg);
2915         return 0;
2916 }
2917
2918 /*
2919  * Input Argument/s: 
2920  *  sp - private member of the device structure, which is a pointer to the 
2921  *      s2io_nic structure.
2922  *  off - offset at which the data must be written
2923  * Return value:
2924  *  -1 on failure and the value read from the Eeprom if successful.
2925  * Description:
2926  *  Will read 4 bytes of data from the user given offset and return the 
2927  *  read data.
2928  * NOTE: Will allow to read only part of the EEPROM visible through the
2929  *       I2C bus.
2930  */
2931 #define S2IO_DEV_ID             5
2932 static u32 readEeprom(nic_t * sp, int off)
2933 {
2934         u32 data = -1, exit_cnt = 0;
2935         u64 val64;
2936         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2937
2938         val64 = I2C_CONTROL_DEV_ID(S2IO_DEV_ID) | I2C_CONTROL_ADDR(off) |
2939             I2C_CONTROL_BYTE_CNT(0x3) | I2C_CONTROL_READ |
2940             I2C_CONTROL_CNTL_START;
2941         writeq(val64, &bar0->i2c_control);
2942
2943         while (exit_cnt < 5) {
2944                 val64 = readq(&bar0->i2c_control);
2945                 if (I2C_CONTROL_CNTL_END(val64)) {
2946                         data = I2C_CONTROL_GET_DATA(val64);
2947                         break;
2948                 }
2949                 set_current_state(TASK_UNINTERRUPTIBLE);
2950                 schedule_timeout(HZ / 20);
2951                 exit_cnt++;
2952         }
2953
2954         return data;
2955 }
2956
2957 /*
2958  * Input Argument/s: 
2959  *  sp - private member of the device structure, which is a pointer to the 
2960  *      s2io_nic structure.
2961  *  off - offset at which the data must be written
2962  *  data - The data that is to be written
2963  *  cnt - Number of bytes of the data that are actually to be written into 
2964  *  the Eeprom. (max of 3)
2965  * Return value:
2966  *  '0' on success, -1 on failure.
2967  * Description:
2968  *  Actually writes the relevant part of the data value into the Eeprom
2969  *  through the I2C bus.
2970  */
2971 static int writeEeprom(nic_t * sp, int off, u32 data, int cnt)
2972 {
2973         int exit_cnt = 0, ret = -1;
2974         u64 val64;
2975         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
2976
2977         val64 = I2C_CONTROL_DEV_ID(S2IO_DEV_ID) | I2C_CONTROL_ADDR(off) |
2978             I2C_CONTROL_BYTE_CNT(cnt) | I2C_CONTROL_SET_DATA(data) |
2979             I2C_CONTROL_CNTL_START;
2980         writeq(val64, &bar0->i2c_control);
2981
2982         while (exit_cnt < 5) {
2983                 val64 = readq(&bar0->i2c_control);
2984                 if (I2C_CONTROL_CNTL_END(val64)) {
2985                         if (!(val64 & I2C_CONTROL_NACK))
2986                                 ret = 0;
2987                         break;
2988                 }
2989                 set_current_state(TASK_UNINTERRUPTIBLE);
2990                 schedule_timeout(HZ / 20);
2991                 exit_cnt++;
2992         }
2993
2994         return ret;
2995 }
2996
2997 /* 
2998  * A helper function used to invert the 4 byte u32 data field
2999  * byte by byte. This will be used by the Read Eeprom function
3000  * for display purposes.
3001  */
3002 u32 inv(u32 data)
3003 {
3004         static u32 ret = 0;
3005
3006         if (data) {
3007                 u8 c = data;
3008                 ret = ((ret << 8) + c);
3009                 data >>= 8;
3010                 inv(data);
3011         }
3012
3013         return ret;
3014 }
3015
3016 /*
3017  * Input Argument/s: 
3018  *  sp - private member of the device structure, which is a pointer to the 
3019  *      s2io_nic structure.
3020  *  eeprom - pointer to the user level structure provided by ethtool, 
3021  *   containing all relevant information.
3022  *  data_buf - user defined value to be written into Eeprom.
3023  * Return value:
3024  *  int  '0' on success
3025  * Description:
3026  *  Reads the values stored in the Eeprom at given offset for a given length.
3027  *  Stores these values int the input argument data buffer 'data_buf' and
3028  *  returns these to the caller (ethtool.)
3029  */
3030 int s2io_ethtool_geeprom(struct net_device *dev,
3031                          struct ethtool_eeprom *eeprom, u8 * data_buf)
3032 {
3033         u32 data, i, valid;
3034         nic_t *sp = dev->priv;
3035
3036         eeprom->magic = sp->pdev->vendor | (sp->pdev->device << 16);
3037
3038         if ((eeprom->offset + eeprom->len) > (XENA_EEPROM_SPACE))
3039                 eeprom->len = XENA_EEPROM_SPACE - eeprom->offset;
3040
3041         for (i = 0; i < eeprom->len; i += 4) {
3042                 data = readEeprom(sp, eeprom->offset + i);
3043                 if (data < 0) {
3044                         DBG_PRINT(ERR_DBG, "Read of EEPROM failed\n");
3045                         return -EFAULT;
3046                 }
3047                 valid = inv(data);
3048                 memcpy((data_buf + i), &valid, 4);
3049         }
3050         return 0;
3051 }
3052
3053 /*
3054  * Input Argument/s: 
3055  *  sp - private member of the device structure, which is a pointer to the 
3056  *      s2io_nic structure.
3057  *  eeprom - pointer to the user level structure provided by ethtool, 
3058  *   containing all relevant information.
3059  *  data_buf - user defined value to be written into Eeprom.
3060  * Return value:
3061  *  '0' on success, -EFAULT on failure.
3062  * Description:
3063  *  Tries to write the user provided value in the Eeprom, at the offset
3064  *  given by the user.
3065  */
3066 static int s2io_ethtool_seeprom(struct net_device *dev,
3067                                 struct ethtool_eeprom *eeprom,
3068                                 u8 * data_buf)
3069 {
3070         int len = eeprom->len, cnt = 0;
3071         u32 valid = 0, data;
3072         nic_t *sp = dev->priv;
3073
3074         if (eeprom->magic != (sp->pdev->vendor | (sp->pdev->device << 16))) {
3075                 DBG_PRINT(ERR_DBG,
3076                           "ETHTOOL_WRITE_EEPROM Err: Magic value ");
3077                 DBG_PRINT(ERR_DBG, "is wrong, Its not 0x%x\n",
3078                           eeprom->magic);
3079                 return -EFAULT;
3080         }
3081
3082         while (len) {
3083                 data = (u32) data_buf[cnt] & 0x000000FF;
3084                 if (data) {
3085                         valid = (u32) (data << 24);
3086                 } else
3087                         valid = data;
3088
3089                 if (writeEeprom(sp, (eeprom->offset + cnt), valid, 0)) {
3090                         DBG_PRINT(ERR_DBG,
3091                                   "ETHTOOL_WRITE_EEPROM Err: Cannot ");
3092                         DBG_PRINT(ERR_DBG,
3093                                   "write into the specified offset\n");
3094                         return -EFAULT;
3095                 }
3096                 cnt++;
3097                 len--;
3098         }
3099
3100         return 0;
3101 }
3102
3103 /*
3104  * Input Argument/s: 
3105  *  sp - private member of the device structure, which is a pointer to the 
3106  *      s2io_nic structure.
3107  *  data - variable that returns the result of each of the test conducted by 
3108  *      the driver.
3109  * Return value:
3110  *  '0' on success.
3111  * Description:
3112  *  Read and write into all clock domains. The NIC has 3 clock domains,
3113  *  see that registers in all the three regions are accessible.
3114  */
3115 static int s2io_registerTest(nic_t * sp, uint64_t * data)
3116 {
3117         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
3118         u64 val64 = 0;
3119         int fail = 0;
3120
3121         val64 = readq(&bar0->pcc_enable);
3122         if (val64 != 0xff00000000000000ULL) {
3123                 fail = 1;
3124                 DBG_PRINT(INFO_DBG, "Read Test level 1 fails\n");
3125         }
3126
3127         val64 = readq(&bar0->rmac_pause_cfg);
3128         if (val64 != 0xc000ffff00000000ULL) {
3129                 fail = 1;
3130                 DBG_PRINT(INFO_DBG, "Read Test level 2 fails\n");
3131         }
3132
3133         val64 = readq(&bar0->rx_queue_cfg);
3134         if (val64 != 0x0808080808080808ULL) {
3135                 fail = 1;
3136                 DBG_PRINT(INFO_DBG, "Read Test level 3 fails\n");
3137         }
3138
3139         val64 = readq(&bar0->xgxs_efifo_cfg);
3140         if (val64 != 0x000000001923141EULL) {
3141                 fail = 1;
3142                 DBG_PRINT(INFO_DBG, "Read Test level 4 fails\n");
3143         }
3144
3145         val64 = 0x5A5A5A5A5A5A5A5AULL;
3146         writeq(val64, &bar0->xmsi_data);
3147         val64 = readq(&bar0->xmsi_data);
3148         if (val64 != 0x5A5A5A5A5A5A5A5AULL) {
3149                 fail = 1;
3150                 DBG_PRINT(ERR_DBG, "Write Test level 1 fails\n");
3151         }
3152
3153         val64 = 0xA5A5A5A5A5A5A5A5ULL;
3154         writeq(val64, &bar0->xmsi_data);
3155         val64 = readq(&bar0->xmsi_data);
3156         if (val64 != 0xA5A5A5A5A5A5A5A5ULL) {
3157                 fail = 1;
3158                 DBG_PRINT(ERR_DBG, "Write Test level 2 fails\n");
3159         }
3160
3161         *data = fail;
3162         return 0;
3163 }
3164
3165 /*
3166  * Input Argument/s: 
3167  *  sp - private member of the device structure, which is a pointer to the 
3168  *      s2io_nic structure.
3169  *  data - variable that returns the result of each of the test conducted by 
3170  *      the driver.
3171  * Return value:
3172  *  '0' on success.
3173  * Description:
3174  *  Verify that EEPROM in the xena can be programmed using I2C_CONTROL 
3175  *  register.
3176  */
3177 static int s2io_eepromTest(nic_t * sp, uint64_t * data)
3178 {
3179         int fail = 0, ret_data;
3180
3181         /* Test Write Error at offset 0 */
3182         if (!writeEeprom(sp, 0, 0, 3))
3183                 fail = 1;
3184
3185         /* Test Write at offset 4f0 */
3186         if (writeEeprom(sp, 0x4F0, 0x01234567, 3))
3187                 fail = 1;
3188         if ((ret_data = readEeprom(sp, 0x4f0)) < 0)
3189                 fail = 1;
3190
3191         if (ret_data != 0x01234567)
3192                 fail = 1;
3193
3194         /* Reset the EEPROM data go FFFF */
3195         writeEeprom(sp, 0x4F0, 0xFFFFFFFF, 3);
3196
3197         /* Test Write Request Error at offset 0x7c */
3198         if (!writeEeprom(sp, 0x07C, 0, 3))
3199                 fail = 1;
3200
3201         /* Test Write Request at offset 0x7fc */
3202         if (writeEeprom(sp, 0x7FC, 0x01234567, 3))
3203                 fail = 1;
3204         if ((ret_data = readEeprom(sp, 0x7FC)) < 0)
3205                 fail = 1;
3206
3207         if (ret_data != 0x01234567)
3208                 fail = 1;
3209
3210         /* Reset the EEPROM data go FFFF */
3211         writeEeprom(sp, 0x7FC, 0xFFFFFFFF, 3);
3212
3213         /* Test Write Error at offset 0x80 */
3214         if (!writeEeprom(sp, 0x080, 0, 3))
3215                 fail = 1;
3216
3217         /* Test Write Error at offset 0xfc */
3218         if (!writeEeprom(sp, 0x0FC, 0, 3))
3219                 fail = 1;
3220
3221         /* Test Write Error at offset 0x100 */
3222         if (!writeEeprom(sp, 0x100, 0, 3))
3223                 fail = 1;
3224
3225         /* Test Write Error at offset 4ec */
3226         if (!writeEeprom(sp, 0x4EC, 0, 3))
3227                 fail = 1;
3228
3229         *data = fail;
3230         return 0;
3231 }
3232
3233 /*
3234  * Input Argument/s: 
3235  *  sp - private member of the device structure, which is a pointer to the 
3236  *      s2io_nic structure.
3237  *  data - variable that returns the result of each of the test conducted by 
3238  *      the driver.
3239  * Return value:
3240  *  '0' on success and -1 on failure.
3241  * Description:
3242  *  This invokes the MemBist test of the card. We give around
3243  *  2 secs time for the Test to complete. If it's still not complete
3244  *  within this peiod, we consider that the test failed. 
3245  */
3246 static int s2io_bistTest(nic_t * sp, uint64_t * data)
3247 {
3248         u8 bist = 0;
3249         int cnt = 0, ret = -1;
3250
3251         pci_read_config_byte(sp->pdev, PCI_BIST, &bist);
3252         bist |= PCI_BIST_START;
3253         pci_write_config_word(sp->pdev, PCI_BIST, bist);
3254
3255         while (cnt < 20) {
3256                 pci_read_config_byte(sp->pdev, PCI_BIST, &bist);
3257                 if (!(bist & PCI_BIST_START)) {
3258                         *data = (bist & PCI_BIST_CODE_MASK);
3259                         ret = 0;
3260                         break;
3261                 }
3262                 set_current_state(TASK_UNINTERRUPTIBLE);
3263                 schedule_timeout(HZ / 10);
3264                 cnt++;
3265         }
3266
3267         return ret;
3268 }
3269
3270 /*
3271  * Input Argument/s: 
3272  *  sp - private member of the device structure, which is a pointer to the 
3273  *      s2io_nic structure.
3274  *  data - variable that returns the result of each of the test conducted by 
3275  *      the driver.
3276  * Return value:
3277  *  '0' on success.
3278  * Description:
3279  *  The function verifies the link state of the NIC and updates the input 
3280  *  argument 'data' appropriately.
3281  */
3282 static int s2io_linkTest(nic_t * sp, uint64_t * data)
3283 {
3284         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
3285         u64 val64;
3286
3287         val64 = readq(&bar0->adapter_status);
3288         if (val64 & ADAPTER_STATUS_RMAC_LOCAL_FAULT)
3289                 *data = 1;
3290
3291         return 0;
3292 }
3293
3294 /*
3295  * Input Argument/s: 
3296  *  sp - private member of the device structure, which is a pointer to the 
3297  *      s2io_nic structure.
3298  *  data - variable that returns the result of each of the test conducted by 
3299  *      the driver.
3300  * Return value:
3301  *  '0' on success.
3302  * Description:
3303  *  This is one of the offline test that tests the read and write 
3304  *  access to the RldRam chip on the NIC.
3305  */
3306 static int s2io_rldramTest(nic_t * sp, uint64_t * data)
3307 {
3308         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
3309         u64 val64;
3310         int cnt, iteration = 0, test_pass = 0;
3311
3312         val64 = readq(&bar0->adapter_control);
3313         val64 &= ~ADAPTER_ECC_EN;
3314         writeq(val64, &bar0->adapter_control);
3315
3316         val64 = readq(&bar0->mc_rldram_test_ctrl);
3317         val64 |= MC_RLDRAM_TEST_MODE;
3318         writeq(val64, &bar0->mc_rldram_test_ctrl);
3319
3320         val64 = readq(&bar0->mc_rldram_mrs);
3321         val64 |= MC_RLDRAM_QUEUE_SIZE_ENABLE;
3322         writeq(val64, &bar0->mc_rldram_mrs);
3323
3324         val64 |= MC_RLDRAM_MRS_ENABLE;
3325         writeq(val64, &bar0->mc_rldram_mrs);
3326
3327         while (iteration < 2) {
3328                 val64 = 0x55555555aaaa0000ULL;
3329                 if (iteration == 1) {
3330                         val64 ^= 0xFFFFFFFFFFFF0000ULL;
3331                 }
3332                 writeq(val64, &bar0->mc_rldram_test_d0);
3333
3334                 val64 = 0xaaaa5a5555550000ULL;
3335                 if (iteration == 1) {
3336                         val64 ^= 0xFFFFFFFFFFFF0000ULL;
3337                 }
3338                 writeq(val64, &bar0->mc_rldram_test_d1);
3339
3340                 val64 = 0x55aaaaaaaa5a0000ULL;
3341                 if (iteration == 1) {
3342                         val64 ^= 0xFFFFFFFFFFFF0000ULL;
3343                 }
3344                 writeq(val64, &bar0->mc_rldram_test_d2);
3345
3346                 val64 = (u64) (0x0000003fffff0000ULL);
3347                 writeq(val64, &bar0->mc_rldram_test_add);
3348
3349
3350                 val64 = MC_RLDRAM_TEST_MODE;
3351                 writeq(val64, &bar0->mc_rldram_test_ctrl);
3352
3353                 val64 |=
3354                     MC_RLDRAM_TEST_MODE | MC_RLDRAM_TEST_WRITE |
3355                     MC_RLDRAM_TEST_GO;
3356                 writeq(val64, &bar0->mc_rldram_test_ctrl);
3357
3358                 for (cnt = 0; cnt < 5; cnt++) {
3359                         val64 = readq(&bar0->mc_rldram_test_ctrl);
3360                         if (val64 & MC_RLDRAM_TEST_DONE)
3361                                 break;
3362                         set_current_state(TASK_UNINTERRUPTIBLE);
3363                         schedule_timeout(HZ / 5);
3364                 }
3365
3366                 if (cnt == 5)
3367                         break;
3368
3369                 val64 = MC_RLDRAM_TEST_MODE;
3370                 writeq(val64, &bar0->mc_rldram_test_ctrl);
3371
3372                 val64 |= MC_RLDRAM_TEST_MODE | MC_RLDRAM_TEST_GO;
3373                 writeq(val64, &bar0->mc_rldram_test_ctrl);
3374
3375                 for (cnt = 0; cnt < 5; cnt++) {
3376                         val64 = readq(&bar0->mc_rldram_test_ctrl);
3377                         if (val64 & MC_RLDRAM_TEST_DONE)
3378                                 break;
3379                         set_current_state(TASK_UNINTERRUPTIBLE);
3380                         schedule_timeout(HZ / 2);
3381                 }
3382
3383                 if (cnt == 5)
3384                         break;
3385
3386                 val64 = readq(&bar0->mc_rldram_test_ctrl);
3387                 if (val64 & MC_RLDRAM_TEST_PASS)
3388                         test_pass = 1;
3389
3390                 iteration++;
3391         }
3392
3393         if (!test_pass)
3394                 *data = 1;
3395         else
3396                 *data = 0;
3397
3398         return 0;
3399 }
3400
3401 /*
3402  * Input Argument/s: 
3403  *  sp - private member of the device structure, which is a pointer to the 
3404  *      s2io_nic structure.
3405  *  ethtest - pointer to a ethtool command specific structure that will be
3406  *      returned to the user.
3407  *  data - variable that returns the result of each of the test conducted by 
3408  *      the driver.
3409  * Return value:
3410  *  SUCCESS on success and an appropriate -1 on failure.
3411  * Description:
3412  *  This function conducts 6 tests ( 4 offline and 2 online) to determine
3413  *      the health of the card.
3414  */
3415 static void s2io_ethtool_test(struct net_device *dev,
3416                               struct ethtool_test *ethtest,
3417                               uint64_t * data)
3418 {
3419         nic_t *sp = dev->priv;
3420         int orig_state = netif_running(sp->dev);
3421
3422         if (ethtest->flags == ETH_TEST_FL_OFFLINE) {
3423                 /* Offline Tests. */
3424                 if (orig_state) {
3425                         s2io_close(sp->dev);
3426                         s2io_set_swapper(sp);
3427                 } else
3428                         s2io_set_swapper(sp);
3429
3430                 if (s2io_registerTest(sp, &data[0]))
3431                         ethtest->flags |= ETH_TEST_FL_FAILED;
3432
3433                 s2io_reset(sp);
3434                 s2io_set_swapper(sp);
3435
3436                 if (s2io_rldramTest(sp, &data[3]))
3437                         ethtest->flags |= ETH_TEST_FL_FAILED;
3438
3439                 s2io_reset(sp);
3440                 s2io_set_swapper(sp);
3441
3442                 if (s2io_eepromTest(sp, &data[1]))
3443                         ethtest->flags |= ETH_TEST_FL_FAILED;
3444
3445                 if (s2io_bistTest(sp, &data[4]))
3446                         ethtest->flags |= ETH_TEST_FL_FAILED;
3447
3448                 if (orig_state)
3449                         s2io_open(sp->dev);
3450
3451                 data[2] = 0;
3452         } else {
3453                 /* Online Tests. */
3454                 if (!orig_state) {
3455                         DBG_PRINT(ERR_DBG,
3456                                   "%s: is not up, cannot run test\n",
3457                                   dev->name);
3458                         data[0] = -1;
3459                         data[1] = -1;
3460                         data[2] = -1;
3461                         data[3] = -1;
3462                         data[4] = -1;
3463                 }
3464
3465                 if (s2io_linkTest(sp, &data[2]))
3466                         ethtest->flags |= ETH_TEST_FL_FAILED;
3467
3468                 data[0] = 0;
3469                 data[1] = 0;
3470                 data[3] = 0;
3471                 data[4] = 0;
3472         }
3473 }
3474
3475 static void s2io_get_ethtool_stats(struct net_device *dev,
3476                                    struct ethtool_stats *estats,
3477                                    u64 * tmp_stats)
3478 {
3479         int i = 0;
3480         nic_t *sp = dev->priv;
3481         StatInfo_t *stat_info = sp->mac_control.StatsInfo;
3482
3483         tmp_stats[i++] = stat_info->tmac_frms;
3484         tmp_stats[i++] = stat_info->tmac_data_octets;
3485         tmp_stats[i++] = stat_info->tmac_drop_frms;
3486         tmp_stats[i++] = stat_info->tmac_mcst_frms;
3487         tmp_stats[i++] = stat_info->tmac_bcst_frms;
3488         tmp_stats[i++] = stat_info->tmac_pause_ctrl_frms;
3489         tmp_stats[i++] = stat_info->tmac_any_err_frms;
3490         tmp_stats[i++] = stat_info->tmac_vld_ip_octets;
3491         tmp_stats[i++] = stat_info->tmac_vld_ip;
3492         tmp_stats[i++] = stat_info->tmac_drop_ip;
3493         tmp_stats[i++] = stat_info->tmac_icmp;
3494         tmp_stats[i++] = stat_info->tmac_rst_tcp;
3495         tmp_stats[i++] = stat_info->tmac_tcp;
3496         tmp_stats[i++] = stat_info->tmac_udp;
3497         tmp_stats[i++] = stat_info->rmac_vld_frms;
3498         tmp_stats[i++] = stat_info->rmac_data_octets;
3499         tmp_stats[i++] = stat_info->rmac_fcs_err_frms;
3500         tmp_stats[i++] = stat_info->rmac_drop_frms;
3501         tmp_stats[i++] = stat_info->rmac_vld_mcst_frms;
3502         tmp_stats[i++] = stat_info->rmac_vld_bcst_frms;
3503         tmp_stats[i++] = stat_info->rmac_in_rng_len_err_frms;
3504         tmp_stats[i++] = stat_info->rmac_long_frms;
3505         tmp_stats[i++] = stat_info->rmac_pause_ctrl_frms;
3506         tmp_stats[i++] = stat_info->rmac_discarded_frms;
3507         tmp_stats[i++] = stat_info->rmac_usized_frms;
3508         tmp_stats[i++] = stat_info->rmac_osized_frms;
3509         tmp_stats[i++] = stat_info->rmac_frag_frms;
3510         tmp_stats[i++] = stat_info->rmac_jabber_frms;
3511         tmp_stats[i++] = stat_info->rmac_ip;
3512         tmp_stats[i++] = stat_info->rmac_ip_octets;
3513         tmp_stats[i++] = stat_info->rmac_hdr_err_ip;
3514         tmp_stats[i++] = stat_info->rmac_drop_ip;
3515         tmp_stats[i++] = stat_info->rmac_icmp;
3516         tmp_stats[i++] = stat_info->rmac_tcp;
3517         tmp_stats[i++] = stat_info->rmac_udp;
3518         tmp_stats[i++] = stat_info->rmac_err_drp_udp;
3519         tmp_stats[i++] = stat_info->rmac_pause_cnt;
3520         tmp_stats[i++] = stat_info->rmac_accepted_ip;
3521         tmp_stats[i++] = stat_info->rmac_err_tcp;
3522 }
3523
3524 int s2io_ethtool_get_regs_len(struct net_device *dev)
3525 {
3526         return (XENA_REG_SPACE);
3527 }
3528
3529
3530 u32 s2io_ethtool_get_rx_csum(struct net_device * dev)
3531 {
3532         nic_t *sp = dev->priv;
3533
3534         return (sp->rx_csum);
3535 }
3536 int s2io_ethtool_set_rx_csum(struct net_device *dev, u32 data)
3537 {
3538         nic_t *sp = dev->priv;
3539
3540         if (data)
3541                 sp->rx_csum = 1;
3542         else
3543                 sp->rx_csum = 0;
3544
3545         return 0;
3546 }
3547 int s2io_get_eeprom_len(struct net_device *dev)
3548 {
3549         return (XENA_EEPROM_SPACE);
3550 }
3551
3552 int s2io_ethtool_self_test_count(struct net_device *dev)
3553 {
3554         return (S2IO_TEST_LEN);
3555 }
3556 void s2io_ethtool_get_strings(struct net_device *dev,
3557                               u32 stringset, u8 * data)
3558 {
3559         switch (stringset) {
3560         case ETH_SS_TEST:
3561                 memcpy(data, s2io_gstrings, S2IO_STRINGS_LEN);
3562                 break;
3563         case ETH_SS_STATS:
3564                 memcpy(data, &ethtool_stats_keys,
3565                        sizeof(ethtool_stats_keys));
3566         }
3567 }
3568 static int s2io_ethtool_get_stats_count(struct net_device *dev)
3569 {
3570         return (S2IO_STAT_LEN);
3571 }
3572
3573 static struct ethtool_ops netdev_ethtool_ops = {
3574         .get_settings = s2io_ethtool_gset,
3575         .set_settings = s2io_ethtool_sset,
3576         .get_drvinfo = s2io_ethtool_gdrvinfo,
3577         .get_regs_len = s2io_ethtool_get_regs_len,
3578         .get_regs = s2io_ethtool_gregs,
3579         .get_link = ethtool_op_get_link,
3580         .get_eeprom_len = s2io_get_eeprom_len,
3581         .get_eeprom = s2io_ethtool_geeprom,
3582         .set_eeprom = s2io_ethtool_seeprom,
3583         .get_pauseparam = s2io_ethtool_getpause_data,
3584         .set_pauseparam = s2io_ethtool_setpause_data,
3585         .get_rx_csum = s2io_ethtool_get_rx_csum,
3586         .set_rx_csum = s2io_ethtool_set_rx_csum,
3587         .get_tx_csum = ethtool_op_get_tx_csum,
3588         .set_tx_csum = ethtool_op_set_tx_csum,
3589         .get_sg = ethtool_op_get_sg,
3590         .set_sg = ethtool_op_set_sg,
3591 #ifdef NETIF_F_TSO
3592         .get_tso = ethtool_op_get_tso,
3593         .set_tso = ethtool_op_set_tso,
3594 #endif
3595         .self_test_count = s2io_ethtool_self_test_count,
3596         .self_test = s2io_ethtool_test,
3597         .get_strings = s2io_ethtool_get_strings,
3598         .phys_id = s2io_ethtool_idnic,
3599         .get_stats_count = s2io_ethtool_get_stats_count,
3600         .get_ethtool_stats = s2io_get_ethtool_stats
3601 };
3602
3603 /*
3604  *  Input Argument/s: 
3605  *  dev -   Device pointer.
3606  *  ifr -   An IOCTL specefic structure, that can contain a pointer to
3607  *      a proprietary structure used to pass information to the driver.
3608  *  cmd -   This is used to distinguish between the different commands that
3609  *      can be passed to the IOCTL functions.
3610  *  Return value:
3611  *  '0' on success and an appropriate (-)ve integer as defined in errno.h
3612  *  file on failure.
3613  *  Description:
3614  *  This function has support for ethtool, adding multiple MAC addresses on 
3615  *  the NIC and some DBG commands for the util tool.
3616  */
3617 int s2io_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3618 {
3619         return -EOPNOTSUPP;
3620 }
3621
3622 /*
3623  *  Input Argument/s: 
3624  *   dev - device pointer.
3625  *   new_mtu - the new MTU size for the device.
3626  *  Return value:
3627  *   '0' on success and an appropriate (-)ve integer as defined in errno.h
3628  *   file on failure.
3629  *  Description:
3630  *   A driver entry point to change MTU size for the device. Before changing
3631  *   the MTU the device must be stopped.
3632  */
3633 int s2io_change_mtu(struct net_device *dev, int new_mtu)
3634 {
3635         nic_t *sp = dev->priv;
3636         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) sp->bar0;
3637         register u64 val64;
3638
3639         if (netif_running(dev)) {
3640                 DBG_PRINT(ERR_DBG, "%s: Must be stopped to ", dev->name);
3641                 DBG_PRINT(ERR_DBG, "change its MTU \n");
3642                 return -EBUSY;
3643         }
3644
3645         if ((new_mtu < MIN_MTU) || (new_mtu > S2IO_JUMBO_SIZE)) {
3646                 DBG_PRINT(ERR_DBG, "%s: MTU size is invalid.\n",
3647                           dev->name);
3648                 return -EPERM;
3649         }
3650
3651 /* Set the new MTU into the PYLD register of the NIC */
3652         val64 = new_mtu;
3653         writeq(vBIT(val64, 2, 14), &bar0->rmac_max_pyld_len);
3654
3655         dev->mtu = new_mtu;
3656
3657         return 0;
3658 }
3659
3660 /*
3661  *  Input Argument/s: 
3662  *  dev_adr - address of the device structure in dma_addr_t format.
3663  *  Return value:
3664  *  void.
3665  *  Description:
3666  *  This is the tasklet or the bottom half of the ISR. This is
3667  *  an extension of the ISR which is scheduled by the scheduler to be run 
3668  *  when the load on the CPU is low. All low priority tasks of the ISR can
3669  *  be pushed into the tasklet. For now the tasklet is used only to 
3670  *  replenish the Rx buffers in the Rx buffer descriptors.
3671  */
3672 static void s2io_tasklet(unsigned long dev_addr)
3673 {
3674         struct net_device *dev = (struct net_device *) dev_addr;
3675         nic_t *sp = dev->priv;
3676         int i, ret;
3677         mac_info_t *mac_control;
3678         struct config_param *config;
3679
3680         mac_control = &sp->mac_control;
3681         config = &sp->config;
3682
3683         if (!TASKLET_IN_USE) {
3684                 for (i = 0; i < config->RxRingNum; i++) {
3685                         ret = fill_rx_buffers(sp, i);
3686                         if (ret == -ENOMEM) {
3687                                 DBG_PRINT(ERR_DBG, "%s: Out of ",
3688                                           dev->name);
3689                                 DBG_PRINT(ERR_DBG, "memory in tasklet\n");
3690                                 return;
3691                         } else if (ret == -EFILL) {
3692                                 DBG_PRINT(ERR_DBG,
3693                                           "%s: Rx Ring %d is full\n",
3694                                           dev->name, i);
3695                                 return;
3696                         }
3697                 }
3698                 clear_bit(0, (unsigned long *) (&sp->tasklet_status));
3699         }
3700 }
3701
3702
3703 /*
3704  * Description:
3705  * 
3706  */
3707 static void s2io_set_link(unsigned long data)
3708 {
3709         nic_t *nic = (nic_t *) data;
3710         struct net_device *dev = nic->dev;
3711         XENA_dev_config_t *bar0 = (XENA_dev_config_t *) nic->bar0;
3712         register u64 val64, err_reg;
3713
3714         /* Allow a small delay for the NICs self initiated 
3715          * cleanup to complete.
3716          */
3717         set_current_state(TASK_UNINTERRUPTIBLE);
3718         schedule_timeout(HZ / 10);
3719
3720         val64 = readq(&bar0->adapter_status);
3721         if (verify_xena_quiescence(val64, nic->device_enabled_once)) {
3722                 /* Acknowledge interrupt and clear the R1 register */
3723                 err_reg = readq(&bar0->mac_rmac_err_reg);
3724                 writeq(err_reg, &bar0->mac_rmac_err_reg);
3725
3726                 if (LINK_IS_UP(val64)) {
3727                         val64 = readq(&bar0->adapter_control);
3728                         val64 |= ADAPTER_CNTL_EN;
3729                         writeq(val64, &bar0->adapter_control);
3730                         val64 |= ADAPTER_LED_ON;
3731                         writeq(val64, &bar0->adapter_control);
3732                         val64 = readq(&bar0->adapter_status);
3733                         if (!LINK_IS_UP(val64)) {
3734                                 DBG_PRINT(ERR_DBG, "%s:", dev->name);
3735                                 DBG_PRINT(ERR_DBG, " Link down");
3736                                 DBG_PRINT(ERR_DBG, "after ");
3737                                 DBG_PRINT(ERR_DBG, "enabling ");
3738                                 DBG_PRINT(ERR_DBG, "device \n");
3739                         }
3740                         if (nic->device_enabled_once == FALSE) {
3741                                 nic->device_enabled_once = TRUE;
3742                         }
3743                         s2io_link(nic, LINK_UP);
3744                 } else {
3745                         s2io_link(nic, LINK_DOWN);
3746                 }
3747         } else {                /* NIC is not Quiescent. */
3748                 DBG_PRINT(ERR_DBG, "%s: Error: ", dev->name);
3749                 DBG_PRINT(ERR_DBG, "device is not Quiescent\n");
3750                 netif_stop_queue(dev);
3751         }
3752 }
3753
3754 /*
3755  * Description:
3756  * This function is scheduled to be run by the s2io_tx_watchdog
3757  * function after 0.5 secs to reset the NIC. The idea is to reduce 
3758  * the run time of the watch dog routine which is run holding a
3759  * spin lock.
3760  */
3761 static void s2io_restart_nic(unsigned long data)
3762 {
3763         struct net_device *dev = (struct net_device *) data;
3764         nic_t *sp = dev->priv;
3765
3766         s2io_close(dev);
3767         sp->device_close_flag = TRUE;
3768         s2io_open(dev);
3769         DBG_PRINT(ERR_DBG,
3770                   "%s: was reset by Tx watchdog timer.\n", dev->name);
3771 }
3772
3773 /*
3774  *  Input Argument/s: 
3775  *  dev - device pointer.
3776  *  Return value:
3777  *  void
3778  *  Description:
3779  *  This function is triggered if the Tx Queue is stopped
3780  *  for a pre-defined amount of time when the Interface is still up.
3781  *  If the Interface is jammed in such a situation, the hardware is
3782  *  reset (by s2io_close) and restarted again (by s2io_open) to
3783  *  overcome any problem that might have been caused in the hardware.
3784  */
3785 static void s2io_tx_watchdog(struct net_device *dev)
3786 {
3787         nic_t *sp = dev->priv;
3788
3789         if (netif_carrier_ok(dev)) {
3790                 schedule_work(&sp->rst_timer_task);
3791         }
3792 }
3793
3794 /*
3795  *  Input Argument/s: 
3796  *   sp - private member of the device structure, which is a pointer to the 
3797  *   s2io_nic structure.
3798  *   skb - the socket buffer pointer.
3799  *   len - length of the packet
3800  *   cksum - FCS checksum of the frame.
3801  *  ring_no - the ring from which this RxD was extracted.
3802  *  Return value:
3803  *   SUCCESS on success and -1 on failure.
3804  *  Description: 
3805  *   This function is called by the Tx interrupt serivce routine to perform 
3806  *   some OS related operations on the SKB before passing it to the upper
3807  *   layers. It mainly checks if the checksum is OK, if so adds it to the
3808  *   SKBs cksum variable, increments the Rx packet count and passes the SKB
3809  *   to the upper layer. If the checksum is wrong, it increments the Rx
3810  *   packet error count, frees the SKB and returns error.
3811  */
3812 static int rxOsmHandler(nic_t * sp, u16 len, RxD_t * rxdp, int ring_no)
3813 {
3814         struct net_device *dev = (struct net_device *) sp->dev;
3815         struct sk_buff *skb =
3816             (struct sk_buff *) ((unsigned long) rxdp->Host_Control);
3817         u16 l3_csum, l4_csum;
3818
3819         l3_csum = RXD_GET_L3_CKSUM(rxdp->Control_1);
3820         if ((rxdp->Control_1 & TCP_OR_UDP_FRAME) && (sp->rx_csum)) {
3821                 l4_csum = RXD_GET_L4_CKSUM(rxdp->Control_1);
3822                 if ((l3_csum == L3_CKSUM_OK) && (l4_csum == L4_CKSUM_OK)) {
3823                         /* NIC verifies if the Checksum of the received
3824                          * frame is Ok or not and accordingly returns
3825                          * a flag in the RxD.
3826                          */
3827                         skb->ip_summed = CHECKSUM_UNNECESSARY;
3828                 } else {
3829                         /* 
3830                          * Packet with erroneous checksum, let the 
3831                          * upper layers deal with it.
3832                          */
3833                         skb->ip_summed = CHECKSUM_NONE;
3834                 }
3835         } else {
3836                 skb->ip_summed = CHECKSUM_NONE;
3837         }
3838
3839         skb->dev = dev;
3840         skb_put(skb, len);
3841         skb->protocol = eth_type_trans(skb, dev);
3842
3843 #ifdef CONFIG_S2IO_NAPI
3844         netif_receive_skb(skb);
3845 #else
3846         netif_rx(skb);
3847 #endif
3848
3849         dev->last_rx = jiffies;
3850 #if DEBUG_ON
3851         sp->rxpkt_cnt++;
3852 #endif
3853         sp->rx_pkt_count++;
3854         sp->stats.rx_packets++;
3855         sp->stats.rx_bytes += len;
3856         sp->rxpkt_bytes += len;
3857
3858         atomic_dec(&sp->rx_bufs_left[ring_no]);
3859         rxdp->Host_Control = 0;
3860         return SUCCESS;
3861 }
3862
3863 int check_for_txSpace(nic_t * sp)
3864 {
3865         u32 put_off, get_off, queue_len;
3866         int ret = TRUE, i;
3867
3868         for (i = 0; i < sp->config.TxFIFONum; i++) {
3869                 queue_len = sp->mac_control.tx_curr_put_info[i].fifo_len
3870                     + 1;
3871                 put_off = sp->mac_control.tx_curr_put_info[i].offset;
3872                 get_off = sp->mac_control.tx_curr_get_info[i].offset;
3873                 if (((put_off + 1) % queue_len) == get_off) {
3874                         ret = FALSE;
3875                         break;
3876                 }
3877         }
3878
3879         return ret;
3880 }
3881
3882 /*
3883 *  Input Argument/s: 
3884 *   sp - private member of the device structure, which is a pointer to the 
3885 *   s2io_nic structure.
3886 *   link - inidicates whether link is UP/DOWN.
3887 *  Return value:
3888 *   void.
3889 *  Description:
3890 *   This function stops/starts the Tx queue depending on whether the link
3891 *   status of the NIC is is down or up. This is called by the Alarm interrupt 
3892 *  handler whenever a link change interrupt comes up. 
3893 */
3894 void s2io_link(nic_t * sp, int link)
3895 {
3896         struct net_device *dev = (struct net_device *) sp->dev;
3897
3898         if (link != sp->last_link_state) {
3899                 if (link == LINK_DOWN) {
3900                         DBG_PRINT(ERR_DBG, "%s: Link down\n", dev->name);
3901                         netif_carrier_off(dev);
3902                         netif_stop_queue(dev);
3903                 } else {
3904                         DBG_PRINT(ERR_DBG, "%s: Link Up\n", dev->name);
3905                         netif_carrier_on(dev);
3906                         if (check_for_txSpace(sp) == TRUE) {
3907                                 /* Don't wake the queue, if we know there
3908                                  * are no free TxDs available.
3909                                  */
3910                                 netif_wake_queue(dev);
3911                         }
3912                 }
3913         }
3914         sp->last_link_state = link;
3915 }
3916
3917 /*
3918 *  Input Argument/s: 
3919 *   pdev - structure containing the PCI related information of the device.
3920 *  Return value:
3921 *   returns the revision ID of the device.
3922 *  Description:
3923 *   Function to identify the Revision ID of xena.
3924 */
3925 int get_xena_rev_id(struct pci_dev *pdev)
3926 {
3927         u8 id = 0;
3928         int ret;
3929         ret = pci_read_config_byte(pdev, PCI_REVISION_ID, (u8 *) & id);
3930         return id;
3931 }
3932
3933 /*
3934 *  Input Argument/s: 
3935 *   sp - private member of the device structure, which is a pointer to the 
3936 *   s2io_nic structure.
3937 *  Return value:
3938 *   void
3939 *  Description:
3940 *   This function initializes a few of the PCI and PCI-X configuration registers
3941 *   with recommended values.
3942 */
3943 static void s2io_init_pci(nic_t * sp)
3944 {
3945         u16 pci_cmd = 0;
3946
3947 /* Enable Data Parity Error Recovery in PCI-X command register. */
3948         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
3949                              &(sp->pcix_cmd));
3950         pci_write_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
3951                               (sp->pcix_cmd | 1));
3952         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
3953                              &(sp->pcix_cmd));
3954
3955 /* Set the PErr Response bit in PCI command register. */
3956         pci_read_config_word(sp->pdev, PCI_COMMAND, &pci_cmd);
3957         pci_write_config_word(sp->pdev, PCI_COMMAND,
3958                               (pci_cmd | PCI_COMMAND_PARITY));
3959         pci_read_config_word(sp->pdev, PCI_COMMAND, &pci_cmd);
3960
3961 /* Set user specified value in Latency Timer */
3962         if (latency_timer) {
3963                 pci_write_config_byte(sp->pdev, PCI_LATENCY_TIMER,
3964                                       latency_timer);
3965                 pci_read_config_byte(sp->pdev, PCI_LATENCY_TIMER,
3966                                      &latency_timer);
3967         }
3968
3969 /* Set MMRB count to 4096 in PCI-X Command register. */
3970         pci_write_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
3971                               (sp->pcix_cmd | 0x0C));
3972         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
3973                              &(sp->pcix_cmd));
3974
3975 /* Setting Maximum outstanding splits to two for now. */
3976         sp->pcix_cmd &= 0xFF1F;
3977
3978         sp->pcix_cmd |=
3979             XENA_MAX_OUTSTANDING_SPLITS(XENA_TWO_SPLIT_TRANSACTION);
3980         pci_write_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
3981                               sp->pcix_cmd);
3982         pci_read_config_word(sp->pdev, PCIX_COMMAND_REGISTER,
3983                              &(sp->pcix_cmd));
3984
3985 }
3986
3987 MODULE_AUTHOR("Raghavendra Koushik <raghavendra.koushik@s2io.com>");
3988 MODULE_LICENSE("GPL");
3989 MODULE_PARM(ring_num, "1-" __MODULE_STRING(1) "i");
3990 MODULE_PARM(frame_len, "1-" __MODULE_STRING(8) "i");
3991 MODULE_PARM(ring_len, "1-" __MODULE_STRING(8) "i");
3992 MODULE_PARM(fifo_num, "1-" __MODULE_STRING(1) "i");
3993 MODULE_PARM(fifo_len, "1-" __MODULE_STRING(8) "i");
3994 MODULE_PARM(rx_prio, "1-" __MODULE_STRING(1) "i");
3995 MODULE_PARM(tx_prio, "1-" __MODULE_STRING(1) "i");
3996 MODULE_PARM(latency_timer, "1-" __MODULE_STRING(1) "i");
3997
3998 /*
3999 *  Input Argument/s: 
4000 *   pdev - structure containing the PCI related information of the device.
4001 *   pre -  the List of PCI devices supported by the driver listed in s2io_tbl.
4002 *  Return value:
4003 *   returns '0' on success and negative on failure.
4004 *  Description:
4005 *  The function initializes an adapter identified by the pci_dec structure.
4006 *  All OS related initialization including memory and device structure and 
4007 *  initlaization of the device private variable is done. Also the swapper 
4008 *  control register is initialized to enable read and write into the I/O 
4009 *  registers of the device.
4010 *  
4011 */
4012 static int __devinit
4013 s2io_init_nic(struct pci_dev *pdev, const struct pci_device_id *pre)
4014 {
4015         nic_t *sp;
4016         struct net_device *dev;
4017         char *dev_name = "S2IO 10GE NIC";
4018         int i, j, ret;
4019         int dma_flag = FALSE;
4020         u32 mac_up, mac_down;
4021         u64 val64 = 0, tmp64 = 0;
4022         XENA_dev_config_t *bar0 = NULL;
4023         u16 subid;
4024         mac_info_t *mac_control;
4025         struct config_param *config;
4026
4027
4028         if ((ret = pci_enable_device(pdev))) {
4029                 DBG_PRINT(ERR_DBG,
4030                           "s2io_init_nic: pci_enable_device failed\n");
4031                 return ret;
4032         }
4033
4034         if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL)) {
4035                 DBG_PRINT(INIT_DBG, "s2io_init_nic: Using 64bit DMA\n");
4036                 dma_flag = TRUE;
4037                 if (pci_set_consistent_dma_mask
4038                     (pdev, 0xffffffffffffffffULL)) {
4039                         DBG_PRINT(ERR_DBG,
4040                                   "Unable to obtain 64bit DMA for \
4041                                         consistent allocations\n");
4042                         pci_disable_device(pdev);
4043                         return -ENOMEM;
4044                 }
4045         } else if (!pci_set_dma_mask(pdev, 0xffffffffUL)) {
4046                 DBG_PRINT(INIT_DBG, "s2io_init_nic: Using 32bit DMA\n");
4047         } else {
4048                 pci_disable_device(pdev);
4049                 return -ENOMEM;
4050         }
4051
4052         if (pci_request_regions(pdev, s2io_driver_name)) {
4053                 DBG_PRINT(ERR_DBG, "Request Regions failed\n"),
4054                     pci_disable_device(pdev);
4055                 return -ENODEV;
4056         }
4057
4058         dev = alloc_etherdev(sizeof(nic_t));
4059         if (dev == NULL) {
4060                 DBG_PRINT(ERR_DBG, "Device allocation failed\n");
4061                 pci_disable_device(pdev);
4062                 pci_release_regions(pdev);
4063                 return -ENODEV;
4064         }
4065
4066         pci_set_master(pdev);
4067         pci_set_drvdata(pdev, dev);
4068         SET_MODULE_OWNER(dev);
4069         SET_NETDEV_DEV(dev, &pdev->dev);
4070
4071         /*  Private member variable initialized to s2io NIC structure */
4072         sp = dev->priv;
4073         memset(sp, 0, sizeof(nic_t));
4074         sp->dev = dev;
4075         sp->pdev = pdev;
4076         sp->vendor_id = pdev->vendor;
4077         sp->device_id = pdev->device;
4078         sp->high_dma_flag = dma_flag;
4079         sp->irq = pdev->irq;
4080         sp->device_enabled_once = FALSE;
4081         strcpy(sp->name, dev_name);
4082
4083         /* Initialize some PCI/PCI-X fields of the NIC. */
4084         s2io_init_pci(sp);
4085
4086         /* Setting the device configuration parameters.
4087          * Most of these parameters can be specified by the user during 
4088          * module insertion as they are module loadable parameters. If 
4089          * these parameters are not not specified during load time, they 
4090          * are initialized with default values.
4091          */
4092         mac_control = &sp->mac_control;
4093         config = &sp->config;
4094
4095         /* Tx side parameters. */
4096         config->TxFIFONum = fifo_num ? fifo_num : 1;
4097
4098         if (!fifo_len[0] && (fifo_num > 1)) {
4099                 printk(KERN_ERR "Fifo Lens not specified for all FIFOs\n");
4100                 goto init_failed;
4101         }
4102
4103         if (fifo_len[0]) {
4104                 int cnt;
4105
4106                 for (cnt = 0; fifo_len[cnt]; cnt++);
4107                 if (fifo_num) {
4108                         if (cnt < fifo_num) {
4109                                 printk(KERN_ERR
4110                                        "Fifo Lens not specified for ");
4111                                 printk(KERN_ERR "all FIFOs\n");
4112                                 goto init_failed;
4113                         }
4114                 }
4115                 for (cnt = 0; cnt < config->TxFIFONum; cnt++) {
4116                         config->TxCfg[cnt].FifoLen = fifo_len[cnt];
4117                         config->TxCfg[cnt].FifoPriority = cnt;
4118                 }
4119         } else {
4120                 config->TxCfg[0].FifoLen = DEFAULT_FIFO_LEN;
4121                 config->TxCfg[0].FifoPriority = 0;
4122         }
4123
4124         config->TxIntrType = TXD_INT_TYPE_UTILZ;
4125         for (i = 0; i < config->TxFIFONum; i++) {
4126                 if (config->TxCfg[i].FifoLen < 65) {
4127                         config->TxIntrType = TXD_INT_TYPE_PER_LIST;
4128                         break;
4129                 }
4130         }
4131
4132         config->TxCfg[0].fNoSnoop = (NO_SNOOP_TXD | NO_SNOOP_TXD_BUFFER);
4133         config->MaxTxDs = MAX_SKB_FRAGS;
4134         config->TxFlow = TRUE;
4135
4136         /* Rx side parameters. */
4137         config->RxRingNum = ring_num ? ring_num : 1;
4138
4139         if (ring_len[0]) {
4140                 int cnt;
4141                 for (cnt = 0; cnt < config->RxRingNum; cnt++) {
4142                         config->RxCfg[cnt].NumRxd = ring_len[cnt];
4143                         config->RxCfg[cnt].RingPriority = cnt;
4144                 }
4145         } else {
4146                 int id;
4147                 if ((id = get_xena_rev_id(pdev)) == 1) {
4148                         config->RxCfg[0].NumRxd = LARGE_RXD_CNT;
4149
4150                 } else {
4151                         config->RxCfg[0].NumRxd = SMALL_RXD_CNT;
4152                 }
4153                 config->RxCfg[0].RingPriority = 0;
4154         }
4155         config->RxCfg[0].RingOrg = RING_ORG_BUFF1;
4156         config->RxCfg[0].RxdThresh = DEFAULT_RXD_THRESHOLD;
4157         config->RxCfg[0].fNoSnoop = (NO_SNOOP_RXD | NO_SNOOP_RXD_BUFFER);
4158         config->RxCfg[0].RxD_BackOff_Interval = TBD;
4159         config->RxFlow = TRUE;
4160
4161         /* Miscellaneous parameters. */
4162         config->RxVLANEnable = TRUE;
4163         config->MTU = MAX_MTU_VLAN;
4164         config->JumboEnable = FALSE;
4165
4166         /*  Setting Mac Control parameters */
4167         mac_control->txdl_len = MAX_SKB_FRAGS;
4168         mac_control->rmac_pause_time = 0;
4169
4170         /* Initialize Ring buffer parameters. */
4171         for (i = 0; i < config->RxRingNum; i++)
4172                 atomic_set(&sp->rx_bufs_left[i], 0);
4173
4174         /*  initialize the shared memory used by the NIC and the host */
4175         if (initSharedMem(sp)) {
4176                 DBG_PRINT(ERR_DBG, "%s: Memory allocation failed\n",
4177                           dev->name);
4178                 goto mem_alloc_failed;
4179         }
4180
4181         sp->bar0 = (caddr_t) ioremap(pci_resource_start(pdev, 0),
4182                                      pci_resource_len(pdev, 0));
4183         if (!sp->bar0) {
4184                 DBG_PRINT(ERR_DBG, "%s: S2IO: cannot remap io mem1\n",
4185                           dev->name);
4186                 goto bar0_remap_failed;
4187         }
4188
4189         sp->bar1 = (caddr_t) ioremap(pci_resource_start(pdev, 2),
4190                                      pci_resource_len(pdev, 2));
4191         if (!sp->bar1) {
4192                 DBG_PRINT(ERR_DBG, "%s: S2IO: cannot remap io mem2\n",
4193                           dev->name);
4194                 goto bar1_remap_failed;
4195         }
4196
4197         dev->irq = pdev->irq;
4198         dev->base_addr = (unsigned long) sp->bar0;
4199
4200         /* Initializing the BAR1 address as the start of the FIFO pointer. */
4201         for (j = 0; j < MAX_TX_FIFOS; j++) {
4202                 mac_control->tx_FIFO_start[j] = (TxFIFO_element_t *)
4203                     (sp->bar1 + (j * 0x00020000));
4204         }
4205
4206         /*  Driver entry points */
4207         dev->open = &s2io_open;
4208         dev->stop = &s2io_close;
4209         dev->hard_start_xmit = &s2io_xmit;
4210         dev->get_stats = &s2io_get_stats;
4211         dev->set_multicast_list = &s2io_set_multicast;
4212         dev->do_ioctl = &s2io_ioctl;
4213         dev->change_mtu = &s2io_change_mtu;
4214         SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
4215
4216         /*
4217          * will use eth_mac_addr() for  dev->set_mac_address
4218          * mac address will be set every time dev->open() is called
4219          */
4220 #ifdef CONFIG_S2IO_NAPI
4221         dev->poll = s2io_poll;
4222         dev->weight = 128;      /* For now. */
4223 #endif
4224
4225         dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
4226         if (sp->high_dma_flag == TRUE)
4227                 dev->features |= NETIF_F_HIGHDMA;
4228 #ifdef NETIF_F_TSO
4229         dev->features |= NETIF_F_TSO;
4230 #endif
4231
4232         dev->tx_timeout = &s2io_tx_watchdog;
4233         dev->watchdog_timeo = WATCH_DOG_TIMEOUT;
4234         INIT_WORK(&sp->rst_timer_task,
4235                   (void (*)(void *)) s2io_restart_nic, dev);
4236         INIT_WORK(&sp->set_link_task,
4237                   (void (*)(void *)) s2io_set_link, sp);
4238
4239         if (register_netdev(dev)) {
4240                 DBG_PRINT(ERR_DBG, "Device registration failed\n");
4241                 goto register_failed;
4242         }
4243
4244         pci_save_state(sp->pdev, sp->config_space);
4245
4246         /* Setting swapper control on the NIC, for proper reset operation */
4247         if (s2io_set_swapper(sp)) {
4248                 DBG_PRINT(ERR_DBG, "%s:swapper settings are wrong\n",
4249                           dev->name);
4250                 goto set_swap_failed;
4251         }
4252
4253         /* Fix for all "FFs" MAC address problems observed on Alpha platforms */
4254         FixMacAddress(sp);
4255         s2io_reset(sp);
4256
4257         /* Setting swapper control on the NIC, so the MAC address can be read.
4258          */
4259         if (s2io_set_swapper(sp)) {
4260                 DBG_PRINT(ERR_DBG,
4261                           "%s: S2IO: swapper settings are wrong\n",
4262                           dev->name);
4263                 goto set_swap_failed;
4264         }
4265
4266         /*  MAC address initialization.
4267          *  For now only one mac address will be read and used.
4268          */
4269         bar0 = (XENA_dev_config_t *) sp->bar0;
4270         val64 = RMAC_ADDR_CMD_MEM_RD | RMAC_ADDR_CMD_MEM_STROBE_NEW_CMD |
4271             RMAC_ADDR_CMD_MEM_OFFSET(0 + MAC_MAC_ADDR_START_OFFSET);
4272         writeq(val64, &bar0->rmac_addr_cmd_mem);
4273         waitForCmdComplete(sp);
4274
4275         tmp64 = readq(&bar0->rmac_addr_data0_mem);
4276         mac_down = (u32) tmp64;
4277         mac_up = (u32) (tmp64 >> 32);
4278
4279         memset(sp->defMacAddr[0].mac_addr, 0, sizeof(ETH_ALEN));
4280
4281         sp->defMacAddr[0].mac_addr[3] = (u8) (mac_up);
4282         sp->defMacAddr[0].mac_addr[2] = (u8) (mac_up >> 8);
4283         sp->defMacAddr[0].mac_addr[1] = (u8) (mac_up >> 16);
4284         sp->defMacAddr[0].mac_addr[0] = (u8) (mac_up >> 24);
4285         sp->defMacAddr[0].mac_addr[5] = (u8) (mac_down >> 16);
4286         sp->defMacAddr[0].mac_addr[4] = (u8) (mac_down >> 24);
4287
4288         DBG_PRINT(INIT_DBG,
4289                   "DEFAULT MAC ADDR:0x%02x-%02x-%02x-%02x-%02x-%02x\n",
4290                   sp->defMacAddr[0].mac_addr[0],
4291                   sp->defMacAddr[0].mac_addr[1],
4292                   sp->defMacAddr[0].mac_addr[2],
4293                   sp->defMacAddr[0].mac_addr[3],
4294                   sp->defMacAddr[0].mac_addr[4],
4295                   sp->defMacAddr[0].mac_addr[5]);
4296
4297         /*  Set the factory defined MAC address initially   */
4298         dev->addr_len = ETH_ALEN;
4299         memcpy(dev->dev_addr, sp->defMacAddr, ETH_ALEN);
4300
4301         /*  Initialize the tasklet status flag */
4302         atomic_set(&(sp->tasklet_status), 0);
4303
4304
4305         /* Initialize spinlocks */
4306         spin_lock_init(&sp->isr_lock);
4307         spin_lock_init(&sp->tx_lock);
4308
4309         /* SXE-002: Configure link and activity LED to init state 
4310          * on driver load. 
4311          */
4312         subid = sp->pdev->subsystem_device;
4313         if ((subid & 0xFF) >= 0x07) {
4314                 val64 = readq(&bar0->gpio_control);
4315                 val64 |= 0x0000800000000000ULL;
4316                 writeq(val64, &bar0->gpio_control);
4317                 val64 = 0x0411040400000000ULL;
4318                 writeq(val64, (u64 *) ((u8 *) bar0 + 0x2700));
4319                 val64 = readq(&bar0->gpio_control);
4320         }
4321
4322         /* Make Link state as off at this point, when the Link change 
4323          * interrupt comes the state will be automatically changed to 
4324          * the right state.
4325          */
4326         netif_carrier_off(dev);
4327         sp->last_link_state = LINK_DOWN;
4328
4329         sp->rx_csum = 1;        /* Rx chksum verify enabled by default */
4330
4331         return 0;
4332
4333       set_swap_failed:
4334         unregister_netdev(dev);
4335       register_failed:
4336         iounmap(sp->bar1);
4337       bar1_remap_failed:
4338         iounmap(sp->bar0);
4339       bar0_remap_failed:
4340       mem_alloc_failed:
4341         freeSharedMem(sp);
4342       init_failed:
4343         pci_disable_device(pdev);
4344         pci_release_regions(pdev);
4345         pci_set_drvdata(pdev, NULL);
4346         free_netdev(dev);
4347
4348         return -ENODEV;
4349 }
4350
4351 /*
4352 *  Input Argument/s: 
4353 *   pdev - structure containing the PCI related information of the device.
4354 *  Return value:
4355 *  void
4356 *  Description:
4357 *  This function is called by the Pci subsystem to release a PCI device 
4358 *  and free up all resource held up by the device. This could be in response 
4359 *  to a Hot plug event or when the driver is to be removed from memory.
4360 */
4361 static void __exit s2io_rem_nic(struct pci_dev *pdev)
4362 {
4363         struct net_device *dev =
4364             (struct net_device *) pci_get_drvdata(pdev);
4365         nic_t *sp;
4366
4367         if (dev == NULL) {
4368                 DBG_PRINT(ERR_DBG, "Driver Data is NULL!!\n");
4369                 return;
4370         }
4371         sp = dev->priv;
4372         freeSharedMem(sp);
4373         iounmap(sp->bar0);
4374         iounmap(sp->bar1);
4375         pci_disable_device(pdev);
4376         pci_release_regions(pdev);
4377         pci_set_drvdata(pdev, NULL);
4378
4379         unregister_netdev(dev);
4380
4381         free_netdev(dev);
4382 }
4383
4384 int __init s2io_starter(void)
4385 {
4386         return pci_module_init(&s2io_driver);
4387 }
4388
4389 void s2io_closer(void)
4390 {
4391         pci_unregister_driver(&s2io_driver);
4392         DBG_PRINT(INIT_DBG, "cleanup done\n");
4393 }
4394
4395 module_init(s2io_starter);
4396 module_exit(s2io_closer);