blob: b3c084f0c751478e3887730bc123d557a1763a56 [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
Dhananjay Phadke29566402008-07-21 19:44:04 -070052static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
53
54/* Default to restricted 1G auto-neg mode */
55static int wol_port_mode = 5;
56
57static int use_msi = 1;
58
59static int use_msi_x = 1;
60
Amit S. Kale3d396eb2006-10-21 15:33:03 -040061#define NETXEN_NETDEV_WEIGHT 120
62#define NETXEN_ADAPTER_UP_MAGIC 777
Amit S. Kalecb8011a2006-11-29 09:00:10 -080063#define NETXEN_NIC_PEG_TUNE 0
Amit S. Kale3d396eb2006-10-21 15:33:03 -040064
65/* Local functions to NetXen NIC driver */
66static int __devinit netxen_nic_probe(struct pci_dev *pdev,
Dhananjay Phadke29566402008-07-21 19:44:04 -070067 const struct pci_device_id *ent);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040068static void __devexit netxen_nic_remove(struct pci_dev *pdev);
69static int netxen_nic_open(struct net_device *netdev);
70static int netxen_nic_close(struct net_device *netdev);
71static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
72static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000073static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040074static void netxen_watchdog(unsigned long);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070075static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040076#ifdef CONFIG_NET_POLL_CONTROLLER
77static void netxen_nic_poll_controller(struct net_device *netdev);
78#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050079static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070080static irqreturn_t netxen_msi_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040081
82/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010083#define ENTRY(device) \
84 {PCI_DEVICE(0x4040, (device)), \
85 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
86
Amit S. Kale3d396eb2006-10-21 15:33:03 -040087static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Al Viro70081ac2008-06-23 02:04:50 +010088 ENTRY(0x0001),
89 ENTRY(0x0002),
90 ENTRY(0x0003),
91 ENTRY(0x0004),
92 ENTRY(0x0005),
93 ENTRY(0x0024),
94 ENTRY(0x0025),
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -070095 ENTRY(0x0100),
Amit S. Kale3d396eb2006-10-21 15:33:03 -040096 {0,}
97};
98
99MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
100
Adrian Bunkb1555132008-03-31 02:22:18 +0300101/*
102 * In netxen_nic_down(), we must wait for any pending callback requests into
103 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
104 * reenabled right after it is deleted in netxen_nic_down().
105 * FLUSH_SCHEDULED_WORK() does this synchronization.
106 *
107 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
108 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
109 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
110 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
111 * linkwatch_event() to be executed which also attempts to acquire the rtnl
112 * lock thus causing a deadlock.
113 */
114
115static struct workqueue_struct *netxen_workq;
116#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
117#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
118
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800119static void netxen_watchdog(unsigned long);
120
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700121static uint32_t crb_cmd_producer[4] = {
122 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
123 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
124};
125
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700126void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700127netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
128 uint32_t crb_producer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700129{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700130 adapter->pci_write_normalize(adapter,
131 adapter->crb_addr_cmd_producer, crb_producer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700132}
133
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700134static uint32_t crb_cmd_consumer[4] = {
135 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
136 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
137};
138
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700139static inline void
140netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
141 u32 crb_consumer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700142{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700143 adapter->pci_write_normalize(adapter,
144 adapter->crb_addr_cmd_consumer, crb_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700145}
146
Dhananjay Phadke29566402008-07-21 19:44:04 -0700147static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700148 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700149 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
150 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
151 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700152};
153
Dhananjay Phadke29566402008-07-21 19:44:04 -0700154static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
155
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700156static void netxen_nic_disable_int(struct netxen_adapter *adapter)
157{
Dhananjay Phadke443be792008-03-17 19:59:48 -0700158 u32 mask = 0x7ff;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700159 int retries = 32;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700160 int pci_fn = adapter->ahw.pci_func;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700161
Jeff Garzikf0c88f92008-03-25 23:53:24 -0400162 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700163 adapter->pci_write_normalize(adapter,
164 adapter->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700165
166 if (adapter->intr_scheme != -1 &&
167 adapter->intr_scheme != INTR_SCHEME_PERPORT)
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700168 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700169
Dhananjay Phadke29566402008-07-21 19:44:04 -0700170 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700171 do {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700172 adapter->pci_write_immediate(adapter,
173 ISR_INT_TARGET_STATUS, 0xffffffff);
174 mask = adapter->pci_read_immediate(adapter,
175 ISR_INT_VECTOR);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700176 if (!(mask & 0x80))
177 break;
178 udelay(10);
179 } while (--retries);
180
181 if (!retries) {
182 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
183 netxen_nic_driver_name);
184 }
Dhananjay Phadke443be792008-03-17 19:59:48 -0700185 } else {
186 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700187 adapter->pci_write_immediate(adapter,
188 msi_tgt_status[pci_fn], 0xffffffff);
Dhananjay Phadke443be792008-03-17 19:59:48 -0700189 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700190 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700191}
192
193static void netxen_nic_enable_int(struct netxen_adapter *adapter)
194{
195 u32 mask;
196
197 DPRINTK(1, INFO, "Entered ISR Enable \n");
198
199 if (adapter->intr_scheme != -1 &&
200 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
201 switch (adapter->ahw.board_type) {
202 case NETXEN_NIC_GBE:
203 mask = 0x77b;
204 break;
205 case NETXEN_NIC_XGBE:
206 mask = 0x77f;
207 break;
208 default:
209 mask = 0x7ff;
210 break;
211 }
212
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700213 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700214 }
215
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700216 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700217
Dhananjay Phadke29566402008-07-21 19:44:04 -0700218 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700219 mask = 0xbff;
220 if (adapter->intr_scheme != -1 &&
221 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700222 adapter->pci_write_normalize(adapter,
223 CRB_INT_VECTOR, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700224 }
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700225 adapter->pci_write_immediate(adapter,
226 ISR_INT_TARGET_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700227 }
228
229 DPRINTK(1, INFO, "Done with enable Int\n");
230}
231
Dhananjay Phadke29566402008-07-21 19:44:04 -0700232static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
233{
234 struct pci_dev *pdev = adapter->pdev;
235 int err;
236 uint64_t mask;
237
238#ifdef CONFIG_IA64
239 adapter->dma_mask = DMA_32BIT_MASK;
240#else
241 if (revision_id >= NX_P3_B0) {
242 /* should go to DMA_64BIT_MASK */
243 adapter->dma_mask = DMA_39BIT_MASK;
244 mask = DMA_39BIT_MASK;
245 } else if (revision_id == NX_P3_A2) {
246 adapter->dma_mask = DMA_39BIT_MASK;
247 mask = DMA_39BIT_MASK;
248 } else if (revision_id == NX_P2_C1) {
249 adapter->dma_mask = DMA_35BIT_MASK;
250 mask = DMA_35BIT_MASK;
251 } else {
252 adapter->dma_mask = DMA_32BIT_MASK;
253 mask = DMA_32BIT_MASK;
254 goto set_32_bit_mask;
255 }
256
257 /*
258 * Consistent DMA mask is set to 32 bit because it cannot be set to
259 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
260 * come off this pool.
261 */
262 if (pci_set_dma_mask(pdev, mask) == 0 &&
263 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
264 adapter->pci_using_dac = 1;
265 return 0;
266 }
267#endif /* CONFIG_IA64 */
268
269set_32_bit_mask:
270 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
271 if (!err)
272 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
273 if (err) {
274 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
275 return err;
276 }
277
278 adapter->pci_using_dac = 0;
279 return 0;
280}
281
282static void netxen_check_options(struct netxen_adapter *adapter)
283{
284 switch (adapter->ahw.boardcfg.board_type) {
285 case NETXEN_BRDTYPE_P3_HMEZ:
286 case NETXEN_BRDTYPE_P3_XG_LOM:
287 case NETXEN_BRDTYPE_P3_10G_CX4:
288 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
289 case NETXEN_BRDTYPE_P3_IMEZ:
290 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
291 case NETXEN_BRDTYPE_P3_10G_XFP:
292 case NETXEN_BRDTYPE_P3_10000_BASE_T:
293 adapter->msix_supported = !!use_msi_x;
294 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
295 break;
296
297 case NETXEN_BRDTYPE_P2_SB31_10G:
298 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
299 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
300 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
301 adapter->msix_supported = 0;
302 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
303 break;
304
305 case NETXEN_BRDTYPE_P3_REF_QG:
306 case NETXEN_BRDTYPE_P3_4_GB:
307 case NETXEN_BRDTYPE_P3_4_GB_MM:
308 case NETXEN_BRDTYPE_P2_SB35_4G:
309 case NETXEN_BRDTYPE_P2_SB31_2G:
310 adapter->msix_supported = 0;
311 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
312 break;
313
314 default:
315 adapter->msix_supported = 0;
316 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
317
318 printk(KERN_WARNING "Unknown board type(0x%x)\n",
319 adapter->ahw.boardcfg.board_type);
320 break;
321 }
322
323 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
324 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
325 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
326
327 adapter->max_possible_rss_rings = 1;
328 return;
329}
330
331static int
332netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
333{
334 int ret = 0;
335
336 if (first_boot == 0x55555555) {
337 /* This is the first boot after power up */
338
339 /* PCI bus master workaround */
340 adapter->hw_read_wx(adapter,
341 NETXEN_PCIE_REG(0x4), &first_boot, 4);
342 if (!(first_boot & 0x4)) {
343 first_boot |= 0x4;
344 adapter->hw_write_wx(adapter,
345 NETXEN_PCIE_REG(0x4), &first_boot, 4);
346 adapter->hw_read_wx(adapter,
347 NETXEN_PCIE_REG(0x4), &first_boot, 4);
348 }
349
350 /* This is the first boot after power up */
351 adapter->hw_read_wx(adapter,
352 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
353 if (first_boot != 0x80000f) {
354 /* clear the register for future unloads/loads */
355 adapter->pci_write_normalize(adapter,
356 NETXEN_CAM_RAM(0x1fc), 0);
357 ret = -1;
358 }
359
360 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
361 /* Start P2 boot loader */
362 adapter->pci_write_normalize(adapter,
363 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
364 adapter->pci_write_normalize(adapter,
365 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
366 }
367 }
368 return ret;
369}
370
371static void netxen_set_port_mode(struct netxen_adapter *adapter)
372{
373 u32 val, data;
374
375 val = adapter->ahw.boardcfg.board_type;
376 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
377 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
378 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
379 data = NETXEN_PORT_MODE_802_3_AP;
380 adapter->hw_write_wx(adapter,
381 NETXEN_PORT_MODE_ADDR, &data, 4);
382 } else if (port_mode == NETXEN_PORT_MODE_XG) {
383 data = NETXEN_PORT_MODE_XG;
384 adapter->hw_write_wx(adapter,
385 NETXEN_PORT_MODE_ADDR, &data, 4);
386 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
387 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
388 adapter->hw_write_wx(adapter,
389 NETXEN_PORT_MODE_ADDR, &data, 4);
390 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
391 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
392 adapter->hw_write_wx(adapter,
393 NETXEN_PORT_MODE_ADDR, &data, 4);
394 } else {
395 data = NETXEN_PORT_MODE_AUTO_NEG;
396 adapter->hw_write_wx(adapter,
397 NETXEN_PORT_MODE_ADDR, &data, 4);
398 }
399
400 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
401 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
402 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
403 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
404 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
405 }
406 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
407 &wol_port_mode, 4);
408 }
409}
410
411#define PCI_CAP_ID_GEN 0x10
412
413static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
414{
415 u32 pdevfuncsave;
416 u32 c8c9value = 0;
417 u32 chicken = 0;
418 u32 control = 0;
419 int i, pos;
420 struct pci_dev *pdev;
421
422 pdev = pci_get_device(0x1166, 0x0140, NULL);
423 if (pdev) {
424 pci_dev_put(pdev);
425 adapter->hw_read_wx(adapter,
426 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
427 chicken |= 0x4000;
428 adapter->hw_write_wx(adapter,
429 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
430 }
431
432 pdev = adapter->pdev;
433
434 adapter->hw_read_wx(adapter,
435 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
436 /* clear chicken3.25:24 */
437 chicken &= 0xFCFFFFFF;
438 /*
439 * if gen1 and B0, set F1020 - if gen 2, do nothing
440 * if gen2 set to F1000
441 */
442 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
443 if (pos == 0xC0) {
444 pci_read_config_dword(pdev, pos + 0x10, &control);
445 if ((control & 0x000F0000) != 0x00020000) {
446 /* set chicken3.24 if gen1 */
447 chicken |= 0x01000000;
448 }
449 printk(KERN_INFO "%s Gen2 strapping detected\n",
450 netxen_nic_driver_name);
451 c8c9value = 0xF1000;
452 } else {
453 /* set chicken3.24 if gen1 */
454 chicken |= 0x01000000;
455 printk(KERN_INFO "%s Gen1 strapping detected\n",
456 netxen_nic_driver_name);
457 if (adapter->ahw.revision_id == NX_P3_B0)
458 c8c9value = 0xF1020;
459 else
460 c8c9value = 0;
461
462 }
463 adapter->hw_write_wx(adapter,
464 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
465
466 if (!c8c9value)
467 return;
468
469 pdevfuncsave = pdev->devfn;
470 if (pdevfuncsave & 0x07)
471 return;
472
473 for (i = 0; i < 8; i++) {
474 pci_read_config_dword(pdev, pos + 8, &control);
475 pci_read_config_dword(pdev, pos + 8, &control);
476 pci_write_config_dword(pdev, pos + 8, c8c9value);
477 pdev->devfn++;
478 }
479 pdev->devfn = pdevfuncsave;
480}
481
482static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
483{
484 u32 control;
485 int pos;
486
487 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
488 if (pos) {
489 pci_read_config_dword(pdev, pos, &control);
490 if (enable)
491 control |= PCI_MSIX_FLAGS_ENABLE;
492 else
493 control = 0;
494 pci_write_config_dword(pdev, pos, control);
495 }
496}
497
498static void netxen_init_msix_entries(struct netxen_adapter *adapter)
499{
500 int i;
501
502 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
503 adapter->msix_entries[i].entry = i;
504}
505
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400506/*
507 * netxen_nic_probe()
508 *
509 * The Linux system will invoke this after identifying the vendor ID and
510 * device Id in the pci_tbl supported by this module.
511 *
512 * A quad port card has one operational PCI config space, (function 0),
513 * which is used to access all four ports.
514 *
515 * This routine will initialize the adapter, and setup the global parameters
516 * along with the port's specific structure.
517 */
518static int __devinit
519netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
520{
521 struct net_device *netdev = NULL;
522 struct netxen_adapter *adapter = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800523 void __iomem *mem_ptr0 = NULL;
524 void __iomem *mem_ptr1 = NULL;
525 void __iomem *mem_ptr2 = NULL;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700526 unsigned long first_page_group_end;
527 unsigned long first_page_group_start;
528
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800529
Al Viroa608ab92007-01-02 10:39:10 +0000530 u8 __iomem *db_ptr = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700531 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
532 int i = 0, err;
533 int first_driver, first_boot;
Al Virof305f782007-12-22 19:44:00 +0000534 __le64 mac_addr[FLASH_NUM_PORTS + 1];
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700535 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700536 int pci_func_id = PCI_FUNC(pdev->devfn);
Joe Perches0795af52007-10-03 17:59:30 -0700537 DECLARE_MAC_BUF(mac);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700538 struct netxen_legacy_intr_set *legacy_intrp;
539 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400540
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700541 if (pci_func_id == 0)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700542 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700543
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700544 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700545 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700546 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800547 return -ENODEV;
548 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700549
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400550 if ((err = pci_enable_device(pdev)))
551 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700552
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400553 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
554 err = -ENODEV;
555 goto err_out_disable_pdev;
556 }
557
558 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
559 goto err_out_disable_pdev;
560
561 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700562
563 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
564 if(!netdev) {
565 printk(KERN_ERR"%s: Failed to allocate memory for the "
566 "device block.Check system memory resource"
567 " usage.\n", netxen_nic_driver_name);
568 goto err_out_free_res;
569 }
570
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700571 SET_NETDEV_DEV(netdev, &pdev->dev);
572
573 adapter = netdev->priv;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700574 adapter->netdev = netdev;
575 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700576 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700577
578 revision_id = pdev->revision;
579 adapter->ahw.revision_id = revision_id;
580
581 err = nx_set_dma_mask(adapter, revision_id);
582 if (err)
583 goto err_out_free_netdev;
584
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700585 rwlock_init(&adapter->adapter_lock);
586 adapter->ahw.qdr_sn_window = -1;
587 adapter->ahw.ddr_mn_window = -1;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700588
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400589 /* remap phys address */
590 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
591 mem_len = pci_resource_len(pdev, 0);
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700592 pci_len0 = 0;
593
594 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
595 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
596 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
597 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
598 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
599 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
600 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
601 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
602 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400603
604 /* 128 Meg of memory */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700605 if (mem_len == NETXEN_PCI_128MB_SIZE) {
606 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
607 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
608 SECOND_PAGE_GROUP_SIZE);
609 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
610 THIRD_PAGE_GROUP_SIZE);
611 first_page_group_start = FIRST_PAGE_GROUP_START;
612 first_page_group_end = FIRST_PAGE_GROUP_END;
613 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
614 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
615 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
616 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
617 first_page_group_start = 0;
618 first_page_group_end = 0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700619 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
620 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
621 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
622 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
623 adapter->pci_write_immediate =
624 netxen_nic_pci_write_immediate_2M;
625 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
626 adapter->pci_write_normalize =
627 netxen_nic_pci_write_normalize_2M;
628 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
629 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
630 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
631
632 mem_ptr0 = ioremap(mem_base, mem_len);
633 pci_len0 = mem_len;
634 first_page_group_start = 0;
635 first_page_group_end = 0;
636
637 adapter->ahw.ddr_mn_window = 0;
638 adapter->ahw.qdr_sn_window = 0;
639
640 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
641 (pci_func_id * 0x20);
642 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
643 if (pci_func_id < 4)
644 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
645 else
646 adapter->ahw.ms_win_crb +=
647 0xA0 + ((pci_func_id - 4) * 0x10);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700648 } else {
Jeff Garzik47906542007-11-23 21:23:36 -0500649 err = -EIO;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700650 goto err_out_free_netdev;
651 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800652
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700653 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800654
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800655 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
656 db_len = pci_resource_len(pdev, 4);
657
658 if (db_len == 0) {
659 printk(KERN_ERR "%s: doorbell is disabled\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700660 netxen_nic_driver_name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800661 err = -EIO;
662 goto err_out_iounmap;
663 }
664 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
665 db_len);
666
667 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
Al Viroa608ab92007-01-02 10:39:10 +0000668 if (!db_ptr) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800669 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700670 netxen_nic_driver_name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800671 err = -EIO;
672 goto err_out_iounmap;
673 }
674 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400675
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700676 adapter->ahw.pci_base0 = mem_ptr0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700677 adapter->ahw.pci_len0 = pci_len0;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700678 adapter->ahw.first_page_group_start = first_page_group_start;
679 adapter->ahw.first_page_group_end = first_page_group_end;
680 adapter->ahw.pci_base1 = mem_ptr1;
681 adapter->ahw.pci_base2 = mem_ptr2;
682 adapter->ahw.db_base = db_ptr;
683 adapter->ahw.db_len = db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400684
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700685 netif_napi_add(netdev, &adapter->napi,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700686 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
687
688 if (revision_id >= NX_P3_B0)
689 legacy_intrp = &legacy_intr[pci_func_id];
690 else
691 legacy_intrp = &legacy_intr[0];
692
693 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
694 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
695 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
696 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700697
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530698 /* this will be read from FW later */
699 adapter->intr_scheme = -1;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700700 adapter->msi_mode = -1;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530701
702 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700703 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530704 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530705 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -0700706 adapter->mc_enabled = 0;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700707 if (NX_IS_REVISION_P3(revision_id)) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700708 adapter->max_mc_count = 38;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700709 adapter->max_rds_rings = 2;
710 } else {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700711 adapter->max_mc_count = 16;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700712 adapter->max_rds_rings = 3;
713 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700714
715 netdev->open = netxen_nic_open;
716 netdev->stop = netxen_nic_close;
717 netdev->hard_start_xmit = netxen_nic_xmit_frame;
Jeff Garzik47906542007-11-23 21:23:36 -0500718 netdev->get_stats = netxen_nic_get_stats;
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700719 if (NX_IS_REVISION_P3(revision_id))
720 netdev->set_multicast_list = netxen_p3_nic_set_multi;
721 else
722 netdev->set_multicast_list = netxen_p2_nic_set_multi;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700723 netdev->set_mac_address = netxen_nic_set_mac;
724 netdev->change_mtu = netxen_nic_change_mtu;
725 netdev->tx_timeout = netxen_tx_timeout;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700726 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700727
728 netxen_nic_change_mtu(netdev, netdev->mtu);
729
730 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700731#ifdef CONFIG_NET_POLL_CONTROLLER
732 netdev->poll_controller = netxen_nic_poll_controller;
733#endif
734 /* ScatterGather support */
735 netdev->features = NETIF_F_SG;
736 netdev->features |= NETIF_F_IP_CSUM;
737 netdev->features |= NETIF_F_TSO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700738 if (NX_IS_REVISION_P3(revision_id))
739 netdev->features |= NETIF_F_TSO6;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700740
Dhananjay Phadke29566402008-07-21 19:44:04 -0700741 if (adapter->pci_using_dac)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700742 netdev->features |= NETIF_F_HIGHDMA;
743
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700744 /*
745 * Set the CRB window to invalid. If any register in window 0 is
746 * accessed it should set the window to 0 and then reset it to 1.
747 */
748 adapter->curr_window = 255;
749
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700750 if (netxen_nic_get_board_info(adapter) != 0) {
751 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700752 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700753 err = -EIO;
754 goto err_out_iounmap;
755 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700756
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700757 netxen_initialize_adapter_ops(adapter);
758
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700759 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700760 switch (adapter->ahw.boardcfg.board_type) {
761 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
762 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
763 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700764 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700765 break;
766 default:
767 break;
768 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700769
Dhananjay Phadke29566402008-07-21 19:44:04 -0700770 /*
771 * This call will setup various max rx/tx counts.
772 * It must be done before any buffer/ring allocations.
773 */
774 netxen_check_options(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400775
Dhananjay Phadke29566402008-07-21 19:44:04 -0700776 first_driver = 0;
777 if (NX_IS_REVISION_P3(revision_id)) {
778 if (adapter->ahw.pci_func == 0)
779 first_driver = 1;
780 } else {
781 if (adapter->portnum == 0)
782 first_driver = 1;
783 }
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700784 adapter->crb_addr_cmd_producer = crb_cmd_producer[adapter->portnum];
785 adapter->crb_addr_cmd_consumer = crb_cmd_consumer[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700786 netxen_nic_update_cmd_producer(adapter, 0);
787 netxen_nic_update_cmd_consumer(adapter, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400788
Dhananjay Phadke29566402008-07-21 19:44:04 -0700789 if (first_driver) {
790 first_boot = adapter->pci_read_normalize(adapter,
791 NETXEN_CAM_RAM(0x1fc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400792
Dhananjay Phadke29566402008-07-21 19:44:04 -0700793 err = netxen_check_hw_init(adapter, first_boot);
794 if (err) {
795 printk(KERN_ERR "%s: error in init HW init sequence\n",
796 netxen_nic_driver_name);
797 goto err_out_iounmap;
798 }
799
800 if (NX_IS_REVISION_P3(revision_id))
801 netxen_set_port_mode(adapter);
802
803 if (first_boot != 0x55555555) {
804 adapter->pci_write_normalize(adapter,
805 CRB_CMDPEG_STATE, 0);
806 netxen_pinit_from_rom(adapter, 0);
807 msleep(1);
808 netxen_load_firmware(adapter);
809 }
810
811 if (NX_IS_REVISION_P3(revision_id))
812 netxen_pcie_strap_init(adapter);
813
814 if (NX_IS_REVISION_P2(revision_id)) {
815
816 /* Initialize multicast addr pool owners */
817 val = 0x7654;
818 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
819 val |= 0x0f000000;
820 netxen_crb_writelit_adapter(adapter,
821 NETXEN_MAC_ADDR_CNTL_REG, val);
822
823 }
824
825 if ((first_boot == 0x55555555) &&
826 (NX_IS_REVISION_P2(revision_id))) {
827 /* Unlock the HW, prompting the boot sequence */
828 adapter->pci_write_normalize(adapter,
829 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
830 }
831
832 err = netxen_initialize_adapter_offload(adapter);
833 if (err)
834 goto err_out_iounmap;
835
836 /*
837 * Tell the hardware our version number.
838 */
839 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
840 | ((_NETXEN_NIC_LINUX_MINOR << 8))
841 | (_NETXEN_NIC_LINUX_SUBVERSION);
842 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
843
844 /* Handshake with the card before we register the devices. */
845 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
846
Dhananjay Phadke29566402008-07-21 19:44:04 -0700847 } /* first_driver */
848
849 netxen_nic_flash_print(adapter);
850
Dhananjay Phadked9e651b2008-07-21 19:44:08 -0700851 if (NX_IS_REVISION_P3(revision_id)) {
852 adapter->hw_read_wx(adapter,
853 NETXEN_MIU_MN_CONTROL, &val, 4);
854 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
855 dev_info(&pdev->dev, "firmware running in %s mode\n",
856 adapter->ahw.cut_through ? "cut through" : "legacy");
857 }
858
Dhananjay Phadke29566402008-07-21 19:44:04 -0700859 /*
860 * See if the firmware gave us a virtual-physical port mapping.
861 */
862 adapter->physical_port = adapter->portnum;
863 i = adapter->pci_read_normalize(adapter, CRB_V2P(adapter->portnum));
864 if (i != 0x55555555)
865 adapter->physical_port = i;
866
867 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
868
869 netxen_set_msix_bit(pdev, 0);
870
871 if (NX_IS_REVISION_P3(revision_id)) {
872 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
873 mem_len != NETXEN_PCI_2MB_SIZE)
874 adapter->msix_supported = 0;
875 }
876
877 if (adapter->msix_supported) {
878
879 netxen_init_msix_entries(adapter);
880
881 if (pci_enable_msix(pdev, adapter->msix_entries,
882 MSIX_ENTRIES_PER_ADAPTER))
883 goto request_msi;
884
885 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
886 netxen_set_msix_bit(pdev, 1);
887 dev_info(&pdev->dev, "using msi-x interrupts\n");
888
889 } else {
890request_msi:
891 if (use_msi && !pci_enable_msi(pdev)) {
892 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
893 dev_info(&pdev->dev, "using msi interrupts\n");
894 } else
895 dev_info(&pdev->dev, "using legacy interrupts\n");
896 }
897
898 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
899 netdev->irq = adapter->msix_entries[0].vector;
900 else
901 netdev->irq = pdev->irq;
902
903 err = netxen_receive_peg_ready(adapter);
904 if (err)
905 goto err_out_disable_msi;
906
907 init_timer(&adapter->watchdog_timer);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -0700908 adapter->ahw.linkup = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700909 adapter->watchdog_timer.function = &netxen_watchdog;
910 adapter->watchdog_timer.data = (unsigned long)adapter;
911 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
912 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
913
914 if (netxen_is_flash_supported(adapter) == 0 &&
915 netxen_get_flash_mac_addr(adapter, mac_addr) == 0) {
916 unsigned char *p;
917
918 p = (unsigned char *)&mac_addr[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700919 netdev->dev_addr[0] = *(p + 5);
920 netdev->dev_addr[1] = *(p + 4);
921 netdev->dev_addr[2] = *(p + 3);
922 netdev->dev_addr[3] = *(p + 2);
923 netdev->dev_addr[4] = *(p + 1);
924 netdev->dev_addr[5] = *(p + 0);
925
926 memcpy(netdev->perm_addr, netdev->dev_addr,
927 netdev->addr_len);
928 if (!is_valid_ether_addr(netdev->perm_addr)) {
Joe Perches0795af52007-10-03 17:59:30 -0700929 printk(KERN_ERR "%s: Bad MAC address %s.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700930 netxen_nic_driver_name,
931 print_mac(mac, netdev->dev_addr));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700932 } else {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700933 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700934 }
935 }
936
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700937 netif_carrier_off(netdev);
938 netif_stop_queue(netdev);
939
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700940 if ((err = register_netdev(netdev))) {
941 printk(KERN_ERR "%s: register_netdev failed port #%d"
942 " aborting\n", netxen_nic_driver_name,
943 adapter->portnum);
944 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700945 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700946 }
947
948 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400949
Dhananjay Phadke29566402008-07-21 19:44:04 -0700950 switch (adapter->ahw.board_type) {
951 case NETXEN_NIC_GBE:
952 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
953 adapter->netdev->name);
954 break;
955 case NETXEN_NIC_XGBE:
956 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
957 adapter->netdev->name);
958 break;
959 }
960
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400961 return 0;
962
Dhananjay Phadke29566402008-07-21 19:44:04 -0700963err_out_disable_msi:
964 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
965 pci_disable_msix(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700966 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
967 pci_disable_msi(pdev);
968
Dhananjay Phadke29566402008-07-21 19:44:04 -0700969 if (first_driver)
970 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400971
Dhananjay Phadke29566402008-07-21 19:44:04 -0700972err_out_iounmap:
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800973 if (db_ptr)
974 iounmap(db_ptr);
975
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800976 if (mem_ptr0)
977 iounmap(mem_ptr0);
978 if (mem_ptr1)
979 iounmap(mem_ptr1);
980 if (mem_ptr2)
981 iounmap(mem_ptr2);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800982
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700983err_out_free_netdev:
984 free_netdev(netdev);
985
986err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400987 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700988
989err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -0700990 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400991 pci_disable_device(pdev);
992 return err;
993}
994
995static void __devexit netxen_nic_remove(struct pci_dev *pdev)
996{
997 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700998 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400999
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001000 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001001 if (adapter == NULL)
1002 return;
1003
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001004 netdev = adapter->netdev;
1005
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301006 unregister_netdev(netdev);
1007
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301008 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001009 netxen_free_hw_resources(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001010 netxen_free_sw_resources(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301011 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001012
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001013 if (adapter->portnum == 0)
1014 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301015
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301016 if (adapter->irq)
1017 free_irq(adapter->irq, adapter);
1018
Dhananjay Phadke29566402008-07-21 19:44:04 -07001019 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1020 pci_disable_msix(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301021 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1022 pci_disable_msi(pdev);
1023
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001024 iounmap(adapter->ahw.db_base);
1025 iounmap(adapter->ahw.pci_base0);
1026 iounmap(adapter->ahw.pci_base1);
1027 iounmap(adapter->ahw.pci_base2);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001028
1029 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301030 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001031 pci_set_drvdata(pdev, NULL);
1032
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001033 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001034}
1035
1036/*
1037 * Called when a network interface is made active
1038 * @returns 0 on success, negative value on failure
1039 */
1040static int netxen_nic_open(struct net_device *netdev)
1041{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001042 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001043 int err = 0;
1044 int ctx, ring;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001045 irq_handler_t handler;
1046 unsigned long flags = IRQF_SAMPLE_RANDOM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001047
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001048 if (adapter->driver_mismatch)
1049 return -EIO;
1050
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001051 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1052 err = netxen_init_firmware(adapter);
1053 if (err != 0) {
1054 printk(KERN_ERR "Failed to init firmware\n");
1055 return -EIO;
1056 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001057
Dhananjay Phadke29566402008-07-21 19:44:04 -07001058 err = netxen_alloc_sw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001059 if (err) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001060 printk(KERN_ERR "%s: Error in setting sw resources\n",
1061 netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001062 return err;
1063 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001064
1065 netxen_nic_clear_stats(adapter);
1066
1067 err = netxen_alloc_hw_resources(adapter);
1068 if (err) {
1069 printk(KERN_ERR "%s: Error in setting hw resources\n",
1070 netdev->name);
1071 goto err_out_free_sw;
1072 }
1073
1074 if (adapter->fw_major < 4) {
1075 adapter->crb_addr_cmd_producer =
1076 crb_cmd_producer[adapter->portnum];
1077 adapter->crb_addr_cmd_consumer =
1078 crb_cmd_consumer[adapter->portnum];
1079 }
1080
1081 netxen_nic_update_cmd_producer(adapter, 0);
1082 netxen_nic_update_cmd_consumer(adapter, 0);
1083
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001084 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001085 for (ring = 0; ring < adapter->max_rds_rings; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001086 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001087 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001088 if (NETXEN_IS_MSI_FAMILY(adapter))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001089 handler = netxen_msi_intr;
1090 else {
1091 flags |= IRQF_SHARED;
1092 handler = netxen_intr;
1093 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001094 adapter->irq = netdev->irq;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001095 err = request_irq(adapter->irq, handler,
1096 flags, netdev->name, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001097 if (err) {
1098 printk(KERN_ERR "request_irq failed with: %d\n", err);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001099 goto err_out_free_hw;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001100 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001101
1102 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1103 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001104
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001105 /* Done here again so that even if phantom sw overwrote it,
1106 * we set it */
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001107 err = adapter->init_port(adapter, adapter->physical_port);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001108 if (err) {
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001109 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1110 netxen_nic_driver_name, adapter->portnum);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001111 goto err_out_free_irq;
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001112 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001113 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001114
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001115 netxen_nic_set_link_parameters(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001116
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001117 netdev->set_multicast_list(netdev);
1118 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1119 nx_fw_cmd_set_mtu(adapter, netdev->mtu);
1120 else
1121 adapter->set_mtu(adapter, netdev->mtu);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001122
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001123 mod_timer(&adapter->watchdog_timer, jiffies);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001124
1125 napi_enable(&adapter->napi);
1126 netxen_nic_enable_int(adapter);
1127
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001128 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001129
1130 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001131
1132err_out_free_irq:
1133 free_irq(adapter->irq, adapter);
1134err_out_free_hw:
1135 netxen_free_hw_resources(adapter);
1136err_out_free_sw:
1137 netxen_free_sw_resources(adapter);
1138 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001139}
1140
1141/*
1142 * netxen_nic_close - Disables a network interface entry point
1143 */
1144static int netxen_nic_close(struct net_device *netdev)
1145{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001146 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001147
1148 netif_carrier_off(netdev);
1149 netif_stop_queue(netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001150 napi_disable(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001151
dhananjay@netxen.com72b0a7a2007-12-26 10:23:56 -08001152 if (adapter->stop_port)
1153 adapter->stop_port(adapter);
1154
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301155 netxen_nic_disable_int(adapter);
1156
Dhananjay Phadke29566402008-07-21 19:44:04 -07001157 netxen_release_tx_buffers(adapter);
1158
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301159 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1160 FLUSH_SCHEDULED_WORK();
1161 del_timer_sync(&adapter->watchdog_timer);
1162 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001163
1164 return 0;
1165}
1166
1167static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1168{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001169 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001170 struct netxen_hardware_context *hw = &adapter->ahw;
1171 unsigned int first_seg_len = skb->len - skb->data_len;
1172 struct netxen_skb_frag *buffrag;
1173 unsigned int i;
1174
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001175 u32 producer, consumer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001176 u32 saved_producer = 0;
1177 struct cmd_desc_type0 *hwdesc;
1178 int k;
1179 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001180 int frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001181 int no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001182 u32 num_txd = adapter->max_tx_desc_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001183
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001184 frag_count = skb_shinfo(skb)->nr_frags + 1;
1185
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001186 /* There 4 fragments per descriptor */
1187 no_of_desc = (frag_count + 3) >> 2;
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001188 if (netdev->features & NETIF_F_TSO) {
1189 if (skb_shinfo(skb)->gso_size > 0) {
1190
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001191 no_of_desc++;
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001192 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001193 sizeof(struct ethhdr)) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001194 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001195 no_of_desc++;
1196 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001197 }
1198 }
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001199
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001200 producer = adapter->cmd_producer;
1201 smp_mb();
1202 consumer = adapter->last_cmd_consumer;
1203 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1204 netif_stop_queue(netdev);
1205 smp_mb();
1206 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001207 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001208
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001209 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001210 saved_producer = producer;
1211 hwdesc = &hw->cmd_desc_head[producer];
1212 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1213 /* Take skb->data itself */
1214 pbuf = &adapter->cmd_buf_arr[producer];
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001215 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
Al Viroa608ab92007-01-02 10:39:10 +00001216 pbuf->mss = skb_shinfo(skb)->gso_size;
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001217 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001218 } else {
1219 pbuf->mss = 0;
1220 hwdesc->mss = 0;
1221 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001222 pbuf->total_length = skb->len;
1223 pbuf->skb = skb;
1224 pbuf->cmd = TX_ETHER_PKT;
1225 pbuf->frag_count = frag_count;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001226 pbuf->port = adapter->portnum;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001227 buffrag = &pbuf->frag_array[0];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001228 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001229 PCI_DMA_TODEVICE);
1230 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001231 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1232 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1233 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001234
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001235 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001236 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001237 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1238 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1239
1240 for (i = 1, k = 1; i < frag_count; i++, k++) {
1241 struct skb_frag_struct *frag;
1242 int len, temp_len;
1243 unsigned long offset;
1244 dma_addr_t temp_dma;
1245
1246 /* move to next desc. if there is a need */
1247 if ((i & 0x3) == 0) {
1248 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001249 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001250 hwdesc = &hw->cmd_desc_head[producer];
1251 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001252 pbuf = &adapter->cmd_buf_arr[producer];
1253 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001254 }
1255 frag = &skb_shinfo(skb)->frags[i - 1];
1256 len = frag->size;
1257 offset = frag->page_offset;
1258
1259 temp_len = len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001260 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001261 len, PCI_DMA_TODEVICE);
1262
1263 buffrag++;
1264 buffrag->dma = temp_dma;
1265 buffrag->length = temp_len;
1266
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001267 switch (k) {
1268 case 0:
1269 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1270 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1271 break;
1272 case 1:
1273 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1274 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1275 break;
1276 case 2:
1277 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1278 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1279 break;
1280 case 3:
Al Viroa608ab92007-01-02 10:39:10 +00001281 hwdesc->buffer4_length = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001282 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1283 break;
1284 }
1285 frag++;
1286 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001287 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001288
1289 /* might change opcode to TX_TCP_LSO */
1290 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1291
1292 /* For LSO, we need to copy the MAC/IP/TCP headers into
1293 * the descriptor ring
1294 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001295 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1296 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001297 int hdr_len, first_hdr_len, more_hdr;
1298 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001299 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1300 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001301 more_hdr = 1;
1302 } else {
1303 first_hdr_len = hdr_len;
1304 more_hdr = 0;
1305 }
1306 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1307 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001308 pbuf = &adapter->cmd_buf_arr[producer];
1309 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001310
1311 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001312 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001313 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001314 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001315
1316 if (more_hdr) {
1317 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001318 pbuf = &adapter->cmd_buf_arr[producer];
1319 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001320 /* copy the next 64 bytes - should be enough except
1321 * for pathological case
1322 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001323 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1324 hwdesc,
1325 (hdr_len -
1326 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001327 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001328 }
1329 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001330
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001331 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001332 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001333
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001334 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001335
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001336 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001337 netdev->trans_start = jiffies;
1338
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001339 return NETDEV_TX_OK;
1340}
1341
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001342static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1343{
1344 struct net_device *netdev = adapter->netdev;
1345 uint32_t temp, temp_state, temp_val;
1346 int rv = 0;
1347
1348 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1349
1350 temp_state = nx_get_temp_state(temp);
1351 temp_val = nx_get_temp_val(temp);
1352
1353 if (temp_state == NX_TEMP_PANIC) {
1354 printk(KERN_ALERT
1355 "%s: Device temperature %d degrees C exceeds"
1356 " maximum allowed. Hardware has been shut down.\n",
1357 netxen_nic_driver_name, temp_val);
1358
1359 netif_carrier_off(netdev);
1360 netif_stop_queue(netdev);
1361 rv = 1;
1362 } else if (temp_state == NX_TEMP_WARN) {
1363 if (adapter->temp == NX_TEMP_NORMAL) {
1364 printk(KERN_ALERT
1365 "%s: Device temperature %d degrees C "
1366 "exceeds operating range."
1367 " Immediate action needed.\n",
1368 netxen_nic_driver_name, temp_val);
1369 }
1370 } else {
1371 if (adapter->temp == NX_TEMP_WARN) {
1372 printk(KERN_INFO
1373 "%s: Device temperature is now %d degrees C"
1374 " in normal range.\n", netxen_nic_driver_name,
1375 temp_val);
1376 }
1377 }
1378 adapter->temp = temp_state;
1379 return rv;
1380}
1381
1382static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1383{
1384 struct net_device *netdev = adapter->netdev;
1385 u32 val, port, linkup;
1386
1387 port = adapter->physical_port;
1388
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001389 if (adapter->ahw.board_type == NETXEN_NIC_GBE) {
1390 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001391 linkup = (val >> port) & 1;
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001392 } else {
1393 if (adapter->fw_major < 4) {
1394 val = adapter->pci_read_normalize(adapter,
1395 CRB_XG_STATE);
1396 val = (val >> port*8) & 0xff;
1397 linkup = (val == XG_LINK_UP);
1398 } else {
1399 val = adapter->pci_read_normalize(adapter,
1400 CRB_XG_STATE_P3);
1401 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1402 linkup = (val == XG_LINK_UP_P3);
1403 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001404 }
1405
1406 if (adapter->ahw.linkup && !linkup) {
1407 printk(KERN_INFO "%s: %s NIC Link is down\n",
1408 netxen_nic_driver_name, netdev->name);
1409 adapter->ahw.linkup = 0;
1410 if (netif_running(netdev)) {
1411 netif_carrier_off(netdev);
1412 netif_stop_queue(netdev);
1413 }
1414 } else if (!adapter->ahw.linkup && linkup) {
1415 printk(KERN_INFO "%s: %s NIC Link is up\n",
1416 netxen_nic_driver_name, netdev->name);
1417 adapter->ahw.linkup = 1;
1418 if (netif_running(netdev)) {
1419 netif_carrier_on(netdev);
1420 netif_wake_queue(netdev);
1421 }
1422 }
1423}
1424
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001425static void netxen_watchdog(unsigned long v)
1426{
1427 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001428
1429 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001430}
1431
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001432void netxen_watchdog_task(struct work_struct *work)
1433{
1434 struct netxen_adapter *adapter =
1435 container_of(work, struct netxen_adapter, watchdog_task);
1436
1437 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1438 return;
1439
1440 netxen_nic_handle_phy_intr(adapter);
1441
1442 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1443}
1444
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001445static void netxen_tx_timeout(struct net_device *netdev)
1446{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001447 struct netxen_adapter *adapter = (struct netxen_adapter *)
1448 netdev_priv(netdev);
1449 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001450}
1451
David Howells6d5aefb2006-12-05 19:36:26 +00001452static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001453{
Jeff Garzik47906542007-11-23 21:23:36 -05001454 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001455 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001456
1457 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001458 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001459
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001460 netxen_nic_disable_int(adapter);
1461 napi_disable(&adapter->napi);
1462
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001463 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001464
1465 napi_enable(&adapter->napi);
1466 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001467 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001468}
1469
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001470/*
1471 * netxen_nic_get_stats - Get System Network Statistics
1472 * @netdev: network interface device structure
1473 */
1474struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1475{
1476 struct netxen_adapter *adapter = netdev_priv(netdev);
1477 struct net_device_stats *stats = &adapter->net_stats;
1478
1479 memset(stats, 0, sizeof(*stats));
1480
1481 /* total packets received */
1482 stats->rx_packets = adapter->stats.no_rcv;
1483 /* total packets transmitted */
1484 stats->tx_packets = adapter->stats.xmitedframes +
1485 adapter->stats.xmitfinished;
1486 /* total bytes received */
1487 stats->rx_bytes = adapter->stats.rxbytes;
1488 /* total bytes transmitted */
1489 stats->tx_bytes = adapter->stats.txbytes;
1490 /* bad packets received */
1491 stats->rx_errors = adapter->stats.rcvdbadskb;
1492 /* packet transmit problems */
1493 stats->tx_errors = adapter->stats.nocmddescriptor;
1494 /* no space in linux buffers */
1495 stats->rx_dropped = adapter->stats.rxdropped;
1496 /* no space available in linux */
1497 stats->tx_dropped = adapter->stats.txdropped;
1498
1499 return stats;
1500}
1501
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001502static inline void
1503netxen_handle_int(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001504{
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301505 netxen_nic_disable_int(adapter);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001506 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001507}
1508
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001509static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001510{
Jeff Garzik1daec862007-10-19 03:14:03 -04001511 struct netxen_adapter *adapter = data;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301512 u32 our_int = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001513
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001514 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001515 /* not our interrupt */
1516 if ((our_int & (0x80 << adapter->portnum)) == 0)
1517 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301518
1519 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1520 /* claim interrupt */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001521 adapter->pci_write_normalize(adapter, CRB_INT_VECTOR,
1522 our_int & ~((u32)(0x80 << adapter->portnum)));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301523 }
1524
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001525 netxen_handle_int(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001526
1527 return IRQ_HANDLED;
1528}
1529
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001530static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001531{
1532 struct netxen_adapter *adapter = data;
1533
1534 netxen_handle_int(adapter);
1535 return IRQ_HANDLED;
1536}
1537
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001538static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001539{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001540 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001541 int tx_complete;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001542 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001543 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001544
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001545 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001546
Amit S. Kale80922fb2006-12-04 09:18:00 -08001547 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001548 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1549 /*
1550 * Fairness issue. This will give undue weight to the
1551 * receive context 0.
1552 */
1553
1554 /*
1555 * To avoid starvation, we give each of our receivers,
1556 * a fraction of the quota. Sometimes, it might happen that we
1557 * have enough quota to process every packet, but since all the
1558 * packets are on one context, it gets only half of the quota,
1559 * and ends up not processing it.
1560 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001561 work_done += netxen_process_rcv_ring(adapter, ctx,
1562 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001563 }
1564
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001565 if ((work_done < budget) && tx_complete) {
1566 netif_rx_complete(adapter->netdev, &adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001567 netxen_nic_enable_int(adapter);
1568 }
1569
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001570 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001571}
1572
1573#ifdef CONFIG_NET_POLL_CONTROLLER
1574static void netxen_nic_poll_controller(struct net_device *netdev)
1575{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001576 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001577 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001578 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001579 enable_irq(adapter->irq);
1580}
1581#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001582
1583static struct pci_driver netxen_driver = {
1584 .name = netxen_nic_driver_name,
1585 .id_table = netxen_pci_tbl,
1586 .probe = netxen_nic_probe,
1587 .remove = __devexit_p(netxen_nic_remove)
1588};
1589
1590/* Driver Registration on NetXen card */
1591
1592static int __init netxen_init_module(void)
1593{
Al Viro79ea13c2008-01-24 02:06:46 -08001594 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001595 return -ENOMEM;
1596
Amit S. Kale184231b2007-01-22 06:52:53 -08001597 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001598}
1599
1600module_init(netxen_init_module);
1601
1602static void __exit netxen_exit_module(void)
1603{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001604 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001605 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001606}
1607
1608module_exit(netxen_exit_module);