2 * Copyright (C) 2003 - 2006 NetXen, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
30 * Main source file for NetXen NIC Driver on Linux
34 #include <linux/vmalloc.h>
35 #include <linux/highmem.h>
36 #include "netxen_nic_hw.h"
38 #include "netxen_nic.h"
39 #include "netxen_nic_phan_reg.h"
41 #include <linux/dma-mapping.h>
44 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
48 char netxen_nic_driver_name[] = "netxen_nic";
49 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
50 NETXEN_NIC_LINUX_VERSIONID;
52 #define NETXEN_NETDEV_WEIGHT 120
53 #define NETXEN_ADAPTER_UP_MAGIC 777
54 #define NETXEN_NIC_PEG_TUNE 0
56 /* Local functions to NetXen NIC driver */
57 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
58 const struct pci_device_id *ent);
59 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
60 static int netxen_nic_open(struct net_device *netdev);
61 static int netxen_nic_close(struct net_device *netdev);
62 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
63 static void netxen_tx_timeout(struct net_device *netdev);
64 static void netxen_tx_timeout_task(struct work_struct *work);
65 static void netxen_watchdog(unsigned long);
66 static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
67 static int netxen_nic_poll(struct napi_struct *napi, int budget);
68 #ifdef CONFIG_NET_POLL_CONTROLLER
69 static void netxen_nic_poll_controller(struct net_device *netdev);
71 static irqreturn_t netxen_intr(int irq, void *data);
73 int physical_port[] = {0, 1, 2, 3};
75 /* PCI Device ID Table */
76 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
77 {PCI_DEVICE(0x4040, 0x0001)},
78 {PCI_DEVICE(0x4040, 0x0002)},
79 {PCI_DEVICE(0x4040, 0x0003)},
80 {PCI_DEVICE(0x4040, 0x0004)},
81 {PCI_DEVICE(0x4040, 0x0005)},
82 {PCI_DEVICE(0x4040, 0x0024)},
83 {PCI_DEVICE(0x4040, 0x0025)},
87 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
89 struct workqueue_struct *netxen_workq;
90 static void netxen_watchdog(unsigned long);
92 static inline void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
93 uint32_t crb_producer)
95 switch (adapter->portnum) {
97 writel(crb_producer, NETXEN_CRB_NORMALIZE
98 (adapter, CRB_CMD_PRODUCER_OFFSET));
101 writel(crb_producer, NETXEN_CRB_NORMALIZE
102 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
105 writel(crb_producer, NETXEN_CRB_NORMALIZE
106 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
109 writel(crb_producer, NETXEN_CRB_NORMALIZE
110 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
113 printk(KERN_WARNING "We tried to update "
114 "CRB_CMD_PRODUCER_OFFSET for invalid "
115 "PCI function id %d\n",
121 static inline void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
124 switch (adapter->portnum) {
126 writel(crb_consumer, NETXEN_CRB_NORMALIZE
127 (adapter, CRB_CMD_CONSUMER_OFFSET));
130 writel(crb_consumer, NETXEN_CRB_NORMALIZE
131 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
134 writel(crb_consumer, NETXEN_CRB_NORMALIZE
135 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
138 writel(crb_consumer, NETXEN_CRB_NORMALIZE
139 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
142 printk(KERN_WARNING "We tried to update "
143 "CRB_CMD_PRODUCER_OFFSET for invalid "
144 "PCI function id %d\n",
150 #define ADAPTER_LIST_SIZE 12
151 int netxen_cards_found;
153 static void netxen_nic_disable_int(struct netxen_adapter *adapter)
155 uint32_t mask = 0x7ff;
158 DPRINTK(1, INFO, "Entered ISR Disable \n");
160 switch (adapter->portnum) {
162 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
165 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
168 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
171 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
175 if (adapter->intr_scheme != -1 &&
176 adapter->intr_scheme != INTR_SCHEME_PERPORT)
177 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
179 /* Window = 0 or 1 */
180 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
183 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
184 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
191 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
192 netxen_nic_driver_name);
196 DPRINTK(1, INFO, "Done with Disable Int\n");
199 static void netxen_nic_enable_int(struct netxen_adapter *adapter)
203 DPRINTK(1, INFO, "Entered ISR Enable \n");
205 if (adapter->intr_scheme != -1 &&
206 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
207 switch (adapter->ahw.board_type) {
211 case NETXEN_NIC_XGBE:
219 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
222 switch (adapter->portnum) {
224 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
227 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
230 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
233 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
237 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
239 if (adapter->intr_scheme != -1 &&
240 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
241 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
244 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
247 DPRINTK(1, INFO, "Done with enable Int\n");
253 * The Linux system will invoke this after identifying the vendor ID and
254 * device Id in the pci_tbl supported by this module.
256 * A quad port card has one operational PCI config space, (function 0),
257 * which is used to access all four ports.
259 * This routine will initialize the adapter, and setup the global parameters
260 * along with the port's specific structure.
263 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
265 struct net_device *netdev = NULL;
266 struct netxen_adapter *adapter = NULL;
267 void __iomem *mem_ptr0 = NULL;
268 void __iomem *mem_ptr1 = NULL;
269 void __iomem *mem_ptr2 = NULL;
270 unsigned long first_page_group_end;
271 unsigned long first_page_group_start;
274 u8 __iomem *db_ptr = NULL;
275 unsigned long mem_base, mem_len, db_base, db_len;
276 int pci_using_dac, i = 0, err;
278 struct netxen_recv_context *recv_ctx = NULL;
279 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
280 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
281 u64 mac_addr[FLASH_NUM_PORTS + 1];
284 int pci_func_id = PCI_FUNC(pdev->devfn);
285 DECLARE_MAC_BUF(mac);
287 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
289 if (pdev->class != 0x020000) {
290 printk(KERN_ERR"NetXen function %d, class %x will not"
291 "be enabled.\n",pci_func_id, pdev->class);
294 if ((err = pci_enable_device(pdev)))
296 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
298 goto err_out_disable_pdev;
301 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
302 goto err_out_disable_pdev;
304 pci_set_master(pdev);
305 if (pdev->revision == NX_P2_C1 &&
306 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
307 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
310 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
311 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
312 goto err_out_free_res;
318 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
320 printk(KERN_ERR"%s: Failed to allocate memory for the "
321 "device block.Check system memory resource"
322 " usage.\n", netxen_nic_driver_name);
323 goto err_out_free_res;
326 SET_NETDEV_DEV(netdev, &pdev->dev);
328 adapter = netdev->priv;
330 adapter->ahw.pdev = pdev;
331 adapter->ahw.pci_func = pci_func_id;
332 spin_lock_init(&adapter->tx_lock);
334 /* remap phys address */
335 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
336 mem_len = pci_resource_len(pdev, 0);
338 /* 128 Meg of memory */
339 if (mem_len == NETXEN_PCI_128MB_SIZE) {
340 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
341 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
342 SECOND_PAGE_GROUP_SIZE);
343 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
344 THIRD_PAGE_GROUP_SIZE);
345 first_page_group_start = FIRST_PAGE_GROUP_START;
346 first_page_group_end = FIRST_PAGE_GROUP_END;
347 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
348 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
349 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
350 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
351 first_page_group_start = 0;
352 first_page_group_end = 0;
355 goto err_out_free_netdev;
358 if (((mem_ptr0 == 0UL) && (mem_len == NETXEN_PCI_128MB_SIZE)) ||
359 (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
361 "Cannot remap adapter memory aborting.:"
362 "0 -> %p, 1 -> %p, 2 -> %p\n",
363 mem_ptr0, mem_ptr1, mem_ptr2);
366 goto err_out_iounmap;
368 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
369 db_len = pci_resource_len(pdev, 4);
372 printk(KERN_ERR "%s: doorbell is disabled\n",
373 netxen_nic_driver_name);
375 goto err_out_iounmap;
377 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
380 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
382 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
383 netxen_nic_driver_name);
385 goto err_out_iounmap;
387 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
389 adapter->ahw.pci_base0 = mem_ptr0;
390 adapter->ahw.first_page_group_start = first_page_group_start;
391 adapter->ahw.first_page_group_end = first_page_group_end;
392 adapter->ahw.pci_base1 = mem_ptr1;
393 adapter->ahw.pci_base2 = mem_ptr2;
394 adapter->ahw.db_base = db_ptr;
395 adapter->ahw.db_len = db_len;
397 adapter->netdev = netdev;
398 adapter->pdev = pdev;
400 netif_napi_add(netdev, &adapter->napi,
401 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
403 /* this will be read from FW later */
404 adapter->intr_scheme = -1;
406 /* This will be reset for mezz cards */
407 adapter->portnum = pci_func_id;
408 adapter->status &= ~NETXEN_NETDEV_STATUS;
409 adapter->rx_csum = 1;
411 netdev->open = netxen_nic_open;
412 netdev->stop = netxen_nic_close;
413 netdev->hard_start_xmit = netxen_nic_xmit_frame;
414 netdev->get_stats = netxen_nic_get_stats;
415 netdev->set_multicast_list = netxen_nic_set_multi;
416 netdev->set_mac_address = netxen_nic_set_mac;
417 netdev->change_mtu = netxen_nic_change_mtu;
418 netdev->tx_timeout = netxen_tx_timeout;
419 netdev->watchdog_timeo = HZ;
421 netxen_nic_change_mtu(netdev, netdev->mtu);
423 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
424 #ifdef CONFIG_NET_POLL_CONTROLLER
425 netdev->poll_controller = netxen_nic_poll_controller;
427 /* ScatterGather support */
428 netdev->features = NETIF_F_SG;
429 netdev->features |= NETIF_F_IP_CSUM;
430 netdev->features |= NETIF_F_TSO;
433 netdev->features |= NETIF_F_HIGHDMA;
435 if (pci_enable_msi(pdev))
436 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
438 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
440 netdev->irq = pdev->irq;
441 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
444 * Set the CRB window to invalid. If any register in window 0 is
445 * accessed it should set the window to 0 and then reset it to 1.
447 adapter->curr_window = 255;
449 /* initialize the adapter */
450 netxen_initialize_adapter_hw(adapter);
453 * Adapter in our case is quad port so initialize it before
454 * initializing the ports
457 netxen_initialize_adapter_ops(adapter);
459 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
460 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
461 (adapter->ahw.boardcfg.board_type ==
462 NETXEN_BRDTYPE_P2_SB31_2G))
463 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
465 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
466 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
467 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
469 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
470 if (cmd_buf_arr == NULL) {
472 "%s: Could not allocate cmd_buf_arr memory:%d\n",
473 netxen_nic_driver_name, (int)TX_RINGSIZE);
475 goto err_out_free_adapter;
477 memset(cmd_buf_arr, 0, TX_RINGSIZE);
478 adapter->cmd_buf_arr = cmd_buf_arr;
480 for (i = 0; i < MAX_RCV_CTX; ++i) {
481 recv_ctx = &adapter->recv_ctx[i];
482 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
483 rcv_desc = &recv_ctx->rcv_desc[ring];
484 switch (RCV_DESC_TYPE(ring)) {
485 case RCV_DESC_NORMAL:
486 rcv_desc->max_rx_desc_count =
487 adapter->max_rx_desc_count;
488 rcv_desc->flags = RCV_DESC_NORMAL;
489 rcv_desc->dma_size = RX_DMA_MAP_LEN;
490 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
494 rcv_desc->max_rx_desc_count =
495 adapter->max_jumbo_rx_desc_count;
496 rcv_desc->flags = RCV_DESC_JUMBO;
497 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
498 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
502 rcv_desc->max_rx_desc_count =
503 adapter->max_lro_rx_desc_count;
504 rcv_desc->flags = RCV_DESC_LRO;
505 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
506 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
510 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
511 vmalloc(RCV_BUFFSIZE);
513 if (rcv_desc->rx_buf_arr == NULL) {
514 printk(KERN_ERR "%s: Could not allocate"
515 "rcv_desc->rx_buf_arr memory:%d\n",
516 netxen_nic_driver_name,
519 goto err_out_free_rx_buffer;
521 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
526 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
528 /* Mezz cards have PCI function 0,2,3 enabled */
529 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB31_10G_IMEZ)
530 && (pci_func_id >= 2))
531 adapter->portnum = pci_func_id - 2;
534 if(adapter->portnum == 0) {
535 netxen_pinit_from_rom(adapter, 0);
537 netxen_load_firmware(adapter);
541 init_timer(&adapter->watchdog_timer);
542 adapter->ahw.xg_linkup = 0;
543 adapter->watchdog_timer.function = &netxen_watchdog;
544 adapter->watchdog_timer.data = (unsigned long)adapter;
545 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
546 adapter->ahw.pdev = pdev;
547 adapter->proc_cmd_buf_counter = 0;
548 adapter->ahw.revision_id = pdev->revision;
550 /* make sure Window == 1 */
551 netxen_nic_pci_change_crbwindow(adapter, 1);
553 netxen_nic_update_cmd_producer(adapter, 0);
554 netxen_nic_update_cmd_consumer(adapter, 0);
555 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
557 if (netxen_is_flash_supported(adapter) == 0 &&
558 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
564 unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum];
565 netdev->dev_addr[0] = *(p + 5);
566 netdev->dev_addr[1] = *(p + 4);
567 netdev->dev_addr[2] = *(p + 3);
568 netdev->dev_addr[3] = *(p + 2);
569 netdev->dev_addr[4] = *(p + 1);
570 netdev->dev_addr[5] = *(p + 0);
572 memcpy(netdev->perm_addr, netdev->dev_addr,
574 if (!is_valid_ether_addr(netdev->perm_addr)) {
575 printk(KERN_ERR "%s: Bad MAC address %s.\n",
576 netxen_nic_driver_name,
577 print_mac(mac, netdev->dev_addr));
579 if (adapter->macaddr_set)
580 adapter->macaddr_set(adapter,
585 if (adapter->portnum == 0) {
586 err = netxen_initialize_adapter_offload(adapter);
588 goto err_out_free_rx_buffer;
589 val = readl(NETXEN_CRB_NORMALIZE(adapter,
590 NETXEN_CAM_RAM(0x1fc)));
591 if (val == 0x55555555) {
592 /* This is the first boot after power up */
593 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
596 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
597 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
599 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
600 netxen_nic_driver_name);
602 val = readl(NETXEN_CRB_NORMALIZE(adapter,
603 NETXEN_ROMUSB_GLB_SW_RESET));
604 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
605 if (val != 0x80000f) {
606 /* clear the register for future unloads/loads */
607 writel(0, NETXEN_CRB_NORMALIZE(adapter,
608 NETXEN_CAM_RAM(0x1fc)));
609 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
611 goto err_out_free_dev;
615 /* clear the register for future unloads/loads */
616 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
617 printk(KERN_INFO "State: 0x%0x\n",
618 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
621 * Tell the hardware our version number.
623 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
624 | ((_NETXEN_NIC_LINUX_MINOR << 8))
625 | (_NETXEN_NIC_LINUX_SUBVERSION);
626 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
628 /* Unlock the HW, prompting the boot sequence */
630 NETXEN_CRB_NORMALIZE(adapter,
631 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
632 /* Handshake with the card before we register the devices. */
633 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
637 * See if the firmware gave us a virtual-physical port mapping.
639 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
641 physical_port[adapter->portnum] = i;
643 netif_carrier_off(netdev);
644 netif_stop_queue(netdev);
646 if ((err = register_netdev(netdev))) {
647 printk(KERN_ERR "%s: register_netdev failed port #%d"
648 " aborting\n", netxen_nic_driver_name,
651 goto err_out_free_dev;
654 pci_set_drvdata(pdev, adapter);
656 switch (adapter->ahw.board_type) {
658 printk(KERN_INFO "%s: QUAD GbE board initialized\n",
659 netxen_nic_driver_name);
662 case NETXEN_NIC_XGBE:
663 printk(KERN_INFO "%s: XGbE board initialized\n",
664 netxen_nic_driver_name);
668 adapter->driver_mismatch = 0;
673 if (adapter->portnum == 0)
674 netxen_free_adapter_offload(adapter);
676 err_out_free_rx_buffer:
677 for (i = 0; i < MAX_RCV_CTX; ++i) {
678 recv_ctx = &adapter->recv_ctx[i];
679 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
680 rcv_desc = &recv_ctx->rcv_desc[ring];
681 if (rcv_desc->rx_buf_arr != NULL) {
682 vfree(rcv_desc->rx_buf_arr);
683 rcv_desc->rx_buf_arr = NULL;
689 err_out_free_adapter:
690 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
691 pci_disable_msi(pdev);
693 pci_set_drvdata(pdev, NULL);
710 pci_release_regions(pdev);
712 err_out_disable_pdev:
713 pci_disable_device(pdev);
717 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
719 struct netxen_adapter *adapter;
720 struct net_device *netdev;
721 struct netxen_rx_buffer *buffer;
722 struct netxen_recv_context *recv_ctx;
723 struct netxen_rcv_desc_ctx *rcv_desc;
725 static int init_firmware_done = 0;
727 adapter = pci_get_drvdata(pdev);
731 netdev = adapter->netdev;
733 unregister_netdev(netdev);
735 if (adapter->stop_port)
736 adapter->stop_port(adapter);
738 netxen_nic_disable_int(adapter);
740 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
741 init_firmware_done++;
742 netxen_free_hw_resources(adapter);
745 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
746 recv_ctx = &adapter->recv_ctx[ctxid];
747 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
748 rcv_desc = &recv_ctx->rcv_desc[ring];
749 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
750 buffer = &(rcv_desc->rx_buf_arr[i]);
751 if (buffer->state == NETXEN_BUFFER_FREE)
753 pci_unmap_single(pdev, buffer->dma,
756 if (buffer->skb != NULL)
757 dev_kfree_skb_any(buffer->skb);
759 vfree(rcv_desc->rx_buf_arr);
763 vfree(adapter->cmd_buf_arr);
765 if (adapter->portnum == 0) {
766 if (init_firmware_done) {
769 if (dma_watchdog_shutdown_request(adapter) == 1)
772 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
777 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
780 /* clear the register for future unloads/loads */
781 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
782 printk(KERN_INFO "State: 0x%0x\n",
783 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
785 /* leave the hw in the same state as reboot */
786 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
787 netxen_pinit_from_rom(adapter, 0);
789 netxen_load_firmware(adapter);
790 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
793 /* clear the register for future unloads/loads */
794 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
795 printk(KERN_INFO "State: 0x%0x\n",
796 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
800 if (dma_watchdog_shutdown_request(adapter) == 1)
803 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
808 netxen_free_adapter_offload(adapter);
810 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
816 free_irq(adapter->irq, adapter);
818 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
819 pci_disable_msi(pdev);
821 iounmap(adapter->ahw.db_base);
822 iounmap(adapter->ahw.pci_base0);
823 iounmap(adapter->ahw.pci_base1);
824 iounmap(adapter->ahw.pci_base2);
826 pci_release_regions(pdev);
827 pci_disable_device(pdev);
828 pci_set_drvdata(pdev, NULL);
834 * Called when a network interface is made active
835 * @returns 0 on success, negative value on failure
837 static int netxen_nic_open(struct net_device *netdev)
839 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
843 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
844 err = netxen_init_firmware(adapter);
846 printk(KERN_ERR "Failed to init firmware\n");
849 netxen_nic_flash_print(adapter);
851 /* setup all the resources for the Phantom... */
852 /* this include the descriptors for rcv, tx, and status */
853 netxen_nic_clear_stats(adapter);
854 err = netxen_nic_hw_resources(adapter);
856 printk(KERN_ERR "Error in setting hw resources:%d\n",
860 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
861 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
862 netxen_post_rx_buffers(adapter, ctx, ring);
864 adapter->irq = adapter->ahw.pdev->irq;
865 err = request_irq(adapter->ahw.pdev->irq, netxen_intr,
866 IRQF_SHARED|IRQF_SAMPLE_RANDOM, netdev->name,
869 printk(KERN_ERR "request_irq failed with: %d\n", err);
870 netxen_free_hw_resources(adapter);
874 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
876 if (!adapter->driver_mismatch)
877 mod_timer(&adapter->watchdog_timer, jiffies);
879 napi_enable(&adapter->napi);
881 netxen_nic_enable_int(adapter);
883 /* Done here again so that even if phantom sw overwrote it,
885 if (adapter->init_port
886 && adapter->init_port(adapter, adapter->portnum) != 0) {
887 del_timer_sync(&adapter->watchdog_timer);
888 printk(KERN_ERR "%s: Failed to initialize port %d\n",
889 netxen_nic_driver_name, adapter->portnum);
890 napi_disable(&adapter->napi);
893 if (adapter->macaddr_set)
894 adapter->macaddr_set(adapter, netdev->dev_addr);
896 netxen_nic_set_link_parameters(adapter);
898 netxen_nic_set_multi(netdev);
899 if (adapter->set_mtu)
900 adapter->set_mtu(adapter, netdev->mtu);
902 if (!adapter->driver_mismatch)
903 netif_start_queue(netdev);
909 * netxen_nic_close - Disables a network interface entry point
911 static int netxen_nic_close(struct net_device *netdev)
913 struct netxen_adapter *adapter = netdev_priv(netdev);
915 struct netxen_cmd_buffer *cmd_buff;
916 struct netxen_skb_frag *buffrag;
918 netif_carrier_off(netdev);
919 netif_stop_queue(netdev);
920 napi_disable(&adapter->napi);
922 netxen_nic_disable_int(adapter);
924 cmd_buff = adapter->cmd_buf_arr;
925 for (i = 0; i < adapter->max_tx_desc_count; i++) {
926 buffrag = cmd_buff->frag_array;
928 pci_unmap_single(adapter->pdev, buffrag->dma,
929 buffrag->length, PCI_DMA_TODEVICE);
932 for (j = 0; j < cmd_buff->frag_count; j++) {
935 pci_unmap_page(adapter->pdev, buffrag->dma,
941 /* Free the skb we received in netxen_nic_xmit_frame */
943 dev_kfree_skb_any(cmd_buff->skb);
944 cmd_buff->skb = NULL;
948 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
949 FLUSH_SCHEDULED_WORK();
950 del_timer_sync(&adapter->watchdog_timer);
956 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
958 struct netxen_adapter *adapter = netdev_priv(netdev);
959 struct netxen_hardware_context *hw = &adapter->ahw;
960 unsigned int first_seg_len = skb->len - skb->data_len;
961 struct netxen_skb_frag *buffrag;
965 u32 saved_producer = 0;
966 struct cmd_desc_type0 *hwdesc;
968 struct netxen_cmd_buffer *pbuf = NULL;
969 static int dropped_packet = 0;
971 u32 local_producer = 0;
972 u32 max_tx_desc_count = 0;
973 u32 last_cmd_consumer = 0;
976 adapter->stats.xmitcalled++;
977 frag_count = skb_shinfo(skb)->nr_frags + 1;
979 if (unlikely(skb->len <= 0)) {
980 dev_kfree_skb_any(skb);
981 adapter->stats.badskblen++;
985 if (frag_count > MAX_BUFFERS_PER_CMD) {
986 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
987 "too large, can handle only %d frags\n",
988 netxen_nic_driver_name, netdev->name,
989 frag_count, MAX_BUFFERS_PER_CMD);
990 adapter->stats.txdropped++;
991 if ((++dropped_packet & 0xff) == 0xff)
992 printk("%s: %s droppped packets = %d\n",
993 netxen_nic_driver_name, netdev->name,
1000 * Everything is set up. Now, we just need to transmit it out.
1001 * Note that we have to copy the contents of buffer over to
1002 * right place. Later on, this can be optimized out by de-coupling the
1003 * producer index from the buffer index.
1005 retry_getting_window:
1006 spin_lock_bh(&adapter->tx_lock);
1007 if (adapter->total_threads >= MAX_XMIT_PRODUCERS) {
1008 spin_unlock_bh(&adapter->tx_lock);
1015 for (i = 0; i < 20; i++)
1016 cpu_relax(); /*This a nop instr on i386 */
1018 goto retry_getting_window;
1020 local_producer = adapter->cmd_producer;
1021 /* There 4 fragments per descriptor */
1022 no_of_desc = (frag_count + 3) >> 2;
1023 if (netdev->features & NETIF_F_TSO) {
1024 if (skb_shinfo(skb)->gso_size > 0) {
1027 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
1028 sizeof(struct ethhdr)) >
1029 (sizeof(struct cmd_desc_type0) - 2)) {
1034 k = adapter->cmd_producer;
1035 max_tx_desc_count = adapter->max_tx_desc_count;
1036 last_cmd_consumer = adapter->last_cmd_consumer;
1037 if ((k + no_of_desc) >=
1038 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
1039 last_cmd_consumer)) {
1040 netif_stop_queue(netdev);
1041 adapter->flags |= NETXEN_NETDEV_STATUS;
1042 spin_unlock_bh(&adapter->tx_lock);
1043 return NETDEV_TX_BUSY;
1045 k = get_index_range(k, max_tx_desc_count, no_of_desc);
1046 adapter->cmd_producer = k;
1047 adapter->total_threads++;
1048 adapter->num_threads++;
1050 spin_unlock_bh(&adapter->tx_lock);
1051 /* Copy the descriptors into the hardware */
1052 producer = local_producer;
1053 saved_producer = producer;
1054 hwdesc = &hw->cmd_desc_head[producer];
1055 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1056 /* Take skb->data itself */
1057 pbuf = &adapter->cmd_buf_arr[producer];
1058 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
1059 pbuf->mss = skb_shinfo(skb)->gso_size;
1060 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1065 pbuf->total_length = skb->len;
1067 pbuf->cmd = TX_ETHER_PKT;
1068 pbuf->frag_count = frag_count;
1069 pbuf->port = adapter->portnum;
1070 buffrag = &pbuf->frag_array[0];
1071 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
1073 buffrag->length = first_seg_len;
1074 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1075 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1076 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
1078 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
1079 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
1080 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1081 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1083 for (i = 1, k = 1; i < frag_count; i++, k++) {
1084 struct skb_frag_struct *frag;
1086 unsigned long offset;
1087 dma_addr_t temp_dma;
1089 /* move to next desc. if there is a need */
1090 if ((i & 0x3) == 0) {
1092 producer = get_next_index(producer,
1093 adapter->max_tx_desc_count);
1094 hwdesc = &hw->cmd_desc_head[producer];
1095 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1097 frag = &skb_shinfo(skb)->frags[i - 1];
1099 offset = frag->page_offset;
1102 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1103 len, PCI_DMA_TODEVICE);
1106 buffrag->dma = temp_dma;
1107 buffrag->length = temp_len;
1109 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
1112 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1113 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1116 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1117 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1120 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1121 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1124 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1125 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1130 producer = get_next_index(producer, adapter->max_tx_desc_count);
1132 /* might change opcode to TX_TCP_LSO */
1133 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1135 /* For LSO, we need to copy the MAC/IP/TCP headers into
1136 * the descriptor ring
1138 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1140 int hdr_len, first_hdr_len, more_hdr;
1141 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1142 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1143 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1146 first_hdr_len = hdr_len;
1149 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1150 hwdesc = &hw->cmd_desc_head[producer];
1152 /* copy the first 64 bytes */
1153 memcpy(((void *)hwdesc) + 2,
1154 (void *)(skb->data), first_hdr_len);
1155 producer = get_next_index(producer, max_tx_desc_count);
1158 hwdesc = &hw->cmd_desc_head[producer];
1159 /* copy the next 64 bytes - should be enough except
1160 * for pathological case
1162 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1166 producer = get_next_index(producer, max_tx_desc_count);
1170 i = netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
1172 hw->cmd_desc_head[saved_producer].flags_opcode =
1173 cpu_to_le16(hw->cmd_desc_head[saved_producer].flags_opcode);
1174 hw->cmd_desc_head[saved_producer].num_of_buffers_total_length =
1175 cpu_to_le32(hw->cmd_desc_head[saved_producer].
1176 num_of_buffers_total_length);
1178 spin_lock_bh(&adapter->tx_lock);
1179 adapter->stats.txbytes += i;
1181 /* Code to update the adapter considering how many producer threads
1182 are currently working */
1183 if ((--adapter->num_threads) == 0) {
1184 /* This is the last thread */
1185 u32 crb_producer = adapter->cmd_producer;
1186 netxen_nic_update_cmd_producer(adapter, crb_producer);
1188 adapter->total_threads = 0;
1191 adapter->stats.xmitfinished++;
1192 spin_unlock_bh(&adapter->tx_lock);
1194 netdev->trans_start = jiffies;
1196 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
1198 DPRINTK(INFO, "Done. Send\n");
1199 return NETDEV_TX_OK;
1202 static void netxen_watchdog(unsigned long v)
1204 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1206 SCHEDULE_WORK(&adapter->watchdog_task);
1209 static void netxen_tx_timeout(struct net_device *netdev)
1211 struct netxen_adapter *adapter = (struct netxen_adapter *)
1212 netdev_priv(netdev);
1213 SCHEDULE_WORK(&adapter->tx_timeout_task);
1216 static void netxen_tx_timeout_task(struct work_struct *work)
1218 struct netxen_adapter *adapter =
1219 container_of(work, struct netxen_adapter, tx_timeout_task);
1221 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1222 netxen_nic_driver_name, adapter->netdev->name);
1224 netxen_nic_close(adapter->netdev);
1225 netxen_nic_open(adapter->netdev);
1226 adapter->netdev->trans_start = jiffies;
1227 netif_wake_queue(adapter->netdev);
1231 netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
1235 DPRINTK(INFO, "Entered handle ISR\n");
1236 adapter->stats.ints++;
1238 netxen_nic_disable_int(adapter);
1240 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1241 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
1243 * Interrupts are already disabled.
1245 __netif_rx_schedule(netdev, &adapter->napi);
1247 static unsigned int intcount = 0;
1248 if ((++intcount & 0xfff) == 0xfff)
1250 "%s: %s interrupt %d while in poll\n",
1251 netxen_nic_driver_name, netdev->name,
1258 netxen_nic_enable_int(adapter);
1265 * netxen_intr - Interrupt Handler
1266 * @irq: interrupt number
1267 * data points to adapter stucture (which may be handling more than 1 port
1269 irqreturn_t netxen_intr(int irq, void *data)
1271 struct netxen_adapter *adapter;
1272 struct net_device *netdev;
1275 if (unlikely(!irq)) {
1276 return IRQ_NONE; /* Not our interrupt */
1279 adapter = (struct netxen_adapter *)data;
1280 netdev = adapter->netdev;
1282 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1283 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1284 /* not our interrupt */
1285 if ((our_int & (0x80 << adapter->portnum)) == 0)
1289 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1290 /* claim interrupt */
1291 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1292 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1293 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1297 if (netif_running(netdev))
1298 netxen_handle_int(adapter, netdev);
1303 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1305 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1306 struct net_device *netdev = adapter->netdev;
1311 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1314 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1316 * Fairness issue. This will give undue weight to the
1317 * receive context 0.
1321 * To avoid starvation, we give each of our receivers,
1322 * a fraction of the quota. Sometimes, it might happen that we
1323 * have enough quota to process every packet, but since all the
1324 * packets are on one context, it gets only half of the quota,
1325 * and ends up not processing it.
1327 work_done += netxen_process_rcv_ring(adapter, ctx,
1328 budget / MAX_RCV_CTX);
1331 if (work_done >= budget && netxen_nic_rx_has_work(adapter) != 0)
1334 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1337 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1338 work_done, work_to_do);
1340 netif_rx_complete(netdev, napi);
1341 netxen_nic_enable_int(adapter);
1347 #ifdef CONFIG_NET_POLL_CONTROLLER
1348 static void netxen_nic_poll_controller(struct net_device *netdev)
1350 struct netxen_adapter *adapter = netdev_priv(netdev);
1351 disable_irq(adapter->irq);
1352 netxen_intr(adapter->irq, adapter);
1353 enable_irq(adapter->irq);
1357 static struct pci_driver netxen_driver = {
1358 .name = netxen_nic_driver_name,
1359 .id_table = netxen_pci_tbl,
1360 .probe = netxen_nic_probe,
1361 .remove = __devexit_p(netxen_nic_remove)
1364 /* Driver Registration on NetXen card */
1366 static int __init netxen_init_module(void)
1368 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1371 return pci_register_driver(&netxen_driver);
1374 module_init(netxen_init_module);
1376 static void __exit netxen_exit_module(void)
1379 * Wait for some time to allow the dma to drain, if any.
1382 pci_unregister_driver(&netxen_driver);
1383 destroy_workqueue(netxen_workq);
1386 module_exit(netxen_exit_module);