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>
45 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
49 char netxen_nic_driver_name[] = "netxen_nic";
50 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
51 NETXEN_NIC_LINUX_VERSIONID;
53 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
55 /* Default to restricted 1G auto-neg mode */
56 static int wol_port_mode = 5;
58 static int use_msi = 1;
60 static int use_msi_x = 1;
62 /* Local functions to NetXen NIC driver */
63 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
64 const struct pci_device_id *ent);
65 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
66 static int netxen_nic_open(struct net_device *netdev);
67 static int netxen_nic_close(struct net_device *netdev);
68 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
69 static void netxen_tx_timeout(struct net_device *netdev);
70 static void netxen_tx_timeout_task(struct work_struct *work);
71 static void netxen_watchdog(unsigned long);
72 static int netxen_nic_poll(struct napi_struct *napi, int budget);
73 #ifdef CONFIG_NET_POLL_CONTROLLER
74 static void netxen_nic_poll_controller(struct net_device *netdev);
76 static irqreturn_t netxen_intr(int irq, void *data);
77 static irqreturn_t netxen_msi_intr(int irq, void *data);
79 /* PCI Device ID Table */
80 #define ENTRY(device) \
81 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
82 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
84 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
85 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
86 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
87 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
88 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
91 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
92 ENTRY(PCI_DEVICE_ID_NX3031),
96 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
99 * In netxen_nic_down(), we must wait for any pending callback requests into
100 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
101 * reenabled right after it is deleted in netxen_nic_down().
102 * FLUSH_SCHEDULED_WORK() does this synchronization.
104 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
105 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
106 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
107 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
108 * linkwatch_event() to be executed which also attempts to acquire the rtnl
109 * lock thus causing a deadlock.
112 static struct workqueue_struct *netxen_workq;
113 #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
114 #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
116 static void netxen_watchdog(unsigned long);
118 static uint32_t crb_cmd_producer[4] = {
119 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
120 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
124 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
125 uint32_t crb_producer)
127 adapter->pci_write_normalize(adapter,
128 adapter->crb_addr_cmd_producer, crb_producer);
131 static uint32_t crb_cmd_consumer[4] = {
132 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
133 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
137 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
140 adapter->pci_write_normalize(adapter,
141 adapter->crb_addr_cmd_consumer, crb_consumer);
144 static uint32_t msi_tgt_status[8] = {
145 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
146 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
147 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
148 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
151 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
153 static inline void netxen_nic_disable_int(struct netxen_adapter *adapter)
155 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0);
158 static inline void netxen_nic_enable_int(struct netxen_adapter *adapter)
160 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
162 if (!NETXEN_IS_MSI_FAMILY(adapter))
163 adapter->pci_write_immediate(adapter,
164 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
167 static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
169 struct pci_dev *pdev = adapter->pdev;
174 adapter->dma_mask = DMA_32BIT_MASK;
176 if (revision_id >= NX_P3_B0) {
177 /* should go to DMA_64BIT_MASK */
178 adapter->dma_mask = DMA_39BIT_MASK;
179 mask = DMA_39BIT_MASK;
180 } else if (revision_id == NX_P3_A2) {
181 adapter->dma_mask = DMA_39BIT_MASK;
182 mask = DMA_39BIT_MASK;
183 } else if (revision_id == NX_P2_C1) {
184 adapter->dma_mask = DMA_35BIT_MASK;
185 mask = DMA_35BIT_MASK;
187 adapter->dma_mask = DMA_32BIT_MASK;
188 mask = DMA_32BIT_MASK;
189 goto set_32_bit_mask;
193 * Consistent DMA mask is set to 32 bit because it cannot be set to
194 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
195 * come off this pool.
197 if (pci_set_dma_mask(pdev, mask) == 0 &&
198 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
199 adapter->pci_using_dac = 1;
202 #endif /* CONFIG_IA64 */
205 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
207 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
209 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
213 adapter->pci_using_dac = 0;
217 static void netxen_check_options(struct netxen_adapter *adapter)
219 switch (adapter->ahw.boardcfg.board_type) {
220 case NETXEN_BRDTYPE_P3_HMEZ:
221 case NETXEN_BRDTYPE_P3_XG_LOM:
222 case NETXEN_BRDTYPE_P3_10G_CX4:
223 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
224 case NETXEN_BRDTYPE_P3_IMEZ:
225 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
226 case NETXEN_BRDTYPE_P3_10G_SFP_QT:
227 case NETXEN_BRDTYPE_P3_10G_SFP_CT:
228 case NETXEN_BRDTYPE_P3_10G_XFP:
229 case NETXEN_BRDTYPE_P3_10000_BASE_T:
230 adapter->msix_supported = !!use_msi_x;
231 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
234 case NETXEN_BRDTYPE_P2_SB31_10G:
235 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
236 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
237 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
238 adapter->msix_supported = 0;
239 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
242 case NETXEN_BRDTYPE_P3_REF_QG:
243 case NETXEN_BRDTYPE_P3_4_GB:
244 case NETXEN_BRDTYPE_P3_4_GB_MM:
245 adapter->msix_supported = !!use_msi_x;
246 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
249 case NETXEN_BRDTYPE_P2_SB35_4G:
250 case NETXEN_BRDTYPE_P2_SB31_2G:
251 adapter->msix_supported = 0;
252 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
255 case NETXEN_BRDTYPE_P3_10G_TP:
256 adapter->msix_supported = !!use_msi_x;
257 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
258 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
260 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
264 adapter->msix_supported = 0;
265 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
267 printk(KERN_WARNING "Unknown board type(0x%x)\n",
268 adapter->ahw.boardcfg.board_type);
272 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
273 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
274 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
276 adapter->max_possible_rss_rings = 1;
281 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
285 if (first_boot == 0x55555555) {
286 /* This is the first boot after power up */
287 adapter->pci_write_normalize(adapter,
288 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
290 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
293 /* PCI bus master workaround */
294 adapter->hw_read_wx(adapter,
295 NETXEN_PCIE_REG(0x4), &first_boot, 4);
296 if (!(first_boot & 0x4)) {
298 adapter->hw_write_wx(adapter,
299 NETXEN_PCIE_REG(0x4), &first_boot, 4);
300 adapter->hw_read_wx(adapter,
301 NETXEN_PCIE_REG(0x4), &first_boot, 4);
304 /* This is the first boot after power up */
305 adapter->hw_read_wx(adapter,
306 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
307 if (first_boot != 0x80000f) {
308 /* clear the register for future unloads/loads */
309 adapter->pci_write_normalize(adapter,
310 NETXEN_CAM_RAM(0x1fc), 0);
314 /* Start P2 boot loader */
315 val = adapter->pci_read_normalize(adapter,
316 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
317 adapter->pci_write_normalize(adapter,
318 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
322 val = adapter->pci_read_normalize(adapter,
323 NETXEN_CAM_RAM(0x1fc));
325 if (++timeout > 5000)
328 } while (val == NETXEN_BDINFO_MAGIC);
333 static void netxen_set_port_mode(struct netxen_adapter *adapter)
337 val = adapter->ahw.boardcfg.board_type;
338 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
339 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
340 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
341 data = NETXEN_PORT_MODE_802_3_AP;
342 adapter->hw_write_wx(adapter,
343 NETXEN_PORT_MODE_ADDR, &data, 4);
344 } else if (port_mode == NETXEN_PORT_MODE_XG) {
345 data = NETXEN_PORT_MODE_XG;
346 adapter->hw_write_wx(adapter,
347 NETXEN_PORT_MODE_ADDR, &data, 4);
348 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
349 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
350 adapter->hw_write_wx(adapter,
351 NETXEN_PORT_MODE_ADDR, &data, 4);
352 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
353 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
354 adapter->hw_write_wx(adapter,
355 NETXEN_PORT_MODE_ADDR, &data, 4);
357 data = NETXEN_PORT_MODE_AUTO_NEG;
358 adapter->hw_write_wx(adapter,
359 NETXEN_PORT_MODE_ADDR, &data, 4);
362 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
363 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
364 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
365 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
366 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
368 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
373 #define PCI_CAP_ID_GEN 0x10
375 static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
382 struct pci_dev *pdev;
384 pdev = adapter->pdev;
386 adapter->hw_read_wx(adapter,
387 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
388 /* clear chicken3.25:24 */
389 chicken &= 0xFCFFFFFF;
391 * if gen1 and B0, set F1020 - if gen 2, do nothing
392 * if gen2 set to F1000
394 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
396 pci_read_config_dword(pdev, pos + 0x10, &control);
397 if ((control & 0x000F0000) != 0x00020000) {
398 /* set chicken3.24 if gen1 */
399 chicken |= 0x01000000;
401 printk(KERN_INFO "%s Gen2 strapping detected\n",
402 netxen_nic_driver_name);
405 /* set chicken3.24 if gen1 */
406 chicken |= 0x01000000;
407 printk(KERN_INFO "%s Gen1 strapping detected\n",
408 netxen_nic_driver_name);
409 if (adapter->ahw.revision_id == NX_P3_B0)
415 adapter->hw_write_wx(adapter,
416 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
421 pdevfuncsave = pdev->devfn;
422 if (pdevfuncsave & 0x07)
425 for (i = 0; i < 8; i++) {
426 pci_read_config_dword(pdev, pos + 8, &control);
427 pci_read_config_dword(pdev, pos + 8, &control);
428 pci_write_config_dword(pdev, pos + 8, c8c9value);
431 pdev->devfn = pdevfuncsave;
434 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
439 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
441 pci_read_config_dword(pdev, pos, &control);
443 control |= PCI_MSIX_FLAGS_ENABLE;
446 pci_write_config_dword(pdev, pos, control);
450 static void netxen_init_msix_entries(struct netxen_adapter *adapter)
454 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
455 adapter->msix_entries[i].entry = i;
459 netxen_read_mac_addr(struct netxen_adapter *adapter)
464 struct net_device *netdev = adapter->netdev;
465 struct pci_dev *pdev = adapter->pdev;
467 if (netxen_is_flash_supported(adapter) != 0)
470 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
471 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
474 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
478 p = (unsigned char *)&mac_addr;
479 for (i = 0; i < 6; i++)
480 netdev->dev_addr[i] = *(p + 5 - i);
482 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
484 /* set station address */
486 if (!is_valid_ether_addr(netdev->perm_addr))
487 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
489 adapter->macaddr_set(adapter, netdev->dev_addr);
494 static void netxen_set_multicast_list(struct net_device *dev)
496 struct netxen_adapter *adapter = netdev_priv(dev);
498 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
499 netxen_p3_nic_set_multi(dev);
501 netxen_p2_nic_set_multi(dev);
504 static const struct net_device_ops netxen_netdev_ops = {
505 .ndo_open = netxen_nic_open,
506 .ndo_stop = netxen_nic_close,
507 .ndo_start_xmit = netxen_nic_xmit_frame,
508 .ndo_get_stats = netxen_nic_get_stats,
509 .ndo_validate_addr = eth_validate_addr,
510 .ndo_set_multicast_list = netxen_set_multicast_list,
511 .ndo_set_mac_address = netxen_nic_set_mac,
512 .ndo_change_mtu = netxen_nic_change_mtu,
513 .ndo_tx_timeout = netxen_tx_timeout,
514 #ifdef CONFIG_NET_POLL_CONTROLLER
515 .ndo_poll_controller = netxen_nic_poll_controller,
522 * The Linux system will invoke this after identifying the vendor ID and
523 * device Id in the pci_tbl supported by this module.
525 * A quad port card has one operational PCI config space, (function 0),
526 * which is used to access all four ports.
528 * This routine will initialize the adapter, and setup the global parameters
529 * along with the port's specific structure.
532 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
534 struct net_device *netdev = NULL;
535 struct netxen_adapter *adapter = NULL;
536 void __iomem *mem_ptr0 = NULL;
537 void __iomem *mem_ptr1 = NULL;
538 void __iomem *mem_ptr2 = NULL;
539 unsigned long first_page_group_end;
540 unsigned long first_page_group_start;
543 u8 __iomem *db_ptr = NULL;
544 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
546 int first_driver, first_boot;
548 int pci_func_id = PCI_FUNC(pdev->devfn);
549 struct netxen_legacy_intr_set *legacy_intrp;
552 if (pci_func_id == 0)
553 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
555 if (pdev->class != 0x020000) {
556 printk(KERN_DEBUG "NetXen function %d, class %x will not "
557 "be enabled.\n",pci_func_id, pdev->class);
561 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
562 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
563 "will not be enabled.\n",
568 if ((err = pci_enable_device(pdev)))
571 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
573 goto err_out_disable_pdev;
576 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
577 goto err_out_disable_pdev;
579 pci_set_master(pdev);
581 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
583 printk(KERN_ERR"%s: Failed to allocate memory for the "
584 "device block.Check system memory resource"
585 " usage.\n", netxen_nic_driver_name);
586 goto err_out_free_res;
589 SET_NETDEV_DEV(netdev, &pdev->dev);
591 adapter = netdev_priv(netdev);
592 adapter->netdev = netdev;
593 adapter->pdev = pdev;
594 adapter->ahw.pci_func = pci_func_id;
596 revision_id = pdev->revision;
597 adapter->ahw.revision_id = revision_id;
599 err = nx_set_dma_mask(adapter, revision_id);
601 goto err_out_free_netdev;
603 rwlock_init(&adapter->adapter_lock);
604 adapter->ahw.qdr_sn_window = -1;
605 adapter->ahw.ddr_mn_window = -1;
607 /* remap phys address */
608 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
609 mem_len = pci_resource_len(pdev, 0);
612 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
613 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
614 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
615 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
616 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
617 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
618 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
619 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
620 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
622 /* 128 Meg of memory */
623 if (mem_len == NETXEN_PCI_128MB_SIZE) {
624 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
625 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
626 SECOND_PAGE_GROUP_SIZE);
627 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
628 THIRD_PAGE_GROUP_SIZE);
629 first_page_group_start = FIRST_PAGE_GROUP_START;
630 first_page_group_end = FIRST_PAGE_GROUP_END;
631 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
632 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
633 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
634 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
635 first_page_group_start = 0;
636 first_page_group_end = 0;
637 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
638 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
639 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
640 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
641 adapter->pci_write_immediate =
642 netxen_nic_pci_write_immediate_2M;
643 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
644 adapter->pci_write_normalize =
645 netxen_nic_pci_write_normalize_2M;
646 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
647 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
648 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
650 mem_ptr0 = ioremap(mem_base, mem_len);
652 first_page_group_start = 0;
653 first_page_group_end = 0;
655 adapter->ahw.ddr_mn_window = 0;
656 adapter->ahw.qdr_sn_window = 0;
658 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
659 (pci_func_id * 0x20);
660 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
662 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
664 adapter->ahw.ms_win_crb +=
665 0xA0 + ((pci_func_id - 4) * 0x10);
668 goto err_out_free_netdev;
671 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
673 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
674 db_len = pci_resource_len(pdev, 4);
677 printk(KERN_ERR "%s: doorbell is disabled\n",
678 netxen_nic_driver_name);
680 goto err_out_iounmap;
682 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
685 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
687 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
688 netxen_nic_driver_name);
690 goto err_out_iounmap;
692 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
694 adapter->ahw.pci_base0 = mem_ptr0;
695 adapter->ahw.pci_len0 = pci_len0;
696 adapter->ahw.first_page_group_start = first_page_group_start;
697 adapter->ahw.first_page_group_end = first_page_group_end;
698 adapter->ahw.pci_base1 = mem_ptr1;
699 adapter->ahw.pci_base2 = mem_ptr2;
700 adapter->ahw.db_base = db_ptr;
701 adapter->ahw.db_len = db_len;
703 netif_napi_add(netdev, &adapter->napi,
704 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
706 if (revision_id >= NX_P3_B0)
707 legacy_intrp = &legacy_intr[pci_func_id];
709 legacy_intrp = &legacy_intr[0];
711 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
712 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
713 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
714 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
716 /* this will be read from FW later */
717 adapter->intr_scheme = -1;
718 adapter->msi_mode = -1;
720 /* This will be reset for mezz cards */
721 adapter->portnum = pci_func_id;
722 adapter->status &= ~NETXEN_NETDEV_STATUS;
723 adapter->rx_csum = 1;
724 adapter->mc_enabled = 0;
725 if (NX_IS_REVISION_P3(revision_id))
726 adapter->max_mc_count = 38;
728 adapter->max_mc_count = 16;
730 netdev->netdev_ops = &netxen_netdev_ops;
731 netdev->watchdog_timeo = 2*HZ;
733 netxen_nic_change_mtu(netdev, netdev->mtu);
735 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
737 /* ScatterGather support */
738 netdev->features = NETIF_F_SG;
739 netdev->features |= NETIF_F_IP_CSUM;
740 netdev->features |= NETIF_F_TSO;
741 if (NX_IS_REVISION_P3(revision_id)) {
742 netdev->features |= NETIF_F_IPV6_CSUM;
743 netdev->features |= NETIF_F_TSO6;
746 if (adapter->pci_using_dac)
747 netdev->features |= NETIF_F_HIGHDMA;
750 * Set the CRB window to invalid. If any register in window 0 is
751 * accessed it should set the window to 0 and then reset it to 1.
753 adapter->curr_window = 255;
755 if (netxen_nic_get_board_info(adapter) != 0) {
756 printk("%s: Error getting board config info.\n",
757 netxen_nic_driver_name);
759 goto err_out_iounmap;
762 netxen_initialize_adapter_ops(adapter);
764 /* Mezz cards have PCI function 0,2,3 enabled */
765 switch (adapter->ahw.boardcfg.board_type) {
766 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
767 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
768 if (pci_func_id >= 2)
769 adapter->portnum = pci_func_id - 2;
776 * This call will setup various max rx/tx counts.
777 * It must be done before any buffer/ring allocations.
779 netxen_check_options(adapter);
782 if (NX_IS_REVISION_P3(revision_id)) {
783 if (adapter->ahw.pci_func == 0)
786 if (adapter->portnum == 0)
791 first_boot = adapter->pci_read_normalize(adapter,
792 NETXEN_CAM_RAM(0x1fc));
794 err = netxen_check_hw_init(adapter, first_boot);
796 printk(KERN_ERR "%s: error in init HW init sequence\n",
797 netxen_nic_driver_name);
798 goto err_out_iounmap;
801 if (NX_IS_REVISION_P3(revision_id))
802 netxen_set_port_mode(adapter);
804 if (first_boot != 0x55555555) {
805 adapter->pci_write_normalize(adapter,
806 CRB_CMDPEG_STATE, 0);
807 netxen_pinit_from_rom(adapter, 0);
810 netxen_load_firmware(adapter);
812 if (NX_IS_REVISION_P3(revision_id))
813 netxen_pcie_strap_init(adapter);
815 if (NX_IS_REVISION_P2(revision_id)) {
817 /* Initialize multicast addr pool owners */
819 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
821 netxen_crb_writelit_adapter(adapter,
822 NETXEN_MAC_ADDR_CNTL_REG, val);
826 err = netxen_initialize_adapter_offload(adapter);
828 goto err_out_iounmap;
831 * Tell the hardware our version number.
833 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
834 | ((_NETXEN_NIC_LINUX_MINOR << 8))
835 | (_NETXEN_NIC_LINUX_SUBVERSION);
836 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
838 /* Handshake with the card before we register the devices. */
839 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
841 goto err_out_free_offload;
845 netxen_nic_flash_print(adapter);
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");
856 * See if the firmware gave us a virtual-physical port mapping.
858 adapter->physical_port = adapter->portnum;
859 i = adapter->pci_read_normalize(adapter, CRB_V2P(adapter->portnum));
861 adapter->physical_port = i;
863 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
865 netxen_set_msix_bit(pdev, 0);
867 if (NX_IS_REVISION_P3(revision_id)) {
868 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
869 mem_len != NETXEN_PCI_2MB_SIZE)
870 adapter->msix_supported = 0;
873 if (adapter->msix_supported) {
875 netxen_init_msix_entries(adapter);
877 if (pci_enable_msix(pdev, adapter->msix_entries,
878 MSIX_ENTRIES_PER_ADAPTER))
881 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
882 netxen_set_msix_bit(pdev, 1);
883 dev_info(&pdev->dev, "using msi-x interrupts\n");
887 if (use_msi && !pci_enable_msi(pdev)) {
888 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
889 dev_info(&pdev->dev, "using msi interrupts\n");
891 dev_info(&pdev->dev, "using legacy interrupts\n");
894 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
895 netdev->irq = adapter->msix_entries[0].vector;
897 netdev->irq = pdev->irq;
899 err = netxen_receive_peg_ready(adapter);
901 goto err_out_disable_msi;
903 init_timer(&adapter->watchdog_timer);
904 adapter->watchdog_timer.function = &netxen_watchdog;
905 adapter->watchdog_timer.data = (unsigned long)adapter;
906 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
907 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
909 err = netxen_read_mac_addr(adapter);
911 dev_warn(&pdev->dev, "failed to read mac addr\n");
913 netif_carrier_off(netdev);
914 netif_stop_queue(netdev);
916 if ((err = register_netdev(netdev))) {
917 printk(KERN_ERR "%s: register_netdev failed port #%d"
918 " aborting\n", netxen_nic_driver_name,
921 goto err_out_disable_msi;
924 pci_set_drvdata(pdev, adapter);
926 switch (adapter->ahw.board_type) {
928 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
929 adapter->netdev->name);
931 case NETXEN_NIC_XGBE:
932 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
933 adapter->netdev->name);
940 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
941 pci_disable_msix(pdev);
942 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
943 pci_disable_msi(pdev);
945 err_out_free_offload:
947 netxen_free_adapter_offload(adapter);
964 pci_release_regions(pdev);
966 err_out_disable_pdev:
967 pci_set_drvdata(pdev, NULL);
968 pci_disable_device(pdev);
972 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
974 struct netxen_adapter *adapter;
975 struct net_device *netdev;
977 adapter = pci_get_drvdata(pdev);
981 netdev = adapter->netdev;
983 unregister_netdev(netdev);
985 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
986 netxen_free_hw_resources(adapter);
987 netxen_release_rx_buffers(adapter);
988 netxen_free_sw_resources(adapter);
990 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
991 netxen_p3_free_mac_list(adapter);
994 if (adapter->portnum == 0)
995 netxen_free_adapter_offload(adapter);
998 free_irq(adapter->irq, adapter);
1000 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1001 pci_disable_msix(pdev);
1002 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1003 pci_disable_msi(pdev);
1005 iounmap(adapter->ahw.db_base);
1006 iounmap(adapter->ahw.pci_base0);
1007 iounmap(adapter->ahw.pci_base1);
1008 iounmap(adapter->ahw.pci_base2);
1010 pci_release_regions(pdev);
1011 pci_disable_device(pdev);
1012 pci_set_drvdata(pdev, NULL);
1014 free_netdev(netdev);
1018 * Called when a network interface is made active
1019 * @returns 0 on success, negative value on failure
1021 static int netxen_nic_open(struct net_device *netdev)
1023 struct netxen_adapter *adapter = netdev_priv(netdev);
1026 irq_handler_t handler;
1027 unsigned long flags = IRQF_SAMPLE_RANDOM;
1029 if (adapter->driver_mismatch)
1032 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1033 err = netxen_init_firmware(adapter);
1035 printk(KERN_ERR "Failed to init firmware\n");
1039 if (adapter->fw_major < 4)
1040 adapter->max_rds_rings = 3;
1042 adapter->max_rds_rings = 2;
1044 err = netxen_alloc_sw_resources(adapter);
1046 printk(KERN_ERR "%s: Error in setting sw resources\n",
1051 netxen_nic_clear_stats(adapter);
1053 err = netxen_alloc_hw_resources(adapter);
1055 printk(KERN_ERR "%s: Error in setting hw resources\n",
1057 goto err_out_free_sw;
1060 if ((adapter->msi_mode != MSI_MODE_MULTIFUNC) ||
1061 (adapter->intr_scheme != INTR_SCHEME_PERPORT)) {
1062 printk(KERN_ERR "%s: Firmware interrupt scheme is "
1063 "incompatible with driver\n",
1065 adapter->driver_mismatch = 1;
1066 goto err_out_free_hw;
1069 if (adapter->fw_major < 4) {
1070 adapter->crb_addr_cmd_producer =
1071 crb_cmd_producer[adapter->portnum];
1072 adapter->crb_addr_cmd_consumer =
1073 crb_cmd_consumer[adapter->portnum];
1075 netxen_nic_update_cmd_producer(adapter, 0);
1076 netxen_nic_update_cmd_consumer(adapter, 0);
1079 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1080 for (ring = 0; ring < adapter->max_rds_rings; ring++)
1081 netxen_post_rx_buffers(adapter, ctx, ring);
1083 if (NETXEN_IS_MSI_FAMILY(adapter))
1084 handler = netxen_msi_intr;
1086 flags |= IRQF_SHARED;
1087 handler = netxen_intr;
1089 adapter->irq = netdev->irq;
1090 err = request_irq(adapter->irq, handler,
1091 flags, netdev->name, adapter);
1093 printk(KERN_ERR "request_irq failed with: %d\n", err);
1094 goto err_out_free_rxbuf;
1097 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1100 /* Done here again so that even if phantom sw overwrote it,
1102 err = adapter->init_port(adapter, adapter->physical_port);
1104 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1105 netxen_nic_driver_name, adapter->portnum);
1106 goto err_out_free_irq;
1108 adapter->macaddr_set(adapter, netdev->dev_addr);
1110 netxen_nic_set_link_parameters(adapter);
1112 netxen_set_multicast_list(netdev);
1113 if (adapter->set_mtu)
1114 adapter->set_mtu(adapter, netdev->mtu);
1116 adapter->ahw.linkup = 0;
1117 mod_timer(&adapter->watchdog_timer, jiffies);
1119 napi_enable(&adapter->napi);
1120 netxen_nic_enable_int(adapter);
1122 netif_start_queue(netdev);
1127 free_irq(adapter->irq, adapter);
1129 netxen_release_rx_buffers(adapter);
1131 netxen_free_hw_resources(adapter);
1133 netxen_free_sw_resources(adapter);
1138 * netxen_nic_close - Disables a network interface entry point
1140 static int netxen_nic_close(struct net_device *netdev)
1142 struct netxen_adapter *adapter = netdev_priv(netdev);
1144 netif_carrier_off(netdev);
1145 netif_stop_queue(netdev);
1146 napi_disable(&adapter->napi);
1148 if (adapter->stop_port)
1149 adapter->stop_port(adapter);
1151 netxen_nic_disable_int(adapter);
1153 netxen_release_tx_buffers(adapter);
1155 FLUSH_SCHEDULED_WORK();
1156 del_timer_sync(&adapter->watchdog_timer);
1161 static bool netxen_tso_check(struct net_device *netdev,
1162 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1165 u8 opcode = TX_ETHER_PKT;
1167 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1168 skb_shinfo(skb)->gso_size > 0) {
1170 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1171 desc->total_hdr_length =
1172 skb_transport_offset(skb) + tcp_hdrlen(skb);
1174 opcode = (skb->protocol == htons(ETH_P_IPV6)) ?
1175 TX_TCP_LSO6 : TX_TCP_LSO;
1178 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1181 if (skb->protocol == htons(ETH_P_IP)) {
1182 l4proto = ip_hdr(skb)->protocol;
1184 if (l4proto == IPPROTO_TCP)
1185 opcode = TX_TCP_PKT;
1186 else if(l4proto == IPPROTO_UDP)
1187 opcode = TX_UDP_PKT;
1188 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1189 l4proto = ipv6_hdr(skb)->nexthdr;
1191 if (l4proto == IPPROTO_TCP)
1192 opcode = TX_TCPV6_PKT;
1193 else if(l4proto == IPPROTO_UDP)
1194 opcode = TX_UDPV6_PKT;
1197 desc->tcp_hdr_offset = skb_transport_offset(skb);
1198 desc->ip_hdr_offset = skb_network_offset(skb);
1199 netxen_set_tx_flags_opcode(desc, 0, opcode);
1204 netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1205 struct netxen_cmd_buffer *pbuf, int last)
1208 struct netxen_skb_frag *buffrag;
1210 buffrag = &pbuf->frag_array[0];
1211 pci_unmap_single(pdev, buffrag->dma,
1212 buffrag->length, PCI_DMA_TODEVICE);
1214 for (k = 1; k < last; k++) {
1215 buffrag = &pbuf->frag_array[k];
1216 pci_unmap_page(pdev, buffrag->dma,
1217 buffrag->length, PCI_DMA_TODEVICE);
1221 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1223 struct netxen_adapter *adapter = netdev_priv(netdev);
1224 struct netxen_hardware_context *hw = &adapter->ahw;
1225 unsigned int first_seg_len = skb->len - skb->data_len;
1226 struct netxen_cmd_buffer *pbuf;
1227 struct netxen_skb_frag *buffrag;
1228 struct cmd_desc_type0 *hwdesc;
1229 struct pci_dev *pdev = adapter->pdev;
1230 dma_addr_t temp_dma;
1233 u32 producer, consumer;
1234 int frag_count, no_of_desc;
1235 u32 num_txd = adapter->max_tx_desc_count;
1236 bool is_tso = false;
1238 frag_count = skb_shinfo(skb)->nr_frags + 1;
1240 /* There 4 fragments per descriptor */
1241 no_of_desc = (frag_count + 3) >> 2;
1243 producer = adapter->cmd_producer;
1245 consumer = adapter->last_cmd_consumer;
1246 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1247 netif_stop_queue(netdev);
1249 return NETDEV_TX_BUSY;
1252 /* Copy the descriptors into the hardware */
1253 hwdesc = &hw->cmd_desc_head[producer];
1254 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1255 /* Take skb->data itself */
1256 pbuf = &adapter->cmd_buf_arr[producer];
1258 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1261 pbuf->frag_count = frag_count;
1262 buffrag = &pbuf->frag_array[0];
1263 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
1265 if (pci_dma_mapping_error(pdev, temp_dma))
1268 buffrag->dma = temp_dma;
1269 buffrag->length = first_seg_len;
1270 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1271 netxen_set_tx_port(hwdesc, adapter->portnum);
1273 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1274 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1276 for (i = 1, k = 1; i < frag_count; i++, k++) {
1277 struct skb_frag_struct *frag;
1279 unsigned long offset;
1281 /* move to next desc. if there is a need */
1282 if ((i & 0x3) == 0) {
1284 producer = get_next_index(producer, num_txd);
1285 hwdesc = &hw->cmd_desc_head[producer];
1286 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1287 pbuf = &adapter->cmd_buf_arr[producer];
1290 frag = &skb_shinfo(skb)->frags[i - 1];
1292 offset = frag->page_offset;
1295 temp_dma = pci_map_page(pdev, frag->page, offset,
1296 len, PCI_DMA_TODEVICE);
1297 if (pci_dma_mapping_error(pdev, temp_dma)) {
1298 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1303 buffrag->dma = temp_dma;
1304 buffrag->length = temp_len;
1308 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1309 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1312 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1313 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1316 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1317 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1320 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1321 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1326 producer = get_next_index(producer, num_txd);
1328 /* For LSO, we need to copy the MAC/IP/TCP headers into
1329 * the descriptor ring
1332 int hdr_len, first_hdr_len, more_hdr;
1333 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1334 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1335 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1338 first_hdr_len = hdr_len;
1341 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1342 hwdesc = &hw->cmd_desc_head[producer];
1343 pbuf = &adapter->cmd_buf_arr[producer];
1346 /* copy the first 64 bytes */
1347 memcpy(((void *)hwdesc) + 2,
1348 (void *)(skb->data), first_hdr_len);
1349 producer = get_next_index(producer, num_txd);
1352 hwdesc = &hw->cmd_desc_head[producer];
1353 pbuf = &adapter->cmd_buf_arr[producer];
1355 /* copy the next 64 bytes - should be enough except
1356 * for pathological case
1358 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1362 producer = get_next_index(producer, num_txd);
1366 adapter->cmd_producer = producer;
1367 adapter->stats.txbytes += skb->len;
1369 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
1371 adapter->stats.xmitcalled++;
1372 netdev->trans_start = jiffies;
1374 return NETDEV_TX_OK;
1377 adapter->stats.txdropped++;
1378 dev_kfree_skb_any(skb);
1379 return NETDEV_TX_OK;
1382 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1384 struct net_device *netdev = adapter->netdev;
1385 uint32_t temp, temp_state, temp_val;
1388 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1390 temp_state = nx_get_temp_state(temp);
1391 temp_val = nx_get_temp_val(temp);
1393 if (temp_state == NX_TEMP_PANIC) {
1395 "%s: Device temperature %d degrees C exceeds"
1396 " maximum allowed. Hardware has been shut down.\n",
1397 netxen_nic_driver_name, temp_val);
1399 netif_carrier_off(netdev);
1400 netif_stop_queue(netdev);
1402 } else if (temp_state == NX_TEMP_WARN) {
1403 if (adapter->temp == NX_TEMP_NORMAL) {
1405 "%s: Device temperature %d degrees C "
1406 "exceeds operating range."
1407 " Immediate action needed.\n",
1408 netxen_nic_driver_name, temp_val);
1411 if (adapter->temp == NX_TEMP_WARN) {
1413 "%s: Device temperature is now %d degrees C"
1414 " in normal range.\n", netxen_nic_driver_name,
1418 adapter->temp = temp_state;
1422 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1424 struct net_device *netdev = adapter->netdev;
1425 u32 val, port, linkup;
1427 port = adapter->physical_port;
1429 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1430 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1431 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1432 linkup = (val == XG_LINK_UP_P3);
1434 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1435 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1436 linkup = (val >> port) & 1;
1438 val = (val >> port*8) & 0xff;
1439 linkup = (val == XG_LINK_UP);
1443 if (adapter->ahw.linkup && !linkup) {
1444 printk(KERN_INFO "%s: %s NIC Link is down\n",
1445 netxen_nic_driver_name, netdev->name);
1446 adapter->ahw.linkup = 0;
1447 if (netif_running(netdev)) {
1448 netif_carrier_off(netdev);
1449 netif_stop_queue(netdev);
1452 netxen_nic_set_link_parameters(adapter);
1453 } else if (!adapter->ahw.linkup && linkup) {
1454 printk(KERN_INFO "%s: %s NIC Link is up\n",
1455 netxen_nic_driver_name, netdev->name);
1456 adapter->ahw.linkup = 1;
1457 if (netif_running(netdev)) {
1458 netif_carrier_on(netdev);
1459 netif_wake_queue(netdev);
1462 netxen_nic_set_link_parameters(adapter);
1466 static void netxen_watchdog(unsigned long v)
1468 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1470 SCHEDULE_WORK(&adapter->watchdog_task);
1473 void netxen_watchdog_task(struct work_struct *work)
1475 struct netxen_adapter *adapter =
1476 container_of(work, struct netxen_adapter, watchdog_task);
1478 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1481 netxen_nic_handle_phy_intr(adapter);
1483 if (netif_running(adapter->netdev))
1484 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1487 static void netxen_tx_timeout(struct net_device *netdev)
1489 struct netxen_adapter *adapter = (struct netxen_adapter *)
1490 netdev_priv(netdev);
1491 SCHEDULE_WORK(&adapter->tx_timeout_task);
1494 static void netxen_tx_timeout_task(struct work_struct *work)
1496 struct netxen_adapter *adapter =
1497 container_of(work, struct netxen_adapter, tx_timeout_task);
1499 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1500 netxen_nic_driver_name, adapter->netdev->name);
1502 netxen_nic_disable_int(adapter);
1503 napi_disable(&adapter->napi);
1505 adapter->netdev->trans_start = jiffies;
1507 napi_enable(&adapter->napi);
1508 netxen_nic_enable_int(adapter);
1509 netif_wake_queue(adapter->netdev);
1513 * netxen_nic_get_stats - Get System Network Statistics
1514 * @netdev: network interface device structure
1516 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1518 struct netxen_adapter *adapter = netdev_priv(netdev);
1519 struct net_device_stats *stats = &adapter->net_stats;
1521 memset(stats, 0, sizeof(*stats));
1523 /* total packets received */
1524 stats->rx_packets = adapter->stats.no_rcv;
1525 /* total packets transmitted */
1526 stats->tx_packets = adapter->stats.xmitedframes +
1527 adapter->stats.xmitfinished;
1528 /* total bytes received */
1529 stats->rx_bytes = adapter->stats.rxbytes;
1530 /* total bytes transmitted */
1531 stats->tx_bytes = adapter->stats.txbytes;
1532 /* bad packets received */
1533 stats->rx_errors = adapter->stats.rcvdbadskb;
1534 /* packet transmit problems */
1535 stats->tx_errors = adapter->stats.nocmddescriptor;
1536 /* no space in linux buffers */
1537 stats->rx_dropped = adapter->stats.rxdropped;
1538 /* no space available in linux */
1539 stats->tx_dropped = adapter->stats.txdropped;
1544 static irqreturn_t netxen_intr(int irq, void *data)
1546 struct netxen_adapter *adapter = data;
1549 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1551 if (!(status & adapter->legacy_intr.int_vec_bit))
1554 if (adapter->ahw.revision_id >= NX_P3_B1) {
1555 /* check interrupt state machine, to be sure */
1556 status = adapter->pci_read_immediate(adapter,
1558 if (!ISR_LEGACY_INT_TRIGGERED(status))
1562 unsigned long our_int = 0;
1564 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
1566 /* not our interrupt */
1567 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
1570 /* claim interrupt */
1571 adapter->pci_write_normalize(adapter,
1572 CRB_INT_VECTOR, (our_int & 0xffffffff));
1575 /* clear interrupt */
1576 if (adapter->fw_major < 4)
1577 netxen_nic_disable_int(adapter);
1579 adapter->pci_write_immediate(adapter,
1580 adapter->legacy_intr.tgt_status_reg,
1582 /* read twice to ensure write is flushed */
1583 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1584 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1586 napi_schedule(&adapter->napi);
1591 static irqreturn_t netxen_msi_intr(int irq, void *data)
1593 struct netxen_adapter *adapter = data;
1595 /* clear interrupt */
1596 adapter->pci_write_immediate(adapter,
1597 msi_tgt_status[adapter->ahw.pci_func], 0xffffffff);
1599 napi_schedule(&adapter->napi);
1603 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1605 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1610 tx_complete = netxen_process_cmd_ring(adapter);
1613 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1615 * Fairness issue. This will give undue weight to the
1616 * receive context 0.
1620 * To avoid starvation, we give each of our receivers,
1621 * a fraction of the quota. Sometimes, it might happen that we
1622 * have enough quota to process every packet, but since all the
1623 * packets are on one context, it gets only half of the quota,
1624 * and ends up not processing it.
1626 work_done += netxen_process_rcv_ring(adapter, ctx,
1627 budget / MAX_RCV_CTX);
1630 if ((work_done < budget) && tx_complete) {
1631 netif_rx_complete(&adapter->napi);
1632 netxen_nic_enable_int(adapter);
1638 #ifdef CONFIG_NET_POLL_CONTROLLER
1639 static void netxen_nic_poll_controller(struct net_device *netdev)
1641 struct netxen_adapter *adapter = netdev_priv(netdev);
1642 disable_irq(adapter->irq);
1643 netxen_intr(adapter->irq, adapter);
1644 enable_irq(adapter->irq);
1648 static struct pci_driver netxen_driver = {
1649 .name = netxen_nic_driver_name,
1650 .id_table = netxen_pci_tbl,
1651 .probe = netxen_nic_probe,
1652 .remove = __devexit_p(netxen_nic_remove)
1655 /* Driver Registration on NetXen card */
1657 static int __init netxen_init_module(void)
1659 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1662 return pci_register_driver(&netxen_driver);
1665 module_init(netxen_init_module);
1667 static void __exit netxen_exit_module(void)
1669 pci_unregister_driver(&netxen_driver);
1670 destroy_workqueue(netxen_workq);
1673 module_exit(netxen_exit_module);