blob: 7b8688e7bfa3d6bac8317c6aa7249a52c28af261 [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/* Local functions to NetXen NIC driver */
62static int __devinit netxen_nic_probe(struct pci_dev *pdev,
Dhananjay Phadke29566402008-07-21 19:44:04 -070063 const struct pci_device_id *ent);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040064static void __devexit netxen_nic_remove(struct pci_dev *pdev);
65static int netxen_nic_open(struct net_device *netdev);
66static int netxen_nic_close(struct net_device *netdev);
67static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
68static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000069static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040070static void netxen_watchdog(unsigned long);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070071static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040072#ifdef CONFIG_NET_POLL_CONTROLLER
73static void netxen_nic_poll_controller(struct net_device *netdev);
74#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050075static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070076static irqreturn_t netxen_msi_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040077
78/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010079#define ENTRY(device) \
80 {PCI_DEVICE(0x4040, (device)), \
81 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
82
Amit S. Kale3d396eb2006-10-21 15:33:03 -040083static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Al Viro70081ac2008-06-23 02:04:50 +010084 ENTRY(0x0001),
85 ENTRY(0x0002),
86 ENTRY(0x0003),
87 ENTRY(0x0004),
88 ENTRY(0x0005),
89 ENTRY(0x0024),
90 ENTRY(0x0025),
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -070091 ENTRY(0x0100),
Amit S. Kale3d396eb2006-10-21 15:33:03 -040092 {0,}
93};
94
95MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
96
Adrian Bunkb15551302008-03-31 02:22:18 +030097/*
98 * In netxen_nic_down(), we must wait for any pending callback requests into
99 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
100 * reenabled right after it is deleted in netxen_nic_down().
101 * FLUSH_SCHEDULED_WORK() does this synchronization.
102 *
103 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
104 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
105 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
106 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
107 * linkwatch_event() to be executed which also attempts to acquire the rtnl
108 * lock thus causing a deadlock.
109 */
110
111static struct workqueue_struct *netxen_workq;
112#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
113#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
114
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800115static void netxen_watchdog(unsigned long);
116
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700117static uint32_t crb_cmd_producer[4] = {
118 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
119 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
120};
121
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700122void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700123netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
124 uint32_t crb_producer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700125{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700126 adapter->pci_write_normalize(adapter,
127 adapter->crb_addr_cmd_producer, crb_producer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700128}
129
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700130static uint32_t crb_cmd_consumer[4] = {
131 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
132 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
133};
134
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700135static inline void
136netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
137 u32 crb_consumer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700138{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700139 adapter->pci_write_normalize(adapter,
140 adapter->crb_addr_cmd_consumer, crb_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700141}
142
Dhananjay Phadke29566402008-07-21 19:44:04 -0700143static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700144 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700145 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
146 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
147 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700148};
149
Dhananjay Phadke29566402008-07-21 19:44:04 -0700150static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
151
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700152static void netxen_nic_disable_int(struct netxen_adapter *adapter)
153{
Dhananjay Phadke443be792008-03-17 19:59:48 -0700154 u32 mask = 0x7ff;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700155 int retries = 32;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700156 int pci_fn = adapter->ahw.pci_func;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700157
Jeff Garzikf0c88f92008-03-25 23:53:24 -0400158 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700159 adapter->pci_write_normalize(adapter,
160 adapter->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700161
162 if (adapter->intr_scheme != -1 &&
163 adapter->intr_scheme != INTR_SCHEME_PERPORT)
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700164 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700165
Dhananjay Phadke29566402008-07-21 19:44:04 -0700166 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700167 do {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700168 adapter->pci_write_immediate(adapter,
169 ISR_INT_TARGET_STATUS, 0xffffffff);
170 mask = adapter->pci_read_immediate(adapter,
171 ISR_INT_VECTOR);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700172 if (!(mask & 0x80))
173 break;
174 udelay(10);
175 } while (--retries);
176
177 if (!retries) {
178 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
179 netxen_nic_driver_name);
180 }
Dhananjay Phadke443be792008-03-17 19:59:48 -0700181 } else {
182 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700183 adapter->pci_write_immediate(adapter,
184 msi_tgt_status[pci_fn], 0xffffffff);
Dhananjay Phadke443be792008-03-17 19:59:48 -0700185 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700186 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700187}
188
189static void netxen_nic_enable_int(struct netxen_adapter *adapter)
190{
191 u32 mask;
192
193 DPRINTK(1, INFO, "Entered ISR Enable \n");
194
195 if (adapter->intr_scheme != -1 &&
196 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
197 switch (adapter->ahw.board_type) {
198 case NETXEN_NIC_GBE:
199 mask = 0x77b;
200 break;
201 case NETXEN_NIC_XGBE:
202 mask = 0x77f;
203 break;
204 default:
205 mask = 0x7ff;
206 break;
207 }
208
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700209 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700210 }
211
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700212 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700213
Dhananjay Phadke29566402008-07-21 19:44:04 -0700214 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700215 mask = 0xbff;
216 if (adapter->intr_scheme != -1 &&
217 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700218 adapter->pci_write_normalize(adapter,
219 CRB_INT_VECTOR, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700220 }
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700221 adapter->pci_write_immediate(adapter,
222 ISR_INT_TARGET_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700223 }
224
225 DPRINTK(1, INFO, "Done with enable Int\n");
226}
227
Dhananjay Phadke29566402008-07-21 19:44:04 -0700228static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
229{
230 struct pci_dev *pdev = adapter->pdev;
231 int err;
232 uint64_t mask;
233
234#ifdef CONFIG_IA64
235 adapter->dma_mask = DMA_32BIT_MASK;
236#else
237 if (revision_id >= NX_P3_B0) {
238 /* should go to DMA_64BIT_MASK */
239 adapter->dma_mask = DMA_39BIT_MASK;
240 mask = DMA_39BIT_MASK;
241 } else if (revision_id == NX_P3_A2) {
242 adapter->dma_mask = DMA_39BIT_MASK;
243 mask = DMA_39BIT_MASK;
244 } else if (revision_id == NX_P2_C1) {
245 adapter->dma_mask = DMA_35BIT_MASK;
246 mask = DMA_35BIT_MASK;
247 } else {
248 adapter->dma_mask = DMA_32BIT_MASK;
249 mask = DMA_32BIT_MASK;
250 goto set_32_bit_mask;
251 }
252
253 /*
254 * Consistent DMA mask is set to 32 bit because it cannot be set to
255 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
256 * come off this pool.
257 */
258 if (pci_set_dma_mask(pdev, mask) == 0 &&
259 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
260 adapter->pci_using_dac = 1;
261 return 0;
262 }
263#endif /* CONFIG_IA64 */
264
265set_32_bit_mask:
266 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
267 if (!err)
268 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
269 if (err) {
270 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
271 return err;
272 }
273
274 adapter->pci_using_dac = 0;
275 return 0;
276}
277
278static void netxen_check_options(struct netxen_adapter *adapter)
279{
280 switch (adapter->ahw.boardcfg.board_type) {
281 case NETXEN_BRDTYPE_P3_HMEZ:
282 case NETXEN_BRDTYPE_P3_XG_LOM:
283 case NETXEN_BRDTYPE_P3_10G_CX4:
284 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
285 case NETXEN_BRDTYPE_P3_IMEZ:
286 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
287 case NETXEN_BRDTYPE_P3_10G_XFP:
288 case NETXEN_BRDTYPE_P3_10000_BASE_T:
289 adapter->msix_supported = !!use_msi_x;
290 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
291 break;
292
293 case NETXEN_BRDTYPE_P2_SB31_10G:
294 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
295 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
296 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
297 adapter->msix_supported = 0;
298 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
299 break;
300
301 case NETXEN_BRDTYPE_P3_REF_QG:
302 case NETXEN_BRDTYPE_P3_4_GB:
303 case NETXEN_BRDTYPE_P3_4_GB_MM:
304 case NETXEN_BRDTYPE_P2_SB35_4G:
305 case NETXEN_BRDTYPE_P2_SB31_2G:
306 adapter->msix_supported = 0;
307 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
308 break;
309
310 default:
311 adapter->msix_supported = 0;
312 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
313
314 printk(KERN_WARNING "Unknown board type(0x%x)\n",
315 adapter->ahw.boardcfg.board_type);
316 break;
317 }
318
319 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
320 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
321 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
322
323 adapter->max_possible_rss_rings = 1;
324 return;
325}
326
327static int
328netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
329{
330 int ret = 0;
331
332 if (first_boot == 0x55555555) {
333 /* This is the first boot after power up */
334
335 /* PCI bus master workaround */
336 adapter->hw_read_wx(adapter,
337 NETXEN_PCIE_REG(0x4), &first_boot, 4);
338 if (!(first_boot & 0x4)) {
339 first_boot |= 0x4;
340 adapter->hw_write_wx(adapter,
341 NETXEN_PCIE_REG(0x4), &first_boot, 4);
342 adapter->hw_read_wx(adapter,
343 NETXEN_PCIE_REG(0x4), &first_boot, 4);
344 }
345
346 /* This is the first boot after power up */
347 adapter->hw_read_wx(adapter,
348 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
349 if (first_boot != 0x80000f) {
350 /* clear the register for future unloads/loads */
351 adapter->pci_write_normalize(adapter,
352 NETXEN_CAM_RAM(0x1fc), 0);
353 ret = -1;
354 }
355
356 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
357 /* Start P2 boot loader */
358 adapter->pci_write_normalize(adapter,
359 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
360 adapter->pci_write_normalize(adapter,
361 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
362 }
363 }
364 return ret;
365}
366
367static void netxen_set_port_mode(struct netxen_adapter *adapter)
368{
369 u32 val, data;
370
371 val = adapter->ahw.boardcfg.board_type;
372 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
373 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
374 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
375 data = NETXEN_PORT_MODE_802_3_AP;
376 adapter->hw_write_wx(adapter,
377 NETXEN_PORT_MODE_ADDR, &data, 4);
378 } else if (port_mode == NETXEN_PORT_MODE_XG) {
379 data = NETXEN_PORT_MODE_XG;
380 adapter->hw_write_wx(adapter,
381 NETXEN_PORT_MODE_ADDR, &data, 4);
382 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
383 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
384 adapter->hw_write_wx(adapter,
385 NETXEN_PORT_MODE_ADDR, &data, 4);
386 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
387 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
388 adapter->hw_write_wx(adapter,
389 NETXEN_PORT_MODE_ADDR, &data, 4);
390 } else {
391 data = NETXEN_PORT_MODE_AUTO_NEG;
392 adapter->hw_write_wx(adapter,
393 NETXEN_PORT_MODE_ADDR, &data, 4);
394 }
395
396 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
397 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
398 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
399 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
400 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
401 }
402 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
403 &wol_port_mode, 4);
404 }
405}
406
407#define PCI_CAP_ID_GEN 0x10
408
409static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
410{
411 u32 pdevfuncsave;
412 u32 c8c9value = 0;
413 u32 chicken = 0;
414 u32 control = 0;
415 int i, pos;
416 struct pci_dev *pdev;
417
418 pdev = pci_get_device(0x1166, 0x0140, NULL);
419 if (pdev) {
420 pci_dev_put(pdev);
421 adapter->hw_read_wx(adapter,
422 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
423 chicken |= 0x4000;
424 adapter->hw_write_wx(adapter,
425 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
426 }
427
428 pdev = adapter->pdev;
429
430 adapter->hw_read_wx(adapter,
431 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
432 /* clear chicken3.25:24 */
433 chicken &= 0xFCFFFFFF;
434 /*
435 * if gen1 and B0, set F1020 - if gen 2, do nothing
436 * if gen2 set to F1000
437 */
438 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
439 if (pos == 0xC0) {
440 pci_read_config_dword(pdev, pos + 0x10, &control);
441 if ((control & 0x000F0000) != 0x00020000) {
442 /* set chicken3.24 if gen1 */
443 chicken |= 0x01000000;
444 }
445 printk(KERN_INFO "%s Gen2 strapping detected\n",
446 netxen_nic_driver_name);
447 c8c9value = 0xF1000;
448 } else {
449 /* set chicken3.24 if gen1 */
450 chicken |= 0x01000000;
451 printk(KERN_INFO "%s Gen1 strapping detected\n",
452 netxen_nic_driver_name);
453 if (adapter->ahw.revision_id == NX_P3_B0)
454 c8c9value = 0xF1020;
455 else
456 c8c9value = 0;
457
458 }
459 adapter->hw_write_wx(adapter,
460 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
461
462 if (!c8c9value)
463 return;
464
465 pdevfuncsave = pdev->devfn;
466 if (pdevfuncsave & 0x07)
467 return;
468
469 for (i = 0; i < 8; i++) {
470 pci_read_config_dword(pdev, pos + 8, &control);
471 pci_read_config_dword(pdev, pos + 8, &control);
472 pci_write_config_dword(pdev, pos + 8, c8c9value);
473 pdev->devfn++;
474 }
475 pdev->devfn = pdevfuncsave;
476}
477
478static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
479{
480 u32 control;
481 int pos;
482
483 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
484 if (pos) {
485 pci_read_config_dword(pdev, pos, &control);
486 if (enable)
487 control |= PCI_MSIX_FLAGS_ENABLE;
488 else
489 control = 0;
490 pci_write_config_dword(pdev, pos, control);
491 }
492}
493
494static void netxen_init_msix_entries(struct netxen_adapter *adapter)
495{
496 int i;
497
498 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
499 adapter->msix_entries[i].entry = i;
500}
501
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400502/*
503 * netxen_nic_probe()
504 *
505 * The Linux system will invoke this after identifying the vendor ID and
506 * device Id in the pci_tbl supported by this module.
507 *
508 * A quad port card has one operational PCI config space, (function 0),
509 * which is used to access all four ports.
510 *
511 * This routine will initialize the adapter, and setup the global parameters
512 * along with the port's specific structure.
513 */
514static int __devinit
515netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
516{
517 struct net_device *netdev = NULL;
518 struct netxen_adapter *adapter = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800519 void __iomem *mem_ptr0 = NULL;
520 void __iomem *mem_ptr1 = NULL;
521 void __iomem *mem_ptr2 = NULL;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700522 unsigned long first_page_group_end;
523 unsigned long first_page_group_start;
524
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800525
Al Viroa608ab9c2007-01-02 10:39:10 +0000526 u8 __iomem *db_ptr = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700527 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
528 int i = 0, err;
529 int first_driver, first_boot;
Al Virof305f782007-12-22 19:44:00 +0000530 __le64 mac_addr[FLASH_NUM_PORTS + 1];
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700531 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700532 int pci_func_id = PCI_FUNC(pdev->devfn);
Joe Perches0795af52007-10-03 17:59:30 -0700533 DECLARE_MAC_BUF(mac);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700534 struct netxen_legacy_intr_set *legacy_intrp;
535 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400536
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700537 if (pci_func_id == 0)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700538 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700539
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700540 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700541 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700542 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800543 return -ENODEV;
544 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700545
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400546 if ((err = pci_enable_device(pdev)))
547 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700548
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400549 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
550 err = -ENODEV;
551 goto err_out_disable_pdev;
552 }
553
554 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
555 goto err_out_disable_pdev;
556
557 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700558
559 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
560 if(!netdev) {
561 printk(KERN_ERR"%s: Failed to allocate memory for the "
562 "device block.Check system memory resource"
563 " usage.\n", netxen_nic_driver_name);
564 goto err_out_free_res;
565 }
566
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700567 SET_NETDEV_DEV(netdev, &pdev->dev);
568
569 adapter = netdev->priv;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700570 adapter->netdev = netdev;
571 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700572 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700573
574 revision_id = pdev->revision;
575 adapter->ahw.revision_id = revision_id;
576
577 err = nx_set_dma_mask(adapter, revision_id);
578 if (err)
579 goto err_out_free_netdev;
580
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700581 rwlock_init(&adapter->adapter_lock);
582 adapter->ahw.qdr_sn_window = -1;
583 adapter->ahw.ddr_mn_window = -1;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700584
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400585 /* remap phys address */
586 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
587 mem_len = pci_resource_len(pdev, 0);
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700588 pci_len0 = 0;
589
590 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
591 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
592 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
593 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
594 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
595 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
596 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
597 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
598 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400599
600 /* 128 Meg of memory */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700601 if (mem_len == NETXEN_PCI_128MB_SIZE) {
602 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
603 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
604 SECOND_PAGE_GROUP_SIZE);
605 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
606 THIRD_PAGE_GROUP_SIZE);
607 first_page_group_start = FIRST_PAGE_GROUP_START;
608 first_page_group_end = FIRST_PAGE_GROUP_END;
609 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
610 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
611 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
612 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
613 first_page_group_start = 0;
614 first_page_group_end = 0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700615 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
616 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
617 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
618 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
619 adapter->pci_write_immediate =
620 netxen_nic_pci_write_immediate_2M;
621 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
622 adapter->pci_write_normalize =
623 netxen_nic_pci_write_normalize_2M;
624 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
625 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
626 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
627
628 mem_ptr0 = ioremap(mem_base, mem_len);
629 pci_len0 = mem_len;
630 first_page_group_start = 0;
631 first_page_group_end = 0;
632
633 adapter->ahw.ddr_mn_window = 0;
634 adapter->ahw.qdr_sn_window = 0;
635
636 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
637 (pci_func_id * 0x20);
638 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
639 if (pci_func_id < 4)
640 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
641 else
642 adapter->ahw.ms_win_crb +=
643 0xA0 + ((pci_func_id - 4) * 0x10);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700644 } else {
Jeff Garzik47906542007-11-23 21:23:36 -0500645 err = -EIO;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700646 goto err_out_free_netdev;
647 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800648
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700649 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800650
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800651 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
652 db_len = pci_resource_len(pdev, 4);
653
654 if (db_len == 0) {
655 printk(KERN_ERR "%s: doorbell is disabled\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700656 netxen_nic_driver_name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800657 err = -EIO;
658 goto err_out_iounmap;
659 }
660 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
661 db_len);
662
663 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
Al Viroa608ab9c2007-01-02 10:39:10 +0000664 if (!db_ptr) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800665 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700666 netxen_nic_driver_name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800667 err = -EIO;
668 goto err_out_iounmap;
669 }
670 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400671
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700672 adapter->ahw.pci_base0 = mem_ptr0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700673 adapter->ahw.pci_len0 = pci_len0;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700674 adapter->ahw.first_page_group_start = first_page_group_start;
675 adapter->ahw.first_page_group_end = first_page_group_end;
676 adapter->ahw.pci_base1 = mem_ptr1;
677 adapter->ahw.pci_base2 = mem_ptr2;
678 adapter->ahw.db_base = db_ptr;
679 adapter->ahw.db_len = db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400680
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700681 netif_napi_add(netdev, &adapter->napi,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700682 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
683
684 if (revision_id >= NX_P3_B0)
685 legacy_intrp = &legacy_intr[pci_func_id];
686 else
687 legacy_intrp = &legacy_intr[0];
688
689 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
690 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
691 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
692 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700693
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530694 /* this will be read from FW later */
695 adapter->intr_scheme = -1;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700696 adapter->msi_mode = -1;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530697
698 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700699 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530700 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530701 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -0700702 adapter->mc_enabled = 0;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700703 if (NX_IS_REVISION_P3(revision_id)) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700704 adapter->max_mc_count = 38;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700705 adapter->max_rds_rings = 2;
706 } else {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700707 adapter->max_mc_count = 16;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700708 adapter->max_rds_rings = 3;
709 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700710
711 netdev->open = netxen_nic_open;
712 netdev->stop = netxen_nic_close;
713 netdev->hard_start_xmit = netxen_nic_xmit_frame;
Jeff Garzik47906542007-11-23 21:23:36 -0500714 netdev->get_stats = netxen_nic_get_stats;
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700715 if (NX_IS_REVISION_P3(revision_id))
716 netdev->set_multicast_list = netxen_p3_nic_set_multi;
717 else
718 netdev->set_multicast_list = netxen_p2_nic_set_multi;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700719 netdev->set_mac_address = netxen_nic_set_mac;
720 netdev->change_mtu = netxen_nic_change_mtu;
721 netdev->tx_timeout = netxen_tx_timeout;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700722 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700723
724 netxen_nic_change_mtu(netdev, netdev->mtu);
725
726 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700727#ifdef CONFIG_NET_POLL_CONTROLLER
728 netdev->poll_controller = netxen_nic_poll_controller;
729#endif
730 /* ScatterGather support */
731 netdev->features = NETIF_F_SG;
732 netdev->features |= NETIF_F_IP_CSUM;
733 netdev->features |= NETIF_F_TSO;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700734 if (NX_IS_REVISION_P3(revision_id)) {
735 netdev->features |= NETIF_F_IPV6_CSUM;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700736 netdev->features |= NETIF_F_TSO6;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700737 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700738
Dhananjay Phadke29566402008-07-21 19:44:04 -0700739 if (adapter->pci_using_dac)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700740 netdev->features |= NETIF_F_HIGHDMA;
741
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700742 /*
743 * Set the CRB window to invalid. If any register in window 0 is
744 * accessed it should set the window to 0 and then reset it to 1.
745 */
746 adapter->curr_window = 255;
747
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700748 if (netxen_nic_get_board_info(adapter) != 0) {
749 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700750 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700751 err = -EIO;
752 goto err_out_iounmap;
753 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700754
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700755 netxen_initialize_adapter_ops(adapter);
756
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700757 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700758 switch (adapter->ahw.boardcfg.board_type) {
759 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
760 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
761 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700762 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700763 break;
764 default:
765 break;
766 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700767
Dhananjay Phadke29566402008-07-21 19:44:04 -0700768 /*
769 * This call will setup various max rx/tx counts.
770 * It must be done before any buffer/ring allocations.
771 */
772 netxen_check_options(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400773
Dhananjay Phadke29566402008-07-21 19:44:04 -0700774 first_driver = 0;
775 if (NX_IS_REVISION_P3(revision_id)) {
776 if (adapter->ahw.pci_func == 0)
777 first_driver = 1;
778 } else {
779 if (adapter->portnum == 0)
780 first_driver = 1;
781 }
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700782 adapter->crb_addr_cmd_producer = crb_cmd_producer[adapter->portnum];
783 adapter->crb_addr_cmd_consumer = crb_cmd_consumer[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700784 netxen_nic_update_cmd_producer(adapter, 0);
785 netxen_nic_update_cmd_consumer(adapter, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400786
Dhananjay Phadke29566402008-07-21 19:44:04 -0700787 if (first_driver) {
788 first_boot = adapter->pci_read_normalize(adapter,
789 NETXEN_CAM_RAM(0x1fc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400790
Dhananjay Phadke29566402008-07-21 19:44:04 -0700791 err = netxen_check_hw_init(adapter, first_boot);
792 if (err) {
793 printk(KERN_ERR "%s: error in init HW init sequence\n",
794 netxen_nic_driver_name);
795 goto err_out_iounmap;
796 }
797
798 if (NX_IS_REVISION_P3(revision_id))
799 netxen_set_port_mode(adapter);
800
801 if (first_boot != 0x55555555) {
802 adapter->pci_write_normalize(adapter,
803 CRB_CMDPEG_STATE, 0);
804 netxen_pinit_from_rom(adapter, 0);
805 msleep(1);
806 netxen_load_firmware(adapter);
807 }
808
809 if (NX_IS_REVISION_P3(revision_id))
810 netxen_pcie_strap_init(adapter);
811
812 if (NX_IS_REVISION_P2(revision_id)) {
813
814 /* Initialize multicast addr pool owners */
815 val = 0x7654;
816 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
817 val |= 0x0f000000;
818 netxen_crb_writelit_adapter(adapter,
819 NETXEN_MAC_ADDR_CNTL_REG, val);
820
821 }
822
823 if ((first_boot == 0x55555555) &&
824 (NX_IS_REVISION_P2(revision_id))) {
825 /* Unlock the HW, prompting the boot sequence */
826 adapter->pci_write_normalize(adapter,
827 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
828 }
829
830 err = netxen_initialize_adapter_offload(adapter);
831 if (err)
832 goto err_out_iounmap;
833
834 /*
835 * Tell the hardware our version number.
836 */
837 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
838 | ((_NETXEN_NIC_LINUX_MINOR << 8))
839 | (_NETXEN_NIC_LINUX_SUBVERSION);
840 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
841
842 /* Handshake with the card before we register the devices. */
843 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
844
Dhananjay Phadke29566402008-07-21 19:44:04 -0700845 } /* first_driver */
846
847 netxen_nic_flash_print(adapter);
848
Dhananjay Phadked9e651b2008-07-21 19:44:08 -0700849 if (NX_IS_REVISION_P3(revision_id)) {
850 adapter->hw_read_wx(adapter,
851 NETXEN_MIU_MN_CONTROL, &val, 4);
852 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
853 dev_info(&pdev->dev, "firmware running in %s mode\n",
854 adapter->ahw.cut_through ? "cut through" : "legacy");
855 }
856
Dhananjay Phadke29566402008-07-21 19:44:04 -0700857 /*
858 * See if the firmware gave us a virtual-physical port mapping.
859 */
860 adapter->physical_port = adapter->portnum;
861 i = adapter->pci_read_normalize(adapter, CRB_V2P(adapter->portnum));
862 if (i != 0x55555555)
863 adapter->physical_port = i;
864
865 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
866
867 netxen_set_msix_bit(pdev, 0);
868
869 if (NX_IS_REVISION_P3(revision_id)) {
870 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
871 mem_len != NETXEN_PCI_2MB_SIZE)
872 adapter->msix_supported = 0;
873 }
874
875 if (adapter->msix_supported) {
876
877 netxen_init_msix_entries(adapter);
878
879 if (pci_enable_msix(pdev, adapter->msix_entries,
880 MSIX_ENTRIES_PER_ADAPTER))
881 goto request_msi;
882
883 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
884 netxen_set_msix_bit(pdev, 1);
885 dev_info(&pdev->dev, "using msi-x interrupts\n");
886
887 } else {
888request_msi:
889 if (use_msi && !pci_enable_msi(pdev)) {
890 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
891 dev_info(&pdev->dev, "using msi interrupts\n");
892 } else
893 dev_info(&pdev->dev, "using legacy interrupts\n");
894 }
895
896 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
897 netdev->irq = adapter->msix_entries[0].vector;
898 else
899 netdev->irq = pdev->irq;
900
901 err = netxen_receive_peg_ready(adapter);
902 if (err)
903 goto err_out_disable_msi;
904
905 init_timer(&adapter->watchdog_timer);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -0700906 adapter->ahw.linkup = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700907 adapter->watchdog_timer.function = &netxen_watchdog;
908 adapter->watchdog_timer.data = (unsigned long)adapter;
909 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
910 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
911
912 if (netxen_is_flash_supported(adapter) == 0 &&
913 netxen_get_flash_mac_addr(adapter, mac_addr) == 0) {
914 unsigned char *p;
915
916 p = (unsigned char *)&mac_addr[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700917 netdev->dev_addr[0] = *(p + 5);
918 netdev->dev_addr[1] = *(p + 4);
919 netdev->dev_addr[2] = *(p + 3);
920 netdev->dev_addr[3] = *(p + 2);
921 netdev->dev_addr[4] = *(p + 1);
922 netdev->dev_addr[5] = *(p + 0);
923
924 memcpy(netdev->perm_addr, netdev->dev_addr,
925 netdev->addr_len);
926 if (!is_valid_ether_addr(netdev->perm_addr)) {
Joe Perches0795af52007-10-03 17:59:30 -0700927 printk(KERN_ERR "%s: Bad MAC address %s.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700928 netxen_nic_driver_name,
929 print_mac(mac, netdev->dev_addr));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700930 } else {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700931 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700932 }
933 }
934
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700935 netif_carrier_off(netdev);
936 netif_stop_queue(netdev);
937
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700938 if ((err = register_netdev(netdev))) {
939 printk(KERN_ERR "%s: register_netdev failed port #%d"
940 " aborting\n", netxen_nic_driver_name,
941 adapter->portnum);
942 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700943 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700944 }
945
946 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400947
Dhananjay Phadke29566402008-07-21 19:44:04 -0700948 switch (adapter->ahw.board_type) {
949 case NETXEN_NIC_GBE:
950 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
951 adapter->netdev->name);
952 break;
953 case NETXEN_NIC_XGBE:
954 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
955 adapter->netdev->name);
956 break;
957 }
958
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400959 return 0;
960
Dhananjay Phadke29566402008-07-21 19:44:04 -0700961err_out_disable_msi:
962 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
963 pci_disable_msix(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700964 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
965 pci_disable_msi(pdev);
966
Dhananjay Phadke29566402008-07-21 19:44:04 -0700967 if (first_driver)
968 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400969
Dhananjay Phadke29566402008-07-21 19:44:04 -0700970err_out_iounmap:
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800971 if (db_ptr)
972 iounmap(db_ptr);
973
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800974 if (mem_ptr0)
975 iounmap(mem_ptr0);
976 if (mem_ptr1)
977 iounmap(mem_ptr1);
978 if (mem_ptr2)
979 iounmap(mem_ptr2);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800980
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700981err_out_free_netdev:
982 free_netdev(netdev);
983
984err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400985 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700986
987err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -0700988 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400989 pci_disable_device(pdev);
990 return err;
991}
992
993static void __devexit netxen_nic_remove(struct pci_dev *pdev)
994{
995 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700996 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400997
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700998 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400999 if (adapter == NULL)
1000 return;
1001
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001002 netdev = adapter->netdev;
1003
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301004 unregister_netdev(netdev);
1005
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301006 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001007 netxen_free_hw_resources(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001008 netxen_free_sw_resources(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301009 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001010
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001011 if (adapter->portnum == 0)
1012 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301013
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301014 if (adapter->irq)
1015 free_irq(adapter->irq, adapter);
1016
Dhananjay Phadke29566402008-07-21 19:44:04 -07001017 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1018 pci_disable_msix(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301019 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1020 pci_disable_msi(pdev);
1021
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001022 iounmap(adapter->ahw.db_base);
1023 iounmap(adapter->ahw.pci_base0);
1024 iounmap(adapter->ahw.pci_base1);
1025 iounmap(adapter->ahw.pci_base2);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001026
1027 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301028 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001029 pci_set_drvdata(pdev, NULL);
1030
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001031 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001032}
1033
1034/*
1035 * Called when a network interface is made active
1036 * @returns 0 on success, negative value on failure
1037 */
1038static int netxen_nic_open(struct net_device *netdev)
1039{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001040 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001041 int err = 0;
1042 int ctx, ring;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001043 irq_handler_t handler;
1044 unsigned long flags = IRQF_SAMPLE_RANDOM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001045
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001046 if (adapter->driver_mismatch)
1047 return -EIO;
1048
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001049 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1050 err = netxen_init_firmware(adapter);
1051 if (err != 0) {
1052 printk(KERN_ERR "Failed to init firmware\n");
1053 return -EIO;
1054 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001055
Dhananjay Phadke29566402008-07-21 19:44:04 -07001056 err = netxen_alloc_sw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001057 if (err) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001058 printk(KERN_ERR "%s: Error in setting sw resources\n",
1059 netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001060 return err;
1061 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001062
1063 netxen_nic_clear_stats(adapter);
1064
1065 err = netxen_alloc_hw_resources(adapter);
1066 if (err) {
1067 printk(KERN_ERR "%s: Error in setting hw resources\n",
1068 netdev->name);
1069 goto err_out_free_sw;
1070 }
1071
1072 if (adapter->fw_major < 4) {
1073 adapter->crb_addr_cmd_producer =
1074 crb_cmd_producer[adapter->portnum];
1075 adapter->crb_addr_cmd_consumer =
1076 crb_cmd_consumer[adapter->portnum];
1077 }
1078
1079 netxen_nic_update_cmd_producer(adapter, 0);
1080 netxen_nic_update_cmd_consumer(adapter, 0);
1081
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001082 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001083 for (ring = 0; ring < adapter->max_rds_rings; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001084 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001085 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001086 if (NETXEN_IS_MSI_FAMILY(adapter))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001087 handler = netxen_msi_intr;
1088 else {
1089 flags |= IRQF_SHARED;
1090 handler = netxen_intr;
1091 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001092 adapter->irq = netdev->irq;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001093 err = request_irq(adapter->irq, handler,
1094 flags, netdev->name, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001095 if (err) {
1096 printk(KERN_ERR "request_irq failed with: %d\n", err);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001097 goto err_out_free_hw;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001098 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001099
1100 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1101 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001102
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001103 /* Done here again so that even if phantom sw overwrote it,
1104 * we set it */
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001105 err = adapter->init_port(adapter, adapter->physical_port);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001106 if (err) {
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001107 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1108 netxen_nic_driver_name, adapter->portnum);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001109 goto err_out_free_irq;
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001110 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001111 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001112
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001113 netxen_nic_set_link_parameters(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001114
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001115 netdev->set_multicast_list(netdev);
1116 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1117 nx_fw_cmd_set_mtu(adapter, netdev->mtu);
1118 else
1119 adapter->set_mtu(adapter, netdev->mtu);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001120
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001121 mod_timer(&adapter->watchdog_timer, jiffies);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001122
1123 napi_enable(&adapter->napi);
1124 netxen_nic_enable_int(adapter);
1125
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001126 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001127
1128 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001129
1130err_out_free_irq:
1131 free_irq(adapter->irq, adapter);
1132err_out_free_hw:
1133 netxen_free_hw_resources(adapter);
1134err_out_free_sw:
1135 netxen_free_sw_resources(adapter);
1136 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001137}
1138
1139/*
1140 * netxen_nic_close - Disables a network interface entry point
1141 */
1142static int netxen_nic_close(struct net_device *netdev)
1143{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001144 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001145
1146 netif_carrier_off(netdev);
1147 netif_stop_queue(netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001148 napi_disable(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001149
dhananjay@netxen.com72b0a7a2007-12-26 10:23:56 -08001150 if (adapter->stop_port)
1151 adapter->stop_port(adapter);
1152
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301153 netxen_nic_disable_int(adapter);
1154
Dhananjay Phadke29566402008-07-21 19:44:04 -07001155 netxen_release_tx_buffers(adapter);
1156
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301157 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1158 FLUSH_SCHEDULED_WORK();
1159 del_timer_sync(&adapter->watchdog_timer);
1160 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001161
1162 return 0;
1163}
1164
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001165void netxen_tso_check(struct netxen_adapter *adapter,
1166 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1167{
1168 if (desc->mss) {
1169 desc->total_hdr_length = (sizeof(struct ethhdr) +
1170 ip_hdrlen(skb) + tcp_hdrlen(skb));
1171
1172 if ((NX_IS_REVISION_P3(adapter->ahw.revision_id)) &&
1173 (skb->protocol == htons(ETH_P_IPV6)))
1174 netxen_set_cmd_desc_opcode(desc, TX_TCP_LSO6);
1175 else
1176 netxen_set_cmd_desc_opcode(desc, TX_TCP_LSO);
1177
1178 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1179 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1180 netxen_set_cmd_desc_opcode(desc, TX_TCP_PKT);
1181 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1182 netxen_set_cmd_desc_opcode(desc, TX_UDP_PKT);
1183 else
1184 return;
1185 }
1186 desc->tcp_hdr_offset = skb_transport_offset(skb);
1187 desc->ip_hdr_offset = skb_network_offset(skb);
1188}
1189
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001190static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1191{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001192 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001193 struct netxen_hardware_context *hw = &adapter->ahw;
1194 unsigned int first_seg_len = skb->len - skb->data_len;
1195 struct netxen_skb_frag *buffrag;
1196 unsigned int i;
1197
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001198 u32 producer, consumer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001199 u32 saved_producer = 0;
1200 struct cmd_desc_type0 *hwdesc;
1201 int k;
1202 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001203 int frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001204 int no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001205 u32 num_txd = adapter->max_tx_desc_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001206
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001207 frag_count = skb_shinfo(skb)->nr_frags + 1;
1208
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001209 /* There 4 fragments per descriptor */
1210 no_of_desc = (frag_count + 3) >> 2;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001211 if (netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001212 if (skb_shinfo(skb)->gso_size > 0) {
1213
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001214 no_of_desc++;
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001215 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001216 sizeof(struct ethhdr)) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001217 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001218 no_of_desc++;
1219 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001220 }
1221 }
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001222
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001223 producer = adapter->cmd_producer;
1224 smp_mb();
1225 consumer = adapter->last_cmd_consumer;
1226 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1227 netif_stop_queue(netdev);
1228 smp_mb();
1229 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001230 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001231
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001232 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001233 saved_producer = producer;
1234 hwdesc = &hw->cmd_desc_head[producer];
1235 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1236 /* Take skb->data itself */
1237 pbuf = &adapter->cmd_buf_arr[producer];
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001238 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1239 skb_shinfo(skb)->gso_size > 0) {
Al Viroa608ab9c2007-01-02 10:39:10 +00001240 pbuf->mss = skb_shinfo(skb)->gso_size;
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001241 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001242 } else {
1243 pbuf->mss = 0;
1244 hwdesc->mss = 0;
1245 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001246 pbuf->total_length = skb->len;
1247 pbuf->skb = skb;
1248 pbuf->cmd = TX_ETHER_PKT;
1249 pbuf->frag_count = frag_count;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001250 pbuf->port = adapter->portnum;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001251 buffrag = &pbuf->frag_array[0];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001252 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001253 PCI_DMA_TODEVICE);
1254 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001255 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1256 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1257 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001258
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001259 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001260 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001261 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1262 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1263
1264 for (i = 1, k = 1; i < frag_count; i++, k++) {
1265 struct skb_frag_struct *frag;
1266 int len, temp_len;
1267 unsigned long offset;
1268 dma_addr_t temp_dma;
1269
1270 /* move to next desc. if there is a need */
1271 if ((i & 0x3) == 0) {
1272 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001273 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001274 hwdesc = &hw->cmd_desc_head[producer];
1275 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001276 pbuf = &adapter->cmd_buf_arr[producer];
1277 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001278 }
1279 frag = &skb_shinfo(skb)->frags[i - 1];
1280 len = frag->size;
1281 offset = frag->page_offset;
1282
1283 temp_len = len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001284 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001285 len, PCI_DMA_TODEVICE);
1286
1287 buffrag++;
1288 buffrag->dma = temp_dma;
1289 buffrag->length = temp_len;
1290
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001291 switch (k) {
1292 case 0:
1293 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1294 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1295 break;
1296 case 1:
1297 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1298 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1299 break;
1300 case 2:
1301 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1302 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1303 break;
1304 case 3:
Al Viroa608ab9c2007-01-02 10:39:10 +00001305 hwdesc->buffer4_length = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001306 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1307 break;
1308 }
1309 frag++;
1310 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001311 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001312
1313 /* might change opcode to TX_TCP_LSO */
1314 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1315
1316 /* For LSO, we need to copy the MAC/IP/TCP headers into
1317 * the descriptor ring
1318 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001319 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1320 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001321 int hdr_len, first_hdr_len, more_hdr;
1322 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001323 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1324 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001325 more_hdr = 1;
1326 } else {
1327 first_hdr_len = hdr_len;
1328 more_hdr = 0;
1329 }
1330 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1331 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001332 pbuf = &adapter->cmd_buf_arr[producer];
1333 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001334
1335 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001336 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001337 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001338 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001339
1340 if (more_hdr) {
1341 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001342 pbuf = &adapter->cmd_buf_arr[producer];
1343 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001344 /* copy the next 64 bytes - should be enough except
1345 * for pathological case
1346 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001347 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1348 hwdesc,
1349 (hdr_len -
1350 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001351 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001352 }
1353 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001354
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001355 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001356 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001357
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001358 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001359
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001360 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001361 netdev->trans_start = jiffies;
1362
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001363 return NETDEV_TX_OK;
1364}
1365
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001366static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1367{
1368 struct net_device *netdev = adapter->netdev;
1369 uint32_t temp, temp_state, temp_val;
1370 int rv = 0;
1371
1372 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1373
1374 temp_state = nx_get_temp_state(temp);
1375 temp_val = nx_get_temp_val(temp);
1376
1377 if (temp_state == NX_TEMP_PANIC) {
1378 printk(KERN_ALERT
1379 "%s: Device temperature %d degrees C exceeds"
1380 " maximum allowed. Hardware has been shut down.\n",
1381 netxen_nic_driver_name, temp_val);
1382
1383 netif_carrier_off(netdev);
1384 netif_stop_queue(netdev);
1385 rv = 1;
1386 } else if (temp_state == NX_TEMP_WARN) {
1387 if (adapter->temp == NX_TEMP_NORMAL) {
1388 printk(KERN_ALERT
1389 "%s: Device temperature %d degrees C "
1390 "exceeds operating range."
1391 " Immediate action needed.\n",
1392 netxen_nic_driver_name, temp_val);
1393 }
1394 } else {
1395 if (adapter->temp == NX_TEMP_WARN) {
1396 printk(KERN_INFO
1397 "%s: Device temperature is now %d degrees C"
1398 " in normal range.\n", netxen_nic_driver_name,
1399 temp_val);
1400 }
1401 }
1402 adapter->temp = temp_state;
1403 return rv;
1404}
1405
1406static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1407{
1408 struct net_device *netdev = adapter->netdev;
1409 u32 val, port, linkup;
1410
1411 port = adapter->physical_port;
1412
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001413 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1414 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1415 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1416 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001417 } else {
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001418 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1419 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1420 linkup = (val >> port) & 1;
1421 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001422 val = (val >> port*8) & 0xff;
1423 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001424 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001425 }
1426
1427 if (adapter->ahw.linkup && !linkup) {
1428 printk(KERN_INFO "%s: %s NIC Link is down\n",
1429 netxen_nic_driver_name, netdev->name);
1430 adapter->ahw.linkup = 0;
1431 if (netif_running(netdev)) {
1432 netif_carrier_off(netdev);
1433 netif_stop_queue(netdev);
1434 }
1435 } else if (!adapter->ahw.linkup && linkup) {
1436 printk(KERN_INFO "%s: %s NIC Link is up\n",
1437 netxen_nic_driver_name, netdev->name);
1438 adapter->ahw.linkup = 1;
1439 if (netif_running(netdev)) {
1440 netif_carrier_on(netdev);
1441 netif_wake_queue(netdev);
1442 }
1443 }
1444}
1445
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001446static void netxen_watchdog(unsigned long v)
1447{
1448 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001449
1450 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001451}
1452
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001453void netxen_watchdog_task(struct work_struct *work)
1454{
1455 struct netxen_adapter *adapter =
1456 container_of(work, struct netxen_adapter, watchdog_task);
1457
1458 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1459 return;
1460
1461 netxen_nic_handle_phy_intr(adapter);
1462
1463 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1464}
1465
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001466static void netxen_tx_timeout(struct net_device *netdev)
1467{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001468 struct netxen_adapter *adapter = (struct netxen_adapter *)
1469 netdev_priv(netdev);
1470 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001471}
1472
David Howells6d5aefb2006-12-05 19:36:26 +00001473static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001474{
Jeff Garzik47906542007-11-23 21:23:36 -05001475 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001476 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001477
1478 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001479 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001480
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001481 netxen_nic_disable_int(adapter);
1482 napi_disable(&adapter->napi);
1483
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001484 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001485
1486 napi_enable(&adapter->napi);
1487 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001488 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001489}
1490
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001491/*
1492 * netxen_nic_get_stats - Get System Network Statistics
1493 * @netdev: network interface device structure
1494 */
1495struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1496{
1497 struct netxen_adapter *adapter = netdev_priv(netdev);
1498 struct net_device_stats *stats = &adapter->net_stats;
1499
1500 memset(stats, 0, sizeof(*stats));
1501
1502 /* total packets received */
1503 stats->rx_packets = adapter->stats.no_rcv;
1504 /* total packets transmitted */
1505 stats->tx_packets = adapter->stats.xmitedframes +
1506 adapter->stats.xmitfinished;
1507 /* total bytes received */
1508 stats->rx_bytes = adapter->stats.rxbytes;
1509 /* total bytes transmitted */
1510 stats->tx_bytes = adapter->stats.txbytes;
1511 /* bad packets received */
1512 stats->rx_errors = adapter->stats.rcvdbadskb;
1513 /* packet transmit problems */
1514 stats->tx_errors = adapter->stats.nocmddescriptor;
1515 /* no space in linux buffers */
1516 stats->rx_dropped = adapter->stats.rxdropped;
1517 /* no space available in linux */
1518 stats->tx_dropped = adapter->stats.txdropped;
1519
1520 return stats;
1521}
1522
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001523static inline void
1524netxen_handle_int(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001525{
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301526 netxen_nic_disable_int(adapter);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001527 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001528}
1529
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001530static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001531{
Jeff Garzik1daec862007-10-19 03:14:03 -04001532 struct netxen_adapter *adapter = data;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301533 u32 our_int = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001534
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001535 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001536 /* not our interrupt */
1537 if ((our_int & (0x80 << adapter->portnum)) == 0)
1538 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301539
1540 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1541 /* claim interrupt */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001542 adapter->pci_write_normalize(adapter, CRB_INT_VECTOR,
1543 our_int & ~((u32)(0x80 << adapter->portnum)));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301544 }
1545
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001546 netxen_handle_int(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001547
1548 return IRQ_HANDLED;
1549}
1550
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001551static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001552{
1553 struct netxen_adapter *adapter = data;
1554
1555 netxen_handle_int(adapter);
1556 return IRQ_HANDLED;
1557}
1558
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001559static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001560{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001561 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001562 int tx_complete;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001563 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001564 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001565
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001566 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001567
Amit S. Kale80922fb2006-12-04 09:18:00 -08001568 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001569 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1570 /*
1571 * Fairness issue. This will give undue weight to the
1572 * receive context 0.
1573 */
1574
1575 /*
1576 * To avoid starvation, we give each of our receivers,
1577 * a fraction of the quota. Sometimes, it might happen that we
1578 * have enough quota to process every packet, but since all the
1579 * packets are on one context, it gets only half of the quota,
1580 * and ends up not processing it.
1581 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001582 work_done += netxen_process_rcv_ring(adapter, ctx,
1583 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001584 }
1585
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001586 if ((work_done < budget) && tx_complete) {
1587 netif_rx_complete(adapter->netdev, &adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001588 netxen_nic_enable_int(adapter);
1589 }
1590
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001591 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001592}
1593
1594#ifdef CONFIG_NET_POLL_CONTROLLER
1595static void netxen_nic_poll_controller(struct net_device *netdev)
1596{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001597 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001598 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001599 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001600 enable_irq(adapter->irq);
1601}
1602#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001603
1604static struct pci_driver netxen_driver = {
1605 .name = netxen_nic_driver_name,
1606 .id_table = netxen_pci_tbl,
1607 .probe = netxen_nic_probe,
1608 .remove = __devexit_p(netxen_nic_remove)
1609};
1610
1611/* Driver Registration on NetXen card */
1612
1613static int __init netxen_init_module(void)
1614{
Al Viro79ea13c2008-01-24 02:06:46 -08001615 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001616 return -ENOMEM;
1617
Amit S. Kale184231b2007-01-22 06:52:53 -08001618 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001619}
1620
1621module_init(netxen_init_module);
1622
1623static void __exit netxen_exit_module(void)
1624{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001625 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001626 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001627}
1628
1629module_exit(netxen_exit_module);