pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / netxen / netxen_nic_main.c
blob2a1d6d7ec351d37f4a7189f2e3987d76a5a8990c
1 /*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
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,
18 * MA 02111-1307, USA.
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
30 * Main source file for NetXen NIC Driver on Linux
34 #include <linux/vmalloc.h>
35 #include <linux/highmem.h>
36 #include "netxen_nic_hw.h"
38 #include "netxen_nic.h"
39 #include "netxen_nic_phan_reg.h"
41 #include <linux/dma-mapping.h>
42 #include <net/ip.h>
44 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
48 char netxen_nic_driver_name[] = "netxen_nic";
49 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
50 NETXEN_NIC_LINUX_VERSIONID;
52 #define NETXEN_NETDEV_WEIGHT 120
53 #define NETXEN_ADAPTER_UP_MAGIC 777
54 #define NETXEN_NIC_PEG_TUNE 0
56 #define DMA_32BIT_MASK 0x00000000ffffffffULL
57 #define DMA_35BIT_MASK 0x00000007ffffffffULL
59 /* Local functions to NetXen NIC driver */
60 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
61 const struct pci_device_id *ent);
62 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
63 static int netxen_nic_open(struct net_device *netdev);
64 static int netxen_nic_close(struct net_device *netdev);
65 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
66 static void netxen_tx_timeout(struct net_device *netdev);
67 static void netxen_tx_timeout_task(struct work_struct *work);
68 static void netxen_watchdog(unsigned long);
69 static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
70 static int netxen_nic_poll(struct napi_struct *napi, int budget);
71 #ifdef CONFIG_NET_POLL_CONTROLLER
72 static void netxen_nic_poll_controller(struct net_device *netdev);
73 #endif
74 static irqreturn_t netxen_intr(int irq, void *data);
76 int physical_port[] = {0, 1, 2, 3};
78 /* PCI Device ID Table */
79 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
80 {PCI_DEVICE(0x4040, 0x0001)},
81 {PCI_DEVICE(0x4040, 0x0002)},
82 {PCI_DEVICE(0x4040, 0x0003)},
83 {PCI_DEVICE(0x4040, 0x0004)},
84 {PCI_DEVICE(0x4040, 0x0005)},
85 {PCI_DEVICE(0x4040, 0x0024)},
86 {PCI_DEVICE(0x4040, 0x0025)},
87 {0,}
90 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
92 struct workqueue_struct *netxen_workq;
93 static void netxen_watchdog(unsigned long);
95 static inline void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
96 uint32_t crb_producer)
98 switch (adapter->portnum) {
99 case 0:
100 writel(crb_producer, NETXEN_CRB_NORMALIZE
101 (adapter, CRB_CMD_PRODUCER_OFFSET));
102 return;
103 case 1:
104 writel(crb_producer, NETXEN_CRB_NORMALIZE
105 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
106 return;
107 case 2:
108 writel(crb_producer, NETXEN_CRB_NORMALIZE
109 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
110 return;
111 case 3:
112 writel(crb_producer, NETXEN_CRB_NORMALIZE
113 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
114 return;
115 default:
116 printk(KERN_WARNING "We tried to update "
117 "CRB_CMD_PRODUCER_OFFSET for invalid "
118 "PCI function id %d\n",
119 adapter->portnum);
120 return;
124 static inline void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
125 u32 crb_consumer)
127 switch (adapter->portnum) {
128 case 0:
129 writel(crb_consumer, NETXEN_CRB_NORMALIZE
130 (adapter, CRB_CMD_CONSUMER_OFFSET));
131 return;
132 case 1:
133 writel(crb_consumer, NETXEN_CRB_NORMALIZE
134 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
135 return;
136 case 2:
137 writel(crb_consumer, NETXEN_CRB_NORMALIZE
138 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
139 return;
140 case 3:
141 writel(crb_consumer, NETXEN_CRB_NORMALIZE
142 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
143 return;
144 default:
145 printk(KERN_WARNING "We tried to update "
146 "CRB_CMD_PRODUCER_OFFSET for invalid "
147 "PCI function id %d\n",
148 adapter->portnum);
149 return;
153 #define ADAPTER_LIST_SIZE 12
154 int netxen_cards_found;
156 static void netxen_nic_disable_int(struct netxen_adapter *adapter)
158 uint32_t mask = 0x7ff;
159 int retries = 32;
161 DPRINTK(1, INFO, "Entered ISR Disable \n");
163 switch (adapter->portnum) {
164 case 0:
165 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
166 break;
167 case 1:
168 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
169 break;
170 case 2:
171 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
172 break;
173 case 3:
174 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
175 break;
178 if (adapter->intr_scheme != -1 &&
179 adapter->intr_scheme != INTR_SCHEME_PERPORT)
180 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
182 /* Window = 0 or 1 */
183 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
184 do {
185 writel(0xffffffff,
186 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
187 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
188 if (!(mask & 0x80))
189 break;
190 udelay(10);
191 } while (--retries);
193 if (!retries) {
194 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
195 netxen_nic_driver_name);
199 DPRINTK(1, INFO, "Done with Disable Int\n");
202 static void netxen_nic_enable_int(struct netxen_adapter *adapter)
204 u32 mask;
206 DPRINTK(1, INFO, "Entered ISR Enable \n");
208 if (adapter->intr_scheme != -1 &&
209 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
210 switch (adapter->ahw.board_type) {
211 case NETXEN_NIC_GBE:
212 mask = 0x77b;
213 break;
214 case NETXEN_NIC_XGBE:
215 mask = 0x77f;
216 break;
217 default:
218 mask = 0x7ff;
219 break;
222 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
225 switch (adapter->portnum) {
226 case 0:
227 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
228 break;
229 case 1:
230 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
231 break;
232 case 2:
233 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
234 break;
235 case 3:
236 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
237 break;
240 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
241 mask = 0xbff;
242 if (adapter->intr_scheme != -1 &&
243 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
244 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
246 writel(mask,
247 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
250 DPRINTK(1, INFO, "Done with enable Int\n");
254 * netxen_nic_probe()
256 * The Linux system will invoke this after identifying the vendor ID and
257 * device Id in the pci_tbl supported by this module.
259 * A quad port card has one operational PCI config space, (function 0),
260 * which is used to access all four ports.
262 * This routine will initialize the adapter, and setup the global parameters
263 * along with the port's specific structure.
265 static int __devinit
266 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
268 struct net_device *netdev = NULL;
269 struct netxen_adapter *adapter = NULL;
270 void __iomem *mem_ptr0 = NULL;
271 void __iomem *mem_ptr1 = NULL;
272 void __iomem *mem_ptr2 = NULL;
273 unsigned long first_page_group_end;
274 unsigned long first_page_group_start;
277 u8 __iomem *db_ptr = NULL;
278 unsigned long mem_base, mem_len, db_base, db_len;
279 int pci_using_dac, i = 0, err;
280 int ring;
281 struct netxen_recv_context *recv_ctx = NULL;
282 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
283 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
284 u64 mac_addr[FLASH_NUM_PORTS + 1];
285 int valid_mac = 0;
286 u32 val;
287 int pci_func_id = PCI_FUNC(pdev->devfn);
288 DECLARE_MAC_BUF(mac);
290 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
292 if (pdev->class != 0x020000) {
293 printk(KERN_ERR"NetXen function %d, class %x will not"
294 "be enabled.\n",pci_func_id, pdev->class);
295 return -ENODEV;
297 if ((err = pci_enable_device(pdev)))
298 return err;
299 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
300 err = -ENODEV;
301 goto err_out_disable_pdev;
304 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
305 goto err_out_disable_pdev;
307 pci_set_master(pdev);
308 if (pdev->revision == NX_P2_C1 &&
309 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
310 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
311 pci_using_dac = 1;
312 } else {
313 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
314 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
315 goto err_out_free_res;
317 pci_using_dac = 0;
321 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
322 if(!netdev) {
323 printk(KERN_ERR"%s: Failed to allocate memory for the "
324 "device block.Check system memory resource"
325 " usage.\n", netxen_nic_driver_name);
326 goto err_out_free_res;
329 SET_NETDEV_DEV(netdev, &pdev->dev);
331 adapter = netdev->priv;
333 adapter->ahw.pdev = pdev;
334 adapter->ahw.pci_func = pci_func_id;
335 spin_lock_init(&adapter->tx_lock);
337 /* remap phys address */
338 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
339 mem_len = pci_resource_len(pdev, 0);
341 /* 128 Meg of memory */
342 if (mem_len == NETXEN_PCI_128MB_SIZE) {
343 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
344 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
345 SECOND_PAGE_GROUP_SIZE);
346 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
347 THIRD_PAGE_GROUP_SIZE);
348 first_page_group_start = FIRST_PAGE_GROUP_START;
349 first_page_group_end = FIRST_PAGE_GROUP_END;
350 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
351 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
352 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
353 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
354 first_page_group_start = 0;
355 first_page_group_end = 0;
356 } else {
357 err = -EIO;
358 goto err_out_free_netdev;
361 if (((mem_ptr0 == 0UL) && (mem_len == NETXEN_PCI_128MB_SIZE)) ||
362 (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
363 DPRINTK(ERR,
364 "Cannot remap adapter memory aborting.:"
365 "0 -> %p, 1 -> %p, 2 -> %p\n",
366 mem_ptr0, mem_ptr1, mem_ptr2);
368 err = -EIO;
369 goto err_out_iounmap;
371 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
372 db_len = pci_resource_len(pdev, 4);
374 if (db_len == 0) {
375 printk(KERN_ERR "%s: doorbell is disabled\n",
376 netxen_nic_driver_name);
377 err = -EIO;
378 goto err_out_iounmap;
380 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
381 db_len);
383 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
384 if (!db_ptr) {
385 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
386 netxen_nic_driver_name);
387 err = -EIO;
388 goto err_out_iounmap;
390 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
392 adapter->ahw.pci_base0 = mem_ptr0;
393 adapter->ahw.first_page_group_start = first_page_group_start;
394 adapter->ahw.first_page_group_end = first_page_group_end;
395 adapter->ahw.pci_base1 = mem_ptr1;
396 adapter->ahw.pci_base2 = mem_ptr2;
397 adapter->ahw.db_base = db_ptr;
398 adapter->ahw.db_len = db_len;
400 adapter->netdev = netdev;
401 adapter->pdev = pdev;
403 netif_napi_add(netdev, &adapter->napi,
404 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
406 /* this will be read from FW later */
407 adapter->intr_scheme = -1;
409 /* This will be reset for mezz cards */
410 adapter->portnum = pci_func_id;
411 adapter->status &= ~NETXEN_NETDEV_STATUS;
412 adapter->rx_csum = 1;
414 netdev->open = netxen_nic_open;
415 netdev->stop = netxen_nic_close;
416 netdev->hard_start_xmit = netxen_nic_xmit_frame;
417 netdev->get_stats = netxen_nic_get_stats;
418 netdev->set_multicast_list = netxen_nic_set_multi;
419 netdev->set_mac_address = netxen_nic_set_mac;
420 netdev->change_mtu = netxen_nic_change_mtu;
421 netdev->tx_timeout = netxen_tx_timeout;
422 netdev->watchdog_timeo = HZ;
424 netxen_nic_change_mtu(netdev, netdev->mtu);
426 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
427 #ifdef CONFIG_NET_POLL_CONTROLLER
428 netdev->poll_controller = netxen_nic_poll_controller;
429 #endif
430 /* ScatterGather support */
431 netdev->features = NETIF_F_SG;
432 netdev->features |= NETIF_F_IP_CSUM;
433 netdev->features |= NETIF_F_TSO;
435 if (pci_using_dac)
436 netdev->features |= NETIF_F_HIGHDMA;
438 if (pci_enable_msi(pdev))
439 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
440 else
441 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
443 netdev->irq = pdev->irq;
444 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
447 * Set the CRB window to invalid. If any register in window 0 is
448 * accessed it should set the window to 0 and then reset it to 1.
450 adapter->curr_window = 255;
452 /* initialize the adapter */
453 netxen_initialize_adapter_hw(adapter);
456 * Adapter in our case is quad port so initialize it before
457 * initializing the ports
460 netxen_initialize_adapter_ops(adapter);
462 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
463 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
464 (adapter->ahw.boardcfg.board_type ==
465 NETXEN_BRDTYPE_P2_SB31_2G))
466 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
467 else
468 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
469 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
470 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
472 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
473 if (cmd_buf_arr == NULL) {
474 printk(KERN_ERR
475 "%s: Could not allocate cmd_buf_arr memory:%d\n",
476 netxen_nic_driver_name, (int)TX_RINGSIZE);
477 err = -ENOMEM;
478 goto err_out_free_adapter;
480 memset(cmd_buf_arr, 0, TX_RINGSIZE);
481 adapter->cmd_buf_arr = cmd_buf_arr;
483 for (i = 0; i < MAX_RCV_CTX; ++i) {
484 recv_ctx = &adapter->recv_ctx[i];
485 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
486 rcv_desc = &recv_ctx->rcv_desc[ring];
487 switch (RCV_DESC_TYPE(ring)) {
488 case RCV_DESC_NORMAL:
489 rcv_desc->max_rx_desc_count =
490 adapter->max_rx_desc_count;
491 rcv_desc->flags = RCV_DESC_NORMAL;
492 rcv_desc->dma_size = RX_DMA_MAP_LEN;
493 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
494 break;
496 case RCV_DESC_JUMBO:
497 rcv_desc->max_rx_desc_count =
498 adapter->max_jumbo_rx_desc_count;
499 rcv_desc->flags = RCV_DESC_JUMBO;
500 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
501 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
502 break;
504 case RCV_RING_LRO:
505 rcv_desc->max_rx_desc_count =
506 adapter->max_lro_rx_desc_count;
507 rcv_desc->flags = RCV_DESC_LRO;
508 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
509 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
510 break;
513 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
514 vmalloc(RCV_BUFFSIZE);
516 if (rcv_desc->rx_buf_arr == NULL) {
517 printk(KERN_ERR "%s: Could not allocate"
518 "rcv_desc->rx_buf_arr memory:%d\n",
519 netxen_nic_driver_name,
520 (int)RCV_BUFFSIZE);
521 err = -ENOMEM;
522 goto err_out_free_rx_buffer;
524 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
529 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
531 /* Mezz cards have PCI function 0,2,3 enabled */
532 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB31_10G_IMEZ)
533 && (pci_func_id >= 2))
534 adapter->portnum = pci_func_id - 2;
536 #ifdef CONFIG_IA64
537 if(adapter->portnum == 0) {
538 netxen_pinit_from_rom(adapter, 0);
539 udelay(500);
540 netxen_load_firmware(adapter);
542 #endif
544 init_timer(&adapter->watchdog_timer);
545 adapter->ahw.xg_linkup = 0;
546 adapter->watchdog_timer.function = &netxen_watchdog;
547 adapter->watchdog_timer.data = (unsigned long)adapter;
548 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
549 adapter->ahw.pdev = pdev;
550 adapter->proc_cmd_buf_counter = 0;
551 adapter->ahw.revision_id = pdev->revision;
553 /* make sure Window == 1 */
554 netxen_nic_pci_change_crbwindow(adapter, 1);
556 netxen_nic_update_cmd_producer(adapter, 0);
557 netxen_nic_update_cmd_consumer(adapter, 0);
558 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
560 if (netxen_is_flash_supported(adapter) == 0 &&
561 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
562 valid_mac = 1;
563 else
564 valid_mac = 0;
566 if (valid_mac) {
567 unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum];
568 netdev->dev_addr[0] = *(p + 5);
569 netdev->dev_addr[1] = *(p + 4);
570 netdev->dev_addr[2] = *(p + 3);
571 netdev->dev_addr[3] = *(p + 2);
572 netdev->dev_addr[4] = *(p + 1);
573 netdev->dev_addr[5] = *(p + 0);
575 memcpy(netdev->perm_addr, netdev->dev_addr,
576 netdev->addr_len);
577 if (!is_valid_ether_addr(netdev->perm_addr)) {
578 printk(KERN_ERR "%s: Bad MAC address %s.\n",
579 netxen_nic_driver_name,
580 print_mac(mac, netdev->dev_addr));
581 } else {
582 if (adapter->macaddr_set)
583 adapter->macaddr_set(adapter,
584 netdev->dev_addr);
588 if (adapter->portnum == 0) {
589 err = netxen_initialize_adapter_offload(adapter);
590 if (err)
591 goto err_out_free_rx_buffer;
592 val = readl(NETXEN_CRB_NORMALIZE(adapter,
593 NETXEN_CAM_RAM(0x1fc)));
594 if (val == 0x55555555) {
595 /* This is the first boot after power up */
596 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
597 if (!(val & 0x4)) {
598 val |= 0x4;
599 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
600 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
601 if (!(val & 0x4))
602 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
603 netxen_nic_driver_name);
605 val = readl(NETXEN_CRB_NORMALIZE(adapter,
606 NETXEN_ROMUSB_GLB_SW_RESET));
607 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
608 if (val != 0x80000f) {
609 /* clear the register for future unloads/loads */
610 writel(0, NETXEN_CRB_NORMALIZE(adapter,
611 NETXEN_CAM_RAM(0x1fc)));
612 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
613 err = -ENODEV;
614 goto err_out_free_dev;
618 /* clear the register for future unloads/loads */
619 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
620 printk(KERN_INFO "State: 0x%0x\n",
621 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
624 * Tell the hardware our version number.
626 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
627 | ((_NETXEN_NIC_LINUX_MINOR << 8))
628 | (_NETXEN_NIC_LINUX_SUBVERSION);
629 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
631 /* Unlock the HW, prompting the boot sequence */
632 writel(1,
633 NETXEN_CRB_NORMALIZE(adapter,
634 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
635 /* Handshake with the card before we register the devices. */
636 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
640 * See if the firmware gave us a virtual-physical port mapping.
642 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
643 if (i != 0x55555555)
644 physical_port[adapter->portnum] = i;
646 netif_carrier_off(netdev);
647 netif_stop_queue(netdev);
649 if ((err = register_netdev(netdev))) {
650 printk(KERN_ERR "%s: register_netdev failed port #%d"
651 " aborting\n", netxen_nic_driver_name,
652 adapter->portnum);
653 err = -EIO;
654 goto err_out_free_dev;
657 pci_set_drvdata(pdev, adapter);
659 switch (adapter->ahw.board_type) {
660 case NETXEN_NIC_GBE:
661 printk(KERN_INFO "%s: QUAD GbE board initialized\n",
662 netxen_nic_driver_name);
663 break;
665 case NETXEN_NIC_XGBE:
666 printk(KERN_INFO "%s: XGbE board initialized\n",
667 netxen_nic_driver_name);
668 break;
671 adapter->driver_mismatch = 0;
673 return 0;
675 err_out_free_dev:
676 if (adapter->portnum == 0)
677 netxen_free_adapter_offload(adapter);
679 err_out_free_rx_buffer:
680 for (i = 0; i < MAX_RCV_CTX; ++i) {
681 recv_ctx = &adapter->recv_ctx[i];
682 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
683 rcv_desc = &recv_ctx->rcv_desc[ring];
684 if (rcv_desc->rx_buf_arr != NULL) {
685 vfree(rcv_desc->rx_buf_arr);
686 rcv_desc->rx_buf_arr = NULL;
690 vfree(cmd_buf_arr);
692 err_out_free_adapter:
693 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
694 pci_disable_msi(pdev);
696 pci_set_drvdata(pdev, NULL);
698 if (db_ptr)
699 iounmap(db_ptr);
701 err_out_iounmap:
702 if (mem_ptr0)
703 iounmap(mem_ptr0);
704 if (mem_ptr1)
705 iounmap(mem_ptr1);
706 if (mem_ptr2)
707 iounmap(mem_ptr2);
709 err_out_free_netdev:
710 free_netdev(netdev);
712 err_out_free_res:
713 pci_release_regions(pdev);
715 err_out_disable_pdev:
716 pci_disable_device(pdev);
717 return err;
720 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
722 struct netxen_adapter *adapter;
723 struct net_device *netdev;
724 struct netxen_rx_buffer *buffer;
725 struct netxen_recv_context *recv_ctx;
726 struct netxen_rcv_desc_ctx *rcv_desc;
727 int i, ctxid, ring;
728 static int init_firmware_done = 0;
730 adapter = pci_get_drvdata(pdev);
731 if (adapter == NULL)
732 return;
734 netdev = adapter->netdev;
736 unregister_netdev(netdev);
738 if (adapter->stop_port)
739 adapter->stop_port(adapter);
741 netxen_nic_disable_int(adapter);
743 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
744 init_firmware_done++;
745 netxen_free_hw_resources(adapter);
748 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
749 recv_ctx = &adapter->recv_ctx[ctxid];
750 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
751 rcv_desc = &recv_ctx->rcv_desc[ring];
752 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
753 buffer = &(rcv_desc->rx_buf_arr[i]);
754 if (buffer->state == NETXEN_BUFFER_FREE)
755 continue;
756 pci_unmap_single(pdev, buffer->dma,
757 rcv_desc->dma_size,
758 PCI_DMA_FROMDEVICE);
759 if (buffer->skb != NULL)
760 dev_kfree_skb_any(buffer->skb);
762 vfree(rcv_desc->rx_buf_arr);
766 vfree(adapter->cmd_buf_arr);
768 if (adapter->portnum == 0) {
769 if (init_firmware_done) {
770 i = 100;
771 do {
772 if (dma_watchdog_shutdown_request(adapter) == 1)
773 break;
774 msleep(100);
775 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
776 break;
777 } while (--i);
779 if (i == 0)
780 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
781 netdev->name);
783 /* clear the register for future unloads/loads */
784 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
785 printk(KERN_INFO "State: 0x%0x\n",
786 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
788 /* leave the hw in the same state as reboot */
789 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
790 netxen_pinit_from_rom(adapter, 0);
791 msleep(1);
792 netxen_load_firmware(adapter);
793 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
796 /* clear the register for future unloads/loads */
797 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
798 printk(KERN_INFO "State: 0x%0x\n",
799 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
801 i = 100;
802 do {
803 if (dma_watchdog_shutdown_request(adapter) == 1)
804 break;
805 msleep(100);
806 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
807 break;
808 } while (--i);
810 if (i) {
811 netxen_free_adapter_offload(adapter);
812 } else {
813 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
814 netdev->name);
818 if (adapter->irq)
819 free_irq(adapter->irq, adapter);
821 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
822 pci_disable_msi(pdev);
824 iounmap(adapter->ahw.db_base);
825 iounmap(adapter->ahw.pci_base0);
826 iounmap(adapter->ahw.pci_base1);
827 iounmap(adapter->ahw.pci_base2);
829 pci_release_regions(pdev);
830 pci_disable_device(pdev);
831 pci_set_drvdata(pdev, NULL);
833 free_netdev(netdev);
837 * Called when a network interface is made active
838 * @returns 0 on success, negative value on failure
840 static int netxen_nic_open(struct net_device *netdev)
842 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
843 int err = 0;
844 int ctx, ring;
846 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
847 err = netxen_init_firmware(adapter);
848 if (err != 0) {
849 printk(KERN_ERR "Failed to init firmware\n");
850 return -EIO;
852 netxen_nic_flash_print(adapter);
854 /* setup all the resources for the Phantom... */
855 /* this include the descriptors for rcv, tx, and status */
856 netxen_nic_clear_stats(adapter);
857 err = netxen_nic_hw_resources(adapter);
858 if (err) {
859 printk(KERN_ERR "Error in setting hw resources:%d\n",
860 err);
861 return err;
863 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
864 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
865 netxen_post_rx_buffers(adapter, ctx, ring);
867 adapter->irq = adapter->ahw.pdev->irq;
868 err = request_irq(adapter->ahw.pdev->irq, netxen_intr,
869 IRQF_SHARED|IRQF_SAMPLE_RANDOM, netdev->name,
870 adapter);
871 if (err) {
872 printk(KERN_ERR "request_irq failed with: %d\n", err);
873 netxen_free_hw_resources(adapter);
874 return err;
877 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
879 if (!adapter->driver_mismatch)
880 mod_timer(&adapter->watchdog_timer, jiffies);
882 napi_enable(&adapter->napi);
884 netxen_nic_enable_int(adapter);
886 /* Done here again so that even if phantom sw overwrote it,
887 * we set it */
888 if (adapter->init_port
889 && adapter->init_port(adapter, adapter->portnum) != 0) {
890 del_timer_sync(&adapter->watchdog_timer);
891 printk(KERN_ERR "%s: Failed to initialize port %d\n",
892 netxen_nic_driver_name, adapter->portnum);
893 napi_disable(&adapter->napi);
894 return -EIO;
896 if (adapter->macaddr_set)
897 adapter->macaddr_set(adapter, netdev->dev_addr);
899 netxen_nic_set_link_parameters(adapter);
901 netxen_nic_set_multi(netdev);
902 if (adapter->set_mtu)
903 adapter->set_mtu(adapter, netdev->mtu);
905 if (!adapter->driver_mismatch)
906 netif_start_queue(netdev);
908 return 0;
912 * netxen_nic_close - Disables a network interface entry point
914 static int netxen_nic_close(struct net_device *netdev)
916 struct netxen_adapter *adapter = netdev_priv(netdev);
917 int i, j;
918 struct netxen_cmd_buffer *cmd_buff;
919 struct netxen_skb_frag *buffrag;
921 netif_carrier_off(netdev);
922 netif_stop_queue(netdev);
923 napi_disable(&adapter->napi);
925 netxen_nic_disable_int(adapter);
927 cmd_buff = adapter->cmd_buf_arr;
928 for (i = 0; i < adapter->max_tx_desc_count; i++) {
929 buffrag = cmd_buff->frag_array;
930 if (buffrag->dma) {
931 pci_unmap_single(adapter->pdev, buffrag->dma,
932 buffrag->length, PCI_DMA_TODEVICE);
933 buffrag->dma = 0ULL;
935 for (j = 0; j < cmd_buff->frag_count; j++) {
936 buffrag++;
937 if (buffrag->dma) {
938 pci_unmap_page(adapter->pdev, buffrag->dma,
939 buffrag->length,
940 PCI_DMA_TODEVICE);
941 buffrag->dma = 0ULL;
944 /* Free the skb we received in netxen_nic_xmit_frame */
945 if (cmd_buff->skb) {
946 dev_kfree_skb_any(cmd_buff->skb);
947 cmd_buff->skb = NULL;
949 cmd_buff++;
951 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
952 FLUSH_SCHEDULED_WORK();
953 del_timer_sync(&adapter->watchdog_timer);
956 return 0;
959 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
961 struct netxen_adapter *adapter = netdev_priv(netdev);
962 struct netxen_hardware_context *hw = &adapter->ahw;
963 unsigned int first_seg_len = skb->len - skb->data_len;
964 struct netxen_skb_frag *buffrag;
965 unsigned int i;
967 u32 producer = 0;
968 u32 saved_producer = 0;
969 struct cmd_desc_type0 *hwdesc;
970 int k;
971 struct netxen_cmd_buffer *pbuf = NULL;
972 static int dropped_packet = 0;
973 int frag_count;
974 u32 local_producer = 0;
975 u32 max_tx_desc_count = 0;
976 u32 last_cmd_consumer = 0;
977 int no_of_desc;
979 adapter->stats.xmitcalled++;
980 frag_count = skb_shinfo(skb)->nr_frags + 1;
982 if (unlikely(skb->len <= 0)) {
983 dev_kfree_skb_any(skb);
984 adapter->stats.badskblen++;
985 return NETDEV_TX_OK;
988 if (frag_count > MAX_BUFFERS_PER_CMD) {
989 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
990 "too large, can handle only %d frags\n",
991 netxen_nic_driver_name, netdev->name,
992 frag_count, MAX_BUFFERS_PER_CMD);
993 adapter->stats.txdropped++;
994 if ((++dropped_packet & 0xff) == 0xff)
995 printk("%s: %s droppped packets = %d\n",
996 netxen_nic_driver_name, netdev->name,
997 dropped_packet);
999 return NETDEV_TX_OK;
1003 * Everything is set up. Now, we just need to transmit it out.
1004 * Note that we have to copy the contents of buffer over to
1005 * right place. Later on, this can be optimized out by de-coupling the
1006 * producer index from the buffer index.
1008 retry_getting_window:
1009 spin_lock_bh(&adapter->tx_lock);
1010 if (adapter->total_threads >= MAX_XMIT_PRODUCERS) {
1011 spin_unlock_bh(&adapter->tx_lock);
1013 * Yield CPU
1015 if (!in_atomic())
1016 schedule();
1017 else {
1018 for (i = 0; i < 20; i++)
1019 cpu_relax(); /*This a nop instr on i386 */
1021 goto retry_getting_window;
1023 local_producer = adapter->cmd_producer;
1024 /* There 4 fragments per descriptor */
1025 no_of_desc = (frag_count + 3) >> 2;
1026 if (netdev->features & NETIF_F_TSO) {
1027 if (skb_shinfo(skb)->gso_size > 0) {
1029 no_of_desc++;
1030 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
1031 sizeof(struct ethhdr)) >
1032 (sizeof(struct cmd_desc_type0) - 2)) {
1033 no_of_desc++;
1037 k = adapter->cmd_producer;
1038 max_tx_desc_count = adapter->max_tx_desc_count;
1039 last_cmd_consumer = adapter->last_cmd_consumer;
1040 if ((k + no_of_desc) >=
1041 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
1042 last_cmd_consumer)) {
1043 netif_stop_queue(netdev);
1044 adapter->flags |= NETXEN_NETDEV_STATUS;
1045 spin_unlock_bh(&adapter->tx_lock);
1046 return NETDEV_TX_BUSY;
1048 k = get_index_range(k, max_tx_desc_count, no_of_desc);
1049 adapter->cmd_producer = k;
1050 adapter->total_threads++;
1051 adapter->num_threads++;
1053 spin_unlock_bh(&adapter->tx_lock);
1054 /* Copy the descriptors into the hardware */
1055 producer = local_producer;
1056 saved_producer = producer;
1057 hwdesc = &hw->cmd_desc_head[producer];
1058 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1059 /* Take skb->data itself */
1060 pbuf = &adapter->cmd_buf_arr[producer];
1061 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
1062 pbuf->mss = skb_shinfo(skb)->gso_size;
1063 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1064 } else {
1065 pbuf->mss = 0;
1066 hwdesc->mss = 0;
1068 pbuf->total_length = skb->len;
1069 pbuf->skb = skb;
1070 pbuf->cmd = TX_ETHER_PKT;
1071 pbuf->frag_count = frag_count;
1072 pbuf->port = adapter->portnum;
1073 buffrag = &pbuf->frag_array[0];
1074 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
1075 PCI_DMA_TODEVICE);
1076 buffrag->length = first_seg_len;
1077 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1078 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1079 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
1081 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
1082 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
1083 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1084 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1086 for (i = 1, k = 1; i < frag_count; i++, k++) {
1087 struct skb_frag_struct *frag;
1088 int len, temp_len;
1089 unsigned long offset;
1090 dma_addr_t temp_dma;
1092 /* move to next desc. if there is a need */
1093 if ((i & 0x3) == 0) {
1094 k = 0;
1095 producer = get_next_index(producer,
1096 adapter->max_tx_desc_count);
1097 hwdesc = &hw->cmd_desc_head[producer];
1098 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1100 frag = &skb_shinfo(skb)->frags[i - 1];
1101 len = frag->size;
1102 offset = frag->page_offset;
1104 temp_len = len;
1105 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1106 len, PCI_DMA_TODEVICE);
1108 buffrag++;
1109 buffrag->dma = temp_dma;
1110 buffrag->length = temp_len;
1112 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
1113 switch (k) {
1114 case 0:
1115 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1116 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1117 break;
1118 case 1:
1119 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1120 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1121 break;
1122 case 2:
1123 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1124 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1125 break;
1126 case 3:
1127 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1128 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1129 break;
1131 frag++;
1133 producer = get_next_index(producer, adapter->max_tx_desc_count);
1135 /* might change opcode to TX_TCP_LSO */
1136 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1138 /* For LSO, we need to copy the MAC/IP/TCP headers into
1139 * the descriptor ring
1141 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1142 == TX_TCP_LSO) {
1143 int hdr_len, first_hdr_len, more_hdr;
1144 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1145 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1146 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1147 more_hdr = 1;
1148 } else {
1149 first_hdr_len = hdr_len;
1150 more_hdr = 0;
1152 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1153 hwdesc = &hw->cmd_desc_head[producer];
1155 /* copy the first 64 bytes */
1156 memcpy(((void *)hwdesc) + 2,
1157 (void *)(skb->data), first_hdr_len);
1158 producer = get_next_index(producer, max_tx_desc_count);
1160 if (more_hdr) {
1161 hwdesc = &hw->cmd_desc_head[producer];
1162 /* copy the next 64 bytes - should be enough except
1163 * for pathological case
1165 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1166 hwdesc,
1167 (hdr_len -
1168 first_hdr_len));
1169 producer = get_next_index(producer, max_tx_desc_count);
1173 i = netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
1175 hw->cmd_desc_head[saved_producer].flags_opcode =
1176 cpu_to_le16(hw->cmd_desc_head[saved_producer].flags_opcode);
1177 hw->cmd_desc_head[saved_producer].num_of_buffers_total_length =
1178 cpu_to_le32(hw->cmd_desc_head[saved_producer].
1179 num_of_buffers_total_length);
1181 spin_lock_bh(&adapter->tx_lock);
1182 adapter->stats.txbytes += i;
1184 /* Code to update the adapter considering how many producer threads
1185 are currently working */
1186 if ((--adapter->num_threads) == 0) {
1187 /* This is the last thread */
1188 u32 crb_producer = adapter->cmd_producer;
1189 netxen_nic_update_cmd_producer(adapter, crb_producer);
1190 wmb();
1191 adapter->total_threads = 0;
1194 adapter->stats.xmitfinished++;
1195 spin_unlock_bh(&adapter->tx_lock);
1197 netdev->trans_start = jiffies;
1199 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
1201 DPRINTK(INFO, "Done. Send\n");
1202 return NETDEV_TX_OK;
1205 static void netxen_watchdog(unsigned long v)
1207 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1209 SCHEDULE_WORK(&adapter->watchdog_task);
1212 static void netxen_tx_timeout(struct net_device *netdev)
1214 struct netxen_adapter *adapter = (struct netxen_adapter *)
1215 netdev_priv(netdev);
1216 SCHEDULE_WORK(&adapter->tx_timeout_task);
1219 static void netxen_tx_timeout_task(struct work_struct *work)
1221 struct netxen_adapter *adapter =
1222 container_of(work, struct netxen_adapter, tx_timeout_task);
1224 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1225 netxen_nic_driver_name, adapter->netdev->name);
1227 netxen_nic_close(adapter->netdev);
1228 netxen_nic_open(adapter->netdev);
1229 adapter->netdev->trans_start = jiffies;
1230 netif_wake_queue(adapter->netdev);
1233 static int
1234 netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
1236 u32 ret = 0;
1238 DPRINTK(INFO, "Entered handle ISR\n");
1239 adapter->stats.ints++;
1241 netxen_nic_disable_int(adapter);
1243 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1244 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
1246 * Interrupts are already disabled.
1248 __netif_rx_schedule(netdev, &adapter->napi);
1249 } else {
1250 static unsigned int intcount = 0;
1251 if ((++intcount & 0xfff) == 0xfff)
1252 DPRINTK(KERN_ERR
1253 "%s: %s interrupt %d while in poll\n",
1254 netxen_nic_driver_name, netdev->name,
1255 intcount);
1257 ret = 1;
1260 if (ret == 0) {
1261 netxen_nic_enable_int(adapter);
1264 return ret;
1268 * netxen_intr - Interrupt Handler
1269 * @irq: interrupt number
1270 * data points to adapter stucture (which may be handling more than 1 port
1272 irqreturn_t netxen_intr(int irq, void *data)
1274 struct netxen_adapter *adapter;
1275 struct net_device *netdev;
1276 u32 our_int = 0;
1278 if (unlikely(!irq)) {
1279 return IRQ_NONE; /* Not our interrupt */
1282 adapter = (struct netxen_adapter *)data;
1283 netdev = adapter->netdev;
1285 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1286 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1287 /* not our interrupt */
1288 if ((our_int & (0x80 << adapter->portnum)) == 0)
1289 return IRQ_NONE;
1292 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1293 /* claim interrupt */
1294 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1295 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1296 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1300 if (netif_running(netdev))
1301 netxen_handle_int(adapter, netdev);
1303 return IRQ_HANDLED;
1306 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1308 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1309 struct net_device *netdev = adapter->netdev;
1310 int done = 1;
1311 int ctx;
1312 int work_done;
1314 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1316 work_done = 0;
1317 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1319 * Fairness issue. This will give undue weight to the
1320 * receive context 0.
1324 * To avoid starvation, we give each of our receivers,
1325 * a fraction of the quota. Sometimes, it might happen that we
1326 * have enough quota to process every packet, but since all the
1327 * packets are on one context, it gets only half of the quota,
1328 * and ends up not processing it.
1330 work_done += netxen_process_rcv_ring(adapter, ctx,
1331 budget / MAX_RCV_CTX);
1334 if (work_done >= budget && netxen_nic_rx_has_work(adapter) != 0)
1335 done = 0;
1337 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1338 done = 0;
1340 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1341 work_done, work_to_do);
1342 if (done) {
1343 netif_rx_complete(netdev, napi);
1344 netxen_nic_enable_int(adapter);
1347 return work_done;
1350 #ifdef CONFIG_NET_POLL_CONTROLLER
1351 static void netxen_nic_poll_controller(struct net_device *netdev)
1353 struct netxen_adapter *adapter = netdev_priv(netdev);
1354 disable_irq(adapter->irq);
1355 netxen_intr(adapter->irq, adapter);
1356 enable_irq(adapter->irq);
1358 #endif
1360 static struct pci_driver netxen_driver = {
1361 .name = netxen_nic_driver_name,
1362 .id_table = netxen_pci_tbl,
1363 .probe = netxen_nic_probe,
1364 .remove = __devexit_p(netxen_nic_remove)
1367 /* Driver Registration on NetXen card */
1369 static int __init netxen_init_module(void)
1371 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1372 return -ENOMEM;
1374 return pci_register_driver(&netxen_driver);
1377 module_init(netxen_init_module);
1379 static void __exit netxen_exit_module(void)
1382 * Wait for some time to allow the dma to drain, if any.
1384 msleep(100);
1385 pci_unregister_driver(&netxen_driver);
1386 destroy_workqueue(netxen_workq);
1389 module_exit(netxen_exit_module);