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 #define DEFINE_GLOBAL_RECV_CRB
40 #include "netxen_nic_phan_reg.h"
42 #include <linux/dma-mapping.h>
43 #include <linux/vmalloc.h>
45 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
49 char netxen_nic_driver_name[] = "netxen-nic";
50 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
51 NETXEN_NIC_LINUX_VERSIONID;
53 #define NETXEN_NETDEV_WEIGHT 120
54 #define NETXEN_ADAPTER_UP_MAGIC 777
55 #define NETXEN_NIC_PEG_TUNE 0
57 u8 nx_p2_id = NX_P2_C0;
59 #define DMA_32BIT_MASK 0x00000000ffffffffULL
60 #define DMA_35BIT_MASK 0x00000007ffffffffULL
62 /* Local functions to NetXen NIC driver */
63 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
64 const struct pci_device_id *ent);
65 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
66 static int netxen_nic_open(struct net_device *netdev);
67 static int netxen_nic_close(struct net_device *netdev);
68 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
69 static void netxen_tx_timeout(struct net_device *netdev);
70 static void netxen_tx_timeout_task(struct work_struct *work);
71 static void netxen_watchdog(unsigned long);
72 static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
73 static int netxen_nic_poll(struct net_device *dev, int *budget);
74 #ifdef CONFIG_NET_POLL_CONTROLLER
75 static void netxen_nic_poll_controller(struct net_device *netdev);
77 static irqreturn_t netxen_intr(int irq, void *data);
79 /* PCI Device ID Table */
80 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
81 {PCI_DEVICE(0x4040, 0x0001)},
82 {PCI_DEVICE(0x4040, 0x0002)},
83 {PCI_DEVICE(0x4040, 0x0003)},
84 {PCI_DEVICE(0x4040, 0x0004)},
85 {PCI_DEVICE(0x4040, 0x0005)},
86 {PCI_DEVICE(0x4040, 0x0024)},
87 {PCI_DEVICE(0x4040, 0x0025)},
91 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
93 struct workqueue_struct *netxen_workq;
94 static void netxen_watchdog(unsigned long);
99 * The Linux system will invoke this after identifying the vendor ID and
100 * device Id in the pci_tbl supported by this module.
102 * A quad port card has one operational PCI config space, (function 0),
103 * which is used to access all four ports.
105 * This routine will initialize the adapter, and setup the global parameters
106 * along with the port's specific structure.
109 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
111 struct net_device *netdev = NULL;
112 struct netxen_adapter *adapter = NULL;
113 struct netxen_port *port = NULL;
114 void __iomem *mem_ptr0 = NULL;
115 void __iomem *mem_ptr1 = NULL;
116 void __iomem *mem_ptr2 = NULL;
118 u8 __iomem *db_ptr = NULL;
119 unsigned long mem_base, mem_len, db_base, db_len;
120 int pci_using_dac, i, err;
122 struct netxen_recv_context *recv_ctx = NULL;
123 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
124 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
125 u64 mac_addr[FLASH_NUM_PORTS + 1];
128 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
129 /* In current scheme, we use only PCI function 0 */
130 if (PCI_FUNC(pdev->devfn) != 0) {
131 DPRINTK(ERR, "NetXen function %d will not be enabled.\n",
132 PCI_FUNC(pdev->devfn));
135 if ((err = pci_enable_device(pdev)))
137 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
139 goto err_out_disable_pdev;
142 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
143 goto err_out_disable_pdev;
145 pci_set_master(pdev);
146 pci_read_config_byte(pdev, PCI_REVISION_ID, &nx_p2_id);
147 if (nx_p2_id == NX_P2_C1 &&
148 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
149 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
152 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
153 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
154 goto err_out_free_res;
159 /* remap phys address */
160 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
161 mem_len = pci_resource_len(pdev, 0);
163 /* 128 Meg of memory */
164 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
166 ioremap(mem_base + SECOND_PAGE_GROUP_START, SECOND_PAGE_GROUP_SIZE);
168 ioremap(mem_base + THIRD_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
170 if ((mem_ptr0 == 0UL) || (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
172 "Cannot remap adapter memory aborting.:"
173 "0 -> %p, 1 -> %p, 2 -> %p\n",
174 mem_ptr0, mem_ptr1, mem_ptr2);
177 goto err_out_iounmap;
179 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
180 db_len = pci_resource_len(pdev, 4);
183 printk(KERN_ERR "%s: doorbell is disabled\n",
184 netxen_nic_driver_name);
186 goto err_out_iounmap;
188 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
191 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
193 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
194 netxen_nic_driver_name);
196 goto err_out_iounmap;
198 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
201 * Allocate a adapter structure which will manage all the initialization
202 * as well as the common resources for all ports...
203 * all the ports will have pointer to this adapter as well as Adapter
204 * will have pointers of all the ports structures.
207 /* One adapter structure for all 4 ports.... */
208 adapter = kzalloc(sizeof(struct netxen_adapter), GFP_KERNEL);
209 if (adapter == NULL) {
210 printk(KERN_ERR "%s: Could not allocate adapter memory:%d\n",
211 netxen_nic_driver_name,
212 (int)sizeof(struct netxen_adapter));
214 goto err_out_dbunmap;
217 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS;
218 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
219 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
220 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
222 pci_set_drvdata(pdev, adapter);
224 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
225 if (cmd_buf_arr == NULL) {
227 "%s: Could not allocate cmd_buf_arr memory:%d\n",
228 netxen_nic_driver_name, (int)TX_RINGSIZE);
230 goto err_out_free_adapter;
232 memset(cmd_buf_arr, 0, TX_RINGSIZE);
234 for (i = 0; i < MAX_RCV_CTX; ++i) {
235 recv_ctx = &adapter->recv_ctx[i];
236 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
237 rcv_desc = &recv_ctx->rcv_desc[ring];
238 switch (RCV_DESC_TYPE(ring)) {
239 case RCV_DESC_NORMAL:
240 rcv_desc->max_rx_desc_count =
241 adapter->max_rx_desc_count;
242 rcv_desc->flags = RCV_DESC_NORMAL;
243 rcv_desc->dma_size = RX_DMA_MAP_LEN;
244 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
248 rcv_desc->max_rx_desc_count =
249 adapter->max_jumbo_rx_desc_count;
250 rcv_desc->flags = RCV_DESC_JUMBO;
251 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
252 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
256 rcv_desc->max_rx_desc_count =
257 adapter->max_lro_rx_desc_count;
258 rcv_desc->flags = RCV_DESC_LRO;
259 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
260 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
264 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
265 vmalloc(RCV_BUFFSIZE);
267 if (rcv_desc->rx_buf_arr == NULL) {
268 printk(KERN_ERR "%s: Could not allocate"
269 "rcv_desc->rx_buf_arr memory:%d\n",
270 netxen_nic_driver_name,
273 goto err_out_free_rx_buffer;
275 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
280 adapter->cmd_buf_arr = cmd_buf_arr;
281 adapter->ahw.pci_base0 = mem_ptr0;
282 adapter->ahw.pci_base1 = mem_ptr1;
283 adapter->ahw.pci_base2 = mem_ptr2;
284 adapter->ahw.db_base = db_ptr;
285 adapter->ahw.db_len = db_len;
286 spin_lock_init(&adapter->tx_lock);
287 spin_lock_init(&adapter->lock);
288 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
290 netxen_pinit_from_rom(adapter, 0);
292 netxen_load_firmware(adapter);
296 * Set the CRB window to invalid. If any register in window 0 is
297 * accessed it should set the window to 0 and then reset it to 1.
299 adapter->curr_window = 255;
301 * Adapter in our case is quad port so initialize it before
302 * initializing the ports
304 netxen_initialize_adapter_hw(adapter); /* initialize the adapter */
306 netxen_initialize_adapter_ops(adapter);
308 init_timer(&adapter->watchdog_timer);
309 adapter->ahw.xg_linkup = 0;
310 adapter->watchdog_timer.function = &netxen_watchdog;
311 adapter->watchdog_timer.data = (unsigned long)adapter;
312 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
313 adapter->ahw.pdev = pdev;
314 adapter->proc_cmd_buf_counter = 0;
315 adapter->ahw.revision_id = nx_p2_id;
317 if (pci_enable_msi(pdev)) {
318 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
319 printk(KERN_WARNING "%s: unable to allocate MSI interrupt"
320 " error\n", netxen_nic_driver_name);
322 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
324 if (netxen_is_flash_supported(adapter) == 0 &&
325 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
331 * Initialize all the CRB registers here.
333 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
334 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_CONSUMER_OFFSET));
335 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
337 /* do this before waking up pegs so that we have valid dummy dma addr */
338 err = netxen_initialize_adapter_offload(adapter);
340 goto err_out_free_dev;
343 /* Unlock the HW, prompting the boot sequence */
345 NETXEN_CRB_NORMALIZE(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
347 /* Handshake with the card before we register the devices. */
348 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
350 /* initialize the all the ports */
351 adapter->active_ports = 0;
353 for (i = 0; i < adapter->ahw.max_ports; i++) {
354 netdev = alloc_etherdev(sizeof(struct netxen_port));
356 printk(KERN_ERR "%s: could not allocate netdev for port"
357 " %d\n", netxen_nic_driver_name, i + 1);
358 goto err_out_free_dev;
361 SET_MODULE_OWNER(netdev);
362 SET_NETDEV_DEV(netdev, &pdev->dev);
364 port = netdev_priv(netdev);
365 port->netdev = netdev;
367 port->adapter = adapter;
368 port->portnum = i; /* Gigabit port number from 0-3 */
370 netdev->open = netxen_nic_open;
371 netdev->stop = netxen_nic_close;
372 netdev->hard_start_xmit = netxen_nic_xmit_frame;
373 netdev->get_stats = netxen_nic_get_stats;
374 netdev->set_multicast_list = netxen_nic_set_multi;
375 netdev->set_mac_address = netxen_nic_set_mac;
376 netdev->change_mtu = netxen_nic_change_mtu;
377 netdev->tx_timeout = netxen_tx_timeout;
378 netdev->watchdog_timeo = HZ;
380 netxen_nic_change_mtu(netdev, netdev->mtu);
382 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
383 netdev->poll = netxen_nic_poll;
384 netdev->weight = NETXEN_NETDEV_WEIGHT;
385 #ifdef CONFIG_NET_POLL_CONTROLLER
386 netdev->poll_controller = netxen_nic_poll_controller;
388 /* ScatterGather support */
389 netdev->features = NETIF_F_SG;
390 netdev->features |= NETIF_F_IP_CSUM;
391 netdev->features |= NETIF_F_TSO;
394 netdev->features |= NETIF_F_HIGHDMA;
397 unsigned char *p = (unsigned char *)&mac_addr[i];
398 netdev->dev_addr[0] = *(p + 5);
399 netdev->dev_addr[1] = *(p + 4);
400 netdev->dev_addr[2] = *(p + 3);
401 netdev->dev_addr[3] = *(p + 2);
402 netdev->dev_addr[4] = *(p + 1);
403 netdev->dev_addr[5] = *(p + 0);
405 memcpy(netdev->perm_addr, netdev->dev_addr,
407 if (!is_valid_ether_addr(netdev->perm_addr)) {
408 printk(KERN_ERR "%s: Bad MAC address "
409 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
410 netxen_nic_driver_name,
416 netdev->dev_addr[5]);
418 if (adapter->macaddr_set)
419 adapter->macaddr_set(port,
423 INIT_WORK(&port->tx_timeout_task, netxen_tx_timeout_task);
424 netif_carrier_off(netdev);
425 netif_stop_queue(netdev);
427 if ((err = register_netdev(netdev))) {
428 printk(KERN_ERR "%s: register_netdev failed port #%d"
429 " aborting\n", netxen_nic_driver_name, i + 1);
432 goto err_out_free_dev;
434 adapter->port_count++;
435 adapter->port[i] = port;
437 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
438 netxen_pinit_from_rom(adapter, 0);
440 netxen_load_firmware(adapter);
441 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
443 * delay a while to ensure that the Pegs are up & running.
444 * Otherwise, we might see some flaky behaviour.
448 switch (adapter->ahw.board_type) {
450 printk("%s: QUAD GbE board initialized\n",
451 netxen_nic_driver_name);
454 case NETXEN_NIC_XGBE:
455 printk("%s: XGbE board initialized\n", netxen_nic_driver_name);
459 adapter->driver_mismatch = 0;
464 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
465 pci_disable_msi(pdev);
466 for (i = 0; i < adapter->port_count; i++) {
467 port = adapter->port[i];
468 if ((port) && (port->netdev)) {
469 unregister_netdev(port->netdev);
470 free_netdev(port->netdev);
474 netxen_free_adapter_offload(adapter);
476 err_out_free_rx_buffer:
477 for (i = 0; i < MAX_RCV_CTX; ++i) {
478 recv_ctx = &adapter->recv_ctx[i];
479 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
480 rcv_desc = &recv_ctx->rcv_desc[ring];
481 if (rcv_desc->rx_buf_arr != NULL) {
482 vfree(rcv_desc->rx_buf_arr);
483 rcv_desc->rx_buf_arr = NULL;
489 err_out_free_adapter:
490 pci_set_drvdata(pdev, NULL);
506 pci_release_regions(pdev);
507 err_out_disable_pdev:
508 pci_disable_device(pdev);
512 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
514 struct netxen_adapter *adapter;
515 struct netxen_port *port;
516 struct netxen_rx_buffer *buffer;
517 struct netxen_recv_context *recv_ctx;
518 struct netxen_rcv_desc_ctx *rcv_desc;
522 adapter = pci_get_drvdata(pdev);
527 free_irq(adapter->irq, adapter);
528 netxen_nic_stop_all_ports(adapter);
529 /* leave the hw in the same state as reboot */
530 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
531 netxen_pinit_from_rom(adapter, 0);
533 netxen_load_firmware(adapter);
534 netxen_free_adapter_offload(adapter);
536 mdelay(1000); /* Delay for a while to drain the DMA engines */
537 for (i = 0; i < adapter->port_count; i++) {
538 port = adapter->port[i];
539 if ((port) && (port->netdev)) {
540 unregister_netdev(port->netdev);
541 free_netdev(port->netdev);
545 if ((adapter->flags & NETXEN_NIC_MSI_ENABLED))
546 pci_disable_msi(pdev);
547 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
548 netxen_free_hw_resources(adapter);
550 iounmap(adapter->ahw.db_base);
551 iounmap(adapter->ahw.pci_base0);
552 iounmap(adapter->ahw.pci_base1);
553 iounmap(adapter->ahw.pci_base2);
555 pci_release_regions(pdev);
556 pci_disable_device(pdev);
557 pci_set_drvdata(pdev, NULL);
559 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
560 recv_ctx = &adapter->recv_ctx[ctxid];
561 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
562 rcv_desc = &recv_ctx->rcv_desc[ring];
563 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
564 buffer = &(rcv_desc->rx_buf_arr[i]);
565 if (buffer->state == NETXEN_BUFFER_FREE)
567 pci_unmap_single(pdev, buffer->dma,
570 if (buffer->skb != NULL)
571 dev_kfree_skb_any(buffer->skb);
573 vfree(rcv_desc->rx_buf_arr);
577 vfree(adapter->cmd_buf_arr);
582 * Called when a network interface is made active
583 * @returns 0 on success, negative value on failure
585 static int netxen_nic_open(struct net_device *netdev)
587 struct netxen_port *port = netdev_priv(netdev);
588 struct netxen_adapter *adapter = port->adapter;
592 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
593 err = netxen_init_firmware(adapter);
595 printk(KERN_ERR "Failed to init firmware\n");
598 netxen_nic_flash_print(adapter);
599 if (adapter->init_niu)
600 adapter->init_niu(adapter);
602 /* setup all the resources for the Phantom... */
603 /* this include the descriptors for rcv, tx, and status */
604 netxen_nic_clear_stats(adapter);
605 err = netxen_nic_hw_resources(adapter);
607 printk(KERN_ERR "Error in setting hw resources:%d\n",
611 if (adapter->init_port
612 && adapter->init_port(adapter, port->portnum) != 0) {
613 printk(KERN_ERR "%s: Failed to initialize port %d\n",
614 netxen_nic_driver_name, port->portnum);
615 netxen_free_hw_resources(adapter);
618 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
619 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
620 netxen_post_rx_buffers(adapter, ctx, ring);
622 adapter->irq = adapter->ahw.pdev->irq;
623 err = request_irq(adapter->ahw.pdev->irq, &netxen_intr,
624 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
625 netdev->name, adapter);
627 printk(KERN_ERR "request_irq failed with: %d\n", err);
628 netxen_free_hw_resources(adapter);
632 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
634 adapter->active_ports++;
635 if (adapter->active_ports == 1) {
636 if (!adapter->driver_mismatch)
637 mod_timer(&adapter->watchdog_timer, jiffies);
639 netxen_nic_enable_int(adapter);
642 /* Done here again so that even if phantom sw overwrote it,
644 if (adapter->macaddr_set)
645 adapter->macaddr_set(port, netdev->dev_addr);
646 netxen_nic_set_link_parameters(port);
648 netxen_nic_set_multi(netdev);
649 if (adapter->set_mtu)
650 adapter->set_mtu(port, netdev->mtu);
652 if (!adapter->driver_mismatch)
653 netif_start_queue(netdev);
659 * netxen_nic_close - Disables a network interface entry point
661 static int netxen_nic_close(struct net_device *netdev)
663 struct netxen_port *port = netdev_priv(netdev);
664 struct netxen_adapter *adapter = port->adapter;
666 struct netxen_cmd_buffer *cmd_buff;
667 struct netxen_skb_frag *buffrag;
669 netif_carrier_off(netdev);
670 netif_stop_queue(netdev);
672 adapter->active_ports--;
674 if (!adapter->active_ports) {
675 netxen_nic_disable_int(adapter);
676 cmd_buff = adapter->cmd_buf_arr;
677 for (i = 0; i < adapter->max_tx_desc_count; i++) {
678 buffrag = cmd_buff->frag_array;
680 pci_unmap_single(port->pdev, buffrag->dma,
683 buffrag->dma = (u64) NULL;
685 for (j = 0; j < cmd_buff->frag_count; j++) {
688 pci_unmap_page(port->pdev,
692 buffrag->dma = (u64) NULL;
695 /* Free the skb we received in netxen_nic_xmit_frame */
697 dev_kfree_skb_any(cmd_buff->skb);
698 cmd_buff->skb = NULL;
702 FLUSH_SCHEDULED_WORK();
703 del_timer_sync(&adapter->watchdog_timer);
709 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
711 struct netxen_port *port = netdev_priv(netdev);
712 struct netxen_adapter *adapter = port->adapter;
713 struct netxen_hardware_context *hw = &adapter->ahw;
714 unsigned int first_seg_len = skb->len - skb->data_len;
715 struct netxen_skb_frag *buffrag;
719 u32 saved_producer = 0;
720 struct cmd_desc_type0 *hwdesc;
722 struct netxen_cmd_buffer *pbuf = NULL;
723 static int dropped_packet = 0;
725 u32 local_producer = 0;
726 u32 max_tx_desc_count = 0;
727 u32 last_cmd_consumer = 0;
730 port->stats.xmitcalled++;
731 frag_count = skb_shinfo(skb)->nr_frags + 1;
733 if (unlikely(skb->len <= 0)) {
734 dev_kfree_skb_any(skb);
735 port->stats.badskblen++;
739 if (frag_count > MAX_BUFFERS_PER_CMD) {
740 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
741 "too large, can handle only %d frags\n",
742 netxen_nic_driver_name, netdev->name,
743 frag_count, MAX_BUFFERS_PER_CMD);
744 port->stats.txdropped++;
745 if ((++dropped_packet & 0xff) == 0xff)
746 printk("%s: %s droppped packets = %d\n",
747 netxen_nic_driver_name, netdev->name,
754 * Everything is set up. Now, we just need to transmit it out.
755 * Note that we have to copy the contents of buffer over to
756 * right place. Later on, this can be optimized out by de-coupling the
757 * producer index from the buffer index.
759 retry_getting_window:
760 spin_lock_bh(&adapter->tx_lock);
761 if (adapter->total_threads == MAX_XMIT_PRODUCERS) {
762 spin_unlock_bh(&adapter->tx_lock);
769 for (i = 0; i < 20; i++)
770 cpu_relax(); /*This a nop instr on i386 */
772 goto retry_getting_window;
774 local_producer = adapter->cmd_producer;
775 /* There 4 fragments per descriptor */
776 no_of_desc = (frag_count + 3) >> 2;
777 if (netdev->features & NETIF_F_TSO) {
778 if (skb_shinfo(skb)->gso_size > 0) {
781 if (((skb->nh.iph)->ihl * sizeof(u32)) +
782 ((skb->h.th)->doff * sizeof(u32)) +
783 sizeof(struct ethhdr) >
784 (sizeof(struct cmd_desc_type0) - 2)) {
789 k = adapter->cmd_producer;
790 max_tx_desc_count = adapter->max_tx_desc_count;
791 last_cmd_consumer = adapter->last_cmd_consumer;
792 if ((k + no_of_desc) >=
793 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
794 last_cmd_consumer)) {
795 port->stats.nocmddescriptor++;
796 DPRINTK(ERR, "No command descriptors available,"
797 " producer = %d, consumer = %d count=%llu,"
798 " dropping packet\n", producer,
799 adapter->last_cmd_consumer,
800 port->stats.nocmddescriptor);
802 netif_stop_queue(netdev);
803 port->flags |= NETXEN_NETDEV_STATUS;
804 spin_unlock_bh(&adapter->tx_lock);
805 return NETDEV_TX_BUSY;
807 k = get_index_range(k, max_tx_desc_count, no_of_desc);
808 adapter->cmd_producer = k;
809 adapter->total_threads++;
810 adapter->num_threads++;
812 spin_unlock_bh(&adapter->tx_lock);
813 /* Copy the descriptors into the hardware */
814 producer = local_producer;
815 saved_producer = producer;
816 hwdesc = &hw->cmd_desc_head[producer];
817 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
818 /* Take skb->data itself */
819 pbuf = &adapter->cmd_buf_arr[producer];
820 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
821 pbuf->mss = skb_shinfo(skb)->gso_size;
822 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
827 pbuf->total_length = skb->len;
829 pbuf->cmd = TX_ETHER_PKT;
830 pbuf->frag_count = frag_count;
831 pbuf->port = port->portnum;
832 buffrag = &pbuf->frag_array[0];
833 buffrag->dma = pci_map_single(port->pdev, skb->data, first_seg_len,
835 buffrag->length = first_seg_len;
836 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
837 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
838 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
840 netxen_set_cmd_desc_port(hwdesc, port->portnum);
841 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
842 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
844 for (i = 1, k = 1; i < frag_count; i++, k++) {
845 struct skb_frag_struct *frag;
847 unsigned long offset;
850 /* move to next desc. if there is a need */
851 if ((i & 0x3) == 0) {
853 producer = get_next_index(producer,
854 adapter->max_tx_desc_count);
855 hwdesc = &hw->cmd_desc_head[producer];
856 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
858 frag = &skb_shinfo(skb)->frags[i - 1];
860 offset = frag->page_offset;
863 temp_dma = pci_map_page(port->pdev, frag->page, offset,
864 len, PCI_DMA_TODEVICE);
867 buffrag->dma = temp_dma;
868 buffrag->length = temp_len;
870 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
873 hwdesc->buffer1_length = cpu_to_le16(temp_len);
874 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
877 hwdesc->buffer2_length = cpu_to_le16(temp_len);
878 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
881 hwdesc->buffer3_length = cpu_to_le16(temp_len);
882 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
885 hwdesc->buffer4_length = cpu_to_le16(temp_len);
886 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
891 producer = get_next_index(producer, adapter->max_tx_desc_count);
893 /* might change opcode to TX_TCP_LSO */
894 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
896 /* For LSO, we need to copy the MAC/IP/TCP headers into
897 * the descriptor ring
899 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
901 int hdr_len, first_hdr_len, more_hdr;
902 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
903 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
904 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
907 first_hdr_len = hdr_len;
910 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
911 hwdesc = &hw->cmd_desc_head[producer];
913 /* copy the first 64 bytes */
914 memcpy(((void *)hwdesc) + 2,
915 (void *)(skb->data), first_hdr_len);
916 producer = get_next_index(producer, max_tx_desc_count);
919 hwdesc = &hw->cmd_desc_head[producer];
920 /* copy the next 64 bytes - should be enough except
921 * for pathological case
923 memcpy((void *)hwdesc, (void *)(skb->data) +
924 first_hdr_len, hdr_len - first_hdr_len);
925 producer = get_next_index(producer, max_tx_desc_count);
928 spin_lock_bh(&adapter->tx_lock);
929 port->stats.txbytes +=
930 netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
931 /* Code to update the adapter considering how many producer threads
932 are currently working */
933 if ((--adapter->num_threads) == 0) {
934 /* This is the last thread */
935 u32 crb_producer = adapter->cmd_producer;
937 NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
939 adapter->total_threads = 0;
942 port->stats.xmitfinished++;
943 spin_unlock_bh(&adapter->tx_lock);
945 netdev->trans_start = jiffies;
947 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
949 DPRINTK(INFO, "Done. Send\n");
953 static void netxen_watchdog(unsigned long v)
955 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
957 SCHEDULE_WORK(&adapter->watchdog_task);
960 static void netxen_tx_timeout(struct net_device *netdev)
962 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
964 SCHEDULE_WORK(&port->tx_timeout_task);
967 static void netxen_tx_timeout_task(struct work_struct *work)
969 struct netxen_port *port =
970 container_of(work, struct netxen_port, tx_timeout_task);
971 struct net_device *netdev = port->netdev;
974 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
975 netxen_nic_driver_name, netdev->name);
977 spin_lock_irqsave(&port->adapter->lock, flags);
978 netxen_nic_close(netdev);
979 netxen_nic_open(netdev);
980 spin_unlock_irqrestore(&port->adapter->lock, flags);
981 netdev->trans_start = jiffies;
982 netif_wake_queue(netdev);
986 netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
990 DPRINTK(INFO, "Entered handle ISR\n");
992 adapter->stats.ints++;
994 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
997 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
998 if ((mask & 0x80) == 0) {
999 /* not our interrupt */
1002 netxen_nic_disable_int(adapter);
1003 /* Window = 0 or 1 */
1005 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
1006 ISR_INT_TARGET_STATUS));
1007 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
1008 } while (((mask & 0x80) != 0) && (++count < 32));
1009 if ((mask & 0x80) != 0)
1010 printk("Could not disable interrupt completely\n");
1013 adapter->stats.hostints++;
1015 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1016 if (netif_rx_schedule_prep(netdev)) {
1018 * Interrupts are already disabled.
1020 __netif_rx_schedule(netdev);
1022 static unsigned int intcount = 0;
1023 if ((++intcount & 0xfff) == 0xfff)
1025 "%s: %s interrupt %d while in poll\n",
1026 netxen_nic_driver_name, netdev->name,
1033 netxen_nic_enable_int(adapter);
1040 * netxen_intr - Interrupt Handler
1041 * @irq: interrupt number
1042 * data points to adapter stucture (which may be handling more than 1 port
1044 irqreturn_t netxen_intr(int irq, void *data)
1046 struct netxen_adapter *adapter;
1047 struct netxen_port *port;
1048 struct net_device *netdev;
1051 if (unlikely(!irq)) {
1052 return IRQ_NONE; /* Not our interrupt */
1055 adapter = (struct netxen_adapter *)data;
1056 for (i = 0; i < adapter->ahw.max_ports; i++) {
1057 port = adapter->port[i];
1058 netdev = port->netdev;
1060 /* process our status queue (for all 4 ports) */
1061 if (netif_running(netdev)) {
1062 netxen_handle_int(adapter, netdev);
1070 static int netxen_nic_poll(struct net_device *netdev, int *budget)
1072 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
1073 struct netxen_adapter *adapter = port->adapter;
1074 int work_to_do = min(*budget, netdev->quota);
1080 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1081 port->stats.polled++;
1084 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1086 * Fairness issue. This will give undue weight to the
1087 * receive context 0.
1091 * To avoid starvation, we give each of our receivers,
1092 * a fraction of the quota. Sometimes, it might happen that we
1093 * have enough quota to process every packet, but since all the
1094 * packets are on one context, it gets only half of the quota,
1095 * and ends up not processing it.
1097 this_work_done = netxen_process_rcv_ring(adapter, ctx,
1100 work_done += this_work_done;
1103 netdev->quota -= work_done;
1104 *budget -= work_done;
1106 if (work_done >= work_to_do && netxen_nic_rx_has_work(adapter) != 0)
1109 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1112 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1113 work_done, work_to_do);
1115 netif_rx_complete(netdev);
1116 netxen_nic_enable_int(adapter);
1122 #ifdef CONFIG_NET_POLL_CONTROLLER
1123 static void netxen_nic_poll_controller(struct net_device *netdev)
1125 struct netxen_port *port = netdev_priv(netdev);
1126 struct netxen_adapter *adapter = port->adapter;
1127 disable_irq(adapter->irq);
1128 netxen_intr(adapter->irq, adapter);
1129 enable_irq(adapter->irq);
1133 static struct pci_driver netxen_driver = {
1134 .name = netxen_nic_driver_name,
1135 .id_table = netxen_pci_tbl,
1136 .probe = netxen_nic_probe,
1137 .remove = __devexit_p(netxen_nic_remove)
1140 /* Driver Registration on NetXen card */
1142 static int __init netxen_init_module(void)
1144 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1147 return pci_register_driver(&netxen_driver);
1150 module_init(netxen_init_module);
1152 static void __exit netxen_exit_module(void)
1155 * Wait for some time to allow the dma to drain, if any.
1157 pci_unregister_driver(&netxen_driver);
1158 destroy_workqueue(netxen_workq);
1161 module_exit(netxen_exit_module);