blob: 2341b89abc4d984f62523e231f36dfc0adb6154f [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);
66static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070067static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040068#ifdef CONFIG_NET_POLL_CONTROLLER
69static void netxen_nic_poll_controller(struct net_device *netdev);
70#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050071static irqreturn_t netxen_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
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080089struct workqueue_struct *netxen_workq;
90static void netxen_watchdog(unsigned long);
91
Adrian Bunk993fb902007-11-05 18:07:31 +010092static void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
93 uint32_t crb_producer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -070094{
95 switch (adapter->portnum) {
96 case 0:
97 writel(crb_producer, NETXEN_CRB_NORMALIZE
98 (adapter, CRB_CMD_PRODUCER_OFFSET));
99 return;
100 case 1:
101 writel(crb_producer, NETXEN_CRB_NORMALIZE
102 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
103 return;
104 case 2:
105 writel(crb_producer, NETXEN_CRB_NORMALIZE
106 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
107 return;
108 case 3:
109 writel(crb_producer, NETXEN_CRB_NORMALIZE
110 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
111 return;
112 default:
113 printk(KERN_WARNING "We tried to update "
114 "CRB_CMD_PRODUCER_OFFSET for invalid "
115 "PCI function id %d\n",
116 adapter->portnum);
117 return;
118 }
119}
120
Adrian Bunk993fb902007-11-05 18:07:31 +0100121static void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
122 u32 crb_consumer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700123{
124 switch (adapter->portnum) {
125 case 0:
126 writel(crb_consumer, NETXEN_CRB_NORMALIZE
127 (adapter, CRB_CMD_CONSUMER_OFFSET));
128 return;
129 case 1:
130 writel(crb_consumer, NETXEN_CRB_NORMALIZE
131 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
132 return;
133 case 2:
134 writel(crb_consumer, NETXEN_CRB_NORMALIZE
135 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
136 return;
137 case 3:
138 writel(crb_consumer, NETXEN_CRB_NORMALIZE
139 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
140 return;
141 default:
142 printk(KERN_WARNING "We tried to update "
143 "CRB_CMD_PRODUCER_OFFSET for invalid "
144 "PCI function id %d\n",
145 adapter->portnum);
146 return;
147 }
148}
149
150#define ADAPTER_LIST_SIZE 12
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700151
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700152static void netxen_nic_disable_int(struct netxen_adapter *adapter)
153{
154 uint32_t mask = 0x7ff;
155 int retries = 32;
156
157 DPRINTK(1, INFO, "Entered ISR Disable \n");
158
159 switch (adapter->portnum) {
160 case 0:
161 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
162 break;
163 case 1:
164 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
165 break;
166 case 2:
167 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
168 break;
169 case 3:
170 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
171 break;
172 }
173
174 if (adapter->intr_scheme != -1 &&
175 adapter->intr_scheme != INTR_SCHEME_PERPORT)
176 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
177
178 /* Window = 0 or 1 */
179 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
180 do {
181 writel(0xffffffff,
182 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
183 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
184 if (!(mask & 0x80))
185 break;
186 udelay(10);
187 } while (--retries);
188
189 if (!retries) {
190 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
191 netxen_nic_driver_name);
192 }
193 }
194
195 DPRINTK(1, INFO, "Done with Disable Int\n");
196}
197
198static void netxen_nic_enable_int(struct netxen_adapter *adapter)
199{
200 u32 mask;
201
202 DPRINTK(1, INFO, "Entered ISR Enable \n");
203
204 if (adapter->intr_scheme != -1 &&
205 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
206 switch (adapter->ahw.board_type) {
207 case NETXEN_NIC_GBE:
208 mask = 0x77b;
209 break;
210 case NETXEN_NIC_XGBE:
211 mask = 0x77f;
212 break;
213 default:
214 mask = 0x7ff;
215 break;
216 }
217
218 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
219 }
220
221 switch (adapter->portnum) {
222 case 0:
223 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
224 break;
225 case 1:
226 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
227 break;
228 case 2:
229 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
230 break;
231 case 3:
232 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
233 break;
234 }
235
236 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
237 mask = 0xbff;
238 if (adapter->intr_scheme != -1 &&
239 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
240 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
241 }
242 writel(mask,
243 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
244 }
245
246 DPRINTK(1, INFO, "Done with enable Int\n");
247}
248
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400249/*
250 * netxen_nic_probe()
251 *
252 * The Linux system will invoke this after identifying the vendor ID and
253 * device Id in the pci_tbl supported by this module.
254 *
255 * A quad port card has one operational PCI config space, (function 0),
256 * which is used to access all four ports.
257 *
258 * This routine will initialize the adapter, and setup the global parameters
259 * along with the port's specific structure.
260 */
261static int __devinit
262netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
263{
264 struct net_device *netdev = NULL;
265 struct netxen_adapter *adapter = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800266 void __iomem *mem_ptr0 = NULL;
267 void __iomem *mem_ptr1 = NULL;
268 void __iomem *mem_ptr2 = NULL;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700269 unsigned long first_page_group_end;
270 unsigned long first_page_group_start;
271
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800272
Al Viroa608ab9c2007-01-02 10:39:10 +0000273 u8 __iomem *db_ptr = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800274 unsigned long mem_base, mem_len, db_base, db_len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700275 int pci_using_dac, i = 0, err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400276 int ring;
277 struct netxen_recv_context *recv_ctx = NULL;
278 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
279 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
280 u64 mac_addr[FLASH_NUM_PORTS + 1];
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700281 int valid_mac = 0;
282 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700283 int pci_func_id = PCI_FUNC(pdev->devfn);
Joe Perches0795af52007-10-03 17:59:30 -0700284 DECLARE_MAC_BUF(mac);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400285
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800286 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700287
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700288 if (pdev->class != 0x020000) {
Joe Perches5bc51422007-11-19 17:48:24 -0800289 printk(KERN_ERR"NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700290 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800291 return -ENODEV;
292 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400293 if ((err = pci_enable_device(pdev)))
294 return err;
295 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
296 err = -ENODEV;
297 goto err_out_disable_pdev;
298 }
299
300 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
301 goto err_out_disable_pdev;
302
303 pci_set_master(pdev);
Auke Kok44c10132007-06-08 15:46:36 -0700304 if (pdev->revision == NX_P2_C1 &&
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800305 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
306 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400307 pci_using_dac = 1;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800308 } else {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400309 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
310 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
311 goto err_out_free_res;
312
313 pci_using_dac = 0;
314 }
315
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700316
317 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
318 if(!netdev) {
319 printk(KERN_ERR"%s: Failed to allocate memory for the "
320 "device block.Check system memory resource"
321 " usage.\n", netxen_nic_driver_name);
322 goto err_out_free_res;
323 }
324
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700325 SET_NETDEV_DEV(netdev, &pdev->dev);
326
327 adapter = netdev->priv;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700328
329 adapter->ahw.pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700330 adapter->ahw.pci_func = pci_func_id;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700331 spin_lock_init(&adapter->tx_lock);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700332
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400333 /* remap phys address */
334 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
335 mem_len = pci_resource_len(pdev, 0);
336
337 /* 128 Meg of memory */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700338 if (mem_len == NETXEN_PCI_128MB_SIZE) {
339 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
340 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
341 SECOND_PAGE_GROUP_SIZE);
342 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
343 THIRD_PAGE_GROUP_SIZE);
344 first_page_group_start = FIRST_PAGE_GROUP_START;
345 first_page_group_end = FIRST_PAGE_GROUP_END;
346 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
347 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
348 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
349 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
350 first_page_group_start = 0;
351 first_page_group_end = 0;
352 } else {
Jeff Garzik47906542007-11-23 21:23:36 -0500353 err = -EIO;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700354 goto err_out_free_netdev;
355 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800356
Al Viro79ea13c2008-01-24 02:06:46 -0800357 if ((!mem_ptr0 && mem_len == NETXEN_PCI_128MB_SIZE) ||
358 !mem_ptr1 || !mem_ptr2) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800359 DPRINTK(ERR,
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800360 "Cannot remap adapter memory aborting.:"
361 "0 -> %p, 1 -> %p, 2 -> %p\n",
362 mem_ptr0, mem_ptr1, mem_ptr2);
363
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400364 err = -EIO;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800365 goto err_out_iounmap;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400366 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800367 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
368 db_len = pci_resource_len(pdev, 4);
369
370 if (db_len == 0) {
371 printk(KERN_ERR "%s: doorbell is disabled\n",
372 netxen_nic_driver_name);
373 err = -EIO;
374 goto err_out_iounmap;
375 }
376 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
377 db_len);
378
379 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
Al Viroa608ab9c2007-01-02 10:39:10 +0000380 if (!db_ptr) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800381 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
382 netxen_nic_driver_name);
383 err = -EIO;
384 goto err_out_iounmap;
385 }
386 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400387
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700388 adapter->ahw.pci_base0 = mem_ptr0;
389 adapter->ahw.first_page_group_start = first_page_group_start;
390 adapter->ahw.first_page_group_end = first_page_group_end;
391 adapter->ahw.pci_base1 = mem_ptr1;
392 adapter->ahw.pci_base2 = mem_ptr2;
393 adapter->ahw.db_base = db_ptr;
394 adapter->ahw.db_len = db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400395
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700396 adapter->netdev = netdev;
397 adapter->pdev = pdev;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530398
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700399 netif_napi_add(netdev, &adapter->napi,
400 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
401
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530402 /* this will be read from FW later */
403 adapter->intr_scheme = -1;
404
405 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700406 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530407 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530408 adapter->rx_csum = 1;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700409
410 netdev->open = netxen_nic_open;
411 netdev->stop = netxen_nic_close;
412 netdev->hard_start_xmit = netxen_nic_xmit_frame;
Jeff Garzik47906542007-11-23 21:23:36 -0500413 netdev->get_stats = netxen_nic_get_stats;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700414 netdev->set_multicast_list = netxen_nic_set_multi;
415 netdev->set_mac_address = netxen_nic_set_mac;
416 netdev->change_mtu = netxen_nic_change_mtu;
417 netdev->tx_timeout = netxen_tx_timeout;
418 netdev->watchdog_timeo = HZ;
419
420 netxen_nic_change_mtu(netdev, netdev->mtu);
421
422 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700423#ifdef CONFIG_NET_POLL_CONTROLLER
424 netdev->poll_controller = netxen_nic_poll_controller;
425#endif
426 /* ScatterGather support */
427 netdev->features = NETIF_F_SG;
428 netdev->features |= NETIF_F_IP_CSUM;
429 netdev->features |= NETIF_F_TSO;
430
431 if (pci_using_dac)
432 netdev->features |= NETIF_F_HIGHDMA;
433
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530434 if (pci_enable_msi(pdev))
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700435 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530436 else
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700437 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
438
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700439 netdev->irq = pdev->irq;
440 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
441
442 /*
443 * Set the CRB window to invalid. If any register in window 0 is
444 * accessed it should set the window to 0 and then reset it to 1.
445 */
446 adapter->curr_window = 255;
447
448 /* initialize the adapter */
449 netxen_initialize_adapter_hw(adapter);
450
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700451 /*
452 * Adapter in our case is quad port so initialize it before
453 * initializing the ports
454 */
455
456 netxen_initialize_adapter_ops(adapter);
457
458 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
459 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
Jeff Garzik47906542007-11-23 21:23:36 -0500460 (adapter->ahw.boardcfg.board_type ==
461 NETXEN_BRDTYPE_P2_SB31_2G))
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700462 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
463 else
464 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
465 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
466 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
467
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400468 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
469 if (cmd_buf_arr == NULL) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800470 printk(KERN_ERR
471 "%s: Could not allocate cmd_buf_arr memory:%d\n",
472 netxen_nic_driver_name, (int)TX_RINGSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400473 err = -ENOMEM;
474 goto err_out_free_adapter;
475 }
476 memset(cmd_buf_arr, 0, TX_RINGSIZE);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700477 adapter->cmd_buf_arr = cmd_buf_arr;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400478
479 for (i = 0; i < MAX_RCV_CTX; ++i) {
480 recv_ctx = &adapter->recv_ctx[i];
481 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
482 rcv_desc = &recv_ctx->rcv_desc[ring];
483 switch (RCV_DESC_TYPE(ring)) {
484 case RCV_DESC_NORMAL:
485 rcv_desc->max_rx_desc_count =
486 adapter->max_rx_desc_count;
487 rcv_desc->flags = RCV_DESC_NORMAL;
488 rcv_desc->dma_size = RX_DMA_MAP_LEN;
489 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
490 break;
491
492 case RCV_DESC_JUMBO:
493 rcv_desc->max_rx_desc_count =
494 adapter->max_jumbo_rx_desc_count;
495 rcv_desc->flags = RCV_DESC_JUMBO;
496 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
497 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
498 break;
499
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800500 case RCV_RING_LRO:
501 rcv_desc->max_rx_desc_count =
502 adapter->max_lro_rx_desc_count;
503 rcv_desc->flags = RCV_DESC_LRO;
504 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
505 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
506 break;
507
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400508 }
509 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
510 vmalloc(RCV_BUFFSIZE);
511
512 if (rcv_desc->rx_buf_arr == NULL) {
Joe Perches5bc51422007-11-19 17:48:24 -0800513 printk(KERN_ERR "%s: Could not allocate "
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800514 "rcv_desc->rx_buf_arr memory:%d\n",
515 netxen_nic_driver_name,
516 (int)RCV_BUFFSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400517 err = -ENOMEM;
518 goto err_out_free_rx_buffer;
519 }
520 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
521 }
522
523 }
524
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800525 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700526
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700527 /* Mezz cards have PCI function 0,2,3 enabled */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700528 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB31_10G_IMEZ)
529 && (pci_func_id >= 2))
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700530 adapter->portnum = pci_func_id - 2;
531
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800532#ifdef CONFIG_IA64
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700533 if(adapter->portnum == 0) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700534 netxen_pinit_from_rom(adapter, 0);
535 udelay(500);
536 netxen_load_firmware(adapter);
537 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800538#endif
539
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400540 init_timer(&adapter->watchdog_timer);
541 adapter->ahw.xg_linkup = 0;
542 adapter->watchdog_timer.function = &netxen_watchdog;
543 adapter->watchdog_timer.data = (unsigned long)adapter;
David Howells6d5aefb2006-12-05 19:36:26 +0000544 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400545 adapter->ahw.pdev = pdev;
546 adapter->proc_cmd_buf_counter = 0;
Auke Kok44c10132007-06-08 15:46:36 -0700547 adapter->ahw.revision_id = pdev->revision;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400548
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700549 /* make sure Window == 1 */
550 netxen_nic_pci_change_crbwindow(adapter, 1);
551
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700552 netxen_nic_update_cmd_producer(adapter, 0);
553 netxen_nic_update_cmd_consumer(adapter, 0);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700554 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400555
556 if (netxen_is_flash_supported(adapter) == 0 &&
557 netxen_get_flash_mac_addr(adapter, mac_addr) == 0)
558 valid_mac = 1;
559 else
560 valid_mac = 0;
561
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700562 if (valid_mac) {
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700563 unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700564 netdev->dev_addr[0] = *(p + 5);
565 netdev->dev_addr[1] = *(p + 4);
566 netdev->dev_addr[2] = *(p + 3);
567 netdev->dev_addr[3] = *(p + 2);
568 netdev->dev_addr[4] = *(p + 1);
569 netdev->dev_addr[5] = *(p + 0);
570
571 memcpy(netdev->perm_addr, netdev->dev_addr,
572 netdev->addr_len);
573 if (!is_valid_ether_addr(netdev->perm_addr)) {
Joe Perches0795af52007-10-03 17:59:30 -0700574 printk(KERN_ERR "%s: Bad MAC address %s.\n",
575 netxen_nic_driver_name,
576 print_mac(mac, netdev->dev_addr));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700577 } else {
578 if (adapter->macaddr_set)
579 adapter->macaddr_set(adapter,
580 netdev->dev_addr);
581 }
582 }
583
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700584 if (adapter->portnum == 0) {
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700585 err = netxen_initialize_adapter_offload(adapter);
Jeff Garzik47906542007-11-23 21:23:36 -0500586 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700587 goto err_out_free_rx_buffer;
Jeff Garzik47906542007-11-23 21:23:36 -0500588 val = readl(NETXEN_CRB_NORMALIZE(adapter,
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700589 NETXEN_CAM_RAM(0x1fc)));
590 if (val == 0x55555555) {
591 /* This is the first boot after power up */
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530592 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530593 if (!(val & 0x4)) {
594 val |= 0x4;
595 netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val);
596 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val);
597 if (!(val & 0x4))
598 printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n",
599 netxen_nic_driver_name);
600 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700601 val = readl(NETXEN_CRB_NORMALIZE(adapter,
602 NETXEN_ROMUSB_GLB_SW_RESET));
603 printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val);
604 if (val != 0x80000f) {
605 /* clear the register for future unloads/loads */
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530606 writel(0, NETXEN_CRB_NORMALIZE(adapter,
607 NETXEN_CAM_RAM(0x1fc)));
608 printk(KERN_ERR "ERROR in NetXen HW init sequence.\n");
609 err = -ENODEV;
610 goto err_out_free_dev;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700611 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700612 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530613
614 /* clear the register for future unloads/loads */
615 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700616 printk(KERN_INFO "State: 0x%0x\n",
617 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
618
619 /*
620 * Tell the hardware our version number.
621 */
Jeff Garzik47906542007-11-23 21:23:36 -0500622 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700623 | ((_NETXEN_NIC_LINUX_MINOR << 8))
624 | (_NETXEN_NIC_LINUX_SUBVERSION);
625 writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION));
626
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700627 /* Unlock the HW, prompting the boot sequence */
628 writel(1,
629 NETXEN_CRB_NORMALIZE(adapter,
630 NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
631 /* Handshake with the card before we register the devices. */
632 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800633 }
634
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400635 /*
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700636 * See if the firmware gave us a virtual-physical port mapping.
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400637 */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700638 i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum)));
639 if (i != 0x55555555)
640 physical_port[adapter->portnum] = i;
641
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700642 netif_carrier_off(netdev);
643 netif_stop_queue(netdev);
644
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700645 if ((err = register_netdev(netdev))) {
646 printk(KERN_ERR "%s: register_netdev failed port #%d"
647 " aborting\n", netxen_nic_driver_name,
648 adapter->portnum);
649 err = -EIO;
650 goto err_out_free_dev;
651 }
652
653 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400654
655 switch (adapter->ahw.board_type) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700656 case NETXEN_NIC_GBE:
657 printk(KERN_INFO "%s: QUAD GbE board initialized\n",
658 netxen_nic_driver_name);
659 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400660
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700661 case NETXEN_NIC_XGBE:
Jeff Garzik47906542007-11-23 21:23:36 -0500662 printk(KERN_INFO "%s: XGbE board initialized\n",
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700663 netxen_nic_driver_name);
664 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400665 }
666
667 adapter->driver_mismatch = 0;
668
669 return 0;
670
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700671err_out_free_dev:
672 if (adapter->portnum == 0)
673 netxen_free_adapter_offload(adapter);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700674
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700675err_out_free_rx_buffer:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400676 for (i = 0; i < MAX_RCV_CTX; ++i) {
677 recv_ctx = &adapter->recv_ctx[i];
678 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
679 rcv_desc = &recv_ctx->rcv_desc[ring];
680 if (rcv_desc->rx_buf_arr != NULL) {
681 vfree(rcv_desc->rx_buf_arr);
682 rcv_desc->rx_buf_arr = NULL;
683 }
684 }
685 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400686 vfree(cmd_buf_arr);
687
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700688err_out_free_adapter:
689 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
690 pci_disable_msi(pdev);
691
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400692 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400693
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800694 if (db_ptr)
695 iounmap(db_ptr);
696
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700697err_out_iounmap:
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800698 if (mem_ptr0)
699 iounmap(mem_ptr0);
700 if (mem_ptr1)
701 iounmap(mem_ptr1);
702 if (mem_ptr2)
703 iounmap(mem_ptr2);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800704
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700705err_out_free_netdev:
706 free_netdev(netdev);
707
708err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400709 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700710
711err_out_disable_pdev:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400712 pci_disable_device(pdev);
713 return err;
714}
715
716static void __devexit netxen_nic_remove(struct pci_dev *pdev)
717{
718 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700719 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400720 struct netxen_rx_buffer *buffer;
721 struct netxen_recv_context *recv_ctx;
722 struct netxen_rcv_desc_ctx *rcv_desc;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530723 int i, ctxid, ring;
724 static int init_firmware_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400725
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700726 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400727 if (adapter == NULL)
728 return;
729
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700730 netdev = adapter->netdev;
731
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530732 unregister_netdev(netdev);
733
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530734 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
735 init_firmware_done++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400736 netxen_free_hw_resources(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530737 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400738
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400739 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
740 recv_ctx = &adapter->recv_ctx[ctxid];
741 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
742 rcv_desc = &recv_ctx->rcv_desc[ring];
743 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
744 buffer = &(rcv_desc->rx_buf_arr[i]);
745 if (buffer->state == NETXEN_BUFFER_FREE)
746 continue;
747 pci_unmap_single(pdev, buffer->dma,
748 rcv_desc->dma_size,
749 PCI_DMA_FROMDEVICE);
750 if (buffer->skb != NULL)
751 dev_kfree_skb_any(buffer->skb);
752 }
753 vfree(rcv_desc->rx_buf_arr);
754 }
755 }
756
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700757 vfree(adapter->cmd_buf_arr);
758
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530759 if (adapter->portnum == 0) {
760 if (init_firmware_done) {
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530761 i = 100;
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530762 do {
763 if (dma_watchdog_shutdown_request(adapter) == 1)
764 break;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530765 msleep(100);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530766 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
767 break;
768 } while (--i);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530769
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530770 if (i == 0)
771 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
772 netdev->name);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530773
774 /* clear the register for future unloads/loads */
775 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
776 printk(KERN_INFO "State: 0x%0x\n",
777 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
778
779 /* leave the hw in the same state as reboot */
780 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530781 netxen_pinit_from_rom(adapter, 0);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530782 msleep(1);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530783 netxen_load_firmware(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530784 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
785 }
786
787 /* clear the register for future unloads/loads */
788 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
789 printk(KERN_INFO "State: 0x%0x\n",
790 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
791
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530792 i = 100;
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530793 do {
794 if (dma_watchdog_shutdown_request(adapter) == 1)
795 break;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530796 msleep(100);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530797 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
798 break;
799 } while (--i);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530800
801 if (i) {
802 netxen_free_adapter_offload(adapter);
803 } else {
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530804 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
805 netdev->name);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530806 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530807 }
808
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530809 if (adapter->irq)
810 free_irq(adapter->irq, adapter);
811
812 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
813 pci_disable_msi(pdev);
814
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700815 iounmap(adapter->ahw.db_base);
816 iounmap(adapter->ahw.pci_base0);
817 iounmap(adapter->ahw.pci_base1);
818 iounmap(adapter->ahw.pci_base2);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700819
820 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530821 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700822 pci_set_drvdata(pdev, NULL);
823
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700824 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400825}
826
827/*
828 * Called when a network interface is made active
829 * @returns 0 on success, negative value on failure
830 */
831static int netxen_nic_open(struct net_device *netdev)
832{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700833 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400834 int err = 0;
835 int ctx, ring;
836
837 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
838 err = netxen_init_firmware(adapter);
839 if (err != 0) {
840 printk(KERN_ERR "Failed to init firmware\n");
841 return -EIO;
842 }
843 netxen_nic_flash_print(adapter);
844
845 /* setup all the resources for the Phantom... */
846 /* this include the descriptors for rcv, tx, and status */
847 netxen_nic_clear_stats(adapter);
848 err = netxen_nic_hw_resources(adapter);
849 if (err) {
850 printk(KERN_ERR "Error in setting hw resources:%d\n",
851 err);
852 return err;
853 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400854 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800855 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400856 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400857 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800858 adapter->irq = adapter->ahw.pdev->irq;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700859 err = request_irq(adapter->ahw.pdev->irq, netxen_intr,
Andrew Morton1c8e0392007-05-10 22:53:00 -0700860 IRQF_SHARED|IRQF_SAMPLE_RANDOM, netdev->name,
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700861 adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400862 if (err) {
863 printk(KERN_ERR "request_irq failed with: %d\n", err);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800864 netxen_free_hw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400865 return err;
866 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800867
868 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
869 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700870 if (!adapter->driver_mismatch)
871 mod_timer(&adapter->watchdog_timer, jiffies);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400872
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700873 napi_enable(&adapter->napi);
874
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700875 netxen_nic_enable_int(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400876
877 /* Done here again so that even if phantom sw overwrote it,
878 * we set it */
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700879 if (adapter->init_port
880 && adapter->init_port(adapter, adapter->portnum) != 0) {
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700881 del_timer_sync(&adapter->watchdog_timer);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700882 printk(KERN_ERR "%s: Failed to initialize port %d\n",
883 netxen_nic_driver_name, adapter->portnum);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700884 napi_disable(&adapter->napi);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700885 return -EIO;
886 }
dhananjay@netxen.com7eec5172007-07-25 20:13:11 +0530887 if (adapter->macaddr_set)
888 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700889
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700890 netxen_nic_set_link_parameters(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400891
892 netxen_nic_set_multi(netdev);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800893 if (adapter->set_mtu)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700894 adapter->set_mtu(adapter, netdev->mtu);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800895
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400896 if (!adapter->driver_mismatch)
897 netif_start_queue(netdev);
898
899 return 0;
900}
901
902/*
903 * netxen_nic_close - Disables a network interface entry point
904 */
905static int netxen_nic_close(struct net_device *netdev)
906{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700907 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400908 int i, j;
909 struct netxen_cmd_buffer *cmd_buff;
910 struct netxen_skb_frag *buffrag;
911
912 netif_carrier_off(netdev);
913 netif_stop_queue(netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700914 napi_disable(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400915
dhananjay@netxen.com72b0a7a2007-12-26 10:23:56 -0800916 if (adapter->stop_port)
917 adapter->stop_port(adapter);
918
dhananjay@netxen.come01872af2007-07-25 20:13:12 +0530919 netxen_nic_disable_int(adapter);
920
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700921 cmd_buff = adapter->cmd_buf_arr;
922 for (i = 0; i < adapter->max_tx_desc_count; i++) {
923 buffrag = cmd_buff->frag_array;
924 if (buffrag->dma) {
925 pci_unmap_single(adapter->pdev, buffrag->dma,
926 buffrag->length, PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530927 buffrag->dma = 0ULL;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700928 }
929 for (j = 0; j < cmd_buff->frag_count; j++) {
930 buffrag++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400931 if (buffrag->dma) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700932 pci_unmap_page(adapter->pdev, buffrag->dma,
Jeff Garzik47906542007-11-23 21:23:36 -0500933 buffrag->length,
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700934 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530935 buffrag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400936 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400937 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700938 /* Free the skb we received in netxen_nic_xmit_frame */
939 if (cmd_buff->skb) {
940 dev_kfree_skb_any(cmd_buff->skb);
941 cmd_buff->skb = NULL;
942 }
943 cmd_buff++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400944 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530945 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
946 FLUSH_SCHEDULED_WORK();
947 del_timer_sync(&adapter->watchdog_timer);
948 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400949
950 return 0;
951}
952
953static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
954{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700955 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400956 struct netxen_hardware_context *hw = &adapter->ahw;
957 unsigned int first_seg_len = skb->len - skb->data_len;
958 struct netxen_skb_frag *buffrag;
959 unsigned int i;
960
961 u32 producer = 0;
962 u32 saved_producer = 0;
963 struct cmd_desc_type0 *hwdesc;
964 int k;
965 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400966 static int dropped_packet = 0;
967 int frag_count;
968 u32 local_producer = 0;
969 u32 max_tx_desc_count = 0;
970 u32 last_cmd_consumer = 0;
971 int no_of_desc;
972
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700973 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400974 frag_count = skb_shinfo(skb)->nr_frags + 1;
975
976 if (unlikely(skb->len <= 0)) {
977 dev_kfree_skb_any(skb);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700978 adapter->stats.badskblen++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400979 return NETDEV_TX_OK;
980 }
981
982 if (frag_count > MAX_BUFFERS_PER_CMD) {
Joe Perches5bc51422007-11-19 17:48:24 -0800983 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d) "
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400984 "too large, can handle only %d frags\n",
985 netxen_nic_driver_name, netdev->name,
986 frag_count, MAX_BUFFERS_PER_CMD);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700987 adapter->stats.txdropped++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400988 if ((++dropped_packet & 0xff) == 0xff)
989 printk("%s: %s droppped packets = %d\n",
990 netxen_nic_driver_name, netdev->name,
991 dropped_packet);
992
993 return NETDEV_TX_OK;
994 }
995
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400996 /* There 4 fragments per descriptor */
997 no_of_desc = (frag_count + 3) >> 2;
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800998 if (netdev->features & NETIF_F_TSO) {
999 if (skb_shinfo(skb)->gso_size > 0) {
1000
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001001 no_of_desc++;
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001002 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001003 sizeof(struct ethhdr)) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001004 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001005 no_of_desc++;
1006 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001007 }
1008 }
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001009
1010 spin_lock_bh(&adapter->tx_lock);
1011 if (adapter->total_threads >= MAX_XMIT_PRODUCERS) {
1012 goto out_requeue;
1013 }
1014 local_producer = adapter->cmd_producer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001015 k = adapter->cmd_producer;
1016 max_tx_desc_count = adapter->max_tx_desc_count;
1017 last_cmd_consumer = adapter->last_cmd_consumer;
1018 if ((k + no_of_desc) >=
1019 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
1020 last_cmd_consumer)) {
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001021 goto out_requeue;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001022 }
1023 k = get_index_range(k, max_tx_desc_count, no_of_desc);
1024 adapter->cmd_producer = k;
1025 adapter->total_threads++;
1026 adapter->num_threads++;
1027
1028 spin_unlock_bh(&adapter->tx_lock);
1029 /* Copy the descriptors into the hardware */
1030 producer = local_producer;
1031 saved_producer = producer;
1032 hwdesc = &hw->cmd_desc_head[producer];
1033 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1034 /* Take skb->data itself */
1035 pbuf = &adapter->cmd_buf_arr[producer];
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001036 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
Al Viroa608ab9c2007-01-02 10:39:10 +00001037 pbuf->mss = skb_shinfo(skb)->gso_size;
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001038 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001039 } else {
1040 pbuf->mss = 0;
1041 hwdesc->mss = 0;
1042 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001043 pbuf->total_length = skb->len;
1044 pbuf->skb = skb;
1045 pbuf->cmd = TX_ETHER_PKT;
1046 pbuf->frag_count = frag_count;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001047 pbuf->port = adapter->portnum;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001048 buffrag = &pbuf->frag_array[0];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001049 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001050 PCI_DMA_TODEVICE);
1051 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001052 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1053 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1054 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001055
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001056 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001057 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001058 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1059 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1060
1061 for (i = 1, k = 1; i < frag_count; i++, k++) {
1062 struct skb_frag_struct *frag;
1063 int len, temp_len;
1064 unsigned long offset;
1065 dma_addr_t temp_dma;
1066
1067 /* move to next desc. if there is a need */
1068 if ((i & 0x3) == 0) {
1069 k = 0;
1070 producer = get_next_index(producer,
1071 adapter->max_tx_desc_count);
1072 hwdesc = &hw->cmd_desc_head[producer];
1073 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001074 pbuf = &adapter->cmd_buf_arr[producer];
1075 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001076 }
1077 frag = &skb_shinfo(skb)->frags[i - 1];
1078 len = frag->size;
1079 offset = frag->page_offset;
1080
1081 temp_len = len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001082 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001083 len, PCI_DMA_TODEVICE);
1084
1085 buffrag++;
1086 buffrag->dma = temp_dma;
1087 buffrag->length = temp_len;
1088
1089 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
1090 switch (k) {
1091 case 0:
1092 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1093 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1094 break;
1095 case 1:
1096 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1097 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1098 break;
1099 case 2:
1100 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1101 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1102 break;
1103 case 3:
Al Viroa608ab9c2007-01-02 10:39:10 +00001104 hwdesc->buffer4_length = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001105 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1106 break;
1107 }
1108 frag++;
1109 }
1110 producer = get_next_index(producer, adapter->max_tx_desc_count);
1111
1112 /* might change opcode to TX_TCP_LSO */
1113 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1114
1115 /* For LSO, we need to copy the MAC/IP/TCP headers into
1116 * the descriptor ring
1117 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001118 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1119 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001120 int hdr_len, first_hdr_len, more_hdr;
1121 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001122 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1123 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001124 more_hdr = 1;
1125 } else {
1126 first_hdr_len = hdr_len;
1127 more_hdr = 0;
1128 }
1129 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1130 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001131 pbuf = &adapter->cmd_buf_arr[producer];
1132 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001133
1134 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001135 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001136 (void *)(skb->data), first_hdr_len);
1137 producer = get_next_index(producer, max_tx_desc_count);
1138
1139 if (more_hdr) {
1140 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001141 pbuf = &adapter->cmd_buf_arr[producer];
1142 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001143 /* copy the next 64 bytes - should be enough except
1144 * for pathological case
1145 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001146 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1147 hwdesc,
1148 (hdr_len -
1149 first_hdr_len));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001150 producer = get_next_index(producer, max_tx_desc_count);
1151 }
1152 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001153
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001154 spin_lock_bh(&adapter->tx_lock);
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001155 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001156
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001157 /* Code to update the adapter considering how many producer threads
1158 are currently working */
1159 if ((--adapter->num_threads) == 0) {
1160 /* This is the last thread */
1161 u32 crb_producer = adapter->cmd_producer;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001162 netxen_nic_update_cmd_producer(adapter, crb_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001163 wmb();
1164 adapter->total_threads = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001165 }
1166
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001167 adapter->stats.xmitfinished++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001168 netdev->trans_start = jiffies;
1169
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001170 spin_unlock_bh(&adapter->tx_lock);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001171 return NETDEV_TX_OK;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001172
1173out_requeue:
1174 netif_stop_queue(netdev);
1175 adapter->flags |= NETXEN_NETDEV_STATUS;
1176
1177 spin_unlock_bh(&adapter->tx_lock);
1178 return NETDEV_TX_BUSY;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001179}
1180
1181static void netxen_watchdog(unsigned long v)
1182{
1183 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001184
1185 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001186}
1187
1188static void netxen_tx_timeout(struct net_device *netdev)
1189{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001190 struct netxen_adapter *adapter = (struct netxen_adapter *)
1191 netdev_priv(netdev);
1192 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001193}
1194
David Howells6d5aefb2006-12-05 19:36:26 +00001195static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001196{
Jeff Garzik47906542007-11-23 21:23:36 -05001197 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001198 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001199
1200 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001201 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001202
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001203 netxen_nic_close(adapter->netdev);
1204 netxen_nic_open(adapter->netdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001205 adapter->netdev->trans_start = jiffies;
1206 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001207}
1208
1209static int
1210netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
1211{
1212 u32 ret = 0;
1213
1214 DPRINTK(INFO, "Entered handle ISR\n");
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001215 adapter->stats.ints++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001216
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301217 netxen_nic_disable_int(adapter);
1218
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001219 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001220 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001221 /*
1222 * Interrupts are already disabled.
1223 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001224 __netif_rx_schedule(netdev, &adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001225 } else {
1226 static unsigned int intcount = 0;
1227 if ((++intcount & 0xfff) == 0xfff)
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301228 DPRINTK(KERN_ERR
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001229 "%s: %s interrupt %d while in poll\n",
1230 netxen_nic_driver_name, netdev->name,
1231 intcount);
1232 }
1233 ret = 1;
1234 }
1235
1236 if (ret == 0) {
1237 netxen_nic_enable_int(adapter);
1238 }
1239
1240 return ret;
1241}
1242
1243/*
1244 * netxen_intr - Interrupt Handler
1245 * @irq: interrupt number
1246 * data points to adapter stucture (which may be handling more than 1 port
1247 */
Jeff Garzik1494a812006-11-07 05:12:16 -05001248irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001249{
Jeff Garzik1daec862007-10-19 03:14:03 -04001250 struct netxen_adapter *adapter = data;
1251 struct net_device *netdev = adapter->netdev;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301252 u32 our_int = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001253
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301254 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1255 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1256 /* not our interrupt */
1257 if ((our_int & (0x80 << adapter->portnum)) == 0)
1258 return IRQ_NONE;
1259 }
1260
1261 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1262 /* claim interrupt */
1263 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1264 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1265 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1266 }
1267 }
1268
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001269 if (netif_running(netdev))
1270 netxen_handle_int(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001271
1272 return IRQ_HANDLED;
1273}
1274
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001275static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001276{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001277 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1278 struct net_device *netdev = adapter->netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001279 int done = 1;
1280 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001281 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001282
1283 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001284
Amit S. Kale80922fb2006-12-04 09:18:00 -08001285 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001286 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1287 /*
1288 * Fairness issue. This will give undue weight to the
1289 * receive context 0.
1290 */
1291
1292 /*
1293 * To avoid starvation, we give each of our receivers,
1294 * a fraction of the quota. Sometimes, it might happen that we
1295 * have enough quota to process every packet, but since all the
1296 * packets are on one context, it gets only half of the quota,
1297 * and ends up not processing it.
1298 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001299 work_done += netxen_process_rcv_ring(adapter, ctx,
1300 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001301 }
1302
David S. Miller17062872008-01-07 20:51:29 -08001303 if (work_done >= budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001304 done = 0;
1305
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001306 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1307 done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001308
1309 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
Amit S. Kale80922fb2006-12-04 09:18:00 -08001310 work_done, work_to_do);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001311 if (done) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001312 netif_rx_complete(netdev, napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001313 netxen_nic_enable_int(adapter);
1314 }
1315
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001316 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001317}
1318
1319#ifdef CONFIG_NET_POLL_CONTROLLER
1320static void netxen_nic_poll_controller(struct net_device *netdev)
1321{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001322 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001323 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001324 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001325 enable_irq(adapter->irq);
1326}
1327#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001328
1329static struct pci_driver netxen_driver = {
1330 .name = netxen_nic_driver_name,
1331 .id_table = netxen_pci_tbl,
1332 .probe = netxen_nic_probe,
1333 .remove = __devexit_p(netxen_nic_remove)
1334};
1335
1336/* Driver Registration on NetXen card */
1337
1338static int __init netxen_init_module(void)
1339{
Al Viro79ea13c2008-01-24 02:06:46 -08001340 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001341 return -ENOMEM;
1342
Amit S. Kale184231b2007-01-22 06:52:53 -08001343 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001344}
1345
1346module_init(netxen_init_module);
1347
1348static void __exit netxen_exit_module(void)
1349{
1350 /*
1351 * Wait for some time to allow the dma to drain, if any.
1352 */
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301353 msleep(100);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001354 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001355 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001356}
1357
1358module_exit(netxen_exit_module);