]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/netxen/netxen_nic_main.c
netxen: mtu, mac, link status changes
[mirror_ubuntu-artful-kernel.git] / drivers / net / netxen / netxen_nic_main.c
CommitLineData
3d396eb1
AK
1/*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
80922fbc 4 *
3d396eb1
AK
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
80922fbc 9 *
3d396eb1
AK
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
80922fbc 14 *
3d396eb1
AK
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
80922fbc 19 *
3d396eb1
AK
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
80922fbc 22 *
3d396eb1
AK
23 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
28 *
29 *
30 * Main source file for NetXen NIC Driver on Linux
31 *
32 */
33
cb8011ad 34#include <linux/vmalloc.h>
ed25ffa1 35#include <linux/highmem.h>
3d396eb1
AK
36#include "netxen_nic_hw.h"
37
38#include "netxen_nic.h"
3d396eb1 39#include "netxen_nic_phan_reg.h"
3d396eb1 40
1494a814 41#include <linux/dma-mapping.h>
c9bdd4b5 42#include <net/ip.h>
1494a814 43
3d396eb1
AK
44MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45MODULE_LICENSE("GPL");
46MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
47
ceded32f 48char netxen_nic_driver_name[] = "netxen_nic";
3d396eb1 49static char netxen_nic_driver_string[] = "NetXen Network Driver version "
cb8011ad 50 NETXEN_NIC_LINUX_VERSIONID;
3d396eb1 51
2956640d
DP
52static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
53
54/* Default to restricted 1G auto-neg mode */
55static int wol_port_mode = 5;
56
57static int use_msi = 1;
58
59static int use_msi_x = 1;
60
3d396eb1
AK
61#define NETXEN_NETDEV_WEIGHT 120
62#define NETXEN_ADAPTER_UP_MAGIC 777
cb8011ad 63#define NETXEN_NIC_PEG_TUNE 0
3d396eb1
AK
64
65/* Local functions to NetXen NIC driver */
66static int __devinit netxen_nic_probe(struct pci_dev *pdev,
2956640d 67 const struct pci_device_id *ent);
3d396eb1
AK
68static void __devexit netxen_nic_remove(struct pci_dev *pdev);
69static int netxen_nic_open(struct net_device *netdev);
70static int netxen_nic_close(struct net_device *netdev);
71static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
72static void netxen_tx_timeout(struct net_device *netdev);
6d5aefb8 73static void netxen_tx_timeout_task(struct work_struct *work);
3d396eb1 74static void netxen_watchdog(unsigned long);
bea3348e 75static int netxen_nic_poll(struct napi_struct *napi, int budget);
3d396eb1
AK
76#ifdef CONFIG_NET_POLL_CONTROLLER
77static void netxen_nic_poll_controller(struct net_device *netdev);
78#endif
1494a814 79static irqreturn_t netxen_intr(int irq, void *data);
05aaa02d 80static irqreturn_t netxen_msi_intr(int irq, void *data);
3d396eb1
AK
81
82/* PCI Device ID Table */
70081ac5
AV
83#define ENTRY(device) \
84 {PCI_DEVICE(0x4040, (device)), \
85 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
86
3d396eb1 87static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
70081ac5
AV
88 ENTRY(0x0001),
89 ENTRY(0x0002),
90 ENTRY(0x0003),
91 ENTRY(0x0004),
92 ENTRY(0x0005),
93 ENTRY(0x0024),
94 ENTRY(0x0025),
e4c93c81 95 ENTRY(0x0100),
3d396eb1
AK
96 {0,}
97};
98
99MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
100
b1555130
AB
101/*
102 * In netxen_nic_down(), we must wait for any pending callback requests into
103 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
104 * reenabled right after it is deleted in netxen_nic_down().
105 * FLUSH_SCHEDULED_WORK() does this synchronization.
106 *
107 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
108 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
109 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
110 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
111 * linkwatch_event() to be executed which also attempts to acquire the rtnl
112 * lock thus causing a deadlock.
113 */
114
115static struct workqueue_struct *netxen_workq;
116#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
117#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
118
ed25ffa1
AK
119static void netxen_watchdog(unsigned long);
120
7830b22c
DP
121static uint32_t crb_cmd_producer[4] = {
122 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
123 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
124};
125
c9fc891f 126void
3ce06a32
DP
127netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
128 uint32_t crb_producer)
3176ff3e 129{
3ce06a32
DP
130 adapter->pci_write_normalize(adapter,
131 adapter->crb_addr_cmd_producer, crb_producer);
3176ff3e
MT
132}
133
7830b22c
DP
134static uint32_t crb_cmd_consumer[4] = {
135 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
136 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
137};
138
3ce06a32
DP
139static inline void
140netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
141 u32 crb_consumer)
3176ff3e 142{
3ce06a32
DP
143 adapter->pci_write_normalize(adapter,
144 adapter->crb_addr_cmd_consumer, crb_consumer);
3176ff3e
MT
145}
146
2956640d 147static uint32_t msi_tgt_status[8] = {
443be796 148 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
2956640d
DP
149 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
150 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
151 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
443be796
DP
152};
153
2956640d
DP
154static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
155
4638aef1
SH
156static void netxen_nic_disable_int(struct netxen_adapter *adapter)
157{
443be796 158 u32 mask = 0x7ff;
4638aef1 159 int retries = 32;
443be796 160 int pci_fn = adapter->ahw.pci_func;
4638aef1 161
f0c88f9c 162 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
48bfd1e0
DP
163 adapter->pci_write_normalize(adapter,
164 adapter->crb_intr_mask, 0);
4638aef1
SH
165
166 if (adapter->intr_scheme != -1 &&
167 adapter->intr_scheme != INTR_SCHEME_PERPORT)
3ce06a32 168 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
4638aef1 169
2956640d 170 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
4638aef1 171 do {
3ce06a32
DP
172 adapter->pci_write_immediate(adapter,
173 ISR_INT_TARGET_STATUS, 0xffffffff);
174 mask = adapter->pci_read_immediate(adapter,
175 ISR_INT_VECTOR);
4638aef1
SH
176 if (!(mask & 0x80))
177 break;
178 udelay(10);
179 } while (--retries);
180
181 if (!retries) {
182 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
183 netxen_nic_driver_name);
184 }
443be796
DP
185 } else {
186 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
3ce06a32
DP
187 adapter->pci_write_immediate(adapter,
188 msi_tgt_status[pci_fn], 0xffffffff);
443be796 189 }
4638aef1 190 }
4638aef1
SH
191}
192
193static void netxen_nic_enable_int(struct netxen_adapter *adapter)
194{
195 u32 mask;
196
197 DPRINTK(1, INFO, "Entered ISR Enable \n");
198
199 if (adapter->intr_scheme != -1 &&
200 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
201 switch (adapter->ahw.board_type) {
202 case NETXEN_NIC_GBE:
203 mask = 0x77b;
204 break;
205 case NETXEN_NIC_XGBE:
206 mask = 0x77f;
207 break;
208 default:
209 mask = 0x7ff;
210 break;
211 }
212
3ce06a32 213 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
4638aef1
SH
214 }
215
48bfd1e0 216 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
4638aef1 217
2956640d 218 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
4638aef1
SH
219 mask = 0xbff;
220 if (adapter->intr_scheme != -1 &&
221 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
3ce06a32
DP
222 adapter->pci_write_normalize(adapter,
223 CRB_INT_VECTOR, 0);
4638aef1 224 }
3ce06a32
DP
225 adapter->pci_write_immediate(adapter,
226 ISR_INT_TARGET_MASK, mask);
4638aef1
SH
227 }
228
229 DPRINTK(1, INFO, "Done with enable Int\n");
230}
231
2956640d
DP
232static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
233{
234 struct pci_dev *pdev = adapter->pdev;
235 int err;
236 uint64_t mask;
237
238#ifdef CONFIG_IA64
239 adapter->dma_mask = DMA_32BIT_MASK;
240#else
241 if (revision_id >= NX_P3_B0) {
242 /* should go to DMA_64BIT_MASK */
243 adapter->dma_mask = DMA_39BIT_MASK;
244 mask = DMA_39BIT_MASK;
245 } else if (revision_id == NX_P3_A2) {
246 adapter->dma_mask = DMA_39BIT_MASK;
247 mask = DMA_39BIT_MASK;
248 } else if (revision_id == NX_P2_C1) {
249 adapter->dma_mask = DMA_35BIT_MASK;
250 mask = DMA_35BIT_MASK;
251 } else {
252 adapter->dma_mask = DMA_32BIT_MASK;
253 mask = DMA_32BIT_MASK;
254 goto set_32_bit_mask;
255 }
256
257 /*
258 * Consistent DMA mask is set to 32 bit because it cannot be set to
259 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
260 * come off this pool.
261 */
262 if (pci_set_dma_mask(pdev, mask) == 0 &&
263 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
264 adapter->pci_using_dac = 1;
265 return 0;
266 }
267#endif /* CONFIG_IA64 */
268
269set_32_bit_mask:
270 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
271 if (!err)
272 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
273 if (err) {
274 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
275 return err;
276 }
277
278 adapter->pci_using_dac = 0;
279 return 0;
280}
281
282static void netxen_check_options(struct netxen_adapter *adapter)
283{
284 switch (adapter->ahw.boardcfg.board_type) {
285 case NETXEN_BRDTYPE_P3_HMEZ:
286 case NETXEN_BRDTYPE_P3_XG_LOM:
287 case NETXEN_BRDTYPE_P3_10G_CX4:
288 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
289 case NETXEN_BRDTYPE_P3_IMEZ:
290 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
291 case NETXEN_BRDTYPE_P3_10G_XFP:
292 case NETXEN_BRDTYPE_P3_10000_BASE_T:
293 adapter->msix_supported = !!use_msi_x;
294 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
295 break;
296
297 case NETXEN_BRDTYPE_P2_SB31_10G:
298 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
299 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
300 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
301 adapter->msix_supported = 0;
302 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
303 break;
304
305 case NETXEN_BRDTYPE_P3_REF_QG:
306 case NETXEN_BRDTYPE_P3_4_GB:
307 case NETXEN_BRDTYPE_P3_4_GB_MM:
308 case NETXEN_BRDTYPE_P2_SB35_4G:
309 case NETXEN_BRDTYPE_P2_SB31_2G:
310 adapter->msix_supported = 0;
311 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
312 break;
313
314 default:
315 adapter->msix_supported = 0;
316 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
317
318 printk(KERN_WARNING "Unknown board type(0x%x)\n",
319 adapter->ahw.boardcfg.board_type);
320 break;
321 }
322
323 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
324 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
325 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
326
327 adapter->max_possible_rss_rings = 1;
328 return;
329}
330
331static int
332netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
333{
334 int ret = 0;
335
336 if (first_boot == 0x55555555) {
337 /* This is the first boot after power up */
338
339 /* PCI bus master workaround */
340 adapter->hw_read_wx(adapter,
341 NETXEN_PCIE_REG(0x4), &first_boot, 4);
342 if (!(first_boot & 0x4)) {
343 first_boot |= 0x4;
344 adapter->hw_write_wx(adapter,
345 NETXEN_PCIE_REG(0x4), &first_boot, 4);
346 adapter->hw_read_wx(adapter,
347 NETXEN_PCIE_REG(0x4), &first_boot, 4);
348 }
349
350 /* This is the first boot after power up */
351 adapter->hw_read_wx(adapter,
352 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
353 if (first_boot != 0x80000f) {
354 /* clear the register for future unloads/loads */
355 adapter->pci_write_normalize(adapter,
356 NETXEN_CAM_RAM(0x1fc), 0);
357 ret = -1;
358 }
359
360 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
361 /* Start P2 boot loader */
362 adapter->pci_write_normalize(adapter,
363 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
364 adapter->pci_write_normalize(adapter,
365 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
366 }
367 }
368 return ret;
369}
370
371static void netxen_set_port_mode(struct netxen_adapter *adapter)
372{
373 u32 val, data;
374
375 val = adapter->ahw.boardcfg.board_type;
376 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
377 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
378 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
379 data = NETXEN_PORT_MODE_802_3_AP;
380 adapter->hw_write_wx(adapter,
381 NETXEN_PORT_MODE_ADDR, &data, 4);
382 } else if (port_mode == NETXEN_PORT_MODE_XG) {
383 data = NETXEN_PORT_MODE_XG;
384 adapter->hw_write_wx(adapter,
385 NETXEN_PORT_MODE_ADDR, &data, 4);
386 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
387 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
388 adapter->hw_write_wx(adapter,
389 NETXEN_PORT_MODE_ADDR, &data, 4);
390 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
391 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
392 adapter->hw_write_wx(adapter,
393 NETXEN_PORT_MODE_ADDR, &data, 4);
394 } else {
395 data = NETXEN_PORT_MODE_AUTO_NEG;
396 adapter->hw_write_wx(adapter,
397 NETXEN_PORT_MODE_ADDR, &data, 4);
398 }
399
400 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
401 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
402 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
403 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
404 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
405 }
406 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
407 &wol_port_mode, 4);
408 }
409}
410
411#define PCI_CAP_ID_GEN 0x10
412
413static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
414{
415 u32 pdevfuncsave;
416 u32 c8c9value = 0;
417 u32 chicken = 0;
418 u32 control = 0;
419 int i, pos;
420 struct pci_dev *pdev;
421
422 pdev = pci_get_device(0x1166, 0x0140, NULL);
423 if (pdev) {
424 pci_dev_put(pdev);
425 adapter->hw_read_wx(adapter,
426 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
427 chicken |= 0x4000;
428 adapter->hw_write_wx(adapter,
429 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
430 }
431
432 pdev = adapter->pdev;
433
434 adapter->hw_read_wx(adapter,
435 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
436 /* clear chicken3.25:24 */
437 chicken &= 0xFCFFFFFF;
438 /*
439 * if gen1 and B0, set F1020 - if gen 2, do nothing
440 * if gen2 set to F1000
441 */
442 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
443 if (pos == 0xC0) {
444 pci_read_config_dword(pdev, pos + 0x10, &control);
445 if ((control & 0x000F0000) != 0x00020000) {
446 /* set chicken3.24 if gen1 */
447 chicken |= 0x01000000;
448 }
449 printk(KERN_INFO "%s Gen2 strapping detected\n",
450 netxen_nic_driver_name);
451 c8c9value = 0xF1000;
452 } else {
453 /* set chicken3.24 if gen1 */
454 chicken |= 0x01000000;
455 printk(KERN_INFO "%s Gen1 strapping detected\n",
456 netxen_nic_driver_name);
457 if (adapter->ahw.revision_id == NX_P3_B0)
458 c8c9value = 0xF1020;
459 else
460 c8c9value = 0;
461
462 }
463 adapter->hw_write_wx(adapter,
464 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
465
466 if (!c8c9value)
467 return;
468
469 pdevfuncsave = pdev->devfn;
470 if (pdevfuncsave & 0x07)
471 return;
472
473 for (i = 0; i < 8; i++) {
474 pci_read_config_dword(pdev, pos + 8, &control);
475 pci_read_config_dword(pdev, pos + 8, &control);
476 pci_write_config_dword(pdev, pos + 8, c8c9value);
477 pdev->devfn++;
478 }
479 pdev->devfn = pdevfuncsave;
480}
481
482static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
483{
484 u32 control;
485 int pos;
486
487 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
488 if (pos) {
489 pci_read_config_dword(pdev, pos, &control);
490 if (enable)
491 control |= PCI_MSIX_FLAGS_ENABLE;
492 else
493 control = 0;
494 pci_write_config_dword(pdev, pos, control);
495 }
496}
497
498static void netxen_init_msix_entries(struct netxen_adapter *adapter)
499{
500 int i;
501
502 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
503 adapter->msix_entries[i].entry = i;
504}
505
3d396eb1
AK
506/*
507 * netxen_nic_probe()
508 *
509 * The Linux system will invoke this after identifying the vendor ID and
510 * device Id in the pci_tbl supported by this module.
511 *
512 * A quad port card has one operational PCI config space, (function 0),
513 * which is used to access all four ports.
514 *
515 * This routine will initialize the adapter, and setup the global parameters
516 * along with the port's specific structure.
517 */
518static int __devinit
519netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
520{
521 struct net_device *netdev = NULL;
522 struct netxen_adapter *adapter = NULL;
ed25ffa1
AK
523 void __iomem *mem_ptr0 = NULL;
524 void __iomem *mem_ptr1 = NULL;
525 void __iomem *mem_ptr2 = NULL;
6c80b18d
MT
526 unsigned long first_page_group_end;
527 unsigned long first_page_group_start;
528
cb8011ad 529
a608ab9c 530 u8 __iomem *db_ptr = NULL;
2956640d
DP
531 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
532 int i = 0, err;
533 int first_driver, first_boot;
f305f789 534 __le64 mac_addr[FLASH_NUM_PORTS + 1];
6c80b18d 535 u32 val;
3176ff3e 536 int pci_func_id = PCI_FUNC(pdev->devfn);
0795af57 537 DECLARE_MAC_BUF(mac);
2956640d
DP
538 struct netxen_legacy_intr_set *legacy_intrp;
539 uint8_t revision_id;
3d396eb1 540
dcd56fdb 541 if (pci_func_id == 0)
2956640d 542 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
6c80b18d 543
3176ff3e 544 if (pdev->class != 0x020000) {
dcd56fdb 545 printk(KERN_DEBUG "NetXen function %d, class %x will not "
3176ff3e 546 "be enabled.\n",pci_func_id, pdev->class);
ed25ffa1
AK
547 return -ENODEV;
548 }
2956640d 549
3d396eb1
AK
550 if ((err = pci_enable_device(pdev)))
551 return err;
2956640d 552
3d396eb1
AK
553 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
554 err = -ENODEV;
555 goto err_out_disable_pdev;
556 }
557
558 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
559 goto err_out_disable_pdev;
560
561 pci_set_master(pdev);
3176ff3e
MT
562
563 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
564 if(!netdev) {
565 printk(KERN_ERR"%s: Failed to allocate memory for the "
566 "device block.Check system memory resource"
567 " usage.\n", netxen_nic_driver_name);
568 goto err_out_free_res;
569 }
570
3176ff3e
MT
571 SET_NETDEV_DEV(netdev, &pdev->dev);
572
573 adapter = netdev->priv;
2956640d
DP
574 adapter->netdev = netdev;
575 adapter->pdev = pdev;
13ba9c77 576 adapter->ahw.pci_func = pci_func_id;
2956640d
DP
577
578 revision_id = pdev->revision;
579 adapter->ahw.revision_id = revision_id;
580
581 err = nx_set_dma_mask(adapter, revision_id);
582 if (err)
583 goto err_out_free_netdev;
584
3ce06a32
DP
585 rwlock_init(&adapter->adapter_lock);
586 adapter->ahw.qdr_sn_window = -1;
587 adapter->ahw.ddr_mn_window = -1;
13ba9c77 588
3d396eb1
AK
589 /* remap phys address */
590 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
591 mem_len = pci_resource_len(pdev, 0);
3ce06a32
DP
592 pci_len0 = 0;
593
594 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
595 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
596 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
597 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
598 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
599 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
600 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
601 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
602 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
3d396eb1
AK
603
604 /* 128 Meg of memory */
6c80b18d
MT
605 if (mem_len == NETXEN_PCI_128MB_SIZE) {
606 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
607 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
608 SECOND_PAGE_GROUP_SIZE);
609 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
610 THIRD_PAGE_GROUP_SIZE);
611 first_page_group_start = FIRST_PAGE_GROUP_START;
612 first_page_group_end = FIRST_PAGE_GROUP_END;
613 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
614 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
615 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
616 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
617 first_page_group_start = 0;
618 first_page_group_end = 0;
3ce06a32
DP
619 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
620 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
621 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
622 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
623 adapter->pci_write_immediate =
624 netxen_nic_pci_write_immediate_2M;
625 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
626 adapter->pci_write_normalize =
627 netxen_nic_pci_write_normalize_2M;
628 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
629 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
630 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
631
632 mem_ptr0 = ioremap(mem_base, mem_len);
633 pci_len0 = mem_len;
634 first_page_group_start = 0;
635 first_page_group_end = 0;
636
637 adapter->ahw.ddr_mn_window = 0;
638 adapter->ahw.qdr_sn_window = 0;
639
640 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
641 (pci_func_id * 0x20);
642 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
643 if (pci_func_id < 4)
644 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
645 else
646 adapter->ahw.ms_win_crb +=
647 0xA0 + ((pci_func_id - 4) * 0x10);
6c80b18d 648 } else {
4790654c 649 err = -EIO;
6c80b18d
MT
650 goto err_out_free_netdev;
651 }
cb8011ad 652
3ce06a32 653 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
cb8011ad 654
ed25ffa1
AK
655 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
656 db_len = pci_resource_len(pdev, 4);
657
658 if (db_len == 0) {
659 printk(KERN_ERR "%s: doorbell is disabled\n",
2956640d 660 netxen_nic_driver_name);
ed25ffa1
AK
661 err = -EIO;
662 goto err_out_iounmap;
663 }
664 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
665 db_len);
666
667 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
a608ab9c 668 if (!db_ptr) {
ed25ffa1 669 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
2956640d 670 netxen_nic_driver_name);
ed25ffa1
AK
671 err = -EIO;
672 goto err_out_iounmap;
3d396eb1 673 }
ed25ffa1 674 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
3d396eb1 675
6c80b18d 676 adapter->ahw.pci_base0 = mem_ptr0;
3ce06a32 677 adapter->ahw.pci_len0 = pci_len0;
6c80b18d
MT
678 adapter->ahw.first_page_group_start = first_page_group_start;
679 adapter->ahw.first_page_group_end = first_page_group_end;
680 adapter->ahw.pci_base1 = mem_ptr1;
681 adapter->ahw.pci_base2 = mem_ptr2;
682 adapter->ahw.db_base = db_ptr;
683 adapter->ahw.db_len = db_len;
3d396eb1 684
bea3348e 685 netif_napi_add(netdev, &adapter->napi,
2956640d
DP
686 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
687
688 if (revision_id >= NX_P3_B0)
689 legacy_intrp = &legacy_intr[pci_func_id];
690 else
691 legacy_intrp = &legacy_intr[0];
692
693 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
694 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
695 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
696 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
bea3348e 697
2d1a3bbd 698 /* this will be read from FW later */
699 adapter->intr_scheme = -1;
443be796 700 adapter->msi_mode = -1;
2d1a3bbd 701
702 /* This will be reset for mezz cards */
3176ff3e 703 adapter->portnum = pci_func_id;
2d1a3bbd 704 adapter->status &= ~NETXEN_NETDEV_STATUS;
200eef20 705 adapter->rx_csum = 1;
623621b0 706 adapter->mc_enabled = 0;
48bfd1e0 707 if (NX_IS_REVISION_P3(revision_id)) {
2956640d 708 adapter->max_mc_count = 38;
48bfd1e0
DP
709 adapter->max_rds_rings = 2;
710 } else {
2956640d 711 adapter->max_mc_count = 16;
48bfd1e0
DP
712 adapter->max_rds_rings = 3;
713 }
3176ff3e
MT
714
715 netdev->open = netxen_nic_open;
716 netdev->stop = netxen_nic_close;
717 netdev->hard_start_xmit = netxen_nic_xmit_frame;
4790654c 718 netdev->get_stats = netxen_nic_get_stats;
c9fc891f
DP
719 if (NX_IS_REVISION_P3(revision_id))
720 netdev->set_multicast_list = netxen_p3_nic_set_multi;
721 else
722 netdev->set_multicast_list = netxen_p2_nic_set_multi;
3176ff3e
MT
723 netdev->set_mac_address = netxen_nic_set_mac;
724 netdev->change_mtu = netxen_nic_change_mtu;
725 netdev->tx_timeout = netxen_tx_timeout;
05aaa02d 726 netdev->watchdog_timeo = 2*HZ;
3176ff3e
MT
727
728 netxen_nic_change_mtu(netdev, netdev->mtu);
729
730 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
3176ff3e
MT
731#ifdef CONFIG_NET_POLL_CONTROLLER
732 netdev->poll_controller = netxen_nic_poll_controller;
733#endif
734 /* ScatterGather support */
735 netdev->features = NETIF_F_SG;
736 netdev->features |= NETIF_F_IP_CSUM;
737 netdev->features |= NETIF_F_TSO;
2956640d
DP
738 if (NX_IS_REVISION_P3(revision_id))
739 netdev->features |= NETIF_F_TSO6;
3176ff3e 740
2956640d 741 if (adapter->pci_using_dac)
3176ff3e
MT
742 netdev->features |= NETIF_F_HIGHDMA;
743
6c80b18d
MT
744 /*
745 * Set the CRB window to invalid. If any register in window 0 is
746 * accessed it should set the window to 0 and then reset it to 1.
747 */
748 adapter->curr_window = 255;
749
dcd56fdb
DP
750 if (netxen_nic_get_board_info(adapter) != 0) {
751 printk("%s: Error getting board config info.\n",
2956640d 752 netxen_nic_driver_name);
dcd56fdb
DP
753 err = -EIO;
754 goto err_out_iounmap;
755 }
6c80b18d 756
6c80b18d
MT
757 netxen_initialize_adapter_ops(adapter);
758
13ba9c77 759 /* Mezz cards have PCI function 0,2,3 enabled */
dc515f2e
DP
760 switch (adapter->ahw.boardcfg.board_type) {
761 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
762 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
763 if (pci_func_id >= 2)
13ba9c77 764 adapter->portnum = pci_func_id - 2;
dc515f2e
DP
765 break;
766 default:
767 break;
768 }
13ba9c77 769
2956640d
DP
770 /*
771 * This call will setup various max rx/tx counts.
772 * It must be done before any buffer/ring allocations.
773 */
774 netxen_check_options(adapter);
3d396eb1 775
2956640d
DP
776 first_driver = 0;
777 if (NX_IS_REVISION_P3(revision_id)) {
778 if (adapter->ahw.pci_func == 0)
779 first_driver = 1;
780 } else {
781 if (adapter->portnum == 0)
782 first_driver = 1;
783 }
7830b22c
DP
784 adapter->crb_addr_cmd_producer = crb_cmd_producer[adapter->portnum];
785 adapter->crb_addr_cmd_consumer = crb_cmd_consumer[adapter->portnum];
3176ff3e
MT
786 netxen_nic_update_cmd_producer(adapter, 0);
787 netxen_nic_update_cmd_consumer(adapter, 0);
3d396eb1 788
2956640d
DP
789 if (first_driver) {
790 first_boot = adapter->pci_read_normalize(adapter,
791 NETXEN_CAM_RAM(0x1fc));
3176ff3e 792
2956640d
DP
793 err = netxen_check_hw_init(adapter, first_boot);
794 if (err) {
795 printk(KERN_ERR "%s: error in init HW init sequence\n",
796 netxen_nic_driver_name);
797 goto err_out_iounmap;
3176ff3e 798 }
3176ff3e 799
2956640d
DP
800 if (NX_IS_REVISION_P3(revision_id))
801 netxen_set_port_mode(adapter);
802
803 if (first_boot != 0x55555555) {
3ce06a32
DP
804 adapter->pci_write_normalize(adapter,
805 CRB_CMDPEG_STATE, 0);
439b454e
DP
806 netxen_pinit_from_rom(adapter, 0);
807 msleep(1);
808 netxen_load_firmware(adapter);
2956640d
DP
809 }
810
811 if (NX_IS_REVISION_P3(revision_id))
812 netxen_pcie_strap_init(adapter);
813
814 if (NX_IS_REVISION_P2(revision_id)) {
623621b0
DP
815
816 /* Initialize multicast addr pool owners */
817 val = 0x7654;
818 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
819 val |= 0x0f000000;
820 netxen_crb_writelit_adapter(adapter,
821 NETXEN_MAC_ADDR_CNTL_REG, val);
822
6c80b18d 823 }
96acb6eb 824
2956640d
DP
825 if ((first_boot == 0x55555555) &&
826 (NX_IS_REVISION_P2(revision_id))) {
827 /* Unlock the HW, prompting the boot sequence */
828 adapter->pci_write_normalize(adapter,
829 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
830 }
831
832 err = netxen_initialize_adapter_offload(adapter);
833 if (err)
834 goto err_out_iounmap;
6c80b18d
MT
835
836 /*
837 * Tell the hardware our version number.
838 */
4790654c 839 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
6c80b18d
MT
840 | ((_NETXEN_NIC_LINUX_MINOR << 8))
841 | (_NETXEN_NIC_LINUX_SUBVERSION);
3ce06a32 842 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
3d396eb1 843
3176ff3e
MT
844 /* Handshake with the card before we register the devices. */
845 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
2956640d
DP
846
847 if (NX_IS_REVISION_P3(revision_id)) {
848 adapter->hw_read_wx(adapter,
849 NETXEN_MIU_MN_CONTROL, &val, 4);
850 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
851 dev_info(&pdev->dev, "firmware running in %s mode\n",
852 adapter->ahw.cut_through ? "cut through" : "legacy");
853 }
854 } /* first_driver */
855
856 netxen_nic_flash_print(adapter);
3d396eb1 857
3d396eb1 858 /*
6c80b18d 859 * See if the firmware gave us a virtual-physical port mapping.
3d396eb1 860 */
dcd56fdb 861 adapter->physical_port = adapter->portnum;
3ce06a32 862 i = adapter->pci_read_normalize(adapter, CRB_V2P(adapter->portnum));
6c80b18d 863 if (i != 0x55555555)
3276fbad 864 adapter->physical_port = i;
6c80b18d 865
2956640d
DP
866 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
867
868 netxen_set_msix_bit(pdev, 0);
869
870 if (NX_IS_REVISION_P3(revision_id)) {
871 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
872 mem_len != NETXEN_PCI_2MB_SIZE)
873 adapter->msix_supported = 0;
874 }
875
876 if (adapter->msix_supported) {
877
878 netxen_init_msix_entries(adapter);
879
880 if (pci_enable_msix(pdev, adapter->msix_entries,
881 MSIX_ENTRIES_PER_ADAPTER))
882 goto request_msi;
883
884 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
885 netxen_set_msix_bit(pdev, 1);
886 dev_info(&pdev->dev, "using msi-x interrupts\n");
887
888 } else {
889request_msi:
890 if (use_msi && !pci_enable_msi(pdev)) {
891 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
892 dev_info(&pdev->dev, "using msi interrupts\n");
893 } else
894 dev_info(&pdev->dev, "using legacy interrupts\n");
895 }
896
897 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
898 netdev->irq = adapter->msix_entries[0].vector;
899 else
900 netdev->irq = pdev->irq;
901
902 err = netxen_receive_peg_ready(adapter);
903 if (err)
904 goto err_out_disable_msi;
905
906 init_timer(&adapter->watchdog_timer);
a97342f9 907 adapter->ahw.linkup = 0;
2956640d
DP
908 adapter->watchdog_timer.function = &netxen_watchdog;
909 adapter->watchdog_timer.data = (unsigned long)adapter;
910 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
911 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
912
913 if (netxen_is_flash_supported(adapter) == 0 &&
914 netxen_get_flash_mac_addr(adapter, mac_addr) == 0) {
915 unsigned char *p;
916
917 p = (unsigned char *)&mac_addr[adapter->portnum];
918 netdev->dev_addr[0] = *(p + 5);
919 netdev->dev_addr[1] = *(p + 4);
920 netdev->dev_addr[2] = *(p + 3);
921 netdev->dev_addr[3] = *(p + 2);
922 netdev->dev_addr[4] = *(p + 1);
923 netdev->dev_addr[5] = *(p + 0);
924
925 memcpy(netdev->perm_addr, netdev->dev_addr,
926 netdev->addr_len);
927 if (!is_valid_ether_addr(netdev->perm_addr)) {
928 printk(KERN_ERR "%s: Bad MAC address %s.\n",
929 netxen_nic_driver_name,
930 print_mac(mac, netdev->dev_addr));
931 } else {
932 adapter->macaddr_set(adapter, netdev->dev_addr);
933 }
934 }
935
3176ff3e
MT
936 netif_carrier_off(netdev);
937 netif_stop_queue(netdev);
938
6c80b18d
MT
939 if ((err = register_netdev(netdev))) {
940 printk(KERN_ERR "%s: register_netdev failed port #%d"
941 " aborting\n", netxen_nic_driver_name,
942 adapter->portnum);
943 err = -EIO;
2956640d 944 goto err_out_disable_msi;
6c80b18d
MT
945 }
946
947 pci_set_drvdata(pdev, adapter);
3d396eb1 948
2956640d
DP
949 switch (adapter->ahw.board_type) {
950 case NETXEN_NIC_GBE:
951 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
952 adapter->netdev->name);
953 break;
954 case NETXEN_NIC_XGBE:
955 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
956 adapter->netdev->name);
957 break;
3d396eb1 958 }
3d396eb1 959
2956640d
DP
960 return 0;
961
962err_out_disable_msi:
963 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
964 pci_disable_msix(pdev);
6c80b18d
MT
965 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
966 pci_disable_msi(pdev);
967
2956640d
DP
968 if (first_driver)
969 netxen_free_adapter_offload(adapter);
3d396eb1 970
2956640d 971err_out_iounmap:
ed25ffa1
AK
972 if (db_ptr)
973 iounmap(db_ptr);
974
ed25ffa1
AK
975 if (mem_ptr0)
976 iounmap(mem_ptr0);
977 if (mem_ptr1)
978 iounmap(mem_ptr1);
979 if (mem_ptr2)
980 iounmap(mem_ptr2);
cb8011ad 981
6c80b18d
MT
982err_out_free_netdev:
983 free_netdev(netdev);
984
985err_out_free_res:
3d396eb1 986 pci_release_regions(pdev);
6c80b18d
MT
987
988err_out_disable_pdev:
2956640d 989 pci_set_drvdata(pdev, NULL);
3d396eb1
AK
990 pci_disable_device(pdev);
991 return err;
992}
993
994static void __devexit netxen_nic_remove(struct pci_dev *pdev)
995{
996 struct netxen_adapter *adapter;
3176ff3e 997 struct net_device *netdev;
3d396eb1 998
6c80b18d 999 adapter = pci_get_drvdata(pdev);
3d396eb1
AK
1000 if (adapter == NULL)
1001 return;
1002
6c80b18d
MT
1003 netdev = adapter->netdev;
1004
96acb6eb
DP
1005 unregister_netdev(netdev);
1006
96acb6eb 1007 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
3d396eb1 1008 netxen_free_hw_resources(adapter);
2956640d 1009 netxen_free_sw_resources(adapter);
96acb6eb 1010 }
3d396eb1 1011
439b454e
DP
1012 if (adapter->portnum == 0)
1013 netxen_free_adapter_offload(adapter);
96acb6eb 1014
3052246c 1015 if (adapter->irq)
1016 free_irq(adapter->irq, adapter);
1017
2956640d
DP
1018 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1019 pci_disable_msix(pdev);
3052246c 1020 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1021 pci_disable_msi(pdev);
1022
6c80b18d
MT
1023 iounmap(adapter->ahw.db_base);
1024 iounmap(adapter->ahw.pci_base0);
1025 iounmap(adapter->ahw.pci_base1);
1026 iounmap(adapter->ahw.pci_base2);
595e3fb8
MT
1027
1028 pci_release_regions(pdev);
3052246c 1029 pci_disable_device(pdev);
595e3fb8
MT
1030 pci_set_drvdata(pdev, NULL);
1031
6c80b18d 1032 free_netdev(netdev);
3d396eb1
AK
1033}
1034
1035/*
1036 * Called when a network interface is made active
1037 * @returns 0 on success, negative value on failure
1038 */
1039static int netxen_nic_open(struct net_device *netdev)
1040{
3176ff3e 1041 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
3d396eb1
AK
1042 int err = 0;
1043 int ctx, ring;
05aaa02d
DP
1044 irq_handler_t handler;
1045 unsigned long flags = IRQF_SAMPLE_RANDOM;
3d396eb1 1046
dcd56fdb
DP
1047 if (adapter->driver_mismatch)
1048 return -EIO;
1049
3d396eb1
AK
1050 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1051 err = netxen_init_firmware(adapter);
1052 if (err != 0) {
1053 printk(KERN_ERR "Failed to init firmware\n");
1054 return -EIO;
1055 }
3d396eb1 1056
2956640d 1057 err = netxen_alloc_sw_resources(adapter);
3d396eb1 1058 if (err) {
2956640d
DP
1059 printk(KERN_ERR "%s: Error in setting sw resources\n",
1060 netdev->name);
3d396eb1
AK
1061 return err;
1062 }
2956640d
DP
1063
1064 netxen_nic_clear_stats(adapter);
1065
1066 err = netxen_alloc_hw_resources(adapter);
1067 if (err) {
1068 printk(KERN_ERR "%s: Error in setting hw resources\n",
1069 netdev->name);
1070 goto err_out_free_sw;
1071 }
1072
1073 if (adapter->fw_major < 4) {
1074 adapter->crb_addr_cmd_producer =
1075 crb_cmd_producer[adapter->portnum];
1076 adapter->crb_addr_cmd_consumer =
1077 crb_cmd_consumer[adapter->portnum];
1078 }
1079
1080 netxen_nic_update_cmd_producer(adapter, 0);
1081 netxen_nic_update_cmd_consumer(adapter, 0);
1082
3d396eb1 1083 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
48bfd1e0 1084 for (ring = 0; ring < adapter->max_rds_rings; ring++)
3d396eb1 1085 netxen_post_rx_buffers(adapter, ctx, ring);
3d396eb1 1086 }
2956640d 1087 if (NETXEN_IS_MSI_FAMILY(adapter))
05aaa02d
DP
1088 handler = netxen_msi_intr;
1089 else {
1090 flags |= IRQF_SHARED;
1091 handler = netxen_intr;
1092 }
2956640d 1093 adapter->irq = netdev->irq;
05aaa02d
DP
1094 err = request_irq(adapter->irq, handler,
1095 flags, netdev->name, adapter);
3d396eb1
AK
1096 if (err) {
1097 printk(KERN_ERR "request_irq failed with: %d\n", err);
2956640d 1098 goto err_out_free_hw;
3d396eb1 1099 }
ed25ffa1
AK
1100
1101 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1102 }
2956640d 1103
3d396eb1
AK
1104 /* Done here again so that even if phantom sw overwrote it,
1105 * we set it */
c9fc891f 1106 err = adapter->init_port(adapter, adapter->physical_port);
2956640d 1107 if (err) {
595e3fb8
MT
1108 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1109 netxen_nic_driver_name, adapter->portnum);
2956640d 1110 goto err_out_free_irq;
595e3fb8 1111 }
2956640d 1112 adapter->macaddr_set(adapter, netdev->dev_addr);
595e3fb8 1113
3176ff3e 1114 netxen_nic_set_link_parameters(adapter);
3d396eb1 1115
c9fc891f
DP
1116 netdev->set_multicast_list(netdev);
1117 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1118 nx_fw_cmd_set_mtu(adapter, netdev->mtu);
1119 else
1120 adapter->set_mtu(adapter, netdev->mtu);
ed25ffa1 1121
dcd56fdb 1122 mod_timer(&adapter->watchdog_timer, jiffies);
05aaa02d
DP
1123
1124 napi_enable(&adapter->napi);
1125 netxen_nic_enable_int(adapter);
1126
dcd56fdb 1127 netif_start_queue(netdev);
3d396eb1
AK
1128
1129 return 0;
2956640d
DP
1130
1131err_out_free_irq:
1132 free_irq(adapter->irq, adapter);
1133err_out_free_hw:
1134 netxen_free_hw_resources(adapter);
1135err_out_free_sw:
1136 netxen_free_sw_resources(adapter);
1137 return err;
3d396eb1
AK
1138}
1139
1140/*
1141 * netxen_nic_close - Disables a network interface entry point
1142 */
1143static int netxen_nic_close(struct net_device *netdev)
1144{
3176ff3e 1145 struct netxen_adapter *adapter = netdev_priv(netdev);
3d396eb1
AK
1146
1147 netif_carrier_off(netdev);
1148 netif_stop_queue(netdev);
bea3348e 1149 napi_disable(&adapter->napi);
3d396eb1 1150
72b0a7a8 1151 if (adapter->stop_port)
1152 adapter->stop_port(adapter);
1153
e01872af 1154 netxen_nic_disable_int(adapter);
1155
2956640d
DP
1156 netxen_release_tx_buffers(adapter);
1157
96acb6eb
DP
1158 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1159 FLUSH_SCHEDULED_WORK();
1160 del_timer_sync(&adapter->watchdog_timer);
1161 }
3d396eb1
AK
1162
1163 return 0;
1164}
1165
1166static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1167{
3176ff3e 1168 struct netxen_adapter *adapter = netdev_priv(netdev);
3d396eb1
AK
1169 struct netxen_hardware_context *hw = &adapter->ahw;
1170 unsigned int first_seg_len = skb->len - skb->data_len;
1171 struct netxen_skb_frag *buffrag;
1172 unsigned int i;
1173
ba53e6b4 1174 u32 producer, consumer;
3d396eb1
AK
1175 u32 saved_producer = 0;
1176 struct cmd_desc_type0 *hwdesc;
1177 int k;
1178 struct netxen_cmd_buffer *pbuf = NULL;
3d396eb1 1179 int frag_count;
3d396eb1 1180 int no_of_desc;
ba53e6b4 1181 u32 num_txd = adapter->max_tx_desc_count;
3d396eb1 1182
3d396eb1
AK
1183 frag_count = skb_shinfo(skb)->nr_frags + 1;
1184
3d396eb1
AK
1185 /* There 4 fragments per descriptor */
1186 no_of_desc = (frag_count + 3) >> 2;
cb8011ad
AK
1187 if (netdev->features & NETIF_F_TSO) {
1188 if (skb_shinfo(skb)->gso_size > 0) {
1189
3d396eb1 1190 no_of_desc++;
ab6a5bb6 1191 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
c9bdd4b5 1192 sizeof(struct ethhdr)) >
ed25ffa1 1193 (sizeof(struct cmd_desc_type0) - 2)) {
cb8011ad
AK
1194 no_of_desc++;
1195 }
3d396eb1
AK
1196 }
1197 }
53a01e00 1198
ba53e6b4
DP
1199 producer = adapter->cmd_producer;
1200 smp_mb();
1201 consumer = adapter->last_cmd_consumer;
1202 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1203 netif_stop_queue(netdev);
1204 smp_mb();
1205 return NETDEV_TX_BUSY;
53a01e00 1206 }
3d396eb1 1207
3d396eb1 1208 /* Copy the descriptors into the hardware */
3d396eb1
AK
1209 saved_producer = producer;
1210 hwdesc = &hw->cmd_desc_head[producer];
1211 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1212 /* Take skb->data itself */
1213 pbuf = &adapter->cmd_buf_arr[producer];
cb8011ad 1214 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
a608ab9c 1215 pbuf->mss = skb_shinfo(skb)->gso_size;
ed33ebe4 1216 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
3d396eb1
AK
1217 } else {
1218 pbuf->mss = 0;
1219 hwdesc->mss = 0;
1220 }
3d396eb1
AK
1221 pbuf->total_length = skb->len;
1222 pbuf->skb = skb;
1223 pbuf->cmd = TX_ETHER_PKT;
1224 pbuf->frag_count = frag_count;
3176ff3e 1225 pbuf->port = adapter->portnum;
3d396eb1 1226 buffrag = &pbuf->frag_array[0];
3176ff3e 1227 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
3d396eb1
AK
1228 PCI_DMA_TODEVICE);
1229 buffrag->length = first_seg_len;
ed25ffa1
AK
1230 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1231 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1232 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
3d396eb1 1233
3176ff3e 1234 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
6c80b18d 1235 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
3d396eb1
AK
1236 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1237 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1238
1239 for (i = 1, k = 1; i < frag_count; i++, k++) {
1240 struct skb_frag_struct *frag;
1241 int len, temp_len;
1242 unsigned long offset;
1243 dma_addr_t temp_dma;
1244
1245 /* move to next desc. if there is a need */
1246 if ((i & 0x3) == 0) {
1247 k = 0;
ba53e6b4 1248 producer = get_next_index(producer, num_txd);
3d396eb1
AK
1249 hwdesc = &hw->cmd_desc_head[producer];
1250 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
53a01e00 1251 pbuf = &adapter->cmd_buf_arr[producer];
1252 pbuf->skb = NULL;
3d396eb1
AK
1253 }
1254 frag = &skb_shinfo(skb)->frags[i - 1];
1255 len = frag->size;
1256 offset = frag->page_offset;
1257
1258 temp_len = len;
3176ff3e 1259 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
3d396eb1
AK
1260 len, PCI_DMA_TODEVICE);
1261
1262 buffrag++;
1263 buffrag->dma = temp_dma;
1264 buffrag->length = temp_len;
1265
3d396eb1
AK
1266 switch (k) {
1267 case 0:
1268 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1269 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1270 break;
1271 case 1:
1272 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1273 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1274 break;
1275 case 2:
1276 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1277 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1278 break;
1279 case 3:
a608ab9c 1280 hwdesc->buffer4_length = cpu_to_le16(temp_len);
3d396eb1
AK
1281 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1282 break;
1283 }
1284 frag++;
1285 }
ba53e6b4 1286 producer = get_next_index(producer, num_txd);
3d396eb1
AK
1287
1288 /* might change opcode to TX_TCP_LSO */
1289 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1290
1291 /* For LSO, we need to copy the MAC/IP/TCP headers into
1292 * the descriptor ring
1293 */
ed25ffa1
AK
1294 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1295 == TX_TCP_LSO) {
3d396eb1
AK
1296 int hdr_len, first_hdr_len, more_hdr;
1297 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
ed25ffa1
AK
1298 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1299 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
3d396eb1
AK
1300 more_hdr = 1;
1301 } else {
1302 first_hdr_len = hdr_len;
1303 more_hdr = 0;
1304 }
1305 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1306 hwdesc = &hw->cmd_desc_head[producer];
53a01e00 1307 pbuf = &adapter->cmd_buf_arr[producer];
1308 pbuf->skb = NULL;
3d396eb1
AK
1309
1310 /* copy the first 64 bytes */
ed25ffa1 1311 memcpy(((void *)hwdesc) + 2,
3d396eb1 1312 (void *)(skb->data), first_hdr_len);
ba53e6b4 1313 producer = get_next_index(producer, num_txd);
3d396eb1
AK
1314
1315 if (more_hdr) {
1316 hwdesc = &hw->cmd_desc_head[producer];
53a01e00 1317 pbuf = &adapter->cmd_buf_arr[producer];
1318 pbuf->skb = NULL;
3d396eb1
AK
1319 /* copy the next 64 bytes - should be enough except
1320 * for pathological case
1321 */
d626f62b
ACM
1322 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1323 hwdesc,
1324 (hdr_len -
1325 first_hdr_len));
ba53e6b4 1326 producer = get_next_index(producer, num_txd);
3d396eb1
AK
1327 }
1328 }
6c80b18d 1329
ba53e6b4 1330 adapter->cmd_producer = producer;
5dc16268 1331 adapter->stats.txbytes += skb->len;
6c80b18d 1332
ba53e6b4 1333 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
3d396eb1 1334
ba53e6b4 1335 adapter->stats.xmitcalled++;
3d396eb1
AK
1336 netdev->trans_start = jiffies;
1337
3d396eb1
AK
1338 return NETDEV_TX_OK;
1339}
1340
a97342f9
DP
1341static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1342{
1343 struct net_device *netdev = adapter->netdev;
1344 uint32_t temp, temp_state, temp_val;
1345 int rv = 0;
1346
1347 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1348
1349 temp_state = nx_get_temp_state(temp);
1350 temp_val = nx_get_temp_val(temp);
1351
1352 if (temp_state == NX_TEMP_PANIC) {
1353 printk(KERN_ALERT
1354 "%s: Device temperature %d degrees C exceeds"
1355 " maximum allowed. Hardware has been shut down.\n",
1356 netxen_nic_driver_name, temp_val);
1357
1358 netif_carrier_off(netdev);
1359 netif_stop_queue(netdev);
1360 rv = 1;
1361 } else if (temp_state == NX_TEMP_WARN) {
1362 if (adapter->temp == NX_TEMP_NORMAL) {
1363 printk(KERN_ALERT
1364 "%s: Device temperature %d degrees C "
1365 "exceeds operating range."
1366 " Immediate action needed.\n",
1367 netxen_nic_driver_name, temp_val);
1368 }
1369 } else {
1370 if (adapter->temp == NX_TEMP_WARN) {
1371 printk(KERN_INFO
1372 "%s: Device temperature is now %d degrees C"
1373 " in normal range.\n", netxen_nic_driver_name,
1374 temp_val);
1375 }
1376 }
1377 adapter->temp = temp_state;
1378 return rv;
1379}
1380
1381static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1382{
1383 struct net_device *netdev = adapter->netdev;
1384 u32 val, port, linkup;
1385
1386 port = adapter->physical_port;
1387
c9fc891f
DP
1388 if (adapter->ahw.board_type == NETXEN_NIC_GBE) {
1389 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
a97342f9 1390 linkup = (val >> port) & 1;
c9fc891f
DP
1391 } else {
1392 if (adapter->fw_major < 4) {
1393 val = adapter->pci_read_normalize(adapter,
1394 CRB_XG_STATE);
1395 val = (val >> port*8) & 0xff;
1396 linkup = (val == XG_LINK_UP);
1397 } else {
1398 val = adapter->pci_read_normalize(adapter,
1399 CRB_XG_STATE_P3);
1400 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1401 linkup = (val == XG_LINK_UP_P3);
1402 }
a97342f9
DP
1403 }
1404
1405 if (adapter->ahw.linkup && !linkup) {
1406 printk(KERN_INFO "%s: %s NIC Link is down\n",
1407 netxen_nic_driver_name, netdev->name);
1408 adapter->ahw.linkup = 0;
1409 if (netif_running(netdev)) {
1410 netif_carrier_off(netdev);
1411 netif_stop_queue(netdev);
1412 }
1413 } else if (!adapter->ahw.linkup && linkup) {
1414 printk(KERN_INFO "%s: %s NIC Link is up\n",
1415 netxen_nic_driver_name, netdev->name);
1416 adapter->ahw.linkup = 1;
1417 if (netif_running(netdev)) {
1418 netif_carrier_on(netdev);
1419 netif_wake_queue(netdev);
1420 }
1421 }
1422}
1423
3d396eb1
AK
1424static void netxen_watchdog(unsigned long v)
1425{
1426 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
ed25ffa1
AK
1427
1428 SCHEDULE_WORK(&adapter->watchdog_task);
3d396eb1
AK
1429}
1430
a97342f9
DP
1431void netxen_watchdog_task(struct work_struct *work)
1432{
1433 struct netxen_adapter *adapter =
1434 container_of(work, struct netxen_adapter, watchdog_task);
1435
1436 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1437 return;
1438
1439 netxen_nic_handle_phy_intr(adapter);
1440
1441 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1442}
1443
3d396eb1
AK
1444static void netxen_tx_timeout(struct net_device *netdev)
1445{
3176ff3e
MT
1446 struct netxen_adapter *adapter = (struct netxen_adapter *)
1447 netdev_priv(netdev);
1448 SCHEDULE_WORK(&adapter->tx_timeout_task);
3d396eb1
AK
1449}
1450
6d5aefb8 1451static void netxen_tx_timeout_task(struct work_struct *work)
3d396eb1 1452{
4790654c 1453 struct netxen_adapter *adapter =
3176ff3e 1454 container_of(work, struct netxen_adapter, tx_timeout_task);
3d396eb1
AK
1455
1456 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
6c80b18d 1457 netxen_nic_driver_name, adapter->netdev->name);
3d396eb1 1458
ba53e6b4
DP
1459 netxen_nic_disable_int(adapter);
1460 napi_disable(&adapter->napi);
1461
6c80b18d 1462 adapter->netdev->trans_start = jiffies;
ba53e6b4
DP
1463
1464 napi_enable(&adapter->napi);
1465 netxen_nic_enable_int(adapter);
6c80b18d 1466 netif_wake_queue(adapter->netdev);
3d396eb1
AK
1467}
1468
a97342f9
DP
1469/*
1470 * netxen_nic_get_stats - Get System Network Statistics
1471 * @netdev: network interface device structure
1472 */
1473struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1474{
1475 struct netxen_adapter *adapter = netdev_priv(netdev);
1476 struct net_device_stats *stats = &adapter->net_stats;
1477
1478 memset(stats, 0, sizeof(*stats));
1479
1480 /* total packets received */
1481 stats->rx_packets = adapter->stats.no_rcv;
1482 /* total packets transmitted */
1483 stats->tx_packets = adapter->stats.xmitedframes +
1484 adapter->stats.xmitfinished;
1485 /* total bytes received */
1486 stats->rx_bytes = adapter->stats.rxbytes;
1487 /* total bytes transmitted */
1488 stats->tx_bytes = adapter->stats.txbytes;
1489 /* bad packets received */
1490 stats->rx_errors = adapter->stats.rcvdbadskb;
1491 /* packet transmit problems */
1492 stats->tx_errors = adapter->stats.nocmddescriptor;
1493 /* no space in linux buffers */
1494 stats->rx_dropped = adapter->stats.rxdropped;
1495 /* no space available in linux */
1496 stats->tx_dropped = adapter->stats.txdropped;
1497
1498 return stats;
1499}
1500
05aaa02d
DP
1501static inline void
1502netxen_handle_int(struct netxen_adapter *adapter)
3d396eb1 1503{
2d1a3bbd 1504 netxen_nic_disable_int(adapter);
05aaa02d 1505 napi_schedule(&adapter->napi);
3d396eb1
AK
1506}
1507
e4c93c81 1508static irqreturn_t netxen_intr(int irq, void *data)
3d396eb1 1509{
1daec86a 1510 struct netxen_adapter *adapter = data;
e01872af 1511 u32 our_int = 0;
3d396eb1 1512
3ce06a32 1513 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
05aaa02d
DP
1514 /* not our interrupt */
1515 if ((our_int & (0x80 << adapter->portnum)) == 0)
1516 return IRQ_NONE;
e01872af 1517
1518 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1519 /* claim interrupt */
3ce06a32
DP
1520 adapter->pci_write_normalize(adapter, CRB_INT_VECTOR,
1521 our_int & ~((u32)(0x80 << adapter->portnum)));
e01872af 1522 }
1523
05aaa02d 1524 netxen_handle_int(adapter);
3d396eb1
AK
1525
1526 return IRQ_HANDLED;
1527}
1528
e4c93c81 1529static irqreturn_t netxen_msi_intr(int irq, void *data)
05aaa02d
DP
1530{
1531 struct netxen_adapter *adapter = data;
1532
1533 netxen_handle_int(adapter);
1534 return IRQ_HANDLED;
1535}
1536
bea3348e 1537static int netxen_nic_poll(struct napi_struct *napi, int budget)
3d396eb1 1538{
bea3348e 1539 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
05aaa02d 1540 int tx_complete;
3d396eb1 1541 int ctx;
bea3348e 1542 int work_done;
3d396eb1 1543
05aaa02d 1544 tx_complete = netxen_process_cmd_ring(adapter);
3d396eb1 1545
80922fbc 1546 work_done = 0;
3d396eb1
AK
1547 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1548 /*
1549 * Fairness issue. This will give undue weight to the
1550 * receive context 0.
1551 */
1552
1553 /*
1554 * To avoid starvation, we give each of our receivers,
1555 * a fraction of the quota. Sometimes, it might happen that we
1556 * have enough quota to process every packet, but since all the
1557 * packets are on one context, it gets only half of the quota,
1558 * and ends up not processing it.
1559 */
bea3348e
SH
1560 work_done += netxen_process_rcv_ring(adapter, ctx,
1561 budget / MAX_RCV_CTX);
3d396eb1
AK
1562 }
1563
05aaa02d
DP
1564 if ((work_done < budget) && tx_complete) {
1565 netif_rx_complete(adapter->netdev, &adapter->napi);
3d396eb1
AK
1566 netxen_nic_enable_int(adapter);
1567 }
1568
bea3348e 1569 return work_done;
3d396eb1
AK
1570}
1571
1572#ifdef CONFIG_NET_POLL_CONTROLLER
1573static void netxen_nic_poll_controller(struct net_device *netdev)
1574{
3176ff3e 1575 struct netxen_adapter *adapter = netdev_priv(netdev);
3d396eb1 1576 disable_irq(adapter->irq);
1494a814 1577 netxen_intr(adapter->irq, adapter);
3d396eb1
AK
1578 enable_irq(adapter->irq);
1579}
1580#endif
3d396eb1
AK
1581
1582static struct pci_driver netxen_driver = {
1583 .name = netxen_nic_driver_name,
1584 .id_table = netxen_pci_tbl,
1585 .probe = netxen_nic_probe,
1586 .remove = __devexit_p(netxen_nic_remove)
1587};
1588
1589/* Driver Registration on NetXen card */
1590
1591static int __init netxen_init_module(void)
1592{
79ea13ce 1593 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
ed25ffa1
AK
1594 return -ENOMEM;
1595
184231bd 1596 return pci_register_driver(&netxen_driver);
3d396eb1
AK
1597}
1598
1599module_init(netxen_init_module);
1600
1601static void __exit netxen_exit_module(void)
1602{
3d396eb1 1603 pci_unregister_driver(&netxen_driver);
9de06610 1604 destroy_workqueue(netxen_workq);
3d396eb1
AK
1605}
1606
1607module_exit(netxen_exit_module);