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/interrupt.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 struct nx_host_tx_ring *tx_ring)
112 NXWR32(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
114 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
115 netif_stop_queue(adapter->netdev);
120 static uint32_t crb_cmd_consumer[4] = {
121 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
122 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
126 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
127 struct nx_host_tx_ring *tx_ring)
129 NXWR32(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
132 static uint32_t msi_tgt_status[8] = {
133 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
134 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
135 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
136 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
139 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
141 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
143 struct netxen_adapter *adapter = sds_ring->adapter;
145 NXWR32(adapter, sds_ring->crb_intr_mask, 0);
148 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
150 struct netxen_adapter *adapter = sds_ring->adapter;
152 NXWR32(adapter, sds_ring->crb_intr_mask, 0x1);
154 if (!NETXEN_IS_MSI_FAMILY(adapter))
155 adapter->pci_write_immediate(adapter,
156 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
160 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
162 int size = sizeof(struct nx_host_sds_ring) * count;
164 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
166 return (recv_ctx->sds_rings == NULL);
170 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
172 if (recv_ctx->sds_rings != NULL)
173 kfree(recv_ctx->sds_rings);
177 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
180 struct nx_host_sds_ring *sds_ring;
181 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
183 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
186 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
187 sds_ring = &recv_ctx->sds_rings[ring];
188 netif_napi_add(netdev, &sds_ring->napi,
189 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
196 netxen_napi_enable(struct netxen_adapter *adapter)
199 struct nx_host_sds_ring *sds_ring;
200 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
202 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
203 sds_ring = &recv_ctx->sds_rings[ring];
204 napi_enable(&sds_ring->napi);
205 netxen_nic_enable_int(sds_ring);
210 netxen_napi_disable(struct netxen_adapter *adapter)
213 struct nx_host_sds_ring *sds_ring;
214 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
216 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
217 sds_ring = &recv_ctx->sds_rings[ring];
218 napi_disable(&sds_ring->napi);
219 netxen_nic_disable_int(sds_ring);
220 synchronize_irq(sds_ring->irq);
224 static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
226 struct pci_dev *pdev = adapter->pdev;
227 uint64_t mask, cmask;
229 adapter->pci_using_dac = 0;
231 mask = DMA_BIT_MASK(32);
233 * Consistent DMA mask is set to 32 bit because it cannot be set to
234 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
235 * come off this pool.
237 cmask = DMA_BIT_MASK(32);
240 if (revision_id >= NX_P3_B0)
241 mask = DMA_BIT_MASK(39);
242 else if (revision_id == NX_P2_C1)
243 mask = DMA_BIT_MASK(35);
245 if (pci_set_dma_mask(pdev, mask) == 0 &&
246 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
247 adapter->pci_using_dac = 1;
254 /* Update addressable range if firmware supports it */
256 nx_update_dma_mask(struct netxen_adapter *adapter)
258 int change, shift, err;
259 uint64_t mask, old_mask;
260 struct pci_dev *pdev = adapter->pdev;
264 shift = NXRD32(adapter, CRB_DMA_SHIFT);
268 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
270 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
274 old_mask = pdev->dma_mask;
275 mask = (1ULL<<(32+shift)) - 1;
277 err = pci_set_dma_mask(pdev, mask);
279 return pci_set_dma_mask(pdev, old_mask);
285 static void netxen_check_options(struct netxen_adapter *adapter)
287 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
288 adapter->num_rxd = MAX_RCV_DESCRIPTORS_10G;
289 else if (adapter->ahw.port_type == NETXEN_NIC_GBE)
290 adapter->num_rxd = MAX_RCV_DESCRIPTORS_1G;
292 adapter->msix_supported = 0;
293 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
294 adapter->msix_supported = !!use_msi_x;
295 adapter->rss_supported = !!use_msi_x;
296 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
297 switch (adapter->ahw.board_type) {
298 case NETXEN_BRDTYPE_P2_SB31_10G:
299 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
300 adapter->msix_supported = !!use_msi_x;
301 adapter->rss_supported = !!use_msi_x;
308 adapter->num_txd = MAX_CMD_DESCRIPTORS_HOST;
309 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS;
310 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
316 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
320 if (first_boot == 0x55555555) {
321 /* This is the first boot after power up */
322 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
324 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
327 /* PCI bus master workaround */
328 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
329 if (!(first_boot & 0x4)) {
331 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
332 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
335 /* This is the first boot after power up */
336 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
337 if (first_boot != 0x80000f) {
338 /* clear the register for future unloads/loads */
339 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
343 /* Start P2 boot loader */
344 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
345 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
349 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
351 if (++timeout > 5000)
354 } while (val == NETXEN_BDINFO_MAGIC);
359 static void netxen_set_port_mode(struct netxen_adapter *adapter)
363 val = adapter->ahw.board_type;
364 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
365 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
366 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
367 data = NETXEN_PORT_MODE_802_3_AP;
368 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
369 } else if (port_mode == NETXEN_PORT_MODE_XG) {
370 data = NETXEN_PORT_MODE_XG;
371 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
372 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
373 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
374 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
375 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
376 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
377 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
379 data = NETXEN_PORT_MODE_AUTO_NEG;
380 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
383 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
384 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
385 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
386 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
387 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
389 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
393 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
398 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
400 pci_read_config_dword(pdev, pos, &control);
402 control |= PCI_MSIX_FLAGS_ENABLE;
405 pci_write_config_dword(pdev, pos, control);
409 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
413 for (i = 0; i < count; i++)
414 adapter->msix_entries[i].entry = i;
418 netxen_read_mac_addr(struct netxen_adapter *adapter)
423 struct net_device *netdev = adapter->netdev;
424 struct pci_dev *pdev = adapter->pdev;
426 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
427 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
430 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
434 p = (unsigned char *)&mac_addr;
435 for (i = 0; i < 6; i++)
436 netdev->dev_addr[i] = *(p + 5 - i);
438 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
440 /* set station address */
442 if (!is_valid_ether_addr(netdev->perm_addr))
443 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
448 int netxen_nic_set_mac(struct net_device *netdev, void *p)
450 struct netxen_adapter *adapter = netdev_priv(netdev);
451 struct sockaddr *addr = p;
453 if (!is_valid_ether_addr(addr->sa_data))
456 if (netif_running(netdev)) {
457 netif_device_detach(netdev);
458 netxen_napi_disable(adapter);
461 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
462 adapter->macaddr_set(adapter, addr->sa_data);
464 if (netif_running(netdev)) {
465 netif_device_attach(netdev);
466 netxen_napi_enable(adapter);
471 static void netxen_set_multicast_list(struct net_device *dev)
473 struct netxen_adapter *adapter = netdev_priv(dev);
475 adapter->set_multi(dev);
478 static const struct net_device_ops netxen_netdev_ops = {
479 .ndo_open = netxen_nic_open,
480 .ndo_stop = netxen_nic_close,
481 .ndo_start_xmit = netxen_nic_xmit_frame,
482 .ndo_get_stats = netxen_nic_get_stats,
483 .ndo_validate_addr = eth_validate_addr,
484 .ndo_set_multicast_list = netxen_set_multicast_list,
485 .ndo_set_mac_address = netxen_nic_set_mac,
486 .ndo_change_mtu = netxen_nic_change_mtu,
487 .ndo_tx_timeout = netxen_tx_timeout,
488 #ifdef CONFIG_NET_POLL_CONTROLLER
489 .ndo_poll_controller = netxen_nic_poll_controller,
494 netxen_setup_intr(struct netxen_adapter *adapter)
496 struct netxen_legacy_intr_set *legacy_intrp;
497 struct pci_dev *pdev = adapter->pdev;
500 if (adapter->rss_supported) {
501 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
502 MSIX_ENTRIES_PER_ADAPTER : 2;
506 adapter->max_sds_rings = 1;
508 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
510 if (adapter->ahw.revision_id >= NX_P3_B0)
511 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
513 legacy_intrp = &legacy_intr[0];
514 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
515 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
516 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
517 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
519 netxen_set_msix_bit(pdev, 0);
521 if (adapter->msix_supported) {
523 netxen_init_msix_entries(adapter, num_msix);
524 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
526 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
527 netxen_set_msix_bit(pdev, 1);
529 if (adapter->rss_supported)
530 adapter->max_sds_rings = num_msix;
532 dev_info(&pdev->dev, "using msi-x interrupts\n");
537 pci_disable_msix(pdev);
539 /* fall through for msi */
542 if (use_msi && !pci_enable_msi(pdev)) {
543 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
544 adapter->msi_tgt_status =
545 msi_tgt_status[adapter->ahw.pci_func];
546 dev_info(&pdev->dev, "using msi interrupts\n");
547 adapter->msix_entries[0].vector = pdev->irq;
551 dev_info(&pdev->dev, "using legacy interrupts\n");
552 adapter->msix_entries[0].vector = pdev->irq;
556 netxen_teardown_intr(struct netxen_adapter *adapter)
558 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
559 pci_disable_msix(adapter->pdev);
560 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
561 pci_disable_msi(adapter->pdev);
565 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
567 if (adapter->ahw.db_base != NULL)
568 iounmap(adapter->ahw.db_base);
569 if (adapter->ahw.pci_base0 != NULL)
570 iounmap(adapter->ahw.pci_base0);
571 if (adapter->ahw.pci_base1 != NULL)
572 iounmap(adapter->ahw.pci_base1);
573 if (adapter->ahw.pci_base2 != NULL)
574 iounmap(adapter->ahw.pci_base2);
578 netxen_setup_pci_map(struct netxen_adapter *adapter)
580 void __iomem *mem_ptr0 = NULL;
581 void __iomem *mem_ptr1 = NULL;
582 void __iomem *mem_ptr2 = NULL;
583 void __iomem *db_ptr = NULL;
585 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
587 struct pci_dev *pdev = adapter->pdev;
588 int pci_func = adapter->ahw.pci_func;
593 * Set the CRB window to invalid. If any register in window 0 is
594 * accessed it should set the window to 0 and then reset it to 1.
596 adapter->curr_window = 255;
597 adapter->ahw.qdr_sn_window = -1;
598 adapter->ahw.ddr_mn_window = -1;
600 /* remap phys address */
601 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
602 mem_len = pci_resource_len(pdev, 0);
605 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
606 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
607 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
608 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
609 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
610 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
611 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
613 /* 128 Meg of memory */
614 if (mem_len == NETXEN_PCI_128MB_SIZE) {
615 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
616 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
617 SECOND_PAGE_GROUP_SIZE);
618 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
619 THIRD_PAGE_GROUP_SIZE);
620 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
621 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
622 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
623 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
624 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
625 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
626 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
627 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
628 adapter->pci_write_immediate =
629 netxen_nic_pci_write_immediate_2M;
630 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
631 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
632 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
634 mem_ptr0 = pci_ioremap_bar(pdev, 0);
635 if (mem_ptr0 == NULL) {
636 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
641 adapter->ahw.ddr_mn_window = 0;
642 adapter->ahw.qdr_sn_window = 0;
644 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
646 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
648 adapter->ahw.ms_win_crb += (pci_func * 0x20);
650 adapter->ahw.ms_win_crb +=
651 0xA0 + ((pci_func - 4) * 0x10);
656 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
658 adapter->ahw.pci_base0 = mem_ptr0;
659 adapter->ahw.pci_len0 = pci_len0;
660 adapter->ahw.pci_base1 = mem_ptr1;
661 adapter->ahw.pci_base2 = mem_ptr2;
663 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
666 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
667 db_len = pci_resource_len(pdev, 4);
670 printk(KERN_ERR "%s: doorbell is disabled\n",
671 netxen_nic_driver_name);
676 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
678 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
679 netxen_nic_driver_name);
685 adapter->ahw.db_base = db_ptr;
686 adapter->ahw.db_len = db_len;
690 netxen_cleanup_pci_map(adapter);
695 netxen_start_firmware(struct netxen_adapter *adapter, int request_fw)
697 int val, err, first_boot;
698 struct pci_dev *pdev = adapter->pdev;
700 int first_driver = 0;
702 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
703 first_driver = (adapter->portnum == 0);
705 first_driver = (adapter->ahw.pci_func == 0);
710 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
712 err = netxen_check_hw_init(adapter, first_boot);
714 dev_err(&pdev->dev, "error in init HW init sequence\n");
719 netxen_request_firmware(adapter);
721 err = netxen_need_fw_reset(adapter);
725 if (first_boot != 0x55555555) {
726 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
727 netxen_pinit_from_rom(adapter, 0);
731 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
732 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
733 netxen_set_port_mode(adapter);
735 netxen_load_firmware(adapter);
737 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
739 /* Initialize multicast addr pool owners */
741 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
743 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
747 err = netxen_initialize_adapter_offload(adapter);
752 * Tell the hardware our version number.
754 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
755 | ((_NETXEN_NIC_LINUX_MINOR << 8))
756 | (_NETXEN_NIC_LINUX_SUBVERSION);
757 NXWR32(adapter, CRB_DRIVER_VERSION, val);
760 /* Handshake with the card before we register the devices. */
761 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
763 netxen_free_adapter_offload(adapter);
771 netxen_nic_request_irq(struct netxen_adapter *adapter)
773 irq_handler_t handler;
774 struct nx_host_sds_ring *sds_ring;
777 unsigned long flags = IRQF_SAMPLE_RANDOM;
778 struct net_device *netdev = adapter->netdev;
779 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
781 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
782 handler = netxen_msix_intr;
783 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
784 handler = netxen_msi_intr;
786 flags |= IRQF_SHARED;
787 handler = netxen_intr;
789 adapter->irq = netdev->irq;
791 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
792 sds_ring = &recv_ctx->sds_rings[ring];
793 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
794 err = request_irq(sds_ring->irq, handler,
795 flags, sds_ring->name, sds_ring);
804 netxen_nic_free_irq(struct netxen_adapter *adapter)
807 struct nx_host_sds_ring *sds_ring;
809 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
811 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
812 sds_ring = &recv_ctx->sds_rings[ring];
813 free_irq(sds_ring->irq, sds_ring);
818 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
822 err = adapter->init_port(adapter, adapter->physical_port);
824 printk(KERN_ERR "%s: Failed to initialize port %d\n",
825 netxen_nic_driver_name, adapter->portnum);
828 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
829 adapter->macaddr_set(adapter, netdev->dev_addr);
831 adapter->set_multi(netdev);
832 adapter->set_mtu(adapter, netdev->mtu);
834 adapter->ahw.linkup = 0;
836 netxen_napi_enable(adapter);
838 if (adapter->max_sds_rings > 1)
839 netxen_config_rss(adapter, 1);
841 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
842 netxen_linkevent_request(adapter, 1);
844 netxen_nic_set_link_parameters(adapter);
846 mod_timer(&adapter->watchdog_timer, jiffies);
852 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
854 netif_carrier_off(netdev);
855 netif_stop_queue(netdev);
857 if (adapter->stop_port)
858 adapter->stop_port(adapter);
860 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
861 netxen_p3_free_mac_list(adapter);
863 netxen_napi_disable(adapter);
865 netxen_release_tx_buffers(adapter);
867 FLUSH_SCHEDULED_WORK();
868 del_timer_sync(&adapter->watchdog_timer);
873 netxen_nic_attach(struct netxen_adapter *adapter)
875 struct net_device *netdev = adapter->netdev;
876 struct pci_dev *pdev = adapter->pdev;
878 struct nx_host_rds_ring *rds_ring;
879 struct nx_host_tx_ring *tx_ring;
881 err = netxen_init_firmware(adapter);
883 printk(KERN_ERR "Failed to init firmware\n");
887 if (adapter->fw_major < 4)
888 adapter->max_rds_rings = 3;
890 adapter->max_rds_rings = 2;
892 err = netxen_alloc_sw_resources(adapter);
894 printk(KERN_ERR "%s: Error in setting sw resources\n",
899 netxen_nic_clear_stats(adapter);
901 err = netxen_alloc_hw_resources(adapter);
903 printk(KERN_ERR "%s: Error in setting hw resources\n",
905 goto err_out_free_sw;
908 if (adapter->fw_major < 4) {
909 tx_ring = adapter->tx_ring;
910 tx_ring->crb_cmd_producer = crb_cmd_producer[adapter->portnum];
911 tx_ring->crb_cmd_consumer = crb_cmd_consumer[adapter->portnum];
913 tx_ring->producer = 0;
914 tx_ring->sw_consumer = 0;
916 netxen_nic_update_cmd_producer(adapter, tx_ring);
917 netxen_nic_update_cmd_consumer(adapter, tx_ring);
920 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
921 rds_ring = &adapter->recv_ctx.rds_rings[ring];
922 netxen_post_rx_buffers(adapter, ring, rds_ring);
925 err = netxen_nic_request_irq(adapter);
927 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
929 goto err_out_free_rxbuf;
932 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
936 netxen_release_rx_buffers(adapter);
937 netxen_free_hw_resources(adapter);
939 netxen_free_sw_resources(adapter);
944 netxen_nic_detach(struct netxen_adapter *adapter)
946 netxen_release_rx_buffers(adapter);
947 netxen_free_hw_resources(adapter);
948 netxen_nic_free_irq(adapter);
949 netxen_free_sw_resources(adapter);
955 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
957 struct net_device *netdev = NULL;
958 struct netxen_adapter *adapter = NULL;
960 int pci_func_id = PCI_FUNC(pdev->devfn);
963 if (pdev->class != 0x020000) {
964 printk(KERN_DEBUG "NetXen function %d, class %x will not "
965 "be enabled.\n",pci_func_id, pdev->class);
969 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
970 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
971 "will not be enabled.\n",
976 if ((err = pci_enable_device(pdev)))
979 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
981 goto err_out_disable_pdev;
984 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
985 goto err_out_disable_pdev;
987 pci_set_master(pdev);
989 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
991 printk(KERN_ERR"%s: Failed to allocate memory for the "
992 "device block.Check system memory resource"
993 " usage.\n", netxen_nic_driver_name);
994 goto err_out_free_res;
997 SET_NETDEV_DEV(netdev, &pdev->dev);
999 adapter = netdev_priv(netdev);
1000 adapter->netdev = netdev;
1001 adapter->pdev = pdev;
1002 adapter->ahw.pci_func = pci_func_id;
1004 revision_id = pdev->revision;
1005 adapter->ahw.revision_id = revision_id;
1007 err = nx_set_dma_mask(adapter, revision_id);
1009 goto err_out_free_netdev;
1011 rwlock_init(&adapter->adapter_lock);
1012 spin_lock_init(&adapter->tx_clean_lock);
1013 INIT_LIST_HEAD(&adapter->mac_list);
1015 err = netxen_setup_pci_map(adapter);
1017 goto err_out_free_netdev;
1019 /* This will be reset for mezz cards */
1020 adapter->portnum = pci_func_id;
1021 adapter->rx_csum = 1;
1022 adapter->mc_enabled = 0;
1023 if (NX_IS_REVISION_P3(revision_id))
1024 adapter->max_mc_count = 38;
1026 adapter->max_mc_count = 16;
1028 netdev->netdev_ops = &netxen_netdev_ops;
1029 netdev->watchdog_timeo = 2*HZ;
1031 netxen_nic_change_mtu(netdev, netdev->mtu);
1033 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1035 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1036 netdev->features |= (NETIF_F_GRO);
1037 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1039 if (NX_IS_REVISION_P3(revision_id)) {
1040 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1041 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1044 if (adapter->pci_using_dac) {
1045 netdev->features |= NETIF_F_HIGHDMA;
1046 netdev->vlan_features |= NETIF_F_HIGHDMA;
1049 if (netxen_nic_get_board_info(adapter) != 0) {
1050 printk("%s: Error getting board config info.\n",
1051 netxen_nic_driver_name);
1053 goto err_out_iounmap;
1056 netxen_initialize_adapter_ops(adapter);
1058 /* Mezz cards have PCI function 0,2,3 enabled */
1059 switch (adapter->ahw.board_type) {
1060 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1061 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1062 if (pci_func_id >= 2)
1063 adapter->portnum = pci_func_id - 2;
1069 err = netxen_start_firmware(adapter, 1);
1071 goto err_out_iounmap;
1073 nx_update_dma_mask(adapter);
1075 netxen_nic_get_firmware_info(adapter);
1078 * See if the firmware gave us a virtual-physical port mapping.
1080 adapter->physical_port = adapter->portnum;
1081 if (adapter->fw_major < 4) {
1082 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1083 if (i != 0x55555555)
1084 adapter->physical_port = i;
1087 netxen_check_options(adapter);
1089 netxen_setup_intr(adapter);
1091 netdev->irq = adapter->msix_entries[0].vector;
1093 if (netxen_napi_add(adapter, netdev))
1094 goto err_out_disable_msi;
1096 init_timer(&adapter->watchdog_timer);
1097 adapter->watchdog_timer.function = &netxen_watchdog;
1098 adapter->watchdog_timer.data = (unsigned long)adapter;
1099 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1100 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1102 err = netxen_read_mac_addr(adapter);
1104 dev_warn(&pdev->dev, "failed to read mac addr\n");
1106 netif_carrier_off(netdev);
1107 netif_stop_queue(netdev);
1109 if ((err = register_netdev(netdev))) {
1110 printk(KERN_ERR "%s: register_netdev failed port #%d"
1111 " aborting\n", netxen_nic_driver_name,
1114 goto err_out_disable_msi;
1117 pci_set_drvdata(pdev, adapter);
1119 switch (adapter->ahw.port_type) {
1120 case NETXEN_NIC_GBE:
1121 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1122 adapter->netdev->name);
1124 case NETXEN_NIC_XGBE:
1125 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1126 adapter->netdev->name);
1132 err_out_disable_msi:
1133 netxen_teardown_intr(adapter);
1135 netxen_free_adapter_offload(adapter);
1138 netxen_cleanup_pci_map(adapter);
1140 err_out_free_netdev:
1141 free_netdev(netdev);
1144 pci_release_regions(pdev);
1146 err_out_disable_pdev:
1147 pci_set_drvdata(pdev, NULL);
1148 pci_disable_device(pdev);
1152 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1154 struct netxen_adapter *adapter;
1155 struct net_device *netdev;
1157 adapter = pci_get_drvdata(pdev);
1158 if (adapter == NULL)
1161 netdev = adapter->netdev;
1163 unregister_netdev(netdev);
1165 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1166 netxen_nic_detach(adapter);
1169 if (adapter->portnum == 0)
1170 netxen_free_adapter_offload(adapter);
1172 netxen_teardown_intr(adapter);
1173 netxen_free_sds_rings(&adapter->recv_ctx);
1175 netxen_cleanup_pci_map(adapter);
1177 netxen_release_firmware(adapter);
1179 pci_release_regions(pdev);
1180 pci_disable_device(pdev);
1181 pci_set_drvdata(pdev, NULL);
1183 free_netdev(netdev);
1188 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1191 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1192 struct net_device *netdev = adapter->netdev;
1194 netif_device_detach(netdev);
1196 if (netif_running(netdev))
1197 netxen_nic_down(adapter, netdev);
1199 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1200 netxen_nic_detach(adapter);
1202 pci_save_state(pdev);
1204 if (netxen_nic_wol_supported(adapter)) {
1205 pci_enable_wake(pdev, PCI_D3cold, 1);
1206 pci_enable_wake(pdev, PCI_D3hot, 1);
1209 pci_disable_device(pdev);
1210 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1216 netxen_nic_resume(struct pci_dev *pdev)
1218 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1219 struct net_device *netdev = adapter->netdev;
1222 pci_set_power_state(pdev, PCI_D0);
1223 pci_restore_state(pdev);
1225 err = pci_enable_device(pdev);
1229 adapter->curr_window = 255;
1231 err = netxen_start_firmware(adapter, 0);
1233 dev_err(&pdev->dev, "failed to start firmware\n");
1237 if (netif_running(netdev)) {
1238 err = netxen_nic_attach(adapter);
1242 err = netxen_nic_up(adapter, netdev);
1246 netif_device_attach(netdev);
1253 static int netxen_nic_open(struct net_device *netdev)
1255 struct netxen_adapter *adapter = netdev_priv(netdev);
1258 if (adapter->driver_mismatch)
1261 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1262 err = netxen_nic_attach(adapter);
1267 err = netxen_nic_up(adapter, netdev);
1271 netif_start_queue(netdev);
1276 netxen_nic_detach(adapter);
1281 * netxen_nic_close - Disables a network interface entry point
1283 static int netxen_nic_close(struct net_device *netdev)
1285 struct netxen_adapter *adapter = netdev_priv(netdev);
1287 netxen_nic_down(adapter, netdev);
1291 static bool netxen_tso_check(struct net_device *netdev,
1292 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1295 u8 opcode = TX_ETHER_PKT;
1296 __be16 protocol = skb->protocol;
1299 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1300 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1301 protocol = vh->h_vlan_encapsulated_proto;
1302 flags = FLAGS_VLAN_TAGGED;
1305 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1306 skb_shinfo(skb)->gso_size > 0) {
1308 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1309 desc->total_hdr_length =
1310 skb_transport_offset(skb) + tcp_hdrlen(skb);
1312 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1313 TX_TCP_LSO6 : TX_TCP_LSO;
1316 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1319 if (protocol == cpu_to_be16(ETH_P_IP)) {
1320 l4proto = ip_hdr(skb)->protocol;
1322 if (l4proto == IPPROTO_TCP)
1323 opcode = TX_TCP_PKT;
1324 else if(l4proto == IPPROTO_UDP)
1325 opcode = TX_UDP_PKT;
1326 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1327 l4proto = ipv6_hdr(skb)->nexthdr;
1329 if (l4proto == IPPROTO_TCP)
1330 opcode = TX_TCPV6_PKT;
1331 else if(l4proto == IPPROTO_UDP)
1332 opcode = TX_UDPV6_PKT;
1335 desc->tcp_hdr_offset = skb_transport_offset(skb);
1336 desc->ip_hdr_offset = skb_network_offset(skb);
1337 netxen_set_tx_flags_opcode(desc, flags, opcode);
1342 netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1343 struct netxen_cmd_buffer *pbuf, int last)
1346 struct netxen_skb_frag *buffrag;
1348 buffrag = &pbuf->frag_array[0];
1349 pci_unmap_single(pdev, buffrag->dma,
1350 buffrag->length, PCI_DMA_TODEVICE);
1352 for (k = 1; k < last; k++) {
1353 buffrag = &pbuf->frag_array[k];
1354 pci_unmap_page(pdev, buffrag->dma,
1355 buffrag->length, PCI_DMA_TODEVICE);
1360 netxen_clear_cmddesc(u64 *desc)
1363 for (i = 0; i < 8; i++)
1368 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1370 struct netxen_adapter *adapter = netdev_priv(netdev);
1371 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1372 unsigned int first_seg_len = skb->len - skb->data_len;
1373 struct netxen_cmd_buffer *pbuf;
1374 struct netxen_skb_frag *buffrag;
1375 struct cmd_desc_type0 *hwdesc;
1376 struct pci_dev *pdev = adapter->pdev;
1377 dma_addr_t temp_dma;
1381 int frag_count, no_of_desc;
1382 u32 num_txd = tx_ring->num_desc;
1383 bool is_tso = false;
1385 frag_count = skb_shinfo(skb)->nr_frags + 1;
1387 /* 4 fragments per cmd des */
1388 no_of_desc = (frag_count + 3) >> 2;
1390 if (unlikely(no_of_desc + 2) > netxen_tx_avail(tx_ring)) {
1391 netif_stop_queue(netdev);
1392 return NETDEV_TX_BUSY;
1395 producer = tx_ring->producer;
1397 hwdesc = &tx_ring->desc_head[producer];
1398 netxen_clear_cmddesc((u64 *)hwdesc);
1399 pbuf = &tx_ring->cmd_buf_arr[producer];
1401 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1404 pbuf->frag_count = frag_count;
1405 buffrag = &pbuf->frag_array[0];
1406 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
1408 if (pci_dma_mapping_error(pdev, temp_dma))
1411 buffrag->dma = temp_dma;
1412 buffrag->length = first_seg_len;
1413 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1414 netxen_set_tx_port(hwdesc, adapter->portnum);
1416 hwdesc->buffer_length[0] = cpu_to_le16(first_seg_len);
1417 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1419 for (i = 1, k = 1; i < frag_count; i++, k++) {
1420 struct skb_frag_struct *frag;
1422 unsigned long offset;
1424 /* move to next desc. if there is a need */
1425 if ((i & 0x3) == 0) {
1427 producer = get_next_index(producer, num_txd);
1428 hwdesc = &tx_ring->desc_head[producer];
1429 netxen_clear_cmddesc((u64 *)hwdesc);
1430 pbuf = &tx_ring->cmd_buf_arr[producer];
1433 frag = &skb_shinfo(skb)->frags[i - 1];
1435 offset = frag->page_offset;
1438 temp_dma = pci_map_page(pdev, frag->page, offset,
1439 len, PCI_DMA_TODEVICE);
1440 if (pci_dma_mapping_error(pdev, temp_dma)) {
1441 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1446 buffrag->dma = temp_dma;
1447 buffrag->length = temp_len;
1449 hwdesc->buffer_length[k] = cpu_to_le16(temp_len);
1452 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1455 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1458 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1461 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1466 producer = get_next_index(producer, num_txd);
1468 /* For LSO, we need to copy the MAC/IP/TCP headers into
1469 * the descriptor ring
1472 int hdr_len, first_hdr_len, more_hdr;
1473 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1474 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1475 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1478 first_hdr_len = hdr_len;
1481 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1482 hwdesc = &tx_ring->desc_head[producer];
1483 pbuf = &tx_ring->cmd_buf_arr[producer];
1486 /* copy the first 64 bytes */
1487 memcpy(((void *)hwdesc) + 2,
1488 (void *)(skb->data), first_hdr_len);
1489 producer = get_next_index(producer, num_txd);
1492 hwdesc = &tx_ring->desc_head[producer];
1493 pbuf = &tx_ring->cmd_buf_arr[producer];
1495 /* copy the next 64 bytes - should be enough except
1496 * for pathological case
1498 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1502 producer = get_next_index(producer, num_txd);
1506 tx_ring->producer = producer;
1507 adapter->stats.txbytes += skb->len;
1509 netxen_nic_update_cmd_producer(adapter, tx_ring);
1511 adapter->stats.xmitcalled++;
1513 return NETDEV_TX_OK;
1516 adapter->stats.txdropped++;
1517 dev_kfree_skb_any(skb);
1518 return NETDEV_TX_OK;
1521 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1523 struct net_device *netdev = adapter->netdev;
1524 uint32_t temp, temp_state, temp_val;
1527 temp = NXRD32(adapter, CRB_TEMP_STATE);
1529 temp_state = nx_get_temp_state(temp);
1530 temp_val = nx_get_temp_val(temp);
1532 if (temp_state == NX_TEMP_PANIC) {
1534 "%s: Device temperature %d degrees C exceeds"
1535 " maximum allowed. Hardware has been shut down.\n",
1536 netxen_nic_driver_name, temp_val);
1538 netif_carrier_off(netdev);
1539 netif_stop_queue(netdev);
1541 } else if (temp_state == NX_TEMP_WARN) {
1542 if (adapter->temp == NX_TEMP_NORMAL) {
1544 "%s: Device temperature %d degrees C "
1545 "exceeds operating range."
1546 " Immediate action needed.\n",
1547 netxen_nic_driver_name, temp_val);
1550 if (adapter->temp == NX_TEMP_WARN) {
1552 "%s: Device temperature is now %d degrees C"
1553 " in normal range.\n", netxen_nic_driver_name,
1557 adapter->temp = temp_state;
1561 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
1563 struct net_device *netdev = adapter->netdev;
1565 if (adapter->ahw.linkup && !linkup) {
1566 printk(KERN_INFO "%s: %s NIC Link is down\n",
1567 netxen_nic_driver_name, netdev->name);
1568 adapter->ahw.linkup = 0;
1569 if (netif_running(netdev)) {
1570 netif_carrier_off(netdev);
1571 netif_stop_queue(netdev);
1574 if (!adapter->has_link_events)
1575 netxen_nic_set_link_parameters(adapter);
1577 } else if (!adapter->ahw.linkup && linkup) {
1578 printk(KERN_INFO "%s: %s NIC Link is up\n",
1579 netxen_nic_driver_name, netdev->name);
1580 adapter->ahw.linkup = 1;
1581 if (netif_running(netdev)) {
1582 netif_carrier_on(netdev);
1583 netif_wake_queue(netdev);
1586 if (!adapter->has_link_events)
1587 netxen_nic_set_link_parameters(adapter);
1591 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1593 u32 val, port, linkup;
1595 port = adapter->physical_port;
1597 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1598 val = NXRD32(adapter, CRB_XG_STATE_P3);
1599 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1600 linkup = (val == XG_LINK_UP_P3);
1602 val = NXRD32(adapter, CRB_XG_STATE);
1603 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
1604 linkup = (val >> port) & 1;
1606 val = (val >> port*8) & 0xff;
1607 linkup = (val == XG_LINK_UP);
1611 netxen_advert_link_change(adapter, linkup);
1614 static void netxen_watchdog(unsigned long v)
1616 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1618 SCHEDULE_WORK(&adapter->watchdog_task);
1621 void netxen_watchdog_task(struct work_struct *work)
1623 struct netxen_adapter *adapter =
1624 container_of(work, struct netxen_adapter, watchdog_task);
1626 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1629 if (!adapter->has_link_events)
1630 netxen_nic_handle_phy_intr(adapter);
1632 if (netif_running(adapter->netdev))
1633 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1636 static void netxen_tx_timeout(struct net_device *netdev)
1638 struct netxen_adapter *adapter = (struct netxen_adapter *)
1639 netdev_priv(netdev);
1640 SCHEDULE_WORK(&adapter->tx_timeout_task);
1643 static void netxen_tx_timeout_task(struct work_struct *work)
1645 struct netxen_adapter *adapter =
1646 container_of(work, struct netxen_adapter, tx_timeout_task);
1648 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1649 netxen_nic_driver_name, adapter->netdev->name);
1651 netxen_napi_disable(adapter);
1653 adapter->netdev->trans_start = jiffies;
1655 netxen_napi_enable(adapter);
1656 netif_wake_queue(adapter->netdev);
1659 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1661 struct netxen_adapter *adapter = netdev_priv(netdev);
1662 struct net_device_stats *stats = &adapter->net_stats;
1664 memset(stats, 0, sizeof(*stats));
1666 stats->rx_packets = adapter->stats.no_rcv;
1667 stats->tx_packets = adapter->stats.xmitfinished;
1668 stats->rx_bytes = adapter->stats.rxbytes;
1669 stats->tx_bytes = adapter->stats.txbytes;
1670 stats->rx_dropped = adapter->stats.rxdropped;
1671 stats->tx_dropped = adapter->stats.txdropped;
1676 static irqreturn_t netxen_intr(int irq, void *data)
1678 struct nx_host_sds_ring *sds_ring = data;
1679 struct netxen_adapter *adapter = sds_ring->adapter;
1682 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1684 if (!(status & adapter->legacy_intr.int_vec_bit))
1687 if (adapter->ahw.revision_id >= NX_P3_B1) {
1688 /* check interrupt state machine, to be sure */
1689 status = adapter->pci_read_immediate(adapter,
1691 if (!ISR_LEGACY_INT_TRIGGERED(status))
1695 unsigned long our_int = 0;
1697 our_int = NXRD32(adapter, CRB_INT_VECTOR);
1699 /* not our interrupt */
1700 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
1703 /* claim interrupt */
1704 NXWR32(adapter, CRB_INT_VECTOR, (our_int & 0xffffffff));
1707 /* clear interrupt */
1708 if (adapter->fw_major < 4)
1709 netxen_nic_disable_int(sds_ring);
1711 adapter->pci_write_immediate(adapter,
1712 adapter->legacy_intr.tgt_status_reg,
1714 /* read twice to ensure write is flushed */
1715 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1716 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1718 napi_schedule(&sds_ring->napi);
1723 static irqreturn_t netxen_msi_intr(int irq, void *data)
1725 struct nx_host_sds_ring *sds_ring = data;
1726 struct netxen_adapter *adapter = sds_ring->adapter;
1728 /* clear interrupt */
1729 adapter->pci_write_immediate(adapter,
1730 adapter->msi_tgt_status, 0xffffffff);
1732 napi_schedule(&sds_ring->napi);
1736 static irqreturn_t netxen_msix_intr(int irq, void *data)
1738 struct nx_host_sds_ring *sds_ring = data;
1740 napi_schedule(&sds_ring->napi);
1744 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1746 struct nx_host_sds_ring *sds_ring =
1747 container_of(napi, struct nx_host_sds_ring, napi);
1749 struct netxen_adapter *adapter = sds_ring->adapter;
1754 tx_complete = netxen_process_cmd_ring(adapter);
1756 work_done = netxen_process_rcv_ring(sds_ring, budget);
1758 if ((work_done < budget) && tx_complete) {
1759 napi_complete(&sds_ring->napi);
1760 netxen_nic_enable_int(sds_ring);
1766 #ifdef CONFIG_NET_POLL_CONTROLLER
1767 static void netxen_nic_poll_controller(struct net_device *netdev)
1769 struct netxen_adapter *adapter = netdev_priv(netdev);
1770 disable_irq(adapter->irq);
1771 netxen_intr(adapter->irq, adapter);
1772 enable_irq(adapter->irq);
1776 static struct pci_driver netxen_driver = {
1777 .name = netxen_nic_driver_name,
1778 .id_table = netxen_pci_tbl,
1779 .probe = netxen_nic_probe,
1780 .remove = __devexit_p(netxen_nic_remove),
1782 .suspend = netxen_nic_suspend,
1783 .resume = netxen_nic_resume
1787 /* Driver Registration on NetXen card */
1789 static int __init netxen_init_module(void)
1791 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
1793 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1796 return pci_register_driver(&netxen_driver);
1799 module_init(netxen_init_module);
1801 static void __exit netxen_exit_module(void)
1803 pci_unregister_driver(&netxen_driver);
1804 destroy_workqueue(netxen_workq);
1807 module_exit(netxen_exit_module);