2 * Copyright (C) 2003 - 2006 NetXen, Inc.
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.
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.
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,
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
30 * Main source file for NetXen NIC Driver on Linux
34 #include <linux/vmalloc.h>
35 #include <linux/highmem.h>
36 #include "netxen_nic_hw.h"
38 #include "netxen_nic.h"
39 #include "netxen_nic_phan_reg.h"
41 #include <linux/dma-mapping.h>
42 #include <linux/if_vlan.h>
44 #include <linux/ipv6.h>
46 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
47 MODULE_LICENSE("GPL");
48 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
50 char netxen_nic_driver_name[] = "netxen_nic";
51 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
52 NETXEN_NIC_LINUX_VERSIONID;
54 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
56 /* Default to restricted 1G auto-neg mode */
57 static int wol_port_mode = 5;
59 static int use_msi = 1;
61 static int use_msi_x = 1;
63 /* Local functions to NetXen NIC driver */
64 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
65 const struct pci_device_id *ent);
66 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
67 static int netxen_nic_open(struct net_device *netdev);
68 static int netxen_nic_close(struct net_device *netdev);
69 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
70 static void netxen_tx_timeout(struct net_device *netdev);
71 static void netxen_tx_timeout_task(struct work_struct *work);
72 static void netxen_watchdog(unsigned long);
73 static int netxen_nic_poll(struct napi_struct *napi, int budget);
74 #ifdef CONFIG_NET_POLL_CONTROLLER
75 static void netxen_nic_poll_controller(struct net_device *netdev);
77 static irqreturn_t netxen_intr(int irq, void *data);
78 static irqreturn_t netxen_msi_intr(int irq, void *data);
79 static irqreturn_t netxen_msix_intr(int irq, void *data);
81 /* PCI Device ID Table */
82 #define ENTRY(device) \
83 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
84 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
86 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
87 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
88 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
89 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
90 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
91 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
92 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
93 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
94 ENTRY(PCI_DEVICE_ID_NX3031),
98 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
101 * In netxen_nic_down(), we must wait for any pending callback requests into
102 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
103 * reenabled right after it is deleted in netxen_nic_down().
104 * FLUSH_SCHEDULED_WORK() does this synchronization.
106 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
107 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
108 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
109 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
110 * linkwatch_event() to be executed which also attempts to acquire the rtnl
111 * lock thus causing a deadlock.
114 static struct workqueue_struct *netxen_workq;
115 #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
116 #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
118 static void netxen_watchdog(unsigned long);
120 static uint32_t crb_cmd_producer[4] = {
121 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
122 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
126 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
127 uint32_t crb_producer)
129 adapter->pci_write_normalize(adapter,
130 adapter->crb_addr_cmd_producer, crb_producer);
133 static uint32_t crb_cmd_consumer[4] = {
134 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
135 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
139 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
142 adapter->pci_write_normalize(adapter,
143 adapter->crb_addr_cmd_consumer, crb_consumer);
146 static uint32_t msi_tgt_status[8] = {
147 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
148 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
149 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
150 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
153 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
155 static inline void netxen_nic_disable_int(struct netxen_adapter *adapter)
157 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0);
160 static inline void netxen_nic_enable_int(struct netxen_adapter *adapter)
162 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
164 if (!NETXEN_IS_MSI_FAMILY(adapter))
165 adapter->pci_write_immediate(adapter,
166 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
169 static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
171 struct pci_dev *pdev = adapter->pdev;
176 adapter->dma_mask = DMA_32BIT_MASK;
178 if (revision_id >= NX_P3_B0) {
179 /* should go to DMA_64BIT_MASK */
180 adapter->dma_mask = DMA_39BIT_MASK;
181 mask = DMA_39BIT_MASK;
182 } else if (revision_id == NX_P3_A2) {
183 adapter->dma_mask = DMA_39BIT_MASK;
184 mask = DMA_39BIT_MASK;
185 } else if (revision_id == NX_P2_C1) {
186 adapter->dma_mask = DMA_35BIT_MASK;
187 mask = DMA_35BIT_MASK;
189 adapter->dma_mask = DMA_32BIT_MASK;
190 mask = DMA_32BIT_MASK;
191 goto set_32_bit_mask;
195 * Consistent DMA mask is set to 32 bit because it cannot be set to
196 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
197 * come off this pool.
199 if (pci_set_dma_mask(pdev, mask) == 0 &&
200 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
201 adapter->pci_using_dac = 1;
205 #endif /* CONFIG_IA64 */
207 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
209 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
211 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
215 adapter->pci_using_dac = 0;
219 static void netxen_check_options(struct netxen_adapter *adapter)
221 switch (adapter->ahw.boardcfg.board_type) {
222 case NETXEN_BRDTYPE_P3_HMEZ:
223 case NETXEN_BRDTYPE_P3_XG_LOM:
224 case NETXEN_BRDTYPE_P3_10G_CX4:
225 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
226 case NETXEN_BRDTYPE_P3_IMEZ:
227 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
228 case NETXEN_BRDTYPE_P3_10G_SFP_QT:
229 case NETXEN_BRDTYPE_P3_10G_SFP_CT:
230 case NETXEN_BRDTYPE_P3_10G_XFP:
231 case NETXEN_BRDTYPE_P3_10000_BASE_T:
232 adapter->msix_supported = !!use_msi_x;
233 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
236 case NETXEN_BRDTYPE_P2_SB31_10G:
237 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
238 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
239 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
240 adapter->msix_supported = 0;
241 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
244 case NETXEN_BRDTYPE_P3_REF_QG:
245 case NETXEN_BRDTYPE_P3_4_GB:
246 case NETXEN_BRDTYPE_P3_4_GB_MM:
247 adapter->msix_supported = !!use_msi_x;
248 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
251 case NETXEN_BRDTYPE_P2_SB35_4G:
252 case NETXEN_BRDTYPE_P2_SB31_2G:
253 adapter->msix_supported = 0;
254 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
257 case NETXEN_BRDTYPE_P3_10G_TP:
258 adapter->msix_supported = !!use_msi_x;
259 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
260 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
262 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
266 adapter->msix_supported = 0;
267 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
269 printk(KERN_WARNING "Unknown board type(0x%x)\n",
270 adapter->ahw.boardcfg.board_type);
274 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
275 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
276 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
278 adapter->max_possible_rss_rings = 1;
283 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
287 if (first_boot == 0x55555555) {
288 /* This is the first boot after power up */
289 adapter->pci_write_normalize(adapter,
290 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
292 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
295 /* PCI bus master workaround */
296 adapter->hw_read_wx(adapter,
297 NETXEN_PCIE_REG(0x4), &first_boot, 4);
298 if (!(first_boot & 0x4)) {
300 adapter->hw_write_wx(adapter,
301 NETXEN_PCIE_REG(0x4), &first_boot, 4);
302 adapter->hw_read_wx(adapter,
303 NETXEN_PCIE_REG(0x4), &first_boot, 4);
306 /* This is the first boot after power up */
307 adapter->hw_read_wx(adapter,
308 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
309 if (first_boot != 0x80000f) {
310 /* clear the register for future unloads/loads */
311 adapter->pci_write_normalize(adapter,
312 NETXEN_CAM_RAM(0x1fc), 0);
316 /* Start P2 boot loader */
317 val = adapter->pci_read_normalize(adapter,
318 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
319 adapter->pci_write_normalize(adapter,
320 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
324 val = adapter->pci_read_normalize(adapter,
325 NETXEN_CAM_RAM(0x1fc));
327 if (++timeout > 5000)
330 } while (val == NETXEN_BDINFO_MAGIC);
335 static void netxen_set_port_mode(struct netxen_adapter *adapter)
339 val = adapter->ahw.boardcfg.board_type;
340 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
341 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
342 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
343 data = NETXEN_PORT_MODE_802_3_AP;
344 adapter->hw_write_wx(adapter,
345 NETXEN_PORT_MODE_ADDR, &data, 4);
346 } else if (port_mode == NETXEN_PORT_MODE_XG) {
347 data = NETXEN_PORT_MODE_XG;
348 adapter->hw_write_wx(adapter,
349 NETXEN_PORT_MODE_ADDR, &data, 4);
350 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
351 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
352 adapter->hw_write_wx(adapter,
353 NETXEN_PORT_MODE_ADDR, &data, 4);
354 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
355 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
356 adapter->hw_write_wx(adapter,
357 NETXEN_PORT_MODE_ADDR, &data, 4);
359 data = NETXEN_PORT_MODE_AUTO_NEG;
360 adapter->hw_write_wx(adapter,
361 NETXEN_PORT_MODE_ADDR, &data, 4);
364 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
365 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
366 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
367 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
368 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
370 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
375 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
380 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
382 pci_read_config_dword(pdev, pos, &control);
384 control |= PCI_MSIX_FLAGS_ENABLE;
387 pci_write_config_dword(pdev, pos, control);
391 static void netxen_init_msix_entries(struct netxen_adapter *adapter)
395 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
396 adapter->msix_entries[i].entry = i;
400 netxen_read_mac_addr(struct netxen_adapter *adapter)
405 struct net_device *netdev = adapter->netdev;
406 struct pci_dev *pdev = adapter->pdev;
408 if (netxen_is_flash_supported(adapter) != 0)
411 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
412 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
415 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
419 p = (unsigned char *)&mac_addr;
420 for (i = 0; i < 6; i++)
421 netdev->dev_addr[i] = *(p + 5 - i);
423 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
425 /* set station address */
427 if (!is_valid_ether_addr(netdev->perm_addr))
428 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
430 adapter->macaddr_set(adapter, netdev->dev_addr);
435 static void netxen_set_multicast_list(struct net_device *dev)
437 struct netxen_adapter *adapter = netdev_priv(dev);
439 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
440 netxen_p3_nic_set_multi(dev);
442 netxen_p2_nic_set_multi(dev);
445 static const struct net_device_ops netxen_netdev_ops = {
446 .ndo_open = netxen_nic_open,
447 .ndo_stop = netxen_nic_close,
448 .ndo_start_xmit = netxen_nic_xmit_frame,
449 .ndo_get_stats = netxen_nic_get_stats,
450 .ndo_validate_addr = eth_validate_addr,
451 .ndo_set_multicast_list = netxen_set_multicast_list,
452 .ndo_set_mac_address = netxen_nic_set_mac,
453 .ndo_change_mtu = netxen_nic_change_mtu,
454 .ndo_tx_timeout = netxen_tx_timeout,
455 #ifdef CONFIG_NET_POLL_CONTROLLER
456 .ndo_poll_controller = netxen_nic_poll_controller,
463 * The Linux system will invoke this after identifying the vendor ID and
464 * device Id in the pci_tbl supported by this module.
466 * A quad port card has one operational PCI config space, (function 0),
467 * which is used to access all four ports.
469 * This routine will initialize the adapter, and setup the global parameters
470 * along with the port's specific structure.
473 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
475 struct net_device *netdev = NULL;
476 struct netxen_adapter *adapter = NULL;
477 void __iomem *mem_ptr0 = NULL;
478 void __iomem *mem_ptr1 = NULL;
479 void __iomem *mem_ptr2 = NULL;
480 unsigned long first_page_group_end;
481 unsigned long first_page_group_start;
484 u8 __iomem *db_ptr = NULL;
485 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
487 int first_driver, first_boot;
489 int pci_func_id = PCI_FUNC(pdev->devfn);
490 struct netxen_legacy_intr_set *legacy_intrp;
493 if (pci_func_id == 0)
494 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
496 if (pdev->class != 0x020000) {
497 printk(KERN_DEBUG "NetXen function %d, class %x will not "
498 "be enabled.\n",pci_func_id, pdev->class);
502 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
503 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
504 "will not be enabled.\n",
509 if ((err = pci_enable_device(pdev)))
512 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
514 goto err_out_disable_pdev;
517 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
518 goto err_out_disable_pdev;
520 pci_set_master(pdev);
522 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
524 printk(KERN_ERR"%s: Failed to allocate memory for the "
525 "device block.Check system memory resource"
526 " usage.\n", netxen_nic_driver_name);
527 goto err_out_free_res;
530 SET_NETDEV_DEV(netdev, &pdev->dev);
532 adapter = netdev_priv(netdev);
533 adapter->netdev = netdev;
534 adapter->pdev = pdev;
535 adapter->ahw.pci_func = pci_func_id;
537 revision_id = pdev->revision;
538 adapter->ahw.revision_id = revision_id;
540 err = nx_set_dma_mask(adapter, revision_id);
542 goto err_out_free_netdev;
544 rwlock_init(&adapter->adapter_lock);
545 adapter->ahw.qdr_sn_window = -1;
546 adapter->ahw.ddr_mn_window = -1;
548 /* remap phys address */
549 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
550 mem_len = pci_resource_len(pdev, 0);
553 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
554 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
555 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
556 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
557 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
558 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
559 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
560 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
561 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
563 /* 128 Meg of memory */
564 if (mem_len == NETXEN_PCI_128MB_SIZE) {
565 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
566 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
567 SECOND_PAGE_GROUP_SIZE);
568 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
569 THIRD_PAGE_GROUP_SIZE);
570 first_page_group_start = FIRST_PAGE_GROUP_START;
571 first_page_group_end = FIRST_PAGE_GROUP_END;
572 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
573 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
574 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
575 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
576 first_page_group_start = 0;
577 first_page_group_end = 0;
578 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
579 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
580 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
581 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
582 adapter->pci_write_immediate =
583 netxen_nic_pci_write_immediate_2M;
584 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
585 adapter->pci_write_normalize =
586 netxen_nic_pci_write_normalize_2M;
587 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
588 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
589 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
591 mem_ptr0 = pci_ioremap_bar(pdev, 0);
592 if (mem_ptr0 == NULL) {
593 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
598 first_page_group_start = 0;
599 first_page_group_end = 0;
601 adapter->ahw.ddr_mn_window = 0;
602 adapter->ahw.qdr_sn_window = 0;
604 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
605 (pci_func_id * 0x20);
606 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
608 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
610 adapter->ahw.ms_win_crb +=
611 0xA0 + ((pci_func_id - 4) * 0x10);
614 goto err_out_free_netdev;
617 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
619 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
620 db_len = pci_resource_len(pdev, 4);
623 printk(KERN_ERR "%s: doorbell is disabled\n",
624 netxen_nic_driver_name);
626 goto err_out_iounmap;
628 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
631 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
633 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
634 netxen_nic_driver_name);
636 goto err_out_iounmap;
638 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
640 adapter->ahw.pci_base0 = mem_ptr0;
641 adapter->ahw.pci_len0 = pci_len0;
642 adapter->ahw.first_page_group_start = first_page_group_start;
643 adapter->ahw.first_page_group_end = first_page_group_end;
644 adapter->ahw.pci_base1 = mem_ptr1;
645 adapter->ahw.pci_base2 = mem_ptr2;
646 adapter->ahw.db_base = db_ptr;
647 adapter->ahw.db_len = db_len;
649 netif_napi_add(netdev, &adapter->napi,
650 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
652 if (revision_id >= NX_P3_B0)
653 legacy_intrp = &legacy_intr[pci_func_id];
655 legacy_intrp = &legacy_intr[0];
657 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
658 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
659 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
660 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
662 /* this will be read from FW later */
663 adapter->intr_scheme = -1;
664 adapter->msi_mode = -1;
666 /* This will be reset for mezz cards */
667 adapter->portnum = pci_func_id;
668 adapter->status &= ~NETXEN_NETDEV_STATUS;
669 adapter->rx_csum = 1;
670 adapter->mc_enabled = 0;
671 if (NX_IS_REVISION_P3(revision_id))
672 adapter->max_mc_count = 38;
674 adapter->max_mc_count = 16;
676 netdev->netdev_ops = &netxen_netdev_ops;
677 netdev->watchdog_timeo = 2*HZ;
679 netxen_nic_change_mtu(netdev, netdev->mtu);
681 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
683 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
684 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
686 if (NX_IS_REVISION_P3(revision_id)) {
687 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
688 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
691 if (adapter->pci_using_dac) {
692 netdev->features |= NETIF_F_HIGHDMA;
693 netdev->vlan_features |= NETIF_F_HIGHDMA;
697 * Set the CRB window to invalid. If any register in window 0 is
698 * accessed it should set the window to 0 and then reset it to 1.
700 adapter->curr_window = 255;
702 if (netxen_nic_get_board_info(adapter) != 0) {
703 printk("%s: Error getting board config info.\n",
704 netxen_nic_driver_name);
706 goto err_out_iounmap;
709 netxen_initialize_adapter_ops(adapter);
711 /* Mezz cards have PCI function 0,2,3 enabled */
712 switch (adapter->ahw.boardcfg.board_type) {
713 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
714 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
715 if (pci_func_id >= 2)
716 adapter->portnum = pci_func_id - 2;
723 * This call will setup various max rx/tx counts.
724 * It must be done before any buffer/ring allocations.
726 netxen_check_options(adapter);
729 if (NX_IS_REVISION_P3(revision_id)) {
730 if (adapter->ahw.pci_func == 0)
733 if (adapter->portnum == 0)
738 first_boot = adapter->pci_read_normalize(adapter,
739 NETXEN_CAM_RAM(0x1fc));
741 err = netxen_check_hw_init(adapter, first_boot);
743 printk(KERN_ERR "%s: error in init HW init sequence\n",
744 netxen_nic_driver_name);
745 goto err_out_iounmap;
748 if (NX_IS_REVISION_P3(revision_id))
749 netxen_set_port_mode(adapter);
751 if (first_boot != 0x55555555) {
752 adapter->pci_write_normalize(adapter,
753 CRB_CMDPEG_STATE, 0);
754 netxen_pinit_from_rom(adapter, 0);
757 netxen_load_firmware(adapter);
759 if (NX_IS_REVISION_P2(revision_id)) {
761 /* Initialize multicast addr pool owners */
763 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
765 netxen_crb_writelit_adapter(adapter,
766 NETXEN_MAC_ADDR_CNTL_REG, val);
770 err = netxen_initialize_adapter_offload(adapter);
772 goto err_out_iounmap;
775 * Tell the hardware our version number.
777 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
778 | ((_NETXEN_NIC_LINUX_MINOR << 8))
779 | (_NETXEN_NIC_LINUX_SUBVERSION);
780 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
782 /* Handshake with the card before we register the devices. */
783 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
785 goto err_out_free_offload;
789 netxen_nic_flash_print(adapter);
791 if (NX_IS_REVISION_P3(revision_id)) {
792 adapter->hw_read_wx(adapter,
793 NETXEN_MIU_MN_CONTROL, &val, 4);
794 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
795 dev_info(&pdev->dev, "firmware running in %s mode\n",
796 adapter->ahw.cut_through ? "cut through" : "legacy");
800 * See if the firmware gave us a virtual-physical port mapping.
802 adapter->physical_port = adapter->portnum;
803 if (adapter->fw_major < 4) {
804 i = adapter->pci_read_normalize(adapter,
805 CRB_V2P(adapter->portnum));
807 adapter->physical_port = i;
810 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
812 netxen_set_msix_bit(pdev, 0);
814 if (NX_IS_REVISION_P3(revision_id)) {
815 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
816 mem_len != NETXEN_PCI_2MB_SIZE)
817 adapter->msix_supported = 0;
820 if (adapter->msix_supported) {
822 netxen_init_msix_entries(adapter);
824 if (pci_enable_msix(pdev, adapter->msix_entries,
825 MSIX_ENTRIES_PER_ADAPTER))
828 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
829 netxen_set_msix_bit(pdev, 1);
830 dev_info(&pdev->dev, "using msi-x interrupts\n");
834 if (use_msi && !pci_enable_msi(pdev)) {
835 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
836 dev_info(&pdev->dev, "using msi interrupts\n");
838 dev_info(&pdev->dev, "using legacy interrupts\n");
841 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
842 netdev->irq = adapter->msix_entries[0].vector;
844 netdev->irq = pdev->irq;
846 err = netxen_receive_peg_ready(adapter);
848 goto err_out_disable_msi;
850 init_timer(&adapter->watchdog_timer);
851 adapter->watchdog_timer.function = &netxen_watchdog;
852 adapter->watchdog_timer.data = (unsigned long)adapter;
853 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
854 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
856 err = netxen_read_mac_addr(adapter);
858 dev_warn(&pdev->dev, "failed to read mac addr\n");
860 netif_carrier_off(netdev);
861 netif_stop_queue(netdev);
863 if ((err = register_netdev(netdev))) {
864 printk(KERN_ERR "%s: register_netdev failed port #%d"
865 " aborting\n", netxen_nic_driver_name,
868 goto err_out_disable_msi;
871 pci_set_drvdata(pdev, adapter);
873 switch (adapter->ahw.board_type) {
875 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
876 adapter->netdev->name);
878 case NETXEN_NIC_XGBE:
879 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
880 adapter->netdev->name);
887 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
888 pci_disable_msix(pdev);
889 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
890 pci_disable_msi(pdev);
892 err_out_free_offload:
894 netxen_free_adapter_offload(adapter);
911 pci_release_regions(pdev);
913 err_out_disable_pdev:
914 pci_set_drvdata(pdev, NULL);
915 pci_disable_device(pdev);
919 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
921 struct netxen_adapter *adapter;
922 struct net_device *netdev;
924 adapter = pci_get_drvdata(pdev);
928 netdev = adapter->netdev;
930 unregister_netdev(netdev);
932 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
933 netxen_free_hw_resources(adapter);
934 netxen_release_rx_buffers(adapter);
935 netxen_free_sw_resources(adapter);
937 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
938 netxen_p3_free_mac_list(adapter);
941 if (adapter->portnum == 0)
942 netxen_free_adapter_offload(adapter);
945 free_irq(adapter->irq, adapter);
947 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
948 pci_disable_msix(pdev);
949 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
950 pci_disable_msi(pdev);
952 iounmap(adapter->ahw.db_base);
953 iounmap(adapter->ahw.pci_base0);
954 if (adapter->ahw.pci_base1 != NULL)
955 iounmap(adapter->ahw.pci_base1);
956 if (adapter->ahw.pci_base2 != NULL)
957 iounmap(adapter->ahw.pci_base2);
959 pci_release_regions(pdev);
960 pci_disable_device(pdev);
961 pci_set_drvdata(pdev, NULL);
967 * Called when a network interface is made active
968 * @returns 0 on success, negative value on failure
970 static int netxen_nic_open(struct net_device *netdev)
972 struct netxen_adapter *adapter = netdev_priv(netdev);
975 irq_handler_t handler;
976 unsigned long flags = IRQF_SAMPLE_RANDOM;
978 if (adapter->driver_mismatch)
981 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
982 err = netxen_init_firmware(adapter);
984 printk(KERN_ERR "Failed to init firmware\n");
988 if (adapter->fw_major < 4)
989 adapter->max_rds_rings = 3;
991 adapter->max_rds_rings = 2;
993 err = netxen_alloc_sw_resources(adapter);
995 printk(KERN_ERR "%s: Error in setting sw resources\n",
1000 netxen_nic_clear_stats(adapter);
1002 err = netxen_alloc_hw_resources(adapter);
1004 printk(KERN_ERR "%s: Error in setting hw resources\n",
1006 goto err_out_free_sw;
1009 if ((adapter->msi_mode != MSI_MODE_MULTIFUNC) ||
1010 (adapter->intr_scheme != INTR_SCHEME_PERPORT)) {
1011 printk(KERN_ERR "%s: Firmware interrupt scheme is "
1012 "incompatible with driver\n",
1014 adapter->driver_mismatch = 1;
1015 goto err_out_free_hw;
1018 if (adapter->fw_major < 4) {
1019 adapter->crb_addr_cmd_producer =
1020 crb_cmd_producer[adapter->portnum];
1021 adapter->crb_addr_cmd_consumer =
1022 crb_cmd_consumer[adapter->portnum];
1024 netxen_nic_update_cmd_producer(adapter, 0);
1025 netxen_nic_update_cmd_consumer(adapter, 0);
1028 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1029 for (ring = 0; ring < adapter->max_rds_rings; ring++)
1030 netxen_post_rx_buffers(adapter, ctx, ring);
1032 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1033 handler = netxen_msix_intr;
1034 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1035 handler = netxen_msi_intr;
1037 flags |= IRQF_SHARED;
1038 handler = netxen_intr;
1040 adapter->irq = netdev->irq;
1041 err = request_irq(adapter->irq, handler,
1042 flags, netdev->name, adapter);
1044 printk(KERN_ERR "request_irq failed with: %d\n", err);
1045 goto err_out_free_rxbuf;
1048 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1051 /* Done here again so that even if phantom sw overwrote it,
1053 err = adapter->init_port(adapter, adapter->physical_port);
1055 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1056 netxen_nic_driver_name, adapter->portnum);
1057 goto err_out_free_irq;
1059 adapter->macaddr_set(adapter, netdev->dev_addr);
1061 netxen_nic_set_link_parameters(adapter);
1063 netxen_set_multicast_list(netdev);
1064 if (adapter->set_mtu)
1065 adapter->set_mtu(adapter, netdev->mtu);
1067 adapter->ahw.linkup = 0;
1068 mod_timer(&adapter->watchdog_timer, jiffies);
1070 napi_enable(&adapter->napi);
1071 netxen_nic_enable_int(adapter);
1073 netif_start_queue(netdev);
1078 free_irq(adapter->irq, adapter);
1080 netxen_release_rx_buffers(adapter);
1082 netxen_free_hw_resources(adapter);
1084 netxen_free_sw_resources(adapter);
1089 * netxen_nic_close - Disables a network interface entry point
1091 static int netxen_nic_close(struct net_device *netdev)
1093 struct netxen_adapter *adapter = netdev_priv(netdev);
1095 netif_carrier_off(netdev);
1096 netif_stop_queue(netdev);
1097 napi_disable(&adapter->napi);
1099 if (adapter->stop_port)
1100 adapter->stop_port(adapter);
1102 netxen_nic_disable_int(adapter);
1104 netxen_release_tx_buffers(adapter);
1106 FLUSH_SCHEDULED_WORK();
1107 del_timer_sync(&adapter->watchdog_timer);
1112 static bool netxen_tso_check(struct net_device *netdev,
1113 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1116 u8 opcode = TX_ETHER_PKT;
1117 __be16 protocol = skb->protocol;
1120 if (protocol == __constant_htons(ETH_P_8021Q)) {
1121 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1122 protocol = vh->h_vlan_encapsulated_proto;
1123 flags = FLAGS_VLAN_TAGGED;
1126 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1127 skb_shinfo(skb)->gso_size > 0) {
1129 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1130 desc->total_hdr_length =
1131 skb_transport_offset(skb) + tcp_hdrlen(skb);
1133 opcode = (protocol == __constant_htons(ETH_P_IPV6)) ?
1134 TX_TCP_LSO6 : TX_TCP_LSO;
1137 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1140 if (protocol == __constant_htons(ETH_P_IP)) {
1141 l4proto = ip_hdr(skb)->protocol;
1143 if (l4proto == IPPROTO_TCP)
1144 opcode = TX_TCP_PKT;
1145 else if(l4proto == IPPROTO_UDP)
1146 opcode = TX_UDP_PKT;
1147 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
1148 l4proto = ipv6_hdr(skb)->nexthdr;
1150 if (l4proto == IPPROTO_TCP)
1151 opcode = TX_TCPV6_PKT;
1152 else if(l4proto == IPPROTO_UDP)
1153 opcode = TX_UDPV6_PKT;
1156 desc->tcp_hdr_offset = skb_transport_offset(skb);
1157 desc->ip_hdr_offset = skb_network_offset(skb);
1158 netxen_set_tx_flags_opcode(desc, flags, opcode);
1163 netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1164 struct netxen_cmd_buffer *pbuf, int last)
1167 struct netxen_skb_frag *buffrag;
1169 buffrag = &pbuf->frag_array[0];
1170 pci_unmap_single(pdev, buffrag->dma,
1171 buffrag->length, PCI_DMA_TODEVICE);
1173 for (k = 1; k < last; k++) {
1174 buffrag = &pbuf->frag_array[k];
1175 pci_unmap_page(pdev, buffrag->dma,
1176 buffrag->length, PCI_DMA_TODEVICE);
1180 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1182 struct netxen_adapter *adapter = netdev_priv(netdev);
1183 struct netxen_hardware_context *hw = &adapter->ahw;
1184 unsigned int first_seg_len = skb->len - skb->data_len;
1185 struct netxen_cmd_buffer *pbuf;
1186 struct netxen_skb_frag *buffrag;
1187 struct cmd_desc_type0 *hwdesc;
1188 struct pci_dev *pdev = adapter->pdev;
1189 dma_addr_t temp_dma;
1192 u32 producer, consumer;
1193 int frag_count, no_of_desc;
1194 u32 num_txd = adapter->max_tx_desc_count;
1195 bool is_tso = false;
1197 frag_count = skb_shinfo(skb)->nr_frags + 1;
1199 /* There 4 fragments per descriptor */
1200 no_of_desc = (frag_count + 3) >> 2;
1202 producer = adapter->cmd_producer;
1204 consumer = adapter->last_cmd_consumer;
1205 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1206 netif_stop_queue(netdev);
1208 return NETDEV_TX_BUSY;
1211 /* Copy the descriptors into the hardware */
1212 hwdesc = &hw->cmd_desc_head[producer];
1213 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1214 /* Take skb->data itself */
1215 pbuf = &adapter->cmd_buf_arr[producer];
1217 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1220 pbuf->frag_count = frag_count;
1221 buffrag = &pbuf->frag_array[0];
1222 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
1224 if (pci_dma_mapping_error(pdev, temp_dma))
1227 buffrag->dma = temp_dma;
1228 buffrag->length = first_seg_len;
1229 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1230 netxen_set_tx_port(hwdesc, adapter->portnum);
1232 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1233 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1235 for (i = 1, k = 1; i < frag_count; i++, k++) {
1236 struct skb_frag_struct *frag;
1238 unsigned long offset;
1240 /* move to next desc. if there is a need */
1241 if ((i & 0x3) == 0) {
1243 producer = get_next_index(producer, num_txd);
1244 hwdesc = &hw->cmd_desc_head[producer];
1245 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1246 pbuf = &adapter->cmd_buf_arr[producer];
1249 frag = &skb_shinfo(skb)->frags[i - 1];
1251 offset = frag->page_offset;
1254 temp_dma = pci_map_page(pdev, frag->page, offset,
1255 len, PCI_DMA_TODEVICE);
1256 if (pci_dma_mapping_error(pdev, temp_dma)) {
1257 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1262 buffrag->dma = temp_dma;
1263 buffrag->length = temp_len;
1267 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1268 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1271 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1272 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1275 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1276 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1279 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1280 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1285 producer = get_next_index(producer, num_txd);
1287 /* For LSO, we need to copy the MAC/IP/TCP headers into
1288 * the descriptor ring
1291 int hdr_len, first_hdr_len, more_hdr;
1292 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1293 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1294 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1297 first_hdr_len = hdr_len;
1300 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1301 hwdesc = &hw->cmd_desc_head[producer];
1302 pbuf = &adapter->cmd_buf_arr[producer];
1305 /* copy the first 64 bytes */
1306 memcpy(((void *)hwdesc) + 2,
1307 (void *)(skb->data), first_hdr_len);
1308 producer = get_next_index(producer, num_txd);
1311 hwdesc = &hw->cmd_desc_head[producer];
1312 pbuf = &adapter->cmd_buf_arr[producer];
1314 /* copy the next 64 bytes - should be enough except
1315 * for pathological case
1317 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1321 producer = get_next_index(producer, num_txd);
1325 adapter->cmd_producer = producer;
1326 adapter->stats.txbytes += skb->len;
1328 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
1330 adapter->stats.xmitcalled++;
1331 netdev->trans_start = jiffies;
1333 return NETDEV_TX_OK;
1336 adapter->stats.txdropped++;
1337 dev_kfree_skb_any(skb);
1338 return NETDEV_TX_OK;
1341 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1343 struct net_device *netdev = adapter->netdev;
1344 uint32_t temp, temp_state, temp_val;
1347 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1349 temp_state = nx_get_temp_state(temp);
1350 temp_val = nx_get_temp_val(temp);
1352 if (temp_state == NX_TEMP_PANIC) {
1354 "%s: Device temperature %d degrees C exceeds"
1355 " maximum allowed. Hardware has been shut down.\n",
1356 netxen_nic_driver_name, temp_val);
1358 netif_carrier_off(netdev);
1359 netif_stop_queue(netdev);
1361 } else if (temp_state == NX_TEMP_WARN) {
1362 if (adapter->temp == NX_TEMP_NORMAL) {
1364 "%s: Device temperature %d degrees C "
1365 "exceeds operating range."
1366 " Immediate action needed.\n",
1367 netxen_nic_driver_name, temp_val);
1370 if (adapter->temp == NX_TEMP_WARN) {
1372 "%s: Device temperature is now %d degrees C"
1373 " in normal range.\n", netxen_nic_driver_name,
1377 adapter->temp = temp_state;
1381 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1383 struct net_device *netdev = adapter->netdev;
1384 u32 val, port, linkup;
1386 port = adapter->physical_port;
1388 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1389 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1390 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1391 linkup = (val == XG_LINK_UP_P3);
1393 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1394 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1395 linkup = (val >> port) & 1;
1397 val = (val >> port*8) & 0xff;
1398 linkup = (val == XG_LINK_UP);
1402 if (adapter->ahw.linkup && !linkup) {
1403 printk(KERN_INFO "%s: %s NIC Link is down\n",
1404 netxen_nic_driver_name, netdev->name);
1405 adapter->ahw.linkup = 0;
1406 if (netif_running(netdev)) {
1407 netif_carrier_off(netdev);
1408 netif_stop_queue(netdev);
1411 netxen_nic_set_link_parameters(adapter);
1412 } else if (!adapter->ahw.linkup && linkup) {
1413 printk(KERN_INFO "%s: %s NIC Link is up\n",
1414 netxen_nic_driver_name, netdev->name);
1415 adapter->ahw.linkup = 1;
1416 if (netif_running(netdev)) {
1417 netif_carrier_on(netdev);
1418 netif_wake_queue(netdev);
1421 netxen_nic_set_link_parameters(adapter);
1425 static void netxen_watchdog(unsigned long v)
1427 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1429 SCHEDULE_WORK(&adapter->watchdog_task);
1432 void netxen_watchdog_task(struct work_struct *work)
1434 struct netxen_adapter *adapter =
1435 container_of(work, struct netxen_adapter, watchdog_task);
1437 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1440 netxen_nic_handle_phy_intr(adapter);
1442 if (netif_running(adapter->netdev))
1443 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1446 static void netxen_tx_timeout(struct net_device *netdev)
1448 struct netxen_adapter *adapter = (struct netxen_adapter *)
1449 netdev_priv(netdev);
1450 SCHEDULE_WORK(&adapter->tx_timeout_task);
1453 static void netxen_tx_timeout_task(struct work_struct *work)
1455 struct netxen_adapter *adapter =
1456 container_of(work, struct netxen_adapter, tx_timeout_task);
1458 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1459 netxen_nic_driver_name, adapter->netdev->name);
1461 netxen_nic_disable_int(adapter);
1462 napi_disable(&adapter->napi);
1464 adapter->netdev->trans_start = jiffies;
1466 napi_enable(&adapter->napi);
1467 netxen_nic_enable_int(adapter);
1468 netif_wake_queue(adapter->netdev);
1472 * netxen_nic_get_stats - Get System Network Statistics
1473 * @netdev: network interface device structure
1475 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1477 struct netxen_adapter *adapter = netdev_priv(netdev);
1478 struct net_device_stats *stats = &adapter->net_stats;
1480 memset(stats, 0, sizeof(*stats));
1482 /* total packets received */
1483 stats->rx_packets = adapter->stats.no_rcv;
1484 /* total packets transmitted */
1485 stats->tx_packets = adapter->stats.xmitedframes +
1486 adapter->stats.xmitfinished;
1487 /* total bytes received */
1488 stats->rx_bytes = adapter->stats.rxbytes;
1489 /* total bytes transmitted */
1490 stats->tx_bytes = adapter->stats.txbytes;
1491 /* bad packets received */
1492 stats->rx_errors = adapter->stats.rcvdbadskb;
1493 /* packet transmit problems */
1494 stats->tx_errors = adapter->stats.nocmddescriptor;
1495 /* no space in linux buffers */
1496 stats->rx_dropped = adapter->stats.rxdropped;
1497 /* no space available in linux */
1498 stats->tx_dropped = adapter->stats.txdropped;
1503 static irqreturn_t netxen_intr(int irq, void *data)
1505 struct netxen_adapter *adapter = data;
1508 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1510 if (!(status & adapter->legacy_intr.int_vec_bit))
1513 if (adapter->ahw.revision_id >= NX_P3_B1) {
1514 /* check interrupt state machine, to be sure */
1515 status = adapter->pci_read_immediate(adapter,
1517 if (!ISR_LEGACY_INT_TRIGGERED(status))
1521 unsigned long our_int = 0;
1523 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
1525 /* not our interrupt */
1526 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
1529 /* claim interrupt */
1530 adapter->pci_write_normalize(adapter,
1531 CRB_INT_VECTOR, (our_int & 0xffffffff));
1534 /* clear interrupt */
1535 if (adapter->fw_major < 4)
1536 netxen_nic_disable_int(adapter);
1538 adapter->pci_write_immediate(adapter,
1539 adapter->legacy_intr.tgt_status_reg,
1541 /* read twice to ensure write is flushed */
1542 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1543 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1545 napi_schedule(&adapter->napi);
1550 static irqreturn_t netxen_msi_intr(int irq, void *data)
1552 struct netxen_adapter *adapter = data;
1554 /* clear interrupt */
1555 adapter->pci_write_immediate(adapter,
1556 msi_tgt_status[adapter->ahw.pci_func], 0xffffffff);
1558 napi_schedule(&adapter->napi);
1562 static irqreturn_t netxen_msix_intr(int irq, void *data)
1564 struct netxen_adapter *adapter = data;
1566 napi_schedule(&adapter->napi);
1570 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1572 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1577 tx_complete = netxen_process_cmd_ring(adapter);
1580 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1582 * Fairness issue. This will give undue weight to the
1583 * receive context 0.
1587 * To avoid starvation, we give each of our receivers,
1588 * a fraction of the quota. Sometimes, it might happen that we
1589 * have enough quota to process every packet, but since all the
1590 * packets are on one context, it gets only half of the quota,
1591 * and ends up not processing it.
1593 work_done += netxen_process_rcv_ring(adapter, ctx,
1594 budget / MAX_RCV_CTX);
1597 if ((work_done < budget) && tx_complete) {
1598 netif_rx_complete(&adapter->napi);
1599 netxen_nic_enable_int(adapter);
1605 #ifdef CONFIG_NET_POLL_CONTROLLER
1606 static void netxen_nic_poll_controller(struct net_device *netdev)
1608 struct netxen_adapter *adapter = netdev_priv(netdev);
1609 disable_irq(adapter->irq);
1610 netxen_intr(adapter->irq, adapter);
1611 enable_irq(adapter->irq);
1615 static struct pci_driver netxen_driver = {
1616 .name = netxen_nic_driver_name,
1617 .id_table = netxen_pci_tbl,
1618 .probe = netxen_nic_probe,
1619 .remove = __devexit_p(netxen_nic_remove)
1622 /* Driver Registration on NetXen card */
1624 static int __init netxen_init_module(void)
1626 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1629 return pci_register_driver(&netxen_driver);
1632 module_init(netxen_init_module);
1634 static void __exit netxen_exit_module(void)
1636 pci_unregister_driver(&netxen_driver);
1637 destroy_workqueue(netxen_workq);
1640 module_exit(netxen_exit_module);