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"
41 #include "netxen_nic_ioctl.h"
43 #include <linux/dma-mapping.h>
44 #include <linux/vmalloc.h>
46 #define PHAN_VENDOR_ID 0x4040
48 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
49 MODULE_LICENSE("GPL");
50 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
52 char netxen_nic_driver_name[] = "netxen-nic";
53 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
54 NETXEN_NIC_LINUX_VERSIONID;
56 struct netxen_adapter *g_adapter = NULL;
58 #define NETXEN_NETDEV_WEIGHT 120
59 #define NETXEN_ADAPTER_UP_MAGIC 777
60 #define NETXEN_NIC_PEG_TUNE 0
62 u8 nx_p2_id = NX_P2_C0;
64 #define DMA_32BIT_MASK 0x00000000ffffffffULL
65 #define DMA_35BIT_MASK 0x00000007ffffffffULL
67 /* Local functions to NetXen NIC driver */
68 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
69 const struct pci_device_id *ent);
70 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
71 static int netxen_nic_open(struct net_device *netdev);
72 static int netxen_nic_close(struct net_device *netdev);
73 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
74 static void netxen_tx_timeout(struct net_device *netdev);
75 static void netxen_tx_timeout_task(struct net_device *netdev);
76 static void netxen_watchdog(unsigned long);
77 static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
78 static int netxen_nic_ioctl(struct net_device *netdev,
79 struct ifreq *ifr, int cmd);
80 static int netxen_nic_poll(struct net_device *dev, int *budget);
81 #ifdef CONFIG_NET_POLL_CONTROLLER
82 static void netxen_nic_poll_controller(struct net_device *netdev);
84 static irqreturn_t netxen_intr(int irq, void *data);
86 /* PCI Device ID Table */
87 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
88 {PCI_DEVICE(0x4040, 0x0001)},
89 {PCI_DEVICE(0x4040, 0x0002)},
90 {PCI_DEVICE(0x4040, 0x0003)},
91 {PCI_DEVICE(0x4040, 0x0004)},
92 {PCI_DEVICE(0x4040, 0x0005)},
96 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
98 struct workqueue_struct *netxen_workq;
99 static void netxen_watchdog(unsigned long);
104 * The Linux system will invoke this after identifying the vendor ID and
105 * device Id in the pci_tbl supported by this module.
107 * A quad port card has one operational PCI config space, (function 0),
108 * which is used to access all four ports.
110 * This routine will initialize the adapter, and setup the global parameters
111 * along with the port's specific structure.
114 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
116 struct net_device *netdev = NULL;
117 struct netxen_adapter *adapter = NULL;
118 struct netxen_port *port = NULL;
119 void __iomem *mem_ptr0 = NULL;
120 void __iomem *mem_ptr1 = NULL;
121 void __iomem *mem_ptr2 = NULL;
124 unsigned long mem_base, mem_len, db_base, db_len;
125 int pci_using_dac, i, err;
127 struct netxen_recv_context *recv_ctx = NULL;
128 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
129 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
130 u64 mac_addr[FLASH_NUM_PORTS + 1];
132 static int netxen_cards_found = 0;
134 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
135 /* In current scheme, we use only PCI function 0 */
136 if (PCI_FUNC(pdev->devfn) != 0) {
137 DPRINTK(ERR, "NetXen function %d will not be enabled.\n",
138 PCI_FUNC(pdev->devfn));
141 if ((err = pci_enable_device(pdev)))
143 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
145 goto err_out_disable_pdev;
148 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
149 goto err_out_disable_pdev;
151 pci_set_master(pdev);
152 pci_read_config_byte(pdev, PCI_REVISION_ID, &nx_p2_id);
153 if (nx_p2_id == NX_P2_C1 &&
154 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
155 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
158 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
159 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
160 goto err_out_free_res;
165 /* remap phys address */
166 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
167 mem_len = pci_resource_len(pdev, 0);
169 /* 128 Meg of memory */
170 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
172 ioremap(mem_base + SECOND_PAGE_GROUP_START, SECOND_PAGE_GROUP_SIZE);
174 ioremap(mem_base + THIRD_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
176 if ((mem_ptr0 == 0UL) || (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
178 "Cannot remap adapter memory aborting.:"
179 "0 -> %p, 1 -> %p, 2 -> %p\n",
180 mem_ptr0, mem_ptr1, mem_ptr2);
183 goto err_out_iounmap;
185 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
186 db_len = pci_resource_len(pdev, 4);
189 printk(KERN_ERR "%s: doorbell is disabled\n",
190 netxen_nic_driver_name);
192 goto err_out_iounmap;
194 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
197 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
199 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
200 netxen_nic_driver_name);
202 goto err_out_iounmap;
204 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
207 * Allocate a adapter structure which will manage all the initialization
208 * as well as the common resources for all ports...
209 * all the ports will have pointer to this adapter as well as Adapter
210 * will have pointers of all the ports structures.
213 /* One adapter structure for all 4 ports.... */
214 adapter = kzalloc(sizeof(struct netxen_adapter), GFP_KERNEL);
215 if (adapter == NULL) {
216 printk(KERN_ERR "%s: Could not allocate adapter memory:%d\n",
217 netxen_nic_driver_name,
218 (int)sizeof(struct netxen_adapter));
220 goto err_out_dbunmap;
223 if (netxen_cards_found == 0) {
226 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS;
227 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
228 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
229 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
231 pci_set_drvdata(pdev, adapter);
233 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
234 if (cmd_buf_arr == NULL) {
236 "%s: Could not allocate cmd_buf_arr memory:%d\n",
237 netxen_nic_driver_name, (int)TX_RINGSIZE);
239 goto err_out_free_adapter;
241 memset(cmd_buf_arr, 0, TX_RINGSIZE);
243 for (i = 0; i < MAX_RCV_CTX; ++i) {
244 recv_ctx = &adapter->recv_ctx[i];
245 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
246 rcv_desc = &recv_ctx->rcv_desc[ring];
247 switch (RCV_DESC_TYPE(ring)) {
248 case RCV_DESC_NORMAL:
249 rcv_desc->max_rx_desc_count =
250 adapter->max_rx_desc_count;
251 rcv_desc->flags = RCV_DESC_NORMAL;
252 rcv_desc->dma_size = RX_DMA_MAP_LEN;
253 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
257 rcv_desc->max_rx_desc_count =
258 adapter->max_jumbo_rx_desc_count;
259 rcv_desc->flags = RCV_DESC_JUMBO;
260 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
261 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
265 rcv_desc->max_rx_desc_count =
266 adapter->max_lro_rx_desc_count;
267 rcv_desc->flags = RCV_DESC_LRO;
268 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
269 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
273 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
274 vmalloc(RCV_BUFFSIZE);
276 if (rcv_desc->rx_buf_arr == NULL) {
277 printk(KERN_ERR "%s: Could not allocate"
278 "rcv_desc->rx_buf_arr memory:%d\n",
279 netxen_nic_driver_name,
282 goto err_out_free_rx_buffer;
284 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
289 adapter->cmd_buf_arr = cmd_buf_arr;
290 adapter->ahw.pci_base0 = mem_ptr0;
291 adapter->ahw.pci_base1 = mem_ptr1;
292 adapter->ahw.pci_base2 = mem_ptr2;
293 adapter->ahw.db_base = db_ptr;
294 adapter->ahw.db_len = db_len;
295 spin_lock_init(&adapter->tx_lock);
296 spin_lock_init(&adapter->lock);
297 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
299 netxen_pinit_from_rom(adapter, 0);
301 netxen_load_firmware(adapter);
305 * Set the CRB window to invalid. If any register in window 0 is
306 * accessed it should set the window to 0 and then reset it to 1.
308 adapter->curr_window = 255;
310 * Adapter in our case is quad port so initialize it before
311 * initializing the ports
313 netxen_initialize_adapter_hw(adapter); /* initialize the adapter */
315 netxen_initialize_adapter_ops(adapter);
317 init_timer(&adapter->watchdog_timer);
318 adapter->ahw.xg_linkup = 0;
319 adapter->watchdog_timer.function = &netxen_watchdog;
320 adapter->watchdog_timer.data = (unsigned long)adapter;
321 INIT_WORK(&adapter->watchdog_task,
322 (void (*)(void *))netxen_watchdog_task, adapter);
323 adapter->ahw.pdev = pdev;
324 adapter->proc_cmd_buf_counter = 0;
325 adapter->ahw.revision_id = nx_p2_id;
327 if (pci_enable_msi(pdev)) {
328 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
329 printk(KERN_WARNING "%s: unable to allocate MSI interrupt"
330 " error\n", netxen_nic_driver_name);
332 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
334 if (netxen_is_flash_supported(adapter) == 0 &&
335 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
341 * Initialize all the CRB registers here.
343 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
344 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_CONSUMER_OFFSET));
345 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
347 /* do this before waking up pegs so that we have valid dummy dma addr */
348 err = netxen_initialize_adapter_offload(adapter);
350 goto err_out_free_dev;
353 /* Unlock the HW, prompting the boot sequence */
355 NETXEN_CRB_NORMALIZE(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
357 /* Handshake with the card before we register the devices. */
358 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
360 /* initialize the all the ports */
361 adapter->active_ports = 0;
363 for (i = 0; i < adapter->ahw.max_ports; i++) {
364 netdev = alloc_etherdev(sizeof(struct netxen_port));
366 printk(KERN_ERR "%s: could not allocate netdev for port"
367 " %d\n", netxen_nic_driver_name, i + 1);
368 goto err_out_free_dev;
371 SET_MODULE_OWNER(netdev);
372 SET_NETDEV_DEV(netdev, &pdev->dev);
374 port = netdev_priv(netdev);
375 port->netdev = netdev;
377 port->adapter = adapter;
378 port->portnum = i; /* Gigabit port number from 0-3 */
380 netdev->open = netxen_nic_open;
381 netdev->stop = netxen_nic_close;
382 netdev->hard_start_xmit = netxen_nic_xmit_frame;
383 netdev->get_stats = netxen_nic_get_stats;
384 netdev->set_multicast_list = netxen_nic_set_multi;
385 netdev->set_mac_address = netxen_nic_set_mac;
386 netdev->change_mtu = netxen_nic_change_mtu;
387 netdev->do_ioctl = netxen_nic_ioctl;
388 netdev->tx_timeout = netxen_tx_timeout;
389 netdev->watchdog_timeo = HZ;
391 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
392 netdev->poll = netxen_nic_poll;
393 netdev->weight = NETXEN_NETDEV_WEIGHT;
394 #ifdef CONFIG_NET_POLL_CONTROLLER
395 netdev->poll_controller = netxen_nic_poll_controller;
397 /* ScatterGather support */
398 netdev->features = NETIF_F_SG;
399 netdev->features |= NETIF_F_IP_CSUM;
400 netdev->features |= NETIF_F_TSO;
403 netdev->features |= NETIF_F_HIGHDMA;
406 unsigned char *p = (unsigned char *)&mac_addr[i];
407 netdev->dev_addr[0] = *(p + 5);
408 netdev->dev_addr[1] = *(p + 4);
409 netdev->dev_addr[2] = *(p + 3);
410 netdev->dev_addr[3] = *(p + 2);
411 netdev->dev_addr[4] = *(p + 1);
412 netdev->dev_addr[5] = *(p + 0);
414 memcpy(netdev->perm_addr, netdev->dev_addr,
416 if (!is_valid_ether_addr(netdev->perm_addr)) {
417 printk(KERN_ERR "%s: Bad MAC address "
418 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
419 netxen_nic_driver_name,
425 netdev->dev_addr[5]);
427 if (adapter->macaddr_set)
428 adapter->macaddr_set(port,
432 INIT_WORK(adapter->tx_timeout_task + i,
433 (void (*)(void *))netxen_tx_timeout_task, netdev);
434 netif_carrier_off(netdev);
435 netif_stop_queue(netdev);
437 if ((err = register_netdev(netdev))) {
438 printk(KERN_ERR "%s: register_netdev failed port #%d"
439 " aborting\n", netxen_nic_driver_name, i + 1);
442 goto err_out_free_dev;
444 adapter->port_count++;
445 adapter->port[i] = port;
449 * delay a while to ensure that the Pegs are up & running.
450 * Otherwise, we might see some flaky behaviour.
454 switch (adapter->ahw.board_type) {
456 printk("%s: QUAD GbE board initialized\n",
457 netxen_nic_driver_name);
460 case NETXEN_NIC_XGBE:
461 printk("%s: XGbE board initialized\n", netxen_nic_driver_name);
465 adapter->number = netxen_cards_found;
466 adapter->driver_mismatch = 0;
471 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
472 pci_disable_msi(pdev);
473 for (i = 0; i < adapter->port_count; i++) {
474 port = adapter->port[i];
475 if ((port) && (port->netdev)) {
476 unregister_netdev(port->netdev);
477 free_netdev(port->netdev);
481 netxen_free_adapter_offload(adapter);
483 err_out_free_rx_buffer:
484 for (i = 0; i < MAX_RCV_CTX; ++i) {
485 recv_ctx = &adapter->recv_ctx[i];
486 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
487 rcv_desc = &recv_ctx->rcv_desc[ring];
488 if (rcv_desc->rx_buf_arr != NULL) {
489 vfree(rcv_desc->rx_buf_arr);
490 rcv_desc->rx_buf_arr = NULL;
496 err_out_free_adapter:
497 pci_set_drvdata(pdev, NULL);
513 pci_release_regions(pdev);
514 err_out_disable_pdev:
515 pci_disable_device(pdev);
519 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
521 struct netxen_adapter *adapter;
522 struct netxen_port *port;
523 struct netxen_rx_buffer *buffer;
524 struct netxen_recv_context *recv_ctx;
525 struct netxen_rcv_desc_ctx *rcv_desc;
529 adapter = pci_get_drvdata(pdev);
533 netxen_nic_stop_all_ports(adapter);
534 /* leave the hw in the same state as reboot */
535 netxen_load_firmware(adapter);
536 netxen_free_adapter_offload(adapter);
538 udelay(500); /* Delay for a while to drain the DMA engines */
539 for (i = 0; i < adapter->port_count; i++) {
540 port = adapter->port[i];
541 if ((port) && (port->netdev)) {
542 unregister_netdev(port->netdev);
543 free_netdev(port->netdev);
547 if ((adapter->flags & NETXEN_NIC_MSI_ENABLED))
548 pci_disable_msi(pdev);
549 pci_set_drvdata(pdev, NULL);
550 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
551 netxen_free_hw_resources(adapter);
553 iounmap(adapter->ahw.db_base);
554 iounmap(adapter->ahw.pci_base0);
555 iounmap(adapter->ahw.pci_base1);
556 iounmap(adapter->ahw.pci_base2);
558 pci_release_regions(pdev);
559 pci_disable_device(pdev);
561 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
562 recv_ctx = &adapter->recv_ctx[ctxid];
563 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
564 rcv_desc = &recv_ctx->rcv_desc[ring];
565 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
566 buffer = &(rcv_desc->rx_buf_arr[i]);
567 if (buffer->state == NETXEN_BUFFER_FREE)
569 pci_unmap_single(pdev, buffer->dma,
572 if (buffer->skb != NULL)
573 dev_kfree_skb_any(buffer->skb);
575 vfree(rcv_desc->rx_buf_arr);
579 vfree(adapter->cmd_buf_arr);
584 * Called when a network interface is made active
585 * @returns 0 on success, negative value on failure
587 static int netxen_nic_open(struct net_device *netdev)
589 struct netxen_port *port = netdev_priv(netdev);
590 struct netxen_adapter *adapter = port->adapter;
594 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
595 err = netxen_init_firmware(adapter);
597 printk(KERN_ERR "Failed to init firmware\n");
600 netxen_nic_flash_print(adapter);
601 if (adapter->init_niu)
602 adapter->init_niu(adapter);
604 /* setup all the resources for the Phantom... */
605 /* this include the descriptors for rcv, tx, and status */
606 netxen_nic_clear_stats(adapter);
607 err = netxen_nic_hw_resources(adapter);
609 printk(KERN_ERR "Error in setting hw resources:%d\n",
613 if (adapter->init_port
614 && adapter->init_port(adapter, port->portnum) != 0) {
615 printk(KERN_ERR "%s: Failed to initialize port %d\n",
616 netxen_nic_driver_name, port->portnum);
617 netxen_free_hw_resources(adapter);
620 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
621 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
622 netxen_post_rx_buffers(adapter, ctx, ring);
624 adapter->irq = adapter->ahw.pdev->irq;
625 err = request_irq(adapter->ahw.pdev->irq, &netxen_intr,
626 SA_SHIRQ | SA_SAMPLE_RANDOM, netdev->name,
629 printk(KERN_ERR "request_irq failed with: %d\n", err);
630 netxen_free_hw_resources(adapter);
634 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
636 adapter->active_ports++;
637 if (adapter->active_ports == 1) {
638 if (!adapter->driver_mismatch)
639 mod_timer(&adapter->watchdog_timer, jiffies);
641 netxen_nic_enable_int(adapter);
644 /* Done here again so that even if phantom sw overwrote it,
646 if (adapter->macaddr_set)
647 adapter->macaddr_set(port, netdev->dev_addr);
648 netxen_nic_set_link_parameters(port);
650 netxen_nic_set_multi(netdev);
651 if (adapter->set_mtu)
652 adapter->set_mtu(port, netdev->mtu);
654 if (!adapter->driver_mismatch)
655 netif_start_queue(netdev);
661 * netxen_nic_close - Disables a network interface entry point
663 static int netxen_nic_close(struct net_device *netdev)
665 struct netxen_port *port = netdev_priv(netdev);
666 struct netxen_adapter *adapter = port->adapter;
668 struct netxen_cmd_buffer *cmd_buff;
669 struct netxen_skb_frag *buffrag;
671 netif_carrier_off(netdev);
672 netif_stop_queue(netdev);
674 adapter->active_ports--;
676 if (!adapter->active_ports) {
677 netxen_nic_disable_int(adapter);
679 free_irq(adapter->irq, adapter);
680 cmd_buff = adapter->cmd_buf_arr;
681 for (i = 0; i < adapter->max_tx_desc_count; i++) {
682 buffrag = cmd_buff->frag_array;
684 pci_unmap_single(port->pdev, buffrag->dma,
687 buffrag->dma = (u64) NULL;
689 for (j = 0; j < cmd_buff->frag_count; j++) {
692 pci_unmap_page(port->pdev,
696 buffrag->dma = (u64) NULL;
699 /* Free the skb we received in netxen_nic_xmit_frame */
701 dev_kfree_skb_any(cmd_buff->skb);
702 cmd_buff->skb = NULL;
706 FLUSH_SCHEDULED_WORK();
707 del_timer_sync(&adapter->watchdog_timer);
713 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
715 struct netxen_port *port = netdev_priv(netdev);
716 struct netxen_adapter *adapter = port->adapter;
717 struct netxen_hardware_context *hw = &adapter->ahw;
718 unsigned int first_seg_len = skb->len - skb->data_len;
719 struct netxen_skb_frag *buffrag;
723 u32 saved_producer = 0;
724 struct cmd_desc_type0 *hwdesc;
726 struct netxen_cmd_buffer *pbuf = NULL;
727 static int dropped_packet = 0;
729 u32 local_producer = 0;
730 u32 max_tx_desc_count = 0;
731 u32 last_cmd_consumer = 0;
734 port->stats.xmitcalled++;
735 frag_count = skb_shinfo(skb)->nr_frags + 1;
737 if (unlikely(skb->len <= 0)) {
738 dev_kfree_skb_any(skb);
739 port->stats.badskblen++;
743 if (frag_count > MAX_BUFFERS_PER_CMD) {
744 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
745 "too large, can handle only %d frags\n",
746 netxen_nic_driver_name, netdev->name,
747 frag_count, MAX_BUFFERS_PER_CMD);
748 port->stats.txdropped++;
749 if ((++dropped_packet & 0xff) == 0xff)
750 printk("%s: %s droppped packets = %d\n",
751 netxen_nic_driver_name, netdev->name,
758 * Everything is set up. Now, we just need to transmit it out.
759 * Note that we have to copy the contents of buffer over to
760 * right place. Later on, this can be optimized out by de-coupling the
761 * producer index from the buffer index.
763 retry_getting_window:
764 spin_lock_bh(&adapter->tx_lock);
765 if (adapter->total_threads == MAX_XMIT_PRODUCERS) {
766 spin_unlock_bh(&adapter->tx_lock);
773 for (i = 0; i < 20; i++)
774 cpu_relax(); /*This a nop instr on i386 */
776 goto retry_getting_window;
778 local_producer = adapter->cmd_producer;
779 /* There 4 fragments per descriptor */
780 no_of_desc = (frag_count + 3) >> 2;
781 if (netdev->features & NETIF_F_TSO) {
782 if (skb_shinfo(skb)->gso_size > 0) {
785 if (((skb->nh.iph)->ihl * sizeof(u32)) +
786 ((skb->h.th)->doff * sizeof(u32)) +
787 sizeof(struct ethhdr) >
788 (sizeof(struct cmd_desc_type0) - 2)) {
793 k = adapter->cmd_producer;
794 max_tx_desc_count = adapter->max_tx_desc_count;
795 last_cmd_consumer = adapter->last_cmd_consumer;
796 if ((k + no_of_desc) >=
797 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
798 last_cmd_consumer)) {
799 port->stats.nocmddescriptor++;
800 DPRINTK(ERR, "No command descriptors available,"
801 " producer = %d, consumer = %d count=%llu,"
802 " dropping packet\n", producer,
803 adapter->last_cmd_consumer,
804 port->stats.nocmddescriptor);
806 netif_stop_queue(netdev);
807 port->flags |= NETXEN_NETDEV_STATUS;
808 spin_unlock_bh(&adapter->tx_lock);
809 return NETDEV_TX_BUSY;
811 k = get_index_range(k, max_tx_desc_count, no_of_desc);
812 adapter->cmd_producer = k;
813 adapter->total_threads++;
814 adapter->num_threads++;
816 spin_unlock_bh(&adapter->tx_lock);
817 /* Copy the descriptors into the hardware */
818 producer = local_producer;
819 saved_producer = producer;
820 hwdesc = &hw->cmd_desc_head[producer];
821 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
822 /* Take skb->data itself */
823 pbuf = &adapter->cmd_buf_arr[producer];
824 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
825 pbuf->mss = skb_shinfo(skb)->gso_size;
826 hwdesc->mss = skb_shinfo(skb)->gso_size;
831 pbuf->total_length = skb->len;
833 pbuf->cmd = TX_ETHER_PKT;
834 pbuf->frag_count = frag_count;
835 pbuf->port = port->portnum;
836 buffrag = &pbuf->frag_array[0];
837 buffrag->dma = pci_map_single(port->pdev, skb->data, first_seg_len,
839 buffrag->length = first_seg_len;
840 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
841 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
842 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
844 netxen_set_cmd_desc_port(hwdesc, port->portnum);
845 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
846 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
848 for (i = 1, k = 1; i < frag_count; i++, k++) {
849 struct skb_frag_struct *frag;
851 unsigned long offset;
854 /* move to next desc. if there is a need */
855 if ((i & 0x3) == 0) {
857 producer = get_next_index(producer,
858 adapter->max_tx_desc_count);
859 hwdesc = &hw->cmd_desc_head[producer];
860 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
862 frag = &skb_shinfo(skb)->frags[i - 1];
864 offset = frag->page_offset;
867 temp_dma = pci_map_page(port->pdev, frag->page, offset,
868 len, PCI_DMA_TODEVICE);
871 buffrag->dma = temp_dma;
872 buffrag->length = temp_len;
874 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
877 hwdesc->buffer1_length = cpu_to_le16(temp_len);
878 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
881 hwdesc->buffer2_length = cpu_to_le16(temp_len);
882 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
885 hwdesc->buffer3_length = cpu_to_le16(temp_len);
886 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
889 hwdesc->buffer4_length = temp_len;
890 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
895 producer = get_next_index(producer, adapter->max_tx_desc_count);
897 /* might change opcode to TX_TCP_LSO */
898 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
900 /* For LSO, we need to copy the MAC/IP/TCP headers into
901 * the descriptor ring
903 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
905 int hdr_len, first_hdr_len, more_hdr;
906 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
907 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
908 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
911 first_hdr_len = hdr_len;
914 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
915 hwdesc = &hw->cmd_desc_head[producer];
917 /* copy the first 64 bytes */
918 memcpy(((void *)hwdesc) + 2,
919 (void *)(skb->data), first_hdr_len);
920 producer = get_next_index(producer, max_tx_desc_count);
923 hwdesc = &hw->cmd_desc_head[producer];
924 /* copy the next 64 bytes - should be enough except
925 * for pathological case
927 memcpy((void *)hwdesc, (void *)(skb->data) +
928 first_hdr_len, hdr_len - first_hdr_len);
929 producer = get_next_index(producer, max_tx_desc_count);
932 spin_lock_bh(&adapter->tx_lock);
933 port->stats.txbytes +=
934 netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
935 /* Code to update the adapter considering how many producer threads
936 are currently working */
937 if ((--adapter->num_threads) == 0) {
938 /* This is the last thread */
939 u32 crb_producer = adapter->cmd_producer;
941 NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
943 adapter->total_threads = 0;
946 port->stats.xmitfinished++;
947 spin_unlock_bh(&adapter->tx_lock);
949 netdev->trans_start = jiffies;
951 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
953 DPRINTK(INFO, "Done. Send\n");
957 static void netxen_watchdog(unsigned long v)
959 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
960 if (adapter != g_adapter) {
961 printk("%s: ***BUG*** adapter[%p] != g_adapter[%p]\n",
962 __FUNCTION__, adapter, g_adapter);
966 SCHEDULE_WORK(&adapter->watchdog_task);
969 static void netxen_tx_timeout(struct net_device *netdev)
971 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
973 SCHEDULE_WORK(port->adapter->tx_timeout_task + port->portnum);
976 static void netxen_tx_timeout_task(struct net_device *netdev)
978 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
981 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
982 netxen_nic_driver_name, netdev->name);
984 spin_lock_irqsave(&port->adapter->lock, flags);
985 netxen_nic_close(netdev);
986 netxen_nic_open(netdev);
987 spin_unlock_irqrestore(&port->adapter->lock, flags);
988 netdev->trans_start = jiffies;
989 netif_wake_queue(netdev);
993 netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
997 DPRINTK(INFO, "Entered handle ISR\n");
999 adapter->stats.ints++;
1001 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1004 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
1005 if ((mask & 0x80) == 0) {
1006 /* not our interrupt */
1009 netxen_nic_disable_int(adapter);
1010 /* Window = 0 or 1 */
1012 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
1013 ISR_INT_TARGET_STATUS));
1014 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
1015 } while (((mask & 0x80) != 0) && (++count < 32));
1016 if ((mask & 0x80) != 0)
1017 printk("Could not disable interrupt completely\n");
1020 adapter->stats.hostints++;
1022 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1023 if (netif_rx_schedule_prep(netdev)) {
1025 * Interrupts are already disabled.
1027 __netif_rx_schedule(netdev);
1029 static unsigned int intcount = 0;
1030 if ((++intcount & 0xfff) == 0xfff)
1032 "%s: %s interrupt %d while in poll\n",
1033 netxen_nic_driver_name, netdev->name,
1040 netxen_nic_enable_int(adapter);
1047 * netxen_intr - Interrupt Handler
1048 * @irq: interrupt number
1049 * data points to adapter stucture (which may be handling more than 1 port
1051 irqreturn_t netxen_intr(int irq, void *data)
1053 struct netxen_adapter *adapter;
1054 struct netxen_port *port;
1055 struct net_device *netdev;
1058 if (unlikely(!irq)) {
1059 return IRQ_NONE; /* Not our interrupt */
1062 adapter = (struct netxen_adapter *)data;
1063 for (i = 0; i < adapter->ahw.max_ports; i++) {
1064 port = adapter->port[i];
1065 netdev = port->netdev;
1067 /* process our status queue (for all 4 ports) */
1068 if (netif_running(netdev)) {
1069 netxen_handle_int(adapter, netdev);
1077 static int netxen_nic_poll(struct net_device *netdev, int *budget)
1079 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
1080 struct netxen_adapter *adapter = port->adapter;
1081 int work_to_do = min(*budget, netdev->quota);
1087 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1088 port->stats.polled++;
1091 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1093 * Fairness issue. This will give undue weight to the
1094 * receive context 0.
1098 * To avoid starvation, we give each of our receivers,
1099 * a fraction of the quota. Sometimes, it might happen that we
1100 * have enough quota to process every packet, but since all the
1101 * packets are on one context, it gets only half of the quota,
1102 * and ends up not processing it.
1104 this_work_done = netxen_process_rcv_ring(adapter, ctx,
1107 work_done += this_work_done;
1110 netdev->quota -= work_done;
1111 *budget -= work_done;
1113 if (work_done >= work_to_do && netxen_nic_rx_has_work(adapter) != 0)
1116 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1119 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1120 work_done, work_to_do);
1122 netif_rx_complete(netdev);
1123 netxen_nic_enable_int(adapter);
1129 #ifdef CONFIG_NET_POLL_CONTROLLER
1130 static void netxen_nic_poll_controller(struct net_device *netdev)
1132 struct netxen_port *port = netdev_priv(netdev);
1133 struct netxen_adapter *adapter = port->adapter;
1134 disable_irq(adapter->irq);
1135 netxen_intr(adapter->irq, adapter);
1136 enable_irq(adapter->irq);
1140 * netxen_nic_ioctl () We provide the tcl/phanmon support through these
1144 netxen_nic_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1147 unsigned long nr_bytes = 0;
1148 struct netxen_port *port = netdev_priv(netdev);
1149 struct netxen_adapter *adapter = port->adapter;
1150 char dev_name[NETXEN_NIC_NAME_LEN];
1152 DPRINTK(INFO, "doing ioctl for %s\n", netdev->name);
1154 case NETXEN_NIC_CMD:
1155 err = netxen_nic_do_ioctl(adapter, (void *)ifr->ifr_data, port);
1158 case NETXEN_NIC_NAME:
1159 DPRINTK(INFO, "ioctl cmd for NetXen\n");
1160 if (ifr->ifr_data) {
1161 sprintf(dev_name, "%s-%d", NETXEN_NIC_NAME_RSP,
1164 copy_to_user((char __user *)ifr->ifr_data, dev_name,
1165 NETXEN_NIC_NAME_LEN);
1173 DPRINTK(INFO, "ioctl cmd %x not supported\n", cmd);
1181 static struct pci_driver netxen_driver = {
1182 .name = netxen_nic_driver_name,
1183 .id_table = netxen_pci_tbl,
1184 .probe = netxen_nic_probe,
1185 .remove = __devexit_p(netxen_nic_remove)
1188 /* Driver Registration on NetXen card */
1190 static int __init netxen_init_module(void)
1192 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1195 return pci_module_init(&netxen_driver);
1198 module_init(netxen_init_module);
1200 static void __exit netxen_exit_module(void)
1203 * Wait for some time to allow the dma to drain, if any.
1205 destroy_workqueue(netxen_workq);
1206 pci_unregister_driver(&netxen_driver);
1209 module_exit(netxen_exit_module);