2 * Copyright (C) 2003 - 2009 NetXen, Inc.
3 * Copyright (C) 2009 - QLogic Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * The full GNU General Public License is included in this distribution
22 * in the file called "COPYING".
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/interrupt.h>
29 #include "netxen_nic_hw.h"
31 #include "netxen_nic.h"
33 #include <linux/dma-mapping.h>
34 #include <linux/if_vlan.h>
36 #include <linux/ipv6.h>
37 #include <linux/inetdevice.h>
38 #include <linux/sysfs.h>
39 #include <linux/aer.h>
41 MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Converged Ethernet Driver");
42 MODULE_LICENSE("GPL");
43 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
44 MODULE_FIRMWARE(NX_P2_MN_ROMIMAGE_NAME);
45 MODULE_FIRMWARE(NX_P3_CT_ROMIMAGE_NAME);
46 MODULE_FIRMWARE(NX_P3_MN_ROMIMAGE_NAME);
47 MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
49 char netxen_nic_driver_name[] = "netxen_nic";
50 static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
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 static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
63 module_param(auto_fw_reset, int, 0644);
64 MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
66 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
67 const struct pci_device_id *ent);
68 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
69 static int netxen_nic_open(struct net_device *netdev);
70 static int netxen_nic_close(struct net_device *netdev);
71 static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
73 static void netxen_tx_timeout(struct net_device *netdev);
74 static void netxen_tx_timeout_task(struct work_struct *work);
75 static void netxen_fw_poll_work(struct work_struct *work);
76 static void netxen_schedule_work(struct netxen_adapter *adapter,
77 work_func_t func, int delay);
78 static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
79 static int netxen_nic_poll(struct napi_struct *napi, int budget);
80 #ifdef CONFIG_NET_POLL_CONTROLLER
81 static void netxen_nic_poll_controller(struct net_device *netdev);
84 static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
85 static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
86 static void netxen_create_diag_entries(struct netxen_adapter *adapter);
87 static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
89 static int nx_dev_request_aer(struct netxen_adapter *adapter);
90 static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
91 static int netxen_can_start_firmware(struct netxen_adapter *adapter);
93 static irqreturn_t netxen_intr(int irq, void *data);
94 static irqreturn_t netxen_msi_intr(int irq, void *data);
95 static irqreturn_t netxen_msix_intr(int irq, void *data);
97 static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
99 /* PCI Device ID Table */
100 #define ENTRY(device) \
101 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
102 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
104 static DEFINE_PCI_DEVICE_TABLE(netxen_pci_tbl) = {
105 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
106 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
107 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
108 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
109 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
110 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
111 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
112 ENTRY(PCI_DEVICE_ID_NX3031),
116 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
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 struct nx_host_tx_ring *tx_ring)
127 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
129 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
130 netif_stop_queue(adapter->netdev);
135 static uint32_t crb_cmd_consumer[4] = {
136 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
137 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
141 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
142 struct nx_host_tx_ring *tx_ring)
144 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
147 static uint32_t msi_tgt_status[8] = {
148 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
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
154 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
156 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
158 struct netxen_adapter *adapter = sds_ring->adapter;
160 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
163 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
165 struct netxen_adapter *adapter = sds_ring->adapter;
167 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
169 if (!NETXEN_IS_MSI_FAMILY(adapter))
170 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
174 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
176 int size = sizeof(struct nx_host_sds_ring) * count;
178 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
180 return (recv_ctx->sds_rings == NULL);
184 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
186 if (recv_ctx->sds_rings != NULL)
187 kfree(recv_ctx->sds_rings);
189 recv_ctx->sds_rings = NULL;
193 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
196 struct nx_host_sds_ring *sds_ring;
197 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
199 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
202 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
203 sds_ring = &recv_ctx->sds_rings[ring];
204 netif_napi_add(netdev, &sds_ring->napi,
205 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
212 netxen_napi_del(struct netxen_adapter *adapter)
215 struct nx_host_sds_ring *sds_ring;
216 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
218 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
219 sds_ring = &recv_ctx->sds_rings[ring];
220 netif_napi_del(&sds_ring->napi);
223 netxen_free_sds_rings(&adapter->recv_ctx);
227 netxen_napi_enable(struct netxen_adapter *adapter)
230 struct nx_host_sds_ring *sds_ring;
231 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
233 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
234 sds_ring = &recv_ctx->sds_rings[ring];
235 napi_enable(&sds_ring->napi);
236 netxen_nic_enable_int(sds_ring);
241 netxen_napi_disable(struct netxen_adapter *adapter)
244 struct nx_host_sds_ring *sds_ring;
245 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
247 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
248 sds_ring = &recv_ctx->sds_rings[ring];
249 netxen_nic_disable_int(sds_ring);
250 napi_synchronize(&sds_ring->napi);
251 napi_disable(&sds_ring->napi);
255 static int nx_set_dma_mask(struct netxen_adapter *adapter)
257 struct pci_dev *pdev = adapter->pdev;
258 uint64_t mask, cmask;
260 adapter->pci_using_dac = 0;
262 mask = DMA_BIT_MASK(32);
263 cmask = DMA_BIT_MASK(32);
265 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
267 mask = DMA_BIT_MASK(35);
270 mask = DMA_BIT_MASK(39);
274 if (pci_set_dma_mask(pdev, mask) == 0 &&
275 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
276 adapter->pci_using_dac = 1;
283 /* Update addressable range if firmware supports it */
285 nx_update_dma_mask(struct netxen_adapter *adapter)
287 int change, shift, err;
288 uint64_t mask, old_mask, old_cmask;
289 struct pci_dev *pdev = adapter->pdev;
293 shift = NXRD32(adapter, CRB_DMA_SHIFT);
297 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
299 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
303 old_mask = pdev->dma_mask;
304 old_cmask = pdev->dev.coherent_dma_mask;
306 mask = DMA_BIT_MASK(32+shift);
308 err = pci_set_dma_mask(pdev, mask);
312 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
314 err = pci_set_consistent_dma_mask(pdev, mask);
318 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
324 pci_set_dma_mask(pdev, old_mask);
325 pci_set_consistent_dma_mask(pdev, old_cmask);
330 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
334 if (first_boot == 0x55555555) {
335 /* This is the first boot after power up */
336 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
338 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
341 /* PCI bus master workaround */
342 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
343 if (!(first_boot & 0x4)) {
345 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
346 NXRD32(adapter, NETXEN_PCIE_REG(0x4));
349 /* This is the first boot after power up */
350 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
351 if (first_boot != 0x80000f) {
352 /* clear the register for future unloads/loads */
353 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
357 /* Start P2 boot loader */
358 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
359 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
363 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
365 if (++timeout > 5000)
368 } while (val == NETXEN_BDINFO_MAGIC);
373 static void netxen_set_port_mode(struct netxen_adapter *adapter)
377 val = adapter->ahw.board_type;
378 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
379 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
380 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
381 data = NETXEN_PORT_MODE_802_3_AP;
382 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
383 } else if (port_mode == NETXEN_PORT_MODE_XG) {
384 data = NETXEN_PORT_MODE_XG;
385 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
386 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
387 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
388 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
389 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
390 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
391 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
393 data = NETXEN_PORT_MODE_AUTO_NEG;
394 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
397 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
398 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
399 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
400 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
401 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
403 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
407 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
412 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
414 pci_read_config_dword(pdev, pos, &control);
416 control |= PCI_MSIX_FLAGS_ENABLE;
419 pci_write_config_dword(pdev, pos, control);
423 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
427 for (i = 0; i < count; i++)
428 adapter->msix_entries[i].entry = i;
432 netxen_read_mac_addr(struct netxen_adapter *adapter)
437 struct net_device *netdev = adapter->netdev;
438 struct pci_dev *pdev = adapter->pdev;
440 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
441 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
444 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
448 p = (unsigned char *)&mac_addr;
449 for (i = 0; i < 6; i++)
450 netdev->dev_addr[i] = *(p + 5 - i);
452 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
453 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
455 /* set station address */
457 if (!is_valid_ether_addr(netdev->perm_addr))
458 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
463 int netxen_nic_set_mac(struct net_device *netdev, void *p)
465 struct netxen_adapter *adapter = netdev_priv(netdev);
466 struct sockaddr *addr = p;
468 if (!is_valid_ether_addr(addr->sa_data))
471 if (netif_running(netdev)) {
472 netif_device_detach(netdev);
473 netxen_napi_disable(adapter);
476 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
477 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
478 adapter->macaddr_set(adapter, addr->sa_data);
480 if (netif_running(netdev)) {
481 netif_device_attach(netdev);
482 netxen_napi_enable(adapter);
487 static void netxen_set_multicast_list(struct net_device *dev)
489 struct netxen_adapter *adapter = netdev_priv(dev);
491 adapter->set_multi(dev);
494 static const struct net_device_ops netxen_netdev_ops = {
495 .ndo_open = netxen_nic_open,
496 .ndo_stop = netxen_nic_close,
497 .ndo_start_xmit = netxen_nic_xmit_frame,
498 .ndo_get_stats = netxen_nic_get_stats,
499 .ndo_validate_addr = eth_validate_addr,
500 .ndo_set_multicast_list = netxen_set_multicast_list,
501 .ndo_set_mac_address = netxen_nic_set_mac,
502 .ndo_change_mtu = netxen_nic_change_mtu,
503 .ndo_tx_timeout = netxen_tx_timeout,
504 #ifdef CONFIG_NET_POLL_CONTROLLER
505 .ndo_poll_controller = netxen_nic_poll_controller,
510 netxen_setup_intr(struct netxen_adapter *adapter)
512 struct netxen_legacy_intr_set *legacy_intrp;
513 struct pci_dev *pdev = adapter->pdev;
516 if (adapter->rss_supported) {
517 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
518 MSIX_ENTRIES_PER_ADAPTER : 2;
522 adapter->max_sds_rings = 1;
524 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
526 if (adapter->ahw.revision_id >= NX_P3_B0)
527 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
529 legacy_intrp = &legacy_intr[0];
531 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
532 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
533 legacy_intrp->tgt_status_reg);
534 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
535 legacy_intrp->tgt_mask_reg);
536 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
537 legacy_intrp->pci_int_reg);
538 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
540 if (adapter->ahw.revision_id >= NX_P3_B1)
541 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
544 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
547 netxen_set_msix_bit(pdev, 0);
549 if (adapter->msix_supported) {
551 netxen_init_msix_entries(adapter, num_msix);
552 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
554 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
555 netxen_set_msix_bit(pdev, 1);
557 if (adapter->rss_supported)
558 adapter->max_sds_rings = num_msix;
560 dev_info(&pdev->dev, "using msi-x interrupts\n");
565 pci_disable_msix(pdev);
567 /* fall through for msi */
570 if (use_msi && !pci_enable_msi(pdev)) {
571 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
572 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
573 msi_tgt_status[adapter->ahw.pci_func]);
574 dev_info(&pdev->dev, "using msi interrupts\n");
575 adapter->msix_entries[0].vector = pdev->irq;
579 dev_info(&pdev->dev, "using legacy interrupts\n");
580 adapter->msix_entries[0].vector = pdev->irq;
584 netxen_teardown_intr(struct netxen_adapter *adapter)
586 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
587 pci_disable_msix(adapter->pdev);
588 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
589 pci_disable_msi(adapter->pdev);
593 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
595 if (adapter->ahw.db_base != NULL)
596 iounmap(adapter->ahw.db_base);
597 if (adapter->ahw.pci_base0 != NULL)
598 iounmap(adapter->ahw.pci_base0);
599 if (adapter->ahw.pci_base1 != NULL)
600 iounmap(adapter->ahw.pci_base1);
601 if (adapter->ahw.pci_base2 != NULL)
602 iounmap(adapter->ahw.pci_base2);
606 netxen_setup_pci_map(struct netxen_adapter *adapter)
608 void __iomem *db_ptr = NULL;
610 resource_size_t mem_base, db_base;
611 unsigned long mem_len, db_len = 0;
613 struct pci_dev *pdev = adapter->pdev;
614 int pci_func = adapter->ahw.pci_func;
615 struct netxen_hardware_context *ahw = &adapter->ahw;
620 * Set the CRB window to invalid. If any register in window 0 is
621 * accessed it should set the window to 0 and then reset it to 1.
623 adapter->ahw.crb_win = -1;
624 adapter->ahw.ocm_win = -1;
626 /* remap phys address */
627 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
628 mem_len = pci_resource_len(pdev, 0);
630 /* 128 Meg of memory */
631 if (mem_len == NETXEN_PCI_128MB_SIZE) {
633 ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
634 ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
635 SECOND_PAGE_GROUP_SIZE);
636 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
637 THIRD_PAGE_GROUP_SIZE);
638 if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
639 ahw->pci_base2 == NULL) {
640 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
645 ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
647 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
649 ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
650 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
651 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
652 if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
653 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
658 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
660 ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
661 if (ahw->pci_base0 == NULL) {
662 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
665 ahw->pci_len0 = mem_len;
670 netxen_setup_hwops(adapter);
672 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
674 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
675 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
676 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
678 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
679 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
680 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
683 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
686 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
687 db_len = pci_resource_len(pdev, 4);
690 printk(KERN_ERR "%s: doorbell is disabled\n",
691 netxen_nic_driver_name);
696 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
698 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
699 netxen_nic_driver_name);
705 adapter->ahw.db_base = db_ptr;
706 adapter->ahw.db_len = db_len;
710 netxen_cleanup_pci_map(adapter);
715 netxen_check_options(struct netxen_adapter *adapter)
717 u32 fw_major, fw_minor, fw_build;
718 char brd_name[NETXEN_MAX_SHORT_NAME];
722 struct pci_dev *pdev = adapter->pdev;
724 adapter->driver_mismatch = 0;
726 ptr32 = (int *)&serial_num;
727 offset = NX_FW_SERIAL_NUM_OFFSET;
728 for (i = 0; i < 8; i++) {
729 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
730 dev_err(&pdev->dev, "error reading board info\n");
731 adapter->driver_mismatch = 1;
734 ptr32[i] = cpu_to_le32(val);
735 offset += sizeof(u32);
738 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
739 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
740 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
742 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
744 if (adapter->portnum == 0) {
745 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
747 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
748 module_name(THIS_MODULE),
749 brd_name, serial_num, adapter->ahw.revision_id);
752 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
753 adapter->driver_mismatch = 1;
754 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
755 fw_major, fw_minor, fw_build);
759 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
760 i = NXRD32(adapter, NETXEN_SRE_MISC);
761 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
764 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
765 fw_major, fw_minor, fw_build,
766 adapter->ahw.cut_through ? "cut-through" : "legacy");
768 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
769 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
771 adapter->flags &= ~NETXEN_NIC_LRO_ENABLED;
773 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
774 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
775 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
776 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
777 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
778 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
781 adapter->msix_supported = 0;
782 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
783 adapter->msix_supported = !!use_msi_x;
784 adapter->rss_supported = !!use_msi_x;
787 netxen_rom_fast_read(adapter,
788 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
789 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
791 if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
792 switch (adapter->ahw.board_type) {
793 case NETXEN_BRDTYPE_P2_SB31_10G:
794 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
795 adapter->msix_supported = !!use_msi_x;
796 adapter->rss_supported = !!use_msi_x;
804 adapter->num_txd = MAX_CMD_DESCRIPTORS;
806 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
807 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
808 adapter->max_rds_rings = 3;
810 adapter->num_lro_rxd = 0;
811 adapter->max_rds_rings = 2;
816 netxen_start_firmware(struct netxen_adapter *adapter)
818 int val, err, first_boot;
819 struct pci_dev *pdev = adapter->pdev;
821 /* required for NX2031 dummy dma */
822 err = nx_set_dma_mask(adapter);
826 if (!netxen_can_start_firmware(adapter))
829 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
831 err = netxen_check_hw_init(adapter, first_boot);
833 dev_err(&pdev->dev, "error in init HW init sequence\n");
837 netxen_request_firmware(adapter);
839 err = netxen_need_fw_reset(adapter);
845 if (first_boot != 0x55555555) {
846 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
847 netxen_pinit_from_rom(adapter);
851 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
852 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
853 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
855 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
856 netxen_set_port_mode(adapter);
858 err = netxen_load_firmware(adapter);
862 netxen_release_firmware(adapter);
864 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
866 /* Initialize multicast addr pool owners */
868 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
870 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
874 err = netxen_init_dummy_dma(adapter);
879 * Tell the hardware our version number.
881 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
882 | ((_NETXEN_NIC_LINUX_MINOR << 8))
883 | (_NETXEN_NIC_LINUX_SUBVERSION);
884 NXWR32(adapter, CRB_DRIVER_VERSION, val);
887 /* Handshake with the card before we register the devices. */
888 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
890 netxen_free_dummy_dma(adapter);
894 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
896 nx_update_dma_mask(adapter);
898 netxen_check_options(adapter);
900 adapter->need_fw_reset = 0;
902 /* fall through and release firmware */
905 netxen_release_firmware(adapter);
910 netxen_nic_request_irq(struct netxen_adapter *adapter)
912 irq_handler_t handler;
913 struct nx_host_sds_ring *sds_ring;
916 unsigned long flags = IRQF_SAMPLE_RANDOM;
917 struct net_device *netdev = adapter->netdev;
918 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
920 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
921 handler = netxen_msix_intr;
922 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
923 handler = netxen_msi_intr;
925 flags |= IRQF_SHARED;
926 handler = netxen_intr;
928 adapter->irq = netdev->irq;
930 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
931 sds_ring = &recv_ctx->sds_rings[ring];
932 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
933 err = request_irq(sds_ring->irq, handler,
934 flags, sds_ring->name, sds_ring);
943 netxen_nic_free_irq(struct netxen_adapter *adapter)
946 struct nx_host_sds_ring *sds_ring;
948 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
950 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
951 sds_ring = &recv_ctx->sds_rings[ring];
952 free_irq(sds_ring->irq, sds_ring);
957 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
959 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
960 adapter->coal.normal.data.rx_time_us =
961 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
962 adapter->coal.normal.data.rx_packets =
963 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
964 adapter->coal.normal.data.tx_time_us =
965 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
966 adapter->coal.normal.data.tx_packets =
967 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
972 __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
976 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
979 err = adapter->init_port(adapter, adapter->physical_port);
981 printk(KERN_ERR "%s: Failed to initialize port %d\n",
982 netxen_nic_driver_name, adapter->portnum);
985 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
986 adapter->macaddr_set(adapter, adapter->mac_addr);
988 adapter->set_multi(netdev);
989 adapter->set_mtu(adapter, netdev->mtu);
991 adapter->ahw.linkup = 0;
993 if (adapter->max_sds_rings > 1)
994 netxen_config_rss(adapter, 1);
996 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
997 netxen_config_intr_coalesce(adapter);
999 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1000 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
1002 netxen_napi_enable(adapter);
1004 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1005 netxen_linkevent_request(adapter, 1);
1007 netxen_nic_set_link_parameters(adapter);
1009 set_bit(__NX_DEV_UP, &adapter->state);
1013 /* Usage: During resume and firmware recovery module.*/
1016 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1021 if (netif_running(netdev))
1022 err = __netxen_nic_up(adapter, netdev);
1028 /* with rtnl_lock */
1030 __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1032 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1035 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1039 spin_lock(&adapter->tx_clean_lock);
1040 netif_carrier_off(netdev);
1041 netif_tx_disable(netdev);
1043 if (adapter->stop_port)
1044 adapter->stop_port(adapter);
1046 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1047 netxen_p3_free_mac_list(adapter);
1049 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1051 netxen_napi_disable(adapter);
1053 netxen_release_tx_buffers(adapter);
1054 spin_unlock(&adapter->tx_clean_lock);
1057 /* Usage: During suspend and firmware recovery module */
1060 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1063 if (netif_running(netdev))
1064 __netxen_nic_down(adapter, netdev);
1070 netxen_nic_attach(struct netxen_adapter *adapter)
1072 struct net_device *netdev = adapter->netdev;
1073 struct pci_dev *pdev = adapter->pdev;
1075 struct nx_host_rds_ring *rds_ring;
1076 struct nx_host_tx_ring *tx_ring;
1078 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1081 err = netxen_init_firmware(adapter);
1085 err = netxen_napi_add(adapter, netdev);
1089 err = netxen_alloc_sw_resources(adapter);
1091 printk(KERN_ERR "%s: Error in setting sw resources\n",
1096 err = netxen_alloc_hw_resources(adapter);
1098 printk(KERN_ERR "%s: Error in setting hw resources\n",
1100 goto err_out_free_sw;
1103 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1104 tx_ring = adapter->tx_ring;
1105 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1106 crb_cmd_producer[adapter->portnum]);
1107 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1108 crb_cmd_consumer[adapter->portnum]);
1110 tx_ring->producer = 0;
1111 tx_ring->sw_consumer = 0;
1113 netxen_nic_update_cmd_producer(adapter, tx_ring);
1114 netxen_nic_update_cmd_consumer(adapter, tx_ring);
1117 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1118 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1119 netxen_post_rx_buffers(adapter, ring, rds_ring);
1122 err = netxen_nic_request_irq(adapter);
1124 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1126 goto err_out_free_rxbuf;
1129 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1130 netxen_nic_init_coalesce_defaults(adapter);
1132 netxen_create_sysfs_entries(adapter);
1134 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1138 netxen_release_rx_buffers(adapter);
1139 netxen_free_hw_resources(adapter);
1141 netxen_free_sw_resources(adapter);
1146 netxen_nic_detach(struct netxen_adapter *adapter)
1148 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1151 netxen_remove_sysfs_entries(adapter);
1153 netxen_free_hw_resources(adapter);
1154 netxen_release_rx_buffers(adapter);
1155 netxen_nic_free_irq(adapter);
1156 netxen_napi_del(adapter);
1157 netxen_free_sw_resources(adapter);
1163 netxen_nic_reset_context(struct netxen_adapter *adapter)
1166 struct net_device *netdev = adapter->netdev;
1168 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1171 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1173 netif_device_detach(netdev);
1175 if (netif_running(netdev))
1176 __netxen_nic_down(adapter, netdev);
1178 netxen_nic_detach(adapter);
1180 if (netif_running(netdev)) {
1181 err = netxen_nic_attach(adapter);
1183 err = __netxen_nic_up(adapter, netdev);
1189 netif_device_attach(netdev);
1193 clear_bit(__NX_RESETTING, &adapter->state);
1198 netxen_setup_netdev(struct netxen_adapter *adapter,
1199 struct net_device *netdev)
1202 struct pci_dev *pdev = adapter->pdev;
1204 adapter->rx_csum = 1;
1205 adapter->mc_enabled = 0;
1206 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1207 adapter->max_mc_count = 38;
1209 adapter->max_mc_count = 16;
1211 netdev->netdev_ops = &netxen_netdev_ops;
1212 netdev->watchdog_timeo = 2*HZ;
1214 netxen_nic_change_mtu(netdev, netdev->mtu);
1216 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1218 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1219 netdev->features |= (NETIF_F_GRO);
1220 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1222 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1223 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1224 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1227 if (adapter->pci_using_dac) {
1228 netdev->features |= NETIF_F_HIGHDMA;
1229 netdev->vlan_features |= NETIF_F_HIGHDMA;
1232 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1233 netdev->features |= (NETIF_F_HW_VLAN_TX);
1235 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1236 netdev->features |= NETIF_F_LRO;
1238 netdev->irq = adapter->msix_entries[0].vector;
1240 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1242 if (netxen_read_mac_addr(adapter))
1243 dev_warn(&pdev->dev, "failed to read mac addr\n");
1245 netif_carrier_off(netdev);
1246 netif_stop_queue(netdev);
1248 err = register_netdev(netdev);
1250 dev_err(&pdev->dev, "failed to register net device\n");
1257 static int __devinit
1258 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1260 struct net_device *netdev = NULL;
1261 struct netxen_adapter *adapter = NULL;
1263 int pci_func_id = PCI_FUNC(pdev->devfn);
1264 uint8_t revision_id;
1266 if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
1267 pr_warning("%s: chip revisions between 0x%x-0x%x "
1268 "will not be enabled.\n",
1269 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1273 if ((err = pci_enable_device(pdev)))
1276 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1278 goto err_out_disable_pdev;
1281 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1282 goto err_out_disable_pdev;
1284 if (NX_IS_REVISION_P3(pdev->revision))
1285 pci_enable_pcie_error_reporting(pdev);
1287 pci_set_master(pdev);
1289 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1291 dev_err(&pdev->dev, "failed to allocate net_device\n");
1293 goto err_out_free_res;
1296 SET_NETDEV_DEV(netdev, &pdev->dev);
1298 adapter = netdev_priv(netdev);
1299 adapter->netdev = netdev;
1300 adapter->pdev = pdev;
1301 adapter->ahw.pci_func = pci_func_id;
1303 revision_id = pdev->revision;
1304 adapter->ahw.revision_id = revision_id;
1306 rwlock_init(&adapter->ahw.crb_lock);
1307 spin_lock_init(&adapter->ahw.mem_lock);
1309 spin_lock_init(&adapter->tx_clean_lock);
1310 INIT_LIST_HEAD(&adapter->mac_list);
1312 err = netxen_setup_pci_map(adapter);
1314 goto err_out_free_netdev;
1316 /* This will be reset for mezz cards */
1317 adapter->portnum = pci_func_id;
1319 err = netxen_nic_get_board_info(adapter);
1321 dev_err(&pdev->dev, "Error getting board config info.\n");
1322 goto err_out_iounmap;
1325 /* Mezz cards have PCI function 0,2,3 enabled */
1326 switch (adapter->ahw.board_type) {
1327 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1328 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1329 if (pci_func_id >= 2)
1330 adapter->portnum = pci_func_id - 2;
1336 err = netxen_start_firmware(adapter);
1338 goto err_out_decr_ref;
1341 * See if the firmware gave us a virtual-physical port mapping.
1343 adapter->physical_port = adapter->portnum;
1344 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1345 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1346 if (i != 0x55555555)
1347 adapter->physical_port = i;
1350 netxen_nic_clear_stats(adapter);
1352 netxen_setup_intr(adapter);
1354 err = netxen_setup_netdev(adapter, netdev);
1356 goto err_out_disable_msi;
1358 pci_set_drvdata(pdev, adapter);
1360 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1362 switch (adapter->ahw.port_type) {
1363 case NETXEN_NIC_GBE:
1364 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1365 adapter->netdev->name);
1367 case NETXEN_NIC_XGBE:
1368 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1369 adapter->netdev->name);
1373 netxen_create_diag_entries(adapter);
1377 err_out_disable_msi:
1378 netxen_teardown_intr(adapter);
1380 netxen_free_dummy_dma(adapter);
1383 nx_decr_dev_ref_cnt(adapter);
1386 netxen_cleanup_pci_map(adapter);
1388 err_out_free_netdev:
1389 free_netdev(netdev);
1392 pci_release_regions(pdev);
1394 err_out_disable_pdev:
1395 pci_set_drvdata(pdev, NULL);
1396 pci_disable_device(pdev);
1400 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1402 struct netxen_adapter *adapter;
1403 struct net_device *netdev;
1405 adapter = pci_get_drvdata(pdev);
1406 if (adapter == NULL)
1409 netdev = adapter->netdev;
1411 netxen_cancel_fw_work(adapter);
1413 unregister_netdev(netdev);
1415 cancel_work_sync(&adapter->tx_timeout_task);
1417 netxen_nic_detach(adapter);
1419 nx_decr_dev_ref_cnt(adapter);
1421 if (adapter->portnum == 0)
1422 netxen_free_dummy_dma(adapter);
1424 clear_bit(__NX_RESETTING, &adapter->state);
1426 netxen_teardown_intr(adapter);
1428 netxen_remove_diag_entries(adapter);
1430 netxen_cleanup_pci_map(adapter);
1432 netxen_release_firmware(adapter);
1434 if (NX_IS_REVISION_P3(pdev->revision))
1435 pci_disable_pcie_error_reporting(pdev);
1437 pci_release_regions(pdev);
1438 pci_disable_device(pdev);
1439 pci_set_drvdata(pdev, NULL);
1441 free_netdev(netdev);
1444 static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1446 struct net_device *netdev = adapter->netdev;
1448 netif_device_detach(netdev);
1450 netxen_cancel_fw_work(adapter);
1452 if (netif_running(netdev))
1453 netxen_nic_down(adapter, netdev);
1455 cancel_work_sync(&adapter->tx_timeout_task);
1457 netxen_nic_detach(adapter);
1459 if (adapter->portnum == 0)
1460 netxen_free_dummy_dma(adapter);
1462 nx_decr_dev_ref_cnt(adapter);
1464 clear_bit(__NX_RESETTING, &adapter->state);
1467 static int netxen_nic_attach_func(struct pci_dev *pdev)
1469 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1470 struct net_device *netdev = adapter->netdev;
1473 err = pci_enable_device(pdev);
1477 pci_set_power_state(pdev, PCI_D0);
1478 pci_set_master(pdev);
1479 pci_restore_state(pdev);
1481 adapter->ahw.crb_win = -1;
1482 adapter->ahw.ocm_win = -1;
1484 err = netxen_start_firmware(adapter);
1486 dev_err(&pdev->dev, "failed to start firmware\n");
1490 if (netif_running(netdev)) {
1491 err = netxen_nic_attach(adapter);
1495 err = netxen_nic_up(adapter, netdev);
1497 goto err_out_detach;
1499 netxen_config_indev_addr(netdev, NETDEV_UP);
1502 netif_device_attach(netdev);
1503 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1507 netxen_nic_detach(adapter);
1509 nx_decr_dev_ref_cnt(adapter);
1513 static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1514 pci_channel_state_t state)
1516 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1518 if (state == pci_channel_io_perm_failure)
1519 return PCI_ERS_RESULT_DISCONNECT;
1521 if (nx_dev_request_aer(adapter))
1522 return PCI_ERS_RESULT_RECOVERED;
1524 netxen_nic_detach_func(adapter);
1526 pci_disable_device(pdev);
1528 return PCI_ERS_RESULT_NEED_RESET;
1531 static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1535 err = netxen_nic_attach_func(pdev);
1537 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1540 static void netxen_io_resume(struct pci_dev *pdev)
1542 pci_cleanup_aer_uncorrect_error_status(pdev);
1545 static void netxen_nic_shutdown(struct pci_dev *pdev)
1547 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1549 netxen_nic_detach_func(adapter);
1551 if (pci_save_state(pdev))
1554 if (netxen_nic_wol_supported(adapter)) {
1555 pci_enable_wake(pdev, PCI_D3cold, 1);
1556 pci_enable_wake(pdev, PCI_D3hot, 1);
1559 pci_disable_device(pdev);
1564 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1566 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1569 netxen_nic_detach_func(adapter);
1571 retval = pci_save_state(pdev);
1575 if (netxen_nic_wol_supported(adapter)) {
1576 pci_enable_wake(pdev, PCI_D3cold, 1);
1577 pci_enable_wake(pdev, PCI_D3hot, 1);
1580 pci_disable_device(pdev);
1581 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1587 netxen_nic_resume(struct pci_dev *pdev)
1589 return netxen_nic_attach_func(pdev);
1593 static int netxen_nic_open(struct net_device *netdev)
1595 struct netxen_adapter *adapter = netdev_priv(netdev);
1598 if (adapter->driver_mismatch)
1601 err = netxen_nic_attach(adapter);
1605 err = __netxen_nic_up(adapter, netdev);
1609 netif_start_queue(netdev);
1614 netxen_nic_detach(adapter);
1619 * netxen_nic_close - Disables a network interface entry point
1621 static int netxen_nic_close(struct net_device *netdev)
1623 struct netxen_adapter *adapter = netdev_priv(netdev);
1625 __netxen_nic_down(adapter, netdev);
1630 netxen_tso_check(struct net_device *netdev,
1631 struct nx_host_tx_ring *tx_ring,
1632 struct cmd_desc_type0 *first_desc,
1633 struct sk_buff *skb)
1635 u8 opcode = TX_ETHER_PKT;
1636 __be16 protocol = skb->protocol;
1637 u16 flags = 0, vid = 0;
1639 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1640 struct cmd_desc_type0 *hwdesc;
1641 struct vlan_ethhdr *vh;
1643 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1645 vh = (struct vlan_ethhdr *)skb->data;
1646 protocol = vh->h_vlan_encapsulated_proto;
1647 flags = FLAGS_VLAN_TAGGED;
1649 } else if (vlan_tx_tag_present(skb)) {
1651 flags = FLAGS_VLAN_OOB;
1652 vid = vlan_tx_tag_get(skb);
1653 netxen_set_tx_vlan_tci(first_desc, vid);
1657 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1658 skb_shinfo(skb)->gso_size > 0) {
1660 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1662 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1663 first_desc->total_hdr_length = hdr_len;
1665 first_desc->total_hdr_length += VLAN_HLEN;
1666 first_desc->tcp_hdr_offset = VLAN_HLEN;
1667 first_desc->ip_hdr_offset = VLAN_HLEN;
1668 /* Only in case of TSO on vlan device */
1669 flags |= FLAGS_VLAN_TAGGED;
1672 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1673 TX_TCP_LSO6 : TX_TCP_LSO;
1676 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1679 if (protocol == cpu_to_be16(ETH_P_IP)) {
1680 l4proto = ip_hdr(skb)->protocol;
1682 if (l4proto == IPPROTO_TCP)
1683 opcode = TX_TCP_PKT;
1684 else if(l4proto == IPPROTO_UDP)
1685 opcode = TX_UDP_PKT;
1686 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1687 l4proto = ipv6_hdr(skb)->nexthdr;
1689 if (l4proto == IPPROTO_TCP)
1690 opcode = TX_TCPV6_PKT;
1691 else if(l4proto == IPPROTO_UDP)
1692 opcode = TX_UDPV6_PKT;
1696 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1697 first_desc->ip_hdr_offset += skb_network_offset(skb);
1698 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1703 /* For LSO, we need to copy the MAC/IP/TCP headers into
1704 * the descriptor ring
1706 producer = tx_ring->producer;
1711 /* Create a TSO vlan header template for firmware */
1713 hwdesc = &tx_ring->desc_head[producer];
1714 tx_ring->cmd_buf_arr[producer].skb = NULL;
1716 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1717 hdr_len + VLAN_HLEN);
1719 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1720 skb_copy_from_linear_data(skb, vh, 12);
1721 vh->h_vlan_proto = htons(ETH_P_8021Q);
1722 vh->h_vlan_TCI = htons(vid);
1723 skb_copy_from_linear_data_offset(skb, 12,
1724 (char *)vh + 16, copy_len - 16);
1726 copied = copy_len - VLAN_HLEN;
1729 producer = get_next_index(producer, tx_ring->num_desc);
1732 while (copied < hdr_len) {
1734 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1735 (hdr_len - copied));
1737 hwdesc = &tx_ring->desc_head[producer];
1738 tx_ring->cmd_buf_arr[producer].skb = NULL;
1740 skb_copy_from_linear_data_offset(skb, copied,
1741 (char *)hwdesc + offset, copy_len);
1746 producer = get_next_index(producer, tx_ring->num_desc);
1749 tx_ring->producer = producer;
1754 netxen_map_tx_skb(struct pci_dev *pdev,
1755 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1757 struct netxen_skb_frag *nf;
1758 struct skb_frag_struct *frag;
1762 nr_frags = skb_shinfo(skb)->nr_frags;
1763 nf = &pbuf->frag_array[0];
1765 map = pci_map_single(pdev, skb->data,
1766 skb_headlen(skb), PCI_DMA_TODEVICE);
1767 if (pci_dma_mapping_error(pdev, map))
1771 nf->length = skb_headlen(skb);
1773 for (i = 0; i < nr_frags; i++) {
1774 frag = &skb_shinfo(skb)->frags[i];
1775 nf = &pbuf->frag_array[i+1];
1777 map = pci_map_page(pdev, frag->page, frag->page_offset,
1778 frag->size, PCI_DMA_TODEVICE);
1779 if (pci_dma_mapping_error(pdev, map))
1783 nf->length = frag->size;
1790 nf = &pbuf->frag_array[i+1];
1791 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1794 nf = &pbuf->frag_array[0];
1795 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1802 netxen_clear_cmddesc(u64 *desc)
1809 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1811 struct netxen_adapter *adapter = netdev_priv(netdev);
1812 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1813 struct netxen_cmd_buffer *pbuf;
1814 struct netxen_skb_frag *buffrag;
1815 struct cmd_desc_type0 *hwdesc, *first_desc;
1816 struct pci_dev *pdev;
1820 int frag_count, no_of_desc;
1821 u32 num_txd = tx_ring->num_desc;
1823 frag_count = skb_shinfo(skb)->nr_frags + 1;
1825 /* 4 fragments per cmd des */
1826 no_of_desc = (frag_count + 3) >> 2;
1828 if (unlikely(no_of_desc + 2 > netxen_tx_avail(tx_ring))) {
1829 netif_stop_queue(netdev);
1830 return NETDEV_TX_BUSY;
1833 producer = tx_ring->producer;
1834 pbuf = &tx_ring->cmd_buf_arr[producer];
1836 pdev = adapter->pdev;
1838 if (netxen_map_tx_skb(pdev, skb, pbuf))
1842 pbuf->frag_count = frag_count;
1844 first_desc = hwdesc = &tx_ring->desc_head[producer];
1845 netxen_clear_cmddesc((u64 *)hwdesc);
1847 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1848 netxen_set_tx_port(first_desc, adapter->portnum);
1850 for (i = 0; i < frag_count; i++) {
1854 if ((k == 0) && (i > 0)) {
1855 /* move to next desc.*/
1856 producer = get_next_index(producer, num_txd);
1857 hwdesc = &tx_ring->desc_head[producer];
1858 netxen_clear_cmddesc((u64 *)hwdesc);
1859 tx_ring->cmd_buf_arr[producer].skb = NULL;
1862 buffrag = &pbuf->frag_array[i];
1864 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1867 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1870 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1873 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1876 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1881 tx_ring->producer = get_next_index(producer, num_txd);
1883 netxen_tso_check(netdev, tx_ring, first_desc, skb);
1885 netxen_nic_update_cmd_producer(adapter, tx_ring);
1887 adapter->stats.txbytes += skb->len;
1888 adapter->stats.xmitcalled++;
1890 return NETDEV_TX_OK;
1893 adapter->stats.txdropped++;
1894 dev_kfree_skb_any(skb);
1895 return NETDEV_TX_OK;
1898 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1900 struct net_device *netdev = adapter->netdev;
1901 uint32_t temp, temp_state, temp_val;
1904 temp = NXRD32(adapter, CRB_TEMP_STATE);
1906 temp_state = nx_get_temp_state(temp);
1907 temp_val = nx_get_temp_val(temp);
1909 if (temp_state == NX_TEMP_PANIC) {
1911 "%s: Device temperature %d degrees C exceeds"
1912 " maximum allowed. Hardware has been shut down.\n",
1913 netdev->name, temp_val);
1915 } else if (temp_state == NX_TEMP_WARN) {
1916 if (adapter->temp == NX_TEMP_NORMAL) {
1918 "%s: Device temperature %d degrees C "
1919 "exceeds operating range."
1920 " Immediate action needed.\n",
1921 netdev->name, temp_val);
1924 if (adapter->temp == NX_TEMP_WARN) {
1926 "%s: Device temperature is now %d degrees C"
1927 " in normal range.\n", netdev->name,
1931 adapter->temp = temp_state;
1935 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
1937 struct net_device *netdev = adapter->netdev;
1939 if (adapter->ahw.linkup && !linkup) {
1940 printk(KERN_INFO "%s: %s NIC Link is down\n",
1941 netxen_nic_driver_name, netdev->name);
1942 adapter->ahw.linkup = 0;
1943 if (netif_running(netdev)) {
1944 netif_carrier_off(netdev);
1945 netif_stop_queue(netdev);
1947 adapter->link_changed = !adapter->has_link_events;
1948 } else if (!adapter->ahw.linkup && linkup) {
1949 printk(KERN_INFO "%s: %s NIC Link is up\n",
1950 netxen_nic_driver_name, netdev->name);
1951 adapter->ahw.linkup = 1;
1952 if (netif_running(netdev)) {
1953 netif_carrier_on(netdev);
1954 netif_wake_queue(netdev);
1956 adapter->link_changed = !adapter->has_link_events;
1960 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1962 u32 val, port, linkup;
1964 port = adapter->physical_port;
1966 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1967 val = NXRD32(adapter, CRB_XG_STATE_P3);
1968 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1969 linkup = (val == XG_LINK_UP_P3);
1971 val = NXRD32(adapter, CRB_XG_STATE);
1972 val = (val >> port*8) & 0xff;
1973 linkup = (val == XG_LINK_UP);
1976 netxen_advert_link_change(adapter, linkup);
1979 static void netxen_tx_timeout(struct net_device *netdev)
1981 struct netxen_adapter *adapter = netdev_priv(netdev);
1983 if (test_bit(__NX_RESETTING, &adapter->state))
1986 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1987 schedule_work(&adapter->tx_timeout_task);
1990 static void netxen_tx_timeout_task(struct work_struct *work)
1992 struct netxen_adapter *adapter =
1993 container_of(work, struct netxen_adapter, tx_timeout_task);
1995 if (!netif_running(adapter->netdev))
1998 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
2001 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
2004 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
2005 /* try to scrub interrupt */
2006 netxen_napi_disable(adapter);
2008 adapter->netdev->trans_start = jiffies;
2010 netxen_napi_enable(adapter);
2012 netif_wake_queue(adapter->netdev);
2014 clear_bit(__NX_RESETTING, &adapter->state);
2017 clear_bit(__NX_RESETTING, &adapter->state);
2018 if (!netxen_nic_reset_context(adapter)) {
2019 adapter->netdev->trans_start = jiffies;
2023 /* context reset failed, fall through for fw reset */
2027 adapter->need_fw_reset = 1;
2028 clear_bit(__NX_RESETTING, &adapter->state);
2031 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
2033 struct netxen_adapter *adapter = netdev_priv(netdev);
2034 struct net_device_stats *stats = &netdev->stats;
2036 memset(stats, 0, sizeof(*stats));
2038 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2039 stats->tx_packets = adapter->stats.xmitfinished;
2040 stats->rx_bytes = adapter->stats.rxbytes;
2041 stats->tx_bytes = adapter->stats.txbytes;
2042 stats->rx_dropped = adapter->stats.rxdropped;
2043 stats->tx_dropped = adapter->stats.txdropped;
2048 static irqreturn_t netxen_intr(int irq, void *data)
2050 struct nx_host_sds_ring *sds_ring = data;
2051 struct netxen_adapter *adapter = sds_ring->adapter;
2054 status = readl(adapter->isr_int_vec);
2056 if (!(status & adapter->int_vec_bit))
2059 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2060 /* check interrupt state machine, to be sure */
2061 status = readl(adapter->crb_int_state_reg);
2062 if (!ISR_LEGACY_INT_TRIGGERED(status))
2066 unsigned long our_int = 0;
2068 our_int = readl(adapter->crb_int_state_reg);
2070 /* not our interrupt */
2071 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2074 /* claim interrupt */
2075 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2077 /* clear interrupt */
2078 netxen_nic_disable_int(sds_ring);
2081 writel(0xffffffff, adapter->tgt_status_reg);
2082 /* read twice to ensure write is flushed */
2083 readl(adapter->isr_int_vec);
2084 readl(adapter->isr_int_vec);
2086 napi_schedule(&sds_ring->napi);
2091 static irqreturn_t netxen_msi_intr(int irq, void *data)
2093 struct nx_host_sds_ring *sds_ring = data;
2094 struct netxen_adapter *adapter = sds_ring->adapter;
2096 /* clear interrupt */
2097 writel(0xffffffff, adapter->tgt_status_reg);
2099 napi_schedule(&sds_ring->napi);
2103 static irqreturn_t netxen_msix_intr(int irq, void *data)
2105 struct nx_host_sds_ring *sds_ring = data;
2107 napi_schedule(&sds_ring->napi);
2111 static int netxen_nic_poll(struct napi_struct *napi, int budget)
2113 struct nx_host_sds_ring *sds_ring =
2114 container_of(napi, struct nx_host_sds_ring, napi);
2116 struct netxen_adapter *adapter = sds_ring->adapter;
2121 tx_complete = netxen_process_cmd_ring(adapter);
2123 work_done = netxen_process_rcv_ring(sds_ring, budget);
2125 if ((work_done < budget) && tx_complete) {
2126 napi_complete(&sds_ring->napi);
2127 if (test_bit(__NX_DEV_UP, &adapter->state))
2128 netxen_nic_enable_int(sds_ring);
2134 #ifdef CONFIG_NET_POLL_CONTROLLER
2135 static void netxen_nic_poll_controller(struct net_device *netdev)
2137 struct netxen_adapter *adapter = netdev_priv(netdev);
2138 disable_irq(adapter->irq);
2139 netxen_intr(adapter->irq, adapter);
2140 enable_irq(adapter->irq);
2145 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2148 if (netxen_api_lock(adapter))
2151 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2153 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2155 netxen_api_unlock(adapter);
2160 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2163 if (netxen_api_lock(adapter))
2166 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2167 WARN_ON(count == 0);
2169 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2172 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2174 netxen_api_unlock(adapter);
2179 nx_dev_request_aer(struct netxen_adapter *adapter)
2184 if (netxen_api_lock(adapter))
2187 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2189 if (state == NX_DEV_NEED_AER)
2191 else if (state == NX_DEV_READY) {
2192 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2196 netxen_api_unlock(adapter);
2201 nx_dev_request_reset(struct netxen_adapter *adapter)
2206 if (netxen_api_lock(adapter))
2209 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2211 if (state == NX_DEV_NEED_RESET)
2213 else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2214 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2218 netxen_api_unlock(adapter);
2224 netxen_can_start_firmware(struct netxen_adapter *adapter)
2229 if (netxen_api_lock(adapter))
2232 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2234 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2239 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2242 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2244 netxen_api_unlock(adapter);
2250 netxen_schedule_work(struct netxen_adapter *adapter,
2251 work_func_t func, int delay)
2253 INIT_DELAYED_WORK(&adapter->fw_work, func);
2254 schedule_delayed_work(&adapter->fw_work, delay);
2258 netxen_cancel_fw_work(struct netxen_adapter *adapter)
2260 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2263 cancel_delayed_work_sync(&adapter->fw_work);
2267 netxen_attach_work(struct work_struct *work)
2269 struct netxen_adapter *adapter = container_of(work,
2270 struct netxen_adapter, fw_work.work);
2271 struct net_device *netdev = adapter->netdev;
2274 if (netif_running(netdev)) {
2275 err = netxen_nic_attach(adapter);
2279 err = netxen_nic_up(adapter, netdev);
2281 netxen_nic_detach(adapter);
2285 netxen_config_indev_addr(netdev, NETDEV_UP);
2288 netif_device_attach(netdev);
2291 adapter->fw_fail_cnt = 0;
2292 clear_bit(__NX_RESETTING, &adapter->state);
2293 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2297 netxen_fwinit_work(struct work_struct *work)
2299 struct netxen_adapter *adapter = container_of(work,
2300 struct netxen_adapter, fw_work.work);
2303 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2305 switch (dev_state) {
2308 if (!netxen_start_firmware(adapter)) {
2309 netxen_schedule_work(adapter, netxen_attach_work, 0);
2314 case NX_DEV_NEED_RESET:
2315 case NX_DEV_INITALIZING:
2316 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2317 netxen_schedule_work(adapter,
2318 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2324 nx_incr_dev_ref_cnt(adapter);
2328 clear_bit(__NX_RESETTING, &adapter->state);
2332 netxen_detach_work(struct work_struct *work)
2334 struct netxen_adapter *adapter = container_of(work,
2335 struct netxen_adapter, fw_work.work);
2336 struct net_device *netdev = adapter->netdev;
2340 netif_device_detach(netdev);
2342 netxen_nic_down(adapter, netdev);
2345 netxen_nic_detach(adapter);
2348 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2350 if (status & NX_RCODE_FATAL_ERROR)
2353 if (adapter->temp == NX_TEMP_PANIC)
2356 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2358 if (ref_cnt == -EIO)
2361 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2363 adapter->fw_wait_cnt = 0;
2364 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2369 clear_bit(__NX_RESETTING, &adapter->state);
2373 netxen_check_health(struct netxen_adapter *adapter)
2375 u32 state, heartbit;
2376 struct net_device *netdev = adapter->netdev;
2378 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2379 if (state == NX_DEV_NEED_AER)
2382 if (netxen_nic_check_temp(adapter))
2385 if (adapter->need_fw_reset) {
2386 if (nx_dev_request_reset(adapter))
2391 /* NX_DEV_NEED_RESET, this state can be marked in two cases
2392 * 1. Tx timeout 2. Fw hang
2393 * Send request to destroy context in case of tx timeout only
2394 * and doesn't required in case of Fw hang
2396 if (state == NX_DEV_NEED_RESET) {
2397 adapter->need_fw_reset = 1;
2398 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2402 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2405 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2406 if (heartbit != adapter->heartbit) {
2407 adapter->heartbit = heartbit;
2408 adapter->fw_fail_cnt = 0;
2409 if (adapter->need_fw_reset)
2414 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2417 if (nx_dev_request_reset(adapter))
2420 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2422 dev_info(&netdev->dev, "firmware hang detected\n");
2425 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2426 !test_and_set_bit(__NX_RESETTING, &adapter->state))
2427 netxen_schedule_work(adapter, netxen_detach_work, 0);
2432 netxen_fw_poll_work(struct work_struct *work)
2434 struct netxen_adapter *adapter = container_of(work,
2435 struct netxen_adapter, fw_work.work);
2437 if (test_bit(__NX_RESETTING, &adapter->state))
2440 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2441 if (!adapter->has_link_events) {
2443 netxen_nic_handle_phy_intr(adapter);
2445 if (adapter->link_changed)
2446 netxen_nic_set_link_parameters(adapter);
2450 if (netxen_check_health(adapter))
2454 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2458 netxen_store_bridged_mode(struct device *dev,
2459 struct device_attribute *attr, const char *buf, size_t len)
2461 struct net_device *net = to_net_dev(dev);
2462 struct netxen_adapter *adapter = netdev_priv(net);
2466 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2469 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2472 if (strict_strtoul(buf, 2, &new))
2475 if (!netxen_config_bridged_mode(adapter, !!new))
2483 netxen_show_bridged_mode(struct device *dev,
2484 struct device_attribute *attr, char *buf)
2486 struct net_device *net = to_net_dev(dev);
2487 struct netxen_adapter *adapter;
2488 int bridged_mode = 0;
2490 adapter = netdev_priv(net);
2492 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2493 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2495 return sprintf(buf, "%d\n", bridged_mode);
2498 static struct device_attribute dev_attr_bridged_mode = {
2499 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2500 .show = netxen_show_bridged_mode,
2501 .store = netxen_store_bridged_mode,
2505 netxen_store_diag_mode(struct device *dev,
2506 struct device_attribute *attr, const char *buf, size_t len)
2508 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2511 if (strict_strtoul(buf, 2, &new))
2514 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2515 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2521 netxen_show_diag_mode(struct device *dev,
2522 struct device_attribute *attr, char *buf)
2524 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2526 return sprintf(buf, "%d\n",
2527 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2530 static struct device_attribute dev_attr_diag_mode = {
2531 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2532 .show = netxen_show_diag_mode,
2533 .store = netxen_store_diag_mode,
2537 netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2538 loff_t offset, size_t size)
2540 size_t crb_size = 4;
2542 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2545 if (offset < NETXEN_PCI_CRBSPACE) {
2546 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2549 if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2550 NETXEN_PCI_CAMQM_2M_END))
2556 if ((size != crb_size) || (offset & (crb_size-1)))
2563 netxen_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2564 char *buf, loff_t offset, size_t size)
2566 struct device *dev = container_of(kobj, struct device, kobj);
2567 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2572 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2576 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2577 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2578 NETXEN_PCI_CAMQM_2M_END)) {
2579 netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
2580 memcpy(buf, &qmdata, size);
2582 data = NXRD32(adapter, offset);
2583 memcpy(buf, &data, size);
2590 netxen_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2591 char *buf, loff_t offset, size_t size)
2593 struct device *dev = container_of(kobj, struct device, kobj);
2594 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2599 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2603 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2604 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2605 NETXEN_PCI_CAMQM_2M_END)) {
2606 memcpy(&qmdata, buf, size);
2607 netxen_pci_camqm_write_2M(adapter, offset, qmdata);
2609 memcpy(&data, buf, size);
2610 NXWR32(adapter, offset, data);
2617 netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2618 loff_t offset, size_t size)
2620 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2623 if ((size != 8) || (offset & 0x7))
2630 netxen_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2631 char *buf, loff_t offset, size_t size)
2633 struct device *dev = container_of(kobj, struct device, kobj);
2634 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2638 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2642 if (adapter->pci_mem_read(adapter, offset, &data))
2645 memcpy(buf, &data, size);
2650 static ssize_t netxen_sysfs_write_mem(struct kobject *kobj,
2651 struct bin_attribute *attr, char *buf,
2652 loff_t offset, size_t size)
2654 struct device *dev = container_of(kobj, struct device, kobj);
2655 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2659 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2663 memcpy(&data, buf, size);
2665 if (adapter->pci_mem_write(adapter, offset, data))
2672 static struct bin_attribute bin_attr_crb = {
2673 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2675 .read = netxen_sysfs_read_crb,
2676 .write = netxen_sysfs_write_crb,
2679 static struct bin_attribute bin_attr_mem = {
2680 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2682 .read = netxen_sysfs_read_mem,
2683 .write = netxen_sysfs_write_mem,
2688 netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2690 struct net_device *netdev = adapter->netdev;
2691 struct device *dev = &netdev->dev;
2693 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2694 /* bridged_mode control */
2695 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2696 dev_warn(&netdev->dev,
2697 "failed to create bridged_mode sysfs entry\n");
2703 netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2705 struct net_device *netdev = adapter->netdev;
2706 struct device *dev = &netdev->dev;
2708 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2709 device_remove_file(dev, &dev_attr_bridged_mode);
2713 netxen_create_diag_entries(struct netxen_adapter *adapter)
2715 struct pci_dev *pdev = adapter->pdev;
2719 if (device_create_file(dev, &dev_attr_diag_mode))
2720 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2721 if (device_create_bin_file(dev, &bin_attr_crb))
2722 dev_info(dev, "failed to create crb sysfs entry\n");
2723 if (device_create_bin_file(dev, &bin_attr_mem))
2724 dev_info(dev, "failed to create mem sysfs entry\n");
2729 netxen_remove_diag_entries(struct netxen_adapter *adapter)
2731 struct pci_dev *pdev = adapter->pdev;
2732 struct device *dev = &pdev->dev;
2734 device_remove_file(dev, &dev_attr_diag_mode);
2735 device_remove_bin_file(dev, &bin_attr_crb);
2736 device_remove_bin_file(dev, &bin_attr_mem);
2741 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2744 netxen_destip_supported(struct netxen_adapter *adapter)
2746 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2749 if (adapter->ahw.cut_through)
2756 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2758 struct in_device *indev;
2759 struct netxen_adapter *adapter = netdev_priv(dev);
2761 if (!netxen_destip_supported(adapter))
2764 indev = in_dev_get(dev);
2771 netxen_config_ipaddr(adapter,
2772 ifa->ifa_address, NX_IP_UP);
2775 netxen_config_ipaddr(adapter,
2776 ifa->ifa_address, NX_IP_DOWN);
2781 } endfor_ifa(indev);
2787 static int netxen_netdev_event(struct notifier_block *this,
2788 unsigned long event, void *ptr)
2790 struct netxen_adapter *adapter;
2791 struct net_device *dev = (struct net_device *)ptr;
2797 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2798 dev = vlan_dev_real_dev(dev);
2802 if (!is_netxen_netdev(dev))
2805 adapter = netdev_priv(dev);
2810 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2813 netxen_config_indev_addr(dev, event);
2819 netxen_inetaddr_event(struct notifier_block *this,
2820 unsigned long event, void *ptr)
2822 struct netxen_adapter *adapter;
2823 struct net_device *dev;
2825 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2827 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2830 if (dev == NULL || !netif_running(dev))
2833 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2834 dev = vlan_dev_real_dev(dev);
2838 if (!is_netxen_netdev(dev))
2841 adapter = netdev_priv(dev);
2843 if (!adapter || !netxen_destip_supported(adapter))
2846 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2851 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2854 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2864 static struct notifier_block netxen_netdev_cb = {
2865 .notifier_call = netxen_netdev_event,
2868 static struct notifier_block netxen_inetaddr_cb = {
2869 .notifier_call = netxen_inetaddr_event,
2873 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2877 static struct pci_error_handlers netxen_err_handler = {
2878 .error_detected = netxen_io_error_detected,
2879 .slot_reset = netxen_io_slot_reset,
2880 .resume = netxen_io_resume,
2883 static struct pci_driver netxen_driver = {
2884 .name = netxen_nic_driver_name,
2885 .id_table = netxen_pci_tbl,
2886 .probe = netxen_nic_probe,
2887 .remove = __devexit_p(netxen_nic_remove),
2889 .suspend = netxen_nic_suspend,
2890 .resume = netxen_nic_resume,
2892 .shutdown = netxen_nic_shutdown,
2893 .err_handler = &netxen_err_handler
2896 static int __init netxen_init_module(void)
2898 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2901 register_netdevice_notifier(&netxen_netdev_cb);
2902 register_inetaddr_notifier(&netxen_inetaddr_cb);
2904 return pci_register_driver(&netxen_driver);
2907 module_init(netxen_init_module);
2909 static void __exit netxen_exit_module(void)
2911 pci_unregister_driver(&netxen_driver);
2914 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2915 unregister_netdevice_notifier(&netxen_netdev_cb);
2919 module_exit(netxen_exit_module);