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 /* PCI Device ID Table */
74 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
75 {PCI_DEVICE(0x4040, 0x0001), PCI_DEVICE_CLASS(0x020000, ~0)},
76 {PCI_DEVICE(0x4040, 0x0002), PCI_DEVICE_CLASS(0x020000, ~0)},
77 {PCI_DEVICE(0x4040, 0x0003), PCI_DEVICE_CLASS(0x020000, ~0)},
78 {PCI_DEVICE(0x4040, 0x0004), PCI_DEVICE_CLASS(0x020000, ~0)},
79 {PCI_DEVICE(0x4040, 0x0005), PCI_DEVICE_CLASS(0x020000, ~0)},
80 {PCI_DEVICE(0x4040, 0x0024), PCI_DEVICE_CLASS(0x020000, ~0)},
81 {PCI_DEVICE(0x4040, 0x0025), PCI_DEVICE_CLASS(0x020000, ~0)},
85 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
88 * In netxen_nic_down(), we must wait for any pending callback requests into
89 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
90 * reenabled right after it is deleted in netxen_nic_down().
91 * FLUSH_SCHEDULED_WORK() does this synchronization.
93 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
94 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
95 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
96 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
97 * linkwatch_event() to be executed which also attempts to acquire the rtnl
98 * lock thus causing a deadlock.
101 static struct workqueue_struct *netxen_workq;
102 #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
103 #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
105 static void netxen_watchdog(unsigned long);
107 static void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
108 uint32_t crb_producer)
110 switch (adapter->portnum) {
112 writel(crb_producer, NETXEN_CRB_NORMALIZE
113 (adapter, CRB_CMD_PRODUCER_OFFSET));
116 writel(crb_producer, NETXEN_CRB_NORMALIZE
117 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
120 writel(crb_producer, NETXEN_CRB_NORMALIZE
121 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
124 writel(crb_producer, NETXEN_CRB_NORMALIZE
125 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
128 printk(KERN_WARNING "We tried to update "
129 "CRB_CMD_PRODUCER_OFFSET for invalid "
130 "PCI function id %d\n",
136 static void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
139 switch (adapter->portnum) {
141 writel(crb_consumer, NETXEN_CRB_NORMALIZE
142 (adapter, CRB_CMD_CONSUMER_OFFSET));
145 writel(crb_consumer, NETXEN_CRB_NORMALIZE
146 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
149 writel(crb_consumer, NETXEN_CRB_NORMALIZE
150 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
153 writel(crb_consumer, NETXEN_CRB_NORMALIZE
154 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
157 printk(KERN_WARNING "We tried to update "
158 "CRB_CMD_PRODUCER_OFFSET for invalid "
159 "PCI function id %d\n",
165 #define ADAPTER_LIST_SIZE 12
167 static uint32_t msi_tgt_status[4] = {
168 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
169 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3
172 static uint32_t sw_int_mask[4] = {
173 CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1,
174 CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3
177 static void netxen_nic_disable_int(struct netxen_adapter *adapter)
181 int port = adapter->portnum;
182 int pci_fn = adapter->ahw.pci_func;
184 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
185 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
187 if (adapter->intr_scheme != -1 &&
188 adapter->intr_scheme != INTR_SCHEME_PERPORT)
189 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
191 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
194 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
195 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
202 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
203 netxen_nic_driver_name);
206 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
207 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
208 msi_tgt_status[pci_fn]));
213 static void netxen_nic_enable_int(struct netxen_adapter *adapter)
216 int port = adapter->portnum;
218 DPRINTK(1, INFO, "Entered ISR Enable \n");
220 if (adapter->intr_scheme != -1 &&
221 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
222 switch (adapter->ahw.board_type) {
226 case NETXEN_NIC_XGBE:
234 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
237 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
239 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
241 if (adapter->intr_scheme != -1 &&
242 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
243 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
246 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
249 DPRINTK(1, INFO, "Done with enable Int\n");
255 * The Linux system will invoke this after identifying the vendor ID and
256 * device Id in the pci_tbl supported by this module.
258 * A quad port card has one operational PCI config space, (function 0),
259 * which is used to access all four ports.
261 * This routine will initialize the adapter, and setup the global parameters
262 * along with the port's specific structure.
265 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
267 struct net_device *netdev = NULL;
268 struct netxen_adapter *adapter = NULL;
269 void __iomem *mem_ptr0 = NULL;
270 void __iomem *mem_ptr1 = NULL;
271 void __iomem *mem_ptr2 = NULL;
272 unsigned long first_page_group_end;
273 unsigned long first_page_group_start;
276 u8 __iomem *db_ptr = NULL;
277 unsigned long mem_base, mem_len, db_base, db_len;
278 int pci_using_dac, i = 0, err;
280 struct netxen_recv_context *recv_ctx = NULL;
281 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
282 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
283 __le64 mac_addr[FLASH_NUM_PORTS + 1];
286 int pci_func_id = PCI_FUNC(pdev->devfn);
287 DECLARE_MAC_BUF(mac);
289 if (pci_func_id == 0)
290 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
292 if (pdev->class != 0x020000) {
293 printk(KERN_DEBUG "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_NETDEV_DEV(netdev, &pdev->dev);
331 adapter = netdev->priv;
333 adapter->ahw.pdev = pdev;
334 adapter->ahw.pci_func = pci_func_id;
336 /* remap phys address */
337 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
338 mem_len = pci_resource_len(pdev, 0);
340 /* 128 Meg of memory */
341 if (mem_len == NETXEN_PCI_128MB_SIZE) {
342 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
343 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
344 SECOND_PAGE_GROUP_SIZE);
345 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
346 THIRD_PAGE_GROUP_SIZE);
347 first_page_group_start = FIRST_PAGE_GROUP_START;
348 first_page_group_end = FIRST_PAGE_GROUP_END;
349 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
350 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
351 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
352 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
353 first_page_group_start = 0;
354 first_page_group_end = 0;
357 goto err_out_free_netdev;
360 if ((!mem_ptr0 && mem_len == NETXEN_PCI_128MB_SIZE) ||
361 !mem_ptr1 || !mem_ptr2) {
363 "Cannot remap adapter memory aborting.:"
364 "0 -> %p, 1 -> %p, 2 -> %p\n",
365 mem_ptr0, mem_ptr1, mem_ptr2);
368 goto err_out_iounmap;
370 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
371 db_len = pci_resource_len(pdev, 4);
374 printk(KERN_ERR "%s: doorbell is disabled\n",
375 netxen_nic_driver_name);
377 goto err_out_iounmap;
379 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
382 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
384 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
385 netxen_nic_driver_name);
387 goto err_out_iounmap;
389 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
391 adapter->ahw.pci_base0 = mem_ptr0;
392 adapter->ahw.first_page_group_start = first_page_group_start;
393 adapter->ahw.first_page_group_end = first_page_group_end;
394 adapter->ahw.pci_base1 = mem_ptr1;
395 adapter->ahw.pci_base2 = mem_ptr2;
396 adapter->ahw.db_base = db_ptr;
397 adapter->ahw.db_len = db_len;
399 adapter->netdev = netdev;
400 adapter->pdev = pdev;
402 netif_napi_add(netdev, &adapter->napi,
403 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
405 /* this will be read from FW later */
406 adapter->intr_scheme = -1;
407 adapter->msi_mode = -1;
409 /* This will be reset for mezz cards */
410 adapter->portnum = pci_func_id;
411 adapter->status &= ~NETXEN_NETDEV_STATUS;
412 adapter->rx_csum = 1;
414 netdev->open = netxen_nic_open;
415 netdev->stop = netxen_nic_close;
416 netdev->hard_start_xmit = netxen_nic_xmit_frame;
417 netdev->get_stats = netxen_nic_get_stats;
418 netdev->set_multicast_list = netxen_nic_set_multi;
419 netdev->set_mac_address = netxen_nic_set_mac;
420 netdev->change_mtu = netxen_nic_change_mtu;
421 netdev->tx_timeout = netxen_tx_timeout;
422 netdev->watchdog_timeo = 2*HZ;
424 netxen_nic_change_mtu(netdev, netdev->mtu);
426 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
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 if (netxen_nic_get_board_info(adapter) != 0) {
453 printk("%s: Error getting board config info.\n",
454 netxen_nic_driver_name);
456 goto err_out_iounmap;
460 * Adapter in our case is quad port so initialize it before
461 * initializing the ports
464 netxen_initialize_adapter_ops(adapter);
466 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
467 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
468 (adapter->ahw.boardcfg.board_type ==
469 NETXEN_BRDTYPE_P2_SB31_2G))
470 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
472 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
473 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
474 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
476 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
477 if (cmd_buf_arr == NULL) {
479 "%s: Could not allocate cmd_buf_arr memory:%d\n",
480 netxen_nic_driver_name, (int)TX_RINGSIZE);
482 goto err_out_free_adapter;
484 memset(cmd_buf_arr, 0, TX_RINGSIZE);
485 adapter->cmd_buf_arr = cmd_buf_arr;
487 for (i = 0; i < MAX_RCV_CTX; ++i) {
488 recv_ctx = &adapter->recv_ctx[i];
489 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
490 rcv_desc = &recv_ctx->rcv_desc[ring];
491 switch (RCV_DESC_TYPE(ring)) {
492 case RCV_DESC_NORMAL:
493 rcv_desc->max_rx_desc_count =
494 adapter->max_rx_desc_count;
495 rcv_desc->flags = RCV_DESC_NORMAL;
496 rcv_desc->dma_size = RX_DMA_MAP_LEN;
497 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
501 rcv_desc->max_rx_desc_count =
502 adapter->max_jumbo_rx_desc_count;
503 rcv_desc->flags = RCV_DESC_JUMBO;
504 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
505 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
509 rcv_desc->max_rx_desc_count =
510 adapter->max_lro_rx_desc_count;
511 rcv_desc->flags = RCV_DESC_LRO;
512 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
513 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
517 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
518 vmalloc(RCV_BUFFSIZE);
520 if (rcv_desc->rx_buf_arr == NULL) {
521 printk(KERN_ERR "%s: Could not allocate "
522 "rcv_desc->rx_buf_arr memory:%d\n",
523 netxen_nic_driver_name,
526 goto err_out_free_rx_buffer;
528 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
533 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
535 /* Mezz cards have PCI function 0,2,3 enabled */
536 switch (adapter->ahw.boardcfg.board_type) {
537 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
538 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
539 if (pci_func_id >= 2)
540 adapter->portnum = pci_func_id - 2;
546 init_timer(&adapter->watchdog_timer);
547 adapter->ahw.xg_linkup = 0;
548 adapter->watchdog_timer.function = &netxen_watchdog;
549 adapter->watchdog_timer.data = (unsigned long)adapter;
550 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
551 adapter->ahw.pdev = pdev;
552 adapter->ahw.revision_id = pdev->revision;
554 /* make sure Window == 1 */
555 netxen_nic_pci_change_crbwindow(adapter, 1);
557 netxen_nic_update_cmd_producer(adapter, 0);
558 netxen_nic_update_cmd_consumer(adapter, 0);
559 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
561 if (netxen_is_flash_supported(adapter) == 0 &&
562 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
568 unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum];
569 netdev->dev_addr[0] = *(p + 5);
570 netdev->dev_addr[1] = *(p + 4);
571 netdev->dev_addr[2] = *(p + 3);
572 netdev->dev_addr[3] = *(p + 2);
573 netdev->dev_addr[4] = *(p + 1);
574 netdev->dev_addr[5] = *(p + 0);
576 memcpy(netdev->perm_addr, netdev->dev_addr,
578 if (!is_valid_ether_addr(netdev->perm_addr)) {
579 printk(KERN_ERR "%s: Bad MAC address %s.\n",
580 netxen_nic_driver_name,
581 print_mac(mac, netdev->dev_addr));
583 if (adapter->macaddr_set)
584 adapter->macaddr_set(adapter,
589 if (adapter->portnum == 0) {
590 err = netxen_initialize_adapter_offload(adapter);
592 goto err_out_free_rx_buffer;
593 val = readl(NETXEN_CRB_NORMALIZE(adapter,
594 NETXEN_CAM_RAM(0x1fc)));
595 if (val == 0x55555555) {
596 /* This is the first boot after power up */
597 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
600 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
601 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
603 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
604 netxen_nic_driver_name);
606 val = readl(NETXEN_CRB_NORMALIZE(adapter,
607 NETXEN_ROMUSB_GLB_SW_RESET));
608 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
609 if (val != 0x80000f) {
610 /* clear the register for future unloads/loads */
611 writel(0, NETXEN_CRB_NORMALIZE(adapter,
612 NETXEN_CAM_RAM(0x1fc)));
613 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
615 goto err_out_free_dev;
618 writel(0, NETXEN_CRB_NORMALIZE(adapter,
620 netxen_pinit_from_rom(adapter, 0);
622 netxen_load_firmware(adapter);
623 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
626 /* clear the register for future unloads/loads */
627 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
628 dev_info(&pdev->dev, "cmdpeg state: 0x%0x\n",
629 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
632 * Tell the hardware our version number.
634 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
635 | ((_NETXEN_NIC_LINUX_MINOR << 8))
636 | (_NETXEN_NIC_LINUX_SUBVERSION);
637 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
639 /* Unlock the HW, prompting the boot sequence */
641 NETXEN_CRB_NORMALIZE(adapter,
642 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
643 /* Handshake with the card before we register the devices. */
644 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
648 * See if the firmware gave us a virtual-physical port mapping.
650 adapter->physical_port = adapter->portnum;
651 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
653 adapter->physical_port = i;
655 netif_carrier_off(netdev);
656 netif_stop_queue(netdev);
658 if ((err = register_netdev(netdev))) {
659 printk(KERN_ERR "%s: register_netdev failed port #%d"
660 " aborting\n", netxen_nic_driver_name,
663 goto err_out_free_dev;
666 netxen_nic_flash_print(adapter);
667 pci_set_drvdata(pdev, adapter);
672 if (adapter->portnum == 0)
673 netxen_free_adapter_offload(adapter);
675 err_out_free_rx_buffer:
676 for (i = 0; i < MAX_RCV_CTX; ++i) {
677 recv_ctx = &adapter->recv_ctx[i];
678 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
679 rcv_desc = &recv_ctx->rcv_desc[ring];
680 if (rcv_desc->rx_buf_arr != NULL) {
681 vfree(rcv_desc->rx_buf_arr);
682 rcv_desc->rx_buf_arr = NULL;
688 err_out_free_adapter:
689 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
690 pci_disable_msi(pdev);
692 pci_set_drvdata(pdev, NULL);
709 pci_release_regions(pdev);
711 err_out_disable_pdev:
712 pci_disable_device(pdev);
716 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
718 struct netxen_adapter *adapter;
719 struct net_device *netdev;
720 struct netxen_rx_buffer *buffer;
721 struct netxen_recv_context *recv_ctx;
722 struct netxen_rcv_desc_ctx *rcv_desc;
724 static int init_firmware_done = 0;
726 adapter = pci_get_drvdata(pdev);
730 netdev = adapter->netdev;
732 unregister_netdev(netdev);
734 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
735 init_firmware_done++;
736 netxen_free_hw_resources(adapter);
739 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
740 recv_ctx = &adapter->recv_ctx[ctxid];
741 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
742 rcv_desc = &recv_ctx->rcv_desc[ring];
743 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
744 buffer = &(rcv_desc->rx_buf_arr[i]);
745 if (buffer->state == NETXEN_BUFFER_FREE)
747 pci_unmap_single(pdev, buffer->dma,
750 if (buffer->skb != NULL)
751 dev_kfree_skb_any(buffer->skb);
753 vfree(rcv_desc->rx_buf_arr);
757 vfree(adapter->cmd_buf_arr);
759 if (adapter->portnum == 0)
760 netxen_free_adapter_offload(adapter);
763 free_irq(adapter->irq, adapter);
765 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
766 pci_disable_msi(pdev);
768 iounmap(adapter->ahw.db_base);
769 iounmap(adapter->ahw.pci_base0);
770 iounmap(adapter->ahw.pci_base1);
771 iounmap(adapter->ahw.pci_base2);
773 pci_release_regions(pdev);
774 pci_disable_device(pdev);
775 pci_set_drvdata(pdev, NULL);
781 * Called when a network interface is made active
782 * @returns 0 on success, negative value on failure
784 static int netxen_nic_open(struct net_device *netdev)
786 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
789 irq_handler_t handler;
790 unsigned long flags = IRQF_SAMPLE_RANDOM;
792 if (adapter->driver_mismatch)
795 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
796 err = netxen_init_firmware(adapter);
798 printk(KERN_ERR "Failed to init firmware\n");
802 /* setup all the resources for the Phantom... */
803 /* this include the descriptors for rcv, tx, and status */
804 netxen_nic_clear_stats(adapter);
805 err = netxen_nic_hw_resources(adapter);
807 printk(KERN_ERR "Error in setting hw resources:%d\n",
811 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
812 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
813 netxen_post_rx_buffers(adapter, ctx, ring);
815 adapter->irq = adapter->ahw.pdev->irq;
816 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
817 handler = netxen_msi_intr;
819 flags |= IRQF_SHARED;
820 handler = netxen_intr;
822 err = request_irq(adapter->irq, handler,
823 flags, netdev->name, adapter);
825 printk(KERN_ERR "request_irq failed with: %d\n", err);
826 netxen_free_hw_resources(adapter);
830 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
832 /* Done here again so that even if phantom sw overwrote it,
834 if (adapter->init_port
835 && adapter->init_port(adapter, adapter->portnum) != 0) {
836 printk(KERN_ERR "%s: Failed to initialize port %d\n",
837 netxen_nic_driver_name, adapter->portnum);
840 if (adapter->macaddr_set)
841 adapter->macaddr_set(adapter, netdev->dev_addr);
843 netxen_nic_set_link_parameters(adapter);
845 netxen_nic_set_multi(netdev);
846 if (adapter->set_mtu)
847 adapter->set_mtu(adapter, netdev->mtu);
849 mod_timer(&adapter->watchdog_timer, jiffies);
851 napi_enable(&adapter->napi);
852 netxen_nic_enable_int(adapter);
854 netif_start_queue(netdev);
860 * netxen_nic_close - Disables a network interface entry point
862 static int netxen_nic_close(struct net_device *netdev)
864 struct netxen_adapter *adapter = netdev_priv(netdev);
866 struct netxen_cmd_buffer *cmd_buff;
867 struct netxen_skb_frag *buffrag;
869 netif_carrier_off(netdev);
870 netif_stop_queue(netdev);
871 napi_disable(&adapter->napi);
873 if (adapter->stop_port)
874 adapter->stop_port(adapter);
876 netxen_nic_disable_int(adapter);
878 cmd_buff = adapter->cmd_buf_arr;
879 for (i = 0; i < adapter->max_tx_desc_count; i++) {
880 buffrag = cmd_buff->frag_array;
882 pci_unmap_single(adapter->pdev, buffrag->dma,
883 buffrag->length, PCI_DMA_TODEVICE);
886 for (j = 0; j < cmd_buff->frag_count; j++) {
889 pci_unmap_page(adapter->pdev, buffrag->dma,
895 /* Free the skb we received in netxen_nic_xmit_frame */
897 dev_kfree_skb_any(cmd_buff->skb);
898 cmd_buff->skb = NULL;
902 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
903 FLUSH_SCHEDULED_WORK();
904 del_timer_sync(&adapter->watchdog_timer);
910 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
912 struct netxen_adapter *adapter = netdev_priv(netdev);
913 struct netxen_hardware_context *hw = &adapter->ahw;
914 unsigned int first_seg_len = skb->len - skb->data_len;
915 struct netxen_skb_frag *buffrag;
918 u32 producer, consumer;
919 u32 saved_producer = 0;
920 struct cmd_desc_type0 *hwdesc;
922 struct netxen_cmd_buffer *pbuf = NULL;
925 u32 num_txd = adapter->max_tx_desc_count;
927 frag_count = skb_shinfo(skb)->nr_frags + 1;
929 /* There 4 fragments per descriptor */
930 no_of_desc = (frag_count + 3) >> 2;
931 if (netdev->features & NETIF_F_TSO) {
932 if (skb_shinfo(skb)->gso_size > 0) {
935 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
936 sizeof(struct ethhdr)) >
937 (sizeof(struct cmd_desc_type0) - 2)) {
943 producer = adapter->cmd_producer;
945 consumer = adapter->last_cmd_consumer;
946 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
947 netif_stop_queue(netdev);
949 return NETDEV_TX_BUSY;
952 /* Copy the descriptors into the hardware */
953 saved_producer = producer;
954 hwdesc = &hw->cmd_desc_head[producer];
955 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
956 /* Take skb->data itself */
957 pbuf = &adapter->cmd_buf_arr[producer];
958 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
959 pbuf->mss = skb_shinfo(skb)->gso_size;
960 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
965 pbuf->total_length = skb->len;
967 pbuf->cmd = TX_ETHER_PKT;
968 pbuf->frag_count = frag_count;
969 pbuf->port = adapter->portnum;
970 buffrag = &pbuf->frag_array[0];
971 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
973 buffrag->length = first_seg_len;
974 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
975 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
976 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
978 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
979 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
980 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
981 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
983 for (i = 1, k = 1; i < frag_count; i++, k++) {
984 struct skb_frag_struct *frag;
986 unsigned long offset;
989 /* move to next desc. if there is a need */
990 if ((i & 0x3) == 0) {
992 producer = get_next_index(producer, num_txd);
993 hwdesc = &hw->cmd_desc_head[producer];
994 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
995 pbuf = &adapter->cmd_buf_arr[producer];
998 frag = &skb_shinfo(skb)->frags[i - 1];
1000 offset = frag->page_offset;
1003 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1004 len, PCI_DMA_TODEVICE);
1007 buffrag->dma = temp_dma;
1008 buffrag->length = temp_len;
1012 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1013 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1016 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1017 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1020 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1021 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1024 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1025 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1030 producer = get_next_index(producer, num_txd);
1032 /* might change opcode to TX_TCP_LSO */
1033 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1035 /* For LSO, we need to copy the MAC/IP/TCP headers into
1036 * the descriptor ring
1038 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1040 int hdr_len, first_hdr_len, more_hdr;
1041 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1042 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1043 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1046 first_hdr_len = hdr_len;
1049 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1050 hwdesc = &hw->cmd_desc_head[producer];
1051 pbuf = &adapter->cmd_buf_arr[producer];
1054 /* copy the first 64 bytes */
1055 memcpy(((void *)hwdesc) + 2,
1056 (void *)(skb->data), first_hdr_len);
1057 producer = get_next_index(producer, num_txd);
1060 hwdesc = &hw->cmd_desc_head[producer];
1061 pbuf = &adapter->cmd_buf_arr[producer];
1063 /* copy the next 64 bytes - should be enough except
1064 * for pathological case
1066 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1070 producer = get_next_index(producer, num_txd);
1074 adapter->cmd_producer = producer;
1075 adapter->stats.txbytes += skb->len;
1077 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
1079 adapter->stats.xmitcalled++;
1080 netdev->trans_start = jiffies;
1082 return NETDEV_TX_OK;
1085 static void netxen_watchdog(unsigned long v)
1087 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1089 SCHEDULE_WORK(&adapter->watchdog_task);
1092 static void netxen_tx_timeout(struct net_device *netdev)
1094 struct netxen_adapter *adapter = (struct netxen_adapter *)
1095 netdev_priv(netdev);
1096 SCHEDULE_WORK(&adapter->tx_timeout_task);
1099 static void netxen_tx_timeout_task(struct work_struct *work)
1101 struct netxen_adapter *adapter =
1102 container_of(work, struct netxen_adapter, tx_timeout_task);
1104 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1105 netxen_nic_driver_name, adapter->netdev->name);
1107 netxen_nic_disable_int(adapter);
1108 napi_disable(&adapter->napi);
1110 adapter->netdev->trans_start = jiffies;
1112 napi_enable(&adapter->napi);
1113 netxen_nic_enable_int(adapter);
1114 netif_wake_queue(adapter->netdev);
1118 netxen_handle_int(struct netxen_adapter *adapter)
1120 netxen_nic_disable_int(adapter);
1121 napi_schedule(&adapter->napi);
1124 irqreturn_t netxen_intr(int irq, void *data)
1126 struct netxen_adapter *adapter = data;
1129 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1130 /* not our interrupt */
1131 if ((our_int & (0x80 << adapter->portnum)) == 0)
1134 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1135 /* claim interrupt */
1136 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1137 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1140 netxen_handle_int(adapter);
1145 irqreturn_t netxen_msi_intr(int irq, void *data)
1147 struct netxen_adapter *adapter = data;
1149 netxen_handle_int(adapter);
1153 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1155 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1160 tx_complete = netxen_process_cmd_ring(adapter);
1163 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1165 * Fairness issue. This will give undue weight to the
1166 * receive context 0.
1170 * To avoid starvation, we give each of our receivers,
1171 * a fraction of the quota. Sometimes, it might happen that we
1172 * have enough quota to process every packet, but since all the
1173 * packets are on one context, it gets only half of the quota,
1174 * and ends up not processing it.
1176 work_done += netxen_process_rcv_ring(adapter, ctx,
1177 budget / MAX_RCV_CTX);
1180 if ((work_done < budget) && tx_complete) {
1181 netif_rx_complete(adapter->netdev, &adapter->napi);
1182 netxen_nic_enable_int(adapter);
1188 #ifdef CONFIG_NET_POLL_CONTROLLER
1189 static void netxen_nic_poll_controller(struct net_device *netdev)
1191 struct netxen_adapter *adapter = netdev_priv(netdev);
1192 disable_irq(adapter->irq);
1193 netxen_intr(adapter->irq, adapter);
1194 enable_irq(adapter->irq);
1198 static struct pci_driver netxen_driver = {
1199 .name = netxen_nic_driver_name,
1200 .id_table = netxen_pci_tbl,
1201 .probe = netxen_nic_probe,
1202 .remove = __devexit_p(netxen_nic_remove)
1205 /* Driver Registration on NetXen card */
1207 static int __init netxen_init_module(void)
1209 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1212 return pci_register_driver(&netxen_driver);
1215 module_init(netxen_init_module);
1217 static void __exit netxen_exit_module(void)
1220 * Wait for some time to allow the dma to drain, if any.
1223 pci_unregister_driver(&netxen_driver);
1224 destroy_workqueue(netxen_workq);
1227 module_exit(netxen_exit_module);