blob: 2d0963f4d194343aee6bbb7be34b061aedb7946a [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
Amit S. Kale80922fb2006-12-04 09:18:00 -08004 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -04005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
Amit S. Kale80922fb2006-12-04 09:18:00 -08009 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040010 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Amit S. Kale80922fb2006-12-04 09:18:00 -080014 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
Amit S. Kale80922fb2006-12-04 09:18:00 -080019 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040020 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
Amit S. Kale80922fb2006-12-04 09:18:00 -080022 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040023 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
28 *
29 *
30 * Main source file for NetXen NIC Driver on Linux
31 *
32 */
33
Amit S. Kalecb8011a2006-11-29 09:00:10 -080034#include <linux/vmalloc.h>
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080035#include <linux/highmem.h>
Amit S. Kale3d396eb2006-10-21 15:33:03 -040036#include "netxen_nic_hw.h"
37
38#include "netxen_nic.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040039#include "netxen_nic_phan_reg.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040040
Jeff Garzik1494a812006-11-07 05:12:16 -050041#include <linux/dma-mapping.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030042#include <net/ip.h>
Jeff Garzik1494a812006-11-07 05:12:16 -050043
Amit S. Kale3d396eb2006-10-21 15:33:03 -040044MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45MODULE_LICENSE("GPL");
46MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
47
dhananjay@netxen.comceded322007-07-19 14:41:09 +053048char netxen_nic_driver_name[] = "netxen_nic";
Amit S. Kale3d396eb2006-10-21 15:33:03 -040049static char netxen_nic_driver_string[] = "NetXen Network Driver version "
Amit S. Kalecb8011a2006-11-29 09:00:10 -080050 NETXEN_NIC_LINUX_VERSIONID;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040051
Dhananjay Phadke29566402008-07-21 19:44:04 -070052static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
53
54/* Default to restricted 1G auto-neg mode */
55static int wol_port_mode = 5;
56
57static int use_msi = 1;
58
59static int use_msi_x = 1;
60
Amit S. Kale3d396eb2006-10-21 15:33:03 -040061#define NETXEN_NETDEV_WEIGHT 120
62#define NETXEN_ADAPTER_UP_MAGIC 777
Amit S. Kalecb8011a2006-11-29 09:00:10 -080063#define NETXEN_NIC_PEG_TUNE 0
Amit S. Kale3d396eb2006-10-21 15:33:03 -040064
65/* Local functions to NetXen NIC driver */
66static int __devinit netxen_nic_probe(struct pci_dev *pdev,
Dhananjay Phadke29566402008-07-21 19:44:04 -070067 const struct pci_device_id *ent);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040068static void __devexit netxen_nic_remove(struct pci_dev *pdev);
69static int netxen_nic_open(struct net_device *netdev);
70static int netxen_nic_close(struct net_device *netdev);
71static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
72static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000073static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040074static void netxen_watchdog(unsigned long);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070075static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040076#ifdef CONFIG_NET_POLL_CONTROLLER
77static void netxen_nic_poll_controller(struct net_device *netdev);
78#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050079static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070080static irqreturn_t netxen_msi_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040081
82/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010083#define ENTRY(device) \
84 {PCI_DEVICE(0x4040, (device)), \
85 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
86
Amit S. Kale3d396eb2006-10-21 15:33:03 -040087static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Al Viro70081ac2008-06-23 02:04:50 +010088 ENTRY(0x0001),
89 ENTRY(0x0002),
90 ENTRY(0x0003),
91 ENTRY(0x0004),
92 ENTRY(0x0005),
93 ENTRY(0x0024),
94 ENTRY(0x0025),
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -070095 ENTRY(0x0100),
Amit S. Kale3d396eb2006-10-21 15:33:03 -040096 {0,}
97};
98
99MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
100
Adrian Bunkb15551302008-03-31 02:22:18 +0300101/*
102 * In netxen_nic_down(), we must wait for any pending callback requests into
103 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
104 * reenabled right after it is deleted in netxen_nic_down().
105 * FLUSH_SCHEDULED_WORK() does this synchronization.
106 *
107 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
108 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
109 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
110 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
111 * linkwatch_event() to be executed which also attempts to acquire the rtnl
112 * lock thus causing a deadlock.
113 */
114
115static struct workqueue_struct *netxen_workq;
116#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
117#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
118
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800119static void netxen_watchdog(unsigned long);
120
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700121static uint32_t crb_cmd_producer[4] = {
122 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
123 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
124};
125
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700126static inline void
127netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
128 uint32_t crb_producer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700129{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700130 adapter->pci_write_normalize(adapter,
131 adapter->crb_addr_cmd_producer, crb_producer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700132}
133
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700134static uint32_t crb_cmd_consumer[4] = {
135 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
136 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
137};
138
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700139static inline void
140netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
141 u32 crb_consumer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700142{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700143 adapter->pci_write_normalize(adapter,
144 adapter->crb_addr_cmd_consumer, crb_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700145}
146
Dhananjay Phadke29566402008-07-21 19:44:04 -0700147static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700148 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700149 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
150 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
151 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700152};
153
154static uint32_t sw_int_mask[4] = {
155 CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1,
156 CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3
157};
158
Dhananjay Phadke29566402008-07-21 19:44:04 -0700159static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
160
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700161static void netxen_nic_disable_int(struct netxen_adapter *adapter)
162{
Dhananjay Phadke443be792008-03-17 19:59:48 -0700163 u32 mask = 0x7ff;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700164 int retries = 32;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700165 int port = adapter->portnum;
166 int pci_fn = adapter->ahw.pci_func;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700167
Jeff Garzikf0c88f92008-03-25 23:53:24 -0400168 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700169 adapter->pci_write_normalize(adapter, sw_int_mask[port], 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700170
171 if (adapter->intr_scheme != -1 &&
172 adapter->intr_scheme != INTR_SCHEME_PERPORT)
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700173 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700174
Dhananjay Phadke29566402008-07-21 19:44:04 -0700175 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700176 do {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700177 adapter->pci_write_immediate(adapter,
178 ISR_INT_TARGET_STATUS, 0xffffffff);
179 mask = adapter->pci_read_immediate(adapter,
180 ISR_INT_VECTOR);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700181 if (!(mask & 0x80))
182 break;
183 udelay(10);
184 } while (--retries);
185
186 if (!retries) {
187 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
188 netxen_nic_driver_name);
189 }
Dhananjay Phadke443be792008-03-17 19:59:48 -0700190 } else {
191 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700192 adapter->pci_write_immediate(adapter,
193 msi_tgt_status[pci_fn], 0xffffffff);
Dhananjay Phadke443be792008-03-17 19:59:48 -0700194 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700195 }
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700196}
197
198static void netxen_nic_enable_int(struct netxen_adapter *adapter)
199{
200 u32 mask;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700201 int port = adapter->portnum;
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700202
203 DPRINTK(1, INFO, "Entered ISR Enable \n");
204
205 if (adapter->intr_scheme != -1 &&
206 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
207 switch (adapter->ahw.board_type) {
208 case NETXEN_NIC_GBE:
209 mask = 0x77b;
210 break;
211 case NETXEN_NIC_XGBE:
212 mask = 0x77f;
213 break;
214 default:
215 mask = 0x7ff;
216 break;
217 }
218
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700219 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700220 }
221
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700222 adapter->pci_write_normalize(adapter, sw_int_mask[port], 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700223
Dhananjay Phadke29566402008-07-21 19:44:04 -0700224 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700225 mask = 0xbff;
226 if (adapter->intr_scheme != -1 &&
227 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700228 adapter->pci_write_normalize(adapter,
229 CRB_INT_VECTOR, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700230 }
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700231 adapter->pci_write_immediate(adapter,
232 ISR_INT_TARGET_MASK, mask);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700233 }
234
235 DPRINTK(1, INFO, "Done with enable Int\n");
236}
237
Dhananjay Phadke29566402008-07-21 19:44:04 -0700238static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
239{
240 struct pci_dev *pdev = adapter->pdev;
241 int err;
242 uint64_t mask;
243
244#ifdef CONFIG_IA64
245 adapter->dma_mask = DMA_32BIT_MASK;
246#else
247 if (revision_id >= NX_P3_B0) {
248 /* should go to DMA_64BIT_MASK */
249 adapter->dma_mask = DMA_39BIT_MASK;
250 mask = DMA_39BIT_MASK;
251 } else if (revision_id == NX_P3_A2) {
252 adapter->dma_mask = DMA_39BIT_MASK;
253 mask = DMA_39BIT_MASK;
254 } else if (revision_id == NX_P2_C1) {
255 adapter->dma_mask = DMA_35BIT_MASK;
256 mask = DMA_35BIT_MASK;
257 } else {
258 adapter->dma_mask = DMA_32BIT_MASK;
259 mask = DMA_32BIT_MASK;
260 goto set_32_bit_mask;
261 }
262
263 /*
264 * Consistent DMA mask is set to 32 bit because it cannot be set to
265 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
266 * come off this pool.
267 */
268 if (pci_set_dma_mask(pdev, mask) == 0 &&
269 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
270 adapter->pci_using_dac = 1;
271 return 0;
272 }
273#endif /* CONFIG_IA64 */
274
275set_32_bit_mask:
276 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
277 if (!err)
278 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
279 if (err) {
280 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
281 return err;
282 }
283
284 adapter->pci_using_dac = 0;
285 return 0;
286}
287
288static void netxen_check_options(struct netxen_adapter *adapter)
289{
290 switch (adapter->ahw.boardcfg.board_type) {
291 case NETXEN_BRDTYPE_P3_HMEZ:
292 case NETXEN_BRDTYPE_P3_XG_LOM:
293 case NETXEN_BRDTYPE_P3_10G_CX4:
294 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
295 case NETXEN_BRDTYPE_P3_IMEZ:
296 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
297 case NETXEN_BRDTYPE_P3_10G_XFP:
298 case NETXEN_BRDTYPE_P3_10000_BASE_T:
299 adapter->msix_supported = !!use_msi_x;
300 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
301 break;
302
303 case NETXEN_BRDTYPE_P2_SB31_10G:
304 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
305 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
306 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
307 adapter->msix_supported = 0;
308 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
309 break;
310
311 case NETXEN_BRDTYPE_P3_REF_QG:
312 case NETXEN_BRDTYPE_P3_4_GB:
313 case NETXEN_BRDTYPE_P3_4_GB_MM:
314 case NETXEN_BRDTYPE_P2_SB35_4G:
315 case NETXEN_BRDTYPE_P2_SB31_2G:
316 adapter->msix_supported = 0;
317 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
318 break;
319
320 default:
321 adapter->msix_supported = 0;
322 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
323
324 printk(KERN_WARNING "Unknown board type(0x%x)\n",
325 adapter->ahw.boardcfg.board_type);
326 break;
327 }
328
329 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
330 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
331 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
332
333 adapter->max_possible_rss_rings = 1;
334 return;
335}
336
337static int
338netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
339{
340 int ret = 0;
341
342 if (first_boot == 0x55555555) {
343 /* This is the first boot after power up */
344
345 /* PCI bus master workaround */
346 adapter->hw_read_wx(adapter,
347 NETXEN_PCIE_REG(0x4), &first_boot, 4);
348 if (!(first_boot & 0x4)) {
349 first_boot |= 0x4;
350 adapter->hw_write_wx(adapter,
351 NETXEN_PCIE_REG(0x4), &first_boot, 4);
352 adapter->hw_read_wx(adapter,
353 NETXEN_PCIE_REG(0x4), &first_boot, 4);
354 }
355
356 /* This is the first boot after power up */
357 adapter->hw_read_wx(adapter,
358 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
359 if (first_boot != 0x80000f) {
360 /* clear the register for future unloads/loads */
361 adapter->pci_write_normalize(adapter,
362 NETXEN_CAM_RAM(0x1fc), 0);
363 ret = -1;
364 }
365
366 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
367 /* Start P2 boot loader */
368 adapter->pci_write_normalize(adapter,
369 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
370 adapter->pci_write_normalize(adapter,
371 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
372 }
373 }
374 return ret;
375}
376
377static void netxen_set_port_mode(struct netxen_adapter *adapter)
378{
379 u32 val, data;
380
381 val = adapter->ahw.boardcfg.board_type;
382 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
383 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
384 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
385 data = NETXEN_PORT_MODE_802_3_AP;
386 adapter->hw_write_wx(adapter,
387 NETXEN_PORT_MODE_ADDR, &data, 4);
388 } else if (port_mode == NETXEN_PORT_MODE_XG) {
389 data = NETXEN_PORT_MODE_XG;
390 adapter->hw_write_wx(adapter,
391 NETXEN_PORT_MODE_ADDR, &data, 4);
392 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
393 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
394 adapter->hw_write_wx(adapter,
395 NETXEN_PORT_MODE_ADDR, &data, 4);
396 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
397 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
398 adapter->hw_write_wx(adapter,
399 NETXEN_PORT_MODE_ADDR, &data, 4);
400 } else {
401 data = NETXEN_PORT_MODE_AUTO_NEG;
402 adapter->hw_write_wx(adapter,
403 NETXEN_PORT_MODE_ADDR, &data, 4);
404 }
405
406 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
407 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
408 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
409 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
410 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
411 }
412 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
413 &wol_port_mode, 4);
414 }
415}
416
417#define PCI_CAP_ID_GEN 0x10
418
419static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
420{
421 u32 pdevfuncsave;
422 u32 c8c9value = 0;
423 u32 chicken = 0;
424 u32 control = 0;
425 int i, pos;
426 struct pci_dev *pdev;
427
428 pdev = pci_get_device(0x1166, 0x0140, NULL);
429 if (pdev) {
430 pci_dev_put(pdev);
431 adapter->hw_read_wx(adapter,
432 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
433 chicken |= 0x4000;
434 adapter->hw_write_wx(adapter,
435 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
436 }
437
438 pdev = adapter->pdev;
439
440 adapter->hw_read_wx(adapter,
441 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
442 /* clear chicken3.25:24 */
443 chicken &= 0xFCFFFFFF;
444 /*
445 * if gen1 and B0, set F1020 - if gen 2, do nothing
446 * if gen2 set to F1000
447 */
448 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
449 if (pos == 0xC0) {
450 pci_read_config_dword(pdev, pos + 0x10, &control);
451 if ((control & 0x000F0000) != 0x00020000) {
452 /* set chicken3.24 if gen1 */
453 chicken |= 0x01000000;
454 }
455 printk(KERN_INFO "%s Gen2 strapping detected\n",
456 netxen_nic_driver_name);
457 c8c9value = 0xF1000;
458 } else {
459 /* set chicken3.24 if gen1 */
460 chicken |= 0x01000000;
461 printk(KERN_INFO "%s Gen1 strapping detected\n",
462 netxen_nic_driver_name);
463 if (adapter->ahw.revision_id == NX_P3_B0)
464 c8c9value = 0xF1020;
465 else
466 c8c9value = 0;
467
468 }
469 adapter->hw_write_wx(adapter,
470 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
471
472 if (!c8c9value)
473 return;
474
475 pdevfuncsave = pdev->devfn;
476 if (pdevfuncsave & 0x07)
477 return;
478
479 for (i = 0; i < 8; i++) {
480 pci_read_config_dword(pdev, pos + 8, &control);
481 pci_read_config_dword(pdev, pos + 8, &control);
482 pci_write_config_dword(pdev, pos + 8, c8c9value);
483 pdev->devfn++;
484 }
485 pdev->devfn = pdevfuncsave;
486}
487
488static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
489{
490 u32 control;
491 int pos;
492
493 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
494 if (pos) {
495 pci_read_config_dword(pdev, pos, &control);
496 if (enable)
497 control |= PCI_MSIX_FLAGS_ENABLE;
498 else
499 control = 0;
500 pci_write_config_dword(pdev, pos, control);
501 }
502}
503
504static void netxen_init_msix_entries(struct netxen_adapter *adapter)
505{
506 int i;
507
508 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
509 adapter->msix_entries[i].entry = i;
510}
511
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400512/*
513 * netxen_nic_probe()
514 *
515 * The Linux system will invoke this after identifying the vendor ID and
516 * device Id in the pci_tbl supported by this module.
517 *
518 * A quad port card has one operational PCI config space, (function 0),
519 * which is used to access all four ports.
520 *
521 * This routine will initialize the adapter, and setup the global parameters
522 * along with the port's specific structure.
523 */
524static int __devinit
525netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
526{
527 struct net_device *netdev = NULL;
528 struct netxen_adapter *adapter = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800529 void __iomem *mem_ptr0 = NULL;
530 void __iomem *mem_ptr1 = NULL;
531 void __iomem *mem_ptr2 = NULL;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700532 unsigned long first_page_group_end;
533 unsigned long first_page_group_start;
534
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800535
Al Viroa608ab9c2007-01-02 10:39:10 +0000536 u8 __iomem *db_ptr = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700537 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
538 int i = 0, err;
539 int first_driver, first_boot;
Al Virof305f782007-12-22 19:44:00 +0000540 __le64 mac_addr[FLASH_NUM_PORTS + 1];
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700541 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700542 int pci_func_id = PCI_FUNC(pdev->devfn);
Joe Perches0795af52007-10-03 17:59:30 -0700543 DECLARE_MAC_BUF(mac);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700544 struct netxen_legacy_intr_set *legacy_intrp;
545 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400546
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700547 if (pci_func_id == 0)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700548 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700549
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700550 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700551 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700552 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800553 return -ENODEV;
554 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700555
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400556 if ((err = pci_enable_device(pdev)))
557 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700558
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400559 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
560 err = -ENODEV;
561 goto err_out_disable_pdev;
562 }
563
564 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
565 goto err_out_disable_pdev;
566
567 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700568
569 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
570 if(!netdev) {
571 printk(KERN_ERR"%s: Failed to allocate memory for the "
572 "device block.Check system memory resource"
573 " usage.\n", netxen_nic_driver_name);
574 goto err_out_free_res;
575 }
576
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700577 SET_NETDEV_DEV(netdev, &pdev->dev);
578
579 adapter = netdev->priv;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700580 adapter->netdev = netdev;
581 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700582 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700583
584 revision_id = pdev->revision;
585 adapter->ahw.revision_id = revision_id;
586
587 err = nx_set_dma_mask(adapter, revision_id);
588 if (err)
589 goto err_out_free_netdev;
590
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700591 rwlock_init(&adapter->adapter_lock);
592 adapter->ahw.qdr_sn_window = -1;
593 adapter->ahw.ddr_mn_window = -1;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700594
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400595 /* remap phys address */
596 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
597 mem_len = pci_resource_len(pdev, 0);
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700598 pci_len0 = 0;
599
600 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
601 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
602 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
603 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
604 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
605 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
606 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
607 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
608 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400609
610 /* 128 Meg of memory */
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700611 if (mem_len == NETXEN_PCI_128MB_SIZE) {
612 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
613 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
614 SECOND_PAGE_GROUP_SIZE);
615 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
616 THIRD_PAGE_GROUP_SIZE);
617 first_page_group_start = FIRST_PAGE_GROUP_START;
618 first_page_group_end = FIRST_PAGE_GROUP_END;
619 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
620 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
621 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
622 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
623 first_page_group_start = 0;
624 first_page_group_end = 0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700625 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
626 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
627 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
628 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
629 adapter->pci_write_immediate =
630 netxen_nic_pci_write_immediate_2M;
631 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
632 adapter->pci_write_normalize =
633 netxen_nic_pci_write_normalize_2M;
634 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
635 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
636 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
637
638 mem_ptr0 = ioremap(mem_base, mem_len);
639 pci_len0 = mem_len;
640 first_page_group_start = 0;
641 first_page_group_end = 0;
642
643 adapter->ahw.ddr_mn_window = 0;
644 adapter->ahw.qdr_sn_window = 0;
645
646 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
647 (pci_func_id * 0x20);
648 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
649 if (pci_func_id < 4)
650 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
651 else
652 adapter->ahw.ms_win_crb +=
653 0xA0 + ((pci_func_id - 4) * 0x10);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700654 } else {
Jeff Garzik47906542007-11-23 21:23:36 -0500655 err = -EIO;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700656 goto err_out_free_netdev;
657 }
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800658
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700659 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800660
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800661 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
662 db_len = pci_resource_len(pdev, 4);
663
664 if (db_len == 0) {
665 printk(KERN_ERR "%s: doorbell is disabled\n",
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 ioremap from %lx a size of %lx\n", db_base,
671 db_len);
672
673 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
Al Viroa608ab9c2007-01-02 10:39:10 +0000674 if (!db_ptr) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800675 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700676 netxen_nic_driver_name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800677 err = -EIO;
678 goto err_out_iounmap;
679 }
680 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400681
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700682 adapter->ahw.pci_base0 = mem_ptr0;
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700683 adapter->ahw.pci_len0 = pci_len0;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700684 adapter->ahw.first_page_group_start = first_page_group_start;
685 adapter->ahw.first_page_group_end = first_page_group_end;
686 adapter->ahw.pci_base1 = mem_ptr1;
687 adapter->ahw.pci_base2 = mem_ptr2;
688 adapter->ahw.db_base = db_ptr;
689 adapter->ahw.db_len = db_len;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400690
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700691 netif_napi_add(netdev, &adapter->napi,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700692 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
693
694 if (revision_id >= NX_P3_B0)
695 legacy_intrp = &legacy_intr[pci_func_id];
696 else
697 legacy_intrp = &legacy_intr[0];
698
699 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
700 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
701 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
702 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700703
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530704 /* this will be read from FW later */
705 adapter->intr_scheme = -1;
Dhananjay Phadke443be792008-03-17 19:59:48 -0700706 adapter->msi_mode = -1;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530707
708 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700709 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530710 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530711 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -0700712 adapter->mc_enabled = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700713 if (NX_IS_REVISION_P3(revision_id))
714 adapter->max_mc_count = 38;
715 else
716 adapter->max_mc_count = 16;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700717
718 netdev->open = netxen_nic_open;
719 netdev->stop = netxen_nic_close;
720 netdev->hard_start_xmit = netxen_nic_xmit_frame;
Jeff Garzik47906542007-11-23 21:23:36 -0500721 netdev->get_stats = netxen_nic_get_stats;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700722 netdev->set_multicast_list = netxen_nic_set_multi;
723 netdev->set_mac_address = netxen_nic_set_mac;
724 netdev->change_mtu = netxen_nic_change_mtu;
725 netdev->tx_timeout = netxen_tx_timeout;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700726 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700727
728 netxen_nic_change_mtu(netdev, netdev->mtu);
729
730 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700731#ifdef CONFIG_NET_POLL_CONTROLLER
732 netdev->poll_controller = netxen_nic_poll_controller;
733#endif
734 /* ScatterGather support */
735 netdev->features = NETIF_F_SG;
736 netdev->features |= NETIF_F_IP_CSUM;
737 netdev->features |= NETIF_F_TSO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700738 if (NX_IS_REVISION_P3(revision_id))
739 netdev->features |= NETIF_F_TSO6;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700740
Dhananjay Phadke29566402008-07-21 19:44:04 -0700741 if (adapter->pci_using_dac)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700742 netdev->features |= NETIF_F_HIGHDMA;
743
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700744 /*
745 * Set the CRB window to invalid. If any register in window 0 is
746 * accessed it should set the window to 0 and then reset it to 1.
747 */
748 adapter->curr_window = 255;
749
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700750 if (netxen_nic_get_board_info(adapter) != 0) {
751 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700752 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700753 err = -EIO;
754 goto err_out_iounmap;
755 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700756
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700757 netxen_initialize_adapter_ops(adapter);
758
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700759 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700760 switch (adapter->ahw.boardcfg.board_type) {
761 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
762 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
763 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700764 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700765 break;
766 default:
767 break;
768 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700769
Dhananjay Phadke29566402008-07-21 19:44:04 -0700770 /*
771 * This call will setup various max rx/tx counts.
772 * It must be done before any buffer/ring allocations.
773 */
774 netxen_check_options(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400775
Dhananjay Phadke29566402008-07-21 19:44:04 -0700776 first_driver = 0;
777 if (NX_IS_REVISION_P3(revision_id)) {
778 if (adapter->ahw.pci_func == 0)
779 first_driver = 1;
780 } else {
781 if (adapter->portnum == 0)
782 first_driver = 1;
783 }
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700784 adapter->crb_addr_cmd_producer = crb_cmd_producer[adapter->portnum];
785 adapter->crb_addr_cmd_consumer = crb_cmd_consumer[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700786 netxen_nic_update_cmd_producer(adapter, 0);
787 netxen_nic_update_cmd_consumer(adapter, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400788
Dhananjay Phadke29566402008-07-21 19:44:04 -0700789 if (first_driver) {
790 first_boot = adapter->pci_read_normalize(adapter,
791 NETXEN_CAM_RAM(0x1fc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400792
Dhananjay Phadke29566402008-07-21 19:44:04 -0700793 err = netxen_check_hw_init(adapter, first_boot);
794 if (err) {
795 printk(KERN_ERR "%s: error in init HW init sequence\n",
796 netxen_nic_driver_name);
797 goto err_out_iounmap;
798 }
799
800 if (NX_IS_REVISION_P3(revision_id))
801 netxen_set_port_mode(adapter);
802
803 if (first_boot != 0x55555555) {
804 adapter->pci_write_normalize(adapter,
805 CRB_CMDPEG_STATE, 0);
806 netxen_pinit_from_rom(adapter, 0);
807 msleep(1);
808 netxen_load_firmware(adapter);
809 }
810
811 if (NX_IS_REVISION_P3(revision_id))
812 netxen_pcie_strap_init(adapter);
813
814 if (NX_IS_REVISION_P2(revision_id)) {
815
816 /* Initialize multicast addr pool owners */
817 val = 0x7654;
818 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
819 val |= 0x0f000000;
820 netxen_crb_writelit_adapter(adapter,
821 NETXEN_MAC_ADDR_CNTL_REG, val);
822
823 }
824
825 if ((first_boot == 0x55555555) &&
826 (NX_IS_REVISION_P2(revision_id))) {
827 /* Unlock the HW, prompting the boot sequence */
828 adapter->pci_write_normalize(adapter,
829 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
830 }
831
832 err = netxen_initialize_adapter_offload(adapter);
833 if (err)
834 goto err_out_iounmap;
835
836 /*
837 * Tell the hardware our version number.
838 */
839 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
840 | ((_NETXEN_NIC_LINUX_MINOR << 8))
841 | (_NETXEN_NIC_LINUX_SUBVERSION);
842 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
843
844 /* Handshake with the card before we register the devices. */
845 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
846
847 if (NX_IS_REVISION_P3(revision_id)) {
848 adapter->hw_read_wx(adapter,
849 NETXEN_MIU_MN_CONTROL, &val, 4);
850 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
851 dev_info(&pdev->dev, "firmware running in %s mode\n",
852 adapter->ahw.cut_through ? "cut through" : "legacy");
853 }
854 } /* first_driver */
855
856 netxen_nic_flash_print(adapter);
857
858 /*
859 * See if the firmware gave us a virtual-physical port mapping.
860 */
861 adapter->physical_port = adapter->portnum;
862 i = adapter->pci_read_normalize(adapter, CRB_V2P(adapter->portnum));
863 if (i != 0x55555555)
864 adapter->physical_port = i;
865
866 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
867
868 netxen_set_msix_bit(pdev, 0);
869
870 if (NX_IS_REVISION_P3(revision_id)) {
871 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
872 mem_len != NETXEN_PCI_2MB_SIZE)
873 adapter->msix_supported = 0;
874 }
875
876 if (adapter->msix_supported) {
877
878 netxen_init_msix_entries(adapter);
879
880 if (pci_enable_msix(pdev, adapter->msix_entries,
881 MSIX_ENTRIES_PER_ADAPTER))
882 goto request_msi;
883
884 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
885 netxen_set_msix_bit(pdev, 1);
886 dev_info(&pdev->dev, "using msi-x interrupts\n");
887
888 } else {
889request_msi:
890 if (use_msi && !pci_enable_msi(pdev)) {
891 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
892 dev_info(&pdev->dev, "using msi interrupts\n");
893 } else
894 dev_info(&pdev->dev, "using legacy interrupts\n");
895 }
896
897 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
898 netdev->irq = adapter->msix_entries[0].vector;
899 else
900 netdev->irq = pdev->irq;
901
902 err = netxen_receive_peg_ready(adapter);
903 if (err)
904 goto err_out_disable_msi;
905
906 init_timer(&adapter->watchdog_timer);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -0700907 adapter->ahw.linkup = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700908 adapter->watchdog_timer.function = &netxen_watchdog;
909 adapter->watchdog_timer.data = (unsigned long)adapter;
910 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
911 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
912
913 if (netxen_is_flash_supported(adapter) == 0 &&
914 netxen_get_flash_mac_addr(adapter, mac_addr) == 0) {
915 unsigned char *p;
916
917 p = (unsigned char *)&mac_addr[adapter->portnum];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700918 netdev->dev_addr[0] = *(p + 5);
919 netdev->dev_addr[1] = *(p + 4);
920 netdev->dev_addr[2] = *(p + 3);
921 netdev->dev_addr[3] = *(p + 2);
922 netdev->dev_addr[4] = *(p + 1);
923 netdev->dev_addr[5] = *(p + 0);
924
925 memcpy(netdev->perm_addr, netdev->dev_addr,
926 netdev->addr_len);
927 if (!is_valid_ether_addr(netdev->perm_addr)) {
Joe Perches0795af52007-10-03 17:59:30 -0700928 printk(KERN_ERR "%s: Bad MAC address %s.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700929 netxen_nic_driver_name,
930 print_mac(mac, netdev->dev_addr));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700931 } else {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700932 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700933 }
934 }
935
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700936 netif_carrier_off(netdev);
937 netif_stop_queue(netdev);
938
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700939 if ((err = register_netdev(netdev))) {
940 printk(KERN_ERR "%s: register_netdev failed port #%d"
941 " aborting\n", netxen_nic_driver_name,
942 adapter->portnum);
943 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700944 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700945 }
946
947 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400948
Dhananjay Phadke29566402008-07-21 19:44:04 -0700949 switch (adapter->ahw.board_type) {
950 case NETXEN_NIC_GBE:
951 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
952 adapter->netdev->name);
953 break;
954 case NETXEN_NIC_XGBE:
955 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
956 adapter->netdev->name);
957 break;
958 }
959
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400960 return 0;
961
Dhananjay Phadke29566402008-07-21 19:44:04 -0700962err_out_disable_msi:
963 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
964 pci_disable_msix(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700965 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
966 pci_disable_msi(pdev);
967
Dhananjay Phadke29566402008-07-21 19:44:04 -0700968 if (first_driver)
969 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400970
Dhananjay Phadke29566402008-07-21 19:44:04 -0700971err_out_iounmap:
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800972 if (db_ptr)
973 iounmap(db_ptr);
974
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800975 if (mem_ptr0)
976 iounmap(mem_ptr0);
977 if (mem_ptr1)
978 iounmap(mem_ptr1);
979 if (mem_ptr2)
980 iounmap(mem_ptr2);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800981
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700982err_out_free_netdev:
983 free_netdev(netdev);
984
985err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400986 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700987
988err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -0700989 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400990 pci_disable_device(pdev);
991 return err;
992}
993
994static void __devexit netxen_nic_remove(struct pci_dev *pdev)
995{
996 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700997 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400998
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700999 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001000 if (adapter == NULL)
1001 return;
1002
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001003 netdev = adapter->netdev;
1004
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301005 unregister_netdev(netdev);
1006
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301007 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001008 netxen_free_hw_resources(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001009 netxen_free_sw_resources(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301010 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001011
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001012 if (adapter->portnum == 0)
1013 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301014
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301015 if (adapter->irq)
1016 free_irq(adapter->irq, adapter);
1017
Dhananjay Phadke29566402008-07-21 19:44:04 -07001018 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1019 pci_disable_msix(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301020 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1021 pci_disable_msi(pdev);
1022
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001023 iounmap(adapter->ahw.db_base);
1024 iounmap(adapter->ahw.pci_base0);
1025 iounmap(adapter->ahw.pci_base1);
1026 iounmap(adapter->ahw.pci_base2);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001027
1028 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301029 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001030 pci_set_drvdata(pdev, NULL);
1031
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001032 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001033}
1034
1035/*
1036 * Called when a network interface is made active
1037 * @returns 0 on success, negative value on failure
1038 */
1039static int netxen_nic_open(struct net_device *netdev)
1040{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001041 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001042 int err = 0;
1043 int ctx, ring;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001044 irq_handler_t handler;
1045 unsigned long flags = IRQF_SAMPLE_RANDOM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001046
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001047 if (adapter->driver_mismatch)
1048 return -EIO;
1049
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001050 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1051 err = netxen_init_firmware(adapter);
1052 if (err != 0) {
1053 printk(KERN_ERR "Failed to init firmware\n");
1054 return -EIO;
1055 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001056
Dhananjay Phadke29566402008-07-21 19:44:04 -07001057 err = netxen_alloc_sw_resources(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001058 if (err) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001059 printk(KERN_ERR "%s: Error in setting sw resources\n",
1060 netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001061 return err;
1062 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001063
1064 netxen_nic_clear_stats(adapter);
1065
1066 err = netxen_alloc_hw_resources(adapter);
1067 if (err) {
1068 printk(KERN_ERR "%s: Error in setting hw resources\n",
1069 netdev->name);
1070 goto err_out_free_sw;
1071 }
1072
1073 if (adapter->fw_major < 4) {
1074 adapter->crb_addr_cmd_producer =
1075 crb_cmd_producer[adapter->portnum];
1076 adapter->crb_addr_cmd_consumer =
1077 crb_cmd_consumer[adapter->portnum];
1078 }
1079
1080 netxen_nic_update_cmd_producer(adapter, 0);
1081 netxen_nic_update_cmd_consumer(adapter, 0);
1082
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001083 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001084 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001085 netxen_post_rx_buffers(adapter, ctx, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001086 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001087 if (NETXEN_IS_MSI_FAMILY(adapter))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001088 handler = netxen_msi_intr;
1089 else {
1090 flags |= IRQF_SHARED;
1091 handler = netxen_intr;
1092 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001093 adapter->irq = netdev->irq;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001094 err = request_irq(adapter->irq, handler,
1095 flags, netdev->name, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001096 if (err) {
1097 printk(KERN_ERR "request_irq failed with: %d\n", err);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001098 goto err_out_free_hw;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001099 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001100
1101 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1102 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001103
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001104 /* Done here again so that even if phantom sw overwrote it,
1105 * we set it */
Dhananjay Phadke29566402008-07-21 19:44:04 -07001106 err = adapter->init_port(adapter, adapter->portnum);
1107 if (err) {
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001108 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1109 netxen_nic_driver_name, adapter->portnum);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001110 goto err_out_free_irq;
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001111 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001112 adapter->macaddr_set(adapter, netdev->dev_addr);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001113
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001114 netxen_nic_set_link_parameters(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001115
1116 netxen_nic_set_multi(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001117 adapter->set_mtu(adapter, netdev->mtu);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001118
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001119 mod_timer(&adapter->watchdog_timer, jiffies);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001120
1121 napi_enable(&adapter->napi);
1122 netxen_nic_enable_int(adapter);
1123
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001124 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001125
1126 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001127
1128err_out_free_irq:
1129 free_irq(adapter->irq, adapter);
1130err_out_free_hw:
1131 netxen_free_hw_resources(adapter);
1132err_out_free_sw:
1133 netxen_free_sw_resources(adapter);
1134 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001135}
1136
1137/*
1138 * netxen_nic_close - Disables a network interface entry point
1139 */
1140static int netxen_nic_close(struct net_device *netdev)
1141{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001142 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001143
1144 netif_carrier_off(netdev);
1145 netif_stop_queue(netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001146 napi_disable(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001147
dhananjay@netxen.com72b0a7a2007-12-26 10:23:56 -08001148 if (adapter->stop_port)
1149 adapter->stop_port(adapter);
1150
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301151 netxen_nic_disable_int(adapter);
1152
Dhananjay Phadke29566402008-07-21 19:44:04 -07001153 netxen_release_tx_buffers(adapter);
1154
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301155 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1156 FLUSH_SCHEDULED_WORK();
1157 del_timer_sync(&adapter->watchdog_timer);
1158 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001159
1160 return 0;
1161}
1162
1163static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1164{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001165 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001166 struct netxen_hardware_context *hw = &adapter->ahw;
1167 unsigned int first_seg_len = skb->len - skb->data_len;
1168 struct netxen_skb_frag *buffrag;
1169 unsigned int i;
1170
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001171 u32 producer, consumer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001172 u32 saved_producer = 0;
1173 struct cmd_desc_type0 *hwdesc;
1174 int k;
1175 struct netxen_cmd_buffer *pbuf = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001176 int frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001177 int no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001178 u32 num_txd = adapter->max_tx_desc_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001179
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001180 frag_count = skb_shinfo(skb)->nr_frags + 1;
1181
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001182 /* There 4 fragments per descriptor */
1183 no_of_desc = (frag_count + 3) >> 2;
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001184 if (netdev->features & NETIF_F_TSO) {
1185 if (skb_shinfo(skb)->gso_size > 0) {
1186
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001187 no_of_desc++;
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001188 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001189 sizeof(struct ethhdr)) >
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001190 (sizeof(struct cmd_desc_type0) - 2)) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001191 no_of_desc++;
1192 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001193 }
1194 }
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001195
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001196 producer = adapter->cmd_producer;
1197 smp_mb();
1198 consumer = adapter->last_cmd_consumer;
1199 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1200 netif_stop_queue(netdev);
1201 smp_mb();
1202 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001203 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001204
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001205 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001206 saved_producer = producer;
1207 hwdesc = &hw->cmd_desc_head[producer];
1208 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1209 /* Take skb->data itself */
1210 pbuf = &adapter->cmd_buf_arr[producer];
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001211 if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) {
Al Viroa608ab9c2007-01-02 10:39:10 +00001212 pbuf->mss = skb_shinfo(skb)->gso_size;
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001213 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001214 } else {
1215 pbuf->mss = 0;
1216 hwdesc->mss = 0;
1217 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001218 pbuf->total_length = skb->len;
1219 pbuf->skb = skb;
1220 pbuf->cmd = TX_ETHER_PKT;
1221 pbuf->frag_count = frag_count;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001222 pbuf->port = adapter->portnum;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001223 buffrag = &pbuf->frag_array[0];
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001224 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001225 PCI_DMA_TODEVICE);
1226 buffrag->length = first_seg_len;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001227 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1228 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1229 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001230
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001231 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001232 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001233 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1234 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1235
1236 for (i = 1, k = 1; i < frag_count; i++, k++) {
1237 struct skb_frag_struct *frag;
1238 int len, temp_len;
1239 unsigned long offset;
1240 dma_addr_t temp_dma;
1241
1242 /* move to next desc. if there is a need */
1243 if ((i & 0x3) == 0) {
1244 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001245 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001246 hwdesc = &hw->cmd_desc_head[producer];
1247 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001248 pbuf = &adapter->cmd_buf_arr[producer];
1249 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001250 }
1251 frag = &skb_shinfo(skb)->frags[i - 1];
1252 len = frag->size;
1253 offset = frag->page_offset;
1254
1255 temp_len = len;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001256 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001257 len, PCI_DMA_TODEVICE);
1258
1259 buffrag++;
1260 buffrag->dma = temp_dma;
1261 buffrag->length = temp_len;
1262
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001263 switch (k) {
1264 case 0:
1265 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1266 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1267 break;
1268 case 1:
1269 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1270 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1271 break;
1272 case 2:
1273 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1274 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1275 break;
1276 case 3:
Al Viroa608ab9c2007-01-02 10:39:10 +00001277 hwdesc->buffer4_length = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001278 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1279 break;
1280 }
1281 frag++;
1282 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001283 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001284
1285 /* might change opcode to TX_TCP_LSO */
1286 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1287
1288 /* For LSO, we need to copy the MAC/IP/TCP headers into
1289 * the descriptor ring
1290 */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001291 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1292 == TX_TCP_LSO) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001293 int hdr_len, first_hdr_len, more_hdr;
1294 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001295 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1296 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001297 more_hdr = 1;
1298 } else {
1299 first_hdr_len = hdr_len;
1300 more_hdr = 0;
1301 }
1302 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
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
1307 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001308 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001309 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001310 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001311
1312 if (more_hdr) {
1313 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001314 pbuf = &adapter->cmd_buf_arr[producer];
1315 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001316 /* copy the next 64 bytes - should be enough except
1317 * for pathological case
1318 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001319 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1320 hwdesc,
1321 (hdr_len -
1322 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001323 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001324 }
1325 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001326
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001327 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001328 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001329
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001330 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001331
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001332 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001333 netdev->trans_start = jiffies;
1334
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001335 return NETDEV_TX_OK;
1336}
1337
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001338static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1339{
1340 struct net_device *netdev = adapter->netdev;
1341 uint32_t temp, temp_state, temp_val;
1342 int rv = 0;
1343
1344 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1345
1346 temp_state = nx_get_temp_state(temp);
1347 temp_val = nx_get_temp_val(temp);
1348
1349 if (temp_state == NX_TEMP_PANIC) {
1350 printk(KERN_ALERT
1351 "%s: Device temperature %d degrees C exceeds"
1352 " maximum allowed. Hardware has been shut down.\n",
1353 netxen_nic_driver_name, temp_val);
1354
1355 netif_carrier_off(netdev);
1356 netif_stop_queue(netdev);
1357 rv = 1;
1358 } else if (temp_state == NX_TEMP_WARN) {
1359 if (adapter->temp == NX_TEMP_NORMAL) {
1360 printk(KERN_ALERT
1361 "%s: Device temperature %d degrees C "
1362 "exceeds operating range."
1363 " Immediate action needed.\n",
1364 netxen_nic_driver_name, temp_val);
1365 }
1366 } else {
1367 if (adapter->temp == NX_TEMP_WARN) {
1368 printk(KERN_INFO
1369 "%s: Device temperature is now %d degrees C"
1370 " in normal range.\n", netxen_nic_driver_name,
1371 temp_val);
1372 }
1373 }
1374 adapter->temp = temp_state;
1375 return rv;
1376}
1377
1378static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1379{
1380 struct net_device *netdev = adapter->netdev;
1381 u32 val, port, linkup;
1382
1383 port = adapter->physical_port;
1384
1385 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1386 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1387 linkup = (val >> port) & 1;
1388 else {
1389 val = (val >> port*8) & 0xff;
1390 linkup = (val == XG_LINK_UP);
1391 }
1392
1393 if (adapter->ahw.linkup && !linkup) {
1394 printk(KERN_INFO "%s: %s NIC Link is down\n",
1395 netxen_nic_driver_name, netdev->name);
1396 adapter->ahw.linkup = 0;
1397 if (netif_running(netdev)) {
1398 netif_carrier_off(netdev);
1399 netif_stop_queue(netdev);
1400 }
1401 } else if (!adapter->ahw.linkup && linkup) {
1402 printk(KERN_INFO "%s: %s NIC Link is up\n",
1403 netxen_nic_driver_name, netdev->name);
1404 adapter->ahw.linkup = 1;
1405 if (netif_running(netdev)) {
1406 netif_carrier_on(netdev);
1407 netif_wake_queue(netdev);
1408 }
1409 }
1410}
1411
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001412static void netxen_watchdog(unsigned long v)
1413{
1414 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001415
1416 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001417}
1418
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001419void netxen_watchdog_task(struct work_struct *work)
1420{
1421 struct netxen_adapter *adapter =
1422 container_of(work, struct netxen_adapter, watchdog_task);
1423
1424 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1425 return;
1426
1427 netxen_nic_handle_phy_intr(adapter);
1428
1429 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1430}
1431
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001432static void netxen_tx_timeout(struct net_device *netdev)
1433{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001434 struct netxen_adapter *adapter = (struct netxen_adapter *)
1435 netdev_priv(netdev);
1436 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001437}
1438
David Howells6d5aefb2006-12-05 19:36:26 +00001439static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001440{
Jeff Garzik47906542007-11-23 21:23:36 -05001441 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001442 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001443
1444 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001445 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001446
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001447 netxen_nic_disable_int(adapter);
1448 napi_disable(&adapter->napi);
1449
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001450 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001451
1452 napi_enable(&adapter->napi);
1453 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001454 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001455}
1456
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001457/*
1458 * netxen_nic_get_stats - Get System Network Statistics
1459 * @netdev: network interface device structure
1460 */
1461struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1462{
1463 struct netxen_adapter *adapter = netdev_priv(netdev);
1464 struct net_device_stats *stats = &adapter->net_stats;
1465
1466 memset(stats, 0, sizeof(*stats));
1467
1468 /* total packets received */
1469 stats->rx_packets = adapter->stats.no_rcv;
1470 /* total packets transmitted */
1471 stats->tx_packets = adapter->stats.xmitedframes +
1472 adapter->stats.xmitfinished;
1473 /* total bytes received */
1474 stats->rx_bytes = adapter->stats.rxbytes;
1475 /* total bytes transmitted */
1476 stats->tx_bytes = adapter->stats.txbytes;
1477 /* bad packets received */
1478 stats->rx_errors = adapter->stats.rcvdbadskb;
1479 /* packet transmit problems */
1480 stats->tx_errors = adapter->stats.nocmddescriptor;
1481 /* no space in linux buffers */
1482 stats->rx_dropped = adapter->stats.rxdropped;
1483 /* no space available in linux */
1484 stats->tx_dropped = adapter->stats.txdropped;
1485
1486 return stats;
1487}
1488
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001489static inline void
1490netxen_handle_int(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001491{
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301492 netxen_nic_disable_int(adapter);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001493 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001494}
1495
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001496static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001497{
Jeff Garzik1daec862007-10-19 03:14:03 -04001498 struct netxen_adapter *adapter = data;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301499 u32 our_int = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001500
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001501 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001502 /* not our interrupt */
1503 if ((our_int & (0x80 << adapter->portnum)) == 0)
1504 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301505
1506 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1507 /* claim interrupt */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001508 adapter->pci_write_normalize(adapter, CRB_INT_VECTOR,
1509 our_int & ~((u32)(0x80 << adapter->portnum)));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301510 }
1511
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001512 netxen_handle_int(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001513
1514 return IRQ_HANDLED;
1515}
1516
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001517static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001518{
1519 struct netxen_adapter *adapter = data;
1520
1521 netxen_handle_int(adapter);
1522 return IRQ_HANDLED;
1523}
1524
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001525static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001526{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001527 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001528 int tx_complete;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001529 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001530 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001531
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001532 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001533
Amit S. Kale80922fb2006-12-04 09:18:00 -08001534 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001535 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1536 /*
1537 * Fairness issue. This will give undue weight to the
1538 * receive context 0.
1539 */
1540
1541 /*
1542 * To avoid starvation, we give each of our receivers,
1543 * a fraction of the quota. Sometimes, it might happen that we
1544 * have enough quota to process every packet, but since all the
1545 * packets are on one context, it gets only half of the quota,
1546 * and ends up not processing it.
1547 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001548 work_done += netxen_process_rcv_ring(adapter, ctx,
1549 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001550 }
1551
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001552 if ((work_done < budget) && tx_complete) {
1553 netif_rx_complete(adapter->netdev, &adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001554 netxen_nic_enable_int(adapter);
1555 }
1556
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001557 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001558}
1559
1560#ifdef CONFIG_NET_POLL_CONTROLLER
1561static void netxen_nic_poll_controller(struct net_device *netdev)
1562{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001563 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001564 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001565 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001566 enable_irq(adapter->irq);
1567}
1568#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001569
1570static struct pci_driver netxen_driver = {
1571 .name = netxen_nic_driver_name,
1572 .id_table = netxen_pci_tbl,
1573 .probe = netxen_nic_probe,
1574 .remove = __devexit_p(netxen_nic_remove)
1575};
1576
1577/* Driver Registration on NetXen card */
1578
1579static int __init netxen_init_module(void)
1580{
Al Viro79ea13c2008-01-24 02:06:46 -08001581 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001582 return -ENOMEM;
1583
Amit S. Kale184231b2007-01-22 06:52:53 -08001584 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001585}
1586
1587module_init(netxen_init_module);
1588
1589static void __exit netxen_exit_module(void)
1590{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001591 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001592 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001593}
1594
1595module_exit(netxen_exit_module);