2 * Copyright (C) 2003 - 2009 NetXen, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
27 * Cupertino, CA 95014-0701
31 #include <linux/vmalloc.h>
32 #include <linux/highmem.h>
33 #include "netxen_nic_hw.h"
35 #include "netxen_nic.h"
36 #include "netxen_nic_phan_reg.h"
38 #include <linux/dma-mapping.h>
39 #include <linux/if_vlan.h>
41 #include <linux/ipv6.h>
43 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
47 char netxen_nic_driver_name[] = "netxen_nic";
48 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
49 NETXEN_NIC_LINUX_VERSIONID;
51 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
53 /* Default to restricted 1G auto-neg mode */
54 static int wol_port_mode = 5;
56 static int use_msi = 1;
58 static int use_msi_x = 1;
60 /* Local functions to NetXen NIC driver */
61 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
62 const struct pci_device_id *ent);
63 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
64 static int netxen_nic_open(struct net_device *netdev);
65 static int netxen_nic_close(struct net_device *netdev);
66 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
67 static void netxen_tx_timeout(struct net_device *netdev);
68 static void netxen_tx_timeout_task(struct work_struct *work);
69 static void netxen_watchdog(unsigned long);
70 static int netxen_nic_poll(struct napi_struct *napi, int budget);
71 #ifdef CONFIG_NET_POLL_CONTROLLER
72 static void netxen_nic_poll_controller(struct net_device *netdev);
74 static irqreturn_t netxen_intr(int irq, void *data);
75 static irqreturn_t netxen_msi_intr(int irq, void *data);
76 static irqreturn_t netxen_msix_intr(int irq, void *data);
78 /* PCI Device ID Table */
79 #define ENTRY(device) \
80 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
81 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
83 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
84 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
85 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
86 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
87 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
88 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
91 ENTRY(PCI_DEVICE_ID_NX3031),
95 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
97 static struct workqueue_struct *netxen_workq;
98 #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
99 #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
101 static void netxen_watchdog(unsigned long);
103 static uint32_t crb_cmd_producer[4] = {
104 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
105 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
109 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
110 uint32_t crb_producer)
112 adapter->pci_write_normalize(adapter,
113 adapter->crb_addr_cmd_producer, crb_producer);
116 static uint32_t crb_cmd_consumer[4] = {
117 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
118 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
122 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
125 adapter->pci_write_normalize(adapter,
126 adapter->crb_addr_cmd_consumer, crb_consumer);
129 static uint32_t msi_tgt_status[8] = {
130 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
131 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
132 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
133 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
136 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
138 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
140 struct netxen_adapter *adapter = sds_ring->adapter;
142 adapter->pci_write_normalize(adapter, sds_ring->crb_intr_mask, 0);
145 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
147 struct netxen_adapter *adapter = sds_ring->adapter;
149 adapter->pci_write_normalize(adapter, sds_ring->crb_intr_mask, 0x1);
151 if (!NETXEN_IS_MSI_FAMILY(adapter))
152 adapter->pci_write_immediate(adapter,
153 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
157 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
160 struct nx_host_sds_ring *sds_ring;
161 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
163 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
164 adapter->max_sds_rings = (num_online_cpus() >= 4) ? 4 : 2;
166 adapter->max_sds_rings = 1;
168 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
169 sds_ring = &recv_ctx->sds_rings[ring];
170 netif_napi_add(netdev, &sds_ring->napi,
171 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
176 netxen_napi_enable(struct netxen_adapter *adapter)
179 struct nx_host_sds_ring *sds_ring;
180 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
182 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
183 sds_ring = &recv_ctx->sds_rings[ring];
184 napi_enable(&sds_ring->napi);
185 netxen_nic_enable_int(sds_ring);
190 netxen_napi_disable(struct netxen_adapter *adapter)
193 struct nx_host_sds_ring *sds_ring;
194 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
196 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
197 sds_ring = &recv_ctx->sds_rings[ring];
198 netxen_nic_disable_int(sds_ring);
199 napi_disable(&sds_ring->napi);
203 static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
205 struct pci_dev *pdev = adapter->pdev;
206 uint64_t mask, cmask;
208 adapter->pci_using_dac = 0;
210 mask = DMA_BIT_MASK(32);
212 * Consistent DMA mask is set to 32 bit because it cannot be set to
213 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
214 * come off this pool.
216 cmask = DMA_BIT_MASK(32);
219 if (revision_id >= NX_P3_B0)
220 mask = DMA_BIT_MASK(39);
221 else if (revision_id == NX_P2_C1)
222 mask = DMA_BIT_MASK(35);
224 if (pci_set_dma_mask(pdev, mask) == 0 &&
225 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
226 adapter->pci_using_dac = 1;
233 /* Update addressable range if firmware supports it */
235 nx_update_dma_mask(struct netxen_adapter *adapter)
237 int change, shift, err;
238 uint64_t mask, old_mask;
239 struct pci_dev *pdev = adapter->pdev;
243 shift = netxen_nic_reg_read(adapter, CRB_DMA_SHIFT);
247 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
249 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
253 old_mask = pdev->dma_mask;
254 mask = (1ULL<<(32+shift)) - 1;
256 err = pci_set_dma_mask(pdev, mask);
258 return pci_set_dma_mask(pdev, old_mask);
264 static void netxen_check_options(struct netxen_adapter *adapter)
266 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
267 adapter->num_rxd = MAX_RCV_DESCRIPTORS_10G;
268 else if (adapter->ahw.port_type == NETXEN_NIC_GBE)
269 adapter->num_rxd = MAX_RCV_DESCRIPTORS_1G;
271 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
272 adapter->msix_supported = !!use_msi_x;
274 adapter->msix_supported = 0;
276 adapter->num_txd = MAX_CMD_DESCRIPTORS_HOST;
277 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS;
278 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
284 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
288 if (first_boot == 0x55555555) {
289 /* This is the first boot after power up */
290 adapter->pci_write_normalize(adapter,
291 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
293 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
296 /* PCI bus master workaround */
297 adapter->hw_read_wx(adapter,
298 NETXEN_PCIE_REG(0x4), &first_boot, 4);
299 if (!(first_boot & 0x4)) {
301 adapter->hw_write_wx(adapter,
302 NETXEN_PCIE_REG(0x4), &first_boot, 4);
303 adapter->hw_read_wx(adapter,
304 NETXEN_PCIE_REG(0x4), &first_boot, 4);
307 /* This is the first boot after power up */
308 adapter->hw_read_wx(adapter,
309 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
310 if (first_boot != 0x80000f) {
311 /* clear the register for future unloads/loads */
312 adapter->pci_write_normalize(adapter,
313 NETXEN_CAM_RAM(0x1fc), 0);
317 /* Start P2 boot loader */
318 val = adapter->pci_read_normalize(adapter,
319 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
320 adapter->pci_write_normalize(adapter,
321 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
325 val = adapter->pci_read_normalize(adapter,
326 NETXEN_CAM_RAM(0x1fc));
328 if (++timeout > 5000)
331 } while (val == NETXEN_BDINFO_MAGIC);
336 static void netxen_set_port_mode(struct netxen_adapter *adapter)
340 val = adapter->ahw.board_type;
341 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
342 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
343 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
344 data = NETXEN_PORT_MODE_802_3_AP;
345 adapter->hw_write_wx(adapter,
346 NETXEN_PORT_MODE_ADDR, &data, 4);
347 } else if (port_mode == NETXEN_PORT_MODE_XG) {
348 data = NETXEN_PORT_MODE_XG;
349 adapter->hw_write_wx(adapter,
350 NETXEN_PORT_MODE_ADDR, &data, 4);
351 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
352 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
353 adapter->hw_write_wx(adapter,
354 NETXEN_PORT_MODE_ADDR, &data, 4);
355 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
356 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
357 adapter->hw_write_wx(adapter,
358 NETXEN_PORT_MODE_ADDR, &data, 4);
360 data = NETXEN_PORT_MODE_AUTO_NEG;
361 adapter->hw_write_wx(adapter,
362 NETXEN_PORT_MODE_ADDR, &data, 4);
365 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
366 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
367 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
368 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
369 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
371 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
376 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
381 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
383 pci_read_config_dword(pdev, pos, &control);
385 control |= PCI_MSIX_FLAGS_ENABLE;
388 pci_write_config_dword(pdev, pos, control);
392 static void netxen_init_msix_entries(struct netxen_adapter *adapter)
396 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
397 adapter->msix_entries[i].entry = i;
401 netxen_read_mac_addr(struct netxen_adapter *adapter)
406 struct net_device *netdev = adapter->netdev;
407 struct pci_dev *pdev = adapter->pdev;
409 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
410 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
413 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
417 p = (unsigned char *)&mac_addr;
418 for (i = 0; i < 6; i++)
419 netdev->dev_addr[i] = *(p + 5 - i);
421 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
423 /* set station address */
425 if (!is_valid_ether_addr(netdev->perm_addr))
426 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
428 adapter->macaddr_set(adapter, netdev->dev_addr);
433 static void netxen_set_multicast_list(struct net_device *dev)
435 struct netxen_adapter *adapter = netdev_priv(dev);
437 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
438 netxen_p3_nic_set_multi(dev);
440 netxen_p2_nic_set_multi(dev);
443 static const struct net_device_ops netxen_netdev_ops = {
444 .ndo_open = netxen_nic_open,
445 .ndo_stop = netxen_nic_close,
446 .ndo_start_xmit = netxen_nic_xmit_frame,
447 .ndo_get_stats = netxen_nic_get_stats,
448 .ndo_validate_addr = eth_validate_addr,
449 .ndo_set_multicast_list = netxen_set_multicast_list,
450 .ndo_set_mac_address = netxen_nic_set_mac,
451 .ndo_change_mtu = netxen_nic_change_mtu,
452 .ndo_tx_timeout = netxen_tx_timeout,
453 #ifdef CONFIG_NET_POLL_CONTROLLER
454 .ndo_poll_controller = netxen_nic_poll_controller,
459 netxen_setup_intr(struct netxen_adapter *adapter)
461 struct netxen_legacy_intr_set *legacy_intrp;
462 struct pci_dev *pdev = adapter->pdev;
464 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
465 adapter->intr_scheme = -1;
466 adapter->msi_mode = -1;
468 if (adapter->ahw.revision_id >= NX_P3_B0)
469 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
471 legacy_intrp = &legacy_intr[0];
472 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
473 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
474 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
475 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
477 netxen_set_msix_bit(pdev, 0);
479 if (adapter->msix_supported) {
481 netxen_init_msix_entries(adapter);
482 if (pci_enable_msix(pdev, adapter->msix_entries,
483 MSIX_ENTRIES_PER_ADAPTER))
486 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
487 netxen_set_msix_bit(pdev, 1);
488 dev_info(&pdev->dev, "using msi-x interrupts\n");
492 if (use_msi && !pci_enable_msi(pdev)) {
493 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
494 dev_info(&pdev->dev, "using msi interrupts\n");
496 dev_info(&pdev->dev, "using legacy interrupts\n");
497 adapter->msix_entries[0].vector = pdev->irq;
502 netxen_teardown_intr(struct netxen_adapter *adapter)
504 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
505 pci_disable_msix(adapter->pdev);
506 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
507 pci_disable_msi(adapter->pdev);
511 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
513 if (adapter->ahw.db_base != NULL)
514 iounmap(adapter->ahw.db_base);
515 if (adapter->ahw.pci_base0 != NULL)
516 iounmap(adapter->ahw.pci_base0);
517 if (adapter->ahw.pci_base1 != NULL)
518 iounmap(adapter->ahw.pci_base1);
519 if (adapter->ahw.pci_base2 != NULL)
520 iounmap(adapter->ahw.pci_base2);
524 netxen_setup_pci_map(struct netxen_adapter *adapter)
526 void __iomem *mem_ptr0 = NULL;
527 void __iomem *mem_ptr1 = NULL;
528 void __iomem *mem_ptr2 = NULL;
529 void __iomem *db_ptr = NULL;
531 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
533 struct pci_dev *pdev = adapter->pdev;
534 int pci_func = adapter->ahw.pci_func;
539 * Set the CRB window to invalid. If any register in window 0 is
540 * accessed it should set the window to 0 and then reset it to 1.
542 adapter->curr_window = 255;
543 adapter->ahw.qdr_sn_window = -1;
544 adapter->ahw.ddr_mn_window = -1;
546 /* remap phys address */
547 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
548 mem_len = pci_resource_len(pdev, 0);
551 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
552 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
553 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
554 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
555 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
556 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
557 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
558 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
559 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
561 /* 128 Meg of memory */
562 if (mem_len == NETXEN_PCI_128MB_SIZE) {
563 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
564 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
565 SECOND_PAGE_GROUP_SIZE);
566 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
567 THIRD_PAGE_GROUP_SIZE);
568 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
569 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
570 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
571 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
572 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
573 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
574 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
575 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
576 adapter->pci_write_immediate =
577 netxen_nic_pci_write_immediate_2M;
578 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
579 adapter->pci_write_normalize =
580 netxen_nic_pci_write_normalize_2M;
581 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
582 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
583 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
585 mem_ptr0 = pci_ioremap_bar(pdev, 0);
586 if (mem_ptr0 == NULL) {
587 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
592 adapter->ahw.ddr_mn_window = 0;
593 adapter->ahw.qdr_sn_window = 0;
595 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
597 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
599 adapter->ahw.ms_win_crb += (pci_func * 0x20);
601 adapter->ahw.ms_win_crb +=
602 0xA0 + ((pci_func - 4) * 0x10);
607 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
609 adapter->ahw.pci_base0 = mem_ptr0;
610 adapter->ahw.pci_len0 = pci_len0;
611 adapter->ahw.pci_base1 = mem_ptr1;
612 adapter->ahw.pci_base2 = mem_ptr2;
614 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
617 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
618 db_len = pci_resource_len(pdev, 4);
621 printk(KERN_ERR "%s: doorbell is disabled\n",
622 netxen_nic_driver_name);
627 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
629 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
630 netxen_nic_driver_name);
636 adapter->ahw.db_base = db_ptr;
637 adapter->ahw.db_len = db_len;
641 netxen_cleanup_pci_map(adapter);
646 netxen_start_firmware(struct netxen_adapter *adapter)
648 int val, err, first_boot;
649 struct pci_dev *pdev = adapter->pdev;
651 int first_driver = 0;
652 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
653 if (adapter->ahw.pci_func == 0)
656 if (adapter->portnum == 0)
663 first_boot = adapter->pci_read_normalize(adapter,
664 NETXEN_CAM_RAM(0x1fc));
666 err = netxen_check_hw_init(adapter, first_boot);
668 dev_err(&pdev->dev, "error in init HW init sequence\n");
672 if (first_boot != 0x55555555) {
673 adapter->pci_write_normalize(adapter,
674 CRB_CMDPEG_STATE, 0);
675 netxen_pinit_from_rom(adapter, 0);
679 netxen_nic_reg_write(adapter, CRB_DMA_SHIFT, 0x55555555);
680 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
681 netxen_set_port_mode(adapter);
683 netxen_load_firmware(adapter);
685 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
687 /* Initialize multicast addr pool owners */
689 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
691 netxen_crb_writelit_adapter(adapter,
692 NETXEN_MAC_ADDR_CNTL_REG, val);
696 err = netxen_initialize_adapter_offload(adapter);
701 * Tell the hardware our version number.
703 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
704 | ((_NETXEN_NIC_LINUX_MINOR << 8))
705 | (_NETXEN_NIC_LINUX_SUBVERSION);
706 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, val);
708 /* Handshake with the card before we register the devices. */
709 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
711 netxen_free_adapter_offload(adapter);
719 netxen_nic_request_irq(struct netxen_adapter *adapter)
721 irq_handler_t handler;
722 struct nx_host_sds_ring *sds_ring;
725 unsigned long flags = IRQF_SAMPLE_RANDOM;
726 struct net_device *netdev = adapter->netdev;
727 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
729 if ((adapter->msi_mode != MSI_MODE_MULTIFUNC) ||
730 (adapter->intr_scheme != INTR_SCHEME_PERPORT)) {
731 printk(KERN_ERR "%s: Firmware interrupt scheme is "
732 "incompatible with driver\n",
734 adapter->driver_mismatch = 1;
738 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
739 handler = netxen_msix_intr;
740 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
741 handler = netxen_msi_intr;
743 flags |= IRQF_SHARED;
744 handler = netxen_intr;
746 adapter->irq = netdev->irq;
748 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
749 sds_ring = &recv_ctx->sds_rings[ring];
750 sprintf(sds_ring->name, "%16s[%d]", netdev->name, ring);
751 err = request_irq(sds_ring->irq, handler,
752 flags, sds_ring->name, sds_ring);
761 netxen_nic_free_irq(struct netxen_adapter *adapter)
764 struct nx_host_sds_ring *sds_ring;
766 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
768 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
769 sds_ring = &recv_ctx->sds_rings[ring];
770 free_irq(sds_ring->irq, sds_ring);
775 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
779 err = adapter->init_port(adapter, adapter->physical_port);
781 printk(KERN_ERR "%s: Failed to initialize port %d\n",
782 netxen_nic_driver_name, adapter->portnum);
785 adapter->macaddr_set(adapter, netdev->dev_addr);
787 netxen_nic_set_link_parameters(adapter);
789 netxen_set_multicast_list(netdev);
790 if (adapter->set_mtu)
791 adapter->set_mtu(adapter, netdev->mtu);
793 adapter->ahw.linkup = 0;
794 mod_timer(&adapter->watchdog_timer, jiffies);
796 netxen_napi_enable(adapter);
798 if (adapter->max_sds_rings > 1)
799 netxen_config_rss(adapter, 1);
805 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
807 netif_carrier_off(netdev);
808 netif_stop_queue(netdev);
809 netxen_napi_disable(adapter);
811 if (adapter->stop_port)
812 adapter->stop_port(adapter);
814 netxen_release_tx_buffers(adapter);
816 FLUSH_SCHEDULED_WORK();
817 del_timer_sync(&adapter->watchdog_timer);
822 netxen_nic_attach(struct netxen_adapter *adapter)
824 struct net_device *netdev = adapter->netdev;
825 struct pci_dev *pdev = adapter->pdev;
827 struct nx_host_rds_ring *rds_ring;
829 err = netxen_init_firmware(adapter);
831 printk(KERN_ERR "Failed to init firmware\n");
835 if (adapter->fw_major < 4)
836 adapter->max_rds_rings = 3;
838 adapter->max_rds_rings = 2;
840 err = netxen_alloc_sw_resources(adapter);
842 printk(KERN_ERR "%s: Error in setting sw resources\n",
847 netxen_nic_clear_stats(adapter);
849 err = netxen_alloc_hw_resources(adapter);
851 printk(KERN_ERR "%s: Error in setting hw resources\n",
853 goto err_out_free_sw;
856 if (adapter->fw_major < 4) {
857 adapter->crb_addr_cmd_producer =
858 crb_cmd_producer[adapter->portnum];
859 adapter->crb_addr_cmd_consumer =
860 crb_cmd_consumer[adapter->portnum];
862 netxen_nic_update_cmd_producer(adapter, 0);
863 netxen_nic_update_cmd_consumer(adapter, 0);
866 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
867 rds_ring = &adapter->recv_ctx.rds_rings[ring];
868 netxen_post_rx_buffers(adapter, ring, rds_ring);
871 err = netxen_nic_request_irq(adapter);
873 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
875 goto err_out_free_rxbuf;
878 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
882 netxen_release_rx_buffers(adapter);
883 netxen_free_hw_resources(adapter);
885 netxen_free_sw_resources(adapter);
890 netxen_nic_detach(struct netxen_adapter *adapter)
892 netxen_nic_free_irq(adapter);
894 netxen_release_rx_buffers(adapter);
895 netxen_free_hw_resources(adapter);
896 netxen_free_sw_resources(adapter);
902 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
904 struct net_device *netdev = NULL;
905 struct netxen_adapter *adapter = NULL;
907 int pci_func_id = PCI_FUNC(pdev->devfn);
910 if (pdev->class != 0x020000) {
911 printk(KERN_DEBUG "NetXen function %d, class %x will not "
912 "be enabled.\n",pci_func_id, pdev->class);
916 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
917 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
918 "will not be enabled.\n",
923 if ((err = pci_enable_device(pdev)))
926 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
928 goto err_out_disable_pdev;
931 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
932 goto err_out_disable_pdev;
934 pci_set_master(pdev);
936 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
938 printk(KERN_ERR"%s: Failed to allocate memory for the "
939 "device block.Check system memory resource"
940 " usage.\n", netxen_nic_driver_name);
941 goto err_out_free_res;
944 SET_NETDEV_DEV(netdev, &pdev->dev);
946 adapter = netdev_priv(netdev);
947 adapter->netdev = netdev;
948 adapter->pdev = pdev;
949 adapter->ahw.pci_func = pci_func_id;
951 revision_id = pdev->revision;
952 adapter->ahw.revision_id = revision_id;
954 err = nx_set_dma_mask(adapter, revision_id);
956 goto err_out_free_netdev;
958 rwlock_init(&adapter->adapter_lock);
959 spin_lock_init(&adapter->tx_clean_lock);
961 err = netxen_setup_pci_map(adapter);
963 goto err_out_free_netdev;
965 /* This will be reset for mezz cards */
966 adapter->portnum = pci_func_id;
967 adapter->rx_csum = 1;
968 adapter->mc_enabled = 0;
969 if (NX_IS_REVISION_P3(revision_id))
970 adapter->max_mc_count = 38;
972 adapter->max_mc_count = 16;
974 netdev->netdev_ops = &netxen_netdev_ops;
975 netdev->watchdog_timeo = 2*HZ;
977 netxen_nic_change_mtu(netdev, netdev->mtu);
979 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
981 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
982 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
984 if (NX_IS_REVISION_P3(revision_id)) {
985 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
986 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
989 if (adapter->pci_using_dac) {
990 netdev->features |= NETIF_F_HIGHDMA;
991 netdev->vlan_features |= NETIF_F_HIGHDMA;
994 if (netxen_nic_get_board_info(adapter) != 0) {
995 printk("%s: Error getting board config info.\n",
996 netxen_nic_driver_name);
998 goto err_out_iounmap;
1001 netxen_initialize_adapter_ops(adapter);
1003 /* Mezz cards have PCI function 0,2,3 enabled */
1004 switch (adapter->ahw.board_type) {
1005 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1006 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1007 if (pci_func_id >= 2)
1008 adapter->portnum = pci_func_id - 2;
1014 err = netxen_start_firmware(adapter);
1016 goto err_out_iounmap;
1018 nx_update_dma_mask(adapter);
1020 netxen_nic_get_firmware_info(adapter);
1023 * See if the firmware gave us a virtual-physical port mapping.
1025 adapter->physical_port = adapter->portnum;
1026 if (adapter->fw_major < 4) {
1027 i = adapter->pci_read_normalize(adapter,
1028 CRB_V2P(adapter->portnum));
1029 if (i != 0x55555555)
1030 adapter->physical_port = i;
1033 netxen_check_options(adapter);
1035 netxen_setup_intr(adapter);
1037 netdev->irq = adapter->msix_entries[0].vector;
1039 netxen_napi_add(adapter, netdev);
1041 err = netxen_receive_peg_ready(adapter);
1043 goto err_out_disable_msi;
1045 init_timer(&adapter->watchdog_timer);
1046 adapter->watchdog_timer.function = &netxen_watchdog;
1047 adapter->watchdog_timer.data = (unsigned long)adapter;
1048 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1049 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1051 err = netxen_read_mac_addr(adapter);
1053 dev_warn(&pdev->dev, "failed to read mac addr\n");
1055 netif_carrier_off(netdev);
1056 netif_stop_queue(netdev);
1058 if ((err = register_netdev(netdev))) {
1059 printk(KERN_ERR "%s: register_netdev failed port #%d"
1060 " aborting\n", netxen_nic_driver_name,
1063 goto err_out_disable_msi;
1066 pci_set_drvdata(pdev, adapter);
1068 switch (adapter->ahw.port_type) {
1069 case NETXEN_NIC_GBE:
1070 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1071 adapter->netdev->name);
1073 case NETXEN_NIC_XGBE:
1074 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1075 adapter->netdev->name);
1081 err_out_disable_msi:
1082 netxen_teardown_intr(adapter);
1084 netxen_free_adapter_offload(adapter);
1087 netxen_cleanup_pci_map(adapter);
1089 err_out_free_netdev:
1090 free_netdev(netdev);
1093 pci_release_regions(pdev);
1095 err_out_disable_pdev:
1096 pci_set_drvdata(pdev, NULL);
1097 pci_disable_device(pdev);
1101 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1103 struct netxen_adapter *adapter;
1104 struct net_device *netdev;
1106 adapter = pci_get_drvdata(pdev);
1107 if (adapter == NULL)
1110 netdev = adapter->netdev;
1112 unregister_netdev(netdev);
1114 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1115 netxen_nic_detach(adapter);
1117 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1118 netxen_p3_free_mac_list(adapter);
1121 if (adapter->portnum == 0)
1122 netxen_free_adapter_offload(adapter);
1124 netxen_teardown_intr(adapter);
1126 netxen_cleanup_pci_map(adapter);
1128 pci_release_regions(pdev);
1129 pci_disable_device(pdev);
1130 pci_set_drvdata(pdev, NULL);
1132 free_netdev(netdev);
1136 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1139 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1140 struct net_device *netdev = adapter->netdev;
1142 netif_device_detach(netdev);
1144 if (netif_running(netdev))
1145 netxen_nic_down(adapter, netdev);
1147 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1148 netxen_nic_detach(adapter);
1150 pci_save_state(pdev);
1152 if (netxen_nic_wol_supported(adapter)) {
1153 pci_enable_wake(pdev, PCI_D3cold, 1);
1154 pci_enable_wake(pdev, PCI_D3hot, 1);
1157 pci_disable_device(pdev);
1158 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1164 netxen_nic_resume(struct pci_dev *pdev)
1166 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1167 struct net_device *netdev = adapter->netdev;
1170 pci_set_power_state(pdev, PCI_D0);
1171 pci_restore_state(pdev);
1173 err = pci_enable_device(pdev);
1177 adapter->curr_window = 255;
1179 err = netxen_start_firmware(adapter);
1181 dev_err(&pdev->dev, "failed to start firmware\n");
1185 if (netif_running(netdev)) {
1186 err = netxen_nic_attach(adapter);
1190 err = netxen_nic_up(adapter, netdev);
1194 netif_device_attach(netdev);
1200 static int netxen_nic_open(struct net_device *netdev)
1202 struct netxen_adapter *adapter = netdev_priv(netdev);
1205 if (adapter->driver_mismatch)
1208 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1209 err = netxen_nic_attach(adapter);
1214 err = netxen_nic_up(adapter, netdev);
1218 netif_start_queue(netdev);
1223 netxen_nic_detach(adapter);
1228 * netxen_nic_close - Disables a network interface entry point
1230 static int netxen_nic_close(struct net_device *netdev)
1232 struct netxen_adapter *adapter = netdev_priv(netdev);
1234 netxen_nic_down(adapter, netdev);
1238 static bool netxen_tso_check(struct net_device *netdev,
1239 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1242 u8 opcode = TX_ETHER_PKT;
1243 __be16 protocol = skb->protocol;
1246 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1247 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1248 protocol = vh->h_vlan_encapsulated_proto;
1249 flags = FLAGS_VLAN_TAGGED;
1252 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1253 skb_shinfo(skb)->gso_size > 0) {
1255 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1256 desc->total_hdr_length =
1257 skb_transport_offset(skb) + tcp_hdrlen(skb);
1259 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1260 TX_TCP_LSO6 : TX_TCP_LSO;
1263 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1266 if (protocol == cpu_to_be16(ETH_P_IP)) {
1267 l4proto = ip_hdr(skb)->protocol;
1269 if (l4proto == IPPROTO_TCP)
1270 opcode = TX_TCP_PKT;
1271 else if(l4proto == IPPROTO_UDP)
1272 opcode = TX_UDP_PKT;
1273 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1274 l4proto = ipv6_hdr(skb)->nexthdr;
1276 if (l4proto == IPPROTO_TCP)
1277 opcode = TX_TCPV6_PKT;
1278 else if(l4proto == IPPROTO_UDP)
1279 opcode = TX_UDPV6_PKT;
1282 desc->tcp_hdr_offset = skb_transport_offset(skb);
1283 desc->ip_hdr_offset = skb_network_offset(skb);
1284 netxen_set_tx_flags_opcode(desc, flags, opcode);
1289 netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1290 struct netxen_cmd_buffer *pbuf, int last)
1293 struct netxen_skb_frag *buffrag;
1295 buffrag = &pbuf->frag_array[0];
1296 pci_unmap_single(pdev, buffrag->dma,
1297 buffrag->length, PCI_DMA_TODEVICE);
1299 for (k = 1; k < last; k++) {
1300 buffrag = &pbuf->frag_array[k];
1301 pci_unmap_page(pdev, buffrag->dma,
1302 buffrag->length, PCI_DMA_TODEVICE);
1307 netxen_clear_cmddesc(u64 *desc)
1310 for (i = 0; i < 8; i++)
1315 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1317 struct netxen_adapter *adapter = netdev_priv(netdev);
1318 struct netxen_hardware_context *hw = &adapter->ahw;
1319 unsigned int first_seg_len = skb->len - skb->data_len;
1320 struct netxen_cmd_buffer *pbuf;
1321 struct netxen_skb_frag *buffrag;
1322 struct cmd_desc_type0 *hwdesc;
1323 struct pci_dev *pdev = adapter->pdev;
1324 dma_addr_t temp_dma;
1327 u32 producer, consumer;
1328 int frag_count, no_of_desc;
1329 u32 num_txd = adapter->num_txd;
1330 bool is_tso = false;
1332 frag_count = skb_shinfo(skb)->nr_frags + 1;
1334 /* There 4 fragments per descriptor */
1335 no_of_desc = (frag_count + 3) >> 2;
1337 producer = adapter->cmd_producer;
1339 consumer = adapter->last_cmd_consumer;
1340 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1341 netif_stop_queue(netdev);
1343 return NETDEV_TX_BUSY;
1346 /* Copy the descriptors into the hardware */
1347 hwdesc = &hw->cmd_desc_head[producer];
1348 netxen_clear_cmddesc((u64 *)hwdesc);
1349 /* Take skb->data itself */
1350 pbuf = &adapter->cmd_buf_arr[producer];
1352 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1355 pbuf->frag_count = frag_count;
1356 buffrag = &pbuf->frag_array[0];
1357 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
1359 if (pci_dma_mapping_error(pdev, temp_dma))
1362 buffrag->dma = temp_dma;
1363 buffrag->length = first_seg_len;
1364 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1365 netxen_set_tx_port(hwdesc, adapter->portnum);
1367 hwdesc->buffer_length[0] = cpu_to_le16(first_seg_len);
1368 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1370 for (i = 1, k = 1; i < frag_count; i++, k++) {
1371 struct skb_frag_struct *frag;
1373 unsigned long offset;
1375 /* move to next desc. if there is a need */
1376 if ((i & 0x3) == 0) {
1378 producer = get_next_index(producer, num_txd);
1379 hwdesc = &hw->cmd_desc_head[producer];
1380 netxen_clear_cmddesc((u64 *)hwdesc);
1381 pbuf = &adapter->cmd_buf_arr[producer];
1384 frag = &skb_shinfo(skb)->frags[i - 1];
1386 offset = frag->page_offset;
1389 temp_dma = pci_map_page(pdev, frag->page, offset,
1390 len, PCI_DMA_TODEVICE);
1391 if (pci_dma_mapping_error(pdev, temp_dma)) {
1392 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1397 buffrag->dma = temp_dma;
1398 buffrag->length = temp_len;
1400 hwdesc->buffer_length[k] = cpu_to_le16(temp_len);
1403 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1406 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1409 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1412 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1417 producer = get_next_index(producer, num_txd);
1419 /* For LSO, we need to copy the MAC/IP/TCP headers into
1420 * the descriptor ring
1423 int hdr_len, first_hdr_len, more_hdr;
1424 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1425 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1426 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1429 first_hdr_len = hdr_len;
1432 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1433 hwdesc = &hw->cmd_desc_head[producer];
1434 pbuf = &adapter->cmd_buf_arr[producer];
1437 /* copy the first 64 bytes */
1438 memcpy(((void *)hwdesc) + 2,
1439 (void *)(skb->data), first_hdr_len);
1440 producer = get_next_index(producer, num_txd);
1443 hwdesc = &hw->cmd_desc_head[producer];
1444 pbuf = &adapter->cmd_buf_arr[producer];
1446 /* copy the next 64 bytes - should be enough except
1447 * for pathological case
1449 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1453 producer = get_next_index(producer, num_txd);
1457 adapter->cmd_producer = producer;
1458 adapter->stats.txbytes += skb->len;
1460 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
1462 adapter->stats.xmitcalled++;
1463 netdev->trans_start = jiffies;
1465 return NETDEV_TX_OK;
1468 adapter->stats.txdropped++;
1469 dev_kfree_skb_any(skb);
1470 return NETDEV_TX_OK;
1473 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1475 struct net_device *netdev = adapter->netdev;
1476 uint32_t temp, temp_state, temp_val;
1479 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1481 temp_state = nx_get_temp_state(temp);
1482 temp_val = nx_get_temp_val(temp);
1484 if (temp_state == NX_TEMP_PANIC) {
1486 "%s: Device temperature %d degrees C exceeds"
1487 " maximum allowed. Hardware has been shut down.\n",
1488 netxen_nic_driver_name, temp_val);
1490 netif_carrier_off(netdev);
1491 netif_stop_queue(netdev);
1493 } else if (temp_state == NX_TEMP_WARN) {
1494 if (adapter->temp == NX_TEMP_NORMAL) {
1496 "%s: Device temperature %d degrees C "
1497 "exceeds operating range."
1498 " Immediate action needed.\n",
1499 netxen_nic_driver_name, temp_val);
1502 if (adapter->temp == NX_TEMP_WARN) {
1504 "%s: Device temperature is now %d degrees C"
1505 " in normal range.\n", netxen_nic_driver_name,
1509 adapter->temp = temp_state;
1513 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1515 struct net_device *netdev = adapter->netdev;
1516 u32 val, port, linkup;
1518 port = adapter->physical_port;
1520 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1521 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1522 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1523 linkup = (val == XG_LINK_UP_P3);
1525 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1526 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
1527 linkup = (val >> port) & 1;
1529 val = (val >> port*8) & 0xff;
1530 linkup = (val == XG_LINK_UP);
1534 if (adapter->ahw.linkup && !linkup) {
1535 printk(KERN_INFO "%s: %s NIC Link is down\n",
1536 netxen_nic_driver_name, netdev->name);
1537 adapter->ahw.linkup = 0;
1538 if (netif_running(netdev)) {
1539 netif_carrier_off(netdev);
1540 netif_stop_queue(netdev);
1543 netxen_nic_set_link_parameters(adapter);
1544 } else if (!adapter->ahw.linkup && linkup) {
1545 printk(KERN_INFO "%s: %s NIC Link is up\n",
1546 netxen_nic_driver_name, netdev->name);
1547 adapter->ahw.linkup = 1;
1548 if (netif_running(netdev)) {
1549 netif_carrier_on(netdev);
1550 netif_wake_queue(netdev);
1553 netxen_nic_set_link_parameters(adapter);
1557 static void netxen_watchdog(unsigned long v)
1559 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1561 SCHEDULE_WORK(&adapter->watchdog_task);
1564 void netxen_watchdog_task(struct work_struct *work)
1566 struct netxen_adapter *adapter =
1567 container_of(work, struct netxen_adapter, watchdog_task);
1569 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1572 netxen_nic_handle_phy_intr(adapter);
1574 if (netif_running(adapter->netdev))
1575 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1578 static void netxen_tx_timeout(struct net_device *netdev)
1580 struct netxen_adapter *adapter = (struct netxen_adapter *)
1581 netdev_priv(netdev);
1582 SCHEDULE_WORK(&adapter->tx_timeout_task);
1585 static void netxen_tx_timeout_task(struct work_struct *work)
1587 struct netxen_adapter *adapter =
1588 container_of(work, struct netxen_adapter, tx_timeout_task);
1590 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1591 netxen_nic_driver_name, adapter->netdev->name);
1593 netxen_napi_disable(adapter);
1595 adapter->netdev->trans_start = jiffies;
1597 netxen_napi_enable(adapter);
1598 netif_wake_queue(adapter->netdev);
1602 * netxen_nic_get_stats - Get System Network Statistics
1603 * @netdev: network interface device structure
1605 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1607 struct netxen_adapter *adapter = netdev_priv(netdev);
1608 struct net_device_stats *stats = &adapter->net_stats;
1610 memset(stats, 0, sizeof(*stats));
1612 /* total packets received */
1613 stats->rx_packets = adapter->stats.no_rcv;
1614 /* total packets transmitted */
1615 stats->tx_packets = adapter->stats.xmitedframes +
1616 adapter->stats.xmitfinished;
1617 /* total bytes received */
1618 stats->rx_bytes = adapter->stats.rxbytes;
1619 /* total bytes transmitted */
1620 stats->tx_bytes = adapter->stats.txbytes;
1621 /* bad packets received */
1622 stats->rx_errors = adapter->stats.rcvdbadskb;
1623 /* packet transmit problems */
1624 stats->tx_errors = adapter->stats.nocmddescriptor;
1625 /* no space in linux buffers */
1626 stats->rx_dropped = adapter->stats.rxdropped;
1627 /* no space available in linux */
1628 stats->tx_dropped = adapter->stats.txdropped;
1633 static irqreturn_t netxen_intr(int irq, void *data)
1635 struct nx_host_sds_ring *sds_ring = data;
1636 struct netxen_adapter *adapter = sds_ring->adapter;
1639 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1641 if (!(status & adapter->legacy_intr.int_vec_bit))
1644 if (adapter->ahw.revision_id >= NX_P3_B1) {
1645 /* check interrupt state machine, to be sure */
1646 status = adapter->pci_read_immediate(adapter,
1648 if (!ISR_LEGACY_INT_TRIGGERED(status))
1652 unsigned long our_int = 0;
1654 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
1656 /* not our interrupt */
1657 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
1660 /* claim interrupt */
1661 adapter->pci_write_normalize(adapter,
1662 CRB_INT_VECTOR, (our_int & 0xffffffff));
1665 /* clear interrupt */
1666 if (adapter->fw_major < 4)
1667 netxen_nic_disable_int(sds_ring);
1669 adapter->pci_write_immediate(adapter,
1670 adapter->legacy_intr.tgt_status_reg,
1672 /* read twice to ensure write is flushed */
1673 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1674 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1676 napi_schedule(&sds_ring->napi);
1681 static irqreturn_t netxen_msi_intr(int irq, void *data)
1683 struct nx_host_sds_ring *sds_ring = data;
1684 struct netxen_adapter *adapter = sds_ring->adapter;
1686 /* clear interrupt */
1687 adapter->pci_write_immediate(adapter,
1688 msi_tgt_status[adapter->ahw.pci_func], 0xffffffff);
1690 napi_schedule(&sds_ring->napi);
1694 static irqreturn_t netxen_msix_intr(int irq, void *data)
1696 struct nx_host_sds_ring *sds_ring = data;
1698 napi_schedule(&sds_ring->napi);
1702 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1704 struct nx_host_sds_ring *sds_ring =
1705 container_of(napi, struct nx_host_sds_ring, napi);
1707 struct netxen_adapter *adapter = sds_ring->adapter;
1712 tx_complete = netxen_process_cmd_ring(adapter);
1714 work_done = netxen_process_rcv_ring(sds_ring, budget);
1716 if ((work_done < budget) && tx_complete) {
1717 napi_complete(&sds_ring->napi);
1718 netxen_nic_enable_int(sds_ring);
1724 #ifdef CONFIG_NET_POLL_CONTROLLER
1725 static void netxen_nic_poll_controller(struct net_device *netdev)
1727 struct netxen_adapter *adapter = netdev_priv(netdev);
1728 disable_irq(adapter->irq);
1729 netxen_intr(adapter->irq, adapter);
1730 enable_irq(adapter->irq);
1734 static struct pci_driver netxen_driver = {
1735 .name = netxen_nic_driver_name,
1736 .id_table = netxen_pci_tbl,
1737 .probe = netxen_nic_probe,
1738 .remove = __devexit_p(netxen_nic_remove),
1739 .suspend = netxen_nic_suspend,
1740 .resume = netxen_nic_resume
1743 /* Driver Registration on NetXen card */
1745 static int __init netxen_init_module(void)
1747 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
1749 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1752 return pci_register_driver(&netxen_driver);
1755 module_init(netxen_init_module);
1757 static void __exit netxen_exit_module(void)
1759 pci_unregister_driver(&netxen_driver);
1760 destroy_workqueue(netxen_workq);
1763 module_exit(netxen_exit_module);