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_nic_poll(struct napi_struct *napi, int budget);
67 #ifdef CONFIG_NET_POLL_CONTROLLER
68 static void netxen_nic_poll_controller(struct net_device *netdev);
70 static irqreturn_t netxen_intr(int irq, void *data);
71 static irqreturn_t netxen_msi_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);
90 * In netxen_nic_down(), we must wait for any pending callback requests into
91 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
92 * reenabled right after it is deleted in netxen_nic_down().
93 * FLUSH_SCHEDULED_WORK() does this synchronization.
95 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
96 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
97 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
98 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
99 * linkwatch_event() to be executed which also attempts to acquire the rtnl
100 * lock thus causing a deadlock.
103 static struct workqueue_struct *netxen_workq;
104 #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
105 #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
107 static void netxen_watchdog(unsigned long);
109 static void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
110 uint32_t crb_producer)
112 switch (adapter->portnum) {
114 writel(crb_producer, NETXEN_CRB_NORMALIZE
115 (adapter, CRB_CMD_PRODUCER_OFFSET));
118 writel(crb_producer, NETXEN_CRB_NORMALIZE
119 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
122 writel(crb_producer, NETXEN_CRB_NORMALIZE
123 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
126 writel(crb_producer, NETXEN_CRB_NORMALIZE
127 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
130 printk(KERN_WARNING "We tried to update "
131 "CRB_CMD_PRODUCER_OFFSET for invalid "
132 "PCI function id %d\n",
138 static void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
141 switch (adapter->portnum) {
143 writel(crb_consumer, NETXEN_CRB_NORMALIZE
144 (adapter, CRB_CMD_CONSUMER_OFFSET));
147 writel(crb_consumer, NETXEN_CRB_NORMALIZE
148 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
151 writel(crb_consumer, NETXEN_CRB_NORMALIZE
152 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
155 writel(crb_consumer, NETXEN_CRB_NORMALIZE
156 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
159 printk(KERN_WARNING "We tried to update "
160 "CRB_CMD_PRODUCER_OFFSET for invalid "
161 "PCI function id %d\n",
167 #define ADAPTER_LIST_SIZE 12
169 static uint32_t msi_tgt_status[4] = {
170 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
171 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3
174 static uint32_t sw_int_mask[4] = {
175 CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1,
176 CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3
179 static void netxen_nic_disable_int(struct netxen_adapter *adapter)
183 int port = adapter->portnum;
184 int pci_fn = adapter->ahw.pci_func;
186 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
187 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
189 if (adapter->intr_scheme != -1 &&
190 adapter->intr_scheme != INTR_SCHEME_PERPORT)
191 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
193 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
196 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
197 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
204 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
205 netxen_nic_driver_name);
208 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
209 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
210 msi_tgt_status[pci_fn]));
215 static void netxen_nic_enable_int(struct netxen_adapter *adapter)
218 int port = adapter->portnum;
220 DPRINTK(1, INFO, "Entered ISR Enable \n");
222 if (adapter->intr_scheme != -1 &&
223 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
224 switch (adapter->ahw.board_type) {
228 case NETXEN_NIC_XGBE:
236 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
239 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
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 __le64 mac_addr[FLASH_NUM_PORTS + 1];
288 int pci_func_id = PCI_FUNC(pdev->devfn);
289 DECLARE_MAC_BUF(mac);
291 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
293 if (pdev->class != 0x020000) {
294 printk(KERN_ERR"NetXen function %d, class %x will not "
295 "be enabled.\n",pci_func_id, pdev->class);
298 if ((err = pci_enable_device(pdev)))
300 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
302 goto err_out_disable_pdev;
305 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
306 goto err_out_disable_pdev;
308 pci_set_master(pdev);
309 if (pdev->revision == NX_P2_C1 &&
310 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
311 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
314 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
315 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
316 goto err_out_free_res;
322 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
324 printk(KERN_ERR"%s: Failed to allocate memory for the "
325 "device block.Check system memory resource"
326 " usage.\n", netxen_nic_driver_name);
327 goto err_out_free_res;
330 SET_NETDEV_DEV(netdev, &pdev->dev);
332 adapter = netdev->priv;
334 adapter->ahw.pdev = pdev;
335 adapter->ahw.pci_func = pci_func_id;
337 /* remap phys address */
338 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
339 mem_len = pci_resource_len(pdev, 0);
341 /* 128 Meg of memory */
342 if (mem_len == NETXEN_PCI_128MB_SIZE) {
343 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
344 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
345 SECOND_PAGE_GROUP_SIZE);
346 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
347 THIRD_PAGE_GROUP_SIZE);
348 first_page_group_start = FIRST_PAGE_GROUP_START;
349 first_page_group_end = FIRST_PAGE_GROUP_END;
350 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
351 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
352 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
353 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
354 first_page_group_start = 0;
355 first_page_group_end = 0;
358 goto err_out_free_netdev;
361 if ((!mem_ptr0 && mem_len == NETXEN_PCI_128MB_SIZE) ||
362 !mem_ptr1 || !mem_ptr2) {
364 "Cannot remap adapter memory aborting.:"
365 "0 -> %p, 1 -> %p, 2 -> %p\n",
366 mem_ptr0, mem_ptr1, mem_ptr2);
369 goto err_out_iounmap;
371 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
372 db_len = pci_resource_len(pdev, 4);
375 printk(KERN_ERR "%s: doorbell is disabled\n",
376 netxen_nic_driver_name);
378 goto err_out_iounmap;
380 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
383 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
385 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
386 netxen_nic_driver_name);
388 goto err_out_iounmap;
390 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
392 adapter->ahw.pci_base0 = mem_ptr0;
393 adapter->ahw.first_page_group_start = first_page_group_start;
394 adapter->ahw.first_page_group_end = first_page_group_end;
395 adapter->ahw.pci_base1 = mem_ptr1;
396 adapter->ahw.pci_base2 = mem_ptr2;
397 adapter->ahw.db_base = db_ptr;
398 adapter->ahw.db_len = db_len;
400 adapter->netdev = netdev;
401 adapter->pdev = pdev;
403 netif_napi_add(netdev, &adapter->napi,
404 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
406 /* this will be read from FW later */
407 adapter->intr_scheme = -1;
408 adapter->msi_mode = -1;
410 /* This will be reset for mezz cards */
411 adapter->portnum = pci_func_id;
412 adapter->status &= ~NETXEN_NETDEV_STATUS;
413 adapter->rx_csum = 1;
415 netdev->open = netxen_nic_open;
416 netdev->stop = netxen_nic_close;
417 netdev->hard_start_xmit = netxen_nic_xmit_frame;
418 netdev->get_stats = netxen_nic_get_stats;
419 netdev->set_multicast_list = netxen_nic_set_multi;
420 netdev->set_mac_address = netxen_nic_set_mac;
421 netdev->change_mtu = netxen_nic_change_mtu;
422 netdev->tx_timeout = netxen_tx_timeout;
423 netdev->watchdog_timeo = 2*HZ;
425 netxen_nic_change_mtu(netdev, netdev->mtu);
427 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
428 #ifdef CONFIG_NET_POLL_CONTROLLER
429 netdev->poll_controller = netxen_nic_poll_controller;
431 /* ScatterGather support */
432 netdev->features = NETIF_F_SG;
433 netdev->features |= NETIF_F_IP_CSUM;
434 netdev->features |= NETIF_F_TSO;
437 netdev->features |= NETIF_F_HIGHDMA;
439 if (pci_enable_msi(pdev))
440 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
442 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
444 netdev->irq = pdev->irq;
445 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
448 * Set the CRB window to invalid. If any register in window 0 is
449 * accessed it should set the window to 0 and then reset it to 1.
451 adapter->curr_window = 255;
453 /* initialize the adapter */
454 netxen_initialize_adapter_hw(adapter);
457 * Adapter in our case is quad port so initialize it before
458 * initializing the ports
461 netxen_initialize_adapter_ops(adapter);
463 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
464 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
465 (adapter->ahw.boardcfg.board_type ==
466 NETXEN_BRDTYPE_P2_SB31_2G))
467 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
469 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
470 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
471 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
473 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
474 if (cmd_buf_arr == NULL) {
476 "%s: Could not allocate cmd_buf_arr memory:%d\n",
477 netxen_nic_driver_name, (int)TX_RINGSIZE);
479 goto err_out_free_adapter;
481 memset(cmd_buf_arr, 0, TX_RINGSIZE);
482 adapter->cmd_buf_arr = cmd_buf_arr;
484 for (i = 0; i < MAX_RCV_CTX; ++i) {
485 recv_ctx = &adapter->recv_ctx[i];
486 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
487 rcv_desc = &recv_ctx->rcv_desc[ring];
488 switch (RCV_DESC_TYPE(ring)) {
489 case RCV_DESC_NORMAL:
490 rcv_desc->max_rx_desc_count =
491 adapter->max_rx_desc_count;
492 rcv_desc->flags = RCV_DESC_NORMAL;
493 rcv_desc->dma_size = RX_DMA_MAP_LEN;
494 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
498 rcv_desc->max_rx_desc_count =
499 adapter->max_jumbo_rx_desc_count;
500 rcv_desc->flags = RCV_DESC_JUMBO;
501 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
502 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
506 rcv_desc->max_rx_desc_count =
507 adapter->max_lro_rx_desc_count;
508 rcv_desc->flags = RCV_DESC_LRO;
509 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
510 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
514 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
515 vmalloc(RCV_BUFFSIZE);
517 if (rcv_desc->rx_buf_arr == NULL) {
518 printk(KERN_ERR "%s: Could not allocate "
519 "rcv_desc->rx_buf_arr memory:%d\n",
520 netxen_nic_driver_name,
523 goto err_out_free_rx_buffer;
525 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
530 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
532 /* Mezz cards have PCI function 0,2,3 enabled */
533 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB31_10G_IMEZ)
534 && (pci_func_id >= 2))
535 adapter->portnum = pci_func_id - 2;
538 if(adapter->portnum == 0) {
539 netxen_pinit_from_rom(adapter, 0);
541 netxen_load_firmware(adapter);
545 init_timer(&adapter->watchdog_timer);
546 adapter->ahw.xg_linkup = 0;
547 adapter->watchdog_timer.function = &netxen_watchdog;
548 adapter->watchdog_timer.data = (unsigned long)adapter;
549 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
550 adapter->ahw.pdev = pdev;
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 %s.\n",
579 netxen_nic_driver_name,
580 print_mac(mac, netdev->dev_addr));
582 if (adapter->macaddr_set)
583 adapter->macaddr_set(adapter,
588 if (adapter->portnum == 0) {
589 err = netxen_initialize_adapter_offload(adapter);
591 goto err_out_free_rx_buffer;
592 val = readl(NETXEN_CRB_NORMALIZE(adapter,
593 NETXEN_CAM_RAM(0x1fc)));
594 if (val == 0x55555555) {
595 /* This is the first boot after power up */
596 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
599 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
600 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
602 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
603 netxen_nic_driver_name);
605 val = readl(NETXEN_CRB_NORMALIZE(adapter,
606 NETXEN_ROMUSB_GLB_SW_RESET));
607 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
608 if (val != 0x80000f) {
609 /* clear the register for future unloads/loads */
610 writel(0, NETXEN_CRB_NORMALIZE(adapter,
611 NETXEN_CAM_RAM(0x1fc)));
612 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
614 goto err_out_free_dev;
618 /* clear the register for future unloads/loads */
619 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
620 printk(KERN_INFO "State: 0x%0x\n",
621 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
624 * Tell the hardware our version number.
626 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
627 | ((_NETXEN_NIC_LINUX_MINOR << 8))
628 | (_NETXEN_NIC_LINUX_SUBVERSION);
629 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
631 /* Unlock the HW, prompting the boot sequence */
633 NETXEN_CRB_NORMALIZE(adapter,
634 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
635 /* Handshake with the card before we register the devices. */
636 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
640 * See if the firmware gave us a virtual-physical port mapping.
642 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
644 physical_port[adapter->portnum] = i;
646 netif_carrier_off(netdev);
647 netif_stop_queue(netdev);
649 if ((err = register_netdev(netdev))) {
650 printk(KERN_ERR "%s: register_netdev failed port #%d"
651 " aborting\n", netxen_nic_driver_name,
654 goto err_out_free_dev;
657 pci_set_drvdata(pdev, adapter);
659 switch (adapter->ahw.board_type) {
661 printk(KERN_INFO "%s: QUAD GbE board initialized\n",
662 netxen_nic_driver_name);
665 case NETXEN_NIC_XGBE:
666 printk(KERN_INFO "%s: XGbE board initialized\n",
667 netxen_nic_driver_name);
671 adapter->driver_mismatch = 0;
676 if (adapter->portnum == 0)
677 netxen_free_adapter_offload(adapter);
679 err_out_free_rx_buffer:
680 for (i = 0; i < MAX_RCV_CTX; ++i) {
681 recv_ctx = &adapter->recv_ctx[i];
682 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
683 rcv_desc = &recv_ctx->rcv_desc[ring];
684 if (rcv_desc->rx_buf_arr != NULL) {
685 vfree(rcv_desc->rx_buf_arr);
686 rcv_desc->rx_buf_arr = NULL;
692 err_out_free_adapter:
693 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
694 pci_disable_msi(pdev);
696 pci_set_drvdata(pdev, NULL);
713 pci_release_regions(pdev);
715 err_out_disable_pdev:
716 pci_disable_device(pdev);
720 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
722 struct netxen_adapter *adapter;
723 struct net_device *netdev;
724 struct netxen_rx_buffer *buffer;
725 struct netxen_recv_context *recv_ctx;
726 struct netxen_rcv_desc_ctx *rcv_desc;
728 static int init_firmware_done = 0;
730 adapter = pci_get_drvdata(pdev);
734 netdev = adapter->netdev;
736 unregister_netdev(netdev);
738 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
739 init_firmware_done++;
740 netxen_free_hw_resources(adapter);
743 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
744 recv_ctx = &adapter->recv_ctx[ctxid];
745 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
746 rcv_desc = &recv_ctx->rcv_desc[ring];
747 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
748 buffer = &(rcv_desc->rx_buf_arr[i]);
749 if (buffer->state == NETXEN_BUFFER_FREE)
751 pci_unmap_single(pdev, buffer->dma,
754 if (buffer->skb != NULL)
755 dev_kfree_skb_any(buffer->skb);
757 vfree(rcv_desc->rx_buf_arr);
761 vfree(adapter->cmd_buf_arr);
763 if (adapter->portnum == 0) {
764 if (init_firmware_done) {
767 if (dma_watchdog_shutdown_request(adapter) == 1)
770 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
775 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
778 /* clear the register for future unloads/loads */
779 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
780 printk(KERN_INFO "State: 0x%0x\n",
781 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
783 /* leave the hw in the same state as reboot */
784 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
785 netxen_pinit_from_rom(adapter, 0);
787 netxen_load_firmware(adapter);
788 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
791 /* clear the register for future unloads/loads */
792 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
793 printk(KERN_INFO "State: 0x%0x\n",
794 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
798 if (dma_watchdog_shutdown_request(adapter) == 1)
801 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
806 netxen_free_adapter_offload(adapter);
808 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
814 free_irq(adapter->irq, adapter);
816 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
817 pci_disable_msi(pdev);
819 iounmap(adapter->ahw.db_base);
820 iounmap(adapter->ahw.pci_base0);
821 iounmap(adapter->ahw.pci_base1);
822 iounmap(adapter->ahw.pci_base2);
824 pci_release_regions(pdev);
825 pci_disable_device(pdev);
826 pci_set_drvdata(pdev, NULL);
832 * Called when a network interface is made active
833 * @returns 0 on success, negative value on failure
835 static int netxen_nic_open(struct net_device *netdev)
837 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
840 irq_handler_t handler;
841 unsigned long flags = IRQF_SAMPLE_RANDOM;
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 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
866 handler = netxen_msi_intr;
868 flags |= IRQF_SHARED;
869 handler = netxen_intr;
871 err = request_irq(adapter->irq, handler,
872 flags, netdev->name, adapter);
874 printk(KERN_ERR "request_irq failed with: %d\n", err);
875 netxen_free_hw_resources(adapter);
879 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
881 /* Done here again so that even if phantom sw overwrote it,
883 if (adapter->init_port
884 && adapter->init_port(adapter, adapter->portnum) != 0) {
885 printk(KERN_ERR "%s: Failed to initialize port %d\n",
886 netxen_nic_driver_name, adapter->portnum);
889 if (adapter->macaddr_set)
890 adapter->macaddr_set(adapter, netdev->dev_addr);
892 netxen_nic_set_link_parameters(adapter);
894 netxen_nic_set_multi(netdev);
895 if (adapter->set_mtu)
896 adapter->set_mtu(adapter, netdev->mtu);
898 if (!adapter->driver_mismatch)
899 mod_timer(&adapter->watchdog_timer, jiffies);
901 napi_enable(&adapter->napi);
902 netxen_nic_enable_int(adapter);
904 if (!adapter->driver_mismatch)
905 netif_start_queue(netdev);
911 * netxen_nic_close - Disables a network interface entry point
913 static int netxen_nic_close(struct net_device *netdev)
915 struct netxen_adapter *adapter = netdev_priv(netdev);
917 struct netxen_cmd_buffer *cmd_buff;
918 struct netxen_skb_frag *buffrag;
920 netif_carrier_off(netdev);
921 netif_stop_queue(netdev);
922 napi_disable(&adapter->napi);
924 if (adapter->stop_port)
925 adapter->stop_port(adapter);
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;
969 u32 producer, consumer;
970 u32 saved_producer = 0;
971 struct cmd_desc_type0 *hwdesc;
973 struct netxen_cmd_buffer *pbuf = NULL;
976 u32 num_txd = adapter->max_tx_desc_count;
978 frag_count = skb_shinfo(skb)->nr_frags + 1;
980 /* There 4 fragments per descriptor */
981 no_of_desc = (frag_count + 3) >> 2;
982 if (netdev->features & NETIF_F_TSO) {
983 if (skb_shinfo(skb)->gso_size > 0) {
986 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
987 sizeof(struct ethhdr)) >
988 (sizeof(struct cmd_desc_type0) - 2)) {
994 producer = adapter->cmd_producer;
996 consumer = adapter->last_cmd_consumer;
997 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
998 netif_stop_queue(netdev);
1000 return NETDEV_TX_BUSY;
1003 /* Copy the descriptors into the hardware */
1004 saved_producer = producer;
1005 hwdesc = &hw->cmd_desc_head[producer];
1006 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1007 /* Take skb->data itself */
1008 pbuf = &adapter->cmd_buf_arr[producer];
1009 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
1010 pbuf->mss = skb_shinfo(skb)->gso_size;
1011 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1016 pbuf->total_length = skb->len;
1018 pbuf->cmd = TX_ETHER_PKT;
1019 pbuf->frag_count = frag_count;
1020 pbuf->port = adapter->portnum;
1021 buffrag = &pbuf->frag_array[0];
1022 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
1024 buffrag->length = first_seg_len;
1025 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1026 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1027 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
1029 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
1030 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
1031 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1032 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1034 for (i = 1, k = 1; i < frag_count; i++, k++) {
1035 struct skb_frag_struct *frag;
1037 unsigned long offset;
1038 dma_addr_t temp_dma;
1040 /* move to next desc. if there is a need */
1041 if ((i & 0x3) == 0) {
1043 producer = get_next_index(producer, num_txd);
1044 hwdesc = &hw->cmd_desc_head[producer];
1045 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1046 pbuf = &adapter->cmd_buf_arr[producer];
1049 frag = &skb_shinfo(skb)->frags[i - 1];
1051 offset = frag->page_offset;
1054 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1055 len, PCI_DMA_TODEVICE);
1058 buffrag->dma = temp_dma;
1059 buffrag->length = temp_len;
1063 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1064 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1067 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1068 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1071 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1072 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1075 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1076 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1081 producer = get_next_index(producer, num_txd);
1083 /* might change opcode to TX_TCP_LSO */
1084 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1086 /* For LSO, we need to copy the MAC/IP/TCP headers into
1087 * the descriptor ring
1089 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1091 int hdr_len, first_hdr_len, more_hdr;
1092 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1093 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1094 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1097 first_hdr_len = hdr_len;
1100 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1101 hwdesc = &hw->cmd_desc_head[producer];
1102 pbuf = &adapter->cmd_buf_arr[producer];
1105 /* copy the first 64 bytes */
1106 memcpy(((void *)hwdesc) + 2,
1107 (void *)(skb->data), first_hdr_len);
1108 producer = get_next_index(producer, num_txd);
1111 hwdesc = &hw->cmd_desc_head[producer];
1112 pbuf = &adapter->cmd_buf_arr[producer];
1114 /* copy the next 64 bytes - should be enough except
1115 * for pathological case
1117 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1121 producer = get_next_index(producer, num_txd);
1125 adapter->cmd_producer = producer;
1126 adapter->stats.txbytes += skb->len;
1128 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
1130 adapter->stats.xmitcalled++;
1131 netdev->trans_start = jiffies;
1133 return NETDEV_TX_OK;
1136 static void netxen_watchdog(unsigned long v)
1138 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1140 SCHEDULE_WORK(&adapter->watchdog_task);
1143 static void netxen_tx_timeout(struct net_device *netdev)
1145 struct netxen_adapter *adapter = (struct netxen_adapter *)
1146 netdev_priv(netdev);
1147 SCHEDULE_WORK(&adapter->tx_timeout_task);
1150 static void netxen_tx_timeout_task(struct work_struct *work)
1152 struct netxen_adapter *adapter =
1153 container_of(work, struct netxen_adapter, tx_timeout_task);
1155 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1156 netxen_nic_driver_name, adapter->netdev->name);
1158 netxen_nic_disable_int(adapter);
1159 napi_disable(&adapter->napi);
1161 adapter->netdev->trans_start = jiffies;
1163 napi_enable(&adapter->napi);
1164 netxen_nic_enable_int(adapter);
1165 netif_wake_queue(adapter->netdev);
1169 netxen_handle_int(struct netxen_adapter *adapter)
1171 netxen_nic_disable_int(adapter);
1172 napi_schedule(&adapter->napi);
1175 irqreturn_t netxen_intr(int irq, void *data)
1177 struct netxen_adapter *adapter = data;
1180 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1181 /* not our interrupt */
1182 if ((our_int & (0x80 << adapter->portnum)) == 0)
1185 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1186 /* claim interrupt */
1187 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1188 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1191 netxen_handle_int(adapter);
1196 irqreturn_t netxen_msi_intr(int irq, void *data)
1198 struct netxen_adapter *adapter = data;
1200 netxen_handle_int(adapter);
1204 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1206 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1211 tx_complete = netxen_process_cmd_ring(adapter);
1214 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1216 * Fairness issue. This will give undue weight to the
1217 * receive context 0.
1221 * To avoid starvation, we give each of our receivers,
1222 * a fraction of the quota. Sometimes, it might happen that we
1223 * have enough quota to process every packet, but since all the
1224 * packets are on one context, it gets only half of the quota,
1225 * and ends up not processing it.
1227 work_done += netxen_process_rcv_ring(adapter, ctx,
1228 budget / MAX_RCV_CTX);
1231 if ((work_done < budget) && tx_complete) {
1232 netif_rx_complete(adapter->netdev, &adapter->napi);
1233 netxen_nic_enable_int(adapter);
1239 #ifdef CONFIG_NET_POLL_CONTROLLER
1240 static void netxen_nic_poll_controller(struct net_device *netdev)
1242 struct netxen_adapter *adapter = netdev_priv(netdev);
1243 disable_irq(adapter->irq);
1244 netxen_intr(adapter->irq, adapter);
1245 enable_irq(adapter->irq);
1249 static struct pci_driver netxen_driver = {
1250 .name = netxen_nic_driver_name,
1251 .id_table = netxen_pci_tbl,
1252 .probe = netxen_nic_probe,
1253 .remove = __devexit_p(netxen_nic_remove)
1256 /* Driver Registration on NetXen card */
1258 static int __init netxen_init_module(void)
1260 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1263 return pci_register_driver(&netxen_driver);
1266 module_init(netxen_init_module);
1268 static void __exit netxen_exit_module(void)
1271 * Wait for some time to allow the dma to drain, if any.
1274 pci_unregister_driver(&netxen_driver);
1275 destroy_workqueue(netxen_workq);
1278 module_exit(netxen_exit_module);