blob: 41cb0af4b5566bde33174756bbc228732926f48d [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
Stephen Hemmingerafdafff2009-12-18 20:13:35 -080060static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
61module_param(auto_fw_reset, int, 0644);
62MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
Narender Kumarcaa2dd52009-10-16 15:50:11 +000063
Amit S. Kale3d396eb2006-10-21 15:33:03 -040064static int __devinit netxen_nic_probe(struct pci_dev *pdev,
Dhananjay Phadke29566402008-07-21 19:44:04 -070065 const struct pci_device_id *ent);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040066static void __devexit netxen_nic_remove(struct pci_dev *pdev);
67static int netxen_nic_open(struct net_device *netdev);
68static int netxen_nic_close(struct net_device *netdev);
Stephen Hemminger613573252009-08-31 19:50:58 +000069static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
70 struct net_device *);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040071static void netxen_tx_timeout(struct net_device *netdev);
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +000072static void netxen_tx_timeout_task(struct work_struct *work);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +000073static void netxen_fw_poll_work(struct work_struct *work);
74static void netxen_schedule_work(struct netxen_adapter *adapter,
75 work_func_t func, int delay);
76static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070077static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040078#ifdef CONFIG_NET_POLL_CONTROLLER
79static void netxen_nic_poll_controller(struct net_device *netdev);
80#endif
Narender Kumarfa3ce352009-08-24 19:23:28 +000081
82static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
83static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +000084static void netxen_create_diag_entries(struct netxen_adapter *adapter);
85static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
Narender Kumarfa3ce352009-08-24 19:23:28 +000086
Dhananjay Phadke6a581e92009-09-05 17:43:08 +000087static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
88static int netxen_can_start_firmware(struct netxen_adapter *adapter);
89
Jeff Garzik1494a812006-11-07 05:12:16 -050090static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070091static irqreturn_t netxen_msi_intr(int irq, void *data);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -080092static irqreturn_t netxen_msix_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040093
Amit Kumar Salechad0725e42009-09-05 17:43:09 +000094static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
95
Amit S. Kale3d396eb2006-10-21 15:33:03 -040096/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010097#define ENTRY(device) \
Dhananjay Phadke040dec32008-09-12 06:55:14 -070098 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
Al Viro70081ac2008-06-23 02:04:50 +010099 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
100
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400101static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Dhananjay Phadke040dec32008-09-12 06:55:14 -0700102 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
103 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
104 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
105 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
106 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
107 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
108 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
109 ENTRY(PCI_DEVICE_ID_NX3031),
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400110 {0,}
111};
112
113MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
114
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700115static uint32_t crb_cmd_producer[4] = {
116 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
117 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
118};
119
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700120void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700121netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000122 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700123{
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000124 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000125
126 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
127 netif_stop_queue(adapter->netdev);
128 smp_mb();
129 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700130}
131
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700132static uint32_t crb_cmd_consumer[4] = {
133 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
134 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
135};
136
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700137static inline void
138netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000139 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700140{
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000141 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700142}
143
Dhananjay Phadke29566402008-07-21 19:44:04 -0700144static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700145 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700146 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
147 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
148 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700149};
150
Dhananjay Phadke29566402008-07-21 19:44:04 -0700151static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
152
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000153static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700154{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000155 struct netxen_adapter *adapter = sds_ring->adapter;
156
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000157 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700158}
159
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000160static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700161{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000162 struct netxen_adapter *adapter = sds_ring->adapter;
163
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000164 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700165
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700166 if (!NETXEN_IS_MSI_FAMILY(adapter))
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000167 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700168}
169
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000170static int
171netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
172{
173 int size = sizeof(struct nx_host_sds_ring) * count;
174
175 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
176
177 return (recv_ctx->sds_rings == NULL);
178}
179
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000180static void
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000181netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
182{
183 if (recv_ctx->sds_rings != NULL)
184 kfree(recv_ctx->sds_rings);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000185
186 recv_ctx->sds_rings = NULL;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000187}
188
189static int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000190netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
191{
192 int ring;
193 struct nx_host_sds_ring *sds_ring;
194 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
195
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000196 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
Dhananjay Phadkef17443f2009-07-26 20:07:43 +0000197 return -ENOMEM;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000198
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000199 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
200 sds_ring = &recv_ctx->sds_rings[ring];
201 netif_napi_add(netdev, &sds_ring->napi,
202 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
203 }
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000204
205 return 0;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000206}
207
208static void
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000209netxen_napi_del(struct netxen_adapter *adapter)
210{
211 int ring;
212 struct nx_host_sds_ring *sds_ring;
213 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
214
215 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
216 sds_ring = &recv_ctx->sds_rings[ring];
217 netif_napi_del(&sds_ring->napi);
218 }
219
220 netxen_free_sds_rings(&adapter->recv_ctx);
221}
222
223static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000224netxen_napi_enable(struct netxen_adapter *adapter)
225{
226 int ring;
227 struct nx_host_sds_ring *sds_ring;
228 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
229
230 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
231 sds_ring = &recv_ctx->sds_rings[ring];
232 napi_enable(&sds_ring->napi);
233 netxen_nic_enable_int(sds_ring);
234 }
235}
236
237static void
238netxen_napi_disable(struct netxen_adapter *adapter)
239{
240 int ring;
241 struct nx_host_sds_ring *sds_ring;
242 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
243
244 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
245 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000246 netxen_nic_disable_int(sds_ring);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000247 napi_synchronize(&sds_ring->napi);
248 napi_disable(&sds_ring->napi);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000249 }
250}
251
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000252static int nx_set_dma_mask(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700253{
254 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000255 uint64_t mask, cmask;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700256
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000257 adapter->pci_using_dac = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700258
Yang Hongyang6afd1422009-04-06 19:01:15 -0700259 mask = DMA_BIT_MASK(32);
Yang Hongyang6afd1422009-04-06 19:01:15 -0700260 cmask = DMA_BIT_MASK(32);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000261
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000262 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000263#ifndef CONFIG_IA64
Yang Hongyang6afd1422009-04-06 19:01:15 -0700264 mask = DMA_BIT_MASK(35);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000265#endif
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000266 } else {
267 mask = DMA_BIT_MASK(39);
268 cmask = mask;
269 }
270
Dhananjay Phadke29566402008-07-21 19:44:04 -0700271 if (pci_set_dma_mask(pdev, mask) == 0 &&
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000272 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700273 adapter->pci_using_dac = 1;
274 return 0;
275 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700276
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000277 return -EIO;
278}
279
280/* Update addressable range if firmware supports it */
281static int
282nx_update_dma_mask(struct netxen_adapter *adapter)
283{
284 int change, shift, err;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000285 uint64_t mask, old_mask, old_cmask;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000286 struct pci_dev *pdev = adapter->pdev;
287
288 change = 0;
289
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000290 shift = NXRD32(adapter, CRB_DMA_SHIFT);
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000291 if (shift > 32)
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000292 return 0;
293
294 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
295 change = 1;
296 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
297 change = 1;
298
299 if (change) {
300 old_mask = pdev->dma_mask;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000301 old_cmask = pdev->dev.coherent_dma_mask;
302
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000303 mask = DMA_BIT_MASK(32+shift);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000304
305 err = pci_set_dma_mask(pdev, mask);
306 if (err)
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000307 goto err_out;
308
309 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
310
311 err = pci_set_consistent_dma_mask(pdev, mask);
312 if (err)
313 goto err_out;
314 }
315 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700316 }
317
Dhananjay Phadke29566402008-07-21 19:44:04 -0700318 return 0;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000319
320err_out:
321 pci_set_dma_mask(pdev, old_mask);
322 pci_set_consistent_dma_mask(pdev, old_cmask);
323 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700324}
325
Dhananjay Phadke29566402008-07-21 19:44:04 -0700326static int
327netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
328{
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800329 u32 val, timeout;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700330
331 if (first_boot == 0x55555555) {
332 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000333 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800334
335 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
336 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700337
338 /* PCI bus master workaround */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000339 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700340 if (!(first_boot & 0x4)) {
341 first_boot |= 0x4;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000342 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
343 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700344 }
345
346 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000347 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700348 if (first_boot != 0x80000f) {
349 /* clear the register for future unloads/loads */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000350 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800351 return -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700352 }
353
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800354 /* Start P2 boot loader */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000355 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
356 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800357 timeout = 0;
358 do {
359 msleep(1);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000360 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800361
362 if (++timeout > 5000)
363 return -EIO;
364
365 } while (val == NETXEN_BDINFO_MAGIC);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700366 }
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800367 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700368}
369
370static void netxen_set_port_mode(struct netxen_adapter *adapter)
371{
372 u32 val, data;
373
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000374 val = adapter->ahw.board_type;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700375 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
376 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
377 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
378 data = NETXEN_PORT_MODE_802_3_AP;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000379 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700380 } else if (port_mode == NETXEN_PORT_MODE_XG) {
381 data = NETXEN_PORT_MODE_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000382 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700383 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
384 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000385 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700386 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
387 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000388 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700389 } else {
390 data = NETXEN_PORT_MODE_AUTO_NEG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000391 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700392 }
393
394 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
395 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
396 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
397 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
398 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
399 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000400 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700401 }
402}
403
Dhananjay Phadke29566402008-07-21 19:44:04 -0700404static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
405{
406 u32 control;
407 int pos;
408
409 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
410 if (pos) {
411 pci_read_config_dword(pdev, pos, &control);
412 if (enable)
413 control |= PCI_MSIX_FLAGS_ENABLE;
414 else
415 control = 0;
416 pci_write_config_dword(pdev, pos, control);
417 }
418}
419
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000420static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700421{
422 int i;
423
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000424 for (i = 0; i < count; i++)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700425 adapter->msix_entries[i].entry = i;
426}
427
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700428static int
429netxen_read_mac_addr(struct netxen_adapter *adapter)
430{
431 int i;
432 unsigned char *p;
433 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700434 struct net_device *netdev = adapter->netdev;
435 struct pci_dev *pdev = adapter->pdev;
436
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700437 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
438 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
439 return -EIO;
440 } else {
441 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
442 return -EIO;
443 }
444
445 p = (unsigned char *)&mac_addr;
446 for (i = 0; i < 6; i++)
447 netdev->dev_addr[i] = *(p + 5 - i);
448
449 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
Narender Kumar5d09e532009-11-20 22:08:57 +0000450 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700451
452 /* set station address */
453
Johannes Berge1749612008-10-27 15:59:26 -0700454 if (!is_valid_ether_addr(netdev->perm_addr))
455 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700456
457 return 0;
458}
459
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000460int netxen_nic_set_mac(struct net_device *netdev, void *p)
461{
462 struct netxen_adapter *adapter = netdev_priv(netdev);
463 struct sockaddr *addr = p;
464
465 if (!is_valid_ether_addr(addr->sa_data))
466 return -EINVAL;
467
468 if (netif_running(netdev)) {
469 netif_device_detach(netdev);
470 netxen_napi_disable(adapter);
471 }
472
Narender Kumar5d09e532009-11-20 22:08:57 +0000473 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000474 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
475 adapter->macaddr_set(adapter, addr->sa_data);
476
477 if (netif_running(netdev)) {
478 netif_device_attach(netdev);
479 netxen_napi_enable(adapter);
480 }
481 return 0;
482}
483
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800484static void netxen_set_multicast_list(struct net_device *dev)
485{
486 struct netxen_adapter *adapter = netdev_priv(dev);
487
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000488 adapter->set_multi(dev);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800489}
490
491static const struct net_device_ops netxen_netdev_ops = {
492 .ndo_open = netxen_nic_open,
493 .ndo_stop = netxen_nic_close,
494 .ndo_start_xmit = netxen_nic_xmit_frame,
495 .ndo_get_stats = netxen_nic_get_stats,
496 .ndo_validate_addr = eth_validate_addr,
497 .ndo_set_multicast_list = netxen_set_multicast_list,
498 .ndo_set_mac_address = netxen_nic_set_mac,
499 .ndo_change_mtu = netxen_nic_change_mtu,
500 .ndo_tx_timeout = netxen_tx_timeout,
501#ifdef CONFIG_NET_POLL_CONTROLLER
502 .ndo_poll_controller = netxen_nic_poll_controller,
503#endif
504};
505
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800506static void
507netxen_setup_intr(struct netxen_adapter *adapter)
508{
509 struct netxen_legacy_intr_set *legacy_intrp;
510 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000511 int err, num_msix;
512
513 if (adapter->rss_supported) {
514 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
515 MSIX_ENTRIES_PER_ADAPTER : 2;
516 } else
517 num_msix = 1;
518
519 adapter->max_sds_rings = 1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800520
521 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800522
523 if (adapter->ahw.revision_id >= NX_P3_B0)
524 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
525 else
526 legacy_intrp = &legacy_intr[0];
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000527
528 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
529 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
530 legacy_intrp->tgt_status_reg);
531 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
532 legacy_intrp->tgt_mask_reg);
533 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
534 legacy_intrp->pci_int_reg);
535 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
536
537 if (adapter->ahw.revision_id >= NX_P3_B1)
538 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
539 ISR_INT_STATE_REG);
540 else
541 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
542 CRB_INT_VECTOR);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800543
544 netxen_set_msix_bit(pdev, 0);
545
546 if (adapter->msix_supported) {
547
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000548 netxen_init_msix_entries(adapter, num_msix);
549 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
550 if (err == 0) {
551 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
552 netxen_set_msix_bit(pdev, 1);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800553
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000554 if (adapter->rss_supported)
555 adapter->max_sds_rings = num_msix;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800556
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000557 dev_info(&pdev->dev, "using msi-x interrupts\n");
558 return;
559 }
560
561 if (err > 0)
562 pci_disable_msix(pdev);
563
564 /* fall through for msi */
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800565 }
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000566
567 if (use_msi && !pci_enable_msi(pdev)) {
568 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000569 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
570 msi_tgt_status[adapter->ahw.pci_func]);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000571 dev_info(&pdev->dev, "using msi interrupts\n");
Dhananjay Phadkef67f3402009-05-18 21:46:40 -0700572 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000573 return;
574 }
575
576 dev_info(&pdev->dev, "using legacy interrupts\n");
577 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800578}
579
580static void
581netxen_teardown_intr(struct netxen_adapter *adapter)
582{
583 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
584 pci_disable_msix(adapter->pdev);
585 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
586 pci_disable_msi(adapter->pdev);
587}
588
589static void
590netxen_cleanup_pci_map(struct netxen_adapter *adapter)
591{
592 if (adapter->ahw.db_base != NULL)
593 iounmap(adapter->ahw.db_base);
594 if (adapter->ahw.pci_base0 != NULL)
595 iounmap(adapter->ahw.pci_base0);
596 if (adapter->ahw.pci_base1 != NULL)
597 iounmap(adapter->ahw.pci_base1);
598 if (adapter->ahw.pci_base2 != NULL)
599 iounmap(adapter->ahw.pci_base2);
600}
601
602static int
603netxen_setup_pci_map(struct netxen_adapter *adapter)
604{
605 void __iomem *mem_ptr0 = NULL;
606 void __iomem *mem_ptr1 = NULL;
607 void __iomem *mem_ptr2 = NULL;
608 void __iomem *db_ptr = NULL;
609
Dhananjay Phadke71fb62f2009-10-13 00:26:04 -0700610 resource_size_t mem_base, db_base;
611 unsigned long mem_len, db_len = 0, pci_len0 = 0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800612
613 struct pci_dev *pdev = adapter->pdev;
614 int pci_func = adapter->ahw.pci_func;
615
616 int err = 0;
617
618 /*
619 * Set the CRB window to invalid. If any register in window 0 is
620 * accessed it should set the window to 0 and then reset it to 1.
621 */
Dhananjay Phadke907fa122009-10-13 05:31:43 +0000622 adapter->ahw.crb_win = -1;
Dhananjay Phadke47abe352009-10-13 05:31:42 +0000623 adapter->ahw.ocm_win = -1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800624
625 /* remap phys address */
626 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
627 mem_len = pci_resource_len(pdev, 0);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800628
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800629 /* 128 Meg of memory */
630 if (mem_len == NETXEN_PCI_128MB_SIZE) {
631 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
632 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
633 SECOND_PAGE_GROUP_SIZE);
634 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
635 THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke47abe352009-10-13 05:31:42 +0000636 pci_len0 = FIRST_PAGE_GROUP_SIZE;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800637 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
638 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
639 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
640 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800641 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800642
643 mem_ptr0 = pci_ioremap_bar(pdev, 0);
644 if (mem_ptr0 == NULL) {
645 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
646 return -EIO;
647 }
648 pci_len0 = mem_len;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800649 } else {
650 return -EIO;
651 }
652
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000653 netxen_setup_hwops(adapter);
654
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800655 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
656
657 adapter->ahw.pci_base0 = mem_ptr0;
658 adapter->ahw.pci_len0 = pci_len0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800659 adapter->ahw.pci_base1 = mem_ptr1;
660 adapter->ahw.pci_base2 = mem_ptr2;
661
Amit Kumar Salecha6abb4b82009-10-16 15:50:09 +0000662 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
663 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
664 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
665
666 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadke47abe352009-10-13 05:31:42 +0000667 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
668 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
669 }
670
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800671 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
672 goto skip_doorbell;
673
674 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
675 db_len = pci_resource_len(pdev, 4);
676
677 if (db_len == 0) {
678 printk(KERN_ERR "%s: doorbell is disabled\n",
679 netxen_nic_driver_name);
680 err = -EIO;
681 goto err_out;
682 }
683
684 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
685 if (!db_ptr) {
686 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
687 netxen_nic_driver_name);
688 err = -EIO;
689 goto err_out;
690 }
691
692skip_doorbell:
693 adapter->ahw.db_base = db_ptr;
694 adapter->ahw.db_len = db_len;
695 return 0;
696
697err_out:
698 netxen_cleanup_pci_map(adapter);
699 return err;
700}
701
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000702static void
703netxen_check_options(struct netxen_adapter *adapter)
704{
705 u32 fw_major, fw_minor, fw_build;
706 char brd_name[NETXEN_MAX_SHORT_NAME];
707 char serial_num[32];
708 int i, offset, val;
709 int *ptr32;
710 struct pci_dev *pdev = adapter->pdev;
711
712 adapter->driver_mismatch = 0;
713
714 ptr32 = (int *)&serial_num;
715 offset = NX_FW_SERIAL_NUM_OFFSET;
716 for (i = 0; i < 8; i++) {
717 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
718 dev_err(&pdev->dev, "error reading board info\n");
719 adapter->driver_mismatch = 1;
720 return;
721 }
722 ptr32[i] = cpu_to_le32(val);
723 offset += sizeof(u32);
724 }
725
726 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
727 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
728 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
729
730 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
731
732 if (adapter->portnum == 0) {
733 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
734
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -0700735 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
736 module_name(THIS_MODULE),
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000737 brd_name, serial_num, adapter->ahw.revision_id);
738 }
739
740 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
741 adapter->driver_mismatch = 1;
742 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
743 fw_major, fw_minor, fw_build);
744 return;
745 }
746
747 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
748 i = NXRD32(adapter, NETXEN_SRE_MISC);
749 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
750 }
751
752 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
753 fw_major, fw_minor, fw_build,
754 adapter->ahw.cut_through ? "cut-through" : "legacy");
755
756 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
757 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
758
759 adapter->flags &= ~NETXEN_NIC_LRO_ENABLED;
760
761 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
762 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
763 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
764 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
765 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
766 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
767 }
768
769 adapter->msix_supported = 0;
770 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
771 adapter->msix_supported = !!use_msi_x;
772 adapter->rss_supported = !!use_msi_x;
773 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
774 switch (adapter->ahw.board_type) {
775 case NETXEN_BRDTYPE_P2_SB31_10G:
776 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
777 adapter->msix_supported = !!use_msi_x;
778 adapter->rss_supported = !!use_msi_x;
779 break;
780 default:
781 break;
782 }
783 }
784
785 adapter->num_txd = MAX_CMD_DESCRIPTORS;
786
787 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
788 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
789 adapter->max_rds_rings = 3;
790 } else {
791 adapter->num_lro_rxd = 0;
792 adapter->max_rds_rings = 2;
793 }
794}
795
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800796static int
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000797netxen_start_firmware(struct netxen_adapter *adapter)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800798{
799 int val, err, first_boot;
800 struct pci_dev *pdev = adapter->pdev;
801
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000802 /* required for NX2031 dummy dma */
803 err = nx_set_dma_mask(adapter);
804 if (err)
805 return err;
806
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000807 if (!netxen_can_start_firmware(adapter))
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000808 goto wait_init;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000809
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000810 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800811
812 err = netxen_check_hw_init(adapter, first_boot);
813 if (err) {
814 dev_err(&pdev->dev, "error in init HW init sequence\n");
815 return err;
816 }
817
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000818 netxen_request_firmware(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000819
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000820 err = netxen_need_fw_reset(adapter);
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000821 if (err < 0)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000822 goto err_out;
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000823 if (err == 0)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +0000824 goto wait_init;
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000825
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800826 if (first_boot != 0x55555555) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000827 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000828 netxen_pinit_from_rom(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800829 msleep(1);
830 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000831
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000832 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000833 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
834 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
835
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000836 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
837 netxen_set_port_mode(adapter);
838
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000839 err = netxen_load_firmware(adapter);
840 if (err)
841 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800842
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000843 netxen_release_firmware(adapter);
844
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800845 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
846
847 /* Initialize multicast addr pool owners */
848 val = 0x7654;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000849 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800850 val |= 0x0f000000;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000851 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800852
853 }
854
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000855 err = netxen_init_dummy_dma(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800856 if (err)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000857 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800858
859 /*
860 * Tell the hardware our version number.
861 */
862 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
863 | ((_NETXEN_NIC_LINUX_MINOR << 8))
864 | (_NETXEN_NIC_LINUX_SUBVERSION);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000865 NXWR32(adapter, CRB_DRIVER_VERSION, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800866
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000867wait_init:
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800868 /* Handshake with the card before we register the devices. */
869 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
870 if (err) {
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000871 netxen_free_dummy_dma(adapter);
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000872 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800873 }
874
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +0000875 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
876
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000877 nx_update_dma_mask(adapter);
878
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000879 netxen_check_options(adapter);
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000880
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +0000881 adapter->need_fw_reset = 0;
882
Dhananjay Phadkeec5c50c2009-09-11 11:28:13 +0000883 /* fall through and release firmware */
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000884
885err_out:
886 netxen_release_firmware(adapter);
887 return err;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800888}
889
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000890static int
891netxen_nic_request_irq(struct netxen_adapter *adapter)
892{
893 irq_handler_t handler;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000894 struct nx_host_sds_ring *sds_ring;
895 int err, ring;
896
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000897 unsigned long flags = IRQF_SAMPLE_RANDOM;
898 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000899 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000900
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000901 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
902 handler = netxen_msix_intr;
903 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
904 handler = netxen_msi_intr;
905 else {
906 flags |= IRQF_SHARED;
907 handler = netxen_intr;
908 }
909 adapter->irq = netdev->irq;
910
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000911 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
912 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000913 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000914 err = request_irq(sds_ring->irq, handler,
915 flags, sds_ring->name, sds_ring);
916 if (err)
917 return err;
918 }
919
920 return 0;
921}
922
923static void
924netxen_nic_free_irq(struct netxen_adapter *adapter)
925{
926 int ring;
927 struct nx_host_sds_ring *sds_ring;
928
929 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
930
931 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
932 sds_ring = &recv_ctx->sds_rings[ring];
933 free_irq(sds_ring->irq, sds_ring);
934 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000935}
936
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000937static void
938netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
939{
940 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
941 adapter->coal.normal.data.rx_time_us =
942 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
943 adapter->coal.normal.data.rx_packets =
944 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
945 adapter->coal.normal.data.tx_time_us =
946 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
947 adapter->coal.normal.data.tx_packets =
948 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
949}
950
Amit Kumar Salecha6f771412009-12-05 12:23:54 +0000951/* with rtnl_lock */
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000952static int
Amit Kumar Salecha6f771412009-12-05 12:23:54 +0000953__netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000954{
955 int err;
956
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000957 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
958 return -EIO;
959
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000960 err = adapter->init_port(adapter, adapter->physical_port);
961 if (err) {
962 printk(KERN_ERR "%s: Failed to initialize port %d\n",
963 netxen_nic_driver_name, adapter->portnum);
964 return err;
965 }
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000966 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
Narender Kumar5d09e532009-11-20 22:08:57 +0000967 adapter->macaddr_set(adapter, adapter->mac_addr);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000968
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000969 adapter->set_multi(netdev);
970 adapter->set_mtu(adapter, netdev->mtu);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000971
972 adapter->ahw.linkup = 0;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000973
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000974 if (adapter->max_sds_rings > 1)
975 netxen_config_rss(adapter, 1);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000976
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000977 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
978 netxen_config_intr_coalesce(adapter);
979
Narender Kumar1bb482f2009-08-23 08:35:09 +0000980 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
981 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
982
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000983 netxen_napi_enable(adapter);
984
Dhananjay Phadke5103c9f2009-04-28 15:29:09 +0000985 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000986 netxen_linkevent_request(adapter, 1);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000987 else
988 netxen_nic_set_link_parameters(adapter);
989
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000990 set_bit(__NX_DEV_UP, &adapter->state);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000991 return 0;
992}
993
Amit Kumar Salecha6f771412009-12-05 12:23:54 +0000994/* Usage: During resume and firmware recovery module.*/
995
996static inline int
997netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
998{
999 int err = 0;
1000
1001 rtnl_lock();
1002 if (netif_running(netdev))
1003 err = __netxen_nic_up(adapter, netdev);
1004 rtnl_unlock();
1005
1006 return err;
1007}
1008
1009/* with rtnl_lock */
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001010static void
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001011__netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001012{
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +00001013 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1014 return;
1015
Amit Kumar Salechaf2251f62009-12-05 12:23:55 +00001016 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1017 return;
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001018
Amit Kumar Salechaf2251f62009-12-05 12:23:55 +00001019 smp_mb();
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001020 spin_lock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001021 netif_carrier_off(netdev);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001022 netif_tx_disable(netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001023
1024 if (adapter->stop_port)
1025 adapter->stop_port(adapter);
1026
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001027 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1028 netxen_p3_free_mac_list(adapter);
1029
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001030 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1031
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +00001032 netxen_napi_disable(adapter);
1033
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001034 netxen_release_tx_buffers(adapter);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001035 spin_unlock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001036}
1037
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001038/* Usage: During suspend and firmware recovery module */
1039
1040static inline void
1041netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1042{
1043 rtnl_lock();
1044 if (netif_running(netdev))
1045 __netxen_nic_down(adapter, netdev);
1046 rtnl_unlock();
1047
1048}
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001049
1050static int
1051netxen_nic_attach(struct netxen_adapter *adapter)
1052{
1053 struct net_device *netdev = adapter->netdev;
1054 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001055 int err, ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001056 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001057 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001058
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001059 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1060 return 0;
1061
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001062 err = netxen_init_firmware(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +00001063 if (err)
1064 return err;
1065
1066 err = netxen_napi_add(adapter, netdev);
1067 if (err)
1068 return err;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001069
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001070 err = netxen_alloc_sw_resources(adapter);
1071 if (err) {
1072 printk(KERN_ERR "%s: Error in setting sw resources\n",
1073 netdev->name);
1074 return err;
1075 }
1076
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001077 err = netxen_alloc_hw_resources(adapter);
1078 if (err) {
1079 printk(KERN_ERR "%s: Error in setting hw resources\n",
1080 netdev->name);
1081 goto err_out_free_sw;
1082 }
1083
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001084 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001085 tx_ring = adapter->tx_ring;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001086 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1087 crb_cmd_producer[adapter->portnum]);
1088 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1089 crb_cmd_consumer[adapter->portnum]);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001090
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001091 tx_ring->producer = 0;
1092 tx_ring->sw_consumer = 0;
1093
1094 netxen_nic_update_cmd_producer(adapter, tx_ring);
1095 netxen_nic_update_cmd_consumer(adapter, tx_ring);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001096 }
1097
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001098 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1099 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1100 netxen_post_rx_buffers(adapter, ring, rds_ring);
1101 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001102
1103 err = netxen_nic_request_irq(adapter);
1104 if (err) {
1105 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1106 netdev->name);
1107 goto err_out_free_rxbuf;
1108 }
1109
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +00001110 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1111 netxen_nic_init_coalesce_defaults(adapter);
1112
Narender Kumarfa3ce352009-08-24 19:23:28 +00001113 netxen_create_sysfs_entries(adapter);
1114
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001115 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1116 return 0;
1117
1118err_out_free_rxbuf:
1119 netxen_release_rx_buffers(adapter);
1120 netxen_free_hw_resources(adapter);
1121err_out_free_sw:
1122 netxen_free_sw_resources(adapter);
1123 return err;
1124}
1125
1126static void
1127netxen_nic_detach(struct netxen_adapter *adapter)
1128{
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001129 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1130 return;
1131
Narender Kumarfa3ce352009-08-24 19:23:28 +00001132 netxen_remove_sysfs_entries(adapter);
1133
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001134 netxen_free_hw_resources(adapter);
Dhananjay Phadkecf981ff2009-07-17 15:27:06 +00001135 netxen_release_rx_buffers(adapter);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +00001136 netxen_nic_free_irq(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +00001137 netxen_napi_del(adapter);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001138 netxen_free_sw_resources(adapter);
1139
1140 adapter->is_up = 0;
1141}
1142
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001143int
1144netxen_nic_reset_context(struct netxen_adapter *adapter)
1145{
1146 int err = 0;
1147 struct net_device *netdev = adapter->netdev;
1148
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001149 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1150 return -EBUSY;
1151
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001152 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1153
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001154 netif_device_detach(netdev);
1155
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001156 if (netif_running(netdev))
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001157 __netxen_nic_down(adapter, netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001158
1159 netxen_nic_detach(adapter);
1160
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001161 if (netif_running(netdev)) {
1162 err = netxen_nic_attach(adapter);
1163 if (!err)
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001164 err = __netxen_nic_up(adapter, netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001165
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001166 if (err)
1167 goto done;
1168 }
1169
1170 netif_device_attach(netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001171 }
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001172
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001173done:
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001174 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001175 return err;
1176}
1177
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001178static int
1179netxen_setup_netdev(struct netxen_adapter *adapter,
1180 struct net_device *netdev)
1181{
1182 int err = 0;
1183 struct pci_dev *pdev = adapter->pdev;
1184
1185 adapter->rx_csum = 1;
1186 adapter->mc_enabled = 0;
1187 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1188 adapter->max_mc_count = 38;
1189 else
1190 adapter->max_mc_count = 16;
1191
1192 netdev->netdev_ops = &netxen_netdev_ops;
1193 netdev->watchdog_timeo = 2*HZ;
1194
1195 netxen_nic_change_mtu(netdev, netdev->mtu);
1196
1197 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1198
1199 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1200 netdev->features |= (NETIF_F_GRO);
1201 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1202
1203 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1204 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1205 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1206 }
1207
1208 if (adapter->pci_using_dac) {
1209 netdev->features |= NETIF_F_HIGHDMA;
1210 netdev->vlan_features |= NETIF_F_HIGHDMA;
1211 }
1212
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001213 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1214 netdev->features |= (NETIF_F_HW_VLAN_TX);
1215
Narender Kumar1bb482f2009-08-23 08:35:09 +00001216 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1217 netdev->features |= NETIF_F_LRO;
1218
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001219 netdev->irq = adapter->msix_entries[0].vector;
1220
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001221 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001222
1223 if (netxen_read_mac_addr(adapter))
1224 dev_warn(&pdev->dev, "failed to read mac addr\n");
1225
1226 netif_carrier_off(netdev);
1227 netif_stop_queue(netdev);
1228
1229 err = register_netdev(netdev);
1230 if (err) {
1231 dev_err(&pdev->dev, "failed to register net device\n");
1232 return err;
1233 }
1234
1235 return 0;
1236}
1237
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001238static int __devinit
1239netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1240{
1241 struct net_device *netdev = NULL;
1242 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001243 int i = 0, err;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001244 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001245 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001246
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001247 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -07001248 pr_warning("%s: chip revisions between 0x%x-0x%x"
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001249 "will not be enabled.\n",
Dhananjay Phadkec25c0b92009-10-28 04:11:04 -07001250 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001251 return -ENODEV;
1252 }
1253
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001254 if ((err = pci_enable_device(pdev)))
1255 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001256
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001257 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1258 err = -ENODEV;
1259 goto err_out_disable_pdev;
1260 }
1261
1262 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1263 goto err_out_disable_pdev;
1264
1265 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001266
1267 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1268 if(!netdev) {
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001269 dev_err(&pdev->dev, "failed to allocate net_device\n");
1270 err = -ENOMEM;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001271 goto err_out_free_res;
1272 }
1273
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001274 SET_NETDEV_DEV(netdev, &pdev->dev);
1275
Wang Chen4cf16532008-11-12 23:38:14 -08001276 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001277 adapter->netdev = netdev;
1278 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001279 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001280
1281 revision_id = pdev->revision;
1282 adapter->ahw.revision_id = revision_id;
1283
Dhananjay Phadkef03b0eb2009-10-13 05:31:44 +00001284 rwlock_init(&adapter->ahw.crb_lock);
1285 spin_lock_init(&adapter->ahw.mem_lock);
1286
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001287 spin_lock_init(&adapter->tx_clean_lock);
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001288 INIT_LIST_HEAD(&adapter->mac_list);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001289
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001290 err = netxen_setup_pci_map(adapter);
1291 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001292 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001293
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301294 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001295 adapter->portnum = pci_func_id;
1296
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001297 err = netxen_nic_get_board_info(adapter);
1298 if (err) {
1299 dev_err(&pdev->dev, "Error getting board config info.\n");
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001300 goto err_out_iounmap;
1301 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001302
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001303 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001304 switch (adapter->ahw.board_type) {
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001305 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1306 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1307 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001308 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001309 break;
1310 default:
1311 break;
1312 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001313
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001314 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001315 if (err)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00001316 goto err_out_decr_ref;
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001317
Dhananjay Phadke29566402008-07-21 19:44:04 -07001318 /*
1319 * See if the firmware gave us a virtual-physical port mapping.
1320 */
1321 adapter->physical_port = adapter->portnum;
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001322 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001323 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001324 if (i != 0x55555555)
1325 adapter->physical_port = i;
1326 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001327
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001328 netxen_nic_clear_stats(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001329
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001330 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001331
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001332 err = netxen_setup_netdev(adapter, netdev);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001333 if (err)
Dhananjay Phadke29566402008-07-21 19:44:04 -07001334 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001335
1336 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001337
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001338 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1339
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001340 switch (adapter->ahw.port_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001341 case NETXEN_NIC_GBE:
1342 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1343 adapter->netdev->name);
1344 break;
1345 case NETXEN_NIC_XGBE:
1346 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1347 adapter->netdev->name);
1348 break;
1349 }
1350
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00001351 netxen_create_diag_entries(adapter);
1352
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001353 return 0;
1354
Dhananjay Phadke29566402008-07-21 19:44:04 -07001355err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001356 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001357
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001358 netxen_free_dummy_dma(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001359
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00001360err_out_decr_ref:
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001361 nx_decr_dev_ref_cnt(adapter);
1362
Dhananjay Phadke29566402008-07-21 19:44:04 -07001363err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001364 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001365
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001366err_out_free_netdev:
1367 free_netdev(netdev);
1368
1369err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001370 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001371
1372err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001373 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001374 pci_disable_device(pdev);
1375 return err;
1376}
1377
1378static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1379{
1380 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001381 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001382
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001383 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001384 if (adapter == NULL)
1385 return;
1386
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001387 netdev = adapter->netdev;
1388
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001389 netxen_cancel_fw_work(adapter);
1390
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301391 unregister_netdev(netdev);
1392
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001393 cancel_work_sync(&adapter->tx_timeout_task);
1394
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001395 netxen_nic_detach(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001396
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001397 nx_decr_dev_ref_cnt(adapter);
1398
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001399 if (adapter->portnum == 0)
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001400 netxen_free_dummy_dma(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301401
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001402 clear_bit(__NX_RESETTING, &adapter->state);
1403
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001404 netxen_teardown_intr(adapter);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301405
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00001406 netxen_remove_diag_entries(adapter);
1407
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001408 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001409
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001410 netxen_release_firmware(adapter);
1411
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001412 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301413 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001414 pci_set_drvdata(pdev, NULL);
1415
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001416 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001417}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001418static int __netxen_nic_shutdown(struct pci_dev *pdev)
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001419{
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001420 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1421 struct net_device *netdev = adapter->netdev;
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001422 int retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001423
1424 netif_device_detach(netdev);
1425
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001426 netxen_cancel_fw_work(adapter);
1427
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001428 if (netif_running(netdev))
1429 netxen_nic_down(adapter, netdev);
1430
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001431 cancel_work_sync(&adapter->tx_timeout_task);
1432
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001433 netxen_nic_detach(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001434
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001435 if (adapter->portnum == 0)
1436 netxen_free_dummy_dma(adapter);
1437
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001438 nx_decr_dev_ref_cnt(adapter);
1439
1440 clear_bit(__NX_RESETTING, &adapter->state);
1441
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001442 retval = pci_save_state(pdev);
1443 if (retval)
1444 return retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001445
1446 if (netxen_nic_wol_supported(adapter)) {
1447 pci_enable_wake(pdev, PCI_D3cold, 1);
1448 pci_enable_wake(pdev, PCI_D3hot, 1);
1449 }
1450
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001451 return 0;
1452}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001453static void netxen_nic_shutdown(struct pci_dev *pdev)
1454{
1455 if (__netxen_nic_shutdown(pdev))
1456 return;
Amit Kumar Salechabc86fcb2009-12-08 20:40:54 +00001457
1458 pci_disable_device(pdev);
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001459}
1460#ifdef CONFIG_PM
1461static int
1462netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1463{
1464 int retval;
1465
1466 retval = __netxen_nic_shutdown(pdev);
1467 if (retval)
1468 return retval;
1469
1470 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Amit Kumar Salechabc86fcb2009-12-08 20:40:54 +00001471
1472 pci_disable_device(pdev);
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001473 return 0;
1474}
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001475
1476static int
1477netxen_nic_resume(struct pci_dev *pdev)
1478{
1479 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1480 struct net_device *netdev = adapter->netdev;
1481 int err;
1482
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001483 err = pci_enable_device(pdev);
1484 if (err)
1485 return err;
1486
Amit Kumar Salechabc86fcb2009-12-08 20:40:54 +00001487 pci_set_power_state(pdev, PCI_D0);
1488 pci_set_master(pdev);
1489 pci_restore_state(pdev);
1490
Dhananjay Phadke907fa122009-10-13 05:31:43 +00001491 adapter->ahw.crb_win = -1;
Dhananjay Phadke47abe352009-10-13 05:31:42 +00001492 adapter->ahw.ocm_win = -1;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001493
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001494 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001495 if (err) {
1496 dev_err(&pdev->dev, "failed to start firmware\n");
1497 return err;
1498 }
1499
1500 if (netif_running(netdev)) {
1501 err = netxen_nic_attach(adapter);
1502 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001503 goto err_out;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001504
1505 err = netxen_nic_up(adapter, netdev);
1506 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001507 goto err_out_detach;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001508
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00001509 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001510 }
1511
Amit Kumar Salechabc86fcb2009-12-08 20:40:54 +00001512 netif_device_attach(netdev);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001513 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
Dhananjay Phadke07653aa2009-09-20 19:20:39 +00001514 return 0;
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001515
1516err_out_detach:
1517 netxen_nic_detach(adapter);
1518err_out:
1519 nx_decr_dev_ref_cnt(adapter);
1520 return err;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001521}
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001522#endif
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001523
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001524static int netxen_nic_open(struct net_device *netdev)
1525{
Wang Chen4cf16532008-11-12 23:38:14 -08001526 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001527 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001528
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001529 if (adapter->driver_mismatch)
1530 return -EIO;
1531
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001532 err = netxen_nic_attach(adapter);
1533 if (err)
1534 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001535
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001536 err = __netxen_nic_up(adapter, netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001537 if (err)
1538 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001539
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001540 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001541
1542 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001543
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001544err_out:
1545 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001546 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001547}
1548
1549/*
1550 * netxen_nic_close - Disables a network interface entry point
1551 */
1552static int netxen_nic_close(struct net_device *netdev)
1553{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001554 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001555
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00001556 __netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001557 return 0;
1558}
1559
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001560static void
1561netxen_tso_check(struct net_device *netdev,
1562 struct nx_host_tx_ring *tx_ring,
1563 struct cmd_desc_type0 *first_desc,
1564 struct sk_buff *skb)
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001565{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001566 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001567 __be16 protocol = skb->protocol;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001568 u16 flags = 0, vid = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001569 u32 producer;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001570 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001571 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001572 struct vlan_ethhdr *vh;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001573
Harvey Harrison09640e62009-02-01 00:45:17 -08001574 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001575
1576 vh = (struct vlan_ethhdr *)skb->data;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001577 protocol = vh->h_vlan_encapsulated_proto;
1578 flags = FLAGS_VLAN_TAGGED;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001579
1580 } else if (vlan_tx_tag_present(skb)) {
1581
1582 flags = FLAGS_VLAN_OOB;
1583 vid = vlan_tx_tag_get(skb);
1584 netxen_set_tx_vlan_tci(first_desc, vid);
1585 vlan_oob = 1;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001586 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001587
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001588 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1589 skb_shinfo(skb)->gso_size > 0) {
1590
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001591 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1592
1593 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1594 first_desc->total_hdr_length = hdr_len;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001595 if (vlan_oob) {
1596 first_desc->total_hdr_length += VLAN_HLEN;
1597 first_desc->tcp_hdr_offset = VLAN_HLEN;
1598 first_desc->ip_hdr_offset = VLAN_HLEN;
1599 /* Only in case of TSO on vlan device */
1600 flags |= FLAGS_VLAN_TAGGED;
1601 }
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001602
Harvey Harrison09640e62009-02-01 00:45:17 -08001603 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001604 TX_TCP_LSO6 : TX_TCP_LSO;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001605 tso = 1;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001606
1607 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001608 u8 l4proto;
1609
Harvey Harrison09640e62009-02-01 00:45:17 -08001610 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001611 l4proto = ip_hdr(skb)->protocol;
1612
1613 if (l4proto == IPPROTO_TCP)
1614 opcode = TX_TCP_PKT;
1615 else if(l4proto == IPPROTO_UDP)
1616 opcode = TX_UDP_PKT;
Harvey Harrison09640e62009-02-01 00:45:17 -08001617 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001618 l4proto = ipv6_hdr(skb)->nexthdr;
1619
1620 if (l4proto == IPPROTO_TCP)
1621 opcode = TX_TCPV6_PKT;
1622 else if(l4proto == IPPROTO_UDP)
1623 opcode = TX_UDPV6_PKT;
1624 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001625 }
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001626
1627 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1628 first_desc->ip_hdr_offset += skb_network_offset(skb);
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001629 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1630
1631 if (!tso)
1632 return;
1633
1634 /* For LSO, we need to copy the MAC/IP/TCP headers into
1635 * the descriptor ring
1636 */
1637 producer = tx_ring->producer;
1638 copied = 0;
1639 offset = 2;
1640
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001641 if (vlan_oob) {
1642 /* Create a TSO vlan header template for firmware */
1643
1644 hwdesc = &tx_ring->desc_head[producer];
1645 tx_ring->cmd_buf_arr[producer].skb = NULL;
1646
1647 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1648 hdr_len + VLAN_HLEN);
1649
1650 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1651 skb_copy_from_linear_data(skb, vh, 12);
1652 vh->h_vlan_proto = htons(ETH_P_8021Q);
1653 vh->h_vlan_TCI = htons(vid);
1654 skb_copy_from_linear_data_offset(skb, 12,
1655 (char *)vh + 16, copy_len - 16);
1656
Dhananjay Phadke545eb372009-08-05 07:34:07 +00001657 copied = copy_len - VLAN_HLEN;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001658 offset = 0;
1659
1660 producer = get_next_index(producer, tx_ring->num_desc);
1661 }
1662
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001663 while (copied < hdr_len) {
1664
1665 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1666 (hdr_len - copied));
1667
1668 hwdesc = &tx_ring->desc_head[producer];
1669 tx_ring->cmd_buf_arr[producer].skb = NULL;
1670
1671 skb_copy_from_linear_data_offset(skb, copied,
1672 (char *)hwdesc + offset, copy_len);
1673
1674 copied += copy_len;
1675 offset = 0;
1676
1677 producer = get_next_index(producer, tx_ring->num_desc);
1678 }
1679
1680 tx_ring->producer = producer;
1681 barrier();
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001682}
1683
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001684static int
1685netxen_map_tx_skb(struct pci_dev *pdev,
1686 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001687{
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001688 struct netxen_skb_frag *nf;
1689 struct skb_frag_struct *frag;
1690 int i, nr_frags;
1691 dma_addr_t map;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001692
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001693 nr_frags = skb_shinfo(skb)->nr_frags;
1694 nf = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001695
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001696 map = pci_map_single(pdev, skb->data,
1697 skb_headlen(skb), PCI_DMA_TODEVICE);
1698 if (pci_dma_mapping_error(pdev, map))
1699 goto out_err;
1700
1701 nf->dma = map;
1702 nf->length = skb_headlen(skb);
1703
1704 for (i = 0; i < nr_frags; i++) {
1705 frag = &skb_shinfo(skb)->frags[i];
1706 nf = &pbuf->frag_array[i+1];
1707
1708 map = pci_map_page(pdev, frag->page, frag->page_offset,
1709 frag->size, PCI_DMA_TODEVICE);
1710 if (pci_dma_mapping_error(pdev, map))
1711 goto unwind;
1712
1713 nf->dma = map;
1714 nf->length = frag->size;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001715 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001716
1717 return 0;
1718
1719unwind:
Dhananjay Phadkecf503e82009-09-03 13:10:55 +00001720 while (--i >= 0) {
1721 nf = &pbuf->frag_array[i+1];
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001722 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1723 }
1724
1725 nf = &pbuf->frag_array[0];
1726 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1727
1728out_err:
1729 return -ENOMEM;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001730}
1731
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001732static inline void
1733netxen_clear_cmddesc(u64 *desc)
1734{
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001735 desc[0] = 0ULL;
1736 desc[2] = 0ULL;
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001737}
1738
Stephen Hemminger613573252009-08-31 19:50:58 +00001739static netdev_tx_t
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001740netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001741{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001742 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001743 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001744 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001745 struct netxen_skb_frag *buffrag;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001746 struct cmd_desc_type0 *hwdesc, *first_desc;
1747 struct pci_dev *pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001748 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001749
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001750 u32 producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001751 int frag_count, no_of_desc;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001752 u32 num_txd = tx_ring->num_desc;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001753
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001754 frag_count = skb_shinfo(skb)->nr_frags + 1;
1755
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001756 /* 4 fragments per cmd des */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001757 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001758
Roel Kluinfa9859e2009-10-06 19:34:39 +00001759 if (unlikely(no_of_desc + 2 > netxen_tx_avail(tx_ring))) {
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001760 netif_stop_queue(netdev);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001761 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001762 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001763
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001764 producer = tx_ring->producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001765 pbuf = &tx_ring->cmd_buf_arr[producer];
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001766
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001767 pdev = adapter->pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001768
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001769 if (netxen_map_tx_skb(pdev, skb, pbuf))
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001770 goto drop_packet;
1771
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001772 pbuf->skb = skb;
1773 pbuf->frag_count = frag_count;
1774
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001775 first_desc = hwdesc = &tx_ring->desc_head[producer];
1776 netxen_clear_cmddesc((u64 *)hwdesc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001777
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001778 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1779 netxen_set_tx_port(first_desc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001780
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001781 for (i = 0; i < frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001782
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001783 k = i % 4;
1784
1785 if ((k == 0) && (i > 0)) {
1786 /* move to next desc.*/
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001787 producer = get_next_index(producer, num_txd);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001788 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001789 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001790 tx_ring->cmd_buf_arr[producer].skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001791 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001792
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001793 buffrag = &pbuf->frag_array[i];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001794
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001795 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001796 switch (k) {
1797 case 0:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001798 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001799 break;
1800 case 1:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001801 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001802 break;
1803 case 2:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001804 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001805 break;
1806 case 3:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001807 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001808 break;
1809 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001810 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001811
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001812 tx_ring->producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001813
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001814 netxen_tso_check(netdev, tx_ring, first_desc, skb);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001815
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001816 netxen_nic_update_cmd_producer(adapter, tx_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001817
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001818 adapter->stats.txbytes += skb->len;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001819 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001820
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001821 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001822
1823drop_packet:
1824 adapter->stats.txdropped++;
1825 dev_kfree_skb_any(skb);
1826 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001827}
1828
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001829static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1830{
1831 struct net_device *netdev = adapter->netdev;
1832 uint32_t temp, temp_state, temp_val;
1833 int rv = 0;
1834
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001835 temp = NXRD32(adapter, CRB_TEMP_STATE);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001836
1837 temp_state = nx_get_temp_state(temp);
1838 temp_val = nx_get_temp_val(temp);
1839
1840 if (temp_state == NX_TEMP_PANIC) {
1841 printk(KERN_ALERT
1842 "%s: Device temperature %d degrees C exceeds"
1843 " maximum allowed. Hardware has been shut down.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001844 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001845 rv = 1;
1846 } else if (temp_state == NX_TEMP_WARN) {
1847 if (adapter->temp == NX_TEMP_NORMAL) {
1848 printk(KERN_ALERT
1849 "%s: Device temperature %d degrees C "
1850 "exceeds operating range."
1851 " Immediate action needed.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001852 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001853 }
1854 } else {
1855 if (adapter->temp == NX_TEMP_WARN) {
1856 printk(KERN_INFO
1857 "%s: Device temperature is now %d degrees C"
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001858 " in normal range.\n", netdev->name,
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001859 temp_val);
1860 }
1861 }
1862 adapter->temp = temp_state;
1863 return rv;
1864}
1865
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001866void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001867{
1868 struct net_device *netdev = adapter->netdev;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001869
1870 if (adapter->ahw.linkup && !linkup) {
1871 printk(KERN_INFO "%s: %s NIC Link is down\n",
1872 netxen_nic_driver_name, netdev->name);
1873 adapter->ahw.linkup = 0;
1874 if (netif_running(netdev)) {
1875 netif_carrier_off(netdev);
1876 netif_stop_queue(netdev);
1877 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001878 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001879 } else if (!adapter->ahw.linkup && linkup) {
1880 printk(KERN_INFO "%s: %s NIC Link is up\n",
1881 netxen_nic_driver_name, netdev->name);
1882 adapter->ahw.linkup = 1;
1883 if (netif_running(netdev)) {
1884 netif_carrier_on(netdev);
1885 netif_wake_queue(netdev);
1886 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001887 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001888 }
1889}
1890
1891static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1892{
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001893 u32 val, port, linkup;
1894
1895 port = adapter->physical_port;
1896
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001897 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001898 val = NXRD32(adapter, CRB_XG_STATE_P3);
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001899 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1900 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001901 } else {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001902 val = NXRD32(adapter, CRB_XG_STATE);
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001903 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001904 linkup = (val >> port) & 1;
1905 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001906 val = (val >> port*8) & 0xff;
1907 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001908 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001909 }
1910
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001911 netxen_advert_link_change(adapter, linkup);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001912}
1913
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001914static void netxen_tx_timeout(struct net_device *netdev)
1915{
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001916 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001917
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001918 if (test_bit(__NX_RESETTING, &adapter->state))
1919 return;
1920
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001921 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001922 schedule_work(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001923}
1924
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001925static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001926{
Jeff Garzik47906542007-11-23 21:23:36 -05001927 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001928 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001929
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001930 if (!netif_running(adapter->netdev))
1931 return;
1932
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001933 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001934 return;
1935
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001936 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
1937 goto request_reset;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001938
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001939 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1940 /* try to scrub interrupt */
1941 netxen_napi_disable(adapter);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001942
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001943 adapter->netdev->trans_start = jiffies;
1944
1945 netxen_napi_enable(adapter);
1946
1947 netif_wake_queue(adapter->netdev);
1948
Dhananjay Phadkea598ae12009-09-20 19:20:38 +00001949 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001950
1951 } else {
Dhananjay Phadkea598ae12009-09-20 19:20:38 +00001952 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001953 if (!netxen_nic_reset_context(adapter)) {
1954 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkea598ae12009-09-20 19:20:38 +00001955 return;
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001956 }
1957
1958 /* context reset failed, fall through for fw reset */
1959 }
1960
1961request_reset:
1962 adapter->need_fw_reset = 1;
Amit Kumar Salechaff8a3062009-10-21 19:39:02 +00001963 clear_bit(__NX_RESETTING, &adapter->state);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001964}
1965
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001966struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1967{
1968 struct netxen_adapter *adapter = netdev_priv(netdev);
Ajit Khaparde5a4d6312009-10-07 02:45:34 +00001969 struct net_device_stats *stats = &netdev->stats;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001970
1971 memset(stats, 0, sizeof(*stats));
1972
Narender Kumar1bb482f2009-08-23 08:35:09 +00001973 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
Dhananjay Phadkee98e3352009-04-07 22:50:38 +00001974 stats->tx_packets = adapter->stats.xmitfinished;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001975 stats->rx_bytes = adapter->stats.rxbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001976 stats->tx_bytes = adapter->stats.txbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001977 stats->rx_dropped = adapter->stats.rxdropped;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001978 stats->tx_dropped = adapter->stats.txdropped;
1979
1980 return stats;
1981}
1982
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001983static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001984{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001985 struct nx_host_sds_ring *sds_ring = data;
1986 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001987 u32 status = 0;
1988
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001989 status = readl(adapter->isr_int_vec);
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001990
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001991 if (!(status & adapter->int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001992 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301993
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001994 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001995 /* check interrupt state machine, to be sure */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001996 status = readl(adapter->crb_int_state_reg);
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001997 if (!ISR_LEGACY_INT_TRIGGERED(status))
1998 return IRQ_NONE;
1999
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002000 } else {
2001 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07002002
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002003 our_int = readl(adapter->crb_int_state_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002004
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07002005 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002006 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07002007 return IRQ_NONE;
2008
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002009 /* claim interrupt */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002010 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2011
2012 /* clear interrupt */
2013 netxen_nic_disable_int(sds_ring);
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05302014 }
2015
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002016 writel(0xffffffff, adapter->tgt_status_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002017 /* read twice to ensure write is flushed */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002018 readl(adapter->isr_int_vec);
2019 readl(adapter->isr_int_vec);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002020
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002021 napi_schedule(&sds_ring->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002022
2023 return IRQ_HANDLED;
2024}
2025
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07002026static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002027{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002028 struct nx_host_sds_ring *sds_ring = data;
2029 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002030
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002031 /* clear interrupt */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00002032 writel(0xffffffff, adapter->tgt_status_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07002033
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002034 napi_schedule(&sds_ring->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002035 return IRQ_HANDLED;
2036}
2037
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08002038static irqreturn_t netxen_msix_intr(int irq, void *data)
2039{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002040 struct nx_host_sds_ring *sds_ring = data;
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08002041
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002042 napi_schedule(&sds_ring->napi);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08002043 return IRQ_HANDLED;
2044}
2045
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002046static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002047{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002048 struct nx_host_sds_ring *sds_ring =
2049 container_of(napi, struct nx_host_sds_ring, napi);
2050
2051 struct netxen_adapter *adapter = sds_ring->adapter;
2052
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002053 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002054 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002055
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002056 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002057
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002058 work_done = netxen_process_rcv_ring(sds_ring, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002059
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07002060 if ((work_done < budget) && tx_complete) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00002061 napi_complete(&sds_ring->napi);
Amit Kumar Salechaf2251f62009-12-05 12:23:55 +00002062 if (test_bit(__NX_DEV_UP, &adapter->state))
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00002063 netxen_nic_enable_int(sds_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002064 }
2065
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002066 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002067}
2068
2069#ifdef CONFIG_NET_POLL_CONTROLLER
2070static void netxen_nic_poll_controller(struct net_device *netdev)
2071{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07002072 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002073 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05002074 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002075 enable_irq(adapter->irq);
2076}
2077#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002078
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002079static int
2080nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2081{
2082 int count;
2083 if (netxen_api_lock(adapter))
2084 return -EIO;
2085
2086 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2087
2088 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2089
2090 netxen_api_unlock(adapter);
2091 return count;
2092}
2093
2094static int
2095nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2096{
2097 int count;
2098 if (netxen_api_lock(adapter))
2099 return -EIO;
2100
2101 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2102 WARN_ON(count == 0);
2103
2104 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2105
2106 if (count == 0)
2107 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2108
2109 netxen_api_unlock(adapter);
2110 return count;
2111}
2112
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00002113static void
2114nx_dev_request_reset(struct netxen_adapter *adapter)
2115{
2116 u32 state;
2117
2118 if (netxen_api_lock(adapter))
2119 return;
2120
2121 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2122
2123 if (state != NX_DEV_INITALIZING)
2124 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2125
2126 netxen_api_unlock(adapter);
2127}
2128
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002129static int
2130netxen_can_start_firmware(struct netxen_adapter *adapter)
2131{
2132 int count;
2133 int can_start = 0;
2134
2135 if (netxen_api_lock(adapter))
2136 return 0;
2137
2138 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2139
2140 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2141 count = 0;
2142
2143 if (count == 0) {
2144 can_start = 1;
2145 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2146 }
2147
2148 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2149
2150 netxen_api_unlock(adapter);
2151
2152 return can_start;
2153}
2154
2155static void
2156netxen_schedule_work(struct netxen_adapter *adapter,
2157 work_func_t func, int delay)
2158{
2159 INIT_DELAYED_WORK(&adapter->fw_work, func);
2160 schedule_delayed_work(&adapter->fw_work, delay);
2161}
2162
2163static void
2164netxen_cancel_fw_work(struct netxen_adapter *adapter)
2165{
2166 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2167 msleep(10);
2168
2169 cancel_delayed_work_sync(&adapter->fw_work);
2170}
2171
2172static void
2173netxen_attach_work(struct work_struct *work)
2174{
2175 struct netxen_adapter *adapter = container_of(work,
2176 struct netxen_adapter, fw_work.work);
2177 struct net_device *netdev = adapter->netdev;
2178 int err = 0;
2179
2180 if (netif_running(netdev)) {
2181 err = netxen_nic_attach(adapter);
2182 if (err)
2183 goto done;
2184
2185 err = netxen_nic_up(adapter, netdev);
2186 if (err) {
2187 netxen_nic_detach(adapter);
2188 goto done;
2189 }
2190
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002191 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002192 }
2193
2194 netif_device_attach(netdev);
2195
2196done:
2197 adapter->fw_fail_cnt = 0;
2198 clear_bit(__NX_RESETTING, &adapter->state);
2199 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2200}
2201
2202static void
2203netxen_fwinit_work(struct work_struct *work)
2204{
2205 struct netxen_adapter *adapter = container_of(work,
2206 struct netxen_adapter, fw_work.work);
2207 int dev_state;
2208
2209 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2210
2211 switch (dev_state) {
2212 case NX_DEV_COLD:
2213 case NX_DEV_READY:
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00002214 if (!netxen_start_firmware(adapter)) {
2215 netxen_schedule_work(adapter, netxen_attach_work, 0);
2216 return;
2217 }
2218 break;
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002219
2220 case NX_DEV_INITALIZING:
2221 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2222 netxen_schedule_work(adapter,
2223 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2224 return;
2225 }
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002226
2227 case NX_DEV_FAILED:
2228 default:
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002229 nx_incr_dev_ref_cnt(adapter);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002230 break;
2231 }
2232
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002233 clear_bit(__NX_RESETTING, &adapter->state);
2234}
2235
2236static void
2237netxen_detach_work(struct work_struct *work)
2238{
2239 struct netxen_adapter *adapter = container_of(work,
2240 struct netxen_adapter, fw_work.work);
2241 struct net_device *netdev = adapter->netdev;
2242 int ref_cnt, delay;
2243 u32 status;
2244
2245 netif_device_detach(netdev);
2246
Amit Kumar Salecha6f771412009-12-05 12:23:54 +00002247 netxen_nic_down(adapter, netdev);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002248
2249 netxen_nic_detach(adapter);
2250
2251 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2252
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002253 if (status & NX_RCODE_FATAL_ERROR)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002254 goto err_ret;
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002255
2256 if (adapter->temp == NX_TEMP_PANIC)
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002257 goto err_ret;
2258
2259 ref_cnt = nx_decr_dev_ref_cnt(adapter);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002260
2261 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2262
2263 adapter->fw_wait_cnt = 0;
2264 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
Amit Kumar Salecha8f9b3f62009-12-02 15:46:19 +00002265
2266 return;
2267
2268err_ret:
2269 clear_bit(__NX_RESETTING, &adapter->state);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002270}
2271
2272static int
2273netxen_check_health(struct netxen_adapter *adapter)
2274{
2275 u32 state, heartbit;
2276 struct net_device *netdev = adapter->netdev;
2277
2278 if (netxen_nic_check_temp(adapter))
2279 goto detach;
2280
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00002281 if (adapter->need_fw_reset) {
2282 nx_dev_request_reset(adapter);
2283 goto detach;
2284 }
2285
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002286 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2287 if (state == NX_DEV_NEED_RESET)
2288 goto detach;
2289
2290 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2291 return 0;
2292
2293 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2294 if (heartbit != adapter->heartbit) {
2295 adapter->heartbit = heartbit;
2296 adapter->fw_fail_cnt = 0;
2297 return 0;
2298 }
2299
2300 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2301 return 0;
2302
2303 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2304
2305 dev_info(&netdev->dev, "firmware hang detected\n");
2306
2307detach:
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002308 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2309 !test_and_set_bit(__NX_RESETTING, &adapter->state))
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002310 netxen_schedule_work(adapter, netxen_detach_work, 0);
2311 return 1;
2312}
2313
2314static void
2315netxen_fw_poll_work(struct work_struct *work)
2316{
2317 struct netxen_adapter *adapter = container_of(work,
2318 struct netxen_adapter, fw_work.work);
2319
2320 if (test_bit(__NX_RESETTING, &adapter->state))
2321 goto reschedule;
2322
2323 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2324 if (!adapter->has_link_events) {
2325
2326 netxen_nic_handle_phy_intr(adapter);
2327
2328 if (adapter->link_changed)
2329 netxen_nic_set_link_parameters(adapter);
2330 }
2331 }
2332
2333 if (netxen_check_health(adapter))
2334 return;
2335
2336reschedule:
2337 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2338}
2339
Narender Kumarfa3ce352009-08-24 19:23:28 +00002340static ssize_t
2341netxen_store_bridged_mode(struct device *dev,
2342 struct device_attribute *attr, const char *buf, size_t len)
2343{
2344 struct net_device *net = to_net_dev(dev);
2345 struct netxen_adapter *adapter = netdev_priv(net);
2346 unsigned long new;
2347 int ret = -EINVAL;
2348
2349 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2350 goto err_out;
2351
2352 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2353 goto err_out;
2354
2355 if (strict_strtoul(buf, 2, &new))
2356 goto err_out;
2357
2358 if (!netxen_config_bridged_mode(adapter, !!new))
2359 ret = len;
2360
2361err_out:
2362 return ret;
2363}
2364
2365static ssize_t
2366netxen_show_bridged_mode(struct device *dev,
2367 struct device_attribute *attr, char *buf)
2368{
2369 struct net_device *net = to_net_dev(dev);
2370 struct netxen_adapter *adapter;
2371 int bridged_mode = 0;
2372
2373 adapter = netdev_priv(net);
2374
2375 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2376 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2377
2378 return sprintf(buf, "%d\n", bridged_mode);
2379}
2380
2381static struct device_attribute dev_attr_bridged_mode = {
2382 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2383 .show = netxen_show_bridged_mode,
2384 .store = netxen_store_bridged_mode,
2385};
2386
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00002387static ssize_t
2388netxen_store_diag_mode(struct device *dev,
2389 struct device_attribute *attr, const char *buf, size_t len)
2390{
2391 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2392 unsigned long new;
2393
2394 if (strict_strtoul(buf, 2, &new))
2395 return -EINVAL;
2396
2397 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2398 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2399
2400 return len;
2401}
2402
2403static ssize_t
2404netxen_show_diag_mode(struct device *dev,
2405 struct device_attribute *attr, char *buf)
2406{
2407 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2408
2409 return sprintf(buf, "%d\n",
2410 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2411}
2412
2413static struct device_attribute dev_attr_diag_mode = {
2414 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2415 .show = netxen_show_diag_mode,
2416 .store = netxen_store_diag_mode,
2417};
2418
2419static int
2420netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2421 loff_t offset, size_t size)
2422{
2423 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2424 return -EIO;
2425
2426 if ((size != 4) || (offset & 0x3))
2427 return -EINVAL;
2428
2429 if (offset < NETXEN_PCI_CRBSPACE)
2430 return -EINVAL;
2431
2432 return 0;
2433}
2434
2435static ssize_t
2436netxen_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2437 char *buf, loff_t offset, size_t size)
2438{
2439 struct device *dev = container_of(kobj, struct device, kobj);
2440 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2441 u32 data;
2442 int ret;
2443
2444 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2445 if (ret != 0)
2446 return ret;
2447
2448 data = NXRD32(adapter, offset);
2449 memcpy(buf, &data, size);
2450 return size;
2451}
2452
2453static ssize_t
2454netxen_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2455 char *buf, loff_t offset, size_t size)
2456{
2457 struct device *dev = container_of(kobj, struct device, kobj);
2458 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2459 u32 data;
2460 int ret;
2461
2462 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2463 if (ret != 0)
2464 return ret;
2465
2466 memcpy(&data, buf, size);
2467 NXWR32(adapter, offset, data);
2468 return size;
2469}
2470
2471static int
2472netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2473 loff_t offset, size_t size)
2474{
2475 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2476 return -EIO;
2477
2478 if ((size != 8) || (offset & 0x7))
2479 return -EIO;
2480
2481 return 0;
2482}
2483
2484static ssize_t
2485netxen_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2486 char *buf, loff_t offset, size_t size)
2487{
2488 struct device *dev = container_of(kobj, struct device, kobj);
2489 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2490 u64 data;
2491 int ret;
2492
2493 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2494 if (ret != 0)
2495 return ret;
2496
2497 if (adapter->pci_mem_read(adapter, offset, &data))
2498 return -EIO;
2499
2500 memcpy(buf, &data, size);
2501
2502 return size;
2503}
2504
2505ssize_t netxen_sysfs_write_mem(struct kobject *kobj,
2506 struct bin_attribute *attr, char *buf,
2507 loff_t offset, size_t size)
2508{
2509 struct device *dev = container_of(kobj, struct device, kobj);
2510 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2511 u64 data;
2512 int ret;
2513
2514 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2515 if (ret != 0)
2516 return ret;
2517
2518 memcpy(&data, buf, size);
2519
2520 if (adapter->pci_mem_write(adapter, offset, data))
2521 return -EIO;
2522
2523 return size;
2524}
2525
2526
2527static struct bin_attribute bin_attr_crb = {
2528 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2529 .size = 0,
2530 .read = netxen_sysfs_read_crb,
2531 .write = netxen_sysfs_write_crb,
2532};
2533
2534static struct bin_attribute bin_attr_mem = {
2535 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2536 .size = 0,
2537 .read = netxen_sysfs_read_mem,
2538 .write = netxen_sysfs_write_mem,
2539};
2540
Narender Kumarcaa2dd52009-10-16 15:50:11 +00002541
Narender Kumarfa3ce352009-08-24 19:23:28 +00002542static void
2543netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2544{
2545 struct net_device *netdev = adapter->netdev;
2546 struct device *dev = &netdev->dev;
2547
2548 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2549 /* bridged_mode control */
2550 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2551 dev_warn(&netdev->dev,
2552 "failed to create bridged_mode sysfs entry\n");
2553 }
2554 }
2555}
2556
2557static void
2558netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2559{
2560 struct net_device *netdev = adapter->netdev;
2561 struct device *dev = &netdev->dev;
2562
2563 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2564 device_remove_file(dev, &dev_attr_bridged_mode);
2565}
2566
Dhananjay Phadke70f9cf82009-10-13 05:31:45 +00002567static void
2568netxen_create_diag_entries(struct netxen_adapter *adapter)
2569{
2570 struct pci_dev *pdev = adapter->pdev;
2571 struct device *dev;
2572
2573 dev = &pdev->dev;
2574 if (device_create_file(dev, &dev_attr_diag_mode))
2575 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2576 if (device_create_bin_file(dev, &bin_attr_crb))
2577 dev_info(dev, "failed to create crb sysfs entry\n");
2578 if (device_create_bin_file(dev, &bin_attr_mem))
2579 dev_info(dev, "failed to create mem sysfs entry\n");
2580}
2581
2582
2583static void
2584netxen_remove_diag_entries(struct netxen_adapter *adapter)
2585{
2586 struct pci_dev *pdev = adapter->pdev;
2587 struct device *dev = &pdev->dev;
2588
2589 device_remove_file(dev, &dev_attr_diag_mode);
2590 device_remove_bin_file(dev, &bin_attr_crb);
2591 device_remove_bin_file(dev, &bin_attr_mem);
2592}
2593
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002594#ifdef CONFIG_INET
2595
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002596#define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2597
2598static int
2599netxen_destip_supported(struct netxen_adapter *adapter)
2600{
2601 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2602 return 0;
2603
2604 if (adapter->ahw.cut_through)
2605 return 0;
2606
2607 return 1;
2608}
2609
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002610static void
2611netxen_config_indev_addr(struct net_device *dev, unsigned long event)
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002612{
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002613 struct in_device *indev;
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002614 struct netxen_adapter *adapter = netdev_priv(dev);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002615
Amit Kumar Salecha3db76752009-09-09 18:12:37 -07002616 if (!netxen_destip_supported(adapter))
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002617 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002618
2619 indev = in_dev_get(dev);
2620 if (!indev)
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002621 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002622
2623 for_ifa(indev) {
2624 switch (event) {
2625 case NETDEV_UP:
2626 netxen_config_ipaddr(adapter,
2627 ifa->ifa_address, NX_IP_UP);
2628 break;
2629 case NETDEV_DOWN:
2630 netxen_config_ipaddr(adapter,
2631 ifa->ifa_address, NX_IP_DOWN);
2632 break;
2633 default:
2634 break;
2635 }
2636 } endfor_ifa(indev);
2637
2638 in_dev_put(indev);
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002639 return;
2640}
2641
2642static int netxen_netdev_event(struct notifier_block *this,
2643 unsigned long event, void *ptr)
2644{
2645 struct netxen_adapter *adapter;
2646 struct net_device *dev = (struct net_device *)ptr;
2647
2648recheck:
2649 if (dev == NULL)
2650 goto done;
2651
2652 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2653 dev = vlan_dev_real_dev(dev);
2654 goto recheck;
2655 }
2656
2657 if (!is_netxen_netdev(dev))
2658 goto done;
2659
2660 adapter = netdev_priv(dev);
2661
2662 if (!adapter)
2663 goto done;
2664
2665 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2666 goto done;
2667
2668 netxen_config_indev_addr(dev, event);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002669done:
2670 return NOTIFY_DONE;
2671}
2672
2673static int
2674netxen_inetaddr_event(struct notifier_block *this,
2675 unsigned long event, void *ptr)
2676{
2677 struct netxen_adapter *adapter;
2678 struct net_device *dev;
2679
2680 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2681
2682 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2683
2684recheck:
2685 if (dev == NULL || !netif_running(dev))
2686 goto done;
2687
2688 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2689 dev = vlan_dev_real_dev(dev);
2690 goto recheck;
2691 }
2692
2693 if (!is_netxen_netdev(dev))
2694 goto done;
2695
2696 adapter = netdev_priv(dev);
2697
2698 if (!adapter || !netxen_destip_supported(adapter))
2699 goto done;
2700
2701 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2702 goto done;
2703
2704 switch (event) {
2705 case NETDEV_UP:
2706 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2707 break;
2708 case NETDEV_DOWN:
2709 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2710 break;
2711 default:
2712 break;
2713 }
2714
2715done:
2716 return NOTIFY_DONE;
2717}
2718
2719static struct notifier_block netxen_netdev_cb = {
2720 .notifier_call = netxen_netdev_event,
2721};
2722
2723static struct notifier_block netxen_inetaddr_cb = {
2724 .notifier_call = netxen_inetaddr_event,
2725};
Randy Dunlap9feae562009-09-11 12:41:04 +00002726#else
2727static void
2728netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2729{ }
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002730#endif
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002731
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002732static struct pci_driver netxen_driver = {
2733 .name = netxen_nic_driver_name,
2734 .id_table = netxen_pci_tbl,
2735 .probe = netxen_nic_probe,
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002736 .remove = __devexit_p(netxen_nic_remove),
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002737#ifdef CONFIG_PM
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002738 .suspend = netxen_nic_suspend,
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002739 .resume = netxen_nic_resume,
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002740#endif
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002741 .shutdown = netxen_nic_shutdown
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002742};
2743
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002744static int __init netxen_init_module(void)
2745{
Dhananjay Phadkeff4fbd42009-03-13 14:52:06 +00002746 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2747
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002748#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002749 register_netdevice_notifier(&netxen_netdev_cb);
2750 register_inetaddr_notifier(&netxen_inetaddr_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002751#endif
Amit S. Kale184231b2007-01-22 06:52:53 -08002752 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002753}
2754
2755module_init(netxen_init_module);
2756
2757static void __exit netxen_exit_module(void)
2758{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002759 pci_unregister_driver(&netxen_driver);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002760
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002761#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002762 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2763 unregister_netdevice_notifier(&netxen_netdev_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002764#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002765}
2766
2767module_exit(netxen_exit_module);