blob: 25bfb3acf5e462a8f0e58ddeac7c5e4d337467b1 [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) \
Dhananjay Phadke040dec32008-09-12 06:55:14 -070080 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
Al Viro70081ac2008-06-23 02:04:50 +010081 .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 = {
Dhananjay Phadke040dec32008-09-12 06:55:14 -070084 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
85 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
86 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
87 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
88 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
91 ENTRY(PCI_DEVICE_ID_NX3031),
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
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700152static inline void netxen_nic_disable_int(struct netxen_adapter *adapter)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700153{
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700154 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700155}
156
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700157static inline void netxen_nic_enable_int(struct netxen_adapter *adapter)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700158{
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700159 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700160
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700161 if (!NETXEN_IS_MSI_FAMILY(adapter))
162 adapter->pci_write_immediate(adapter,
163 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700164}
165
Dhananjay Phadke29566402008-07-21 19:44:04 -0700166static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
167{
168 struct pci_dev *pdev = adapter->pdev;
169 int err;
170 uint64_t mask;
171
172#ifdef CONFIG_IA64
173 adapter->dma_mask = DMA_32BIT_MASK;
174#else
175 if (revision_id >= NX_P3_B0) {
176 /* should go to DMA_64BIT_MASK */
177 adapter->dma_mask = DMA_39BIT_MASK;
178 mask = DMA_39BIT_MASK;
179 } else if (revision_id == NX_P3_A2) {
180 adapter->dma_mask = DMA_39BIT_MASK;
181 mask = DMA_39BIT_MASK;
182 } else if (revision_id == NX_P2_C1) {
183 adapter->dma_mask = DMA_35BIT_MASK;
184 mask = DMA_35BIT_MASK;
185 } else {
186 adapter->dma_mask = DMA_32BIT_MASK;
187 mask = DMA_32BIT_MASK;
188 goto set_32_bit_mask;
189 }
190
191 /*
192 * Consistent DMA mask is set to 32 bit because it cannot be set to
193 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
194 * come off this pool.
195 */
196 if (pci_set_dma_mask(pdev, mask) == 0 &&
197 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
198 adapter->pci_using_dac = 1;
199 return 0;
200 }
201#endif /* CONFIG_IA64 */
202
203set_32_bit_mask:
204 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
205 if (!err)
206 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
207 if (err) {
208 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
209 return err;
210 }
211
212 adapter->pci_using_dac = 0;
213 return 0;
214}
215
216static void netxen_check_options(struct netxen_adapter *adapter)
217{
218 switch (adapter->ahw.boardcfg.board_type) {
219 case NETXEN_BRDTYPE_P3_HMEZ:
220 case NETXEN_BRDTYPE_P3_XG_LOM:
221 case NETXEN_BRDTYPE_P3_10G_CX4:
222 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
223 case NETXEN_BRDTYPE_P3_IMEZ:
224 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
Dhananjay Phadkea70f9392008-08-01 03:14:56 -0700225 case NETXEN_BRDTYPE_P3_10G_SFP_QT:
226 case NETXEN_BRDTYPE_P3_10G_SFP_CT:
Dhananjay Phadke29566402008-07-21 19:44:04 -0700227 case NETXEN_BRDTYPE_P3_10G_XFP:
228 case NETXEN_BRDTYPE_P3_10000_BASE_T:
229 adapter->msix_supported = !!use_msi_x;
230 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
231 break;
232
233 case NETXEN_BRDTYPE_P2_SB31_10G:
234 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
235 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
236 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
237 adapter->msix_supported = 0;
238 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
239 break;
240
241 case NETXEN_BRDTYPE_P3_REF_QG:
242 case NETXEN_BRDTYPE_P3_4_GB:
243 case NETXEN_BRDTYPE_P3_4_GB_MM:
Dhananjay Phadkeec68db62008-08-20 13:52:48 -0700244 adapter->msix_supported = !!use_msi_x;
Dhananjay Phadkea70f9392008-08-01 03:14:56 -0700245 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
246 break;
247
Dhananjay Phadke29566402008-07-21 19:44:04 -0700248 case NETXEN_BRDTYPE_P2_SB35_4G:
249 case NETXEN_BRDTYPE_P2_SB31_2G:
250 adapter->msix_supported = 0;
251 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
252 break;
253
254 default:
255 adapter->msix_supported = 0;
256 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
257
258 printk(KERN_WARNING "Unknown board type(0x%x)\n",
259 adapter->ahw.boardcfg.board_type);
260 break;
261 }
262
263 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
264 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
265 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
266
267 adapter->max_possible_rss_rings = 1;
268 return;
269}
270
271static int
272netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
273{
274 int ret = 0;
275
276 if (first_boot == 0x55555555) {
277 /* This is the first boot after power up */
278
279 /* PCI bus master workaround */
280 adapter->hw_read_wx(adapter,
281 NETXEN_PCIE_REG(0x4), &first_boot, 4);
282 if (!(first_boot & 0x4)) {
283 first_boot |= 0x4;
284 adapter->hw_write_wx(adapter,
285 NETXEN_PCIE_REG(0x4), &first_boot, 4);
286 adapter->hw_read_wx(adapter,
287 NETXEN_PCIE_REG(0x4), &first_boot, 4);
288 }
289
290 /* This is the first boot after power up */
291 adapter->hw_read_wx(adapter,
292 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
293 if (first_boot != 0x80000f) {
294 /* clear the register for future unloads/loads */
295 adapter->pci_write_normalize(adapter,
296 NETXEN_CAM_RAM(0x1fc), 0);
297 ret = -1;
298 }
299
300 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
301 /* Start P2 boot loader */
302 adapter->pci_write_normalize(adapter,
303 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
304 adapter->pci_write_normalize(adapter,
305 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
306 }
307 }
308 return ret;
309}
310
311static void netxen_set_port_mode(struct netxen_adapter *adapter)
312{
313 u32 val, data;
314
315 val = adapter->ahw.boardcfg.board_type;
316 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
317 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
318 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
319 data = NETXEN_PORT_MODE_802_3_AP;
320 adapter->hw_write_wx(adapter,
321 NETXEN_PORT_MODE_ADDR, &data, 4);
322 } else if (port_mode == NETXEN_PORT_MODE_XG) {
323 data = NETXEN_PORT_MODE_XG;
324 adapter->hw_write_wx(adapter,
325 NETXEN_PORT_MODE_ADDR, &data, 4);
326 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
327 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
328 adapter->hw_write_wx(adapter,
329 NETXEN_PORT_MODE_ADDR, &data, 4);
330 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
331 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
332 adapter->hw_write_wx(adapter,
333 NETXEN_PORT_MODE_ADDR, &data, 4);
334 } else {
335 data = NETXEN_PORT_MODE_AUTO_NEG;
336 adapter->hw_write_wx(adapter,
337 NETXEN_PORT_MODE_ADDR, &data, 4);
338 }
339
340 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
341 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
342 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
343 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
344 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
345 }
346 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
347 &wol_port_mode, 4);
348 }
349}
350
351#define PCI_CAP_ID_GEN 0x10
352
353static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
354{
355 u32 pdevfuncsave;
356 u32 c8c9value = 0;
357 u32 chicken = 0;
358 u32 control = 0;
359 int i, pos;
360 struct pci_dev *pdev;
361
Dhananjay Phadke29566402008-07-21 19:44:04 -0700362 pdev = adapter->pdev;
363
364 adapter->hw_read_wx(adapter,
365 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
366 /* clear chicken3.25:24 */
367 chicken &= 0xFCFFFFFF;
368 /*
369 * if gen1 and B0, set F1020 - if gen 2, do nothing
370 * if gen2 set to F1000
371 */
372 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
373 if (pos == 0xC0) {
374 pci_read_config_dword(pdev, pos + 0x10, &control);
375 if ((control & 0x000F0000) != 0x00020000) {
376 /* set chicken3.24 if gen1 */
377 chicken |= 0x01000000;
378 }
379 printk(KERN_INFO "%s Gen2 strapping detected\n",
380 netxen_nic_driver_name);
381 c8c9value = 0xF1000;
382 } else {
383 /* set chicken3.24 if gen1 */
384 chicken |= 0x01000000;
385 printk(KERN_INFO "%s Gen1 strapping detected\n",
386 netxen_nic_driver_name);
387 if (adapter->ahw.revision_id == NX_P3_B0)
388 c8c9value = 0xF1020;
389 else
390 c8c9value = 0;
391
392 }
393 adapter->hw_write_wx(adapter,
394 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
395
396 if (!c8c9value)
397 return;
398
399 pdevfuncsave = pdev->devfn;
400 if (pdevfuncsave & 0x07)
401 return;
402
403 for (i = 0; i < 8; i++) {
404 pci_read_config_dword(pdev, pos + 8, &control);
405 pci_read_config_dword(pdev, pos + 8, &control);
406 pci_write_config_dword(pdev, pos + 8, c8c9value);
407 pdev->devfn++;
408 }
409 pdev->devfn = pdevfuncsave;
410}
411
412static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
413{
414 u32 control;
415 int pos;
416
417 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
418 if (pos) {
419 pci_read_config_dword(pdev, pos, &control);
420 if (enable)
421 control |= PCI_MSIX_FLAGS_ENABLE;
422 else
423 control = 0;
424 pci_write_config_dword(pdev, pos, control);
425 }
426}
427
428static void netxen_init_msix_entries(struct netxen_adapter *adapter)
429{
430 int i;
431
432 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
433 adapter->msix_entries[i].entry = i;
434}
435
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700436static int
437netxen_read_mac_addr(struct netxen_adapter *adapter)
438{
439 int i;
440 unsigned char *p;
441 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700442 struct net_device *netdev = adapter->netdev;
443 struct pci_dev *pdev = adapter->pdev;
444
445 if (netxen_is_flash_supported(adapter) != 0)
446 return -EIO;
447
448 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
449 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
450 return -EIO;
451 } else {
452 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
453 return -EIO;
454 }
455
456 p = (unsigned char *)&mac_addr;
457 for (i = 0; i < 6; i++)
458 netdev->dev_addr[i] = *(p + 5 - i);
459
460 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
461
462 /* set station address */
463
Johannes Berge1749612008-10-27 15:59:26 -0700464 if (!is_valid_ether_addr(netdev->perm_addr))
465 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
466 else
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700467 adapter->macaddr_set(adapter, netdev->dev_addr);
468
469 return 0;
470}
471
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400472/*
473 * netxen_nic_probe()
474 *
475 * The Linux system will invoke this after identifying the vendor ID and
476 * device Id in the pci_tbl supported by this module.
477 *
478 * A quad port card has one operational PCI config space, (function 0),
479 * which is used to access all four ports.
480 *
481 * This routine will initialize the adapter, and setup the global parameters
482 * along with the port's specific structure.
483 */
484static int __devinit
485netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
486{
487 struct net_device *netdev = NULL;
488 struct netxen_adapter *adapter = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800489 void __iomem *mem_ptr0 = NULL;
490 void __iomem *mem_ptr1 = NULL;
491 void __iomem *mem_ptr2 = NULL;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700492 unsigned long first_page_group_end;
493 unsigned long first_page_group_start;
494
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800495
Al Viroa608ab9c2007-01-02 10:39:10 +0000496 u8 __iomem *db_ptr = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700497 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
498 int i = 0, err;
499 int first_driver, first_boot;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700500 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700501 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700502 struct netxen_legacy_intr_set *legacy_intrp;
503 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400504
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700505 if (pci_func_id == 0)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700506 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700507
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700508 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700509 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700510 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800511 return -ENODEV;
512 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700513
Dhananjay Phadke11d89d62008-08-08 00:08:45 -0700514 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
515 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
516 "will not be enabled.\n",
517 NX_P3_A0, NX_P3_B1);
518 return -ENODEV;
519 }
520
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400521 if ((err = pci_enable_device(pdev)))
522 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700523
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400524 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
525 err = -ENODEV;
526 goto err_out_disable_pdev;
527 }
528
529 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
530 goto err_out_disable_pdev;
531
532 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700533
534 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
535 if(!netdev) {
536 printk(KERN_ERR"%s: Failed to allocate memory for the "
537 "device block.Check system memory resource"
538 " usage.\n", netxen_nic_driver_name);
539 goto err_out_free_res;
540 }
541
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700542 SET_NETDEV_DEV(netdev, &pdev->dev);
543
Wang Chen4cf16532008-11-12 23:38:14 -0800544 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700545 adapter->netdev = netdev;
546 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700547 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700548
549 revision_id = pdev->revision;
550 adapter->ahw.revision_id = revision_id;
551
552 err = nx_set_dma_mask(adapter, revision_id);
553 if (err)
554 goto err_out_free_netdev;
555
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700556 rwlock_init(&adapter->adapter_lock);
557 adapter->ahw.qdr_sn_window = -1;
558 adapter->ahw.ddr_mn_window = -1;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700559
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400560 /* remap phys address */
561 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
562 mem_len = pci_resource_len(pdev, 0);
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700563 pci_len0 = 0;
564
565 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
566 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
567 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
568 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
569 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
570 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
571 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
572 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
573 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400574
575 /* 128 Meg of memory */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700576 if (mem_len == NETXEN_PCI_128MB_SIZE) {
577 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
578 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
579 SECOND_PAGE_GROUP_SIZE);
580 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
581 THIRD_PAGE_GROUP_SIZE);
582 first_page_group_start = FIRST_PAGE_GROUP_START;
583 first_page_group_end = FIRST_PAGE_GROUP_END;
584 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
585 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
586 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
587 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
588 first_page_group_start = 0;
589 first_page_group_end = 0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700590 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
591 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
592 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
593 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
594 adapter->pci_write_immediate =
595 netxen_nic_pci_write_immediate_2M;
596 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
597 adapter->pci_write_normalize =
598 netxen_nic_pci_write_normalize_2M;
599 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
600 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
601 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
602
603 mem_ptr0 = ioremap(mem_base, mem_len);
604 pci_len0 = mem_len;
605 first_page_group_start = 0;
606 first_page_group_end = 0;
607
608 adapter->ahw.ddr_mn_window = 0;
609 adapter->ahw.qdr_sn_window = 0;
610
611 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
612 (pci_func_id * 0x20);
613 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
614 if (pci_func_id < 4)
615 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
616 else
617 adapter->ahw.ms_win_crb +=
618 0xA0 + ((pci_func_id - 4) * 0x10);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700619 } else {
Jeff Garzik47906542007-11-23 21:23:36 -0500620 err = -EIO;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700621 goto err_out_free_netdev;
622 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800623
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700624 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800625
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800626 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
627 db_len = pci_resource_len(pdev, 4);
628
629 if (db_len == 0) {
630 printk(KERN_ERR "%s: doorbell is disabled\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700631 netxen_nic_driver_name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800632 err = -EIO;
633 goto err_out_iounmap;
634 }
635 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
636 db_len);
637
638 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
Al Viroa608ab9c2007-01-02 10:39:10 +0000639 if (!db_ptr) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800640 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700641 netxen_nic_driver_name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800642 err = -EIO;
643 goto err_out_iounmap;
644 }
645 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400646
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700647 adapter->ahw.pci_base0 = mem_ptr0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700648 adapter->ahw.pci_len0 = pci_len0;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700649 adapter->ahw.first_page_group_start = first_page_group_start;
650 adapter->ahw.first_page_group_end = first_page_group_end;
651 adapter->ahw.pci_base1 = mem_ptr1;
652 adapter->ahw.pci_base2 = mem_ptr2;
653 adapter->ahw.db_base = db_ptr;
654 adapter->ahw.db_len = db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400655
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700656 netif_napi_add(netdev, &adapter->napi,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700657 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
658
659 if (revision_id >= NX_P3_B0)
660 legacy_intrp = &legacy_intr[pci_func_id];
661 else
662 legacy_intrp = &legacy_intr[0];
663
664 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
665 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
666 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
667 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700668
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530669 /* this will be read from FW later */
670 adapter->intr_scheme = -1;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700671 adapter->msi_mode = -1;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530672
673 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700674 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530675 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530676 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -0700677 adapter->mc_enabled = 0;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700678 if (NX_IS_REVISION_P3(revision_id))
Dhananjay Phadke29566402008-07-21 19:44:04 -0700679 adapter->max_mc_count = 38;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700680 else
Dhananjay Phadke29566402008-07-21 19:44:04 -0700681 adapter->max_mc_count = 16;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700682
683 netdev->open = netxen_nic_open;
684 netdev->stop = netxen_nic_close;
685 netdev->hard_start_xmit = netxen_nic_xmit_frame;
Jeff Garzik47906542007-11-23 21:23:36 -0500686 netdev->get_stats = netxen_nic_get_stats;
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700687 if (NX_IS_REVISION_P3(revision_id))
688 netdev->set_multicast_list = netxen_p3_nic_set_multi;
689 else
690 netdev->set_multicast_list = netxen_p2_nic_set_multi;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700691 netdev->set_mac_address = netxen_nic_set_mac;
692 netdev->change_mtu = netxen_nic_change_mtu;
693 netdev->tx_timeout = netxen_tx_timeout;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700694 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700695
696 netxen_nic_change_mtu(netdev, netdev->mtu);
697
698 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700699#ifdef CONFIG_NET_POLL_CONTROLLER
700 netdev->poll_controller = netxen_nic_poll_controller;
701#endif
702 /* ScatterGather support */
703 netdev->features = NETIF_F_SG;
704 netdev->features |= NETIF_F_IP_CSUM;
705 netdev->features |= NETIF_F_TSO;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700706 if (NX_IS_REVISION_P3(revision_id)) {
707 netdev->features |= NETIF_F_IPV6_CSUM;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700708 netdev->features |= NETIF_F_TSO6;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700709 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700710
Dhananjay Phadke29566402008-07-21 19:44:04 -0700711 if (adapter->pci_using_dac)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700712 netdev->features |= NETIF_F_HIGHDMA;
713
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700714 /*
715 * Set the CRB window to invalid. If any register in window 0 is
716 * accessed it should set the window to 0 and then reset it to 1.
717 */
718 adapter->curr_window = 255;
719
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700720 if (netxen_nic_get_board_info(adapter) != 0) {
721 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700722 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700723 err = -EIO;
724 goto err_out_iounmap;
725 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700726
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700727 netxen_initialize_adapter_ops(adapter);
728
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700729 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700730 switch (adapter->ahw.boardcfg.board_type) {
731 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
732 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
733 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700734 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700735 break;
736 default:
737 break;
738 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700739
Dhananjay Phadke29566402008-07-21 19:44:04 -0700740 /*
741 * This call will setup various max rx/tx counts.
742 * It must be done before any buffer/ring allocations.
743 */
744 netxen_check_options(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400745
Dhananjay Phadke29566402008-07-21 19:44:04 -0700746 first_driver = 0;
747 if (NX_IS_REVISION_P3(revision_id)) {
748 if (adapter->ahw.pci_func == 0)
749 first_driver = 1;
750 } else {
751 if (adapter->portnum == 0)
752 first_driver = 1;
753 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400754
Dhananjay Phadke29566402008-07-21 19:44:04 -0700755 if (first_driver) {
756 first_boot = adapter->pci_read_normalize(adapter,
757 NETXEN_CAM_RAM(0x1fc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400758
Dhananjay Phadke29566402008-07-21 19:44:04 -0700759 err = netxen_check_hw_init(adapter, first_boot);
760 if (err) {
761 printk(KERN_ERR "%s: error in init HW init sequence\n",
762 netxen_nic_driver_name);
763 goto err_out_iounmap;
764 }
765
766 if (NX_IS_REVISION_P3(revision_id))
767 netxen_set_port_mode(adapter);
768
769 if (first_boot != 0x55555555) {
770 adapter->pci_write_normalize(adapter,
771 CRB_CMDPEG_STATE, 0);
772 netxen_pinit_from_rom(adapter, 0);
773 msleep(1);
774 netxen_load_firmware(adapter);
775 }
776
777 if (NX_IS_REVISION_P3(revision_id))
778 netxen_pcie_strap_init(adapter);
779
780 if (NX_IS_REVISION_P2(revision_id)) {
781
782 /* Initialize multicast addr pool owners */
783 val = 0x7654;
784 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
785 val |= 0x0f000000;
786 netxen_crb_writelit_adapter(adapter,
787 NETXEN_MAC_ADDR_CNTL_REG, val);
788
789 }
790
791 if ((first_boot == 0x55555555) &&
792 (NX_IS_REVISION_P2(revision_id))) {
793 /* Unlock the HW, prompting the boot sequence */
794 adapter->pci_write_normalize(adapter,
795 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
796 }
797
798 err = netxen_initialize_adapter_offload(adapter);
799 if (err)
800 goto err_out_iounmap;
801
802 /*
803 * Tell the hardware our version number.
804 */
805 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
806 | ((_NETXEN_NIC_LINUX_MINOR << 8))
807 | (_NETXEN_NIC_LINUX_SUBVERSION);
808 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
809
810 /* Handshake with the card before we register the devices. */
811 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
812
Dhananjay Phadke29566402008-07-21 19:44:04 -0700813 } /* first_driver */
814
815 netxen_nic_flash_print(adapter);
816
Dhananjay Phadked9e651b2008-07-21 19:44:08 -0700817 if (NX_IS_REVISION_P3(revision_id)) {
818 adapter->hw_read_wx(adapter,
819 NETXEN_MIU_MN_CONTROL, &val, 4);
820 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
821 dev_info(&pdev->dev, "firmware running in %s mode\n",
822 adapter->ahw.cut_through ? "cut through" : "legacy");
823 }
824
Dhananjay Phadke29566402008-07-21 19:44:04 -0700825 /*
826 * See if the firmware gave us a virtual-physical port mapping.
827 */
828 adapter->physical_port = adapter->portnum;
829 i = adapter->pci_read_normalize(adapter, CRB_V2P(adapter->portnum));
830 if (i != 0x55555555)
831 adapter->physical_port = i;
832
833 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
834
835 netxen_set_msix_bit(pdev, 0);
836
837 if (NX_IS_REVISION_P3(revision_id)) {
838 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
839 mem_len != NETXEN_PCI_2MB_SIZE)
840 adapter->msix_supported = 0;
841 }
842
843 if (adapter->msix_supported) {
844
845 netxen_init_msix_entries(adapter);
846
847 if (pci_enable_msix(pdev, adapter->msix_entries,
848 MSIX_ENTRIES_PER_ADAPTER))
849 goto request_msi;
850
851 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
852 netxen_set_msix_bit(pdev, 1);
853 dev_info(&pdev->dev, "using msi-x interrupts\n");
854
855 } else {
856request_msi:
857 if (use_msi && !pci_enable_msi(pdev)) {
858 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
859 dev_info(&pdev->dev, "using msi interrupts\n");
860 } else
861 dev_info(&pdev->dev, "using legacy interrupts\n");
862 }
863
864 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
865 netdev->irq = adapter->msix_entries[0].vector;
866 else
867 netdev->irq = pdev->irq;
868
869 err = netxen_receive_peg_ready(adapter);
870 if (err)
871 goto err_out_disable_msi;
872
873 init_timer(&adapter->watchdog_timer);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700874 adapter->watchdog_timer.function = &netxen_watchdog;
875 adapter->watchdog_timer.data = (unsigned long)adapter;
876 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
877 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
878
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700879 err = netxen_read_mac_addr(adapter);
880 if (err)
881 dev_warn(&pdev->dev, "failed to read mac addr\n");
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700882
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700883 netif_carrier_off(netdev);
884 netif_stop_queue(netdev);
885
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700886 if ((err = register_netdev(netdev))) {
887 printk(KERN_ERR "%s: register_netdev failed port #%d"
888 " aborting\n", netxen_nic_driver_name,
889 adapter->portnum);
890 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700891 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700892 }
893
894 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400895
Dhananjay Phadke29566402008-07-21 19:44:04 -0700896 switch (adapter->ahw.board_type) {
897 case NETXEN_NIC_GBE:
898 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
899 adapter->netdev->name);
900 break;
901 case NETXEN_NIC_XGBE:
902 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
903 adapter->netdev->name);
904 break;
905 }
906
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400907 return 0;
908
Dhananjay Phadke29566402008-07-21 19:44:04 -0700909err_out_disable_msi:
910 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
911 pci_disable_msix(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700912 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
913 pci_disable_msi(pdev);
914
Dhananjay Phadke29566402008-07-21 19:44:04 -0700915 if (first_driver)
916 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400917
Dhananjay Phadke29566402008-07-21 19:44:04 -0700918err_out_iounmap:
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800919 if (db_ptr)
920 iounmap(db_ptr);
921
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800922 if (mem_ptr0)
923 iounmap(mem_ptr0);
924 if (mem_ptr1)
925 iounmap(mem_ptr1);
926 if (mem_ptr2)
927 iounmap(mem_ptr2);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800928
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700929err_out_free_netdev:
930 free_netdev(netdev);
931
932err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400933 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700934
935err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -0700936 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400937 pci_disable_device(pdev);
938 return err;
939}
940
941static void __devexit netxen_nic_remove(struct pci_dev *pdev)
942{
943 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700944 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400945
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700946 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400947 if (adapter == NULL)
948 return;
949
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700950 netdev = adapter->netdev;
951
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530952 unregister_netdev(netdev);
953
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530954 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400955 netxen_free_hw_resources(adapter);
Dhananjay Phadke9e410772008-08-08 00:08:40 -0700956 netxen_release_rx_buffers(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700957 netxen_free_sw_resources(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530958 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400959
Dhananjay Phadke439b4542008-06-15 22:59:46 -0700960 if (adapter->portnum == 0)
961 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530962
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530963 if (adapter->irq)
964 free_irq(adapter->irq, adapter);
965
Dhananjay Phadke29566402008-07-21 19:44:04 -0700966 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
967 pci_disable_msix(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530968 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
969 pci_disable_msi(pdev);
970
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700971 iounmap(adapter->ahw.db_base);
972 iounmap(adapter->ahw.pci_base0);
973 iounmap(adapter->ahw.pci_base1);
974 iounmap(adapter->ahw.pci_base2);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700975
976 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +0530977 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -0700978 pci_set_drvdata(pdev, NULL);
979
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700980 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400981}
982
983/*
984 * Called when a network interface is made active
985 * @returns 0 on success, negative value on failure
986 */
987static int netxen_nic_open(struct net_device *netdev)
988{
Wang Chen4cf16532008-11-12 23:38:14 -0800989 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400990 int err = 0;
991 int ctx, ring;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700992 irq_handler_t handler;
993 unsigned long flags = IRQF_SAMPLE_RANDOM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400994
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700995 if (adapter->driver_mismatch)
996 return -EIO;
997
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400998 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
999 err = netxen_init_firmware(adapter);
1000 if (err != 0) {
1001 printk(KERN_ERR "Failed to init firmware\n");
1002 return -EIO;
1003 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001004
Dhananjay Phadke83821a02008-08-01 03:14:58 -07001005 if (adapter->fw_major < 4)
1006 adapter->max_rds_rings = 3;
1007 else
1008 adapter->max_rds_rings = 2;
1009
Dhananjay Phadke29566402008-07-21 19:44:04 -07001010 err = netxen_alloc_sw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001011 if (err) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001012 printk(KERN_ERR "%s: Error in setting sw resources\n",
1013 netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001014 return err;
1015 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001016
1017 netxen_nic_clear_stats(adapter);
1018
1019 err = netxen_alloc_hw_resources(adapter);
1020 if (err) {
1021 printk(KERN_ERR "%s: Error in setting hw resources\n",
1022 netdev->name);
1023 goto err_out_free_sw;
1024 }
1025
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001026 if ((adapter->msi_mode != MSI_MODE_MULTIFUNC) ||
1027 (adapter->intr_scheme != INTR_SCHEME_PERPORT)) {
1028 printk(KERN_ERR "%s: Firmware interrupt scheme is "
1029 "incompatible with driver\n",
1030 netdev->name);
1031 adapter->driver_mismatch = 1;
1032 goto err_out_free_hw;
1033 }
1034
Dhananjay Phadke29566402008-07-21 19:44:04 -07001035 if (adapter->fw_major < 4) {
1036 adapter->crb_addr_cmd_producer =
1037 crb_cmd_producer[adapter->portnum];
1038 adapter->crb_addr_cmd_consumer =
1039 crb_cmd_consumer[adapter->portnum];
Dhananjay Phadke29566402008-07-21 19:44:04 -07001040
Dhananjay Phadke83821a02008-08-01 03:14:58 -07001041 netxen_nic_update_cmd_producer(adapter, 0);
1042 netxen_nic_update_cmd_consumer(adapter, 0);
1043 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001044
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001045 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001046 for (ring = 0; ring < adapter->max_rds_rings; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001047 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001048 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001049 if (NETXEN_IS_MSI_FAMILY(adapter))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001050 handler = netxen_msi_intr;
1051 else {
1052 flags |= IRQF_SHARED;
1053 handler = netxen_intr;
1054 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001055 adapter->irq = netdev->irq;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001056 err = request_irq(adapter->irq, handler,
1057 flags, netdev->name, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001058 if (err) {
1059 printk(KERN_ERR "request_irq failed with: %d\n", err);
Dhananjay Phadke9e410772008-08-08 00:08:40 -07001060 goto err_out_free_rxbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001061 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001062
1063 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1064 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001065
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001066 /* Done here again so that even if phantom sw overwrote it,
1067 * we set it */
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001068 err = adapter->init_port(adapter, adapter->physical_port);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001069 if (err) {
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001070 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1071 netxen_nic_driver_name, adapter->portnum);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001072 goto err_out_free_irq;
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001073 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001074 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001075
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001076 netxen_nic_set_link_parameters(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001077
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001078 netdev->set_multicast_list(netdev);
Dhananjay Phadke9ad27642008-08-01 03:14:59 -07001079 if (adapter->set_mtu)
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001080 adapter->set_mtu(adapter, netdev->mtu);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001081
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001082 adapter->ahw.linkup = 0;
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001083 mod_timer(&adapter->watchdog_timer, jiffies);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001084
1085 napi_enable(&adapter->napi);
1086 netxen_nic_enable_int(adapter);
1087
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001088 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001089
1090 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001091
1092err_out_free_irq:
1093 free_irq(adapter->irq, adapter);
Dhananjay Phadke9e410772008-08-08 00:08:40 -07001094err_out_free_rxbuf:
1095 netxen_release_rx_buffers(adapter);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001096err_out_free_hw:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001097 netxen_free_hw_resources(adapter);
1098err_out_free_sw:
1099 netxen_free_sw_resources(adapter);
1100 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001101}
1102
1103/*
1104 * netxen_nic_close - Disables a network interface entry point
1105 */
1106static int netxen_nic_close(struct net_device *netdev)
1107{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001108 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001109
1110 netif_carrier_off(netdev);
1111 netif_stop_queue(netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001112 napi_disable(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001113
dhananjay@netxen.com72b0a7a2007-12-26 10:23:56 -08001114 if (adapter->stop_port)
1115 adapter->stop_port(adapter);
1116
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301117 netxen_nic_disable_int(adapter);
1118
Dhananjay Phadke29566402008-07-21 19:44:04 -07001119 netxen_release_tx_buffers(adapter);
1120
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001121 FLUSH_SCHEDULED_WORK();
1122 del_timer_sync(&adapter->watchdog_timer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001123
1124 return 0;
1125}
1126
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001127void netxen_tso_check(struct netxen_adapter *adapter,
1128 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1129{
1130 if (desc->mss) {
1131 desc->total_hdr_length = (sizeof(struct ethhdr) +
1132 ip_hdrlen(skb) + tcp_hdrlen(skb));
1133
1134 if ((NX_IS_REVISION_P3(adapter->ahw.revision_id)) &&
1135 (skb->protocol == htons(ETH_P_IPV6)))
1136 netxen_set_cmd_desc_opcode(desc, TX_TCP_LSO6);
1137 else
1138 netxen_set_cmd_desc_opcode(desc, TX_TCP_LSO);
1139
1140 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1141 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1142 netxen_set_cmd_desc_opcode(desc, TX_TCP_PKT);
1143 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1144 netxen_set_cmd_desc_opcode(desc, TX_UDP_PKT);
1145 else
1146 return;
1147 }
1148 desc->tcp_hdr_offset = skb_transport_offset(skb);
1149 desc->ip_hdr_offset = skb_network_offset(skb);
1150}
1151
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001152static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1153{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001154 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001155 struct netxen_hardware_context *hw = &adapter->ahw;
1156 unsigned int first_seg_len = skb->len - skb->data_len;
1157 struct netxen_skb_frag *buffrag;
1158 unsigned int i;
1159
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001160 u32 producer, consumer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001161 u32 saved_producer = 0;
1162 struct cmd_desc_type0 *hwdesc;
1163 int k;
1164 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001165 int frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001166 int no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001167 u32 num_txd = adapter->max_tx_desc_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001168
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001169 frag_count = skb_shinfo(skb)->nr_frags + 1;
1170
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001171 /* There 4 fragments per descriptor */
1172 no_of_desc = (frag_count + 3) >> 2;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001173 if (netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001174 if (skb_shinfo(skb)->gso_size > 0) {
1175
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001176 no_of_desc++;
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001177 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001178 sizeof(struct ethhdr)) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001179 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001180 no_of_desc++;
1181 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001182 }
1183 }
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001184
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001185 producer = adapter->cmd_producer;
1186 smp_mb();
1187 consumer = adapter->last_cmd_consumer;
1188 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1189 netif_stop_queue(netdev);
1190 smp_mb();
1191 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001192 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001193
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001194 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001195 saved_producer = producer;
1196 hwdesc = &hw->cmd_desc_head[producer];
1197 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1198 /* Take skb->data itself */
1199 pbuf = &adapter->cmd_buf_arr[producer];
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001200 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1201 skb_shinfo(skb)->gso_size > 0) {
Al Viroa608ab9c2007-01-02 10:39:10 +00001202 pbuf->mss = skb_shinfo(skb)->gso_size;
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001203 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001204 } else {
1205 pbuf->mss = 0;
1206 hwdesc->mss = 0;
1207 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001208 pbuf->total_length = skb->len;
1209 pbuf->skb = skb;
1210 pbuf->cmd = TX_ETHER_PKT;
1211 pbuf->frag_count = frag_count;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001212 pbuf->port = adapter->portnum;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001213 buffrag = &pbuf->frag_array[0];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001214 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001215 PCI_DMA_TODEVICE);
1216 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001217 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1218 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1219 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001220
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001221 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001222 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001223 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1224 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1225
1226 for (i = 1, k = 1; i < frag_count; i++, k++) {
1227 struct skb_frag_struct *frag;
1228 int len, temp_len;
1229 unsigned long offset;
1230 dma_addr_t temp_dma;
1231
1232 /* move to next desc. if there is a need */
1233 if ((i & 0x3) == 0) {
1234 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001235 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001236 hwdesc = &hw->cmd_desc_head[producer];
1237 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001238 pbuf = &adapter->cmd_buf_arr[producer];
1239 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001240 }
1241 frag = &skb_shinfo(skb)->frags[i - 1];
1242 len = frag->size;
1243 offset = frag->page_offset;
1244
1245 temp_len = len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001246 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001247 len, PCI_DMA_TODEVICE);
1248
1249 buffrag++;
1250 buffrag->dma = temp_dma;
1251 buffrag->length = temp_len;
1252
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001253 switch (k) {
1254 case 0:
1255 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1256 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1257 break;
1258 case 1:
1259 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1260 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1261 break;
1262 case 2:
1263 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1264 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1265 break;
1266 case 3:
Al Viroa608ab9c2007-01-02 10:39:10 +00001267 hwdesc->buffer4_length = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001268 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1269 break;
1270 }
1271 frag++;
1272 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001273 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001274
1275 /* might change opcode to TX_TCP_LSO */
1276 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1277
1278 /* For LSO, we need to copy the MAC/IP/TCP headers into
1279 * the descriptor ring
1280 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001281 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1282 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001283 int hdr_len, first_hdr_len, more_hdr;
1284 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001285 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1286 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001287 more_hdr = 1;
1288 } else {
1289 first_hdr_len = hdr_len;
1290 more_hdr = 0;
1291 }
1292 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1293 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001294 pbuf = &adapter->cmd_buf_arr[producer];
1295 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001296
1297 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001298 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001299 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001300 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001301
1302 if (more_hdr) {
1303 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001304 pbuf = &adapter->cmd_buf_arr[producer];
1305 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001306 /* copy the next 64 bytes - should be enough except
1307 * for pathological case
1308 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001309 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1310 hwdesc,
1311 (hdr_len -
1312 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001313 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001314 }
1315 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001316
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001317 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001318 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001319
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001320 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001321
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001322 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001323 netdev->trans_start = jiffies;
1324
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001325 return NETDEV_TX_OK;
1326}
1327
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001328static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1329{
1330 struct net_device *netdev = adapter->netdev;
1331 uint32_t temp, temp_state, temp_val;
1332 int rv = 0;
1333
1334 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1335
1336 temp_state = nx_get_temp_state(temp);
1337 temp_val = nx_get_temp_val(temp);
1338
1339 if (temp_state == NX_TEMP_PANIC) {
1340 printk(KERN_ALERT
1341 "%s: Device temperature %d degrees C exceeds"
1342 " maximum allowed. Hardware has been shut down.\n",
1343 netxen_nic_driver_name, temp_val);
1344
1345 netif_carrier_off(netdev);
1346 netif_stop_queue(netdev);
1347 rv = 1;
1348 } else if (temp_state == NX_TEMP_WARN) {
1349 if (adapter->temp == NX_TEMP_NORMAL) {
1350 printk(KERN_ALERT
1351 "%s: Device temperature %d degrees C "
1352 "exceeds operating range."
1353 " Immediate action needed.\n",
1354 netxen_nic_driver_name, temp_val);
1355 }
1356 } else {
1357 if (adapter->temp == NX_TEMP_WARN) {
1358 printk(KERN_INFO
1359 "%s: Device temperature is now %d degrees C"
1360 " in normal range.\n", netxen_nic_driver_name,
1361 temp_val);
1362 }
1363 }
1364 adapter->temp = temp_state;
1365 return rv;
1366}
1367
1368static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1369{
1370 struct net_device *netdev = adapter->netdev;
1371 u32 val, port, linkup;
1372
1373 port = adapter->physical_port;
1374
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001375 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1376 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1377 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1378 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001379 } else {
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001380 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1381 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1382 linkup = (val >> port) & 1;
1383 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001384 val = (val >> port*8) & 0xff;
1385 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001386 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001387 }
1388
1389 if (adapter->ahw.linkup && !linkup) {
1390 printk(KERN_INFO "%s: %s NIC Link is down\n",
1391 netxen_nic_driver_name, netdev->name);
1392 adapter->ahw.linkup = 0;
1393 if (netif_running(netdev)) {
1394 netif_carrier_off(netdev);
1395 netif_stop_queue(netdev);
1396 }
1397 } else if (!adapter->ahw.linkup && linkup) {
1398 printk(KERN_INFO "%s: %s NIC Link is up\n",
1399 netxen_nic_driver_name, netdev->name);
1400 adapter->ahw.linkup = 1;
1401 if (netif_running(netdev)) {
1402 netif_carrier_on(netdev);
1403 netif_wake_queue(netdev);
1404 }
1405 }
1406}
1407
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001408static void netxen_watchdog(unsigned long v)
1409{
1410 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001411
1412 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001413}
1414
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001415void netxen_watchdog_task(struct work_struct *work)
1416{
1417 struct netxen_adapter *adapter =
1418 container_of(work, struct netxen_adapter, watchdog_task);
1419
1420 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1421 return;
1422
1423 netxen_nic_handle_phy_intr(adapter);
1424
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001425 if (netif_running(adapter->netdev))
1426 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001427}
1428
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001429static void netxen_tx_timeout(struct net_device *netdev)
1430{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001431 struct netxen_adapter *adapter = (struct netxen_adapter *)
1432 netdev_priv(netdev);
1433 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001434}
1435
David Howells6d5aefb2006-12-05 19:36:26 +00001436static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001437{
Jeff Garzik47906542007-11-23 21:23:36 -05001438 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001439 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001440
1441 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001442 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001443
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001444 netxen_nic_disable_int(adapter);
1445 napi_disable(&adapter->napi);
1446
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001447 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001448
1449 napi_enable(&adapter->napi);
1450 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001451 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001452}
1453
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001454/*
1455 * netxen_nic_get_stats - Get System Network Statistics
1456 * @netdev: network interface device structure
1457 */
1458struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1459{
1460 struct netxen_adapter *adapter = netdev_priv(netdev);
1461 struct net_device_stats *stats = &adapter->net_stats;
1462
1463 memset(stats, 0, sizeof(*stats));
1464
1465 /* total packets received */
1466 stats->rx_packets = adapter->stats.no_rcv;
1467 /* total packets transmitted */
1468 stats->tx_packets = adapter->stats.xmitedframes +
1469 adapter->stats.xmitfinished;
1470 /* total bytes received */
1471 stats->rx_bytes = adapter->stats.rxbytes;
1472 /* total bytes transmitted */
1473 stats->tx_bytes = adapter->stats.txbytes;
1474 /* bad packets received */
1475 stats->rx_errors = adapter->stats.rcvdbadskb;
1476 /* packet transmit problems */
1477 stats->tx_errors = adapter->stats.nocmddescriptor;
1478 /* no space in linux buffers */
1479 stats->rx_dropped = adapter->stats.rxdropped;
1480 /* no space available in linux */
1481 stats->tx_dropped = adapter->stats.txdropped;
1482
1483 return stats;
1484}
1485
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001486static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001487{
Jeff Garzik1daec862007-10-19 03:14:03 -04001488 struct netxen_adapter *adapter = data;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001489 u32 status = 0;
1490
1491 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1492
1493 if (!(status & adapter->legacy_intr.int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001494 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301495
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001496 if (adapter->ahw.revision_id >= NX_P3_B1) {
1497 /* check interrupt state machine, to be sure */
1498 status = adapter->pci_read_immediate(adapter,
1499 ISR_INT_STATE_REG);
1500 if (!ISR_LEGACY_INT_TRIGGERED(status))
1501 return IRQ_NONE;
1502
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001503 } else {
1504 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001505
1506 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001507
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001508 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001509 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001510 return IRQ_NONE;
1511
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001512 /* claim interrupt */
1513 adapter->pci_write_normalize(adapter,
1514 CRB_INT_VECTOR, (our_int & 0xffffffff));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301515 }
1516
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001517 /* clear interrupt */
1518 if (adapter->fw_major < 4)
1519 netxen_nic_disable_int(adapter);
1520
1521 adapter->pci_write_immediate(adapter,
1522 adapter->legacy_intr.tgt_status_reg,
1523 0xffffffff);
1524 /* read twice to ensure write is flushed */
1525 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1526 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1527
1528 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001529
1530 return IRQ_HANDLED;
1531}
1532
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001533static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001534{
1535 struct netxen_adapter *adapter = data;
1536
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001537 /* clear interrupt */
1538 adapter->pci_write_immediate(adapter,
1539 msi_tgt_status[adapter->ahw.pci_func], 0xffffffff);
1540
1541 napi_schedule(&adapter->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001542 return IRQ_HANDLED;
1543}
1544
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001545static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001546{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001547 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001548 int tx_complete;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001549 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001550 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001551
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001552 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001553
Amit S. Kale80922fb2006-12-04 09:18:00 -08001554 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001555 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1556 /*
1557 * Fairness issue. This will give undue weight to the
1558 * receive context 0.
1559 */
1560
1561 /*
1562 * To avoid starvation, we give each of our receivers,
1563 * a fraction of the quota. Sometimes, it might happen that we
1564 * have enough quota to process every packet, but since all the
1565 * packets are on one context, it gets only half of the quota,
1566 * and ends up not processing it.
1567 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001568 work_done += netxen_process_rcv_ring(adapter, ctx,
1569 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001570 }
1571
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001572 if ((work_done < budget) && tx_complete) {
1573 netif_rx_complete(adapter->netdev, &adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001574 netxen_nic_enable_int(adapter);
1575 }
1576
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001577 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001578}
1579
1580#ifdef CONFIG_NET_POLL_CONTROLLER
1581static void netxen_nic_poll_controller(struct net_device *netdev)
1582{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001583 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001584 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001585 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001586 enable_irq(adapter->irq);
1587}
1588#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001589
1590static struct pci_driver netxen_driver = {
1591 .name = netxen_nic_driver_name,
1592 .id_table = netxen_pci_tbl,
1593 .probe = netxen_nic_probe,
1594 .remove = __devexit_p(netxen_nic_remove)
1595};
1596
1597/* Driver Registration on NetXen card */
1598
1599static int __init netxen_init_module(void)
1600{
Al Viro79ea13c2008-01-24 02:06:46 -08001601 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001602 return -ENOMEM;
1603
Amit S. Kale184231b2007-01-22 06:52:53 -08001604 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001605}
1606
1607module_init(netxen_init_module);
1608
1609static void __exit netxen_exit_module(void)
1610{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001611 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001612 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001613}
1614
1615module_exit(netxen_exit_module);