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