blob: ffd272c93a6be86cac90d6c1f4b9101e5e434648 [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
4 *
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.
9 *
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.
14 *
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.
19 *
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
22 *
23 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
28 *
29 *
30 * Main source file for NetXen NIC Driver on Linux
31 *
32 */
33
34#include "netxen_nic_hw.h"
35
36#include "netxen_nic.h"
37#define DEFINE_GLOBAL_RECV_CRB
38#include "netxen_nic_phan_reg.h"
39#include "netxen_nic_ioctl.h"
40
Jeff Garzik1494a812006-11-07 05:12:16 -050041#include <linux/dma-mapping.h>
42#include <linux/vmalloc.h>
43
Amit S. Kale3d396eb2006-10-21 15:33:03 -040044MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45MODULE_LICENSE("GPL");
46MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
47
48char netxen_nic_driver_name[] = "netxen";
49static char netxen_nic_driver_string[] = "NetXen Network Driver version "
50 NETXEN_NIC_LINUX_VERSIONID "-" NETXEN_NIC_BUILD_NO;
51
52#define NETXEN_NETDEV_WEIGHT 120
53#define NETXEN_ADAPTER_UP_MAGIC 777
54
55/* Local functions to NetXen NIC driver */
56static int __devinit netxen_nic_probe(struct pci_dev *pdev,
57 const struct pci_device_id *ent);
58static void __devexit netxen_nic_remove(struct pci_dev *pdev);
59static int netxen_nic_open(struct net_device *netdev);
60static int netxen_nic_close(struct net_device *netdev);
61static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
62static void netxen_tx_timeout(struct net_device *netdev);
63static void netxen_tx_timeout_task(struct net_device *netdev);
64static void netxen_watchdog(unsigned long);
65static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
66static int netxen_nic_ioctl(struct net_device *netdev,
67 struct ifreq *ifr, int cmd);
68static int netxen_nic_poll(struct net_device *dev, int *budget);
69#ifdef CONFIG_NET_POLL_CONTROLLER
70static void netxen_nic_poll_controller(struct net_device *netdev);
71#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050072static irqreturn_t netxen_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040073
74/* PCI Device ID Table */
75static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
76 {PCI_DEVICE(0x4040, 0x0001)},
77 {PCI_DEVICE(0x4040, 0x0002)},
78 {PCI_DEVICE(0x4040, 0x0003)},
79 {PCI_DEVICE(0x4040, 0x0004)},
80 {PCI_DEVICE(0x4040, 0x0005)},
81 {0,}
82};
83
84MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
85
86/*
87 * netxen_nic_probe()
88 *
89 * The Linux system will invoke this after identifying the vendor ID and
90 * device Id in the pci_tbl supported by this module.
91 *
92 * A quad port card has one operational PCI config space, (function 0),
93 * which is used to access all four ports.
94 *
95 * This routine will initialize the adapter, and setup the global parameters
96 * along with the port's specific structure.
97 */
98static int __devinit
99netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
100{
101 struct net_device *netdev = NULL;
102 struct netxen_adapter *adapter = NULL;
103 struct netxen_port *port = NULL;
104 u8 __iomem *mem_ptr = NULL;
105 unsigned long mem_base, mem_len;
106 int pci_using_dac, i, err;
107 int ring;
108 struct netxen_recv_context *recv_ctx = NULL;
109 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
110 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
111 u64 mac_addr[FLASH_NUM_PORTS + 1];
112 int valid_mac;
113
114 if ((err = pci_enable_device(pdev)))
115 return err;
116 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
117 err = -ENODEV;
118 goto err_out_disable_pdev;
119 }
120
121 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
122 goto err_out_disable_pdev;
123
124 pci_set_master(pdev);
125 if ((pci_set_dma_mask(pdev, DMA_64BIT_MASK) == 0) &&
126 (pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK) == 0))
127 pci_using_dac = 1;
128 else {
129 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
130 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
131 goto err_out_free_res;
132
133 pci_using_dac = 0;
134 }
135
136 /* remap phys address */
137 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
138 mem_len = pci_resource_len(pdev, 0);
139
140 /* 128 Meg of memory */
141 mem_ptr = ioremap(mem_base, NETXEN_PCI_MAPSIZE_BYTES);
142 if (mem_ptr == 0UL) {
143 printk(KERN_ERR "%s: Cannot ioremap adapter memory aborting."
144 ":%p\n", netxen_nic_driver_name, mem_ptr);
145 err = -EIO;
146 goto err_out_free_res;
147 }
148
149/*
150 * Allocate a adapter structure which will manage all the initialization
151 * as well as the common resources for all ports...
152 * all the ports will have pointer to this adapter as well as Adapter
153 * will have pointers of all the ports structures.
154 */
155
156 /* One adapter structure for all 4 ports.... */
157 adapter = kzalloc(sizeof(struct netxen_adapter), GFP_KERNEL);
158 if (adapter == NULL) {
159 printk(KERN_ERR "%s: Could not allocate adapter memory:%d\n",
160 netxen_nic_driver_name,
161 (int)sizeof(struct netxen_adapter));
162 err = -ENOMEM;
163 goto err_out_iounmap;
164 }
165
166 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS;
167 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
168 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
169
170 pci_set_drvdata(pdev, adapter);
171
172 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
173 if (cmd_buf_arr == NULL) {
174 err = -ENOMEM;
175 goto err_out_free_adapter;
176 }
177 memset(cmd_buf_arr, 0, TX_RINGSIZE);
178
179 for (i = 0; i < MAX_RCV_CTX; ++i) {
180 recv_ctx = &adapter->recv_ctx[i];
181 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
182 rcv_desc = &recv_ctx->rcv_desc[ring];
183 switch (RCV_DESC_TYPE(ring)) {
184 case RCV_DESC_NORMAL:
185 rcv_desc->max_rx_desc_count =
186 adapter->max_rx_desc_count;
187 rcv_desc->flags = RCV_DESC_NORMAL;
188 rcv_desc->dma_size = RX_DMA_MAP_LEN;
189 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
190 break;
191
192 case RCV_DESC_JUMBO:
193 rcv_desc->max_rx_desc_count =
194 adapter->max_jumbo_rx_desc_count;
195 rcv_desc->flags = RCV_DESC_JUMBO;
196 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
197 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
198 break;
199
200 }
201 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
202 vmalloc(RCV_BUFFSIZE);
203
204 if (rcv_desc->rx_buf_arr == NULL) {
205 err = -ENOMEM;
206 goto err_out_free_rx_buffer;
207 }
208 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
209 }
210
211 }
212
213 adapter->ops = kzalloc(sizeof(struct netxen_drvops), GFP_KERNEL);
214 if (adapter->ops == NULL) {
215 printk(KERN_ERR
216 "%s: Could not allocate memory for adapter->ops:%d\n",
217 netxen_nic_driver_name,
218 (int)sizeof(struct netxen_adapter));
219 err = -ENOMEM;
220 goto err_out_free_rx_buffer;
221 }
222
223 adapter->cmd_buf_arr = cmd_buf_arr;
224 adapter->ahw.pci_base = mem_ptr;
225 spin_lock_init(&adapter->tx_lock);
226 spin_lock_init(&adapter->lock);
227 /* initialize the buffers in adapter */
228 netxen_initialize_adapter_sw(adapter);
229 /*
230 * Set the CRB window to invalid. If any register in window 0 is
231 * accessed it should set the window to 0 and then reset it to 1.
232 */
233 adapter->curr_window = 255;
234 /*
235 * Adapter in our case is quad port so initialize it before
236 * initializing the ports
237 */
238 netxen_initialize_adapter_hw(adapter); /* initialize the adapter */
239
240 netxen_initialize_adapter_ops(adapter);
241
242 init_timer(&adapter->watchdog_timer);
243 adapter->ahw.xg_linkup = 0;
244 adapter->watchdog_timer.function = &netxen_watchdog;
245 adapter->watchdog_timer.data = (unsigned long)adapter;
246 INIT_WORK(&adapter->watchdog_task,
247 (void (*)(void *))netxen_watchdog_task, adapter);
248 adapter->ahw.pdev = pdev;
249 adapter->proc_cmd_buf_counter = 0;
250 pci_read_config_byte(pdev, PCI_REVISION_ID, &adapter->ahw.revision_id);
251
252 if (pci_enable_msi(pdev)) {
253 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
254 printk(KERN_WARNING "%s: unable to allocate MSI interrupt"
255 " error\n", netxen_nic_driver_name);
256 } else
257 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
258
259 if (netxen_is_flash_supported(adapter) == 0 &&
260 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
261 valid_mac = 1;
262 else
263 valid_mac = 0;
264
265 /* initialize the all the ports */
266
267 for (i = 0; i < adapter->ahw.max_ports; i++) {
268 netdev = alloc_etherdev(sizeof(struct netxen_port));
269 if (!netdev) {
270 printk(KERN_ERR "%s: could not allocate netdev for port"
271 " %d\n", netxen_nic_driver_name, i + 1);
272 goto err_out_free_dev;
273 }
274
275 SET_MODULE_OWNER(netdev);
Amit S. Kaleedf90162006-11-29 08:58:11 -0800276 SET_NETDEV_DEV(netdev, &pdev->dev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400277
278 port = netdev_priv(netdev);
279 port->netdev = netdev;
280 port->pdev = pdev;
281 port->adapter = adapter;
282 port->portnum = i; /* Gigabit port number from 0-3 */
283
284 netdev->open = netxen_nic_open;
285 netdev->stop = netxen_nic_close;
286 netdev->hard_start_xmit = netxen_nic_xmit_frame;
287 netdev->get_stats = netxen_nic_get_stats;
288 netdev->set_multicast_list = netxen_nic_set_multi;
289 netdev->set_mac_address = netxen_nic_set_mac;
290 netdev->change_mtu = netxen_nic_change_mtu;
291 netdev->do_ioctl = netxen_nic_ioctl;
292 netdev->tx_timeout = netxen_tx_timeout;
293 netdev->watchdog_timeo = HZ;
294
295 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
296 netdev->poll = netxen_nic_poll;
297 netdev->weight = NETXEN_NETDEV_WEIGHT;
298#ifdef CONFIG_NET_POLL_CONTROLLER
299 netdev->poll_controller = netxen_nic_poll_controller;
300#endif
301 /* ScatterGather support */
302 netdev->features = NETIF_F_SG;
303 netdev->features |= NETIF_F_IP_CSUM;
304 netdev->features |= NETIF_F_TSO;
305
306 if (pci_using_dac)
307 netdev->features |= NETIF_F_HIGHDMA;
308
309 if (valid_mac) {
310 unsigned char *p = (unsigned char *)&mac_addr[i];
311 netdev->dev_addr[0] = *(p + 5);
312 netdev->dev_addr[1] = *(p + 4);
313 netdev->dev_addr[2] = *(p + 3);
314 netdev->dev_addr[3] = *(p + 2);
315 netdev->dev_addr[4] = *(p + 1);
316 netdev->dev_addr[5] = *(p + 0);
317
318 memcpy(netdev->perm_addr, netdev->dev_addr,
319 netdev->addr_len);
320 if (!is_valid_ether_addr(netdev->perm_addr)) {
321 printk(KERN_ERR "%s: Bad MAC address "
322 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
323 netxen_nic_driver_name,
324 netdev->dev_addr[0],
325 netdev->dev_addr[1],
326 netdev->dev_addr[2],
327 netdev->dev_addr[3],
328 netdev->dev_addr[4],
329 netdev->dev_addr[5]);
330 } else {
331 if (adapter->ops->macaddr_set)
332 adapter->ops->macaddr_set(port,
333 netdev->
334 dev_addr);
335 }
336 }
337 INIT_WORK(&adapter->tx_timeout_task,
338 (void (*)(void *))netxen_tx_timeout_task, netdev);
339 netif_carrier_off(netdev);
340 netif_stop_queue(netdev);
341
342 if ((err = register_netdev(netdev))) {
343 printk(KERN_ERR "%s: register_netdev failed port #%d"
344 " aborting\n", netxen_nic_driver_name, i + 1);
345 err = -EIO;
346 free_netdev(netdev);
347 goto err_out_free_dev;
348 }
349 adapter->port_count++;
350 adapter->active_ports = 0;
351 adapter->port[i] = port;
352 }
353
354 /*
355 * Initialize all the CRB registers here.
356 */
357 /* Window = 1 */
358 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
359 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_CONSUMER_OFFSET));
360 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
361
362 netxen_phantom_init(adapter);
363 /*
364 * delay a while to ensure that the Pegs are up & running.
365 * Otherwise, we might see some flaky behaviour.
366 */
367 udelay(100);
368
369 switch (adapter->ahw.board_type) {
370 case NETXEN_NIC_GBE:
371 printk("%s: QUAD GbE board initialized\n",
372 netxen_nic_driver_name);
373 break;
374
375 case NETXEN_NIC_XGBE:
376 printk("%s: XGbE board initialized\n", netxen_nic_driver_name);
377 break;
378 }
379
380 adapter->driver_mismatch = 0;
381
382 return 0;
383
384 err_out_free_dev:
385 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
386 pci_disable_msi(pdev);
387 for (i = 0; i < adapter->port_count; i++) {
388 port = adapter->port[i];
389 if ((port) && (port->netdev)) {
390 unregister_netdev(port->netdev);
391 free_netdev(port->netdev);
392 }
393 }
394 kfree(adapter->ops);
395
396 err_out_free_rx_buffer:
397 for (i = 0; i < MAX_RCV_CTX; ++i) {
398 recv_ctx = &adapter->recv_ctx[i];
399 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
400 rcv_desc = &recv_ctx->rcv_desc[ring];
401 if (rcv_desc->rx_buf_arr != NULL) {
402 vfree(rcv_desc->rx_buf_arr);
403 rcv_desc->rx_buf_arr = NULL;
404 }
405 }
406 }
407
408 vfree(cmd_buf_arr);
409
410 kfree(adapter->port);
411
412 err_out_free_adapter:
413 pci_set_drvdata(pdev, NULL);
414 kfree(adapter);
415
416 err_out_iounmap:
417 iounmap(mem_ptr);
418 err_out_free_res:
419 pci_release_regions(pdev);
420 err_out_disable_pdev:
421 pci_disable_device(pdev);
422 return err;
423}
424
425static void __devexit netxen_nic_remove(struct pci_dev *pdev)
426{
427 struct netxen_adapter *adapter;
428 struct netxen_port *port;
429 struct netxen_rx_buffer *buffer;
430 struct netxen_recv_context *recv_ctx;
431 struct netxen_rcv_desc_ctx *rcv_desc;
432 int i;
433 int ctxid, ring;
434
435 adapter = pci_get_drvdata(pdev);
436 if (adapter == NULL)
437 return;
438
439 netxen_nic_stop_all_ports(adapter);
440 /* leave the hw in the same state as reboot */
441 netxen_pinit_from_rom(adapter, 0);
442 udelay(500);
443 netxen_load_firmware(adapter);
444
445 if ((adapter->flags & NETXEN_NIC_MSI_ENABLED))
446 netxen_nic_disable_int(adapter);
447
448 udelay(500); /* Delay for a while to drain the DMA engines */
449 for (i = 0; i < adapter->port_count; i++) {
450 port = adapter->port[i];
451 if ((port) && (port->netdev)) {
452 unregister_netdev(port->netdev);
453 free_netdev(port->netdev);
454 }
455 }
456
457 if ((adapter->flags & NETXEN_NIC_MSI_ENABLED))
458 pci_disable_msi(pdev);
459 pci_set_drvdata(pdev, NULL);
460 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
461 netxen_free_hw_resources(adapter);
462
463 iounmap(adapter->ahw.pci_base);
464
465 pci_release_regions(pdev);
466 pci_disable_device(pdev);
467
468 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
469 recv_ctx = &adapter->recv_ctx[ctxid];
470 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
471 rcv_desc = &recv_ctx->rcv_desc[ring];
472 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
473 buffer = &(rcv_desc->rx_buf_arr[i]);
474 if (buffer->state == NETXEN_BUFFER_FREE)
475 continue;
476 pci_unmap_single(pdev, buffer->dma,
477 rcv_desc->dma_size,
478 PCI_DMA_FROMDEVICE);
479 if (buffer->skb != NULL)
480 dev_kfree_skb_any(buffer->skb);
481 }
482 vfree(rcv_desc->rx_buf_arr);
483 }
484 }
485
486 vfree(adapter->cmd_buf_arr);
487 kfree(adapter->ops);
488 kfree(adapter);
489}
490
491/*
492 * Called when a network interface is made active
493 * @returns 0 on success, negative value on failure
494 */
495static int netxen_nic_open(struct net_device *netdev)
496{
497 struct netxen_port *port = netdev_priv(netdev);
498 struct netxen_adapter *adapter = port->adapter;
499 struct netxen_rcv_desc_ctx *rcv_desc;
500 int err = 0;
501 int ctx, ring;
502
503 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
504 err = netxen_init_firmware(adapter);
505 if (err != 0) {
506 printk(KERN_ERR "Failed to init firmware\n");
507 return -EIO;
508 }
509 netxen_nic_flash_print(adapter);
510
511 /* setup all the resources for the Phantom... */
512 /* this include the descriptors for rcv, tx, and status */
513 netxen_nic_clear_stats(adapter);
514 err = netxen_nic_hw_resources(adapter);
515 if (err) {
516 printk(KERN_ERR "Error in setting hw resources:%d\n",
517 err);
518 return err;
519 }
520 if (adapter->ops->init_port
521 && adapter->ops->init_port(adapter, port->portnum) != 0) {
522 printk(KERN_ERR "%s: Failed to initialize port %d\n",
523 netxen_nic_driver_name, port->portnum);
524 netxen_free_hw_resources(adapter);
525 return -EIO;
526 }
527 if (adapter->ops->init_niu)
528 adapter->ops->init_niu(adapter);
529 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
530 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
531 rcv_desc =
532 &adapter->recv_ctx[ctx].rcv_desc[ring];
533 netxen_post_rx_buffers(adapter, ctx, ring);
534 }
535 }
536 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
537 }
538 adapter->active_ports++;
539 if (adapter->active_ports == 1) {
540 err = request_irq(adapter->ahw.pdev->irq, &netxen_intr,
541 SA_SHIRQ | SA_SAMPLE_RANDOM, netdev->name,
542 adapter);
543 if (err) {
544 printk(KERN_ERR "request_irq failed with: %d\n", err);
545 adapter->active_ports--;
546 return err;
547 }
548 adapter->irq = adapter->ahw.pdev->irq;
549 if (!adapter->driver_mismatch)
550 mod_timer(&adapter->watchdog_timer, jiffies);
551
552 netxen_nic_enable_int(adapter);
553 }
554
555 /* Done here again so that even if phantom sw overwrote it,
556 * we set it */
557 if (adapter->ops->macaddr_set)
558 adapter->ops->macaddr_set(port, netdev->dev_addr);
559 netxen_nic_set_link_parameters(port);
560
561 netxen_nic_set_multi(netdev);
562 if (!adapter->driver_mismatch)
563 netif_start_queue(netdev);
564
565 return 0;
566}
567
568/*
569 * netxen_nic_close - Disables a network interface entry point
570 */
571static int netxen_nic_close(struct net_device *netdev)
572{
573 struct netxen_port *port = netdev_priv(netdev);
574 struct netxen_adapter *adapter = port->adapter;
575 int i, j;
576 struct netxen_cmd_buffer *cmd_buff;
577 struct netxen_skb_frag *buffrag;
578
579 netif_carrier_off(netdev);
580 netif_stop_queue(netdev);
581
582 /* disable phy_ints */
583 if (adapter->ops->disable_phy_interrupts)
584 adapter->ops->disable_phy_interrupts(adapter, port->portnum);
585
586 adapter->active_ports--;
587
588 if (!adapter->active_ports) {
589 netxen_nic_disable_int(adapter);
590 if (adapter->irq)
591 free_irq(adapter->irq, adapter);
592 cmd_buff = adapter->cmd_buf_arr;
593 for (i = 0; i < adapter->max_tx_desc_count; i++) {
594 buffrag = cmd_buff->frag_array;
595 if (buffrag->dma) {
596 pci_unmap_single(port->pdev, buffrag->dma,
597 buffrag->length,
598 PCI_DMA_TODEVICE);
599 buffrag->dma = (u64) NULL;
600 }
601 for (j = 0; j < cmd_buff->frag_count; j++) {
602 buffrag++;
603 if (buffrag->dma) {
604 pci_unmap_page(port->pdev,
605 buffrag->dma,
606 buffrag->length,
607 PCI_DMA_TODEVICE);
608 buffrag->dma = (u64) NULL;
609 }
610 }
611 /* Free the skb we received in netxen_nic_xmit_frame */
612 if (cmd_buff->skb) {
613 dev_kfree_skb_any(cmd_buff->skb);
614 cmd_buff->skb = NULL;
615 }
616 cmd_buff++;
617 }
618 del_timer_sync(&adapter->watchdog_timer);
619 }
620
621 return 0;
622}
623
624static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
625{
626 struct netxen_port *port = netdev_priv(netdev);
627 struct netxen_adapter *adapter = port->adapter;
628 struct netxen_hardware_context *hw = &adapter->ahw;
629 unsigned int first_seg_len = skb->len - skb->data_len;
630 struct netxen_skb_frag *buffrag;
631 unsigned int i;
632
633 u32 producer = 0;
634 u32 saved_producer = 0;
635 struct cmd_desc_type0 *hwdesc;
636 int k;
637 struct netxen_cmd_buffer *pbuf = NULL;
638 unsigned int tries = 0;
639 static int dropped_packet = 0;
640 int frag_count;
641 u32 local_producer = 0;
642 u32 max_tx_desc_count = 0;
643 u32 last_cmd_consumer = 0;
644 int no_of_desc;
645
646 port->stats.xmitcalled++;
647 frag_count = skb_shinfo(skb)->nr_frags + 1;
648
649 if (unlikely(skb->len <= 0)) {
650 dev_kfree_skb_any(skb);
651 port->stats.badskblen++;
652 return NETDEV_TX_OK;
653 }
654
655 if (frag_count > MAX_BUFFERS_PER_CMD) {
656 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
657 "too large, can handle only %d frags\n",
658 netxen_nic_driver_name, netdev->name,
659 frag_count, MAX_BUFFERS_PER_CMD);
660 port->stats.txdropped++;
661 if ((++dropped_packet & 0xff) == 0xff)
662 printk("%s: %s droppped packets = %d\n",
663 netxen_nic_driver_name, netdev->name,
664 dropped_packet);
665
666 return NETDEV_TX_OK;
667 }
668
669 /*
670 * Everything is set up. Now, we just need to transmit it out.
671 * Note that we have to copy the contents of buffer over to
672 * right place. Later on, this can be optimized out by de-coupling the
673 * producer index from the buffer index.
674 */
675 retry_getting_window:
676 spin_lock_bh(&adapter->tx_lock);
677 if (adapter->total_threads == MAX_XMIT_PRODUCERS) {
678 spin_unlock_bh(&adapter->tx_lock);
679 /*
680 * Yield CPU
681 */
682 if (!in_atomic())
683 schedule();
684 else {
685 for (i = 0; i < 20; i++)
686 cpu_relax(); /*This a nop instr on i386 */
687 }
688 goto retry_getting_window;
689 }
690 local_producer = adapter->cmd_producer;
691 /* There 4 fragments per descriptor */
692 no_of_desc = (frag_count + 3) >> 2;
693 if (skb_shinfo(skb)->gso_size > 0) {
694 no_of_desc++;
695 if (((skb->nh.iph)->ihl * sizeof(u32)) +
696 ((skb->h.th)->doff * sizeof(u32)) +
697 sizeof(struct ethhdr) >
698 (sizeof(struct cmd_desc_type0) - NET_IP_ALIGN)) {
699 no_of_desc++;
700 }
701 }
702 k = adapter->cmd_producer;
703 max_tx_desc_count = adapter->max_tx_desc_count;
704 last_cmd_consumer = adapter->last_cmd_consumer;
705 if ((k + no_of_desc) >=
706 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
707 last_cmd_consumer)) {
708 spin_unlock_bh(&adapter->tx_lock);
709 if (tries == 0) {
710 local_bh_disable();
711 netxen_process_cmd_ring((unsigned long)adapter);
712 local_bh_enable();
713 ++tries;
714 goto retry_getting_window;
715 } else {
716 port->stats.nocmddescriptor++;
717 DPRINTK(ERR, "No command descriptors available,"
718 " producer = %d, consumer = %d count=%llu,"
719 " dropping packet\n", producer,
720 adapter->last_cmd_consumer,
721 port->stats.nocmddescriptor);
722
723 spin_lock_bh(&adapter->tx_lock);
724 netif_stop_queue(netdev);
725 port->flags |= NETXEN_NETDEV_STATUS;
726 spin_unlock_bh(&adapter->tx_lock);
727 return NETDEV_TX_BUSY;
728 }
729 }
730 k = get_index_range(k, max_tx_desc_count, no_of_desc);
731 adapter->cmd_producer = k;
732 adapter->total_threads++;
733 adapter->num_threads++;
734
735 spin_unlock_bh(&adapter->tx_lock);
736 /* Copy the descriptors into the hardware */
737 producer = local_producer;
738 saved_producer = producer;
739 hwdesc = &hw->cmd_desc_head[producer];
740 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
741 /* Take skb->data itself */
742 pbuf = &adapter->cmd_buf_arr[producer];
743 if (skb_shinfo(skb)->gso_size > 0) {
744 pbuf->mss = skb_shinfo(skb)->gso_size;
745 hwdesc->mss = skb_shinfo(skb)->gso_size;
746 } else {
747 pbuf->mss = 0;
748 hwdesc->mss = 0;
749 }
750 pbuf->no_of_descriptors = no_of_desc;
751 pbuf->total_length = skb->len;
752 pbuf->skb = skb;
753 pbuf->cmd = TX_ETHER_PKT;
754 pbuf->frag_count = frag_count;
755 pbuf->port = port->portnum;
756 buffrag = &pbuf->frag_array[0];
757 buffrag->dma = pci_map_single(port->pdev, skb->data, first_seg_len,
758 PCI_DMA_TODEVICE);
759 buffrag->length = first_seg_len;
760 CMD_DESC_TOTAL_LENGTH_WRT(hwdesc, skb->len);
761 hwdesc->num_of_buffers = frag_count;
762 hwdesc->opcode = TX_ETHER_PKT;
763
764 CMD_DESC_PORT_WRT(hwdesc, port->portnum);
765 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
766 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
767
768 for (i = 1, k = 1; i < frag_count; i++, k++) {
769 struct skb_frag_struct *frag;
770 int len, temp_len;
771 unsigned long offset;
772 dma_addr_t temp_dma;
773
774 /* move to next desc. if there is a need */
775 if ((i & 0x3) == 0) {
776 k = 0;
777 producer = get_next_index(producer,
778 adapter->max_tx_desc_count);
779 hwdesc = &hw->cmd_desc_head[producer];
780 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
781 }
782 frag = &skb_shinfo(skb)->frags[i - 1];
783 len = frag->size;
784 offset = frag->page_offset;
785
786 temp_len = len;
787 temp_dma = pci_map_page(port->pdev, frag->page, offset,
788 len, PCI_DMA_TODEVICE);
789
790 buffrag++;
791 buffrag->dma = temp_dma;
792 buffrag->length = temp_len;
793
794 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
795 switch (k) {
796 case 0:
797 hwdesc->buffer1_length = cpu_to_le16(temp_len);
798 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
799 break;
800 case 1:
801 hwdesc->buffer2_length = cpu_to_le16(temp_len);
802 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
803 break;
804 case 2:
805 hwdesc->buffer3_length = cpu_to_le16(temp_len);
806 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
807 break;
808 case 3:
809 hwdesc->buffer4_length = temp_len;
810 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
811 break;
812 }
813 frag++;
814 }
815 producer = get_next_index(producer, adapter->max_tx_desc_count);
816
817 /* might change opcode to TX_TCP_LSO */
818 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
819
820 /* For LSO, we need to copy the MAC/IP/TCP headers into
821 * the descriptor ring
822 */
823 if (hw->cmd_desc_head[saved_producer].opcode == TX_TCP_LSO) {
824 int hdr_len, first_hdr_len, more_hdr;
825 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
826 if (hdr_len > (sizeof(struct cmd_desc_type0) - NET_IP_ALIGN)) {
827 first_hdr_len =
828 sizeof(struct cmd_desc_type0) - NET_IP_ALIGN;
829 more_hdr = 1;
830 } else {
831 first_hdr_len = hdr_len;
832 more_hdr = 0;
833 }
834 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
835 hwdesc = &hw->cmd_desc_head[producer];
836
837 /* copy the first 64 bytes */
838 memcpy(((void *)hwdesc) + NET_IP_ALIGN,
839 (void *)(skb->data), first_hdr_len);
840 producer = get_next_index(producer, max_tx_desc_count);
841
842 if (more_hdr) {
843 hwdesc = &hw->cmd_desc_head[producer];
844 /* copy the next 64 bytes - should be enough except
845 * for pathological case
846 */
847 memcpy((void *)hwdesc, (void *)(skb->data) +
848 first_hdr_len, hdr_len - first_hdr_len);
849 producer = get_next_index(producer, max_tx_desc_count);
850 }
851 }
852 spin_lock_bh(&adapter->tx_lock);
853 port->stats.txbytes +=
854 CMD_DESC_TOTAL_LENGTH(&hw->cmd_desc_head[saved_producer]);
855 /* Code to update the adapter considering how many producer threads
856 are currently working */
857 if ((--adapter->num_threads) == 0) {
858 /* This is the last thread */
859 u32 crb_producer = adapter->cmd_producer;
860 writel(crb_producer,
861 NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
862 wmb();
863 adapter->total_threads = 0;
864 } else {
865 u32 crb_producer = 0;
866 crb_producer =
867 readl(NETXEN_CRB_NORMALIZE
868 (adapter, CRB_CMD_PRODUCER_OFFSET));
869 if (crb_producer == local_producer) {
870 crb_producer = get_index_range(crb_producer,
871 max_tx_desc_count,
872 no_of_desc);
873 writel(crb_producer,
874 NETXEN_CRB_NORMALIZE(adapter,
875 CRB_CMD_PRODUCER_OFFSET));
876 wmb();
877 }
878 }
879
880 port->stats.xmitfinished++;
881 spin_unlock_bh(&adapter->tx_lock);
882
883 netdev->trans_start = jiffies;
884
885 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
886
887 DPRINTK(INFO, "Done. Send\n");
888 return NETDEV_TX_OK;
889}
890
891static void netxen_watchdog(unsigned long v)
892{
893 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
894 schedule_work(&adapter->watchdog_task);
895}
896
897static void netxen_tx_timeout(struct net_device *netdev)
898{
899 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
900 struct netxen_adapter *adapter = port->adapter;
901
902 schedule_work(&adapter->tx_timeout_task);
903}
904
905static void netxen_tx_timeout_task(struct net_device *netdev)
906{
907 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
908 unsigned long flags;
909
910 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
911 netxen_nic_driver_name, netdev->name);
912
913 spin_lock_irqsave(&port->adapter->lock, flags);
914 netxen_nic_close(netdev);
915 netxen_nic_open(netdev);
916 spin_unlock_irqrestore(&port->adapter->lock, flags);
917 netdev->trans_start = jiffies;
918 netif_wake_queue(netdev);
919}
920
921static int
922netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
923{
924 u32 ret = 0;
925
926 DPRINTK(INFO, "Entered handle ISR\n");
927
928 adapter->stats.ints++;
929
930 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
931 int count = 0;
932 u32 mask;
933 netxen_nic_disable_int(adapter);
934 /* Window = 0 or 1 */
935 do {
936 writel(0xffffffff, (void __iomem *)
937 (adapter->ahw.pci_base + ISR_INT_TARGET_STATUS));
938 mask = readl((void __iomem *)
939 (adapter->ahw.pci_base + ISR_INT_VECTOR));
940 } while (((mask & 0x80) != 0) && (++count < 32));
941 if ((mask & 0x80) != 0)
942 printk("Could not disable interrupt completely\n");
943
944 }
945 adapter->stats.hostints++;
946
947 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
948 if (netif_rx_schedule_prep(netdev)) {
949 /*
950 * Interrupts are already disabled.
951 */
952 __netif_rx_schedule(netdev);
953 } else {
954 static unsigned int intcount = 0;
955 if ((++intcount & 0xfff) == 0xfff)
956 printk(KERN_ERR
957 "%s: %s interrupt %d while in poll\n",
958 netxen_nic_driver_name, netdev->name,
959 intcount);
960 }
961 ret = 1;
962 }
963
964 if (ret == 0) {
965 netxen_nic_enable_int(adapter);
966 }
967
968 return ret;
969}
970
971/*
972 * netxen_intr - Interrupt Handler
973 * @irq: interrupt number
974 * data points to adapter stucture (which may be handling more than 1 port
975 */
Jeff Garzik1494a812006-11-07 05:12:16 -0500976irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400977{
978 struct netxen_adapter *adapter;
979 struct netxen_port *port;
980 struct net_device *netdev;
981 int i;
982
983 if (unlikely(!irq)) {
984 return IRQ_NONE; /* Not our interrupt */
985 }
986
987 adapter = (struct netxen_adapter *)data;
988 for (i = 0; i < adapter->ahw.max_ports; i++) {
989 port = adapter->port[i];
990 netdev = port->netdev;
991
992 /* process our status queue (for all 4 ports) */
993 netxen_handle_int(adapter, netdev);
994 }
995
996 return IRQ_HANDLED;
997}
998
999static int netxen_nic_poll(struct net_device *netdev, int *budget)
1000{
1001 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
1002 struct netxen_adapter *adapter = port->adapter;
1003 int work_to_do = min(*budget, netdev->quota);
1004 int done = 1;
1005 int ctx;
1006 int this_work_done;
1007
1008 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1009 port->stats.polled++;
1010
1011 adapter->work_done = 0;
1012 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1013 /*
1014 * Fairness issue. This will give undue weight to the
1015 * receive context 0.
1016 */
1017
1018 /*
1019 * To avoid starvation, we give each of our receivers,
1020 * a fraction of the quota. Sometimes, it might happen that we
1021 * have enough quota to process every packet, but since all the
1022 * packets are on one context, it gets only half of the quota,
1023 * and ends up not processing it.
1024 */
1025 this_work_done = netxen_process_rcv_ring(adapter, ctx,
1026 work_to_do /
1027 MAX_RCV_CTX);
1028 adapter->work_done += this_work_done;
1029 }
1030
1031 netdev->quota -= adapter->work_done;
1032 *budget -= adapter->work_done;
1033
1034 if (adapter->work_done >= work_to_do
1035 && netxen_nic_rx_has_work(adapter) != 0)
1036 done = 0;
1037
1038 netxen_process_cmd_ring((unsigned long)adapter);
1039
1040 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
1041 adapter->work_done, work_to_do);
1042 if (done) {
1043 netif_rx_complete(netdev);
1044 netxen_nic_enable_int(adapter);
1045 }
1046
Amit S. Kaleedf90162006-11-29 08:58:11 -08001047 return !done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001048}
1049
1050#ifdef CONFIG_NET_POLL_CONTROLLER
1051static void netxen_nic_poll_controller(struct net_device *netdev)
1052{
1053 struct netxen_port *port = netdev_priv(netdev);
1054 struct netxen_adapter *adapter = port->adapter;
1055 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001056 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001057 enable_irq(adapter->irq);
1058}
1059#endif
1060/*
1061 * netxen_nic_ioctl () We provide the tcl/phanmon support through these
1062 * ioctls.
1063 */
1064static int
1065netxen_nic_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1066{
1067 int err = 0;
1068 struct netxen_port *port = netdev_priv(netdev);
1069 struct netxen_adapter *adapter = port->adapter;
1070
1071 DPRINTK(INFO, "doing ioctl for %s\n", netdev->name);
1072 switch (cmd) {
1073 case NETXEN_NIC_CMD:
1074 err = netxen_nic_do_ioctl(adapter, (void *)ifr->ifr_data, port);
1075 break;
1076
1077 case NETXEN_NIC_NAME:
1078 DPRINTK(INFO, "ioctl cmd for NetXen\n");
1079 if (ifr->ifr_data) {
1080 put_user(port->portnum, (u16 __user *) ifr->ifr_data);
1081 }
1082 break;
1083
1084 default:
1085 DPRINTK(INFO, "ioctl cmd %x not supported\n", cmd);
1086 err = -EOPNOTSUPP;
1087 break;
1088 }
1089
1090 return err;
1091}
1092
1093static struct pci_driver netxen_driver = {
1094 .name = netxen_nic_driver_name,
1095 .id_table = netxen_pci_tbl,
1096 .probe = netxen_nic_probe,
1097 .remove = __devexit_p(netxen_nic_remove)
1098};
1099
1100/* Driver Registration on NetXen card */
1101
1102static int __init netxen_init_module(void)
1103{
1104 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
1105
1106 return pci_module_init(&netxen_driver);
1107}
1108
1109module_init(netxen_init_module);
1110
1111static void __exit netxen_exit_module(void)
1112{
1113 /*
1114 * Wait for some time to allow the dma to drain, if any.
1115 */
1116 mdelay(5);
1117 pci_unregister_driver(&netxen_driver);
1118}
1119
1120module_exit(netxen_exit_module);