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>
42 #include <linux/vmalloc.h>
45 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
49 char netxen_nic_driver_name[] = "netxen_nic";
50 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
51 NETXEN_NIC_LINUX_VERSIONID;
53 #define NETXEN_NETDEV_WEIGHT 120
54 #define NETXEN_ADAPTER_UP_MAGIC 777
55 #define NETXEN_NIC_PEG_TUNE 0
57 #define DMA_32BIT_MASK 0x00000000ffffffffULL
58 #define DMA_35BIT_MASK 0x00000007ffffffffULL
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_handle_int(struct netxen_adapter *, struct net_device *);
71 static int netxen_nic_poll(struct net_device *dev, int *budget);
72 #ifdef CONFIG_NET_POLL_CONTROLLER
73 static void netxen_nic_poll_controller(struct net_device *netdev);
75 static irqreturn_t netxen_intr(int irq, void *data);
77 int physical_port[] = {0, 1, 2, 3};
79 /* PCI Device ID Table */
80 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
81 {PCI_DEVICE(0x4040, 0x0001)},
82 {PCI_DEVICE(0x4040, 0x0002)},
83 {PCI_DEVICE(0x4040, 0x0003)},
84 {PCI_DEVICE(0x4040, 0x0004)},
85 {PCI_DEVICE(0x4040, 0x0005)},
86 {PCI_DEVICE(0x4040, 0x0024)},
87 {PCI_DEVICE(0x4040, 0x0025)},
91 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
93 struct workqueue_struct *netxen_workq;
94 static void netxen_watchdog(unsigned long);
96 static inline void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
97 uint32_t crb_producer)
99 switch (adapter->portnum) {
101 writel(crb_producer, NETXEN_CRB_NORMALIZE
102 (adapter, CRB_CMD_PRODUCER_OFFSET));
105 writel(crb_producer, NETXEN_CRB_NORMALIZE
106 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
109 writel(crb_producer, NETXEN_CRB_NORMALIZE
110 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
113 writel(crb_producer, NETXEN_CRB_NORMALIZE
114 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
117 printk(KERN_WARNING "We tried to update "
118 "CRB_CMD_PRODUCER_OFFSET for invalid "
119 "PCI function id %d\n",
125 static inline void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
128 switch (adapter->portnum) {
130 writel(crb_consumer, NETXEN_CRB_NORMALIZE
131 (adapter, CRB_CMD_CONSUMER_OFFSET));
134 writel(crb_consumer, NETXEN_CRB_NORMALIZE
135 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
138 writel(crb_consumer, NETXEN_CRB_NORMALIZE
139 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
142 writel(crb_consumer, NETXEN_CRB_NORMALIZE
143 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
146 printk(KERN_WARNING "We tried to update "
147 "CRB_CMD_PRODUCER_OFFSET for invalid "
148 "PCI function id %d\n",
154 #define ADAPTER_LIST_SIZE 12
155 int netxen_cards_found;
157 static void netxen_nic_disable_int(struct netxen_adapter *adapter)
159 uint32_t mask = 0x7ff;
162 DPRINTK(1, INFO, "Entered ISR Disable \n");
164 switch (adapter->portnum) {
166 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
169 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
172 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
175 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
179 if (adapter->intr_scheme != -1 &&
180 adapter->intr_scheme != INTR_SCHEME_PERPORT)
181 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
183 /* Window = 0 or 1 */
184 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
187 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
188 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
195 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
196 netxen_nic_driver_name);
200 DPRINTK(1, INFO, "Done with Disable Int\n");
203 static void netxen_nic_enable_int(struct netxen_adapter *adapter)
207 DPRINTK(1, INFO, "Entered ISR Enable \n");
209 if (adapter->intr_scheme != -1 &&
210 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
211 switch (adapter->ahw.board_type) {
215 case NETXEN_NIC_XGBE:
223 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
226 switch (adapter->portnum) {
228 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
231 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
234 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
237 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
241 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
243 if (adapter->intr_scheme != -1 &&
244 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
245 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
248 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
251 DPRINTK(1, INFO, "Done with enable Int\n");
257 * The Linux system will invoke this after identifying the vendor ID and
258 * device Id in the pci_tbl supported by this module.
260 * A quad port card has one operational PCI config space, (function 0),
261 * which is used to access all four ports.
263 * This routine will initialize the adapter, and setup the global parameters
264 * along with the port's specific structure.
267 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
269 struct net_device *netdev = NULL;
270 struct netxen_adapter *adapter = NULL;
271 void __iomem *mem_ptr0 = NULL;
272 void __iomem *mem_ptr1 = NULL;
273 void __iomem *mem_ptr2 = NULL;
274 unsigned long first_page_group_end;
275 unsigned long first_page_group_start;
278 u8 __iomem *db_ptr = NULL;
279 unsigned long mem_base, mem_len, db_base, db_len;
280 int pci_using_dac, i = 0, err;
282 struct netxen_recv_context *recv_ctx = NULL;
283 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
284 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
285 u64 mac_addr[FLASH_NUM_PORTS + 1];
288 int pci_func_id = PCI_FUNC(pdev->devfn);
290 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
292 if (pdev->class != 0x020000) {
293 printk(KERN_ERR"NetXen function %d, class %x will not"
294 "be enabled.\n",pci_func_id, pdev->class);
297 if ((err = pci_enable_device(pdev)))
299 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
301 goto err_out_disable_pdev;
304 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
305 goto err_out_disable_pdev;
307 pci_set_master(pdev);
308 if (pdev->revision == NX_P2_C1 &&
309 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
310 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
313 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
314 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
315 goto err_out_free_res;
321 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
323 printk(KERN_ERR"%s: Failed to allocate memory for the "
324 "device block.Check system memory resource"
325 " usage.\n", netxen_nic_driver_name);
326 goto err_out_free_res;
329 SET_MODULE_OWNER(netdev);
330 SET_NETDEV_DEV(netdev, &pdev->dev);
332 adapter = netdev->priv;
333 memset(adapter, 0 , sizeof(struct netxen_adapter));
335 adapter->ahw.pdev = pdev;
336 adapter->ahw.pci_func = pci_func_id;
337 spin_lock_init(&adapter->tx_lock);
339 /* remap phys address */
340 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
341 mem_len = pci_resource_len(pdev, 0);
343 /* 128 Meg of memory */
344 if (mem_len == NETXEN_PCI_128MB_SIZE) {
345 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
346 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
347 SECOND_PAGE_GROUP_SIZE);
348 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
349 THIRD_PAGE_GROUP_SIZE);
350 first_page_group_start = FIRST_PAGE_GROUP_START;
351 first_page_group_end = FIRST_PAGE_GROUP_END;
352 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
353 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
354 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
355 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
356 first_page_group_start = 0;
357 first_page_group_end = 0;
360 goto err_out_free_netdev;
363 if (((mem_ptr0 == 0UL) && (mem_len == NETXEN_PCI_128MB_SIZE)) ||
364 (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
366 "Cannot remap adapter memory aborting.:"
367 "0 -> %p, 1 -> %p, 2 -> %p\n",
368 mem_ptr0, mem_ptr1, mem_ptr2);
371 goto err_out_iounmap;
373 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
374 db_len = pci_resource_len(pdev, 4);
377 printk(KERN_ERR "%s: doorbell is disabled\n",
378 netxen_nic_driver_name);
380 goto err_out_iounmap;
382 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
385 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
387 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
388 netxen_nic_driver_name);
390 goto err_out_iounmap;
392 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
394 adapter->ahw.pci_base0 = mem_ptr0;
395 adapter->ahw.first_page_group_start = first_page_group_start;
396 adapter->ahw.first_page_group_end = first_page_group_end;
397 adapter->ahw.pci_base1 = mem_ptr1;
398 adapter->ahw.pci_base2 = mem_ptr2;
399 adapter->ahw.db_base = db_ptr;
400 adapter->ahw.db_len = db_len;
402 adapter->netdev = netdev;
403 adapter->pdev = pdev;
405 /* this will be read from FW later */
406 adapter->intr_scheme = -1;
408 /* This will be reset for mezz cards */
409 adapter->portnum = pci_func_id;
410 adapter->status &= ~NETXEN_NETDEV_STATUS;
412 netdev->open = netxen_nic_open;
413 netdev->stop = netxen_nic_close;
414 netdev->hard_start_xmit = netxen_nic_xmit_frame;
415 netdev->get_stats = netxen_nic_get_stats;
416 netdev->set_multicast_list = netxen_nic_set_multi;
417 netdev->set_mac_address = netxen_nic_set_mac;
418 netdev->change_mtu = netxen_nic_change_mtu;
419 netdev->tx_timeout = netxen_tx_timeout;
420 netdev->watchdog_timeo = HZ;
422 netxen_nic_change_mtu(netdev, netdev->mtu);
424 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
425 netdev->poll = netxen_nic_poll;
426 netdev->weight = NETXEN_NETDEV_WEIGHT;
427 #ifdef CONFIG_NET_POLL_CONTROLLER
428 netdev->poll_controller = netxen_nic_poll_controller;
430 /* ScatterGather support */
431 netdev->features = NETIF_F_SG;
432 netdev->features |= NETIF_F_IP_CSUM;
433 netdev->features |= NETIF_F_TSO;
436 netdev->features |= NETIF_F_HIGHDMA;
438 if (pci_enable_msi(pdev))
439 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
441 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
443 netdev->irq = pdev->irq;
444 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
447 * Set the CRB window to invalid. If any register in window 0 is
448 * accessed it should set the window to 0 and then reset it to 1.
450 adapter->curr_window = 255;
452 /* initialize the adapter */
453 netxen_initialize_adapter_hw(adapter);
456 * Adapter in our case is quad port so initialize it before
457 * initializing the ports
460 netxen_initialize_adapter_ops(adapter);
462 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
463 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
464 (adapter->ahw.boardcfg.board_type ==
465 NETXEN_BRDTYPE_P2_SB31_2G))
466 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
468 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
469 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
470 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
472 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
473 if (cmd_buf_arr == NULL) {
475 "%s: Could not allocate cmd_buf_arr memory:%d\n",
476 netxen_nic_driver_name, (int)TX_RINGSIZE);
478 goto err_out_free_adapter;
480 memset(cmd_buf_arr, 0, TX_RINGSIZE);
481 adapter->cmd_buf_arr = cmd_buf_arr;
483 for (i = 0; i < MAX_RCV_CTX; ++i) {
484 recv_ctx = &adapter->recv_ctx[i];
485 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
486 rcv_desc = &recv_ctx->rcv_desc[ring];
487 switch (RCV_DESC_TYPE(ring)) {
488 case RCV_DESC_NORMAL:
489 rcv_desc->max_rx_desc_count =
490 adapter->max_rx_desc_count;
491 rcv_desc->flags = RCV_DESC_NORMAL;
492 rcv_desc->dma_size = RX_DMA_MAP_LEN;
493 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
497 rcv_desc->max_rx_desc_count =
498 adapter->max_jumbo_rx_desc_count;
499 rcv_desc->flags = RCV_DESC_JUMBO;
500 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
501 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
505 rcv_desc->max_rx_desc_count =
506 adapter->max_lro_rx_desc_count;
507 rcv_desc->flags = RCV_DESC_LRO;
508 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
509 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
513 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
514 vmalloc(RCV_BUFFSIZE);
516 if (rcv_desc->rx_buf_arr == NULL) {
517 printk(KERN_ERR "%s: Could not allocate"
518 "rcv_desc->rx_buf_arr memory:%d\n",
519 netxen_nic_driver_name,
522 goto err_out_free_rx_buffer;
524 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
529 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
531 /* Mezz cards have PCI function 0,2,3 enabled */
532 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB31_10G_IMEZ)
533 && (pci_func_id >= 2))
534 adapter->portnum = pci_func_id - 2;
537 if(adapter->portnum == 0) {
538 netxen_pinit_from_rom(adapter, 0);
540 netxen_load_firmware(adapter);
544 init_timer(&adapter->watchdog_timer);
545 adapter->ahw.xg_linkup = 0;
546 adapter->watchdog_timer.function = &netxen_watchdog;
547 adapter->watchdog_timer.data = (unsigned long)adapter;
548 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
549 adapter->ahw.pdev = pdev;
550 adapter->proc_cmd_buf_counter = 0;
551 adapter->ahw.revision_id = pdev->revision;
553 /* make sure Window == 1 */
554 netxen_nic_pci_change_crbwindow(adapter, 1);
556 netxen_nic_update_cmd_producer(adapter, 0);
557 netxen_nic_update_cmd_consumer(adapter, 0);
558 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
560 if (netxen_is_flash_supported(adapter) == 0 &&
561 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
567 unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum];
568 netdev->dev_addr[0] = *(p + 5);
569 netdev->dev_addr[1] = *(p + 4);
570 netdev->dev_addr[2] = *(p + 3);
571 netdev->dev_addr[3] = *(p + 2);
572 netdev->dev_addr[4] = *(p + 1);
573 netdev->dev_addr[5] = *(p + 0);
575 memcpy(netdev->perm_addr, netdev->dev_addr,
577 if (!is_valid_ether_addr(netdev->perm_addr)) {
578 printk(KERN_ERR "%s: Bad MAC address "
579 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
580 netxen_nic_driver_name,
586 netdev->dev_addr[5]);
588 if (adapter->macaddr_set)
589 adapter->macaddr_set(adapter,
594 if (adapter->portnum == 0) {
595 err = netxen_initialize_adapter_offload(adapter);
597 goto err_out_free_rx_buffer;
598 val = readl(NETXEN_CRB_NORMALIZE(adapter,
599 NETXEN_CAM_RAM(0x1fc)));
600 if (val == 0x55555555) {
601 /* This is the first boot after power up */
602 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
605 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
606 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
608 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
609 netxen_nic_driver_name);
611 val = readl(NETXEN_CRB_NORMALIZE(adapter,
612 NETXEN_ROMUSB_GLB_SW_RESET));
613 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
614 if (val != 0x80000f) {
615 /* clear the register for future unloads/loads */
616 writel(0, NETXEN_CRB_NORMALIZE(adapter,
617 NETXEN_CAM_RAM(0x1fc)));
618 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
620 goto err_out_free_dev;
624 /* clear the register for future unloads/loads */
625 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
626 printk(KERN_INFO "State: 0x%0x\n",
627 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
630 * Tell the hardware our version number.
632 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
633 | ((_NETXEN_NIC_LINUX_MINOR << 8))
634 | (_NETXEN_NIC_LINUX_SUBVERSION);
635 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
637 /* Unlock the HW, prompting the boot sequence */
639 NETXEN_CRB_NORMALIZE(adapter,
640 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
641 /* Handshake with the card before we register the devices. */
642 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
646 * See if the firmware gave us a virtual-physical port mapping.
648 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
650 physical_port[adapter->portnum] = i;
652 netif_carrier_off(netdev);
653 netif_stop_queue(netdev);
655 if ((err = register_netdev(netdev))) {
656 printk(KERN_ERR "%s: register_netdev failed port #%d"
657 " aborting\n", netxen_nic_driver_name,
660 goto err_out_free_dev;
663 pci_set_drvdata(pdev, adapter);
665 switch (adapter->ahw.board_type) {
667 printk(KERN_INFO "%s: QUAD GbE board initialized\n",
668 netxen_nic_driver_name);
671 case NETXEN_NIC_XGBE:
672 printk(KERN_INFO "%s: XGbE board initialized\n",
673 netxen_nic_driver_name);
677 adapter->driver_mismatch = 0;
682 if (adapter->portnum == 0)
683 netxen_free_adapter_offload(adapter);
685 err_out_free_rx_buffer:
686 for (i = 0; i < MAX_RCV_CTX; ++i) {
687 recv_ctx = &adapter->recv_ctx[i];
688 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
689 rcv_desc = &recv_ctx->rcv_desc[ring];
690 if (rcv_desc->rx_buf_arr != NULL) {
691 vfree(rcv_desc->rx_buf_arr);
692 rcv_desc->rx_buf_arr = NULL;
698 err_out_free_adapter:
699 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
700 pci_disable_msi(pdev);
702 pci_set_drvdata(pdev, NULL);
719 pci_release_regions(pdev);
721 err_out_disable_pdev:
722 pci_disable_device(pdev);
726 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
728 struct netxen_adapter *adapter;
729 struct net_device *netdev;
730 struct netxen_rx_buffer *buffer;
731 struct netxen_recv_context *recv_ctx;
732 struct netxen_rcv_desc_ctx *rcv_desc;
734 static int init_firmware_done = 0;
736 adapter = pci_get_drvdata(pdev);
740 netdev = adapter->netdev;
742 unregister_netdev(netdev);
744 if (adapter->stop_port)
745 adapter->stop_port(adapter);
747 netxen_nic_disable_int(adapter);
749 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
750 init_firmware_done++;
751 netxen_free_hw_resources(adapter);
754 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
755 recv_ctx = &adapter->recv_ctx[ctxid];
756 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
757 rcv_desc = &recv_ctx->rcv_desc[ring];
758 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
759 buffer = &(rcv_desc->rx_buf_arr[i]);
760 if (buffer->state == NETXEN_BUFFER_FREE)
762 pci_unmap_single(pdev, buffer->dma,
765 if (buffer->skb != NULL)
766 dev_kfree_skb_any(buffer->skb);
768 vfree(rcv_desc->rx_buf_arr);
772 vfree(adapter->cmd_buf_arr);
774 if (adapter->portnum == 0) {
775 if (init_firmware_done) {
778 if (dma_watchdog_shutdown_request(adapter) == 1)
781 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
786 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
789 /* clear the register for future unloads/loads */
790 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
791 printk(KERN_INFO "State: 0x%0x\n",
792 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
794 /* leave the hw in the same state as reboot */
795 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
796 netxen_pinit_from_rom(adapter, 0);
798 netxen_load_firmware(adapter);
799 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
802 /* clear the register for future unloads/loads */
803 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
804 printk(KERN_INFO "State: 0x%0x\n",
805 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
809 if (dma_watchdog_shutdown_request(adapter) == 1)
812 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
817 netxen_free_adapter_offload(adapter);
819 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
825 free_irq(adapter->irq, adapter);
827 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
828 pci_disable_msi(pdev);
830 iounmap(adapter->ahw.db_base);
831 iounmap(adapter->ahw.pci_base0);
832 iounmap(adapter->ahw.pci_base1);
833 iounmap(adapter->ahw.pci_base2);
835 pci_release_regions(pdev);
836 pci_disable_device(pdev);
837 pci_set_drvdata(pdev, NULL);
843 * Called when a network interface is made active
844 * @returns 0 on success, negative value on failure
846 static int netxen_nic_open(struct net_device *netdev)
848 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
852 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
853 err = netxen_init_firmware(adapter);
855 printk(KERN_ERR "Failed to init firmware\n");
858 netxen_nic_flash_print(adapter);
860 /* setup all the resources for the Phantom... */
861 /* this include the descriptors for rcv, tx, and status */
862 netxen_nic_clear_stats(adapter);
863 err = netxen_nic_hw_resources(adapter);
865 printk(KERN_ERR "Error in setting hw resources:%d\n",
869 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
870 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
871 netxen_post_rx_buffers(adapter, ctx, ring);
873 adapter->irq = adapter->ahw.pdev->irq;
874 err = request_irq(adapter->ahw.pdev->irq, netxen_intr,
875 IRQF_SHARED|IRQF_SAMPLE_RANDOM, netdev->name,
878 printk(KERN_ERR "request_irq failed with: %d\n", err);
879 netxen_free_hw_resources(adapter);
883 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
885 if (!adapter->driver_mismatch)
886 mod_timer(&adapter->watchdog_timer, jiffies);
888 netxen_nic_enable_int(adapter);
890 /* Done here again so that even if phantom sw overwrote it,
892 if (adapter->init_port
893 && adapter->init_port(adapter, adapter->portnum) != 0) {
894 del_timer_sync(&adapter->watchdog_timer);
895 printk(KERN_ERR "%s: Failed to initialize port %d\n",
896 netxen_nic_driver_name, adapter->portnum);
899 if (adapter->macaddr_set)
900 adapter->macaddr_set(adapter, netdev->dev_addr);
902 netxen_nic_set_link_parameters(adapter);
904 netxen_nic_set_multi(netdev);
905 if (adapter->set_mtu)
906 adapter->set_mtu(adapter, netdev->mtu);
908 if (!adapter->driver_mismatch)
909 netif_start_queue(netdev);
915 * netxen_nic_close - Disables a network interface entry point
917 static int netxen_nic_close(struct net_device *netdev)
919 struct netxen_adapter *adapter = netdev_priv(netdev);
921 struct netxen_cmd_buffer *cmd_buff;
922 struct netxen_skb_frag *buffrag;
924 netif_carrier_off(netdev);
925 netif_stop_queue(netdev);
927 netxen_nic_disable_int(adapter);
929 cmd_buff = adapter->cmd_buf_arr;
930 for (i = 0; i < adapter->max_tx_desc_count; i++) {
931 buffrag = cmd_buff->frag_array;
933 pci_unmap_single(adapter->pdev, buffrag->dma,
934 buffrag->length, PCI_DMA_TODEVICE);
937 for (j = 0; j < cmd_buff->frag_count; j++) {
940 pci_unmap_page(adapter->pdev, buffrag->dma,
946 /* Free the skb we received in netxen_nic_xmit_frame */
948 dev_kfree_skb_any(cmd_buff->skb);
949 cmd_buff->skb = NULL;
953 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
954 FLUSH_SCHEDULED_WORK();
955 del_timer_sync(&adapter->watchdog_timer);
961 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
963 struct netxen_adapter *adapter = netdev_priv(netdev);
964 struct netxen_hardware_context *hw = &adapter->ahw;
965 unsigned int first_seg_len = skb->len - skb->data_len;
966 struct netxen_skb_frag *buffrag;
970 u32 saved_producer = 0;
971 struct cmd_desc_type0 *hwdesc;
973 struct netxen_cmd_buffer *pbuf = NULL;
974 static int dropped_packet = 0;
976 u32 local_producer = 0;
977 u32 max_tx_desc_count = 0;
978 u32 last_cmd_consumer = 0;
981 adapter->stats.xmitcalled++;
982 frag_count = skb_shinfo(skb)->nr_frags + 1;
984 if (unlikely(skb->len <= 0)) {
985 dev_kfree_skb_any(skb);
986 adapter->stats.badskblen++;
990 if (frag_count > MAX_BUFFERS_PER_CMD) {
991 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
992 "too large, can handle only %d frags\n",
993 netxen_nic_driver_name, netdev->name,
994 frag_count, MAX_BUFFERS_PER_CMD);
995 adapter->stats.txdropped++;
996 if ((++dropped_packet & 0xff) == 0xff)
997 printk("%s: %s droppped packets = %d\n",
998 netxen_nic_driver_name, netdev->name,
1001 return NETDEV_TX_OK;
1005 * Everything is set up. Now, we just need to transmit it out.
1006 * Note that we have to copy the contents of buffer over to
1007 * right place. Later on, this can be optimized out by de-coupling the
1008 * producer index from the buffer index.
1010 retry_getting_window:
1011 spin_lock_bh(&adapter->tx_lock);
1012 if (adapter->total_threads >= MAX_XMIT_PRODUCERS) {
1013 spin_unlock_bh(&adapter->tx_lock);
1020 for (i = 0; i < 20; i++)
1021 cpu_relax(); /*This a nop instr on i386 */
1023 goto retry_getting_window;
1025 local_producer = adapter->cmd_producer;
1026 /* There 4 fragments per descriptor */
1027 no_of_desc = (frag_count + 3) >> 2;
1028 if (netdev->features & NETIF_F_TSO) {
1029 if (skb_shinfo(skb)->gso_size > 0) {
1032 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
1033 sizeof(struct ethhdr)) >
1034 (sizeof(struct cmd_desc_type0) - 2)) {
1039 k = adapter->cmd_producer;
1040 max_tx_desc_count = adapter->max_tx_desc_count;
1041 last_cmd_consumer = adapter->last_cmd_consumer;
1042 if ((k + no_of_desc) >=
1043 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
1044 last_cmd_consumer)) {
1045 netif_stop_queue(netdev);
1046 adapter->flags |= NETXEN_NETDEV_STATUS;
1047 spin_unlock_bh(&adapter->tx_lock);
1048 return NETDEV_TX_BUSY;
1050 k = get_index_range(k, max_tx_desc_count, no_of_desc);
1051 adapter->cmd_producer = k;
1052 adapter->total_threads++;
1053 adapter->num_threads++;
1055 spin_unlock_bh(&adapter->tx_lock);
1056 /* Copy the descriptors into the hardware */
1057 producer = local_producer;
1058 saved_producer = producer;
1059 hwdesc = &hw->cmd_desc_head[producer];
1060 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1061 /* Take skb->data itself */
1062 pbuf = &adapter->cmd_buf_arr[producer];
1063 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
1064 pbuf->mss = skb_shinfo(skb)->gso_size;
1065 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1070 pbuf->total_length = skb->len;
1072 pbuf->cmd = TX_ETHER_PKT;
1073 pbuf->frag_count = frag_count;
1074 pbuf->port = adapter->portnum;
1075 buffrag = &pbuf->frag_array[0];
1076 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
1078 buffrag->length = first_seg_len;
1079 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1080 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1081 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
1083 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
1084 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
1085 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1086 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1088 for (i = 1, k = 1; i < frag_count; i++, k++) {
1089 struct skb_frag_struct *frag;
1091 unsigned long offset;
1092 dma_addr_t temp_dma;
1094 /* move to next desc. if there is a need */
1095 if ((i & 0x3) == 0) {
1097 producer = get_next_index(producer,
1098 adapter->max_tx_desc_count);
1099 hwdesc = &hw->cmd_desc_head[producer];
1100 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1102 frag = &skb_shinfo(skb)->frags[i - 1];
1104 offset = frag->page_offset;
1107 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1108 len, PCI_DMA_TODEVICE);
1111 buffrag->dma = temp_dma;
1112 buffrag->length = temp_len;
1114 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
1117 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1118 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1121 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1122 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1125 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1126 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1129 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1130 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1135 producer = get_next_index(producer, adapter->max_tx_desc_count);
1137 /* might change opcode to TX_TCP_LSO */
1138 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1140 /* For LSO, we need to copy the MAC/IP/TCP headers into
1141 * the descriptor ring
1143 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1145 int hdr_len, first_hdr_len, more_hdr;
1146 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1147 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1148 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1151 first_hdr_len = hdr_len;
1154 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1155 hwdesc = &hw->cmd_desc_head[producer];
1157 /* copy the first 64 bytes */
1158 memcpy(((void *)hwdesc) + 2,
1159 (void *)(skb->data), first_hdr_len);
1160 producer = get_next_index(producer, max_tx_desc_count);
1163 hwdesc = &hw->cmd_desc_head[producer];
1164 /* copy the next 64 bytes - should be enough except
1165 * for pathological case
1167 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1171 producer = get_next_index(producer, max_tx_desc_count);
1175 i = netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
1177 hw->cmd_desc_head[saved_producer].flags_opcode =
1178 cpu_to_le16(hw->cmd_desc_head[saved_producer].flags_opcode);
1179 hw->cmd_desc_head[saved_producer].num_of_buffers_total_length =
1180 cpu_to_le32(hw->cmd_desc_head[saved_producer].
1181 num_of_buffers_total_length);
1183 spin_lock_bh(&adapter->tx_lock);
1184 adapter->stats.txbytes += i;
1186 /* Code to update the adapter considering how many producer threads
1187 are currently working */
1188 if ((--adapter->num_threads) == 0) {
1189 /* This is the last thread */
1190 u32 crb_producer = adapter->cmd_producer;
1191 netxen_nic_update_cmd_producer(adapter, crb_producer);
1193 adapter->total_threads = 0;
1196 adapter->stats.xmitfinished++;
1197 spin_unlock_bh(&adapter->tx_lock);
1199 netdev->trans_start = jiffies;
1201 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
1203 DPRINTK(INFO, "Done. Send\n");
1204 return NETDEV_TX_OK;
1207 static void netxen_watchdog(unsigned long v)
1209 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1211 SCHEDULE_WORK(&adapter->watchdog_task);
1214 static void netxen_tx_timeout(struct net_device *netdev)
1216 struct netxen_adapter *adapter = (struct netxen_adapter *)
1217 netdev_priv(netdev);
1218 SCHEDULE_WORK(&adapter->tx_timeout_task);
1221 static void netxen_tx_timeout_task(struct work_struct *work)
1223 struct netxen_adapter *adapter =
1224 container_of(work, struct netxen_adapter, tx_timeout_task);
1226 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1227 netxen_nic_driver_name, adapter->netdev->name);
1229 netxen_nic_close(adapter->netdev);
1230 netxen_nic_open(adapter->netdev);
1231 adapter->netdev->trans_start = jiffies;
1232 netif_wake_queue(adapter->netdev);
1236 netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
1240 DPRINTK(INFO, "Entered handle ISR\n");
1241 adapter->stats.ints++;
1243 netxen_nic_disable_int(adapter);
1245 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1246 if (netif_rx_schedule_prep(netdev)) {
1248 * Interrupts are already disabled.
1250 __netif_rx_schedule(netdev);
1252 static unsigned int intcount = 0;
1253 if ((++intcount & 0xfff) == 0xfff)
1255 "%s: %s interrupt %d while in poll\n",
1256 netxen_nic_driver_name, netdev->name,
1263 netxen_nic_enable_int(adapter);
1270 * netxen_intr - Interrupt Handler
1271 * @irq: interrupt number
1272 * data points to adapter stucture (which may be handling more than 1 port
1274 irqreturn_t netxen_intr(int irq, void *data)
1276 struct netxen_adapter *adapter;
1277 struct net_device *netdev;
1280 if (unlikely(!irq)) {
1281 return IRQ_NONE; /* Not our interrupt */
1284 adapter = (struct netxen_adapter *)data;
1285 netdev = adapter->netdev;
1287 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1288 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1289 /* not our interrupt */
1290 if ((our_int & (0x80 << adapter->portnum)) == 0)
1294 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1295 /* claim interrupt */
1296 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1297 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1298 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1302 if (netif_running(netdev))
1303 netxen_handle_int(adapter, netdev);
1308 static int netxen_nic_poll(struct net_device *netdev, int *budget)
1310 struct netxen_adapter *adapter = netdev_priv(netdev);
1311 int work_to_do = min(*budget, netdev->quota);
1317 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1320 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1322 * Fairness issue. This will give undue weight to the
1323 * receive context 0.
1327 * To avoid starvation, we give each of our receivers,
1328 * a fraction of the quota. Sometimes, it might happen that we
1329 * have enough quota to process every packet, but since all the
1330 * packets are on one context, it gets only half of the quota,
1331 * and ends up not processing it.
1333 this_work_done = netxen_process_rcv_ring(adapter, ctx,
1336 work_done += this_work_done;
1339 netdev->quota -= work_done;
1340 *budget -= work_done;
1342 if (work_done >= work_to_do && netxen_nic_rx_has_work(adapter) != 0)
1345 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1348 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1349 work_done, work_to_do);
1351 netif_rx_complete(netdev);
1352 netxen_nic_enable_int(adapter);
1358 #ifdef CONFIG_NET_POLL_CONTROLLER
1359 static void netxen_nic_poll_controller(struct net_device *netdev)
1361 struct netxen_adapter *adapter = netdev_priv(netdev);
1362 disable_irq(adapter->irq);
1363 netxen_intr(adapter->irq, adapter);
1364 enable_irq(adapter->irq);
1368 static struct pci_driver netxen_driver = {
1369 .name = netxen_nic_driver_name,
1370 .id_table = netxen_pci_tbl,
1371 .probe = netxen_nic_probe,
1372 .remove = __devexit_p(netxen_nic_remove)
1375 /* Driver Registration on NetXen card */
1377 static int __init netxen_init_module(void)
1379 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1382 return pci_register_driver(&netxen_driver);
1385 module_init(netxen_init_module);
1387 static void __exit netxen_exit_module(void)
1390 * Wait for some time to allow the dma to drain, if any.
1393 pci_unregister_driver(&netxen_driver);
1394 destroy_workqueue(netxen_workq);
1397 module_exit(netxen_exit_module);