blob: 7144c255ce54aa3b23856fb9df2f1dfe2f28d48e [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"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040039#include "netxen_nic_phan_reg.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040040
Jeff Garzik1494a812006-11-07 05:12:16 -050041#include <linux/dma-mapping.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030042#include <net/ip.h>
Jeff Garzik1494a812006-11-07 05:12:16 -050043
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
dhananjay@netxen.comceded322007-07-19 14:41:09 +053048char netxen_nic_driver_name[] = "netxen_nic";
Amit S. Kale3d396eb2006-10-21 15:33:03 -040049static char netxen_nic_driver_string[] = "NetXen Network Driver version "
Amit S. Kalecb8011a2006-11-29 09:00:10 -080050 NETXEN_NIC_LINUX_VERSIONID;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040051
52#define NETXEN_NETDEV_WEIGHT 120
53#define NETXEN_ADAPTER_UP_MAGIC 777
Amit S. Kalecb8011a2006-11-29 09:00:10 -080054#define NETXEN_NIC_PEG_TUNE 0
Amit S. Kale3d396eb2006-10-21 15:33:03 -040055
56/* Local functions to NetXen NIC driver */
57static int __devinit netxen_nic_probe(struct pci_dev *pdev,
58 const struct pci_device_id *ent);
59static void __devexit netxen_nic_remove(struct pci_dev *pdev);
60static int netxen_nic_open(struct net_device *netdev);
61static int netxen_nic_close(struct net_device *netdev);
62static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
63static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000064static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040065static void netxen_watchdog(unsigned long);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070066static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040067#ifdef CONFIG_NET_POLL_CONTROLLER
68static void netxen_nic_poll_controller(struct net_device *netdev);
69#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050070static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070071static irqreturn_t netxen_msi_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040072
Mithlesh Thukral6c80b182007-04-20 07:55:26 -070073int physical_port[] = {0, 1, 2, 3};
74
Amit S. Kale3d396eb2006-10-21 15:33:03 -040075/* PCI Device ID Table */
76static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
77 {PCI_DEVICE(0x4040, 0x0001)},
78 {PCI_DEVICE(0x4040, 0x0002)},
79 {PCI_DEVICE(0x4040, 0x0003)},
80 {PCI_DEVICE(0x4040, 0x0004)},
81 {PCI_DEVICE(0x4040, 0x0005)},
Amit S. Kale0d390732006-12-18 05:50:59 -080082 {PCI_DEVICE(0x4040, 0x0024)},
83 {PCI_DEVICE(0x4040, 0x0025)},
Amit S. Kale3d396eb2006-10-21 15:33:03 -040084 {0,}
85};
86
87MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
88
Adrian Bunkb15551302008-03-31 02:22:18 +030089/*
90 * In netxen_nic_down(), we must wait for any pending callback requests into
91 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
92 * reenabled right after it is deleted in netxen_nic_down().
93 * FLUSH_SCHEDULED_WORK() does this synchronization.
94 *
95 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
96 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
97 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
98 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
99 * linkwatch_event() to be executed which also attempts to acquire the rtnl
100 * lock thus causing a deadlock.
101 */
102
103static struct workqueue_struct *netxen_workq;
104#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
105#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
106
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800107static void netxen_watchdog(unsigned long);
108
Adrian Bunk993fb902007-11-05 18:07:31 +0100109static void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
110 uint32_t crb_producer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700111{
112 switch (adapter->portnum) {
113 case 0:
114 writel(crb_producer, NETXEN_CRB_NORMALIZE
115 (adapter, CRB_CMD_PRODUCER_OFFSET));
116 return;
117 case 1:
118 writel(crb_producer, NETXEN_CRB_NORMALIZE
119 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
120 return;
121 case 2:
122 writel(crb_producer, NETXEN_CRB_NORMALIZE
123 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
124 return;
125 case 3:
126 writel(crb_producer, NETXEN_CRB_NORMALIZE
127 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
128 return;
129 default:
130 printk(KERN_WARNING "We tried to update "
131 "CRB_CMD_PRODUCER_OFFSET for invalid "
132 "PCI function id %d\n",
133 adapter->portnum);
134 return;
135 }
136}
137
Adrian Bunk993fb902007-11-05 18:07:31 +0100138static void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
139 u32 crb_consumer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700140{
141 switch (adapter->portnum) {
142 case 0:
143 writel(crb_consumer, NETXEN_CRB_NORMALIZE
144 (adapter, CRB_CMD_CONSUMER_OFFSET));
145 return;
146 case 1:
147 writel(crb_consumer, NETXEN_CRB_NORMALIZE
148 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
149 return;
150 case 2:
151 writel(crb_consumer, NETXEN_CRB_NORMALIZE
152 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
153 return;
154 case 3:
155 writel(crb_consumer, NETXEN_CRB_NORMALIZE
156 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
157 return;
158 default:
159 printk(KERN_WARNING "We tried to update "
160 "CRB_CMD_PRODUCER_OFFSET for invalid "
161 "PCI function id %d\n",
162 adapter->portnum);
163 return;
164 }
165}
166
167#define ADAPTER_LIST_SIZE 12
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700168
Dhananjay Phadke443be792008-03-17 19:59:48 -0700169static uint32_t msi_tgt_status[4] = {
170 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
171 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3
172};
173
174static uint32_t sw_int_mask[4] = {
175 CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1,
176 CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3
177};
178
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700179static void netxen_nic_disable_int(struct netxen_adapter *adapter)
180{
Dhananjay Phadke443be792008-03-17 19:59:48 -0700181 u32 mask = 0x7ff;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700182 int retries = 32;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700183 int port = adapter->portnum;
184 int pci_fn = adapter->ahw.pci_func;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700185
Jeff Garzikf0c88f92008-03-25 23:53:24 -0400186 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
Dhananjay Phadke443be792008-03-17 19:59:48 -0700187 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700188
189 if (adapter->intr_scheme != -1 &&
190 adapter->intr_scheme != INTR_SCHEME_PERPORT)
191 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
192
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700193 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
194 do {
195 writel(0xffffffff,
196 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
197 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
198 if (!(mask & 0x80))
199 break;
200 udelay(10);
201 } while (--retries);
202
203 if (!retries) {
204 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
205 netxen_nic_driver_name);
206 }
Dhananjay Phadke443be792008-03-17 19:59:48 -0700207 } else {
208 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
209 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter,
210 msi_tgt_status[pci_fn]));
211 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700212 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700213}
214
215static void netxen_nic_enable_int(struct netxen_adapter *adapter)
216{
217 u32 mask;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700218 int port = adapter->portnum;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700219
220 DPRINTK(1, INFO, "Entered ISR Enable \n");
221
222 if (adapter->intr_scheme != -1 &&
223 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
224 switch (adapter->ahw.board_type) {
225 case NETXEN_NIC_GBE:
226 mask = 0x77b;
227 break;
228 case NETXEN_NIC_XGBE:
229 mask = 0x77f;
230 break;
231 default:
232 mask = 0x7ff;
233 break;
234 }
235
236 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
237 }
238
Dhananjay Phadke443be792008-03-17 19:59:48 -0700239 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port]));
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700240
241 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
242 mask = 0xbff;
243 if (adapter->intr_scheme != -1 &&
244 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
245 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
246 }
247 writel(mask,
248 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
249 }
250
251 DPRINTK(1, INFO, "Done with enable Int\n");
252}
253
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400254/*
255 * netxen_nic_probe()
256 *
257 * The Linux system will invoke this after identifying the vendor ID and
258 * device Id in the pci_tbl supported by this module.
259 *
260 * A quad port card has one operational PCI config space, (function 0),
261 * which is used to access all four ports.
262 *
263 * This routine will initialize the adapter, and setup the global parameters
264 * along with the port's specific structure.
265 */
266static int __devinit
267netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
268{
269 struct net_device *netdev = NULL;
270 struct netxen_adapter *adapter = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800271 void __iomem *mem_ptr0 = NULL;
272 void __iomem *mem_ptr1 = NULL;
273 void __iomem *mem_ptr2 = NULL;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700274 unsigned long first_page_group_end;
275 unsigned long first_page_group_start;
276
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800277
Al Viroa608ab9c2007-01-02 10:39:10 +0000278 u8 __iomem *db_ptr = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800279 unsigned long mem_base, mem_len, db_base, db_len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700280 int pci_using_dac, i = 0, err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400281 int ring;
282 struct netxen_recv_context *recv_ctx = NULL;
283 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
284 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
Al Virof305f782007-12-22 19:44:00 +0000285 __le64 mac_addr[FLASH_NUM_PORTS + 1];
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700286 int valid_mac = 0;
287 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700288 int pci_func_id = PCI_FUNC(pdev->devfn);
Joe Perches0795af52007-10-03 17:59:30 -0700289 DECLARE_MAC_BUF(mac);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400290
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800291 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700292
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700293 if (pdev->class != 0x020000) {
Joe Perches5bc51422007-11-19 17:48:24 -0800294 printk(KERN_ERR"NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700295 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800296 return -ENODEV;
297 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400298 if ((err = pci_enable_device(pdev)))
299 return err;
300 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
301 err = -ENODEV;
302 goto err_out_disable_pdev;
303 }
304
305 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
306 goto err_out_disable_pdev;
307
308 pci_set_master(pdev);
Auke Kok44c10132007-06-08 15:46:36 -0700309 if (pdev->revision == NX_P2_C1 &&
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800310 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
311 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400312 pci_using_dac = 1;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800313 } else {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400314 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
315 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
316 goto err_out_free_res;
317
318 pci_using_dac = 0;
319 }
320
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700321
322 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
323 if(!netdev) {
324 printk(KERN_ERR"%s: Failed to allocate memory for the "
325 "device block.Check system memory resource"
326 " usage.\n", netxen_nic_driver_name);
327 goto err_out_free_res;
328 }
329
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700330 SET_NETDEV_DEV(netdev, &pdev->dev);
331
332 adapter = netdev->priv;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700333
334 adapter->ahw.pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700335 adapter->ahw.pci_func = pci_func_id;
336
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400337 /* remap phys address */
338 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
339 mem_len = pci_resource_len(pdev, 0);
340
341 /* 128 Meg of memory */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700342 if (mem_len == NETXEN_PCI_128MB_SIZE) {
343 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
344 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
345 SECOND_PAGE_GROUP_SIZE);
346 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
347 THIRD_PAGE_GROUP_SIZE);
348 first_page_group_start = FIRST_PAGE_GROUP_START;
349 first_page_group_end = FIRST_PAGE_GROUP_END;
350 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
351 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
352 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
353 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
354 first_page_group_start = 0;
355 first_page_group_end = 0;
356 } else {
Jeff Garzik47906542007-11-23 21:23:36 -0500357 err = -EIO;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700358 goto err_out_free_netdev;
359 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800360
Al Viro79ea13c2008-01-24 02:06:46 -0800361 if ((!mem_ptr0 && mem_len == NETXEN_PCI_128MB_SIZE) ||
362 !mem_ptr1 || !mem_ptr2) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800363 DPRINTK(ERR,
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800364 "Cannot remap adapter memory aborting.:"
365 "0 -> %p, 1 -> %p, 2 -> %p\n",
366 mem_ptr0, mem_ptr1, mem_ptr2);
367
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400368 err = -EIO;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800369 goto err_out_iounmap;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400370 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800371 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
372 db_len = pci_resource_len(pdev, 4);
373
374 if (db_len == 0) {
375 printk(KERN_ERR "%s: doorbell is disabled\n",
376 netxen_nic_driver_name);
377 err = -EIO;
378 goto err_out_iounmap;
379 }
380 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
381 db_len);
382
383 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
Al Viroa608ab9c2007-01-02 10:39:10 +0000384 if (!db_ptr) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800385 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
386 netxen_nic_driver_name);
387 err = -EIO;
388 goto err_out_iounmap;
389 }
390 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400391
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700392 adapter->ahw.pci_base0 = mem_ptr0;
393 adapter->ahw.first_page_group_start = first_page_group_start;
394 adapter->ahw.first_page_group_end = first_page_group_end;
395 adapter->ahw.pci_base1 = mem_ptr1;
396 adapter->ahw.pci_base2 = mem_ptr2;
397 adapter->ahw.db_base = db_ptr;
398 adapter->ahw.db_len = db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400399
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700400 adapter->netdev = netdev;
401 adapter->pdev = pdev;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530402
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700403 netif_napi_add(netdev, &adapter->napi,
404 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
405
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530406 /* this will be read from FW later */
407 adapter->intr_scheme = -1;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700408 adapter->msi_mode = -1;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530409
410 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700411 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530412 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530413 adapter->rx_csum = 1;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700414
415 netdev->open = netxen_nic_open;
416 netdev->stop = netxen_nic_close;
417 netdev->hard_start_xmit = netxen_nic_xmit_frame;
Jeff Garzik47906542007-11-23 21:23:36 -0500418 netdev->get_stats = netxen_nic_get_stats;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700419 netdev->set_multicast_list = netxen_nic_set_multi;
420 netdev->set_mac_address = netxen_nic_set_mac;
421 netdev->change_mtu = netxen_nic_change_mtu;
422 netdev->tx_timeout = netxen_tx_timeout;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700423 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700424
425 netxen_nic_change_mtu(netdev, netdev->mtu);
426
427 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700428#ifdef CONFIG_NET_POLL_CONTROLLER
429 netdev->poll_controller = netxen_nic_poll_controller;
430#endif
431 /* ScatterGather support */
432 netdev->features = NETIF_F_SG;
433 netdev->features |= NETIF_F_IP_CSUM;
434 netdev->features |= NETIF_F_TSO;
435
436 if (pci_using_dac)
437 netdev->features |= NETIF_F_HIGHDMA;
438
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530439 if (pci_enable_msi(pdev))
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700440 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530441 else
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700442 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
443
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700444 netdev->irq = pdev->irq;
445 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
446
447 /*
448 * Set the CRB window to invalid. If any register in window 0 is
449 * accessed it should set the window to 0 and then reset it to 1.
450 */
451 adapter->curr_window = 255;
452
453 /* initialize the adapter */
454 netxen_initialize_adapter_hw(adapter);
455
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700456 /*
457 * Adapter in our case is quad port so initialize it before
458 * initializing the ports
459 */
460
461 netxen_initialize_adapter_ops(adapter);
462
463 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
464 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
Jeff Garzik47906542007-11-23 21:23:36 -0500465 (adapter->ahw.boardcfg.board_type ==
466 NETXEN_BRDTYPE_P2_SB31_2G))
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700467 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
468 else
469 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
470 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
471 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
472
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400473 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
474 if (cmd_buf_arr == NULL) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800475 printk(KERN_ERR
476 "%s: Could not allocate cmd_buf_arr memory:%d\n",
477 netxen_nic_driver_name, (int)TX_RINGSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400478 err = -ENOMEM;
479 goto err_out_free_adapter;
480 }
481 memset(cmd_buf_arr, 0, TX_RINGSIZE);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700482 adapter->cmd_buf_arr = cmd_buf_arr;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400483
484 for (i = 0; i < MAX_RCV_CTX; ++i) {
485 recv_ctx = &adapter->recv_ctx[i];
486 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
487 rcv_desc = &recv_ctx->rcv_desc[ring];
488 switch (RCV_DESC_TYPE(ring)) {
489 case RCV_DESC_NORMAL:
490 rcv_desc->max_rx_desc_count =
491 adapter->max_rx_desc_count;
492 rcv_desc->flags = RCV_DESC_NORMAL;
493 rcv_desc->dma_size = RX_DMA_MAP_LEN;
494 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
495 break;
496
497 case RCV_DESC_JUMBO:
498 rcv_desc->max_rx_desc_count =
499 adapter->max_jumbo_rx_desc_count;
500 rcv_desc->flags = RCV_DESC_JUMBO;
501 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
502 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
503 break;
504
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800505 case RCV_RING_LRO:
506 rcv_desc->max_rx_desc_count =
507 adapter->max_lro_rx_desc_count;
508 rcv_desc->flags = RCV_DESC_LRO;
509 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
510 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
511 break;
512
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400513 }
514 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
515 vmalloc(RCV_BUFFSIZE);
516
517 if (rcv_desc->rx_buf_arr == NULL) {
Joe Perches5bc51422007-11-19 17:48:24 -0800518 printk(KERN_ERR "%s: Could not allocate "
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800519 "rcv_desc->rx_buf_arr memory:%d\n",
520 netxen_nic_driver_name,
521 (int)RCV_BUFFSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400522 err = -ENOMEM;
523 goto err_out_free_rx_buffer;
524 }
525 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
526 }
527
528 }
529
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800530 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700531
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700532 /* Mezz cards have PCI function 0,2,3 enabled */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700533 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB31_10G_IMEZ)
534 && (pci_func_id >= 2))
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700535 adapter->portnum = pci_func_id - 2;
536
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800537#ifdef CONFIG_IA64
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700538 if(adapter->portnum == 0) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700539 netxen_pinit_from_rom(adapter, 0);
540 udelay(500);
541 netxen_load_firmware(adapter);
542 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800543#endif
544
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400545 init_timer(&adapter->watchdog_timer);
546 adapter->ahw.xg_linkup = 0;
547 adapter->watchdog_timer.function = &netxen_watchdog;
548 adapter->watchdog_timer.data = (unsigned long)adapter;
David Howells6d5aefb2006-12-05 19:36:26 +0000549 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400550 adapter->ahw.pdev = pdev;
Auke Kok44c10132007-06-08 15:46:36 -0700551 adapter->ahw.revision_id = pdev->revision;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400552
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700553 /* make sure Window == 1 */
554 netxen_nic_pci_change_crbwindow(adapter, 1);
555
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700556 netxen_nic_update_cmd_producer(adapter, 0);
557 netxen_nic_update_cmd_consumer(adapter, 0);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700558 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400559
560 if (netxen_is_flash_supported(adapter) == 0 &&
561 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
562 valid_mac = 1;
563 else
564 valid_mac = 0;
565
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700566 if (valid_mac) {
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700567 unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700568 netdev->dev_addr[0] = *(p + 5);
569 netdev->dev_addr[1] = *(p + 4);
570 netdev->dev_addr[2] = *(p + 3);
571 netdev->dev_addr[3] = *(p + 2);
572 netdev->dev_addr[4] = *(p + 1);
573 netdev->dev_addr[5] = *(p + 0);
574
575 memcpy(netdev->perm_addr, netdev->dev_addr,
576 netdev->addr_len);
577 if (!is_valid_ether_addr(netdev->perm_addr)) {
Joe Perches0795af52007-10-03 17:59:30 -0700578 printk(KERN_ERR "%s: Bad MAC address %s.\n",
579 netxen_nic_driver_name,
580 print_mac(mac, netdev->dev_addr));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700581 } else {
582 if (adapter->macaddr_set)
583 adapter->macaddr_set(adapter,
584 netdev->dev_addr);
585 }
586 }
587
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700588 if (adapter->portnum == 0) {
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700589 err = netxen_initialize_adapter_offload(adapter);
Jeff Garzik47906542007-11-23 21:23:36 -0500590 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700591 goto err_out_free_rx_buffer;
Jeff Garzik47906542007-11-23 21:23:36 -0500592 val = readl(NETXEN_CRB_NORMALIZE(adapter,
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700593 NETXEN_CAM_RAM(0x1fc)));
594 if (val == 0x55555555) {
595 /* This is the first boot after power up */
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530596 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530597 if (!(val & 0x4)) {
598 val |= 0x4;
599 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
600 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
601 if (!(val & 0x4))
602 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
603 netxen_nic_driver_name);
604 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700605 val = readl(NETXEN_CRB_NORMALIZE(adapter,
606 NETXEN_ROMUSB_GLB_SW_RESET));
607 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
608 if (val != 0x80000f) {
609 /* clear the register for future unloads/loads */
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530610 writel(0, NETXEN_CRB_NORMALIZE(adapter,
611 NETXEN_CAM_RAM(0x1fc)));
612 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
613 err = -ENODEV;
614 goto err_out_free_dev;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700615 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700616 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530617
618 /* clear the register for future unloads/loads */
619 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700620 printk(KERN_INFO "State: 0x%0x\n",
621 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
622
623 /*
624 * Tell the hardware our version number.
625 */
Jeff Garzik47906542007-11-23 21:23:36 -0500626 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700627 | ((_NETXEN_NIC_LINUX_MINOR << 8))
628 | (_NETXEN_NIC_LINUX_SUBVERSION);
629 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
630
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700631 /* Unlock the HW, prompting the boot sequence */
632 writel(1,
633 NETXEN_CRB_NORMALIZE(adapter,
634 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
635 /* Handshake with the card before we register the devices. */
636 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800637 }
638
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400639 /*
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700640 * See if the firmware gave us a virtual-physical port mapping.
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400641 */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700642 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
643 if (i != 0x55555555)
644 physical_port[adapter->portnum] = i;
645
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700646 netif_carrier_off(netdev);
647 netif_stop_queue(netdev);
648
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700649 if ((err = register_netdev(netdev))) {
650 printk(KERN_ERR "%s: register_netdev failed port #%d"
651 " aborting\n", netxen_nic_driver_name,
652 adapter->portnum);
653 err = -EIO;
654 goto err_out_free_dev;
655 }
656
657 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400658
659 switch (adapter->ahw.board_type) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700660 case NETXEN_NIC_GBE:
661 printk(KERN_INFO "%s: QUAD GbE board initialized\n",
662 netxen_nic_driver_name);
663 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400664
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700665 case NETXEN_NIC_XGBE:
Jeff Garzik47906542007-11-23 21:23:36 -0500666 printk(KERN_INFO "%s: XGbE board initialized\n",
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700667 netxen_nic_driver_name);
668 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400669 }
670
671 adapter->driver_mismatch = 0;
672
673 return 0;
674
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700675err_out_free_dev:
676 if (adapter->portnum == 0)
677 netxen_free_adapter_offload(adapter);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700678
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700679err_out_free_rx_buffer:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400680 for (i = 0; i < MAX_RCV_CTX; ++i) {
681 recv_ctx = &adapter->recv_ctx[i];
682 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
683 rcv_desc = &recv_ctx->rcv_desc[ring];
684 if (rcv_desc->rx_buf_arr != NULL) {
685 vfree(rcv_desc->rx_buf_arr);
686 rcv_desc->rx_buf_arr = NULL;
687 }
688 }
689 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400690 vfree(cmd_buf_arr);
691
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700692err_out_free_adapter:
693 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
694 pci_disable_msi(pdev);
695
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400696 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400697
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800698 if (db_ptr)
699 iounmap(db_ptr);
700
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700701err_out_iounmap:
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800702 if (mem_ptr0)
703 iounmap(mem_ptr0);
704 if (mem_ptr1)
705 iounmap(mem_ptr1);
706 if (mem_ptr2)
707 iounmap(mem_ptr2);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800708
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700709err_out_free_netdev:
710 free_netdev(netdev);
711
712err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400713 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700714
715err_out_disable_pdev:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400716 pci_disable_device(pdev);
717 return err;
718}
719
720static void __devexit netxen_nic_remove(struct pci_dev *pdev)
721{
722 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700723 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400724 struct netxen_rx_buffer *buffer;
725 struct netxen_recv_context *recv_ctx;
726 struct netxen_rcv_desc_ctx *rcv_desc;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530727 int i, ctxid, ring;
728 static int init_firmware_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400729
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700730 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400731 if (adapter == NULL)
732 return;
733
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700734 netdev = adapter->netdev;
735
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530736 unregister_netdev(netdev);
737
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530738 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
739 init_firmware_done++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400740 netxen_free_hw_resources(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530741 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400742
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400743 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
744 recv_ctx = &adapter->recv_ctx[ctxid];
745 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
746 rcv_desc = &recv_ctx->rcv_desc[ring];
747 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
748 buffer = &(rcv_desc->rx_buf_arr[i]);
749 if (buffer->state == NETXEN_BUFFER_FREE)
750 continue;
751 pci_unmap_single(pdev, buffer->dma,
752 rcv_desc->dma_size,
753 PCI_DMA_FROMDEVICE);
754 if (buffer->skb != NULL)
755 dev_kfree_skb_any(buffer->skb);
756 }
757 vfree(rcv_desc->rx_buf_arr);
758 }
759 }
760
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700761 vfree(adapter->cmd_buf_arr);
762
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530763 if (adapter->portnum == 0) {
764 if (init_firmware_done) {
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530765 i = 100;
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530766 do {
767 if (dma_watchdog_shutdown_request(adapter) == 1)
768 break;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530769 msleep(100);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530770 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
771 break;
772 } while (--i);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530773
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530774 if (i == 0)
775 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
776 netdev->name);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530777
778 /* clear the register for future unloads/loads */
779 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
780 printk(KERN_INFO "State: 0x%0x\n",
781 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
782
783 /* leave the hw in the same state as reboot */
784 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530785 netxen_pinit_from_rom(adapter, 0);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530786 msleep(1);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530787 netxen_load_firmware(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530788 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
789 }
790
791 /* clear the register for future unloads/loads */
792 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
793 printk(KERN_INFO "State: 0x%0x\n",
794 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
795
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530796 i = 100;
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530797 do {
798 if (dma_watchdog_shutdown_request(adapter) == 1)
799 break;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530800 msleep(100);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530801 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
802 break;
803 } while (--i);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530804
805 if (i) {
806 netxen_free_adapter_offload(adapter);
807 } else {
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530808 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
809 netdev->name);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530810 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530811 }
812
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530813 if (adapter->irq)
814 free_irq(adapter->irq, adapter);
815
816 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
817 pci_disable_msi(pdev);
818
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700819 iounmap(adapter->ahw.db_base);
820 iounmap(adapter->ahw.pci_base0);
821 iounmap(adapter->ahw.pci_base1);
822 iounmap(adapter->ahw.pci_base2);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700823
824 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530825 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700826 pci_set_drvdata(pdev, NULL);
827
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700828 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400829}
830
831/*
832 * Called when a network interface is made active
833 * @returns 0 on success, negative value on failure
834 */
835static int netxen_nic_open(struct net_device *netdev)
836{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700837 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400838 int err = 0;
839 int ctx, ring;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700840 irq_handler_t handler;
841 unsigned long flags = IRQF_SAMPLE_RANDOM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400842
843 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
844 err = netxen_init_firmware(adapter);
845 if (err != 0) {
846 printk(KERN_ERR "Failed to init firmware\n");
847 return -EIO;
848 }
849 netxen_nic_flash_print(adapter);
850
851 /* setup all the resources for the Phantom... */
852 /* this include the descriptors for rcv, tx, and status */
853 netxen_nic_clear_stats(adapter);
854 err = netxen_nic_hw_resources(adapter);
855 if (err) {
856 printk(KERN_ERR "Error in setting hw resources:%d\n",
857 err);
858 return err;
859 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400860 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800861 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400862 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400863 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800864 adapter->irq = adapter->ahw.pdev->irq;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700865 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
866 handler = netxen_msi_intr;
867 else {
868 flags |= IRQF_SHARED;
869 handler = netxen_intr;
870 }
871 err = request_irq(adapter->irq, handler,
872 flags, netdev->name, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400873 if (err) {
874 printk(KERN_ERR "request_irq failed with: %d\n", err);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800875 netxen_free_hw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400876 return err;
877 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800878
879 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
880 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400881 /* Done here again so that even if phantom sw overwrote it,
882 * we set it */
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700883 if (adapter->init_port
884 && adapter->init_port(adapter, adapter->portnum) != 0) {
885 printk(KERN_ERR "%s: Failed to initialize port %d\n",
886 netxen_nic_driver_name, adapter->portnum);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700887 return -EIO;
888 }
dhananjay@netxen.com7eec5172007-07-25 20:13:11 +0530889 if (adapter->macaddr_set)
890 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700891
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700892 netxen_nic_set_link_parameters(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400893
894 netxen_nic_set_multi(netdev);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800895 if (adapter->set_mtu)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700896 adapter->set_mtu(adapter, netdev->mtu);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800897
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400898 if (!adapter->driver_mismatch)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700899 mod_timer(&adapter->watchdog_timer, jiffies);
900
901 napi_enable(&adapter->napi);
902 netxen_nic_enable_int(adapter);
903
904 if (!adapter->driver_mismatch)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400905 netif_start_queue(netdev);
906
907 return 0;
908}
909
910/*
911 * netxen_nic_close - Disables a network interface entry point
912 */
913static int netxen_nic_close(struct net_device *netdev)
914{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700915 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400916 int i, j;
917 struct netxen_cmd_buffer *cmd_buff;
918 struct netxen_skb_frag *buffrag;
919
920 netif_carrier_off(netdev);
921 netif_stop_queue(netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700922 napi_disable(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400923
dhananjay@netxen.com72b0a7a2007-12-26 10:23:56 -0800924 if (adapter->stop_port)
925 adapter->stop_port(adapter);
926
dhananjay@netxen.come01872af2007-07-25 20:13:12 +0530927 netxen_nic_disable_int(adapter);
928
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700929 cmd_buff = adapter->cmd_buf_arr;
930 for (i = 0; i < adapter->max_tx_desc_count; i++) {
931 buffrag = cmd_buff->frag_array;
932 if (buffrag->dma) {
933 pci_unmap_single(adapter->pdev, buffrag->dma,
934 buffrag->length, PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530935 buffrag->dma = 0ULL;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700936 }
937 for (j = 0; j < cmd_buff->frag_count; j++) {
938 buffrag++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400939 if (buffrag->dma) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700940 pci_unmap_page(adapter->pdev, buffrag->dma,
Jeff Garzik47906542007-11-23 21:23:36 -0500941 buffrag->length,
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700942 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530943 buffrag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400944 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400945 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700946 /* Free the skb we received in netxen_nic_xmit_frame */
947 if (cmd_buff->skb) {
948 dev_kfree_skb_any(cmd_buff->skb);
949 cmd_buff->skb = NULL;
950 }
951 cmd_buff++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400952 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530953 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
954 FLUSH_SCHEDULED_WORK();
955 del_timer_sync(&adapter->watchdog_timer);
956 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400957
958 return 0;
959}
960
961static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
962{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700963 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400964 struct netxen_hardware_context *hw = &adapter->ahw;
965 unsigned int first_seg_len = skb->len - skb->data_len;
966 struct netxen_skb_frag *buffrag;
967 unsigned int i;
968
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -0700969 u32 producer, consumer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400970 u32 saved_producer = 0;
971 struct cmd_desc_type0 *hwdesc;
972 int k;
973 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400974 int frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400975 int no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -0700976 u32 num_txd = adapter->max_tx_desc_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400977
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400978 frag_count = skb_shinfo(skb)->nr_frags + 1;
979
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400980 /* There 4 fragments per descriptor */
981 no_of_desc = (frag_count + 3) >> 2;
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800982 if (netdev->features & NETIF_F_TSO) {
983 if (skb_shinfo(skb)->gso_size > 0) {
984
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400985 no_of_desc++;
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -0700986 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300987 sizeof(struct ethhdr)) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800988 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800989 no_of_desc++;
990 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400991 }
992 }
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -0800993
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -0700994 producer = adapter->cmd_producer;
995 smp_mb();
996 consumer = adapter->last_cmd_consumer;
997 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
998 netif_stop_queue(netdev);
999 smp_mb();
1000 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001001 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001002
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001003 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001004 saved_producer = producer;
1005 hwdesc = &hw->cmd_desc_head[producer];
1006 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1007 /* Take skb->data itself */
1008 pbuf = &adapter->cmd_buf_arr[producer];
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001009 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
Al Viroa608ab9c2007-01-02 10:39:10 +00001010 pbuf->mss = skb_shinfo(skb)->gso_size;
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001011 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001012 } else {
1013 pbuf->mss = 0;
1014 hwdesc->mss = 0;
1015 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001016 pbuf->total_length = skb->len;
1017 pbuf->skb = skb;
1018 pbuf->cmd = TX_ETHER_PKT;
1019 pbuf->frag_count = frag_count;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001020 pbuf->port = adapter->portnum;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001021 buffrag = &pbuf->frag_array[0];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001022 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001023 PCI_DMA_TODEVICE);
1024 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001025 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1026 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1027 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001028
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001029 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001030 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001031 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1032 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1033
1034 for (i = 1, k = 1; i < frag_count; i++, k++) {
1035 struct skb_frag_struct *frag;
1036 int len, temp_len;
1037 unsigned long offset;
1038 dma_addr_t temp_dma;
1039
1040 /* move to next desc. if there is a need */
1041 if ((i & 0x3) == 0) {
1042 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001043 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001044 hwdesc = &hw->cmd_desc_head[producer];
1045 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001046 pbuf = &adapter->cmd_buf_arr[producer];
1047 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001048 }
1049 frag = &skb_shinfo(skb)->frags[i - 1];
1050 len = frag->size;
1051 offset = frag->page_offset;
1052
1053 temp_len = len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001054 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001055 len, PCI_DMA_TODEVICE);
1056
1057 buffrag++;
1058 buffrag->dma = temp_dma;
1059 buffrag->length = temp_len;
1060
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001061 switch (k) {
1062 case 0:
1063 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1064 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1065 break;
1066 case 1:
1067 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1068 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1069 break;
1070 case 2:
1071 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1072 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1073 break;
1074 case 3:
Al Viroa608ab9c2007-01-02 10:39:10 +00001075 hwdesc->buffer4_length = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001076 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1077 break;
1078 }
1079 frag++;
1080 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001081 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001082
1083 /* might change opcode to TX_TCP_LSO */
1084 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1085
1086 /* For LSO, we need to copy the MAC/IP/TCP headers into
1087 * the descriptor ring
1088 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001089 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1090 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001091 int hdr_len, first_hdr_len, more_hdr;
1092 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001093 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1094 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001095 more_hdr = 1;
1096 } else {
1097 first_hdr_len = hdr_len;
1098 more_hdr = 0;
1099 }
1100 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1101 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001102 pbuf = &adapter->cmd_buf_arr[producer];
1103 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001104
1105 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001106 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001107 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001108 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001109
1110 if (more_hdr) {
1111 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001112 pbuf = &adapter->cmd_buf_arr[producer];
1113 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001114 /* copy the next 64 bytes - should be enough except
1115 * for pathological case
1116 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001117 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1118 hwdesc,
1119 (hdr_len -
1120 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001121 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001122 }
1123 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001124
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001125 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001126 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001127
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001128 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001129
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001130 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001131 netdev->trans_start = jiffies;
1132
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001133 return NETDEV_TX_OK;
1134}
1135
1136static void netxen_watchdog(unsigned long v)
1137{
1138 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001139
1140 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001141}
1142
1143static void netxen_tx_timeout(struct net_device *netdev)
1144{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001145 struct netxen_adapter *adapter = (struct netxen_adapter *)
1146 netdev_priv(netdev);
1147 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001148}
1149
David Howells6d5aefb2006-12-05 19:36:26 +00001150static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001151{
Jeff Garzik47906542007-11-23 21:23:36 -05001152 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001153 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001154
1155 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001156 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001157
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001158 netxen_nic_disable_int(adapter);
1159 napi_disable(&adapter->napi);
1160
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001161 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001162
1163 napi_enable(&adapter->napi);
1164 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001165 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001166}
1167
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001168static inline void
1169netxen_handle_int(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001170{
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301171 netxen_nic_disable_int(adapter);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001172 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001173}
1174
Jeff Garzik1494a812006-11-07 05:12:16 -05001175irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001176{
Jeff Garzik1daec862007-10-19 03:14:03 -04001177 struct netxen_adapter *adapter = data;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301178 u32 our_int = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001179
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001180 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1181 /* not our interrupt */
1182 if ((our_int & (0x80 << adapter->portnum)) == 0)
1183 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301184
1185 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1186 /* claim interrupt */
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001187 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301188 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301189 }
1190
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001191 netxen_handle_int(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001192
1193 return IRQ_HANDLED;
1194}
1195
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001196irqreturn_t netxen_msi_intr(int irq, void *data)
1197{
1198 struct netxen_adapter *adapter = data;
1199
1200 netxen_handle_int(adapter);
1201 return IRQ_HANDLED;
1202}
1203
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001204static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001205{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001206 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001207 int tx_complete;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001208 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001209 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001210
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001211 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001212
Amit S. Kale80922fb2006-12-04 09:18:00 -08001213 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001214 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1215 /*
1216 * Fairness issue. This will give undue weight to the
1217 * receive context 0.
1218 */
1219
1220 /*
1221 * To avoid starvation, we give each of our receivers,
1222 * a fraction of the quota. Sometimes, it might happen that we
1223 * have enough quota to process every packet, but since all the
1224 * packets are on one context, it gets only half of the quota,
1225 * and ends up not processing it.
1226 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001227 work_done += netxen_process_rcv_ring(adapter, ctx,
1228 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001229 }
1230
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001231 if ((work_done < budget) && tx_complete) {
1232 netif_rx_complete(adapter->netdev, &adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001233 netxen_nic_enable_int(adapter);
1234 }
1235
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001236 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001237}
1238
1239#ifdef CONFIG_NET_POLL_CONTROLLER
1240static void netxen_nic_poll_controller(struct net_device *netdev)
1241{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001242 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001243 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001244 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001245 enable_irq(adapter->irq);
1246}
1247#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001248
1249static struct pci_driver netxen_driver = {
1250 .name = netxen_nic_driver_name,
1251 .id_table = netxen_pci_tbl,
1252 .probe = netxen_nic_probe,
1253 .remove = __devexit_p(netxen_nic_remove)
1254};
1255
1256/* Driver Registration on NetXen card */
1257
1258static int __init netxen_init_module(void)
1259{
Al Viro79ea13c2008-01-24 02:06:46 -08001260 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001261 return -ENOMEM;
1262
Amit S. Kale184231b2007-01-22 06:52:53 -08001263 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001264}
1265
1266module_init(netxen_init_module);
1267
1268static void __exit netxen_exit_module(void)
1269{
1270 /*
1271 * Wait for some time to allow the dma to drain, if any.
1272 */
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301273 msleep(100);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001274 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001275 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001276}
1277
1278module_exit(netxen_exit_module);