drivers/net/: use DEFINE_PCI_DEVICE_TABLE()
[firewire-audio.git] / drivers / net / netxen / netxen_nic_main.c
blob3fdfc9b7c5b2aba751b391a5af4e679d7f5cf253
1 /*
2 * Copyright (C) 2003 - 2009 NetXen, Inc.
3 * Copyright (C) 2009 - QLogic Corporation.
4 * All rights reserved.
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,
19 * MA 02111-1307, USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.
26 #include <linux/vmalloc.h>
27 #include <linux/interrupt.h>
28 #include "netxen_nic_hw.h"
30 #include "netxen_nic.h"
32 #include <linux/dma-mapping.h>
33 #include <linux/if_vlan.h>
34 #include <net/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/inetdevice.h>
37 #include <linux/sysfs.h>
38 #include <linux/aer.h>
40 MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Converged Ethernet Driver");
41 MODULE_LICENSE("GPL");
42 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
43 MODULE_FIRMWARE(NX_P2_MN_ROMIMAGE_NAME);
44 MODULE_FIRMWARE(NX_P3_CT_ROMIMAGE_NAME);
45 MODULE_FIRMWARE(NX_P3_MN_ROMIMAGE_NAME);
46 MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
48 char netxen_nic_driver_name[] = "netxen_nic";
49 static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
50 NETXEN_NIC_LINUX_VERSIONID;
52 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
54 /* Default to restricted 1G auto-neg mode */
55 static int wol_port_mode = 5;
57 static int use_msi = 1;
59 static int use_msi_x = 1;
61 static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
62 module_param(auto_fw_reset, int, 0644);
63 MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
65 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
66 const struct pci_device_id *ent);
67 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
68 static int netxen_nic_open(struct net_device *netdev);
69 static int netxen_nic_close(struct net_device *netdev);
70 static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
71 struct net_device *);
72 static void netxen_tx_timeout(struct net_device *netdev);
73 static void netxen_tx_timeout_task(struct work_struct *work);
74 static void netxen_fw_poll_work(struct work_struct *work);
75 static void netxen_schedule_work(struct netxen_adapter *adapter,
76 work_func_t func, int delay);
77 static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
78 static int netxen_nic_poll(struct napi_struct *napi, int budget);
79 #ifdef CONFIG_NET_POLL_CONTROLLER
80 static void netxen_nic_poll_controller(struct net_device *netdev);
81 #endif
83 static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
84 static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
85 static void netxen_create_diag_entries(struct netxen_adapter *adapter);
86 static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
88 static int nx_dev_request_aer(struct netxen_adapter *adapter);
89 static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
90 static int netxen_can_start_firmware(struct netxen_adapter *adapter);
92 static irqreturn_t netxen_intr(int irq, void *data);
93 static irqreturn_t netxen_msi_intr(int irq, void *data);
94 static irqreturn_t netxen_msix_intr(int irq, void *data);
96 static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
98 /* PCI Device ID Table */
99 #define ENTRY(device) \
100 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
101 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
103 static DEFINE_PCI_DEVICE_TABLE(netxen_pci_tbl) = {
104 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
105 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
106 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
107 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
108 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
109 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
110 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
111 ENTRY(PCI_DEVICE_ID_NX3031),
112 {0,}
115 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
117 static uint32_t crb_cmd_producer[4] = {
118 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
119 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
122 void
123 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
124 struct nx_host_tx_ring *tx_ring)
126 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
128 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
129 netif_stop_queue(adapter->netdev);
130 smp_mb();
134 static uint32_t crb_cmd_consumer[4] = {
135 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
136 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
139 static inline void
140 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
141 struct nx_host_tx_ring *tx_ring)
143 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
146 static uint32_t msi_tgt_status[8] = {
147 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
148 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
149 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
150 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
153 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
155 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
157 struct netxen_adapter *adapter = sds_ring->adapter;
159 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
162 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
164 struct netxen_adapter *adapter = sds_ring->adapter;
166 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
168 if (!NETXEN_IS_MSI_FAMILY(adapter))
169 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
172 static int
173 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
175 int size = sizeof(struct nx_host_sds_ring) * count;
177 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
179 return (recv_ctx->sds_rings == NULL);
182 static void
183 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
185 if (recv_ctx->sds_rings != NULL)
186 kfree(recv_ctx->sds_rings);
188 recv_ctx->sds_rings = NULL;
191 static int
192 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
194 int ring;
195 struct nx_host_sds_ring *sds_ring;
196 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
198 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
199 return -ENOMEM;
201 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
202 sds_ring = &recv_ctx->sds_rings[ring];
203 netif_napi_add(netdev, &sds_ring->napi,
204 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
207 return 0;
210 static void
211 netxen_napi_del(struct netxen_adapter *adapter)
213 int ring;
214 struct nx_host_sds_ring *sds_ring;
215 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
217 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
218 sds_ring = &recv_ctx->sds_rings[ring];
219 netif_napi_del(&sds_ring->napi);
222 netxen_free_sds_rings(&adapter->recv_ctx);
225 static void
226 netxen_napi_enable(struct netxen_adapter *adapter)
228 int ring;
229 struct nx_host_sds_ring *sds_ring;
230 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
232 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
233 sds_ring = &recv_ctx->sds_rings[ring];
234 napi_enable(&sds_ring->napi);
235 netxen_nic_enable_int(sds_ring);
239 static void
240 netxen_napi_disable(struct netxen_adapter *adapter)
242 int ring;
243 struct nx_host_sds_ring *sds_ring;
244 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
246 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
247 sds_ring = &recv_ctx->sds_rings[ring];
248 netxen_nic_disable_int(sds_ring);
249 napi_synchronize(&sds_ring->napi);
250 napi_disable(&sds_ring->napi);
254 static int nx_set_dma_mask(struct netxen_adapter *adapter)
256 struct pci_dev *pdev = adapter->pdev;
257 uint64_t mask, cmask;
259 adapter->pci_using_dac = 0;
261 mask = DMA_BIT_MASK(32);
262 cmask = DMA_BIT_MASK(32);
264 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
265 #ifndef CONFIG_IA64
266 mask = DMA_BIT_MASK(35);
267 #endif
268 } else {
269 mask = DMA_BIT_MASK(39);
270 cmask = mask;
273 if (pci_set_dma_mask(pdev, mask) == 0 &&
274 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
275 adapter->pci_using_dac = 1;
276 return 0;
279 return -EIO;
282 /* Update addressable range if firmware supports it */
283 static int
284 nx_update_dma_mask(struct netxen_adapter *adapter)
286 int change, shift, err;
287 uint64_t mask, old_mask, old_cmask;
288 struct pci_dev *pdev = adapter->pdev;
290 change = 0;
292 shift = NXRD32(adapter, CRB_DMA_SHIFT);
293 if (shift > 32)
294 return 0;
296 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
297 change = 1;
298 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
299 change = 1;
301 if (change) {
302 old_mask = pdev->dma_mask;
303 old_cmask = pdev->dev.coherent_dma_mask;
305 mask = DMA_BIT_MASK(32+shift);
307 err = pci_set_dma_mask(pdev, mask);
308 if (err)
309 goto err_out;
311 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
313 err = pci_set_consistent_dma_mask(pdev, mask);
314 if (err)
315 goto err_out;
317 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
320 return 0;
322 err_out:
323 pci_set_dma_mask(pdev, old_mask);
324 pci_set_consistent_dma_mask(pdev, old_cmask);
325 return err;
328 static int
329 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
331 u32 val, timeout;
333 if (first_boot == 0x55555555) {
334 /* This is the first boot after power up */
335 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
337 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
338 return 0;
340 /* PCI bus master workaround */
341 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
342 if (!(first_boot & 0x4)) {
343 first_boot |= 0x4;
344 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
345 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
348 /* This is the first boot after power up */
349 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
350 if (first_boot != 0x80000f) {
351 /* clear the register for future unloads/loads */
352 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
353 return -EIO;
356 /* Start P2 boot loader */
357 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
358 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
359 timeout = 0;
360 do {
361 msleep(1);
362 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
364 if (++timeout > 5000)
365 return -EIO;
367 } while (val == NETXEN_BDINFO_MAGIC);
369 return 0;
372 static void netxen_set_port_mode(struct netxen_adapter *adapter)
374 u32 val, data;
376 val = adapter->ahw.board_type;
377 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
378 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
379 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
380 data = NETXEN_PORT_MODE_802_3_AP;
381 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
382 } else if (port_mode == NETXEN_PORT_MODE_XG) {
383 data = NETXEN_PORT_MODE_XG;
384 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
385 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
386 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
387 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
388 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
389 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
390 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
391 } else {
392 data = NETXEN_PORT_MODE_AUTO_NEG;
393 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
396 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
397 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
398 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
399 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
400 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
402 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
406 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
408 u32 control;
409 int pos;
411 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
412 if (pos) {
413 pci_read_config_dword(pdev, pos, &control);
414 if (enable)
415 control |= PCI_MSIX_FLAGS_ENABLE;
416 else
417 control = 0;
418 pci_write_config_dword(pdev, pos, control);
422 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
424 int i;
426 for (i = 0; i < count; i++)
427 adapter->msix_entries[i].entry = i;
430 static int
431 netxen_read_mac_addr(struct netxen_adapter *adapter)
433 int i;
434 unsigned char *p;
435 __le64 mac_addr;
436 struct net_device *netdev = adapter->netdev;
437 struct pci_dev *pdev = adapter->pdev;
439 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
440 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
441 return -EIO;
442 } else {
443 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
444 return -EIO;
447 p = (unsigned char *)&mac_addr;
448 for (i = 0; i < 6; i++)
449 netdev->dev_addr[i] = *(p + 5 - i);
451 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
452 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
454 /* set station address */
456 if (!is_valid_ether_addr(netdev->perm_addr))
457 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
459 return 0;
462 int netxen_nic_set_mac(struct net_device *netdev, void *p)
464 struct netxen_adapter *adapter = netdev_priv(netdev);
465 struct sockaddr *addr = p;
467 if (!is_valid_ether_addr(addr->sa_data))
468 return -EINVAL;
470 if (netif_running(netdev)) {
471 netif_device_detach(netdev);
472 netxen_napi_disable(adapter);
475 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
476 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
477 adapter->macaddr_set(adapter, addr->sa_data);
479 if (netif_running(netdev)) {
480 netif_device_attach(netdev);
481 netxen_napi_enable(adapter);
483 return 0;
486 static void netxen_set_multicast_list(struct net_device *dev)
488 struct netxen_adapter *adapter = netdev_priv(dev);
490 adapter->set_multi(dev);
493 static const struct net_device_ops netxen_netdev_ops = {
494 .ndo_open = netxen_nic_open,
495 .ndo_stop = netxen_nic_close,
496 .ndo_start_xmit = netxen_nic_xmit_frame,
497 .ndo_get_stats = netxen_nic_get_stats,
498 .ndo_validate_addr = eth_validate_addr,
499 .ndo_set_multicast_list = netxen_set_multicast_list,
500 .ndo_set_mac_address = netxen_nic_set_mac,
501 .ndo_change_mtu = netxen_nic_change_mtu,
502 .ndo_tx_timeout = netxen_tx_timeout,
503 #ifdef CONFIG_NET_POLL_CONTROLLER
504 .ndo_poll_controller = netxen_nic_poll_controller,
505 #endif
508 static void
509 netxen_setup_intr(struct netxen_adapter *adapter)
511 struct netxen_legacy_intr_set *legacy_intrp;
512 struct pci_dev *pdev = adapter->pdev;
513 int err, num_msix;
515 if (adapter->rss_supported) {
516 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
517 MSIX_ENTRIES_PER_ADAPTER : 2;
518 } else
519 num_msix = 1;
521 adapter->max_sds_rings = 1;
523 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
525 if (adapter->ahw.revision_id >= NX_P3_B0)
526 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
527 else
528 legacy_intrp = &legacy_intr[0];
530 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
531 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
532 legacy_intrp->tgt_status_reg);
533 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
534 legacy_intrp->tgt_mask_reg);
535 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
536 legacy_intrp->pci_int_reg);
537 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
539 if (adapter->ahw.revision_id >= NX_P3_B1)
540 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
541 ISR_INT_STATE_REG);
542 else
543 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
544 CRB_INT_VECTOR);
546 netxen_set_msix_bit(pdev, 0);
548 if (adapter->msix_supported) {
550 netxen_init_msix_entries(adapter, num_msix);
551 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
552 if (err == 0) {
553 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
554 netxen_set_msix_bit(pdev, 1);
556 if (adapter->rss_supported)
557 adapter->max_sds_rings = num_msix;
559 dev_info(&pdev->dev, "using msi-x interrupts\n");
560 return;
563 if (err > 0)
564 pci_disable_msix(pdev);
566 /* fall through for msi */
569 if (use_msi && !pci_enable_msi(pdev)) {
570 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
571 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
572 msi_tgt_status[adapter->ahw.pci_func]);
573 dev_info(&pdev->dev, "using msi interrupts\n");
574 adapter->msix_entries[0].vector = pdev->irq;
575 return;
578 dev_info(&pdev->dev, "using legacy interrupts\n");
579 adapter->msix_entries[0].vector = pdev->irq;
582 static void
583 netxen_teardown_intr(struct netxen_adapter *adapter)
585 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
586 pci_disable_msix(adapter->pdev);
587 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
588 pci_disable_msi(adapter->pdev);
591 static void
592 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
594 if (adapter->ahw.db_base != NULL)
595 iounmap(adapter->ahw.db_base);
596 if (adapter->ahw.pci_base0 != NULL)
597 iounmap(adapter->ahw.pci_base0);
598 if (adapter->ahw.pci_base1 != NULL)
599 iounmap(adapter->ahw.pci_base1);
600 if (adapter->ahw.pci_base2 != NULL)
601 iounmap(adapter->ahw.pci_base2);
604 static int
605 netxen_setup_pci_map(struct netxen_adapter *adapter)
607 void __iomem *mem_ptr0 = NULL;
608 void __iomem *mem_ptr1 = NULL;
609 void __iomem *mem_ptr2 = NULL;
610 void __iomem *db_ptr = NULL;
612 resource_size_t mem_base, db_base;
613 unsigned long mem_len, db_len = 0, pci_len0 = 0;
615 struct pci_dev *pdev = adapter->pdev;
616 int pci_func = adapter->ahw.pci_func;
618 int err = 0;
621 * Set the CRB window to invalid. If any register in window 0 is
622 * accessed it should set the window to 0 and then reset it to 1.
624 adapter->ahw.crb_win = -1;
625 adapter->ahw.ocm_win = -1;
627 /* remap phys address */
628 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
629 mem_len = pci_resource_len(pdev, 0);
631 /* 128 Meg of memory */
632 if (mem_len == NETXEN_PCI_128MB_SIZE) {
633 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
634 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
635 SECOND_PAGE_GROUP_SIZE);
636 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
637 THIRD_PAGE_GROUP_SIZE);
638 pci_len0 = FIRST_PAGE_GROUP_SIZE;
639 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
640 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
641 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
642 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
643 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
645 mem_ptr0 = pci_ioremap_bar(pdev, 0);
646 if (mem_ptr0 == NULL) {
647 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
648 return -EIO;
650 pci_len0 = mem_len;
651 } else {
652 return -EIO;
655 netxen_setup_hwops(adapter);
657 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
659 adapter->ahw.pci_base0 = mem_ptr0;
660 adapter->ahw.pci_len0 = pci_len0;
661 adapter->ahw.pci_base1 = mem_ptr1;
662 adapter->ahw.pci_base2 = mem_ptr2;
664 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
665 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
666 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
668 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
669 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
670 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
673 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
674 goto skip_doorbell;
676 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
677 db_len = pci_resource_len(pdev, 4);
679 if (db_len == 0) {
680 printk(KERN_ERR "%s: doorbell is disabled\n",
681 netxen_nic_driver_name);
682 err = -EIO;
683 goto err_out;
686 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
687 if (!db_ptr) {
688 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
689 netxen_nic_driver_name);
690 err = -EIO;
691 goto err_out;
694 skip_doorbell:
695 adapter->ahw.db_base = db_ptr;
696 adapter->ahw.db_len = db_len;
697 return 0;
699 err_out:
700 netxen_cleanup_pci_map(adapter);
701 return err;
704 static void
705 netxen_check_options(struct netxen_adapter *adapter)
707 u32 fw_major, fw_minor, fw_build;
708 char brd_name[NETXEN_MAX_SHORT_NAME];
709 char serial_num[32];
710 int i, offset, val;
711 int *ptr32;
712 struct pci_dev *pdev = adapter->pdev;
714 adapter->driver_mismatch = 0;
716 ptr32 = (int *)&serial_num;
717 offset = NX_FW_SERIAL_NUM_OFFSET;
718 for (i = 0; i < 8; i++) {
719 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
720 dev_err(&pdev->dev, "error reading board info\n");
721 adapter->driver_mismatch = 1;
722 return;
724 ptr32[i] = cpu_to_le32(val);
725 offset += sizeof(u32);
728 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
729 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
730 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
732 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
734 if (adapter->portnum == 0) {
735 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
737 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
738 module_name(THIS_MODULE),
739 brd_name, serial_num, adapter->ahw.revision_id);
742 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
743 adapter->driver_mismatch = 1;
744 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
745 fw_major, fw_minor, fw_build);
746 return;
749 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
750 i = NXRD32(adapter, NETXEN_SRE_MISC);
751 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
754 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
755 fw_major, fw_minor, fw_build,
756 adapter->ahw.cut_through ? "cut-through" : "legacy");
758 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
759 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
761 adapter->flags &= ~NETXEN_NIC_LRO_ENABLED;
763 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
764 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
765 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
766 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
767 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
768 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
771 adapter->msix_supported = 0;
772 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
773 adapter->msix_supported = !!use_msi_x;
774 adapter->rss_supported = !!use_msi_x;
775 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
776 switch (adapter->ahw.board_type) {
777 case NETXEN_BRDTYPE_P2_SB31_10G:
778 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
779 adapter->msix_supported = !!use_msi_x;
780 adapter->rss_supported = !!use_msi_x;
781 break;
782 default:
783 break;
787 adapter->num_txd = MAX_CMD_DESCRIPTORS;
789 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
790 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
791 adapter->max_rds_rings = 3;
792 } else {
793 adapter->num_lro_rxd = 0;
794 adapter->max_rds_rings = 2;
798 static int
799 netxen_start_firmware(struct netxen_adapter *adapter)
801 int val, err, first_boot;
802 struct pci_dev *pdev = adapter->pdev;
804 /* required for NX2031 dummy dma */
805 err = nx_set_dma_mask(adapter);
806 if (err)
807 return err;
809 if (!netxen_can_start_firmware(adapter))
810 goto wait_init;
812 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
814 err = netxen_check_hw_init(adapter, first_boot);
815 if (err) {
816 dev_err(&pdev->dev, "error in init HW init sequence\n");
817 return err;
820 netxen_request_firmware(adapter);
822 err = netxen_need_fw_reset(adapter);
823 if (err < 0)
824 goto err_out;
825 if (err == 0)
826 goto wait_init;
828 if (first_boot != 0x55555555) {
829 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
830 netxen_pinit_from_rom(adapter);
831 msleep(1);
834 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
835 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
836 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
838 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
839 netxen_set_port_mode(adapter);
841 err = netxen_load_firmware(adapter);
842 if (err)
843 goto err_out;
845 netxen_release_firmware(adapter);
847 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
849 /* Initialize multicast addr pool owners */
850 val = 0x7654;
851 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
852 val |= 0x0f000000;
853 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
857 err = netxen_init_dummy_dma(adapter);
858 if (err)
859 goto err_out;
862 * Tell the hardware our version number.
864 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
865 | ((_NETXEN_NIC_LINUX_MINOR << 8))
866 | (_NETXEN_NIC_LINUX_SUBVERSION);
867 NXWR32(adapter, CRB_DRIVER_VERSION, val);
869 wait_init:
870 /* Handshake with the card before we register the devices. */
871 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
872 if (err) {
873 netxen_free_dummy_dma(adapter);
874 goto err_out;
877 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
879 nx_update_dma_mask(adapter);
881 netxen_check_options(adapter);
883 adapter->need_fw_reset = 0;
885 /* fall through and release firmware */
887 err_out:
888 netxen_release_firmware(adapter);
889 return err;
892 static int
893 netxen_nic_request_irq(struct netxen_adapter *adapter)
895 irq_handler_t handler;
896 struct nx_host_sds_ring *sds_ring;
897 int err, ring;
899 unsigned long flags = IRQF_SAMPLE_RANDOM;
900 struct net_device *netdev = adapter->netdev;
901 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
903 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
904 handler = netxen_msix_intr;
905 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
906 handler = netxen_msi_intr;
907 else {
908 flags |= IRQF_SHARED;
909 handler = netxen_intr;
911 adapter->irq = netdev->irq;
913 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
914 sds_ring = &recv_ctx->sds_rings[ring];
915 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
916 err = request_irq(sds_ring->irq, handler,
917 flags, sds_ring->name, sds_ring);
918 if (err)
919 return err;
922 return 0;
925 static void
926 netxen_nic_free_irq(struct netxen_adapter *adapter)
928 int ring;
929 struct nx_host_sds_ring *sds_ring;
931 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
933 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
934 sds_ring = &recv_ctx->sds_rings[ring];
935 free_irq(sds_ring->irq, sds_ring);
939 static void
940 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
942 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
943 adapter->coal.normal.data.rx_time_us =
944 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
945 adapter->coal.normal.data.rx_packets =
946 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
947 adapter->coal.normal.data.tx_time_us =
948 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
949 adapter->coal.normal.data.tx_packets =
950 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
953 /* with rtnl_lock */
954 static int
955 __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
957 int err;
959 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
960 return -EIO;
962 err = adapter->init_port(adapter, adapter->physical_port);
963 if (err) {
964 printk(KERN_ERR "%s: Failed to initialize port %d\n",
965 netxen_nic_driver_name, adapter->portnum);
966 return err;
968 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
969 adapter->macaddr_set(adapter, adapter->mac_addr);
971 adapter->set_multi(netdev);
972 adapter->set_mtu(adapter, netdev->mtu);
974 adapter->ahw.linkup = 0;
976 if (adapter->max_sds_rings > 1)
977 netxen_config_rss(adapter, 1);
979 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
980 netxen_config_intr_coalesce(adapter);
982 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
983 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
985 netxen_napi_enable(adapter);
987 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
988 netxen_linkevent_request(adapter, 1);
989 else
990 netxen_nic_set_link_parameters(adapter);
992 set_bit(__NX_DEV_UP, &adapter->state);
993 return 0;
996 /* Usage: During resume and firmware recovery module.*/
998 static inline int
999 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1001 int err = 0;
1003 rtnl_lock();
1004 if (netif_running(netdev))
1005 err = __netxen_nic_up(adapter, netdev);
1006 rtnl_unlock();
1008 return err;
1011 /* with rtnl_lock */
1012 static void
1013 __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1015 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1016 return;
1018 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1019 return;
1021 smp_mb();
1022 spin_lock(&adapter->tx_clean_lock);
1023 netif_carrier_off(netdev);
1024 netif_tx_disable(netdev);
1026 if (adapter->stop_port)
1027 adapter->stop_port(adapter);
1029 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1030 netxen_p3_free_mac_list(adapter);
1032 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1034 netxen_napi_disable(adapter);
1036 netxen_release_tx_buffers(adapter);
1037 spin_unlock(&adapter->tx_clean_lock);
1040 /* Usage: During suspend and firmware recovery module */
1042 static inline void
1043 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1045 rtnl_lock();
1046 if (netif_running(netdev))
1047 __netxen_nic_down(adapter, netdev);
1048 rtnl_unlock();
1052 static int
1053 netxen_nic_attach(struct netxen_adapter *adapter)
1055 struct net_device *netdev = adapter->netdev;
1056 struct pci_dev *pdev = adapter->pdev;
1057 int err, ring;
1058 struct nx_host_rds_ring *rds_ring;
1059 struct nx_host_tx_ring *tx_ring;
1061 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1062 return 0;
1064 err = netxen_init_firmware(adapter);
1065 if (err)
1066 return err;
1068 err = netxen_napi_add(adapter, netdev);
1069 if (err)
1070 return err;
1072 err = netxen_alloc_sw_resources(adapter);
1073 if (err) {
1074 printk(KERN_ERR "%s: Error in setting sw resources\n",
1075 netdev->name);
1076 return err;
1079 err = netxen_alloc_hw_resources(adapter);
1080 if (err) {
1081 printk(KERN_ERR "%s: Error in setting hw resources\n",
1082 netdev->name);
1083 goto err_out_free_sw;
1086 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1087 tx_ring = adapter->tx_ring;
1088 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1089 crb_cmd_producer[adapter->portnum]);
1090 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1091 crb_cmd_consumer[adapter->portnum]);
1093 tx_ring->producer = 0;
1094 tx_ring->sw_consumer = 0;
1096 netxen_nic_update_cmd_producer(adapter, tx_ring);
1097 netxen_nic_update_cmd_consumer(adapter, tx_ring);
1100 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1101 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1102 netxen_post_rx_buffers(adapter, ring, rds_ring);
1105 err = netxen_nic_request_irq(adapter);
1106 if (err) {
1107 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1108 netdev->name);
1109 goto err_out_free_rxbuf;
1112 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1113 netxen_nic_init_coalesce_defaults(adapter);
1115 netxen_create_sysfs_entries(adapter);
1117 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1118 return 0;
1120 err_out_free_rxbuf:
1121 netxen_release_rx_buffers(adapter);
1122 netxen_free_hw_resources(adapter);
1123 err_out_free_sw:
1124 netxen_free_sw_resources(adapter);
1125 return err;
1128 static void
1129 netxen_nic_detach(struct netxen_adapter *adapter)
1131 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1132 return;
1134 netxen_remove_sysfs_entries(adapter);
1136 netxen_free_hw_resources(adapter);
1137 netxen_release_rx_buffers(adapter);
1138 netxen_nic_free_irq(adapter);
1139 netxen_napi_del(adapter);
1140 netxen_free_sw_resources(adapter);
1142 adapter->is_up = 0;
1146 netxen_nic_reset_context(struct netxen_adapter *adapter)
1148 int err = 0;
1149 struct net_device *netdev = adapter->netdev;
1151 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1152 return -EBUSY;
1154 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1156 netif_device_detach(netdev);
1158 if (netif_running(netdev))
1159 __netxen_nic_down(adapter, netdev);
1161 netxen_nic_detach(adapter);
1163 if (netif_running(netdev)) {
1164 err = netxen_nic_attach(adapter);
1165 if (!err)
1166 err = __netxen_nic_up(adapter, netdev);
1168 if (err)
1169 goto done;
1172 netif_device_attach(netdev);
1175 done:
1176 clear_bit(__NX_RESETTING, &adapter->state);
1177 return err;
1180 static int
1181 netxen_setup_netdev(struct netxen_adapter *adapter,
1182 struct net_device *netdev)
1184 int err = 0;
1185 struct pci_dev *pdev = adapter->pdev;
1187 adapter->rx_csum = 1;
1188 adapter->mc_enabled = 0;
1189 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1190 adapter->max_mc_count = 38;
1191 else
1192 adapter->max_mc_count = 16;
1194 netdev->netdev_ops = &netxen_netdev_ops;
1195 netdev->watchdog_timeo = 2*HZ;
1197 netxen_nic_change_mtu(netdev, netdev->mtu);
1199 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1201 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1202 netdev->features |= (NETIF_F_GRO);
1203 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1205 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1206 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1207 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1210 if (adapter->pci_using_dac) {
1211 netdev->features |= NETIF_F_HIGHDMA;
1212 netdev->vlan_features |= NETIF_F_HIGHDMA;
1215 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1216 netdev->features |= (NETIF_F_HW_VLAN_TX);
1218 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1219 netdev->features |= NETIF_F_LRO;
1221 netdev->irq = adapter->msix_entries[0].vector;
1223 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1225 if (netxen_read_mac_addr(adapter))
1226 dev_warn(&pdev->dev, "failed to read mac addr\n");
1228 netif_carrier_off(netdev);
1229 netif_stop_queue(netdev);
1231 err = register_netdev(netdev);
1232 if (err) {
1233 dev_err(&pdev->dev, "failed to register net device\n");
1234 return err;
1237 return 0;
1240 static int __devinit
1241 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1243 struct net_device *netdev = NULL;
1244 struct netxen_adapter *adapter = NULL;
1245 int i = 0, err;
1246 int pci_func_id = PCI_FUNC(pdev->devfn);
1247 uint8_t revision_id;
1249 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
1250 pr_warning("%s: chip revisions between 0x%x-0x%x"
1251 "will not be enabled.\n",
1252 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1253 return -ENODEV;
1256 if ((err = pci_enable_device(pdev)))
1257 return err;
1259 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1260 err = -ENODEV;
1261 goto err_out_disable_pdev;
1264 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1265 goto err_out_disable_pdev;
1267 if (NX_IS_REVISION_P3(pdev->revision))
1268 pci_enable_pcie_error_reporting(pdev);
1270 pci_set_master(pdev);
1272 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1273 if(!netdev) {
1274 dev_err(&pdev->dev, "failed to allocate net_device\n");
1275 err = -ENOMEM;
1276 goto err_out_free_res;
1279 SET_NETDEV_DEV(netdev, &pdev->dev);
1281 adapter = netdev_priv(netdev);
1282 adapter->netdev = netdev;
1283 adapter->pdev = pdev;
1284 adapter->ahw.pci_func = pci_func_id;
1286 revision_id = pdev->revision;
1287 adapter->ahw.revision_id = revision_id;
1289 rwlock_init(&adapter->ahw.crb_lock);
1290 spin_lock_init(&adapter->ahw.mem_lock);
1292 spin_lock_init(&adapter->tx_clean_lock);
1293 INIT_LIST_HEAD(&adapter->mac_list);
1295 err = netxen_setup_pci_map(adapter);
1296 if (err)
1297 goto err_out_free_netdev;
1299 /* This will be reset for mezz cards */
1300 adapter->portnum = pci_func_id;
1302 err = netxen_nic_get_board_info(adapter);
1303 if (err) {
1304 dev_err(&pdev->dev, "Error getting board config info.\n");
1305 goto err_out_iounmap;
1308 /* Mezz cards have PCI function 0,2,3 enabled */
1309 switch (adapter->ahw.board_type) {
1310 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1311 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1312 if (pci_func_id >= 2)
1313 adapter->portnum = pci_func_id - 2;
1314 break;
1315 default:
1316 break;
1319 err = netxen_start_firmware(adapter);
1320 if (err)
1321 goto err_out_decr_ref;
1324 * See if the firmware gave us a virtual-physical port mapping.
1326 adapter->physical_port = adapter->portnum;
1327 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1328 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1329 if (i != 0x55555555)
1330 adapter->physical_port = i;
1333 netxen_nic_clear_stats(adapter);
1335 netxen_setup_intr(adapter);
1337 err = netxen_setup_netdev(adapter, netdev);
1338 if (err)
1339 goto err_out_disable_msi;
1341 pci_set_drvdata(pdev, adapter);
1343 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1345 switch (adapter->ahw.port_type) {
1346 case NETXEN_NIC_GBE:
1347 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1348 adapter->netdev->name);
1349 break;
1350 case NETXEN_NIC_XGBE:
1351 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1352 adapter->netdev->name);
1353 break;
1356 netxen_create_diag_entries(adapter);
1358 return 0;
1360 err_out_disable_msi:
1361 netxen_teardown_intr(adapter);
1363 netxen_free_dummy_dma(adapter);
1365 err_out_decr_ref:
1366 nx_decr_dev_ref_cnt(adapter);
1368 err_out_iounmap:
1369 netxen_cleanup_pci_map(adapter);
1371 err_out_free_netdev:
1372 free_netdev(netdev);
1374 err_out_free_res:
1375 pci_release_regions(pdev);
1377 err_out_disable_pdev:
1378 pci_set_drvdata(pdev, NULL);
1379 pci_disable_device(pdev);
1380 return err;
1383 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1385 struct netxen_adapter *adapter;
1386 struct net_device *netdev;
1388 adapter = pci_get_drvdata(pdev);
1389 if (adapter == NULL)
1390 return;
1392 netdev = adapter->netdev;
1394 netxen_cancel_fw_work(adapter);
1396 unregister_netdev(netdev);
1398 cancel_work_sync(&adapter->tx_timeout_task);
1400 netxen_nic_detach(adapter);
1402 nx_decr_dev_ref_cnt(adapter);
1404 if (adapter->portnum == 0)
1405 netxen_free_dummy_dma(adapter);
1407 clear_bit(__NX_RESETTING, &adapter->state);
1409 netxen_teardown_intr(adapter);
1411 netxen_remove_diag_entries(adapter);
1413 netxen_cleanup_pci_map(adapter);
1415 netxen_release_firmware(adapter);
1417 if (NX_IS_REVISION_P3(pdev->revision))
1418 pci_disable_pcie_error_reporting(pdev);
1420 pci_release_regions(pdev);
1421 pci_disable_device(pdev);
1422 pci_set_drvdata(pdev, NULL);
1424 free_netdev(netdev);
1427 static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1429 struct net_device *netdev = adapter->netdev;
1431 netif_device_detach(netdev);
1433 netxen_cancel_fw_work(adapter);
1435 if (netif_running(netdev))
1436 netxen_nic_down(adapter, netdev);
1438 cancel_work_sync(&adapter->tx_timeout_task);
1440 netxen_nic_detach(adapter);
1442 if (adapter->portnum == 0)
1443 netxen_free_dummy_dma(adapter);
1445 nx_decr_dev_ref_cnt(adapter);
1447 clear_bit(__NX_RESETTING, &adapter->state);
1450 static int netxen_nic_attach_func(struct pci_dev *pdev)
1452 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1453 struct net_device *netdev = adapter->netdev;
1454 int err;
1456 err = pci_enable_device(pdev);
1457 if (err)
1458 return err;
1460 pci_set_power_state(pdev, PCI_D0);
1461 pci_set_master(pdev);
1462 pci_restore_state(pdev);
1464 adapter->ahw.crb_win = -1;
1465 adapter->ahw.ocm_win = -1;
1467 err = netxen_start_firmware(adapter);
1468 if (err) {
1469 dev_err(&pdev->dev, "failed to start firmware\n");
1470 return err;
1473 if (netif_running(netdev)) {
1474 err = netxen_nic_attach(adapter);
1475 if (err)
1476 goto err_out;
1478 err = netxen_nic_up(adapter, netdev);
1479 if (err)
1480 goto err_out_detach;
1482 netxen_config_indev_addr(netdev, NETDEV_UP);
1485 netif_device_attach(netdev);
1486 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1487 return 0;
1489 err_out_detach:
1490 netxen_nic_detach(adapter);
1491 err_out:
1492 nx_decr_dev_ref_cnt(adapter);
1493 return err;
1496 static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1497 pci_channel_state_t state)
1499 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1501 if (state == pci_channel_io_perm_failure)
1502 return PCI_ERS_RESULT_DISCONNECT;
1504 if (nx_dev_request_aer(adapter))
1505 return PCI_ERS_RESULT_RECOVERED;
1507 netxen_nic_detach_func(adapter);
1509 pci_disable_device(pdev);
1511 return PCI_ERS_RESULT_NEED_RESET;
1514 static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1516 int err = 0;
1518 err = netxen_nic_attach_func(pdev);
1520 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1523 static void netxen_io_resume(struct pci_dev *pdev)
1525 pci_cleanup_aer_uncorrect_error_status(pdev);
1528 static void netxen_nic_shutdown(struct pci_dev *pdev)
1530 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1532 netxen_nic_detach_func(adapter);
1534 if (pci_save_state(pdev))
1535 return;
1537 if (netxen_nic_wol_supported(adapter)) {
1538 pci_enable_wake(pdev, PCI_D3cold, 1);
1539 pci_enable_wake(pdev, PCI_D3hot, 1);
1542 pci_disable_device(pdev);
1545 #ifdef CONFIG_PM
1546 static int
1547 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1549 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1550 int retval;
1552 netxen_nic_detach_func(adapter);
1554 retval = pci_save_state(pdev);
1555 if (retval)
1556 return retval;
1558 if (netxen_nic_wol_supported(adapter)) {
1559 pci_enable_wake(pdev, PCI_D3cold, 1);
1560 pci_enable_wake(pdev, PCI_D3hot, 1);
1563 pci_disable_device(pdev);
1564 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1566 return 0;
1569 static int
1570 netxen_nic_resume(struct pci_dev *pdev)
1572 return netxen_nic_attach_func(pdev);
1574 #endif
1576 static int netxen_nic_open(struct net_device *netdev)
1578 struct netxen_adapter *adapter = netdev_priv(netdev);
1579 int err = 0;
1581 if (adapter->driver_mismatch)
1582 return -EIO;
1584 err = netxen_nic_attach(adapter);
1585 if (err)
1586 return err;
1588 err = __netxen_nic_up(adapter, netdev);
1589 if (err)
1590 goto err_out;
1592 netif_start_queue(netdev);
1594 return 0;
1596 err_out:
1597 netxen_nic_detach(adapter);
1598 return err;
1602 * netxen_nic_close - Disables a network interface entry point
1604 static int netxen_nic_close(struct net_device *netdev)
1606 struct netxen_adapter *adapter = netdev_priv(netdev);
1608 __netxen_nic_down(adapter, netdev);
1609 return 0;
1612 static void
1613 netxen_tso_check(struct net_device *netdev,
1614 struct nx_host_tx_ring *tx_ring,
1615 struct cmd_desc_type0 *first_desc,
1616 struct sk_buff *skb)
1618 u8 opcode = TX_ETHER_PKT;
1619 __be16 protocol = skb->protocol;
1620 u16 flags = 0, vid = 0;
1621 u32 producer;
1622 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1623 struct cmd_desc_type0 *hwdesc;
1624 struct vlan_ethhdr *vh;
1626 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1628 vh = (struct vlan_ethhdr *)skb->data;
1629 protocol = vh->h_vlan_encapsulated_proto;
1630 flags = FLAGS_VLAN_TAGGED;
1632 } else if (vlan_tx_tag_present(skb)) {
1634 flags = FLAGS_VLAN_OOB;
1635 vid = vlan_tx_tag_get(skb);
1636 netxen_set_tx_vlan_tci(first_desc, vid);
1637 vlan_oob = 1;
1640 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1641 skb_shinfo(skb)->gso_size > 0) {
1643 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1645 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1646 first_desc->total_hdr_length = hdr_len;
1647 if (vlan_oob) {
1648 first_desc->total_hdr_length += VLAN_HLEN;
1649 first_desc->tcp_hdr_offset = VLAN_HLEN;
1650 first_desc->ip_hdr_offset = VLAN_HLEN;
1651 /* Only in case of TSO on vlan device */
1652 flags |= FLAGS_VLAN_TAGGED;
1655 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1656 TX_TCP_LSO6 : TX_TCP_LSO;
1657 tso = 1;
1659 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1660 u8 l4proto;
1662 if (protocol == cpu_to_be16(ETH_P_IP)) {
1663 l4proto = ip_hdr(skb)->protocol;
1665 if (l4proto == IPPROTO_TCP)
1666 opcode = TX_TCP_PKT;
1667 else if(l4proto == IPPROTO_UDP)
1668 opcode = TX_UDP_PKT;
1669 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1670 l4proto = ipv6_hdr(skb)->nexthdr;
1672 if (l4proto == IPPROTO_TCP)
1673 opcode = TX_TCPV6_PKT;
1674 else if(l4proto == IPPROTO_UDP)
1675 opcode = TX_UDPV6_PKT;
1679 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1680 first_desc->ip_hdr_offset += skb_network_offset(skb);
1681 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1683 if (!tso)
1684 return;
1686 /* For LSO, we need to copy the MAC/IP/TCP headers into
1687 * the descriptor ring
1689 producer = tx_ring->producer;
1690 copied = 0;
1691 offset = 2;
1693 if (vlan_oob) {
1694 /* Create a TSO vlan header template for firmware */
1696 hwdesc = &tx_ring->desc_head[producer];
1697 tx_ring->cmd_buf_arr[producer].skb = NULL;
1699 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1700 hdr_len + VLAN_HLEN);
1702 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1703 skb_copy_from_linear_data(skb, vh, 12);
1704 vh->h_vlan_proto = htons(ETH_P_8021Q);
1705 vh->h_vlan_TCI = htons(vid);
1706 skb_copy_from_linear_data_offset(skb, 12,
1707 (char *)vh + 16, copy_len - 16);
1709 copied = copy_len - VLAN_HLEN;
1710 offset = 0;
1712 producer = get_next_index(producer, tx_ring->num_desc);
1715 while (copied < hdr_len) {
1717 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1718 (hdr_len - copied));
1720 hwdesc = &tx_ring->desc_head[producer];
1721 tx_ring->cmd_buf_arr[producer].skb = NULL;
1723 skb_copy_from_linear_data_offset(skb, copied,
1724 (char *)hwdesc + offset, copy_len);
1726 copied += copy_len;
1727 offset = 0;
1729 producer = get_next_index(producer, tx_ring->num_desc);
1732 tx_ring->producer = producer;
1733 barrier();
1736 static int
1737 netxen_map_tx_skb(struct pci_dev *pdev,
1738 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1740 struct netxen_skb_frag *nf;
1741 struct skb_frag_struct *frag;
1742 int i, nr_frags;
1743 dma_addr_t map;
1745 nr_frags = skb_shinfo(skb)->nr_frags;
1746 nf = &pbuf->frag_array[0];
1748 map = pci_map_single(pdev, skb->data,
1749 skb_headlen(skb), PCI_DMA_TODEVICE);
1750 if (pci_dma_mapping_error(pdev, map))
1751 goto out_err;
1753 nf->dma = map;
1754 nf->length = skb_headlen(skb);
1756 for (i = 0; i < nr_frags; i++) {
1757 frag = &skb_shinfo(skb)->frags[i];
1758 nf = &pbuf->frag_array[i+1];
1760 map = pci_map_page(pdev, frag->page, frag->page_offset,
1761 frag->size, PCI_DMA_TODEVICE);
1762 if (pci_dma_mapping_error(pdev, map))
1763 goto unwind;
1765 nf->dma = map;
1766 nf->length = frag->size;
1769 return 0;
1771 unwind:
1772 while (--i >= 0) {
1773 nf = &pbuf->frag_array[i+1];
1774 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1777 nf = &pbuf->frag_array[0];
1778 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1780 out_err:
1781 return -ENOMEM;
1784 static inline void
1785 netxen_clear_cmddesc(u64 *desc)
1787 desc[0] = 0ULL;
1788 desc[2] = 0ULL;
1791 static netdev_tx_t
1792 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1794 struct netxen_adapter *adapter = netdev_priv(netdev);
1795 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1796 struct netxen_cmd_buffer *pbuf;
1797 struct netxen_skb_frag *buffrag;
1798 struct cmd_desc_type0 *hwdesc, *first_desc;
1799 struct pci_dev *pdev;
1800 int i, k;
1802 u32 producer;
1803 int frag_count, no_of_desc;
1804 u32 num_txd = tx_ring->num_desc;
1806 frag_count = skb_shinfo(skb)->nr_frags + 1;
1808 /* 4 fragments per cmd des */
1809 no_of_desc = (frag_count + 3) >> 2;
1811 if (unlikely(no_of_desc + 2 > netxen_tx_avail(tx_ring))) {
1812 netif_stop_queue(netdev);
1813 return NETDEV_TX_BUSY;
1816 producer = tx_ring->producer;
1817 pbuf = &tx_ring->cmd_buf_arr[producer];
1819 pdev = adapter->pdev;
1821 if (netxen_map_tx_skb(pdev, skb, pbuf))
1822 goto drop_packet;
1824 pbuf->skb = skb;
1825 pbuf->frag_count = frag_count;
1827 first_desc = hwdesc = &tx_ring->desc_head[producer];
1828 netxen_clear_cmddesc((u64 *)hwdesc);
1830 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1831 netxen_set_tx_port(first_desc, adapter->portnum);
1833 for (i = 0; i < frag_count; i++) {
1835 k = i % 4;
1837 if ((k == 0) && (i > 0)) {
1838 /* move to next desc.*/
1839 producer = get_next_index(producer, num_txd);
1840 hwdesc = &tx_ring->desc_head[producer];
1841 netxen_clear_cmddesc((u64 *)hwdesc);
1842 tx_ring->cmd_buf_arr[producer].skb = NULL;
1845 buffrag = &pbuf->frag_array[i];
1847 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1848 switch (k) {
1849 case 0:
1850 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1851 break;
1852 case 1:
1853 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1854 break;
1855 case 2:
1856 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1857 break;
1858 case 3:
1859 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1860 break;
1864 tx_ring->producer = get_next_index(producer, num_txd);
1866 netxen_tso_check(netdev, tx_ring, first_desc, skb);
1868 netxen_nic_update_cmd_producer(adapter, tx_ring);
1870 adapter->stats.txbytes += skb->len;
1871 adapter->stats.xmitcalled++;
1873 return NETDEV_TX_OK;
1875 drop_packet:
1876 adapter->stats.txdropped++;
1877 dev_kfree_skb_any(skb);
1878 return NETDEV_TX_OK;
1881 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1883 struct net_device *netdev = adapter->netdev;
1884 uint32_t temp, temp_state, temp_val;
1885 int rv = 0;
1887 temp = NXRD32(adapter, CRB_TEMP_STATE);
1889 temp_state = nx_get_temp_state(temp);
1890 temp_val = nx_get_temp_val(temp);
1892 if (temp_state == NX_TEMP_PANIC) {
1893 printk(KERN_ALERT
1894 "%s: Device temperature %d degrees C exceeds"
1895 " maximum allowed. Hardware has been shut down.\n",
1896 netdev->name, temp_val);
1897 rv = 1;
1898 } else if (temp_state == NX_TEMP_WARN) {
1899 if (adapter->temp == NX_TEMP_NORMAL) {
1900 printk(KERN_ALERT
1901 "%s: Device temperature %d degrees C "
1902 "exceeds operating range."
1903 " Immediate action needed.\n",
1904 netdev->name, temp_val);
1906 } else {
1907 if (adapter->temp == NX_TEMP_WARN) {
1908 printk(KERN_INFO
1909 "%s: Device temperature is now %d degrees C"
1910 " in normal range.\n", netdev->name,
1911 temp_val);
1914 adapter->temp = temp_state;
1915 return rv;
1918 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
1920 struct net_device *netdev = adapter->netdev;
1922 if (adapter->ahw.linkup && !linkup) {
1923 printk(KERN_INFO "%s: %s NIC Link is down\n",
1924 netxen_nic_driver_name, netdev->name);
1925 adapter->ahw.linkup = 0;
1926 if (netif_running(netdev)) {
1927 netif_carrier_off(netdev);
1928 netif_stop_queue(netdev);
1930 adapter->link_changed = !adapter->has_link_events;
1931 } else if (!adapter->ahw.linkup && linkup) {
1932 printk(KERN_INFO "%s: %s NIC Link is up\n",
1933 netxen_nic_driver_name, netdev->name);
1934 adapter->ahw.linkup = 1;
1935 if (netif_running(netdev)) {
1936 netif_carrier_on(netdev);
1937 netif_wake_queue(netdev);
1939 adapter->link_changed = !adapter->has_link_events;
1943 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1945 u32 val, port, linkup;
1947 port = adapter->physical_port;
1949 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1950 val = NXRD32(adapter, CRB_XG_STATE_P3);
1951 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1952 linkup = (val == XG_LINK_UP_P3);
1953 } else {
1954 val = NXRD32(adapter, CRB_XG_STATE);
1955 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
1956 linkup = (val >> port) & 1;
1957 else {
1958 val = (val >> port*8) & 0xff;
1959 linkup = (val == XG_LINK_UP);
1963 netxen_advert_link_change(adapter, linkup);
1966 static void netxen_tx_timeout(struct net_device *netdev)
1968 struct netxen_adapter *adapter = netdev_priv(netdev);
1970 if (test_bit(__NX_RESETTING, &adapter->state))
1971 return;
1973 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1974 schedule_work(&adapter->tx_timeout_task);
1977 static void netxen_tx_timeout_task(struct work_struct *work)
1979 struct netxen_adapter *adapter =
1980 container_of(work, struct netxen_adapter, tx_timeout_task);
1982 if (!netif_running(adapter->netdev))
1983 return;
1985 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1986 return;
1988 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
1989 goto request_reset;
1991 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1992 /* try to scrub interrupt */
1993 netxen_napi_disable(adapter);
1995 adapter->netdev->trans_start = jiffies;
1997 netxen_napi_enable(adapter);
1999 netif_wake_queue(adapter->netdev);
2001 clear_bit(__NX_RESETTING, &adapter->state);
2003 } else {
2004 clear_bit(__NX_RESETTING, &adapter->state);
2005 if (!netxen_nic_reset_context(adapter)) {
2006 adapter->netdev->trans_start = jiffies;
2007 return;
2010 /* context reset failed, fall through for fw reset */
2013 request_reset:
2014 adapter->need_fw_reset = 1;
2015 clear_bit(__NX_RESETTING, &adapter->state);
2018 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
2020 struct netxen_adapter *adapter = netdev_priv(netdev);
2021 struct net_device_stats *stats = &netdev->stats;
2023 memset(stats, 0, sizeof(*stats));
2025 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2026 stats->tx_packets = adapter->stats.xmitfinished;
2027 stats->rx_bytes = adapter->stats.rxbytes;
2028 stats->tx_bytes = adapter->stats.txbytes;
2029 stats->rx_dropped = adapter->stats.rxdropped;
2030 stats->tx_dropped = adapter->stats.txdropped;
2032 return stats;
2035 static irqreturn_t netxen_intr(int irq, void *data)
2037 struct nx_host_sds_ring *sds_ring = data;
2038 struct netxen_adapter *adapter = sds_ring->adapter;
2039 u32 status = 0;
2041 status = readl(adapter->isr_int_vec);
2043 if (!(status & adapter->int_vec_bit))
2044 return IRQ_NONE;
2046 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2047 /* check interrupt state machine, to be sure */
2048 status = readl(adapter->crb_int_state_reg);
2049 if (!ISR_LEGACY_INT_TRIGGERED(status))
2050 return IRQ_NONE;
2052 } else {
2053 unsigned long our_int = 0;
2055 our_int = readl(adapter->crb_int_state_reg);
2057 /* not our interrupt */
2058 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2059 return IRQ_NONE;
2061 /* claim interrupt */
2062 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2064 /* clear interrupt */
2065 netxen_nic_disable_int(sds_ring);
2068 writel(0xffffffff, adapter->tgt_status_reg);
2069 /* read twice to ensure write is flushed */
2070 readl(adapter->isr_int_vec);
2071 readl(adapter->isr_int_vec);
2073 napi_schedule(&sds_ring->napi);
2075 return IRQ_HANDLED;
2078 static irqreturn_t netxen_msi_intr(int irq, void *data)
2080 struct nx_host_sds_ring *sds_ring = data;
2081 struct netxen_adapter *adapter = sds_ring->adapter;
2083 /* clear interrupt */
2084 writel(0xffffffff, adapter->tgt_status_reg);
2086 napi_schedule(&sds_ring->napi);
2087 return IRQ_HANDLED;
2090 static irqreturn_t netxen_msix_intr(int irq, void *data)
2092 struct nx_host_sds_ring *sds_ring = data;
2094 napi_schedule(&sds_ring->napi);
2095 return IRQ_HANDLED;
2098 static int netxen_nic_poll(struct napi_struct *napi, int budget)
2100 struct nx_host_sds_ring *sds_ring =
2101 container_of(napi, struct nx_host_sds_ring, napi);
2103 struct netxen_adapter *adapter = sds_ring->adapter;
2105 int tx_complete;
2106 int work_done;
2108 tx_complete = netxen_process_cmd_ring(adapter);
2110 work_done = netxen_process_rcv_ring(sds_ring, budget);
2112 if ((work_done < budget) && tx_complete) {
2113 napi_complete(&sds_ring->napi);
2114 if (test_bit(__NX_DEV_UP, &adapter->state))
2115 netxen_nic_enable_int(sds_ring);
2118 return work_done;
2121 #ifdef CONFIG_NET_POLL_CONTROLLER
2122 static void netxen_nic_poll_controller(struct net_device *netdev)
2124 struct netxen_adapter *adapter = netdev_priv(netdev);
2125 disable_irq(adapter->irq);
2126 netxen_intr(adapter->irq, adapter);
2127 enable_irq(adapter->irq);
2129 #endif
2131 static int
2132 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2134 int count;
2135 if (netxen_api_lock(adapter))
2136 return -EIO;
2138 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2140 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2142 netxen_api_unlock(adapter);
2143 return count;
2146 static int
2147 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2149 int count;
2150 if (netxen_api_lock(adapter))
2151 return -EIO;
2153 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2154 WARN_ON(count == 0);
2156 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2158 if (count == 0)
2159 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2161 netxen_api_unlock(adapter);
2162 return count;
2165 static int
2166 nx_dev_request_aer(struct netxen_adapter *adapter)
2168 u32 state;
2169 int ret = -EINVAL;
2171 if (netxen_api_lock(adapter))
2172 return ret;
2174 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2176 if (state == NX_DEV_NEED_AER)
2177 ret = 0;
2178 else if (state == NX_DEV_READY) {
2179 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2180 ret = 0;
2183 netxen_api_unlock(adapter);
2184 return ret;
2187 static int
2188 nx_dev_request_reset(struct netxen_adapter *adapter)
2190 u32 state;
2191 int ret = -EINVAL;
2193 if (netxen_api_lock(adapter))
2194 return ret;
2196 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2198 if (state == NX_DEV_NEED_RESET)
2199 ret = 0;
2200 else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2201 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2202 ret = 0;
2205 netxen_api_unlock(adapter);
2207 return ret;
2210 static int
2211 netxen_can_start_firmware(struct netxen_adapter *adapter)
2213 int count;
2214 int can_start = 0;
2216 if (netxen_api_lock(adapter))
2217 return 0;
2219 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2221 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2222 count = 0;
2224 if (count == 0) {
2225 can_start = 1;
2226 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2229 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2231 netxen_api_unlock(adapter);
2233 return can_start;
2236 static void
2237 netxen_schedule_work(struct netxen_adapter *adapter,
2238 work_func_t func, int delay)
2240 INIT_DELAYED_WORK(&adapter->fw_work, func);
2241 schedule_delayed_work(&adapter->fw_work, delay);
2244 static void
2245 netxen_cancel_fw_work(struct netxen_adapter *adapter)
2247 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2248 msleep(10);
2250 cancel_delayed_work_sync(&adapter->fw_work);
2253 static void
2254 netxen_attach_work(struct work_struct *work)
2256 struct netxen_adapter *adapter = container_of(work,
2257 struct netxen_adapter, fw_work.work);
2258 struct net_device *netdev = adapter->netdev;
2259 int err = 0;
2261 if (netif_running(netdev)) {
2262 err = netxen_nic_attach(adapter);
2263 if (err)
2264 goto done;
2266 err = netxen_nic_up(adapter, netdev);
2267 if (err) {
2268 netxen_nic_detach(adapter);
2269 goto done;
2272 netxen_config_indev_addr(netdev, NETDEV_UP);
2275 netif_device_attach(netdev);
2277 done:
2278 adapter->fw_fail_cnt = 0;
2279 clear_bit(__NX_RESETTING, &adapter->state);
2280 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2283 static void
2284 netxen_fwinit_work(struct work_struct *work)
2286 struct netxen_adapter *adapter = container_of(work,
2287 struct netxen_adapter, fw_work.work);
2288 int dev_state;
2290 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2292 switch (dev_state) {
2293 case NX_DEV_COLD:
2294 case NX_DEV_READY:
2295 if (!netxen_start_firmware(adapter)) {
2296 netxen_schedule_work(adapter, netxen_attach_work, 0);
2297 return;
2299 break;
2301 case NX_DEV_INITALIZING:
2302 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2303 netxen_schedule_work(adapter,
2304 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2305 return;
2308 case NX_DEV_FAILED:
2309 default:
2310 nx_incr_dev_ref_cnt(adapter);
2311 break;
2314 clear_bit(__NX_RESETTING, &adapter->state);
2317 static void
2318 netxen_detach_work(struct work_struct *work)
2320 struct netxen_adapter *adapter = container_of(work,
2321 struct netxen_adapter, fw_work.work);
2322 struct net_device *netdev = adapter->netdev;
2323 int ref_cnt, delay;
2324 u32 status;
2326 netif_device_detach(netdev);
2328 netxen_nic_down(adapter, netdev);
2330 netxen_nic_detach(adapter);
2332 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2334 if (status & NX_RCODE_FATAL_ERROR)
2335 goto err_ret;
2337 if (adapter->temp == NX_TEMP_PANIC)
2338 goto err_ret;
2340 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2342 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2344 adapter->fw_wait_cnt = 0;
2345 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2347 return;
2349 err_ret:
2350 clear_bit(__NX_RESETTING, &adapter->state);
2353 static int
2354 netxen_check_health(struct netxen_adapter *adapter)
2356 u32 state, heartbit;
2357 struct net_device *netdev = adapter->netdev;
2359 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2360 if (state == NX_DEV_NEED_AER)
2361 return 0;
2363 if (netxen_nic_check_temp(adapter))
2364 goto detach;
2366 if (adapter->need_fw_reset) {
2367 if (nx_dev_request_reset(adapter))
2368 return 0;
2369 goto detach;
2372 /* NX_DEV_NEED_RESET, this state can be marked in two cases
2373 * 1. Tx timeout 2. Fw hang
2374 * Send request to destroy context in case of tx timeout only
2375 * and doesn't required in case of Fw hang
2377 if (state == NX_DEV_NEED_RESET) {
2378 adapter->need_fw_reset = 1;
2379 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2380 goto detach;
2383 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2384 return 0;
2386 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2387 if (heartbit != adapter->heartbit) {
2388 adapter->heartbit = heartbit;
2389 adapter->fw_fail_cnt = 0;
2390 if (adapter->need_fw_reset)
2391 goto detach;
2392 return 0;
2395 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2396 return 0;
2398 if (nx_dev_request_reset(adapter))
2399 return 0;
2401 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2403 dev_info(&netdev->dev, "firmware hang detected\n");
2405 detach:
2406 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2407 !test_and_set_bit(__NX_RESETTING, &adapter->state))
2408 netxen_schedule_work(adapter, netxen_detach_work, 0);
2409 return 1;
2412 static void
2413 netxen_fw_poll_work(struct work_struct *work)
2415 struct netxen_adapter *adapter = container_of(work,
2416 struct netxen_adapter, fw_work.work);
2418 if (test_bit(__NX_RESETTING, &adapter->state))
2419 goto reschedule;
2421 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2422 if (!adapter->has_link_events) {
2424 netxen_nic_handle_phy_intr(adapter);
2426 if (adapter->link_changed)
2427 netxen_nic_set_link_parameters(adapter);
2431 if (netxen_check_health(adapter))
2432 return;
2434 reschedule:
2435 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2438 static ssize_t
2439 netxen_store_bridged_mode(struct device *dev,
2440 struct device_attribute *attr, const char *buf, size_t len)
2442 struct net_device *net = to_net_dev(dev);
2443 struct netxen_adapter *adapter = netdev_priv(net);
2444 unsigned long new;
2445 int ret = -EINVAL;
2447 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2448 goto err_out;
2450 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2451 goto err_out;
2453 if (strict_strtoul(buf, 2, &new))
2454 goto err_out;
2456 if (!netxen_config_bridged_mode(adapter, !!new))
2457 ret = len;
2459 err_out:
2460 return ret;
2463 static ssize_t
2464 netxen_show_bridged_mode(struct device *dev,
2465 struct device_attribute *attr, char *buf)
2467 struct net_device *net = to_net_dev(dev);
2468 struct netxen_adapter *adapter;
2469 int bridged_mode = 0;
2471 adapter = netdev_priv(net);
2473 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2474 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2476 return sprintf(buf, "%d\n", bridged_mode);
2479 static struct device_attribute dev_attr_bridged_mode = {
2480 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2481 .show = netxen_show_bridged_mode,
2482 .store = netxen_store_bridged_mode,
2485 static ssize_t
2486 netxen_store_diag_mode(struct device *dev,
2487 struct device_attribute *attr, const char *buf, size_t len)
2489 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2490 unsigned long new;
2492 if (strict_strtoul(buf, 2, &new))
2493 return -EINVAL;
2495 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2496 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2498 return len;
2501 static ssize_t
2502 netxen_show_diag_mode(struct device *dev,
2503 struct device_attribute *attr, char *buf)
2505 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2507 return sprintf(buf, "%d\n",
2508 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2511 static struct device_attribute dev_attr_diag_mode = {
2512 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2513 .show = netxen_show_diag_mode,
2514 .store = netxen_store_diag_mode,
2517 static int
2518 netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2519 loff_t offset, size_t size)
2521 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2522 return -EIO;
2524 if ((size != 4) || (offset & 0x3))
2525 return -EINVAL;
2527 if (offset < NETXEN_PCI_CRBSPACE)
2528 return -EINVAL;
2530 return 0;
2533 static ssize_t
2534 netxen_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2535 char *buf, loff_t offset, size_t size)
2537 struct device *dev = container_of(kobj, struct device, kobj);
2538 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2539 u32 data;
2540 int ret;
2542 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2543 if (ret != 0)
2544 return ret;
2546 data = NXRD32(adapter, offset);
2547 memcpy(buf, &data, size);
2548 return size;
2551 static ssize_t
2552 netxen_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2553 char *buf, loff_t offset, size_t size)
2555 struct device *dev = container_of(kobj, struct device, kobj);
2556 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2557 u32 data;
2558 int ret;
2560 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2561 if (ret != 0)
2562 return ret;
2564 memcpy(&data, buf, size);
2565 NXWR32(adapter, offset, data);
2566 return size;
2569 static int
2570 netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2571 loff_t offset, size_t size)
2573 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2574 return -EIO;
2576 if ((size != 8) || (offset & 0x7))
2577 return -EIO;
2579 return 0;
2582 static ssize_t
2583 netxen_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2584 char *buf, loff_t offset, size_t size)
2586 struct device *dev = container_of(kobj, struct device, kobj);
2587 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2588 u64 data;
2589 int ret;
2591 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2592 if (ret != 0)
2593 return ret;
2595 if (adapter->pci_mem_read(adapter, offset, &data))
2596 return -EIO;
2598 memcpy(buf, &data, size);
2600 return size;
2603 ssize_t netxen_sysfs_write_mem(struct kobject *kobj,
2604 struct bin_attribute *attr, char *buf,
2605 loff_t offset, size_t size)
2607 struct device *dev = container_of(kobj, struct device, kobj);
2608 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2609 u64 data;
2610 int ret;
2612 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2613 if (ret != 0)
2614 return ret;
2616 memcpy(&data, buf, size);
2618 if (adapter->pci_mem_write(adapter, offset, data))
2619 return -EIO;
2621 return size;
2625 static struct bin_attribute bin_attr_crb = {
2626 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2627 .size = 0,
2628 .read = netxen_sysfs_read_crb,
2629 .write = netxen_sysfs_write_crb,
2632 static struct bin_attribute bin_attr_mem = {
2633 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2634 .size = 0,
2635 .read = netxen_sysfs_read_mem,
2636 .write = netxen_sysfs_write_mem,
2640 static void
2641 netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2643 struct net_device *netdev = adapter->netdev;
2644 struct device *dev = &netdev->dev;
2646 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2647 /* bridged_mode control */
2648 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2649 dev_warn(&netdev->dev,
2650 "failed to create bridged_mode sysfs entry\n");
2655 static void
2656 netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2658 struct net_device *netdev = adapter->netdev;
2659 struct device *dev = &netdev->dev;
2661 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2662 device_remove_file(dev, &dev_attr_bridged_mode);
2665 static void
2666 netxen_create_diag_entries(struct netxen_adapter *adapter)
2668 struct pci_dev *pdev = adapter->pdev;
2669 struct device *dev;
2671 dev = &pdev->dev;
2672 if (device_create_file(dev, &dev_attr_diag_mode))
2673 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2674 if (device_create_bin_file(dev, &bin_attr_crb))
2675 dev_info(dev, "failed to create crb sysfs entry\n");
2676 if (device_create_bin_file(dev, &bin_attr_mem))
2677 dev_info(dev, "failed to create mem sysfs entry\n");
2681 static void
2682 netxen_remove_diag_entries(struct netxen_adapter *adapter)
2684 struct pci_dev *pdev = adapter->pdev;
2685 struct device *dev = &pdev->dev;
2687 device_remove_file(dev, &dev_attr_diag_mode);
2688 device_remove_bin_file(dev, &bin_attr_crb);
2689 device_remove_bin_file(dev, &bin_attr_mem);
2692 #ifdef CONFIG_INET
2694 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2696 static int
2697 netxen_destip_supported(struct netxen_adapter *adapter)
2699 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2700 return 0;
2702 if (adapter->ahw.cut_through)
2703 return 0;
2705 return 1;
2708 static void
2709 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2711 struct in_device *indev;
2712 struct netxen_adapter *adapter = netdev_priv(dev);
2714 if (!netxen_destip_supported(adapter))
2715 return;
2717 indev = in_dev_get(dev);
2718 if (!indev)
2719 return;
2721 for_ifa(indev) {
2722 switch (event) {
2723 case NETDEV_UP:
2724 netxen_config_ipaddr(adapter,
2725 ifa->ifa_address, NX_IP_UP);
2726 break;
2727 case NETDEV_DOWN:
2728 netxen_config_ipaddr(adapter,
2729 ifa->ifa_address, NX_IP_DOWN);
2730 break;
2731 default:
2732 break;
2734 } endfor_ifa(indev);
2736 in_dev_put(indev);
2737 return;
2740 static int netxen_netdev_event(struct notifier_block *this,
2741 unsigned long event, void *ptr)
2743 struct netxen_adapter *adapter;
2744 struct net_device *dev = (struct net_device *)ptr;
2746 recheck:
2747 if (dev == NULL)
2748 goto done;
2750 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2751 dev = vlan_dev_real_dev(dev);
2752 goto recheck;
2755 if (!is_netxen_netdev(dev))
2756 goto done;
2758 adapter = netdev_priv(dev);
2760 if (!adapter)
2761 goto done;
2763 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2764 goto done;
2766 netxen_config_indev_addr(dev, event);
2767 done:
2768 return NOTIFY_DONE;
2771 static int
2772 netxen_inetaddr_event(struct notifier_block *this,
2773 unsigned long event, void *ptr)
2775 struct netxen_adapter *adapter;
2776 struct net_device *dev;
2778 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2780 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2782 recheck:
2783 if (dev == NULL || !netif_running(dev))
2784 goto done;
2786 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2787 dev = vlan_dev_real_dev(dev);
2788 goto recheck;
2791 if (!is_netxen_netdev(dev))
2792 goto done;
2794 adapter = netdev_priv(dev);
2796 if (!adapter || !netxen_destip_supported(adapter))
2797 goto done;
2799 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2800 goto done;
2802 switch (event) {
2803 case NETDEV_UP:
2804 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2805 break;
2806 case NETDEV_DOWN:
2807 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2808 break;
2809 default:
2810 break;
2813 done:
2814 return NOTIFY_DONE;
2817 static struct notifier_block netxen_netdev_cb = {
2818 .notifier_call = netxen_netdev_event,
2821 static struct notifier_block netxen_inetaddr_cb = {
2822 .notifier_call = netxen_inetaddr_event,
2824 #else
2825 static void
2826 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2828 #endif
2830 static struct pci_error_handlers netxen_err_handler = {
2831 .error_detected = netxen_io_error_detected,
2832 .slot_reset = netxen_io_slot_reset,
2833 .resume = netxen_io_resume,
2836 static struct pci_driver netxen_driver = {
2837 .name = netxen_nic_driver_name,
2838 .id_table = netxen_pci_tbl,
2839 .probe = netxen_nic_probe,
2840 .remove = __devexit_p(netxen_nic_remove),
2841 #ifdef CONFIG_PM
2842 .suspend = netxen_nic_suspend,
2843 .resume = netxen_nic_resume,
2844 #endif
2845 .shutdown = netxen_nic_shutdown,
2846 .err_handler = &netxen_err_handler
2849 static int __init netxen_init_module(void)
2851 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2853 #ifdef CONFIG_INET
2854 register_netdevice_notifier(&netxen_netdev_cb);
2855 register_inetaddr_notifier(&netxen_inetaddr_cb);
2856 #endif
2857 return pci_register_driver(&netxen_driver);
2860 module_init(netxen_init_module);
2862 static void __exit netxen_exit_module(void)
2864 pci_unregister_driver(&netxen_driver);
2866 #ifdef CONFIG_INET
2867 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2868 unregister_netdevice_notifier(&netxen_netdev_cb);
2869 #endif
2872 module_exit(netxen_exit_module);