blob: 8ffb30bc798f3c068e682c360a08a5a6166b342e [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
Dhananjay Phadke5d242f12009-02-25 15:57:56 +00002 * Copyright (C) 2003 - 2009 NetXen, Inc.
Dhananjay Phadke13af7a62009-09-11 11:28:15 +00003 * Copyright (C) 2009 - QLogic Corporation.
Amit S. Kale3d396eb2006-10-21 15:33:03 -04004 * All rights reserved.
Amit S. Kale80922fb2006-12-04 09:18:00 -08005 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -04006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Amit S. Kale80922fb2006-12-04 09:18:00 -080010 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040011 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Amit S. Kale80922fb2006-12-04 09:18:00 -080015 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
Amit S. Kale80922fb2006-12-04 09:18:00 -080020 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040021 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.
Amit S. Kale80922fb2006-12-04 09:18:00 -080023 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040024 */
25
Amit S. Kalecb8011a2006-11-29 09:00:10 -080026#include <linux/vmalloc.h>
Dhananjay Phadkee98e3352009-04-07 22:50:38 +000027#include <linux/interrupt.h>
Amit S. Kale3d396eb2006-10-21 15:33:03 -040028#include "netxen_nic_hw.h"
29
30#include "netxen_nic.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040031
Jeff Garzik1494a812006-11-07 05:12:16 -050032#include <linux/dma-mapping.h>
Dhananjay Phadke391587c2009-01-14 20:48:11 -080033#include <linux/if_vlan.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030034#include <net/ip.h>
Dhananjay Phadke00977782009-01-16 11:03:25 -080035#include <linux/ipv6.h>
Dhananjay Phadke6598b162009-07-26 20:07:37 +000036#include <linux/inetdevice.h>
Randy Dunlap0531d7b82009-10-26 12:09:45 +000037#include <linux/sysfs.h>
Jeff Garzik1494a812006-11-07 05:12:16 -050038
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -070039MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Converged Ethernet Driver");
Amit S. Kale3d396eb2006-10-21 15:33:03 -040040MODULE_LICENSE("GPL");
41MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
Dhananjay Phadke7e8e5d92009-10-24 16:04:02 +000042MODULE_FIRMWARE(NX_P2_MN_ROMIMAGE_NAME);
43MODULE_FIRMWARE(NX_P3_CT_ROMIMAGE_NAME);
44MODULE_FIRMWARE(NX_P3_MN_ROMIMAGE_NAME);
45MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040046
dhananjay@netxen.comceded322007-07-19 14:41:09 +053047char netxen_nic_driver_name[] = "netxen_nic";
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -070048static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
Amit S. Kalecb8011a2006-11-29 09:00:10 -080049 NETXEN_NIC_LINUX_VERSIONID;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040050
Dhananjay Phadke29566402008-07-21 19:44:04 -070051static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
52
53/* Default to restricted 1G auto-neg mode */
54static int wol_port_mode = 5;
55
56static int use_msi = 1;
57
58static int use_msi_x = 1;
59
Narender Kumarcaa2dd52009-10-16 15:50:11 +000060static unsigned long auto_fw_reset = AUTO_FW_RESET_ENABLED;
61
Amit S. Kale3d396eb2006-10-21 15:33:03 -040062static 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);
Stephen Hemminger613573252009-08-31 19:50:58 +000067static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
68 struct net_device *);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040069static void netxen_tx_timeout(struct net_device *netdev);
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +000070static void netxen_tx_timeout_task(struct work_struct *work);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +000071static void netxen_fw_poll_work(struct work_struct *work);
72static void netxen_schedule_work(struct netxen_adapter *adapter,
73 work_func_t func, int delay);
74static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
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
Narender Kumarfa3ce352009-08-24 19:23:28 +000079
80static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
81static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +000082static void netxen_create_diag_entries(struct netxen_adapter *adapter);
83static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
Narender Kumarfa3ce352009-08-24 19:23:28 +000084
Dhananjay Phadke6a581e92009-09-05 17:43:08 +000085static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
86static int netxen_can_start_firmware(struct netxen_adapter *adapter);
87
Jeff Garzik1494a812006-11-07 05:12:16 -050088static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070089static irqreturn_t netxen_msi_intr(int irq, void *data);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -080090static irqreturn_t netxen_msix_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040091
Amit Kumar Salechad0725e42009-09-05 17:43:09 +000092static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
93
Amit S. Kale3d396eb2006-10-21 15:33:03 -040094/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010095#define ENTRY(device) \
Dhananjay Phadke040dec32008-09-12 06:55:14 -070096 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
Al Viro70081ac2008-06-23 02:04:50 +010097 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
98
Amit S. Kale3d396eb2006-10-21 15:33:03 -040099static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Dhananjay Phadke040dec32008-09-12 06:55:14 -0700100 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
101 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
102 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
103 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
104 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
105 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
106 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
107 ENTRY(PCI_DEVICE_ID_NX3031),
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400108 {0,}
109};
110
111MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
112
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700113static uint32_t crb_cmd_producer[4] = {
114 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
115 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
116};
117
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700118void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700119netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000120 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700121{
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000122 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000123
124 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
125 netif_stop_queue(adapter->netdev);
126 smp_mb();
127 }
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,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000137 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700138{
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000139 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700140}
141
Dhananjay Phadke29566402008-07-21 19:44:04 -0700142static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700143 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700144 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
145 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
146 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700147};
148
Dhananjay Phadke29566402008-07-21 19:44:04 -0700149static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
150
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000151static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700152{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000153 struct netxen_adapter *adapter = sds_ring->adapter;
154
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000155 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700156}
157
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000158static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700159{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000160 struct netxen_adapter *adapter = sds_ring->adapter;
161
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000162 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700163
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700164 if (!NETXEN_IS_MSI_FAMILY(adapter))
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000165 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700166}
167
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000168static int
169netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
170{
171 int size = sizeof(struct nx_host_sds_ring) * count;
172
173 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
174
175 return (recv_ctx->sds_rings == NULL);
176}
177
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000178static void
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000179netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
180{
181 if (recv_ctx->sds_rings != NULL)
182 kfree(recv_ctx->sds_rings);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000183
184 recv_ctx->sds_rings = NULL;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000185}
186
187static int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000188netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
189{
190 int ring;
191 struct nx_host_sds_ring *sds_ring;
192 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
193
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000194 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
Dhananjay Phadkef17443f2009-07-26 20:07:43 +0000195 return -ENOMEM;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000196
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000197 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
198 sds_ring = &recv_ctx->sds_rings[ring];
199 netif_napi_add(netdev, &sds_ring->napi,
200 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
201 }
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000202
203 return 0;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000204}
205
206static void
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000207netxen_napi_del(struct netxen_adapter *adapter)
208{
209 int ring;
210 struct nx_host_sds_ring *sds_ring;
211 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
212
213 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
214 sds_ring = &recv_ctx->sds_rings[ring];
215 netif_napi_del(&sds_ring->napi);
216 }
217
218 netxen_free_sds_rings(&adapter->recv_ctx);
219}
220
221static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000222netxen_napi_enable(struct netxen_adapter *adapter)
223{
224 int ring;
225 struct nx_host_sds_ring *sds_ring;
226 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
227
228 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
229 sds_ring = &recv_ctx->sds_rings[ring];
230 napi_enable(&sds_ring->napi);
231 netxen_nic_enable_int(sds_ring);
232 }
233}
234
235static void
236netxen_napi_disable(struct netxen_adapter *adapter)
237{
238 int ring;
239 struct nx_host_sds_ring *sds_ring;
240 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
241
242 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
243 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000244 netxen_nic_disable_int(sds_ring);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000245 napi_synchronize(&sds_ring->napi);
246 napi_disable(&sds_ring->napi);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000247 }
248}
249
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000250static int nx_set_dma_mask(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700251{
252 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000253 uint64_t mask, cmask;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700254
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000255 adapter->pci_using_dac = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700256
Yang Hongyang6afd1422009-04-06 19:01:15 -0700257 mask = DMA_BIT_MASK(32);
Yang Hongyang6afd1422009-04-06 19:01:15 -0700258 cmask = DMA_BIT_MASK(32);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000259
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000260 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000261#ifndef CONFIG_IA64
Yang Hongyang6afd1422009-04-06 19:01:15 -0700262 mask = DMA_BIT_MASK(35);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000263#endif
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000264 } else {
265 mask = DMA_BIT_MASK(39);
266 cmask = mask;
267 }
268
Dhananjay Phadke29566402008-07-21 19:44:04 -0700269 if (pci_set_dma_mask(pdev, mask) == 0 &&
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000270 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700271 adapter->pci_using_dac = 1;
272 return 0;
273 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700274
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000275 return -EIO;
276}
277
278/* Update addressable range if firmware supports it */
279static int
280nx_update_dma_mask(struct netxen_adapter *adapter)
281{
282 int change, shift, err;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000283 uint64_t mask, old_mask, old_cmask;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000284 struct pci_dev *pdev = adapter->pdev;
285
286 change = 0;
287
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000288 shift = NXRD32(adapter, CRB_DMA_SHIFT);
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000289 if (shift > 32)
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000290 return 0;
291
292 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
293 change = 1;
294 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
295 change = 1;
296
297 if (change) {
298 old_mask = pdev->dma_mask;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000299 old_cmask = pdev->dev.coherent_dma_mask;
300
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000301 mask = DMA_BIT_MASK(32+shift);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000302
303 err = pci_set_dma_mask(pdev, mask);
304 if (err)
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000305 goto err_out;
306
307 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
308
309 err = pci_set_consistent_dma_mask(pdev, mask);
310 if (err)
311 goto err_out;
312 }
313 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700314 }
315
Dhananjay Phadke29566402008-07-21 19:44:04 -0700316 return 0;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000317
318err_out:
319 pci_set_dma_mask(pdev, old_mask);
320 pci_set_consistent_dma_mask(pdev, old_cmask);
321 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700322}
323
Dhananjay Phadke29566402008-07-21 19:44:04 -0700324static int
325netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
326{
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800327 u32 val, timeout;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700328
329 if (first_boot == 0x55555555) {
330 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000331 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800332
333 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
334 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700335
336 /* PCI bus master workaround */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000337 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700338 if (!(first_boot & 0x4)) {
339 first_boot |= 0x4;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000340 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
341 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700342 }
343
344 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000345 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700346 if (first_boot != 0x80000f) {
347 /* clear the register for future unloads/loads */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000348 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800349 return -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700350 }
351
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800352 /* Start P2 boot loader */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000353 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
354 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800355 timeout = 0;
356 do {
357 msleep(1);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000358 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800359
360 if (++timeout > 5000)
361 return -EIO;
362
363 } while (val == NETXEN_BDINFO_MAGIC);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700364 }
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800365 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700366}
367
368static void netxen_set_port_mode(struct netxen_adapter *adapter)
369{
370 u32 val, data;
371
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000372 val = adapter->ahw.board_type;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700373 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
374 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
375 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
376 data = NETXEN_PORT_MODE_802_3_AP;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000377 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700378 } else if (port_mode == NETXEN_PORT_MODE_XG) {
379 data = NETXEN_PORT_MODE_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000380 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700381 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
382 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000383 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700384 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
385 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000386 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700387 } else {
388 data = NETXEN_PORT_MODE_AUTO_NEG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000389 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700390 }
391
392 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
393 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
394 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
395 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
396 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
397 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000398 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700399 }
400}
401
Dhananjay Phadke29566402008-07-21 19:44:04 -0700402static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
403{
404 u32 control;
405 int pos;
406
407 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
408 if (pos) {
409 pci_read_config_dword(pdev, pos, &control);
410 if (enable)
411 control |= PCI_MSIX_FLAGS_ENABLE;
412 else
413 control = 0;
414 pci_write_config_dword(pdev, pos, control);
415 }
416}
417
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000418static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700419{
420 int i;
421
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000422 for (i = 0; i < count; i++)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700423 adapter->msix_entries[i].entry = i;
424}
425
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700426static int
427netxen_read_mac_addr(struct netxen_adapter *adapter)
428{
429 int i;
430 unsigned char *p;
431 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700432 struct net_device *netdev = adapter->netdev;
433 struct pci_dev *pdev = adapter->pdev;
434
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700435 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
436 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
437 return -EIO;
438 } else {
439 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
440 return -EIO;
441 }
442
443 p = (unsigned char *)&mac_addr;
444 for (i = 0; i < 6; i++)
445 netdev->dev_addr[i] = *(p + 5 - i);
446
447 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
Narender Kumar5d09e532009-11-20 22:08:57 +0000448 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700449
450 /* set station address */
451
Johannes Berge1749612008-10-27 15:59:26 -0700452 if (!is_valid_ether_addr(netdev->perm_addr))
453 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700454
455 return 0;
456}
457
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000458int netxen_nic_set_mac(struct net_device *netdev, void *p)
459{
460 struct netxen_adapter *adapter = netdev_priv(netdev);
461 struct sockaddr *addr = p;
462
463 if (!is_valid_ether_addr(addr->sa_data))
464 return -EINVAL;
465
466 if (netif_running(netdev)) {
467 netif_device_detach(netdev);
468 netxen_napi_disable(adapter);
469 }
470
Narender Kumar5d09e532009-11-20 22:08:57 +0000471 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000472 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
473 adapter->macaddr_set(adapter, addr->sa_data);
474
475 if (netif_running(netdev)) {
476 netif_device_attach(netdev);
477 netxen_napi_enable(adapter);
478 }
479 return 0;
480}
481
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800482static void netxen_set_multicast_list(struct net_device *dev)
483{
484 struct netxen_adapter *adapter = netdev_priv(dev);
485
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000486 adapter->set_multi(dev);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800487}
488
489static const struct net_device_ops netxen_netdev_ops = {
490 .ndo_open = netxen_nic_open,
491 .ndo_stop = netxen_nic_close,
492 .ndo_start_xmit = netxen_nic_xmit_frame,
493 .ndo_get_stats = netxen_nic_get_stats,
494 .ndo_validate_addr = eth_validate_addr,
495 .ndo_set_multicast_list = netxen_set_multicast_list,
496 .ndo_set_mac_address = netxen_nic_set_mac,
497 .ndo_change_mtu = netxen_nic_change_mtu,
498 .ndo_tx_timeout = netxen_tx_timeout,
499#ifdef CONFIG_NET_POLL_CONTROLLER
500 .ndo_poll_controller = netxen_nic_poll_controller,
501#endif
502};
503
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800504static void
505netxen_setup_intr(struct netxen_adapter *adapter)
506{
507 struct netxen_legacy_intr_set *legacy_intrp;
508 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000509 int err, num_msix;
510
511 if (adapter->rss_supported) {
512 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
513 MSIX_ENTRIES_PER_ADAPTER : 2;
514 } else
515 num_msix = 1;
516
517 adapter->max_sds_rings = 1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800518
519 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800520
521 if (adapter->ahw.revision_id >= NX_P3_B0)
522 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
523 else
524 legacy_intrp = &legacy_intr[0];
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000525
526 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
527 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
528 legacy_intrp->tgt_status_reg);
529 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
530 legacy_intrp->tgt_mask_reg);
531 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
532 legacy_intrp->pci_int_reg);
533 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
534
535 if (adapter->ahw.revision_id >= NX_P3_B1)
536 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
537 ISR_INT_STATE_REG);
538 else
539 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
540 CRB_INT_VECTOR);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800541
542 netxen_set_msix_bit(pdev, 0);
543
544 if (adapter->msix_supported) {
545
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000546 netxen_init_msix_entries(adapter, num_msix);
547 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
548 if (err == 0) {
549 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
550 netxen_set_msix_bit(pdev, 1);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800551
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000552 if (adapter->rss_supported)
553 adapter->max_sds_rings = num_msix;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800554
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000555 dev_info(&pdev->dev, "using msi-x interrupts\n");
556 return;
557 }
558
559 if (err > 0)
560 pci_disable_msix(pdev);
561
562 /* fall through for msi */
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800563 }
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000564
565 if (use_msi && !pci_enable_msi(pdev)) {
566 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000567 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
568 msi_tgt_status[adapter->ahw.pci_func]);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000569 dev_info(&pdev->dev, "using msi interrupts\n");
Dhananjay Phadkef67f3402009-05-18 21:46:40 -0700570 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000571 return;
572 }
573
574 dev_info(&pdev->dev, "using legacy interrupts\n");
575 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800576}
577
578static void
579netxen_teardown_intr(struct netxen_adapter *adapter)
580{
581 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
582 pci_disable_msix(adapter->pdev);
583 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
584 pci_disable_msi(adapter->pdev);
585}
586
587static void
588netxen_cleanup_pci_map(struct netxen_adapter *adapter)
589{
590 if (adapter->ahw.db_base != NULL)
591 iounmap(adapter->ahw.db_base);
592 if (adapter->ahw.pci_base0 != NULL)
593 iounmap(adapter->ahw.pci_base0);
594 if (adapter->ahw.pci_base1 != NULL)
595 iounmap(adapter->ahw.pci_base1);
596 if (adapter->ahw.pci_base2 != NULL)
597 iounmap(adapter->ahw.pci_base2);
598}
599
600static int
601netxen_setup_pci_map(struct netxen_adapter *adapter)
602{
603 void __iomem *mem_ptr0 = NULL;
604 void __iomem *mem_ptr1 = NULL;
605 void __iomem *mem_ptr2 = NULL;
606 void __iomem *db_ptr = NULL;
607
Dhananjay Phadke71fb62f2009-10-13 00:26:04 -0700608 resource_size_t mem_base, db_base;
609 unsigned long mem_len, db_len = 0, pci_len0 = 0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800610
611 struct pci_dev *pdev = adapter->pdev;
612 int pci_func = adapter->ahw.pci_func;
613
614 int err = 0;
615
616 /*
617 * Set the CRB window to invalid. If any register in window 0 is
618 * accessed it should set the window to 0 and then reset it to 1.
619 */
Dhananjay Phadke907fa122009-10-13 05:31:43 +0000620 adapter->ahw.crb_win = -1;
Dhananjay Phadke47abe352009-10-13 05:31:42 +0000621 adapter->ahw.ocm_win = -1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800622
623 /* remap phys address */
624 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
625 mem_len = pci_resource_len(pdev, 0);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800626
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800627 /* 128 Meg of memory */
628 if (mem_len == NETXEN_PCI_128MB_SIZE) {
629 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
630 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
631 SECOND_PAGE_GROUP_SIZE);
632 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
633 THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke47abe352009-10-13 05:31:42 +0000634 pci_len0 = FIRST_PAGE_GROUP_SIZE;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800635 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
636 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
637 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
638 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800639 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800640
641 mem_ptr0 = pci_ioremap_bar(pdev, 0);
642 if (mem_ptr0 == NULL) {
643 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
644 return -EIO;
645 }
646 pci_len0 = mem_len;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800647 } else {
648 return -EIO;
649 }
650
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000651 netxen_setup_hwops(adapter);
652
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800653 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
654
655 adapter->ahw.pci_base0 = mem_ptr0;
656 adapter->ahw.pci_len0 = pci_len0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800657 adapter->ahw.pci_base1 = mem_ptr1;
658 adapter->ahw.pci_base2 = mem_ptr2;
659
Amit Kumar Salecha6abb4b82009-10-16 15:50:09 +0000660 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
661 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
662 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
663
664 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadke47abe352009-10-13 05:31:42 +0000665 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
666 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
667 }
668
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800669 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
670 goto skip_doorbell;
671
672 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
673 db_len = pci_resource_len(pdev, 4);
674
675 if (db_len == 0) {
676 printk(KERN_ERR "%s: doorbell is disabled\n",
677 netxen_nic_driver_name);
678 err = -EIO;
679 goto err_out;
680 }
681
682 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
683 if (!db_ptr) {
684 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
685 netxen_nic_driver_name);
686 err = -EIO;
687 goto err_out;
688 }
689
690skip_doorbell:
691 adapter->ahw.db_base = db_ptr;
692 adapter->ahw.db_len = db_len;
693 return 0;
694
695err_out:
696 netxen_cleanup_pci_map(adapter);
697 return err;
698}
699
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000700static void
701netxen_check_options(struct netxen_adapter *adapter)
702{
703 u32 fw_major, fw_minor, fw_build;
704 char brd_name[NETXEN_MAX_SHORT_NAME];
705 char serial_num[32];
706 int i, offset, val;
707 int *ptr32;
708 struct pci_dev *pdev = adapter->pdev;
709
710 adapter->driver_mismatch = 0;
711
712 ptr32 = (int *)&serial_num;
713 offset = NX_FW_SERIAL_NUM_OFFSET;
714 for (i = 0; i < 8; i++) {
715 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
716 dev_err(&pdev->dev, "error reading board info\n");
717 adapter->driver_mismatch = 1;
718 return;
719 }
720 ptr32[i] = cpu_to_le32(val);
721 offset += sizeof(u32);
722 }
723
724 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
725 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
726 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
727
728 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
729
730 if (adapter->portnum == 0) {
731 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
732
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -0700733 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
734 module_name(THIS_MODULE),
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000735 brd_name, serial_num, adapter->ahw.revision_id);
736 }
737
738 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
739 adapter->driver_mismatch = 1;
740 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
741 fw_major, fw_minor, fw_build);
742 return;
743 }
744
745 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
746 i = NXRD32(adapter, NETXEN_SRE_MISC);
747 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
748 }
749
750 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
751 fw_major, fw_minor, fw_build,
752 adapter->ahw.cut_through ? "cut-through" : "legacy");
753
754 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
755 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
756
757 adapter->flags &= ~NETXEN_NIC_LRO_ENABLED;
758
759 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
760 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
761 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
762 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
763 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
764 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
765 }
766
767 adapter->msix_supported = 0;
768 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
769 adapter->msix_supported = !!use_msi_x;
770 adapter->rss_supported = !!use_msi_x;
771 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
772 switch (adapter->ahw.board_type) {
773 case NETXEN_BRDTYPE_P2_SB31_10G:
774 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
775 adapter->msix_supported = !!use_msi_x;
776 adapter->rss_supported = !!use_msi_x;
777 break;
778 default:
779 break;
780 }
781 }
782
783 adapter->num_txd = MAX_CMD_DESCRIPTORS;
784
785 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
786 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
787 adapter->max_rds_rings = 3;
788 } else {
789 adapter->num_lro_rxd = 0;
790 adapter->max_rds_rings = 2;
791 }
792}
793
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800794static int
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000795netxen_start_firmware(struct netxen_adapter *adapter)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800796{
797 int val, err, first_boot;
798 struct pci_dev *pdev = adapter->pdev;
799
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000800 /* required for NX2031 dummy dma */
801 err = nx_set_dma_mask(adapter);
802 if (err)
803 return err;
804
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000805 if (!netxen_can_start_firmware(adapter))
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000806 goto wait_init;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000807
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000808 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800809
810 err = netxen_check_hw_init(adapter, first_boot);
811 if (err) {
812 dev_err(&pdev->dev, "error in init HW init sequence\n");
813 return err;
814 }
815
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000816 netxen_request_firmware(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000817
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000818 err = netxen_need_fw_reset(adapter);
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000819 if (err < 0)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000820 goto err_out;
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000821 if (err == 0)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +0000822 goto wait_init;
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000823
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800824 if (first_boot != 0x55555555) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000825 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000826 netxen_pinit_from_rom(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800827 msleep(1);
828 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000829
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000830 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000831 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
832 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
833
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000834 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
835 netxen_set_port_mode(adapter);
836
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000837 err = netxen_load_firmware(adapter);
838 if (err)
839 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800840
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000841 netxen_release_firmware(adapter);
842
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800843 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
844
845 /* Initialize multicast addr pool owners */
846 val = 0x7654;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000847 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800848 val |= 0x0f000000;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000849 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800850
851 }
852
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000853 err = netxen_init_dummy_dma(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800854 if (err)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000855 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800856
857 /*
858 * Tell the hardware our version number.
859 */
860 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
861 | ((_NETXEN_NIC_LINUX_MINOR << 8))
862 | (_NETXEN_NIC_LINUX_SUBVERSION);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000863 NXWR32(adapter, CRB_DRIVER_VERSION, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800864
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000865wait_init:
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800866 /* Handshake with the card before we register the devices. */
867 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
868 if (err) {
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000869 netxen_free_dummy_dma(adapter);
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000870 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800871 }
872
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +0000873 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
874
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000875 nx_update_dma_mask(adapter);
876
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000877 netxen_check_options(adapter);
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000878
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +0000879 adapter->need_fw_reset = 0;
880
Dhananjay Phadkeec5c50c2009-09-11 11:28:13 +0000881 /* fall through and release firmware */
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000882
883err_out:
884 netxen_release_firmware(adapter);
885 return err;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800886}
887
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000888static int
889netxen_nic_request_irq(struct netxen_adapter *adapter)
890{
891 irq_handler_t handler;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000892 struct nx_host_sds_ring *sds_ring;
893 int err, ring;
894
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000895 unsigned long flags = IRQF_SAMPLE_RANDOM;
896 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000897 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000898
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000899 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
900 handler = netxen_msix_intr;
901 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
902 handler = netxen_msi_intr;
903 else {
904 flags |= IRQF_SHARED;
905 handler = netxen_intr;
906 }
907 adapter->irq = netdev->irq;
908
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000909 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
910 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000911 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000912 err = request_irq(sds_ring->irq, handler,
913 flags, sds_ring->name, sds_ring);
914 if (err)
915 return err;
916 }
917
918 return 0;
919}
920
921static void
922netxen_nic_free_irq(struct netxen_adapter *adapter)
923{
924 int ring;
925 struct nx_host_sds_ring *sds_ring;
926
927 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
928
929 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
930 sds_ring = &recv_ctx->sds_rings[ring];
931 free_irq(sds_ring->irq, sds_ring);
932 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000933}
934
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000935static void
936netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
937{
938 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
939 adapter->coal.normal.data.rx_time_us =
940 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
941 adapter->coal.normal.data.rx_packets =
942 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
943 adapter->coal.normal.data.tx_time_us =
944 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
945 adapter->coal.normal.data.tx_packets =
946 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
947}
948
Amit Kumar Salecha6f771412009-12-05 12:23:54 +0000949/* with rtnl_lock */
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000950static int
Amit Kumar Salecha6f771412009-12-05 12:23:54 +0000951__netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000952{
953 int err;
954
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000955 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
956 return -EIO;
957
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000958 err = adapter->init_port(adapter, adapter->physical_port);
959 if (err) {
960 printk(KERN_ERR "%s: Failed to initialize port %d\n",
961 netxen_nic_driver_name, adapter->portnum);
962 return err;
963 }
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000964 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
Narender Kumar5d09e532009-11-20 22:08:57 +0000965 adapter->macaddr_set(adapter, adapter->mac_addr);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000966
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000967 adapter->set_multi(netdev);
968 adapter->set_mtu(adapter, netdev->mtu);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000969
970 adapter->ahw.linkup = 0;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000971
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000972 if (adapter->max_sds_rings > 1)
973 netxen_config_rss(adapter, 1);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000974
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000975 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
976 netxen_config_intr_coalesce(adapter);
977
Narender Kumar1bb482f2009-08-23 08:35:09 +0000978 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
979 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
980
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000981 netxen_napi_enable(adapter);
982
Dhananjay Phadke5103c9f2009-04-28 15:29:09 +0000983 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000984 netxen_linkevent_request(adapter, 1);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000985 else
986 netxen_nic_set_link_parameters(adapter);
987
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000988 set_bit(__NX_DEV_UP, &adapter->state);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000989 return 0;
990}
991
Amit Kumar Salecha6f771412009-12-05 12:23:54 +0000992/* Usage: During resume and firmware recovery module.*/
993
994static inline int
995netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
996{
997 int err = 0;
998
999 rtnl_lock();
1000 if (netif_running(netdev))
1001 err = __netxen_nic_up(adapter, netdev);
1002 rtnl_unlock();
1003
1004 return err;
1005}
1006
1007/* with rtnl_lock */
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001008static void
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001009__netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001010{
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +00001011 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1012 return;
1013
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001014 clear_bit(__NX_DEV_UP, &adapter->state);
1015
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001016 spin_lock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001017 netif_carrier_off(netdev);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001018 netif_tx_disable(netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001019
1020 if (adapter->stop_port)
1021 adapter->stop_port(adapter);
1022
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001023 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1024 netxen_p3_free_mac_list(adapter);
1025
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001026 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1027
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +00001028 netxen_napi_disable(adapter);
1029
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001030 netxen_release_tx_buffers(adapter);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001031 spin_unlock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001032}
1033
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001034/* Usage: During suspend and firmware recovery module */
1035
1036static inline void
1037netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1038{
1039 rtnl_lock();
1040 if (netif_running(netdev))
1041 __netxen_nic_down(adapter, netdev);
1042 rtnl_unlock();
1043
1044}
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001045
1046static int
1047netxen_nic_attach(struct netxen_adapter *adapter)
1048{
1049 struct net_device *netdev = adapter->netdev;
1050 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001051 int err, ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001052 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001053 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001054
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001055 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1056 return 0;
1057
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001058 err = netxen_init_firmware(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +00001059 if (err)
1060 return err;
1061
1062 err = netxen_napi_add(adapter, netdev);
1063 if (err)
1064 return err;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001065
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001066 err = netxen_alloc_sw_resources(adapter);
1067 if (err) {
1068 printk(KERN_ERR "%s: Error in setting sw resources\n",
1069 netdev->name);
1070 return err;
1071 }
1072
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001073 err = netxen_alloc_hw_resources(adapter);
1074 if (err) {
1075 printk(KERN_ERR "%s: Error in setting hw resources\n",
1076 netdev->name);
1077 goto err_out_free_sw;
1078 }
1079
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001080 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001081 tx_ring = adapter->tx_ring;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001082 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1083 crb_cmd_producer[adapter->portnum]);
1084 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1085 crb_cmd_consumer[adapter->portnum]);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001086
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001087 tx_ring->producer = 0;
1088 tx_ring->sw_consumer = 0;
1089
1090 netxen_nic_update_cmd_producer(adapter, tx_ring);
1091 netxen_nic_update_cmd_consumer(adapter, tx_ring);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001092 }
1093
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001094 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1095 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1096 netxen_post_rx_buffers(adapter, ring, rds_ring);
1097 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001098
1099 err = netxen_nic_request_irq(adapter);
1100 if (err) {
1101 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1102 netdev->name);
1103 goto err_out_free_rxbuf;
1104 }
1105
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +00001106 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1107 netxen_nic_init_coalesce_defaults(adapter);
1108
Narender Kumarfa3ce352009-08-24 19:23:28 +00001109 netxen_create_sysfs_entries(adapter);
1110
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001111 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1112 return 0;
1113
1114err_out_free_rxbuf:
1115 netxen_release_rx_buffers(adapter);
1116 netxen_free_hw_resources(adapter);
1117err_out_free_sw:
1118 netxen_free_sw_resources(adapter);
1119 return err;
1120}
1121
1122static void
1123netxen_nic_detach(struct netxen_adapter *adapter)
1124{
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001125 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1126 return;
1127
Narender Kumarfa3ce352009-08-24 19:23:28 +00001128 netxen_remove_sysfs_entries(adapter);
1129
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001130 netxen_free_hw_resources(adapter);
Dhananjay Phadkecf981ff2009-07-17 15:27:06 +00001131 netxen_release_rx_buffers(adapter);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +00001132 netxen_nic_free_irq(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +00001133 netxen_napi_del(adapter);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001134 netxen_free_sw_resources(adapter);
1135
1136 adapter->is_up = 0;
1137}
1138
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001139int
1140netxen_nic_reset_context(struct netxen_adapter *adapter)
1141{
1142 int err = 0;
1143 struct net_device *netdev = adapter->netdev;
1144
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001145 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1146 return -EBUSY;
1147
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001148 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1149
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001150 netif_device_detach(netdev);
1151
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001152 if (netif_running(netdev))
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001153 __netxen_nic_down(adapter, netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001154
1155 netxen_nic_detach(adapter);
1156
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001157 if (netif_running(netdev)) {
1158 err = netxen_nic_attach(adapter);
1159 if (!err)
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001160 err = __netxen_nic_up(adapter, netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001161
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001162 if (err)
1163 goto done;
1164 }
1165
1166 netif_device_attach(netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001167 }
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001168
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001169done:
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001170 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001171 return err;
1172}
1173
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001174static int
1175netxen_setup_netdev(struct netxen_adapter *adapter,
1176 struct net_device *netdev)
1177{
1178 int err = 0;
1179 struct pci_dev *pdev = adapter->pdev;
1180
1181 adapter->rx_csum = 1;
1182 adapter->mc_enabled = 0;
1183 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1184 adapter->max_mc_count = 38;
1185 else
1186 adapter->max_mc_count = 16;
1187
1188 netdev->netdev_ops = &netxen_netdev_ops;
1189 netdev->watchdog_timeo = 2*HZ;
1190
1191 netxen_nic_change_mtu(netdev, netdev->mtu);
1192
1193 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1194
1195 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1196 netdev->features |= (NETIF_F_GRO);
1197 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1198
1199 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1200 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1201 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1202 }
1203
1204 if (adapter->pci_using_dac) {
1205 netdev->features |= NETIF_F_HIGHDMA;
1206 netdev->vlan_features |= NETIF_F_HIGHDMA;
1207 }
1208
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001209 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1210 netdev->features |= (NETIF_F_HW_VLAN_TX);
1211
Narender Kumar1bb482f2009-08-23 08:35:09 +00001212 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1213 netdev->features |= NETIF_F_LRO;
1214
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001215 netdev->irq = adapter->msix_entries[0].vector;
1216
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001217 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001218
1219 if (netxen_read_mac_addr(adapter))
1220 dev_warn(&pdev->dev, "failed to read mac addr\n");
1221
1222 netif_carrier_off(netdev);
1223 netif_stop_queue(netdev);
1224
1225 err = register_netdev(netdev);
1226 if (err) {
1227 dev_err(&pdev->dev, "failed to register net device\n");
1228 return err;
1229 }
1230
1231 return 0;
1232}
1233
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001234static int __devinit
1235netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1236{
1237 struct net_device *netdev = NULL;
1238 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001239 int i = 0, err;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001240 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001241 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001242
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001243 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -07001244 pr_warning("%s: chip revisions between 0x%x-0x%x"
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001245 "will not be enabled.\n",
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -07001246 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001247 return -ENODEV;
1248 }
1249
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001250 if ((err = pci_enable_device(pdev)))
1251 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001252
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001253 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1254 err = -ENODEV;
1255 goto err_out_disable_pdev;
1256 }
1257
1258 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1259 goto err_out_disable_pdev;
1260
1261 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001262
1263 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1264 if(!netdev) {
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001265 dev_err(&pdev->dev, "failed to allocate net_device\n");
1266 err = -ENOMEM;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001267 goto err_out_free_res;
1268 }
1269
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001270 SET_NETDEV_DEV(netdev, &pdev->dev);
1271
Wang Chen4cf16532008-11-12 23:38:14 -08001272 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001273 adapter->netdev = netdev;
1274 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001275 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001276
1277 revision_id = pdev->revision;
1278 adapter->ahw.revision_id = revision_id;
1279
Dhananjay Phadkef03b0eb2009-10-13 05:31:44 +00001280 rwlock_init(&adapter->ahw.crb_lock);
1281 spin_lock_init(&adapter->ahw.mem_lock);
1282
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001283 spin_lock_init(&adapter->tx_clean_lock);
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001284 INIT_LIST_HEAD(&adapter->mac_list);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001285
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001286 err = netxen_setup_pci_map(adapter);
1287 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001288 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001289
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301290 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001291 adapter->portnum = pci_func_id;
1292
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001293 err = netxen_nic_get_board_info(adapter);
1294 if (err) {
1295 dev_err(&pdev->dev, "Error getting board config info.\n");
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001296 goto err_out_iounmap;
1297 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001298
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001299 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001300 switch (adapter->ahw.board_type) {
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001301 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1302 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1303 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001304 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001305 break;
1306 default:
1307 break;
1308 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001309
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001310 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001311 if (err)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00001312 goto err_out_decr_ref;
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001313
Dhananjay Phadke29566402008-07-21 19:44:04 -07001314 /*
1315 * See if the firmware gave us a virtual-physical port mapping.
1316 */
1317 adapter->physical_port = adapter->portnum;
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001318 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001319 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001320 if (i != 0x55555555)
1321 adapter->physical_port = i;
1322 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001323
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001324 netxen_nic_clear_stats(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001325
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001326 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001327
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001328 err = netxen_setup_netdev(adapter, netdev);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001329 if (err)
Dhananjay Phadke29566402008-07-21 19:44:04 -07001330 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001331
1332 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001333
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001334 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1335
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001336 switch (adapter->ahw.port_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001337 case NETXEN_NIC_GBE:
1338 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1339 adapter->netdev->name);
1340 break;
1341 case NETXEN_NIC_XGBE:
1342 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1343 adapter->netdev->name);
1344 break;
1345 }
1346
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00001347 netxen_create_diag_entries(adapter);
1348
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001349 return 0;
1350
Dhananjay Phadke29566402008-07-21 19:44:04 -07001351err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001352 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001353
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001354 netxen_free_dummy_dma(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001355
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00001356err_out_decr_ref:
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001357 nx_decr_dev_ref_cnt(adapter);
1358
Dhananjay Phadke29566402008-07-21 19:44:04 -07001359err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001360 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001361
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001362err_out_free_netdev:
1363 free_netdev(netdev);
1364
1365err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001366 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001367
1368err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001369 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001370 pci_disable_device(pdev);
1371 return err;
1372}
1373
1374static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1375{
1376 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001377 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001378
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001379 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001380 if (adapter == NULL)
1381 return;
1382
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001383 netdev = adapter->netdev;
1384
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001385 netxen_cancel_fw_work(adapter);
1386
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301387 unregister_netdev(netdev);
1388
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001389 cancel_work_sync(&adapter->tx_timeout_task);
1390
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001391 netxen_nic_detach(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001392
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001393 nx_decr_dev_ref_cnt(adapter);
1394
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001395 if (adapter->portnum == 0)
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001396 netxen_free_dummy_dma(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301397
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001398 clear_bit(__NX_RESETTING, &adapter->state);
1399
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001400 netxen_teardown_intr(adapter);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301401
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00001402 netxen_remove_diag_entries(adapter);
1403
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001404 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001405
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001406 netxen_release_firmware(adapter);
1407
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001408 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301409 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001410 pci_set_drvdata(pdev, NULL);
1411
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001412 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001413}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001414static int __netxen_nic_shutdown(struct pci_dev *pdev)
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001415{
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001416 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1417 struct net_device *netdev = adapter->netdev;
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001418 int retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001419
1420 netif_device_detach(netdev);
1421
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001422 netxen_cancel_fw_work(adapter);
1423
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001424 if (netif_running(netdev))
1425 netxen_nic_down(adapter, netdev);
1426
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001427 cancel_work_sync(&adapter->tx_timeout_task);
1428
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001429 netxen_nic_detach(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001430
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001431 if (adapter->portnum == 0)
1432 netxen_free_dummy_dma(adapter);
1433
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001434 nx_decr_dev_ref_cnt(adapter);
1435
1436 clear_bit(__NX_RESETTING, &adapter->state);
1437
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001438 retval = pci_save_state(pdev);
1439 if (retval)
1440 return retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001441
1442 if (netxen_nic_wol_supported(adapter)) {
1443 pci_enable_wake(pdev, PCI_D3cold, 1);
1444 pci_enable_wake(pdev, PCI_D3hot, 1);
1445 }
1446
1447 pci_disable_device(pdev);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001448
1449 return 0;
1450}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001451static void netxen_nic_shutdown(struct pci_dev *pdev)
1452{
1453 if (__netxen_nic_shutdown(pdev))
1454 return;
1455}
1456#ifdef CONFIG_PM
1457static int
1458netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1459{
1460 int retval;
1461
1462 retval = __netxen_nic_shutdown(pdev);
1463 if (retval)
1464 return retval;
1465
1466 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1467 return 0;
1468}
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001469
1470static int
1471netxen_nic_resume(struct pci_dev *pdev)
1472{
1473 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1474 struct net_device *netdev = adapter->netdev;
1475 int err;
1476
1477 pci_set_power_state(pdev, PCI_D0);
1478 pci_restore_state(pdev);
1479
1480 err = pci_enable_device(pdev);
1481 if (err)
1482 return err;
1483
Dhananjay Phadke907fa122009-10-13 05:31:43 +00001484 adapter->ahw.crb_win = -1;
Dhananjay Phadke47abe352009-10-13 05:31:42 +00001485 adapter->ahw.ocm_win = -1;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001486
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001487 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001488 if (err) {
1489 dev_err(&pdev->dev, "failed to start firmware\n");
1490 return err;
1491 }
1492
1493 if (netif_running(netdev)) {
1494 err = netxen_nic_attach(adapter);
1495 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001496 goto err_out;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001497
1498 err = netxen_nic_up(adapter, netdev);
1499 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001500 goto err_out_detach;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001501
1502 netif_device_attach(netdev);
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00001503
1504 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001505 }
1506
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001507 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
Dhananjay Phadke07653aa2009-09-20 19:20:39 +00001508 return 0;
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001509
1510err_out_detach:
1511 netxen_nic_detach(adapter);
1512err_out:
1513 nx_decr_dev_ref_cnt(adapter);
1514 return err;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001515}
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001516#endif
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001517
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001518static int netxen_nic_open(struct net_device *netdev)
1519{
Wang Chen4cf16532008-11-12 23:38:14 -08001520 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001521 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001522
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001523 if (adapter->driver_mismatch)
1524 return -EIO;
1525
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001526 err = netxen_nic_attach(adapter);
1527 if (err)
1528 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001529
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001530 err = __netxen_nic_up(adapter, netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001531 if (err)
1532 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001533
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001534 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001535
1536 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001537
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001538err_out:
1539 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001540 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001541}
1542
1543/*
1544 * netxen_nic_close - Disables a network interface entry point
1545 */
1546static int netxen_nic_close(struct net_device *netdev)
1547{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001548 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001549
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001550 __netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001551 return 0;
1552}
1553
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001554static void
1555netxen_tso_check(struct net_device *netdev,
1556 struct nx_host_tx_ring *tx_ring,
1557 struct cmd_desc_type0 *first_desc,
1558 struct sk_buff *skb)
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001559{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001560 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001561 __be16 protocol = skb->protocol;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001562 u16 flags = 0, vid = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001563 u32 producer;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001564 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001565 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001566 struct vlan_ethhdr *vh;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001567
Harvey Harrison09640e62009-02-01 00:45:17 -08001568 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001569
1570 vh = (struct vlan_ethhdr *)skb->data;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001571 protocol = vh->h_vlan_encapsulated_proto;
1572 flags = FLAGS_VLAN_TAGGED;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001573
1574 } else if (vlan_tx_tag_present(skb)) {
1575
1576 flags = FLAGS_VLAN_OOB;
1577 vid = vlan_tx_tag_get(skb);
1578 netxen_set_tx_vlan_tci(first_desc, vid);
1579 vlan_oob = 1;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001580 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001581
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001582 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1583 skb_shinfo(skb)->gso_size > 0) {
1584
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001585 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1586
1587 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1588 first_desc->total_hdr_length = hdr_len;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001589 if (vlan_oob) {
1590 first_desc->total_hdr_length += VLAN_HLEN;
1591 first_desc->tcp_hdr_offset = VLAN_HLEN;
1592 first_desc->ip_hdr_offset = VLAN_HLEN;
1593 /* Only in case of TSO on vlan device */
1594 flags |= FLAGS_VLAN_TAGGED;
1595 }
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001596
Harvey Harrison09640e62009-02-01 00:45:17 -08001597 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001598 TX_TCP_LSO6 : TX_TCP_LSO;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001599 tso = 1;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001600
1601 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001602 u8 l4proto;
1603
Harvey Harrison09640e62009-02-01 00:45:17 -08001604 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001605 l4proto = ip_hdr(skb)->protocol;
1606
1607 if (l4proto == IPPROTO_TCP)
1608 opcode = TX_TCP_PKT;
1609 else if(l4proto == IPPROTO_UDP)
1610 opcode = TX_UDP_PKT;
Harvey Harrison09640e62009-02-01 00:45:17 -08001611 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001612 l4proto = ipv6_hdr(skb)->nexthdr;
1613
1614 if (l4proto == IPPROTO_TCP)
1615 opcode = TX_TCPV6_PKT;
1616 else if(l4proto == IPPROTO_UDP)
1617 opcode = TX_UDPV6_PKT;
1618 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001619 }
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001620
1621 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1622 first_desc->ip_hdr_offset += skb_network_offset(skb);
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001623 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1624
1625 if (!tso)
1626 return;
1627
1628 /* For LSO, we need to copy the MAC/IP/TCP headers into
1629 * the descriptor ring
1630 */
1631 producer = tx_ring->producer;
1632 copied = 0;
1633 offset = 2;
1634
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001635 if (vlan_oob) {
1636 /* Create a TSO vlan header template for firmware */
1637
1638 hwdesc = &tx_ring->desc_head[producer];
1639 tx_ring->cmd_buf_arr[producer].skb = NULL;
1640
1641 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1642 hdr_len + VLAN_HLEN);
1643
1644 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1645 skb_copy_from_linear_data(skb, vh, 12);
1646 vh->h_vlan_proto = htons(ETH_P_8021Q);
1647 vh->h_vlan_TCI = htons(vid);
1648 skb_copy_from_linear_data_offset(skb, 12,
1649 (char *)vh + 16, copy_len - 16);
1650
Dhananjay Phadke545eb372009-08-05 07:34:07 +00001651 copied = copy_len - VLAN_HLEN;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001652 offset = 0;
1653
1654 producer = get_next_index(producer, tx_ring->num_desc);
1655 }
1656
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001657 while (copied < hdr_len) {
1658
1659 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1660 (hdr_len - copied));
1661
1662 hwdesc = &tx_ring->desc_head[producer];
1663 tx_ring->cmd_buf_arr[producer].skb = NULL;
1664
1665 skb_copy_from_linear_data_offset(skb, copied,
1666 (char *)hwdesc + offset, copy_len);
1667
1668 copied += copy_len;
1669 offset = 0;
1670
1671 producer = get_next_index(producer, tx_ring->num_desc);
1672 }
1673
1674 tx_ring->producer = producer;
1675 barrier();
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001676}
1677
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001678static int
1679netxen_map_tx_skb(struct pci_dev *pdev,
1680 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001681{
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001682 struct netxen_skb_frag *nf;
1683 struct skb_frag_struct *frag;
1684 int i, nr_frags;
1685 dma_addr_t map;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001686
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001687 nr_frags = skb_shinfo(skb)->nr_frags;
1688 nf = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001689
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001690 map = pci_map_single(pdev, skb->data,
1691 skb_headlen(skb), PCI_DMA_TODEVICE);
1692 if (pci_dma_mapping_error(pdev, map))
1693 goto out_err;
1694
1695 nf->dma = map;
1696 nf->length = skb_headlen(skb);
1697
1698 for (i = 0; i < nr_frags; i++) {
1699 frag = &skb_shinfo(skb)->frags[i];
1700 nf = &pbuf->frag_array[i+1];
1701
1702 map = pci_map_page(pdev, frag->page, frag->page_offset,
1703 frag->size, PCI_DMA_TODEVICE);
1704 if (pci_dma_mapping_error(pdev, map))
1705 goto unwind;
1706
1707 nf->dma = map;
1708 nf->length = frag->size;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001709 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001710
1711 return 0;
1712
1713unwind:
Dhananjay Phadkecf503e82009-09-03 13:10:55 +00001714 while (--i >= 0) {
1715 nf = &pbuf->frag_array[i+1];
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001716 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1717 }
1718
1719 nf = &pbuf->frag_array[0];
1720 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1721
1722out_err:
1723 return -ENOMEM;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001724}
1725
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001726static inline void
1727netxen_clear_cmddesc(u64 *desc)
1728{
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001729 desc[0] = 0ULL;
1730 desc[2] = 0ULL;
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001731}
1732
Stephen Hemminger613573252009-08-31 19:50:58 +00001733static netdev_tx_t
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001734netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001735{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001736 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001737 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001738 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001739 struct netxen_skb_frag *buffrag;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001740 struct cmd_desc_type0 *hwdesc, *first_desc;
1741 struct pci_dev *pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001742 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001743
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001744 u32 producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001745 int frag_count, no_of_desc;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001746 u32 num_txd = tx_ring->num_desc;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001747
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001748 frag_count = skb_shinfo(skb)->nr_frags + 1;
1749
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001750 /* 4 fragments per cmd des */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001751 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001752
Roel Kluinfa9859e2009-10-06 19:34:39 +00001753 if (unlikely(no_of_desc + 2 > netxen_tx_avail(tx_ring))) {
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001754 netif_stop_queue(netdev);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001755 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001756 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001757
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001758 producer = tx_ring->producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001759 pbuf = &tx_ring->cmd_buf_arr[producer];
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001760
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001761 pdev = adapter->pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001762
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001763 if (netxen_map_tx_skb(pdev, skb, pbuf))
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001764 goto drop_packet;
1765
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001766 pbuf->skb = skb;
1767 pbuf->frag_count = frag_count;
1768
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001769 first_desc = hwdesc = &tx_ring->desc_head[producer];
1770 netxen_clear_cmddesc((u64 *)hwdesc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001771
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001772 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1773 netxen_set_tx_port(first_desc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001774
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001775 for (i = 0; i < frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001776
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001777 k = i % 4;
1778
1779 if ((k == 0) && (i > 0)) {
1780 /* move to next desc.*/
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001781 producer = get_next_index(producer, num_txd);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001782 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001783 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001784 tx_ring->cmd_buf_arr[producer].skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001785 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001786
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001787 buffrag = &pbuf->frag_array[i];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001788
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001789 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001790 switch (k) {
1791 case 0:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001792 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001793 break;
1794 case 1:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001795 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001796 break;
1797 case 2:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001798 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001799 break;
1800 case 3:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001801 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001802 break;
1803 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001804 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001805
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001806 tx_ring->producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001807
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001808 netxen_tso_check(netdev, tx_ring, first_desc, skb);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001809
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001810 netxen_nic_update_cmd_producer(adapter, tx_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001811
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001812 adapter->stats.txbytes += skb->len;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001813 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001814
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001815 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001816
1817drop_packet:
1818 adapter->stats.txdropped++;
1819 dev_kfree_skb_any(skb);
1820 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001821}
1822
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001823static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1824{
1825 struct net_device *netdev = adapter->netdev;
1826 uint32_t temp, temp_state, temp_val;
1827 int rv = 0;
1828
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001829 temp = NXRD32(adapter, CRB_TEMP_STATE);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001830
1831 temp_state = nx_get_temp_state(temp);
1832 temp_val = nx_get_temp_val(temp);
1833
1834 if (temp_state == NX_TEMP_PANIC) {
1835 printk(KERN_ALERT
1836 "%s: Device temperature %d degrees C exceeds"
1837 " maximum allowed. Hardware has been shut down.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001838 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001839 rv = 1;
1840 } else if (temp_state == NX_TEMP_WARN) {
1841 if (adapter->temp == NX_TEMP_NORMAL) {
1842 printk(KERN_ALERT
1843 "%s: Device temperature %d degrees C "
1844 "exceeds operating range."
1845 " Immediate action needed.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001846 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001847 }
1848 } else {
1849 if (adapter->temp == NX_TEMP_WARN) {
1850 printk(KERN_INFO
1851 "%s: Device temperature is now %d degrees C"
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001852 " in normal range.\n", netdev->name,
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001853 temp_val);
1854 }
1855 }
1856 adapter->temp = temp_state;
1857 return rv;
1858}
1859
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001860void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001861{
1862 struct net_device *netdev = adapter->netdev;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001863
1864 if (adapter->ahw.linkup && !linkup) {
1865 printk(KERN_INFO "%s: %s NIC Link is down\n",
1866 netxen_nic_driver_name, netdev->name);
1867 adapter->ahw.linkup = 0;
1868 if (netif_running(netdev)) {
1869 netif_carrier_off(netdev);
1870 netif_stop_queue(netdev);
1871 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001872 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001873 } else if (!adapter->ahw.linkup && linkup) {
1874 printk(KERN_INFO "%s: %s NIC Link is up\n",
1875 netxen_nic_driver_name, netdev->name);
1876 adapter->ahw.linkup = 1;
1877 if (netif_running(netdev)) {
1878 netif_carrier_on(netdev);
1879 netif_wake_queue(netdev);
1880 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001881 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001882 }
1883}
1884
1885static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1886{
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001887 u32 val, port, linkup;
1888
1889 port = adapter->physical_port;
1890
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001891 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001892 val = NXRD32(adapter, CRB_XG_STATE_P3);
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001893 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1894 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001895 } else {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001896 val = NXRD32(adapter, CRB_XG_STATE);
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001897 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001898 linkup = (val >> port) & 1;
1899 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001900 val = (val >> port*8) & 0xff;
1901 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001902 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001903 }
1904
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001905 netxen_advert_link_change(adapter, linkup);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001906}
1907
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001908static void netxen_tx_timeout(struct net_device *netdev)
1909{
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001910 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001911
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001912 if (test_bit(__NX_RESETTING, &adapter->state))
1913 return;
1914
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001915 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001916 schedule_work(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001917}
1918
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001919static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001920{
Jeff Garzik47906542007-11-23 21:23:36 -05001921 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001922 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001923
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001924 if (!netif_running(adapter->netdev))
1925 return;
1926
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001927 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001928 return;
1929
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001930 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
1931 goto request_reset;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001932
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001933 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1934 /* try to scrub interrupt */
1935 netxen_napi_disable(adapter);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001936
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001937 adapter->netdev->trans_start = jiffies;
1938
1939 netxen_napi_enable(adapter);
1940
1941 netif_wake_queue(adapter->netdev);
1942
Dhananjay Phadkea598ae12009-09-20 19:20:38 +00001943 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001944
1945 } else {
Dhananjay Phadkea598ae12009-09-20 19:20:38 +00001946 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001947 if (!netxen_nic_reset_context(adapter)) {
1948 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkea598ae12009-09-20 19:20:38 +00001949 return;
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001950 }
1951
1952 /* context reset failed, fall through for fw reset */
1953 }
1954
1955request_reset:
1956 adapter->need_fw_reset = 1;
Amit Kumar Salechaff8a3062009-10-21 19:39:02 +00001957 clear_bit(__NX_RESETTING, &adapter->state);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001958}
1959
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001960struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1961{
1962 struct netxen_adapter *adapter = netdev_priv(netdev);
Ajit Khaparde5a4d6312009-10-07 02:45:34 +00001963 struct net_device_stats *stats = &netdev->stats;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001964
1965 memset(stats, 0, sizeof(*stats));
1966
Narender Kumar1bb482f2009-08-23 08:35:09 +00001967 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
Dhananjay Phadkee98e3352009-04-07 22:50:38 +00001968 stats->tx_packets = adapter->stats.xmitfinished;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001969 stats->rx_bytes = adapter->stats.rxbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001970 stats->tx_bytes = adapter->stats.txbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001971 stats->rx_dropped = adapter->stats.rxdropped;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001972 stats->tx_dropped = adapter->stats.txdropped;
1973
1974 return stats;
1975}
1976
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001977static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001978{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001979 struct nx_host_sds_ring *sds_ring = data;
1980 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001981 u32 status = 0;
1982
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001983 status = readl(adapter->isr_int_vec);
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001984
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001985 if (!(status & adapter->int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001986 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301987
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001988 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001989 /* check interrupt state machine, to be sure */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001990 status = readl(adapter->crb_int_state_reg);
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001991 if (!ISR_LEGACY_INT_TRIGGERED(status))
1992 return IRQ_NONE;
1993
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001994 } else {
1995 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001996
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001997 our_int = readl(adapter->crb_int_state_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001998
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001999 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002000 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07002001 return IRQ_NONE;
2002
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002003 /* claim interrupt */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002004 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2005
2006 /* clear interrupt */
2007 netxen_nic_disable_int(sds_ring);
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05302008 }
2009
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002010 writel(0xffffffff, adapter->tgt_status_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002011 /* read twice to ensure write is flushed */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002012 readl(adapter->isr_int_vec);
2013 readl(adapter->isr_int_vec);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002014
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002015 napi_schedule(&sds_ring->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002016
2017 return IRQ_HANDLED;
2018}
2019
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07002020static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002021{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002022 struct nx_host_sds_ring *sds_ring = data;
2023 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002024
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002025 /* clear interrupt */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002026 writel(0xffffffff, adapter->tgt_status_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002027
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002028 napi_schedule(&sds_ring->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002029 return IRQ_HANDLED;
2030}
2031
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08002032static irqreturn_t netxen_msix_intr(int irq, void *data)
2033{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002034 struct nx_host_sds_ring *sds_ring = data;
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08002035
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002036 napi_schedule(&sds_ring->napi);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08002037 return IRQ_HANDLED;
2038}
2039
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002040static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002041{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002042 struct nx_host_sds_ring *sds_ring =
2043 container_of(napi, struct nx_host_sds_ring, napi);
2044
2045 struct netxen_adapter *adapter = sds_ring->adapter;
2046
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002047 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002048 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002049
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002050 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002051
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002052 work_done = netxen_process_rcv_ring(sds_ring, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002053
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002054 if ((work_done < budget) && tx_complete) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002055 napi_complete(&sds_ring->napi);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00002056 if (netif_running(adapter->netdev))
2057 netxen_nic_enable_int(sds_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002058 }
2059
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002060 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002061}
2062
2063#ifdef CONFIG_NET_POLL_CONTROLLER
2064static void netxen_nic_poll_controller(struct net_device *netdev)
2065{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07002066 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002067 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05002068 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002069 enable_irq(adapter->irq);
2070}
2071#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002072
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002073static int
2074nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2075{
2076 int count;
2077 if (netxen_api_lock(adapter))
2078 return -EIO;
2079
2080 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2081
2082 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2083
2084 netxen_api_unlock(adapter);
2085 return count;
2086}
2087
2088static int
2089nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2090{
2091 int count;
2092 if (netxen_api_lock(adapter))
2093 return -EIO;
2094
2095 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2096 WARN_ON(count == 0);
2097
2098 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2099
2100 if (count == 0)
2101 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2102
2103 netxen_api_unlock(adapter);
2104 return count;
2105}
2106
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00002107static void
2108nx_dev_request_reset(struct netxen_adapter *adapter)
2109{
2110 u32 state;
2111
2112 if (netxen_api_lock(adapter))
2113 return;
2114
2115 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2116
2117 if (state != NX_DEV_INITALIZING)
2118 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2119
2120 netxen_api_unlock(adapter);
2121}
2122
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002123static int
2124netxen_can_start_firmware(struct netxen_adapter *adapter)
2125{
2126 int count;
2127 int can_start = 0;
2128
2129 if (netxen_api_lock(adapter))
2130 return 0;
2131
2132 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2133
2134 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2135 count = 0;
2136
2137 if (count == 0) {
2138 can_start = 1;
2139 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2140 }
2141
2142 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2143
2144 netxen_api_unlock(adapter);
2145
2146 return can_start;
2147}
2148
2149static void
2150netxen_schedule_work(struct netxen_adapter *adapter,
2151 work_func_t func, int delay)
2152{
2153 INIT_DELAYED_WORK(&adapter->fw_work, func);
2154 schedule_delayed_work(&adapter->fw_work, delay);
2155}
2156
2157static void
2158netxen_cancel_fw_work(struct netxen_adapter *adapter)
2159{
2160 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2161 msleep(10);
2162
2163 cancel_delayed_work_sync(&adapter->fw_work);
2164}
2165
2166static void
2167netxen_attach_work(struct work_struct *work)
2168{
2169 struct netxen_adapter *adapter = container_of(work,
2170 struct netxen_adapter, fw_work.work);
2171 struct net_device *netdev = adapter->netdev;
2172 int err = 0;
2173
2174 if (netif_running(netdev)) {
2175 err = netxen_nic_attach(adapter);
2176 if (err)
2177 goto done;
2178
2179 err = netxen_nic_up(adapter, netdev);
2180 if (err) {
2181 netxen_nic_detach(adapter);
2182 goto done;
2183 }
2184
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002185 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002186 }
2187
2188 netif_device_attach(netdev);
2189
2190done:
2191 adapter->fw_fail_cnt = 0;
2192 clear_bit(__NX_RESETTING, &adapter->state);
2193 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2194}
2195
2196static void
2197netxen_fwinit_work(struct work_struct *work)
2198{
2199 struct netxen_adapter *adapter = container_of(work,
2200 struct netxen_adapter, fw_work.work);
2201 int dev_state;
2202
2203 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2204
2205 switch (dev_state) {
2206 case NX_DEV_COLD:
2207 case NX_DEV_READY:
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00002208 if (!netxen_start_firmware(adapter)) {
2209 netxen_schedule_work(adapter, netxen_attach_work, 0);
2210 return;
2211 }
2212 break;
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002213
2214 case NX_DEV_INITALIZING:
2215 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2216 netxen_schedule_work(adapter,
2217 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2218 return;
2219 }
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002220
2221 case NX_DEV_FAILED:
2222 default:
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002223 nx_incr_dev_ref_cnt(adapter);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002224 break;
2225 }
2226
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002227 clear_bit(__NX_RESETTING, &adapter->state);
2228}
2229
2230static void
2231netxen_detach_work(struct work_struct *work)
2232{
2233 struct netxen_adapter *adapter = container_of(work,
2234 struct netxen_adapter, fw_work.work);
2235 struct net_device *netdev = adapter->netdev;
2236 int ref_cnt, delay;
2237 u32 status;
2238
2239 netif_device_detach(netdev);
2240
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00002241 netxen_nic_down(adapter, netdev);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002242
2243 netxen_nic_detach(adapter);
2244
2245 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2246
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002247 if (status & NX_RCODE_FATAL_ERROR)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002248 goto err_ret;
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002249
2250 if (adapter->temp == NX_TEMP_PANIC)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002251 goto err_ret;
2252
2253 ref_cnt = nx_decr_dev_ref_cnt(adapter);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002254
2255 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2256
2257 adapter->fw_wait_cnt = 0;
2258 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002259
2260 return;
2261
2262err_ret:
2263 clear_bit(__NX_RESETTING, &adapter->state);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002264}
2265
2266static int
2267netxen_check_health(struct netxen_adapter *adapter)
2268{
2269 u32 state, heartbit;
2270 struct net_device *netdev = adapter->netdev;
2271
2272 if (netxen_nic_check_temp(adapter))
2273 goto detach;
2274
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00002275 if (adapter->need_fw_reset) {
2276 nx_dev_request_reset(adapter);
2277 goto detach;
2278 }
2279
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002280 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2281 if (state == NX_DEV_NEED_RESET)
2282 goto detach;
2283
2284 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2285 return 0;
2286
2287 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2288 if (heartbit != adapter->heartbit) {
2289 adapter->heartbit = heartbit;
2290 adapter->fw_fail_cnt = 0;
2291 return 0;
2292 }
2293
2294 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2295 return 0;
2296
2297 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2298
2299 dev_info(&netdev->dev, "firmware hang detected\n");
2300
2301detach:
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002302 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2303 !test_and_set_bit(__NX_RESETTING, &adapter->state))
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002304 netxen_schedule_work(adapter, netxen_detach_work, 0);
2305 return 1;
2306}
2307
2308static void
2309netxen_fw_poll_work(struct work_struct *work)
2310{
2311 struct netxen_adapter *adapter = container_of(work,
2312 struct netxen_adapter, fw_work.work);
2313
2314 if (test_bit(__NX_RESETTING, &adapter->state))
2315 goto reschedule;
2316
2317 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2318 if (!adapter->has_link_events) {
2319
2320 netxen_nic_handle_phy_intr(adapter);
2321
2322 if (adapter->link_changed)
2323 netxen_nic_set_link_parameters(adapter);
2324 }
2325 }
2326
2327 if (netxen_check_health(adapter))
2328 return;
2329
2330reschedule:
2331 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2332}
2333
Narender Kumarfa3ce352009-08-24 19:23:28 +00002334static ssize_t
2335netxen_store_bridged_mode(struct device *dev,
2336 struct device_attribute *attr, const char *buf, size_t len)
2337{
2338 struct net_device *net = to_net_dev(dev);
2339 struct netxen_adapter *adapter = netdev_priv(net);
2340 unsigned long new;
2341 int ret = -EINVAL;
2342
2343 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2344 goto err_out;
2345
2346 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2347 goto err_out;
2348
2349 if (strict_strtoul(buf, 2, &new))
2350 goto err_out;
2351
2352 if (!netxen_config_bridged_mode(adapter, !!new))
2353 ret = len;
2354
2355err_out:
2356 return ret;
2357}
2358
2359static ssize_t
2360netxen_show_bridged_mode(struct device *dev,
2361 struct device_attribute *attr, char *buf)
2362{
2363 struct net_device *net = to_net_dev(dev);
2364 struct netxen_adapter *adapter;
2365 int bridged_mode = 0;
2366
2367 adapter = netdev_priv(net);
2368
2369 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2370 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2371
2372 return sprintf(buf, "%d\n", bridged_mode);
2373}
2374
2375static struct device_attribute dev_attr_bridged_mode = {
2376 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2377 .show = netxen_show_bridged_mode,
2378 .store = netxen_store_bridged_mode,
2379};
2380
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00002381static ssize_t
2382netxen_store_diag_mode(struct device *dev,
2383 struct device_attribute *attr, const char *buf, size_t len)
2384{
2385 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2386 unsigned long new;
2387
2388 if (strict_strtoul(buf, 2, &new))
2389 return -EINVAL;
2390
2391 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2392 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2393
2394 return len;
2395}
2396
2397static ssize_t
2398netxen_show_diag_mode(struct device *dev,
2399 struct device_attribute *attr, char *buf)
2400{
2401 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2402
2403 return sprintf(buf, "%d\n",
2404 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2405}
2406
2407static struct device_attribute dev_attr_diag_mode = {
2408 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2409 .show = netxen_show_diag_mode,
2410 .store = netxen_store_diag_mode,
2411};
2412
2413static int
2414netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2415 loff_t offset, size_t size)
2416{
2417 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2418 return -EIO;
2419
2420 if ((size != 4) || (offset & 0x3))
2421 return -EINVAL;
2422
2423 if (offset < NETXEN_PCI_CRBSPACE)
2424 return -EINVAL;
2425
2426 return 0;
2427}
2428
2429static ssize_t
2430netxen_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2431 char *buf, loff_t offset, size_t size)
2432{
2433 struct device *dev = container_of(kobj, struct device, kobj);
2434 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2435 u32 data;
2436 int ret;
2437
2438 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2439 if (ret != 0)
2440 return ret;
2441
2442 data = NXRD32(adapter, offset);
2443 memcpy(buf, &data, size);
2444 return size;
2445}
2446
2447static ssize_t
2448netxen_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2449 char *buf, loff_t offset, size_t size)
2450{
2451 struct device *dev = container_of(kobj, struct device, kobj);
2452 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2453 u32 data;
2454 int ret;
2455
2456 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2457 if (ret != 0)
2458 return ret;
2459
2460 memcpy(&data, buf, size);
2461 NXWR32(adapter, offset, data);
2462 return size;
2463}
2464
2465static int
2466netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2467 loff_t offset, size_t size)
2468{
2469 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2470 return -EIO;
2471
2472 if ((size != 8) || (offset & 0x7))
2473 return -EIO;
2474
2475 return 0;
2476}
2477
2478static ssize_t
2479netxen_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2480 char *buf, loff_t offset, size_t size)
2481{
2482 struct device *dev = container_of(kobj, struct device, kobj);
2483 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2484 u64 data;
2485 int ret;
2486
2487 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2488 if (ret != 0)
2489 return ret;
2490
2491 if (adapter->pci_mem_read(adapter, offset, &data))
2492 return -EIO;
2493
2494 memcpy(buf, &data, size);
2495
2496 return size;
2497}
2498
2499ssize_t netxen_sysfs_write_mem(struct kobject *kobj,
2500 struct bin_attribute *attr, char *buf,
2501 loff_t offset, size_t size)
2502{
2503 struct device *dev = container_of(kobj, struct device, kobj);
2504 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2505 u64 data;
2506 int ret;
2507
2508 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2509 if (ret != 0)
2510 return ret;
2511
2512 memcpy(&data, buf, size);
2513
2514 if (adapter->pci_mem_write(adapter, offset, data))
2515 return -EIO;
2516
2517 return size;
2518}
2519
2520
2521static struct bin_attribute bin_attr_crb = {
2522 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2523 .size = 0,
2524 .read = netxen_sysfs_read_crb,
2525 .write = netxen_sysfs_write_crb,
2526};
2527
2528static struct bin_attribute bin_attr_mem = {
2529 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2530 .size = 0,
2531 .read = netxen_sysfs_read_mem,
2532 .write = netxen_sysfs_write_mem,
2533};
2534
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002535#ifdef CONFIG_MODULES
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002536static ssize_t
2537netxen_store_auto_fw_reset(struct module_attribute *mattr,
2538 struct module *mod, const char *buf, size_t count)
2539
2540{
2541 unsigned long new;
2542
2543 if (strict_strtoul(buf, 16, &new))
2544 return -EINVAL;
2545
2546 if ((new == AUTO_FW_RESET_ENABLED) || (new == AUTO_FW_RESET_DISABLED)) {
2547 auto_fw_reset = new;
2548 return count;
2549 }
2550
2551 return -EINVAL;
2552}
2553
2554static ssize_t
2555netxen_show_auto_fw_reset(struct module_attribute *mattr,
2556 struct module *mod, char *buf)
2557
2558{
2559 if (auto_fw_reset == AUTO_FW_RESET_ENABLED)
2560 return sprintf(buf, "enabled\n");
2561 else
2562 return sprintf(buf, "disabled\n");
2563}
2564
2565static struct module_attribute mod_attr_fw_reset = {
2566 .attr = {.name = "auto_fw_reset", .mode = (S_IRUGO | S_IWUSR)},
2567 .show = netxen_show_auto_fw_reset,
2568 .store = netxen_store_auto_fw_reset,
2569};
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002570#endif
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002571
Narender Kumarfa3ce352009-08-24 19:23:28 +00002572static void
2573netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2574{
2575 struct net_device *netdev = adapter->netdev;
2576 struct device *dev = &netdev->dev;
2577
2578 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2579 /* bridged_mode control */
2580 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2581 dev_warn(&netdev->dev,
2582 "failed to create bridged_mode sysfs entry\n");
2583 }
2584 }
2585}
2586
2587static void
2588netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2589{
2590 struct net_device *netdev = adapter->netdev;
2591 struct device *dev = &netdev->dev;
2592
2593 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2594 device_remove_file(dev, &dev_attr_bridged_mode);
2595}
2596
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00002597static void
2598netxen_create_diag_entries(struct netxen_adapter *adapter)
2599{
2600 struct pci_dev *pdev = adapter->pdev;
2601 struct device *dev;
2602
2603 dev = &pdev->dev;
2604 if (device_create_file(dev, &dev_attr_diag_mode))
2605 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2606 if (device_create_bin_file(dev, &bin_attr_crb))
2607 dev_info(dev, "failed to create crb sysfs entry\n");
2608 if (device_create_bin_file(dev, &bin_attr_mem))
2609 dev_info(dev, "failed to create mem sysfs entry\n");
2610}
2611
2612
2613static void
2614netxen_remove_diag_entries(struct netxen_adapter *adapter)
2615{
2616 struct pci_dev *pdev = adapter->pdev;
2617 struct device *dev = &pdev->dev;
2618
2619 device_remove_file(dev, &dev_attr_diag_mode);
2620 device_remove_bin_file(dev, &bin_attr_crb);
2621 device_remove_bin_file(dev, &bin_attr_mem);
2622}
2623
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002624#ifdef CONFIG_INET
2625
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002626#define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2627
2628static int
2629netxen_destip_supported(struct netxen_adapter *adapter)
2630{
2631 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2632 return 0;
2633
2634 if (adapter->ahw.cut_through)
2635 return 0;
2636
2637 return 1;
2638}
2639
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002640static void
2641netxen_config_indev_addr(struct net_device *dev, unsigned long event)
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002642{
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002643 struct in_device *indev;
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002644 struct netxen_adapter *adapter = netdev_priv(dev);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002645
Amit Kumar Salecha3db76752009-09-09 18:12:37 -07002646 if (!netxen_destip_supported(adapter))
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002647 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002648
2649 indev = in_dev_get(dev);
2650 if (!indev)
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002651 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002652
2653 for_ifa(indev) {
2654 switch (event) {
2655 case NETDEV_UP:
2656 netxen_config_ipaddr(adapter,
2657 ifa->ifa_address, NX_IP_UP);
2658 break;
2659 case NETDEV_DOWN:
2660 netxen_config_ipaddr(adapter,
2661 ifa->ifa_address, NX_IP_DOWN);
2662 break;
2663 default:
2664 break;
2665 }
2666 } endfor_ifa(indev);
2667
2668 in_dev_put(indev);
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002669 return;
2670}
2671
2672static int netxen_netdev_event(struct notifier_block *this,
2673 unsigned long event, void *ptr)
2674{
2675 struct netxen_adapter *adapter;
2676 struct net_device *dev = (struct net_device *)ptr;
2677
2678recheck:
2679 if (dev == NULL)
2680 goto done;
2681
2682 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2683 dev = vlan_dev_real_dev(dev);
2684 goto recheck;
2685 }
2686
2687 if (!is_netxen_netdev(dev))
2688 goto done;
2689
2690 adapter = netdev_priv(dev);
2691
2692 if (!adapter)
2693 goto done;
2694
2695 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2696 goto done;
2697
2698 netxen_config_indev_addr(dev, event);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002699done:
2700 return NOTIFY_DONE;
2701}
2702
2703static int
2704netxen_inetaddr_event(struct notifier_block *this,
2705 unsigned long event, void *ptr)
2706{
2707 struct netxen_adapter *adapter;
2708 struct net_device *dev;
2709
2710 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2711
2712 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2713
2714recheck:
2715 if (dev == NULL || !netif_running(dev))
2716 goto done;
2717
2718 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2719 dev = vlan_dev_real_dev(dev);
2720 goto recheck;
2721 }
2722
2723 if (!is_netxen_netdev(dev))
2724 goto done;
2725
2726 adapter = netdev_priv(dev);
2727
2728 if (!adapter || !netxen_destip_supported(adapter))
2729 goto done;
2730
2731 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2732 goto done;
2733
2734 switch (event) {
2735 case NETDEV_UP:
2736 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2737 break;
2738 case NETDEV_DOWN:
2739 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2740 break;
2741 default:
2742 break;
2743 }
2744
2745done:
2746 return NOTIFY_DONE;
2747}
2748
2749static struct notifier_block netxen_netdev_cb = {
2750 .notifier_call = netxen_netdev_event,
2751};
2752
2753static struct notifier_block netxen_inetaddr_cb = {
2754 .notifier_call = netxen_inetaddr_event,
2755};
Randy Dunlap9feae562009-09-11 12:41:04 +00002756#else
2757static void
2758netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2759{ }
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002760#endif
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002761
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002762static struct pci_driver netxen_driver = {
2763 .name = netxen_nic_driver_name,
2764 .id_table = netxen_pci_tbl,
2765 .probe = netxen_nic_probe,
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002766 .remove = __devexit_p(netxen_nic_remove),
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002767#ifdef CONFIG_PM
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002768 .suspend = netxen_nic_suspend,
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002769 .resume = netxen_nic_resume,
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002770#endif
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002771 .shutdown = netxen_nic_shutdown
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002772};
2773
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002774static int __init netxen_init_module(void)
2775{
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002776#ifdef CONFIG_MODULES
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002777 struct module *mod = THIS_MODULE;
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002778#endif
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002779
Dhananjay Phadkeff4fbd42009-03-13 14:52:06 +00002780 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2781
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002782#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002783 register_netdevice_notifier(&netxen_netdev_cb);
2784 register_inetaddr_notifier(&netxen_inetaddr_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002785#endif
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002786
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002787#ifdef CONFIG_MODULES
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002788 if (sysfs_create_file(&mod->mkobj.kobj, &mod_attr_fw_reset.attr))
2789 printk(KERN_ERR "%s: Failed to create auto_fw_reset "
2790 "sysfs entry.", netxen_nic_driver_name);
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002791#endif
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002792
Amit S. Kale184231b2007-01-22 06:52:53 -08002793 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002794}
2795
2796module_init(netxen_init_module);
2797
2798static void __exit netxen_exit_module(void)
2799{
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002800#ifdef CONFIG_MODULES
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002801 struct module *mod = THIS_MODULE;
2802
2803 sysfs_remove_file(&mod->mkobj.kobj, &mod_attr_fw_reset.attr);
Randy Dunlap0531d7b82009-10-26 12:09:45 +00002804#endif
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002805
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002806 pci_unregister_driver(&netxen_driver);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002807
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002808#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002809 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2810 unregister_netdevice_notifier(&netxen_netdev_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002811#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002812}
2813
2814module_exit(netxen_exit_module);