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 #define PHAN_VENDOR_ID 0x4040
47 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
48 MODULE_LICENSE("GPL");
49 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
51 char netxen_nic_driver_name[] = "netxen-nic";
52 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
53 NETXEN_NIC_LINUX_VERSIONID;
55 #define NETXEN_NETDEV_WEIGHT 120
56 #define NETXEN_ADAPTER_UP_MAGIC 777
57 #define NETXEN_NIC_PEG_TUNE 0
59 u8 nx_p2_id = NX_P2_C0;
61 #define DMA_32BIT_MASK 0x00000000ffffffffULL
62 #define DMA_35BIT_MASK 0x00000007ffffffffULL
64 /* Local functions to NetXen NIC driver */
65 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
66 const struct pci_device_id *ent);
67 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
68 static int netxen_nic_open(struct net_device *netdev);
69 static int netxen_nic_close(struct net_device *netdev);
70 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
71 static void netxen_tx_timeout(struct net_device *netdev);
72 static void netxen_tx_timeout_task(struct work_struct *work);
73 static void netxen_watchdog(unsigned long);
74 static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
75 static int netxen_nic_poll(struct net_device *dev, int *budget);
76 #ifdef CONFIG_NET_POLL_CONTROLLER
77 static void netxen_nic_poll_controller(struct net_device *netdev);
79 static irqreturn_t netxen_intr(int irq, void *data);
81 /* PCI Device ID Table */
82 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
83 {PCI_DEVICE(0x4040, 0x0001)},
84 {PCI_DEVICE(0x4040, 0x0002)},
85 {PCI_DEVICE(0x4040, 0x0003)},
86 {PCI_DEVICE(0x4040, 0x0004)},
87 {PCI_DEVICE(0x4040, 0x0005)},
88 {PCI_DEVICE(0x4040, 0x0024)},
89 {PCI_DEVICE(0x4040, 0x0025)},
93 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
95 struct workqueue_struct *netxen_workq;
96 static void netxen_watchdog(unsigned long);
101 * The Linux system will invoke this after identifying the vendor ID and
102 * device Id in the pci_tbl supported by this module.
104 * A quad port card has one operational PCI config space, (function 0),
105 * which is used to access all four ports.
107 * This routine will initialize the adapter, and setup the global parameters
108 * along with the port's specific structure.
111 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
113 struct net_device *netdev = NULL;
114 struct netxen_adapter *adapter = NULL;
115 struct netxen_port *port = NULL;
116 void __iomem *mem_ptr0 = NULL;
117 void __iomem *mem_ptr1 = NULL;
118 void __iomem *mem_ptr2 = NULL;
120 u8 __iomem *db_ptr = NULL;
121 unsigned long mem_base, mem_len, db_base, db_len;
122 int pci_using_dac, i, err;
124 struct netxen_recv_context *recv_ctx = NULL;
125 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
126 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
127 u64 mac_addr[FLASH_NUM_PORTS + 1];
130 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
131 /* In current scheme, we use only PCI function 0 */
132 if (PCI_FUNC(pdev->devfn) != 0) {
133 DPRINTK(ERR, "NetXen function %d will not be enabled.\n",
134 PCI_FUNC(pdev->devfn));
137 if ((err = pci_enable_device(pdev)))
139 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
141 goto err_out_disable_pdev;
144 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
145 goto err_out_disable_pdev;
147 pci_set_master(pdev);
148 pci_read_config_byte(pdev, PCI_REVISION_ID, &nx_p2_id);
149 if (nx_p2_id == NX_P2_C1 &&
150 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
151 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
154 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
155 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
156 goto err_out_free_res;
161 /* remap phys address */
162 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
163 mem_len = pci_resource_len(pdev, 0);
165 /* 128 Meg of memory */
166 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
168 ioremap(mem_base + SECOND_PAGE_GROUP_START, SECOND_PAGE_GROUP_SIZE);
170 ioremap(mem_base + THIRD_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
172 if ((mem_ptr0 == 0UL) || (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
174 "Cannot remap adapter memory aborting.:"
175 "0 -> %p, 1 -> %p, 2 -> %p\n",
176 mem_ptr0, mem_ptr1, mem_ptr2);
179 goto err_out_iounmap;
181 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
182 db_len = pci_resource_len(pdev, 4);
185 printk(KERN_ERR "%s: doorbell is disabled\n",
186 netxen_nic_driver_name);
188 goto err_out_iounmap;
190 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
193 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
195 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
196 netxen_nic_driver_name);
198 goto err_out_iounmap;
200 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
203 * Allocate a adapter structure which will manage all the initialization
204 * as well as the common resources for all ports...
205 * all the ports will have pointer to this adapter as well as Adapter
206 * will have pointers of all the ports structures.
209 /* One adapter structure for all 4 ports.... */
210 adapter = kzalloc(sizeof(struct netxen_adapter), GFP_KERNEL);
211 if (adapter == NULL) {
212 printk(KERN_ERR "%s: Could not allocate adapter memory:%d\n",
213 netxen_nic_driver_name,
214 (int)sizeof(struct netxen_adapter));
216 goto err_out_dbunmap;
219 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS;
220 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
221 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
222 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
224 pci_set_drvdata(pdev, adapter);
226 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
227 if (cmd_buf_arr == NULL) {
229 "%s: Could not allocate cmd_buf_arr memory:%d\n",
230 netxen_nic_driver_name, (int)TX_RINGSIZE);
232 goto err_out_free_adapter;
234 memset(cmd_buf_arr, 0, TX_RINGSIZE);
236 for (i = 0; i < MAX_RCV_CTX; ++i) {
237 recv_ctx = &adapter->recv_ctx[i];
238 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
239 rcv_desc = &recv_ctx->rcv_desc[ring];
240 switch (RCV_DESC_TYPE(ring)) {
241 case RCV_DESC_NORMAL:
242 rcv_desc->max_rx_desc_count =
243 adapter->max_rx_desc_count;
244 rcv_desc->flags = RCV_DESC_NORMAL;
245 rcv_desc->dma_size = RX_DMA_MAP_LEN;
246 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
250 rcv_desc->max_rx_desc_count =
251 adapter->max_jumbo_rx_desc_count;
252 rcv_desc->flags = RCV_DESC_JUMBO;
253 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
254 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
258 rcv_desc->max_rx_desc_count =
259 adapter->max_lro_rx_desc_count;
260 rcv_desc->flags = RCV_DESC_LRO;
261 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
262 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
266 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
267 vmalloc(RCV_BUFFSIZE);
269 if (rcv_desc->rx_buf_arr == NULL) {
270 printk(KERN_ERR "%s: Could not allocate"
271 "rcv_desc->rx_buf_arr memory:%d\n",
272 netxen_nic_driver_name,
275 goto err_out_free_rx_buffer;
277 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
282 adapter->cmd_buf_arr = cmd_buf_arr;
283 adapter->ahw.pci_base0 = mem_ptr0;
284 adapter->ahw.pci_base1 = mem_ptr1;
285 adapter->ahw.pci_base2 = mem_ptr2;
286 adapter->ahw.db_base = db_ptr;
287 adapter->ahw.db_len = db_len;
288 spin_lock_init(&adapter->tx_lock);
289 spin_lock_init(&adapter->lock);
290 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
292 netxen_pinit_from_rom(adapter, 0);
294 netxen_load_firmware(adapter);
298 * Set the CRB window to invalid. If any register in window 0 is
299 * accessed it should set the window to 0 and then reset it to 1.
301 adapter->curr_window = 255;
303 * Adapter in our case is quad port so initialize it before
304 * initializing the ports
306 netxen_initialize_adapter_hw(adapter); /* initialize the adapter */
308 netxen_initialize_adapter_ops(adapter);
310 init_timer(&adapter->watchdog_timer);
311 adapter->ahw.xg_linkup = 0;
312 adapter->watchdog_timer.function = &netxen_watchdog;
313 adapter->watchdog_timer.data = (unsigned long)adapter;
314 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
315 adapter->ahw.pdev = pdev;
316 adapter->proc_cmd_buf_counter = 0;
317 adapter->ahw.revision_id = nx_p2_id;
319 if (pci_enable_msi(pdev)) {
320 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
321 printk(KERN_WARNING "%s: unable to allocate MSI interrupt"
322 " error\n", netxen_nic_driver_name);
324 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
326 if (netxen_is_flash_supported(adapter) == 0 &&
327 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
333 * Initialize all the CRB registers here.
335 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
336 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_CONSUMER_OFFSET));
337 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
339 /* do this before waking up pegs so that we have valid dummy dma addr */
340 err = netxen_initialize_adapter_offload(adapter);
342 goto err_out_free_dev;
345 /* Unlock the HW, prompting the boot sequence */
347 NETXEN_CRB_NORMALIZE(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
349 /* Handshake with the card before we register the devices. */
350 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
352 /* initialize the all the ports */
353 adapter->active_ports = 0;
355 for (i = 0; i < adapter->ahw.max_ports; i++) {
356 netdev = alloc_etherdev(sizeof(struct netxen_port));
358 printk(KERN_ERR "%s: could not allocate netdev for port"
359 " %d\n", netxen_nic_driver_name, i + 1);
360 goto err_out_free_dev;
363 SET_MODULE_OWNER(netdev);
364 SET_NETDEV_DEV(netdev, &pdev->dev);
366 port = netdev_priv(netdev);
367 port->netdev = netdev;
369 port->adapter = adapter;
370 port->portnum = i; /* Gigabit port number from 0-3 */
372 netdev->open = netxen_nic_open;
373 netdev->stop = netxen_nic_close;
374 netdev->hard_start_xmit = netxen_nic_xmit_frame;
375 netdev->get_stats = netxen_nic_get_stats;
376 netdev->set_multicast_list = netxen_nic_set_multi;
377 netdev->set_mac_address = netxen_nic_set_mac;
378 netdev->change_mtu = netxen_nic_change_mtu;
379 netdev->tx_timeout = netxen_tx_timeout;
380 netdev->watchdog_timeo = HZ;
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;
438 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
439 netxen_pinit_from_rom(adapter, 0);
441 netxen_load_firmware(adapter);
442 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
445 * delay a while to ensure that the Pegs are up & running.
446 * Otherwise, we might see some flaky behaviour.
450 switch (adapter->ahw.board_type) {
452 printk("%s: QUAD GbE board initialized\n",
453 netxen_nic_driver_name);
456 case NETXEN_NIC_XGBE:
457 printk("%s: XGbE board initialized\n", netxen_nic_driver_name);
461 adapter->driver_mismatch = 0;
466 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
467 pci_disable_msi(pdev);
468 for (i = 0; i < adapter->port_count; i++) {
469 port = adapter->port[i];
470 if ((port) && (port->netdev)) {
471 unregister_netdev(port->netdev);
472 free_netdev(port->netdev);
476 netxen_free_adapter_offload(adapter);
478 err_out_free_rx_buffer:
479 for (i = 0; i < MAX_RCV_CTX; ++i) {
480 recv_ctx = &adapter->recv_ctx[i];
481 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
482 rcv_desc = &recv_ctx->rcv_desc[ring];
483 if (rcv_desc->rx_buf_arr != NULL) {
484 vfree(rcv_desc->rx_buf_arr);
485 rcv_desc->rx_buf_arr = NULL;
491 err_out_free_adapter:
492 pci_set_drvdata(pdev, NULL);
508 pci_release_regions(pdev);
509 err_out_disable_pdev:
510 pci_disable_device(pdev);
514 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
516 struct netxen_adapter *adapter;
517 struct netxen_port *port;
518 struct netxen_rx_buffer *buffer;
519 struct netxen_recv_context *recv_ctx;
520 struct netxen_rcv_desc_ctx *rcv_desc;
524 adapter = pci_get_drvdata(pdev);
528 netxen_nic_stop_all_ports(adapter);
529 /* leave the hw in the same state as reboot */
530 netxen_pinit_from_rom(adapter, 0);
531 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
532 netxen_load_firmware(adapter);
533 netxen_free_adapter_offload(adapter);
535 udelay(500); /* Delay for a while to drain the DMA engines */
536 for (i = 0; i < adapter->port_count; i++) {
537 port = adapter->port[i];
538 if ((port) && (port->netdev)) {
539 unregister_netdev(port->netdev);
540 free_netdev(port->netdev);
544 if ((adapter->flags & NETXEN_NIC_MSI_ENABLED))
545 pci_disable_msi(pdev);
546 pci_set_drvdata(pdev, NULL);
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);
558 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
559 recv_ctx = &adapter->recv_ctx[ctxid];
560 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
561 rcv_desc = &recv_ctx->rcv_desc[ring];
562 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
563 buffer = &(rcv_desc->rx_buf_arr[i]);
564 if (buffer->state == NETXEN_BUFFER_FREE)
566 pci_unmap_single(pdev, buffer->dma,
569 if (buffer->skb != NULL)
570 dev_kfree_skb_any(buffer->skb);
572 vfree(rcv_desc->rx_buf_arr);
576 vfree(adapter->cmd_buf_arr);
581 * Called when a network interface is made active
582 * @returns 0 on success, negative value on failure
584 static int netxen_nic_open(struct net_device *netdev)
586 struct netxen_port *port = netdev_priv(netdev);
587 struct netxen_adapter *adapter = port->adapter;
591 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
592 err = netxen_init_firmware(adapter);
594 printk(KERN_ERR "Failed to init firmware\n");
597 netxen_nic_flash_print(adapter);
598 if (adapter->init_niu)
599 adapter->init_niu(adapter);
601 /* setup all the resources for the Phantom... */
602 /* this include the descriptors for rcv, tx, and status */
603 netxen_nic_clear_stats(adapter);
604 err = netxen_nic_hw_resources(adapter);
606 printk(KERN_ERR "Error in setting hw resources:%d\n",
610 if (adapter->init_port
611 && adapter->init_port(adapter, port->portnum) != 0) {
612 printk(KERN_ERR "%s: Failed to initialize port %d\n",
613 netxen_nic_driver_name, port->portnum);
614 netxen_free_hw_resources(adapter);
617 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
618 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
619 netxen_post_rx_buffers(adapter, ctx, ring);
621 adapter->irq = adapter->ahw.pdev->irq;
622 err = request_irq(adapter->ahw.pdev->irq, &netxen_intr,
623 SA_SHIRQ | SA_SAMPLE_RANDOM, netdev->name,
626 printk(KERN_ERR "request_irq failed with: %d\n", err);
627 netxen_free_hw_resources(adapter);
631 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
633 adapter->active_ports++;
634 if (adapter->active_ports == 1) {
635 if (!adapter->driver_mismatch)
636 mod_timer(&adapter->watchdog_timer, jiffies);
638 netxen_nic_enable_int(adapter);
641 /* Done here again so that even if phantom sw overwrote it,
643 if (adapter->macaddr_set)
644 adapter->macaddr_set(port, netdev->dev_addr);
645 netxen_nic_set_link_parameters(port);
647 netxen_nic_set_multi(netdev);
648 if (adapter->set_mtu)
649 adapter->set_mtu(port, netdev->mtu);
651 if (!adapter->driver_mismatch)
652 netif_start_queue(netdev);
658 * netxen_nic_close - Disables a network interface entry point
660 static int netxen_nic_close(struct net_device *netdev)
662 struct netxen_port *port = netdev_priv(netdev);
663 struct netxen_adapter *adapter = port->adapter;
665 struct netxen_cmd_buffer *cmd_buff;
666 struct netxen_skb_frag *buffrag;
668 netif_carrier_off(netdev);
669 netif_stop_queue(netdev);
671 adapter->active_ports--;
673 if (!adapter->active_ports) {
674 netxen_nic_disable_int(adapter);
676 free_irq(adapter->irq, adapter);
677 cmd_buff = adapter->cmd_buf_arr;
678 for (i = 0; i < adapter->max_tx_desc_count; i++) {
679 buffrag = cmd_buff->frag_array;
681 pci_unmap_single(port->pdev, buffrag->dma,
684 buffrag->dma = (u64) NULL;
686 for (j = 0; j < cmd_buff->frag_count; j++) {
689 pci_unmap_page(port->pdev,
693 buffrag->dma = (u64) NULL;
696 /* Free the skb we received in netxen_nic_xmit_frame */
698 dev_kfree_skb_any(cmd_buff->skb);
699 cmd_buff->skb = NULL;
703 FLUSH_SCHEDULED_WORK();
704 del_timer_sync(&adapter->watchdog_timer);
710 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
712 struct netxen_port *port = netdev_priv(netdev);
713 struct netxen_adapter *adapter = port->adapter;
714 struct netxen_hardware_context *hw = &adapter->ahw;
715 unsigned int first_seg_len = skb->len - skb->data_len;
716 struct netxen_skb_frag *buffrag;
720 u32 saved_producer = 0;
721 struct cmd_desc_type0 *hwdesc;
723 struct netxen_cmd_buffer *pbuf = NULL;
724 static int dropped_packet = 0;
726 u32 local_producer = 0;
727 u32 max_tx_desc_count = 0;
728 u32 last_cmd_consumer = 0;
731 port->stats.xmitcalled++;
732 frag_count = skb_shinfo(skb)->nr_frags + 1;
734 if (unlikely(skb->len <= 0)) {
735 dev_kfree_skb_any(skb);
736 port->stats.badskblen++;
740 if (frag_count > MAX_BUFFERS_PER_CMD) {
741 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
742 "too large, can handle only %d frags\n",
743 netxen_nic_driver_name, netdev->name,
744 frag_count, MAX_BUFFERS_PER_CMD);
745 port->stats.txdropped++;
746 if ((++dropped_packet & 0xff) == 0xff)
747 printk("%s: %s droppped packets = %d\n",
748 netxen_nic_driver_name, netdev->name,
755 * Everything is set up. Now, we just need to transmit it out.
756 * Note that we have to copy the contents of buffer over to
757 * right place. Later on, this can be optimized out by de-coupling the
758 * producer index from the buffer index.
760 retry_getting_window:
761 spin_lock_bh(&adapter->tx_lock);
762 if (adapter->total_threads == MAX_XMIT_PRODUCERS) {
763 spin_unlock_bh(&adapter->tx_lock);
770 for (i = 0; i < 20; i++)
771 cpu_relax(); /*This a nop instr on i386 */
773 goto retry_getting_window;
775 local_producer = adapter->cmd_producer;
776 /* There 4 fragments per descriptor */
777 no_of_desc = (frag_count + 3) >> 2;
778 if (netdev->features & NETIF_F_TSO) {
779 if (skb_shinfo(skb)->gso_size > 0) {
782 if (((skb->nh.iph)->ihl * sizeof(u32)) +
783 ((skb->h.th)->doff * sizeof(u32)) +
784 sizeof(struct ethhdr) >
785 (sizeof(struct cmd_desc_type0) - 2)) {
790 k = adapter->cmd_producer;
791 max_tx_desc_count = adapter->max_tx_desc_count;
792 last_cmd_consumer = adapter->last_cmd_consumer;
793 if ((k + no_of_desc) >=
794 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
795 last_cmd_consumer)) {
796 port->stats.nocmddescriptor++;
797 DPRINTK(ERR, "No command descriptors available,"
798 " producer = %d, consumer = %d count=%llu,"
799 " dropping packet\n", producer,
800 adapter->last_cmd_consumer,
801 port->stats.nocmddescriptor);
803 netif_stop_queue(netdev);
804 port->flags |= NETXEN_NETDEV_STATUS;
805 spin_unlock_bh(&adapter->tx_lock);
806 return NETDEV_TX_BUSY;
808 k = get_index_range(k, max_tx_desc_count, no_of_desc);
809 adapter->cmd_producer = k;
810 adapter->total_threads++;
811 adapter->num_threads++;
813 spin_unlock_bh(&adapter->tx_lock);
814 /* Copy the descriptors into the hardware */
815 producer = local_producer;
816 saved_producer = producer;
817 hwdesc = &hw->cmd_desc_head[producer];
818 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
819 /* Take skb->data itself */
820 pbuf = &adapter->cmd_buf_arr[producer];
821 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
822 pbuf->mss = skb_shinfo(skb)->gso_size;
823 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
828 pbuf->total_length = skb->len;
830 pbuf->cmd = TX_ETHER_PKT;
831 pbuf->frag_count = frag_count;
832 pbuf->port = port->portnum;
833 buffrag = &pbuf->frag_array[0];
834 buffrag->dma = pci_map_single(port->pdev, skb->data, first_seg_len,
836 buffrag->length = first_seg_len;
837 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
838 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
839 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
841 netxen_set_cmd_desc_port(hwdesc, port->portnum);
842 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
843 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
845 for (i = 1, k = 1; i < frag_count; i++, k++) {
846 struct skb_frag_struct *frag;
848 unsigned long offset;
851 /* move to next desc. if there is a need */
852 if ((i & 0x3) == 0) {
854 producer = get_next_index(producer,
855 adapter->max_tx_desc_count);
856 hwdesc = &hw->cmd_desc_head[producer];
857 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
859 frag = &skb_shinfo(skb)->frags[i - 1];
861 offset = frag->page_offset;
864 temp_dma = pci_map_page(port->pdev, frag->page, offset,
865 len, PCI_DMA_TODEVICE);
868 buffrag->dma = temp_dma;
869 buffrag->length = temp_len;
871 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
874 hwdesc->buffer1_length = cpu_to_le16(temp_len);
875 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
878 hwdesc->buffer2_length = cpu_to_le16(temp_len);
879 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
882 hwdesc->buffer3_length = cpu_to_le16(temp_len);
883 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
886 hwdesc->buffer4_length = cpu_to_le16(temp_len);
887 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
892 producer = get_next_index(producer, adapter->max_tx_desc_count);
894 /* might change opcode to TX_TCP_LSO */
895 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
897 /* For LSO, we need to copy the MAC/IP/TCP headers into
898 * the descriptor ring
900 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
902 int hdr_len, first_hdr_len, more_hdr;
903 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
904 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
905 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
908 first_hdr_len = hdr_len;
911 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
912 hwdesc = &hw->cmd_desc_head[producer];
914 /* copy the first 64 bytes */
915 memcpy(((void *)hwdesc) + 2,
916 (void *)(skb->data), first_hdr_len);
917 producer = get_next_index(producer, max_tx_desc_count);
920 hwdesc = &hw->cmd_desc_head[producer];
921 /* copy the next 64 bytes - should be enough except
922 * for pathological case
924 memcpy((void *)hwdesc, (void *)(skb->data) +
925 first_hdr_len, hdr_len - first_hdr_len);
926 producer = get_next_index(producer, max_tx_desc_count);
929 spin_lock_bh(&adapter->tx_lock);
930 port->stats.txbytes +=
931 netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
932 /* Code to update the adapter considering how many producer threads
933 are currently working */
934 if ((--adapter->num_threads) == 0) {
935 /* This is the last thread */
936 u32 crb_producer = adapter->cmd_producer;
938 NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
940 adapter->total_threads = 0;
943 port->stats.xmitfinished++;
944 spin_unlock_bh(&adapter->tx_lock);
946 netdev->trans_start = jiffies;
948 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
950 DPRINTK(INFO, "Done. Send\n");
954 static void netxen_watchdog(unsigned long v)
956 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
958 SCHEDULE_WORK(&adapter->watchdog_task);
961 static void netxen_tx_timeout(struct net_device *netdev)
963 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
965 SCHEDULE_WORK(&port->tx_timeout_task);
968 static void netxen_tx_timeout_task(struct work_struct *work)
970 struct netxen_port *port =
971 container_of(work, struct netxen_port, tx_timeout_task);
972 struct net_device *netdev = port->netdev;
975 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
976 netxen_nic_driver_name, netdev->name);
978 spin_lock_irqsave(&port->adapter->lock, flags);
979 netxen_nic_close(netdev);
980 netxen_nic_open(netdev);
981 spin_unlock_irqrestore(&port->adapter->lock, flags);
982 netdev->trans_start = jiffies;
983 netif_wake_queue(netdev);
987 netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
991 DPRINTK(INFO, "Entered handle ISR\n");
993 adapter->stats.ints++;
995 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
998 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
999 if ((mask & 0x80) == 0) {
1000 /* not our interrupt */
1003 netxen_nic_disable_int(adapter);
1004 /* Window = 0 or 1 */
1006 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
1007 ISR_INT_TARGET_STATUS));
1008 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
1009 } while (((mask & 0x80) != 0) && (++count < 32));
1010 if ((mask & 0x80) != 0)
1011 printk("Could not disable interrupt completely\n");
1014 adapter->stats.hostints++;
1016 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1017 if (netif_rx_schedule_prep(netdev)) {
1019 * Interrupts are already disabled.
1021 __netif_rx_schedule(netdev);
1023 static unsigned int intcount = 0;
1024 if ((++intcount & 0xfff) == 0xfff)
1026 "%s: %s interrupt %d while in poll\n",
1027 netxen_nic_driver_name, netdev->name,
1034 netxen_nic_enable_int(adapter);
1041 * netxen_intr - Interrupt Handler
1042 * @irq: interrupt number
1043 * data points to adapter stucture (which may be handling more than 1 port
1045 irqreturn_t netxen_intr(int irq, void *data)
1047 struct netxen_adapter *adapter;
1048 struct netxen_port *port;
1049 struct net_device *netdev;
1052 if (unlikely(!irq)) {
1053 return IRQ_NONE; /* Not our interrupt */
1056 adapter = (struct netxen_adapter *)data;
1057 for (i = 0; i < adapter->ahw.max_ports; i++) {
1058 port = adapter->port[i];
1059 netdev = port->netdev;
1061 /* process our status queue (for all 4 ports) */
1062 if (netif_running(netdev)) {
1063 netxen_handle_int(adapter, netdev);
1071 static int netxen_nic_poll(struct net_device *netdev, int *budget)
1073 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
1074 struct netxen_adapter *adapter = port->adapter;
1075 int work_to_do = min(*budget, netdev->quota);
1081 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1082 port->stats.polled++;
1085 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1087 * Fairness issue. This will give undue weight to the
1088 * receive context 0.
1092 * To avoid starvation, we give each of our receivers,
1093 * a fraction of the quota. Sometimes, it might happen that we
1094 * have enough quota to process every packet, but since all the
1095 * packets are on one context, it gets only half of the quota,
1096 * and ends up not processing it.
1098 this_work_done = netxen_process_rcv_ring(adapter, ctx,
1101 work_done += this_work_done;
1104 netdev->quota -= work_done;
1105 *budget -= work_done;
1107 if (work_done >= work_to_do && netxen_nic_rx_has_work(adapter) != 0)
1110 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1113 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1114 work_done, work_to_do);
1116 netif_rx_complete(netdev);
1117 netxen_nic_enable_int(adapter);
1123 #ifdef CONFIG_NET_POLL_CONTROLLER
1124 static void netxen_nic_poll_controller(struct net_device *netdev)
1126 struct netxen_port *port = netdev_priv(netdev);
1127 struct netxen_adapter *adapter = port->adapter;
1128 disable_irq(adapter->irq);
1129 netxen_intr(adapter->irq, adapter);
1130 enable_irq(adapter->irq);
1134 static struct pci_driver netxen_driver = {
1135 .name = netxen_nic_driver_name,
1136 .id_table = netxen_pci_tbl,
1137 .probe = netxen_nic_probe,
1138 .remove = __devexit_p(netxen_nic_remove)
1141 /* Driver Registration on NetXen card */
1143 static int __init netxen_init_module(void)
1145 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1148 return pci_register_driver(&netxen_driver);
1151 module_init(netxen_init_module);
1153 static void __exit netxen_exit_module(void)
1156 * Wait for some time to allow the dma to drain, if any.
1158 destroy_workqueue(netxen_workq);
1159 pci_unregister_driver(&netxen_driver);
1162 module_exit(netxen_exit_module);