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 #define ENTRY(device) \
75 {PCI_DEVICE(0x4040, (device)), \
76 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
78 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
89 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
92 * In netxen_nic_down(), we must wait for any pending callback requests into
93 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
94 * reenabled right after it is deleted in netxen_nic_down().
95 * FLUSH_SCHEDULED_WORK() does this synchronization.
97 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
98 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
99 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
100 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
101 * linkwatch_event() to be executed which also attempts to acquire the rtnl
102 * lock thus causing a deadlock.
105 static struct workqueue_struct *netxen_workq;
106 #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
107 #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
109 static void netxen_watchdog(unsigned long);
111 static void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
112 uint32_t crb_producer)
114 switch (adapter->portnum) {
116 writel(crb_producer, NETXEN_CRB_NORMALIZE
117 (adapter, CRB_CMD_PRODUCER_OFFSET));
120 writel(crb_producer, NETXEN_CRB_NORMALIZE
121 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
124 writel(crb_producer, NETXEN_CRB_NORMALIZE
125 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
128 writel(crb_producer, NETXEN_CRB_NORMALIZE
129 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
132 printk(KERN_WARNING "We tried to update "
133 "CRB_CMD_PRODUCER_OFFSET for invalid "
134 "PCI function id %d\n",
140 static void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
143 switch (adapter->portnum) {
145 writel(crb_consumer, NETXEN_CRB_NORMALIZE
146 (adapter, CRB_CMD_CONSUMER_OFFSET));
149 writel(crb_consumer, NETXEN_CRB_NORMALIZE
150 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
153 writel(crb_consumer, NETXEN_CRB_NORMALIZE
154 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
157 writel(crb_consumer, NETXEN_CRB_NORMALIZE
158 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
161 printk(KERN_WARNING "We tried to update "
162 "CRB_CMD_PRODUCER_OFFSET for invalid "
163 "PCI function id %d\n",
169 #define ADAPTER_LIST_SIZE 12
171 static uint32_t msi_tgt_status[4] = {
172 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
173 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3
176 static uint32_t sw_int_mask[4] = {
177 CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1,
178 CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3
181 static void netxen_nic_disable_int(struct netxen_adapter *adapter)
185 int port = adapter->portnum;
186 int pci_fn = adapter->ahw.pci_func;
188 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
189 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
191 if (adapter->intr_scheme != -1 &&
192 adapter->intr_scheme != INTR_SCHEME_PERPORT)
193 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
195 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
198 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
199 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
206 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
207 netxen_nic_driver_name);
210 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
211 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
212 msi_tgt_status[pci_fn]));
217 static void netxen_nic_enable_int(struct netxen_adapter *adapter)
220 int port = adapter->portnum;
222 DPRINTK(1, INFO, "Entered ISR Enable \n");
224 if (adapter->intr_scheme != -1 &&
225 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
226 switch (adapter->ahw.board_type) {
230 case NETXEN_NIC_XGBE:
238 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
241 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
243 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
245 if (adapter->intr_scheme != -1 &&
246 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
247 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
250 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
253 DPRINTK(1, INFO, "Done with enable Int\n");
259 * The Linux system will invoke this after identifying the vendor ID and
260 * device Id in the pci_tbl supported by this module.
262 * A quad port card has one operational PCI config space, (function 0),
263 * which is used to access all four ports.
265 * This routine will initialize the adapter, and setup the global parameters
266 * along with the port's specific structure.
269 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
271 struct net_device *netdev = NULL;
272 struct netxen_adapter *adapter = NULL;
273 void __iomem *mem_ptr0 = NULL;
274 void __iomem *mem_ptr1 = NULL;
275 void __iomem *mem_ptr2 = NULL;
276 unsigned long first_page_group_end;
277 unsigned long first_page_group_start;
280 u8 __iomem *db_ptr = NULL;
281 unsigned long mem_base, mem_len, db_base, db_len;
282 int pci_using_dac, i = 0, err;
284 struct netxen_recv_context *recv_ctx = NULL;
285 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
286 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
287 __le64 mac_addr[FLASH_NUM_PORTS + 1];
290 int pci_func_id = PCI_FUNC(pdev->devfn);
291 DECLARE_MAC_BUF(mac);
293 if (pci_func_id == 0)
294 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
296 if (pdev->class != 0x020000) {
297 printk(KERN_DEBUG "NetXen function %d, class %x will not "
298 "be enabled.\n",pci_func_id, pdev->class);
301 if ((err = pci_enable_device(pdev)))
303 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
305 goto err_out_disable_pdev;
308 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
309 goto err_out_disable_pdev;
311 pci_set_master(pdev);
312 if (pdev->revision == NX_P2_C1 &&
313 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
314 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
317 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
318 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
319 goto err_out_free_res;
325 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
327 printk(KERN_ERR"%s: Failed to allocate memory for the "
328 "device block.Check system memory resource"
329 " usage.\n", netxen_nic_driver_name);
330 goto err_out_free_res;
333 SET_NETDEV_DEV(netdev, &pdev->dev);
335 adapter = netdev->priv;
337 adapter->ahw.pdev = pdev;
338 adapter->ahw.pci_func = pci_func_id;
340 /* remap phys address */
341 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
342 mem_len = pci_resource_len(pdev, 0);
344 /* 128 Meg of memory */
345 if (mem_len == NETXEN_PCI_128MB_SIZE) {
346 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
347 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
348 SECOND_PAGE_GROUP_SIZE);
349 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
350 THIRD_PAGE_GROUP_SIZE);
351 first_page_group_start = FIRST_PAGE_GROUP_START;
352 first_page_group_end = FIRST_PAGE_GROUP_END;
353 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
354 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
355 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
356 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
357 first_page_group_start = 0;
358 first_page_group_end = 0;
361 goto err_out_free_netdev;
364 if ((!mem_ptr0 && mem_len == NETXEN_PCI_128MB_SIZE) ||
365 !mem_ptr1 || !mem_ptr2) {
367 "Cannot remap adapter memory aborting.:"
368 "0 -> %p, 1 -> %p, 2 -> %p\n",
369 mem_ptr0, mem_ptr1, mem_ptr2);
372 goto err_out_iounmap;
374 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
375 db_len = pci_resource_len(pdev, 4);
378 printk(KERN_ERR "%s: doorbell is disabled\n",
379 netxen_nic_driver_name);
381 goto err_out_iounmap;
383 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
386 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
388 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
389 netxen_nic_driver_name);
391 goto err_out_iounmap;
393 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
395 adapter->ahw.pci_base0 = mem_ptr0;
396 adapter->ahw.first_page_group_start = first_page_group_start;
397 adapter->ahw.first_page_group_end = first_page_group_end;
398 adapter->ahw.pci_base1 = mem_ptr1;
399 adapter->ahw.pci_base2 = mem_ptr2;
400 adapter->ahw.db_base = db_ptr;
401 adapter->ahw.db_len = db_len;
403 adapter->netdev = netdev;
404 adapter->pdev = pdev;
406 netif_napi_add(netdev, &adapter->napi,
407 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
409 /* this will be read from FW later */
410 adapter->intr_scheme = -1;
411 adapter->msi_mode = -1;
413 /* This will be reset for mezz cards */
414 adapter->portnum = pci_func_id;
415 adapter->status &= ~NETXEN_NETDEV_STATUS;
416 adapter->rx_csum = 1;
418 netdev->open = netxen_nic_open;
419 netdev->stop = netxen_nic_close;
420 netdev->hard_start_xmit = netxen_nic_xmit_frame;
421 netdev->get_stats = netxen_nic_get_stats;
422 netdev->set_multicast_list = netxen_nic_set_multi;
423 netdev->set_mac_address = netxen_nic_set_mac;
424 netdev->change_mtu = netxen_nic_change_mtu;
425 netdev->tx_timeout = netxen_tx_timeout;
426 netdev->watchdog_timeo = 2*HZ;
428 netxen_nic_change_mtu(netdev, netdev->mtu);
430 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
431 #ifdef CONFIG_NET_POLL_CONTROLLER
432 netdev->poll_controller = netxen_nic_poll_controller;
434 /* ScatterGather support */
435 netdev->features = NETIF_F_SG;
436 netdev->features |= NETIF_F_IP_CSUM;
437 netdev->features |= NETIF_F_TSO;
440 netdev->features |= NETIF_F_HIGHDMA;
442 if (pci_enable_msi(pdev))
443 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
445 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
447 netdev->irq = pdev->irq;
448 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
451 * Set the CRB window to invalid. If any register in window 0 is
452 * accessed it should set the window to 0 and then reset it to 1.
454 adapter->curr_window = 255;
456 if (netxen_nic_get_board_info(adapter) != 0) {
457 printk("%s: Error getting board config info.\n",
458 netxen_nic_driver_name);
460 goto err_out_iounmap;
464 * Adapter in our case is quad port so initialize it before
465 * initializing the ports
468 netxen_initialize_adapter_ops(adapter);
470 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
471 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
472 (adapter->ahw.boardcfg.board_type ==
473 NETXEN_BRDTYPE_P2_SB31_2G))
474 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
476 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
477 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
478 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
480 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
481 if (cmd_buf_arr == NULL) {
483 "%s: Could not allocate cmd_buf_arr memory:%d\n",
484 netxen_nic_driver_name, (int)TX_RINGSIZE);
486 goto err_out_free_adapter;
488 memset(cmd_buf_arr, 0, TX_RINGSIZE);
489 adapter->cmd_buf_arr = cmd_buf_arr;
491 for (i = 0; i < MAX_RCV_CTX; ++i) {
492 recv_ctx = &adapter->recv_ctx[i];
493 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
494 rcv_desc = &recv_ctx->rcv_desc[ring];
495 switch (RCV_DESC_TYPE(ring)) {
496 case RCV_DESC_NORMAL:
497 rcv_desc->max_rx_desc_count =
498 adapter->max_rx_desc_count;
499 rcv_desc->flags = RCV_DESC_NORMAL;
500 rcv_desc->dma_size = RX_DMA_MAP_LEN;
501 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
505 rcv_desc->max_rx_desc_count =
506 adapter->max_jumbo_rx_desc_count;
507 rcv_desc->flags = RCV_DESC_JUMBO;
508 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
509 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
513 rcv_desc->max_rx_desc_count =
514 adapter->max_lro_rx_desc_count;
515 rcv_desc->flags = RCV_DESC_LRO;
516 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
517 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
521 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
522 vmalloc(RCV_BUFFSIZE);
524 if (rcv_desc->rx_buf_arr == NULL) {
525 printk(KERN_ERR "%s: Could not allocate "
526 "rcv_desc->rx_buf_arr memory:%d\n",
527 netxen_nic_driver_name,
530 goto err_out_free_rx_buffer;
532 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
537 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
539 /* Mezz cards have PCI function 0,2,3 enabled */
540 switch (adapter->ahw.boardcfg.board_type) {
541 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
542 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
543 if (pci_func_id >= 2)
544 adapter->portnum = pci_func_id - 2;
550 init_timer(&adapter->watchdog_timer);
551 adapter->ahw.xg_linkup = 0;
552 adapter->watchdog_timer.function = &netxen_watchdog;
553 adapter->watchdog_timer.data = (unsigned long)adapter;
554 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
555 adapter->ahw.pdev = pdev;
556 adapter->ahw.revision_id = pdev->revision;
558 /* make sure Window == 1 */
559 netxen_nic_pci_change_crbwindow(adapter, 1);
561 netxen_nic_update_cmd_producer(adapter, 0);
562 netxen_nic_update_cmd_consumer(adapter, 0);
563 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
565 if (netxen_is_flash_supported(adapter) == 0 &&
566 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
572 unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum];
573 netdev->dev_addr[0] = *(p + 5);
574 netdev->dev_addr[1] = *(p + 4);
575 netdev->dev_addr[2] = *(p + 3);
576 netdev->dev_addr[3] = *(p + 2);
577 netdev->dev_addr[4] = *(p + 1);
578 netdev->dev_addr[5] = *(p + 0);
580 memcpy(netdev->perm_addr, netdev->dev_addr,
582 if (!is_valid_ether_addr(netdev->perm_addr)) {
583 printk(KERN_ERR "%s: Bad MAC address %s.\n",
584 netxen_nic_driver_name,
585 print_mac(mac, netdev->dev_addr));
587 if (adapter->macaddr_set)
588 adapter->macaddr_set(adapter,
593 if (adapter->portnum == 0) {
594 err = netxen_initialize_adapter_offload(adapter);
596 goto err_out_free_rx_buffer;
597 val = readl(NETXEN_CRB_NORMALIZE(adapter,
598 NETXEN_CAM_RAM(0x1fc)));
599 if (val == 0x55555555) {
600 /* This is the first boot after power up */
601 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
604 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
605 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
607 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
608 netxen_nic_driver_name);
610 val = readl(NETXEN_CRB_NORMALIZE(adapter,
611 NETXEN_ROMUSB_GLB_SW_RESET));
612 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
613 if (val != 0x80000f) {
614 /* clear the register for future unloads/loads */
615 writel(0, NETXEN_CRB_NORMALIZE(adapter,
616 NETXEN_CAM_RAM(0x1fc)));
617 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
619 goto err_out_free_dev;
622 writel(0, NETXEN_CRB_NORMALIZE(adapter,
624 netxen_pinit_from_rom(adapter, 0);
626 netxen_load_firmware(adapter);
627 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
630 /* clear the register for future unloads/loads */
631 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
632 dev_info(&pdev->dev, "cmdpeg state: 0x%0x\n",
633 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
636 * Tell the hardware our version number.
638 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
639 | ((_NETXEN_NIC_LINUX_MINOR << 8))
640 | (_NETXEN_NIC_LINUX_SUBVERSION);
641 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
643 /* Unlock the HW, prompting the boot sequence */
645 NETXEN_CRB_NORMALIZE(adapter,
646 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
647 /* Handshake with the card before we register the devices. */
648 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
652 * See if the firmware gave us a virtual-physical port mapping.
654 adapter->physical_port = adapter->portnum;
655 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
657 adapter->physical_port = i;
659 netif_carrier_off(netdev);
660 netif_stop_queue(netdev);
662 if ((err = register_netdev(netdev))) {
663 printk(KERN_ERR "%s: register_netdev failed port #%d"
664 " aborting\n", netxen_nic_driver_name,
667 goto err_out_free_dev;
670 netxen_nic_flash_print(adapter);
671 pci_set_drvdata(pdev, adapter);
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 netxen_free_adapter_offload(adapter);
767 free_irq(adapter->irq, adapter);
769 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
770 pci_disable_msi(pdev);
772 iounmap(adapter->ahw.db_base);
773 iounmap(adapter->ahw.pci_base0);
774 iounmap(adapter->ahw.pci_base1);
775 iounmap(adapter->ahw.pci_base2);
777 pci_release_regions(pdev);
778 pci_disable_device(pdev);
779 pci_set_drvdata(pdev, NULL);
785 * Called when a network interface is made active
786 * @returns 0 on success, negative value on failure
788 static int netxen_nic_open(struct net_device *netdev)
790 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
793 irq_handler_t handler;
794 unsigned long flags = IRQF_SAMPLE_RANDOM;
796 if (adapter->driver_mismatch)
799 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
800 err = netxen_init_firmware(adapter);
802 printk(KERN_ERR "Failed to init firmware\n");
806 /* setup all the resources for the Phantom... */
807 /* this include the descriptors for rcv, tx, and status */
808 netxen_nic_clear_stats(adapter);
809 err = netxen_nic_hw_resources(adapter);
811 printk(KERN_ERR "Error in setting hw resources:%d\n",
815 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
816 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
817 netxen_post_rx_buffers(adapter, ctx, ring);
819 adapter->irq = adapter->ahw.pdev->irq;
820 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
821 handler = netxen_msi_intr;
823 flags |= IRQF_SHARED;
824 handler = netxen_intr;
826 err = request_irq(adapter->irq, handler,
827 flags, netdev->name, adapter);
829 printk(KERN_ERR "request_irq failed with: %d\n", err);
830 netxen_free_hw_resources(adapter);
834 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
836 /* Done here again so that even if phantom sw overwrote it,
838 if (adapter->init_port
839 && adapter->init_port(adapter, adapter->portnum) != 0) {
840 printk(KERN_ERR "%s: Failed to initialize port %d\n",
841 netxen_nic_driver_name, adapter->portnum);
844 if (adapter->macaddr_set)
845 adapter->macaddr_set(adapter, netdev->dev_addr);
847 netxen_nic_set_link_parameters(adapter);
849 netxen_nic_set_multi(netdev);
850 if (adapter->set_mtu)
851 adapter->set_mtu(adapter, netdev->mtu);
853 mod_timer(&adapter->watchdog_timer, jiffies);
855 napi_enable(&adapter->napi);
856 netxen_nic_enable_int(adapter);
858 netif_start_queue(netdev);
864 * netxen_nic_close - Disables a network interface entry point
866 static int netxen_nic_close(struct net_device *netdev)
868 struct netxen_adapter *adapter = netdev_priv(netdev);
870 struct netxen_cmd_buffer *cmd_buff;
871 struct netxen_skb_frag *buffrag;
873 netif_carrier_off(netdev);
874 netif_stop_queue(netdev);
875 napi_disable(&adapter->napi);
877 if (adapter->stop_port)
878 adapter->stop_port(adapter);
880 netxen_nic_disable_int(adapter);
882 cmd_buff = adapter->cmd_buf_arr;
883 for (i = 0; i < adapter->max_tx_desc_count; i++) {
884 buffrag = cmd_buff->frag_array;
886 pci_unmap_single(adapter->pdev, buffrag->dma,
887 buffrag->length, PCI_DMA_TODEVICE);
890 for (j = 0; j < cmd_buff->frag_count; j++) {
893 pci_unmap_page(adapter->pdev, buffrag->dma,
899 /* Free the skb we received in netxen_nic_xmit_frame */
901 dev_kfree_skb_any(cmd_buff->skb);
902 cmd_buff->skb = NULL;
906 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
907 FLUSH_SCHEDULED_WORK();
908 del_timer_sync(&adapter->watchdog_timer);
914 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
916 struct netxen_adapter *adapter = netdev_priv(netdev);
917 struct netxen_hardware_context *hw = &adapter->ahw;
918 unsigned int first_seg_len = skb->len - skb->data_len;
919 struct netxen_skb_frag *buffrag;
922 u32 producer, consumer;
923 u32 saved_producer = 0;
924 struct cmd_desc_type0 *hwdesc;
926 struct netxen_cmd_buffer *pbuf = NULL;
929 u32 num_txd = adapter->max_tx_desc_count;
931 frag_count = skb_shinfo(skb)->nr_frags + 1;
933 /* There 4 fragments per descriptor */
934 no_of_desc = (frag_count + 3) >> 2;
935 if (netdev->features & NETIF_F_TSO) {
936 if (skb_shinfo(skb)->gso_size > 0) {
939 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
940 sizeof(struct ethhdr)) >
941 (sizeof(struct cmd_desc_type0) - 2)) {
947 producer = adapter->cmd_producer;
949 consumer = adapter->last_cmd_consumer;
950 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
951 netif_stop_queue(netdev);
953 return NETDEV_TX_BUSY;
956 /* Copy the descriptors into the hardware */
957 saved_producer = producer;
958 hwdesc = &hw->cmd_desc_head[producer];
959 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
960 /* Take skb->data itself */
961 pbuf = &adapter->cmd_buf_arr[producer];
962 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
963 pbuf->mss = skb_shinfo(skb)->gso_size;
964 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
969 pbuf->total_length = skb->len;
971 pbuf->cmd = TX_ETHER_PKT;
972 pbuf->frag_count = frag_count;
973 pbuf->port = adapter->portnum;
974 buffrag = &pbuf->frag_array[0];
975 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
977 buffrag->length = first_seg_len;
978 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
979 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
980 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
982 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
983 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
984 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
985 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
987 for (i = 1, k = 1; i < frag_count; i++, k++) {
988 struct skb_frag_struct *frag;
990 unsigned long offset;
993 /* move to next desc. if there is a need */
994 if ((i & 0x3) == 0) {
996 producer = get_next_index(producer, num_txd);
997 hwdesc = &hw->cmd_desc_head[producer];
998 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
999 pbuf = &adapter->cmd_buf_arr[producer];
1002 frag = &skb_shinfo(skb)->frags[i - 1];
1004 offset = frag->page_offset;
1007 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1008 len, PCI_DMA_TODEVICE);
1011 buffrag->dma = temp_dma;
1012 buffrag->length = temp_len;
1016 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1017 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1020 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1021 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1024 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1025 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1028 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1029 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1034 producer = get_next_index(producer, num_txd);
1036 /* might change opcode to TX_TCP_LSO */
1037 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1039 /* For LSO, we need to copy the MAC/IP/TCP headers into
1040 * the descriptor ring
1042 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1044 int hdr_len, first_hdr_len, more_hdr;
1045 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1046 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1047 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1050 first_hdr_len = hdr_len;
1053 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1054 hwdesc = &hw->cmd_desc_head[producer];
1055 pbuf = &adapter->cmd_buf_arr[producer];
1058 /* copy the first 64 bytes */
1059 memcpy(((void *)hwdesc) + 2,
1060 (void *)(skb->data), first_hdr_len);
1061 producer = get_next_index(producer, num_txd);
1064 hwdesc = &hw->cmd_desc_head[producer];
1065 pbuf = &adapter->cmd_buf_arr[producer];
1067 /* copy the next 64 bytes - should be enough except
1068 * for pathological case
1070 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1074 producer = get_next_index(producer, num_txd);
1078 adapter->cmd_producer = producer;
1079 adapter->stats.txbytes += skb->len;
1081 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
1083 adapter->stats.xmitcalled++;
1084 netdev->trans_start = jiffies;
1086 return NETDEV_TX_OK;
1089 static void netxen_watchdog(unsigned long v)
1091 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1093 SCHEDULE_WORK(&adapter->watchdog_task);
1096 static void netxen_tx_timeout(struct net_device *netdev)
1098 struct netxen_adapter *adapter = (struct netxen_adapter *)
1099 netdev_priv(netdev);
1100 SCHEDULE_WORK(&adapter->tx_timeout_task);
1103 static void netxen_tx_timeout_task(struct work_struct *work)
1105 struct netxen_adapter *adapter =
1106 container_of(work, struct netxen_adapter, tx_timeout_task);
1108 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1109 netxen_nic_driver_name, adapter->netdev->name);
1111 netxen_nic_disable_int(adapter);
1112 napi_disable(&adapter->napi);
1114 adapter->netdev->trans_start = jiffies;
1116 napi_enable(&adapter->napi);
1117 netxen_nic_enable_int(adapter);
1118 netif_wake_queue(adapter->netdev);
1122 netxen_handle_int(struct netxen_adapter *adapter)
1124 netxen_nic_disable_int(adapter);
1125 napi_schedule(&adapter->napi);
1128 irqreturn_t netxen_intr(int irq, void *data)
1130 struct netxen_adapter *adapter = data;
1133 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1134 /* not our interrupt */
1135 if ((our_int & (0x80 << adapter->portnum)) == 0)
1138 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1139 /* claim interrupt */
1140 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1141 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1144 netxen_handle_int(adapter);
1149 irqreturn_t netxen_msi_intr(int irq, void *data)
1151 struct netxen_adapter *adapter = data;
1153 netxen_handle_int(adapter);
1157 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1159 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1164 tx_complete = netxen_process_cmd_ring(adapter);
1167 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1169 * Fairness issue. This will give undue weight to the
1170 * receive context 0.
1174 * To avoid starvation, we give each of our receivers,
1175 * a fraction of the quota. Sometimes, it might happen that we
1176 * have enough quota to process every packet, but since all the
1177 * packets are on one context, it gets only half of the quota,
1178 * and ends up not processing it.
1180 work_done += netxen_process_rcv_ring(adapter, ctx,
1181 budget / MAX_RCV_CTX);
1184 if ((work_done < budget) && tx_complete) {
1185 netif_rx_complete(adapter->netdev, &adapter->napi);
1186 netxen_nic_enable_int(adapter);
1192 #ifdef CONFIG_NET_POLL_CONTROLLER
1193 static void netxen_nic_poll_controller(struct net_device *netdev)
1195 struct netxen_adapter *adapter = netdev_priv(netdev);
1196 disable_irq(adapter->irq);
1197 netxen_intr(adapter->irq, adapter);
1198 enable_irq(adapter->irq);
1202 static struct pci_driver netxen_driver = {
1203 .name = netxen_nic_driver_name,
1204 .id_table = netxen_pci_tbl,
1205 .probe = netxen_nic_probe,
1206 .remove = __devexit_p(netxen_nic_remove)
1209 /* Driver Registration on NetXen card */
1211 static int __init netxen_init_module(void)
1213 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1216 return pci_register_driver(&netxen_driver);
1219 module_init(netxen_init_module);
1221 static void __exit netxen_exit_module(void)
1224 * Wait for some time to allow the dma to drain, if any.
1227 pci_unregister_driver(&netxen_driver);
1228 destroy_workqueue(netxen_workq);
1231 module_exit(netxen_exit_module);