blob: 1399102fa4bd6ad36c935e9f60a30e3e44925760 [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.
Amit S. Kale3d396eb2006-10-21 15:33:03 -04003 * All rights reserved.
Amit S. Kale80922fb2006-12-04 09:18:00 -08004 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -04005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
Amit S. Kale80922fb2006-12-04 09:18:00 -08009 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040010 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Amit S. Kale80922fb2006-12-04 09:18:00 -080014 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
Amit S. Kale80922fb2006-12-04 09:18:00 -080019 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040020 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
Amit S. Kale80922fb2006-12-04 09:18:00 -080022 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040023 * Contact Information:
24 * info@netxen.com
Dhananjay Phadke5d242f12009-02-25 15:57:56 +000025 * NetXen Inc,
26 * 18922 Forge Drive
27 * Cupertino, CA 95014-0701
Amit S. Kale3d396eb2006-10-21 15:33:03 -040028 *
29 */
30
Amit S. Kalecb8011a2006-11-29 09:00:10 -080031#include <linux/vmalloc.h>
Dhananjay Phadkee98e3352009-04-07 22:50:38 +000032#include <linux/interrupt.h>
Amit S. Kale3d396eb2006-10-21 15:33:03 -040033#include "netxen_nic_hw.h"
34
35#include "netxen_nic.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040036
Jeff Garzik1494a812006-11-07 05:12:16 -050037#include <linux/dma-mapping.h>
Dhananjay Phadke391587c2009-01-14 20:48:11 -080038#include <linux/if_vlan.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030039#include <net/ip.h>
Dhananjay Phadke00977782009-01-16 11:03:25 -080040#include <linux/ipv6.h>
Dhananjay Phadke6598b162009-07-26 20:07:37 +000041#include <linux/inetdevice.h>
Jeff Garzik1494a812006-11-07 05:12:16 -050042
Amit S. Kale3d396eb2006-10-21 15:33:03 -040043MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
44MODULE_LICENSE("GPL");
45MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
46
dhananjay@netxen.comceded322007-07-19 14:41:09 +053047char netxen_nic_driver_name[] = "netxen_nic";
Amit S. Kale3d396eb2006-10-21 15:33:03 -040048static char netxen_nic_driver_string[] = "NetXen Network Driver version "
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
Amit S. Kale3d396eb2006-10-21 15:33:03 -040060/* Local functions to NetXen NIC driver */
61static int __devinit netxen_nic_probe(struct pci_dev *pdev,
Dhananjay Phadke29566402008-07-21 19:44:04 -070062 const struct pci_device_id *ent);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040063static void __devexit netxen_nic_remove(struct pci_dev *pdev);
64static int netxen_nic_open(struct net_device *netdev);
65static int netxen_nic_close(struct net_device *netdev);
Stephen Hemminger613573252009-08-31 19:50:58 +000066static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
67 struct net_device *);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040068static void netxen_tx_timeout(struct net_device *netdev);
Dhananjay Phadkef17443f2009-07-26 20:07:43 +000069static void netxen_reset_task(struct work_struct *work);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +000070static void netxen_fw_poll_work(struct work_struct *work);
71static void netxen_schedule_work(struct netxen_adapter *adapter,
72 work_func_t func, int delay);
73static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070074static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040075#ifdef CONFIG_NET_POLL_CONTROLLER
76static void netxen_nic_poll_controller(struct net_device *netdev);
77#endif
Narender Kumarfa3ce352009-08-24 19:23:28 +000078
79static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
80static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
81
Dhananjay Phadke6a581e92009-09-05 17:43:08 +000082static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
83static int netxen_can_start_firmware(struct netxen_adapter *adapter);
84
Jeff Garzik1494a812006-11-07 05:12:16 -050085static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070086static irqreturn_t netxen_msi_intr(int irq, void *data);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -080087static irqreturn_t netxen_msix_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040088
Amit Kumar Salechad0725e42009-09-05 17:43:09 +000089static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
90
Amit S. Kale3d396eb2006-10-21 15:33:03 -040091/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010092#define ENTRY(device) \
Dhananjay Phadke040dec32008-09-12 06:55:14 -070093 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
Al Viro70081ac2008-06-23 02:04:50 +010094 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
95
Amit S. Kale3d396eb2006-10-21 15:33:03 -040096static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Dhananjay Phadke040dec32008-09-12 06:55:14 -070097 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
98 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
99 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
100 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
101 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
102 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
103 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
104 ENTRY(PCI_DEVICE_ID_NX3031),
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400105 {0,}
106};
107
108MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
109
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700110static uint32_t crb_cmd_producer[4] = {
111 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
112 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
113};
114
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700115void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700116netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000117 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700118{
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000119 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000120
121 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
122 netif_stop_queue(adapter->netdev);
123 smp_mb();
124 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700125}
126
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700127static uint32_t crb_cmd_consumer[4] = {
128 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
129 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
130};
131
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700132static inline void
133netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000134 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700135{
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000136 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700137}
138
Dhananjay Phadke29566402008-07-21 19:44:04 -0700139static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700140 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700141 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
142 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
143 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700144};
145
Dhananjay Phadke29566402008-07-21 19:44:04 -0700146static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
147
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000148static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700149{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000150 struct netxen_adapter *adapter = sds_ring->adapter;
151
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000152 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700153}
154
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000155static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700156{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000157 struct netxen_adapter *adapter = sds_ring->adapter;
158
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000159 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700160
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700161 if (!NETXEN_IS_MSI_FAMILY(adapter))
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000162 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700163}
164
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000165static int
166netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
167{
168 int size = sizeof(struct nx_host_sds_ring) * count;
169
170 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
171
172 return (recv_ctx->sds_rings == NULL);
173}
174
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000175static void
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000176netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
177{
178 if (recv_ctx->sds_rings != NULL)
179 kfree(recv_ctx->sds_rings);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000180
181 recv_ctx->sds_rings = NULL;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000182}
183
184static int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000185netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
186{
187 int ring;
188 struct nx_host_sds_ring *sds_ring;
189 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
190
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000191 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
Dhananjay Phadkef17443f2009-07-26 20:07:43 +0000192 return -ENOMEM;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000193
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000194 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
195 sds_ring = &recv_ctx->sds_rings[ring];
196 netif_napi_add(netdev, &sds_ring->napi,
197 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
198 }
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000199
200 return 0;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000201}
202
203static void
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000204netxen_napi_del(struct netxen_adapter *adapter)
205{
206 int ring;
207 struct nx_host_sds_ring *sds_ring;
208 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
209
210 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
211 sds_ring = &recv_ctx->sds_rings[ring];
212 netif_napi_del(&sds_ring->napi);
213 }
214
215 netxen_free_sds_rings(&adapter->recv_ctx);
216}
217
218static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000219netxen_napi_enable(struct netxen_adapter *adapter)
220{
221 int ring;
222 struct nx_host_sds_ring *sds_ring;
223 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
224
225 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
226 sds_ring = &recv_ctx->sds_rings[ring];
227 napi_enable(&sds_ring->napi);
228 netxen_nic_enable_int(sds_ring);
229 }
230}
231
232static void
233netxen_napi_disable(struct netxen_adapter *adapter)
234{
235 int ring;
236 struct nx_host_sds_ring *sds_ring;
237 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
238
239 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
240 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000241 netxen_nic_disable_int(sds_ring);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000242 napi_synchronize(&sds_ring->napi);
243 napi_disable(&sds_ring->napi);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000244 }
245}
246
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000247static int nx_set_dma_mask(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700248{
249 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000250 uint64_t mask, cmask;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700251
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000252 adapter->pci_using_dac = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700253
Yang Hongyang6afd1422009-04-06 19:01:15 -0700254 mask = DMA_BIT_MASK(32);
Yang Hongyang6afd1422009-04-06 19:01:15 -0700255 cmask = DMA_BIT_MASK(32);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000256
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000257 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000258#ifndef CONFIG_IA64
Yang Hongyang6afd1422009-04-06 19:01:15 -0700259 mask = DMA_BIT_MASK(35);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000260#endif
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000261 } else {
262 mask = DMA_BIT_MASK(39);
263 cmask = mask;
264 }
265
Dhananjay Phadke29566402008-07-21 19:44:04 -0700266 if (pci_set_dma_mask(pdev, mask) == 0 &&
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000267 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700268 adapter->pci_using_dac = 1;
269 return 0;
270 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700271
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000272 return -EIO;
273}
274
275/* Update addressable range if firmware supports it */
276static int
277nx_update_dma_mask(struct netxen_adapter *adapter)
278{
279 int change, shift, err;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000280 uint64_t mask, old_mask, old_cmask;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000281 struct pci_dev *pdev = adapter->pdev;
282
283 change = 0;
284
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000285 shift = NXRD32(adapter, CRB_DMA_SHIFT);
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000286 if (shift > 32)
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000287 return 0;
288
289 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
290 change = 1;
291 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
292 change = 1;
293
294 if (change) {
295 old_mask = pdev->dma_mask;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000296 old_cmask = pdev->dev.coherent_dma_mask;
297
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000298 mask = DMA_BIT_MASK(32+shift);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000299
300 err = pci_set_dma_mask(pdev, mask);
301 if (err)
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000302 goto err_out;
303
304 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
305
306 err = pci_set_consistent_dma_mask(pdev, mask);
307 if (err)
308 goto err_out;
309 }
310 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700311 }
312
Dhananjay Phadke29566402008-07-21 19:44:04 -0700313 return 0;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000314
315err_out:
316 pci_set_dma_mask(pdev, old_mask);
317 pci_set_consistent_dma_mask(pdev, old_cmask);
318 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700319}
320
Dhananjay Phadke29566402008-07-21 19:44:04 -0700321static int
322netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
323{
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800324 u32 val, timeout;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700325
326 if (first_boot == 0x55555555) {
327 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000328 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800329
330 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
331 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700332
333 /* PCI bus master workaround */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000334 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700335 if (!(first_boot & 0x4)) {
336 first_boot |= 0x4;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000337 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
338 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700339 }
340
341 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000342 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700343 if (first_boot != 0x80000f) {
344 /* clear the register for future unloads/loads */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000345 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800346 return -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700347 }
348
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800349 /* Start P2 boot loader */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000350 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
351 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800352 timeout = 0;
353 do {
354 msleep(1);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000355 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800356
357 if (++timeout > 5000)
358 return -EIO;
359
360 } while (val == NETXEN_BDINFO_MAGIC);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700361 }
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800362 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700363}
364
365static void netxen_set_port_mode(struct netxen_adapter *adapter)
366{
367 u32 val, data;
368
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000369 val = adapter->ahw.board_type;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700370 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
371 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
372 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
373 data = NETXEN_PORT_MODE_802_3_AP;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000374 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700375 } else if (port_mode == NETXEN_PORT_MODE_XG) {
376 data = NETXEN_PORT_MODE_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000377 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700378 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
379 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000380 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700381 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
382 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000383 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700384 } else {
385 data = NETXEN_PORT_MODE_AUTO_NEG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000386 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700387 }
388
389 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
390 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
391 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
392 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
393 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
394 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000395 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700396 }
397}
398
Dhananjay Phadke29566402008-07-21 19:44:04 -0700399static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
400{
401 u32 control;
402 int pos;
403
404 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
405 if (pos) {
406 pci_read_config_dword(pdev, pos, &control);
407 if (enable)
408 control |= PCI_MSIX_FLAGS_ENABLE;
409 else
410 control = 0;
411 pci_write_config_dword(pdev, pos, control);
412 }
413}
414
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000415static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700416{
417 int i;
418
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000419 for (i = 0; i < count; i++)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700420 adapter->msix_entries[i].entry = i;
421}
422
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700423static int
424netxen_read_mac_addr(struct netxen_adapter *adapter)
425{
426 int i;
427 unsigned char *p;
428 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700429 struct net_device *netdev = adapter->netdev;
430 struct pci_dev *pdev = adapter->pdev;
431
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700432 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
433 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
434 return -EIO;
435 } else {
436 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
437 return -EIO;
438 }
439
440 p = (unsigned char *)&mac_addr;
441 for (i = 0; i < 6; i++)
442 netdev->dev_addr[i] = *(p + 5 - i);
443
444 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
445
446 /* set station address */
447
Johannes Berge1749612008-10-27 15:59:26 -0700448 if (!is_valid_ether_addr(netdev->perm_addr))
449 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700450
451 return 0;
452}
453
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000454int netxen_nic_set_mac(struct net_device *netdev, void *p)
455{
456 struct netxen_adapter *adapter = netdev_priv(netdev);
457 struct sockaddr *addr = p;
458
459 if (!is_valid_ether_addr(addr->sa_data))
460 return -EINVAL;
461
462 if (netif_running(netdev)) {
463 netif_device_detach(netdev);
464 netxen_napi_disable(adapter);
465 }
466
467 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
468 adapter->macaddr_set(adapter, addr->sa_data);
469
470 if (netif_running(netdev)) {
471 netif_device_attach(netdev);
472 netxen_napi_enable(adapter);
473 }
474 return 0;
475}
476
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800477static void netxen_set_multicast_list(struct net_device *dev)
478{
479 struct netxen_adapter *adapter = netdev_priv(dev);
480
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000481 adapter->set_multi(dev);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800482}
483
484static const struct net_device_ops netxen_netdev_ops = {
485 .ndo_open = netxen_nic_open,
486 .ndo_stop = netxen_nic_close,
487 .ndo_start_xmit = netxen_nic_xmit_frame,
488 .ndo_get_stats = netxen_nic_get_stats,
489 .ndo_validate_addr = eth_validate_addr,
490 .ndo_set_multicast_list = netxen_set_multicast_list,
491 .ndo_set_mac_address = netxen_nic_set_mac,
492 .ndo_change_mtu = netxen_nic_change_mtu,
493 .ndo_tx_timeout = netxen_tx_timeout,
494#ifdef CONFIG_NET_POLL_CONTROLLER
495 .ndo_poll_controller = netxen_nic_poll_controller,
496#endif
497};
498
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800499static void
500netxen_setup_intr(struct netxen_adapter *adapter)
501{
502 struct netxen_legacy_intr_set *legacy_intrp;
503 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000504 int err, num_msix;
505
506 if (adapter->rss_supported) {
507 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
508 MSIX_ENTRIES_PER_ADAPTER : 2;
509 } else
510 num_msix = 1;
511
512 adapter->max_sds_rings = 1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800513
514 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800515
516 if (adapter->ahw.revision_id >= NX_P3_B0)
517 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
518 else
519 legacy_intrp = &legacy_intr[0];
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000520
521 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
522 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
523 legacy_intrp->tgt_status_reg);
524 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
525 legacy_intrp->tgt_mask_reg);
526 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
527 legacy_intrp->pci_int_reg);
528 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
529
530 if (adapter->ahw.revision_id >= NX_P3_B1)
531 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
532 ISR_INT_STATE_REG);
533 else
534 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
535 CRB_INT_VECTOR);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800536
537 netxen_set_msix_bit(pdev, 0);
538
539 if (adapter->msix_supported) {
540
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000541 netxen_init_msix_entries(adapter, num_msix);
542 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
543 if (err == 0) {
544 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
545 netxen_set_msix_bit(pdev, 1);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800546
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000547 if (adapter->rss_supported)
548 adapter->max_sds_rings = num_msix;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800549
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000550 dev_info(&pdev->dev, "using msi-x interrupts\n");
551 return;
552 }
553
554 if (err > 0)
555 pci_disable_msix(pdev);
556
557 /* fall through for msi */
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800558 }
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000559
560 if (use_msi && !pci_enable_msi(pdev)) {
561 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000562 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
563 msi_tgt_status[adapter->ahw.pci_func]);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000564 dev_info(&pdev->dev, "using msi interrupts\n");
Dhananjay Phadkef67f3402009-05-18 21:46:40 -0700565 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000566 return;
567 }
568
569 dev_info(&pdev->dev, "using legacy interrupts\n");
570 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800571}
572
573static void
574netxen_teardown_intr(struct netxen_adapter *adapter)
575{
576 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
577 pci_disable_msix(adapter->pdev);
578 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
579 pci_disable_msi(adapter->pdev);
580}
581
582static void
583netxen_cleanup_pci_map(struct netxen_adapter *adapter)
584{
585 if (adapter->ahw.db_base != NULL)
586 iounmap(adapter->ahw.db_base);
587 if (adapter->ahw.pci_base0 != NULL)
588 iounmap(adapter->ahw.pci_base0);
589 if (adapter->ahw.pci_base1 != NULL)
590 iounmap(adapter->ahw.pci_base1);
591 if (adapter->ahw.pci_base2 != NULL)
592 iounmap(adapter->ahw.pci_base2);
593}
594
595static int
596netxen_setup_pci_map(struct netxen_adapter *adapter)
597{
598 void __iomem *mem_ptr0 = NULL;
599 void __iomem *mem_ptr1 = NULL;
600 void __iomem *mem_ptr2 = NULL;
601 void __iomem *db_ptr = NULL;
602
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800603 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
604
605 struct pci_dev *pdev = adapter->pdev;
606 int pci_func = adapter->ahw.pci_func;
607
608 int err = 0;
609
610 /*
611 * Set the CRB window to invalid. If any register in window 0 is
612 * accessed it should set the window to 0 and then reset it to 1.
613 */
614 adapter->curr_window = 255;
615 adapter->ahw.qdr_sn_window = -1;
616 adapter->ahw.ddr_mn_window = -1;
617
618 /* remap phys address */
619 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
620 mem_len = pci_resource_len(pdev, 0);
621 pci_len0 = 0;
622
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800623 /* 128 Meg of memory */
624 if (mem_len == NETXEN_PCI_128MB_SIZE) {
625 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
626 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
627 SECOND_PAGE_GROUP_SIZE);
628 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
629 THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800630 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
631 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
632 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
633 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800634 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800635
636 mem_ptr0 = pci_ioremap_bar(pdev, 0);
637 if (mem_ptr0 == NULL) {
638 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
639 return -EIO;
640 }
641 pci_len0 = mem_len;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800642
643 adapter->ahw.ddr_mn_window = 0;
644 adapter->ahw.qdr_sn_window = 0;
645
646 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
647 (pci_func * 0x20);
648 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
649 if (pci_func < 4)
650 adapter->ahw.ms_win_crb += (pci_func * 0x20);
651 else
652 adapter->ahw.ms_win_crb +=
653 0xA0 + ((pci_func - 4) * 0x10);
654 } else {
655 return -EIO;
656 }
657
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +0000658 netxen_setup_hwops(adapter);
659
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800660 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
661
662 adapter->ahw.pci_base0 = mem_ptr0;
663 adapter->ahw.pci_len0 = pci_len0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800664 adapter->ahw.pci_base1 = mem_ptr1;
665 adapter->ahw.pci_base2 = mem_ptr2;
666
667 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
668 goto skip_doorbell;
669
670 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
671 db_len = pci_resource_len(pdev, 4);
672
673 if (db_len == 0) {
674 printk(KERN_ERR "%s: doorbell is disabled\n",
675 netxen_nic_driver_name);
676 err = -EIO;
677 goto err_out;
678 }
679
680 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
681 if (!db_ptr) {
682 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
683 netxen_nic_driver_name);
684 err = -EIO;
685 goto err_out;
686 }
687
688skip_doorbell:
689 adapter->ahw.db_base = db_ptr;
690 adapter->ahw.db_len = db_len;
691 return 0;
692
693err_out:
694 netxen_cleanup_pci_map(adapter);
695 return err;
696}
697
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000698static void
699netxen_check_options(struct netxen_adapter *adapter)
700{
701 u32 fw_major, fw_minor, fw_build;
702 char brd_name[NETXEN_MAX_SHORT_NAME];
703 char serial_num[32];
704 int i, offset, val;
705 int *ptr32;
706 struct pci_dev *pdev = adapter->pdev;
707
708 adapter->driver_mismatch = 0;
709
710 ptr32 = (int *)&serial_num;
711 offset = NX_FW_SERIAL_NUM_OFFSET;
712 for (i = 0; i < 8; i++) {
713 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
714 dev_err(&pdev->dev, "error reading board info\n");
715 adapter->driver_mismatch = 1;
716 return;
717 }
718 ptr32[i] = cpu_to_le32(val);
719 offset += sizeof(u32);
720 }
721
722 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
723 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
724 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
725
726 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
727
728 if (adapter->portnum == 0) {
729 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
730
731 printk(KERN_INFO "NetXen %s Board S/N %s Chip rev 0x%x\n",
732 brd_name, serial_num, adapter->ahw.revision_id);
733 }
734
735 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
736 adapter->driver_mismatch = 1;
737 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
738 fw_major, fw_minor, fw_build);
739 return;
740 }
741
742 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
743 i = NXRD32(adapter, NETXEN_SRE_MISC);
744 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
745 }
746
747 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
748 fw_major, fw_minor, fw_build,
749 adapter->ahw.cut_through ? "cut-through" : "legacy");
750
751 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
752 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
753
754 adapter->flags &= ~NETXEN_NIC_LRO_ENABLED;
755
756 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
757 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
758 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
759 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
760 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
761 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
762 }
763
764 adapter->msix_supported = 0;
765 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
766 adapter->msix_supported = !!use_msi_x;
767 adapter->rss_supported = !!use_msi_x;
768 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
769 switch (adapter->ahw.board_type) {
770 case NETXEN_BRDTYPE_P2_SB31_10G:
771 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
772 adapter->msix_supported = !!use_msi_x;
773 adapter->rss_supported = !!use_msi_x;
774 break;
775 default:
776 break;
777 }
778 }
779
780 adapter->num_txd = MAX_CMD_DESCRIPTORS;
781
782 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
783 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
784 adapter->max_rds_rings = 3;
785 } else {
786 adapter->num_lro_rxd = 0;
787 adapter->max_rds_rings = 2;
788 }
789}
790
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800791static int
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000792netxen_start_firmware(struct netxen_adapter *adapter)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800793{
794 int val, err, first_boot;
795 struct pci_dev *pdev = adapter->pdev;
796
Dhananjay Phadke2b9e62e2009-09-05 17:43:11 +0000797 /* required for NX2031 dummy dma */
798 err = nx_set_dma_mask(adapter);
799 if (err)
800 return err;
801
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000802 if (!netxen_can_start_firmware(adapter))
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000803 goto wait_init;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000804
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000805 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800806
807 err = netxen_check_hw_init(adapter, first_boot);
808 if (err) {
809 dev_err(&pdev->dev, "error in init HW init sequence\n");
810 return err;
811 }
812
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000813 netxen_request_firmware(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000814
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000815 err = netxen_need_fw_reset(adapter);
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000816 if (err < 0)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000817 goto err_out;
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000818 if (err == 0)
819 goto wait_init;
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000820
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800821 if (first_boot != 0x55555555) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000822 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800823 netxen_pinit_from_rom(adapter, 0);
824 msleep(1);
825 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000826
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000827 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000828 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
829 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
830
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000831 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
832 netxen_set_port_mode(adapter);
833
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000834 err = netxen_load_firmware(adapter);
835 if (err)
836 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800837
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000838 netxen_release_firmware(adapter);
839
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800840 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
841
842 /* Initialize multicast addr pool owners */
843 val = 0x7654;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000844 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800845 val |= 0x0f000000;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000846 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800847
848 }
849
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000850 err = netxen_init_dummy_dma(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800851 if (err)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000852 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800853
854 /*
855 * Tell the hardware our version number.
856 */
857 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
858 | ((_NETXEN_NIC_LINUX_MINOR << 8))
859 | (_NETXEN_NIC_LINUX_SUBVERSION);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000860 NXWR32(adapter, CRB_DRIVER_VERSION, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800861
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000862 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
863
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000864wait_init:
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800865 /* Handshake with the card before we register the devices. */
866 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
867 if (err) {
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000868 netxen_free_dummy_dma(adapter);
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000869 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800870 }
871
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000872 nx_update_dma_mask(adapter);
873
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000874 netxen_check_options(adapter);
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000875
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800876 return 0;
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000877
878err_out:
879 netxen_release_firmware(adapter);
880 return err;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800881}
882
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000883static int
884netxen_nic_request_irq(struct netxen_adapter *adapter)
885{
886 irq_handler_t handler;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000887 struct nx_host_sds_ring *sds_ring;
888 int err, ring;
889
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000890 unsigned long flags = IRQF_SAMPLE_RANDOM;
891 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000892 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000893
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000894 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
895 handler = netxen_msix_intr;
896 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
897 handler = netxen_msi_intr;
898 else {
899 flags |= IRQF_SHARED;
900 handler = netxen_intr;
901 }
902 adapter->irq = netdev->irq;
903
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000904 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
905 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000906 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000907 err = request_irq(sds_ring->irq, handler,
908 flags, sds_ring->name, sds_ring);
909 if (err)
910 return err;
911 }
912
913 return 0;
914}
915
916static void
917netxen_nic_free_irq(struct netxen_adapter *adapter)
918{
919 int ring;
920 struct nx_host_sds_ring *sds_ring;
921
922 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
923
924 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
925 sds_ring = &recv_ctx->sds_rings[ring];
926 free_irq(sds_ring->irq, sds_ring);
927 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000928}
929
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000930static void
931netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
932{
933 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
934 adapter->coal.normal.data.rx_time_us =
935 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
936 adapter->coal.normal.data.rx_packets =
937 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
938 adapter->coal.normal.data.tx_time_us =
939 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
940 adapter->coal.normal.data.tx_packets =
941 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
942}
943
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000944static int
945netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
946{
947 int err;
948
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000949 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
950 return -EIO;
951
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000952 err = adapter->init_port(adapter, adapter->physical_port);
953 if (err) {
954 printk(KERN_ERR "%s: Failed to initialize port %d\n",
955 netxen_nic_driver_name, adapter->portnum);
956 return err;
957 }
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000958 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
959 adapter->macaddr_set(adapter, netdev->dev_addr);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000960
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000961 adapter->set_multi(netdev);
962 adapter->set_mtu(adapter, netdev->mtu);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000963
964 adapter->ahw.linkup = 0;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000965
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000966 if (adapter->max_sds_rings > 1)
967 netxen_config_rss(adapter, 1);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000968
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000969 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
970 netxen_config_intr_coalesce(adapter);
971
Narender Kumar1bb482f2009-08-23 08:35:09 +0000972 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
973 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
974
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000975 netxen_napi_enable(adapter);
976
Dhananjay Phadke5103c9f2009-04-28 15:29:09 +0000977 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000978 netxen_linkevent_request(adapter, 1);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000979 else
980 netxen_nic_set_link_parameters(adapter);
981
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000982 set_bit(__NX_DEV_UP, &adapter->state);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000983 return 0;
984}
985
986static void
987netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
988{
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000989 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
990 return;
991
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000992 clear_bit(__NX_DEV_UP, &adapter->state);
993
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000994 spin_lock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000995 netif_carrier_off(netdev);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000996 netif_tx_disable(netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000997
998 if (adapter->stop_port)
999 adapter->stop_port(adapter);
1000
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001001 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1002 netxen_p3_free_mac_list(adapter);
1003
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001004 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1005
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +00001006 netxen_napi_disable(adapter);
1007
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001008 netxen_release_tx_buffers(adapter);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001009 spin_unlock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001010}
1011
1012
1013static int
1014netxen_nic_attach(struct netxen_adapter *adapter)
1015{
1016 struct net_device *netdev = adapter->netdev;
1017 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001018 int err, ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001019 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001020 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001021
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001022 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1023 return 0;
1024
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001025 err = netxen_init_firmware(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +00001026 if (err)
1027 return err;
1028
1029 err = netxen_napi_add(adapter, netdev);
1030 if (err)
1031 return err;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001032
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001033 err = netxen_alloc_sw_resources(adapter);
1034 if (err) {
1035 printk(KERN_ERR "%s: Error in setting sw resources\n",
1036 netdev->name);
1037 return err;
1038 }
1039
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001040 err = netxen_alloc_hw_resources(adapter);
1041 if (err) {
1042 printk(KERN_ERR "%s: Error in setting hw resources\n",
1043 netdev->name);
1044 goto err_out_free_sw;
1045 }
1046
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001047 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001048 tx_ring = adapter->tx_ring;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001049 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1050 crb_cmd_producer[adapter->portnum]);
1051 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1052 crb_cmd_consumer[adapter->portnum]);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001053
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001054 tx_ring->producer = 0;
1055 tx_ring->sw_consumer = 0;
1056
1057 netxen_nic_update_cmd_producer(adapter, tx_ring);
1058 netxen_nic_update_cmd_consumer(adapter, tx_ring);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001059 }
1060
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001061 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1062 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1063 netxen_post_rx_buffers(adapter, ring, rds_ring);
1064 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001065
1066 err = netxen_nic_request_irq(adapter);
1067 if (err) {
1068 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1069 netdev->name);
1070 goto err_out_free_rxbuf;
1071 }
1072
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +00001073 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1074 netxen_nic_init_coalesce_defaults(adapter);
1075
Narender Kumarfa3ce352009-08-24 19:23:28 +00001076 netxen_create_sysfs_entries(adapter);
1077
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001078 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1079 return 0;
1080
1081err_out_free_rxbuf:
1082 netxen_release_rx_buffers(adapter);
1083 netxen_free_hw_resources(adapter);
1084err_out_free_sw:
1085 netxen_free_sw_resources(adapter);
1086 return err;
1087}
1088
1089static void
1090netxen_nic_detach(struct netxen_adapter *adapter)
1091{
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001092 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1093 return;
1094
Narender Kumarfa3ce352009-08-24 19:23:28 +00001095 netxen_remove_sysfs_entries(adapter);
1096
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001097 netxen_free_hw_resources(adapter);
Dhananjay Phadkecf981ff2009-07-17 15:27:06 +00001098 netxen_release_rx_buffers(adapter);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +00001099 netxen_nic_free_irq(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +00001100 netxen_napi_del(adapter);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001101 netxen_free_sw_resources(adapter);
1102
1103 adapter->is_up = 0;
1104}
1105
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001106int
1107netxen_nic_reset_context(struct netxen_adapter *adapter)
1108{
1109 int err = 0;
1110 struct net_device *netdev = adapter->netdev;
1111
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001112 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1113 return -EBUSY;
1114
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001115 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1116
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001117 netif_device_detach(netdev);
1118
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001119 if (netif_running(netdev))
1120 netxen_nic_down(adapter, netdev);
1121
1122 netxen_nic_detach(adapter);
1123
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001124 if (netif_running(netdev)) {
1125 err = netxen_nic_attach(adapter);
1126 if (!err)
1127 err = netxen_nic_up(adapter, netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001128
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001129 if (err)
1130 goto done;
1131 }
1132
1133 netif_device_attach(netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001134 }
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001135
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001136done:
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001137 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001138 return err;
1139}
1140
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001141static int
1142netxen_setup_netdev(struct netxen_adapter *adapter,
1143 struct net_device *netdev)
1144{
1145 int err = 0;
1146 struct pci_dev *pdev = adapter->pdev;
1147
1148 adapter->rx_csum = 1;
1149 adapter->mc_enabled = 0;
1150 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1151 adapter->max_mc_count = 38;
1152 else
1153 adapter->max_mc_count = 16;
1154
1155 netdev->netdev_ops = &netxen_netdev_ops;
1156 netdev->watchdog_timeo = 2*HZ;
1157
1158 netxen_nic_change_mtu(netdev, netdev->mtu);
1159
1160 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1161
1162 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1163 netdev->features |= (NETIF_F_GRO);
1164 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1165
1166 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1167 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1168 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1169 }
1170
1171 if (adapter->pci_using_dac) {
1172 netdev->features |= NETIF_F_HIGHDMA;
1173 netdev->vlan_features |= NETIF_F_HIGHDMA;
1174 }
1175
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001176 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1177 netdev->features |= (NETIF_F_HW_VLAN_TX);
1178
Narender Kumar1bb482f2009-08-23 08:35:09 +00001179 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1180 netdev->features |= NETIF_F_LRO;
1181
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001182 netdev->irq = adapter->msix_entries[0].vector;
1183
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001184 INIT_WORK(&adapter->tx_timeout_task, netxen_reset_task);
1185
1186 if (netxen_read_mac_addr(adapter))
1187 dev_warn(&pdev->dev, "failed to read mac addr\n");
1188
1189 netif_carrier_off(netdev);
1190 netif_stop_queue(netdev);
1191
1192 err = register_netdev(netdev);
1193 if (err) {
1194 dev_err(&pdev->dev, "failed to register net device\n");
1195 return err;
1196 }
1197
1198 return 0;
1199}
1200
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001201static int __devinit
1202netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1203{
1204 struct net_device *netdev = NULL;
1205 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001206 int i = 0, err;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001207 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001208 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001209
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001210 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001211 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001212 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001213 return -ENODEV;
1214 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001215
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001216 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
1217 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
1218 "will not be enabled.\n",
1219 NX_P3_A0, NX_P3_B1);
1220 return -ENODEV;
1221 }
1222
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001223 if ((err = pci_enable_device(pdev)))
1224 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001225
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001226 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1227 err = -ENODEV;
1228 goto err_out_disable_pdev;
1229 }
1230
1231 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1232 goto err_out_disable_pdev;
1233
1234 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001235
1236 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1237 if(!netdev) {
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001238 dev_err(&pdev->dev, "failed to allocate net_device\n");
1239 err = -ENOMEM;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001240 goto err_out_free_res;
1241 }
1242
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001243 SET_NETDEV_DEV(netdev, &pdev->dev);
1244
Wang Chen4cf16532008-11-12 23:38:14 -08001245 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001246 adapter->netdev = netdev;
1247 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001248 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001249
1250 revision_id = pdev->revision;
1251 adapter->ahw.revision_id = revision_id;
1252
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001253 rwlock_init(&adapter->adapter_lock);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001254 spin_lock_init(&adapter->tx_clean_lock);
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001255 INIT_LIST_HEAD(&adapter->mac_list);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001256
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001257 err = netxen_setup_pci_map(adapter);
1258 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001259 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001260
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301261 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001262 adapter->portnum = pci_func_id;
1263
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001264 err = netxen_nic_get_board_info(adapter);
1265 if (err) {
1266 dev_err(&pdev->dev, "Error getting board config info.\n");
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001267 goto err_out_iounmap;
1268 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001269
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001270 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001271 switch (adapter->ahw.board_type) {
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001272 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1273 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1274 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001275 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001276 break;
1277 default:
1278 break;
1279 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001280
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001281 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001282 if (err)
1283 goto err_out_iounmap;
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001284
Dhananjay Phadke29566402008-07-21 19:44:04 -07001285 /*
1286 * See if the firmware gave us a virtual-physical port mapping.
1287 */
1288 adapter->physical_port = adapter->portnum;
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001289 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001290 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001291 if (i != 0x55555555)
1292 adapter->physical_port = i;
1293 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001294
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001295 netxen_nic_clear_stats(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001296
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001297 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001298
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001299 err = netxen_setup_netdev(adapter, netdev);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001300 if (err)
Dhananjay Phadke29566402008-07-21 19:44:04 -07001301 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001302
1303 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001304
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001305 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1306
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001307 switch (adapter->ahw.port_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001308 case NETXEN_NIC_GBE:
1309 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1310 adapter->netdev->name);
1311 break;
1312 case NETXEN_NIC_XGBE:
1313 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1314 adapter->netdev->name);
1315 break;
1316 }
1317
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001318 return 0;
1319
Dhananjay Phadke29566402008-07-21 19:44:04 -07001320err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001321 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001322
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001323 netxen_free_dummy_dma(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001324
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001325 nx_decr_dev_ref_cnt(adapter);
1326
Dhananjay Phadke29566402008-07-21 19:44:04 -07001327err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001328 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001329
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001330err_out_free_netdev:
1331 free_netdev(netdev);
1332
1333err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001334 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001335
1336err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001337 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001338 pci_disable_device(pdev);
1339 return err;
1340}
1341
1342static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1343{
1344 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001345 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001346
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001347 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001348 if (adapter == NULL)
1349 return;
1350
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001351 netdev = adapter->netdev;
1352
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001353 netxen_cancel_fw_work(adapter);
1354
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301355 unregister_netdev(netdev);
1356
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001357 cancel_work_sync(&adapter->tx_timeout_task);
1358
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001359 netxen_nic_detach(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001360
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001361 nx_decr_dev_ref_cnt(adapter);
1362
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001363 if (adapter->portnum == 0)
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001364 netxen_free_dummy_dma(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301365
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001366 clear_bit(__NX_RESETTING, &adapter->state);
1367
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001368 netxen_teardown_intr(adapter);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301369
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001370 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001371
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001372 netxen_release_firmware(adapter);
1373
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001374 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301375 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001376 pci_set_drvdata(pdev, NULL);
1377
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001378 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001379}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001380static int __netxen_nic_shutdown(struct pci_dev *pdev)
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001381{
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001382 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1383 struct net_device *netdev = adapter->netdev;
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001384 int retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001385
1386 netif_device_detach(netdev);
1387
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001388 netxen_cancel_fw_work(adapter);
1389
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001390 if (netif_running(netdev))
1391 netxen_nic_down(adapter, 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);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001396
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001397 if (adapter->portnum == 0)
1398 netxen_free_dummy_dma(adapter);
1399
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001400 nx_decr_dev_ref_cnt(adapter);
1401
1402 clear_bit(__NX_RESETTING, &adapter->state);
1403
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001404 retval = pci_save_state(pdev);
1405 if (retval)
1406 return retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001407
1408 if (netxen_nic_wol_supported(adapter)) {
1409 pci_enable_wake(pdev, PCI_D3cold, 1);
1410 pci_enable_wake(pdev, PCI_D3hot, 1);
1411 }
1412
1413 pci_disable_device(pdev);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001414
1415 return 0;
1416}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001417static void netxen_nic_shutdown(struct pci_dev *pdev)
1418{
1419 if (__netxen_nic_shutdown(pdev))
1420 return;
1421}
1422#ifdef CONFIG_PM
1423static int
1424netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1425{
1426 int retval;
1427
1428 retval = __netxen_nic_shutdown(pdev);
1429 if (retval)
1430 return retval;
1431
1432 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1433 return 0;
1434}
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001435
1436static int
1437netxen_nic_resume(struct pci_dev *pdev)
1438{
1439 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1440 struct net_device *netdev = adapter->netdev;
1441 int err;
1442
1443 pci_set_power_state(pdev, PCI_D0);
1444 pci_restore_state(pdev);
1445
1446 err = pci_enable_device(pdev);
1447 if (err)
1448 return err;
1449
1450 adapter->curr_window = 255;
1451
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001452 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001453 if (err) {
1454 dev_err(&pdev->dev, "failed to start firmware\n");
1455 return err;
1456 }
1457
1458 if (netif_running(netdev)) {
1459 err = netxen_nic_attach(adapter);
1460 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001461 goto err_out;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001462
1463 err = netxen_nic_up(adapter, netdev);
1464 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001465 goto err_out_detach;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001466
1467 netif_device_attach(netdev);
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00001468
1469 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001470 }
1471
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001472 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1473
1474err_out_detach:
1475 netxen_nic_detach(adapter);
1476err_out:
1477 nx_decr_dev_ref_cnt(adapter);
1478 return err;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001479}
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001480#endif
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001481
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001482static int netxen_nic_open(struct net_device *netdev)
1483{
Wang Chen4cf16532008-11-12 23:38:14 -08001484 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001485 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001486
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001487 if (adapter->driver_mismatch)
1488 return -EIO;
1489
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001490 err = netxen_nic_attach(adapter);
1491 if (err)
1492 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001493
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001494 err = netxen_nic_up(adapter, netdev);
1495 if (err)
1496 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001497
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001498 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001499
1500 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001501
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001502err_out:
1503 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001504 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001505}
1506
1507/*
1508 * netxen_nic_close - Disables a network interface entry point
1509 */
1510static int netxen_nic_close(struct net_device *netdev)
1511{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001512 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001513
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001514 netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001515 return 0;
1516}
1517
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001518static void
1519netxen_tso_check(struct net_device *netdev,
1520 struct nx_host_tx_ring *tx_ring,
1521 struct cmd_desc_type0 *first_desc,
1522 struct sk_buff *skb)
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001523{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001524 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001525 __be16 protocol = skb->protocol;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001526 u16 flags = 0, vid = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001527 u32 producer;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001528 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001529 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001530 struct vlan_ethhdr *vh;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001531
Harvey Harrison09640e62009-02-01 00:45:17 -08001532 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001533
1534 vh = (struct vlan_ethhdr *)skb->data;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001535 protocol = vh->h_vlan_encapsulated_proto;
1536 flags = FLAGS_VLAN_TAGGED;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001537
1538 } else if (vlan_tx_tag_present(skb)) {
1539
1540 flags = FLAGS_VLAN_OOB;
1541 vid = vlan_tx_tag_get(skb);
1542 netxen_set_tx_vlan_tci(first_desc, vid);
1543 vlan_oob = 1;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001544 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001545
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001546 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1547 skb_shinfo(skb)->gso_size > 0) {
1548
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001549 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1550
1551 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1552 first_desc->total_hdr_length = hdr_len;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001553 if (vlan_oob) {
1554 first_desc->total_hdr_length += VLAN_HLEN;
1555 first_desc->tcp_hdr_offset = VLAN_HLEN;
1556 first_desc->ip_hdr_offset = VLAN_HLEN;
1557 /* Only in case of TSO on vlan device */
1558 flags |= FLAGS_VLAN_TAGGED;
1559 }
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001560
Harvey Harrison09640e62009-02-01 00:45:17 -08001561 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001562 TX_TCP_LSO6 : TX_TCP_LSO;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001563 tso = 1;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001564
1565 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001566 u8 l4proto;
1567
Harvey Harrison09640e62009-02-01 00:45:17 -08001568 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001569 l4proto = ip_hdr(skb)->protocol;
1570
1571 if (l4proto == IPPROTO_TCP)
1572 opcode = TX_TCP_PKT;
1573 else if(l4proto == IPPROTO_UDP)
1574 opcode = TX_UDP_PKT;
Harvey Harrison09640e62009-02-01 00:45:17 -08001575 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001576 l4proto = ipv6_hdr(skb)->nexthdr;
1577
1578 if (l4proto == IPPROTO_TCP)
1579 opcode = TX_TCPV6_PKT;
1580 else if(l4proto == IPPROTO_UDP)
1581 opcode = TX_UDPV6_PKT;
1582 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001583 }
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001584
1585 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1586 first_desc->ip_hdr_offset += skb_network_offset(skb);
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001587 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1588
1589 if (!tso)
1590 return;
1591
1592 /* For LSO, we need to copy the MAC/IP/TCP headers into
1593 * the descriptor ring
1594 */
1595 producer = tx_ring->producer;
1596 copied = 0;
1597 offset = 2;
1598
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001599 if (vlan_oob) {
1600 /* Create a TSO vlan header template for firmware */
1601
1602 hwdesc = &tx_ring->desc_head[producer];
1603 tx_ring->cmd_buf_arr[producer].skb = NULL;
1604
1605 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1606 hdr_len + VLAN_HLEN);
1607
1608 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1609 skb_copy_from_linear_data(skb, vh, 12);
1610 vh->h_vlan_proto = htons(ETH_P_8021Q);
1611 vh->h_vlan_TCI = htons(vid);
1612 skb_copy_from_linear_data_offset(skb, 12,
1613 (char *)vh + 16, copy_len - 16);
1614
Dhananjay Phadke545eb372009-08-05 07:34:07 +00001615 copied = copy_len - VLAN_HLEN;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001616 offset = 0;
1617
1618 producer = get_next_index(producer, tx_ring->num_desc);
1619 }
1620
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001621 while (copied < hdr_len) {
1622
1623 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1624 (hdr_len - copied));
1625
1626 hwdesc = &tx_ring->desc_head[producer];
1627 tx_ring->cmd_buf_arr[producer].skb = NULL;
1628
1629 skb_copy_from_linear_data_offset(skb, copied,
1630 (char *)hwdesc + offset, copy_len);
1631
1632 copied += copy_len;
1633 offset = 0;
1634
1635 producer = get_next_index(producer, tx_ring->num_desc);
1636 }
1637
1638 tx_ring->producer = producer;
1639 barrier();
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001640}
1641
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001642static int
1643netxen_map_tx_skb(struct pci_dev *pdev,
1644 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001645{
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001646 struct netxen_skb_frag *nf;
1647 struct skb_frag_struct *frag;
1648 int i, nr_frags;
1649 dma_addr_t map;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001650
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001651 nr_frags = skb_shinfo(skb)->nr_frags;
1652 nf = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001653
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001654 map = pci_map_single(pdev, skb->data,
1655 skb_headlen(skb), PCI_DMA_TODEVICE);
1656 if (pci_dma_mapping_error(pdev, map))
1657 goto out_err;
1658
1659 nf->dma = map;
1660 nf->length = skb_headlen(skb);
1661
1662 for (i = 0; i < nr_frags; i++) {
1663 frag = &skb_shinfo(skb)->frags[i];
1664 nf = &pbuf->frag_array[i+1];
1665
1666 map = pci_map_page(pdev, frag->page, frag->page_offset,
1667 frag->size, PCI_DMA_TODEVICE);
1668 if (pci_dma_mapping_error(pdev, map))
1669 goto unwind;
1670
1671 nf->dma = map;
1672 nf->length = frag->size;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001673 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001674
1675 return 0;
1676
1677unwind:
Dhananjay Phadkecf503e82009-09-03 13:10:55 +00001678 while (--i >= 0) {
1679 nf = &pbuf->frag_array[i+1];
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001680 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1681 }
1682
1683 nf = &pbuf->frag_array[0];
1684 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1685
1686out_err:
1687 return -ENOMEM;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001688}
1689
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001690static inline void
1691netxen_clear_cmddesc(u64 *desc)
1692{
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001693 desc[0] = 0ULL;
1694 desc[2] = 0ULL;
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001695}
1696
Stephen Hemminger613573252009-08-31 19:50:58 +00001697static netdev_tx_t
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001698netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001699{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001700 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001701 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001702 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001703 struct netxen_skb_frag *buffrag;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001704 struct cmd_desc_type0 *hwdesc, *first_desc;
1705 struct pci_dev *pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001706 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001707
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001708 u32 producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001709 int frag_count, no_of_desc;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001710 u32 num_txd = tx_ring->num_desc;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001711
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001712 frag_count = skb_shinfo(skb)->nr_frags + 1;
1713
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001714 /* 4 fragments per cmd des */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001715 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001716
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001717 if (unlikely(no_of_desc + 2) > netxen_tx_avail(tx_ring)) {
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001718 netif_stop_queue(netdev);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001719 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001720 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001721
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001722 producer = tx_ring->producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001723 pbuf = &tx_ring->cmd_buf_arr[producer];
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001724
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001725 pdev = adapter->pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001726
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001727 if (netxen_map_tx_skb(pdev, skb, pbuf))
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001728 goto drop_packet;
1729
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001730 pbuf->skb = skb;
1731 pbuf->frag_count = frag_count;
1732
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001733 first_desc = hwdesc = &tx_ring->desc_head[producer];
1734 netxen_clear_cmddesc((u64 *)hwdesc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001735
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001736 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1737 netxen_set_tx_port(first_desc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001738
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001739 for (i = 0; i < frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001740
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001741 k = i % 4;
1742
1743 if ((k == 0) && (i > 0)) {
1744 /* move to next desc.*/
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001745 producer = get_next_index(producer, num_txd);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001746 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001747 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001748 tx_ring->cmd_buf_arr[producer].skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001749 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001750
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001751 buffrag = &pbuf->frag_array[i];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001752
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001753 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001754 switch (k) {
1755 case 0:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001756 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001757 break;
1758 case 1:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001759 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001760 break;
1761 case 2:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001762 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001763 break;
1764 case 3:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001765 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001766 break;
1767 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001768 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001769
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001770 tx_ring->producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001771
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001772 netxen_tso_check(netdev, tx_ring, first_desc, skb);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001773
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001774 netxen_nic_update_cmd_producer(adapter, tx_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001775
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001776 adapter->stats.txbytes += skb->len;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001777 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001778
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001779 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001780
1781drop_packet:
1782 adapter->stats.txdropped++;
1783 dev_kfree_skb_any(skb);
1784 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001785}
1786
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001787static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1788{
1789 struct net_device *netdev = adapter->netdev;
1790 uint32_t temp, temp_state, temp_val;
1791 int rv = 0;
1792
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001793 temp = NXRD32(adapter, CRB_TEMP_STATE);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001794
1795 temp_state = nx_get_temp_state(temp);
1796 temp_val = nx_get_temp_val(temp);
1797
1798 if (temp_state == NX_TEMP_PANIC) {
1799 printk(KERN_ALERT
1800 "%s: Device temperature %d degrees C exceeds"
1801 " maximum allowed. Hardware has been shut down.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001802 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001803 rv = 1;
1804 } else if (temp_state == NX_TEMP_WARN) {
1805 if (adapter->temp == NX_TEMP_NORMAL) {
1806 printk(KERN_ALERT
1807 "%s: Device temperature %d degrees C "
1808 "exceeds operating range."
1809 " Immediate action needed.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001810 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001811 }
1812 } else {
1813 if (adapter->temp == NX_TEMP_WARN) {
1814 printk(KERN_INFO
1815 "%s: Device temperature is now %d degrees C"
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001816 " in normal range.\n", netdev->name,
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001817 temp_val);
1818 }
1819 }
1820 adapter->temp = temp_state;
1821 return rv;
1822}
1823
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001824void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001825{
1826 struct net_device *netdev = adapter->netdev;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001827
1828 if (adapter->ahw.linkup && !linkup) {
1829 printk(KERN_INFO "%s: %s NIC Link is down\n",
1830 netxen_nic_driver_name, netdev->name);
1831 adapter->ahw.linkup = 0;
1832 if (netif_running(netdev)) {
1833 netif_carrier_off(netdev);
1834 netif_stop_queue(netdev);
1835 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001836 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001837 } else if (!adapter->ahw.linkup && linkup) {
1838 printk(KERN_INFO "%s: %s NIC Link is up\n",
1839 netxen_nic_driver_name, netdev->name);
1840 adapter->ahw.linkup = 1;
1841 if (netif_running(netdev)) {
1842 netif_carrier_on(netdev);
1843 netif_wake_queue(netdev);
1844 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001845 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001846 }
1847}
1848
1849static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1850{
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001851 u32 val, port, linkup;
1852
1853 port = adapter->physical_port;
1854
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001855 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001856 val = NXRD32(adapter, CRB_XG_STATE_P3);
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001857 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1858 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001859 } else {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001860 val = NXRD32(adapter, CRB_XG_STATE);
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001861 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001862 linkup = (val >> port) & 1;
1863 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001864 val = (val >> port*8) & 0xff;
1865 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001866 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001867 }
1868
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001869 netxen_advert_link_change(adapter, linkup);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001870}
1871
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001872static void netxen_tx_timeout(struct net_device *netdev)
1873{
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001874 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001875
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001876 if (test_bit(__NX_RESETTING, &adapter->state))
1877 return;
1878
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001879 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001880 schedule_work(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001881}
1882
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001883static void netxen_reset_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001884{
Jeff Garzik47906542007-11-23 21:23:36 -05001885 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001886 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001887
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001888 if (!netif_running(adapter->netdev))
1889 return;
1890
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001891 if (test_bit(__NX_RESETTING, &adapter->state))
1892 return;
1893
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001894 netxen_napi_disable(adapter);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001895
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001896 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001897
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001898 netxen_napi_enable(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001899 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001900}
1901
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001902struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1903{
1904 struct netxen_adapter *adapter = netdev_priv(netdev);
1905 struct net_device_stats *stats = &adapter->net_stats;
1906
1907 memset(stats, 0, sizeof(*stats));
1908
Narender Kumar1bb482f2009-08-23 08:35:09 +00001909 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
Dhananjay Phadkee98e3352009-04-07 22:50:38 +00001910 stats->tx_packets = adapter->stats.xmitfinished;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001911 stats->rx_bytes = adapter->stats.rxbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001912 stats->tx_bytes = adapter->stats.txbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001913 stats->rx_dropped = adapter->stats.rxdropped;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001914 stats->tx_dropped = adapter->stats.txdropped;
1915
1916 return stats;
1917}
1918
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001919static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001920{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001921 struct nx_host_sds_ring *sds_ring = data;
1922 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001923 u32 status = 0;
1924
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001925 status = readl(adapter->isr_int_vec);
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001926
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001927 if (!(status & adapter->int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001928 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301929
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001930 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001931 /* check interrupt state machine, to be sure */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001932 status = readl(adapter->crb_int_state_reg);
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001933 if (!ISR_LEGACY_INT_TRIGGERED(status))
1934 return IRQ_NONE;
1935
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001936 } else {
1937 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001938
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001939 our_int = readl(adapter->crb_int_state_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001940
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001941 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001942 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001943 return IRQ_NONE;
1944
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001945 /* claim interrupt */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001946 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
1947
1948 /* clear interrupt */
1949 netxen_nic_disable_int(sds_ring);
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301950 }
1951
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001952 writel(0xffffffff, adapter->tgt_status_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001953 /* read twice to ensure write is flushed */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001954 readl(adapter->isr_int_vec);
1955 readl(adapter->isr_int_vec);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001956
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001957 napi_schedule(&sds_ring->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001958
1959 return IRQ_HANDLED;
1960}
1961
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001962static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001963{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001964 struct nx_host_sds_ring *sds_ring = data;
1965 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001966
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001967 /* clear interrupt */
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001968 writel(0xffffffff, adapter->tgt_status_reg);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001969
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001970 napi_schedule(&sds_ring->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001971 return IRQ_HANDLED;
1972}
1973
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001974static irqreturn_t netxen_msix_intr(int irq, void *data)
1975{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001976 struct nx_host_sds_ring *sds_ring = data;
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001977
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001978 napi_schedule(&sds_ring->napi);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001979 return IRQ_HANDLED;
1980}
1981
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001982static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001983{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001984 struct nx_host_sds_ring *sds_ring =
1985 container_of(napi, struct nx_host_sds_ring, napi);
1986
1987 struct netxen_adapter *adapter = sds_ring->adapter;
1988
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001989 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001990 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001991
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001992 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001993
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001994 work_done = netxen_process_rcv_ring(sds_ring, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001995
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001996 if ((work_done < budget) && tx_complete) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001997 napi_complete(&sds_ring->napi);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001998 if (netif_running(adapter->netdev))
1999 netxen_nic_enable_int(sds_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002000 }
2001
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002002 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002003}
2004
2005#ifdef CONFIG_NET_POLL_CONTROLLER
2006static void netxen_nic_poll_controller(struct net_device *netdev)
2007{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07002008 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002009 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05002010 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002011 enable_irq(adapter->irq);
2012}
2013#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002014
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002015static int
2016nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2017{
2018 int count;
2019 if (netxen_api_lock(adapter))
2020 return -EIO;
2021
2022 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2023
2024 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2025
2026 netxen_api_unlock(adapter);
2027 return count;
2028}
2029
2030static int
2031nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2032{
2033 int count;
2034 if (netxen_api_lock(adapter))
2035 return -EIO;
2036
2037 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2038 WARN_ON(count == 0);
2039
2040 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2041
2042 if (count == 0)
2043 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2044
2045 netxen_api_unlock(adapter);
2046 return count;
2047}
2048
2049static int
2050netxen_can_start_firmware(struct netxen_adapter *adapter)
2051{
2052 int count;
2053 int can_start = 0;
2054
2055 if (netxen_api_lock(adapter))
2056 return 0;
2057
2058 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2059
2060 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2061 count = 0;
2062
2063 if (count == 0) {
2064 can_start = 1;
2065 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2066 }
2067
2068 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2069
2070 netxen_api_unlock(adapter);
2071
2072 return can_start;
2073}
2074
2075static void
2076netxen_schedule_work(struct netxen_adapter *adapter,
2077 work_func_t func, int delay)
2078{
2079 INIT_DELAYED_WORK(&adapter->fw_work, func);
2080 schedule_delayed_work(&adapter->fw_work, delay);
2081}
2082
2083static void
2084netxen_cancel_fw_work(struct netxen_adapter *adapter)
2085{
2086 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2087 msleep(10);
2088
2089 cancel_delayed_work_sync(&adapter->fw_work);
2090}
2091
2092static void
2093netxen_attach_work(struct work_struct *work)
2094{
2095 struct netxen_adapter *adapter = container_of(work,
2096 struct netxen_adapter, fw_work.work);
2097 struct net_device *netdev = adapter->netdev;
2098 int err = 0;
2099
2100 if (netif_running(netdev)) {
2101 err = netxen_nic_attach(adapter);
2102 if (err)
2103 goto done;
2104
2105 err = netxen_nic_up(adapter, netdev);
2106 if (err) {
2107 netxen_nic_detach(adapter);
2108 goto done;
2109 }
2110
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002111 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002112 }
2113
2114 netif_device_attach(netdev);
2115
2116done:
2117 adapter->fw_fail_cnt = 0;
2118 clear_bit(__NX_RESETTING, &adapter->state);
2119 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2120}
2121
2122static void
2123netxen_fwinit_work(struct work_struct *work)
2124{
2125 struct netxen_adapter *adapter = container_of(work,
2126 struct netxen_adapter, fw_work.work);
2127 int dev_state;
2128
2129 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2130
2131 switch (dev_state) {
2132 case NX_DEV_COLD:
2133 case NX_DEV_READY:
2134 netxen_start_firmware(adapter);
2135 netxen_schedule_work(adapter, netxen_attach_work, 0);
2136 return;
2137
2138 case NX_DEV_INITALIZING:
2139 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2140 netxen_schedule_work(adapter,
2141 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2142 return;
2143 }
2144 break;
2145
2146 case NX_DEV_FAILED:
2147 default:
2148 break;
2149 }
2150
2151 nx_incr_dev_ref_cnt(adapter);
2152 clear_bit(__NX_RESETTING, &adapter->state);
2153}
2154
2155static void
2156netxen_detach_work(struct work_struct *work)
2157{
2158 struct netxen_adapter *adapter = container_of(work,
2159 struct netxen_adapter, fw_work.work);
2160 struct net_device *netdev = adapter->netdev;
2161 int ref_cnt, delay;
2162 u32 status;
2163
2164 netif_device_detach(netdev);
2165
2166 if (netif_running(netdev))
2167 netxen_nic_down(adapter, netdev);
2168
2169 netxen_nic_detach(adapter);
2170
2171 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2172
2173 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2174
2175 if (status & NX_RCODE_FATAL_ERROR)
2176 return;
2177
2178 if (adapter->temp == NX_TEMP_PANIC)
2179 return;
2180
2181 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2182
2183 adapter->fw_wait_cnt = 0;
2184 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2185}
2186
2187static int
2188netxen_check_health(struct netxen_adapter *adapter)
2189{
2190 u32 state, heartbit;
2191 struct net_device *netdev = adapter->netdev;
2192
2193 if (netxen_nic_check_temp(adapter))
2194 goto detach;
2195
2196 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2197 if (state == NX_DEV_NEED_RESET)
2198 goto detach;
2199
2200 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2201 return 0;
2202
2203 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2204 if (heartbit != adapter->heartbit) {
2205 adapter->heartbit = heartbit;
2206 adapter->fw_fail_cnt = 0;
2207 return 0;
2208 }
2209
2210 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2211 return 0;
2212
2213 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2214
2215 dev_info(&netdev->dev, "firmware hang detected\n");
2216
2217detach:
2218 if (!test_and_set_bit(__NX_RESETTING, &adapter->state))
2219 netxen_schedule_work(adapter, netxen_detach_work, 0);
2220 return 1;
2221}
2222
2223static void
2224netxen_fw_poll_work(struct work_struct *work)
2225{
2226 struct netxen_adapter *adapter = container_of(work,
2227 struct netxen_adapter, fw_work.work);
2228
2229 if (test_bit(__NX_RESETTING, &adapter->state))
2230 goto reschedule;
2231
2232 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2233 if (!adapter->has_link_events) {
2234
2235 netxen_nic_handle_phy_intr(adapter);
2236
2237 if (adapter->link_changed)
2238 netxen_nic_set_link_parameters(adapter);
2239 }
2240 }
2241
2242 if (netxen_check_health(adapter))
2243 return;
2244
2245reschedule:
2246 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2247}
2248
Narender Kumarfa3ce352009-08-24 19:23:28 +00002249static ssize_t
2250netxen_store_bridged_mode(struct device *dev,
2251 struct device_attribute *attr, const char *buf, size_t len)
2252{
2253 struct net_device *net = to_net_dev(dev);
2254 struct netxen_adapter *adapter = netdev_priv(net);
2255 unsigned long new;
2256 int ret = -EINVAL;
2257
2258 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2259 goto err_out;
2260
2261 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2262 goto err_out;
2263
2264 if (strict_strtoul(buf, 2, &new))
2265 goto err_out;
2266
2267 if (!netxen_config_bridged_mode(adapter, !!new))
2268 ret = len;
2269
2270err_out:
2271 return ret;
2272}
2273
2274static ssize_t
2275netxen_show_bridged_mode(struct device *dev,
2276 struct device_attribute *attr, char *buf)
2277{
2278 struct net_device *net = to_net_dev(dev);
2279 struct netxen_adapter *adapter;
2280 int bridged_mode = 0;
2281
2282 adapter = netdev_priv(net);
2283
2284 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2285 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2286
2287 return sprintf(buf, "%d\n", bridged_mode);
2288}
2289
2290static struct device_attribute dev_attr_bridged_mode = {
2291 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2292 .show = netxen_show_bridged_mode,
2293 .store = netxen_store_bridged_mode,
2294};
2295
2296static void
2297netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2298{
2299 struct net_device *netdev = adapter->netdev;
2300 struct device *dev = &netdev->dev;
2301
2302 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2303 /* bridged_mode control */
2304 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2305 dev_warn(&netdev->dev,
2306 "failed to create bridged_mode sysfs entry\n");
2307 }
2308 }
2309}
2310
2311static void
2312netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2313{
2314 struct net_device *netdev = adapter->netdev;
2315 struct device *dev = &netdev->dev;
2316
2317 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2318 device_remove_file(dev, &dev_attr_bridged_mode);
2319}
2320
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002321#ifdef CONFIG_INET
2322
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002323#define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2324
2325static int
2326netxen_destip_supported(struct netxen_adapter *adapter)
2327{
2328 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2329 return 0;
2330
2331 if (adapter->ahw.cut_through)
2332 return 0;
2333
2334 return 1;
2335}
2336
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002337static void
2338netxen_config_indev_addr(struct net_device *dev, unsigned long event)
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002339{
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002340 struct in_device *indev;
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002341 struct netxen_adapter *adapter = netdev_priv(dev);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002342
Amit Kumar Salecha3db76752009-09-09 18:12:37 -07002343 if (!netxen_destip_supported(adapter))
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002344 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002345
2346 indev = in_dev_get(dev);
2347 if (!indev)
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002348 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002349
2350 for_ifa(indev) {
2351 switch (event) {
2352 case NETDEV_UP:
2353 netxen_config_ipaddr(adapter,
2354 ifa->ifa_address, NX_IP_UP);
2355 break;
2356 case NETDEV_DOWN:
2357 netxen_config_ipaddr(adapter,
2358 ifa->ifa_address, NX_IP_DOWN);
2359 break;
2360 default:
2361 break;
2362 }
2363 } endfor_ifa(indev);
2364
2365 in_dev_put(indev);
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002366 return;
2367}
2368
2369static int netxen_netdev_event(struct notifier_block *this,
2370 unsigned long event, void *ptr)
2371{
2372 struct netxen_adapter *adapter;
2373 struct net_device *dev = (struct net_device *)ptr;
2374
2375recheck:
2376 if (dev == NULL)
2377 goto done;
2378
2379 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2380 dev = vlan_dev_real_dev(dev);
2381 goto recheck;
2382 }
2383
2384 if (!is_netxen_netdev(dev))
2385 goto done;
2386
2387 adapter = netdev_priv(dev);
2388
2389 if (!adapter)
2390 goto done;
2391
2392 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2393 goto done;
2394
2395 netxen_config_indev_addr(dev, event);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002396done:
2397 return NOTIFY_DONE;
2398}
2399
2400static int
2401netxen_inetaddr_event(struct notifier_block *this,
2402 unsigned long event, void *ptr)
2403{
2404 struct netxen_adapter *adapter;
2405 struct net_device *dev;
2406
2407 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2408
2409 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2410
2411recheck:
2412 if (dev == NULL || !netif_running(dev))
2413 goto done;
2414
2415 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2416 dev = vlan_dev_real_dev(dev);
2417 goto recheck;
2418 }
2419
2420 if (!is_netxen_netdev(dev))
2421 goto done;
2422
2423 adapter = netdev_priv(dev);
2424
2425 if (!adapter || !netxen_destip_supported(adapter))
2426 goto done;
2427
2428 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2429 goto done;
2430
2431 switch (event) {
2432 case NETDEV_UP:
2433 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2434 break;
2435 case NETDEV_DOWN:
2436 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2437 break;
2438 default:
2439 break;
2440 }
2441
2442done:
2443 return NOTIFY_DONE;
2444}
2445
2446static struct notifier_block netxen_netdev_cb = {
2447 .notifier_call = netxen_netdev_event,
2448};
2449
2450static struct notifier_block netxen_inetaddr_cb = {
2451 .notifier_call = netxen_inetaddr_event,
2452};
Randy Dunlap9feae562009-09-11 12:41:04 +00002453#else
2454static void
2455netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2456{ }
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002457#endif
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002458
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002459static struct pci_driver netxen_driver = {
2460 .name = netxen_nic_driver_name,
2461 .id_table = netxen_pci_tbl,
2462 .probe = netxen_nic_probe,
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002463 .remove = __devexit_p(netxen_nic_remove),
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002464#ifdef CONFIG_PM
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002465 .suspend = netxen_nic_suspend,
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002466 .resume = netxen_nic_resume,
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002467#endif
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002468 .shutdown = netxen_nic_shutdown
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002469};
2470
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002471static int __init netxen_init_module(void)
2472{
Dhananjay Phadkeff4fbd42009-03-13 14:52:06 +00002473 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2474
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002475#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002476 register_netdevice_notifier(&netxen_netdev_cb);
2477 register_inetaddr_notifier(&netxen_inetaddr_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002478#endif
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002479
Amit S. Kale184231b2007-01-22 06:52:53 -08002480 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002481}
2482
2483module_init(netxen_init_module);
2484
2485static void __exit netxen_exit_module(void)
2486{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002487 pci_unregister_driver(&netxen_driver);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002488
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002489#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002490 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2491 unregister_netdevice_notifier(&netxen_netdev_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002492#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002493}
2494
2495module_exit(netxen_exit_module);