blob: 575b71b672028e4896cc347efe76cbb2a3ef7103 [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
Amit S. Kale80922fb2006-12-04 09:18:00 -08004 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -04005 * 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.
Amit S. Kale80922fb2006-12-04 09:18:00 -08009 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040010 * 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.
Amit S. Kale80922fb2006-12-04 09:18:00 -080014 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040015 * 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.
Amit S. Kale80922fb2006-12-04 09:18:00 -080019 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040020 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
Amit S. Kale80922fb2006-12-04 09:18:00 -080022 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040023 * 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. Kaleed25ffa2006-12-04 09:23:25 -080035#include <linux/highmem.h>
Amit S. Kale3d396eb2006-10-21 15:33:03 -040036#include "netxen_nic_hw.h"
37
38#include "netxen_nic.h"
39#define DEFINE_GLOBAL_RECV_CRB
40#include "netxen_nic_phan_reg.h"
41#include "netxen_nic_ioctl.h"
42
Jeff Garzik1494a812006-11-07 05:12:16 -050043#include <linux/dma-mapping.h>
44#include <linux/vmalloc.h>
45
Amit S. Kalecb8011a2006-11-29 09:00:10 -080046#define PHAN_VENDOR_ID 0x4040
47
Amit S. Kale3d396eb2006-10-21 15:33:03 -040048MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
49MODULE_LICENSE("GPL");
50MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
51
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080052char netxen_nic_driver_name[] = "netxen-nic";
Amit S. Kale3d396eb2006-10-21 15:33:03 -040053static char netxen_nic_driver_string[] = "NetXen Network Driver version "
Amit S. Kalecb8011a2006-11-29 09:00:10 -080054 NETXEN_NIC_LINUX_VERSIONID;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040055
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080056struct netxen_adapter *g_adapter = NULL;
57
Amit S. Kale3d396eb2006-10-21 15:33:03 -040058#define NETXEN_NETDEV_WEIGHT 120
59#define NETXEN_ADAPTER_UP_MAGIC 777
Amit S. Kalecb8011a2006-11-29 09:00:10 -080060#define NETXEN_NIC_PEG_TUNE 0
Amit S. Kale3d396eb2006-10-21 15:33:03 -040061
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080062u8 nx_p2_id = NX_P2_C0;
63
64#define DMA_32BIT_MASK 0x00000000ffffffffULL
65#define DMA_35BIT_MASK 0x00000007ffffffffULL
66
Amit S. Kale3d396eb2006-10-21 15:33:03 -040067/* Local functions to NetXen NIC driver */
68static int __devinit netxen_nic_probe(struct pci_dev *pdev,
69 const struct pci_device_id *ent);
70static void __devexit netxen_nic_remove(struct pci_dev *pdev);
71static int netxen_nic_open(struct net_device *netdev);
72static int netxen_nic_close(struct net_device *netdev);
73static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
74static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000075static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040076static void netxen_watchdog(unsigned long);
77static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
78static int netxen_nic_ioctl(struct net_device *netdev,
79 struct ifreq *ifr, int cmd);
80static int netxen_nic_poll(struct net_device *dev, int *budget);
81#ifdef CONFIG_NET_POLL_CONTROLLER
82static void netxen_nic_poll_controller(struct net_device *netdev);
83#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050084static irqreturn_t netxen_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040085
86/* PCI Device ID Table */
87static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
88 {PCI_DEVICE(0x4040, 0x0001)},
89 {PCI_DEVICE(0x4040, 0x0002)},
90 {PCI_DEVICE(0x4040, 0x0003)},
91 {PCI_DEVICE(0x4040, 0x0004)},
92 {PCI_DEVICE(0x4040, 0x0005)},
93 {0,}
94};
95
96MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
97
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080098struct workqueue_struct *netxen_workq;
99static void netxen_watchdog(unsigned long);
100
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400101/*
102 * netxen_nic_probe()
103 *
104 * The Linux system will invoke this after identifying the vendor ID and
105 * device Id in the pci_tbl supported by this module.
106 *
107 * A quad port card has one operational PCI config space, (function 0),
108 * which is used to access all four ports.
109 *
110 * This routine will initialize the adapter, and setup the global parameters
111 * along with the port's specific structure.
112 */
113static int __devinit
114netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
115{
116 struct net_device *netdev = NULL;
117 struct netxen_adapter *adapter = NULL;
118 struct netxen_port *port = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800119 void __iomem *mem_ptr0 = NULL;
120 void __iomem *mem_ptr1 = NULL;
121 void __iomem *mem_ptr2 = NULL;
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800122
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800123 u8 *db_ptr = NULL;
124 unsigned long mem_base, mem_len, db_base, db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400125 int pci_using_dac, i, err;
126 int ring;
127 struct netxen_recv_context *recv_ctx = NULL;
128 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
129 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
130 u64 mac_addr[FLASH_NUM_PORTS + 1];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800131 int valid_mac = 0;
132 static int netxen_cards_found = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400133
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800134 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800135 /* In current scheme, we use only PCI function 0 */
136 if (PCI_FUNC(pdev->devfn) != 0) {
137 DPRINTK(ERR, "NetXen function %d will not be enabled.\n",
138 PCI_FUNC(pdev->devfn));
139 return -ENODEV;
140 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400141 if ((err = pci_enable_device(pdev)))
142 return err;
143 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
144 err = -ENODEV;
145 goto err_out_disable_pdev;
146 }
147
148 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
149 goto err_out_disable_pdev;
150
151 pci_set_master(pdev);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800152 pci_read_config_byte(pdev, PCI_REVISION_ID, &nx_p2_id);
153 if (nx_p2_id == NX_P2_C1 &&
154 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
155 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400156 pci_using_dac = 1;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800157 } else {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400158 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
159 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
160 goto err_out_free_res;
161
162 pci_using_dac = 0;
163 }
164
165 /* remap phys address */
166 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
167 mem_len = pci_resource_len(pdev, 0);
168
169 /* 128 Meg of memory */
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800170 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
171 mem_ptr1 =
172 ioremap(mem_base + SECOND_PAGE_GROUP_START, SECOND_PAGE_GROUP_SIZE);
173 mem_ptr2 =
174 ioremap(mem_base + THIRD_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
175
176 if ((mem_ptr0 == 0UL) || (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800177 DPRINTK(ERR,
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800178 "Cannot remap adapter memory aborting.:"
179 "0 -> %p, 1 -> %p, 2 -> %p\n",
180 mem_ptr0, mem_ptr1, mem_ptr2);
181
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400182 err = -EIO;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800183 goto err_out_iounmap;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400184 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800185 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
186 db_len = pci_resource_len(pdev, 4);
187
188 if (db_len == 0) {
189 printk(KERN_ERR "%s: doorbell is disabled\n",
190 netxen_nic_driver_name);
191 err = -EIO;
192 goto err_out_iounmap;
193 }
194 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
195 db_len);
196
197 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
198 if (db_ptr == 0UL) {
199 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
200 netxen_nic_driver_name);
201 err = -EIO;
202 goto err_out_iounmap;
203 }
204 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400205
206/*
207 * Allocate a adapter structure which will manage all the initialization
208 * as well as the common resources for all ports...
209 * all the ports will have pointer to this adapter as well as Adapter
210 * will have pointers of all the ports structures.
211 */
212
213 /* One adapter structure for all 4 ports.... */
214 adapter = kzalloc(sizeof(struct netxen_adapter), GFP_KERNEL);
215 if (adapter == NULL) {
216 printk(KERN_ERR "%s: Could not allocate adapter memory:%d\n",
217 netxen_nic_driver_name,
218 (int)sizeof(struct netxen_adapter));
219 err = -ENOMEM;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800220 goto err_out_dbunmap;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400221 }
222
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800223 if (netxen_cards_found == 0) {
224 g_adapter = adapter;
225 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400226 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS;
227 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
228 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800229 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400230
231 pci_set_drvdata(pdev, adapter);
232
233 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
234 if (cmd_buf_arr == NULL) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800235 printk(KERN_ERR
236 "%s: Could not allocate cmd_buf_arr memory:%d\n",
237 netxen_nic_driver_name, (int)TX_RINGSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400238 err = -ENOMEM;
239 goto err_out_free_adapter;
240 }
241 memset(cmd_buf_arr, 0, TX_RINGSIZE);
242
243 for (i = 0; i < MAX_RCV_CTX; ++i) {
244 recv_ctx = &adapter->recv_ctx[i];
245 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
246 rcv_desc = &recv_ctx->rcv_desc[ring];
247 switch (RCV_DESC_TYPE(ring)) {
248 case RCV_DESC_NORMAL:
249 rcv_desc->max_rx_desc_count =
250 adapter->max_rx_desc_count;
251 rcv_desc->flags = RCV_DESC_NORMAL;
252 rcv_desc->dma_size = RX_DMA_MAP_LEN;
253 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
254 break;
255
256 case RCV_DESC_JUMBO:
257 rcv_desc->max_rx_desc_count =
258 adapter->max_jumbo_rx_desc_count;
259 rcv_desc->flags = RCV_DESC_JUMBO;
260 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
261 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
262 break;
263
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800264 case RCV_RING_LRO:
265 rcv_desc->max_rx_desc_count =
266 adapter->max_lro_rx_desc_count;
267 rcv_desc->flags = RCV_DESC_LRO;
268 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
269 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
270 break;
271
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400272 }
273 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
274 vmalloc(RCV_BUFFSIZE);
275
276 if (rcv_desc->rx_buf_arr == NULL) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800277 printk(KERN_ERR "%s: Could not allocate"
278 "rcv_desc->rx_buf_arr memory:%d\n",
279 netxen_nic_driver_name,
280 (int)RCV_BUFFSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400281 err = -ENOMEM;
282 goto err_out_free_rx_buffer;
283 }
284 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
285 }
286
287 }
288
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400289 adapter->cmd_buf_arr = cmd_buf_arr;
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800290 adapter->ahw.pci_base0 = mem_ptr0;
291 adapter->ahw.pci_base1 = mem_ptr1;
292 adapter->ahw.pci_base2 = mem_ptr2;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800293 adapter->ahw.db_base = db_ptr;
294 adapter->ahw.db_len = db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400295 spin_lock_init(&adapter->tx_lock);
296 spin_lock_init(&adapter->lock);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800297 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800298#ifdef CONFIG_IA64
299 netxen_pinit_from_rom(adapter, 0);
300 udelay(500);
301 netxen_load_firmware(adapter);
302#endif
303
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400304 /*
305 * Set the CRB window to invalid. If any register in window 0 is
306 * accessed it should set the window to 0 and then reset it to 1.
307 */
308 adapter->curr_window = 255;
309 /*
310 * Adapter in our case is quad port so initialize it before
311 * initializing the ports
312 */
313 netxen_initialize_adapter_hw(adapter); /* initialize the adapter */
314
315 netxen_initialize_adapter_ops(adapter);
316
317 init_timer(&adapter->watchdog_timer);
318 adapter->ahw.xg_linkup = 0;
319 adapter->watchdog_timer.function = &netxen_watchdog;
320 adapter->watchdog_timer.data = (unsigned long)adapter;
David Howells6d5aefb2006-12-05 19:36:26 +0000321 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400322 adapter->ahw.pdev = pdev;
323 adapter->proc_cmd_buf_counter = 0;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800324 adapter->ahw.revision_id = nx_p2_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400325
326 if (pci_enable_msi(pdev)) {
327 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
328 printk(KERN_WARNING "%s: unable to allocate MSI interrupt"
329 " error\n", netxen_nic_driver_name);
330 } else
331 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
332
333 if (netxen_is_flash_supported(adapter) == 0 &&
334 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
335 valid_mac = 1;
336 else
337 valid_mac = 0;
338
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800339 /*
340 * Initialize all the CRB registers here.
341 */
342 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
343 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_CONSUMER_OFFSET));
344 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
345
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800346 /* do this before waking up pegs so that we have valid dummy dma addr */
347 err = netxen_initialize_adapter_offload(adapter);
348 if (err) {
349 goto err_out_free_dev;
350 }
351
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800352 /* Unlock the HW, prompting the boot sequence */
353 writel(1,
354 NETXEN_CRB_NORMALIZE(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
355
356 /* Handshake with the card before we register the devices. */
357 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
358
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400359 /* initialize the all the ports */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800360 adapter->active_ports = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400361
362 for (i = 0; i < adapter->ahw.max_ports; i++) {
363 netdev = alloc_etherdev(sizeof(struct netxen_port));
364 if (!netdev) {
365 printk(KERN_ERR "%s: could not allocate netdev for port"
366 " %d\n", netxen_nic_driver_name, i + 1);
367 goto err_out_free_dev;
368 }
369
370 SET_MODULE_OWNER(netdev);
Amit S. Kaleedf90162006-11-29 08:58:11 -0800371 SET_NETDEV_DEV(netdev, &pdev->dev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400372
373 port = netdev_priv(netdev);
374 port->netdev = netdev;
375 port->pdev = pdev;
376 port->adapter = adapter;
377 port->portnum = i; /* Gigabit port number from 0-3 */
378
379 netdev->open = netxen_nic_open;
380 netdev->stop = netxen_nic_close;
381 netdev->hard_start_xmit = netxen_nic_xmit_frame;
382 netdev->get_stats = netxen_nic_get_stats;
383 netdev->set_multicast_list = netxen_nic_set_multi;
384 netdev->set_mac_address = netxen_nic_set_mac;
385 netdev->change_mtu = netxen_nic_change_mtu;
386 netdev->do_ioctl = netxen_nic_ioctl;
387 netdev->tx_timeout = netxen_tx_timeout;
388 netdev->watchdog_timeo = HZ;
389
390 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
391 netdev->poll = netxen_nic_poll;
392 netdev->weight = NETXEN_NETDEV_WEIGHT;
393#ifdef CONFIG_NET_POLL_CONTROLLER
394 netdev->poll_controller = netxen_nic_poll_controller;
395#endif
396 /* ScatterGather support */
397 netdev->features = NETIF_F_SG;
398 netdev->features |= NETIF_F_IP_CSUM;
399 netdev->features |= NETIF_F_TSO;
400
401 if (pci_using_dac)
402 netdev->features |= NETIF_F_HIGHDMA;
403
404 if (valid_mac) {
405 unsigned char *p = (unsigned char *)&mac_addr[i];
406 netdev->dev_addr[0] = *(p + 5);
407 netdev->dev_addr[1] = *(p + 4);
408 netdev->dev_addr[2] = *(p + 3);
409 netdev->dev_addr[3] = *(p + 2);
410 netdev->dev_addr[4] = *(p + 1);
411 netdev->dev_addr[5] = *(p + 0);
412
413 memcpy(netdev->perm_addr, netdev->dev_addr,
414 netdev->addr_len);
415 if (!is_valid_ether_addr(netdev->perm_addr)) {
416 printk(KERN_ERR "%s: Bad MAC address "
417 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
418 netxen_nic_driver_name,
419 netdev->dev_addr[0],
420 netdev->dev_addr[1],
421 netdev->dev_addr[2],
422 netdev->dev_addr[3],
423 netdev->dev_addr[4],
424 netdev->dev_addr[5]);
425 } else {
Amit S. Kale80922fb2006-12-04 09:18:00 -0800426 if (adapter->macaddr_set)
427 adapter->macaddr_set(port,
428 netdev->dev_addr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400429 }
430 }
David Howells6d5aefb2006-12-05 19:36:26 +0000431 adapter->netdev = netdev;
432 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400433 netif_carrier_off(netdev);
434 netif_stop_queue(netdev);
435
436 if ((err = register_netdev(netdev))) {
437 printk(KERN_ERR "%s: register_netdev failed port #%d"
438 " aborting\n", netxen_nic_driver_name, i + 1);
439 err = -EIO;
440 free_netdev(netdev);
441 goto err_out_free_dev;
442 }
443 adapter->port_count++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400444 adapter->port[i] = port;
445 }
446
447 /*
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400448 * delay a while to ensure that the Pegs are up & running.
449 * Otherwise, we might see some flaky behaviour.
450 */
451 udelay(100);
452
453 switch (adapter->ahw.board_type) {
454 case NETXEN_NIC_GBE:
455 printk("%s: QUAD GbE board initialized\n",
456 netxen_nic_driver_name);
457 break;
458
459 case NETXEN_NIC_XGBE:
460 printk("%s: XGbE board initialized\n", netxen_nic_driver_name);
461 break;
462 }
463
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800464 adapter->number = netxen_cards_found;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400465 adapter->driver_mismatch = 0;
466
467 return 0;
468
469 err_out_free_dev:
470 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
471 pci_disable_msi(pdev);
472 for (i = 0; i < adapter->port_count; i++) {
473 port = adapter->port[i];
474 if ((port) && (port->netdev)) {
475 unregister_netdev(port->netdev);
476 free_netdev(port->netdev);
477 }
478 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400479
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800480 netxen_free_adapter_offload(adapter);
481
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400482 err_out_free_rx_buffer:
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 if (rcv_desc->rx_buf_arr != NULL) {
488 vfree(rcv_desc->rx_buf_arr);
489 rcv_desc->rx_buf_arr = NULL;
490 }
491 }
492 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400493 vfree(cmd_buf_arr);
494
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400495 err_out_free_adapter:
496 pci_set_drvdata(pdev, NULL);
497 kfree(adapter);
498
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800499 err_out_dbunmap:
500 if (db_ptr)
501 iounmap(db_ptr);
502
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400503 err_out_iounmap:
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800504 if (mem_ptr0)
505 iounmap(mem_ptr0);
506 if (mem_ptr1)
507 iounmap(mem_ptr1);
508 if (mem_ptr2)
509 iounmap(mem_ptr2);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800510
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400511 err_out_free_res:
512 pci_release_regions(pdev);
513 err_out_disable_pdev:
514 pci_disable_device(pdev);
515 return err;
516}
517
518static void __devexit netxen_nic_remove(struct pci_dev *pdev)
519{
520 struct netxen_adapter *adapter;
521 struct netxen_port *port;
522 struct netxen_rx_buffer *buffer;
523 struct netxen_recv_context *recv_ctx;
524 struct netxen_rcv_desc_ctx *rcv_desc;
525 int i;
526 int ctxid, ring;
527
528 adapter = pci_get_drvdata(pdev);
529 if (adapter == NULL)
530 return;
531
532 netxen_nic_stop_all_ports(adapter);
533 /* leave the hw in the same state as reboot */
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400534 netxen_load_firmware(adapter);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800535 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400536
537 udelay(500); /* Delay for a while to drain the DMA engines */
538 for (i = 0; i < adapter->port_count; i++) {
539 port = adapter->port[i];
540 if ((port) && (port->netdev)) {
541 unregister_netdev(port->netdev);
542 free_netdev(port->netdev);
543 }
544 }
545
546 if ((adapter->flags & NETXEN_NIC_MSI_ENABLED))
547 pci_disable_msi(pdev);
548 pci_set_drvdata(pdev, NULL);
549 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
550 netxen_free_hw_resources(adapter);
551
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800552 iounmap(adapter->ahw.db_base);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800553 iounmap(adapter->ahw.pci_base0);
554 iounmap(adapter->ahw.pci_base1);
555 iounmap(adapter->ahw.pci_base2);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400556
557 pci_release_regions(pdev);
558 pci_disable_device(pdev);
559
560 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
561 recv_ctx = &adapter->recv_ctx[ctxid];
562 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
563 rcv_desc = &recv_ctx->rcv_desc[ring];
564 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
565 buffer = &(rcv_desc->rx_buf_arr[i]);
566 if (buffer->state == NETXEN_BUFFER_FREE)
567 continue;
568 pci_unmap_single(pdev, buffer->dma,
569 rcv_desc->dma_size,
570 PCI_DMA_FROMDEVICE);
571 if (buffer->skb != NULL)
572 dev_kfree_skb_any(buffer->skb);
573 }
574 vfree(rcv_desc->rx_buf_arr);
575 }
576 }
577
578 vfree(adapter->cmd_buf_arr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400579 kfree(adapter);
580}
581
582/*
583 * Called when a network interface is made active
584 * @returns 0 on success, negative value on failure
585 */
586static int netxen_nic_open(struct net_device *netdev)
587{
588 struct netxen_port *port = netdev_priv(netdev);
589 struct netxen_adapter *adapter = port->adapter;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400590 int err = 0;
591 int ctx, ring;
592
593 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
594 err = netxen_init_firmware(adapter);
595 if (err != 0) {
596 printk(KERN_ERR "Failed to init firmware\n");
597 return -EIO;
598 }
599 netxen_nic_flash_print(adapter);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800600 if (adapter->init_niu)
601 adapter->init_niu(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400602
603 /* setup all the resources for the Phantom... */
604 /* this include the descriptors for rcv, tx, and status */
605 netxen_nic_clear_stats(adapter);
606 err = netxen_nic_hw_resources(adapter);
607 if (err) {
608 printk(KERN_ERR "Error in setting hw resources:%d\n",
609 err);
610 return err;
611 }
Amit S. Kale80922fb2006-12-04 09:18:00 -0800612 if (adapter->init_port
613 && adapter->init_port(adapter, port->portnum) != 0) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400614 printk(KERN_ERR "%s: Failed to initialize port %d\n",
615 netxen_nic_driver_name, port->portnum);
616 netxen_free_hw_resources(adapter);
617 return -EIO;
618 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400619 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800620 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400621 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400622 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800623 adapter->irq = adapter->ahw.pdev->irq;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400624 err = request_irq(adapter->ahw.pdev->irq, &netxen_intr,
625 SA_SHIRQ | SA_SAMPLE_RANDOM, netdev->name,
626 adapter);
627 if (err) {
628 printk(KERN_ERR "request_irq failed with: %d\n", err);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800629 netxen_free_hw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400630 return err;
631 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800632
633 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
634 }
635 adapter->active_ports++;
636 if (adapter->active_ports == 1) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400637 if (!adapter->driver_mismatch)
638 mod_timer(&adapter->watchdog_timer, jiffies);
639
640 netxen_nic_enable_int(adapter);
641 }
642
643 /* Done here again so that even if phantom sw overwrote it,
644 * we set it */
Amit S. Kale80922fb2006-12-04 09:18:00 -0800645 if (adapter->macaddr_set)
646 adapter->macaddr_set(port, netdev->dev_addr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400647 netxen_nic_set_link_parameters(port);
648
649 netxen_nic_set_multi(netdev);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800650 if (adapter->set_mtu)
651 adapter->set_mtu(port, netdev->mtu);
652
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400653 if (!adapter->driver_mismatch)
654 netif_start_queue(netdev);
655
656 return 0;
657}
658
659/*
660 * netxen_nic_close - Disables a network interface entry point
661 */
662static int netxen_nic_close(struct net_device *netdev)
663{
664 struct netxen_port *port = netdev_priv(netdev);
665 struct netxen_adapter *adapter = port->adapter;
666 int i, j;
667 struct netxen_cmd_buffer *cmd_buff;
668 struct netxen_skb_frag *buffrag;
669
670 netif_carrier_off(netdev);
671 netif_stop_queue(netdev);
672
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400673 adapter->active_ports--;
674
675 if (!adapter->active_ports) {
676 netxen_nic_disable_int(adapter);
677 if (adapter->irq)
678 free_irq(adapter->irq, adapter);
679 cmd_buff = adapter->cmd_buf_arr;
680 for (i = 0; i < adapter->max_tx_desc_count; i++) {
681 buffrag = cmd_buff->frag_array;
682 if (buffrag->dma) {
683 pci_unmap_single(port->pdev, buffrag->dma,
684 buffrag->length,
685 PCI_DMA_TODEVICE);
686 buffrag->dma = (u64) NULL;
687 }
688 for (j = 0; j < cmd_buff->frag_count; j++) {
689 buffrag++;
690 if (buffrag->dma) {
691 pci_unmap_page(port->pdev,
692 buffrag->dma,
693 buffrag->length,
694 PCI_DMA_TODEVICE);
695 buffrag->dma = (u64) NULL;
696 }
697 }
698 /* Free the skb we received in netxen_nic_xmit_frame */
699 if (cmd_buff->skb) {
700 dev_kfree_skb_any(cmd_buff->skb);
701 cmd_buff->skb = NULL;
702 }
703 cmd_buff++;
704 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800705 FLUSH_SCHEDULED_WORK();
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400706 del_timer_sync(&adapter->watchdog_timer);
707 }
708
709 return 0;
710}
711
712static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
713{
714 struct netxen_port *port = netdev_priv(netdev);
715 struct netxen_adapter *adapter = port->adapter;
716 struct netxen_hardware_context *hw = &adapter->ahw;
717 unsigned int first_seg_len = skb->len - skb->data_len;
718 struct netxen_skb_frag *buffrag;
719 unsigned int i;
720
721 u32 producer = 0;
722 u32 saved_producer = 0;
723 struct cmd_desc_type0 *hwdesc;
724 int k;
725 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400726 static int dropped_packet = 0;
727 int frag_count;
728 u32 local_producer = 0;
729 u32 max_tx_desc_count = 0;
730 u32 last_cmd_consumer = 0;
731 int no_of_desc;
732
733 port->stats.xmitcalled++;
734 frag_count = skb_shinfo(skb)->nr_frags + 1;
735
736 if (unlikely(skb->len <= 0)) {
737 dev_kfree_skb_any(skb);
738 port->stats.badskblen++;
739 return NETDEV_TX_OK;
740 }
741
742 if (frag_count > MAX_BUFFERS_PER_CMD) {
743 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
744 "too large, can handle only %d frags\n",
745 netxen_nic_driver_name, netdev->name,
746 frag_count, MAX_BUFFERS_PER_CMD);
747 port->stats.txdropped++;
748 if ((++dropped_packet & 0xff) == 0xff)
749 printk("%s: %s droppped packets = %d\n",
750 netxen_nic_driver_name, netdev->name,
751 dropped_packet);
752
753 return NETDEV_TX_OK;
754 }
755
756 /*
757 * Everything is set up. Now, we just need to transmit it out.
758 * Note that we have to copy the contents of buffer over to
759 * right place. Later on, this can be optimized out by de-coupling the
760 * producer index from the buffer index.
761 */
762 retry_getting_window:
763 spin_lock_bh(&adapter->tx_lock);
764 if (adapter->total_threads == MAX_XMIT_PRODUCERS) {
765 spin_unlock_bh(&adapter->tx_lock);
766 /*
767 * Yield CPU
768 */
769 if (!in_atomic())
770 schedule();
771 else {
772 for (i = 0; i < 20; i++)
773 cpu_relax(); /*This a nop instr on i386 */
774 }
775 goto retry_getting_window;
776 }
777 local_producer = adapter->cmd_producer;
778 /* There 4 fragments per descriptor */
779 no_of_desc = (frag_count + 3) >> 2;
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800780 if (netdev->features & NETIF_F_TSO) {
781 if (skb_shinfo(skb)->gso_size > 0) {
782
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400783 no_of_desc++;
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800784 if (((skb->nh.iph)->ihl * sizeof(u32)) +
785 ((skb->h.th)->doff * sizeof(u32)) +
786 sizeof(struct ethhdr) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800787 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800788 no_of_desc++;
789 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400790 }
791 }
792 k = adapter->cmd_producer;
793 max_tx_desc_count = adapter->max_tx_desc_count;
794 last_cmd_consumer = adapter->last_cmd_consumer;
795 if ((k + no_of_desc) >=
796 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
797 last_cmd_consumer)) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800798 port->stats.nocmddescriptor++;
799 DPRINTK(ERR, "No command descriptors available,"
800 " producer = %d, consumer = %d count=%llu,"
801 " dropping packet\n", producer,
802 adapter->last_cmd_consumer,
803 port->stats.nocmddescriptor);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400804
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800805 netif_stop_queue(netdev);
806 port->flags |= NETXEN_NETDEV_STATUS;
807 spin_unlock_bh(&adapter->tx_lock);
808 return NETDEV_TX_BUSY;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400809 }
810 k = get_index_range(k, max_tx_desc_count, no_of_desc);
811 adapter->cmd_producer = k;
812 adapter->total_threads++;
813 adapter->num_threads++;
814
815 spin_unlock_bh(&adapter->tx_lock);
816 /* Copy the descriptors into the hardware */
817 producer = local_producer;
818 saved_producer = producer;
819 hwdesc = &hw->cmd_desc_head[producer];
820 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
821 /* Take skb->data itself */
822 pbuf = &adapter->cmd_buf_arr[producer];
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800823 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400824 pbuf->mss = skb_shinfo(skb)->gso_size;
825 hwdesc->mss = skb_shinfo(skb)->gso_size;
826 } else {
827 pbuf->mss = 0;
828 hwdesc->mss = 0;
829 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400830 pbuf->total_length = skb->len;
831 pbuf->skb = skb;
832 pbuf->cmd = TX_ETHER_PKT;
833 pbuf->frag_count = frag_count;
834 pbuf->port = port->portnum;
835 buffrag = &pbuf->frag_array[0];
836 buffrag->dma = pci_map_single(port->pdev, skb->data, first_seg_len,
837 PCI_DMA_TODEVICE);
838 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800839 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
840 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
841 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400842
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800843 netxen_set_cmd_desc_port(hwdesc, port->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400844 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
845 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
846
847 for (i = 1, k = 1; i < frag_count; i++, k++) {
848 struct skb_frag_struct *frag;
849 int len, temp_len;
850 unsigned long offset;
851 dma_addr_t temp_dma;
852
853 /* move to next desc. if there is a need */
854 if ((i & 0x3) == 0) {
855 k = 0;
856 producer = get_next_index(producer,
857 adapter->max_tx_desc_count);
858 hwdesc = &hw->cmd_desc_head[producer];
859 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
860 }
861 frag = &skb_shinfo(skb)->frags[i - 1];
862 len = frag->size;
863 offset = frag->page_offset;
864
865 temp_len = len;
866 temp_dma = pci_map_page(port->pdev, frag->page, offset,
867 len, PCI_DMA_TODEVICE);
868
869 buffrag++;
870 buffrag->dma = temp_dma;
871 buffrag->length = temp_len;
872
873 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
874 switch (k) {
875 case 0:
876 hwdesc->buffer1_length = cpu_to_le16(temp_len);
877 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
878 break;
879 case 1:
880 hwdesc->buffer2_length = cpu_to_le16(temp_len);
881 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
882 break;
883 case 2:
884 hwdesc->buffer3_length = cpu_to_le16(temp_len);
885 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
886 break;
887 case 3:
888 hwdesc->buffer4_length = temp_len;
889 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
890 break;
891 }
892 frag++;
893 }
894 producer = get_next_index(producer, adapter->max_tx_desc_count);
895
896 /* might change opcode to TX_TCP_LSO */
897 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
898
899 /* For LSO, we need to copy the MAC/IP/TCP headers into
900 * the descriptor ring
901 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800902 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
903 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400904 int hdr_len, first_hdr_len, more_hdr;
905 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800906 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
907 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400908 more_hdr = 1;
909 } else {
910 first_hdr_len = hdr_len;
911 more_hdr = 0;
912 }
913 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
914 hwdesc = &hw->cmd_desc_head[producer];
915
916 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800917 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400918 (void *)(skb->data), first_hdr_len);
919 producer = get_next_index(producer, max_tx_desc_count);
920
921 if (more_hdr) {
922 hwdesc = &hw->cmd_desc_head[producer];
923 /* copy the next 64 bytes - should be enough except
924 * for pathological case
925 */
926 memcpy((void *)hwdesc, (void *)(skb->data) +
927 first_hdr_len, hdr_len - first_hdr_len);
928 producer = get_next_index(producer, max_tx_desc_count);
929 }
930 }
931 spin_lock_bh(&adapter->tx_lock);
932 port->stats.txbytes +=
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800933 netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400934 /* Code to update the adapter considering how many producer threads
935 are currently working */
936 if ((--adapter->num_threads) == 0) {
937 /* This is the last thread */
938 u32 crb_producer = adapter->cmd_producer;
939 writel(crb_producer,
940 NETXEN_CRB_NORMALIZE(adapter, CRB_CMD_PRODUCER_OFFSET));
941 wmb();
942 adapter->total_threads = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400943 }
944
945 port->stats.xmitfinished++;
946 spin_unlock_bh(&adapter->tx_lock);
947
948 netdev->trans_start = jiffies;
949
950 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
951
952 DPRINTK(INFO, "Done. Send\n");
953 return NETDEV_TX_OK;
954}
955
956static void netxen_watchdog(unsigned long v)
957{
958 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800959 if (adapter != g_adapter) {
960 printk("%s: ***BUG*** adapter[%p] != g_adapter[%p]\n",
961 __FUNCTION__, adapter, g_adapter);
962 return;
963 }
964
965 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400966}
967
968static void netxen_tx_timeout(struct net_device *netdev)
969{
970 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400971
Jeff Garzik0bfdcc82006-12-07 06:30:07 -0500972 SCHEDULE_WORK(&port->adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400973}
974
David Howells6d5aefb2006-12-05 19:36:26 +0000975static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400976{
David Howells6d5aefb2006-12-05 19:36:26 +0000977 struct netxen_adapter *adapter =
978 container_of(work, struct netxen_adapter, tx_timeout_task);
979 struct net_device *netdev = adapter->netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400980 unsigned long flags;
981
982 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
983 netxen_nic_driver_name, netdev->name);
984
David Howells6d5aefb2006-12-05 19:36:26 +0000985 spin_lock_irqsave(&adapter->lock, flags);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400986 netxen_nic_close(netdev);
987 netxen_nic_open(netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000988 spin_unlock_irqrestore(&adapter->lock, flags);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400989 netdev->trans_start = jiffies;
990 netif_wake_queue(netdev);
991}
992
993static int
994netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
995{
996 u32 ret = 0;
997
998 DPRINTK(INFO, "Entered handle ISR\n");
999
1000 adapter->stats.ints++;
1001
1002 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1003 int count = 0;
1004 u32 mask;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001005 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
1006 if ((mask & 0x80) == 0) {
1007 /* not our interrupt */
1008 return ret;
1009 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001010 netxen_nic_disable_int(adapter);
1011 /* Window = 0 or 1 */
1012 do {
Amit S. Kale71bd7872006-12-01 05:36:22 -08001013 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
1014 ISR_INT_TARGET_STATUS));
1015 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001016 } while (((mask & 0x80) != 0) && (++count < 32));
1017 if ((mask & 0x80) != 0)
1018 printk("Could not disable interrupt completely\n");
1019
1020 }
1021 adapter->stats.hostints++;
1022
1023 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
1024 if (netif_rx_schedule_prep(netdev)) {
1025 /*
1026 * Interrupts are already disabled.
1027 */
1028 __netif_rx_schedule(netdev);
1029 } else {
1030 static unsigned int intcount = 0;
1031 if ((++intcount & 0xfff) == 0xfff)
1032 printk(KERN_ERR
1033 "%s: %s interrupt %d while in poll\n",
1034 netxen_nic_driver_name, netdev->name,
1035 intcount);
1036 }
1037 ret = 1;
1038 }
1039
1040 if (ret == 0) {
1041 netxen_nic_enable_int(adapter);
1042 }
1043
1044 return ret;
1045}
1046
1047/*
1048 * netxen_intr - Interrupt Handler
1049 * @irq: interrupt number
1050 * data points to adapter stucture (which may be handling more than 1 port
1051 */
Jeff Garzik1494a812006-11-07 05:12:16 -05001052irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001053{
1054 struct netxen_adapter *adapter;
1055 struct netxen_port *port;
1056 struct net_device *netdev;
1057 int i;
1058
1059 if (unlikely(!irq)) {
1060 return IRQ_NONE; /* Not our interrupt */
1061 }
1062
1063 adapter = (struct netxen_adapter *)data;
1064 for (i = 0; i < adapter->ahw.max_ports; i++) {
1065 port = adapter->port[i];
1066 netdev = port->netdev;
1067
1068 /* process our status queue (for all 4 ports) */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001069 if (netif_running(netdev)) {
1070 netxen_handle_int(adapter, netdev);
1071 break;
1072 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001073 }
1074
1075 return IRQ_HANDLED;
1076}
1077
1078static int netxen_nic_poll(struct net_device *netdev, int *budget)
1079{
1080 struct netxen_port *port = (struct netxen_port *)netdev_priv(netdev);
1081 struct netxen_adapter *adapter = port->adapter;
1082 int work_to_do = min(*budget, netdev->quota);
1083 int done = 1;
1084 int ctx;
1085 int this_work_done;
Amit S. Kale80922fb2006-12-04 09:18:00 -08001086 int work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001087
1088 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
1089 port->stats.polled++;
1090
Amit S. Kale80922fb2006-12-04 09:18:00 -08001091 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001092 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1093 /*
1094 * Fairness issue. This will give undue weight to the
1095 * receive context 0.
1096 */
1097
1098 /*
1099 * To avoid starvation, we give each of our receivers,
1100 * a fraction of the quota. Sometimes, it might happen that we
1101 * have enough quota to process every packet, but since all the
1102 * packets are on one context, it gets only half of the quota,
1103 * and ends up not processing it.
1104 */
1105 this_work_done = netxen_process_rcv_ring(adapter, ctx,
1106 work_to_do /
1107 MAX_RCV_CTX);
Amit S. Kale80922fb2006-12-04 09:18:00 -08001108 work_done += this_work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001109 }
1110
Amit S. Kale80922fb2006-12-04 09:18:00 -08001111 netdev->quota -= work_done;
1112 *budget -= work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001113
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001114 if (work_done >= work_to_do && netxen_nic_rx_has_work(adapter) != 0)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001115 done = 0;
1116
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001117 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1118 done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001119
1120 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
Amit S. Kale80922fb2006-12-04 09:18:00 -08001121 work_done, work_to_do);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001122 if (done) {
1123 netif_rx_complete(netdev);
1124 netxen_nic_enable_int(adapter);
1125 }
1126
Amit S. Kaleedf90162006-11-29 08:58:11 -08001127 return !done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001128}
1129
1130#ifdef CONFIG_NET_POLL_CONTROLLER
1131static void netxen_nic_poll_controller(struct net_device *netdev)
1132{
1133 struct netxen_port *port = netdev_priv(netdev);
1134 struct netxen_adapter *adapter = port->adapter;
1135 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001136 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001137 enable_irq(adapter->irq);
1138}
1139#endif
1140/*
1141 * netxen_nic_ioctl () We provide the tcl/phanmon support through these
1142 * ioctls.
1143 */
1144static int
1145netxen_nic_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1146{
1147 int err = 0;
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001148 unsigned long nr_bytes = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001149 struct netxen_port *port = netdev_priv(netdev);
1150 struct netxen_adapter *adapter = port->adapter;
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001151 char dev_name[NETXEN_NIC_NAME_LEN];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001152
1153 DPRINTK(INFO, "doing ioctl for %s\n", netdev->name);
1154 switch (cmd) {
1155 case NETXEN_NIC_CMD:
1156 err = netxen_nic_do_ioctl(adapter, (void *)ifr->ifr_data, port);
1157 break;
1158
1159 case NETXEN_NIC_NAME:
1160 DPRINTK(INFO, "ioctl cmd for NetXen\n");
1161 if (ifr->ifr_data) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001162 sprintf(dev_name, "%s-%d", NETXEN_NIC_NAME_RSP,
1163 port->portnum);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001164 nr_bytes =
1165 copy_to_user((char __user *)ifr->ifr_data, dev_name,
1166 NETXEN_NIC_NAME_LEN);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001167 if (nr_bytes)
1168 err = -EIO;
1169
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001170 }
1171 break;
1172
1173 default:
1174 DPRINTK(INFO, "ioctl cmd %x not supported\n", cmd);
1175 err = -EOPNOTSUPP;
1176 break;
1177 }
1178
1179 return err;
1180}
1181
1182static struct pci_driver netxen_driver = {
1183 .name = netxen_nic_driver_name,
1184 .id_table = netxen_pci_tbl,
1185 .probe = netxen_nic_probe,
1186 .remove = __devexit_p(netxen_nic_remove)
1187};
1188
1189/* Driver Registration on NetXen card */
1190
1191static int __init netxen_init_module(void)
1192{
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001193 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1194 return -ENOMEM;
1195
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001196 return pci_module_init(&netxen_driver);
1197}
1198
1199module_init(netxen_init_module);
1200
1201static void __exit netxen_exit_module(void)
1202{
1203 /*
1204 * Wait for some time to allow the dma to drain, if any.
1205 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001206 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001207 pci_unregister_driver(&netxen_driver);
1208}
1209
1210module_exit(netxen_exit_module);