blob: 2a1d6d7ec351d37f4a7189f2e3987d76a5a8990c [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
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080056#define DMA_32BIT_MASK 0x00000000ffffffffULL
57#define DMA_35BIT_MASK 0x00000007ffffffffULL
58
Amit S. Kale3d396eb2006-10-21 15:33:03 -040059/* Local functions to NetXen NIC driver */
60static int __devinit netxen_nic_probe(struct pci_dev *pdev,
61 const struct pci_device_id *ent);
62static void __devexit netxen_nic_remove(struct pci_dev *pdev);
63static int netxen_nic_open(struct net_device *netdev);
64static int netxen_nic_close(struct net_device *netdev);
65static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
66static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000067static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040068static void netxen_watchdog(unsigned long);
69static int netxen_handle_int(struct netxen_adapter *, struct net_device *);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070070static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040071#ifdef CONFIG_NET_POLL_CONTROLLER
72static void netxen_nic_poll_controller(struct net_device *netdev);
73#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050074static irqreturn_t netxen_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040075
Mithlesh Thukral6c80b182007-04-20 07:55:26 -070076int physical_port[] = {0, 1, 2, 3};
77
Amit S. Kale3d396eb2006-10-21 15:33:03 -040078/* PCI Device ID Table */
79static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
80 {PCI_DEVICE(0x4040, 0x0001)},
81 {PCI_DEVICE(0x4040, 0x0002)},
82 {PCI_DEVICE(0x4040, 0x0003)},
83 {PCI_DEVICE(0x4040, 0x0004)},
84 {PCI_DEVICE(0x4040, 0x0005)},
Amit S. Kale0d390732006-12-18 05:50:59 -080085 {PCI_DEVICE(0x4040, 0x0024)},
86 {PCI_DEVICE(0x4040, 0x0025)},
Amit S. Kale3d396eb2006-10-21 15:33:03 -040087 {0,}
88};
89
90MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
91
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080092struct workqueue_struct *netxen_workq;
93static void netxen_watchdog(unsigned long);
94
Mithlesh Thukral3176ff32007-04-20 07:52:37 -070095static inline void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
96 uint32_t crb_producer)
97{
98 switch (adapter->portnum) {
99 case 0:
100 writel(crb_producer, NETXEN_CRB_NORMALIZE
101 (adapter, CRB_CMD_PRODUCER_OFFSET));
102 return;
103 case 1:
104 writel(crb_producer, NETXEN_CRB_NORMALIZE
105 (adapter, CRB_CMD_PRODUCER_OFFSET_1));
106 return;
107 case 2:
108 writel(crb_producer, NETXEN_CRB_NORMALIZE
109 (adapter, CRB_CMD_PRODUCER_OFFSET_2));
110 return;
111 case 3:
112 writel(crb_producer, NETXEN_CRB_NORMALIZE
113 (adapter, CRB_CMD_PRODUCER_OFFSET_3));
114 return;
115 default:
116 printk(KERN_WARNING "We tried to update "
117 "CRB_CMD_PRODUCER_OFFSET for invalid "
118 "PCI function id %d\n",
119 adapter->portnum);
120 return;
121 }
122}
123
124static inline void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
125 u32 crb_consumer)
126{
127 switch (adapter->portnum) {
128 case 0:
129 writel(crb_consumer, NETXEN_CRB_NORMALIZE
130 (adapter, CRB_CMD_CONSUMER_OFFSET));
131 return;
132 case 1:
133 writel(crb_consumer, NETXEN_CRB_NORMALIZE
134 (adapter, CRB_CMD_CONSUMER_OFFSET_1));
135 return;
136 case 2:
137 writel(crb_consumer, NETXEN_CRB_NORMALIZE
138 (adapter, CRB_CMD_CONSUMER_OFFSET_2));
139 return;
140 case 3:
141 writel(crb_consumer, NETXEN_CRB_NORMALIZE
142 (adapter, CRB_CMD_CONSUMER_OFFSET_3));
143 return;
144 default:
145 printk(KERN_WARNING "We tried to update "
146 "CRB_CMD_PRODUCER_OFFSET for invalid "
147 "PCI function id %d\n",
148 adapter->portnum);
149 return;
150 }
151}
152
153#define ADAPTER_LIST_SIZE 12
154int netxen_cards_found;
155
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700156static void netxen_nic_disable_int(struct netxen_adapter *adapter)
157{
158 uint32_t mask = 0x7ff;
159 int retries = 32;
160
161 DPRINTK(1, INFO, "Entered ISR Disable \n");
162
163 switch (adapter->portnum) {
164 case 0:
165 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
166 break;
167 case 1:
168 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
169 break;
170 case 2:
171 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
172 break;
173 case 3:
174 writel(0x0, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
175 break;
176 }
177
178 if (adapter->intr_scheme != -1 &&
179 adapter->intr_scheme != INTR_SCHEME_PERPORT)
180 writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
181
182 /* Window = 0 or 1 */
183 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
184 do {
185 writel(0xffffffff,
186 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS));
187 mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR));
188 if (!(mask & 0x80))
189 break;
190 udelay(10);
191 } while (--retries);
192
193 if (!retries) {
194 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
195 netxen_nic_driver_name);
196 }
197 }
198
199 DPRINTK(1, INFO, "Done with Disable Int\n");
200}
201
202static void netxen_nic_enable_int(struct netxen_adapter *adapter)
203{
204 u32 mask;
205
206 DPRINTK(1, INFO, "Entered ISR Enable \n");
207
208 if (adapter->intr_scheme != -1 &&
209 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
210 switch (adapter->ahw.board_type) {
211 case NETXEN_NIC_GBE:
212 mask = 0x77b;
213 break;
214 case NETXEN_NIC_XGBE:
215 mask = 0x77f;
216 break;
217 default:
218 mask = 0x7ff;
219 break;
220 }
221
222 writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK));
223 }
224
225 switch (adapter->portnum) {
226 case 0:
227 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_0));
228 break;
229 case 1:
230 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_1));
231 break;
232 case 2:
233 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_2));
234 break;
235 case 3:
236 writel(0x1, NETXEN_CRB_NORMALIZE(adapter, CRB_SW_INT_MASK_3));
237 break;
238 }
239
240 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
241 mask = 0xbff;
242 if (adapter->intr_scheme != -1 &&
243 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
244 writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
245 }
246 writel(mask,
247 PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK));
248 }
249
250 DPRINTK(1, INFO, "Done with enable Int\n");
251}
252
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400253/*
254 * netxen_nic_probe()
255 *
256 * The Linux system will invoke this after identifying the vendor ID and
257 * device Id in the pci_tbl supported by this module.
258 *
259 * A quad port card has one operational PCI config space, (function 0),
260 * which is used to access all four ports.
261 *
262 * This routine will initialize the adapter, and setup the global parameters
263 * along with the port's specific structure.
264 */
265static int __devinit
266netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
267{
268 struct net_device *netdev = NULL;
269 struct netxen_adapter *adapter = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800270 void __iomem *mem_ptr0 = NULL;
271 void __iomem *mem_ptr1 = NULL;
272 void __iomem *mem_ptr2 = NULL;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700273 unsigned long first_page_group_end;
274 unsigned long first_page_group_start;
275
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800276
Al Viroa608ab9c2007-01-02 10:39:10 +0000277 u8 __iomem *db_ptr = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800278 unsigned long mem_base, mem_len, db_base, db_len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700279 int pci_using_dac, i = 0, err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400280 int ring;
281 struct netxen_recv_context *recv_ctx = NULL;
282 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
283 struct netxen_cmd_buffer *cmd_buf_arr = NULL;
284 u64 mac_addr[FLASH_NUM_PORTS + 1];
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700285 int valid_mac = 0;
286 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700287 int pci_func_id = PCI_FUNC(pdev->devfn);
Joe Perches0795af52007-10-03 17:59:30 -0700288 DECLARE_MAC_BUF(mac);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400289
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800290 printk(KERN_INFO "%s \n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700291
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700292 if (pdev->class != 0x020000) {
293 printk(KERN_ERR"NetXen function %d, class %x will not"
294 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800295 return -ENODEV;
296 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400297 if ((err = pci_enable_device(pdev)))
298 return err;
299 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
300 err = -ENODEV;
301 goto err_out_disable_pdev;
302 }
303
304 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
305 goto err_out_disable_pdev;
306
307 pci_set_master(pdev);
Auke Kok44c10132007-06-08 15:46:36 -0700308 if (pdev->revision == NX_P2_C1 &&
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800309 (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) &&
310 (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400311 pci_using_dac = 1;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800312 } else {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400313 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
314 (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)))
315 goto err_out_free_res;
316
317 pci_using_dac = 0;
318 }
319
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700320
321 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
322 if(!netdev) {
323 printk(KERN_ERR"%s: Failed to allocate memory for the "
324 "device block.Check system memory resource"
325 " usage.\n", netxen_nic_driver_name);
326 goto err_out_free_res;
327 }
328
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700329 SET_NETDEV_DEV(netdev, &pdev->dev);
330
331 adapter = netdev->priv;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700332
333 adapter->ahw.pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700334 adapter->ahw.pci_func = pci_func_id;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700335 spin_lock_init(&adapter->tx_lock);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700336
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 {
357 err = -EIO;
358 goto err_out_free_netdev;
359 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800360
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700361 if (((mem_ptr0 == 0UL) && (mem_len == NETXEN_PCI_128MB_SIZE)) ||
362 (mem_ptr1 == 0UL) || (mem_ptr2 == 0UL)) {
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;
408
409 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700410 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530411 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530412 adapter->rx_csum = 1;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700413
414 netdev->open = netxen_nic_open;
415 netdev->stop = netxen_nic_close;
416 netdev->hard_start_xmit = netxen_nic_xmit_frame;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700417 netdev->get_stats = netxen_nic_get_stats;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700418 netdev->set_multicast_list = netxen_nic_set_multi;
419 netdev->set_mac_address = netxen_nic_set_mac;
420 netdev->change_mtu = netxen_nic_change_mtu;
421 netdev->tx_timeout = netxen_tx_timeout;
422 netdev->watchdog_timeo = HZ;
423
424 netxen_nic_change_mtu(netdev, netdev->mtu);
425
426 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700427#ifdef CONFIG_NET_POLL_CONTROLLER
428 netdev->poll_controller = netxen_nic_poll_controller;
429#endif
430 /* ScatterGather support */
431 netdev->features = NETIF_F_SG;
432 netdev->features |= NETIF_F_IP_CSUM;
433 netdev->features |= NETIF_F_TSO;
434
435 if (pci_using_dac)
436 netdev->features |= NETIF_F_HIGHDMA;
437
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530438 if (pci_enable_msi(pdev))
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700439 adapter->flags &= ~NETXEN_NIC_MSI_ENABLED;
dhananjay.phadke@gmail.com644caee2007-07-02 00:25:59 +0530440 else
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700441 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
442
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700443 netdev->irq = pdev->irq;
444 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
445
446 /*
447 * Set the CRB window to invalid. If any register in window 0 is
448 * accessed it should set the window to 0 and then reset it to 1.
449 */
450 adapter->curr_window = 255;
451
452 /* initialize the adapter */
453 netxen_initialize_adapter_hw(adapter);
454
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700455 /*
456 * Adapter in our case is quad port so initialize it before
457 * initializing the ports
458 */
459
460 netxen_initialize_adapter_ops(adapter);
461
462 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
463 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) ||
464 (adapter->ahw.boardcfg.board_type ==
465 NETXEN_BRDTYPE_P2_SB31_2G))
466 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
467 else
468 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS;
469 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
470 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
471
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400472 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
473 if (cmd_buf_arr == NULL) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800474 printk(KERN_ERR
475 "%s: Could not allocate cmd_buf_arr memory:%d\n",
476 netxen_nic_driver_name, (int)TX_RINGSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400477 err = -ENOMEM;
478 goto err_out_free_adapter;
479 }
480 memset(cmd_buf_arr, 0, TX_RINGSIZE);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700481 adapter->cmd_buf_arr = cmd_buf_arr;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400482
483 for (i = 0; i < MAX_RCV_CTX; ++i) {
484 recv_ctx = &adapter->recv_ctx[i];
485 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
486 rcv_desc = &recv_ctx->rcv_desc[ring];
487 switch (RCV_DESC_TYPE(ring)) {
488 case RCV_DESC_NORMAL:
489 rcv_desc->max_rx_desc_count =
490 adapter->max_rx_desc_count;
491 rcv_desc->flags = RCV_DESC_NORMAL;
492 rcv_desc->dma_size = RX_DMA_MAP_LEN;
493 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
494 break;
495
496 case RCV_DESC_JUMBO:
497 rcv_desc->max_rx_desc_count =
498 adapter->max_jumbo_rx_desc_count;
499 rcv_desc->flags = RCV_DESC_JUMBO;
500 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
501 rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH;
502 break;
503
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800504 case RCV_RING_LRO:
505 rcv_desc->max_rx_desc_count =
506 adapter->max_lro_rx_desc_count;
507 rcv_desc->flags = RCV_DESC_LRO;
508 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
509 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
510 break;
511
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400512 }
513 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
514 vmalloc(RCV_BUFFSIZE);
515
516 if (rcv_desc->rx_buf_arr == NULL) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800517 printk(KERN_ERR "%s: Could not allocate"
518 "rcv_desc->rx_buf_arr memory:%d\n",
519 netxen_nic_driver_name,
520 (int)RCV_BUFFSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400521 err = -ENOMEM;
522 goto err_out_free_rx_buffer;
523 }
524 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
525 }
526
527 }
528
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800529 netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700530
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700531 /* Mezz cards have PCI function 0,2,3 enabled */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700532 if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB31_10G_IMEZ)
533 && (pci_func_id >= 2))
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700534 adapter->portnum = pci_func_id - 2;
535
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800536#ifdef CONFIG_IA64
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700537 if(adapter->portnum == 0) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700538 netxen_pinit_from_rom(adapter, 0);
539 udelay(500);
540 netxen_load_firmware(adapter);
541 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800542#endif
543
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400544 init_timer(&adapter->watchdog_timer);
545 adapter->ahw.xg_linkup = 0;
546 adapter->watchdog_timer.function = &netxen_watchdog;
547 adapter->watchdog_timer.data = (unsigned long)adapter;
David Howells6d5aefb2006-12-05 19:36:26 +0000548 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400549 adapter->ahw.pdev = pdev;
550 adapter->proc_cmd_buf_counter = 0;
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);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700590 if (err)
591 goto err_out_free_rx_buffer;
592 val = readl(NETXEN_CRB_NORMALIZE(adapter,
593 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 */
626 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
627 | ((_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:
666 printk(KERN_INFO "%s: XGbE board initialized\n",
667 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
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700738 if (adapter->stop_port)
739 adapter->stop_port(adapter);
740
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530741 netxen_nic_disable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700742
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530743 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
744 init_firmware_done++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400745 netxen_free_hw_resources(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530746 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400747
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400748 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
749 recv_ctx = &adapter->recv_ctx[ctxid];
750 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
751 rcv_desc = &recv_ctx->rcv_desc[ring];
752 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
753 buffer = &(rcv_desc->rx_buf_arr[i]);
754 if (buffer->state == NETXEN_BUFFER_FREE)
755 continue;
756 pci_unmap_single(pdev, buffer->dma,
757 rcv_desc->dma_size,
758 PCI_DMA_FROMDEVICE);
759 if (buffer->skb != NULL)
760 dev_kfree_skb_any(buffer->skb);
761 }
762 vfree(rcv_desc->rx_buf_arr);
763 }
764 }
765
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700766 vfree(adapter->cmd_buf_arr);
767
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530768 if (adapter->portnum == 0) {
769 if (init_firmware_done) {
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530770 i = 100;
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530771 do {
772 if (dma_watchdog_shutdown_request(adapter) == 1)
773 break;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530774 msleep(100);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530775 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
776 break;
777 } while (--i);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530778
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530779 if (i == 0)
780 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
781 netdev->name);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530782
783 /* clear the register for future unloads/loads */
784 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
785 printk(KERN_INFO "State: 0x%0x\n",
786 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
787
788 /* leave the hw in the same state as reboot */
789 writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530790 netxen_pinit_from_rom(adapter, 0);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530791 msleep(1);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530792 netxen_load_firmware(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530793 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
794 }
795
796 /* clear the register for future unloads/loads */
797 writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc)));
798 printk(KERN_INFO "State: 0x%0x\n",
799 readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE)));
800
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530801 i = 100;
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530802 do {
803 if (dma_watchdog_shutdown_request(adapter) == 1)
804 break;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530805 msleep(100);
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530806 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
807 break;
808 } while (--i);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530809
810 if (i) {
811 netxen_free_adapter_offload(adapter);
812 } else {
dhananjay@netxen.comceded322007-07-19 14:41:09 +0530813 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
814 netdev->name);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530815 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530816 }
817
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530818 if (adapter->irq)
819 free_irq(adapter->irq, adapter);
820
821 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
822 pci_disable_msi(pdev);
823
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700824 iounmap(adapter->ahw.db_base);
825 iounmap(adapter->ahw.pci_base0);
826 iounmap(adapter->ahw.pci_base1);
827 iounmap(adapter->ahw.pci_base2);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700828
829 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530830 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700831 pci_set_drvdata(pdev, NULL);
832
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700833 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400834}
835
836/*
837 * Called when a network interface is made active
838 * @returns 0 on success, negative value on failure
839 */
840static int netxen_nic_open(struct net_device *netdev)
841{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700842 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400843 int err = 0;
844 int ctx, ring;
845
846 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
847 err = netxen_init_firmware(adapter);
848 if (err != 0) {
849 printk(KERN_ERR "Failed to init firmware\n");
850 return -EIO;
851 }
852 netxen_nic_flash_print(adapter);
853
854 /* setup all the resources for the Phantom... */
855 /* this include the descriptors for rcv, tx, and status */
856 netxen_nic_clear_stats(adapter);
857 err = netxen_nic_hw_resources(adapter);
858 if (err) {
859 printk(KERN_ERR "Error in setting hw resources:%d\n",
860 err);
861 return err;
862 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400863 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800864 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400865 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400866 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800867 adapter->irq = adapter->ahw.pdev->irq;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700868 err = request_irq(adapter->ahw.pdev->irq, netxen_intr,
Andrew Morton1c8e0392007-05-10 22:53:00 -0700869 IRQF_SHARED|IRQF_SAMPLE_RANDOM, netdev->name,
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700870 adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400871 if (err) {
872 printk(KERN_ERR "request_irq failed with: %d\n", err);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800873 netxen_free_hw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400874 return err;
875 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800876
877 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
878 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700879 if (!adapter->driver_mismatch)
880 mod_timer(&adapter->watchdog_timer, jiffies);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400881
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700882 napi_enable(&adapter->napi);
883
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700884 netxen_nic_enable_int(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400885
886 /* Done here again so that even if phantom sw overwrote it,
887 * we set it */
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700888 if (adapter->init_port
889 && adapter->init_port(adapter, adapter->portnum) != 0) {
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700890 del_timer_sync(&adapter->watchdog_timer);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700891 printk(KERN_ERR "%s: Failed to initialize port %d\n",
892 netxen_nic_driver_name, adapter->portnum);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700893 napi_disable(&adapter->napi);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700894 return -EIO;
895 }
dhananjay@netxen.com7eec5172007-07-25 20:13:11 +0530896 if (adapter->macaddr_set)
897 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700898
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700899 netxen_nic_set_link_parameters(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400900
901 netxen_nic_set_multi(netdev);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800902 if (adapter->set_mtu)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700903 adapter->set_mtu(adapter, netdev->mtu);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800904
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400905 if (!adapter->driver_mismatch)
906 netif_start_queue(netdev);
907
908 return 0;
909}
910
911/*
912 * netxen_nic_close - Disables a network interface entry point
913 */
914static int netxen_nic_close(struct net_device *netdev)
915{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700916 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400917 int i, j;
918 struct netxen_cmd_buffer *cmd_buff;
919 struct netxen_skb_frag *buffrag;
920
921 netif_carrier_off(netdev);
922 netif_stop_queue(netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700923 napi_disable(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400924
dhananjay@netxen.come01872af2007-07-25 20:13:12 +0530925 netxen_nic_disable_int(adapter);
926
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700927 cmd_buff = adapter->cmd_buf_arr;
928 for (i = 0; i < adapter->max_tx_desc_count; i++) {
929 buffrag = cmd_buff->frag_array;
930 if (buffrag->dma) {
931 pci_unmap_single(adapter->pdev, buffrag->dma,
932 buffrag->length, PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530933 buffrag->dma = 0ULL;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700934 }
935 for (j = 0; j < cmd_buff->frag_count; j++) {
936 buffrag++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400937 if (buffrag->dma) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700938 pci_unmap_page(adapter->pdev, buffrag->dma,
939 buffrag->length,
940 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530941 buffrag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400942 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400943 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700944 /* Free the skb we received in netxen_nic_xmit_frame */
945 if (cmd_buff->skb) {
946 dev_kfree_skb_any(cmd_buff->skb);
947 cmd_buff->skb = NULL;
948 }
949 cmd_buff++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400950 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530951 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
952 FLUSH_SCHEDULED_WORK();
953 del_timer_sync(&adapter->watchdog_timer);
954 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400955
956 return 0;
957}
958
959static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
960{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700961 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400962 struct netxen_hardware_context *hw = &adapter->ahw;
963 unsigned int first_seg_len = skb->len - skb->data_len;
964 struct netxen_skb_frag *buffrag;
965 unsigned int i;
966
967 u32 producer = 0;
968 u32 saved_producer = 0;
969 struct cmd_desc_type0 *hwdesc;
970 int k;
971 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400972 static int dropped_packet = 0;
973 int frag_count;
974 u32 local_producer = 0;
975 u32 max_tx_desc_count = 0;
976 u32 last_cmd_consumer = 0;
977 int no_of_desc;
978
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700979 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400980 frag_count = skb_shinfo(skb)->nr_frags + 1;
981
982 if (unlikely(skb->len <= 0)) {
983 dev_kfree_skb_any(skb);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700984 adapter->stats.badskblen++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400985 return NETDEV_TX_OK;
986 }
987
988 if (frag_count > MAX_BUFFERS_PER_CMD) {
989 printk("%s: %s netxen_nic_xmit_frame: frag_count (%d)"
990 "too large, can handle only %d frags\n",
991 netxen_nic_driver_name, netdev->name,
992 frag_count, MAX_BUFFERS_PER_CMD);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700993 adapter->stats.txdropped++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400994 if ((++dropped_packet & 0xff) == 0xff)
995 printk("%s: %s droppped packets = %d\n",
996 netxen_nic_driver_name, netdev->name,
997 dropped_packet);
998
999 return NETDEV_TX_OK;
1000 }
1001
1002 /*
1003 * Everything is set up. Now, we just need to transmit it out.
1004 * Note that we have to copy the contents of buffer over to
1005 * right place. Later on, this can be optimized out by de-coupling the
1006 * producer index from the buffer index.
1007 */
1008 retry_getting_window:
1009 spin_lock_bh(&adapter->tx_lock);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001010 if (adapter->total_threads >= MAX_XMIT_PRODUCERS) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001011 spin_unlock_bh(&adapter->tx_lock);
1012 /*
1013 * Yield CPU
1014 */
1015 if (!in_atomic())
1016 schedule();
1017 else {
1018 for (i = 0; i < 20; i++)
1019 cpu_relax(); /*This a nop instr on i386 */
1020 }
1021 goto retry_getting_window;
1022 }
1023 local_producer = adapter->cmd_producer;
1024 /* There 4 fragments per descriptor */
1025 no_of_desc = (frag_count + 3) >> 2;
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001026 if (netdev->features & NETIF_F_TSO) {
1027 if (skb_shinfo(skb)->gso_size > 0) {
1028
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001029 no_of_desc++;
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001030 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001031 sizeof(struct ethhdr)) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001032 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001033 no_of_desc++;
1034 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001035 }
1036 }
1037 k = adapter->cmd_producer;
1038 max_tx_desc_count = adapter->max_tx_desc_count;
1039 last_cmd_consumer = adapter->last_cmd_consumer;
1040 if ((k + no_of_desc) >=
1041 ((last_cmd_consumer <= k) ? last_cmd_consumer + max_tx_desc_count :
1042 last_cmd_consumer)) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001043 netif_stop_queue(netdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001044 adapter->flags |= NETXEN_NETDEV_STATUS;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001045 spin_unlock_bh(&adapter->tx_lock);
1046 return NETDEV_TX_BUSY;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001047 }
1048 k = get_index_range(k, max_tx_desc_count, no_of_desc);
1049 adapter->cmd_producer = k;
1050 adapter->total_threads++;
1051 adapter->num_threads++;
1052
1053 spin_unlock_bh(&adapter->tx_lock);
1054 /* Copy the descriptors into the hardware */
1055 producer = local_producer;
1056 saved_producer = producer;
1057 hwdesc = &hw->cmd_desc_head[producer];
1058 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1059 /* Take skb->data itself */
1060 pbuf = &adapter->cmd_buf_arr[producer];
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001061 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
Al Viroa608ab9c2007-01-02 10:39:10 +00001062 pbuf->mss = skb_shinfo(skb)->gso_size;
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001063 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001064 } else {
1065 pbuf->mss = 0;
1066 hwdesc->mss = 0;
1067 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001068 pbuf->total_length = skb->len;
1069 pbuf->skb = skb;
1070 pbuf->cmd = TX_ETHER_PKT;
1071 pbuf->frag_count = frag_count;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001072 pbuf->port = adapter->portnum;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001073 buffrag = &pbuf->frag_array[0];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001074 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001075 PCI_DMA_TODEVICE);
1076 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001077 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1078 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1079 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001080
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001081 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001082 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001083 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1084 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1085
1086 for (i = 1, k = 1; i < frag_count; i++, k++) {
1087 struct skb_frag_struct *frag;
1088 int len, temp_len;
1089 unsigned long offset;
1090 dma_addr_t temp_dma;
1091
1092 /* move to next desc. if there is a need */
1093 if ((i & 0x3) == 0) {
1094 k = 0;
1095 producer = get_next_index(producer,
1096 adapter->max_tx_desc_count);
1097 hwdesc = &hw->cmd_desc_head[producer];
1098 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1099 }
1100 frag = &skb_shinfo(skb)->frags[i - 1];
1101 len = frag->size;
1102 offset = frag->page_offset;
1103
1104 temp_len = len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001105 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001106 len, PCI_DMA_TODEVICE);
1107
1108 buffrag++;
1109 buffrag->dma = temp_dma;
1110 buffrag->length = temp_len;
1111
1112 DPRINTK(INFO, "for loop. i=%d k=%d\n", i, k);
1113 switch (k) {
1114 case 0:
1115 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1116 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1117 break;
1118 case 1:
1119 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1120 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1121 break;
1122 case 2:
1123 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1124 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1125 break;
1126 case 3:
Al Viroa608ab9c2007-01-02 10:39:10 +00001127 hwdesc->buffer4_length = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001128 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1129 break;
1130 }
1131 frag++;
1132 }
1133 producer = get_next_index(producer, adapter->max_tx_desc_count);
1134
1135 /* might change opcode to TX_TCP_LSO */
1136 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1137
1138 /* For LSO, we need to copy the MAC/IP/TCP headers into
1139 * the descriptor ring
1140 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001141 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1142 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001143 int hdr_len, first_hdr_len, more_hdr;
1144 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001145 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1146 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001147 more_hdr = 1;
1148 } else {
1149 first_hdr_len = hdr_len;
1150 more_hdr = 0;
1151 }
1152 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1153 hwdesc = &hw->cmd_desc_head[producer];
1154
1155 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001156 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001157 (void *)(skb->data), first_hdr_len);
1158 producer = get_next_index(producer, max_tx_desc_count);
1159
1160 if (more_hdr) {
1161 hwdesc = &hw->cmd_desc_head[producer];
1162 /* copy the next 64 bytes - should be enough except
1163 * for pathological case
1164 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001165 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1166 hwdesc,
1167 (hdr_len -
1168 first_hdr_len));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001169 producer = get_next_index(producer, max_tx_desc_count);
1170 }
1171 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001172
1173 i = netxen_get_cmd_desc_totallength(&hw->cmd_desc_head[saved_producer]);
1174
1175 hw->cmd_desc_head[saved_producer].flags_opcode =
1176 cpu_to_le16(hw->cmd_desc_head[saved_producer].flags_opcode);
1177 hw->cmd_desc_head[saved_producer].num_of_buffers_total_length =
1178 cpu_to_le32(hw->cmd_desc_head[saved_producer].
1179 num_of_buffers_total_length);
1180
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001181 spin_lock_bh(&adapter->tx_lock);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001182 adapter->stats.txbytes += i;
1183
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001184 /* Code to update the adapter considering how many producer threads
1185 are currently working */
1186 if ((--adapter->num_threads) == 0) {
1187 /* This is the last thread */
1188 u32 crb_producer = adapter->cmd_producer;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001189 netxen_nic_update_cmd_producer(adapter, crb_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001190 wmb();
1191 adapter->total_threads = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001192 }
1193
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001194 adapter->stats.xmitfinished++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001195 spin_unlock_bh(&adapter->tx_lock);
1196
1197 netdev->trans_start = jiffies;
1198
1199 DPRINTK(INFO, "wrote CMD producer %x to phantom\n", producer);
1200
1201 DPRINTK(INFO, "Done. Send\n");
1202 return NETDEV_TX_OK;
1203}
1204
1205static void netxen_watchdog(unsigned long v)
1206{
1207 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001208
1209 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001210}
1211
1212static void netxen_tx_timeout(struct net_device *netdev)
1213{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001214 struct netxen_adapter *adapter = (struct netxen_adapter *)
1215 netdev_priv(netdev);
1216 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001217}
1218
David Howells6d5aefb2006-12-05 19:36:26 +00001219static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001220{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001221 struct netxen_adapter *adapter =
1222 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001223
1224 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001225 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001226
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001227 netxen_nic_close(adapter->netdev);
1228 netxen_nic_open(adapter->netdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001229 adapter->netdev->trans_start = jiffies;
1230 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001231}
1232
1233static int
1234netxen_handle_int(struct netxen_adapter *adapter, struct net_device *netdev)
1235{
1236 u32 ret = 0;
1237
1238 DPRINTK(INFO, "Entered handle ISR\n");
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001239 adapter->stats.ints++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001240
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301241 netxen_nic_disable_int(adapter);
1242
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001243 if (netxen_nic_rx_has_work(adapter) || netxen_nic_tx_has_work(adapter)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001244 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001245 /*
1246 * Interrupts are already disabled.
1247 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001248 __netif_rx_schedule(netdev, &adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001249 } else {
1250 static unsigned int intcount = 0;
1251 if ((++intcount & 0xfff) == 0xfff)
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301252 DPRINTK(KERN_ERR
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001253 "%s: %s interrupt %d while in poll\n",
1254 netxen_nic_driver_name, netdev->name,
1255 intcount);
1256 }
1257 ret = 1;
1258 }
1259
1260 if (ret == 0) {
1261 netxen_nic_enable_int(adapter);
1262 }
1263
1264 return ret;
1265}
1266
1267/*
1268 * netxen_intr - Interrupt Handler
1269 * @irq: interrupt number
1270 * data points to adapter stucture (which may be handling more than 1 port
1271 */
Jeff Garzik1494a812006-11-07 05:12:16 -05001272irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001273{
1274 struct netxen_adapter *adapter;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001275 struct net_device *netdev;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301276 u32 our_int = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001277
1278 if (unlikely(!irq)) {
1279 return IRQ_NONE; /* Not our interrupt */
1280 }
1281
1282 adapter = (struct netxen_adapter *)data;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001283 netdev = adapter->netdev;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301284
1285 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1286 our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1287 /* not our interrupt */
1288 if ((our_int & (0x80 << adapter->portnum)) == 0)
1289 return IRQ_NONE;
1290 }
1291
1292 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1293 /* claim interrupt */
1294 if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) {
1295 writel(our_int & ~((u32)(0x80 << adapter->portnum)),
1296 NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR));
1297 }
1298 }
1299
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001300 if (netif_running(netdev))
1301 netxen_handle_int(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001302
1303 return IRQ_HANDLED;
1304}
1305
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001306static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001307{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001308 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1309 struct net_device *netdev = adapter->netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001310 int done = 1;
1311 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001312 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001313
1314 DPRINTK(INFO, "polling for %d descriptors\n", *budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001315
Amit S. Kale80922fb2006-12-04 09:18:00 -08001316 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001317 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1318 /*
1319 * Fairness issue. This will give undue weight to the
1320 * receive context 0.
1321 */
1322
1323 /*
1324 * To avoid starvation, we give each of our receivers,
1325 * a fraction of the quota. Sometimes, it might happen that we
1326 * have enough quota to process every packet, but since all the
1327 * packets are on one context, it gets only half of the quota,
1328 * and ends up not processing it.
1329 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001330 work_done += netxen_process_rcv_ring(adapter, ctx,
1331 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001332 }
1333
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001334 if (work_done >= budget && netxen_nic_rx_has_work(adapter) != 0)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001335 done = 0;
1336
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001337 if (netxen_process_cmd_ring((unsigned long)adapter) == 0)
1338 done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001339
1340 DPRINTK(INFO, "new work_done: %d work_to_do: %d\n",
Amit S. Kale80922fb2006-12-04 09:18:00 -08001341 work_done, work_to_do);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001342 if (done) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001343 netif_rx_complete(netdev, napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001344 netxen_nic_enable_int(adapter);
1345 }
1346
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001347 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001348}
1349
1350#ifdef CONFIG_NET_POLL_CONTROLLER
1351static void netxen_nic_poll_controller(struct net_device *netdev)
1352{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001353 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001354 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001355 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001356 enable_irq(adapter->irq);
1357}
1358#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001359
1360static struct pci_driver netxen_driver = {
1361 .name = netxen_nic_driver_name,
1362 .id_table = netxen_pci_tbl,
1363 .probe = netxen_nic_probe,
1364 .remove = __devexit_p(netxen_nic_remove)
1365};
1366
1367/* Driver Registration on NetXen card */
1368
1369static int __init netxen_init_module(void)
1370{
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001371 if ((netxen_workq = create_singlethread_workqueue("netxen")) == 0)
1372 return -ENOMEM;
1373
Amit S. Kale184231b2007-01-22 06:52:53 -08001374 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001375}
1376
1377module_init(netxen_init_module);
1378
1379static void __exit netxen_exit_module(void)
1380{
1381 /*
1382 * Wait for some time to allow the dma to drain, if any.
1383 */
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301384 msleep(100);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001385 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001386 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001387}
1388
1389module_exit(netxen_exit_module);