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->is_up == NETXEN_ADAPTER_UP_MAGIC) {
736 init_firmware_done++;
737 netxen_free_hw_resources(adapter);
740 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
741 recv_ctx = &adapter->recv_ctx[ctxid];
742 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
743 rcv_desc = &recv_ctx->rcv_desc[ring];
744 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
745 buffer = &(rcv_desc->rx_buf_arr[i]);
746 if (buffer->state == NETXEN_BUFFER_FREE)
748 pci_unmap_single(pdev, buffer->dma,
751 if (buffer->skb != NULL)
752 dev_kfree_skb_any(buffer->skb);
754 vfree(rcv_desc->rx_buf_arr);
758 vfree(adapter->cmd_buf_arr);
760 if (adapter->portnum == 0) {
761 if (init_firmware_done) {
764 if (dma_watchdog_shutdown_request(adapter) == 1)
767 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
772 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
775 /* clear the register for future unloads/loads */
776 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
777 printk(KERN_INFO "State: 0x%0x\n",
778 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
780 /* leave the hw in the same state as reboot */
781 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
782 netxen_pinit_from_rom(adapter, 0);
784 netxen_load_firmware(adapter);
785 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
788 /* clear the register for future unloads/loads */
789 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
790 printk(KERN_INFO "State: 0x%0x\n",
791 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
795 if (dma_watchdog_shutdown_request(adapter) == 1)
798 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
803 netxen_free_adapter_offload(adapter);
805 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
811 free_irq(adapter->irq, adapter);
813 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
814 pci_disable_msi(pdev);
816 iounmap(adapter->ahw.db_base);
817 iounmap(adapter->ahw.pci_base0);
818 iounmap(adapter->ahw.pci_base1);
819 iounmap(adapter->ahw.pci_base2);
821 pci_release_regions(pdev);
822 pci_disable_device(pdev);
823 pci_set_drvdata(pdev, NULL);
829 * Called when a network interface is made active
830 * @returns 0 on success, negative value on failure
832 static int netxen_nic_open(struct net_device *netdev)
834 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
838 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
839 err = netxen_init_firmware(adapter);
841 printk(KERN_ERR "Failed to init firmware\n");
844 netxen_nic_flash_print(adapter);
846 /* setup all the resources for the Phantom... */
847 /* this include the descriptors for rcv, tx, and status */
848 netxen_nic_clear_stats(adapter);
849 err = netxen_nic_hw_resources(adapter);
851 printk(KERN_ERR "Error in setting hw resources:%d\n",
855 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
856 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
857 netxen_post_rx_buffers(adapter, ctx, ring);
859 adapter->irq = adapter->ahw.pdev->irq;
860 err = request_irq(adapter->ahw.pdev->irq, netxen_intr,
861 IRQF_SHARED|IRQF_SAMPLE_RANDOM, netdev->name,
864 printk(KERN_ERR "request_irq failed with: %d\n", err);
865 netxen_free_hw_resources(adapter);
869 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
871 if (!adapter->driver_mismatch)
872 mod_timer(&adapter->watchdog_timer, jiffies);
874 napi_enable(&adapter->napi);
876 netxen_nic_enable_int(adapter);
878 /* Done here again so that even if phantom sw overwrote it,
880 if (adapter->init_port
881 && adapter->init_port(adapter, adapter->portnum) != 0) {
882 del_timer_sync(&adapter->watchdog_timer);
883 printk(KERN_ERR "%s: Failed to initialize port %d\n",
884 netxen_nic_driver_name, adapter->portnum);
885 napi_disable(&adapter->napi);
888 if (adapter->macaddr_set)
889 adapter->macaddr_set(adapter, netdev->dev_addr);
891 netxen_nic_set_link_parameters(adapter);
893 netxen_nic_set_multi(netdev);
894 if (adapter->set_mtu)
895 adapter->set_mtu(adapter, netdev->mtu);
897 if (!adapter->driver_mismatch)
898 netif_start_queue(netdev);
904 * netxen_nic_close - Disables a network interface entry point
906 static int netxen_nic_close(struct net_device *netdev)
908 struct netxen_adapter *adapter = netdev_priv(netdev);
910 struct netxen_cmd_buffer *cmd_buff;
911 struct netxen_skb_frag *buffrag;
913 netif_carrier_off(netdev);
914 netif_stop_queue(netdev);
915 napi_disable(&adapter->napi);
917 if (adapter->stop_port)
918 adapter->stop_port(adapter);
920 netxen_nic_disable_int(adapter);
922 cmd_buff = adapter->cmd_buf_arr;
923 for (i = 0; i < adapter->max_tx_desc_count; i++) {
924 buffrag = cmd_buff->frag_array;
926 pci_unmap_single(adapter->pdev, buffrag->dma,
927 buffrag->length, PCI_DMA_TODEVICE);
930 for (j = 0; j < cmd_buff->frag_count; j++) {
933 pci_unmap_page(adapter->pdev, buffrag->dma,
939 /* Free the skb we received in netxen_nic_xmit_frame */
941 dev_kfree_skb_any(cmd_buff->skb);
942 cmd_buff->skb = NULL;
946 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
947 FLUSH_SCHEDULED_WORK();
948 del_timer_sync(&adapter->watchdog_timer);
954 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
956 struct netxen_adapter *adapter = netdev_priv(netdev);
957 struct netxen_hardware_context *hw = &adapter->ahw;
958 unsigned int first_seg_len = skb->len - skb->data_len;
959 struct netxen_skb_frag *buffrag;
963 u32 saved_producer = 0;
964 struct cmd_desc_type0 *hwdesc;
966 struct netxen_cmd_buffer *pbuf = NULL;
967 static int dropped_packet = 0;
969 u32 local_producer = 0;
970 u32 max_tx_desc_count = 0;
971 u32 last_cmd_consumer = 0;
974 adapter->stats.xmitcalled++;
975 frag_count = skb_shinfo(skb)->nr_frags + 1;
977 if (unlikely(skb->len <= 0)) {
978 dev_kfree_skb_any(skb);
979 adapter->stats.badskblen++;
983 if (frag_count > MAX_BUFFERS_PER_CMD) {
984 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
985 "too large, can handle only %d frags\n",
986 netxen_nic_driver_name, netdev->name,
987 frag_count, MAX_BUFFERS_PER_CMD);
988 adapter->stats.txdropped++;
989 if ((++dropped_packet & 0xff) == 0xff)
990 printk("%s: %s droppped packets = %d\n",
991 netxen_nic_driver_name, netdev->name,
997 /* There 4 fragments per descriptor */
998 no_of_desc = (frag_count + 3) >> 2;
999 if (netdev->features & NETIF_F_TSO) {
1000 if (skb_shinfo(skb)->gso_size > 0) {
1003 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
1004 sizeof(struct ethhdr)) >
1005 (sizeof(struct cmd_desc_type0) - 2)) {
1011 spin_lock_bh(&adapter->tx_lock);
1012 if (adapter->total_threads >= MAX_XMIT_PRODUCERS) {
1015 local_producer = adapter->cmd_producer;
1016 k = adapter->cmd_producer;
1017 max_tx_desc_count = adapter->max_tx_desc_count;
1018 last_cmd_consumer = adapter->last_cmd_consumer;
1019 if ((k + no_of_desc) >=
1020 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
1021 last_cmd_consumer)) {
1024 k = get_index_range(k, max_tx_desc_count, no_of_desc);
1025 adapter->cmd_producer = k;
1026 adapter->total_threads++;
1027 adapter->num_threads++;
1029 spin_unlock_bh(&adapter->tx_lock);
1030 /* Copy the descriptors into the hardware */
1031 producer = local_producer;
1032 saved_producer = producer;
1033 hwdesc = &hw->cmd_desc_head[producer];
1034 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1035 /* Take skb->data itself */
1036 pbuf = &adapter->cmd_buf_arr[producer];
1037 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
1038 pbuf->mss = skb_shinfo(skb)->gso_size;
1039 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1044 pbuf->total_length = skb->len;
1046 pbuf->cmd = TX_ETHER_PKT;
1047 pbuf->frag_count = frag_count;
1048 pbuf->port = adapter->portnum;
1049 buffrag = &pbuf->frag_array[0];
1050 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
1052 buffrag->length = first_seg_len;
1053 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1054 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1055 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
1057 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
1058 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
1059 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1060 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1062 for (i = 1, k = 1; i < frag_count; i++, k++) {
1063 struct skb_frag_struct *frag;
1065 unsigned long offset;
1066 dma_addr_t temp_dma;
1068 /* move to next desc. if there is a need */
1069 if ((i & 0x3) == 0) {
1071 producer = get_next_index(producer,
1072 adapter->max_tx_desc_count);
1073 hwdesc = &hw->cmd_desc_head[producer];
1074 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1075 pbuf = &adapter->cmd_buf_arr[producer];
1078 frag = &skb_shinfo(skb)->frags[i - 1];
1080 offset = frag->page_offset;
1083 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1084 len, PCI_DMA_TODEVICE);
1087 buffrag->dma = temp_dma;
1088 buffrag->length = temp_len;
1090 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
1093 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1094 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1097 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1098 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1101 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1102 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1105 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1106 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1111 producer = get_next_index(producer, adapter->max_tx_desc_count);
1113 /* might change opcode to TX_TCP_LSO */
1114 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1116 /* For LSO, we need to copy the MAC/IP/TCP headers into
1117 * the descriptor ring
1119 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1121 int hdr_len, first_hdr_len, more_hdr;
1122 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1123 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1124 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1127 first_hdr_len = hdr_len;
1130 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1131 hwdesc = &hw->cmd_desc_head[producer];
1132 pbuf = &adapter->cmd_buf_arr[producer];
1135 /* copy the first 64 bytes */
1136 memcpy(((void *)hwdesc) + 2,
1137 (void *)(skb->data), first_hdr_len);
1138 producer = get_next_index(producer, max_tx_desc_count);
1141 hwdesc = &hw->cmd_desc_head[producer];
1142 pbuf = &adapter->cmd_buf_arr[producer];
1144 /* copy the next 64 bytes - should be enough except
1145 * for pathological case
1147 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1151 producer = get_next_index(producer, max_tx_desc_count);
1155 spin_lock_bh(&adapter->tx_lock);
1156 adapter->stats.txbytes += skb->len;
1158 /* Code to update the adapter considering how many producer threads
1159 are currently working */
1160 if ((--adapter->num_threads) == 0) {
1161 /* This is the last thread */
1162 u32 crb_producer = adapter->cmd_producer;
1163 netxen_nic_update_cmd_producer(adapter, crb_producer);
1165 adapter->total_threads = 0;
1168 adapter->stats.xmitfinished++;
1169 netdev->trans_start = jiffies;
1171 spin_unlock_bh(&adapter->tx_lock);
1172 return NETDEV_TX_OK;
1175 netif_stop_queue(netdev);
1176 adapter->flags |= NETXEN_NETDEV_STATUS;
1178 spin_unlock_bh(&adapter->tx_lock);
1179 return NETDEV_TX_BUSY;
1182 static void netxen_watchdog(unsigned long v)
1184 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1186 SCHEDULE_WORK(&adapter->watchdog_task);
1189 static void netxen_tx_timeout(struct net_device *netdev)
1191 struct netxen_adapter *adapter = (struct netxen_adapter *)
1192 netdev_priv(netdev);
1193 SCHEDULE_WORK(&adapter->tx_timeout_task);
1196 static void netxen_tx_timeout_task(struct work_struct *work)
1198 struct netxen_adapter *adapter =
1199 container_of(work, struct netxen_adapter, tx_timeout_task);
1201 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1202 netxen_nic_driver_name, adapter->netdev->name);
1204 netxen_nic_close(adapter->netdev);
1205 netxen_nic_open(adapter->netdev);
1206 adapter->netdev->trans_start = jiffies;
1207 netif_wake_queue(adapter->netdev);
1211 netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
1215 DPRINTK(INFO, "Entered handle ISR\n");
1216 adapter->stats.ints++;
1218 netxen_nic_disable_int(adapter);
1220 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1221 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
1223 * Interrupts are already disabled.
1225 __netif_rx_schedule(netdev, &adapter->napi);
1227 static unsigned int intcount = 0;
1228 if ((++intcount & 0xfff) == 0xfff)
1230 "%s: %s interrupt %d while in poll\n",
1231 netxen_nic_driver_name, netdev->name,
1238 netxen_nic_enable_int(adapter);
1245 * netxen_intr - Interrupt Handler
1246 * @irq: interrupt number
1247 * data points to adapter stucture (which may be handling more than 1 port
1249 irqreturn_t netxen_intr(int irq, void *data)
1251 struct netxen_adapter *adapter = data;
1252 struct net_device *netdev = adapter->netdev;
1255 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1256 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1257 /* not our interrupt */
1258 if ((our_int & (0x80 << adapter->portnum)) == 0)
1262 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1263 /* claim interrupt */
1264 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1265 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1266 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1270 if (netif_running(netdev))
1271 netxen_handle_int(adapter, netdev);
1276 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1278 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1279 struct net_device *netdev = adapter->netdev;
1284 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1287 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1289 * Fairness issue. This will give undue weight to the
1290 * receive context 0.
1294 * To avoid starvation, we give each of our receivers,
1295 * a fraction of the quota. Sometimes, it might happen that we
1296 * have enough quota to process every packet, but since all the
1297 * packets are on one context, it gets only half of the quota,
1298 * and ends up not processing it.
1300 work_done += netxen_process_rcv_ring(adapter, ctx,
1301 budget / MAX_RCV_CTX);
1304 if (work_done >= budget)
1307 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1310 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1311 work_done, work_to_do);
1313 netif_rx_complete(netdev, napi);
1314 netxen_nic_enable_int(adapter);
1320 #ifdef CONFIG_NET_POLL_CONTROLLER
1321 static void netxen_nic_poll_controller(struct net_device *netdev)
1323 struct netxen_adapter *adapter = netdev_priv(netdev);
1324 disable_irq(adapter->irq);
1325 netxen_intr(adapter->irq, adapter);
1326 enable_irq(adapter->irq);
1330 static struct pci_driver netxen_driver = {
1331 .name = netxen_nic_driver_name,
1332 .id_table = netxen_pci_tbl,
1333 .probe = netxen_nic_probe,
1334 .remove = __devexit_p(netxen_nic_remove)
1337 /* Driver Registration on NetXen card */
1339 static int __init netxen_init_module(void)
1341 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1344 return pci_register_driver(&netxen_driver);
1347 module_init(netxen_init_module);
1349 static void __exit netxen_exit_module(void)
1352 * Wait for some time to allow the dma to drain, if any.
1355 pci_unregister_driver(&netxen_driver);
1356 destroy_workqueue(netxen_workq);
1359 module_exit(netxen_exit_module);