blob: 7038e1baaf4f22676f3bfa534e9d7335fd0f0aa4 [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{
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000119 NXWR32(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
120
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{
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000136 NXWR32(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
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000152 NXWR32(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
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000159 NXWR32(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))
162 adapter->pci_write_immediate(adapter,
163 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700164}
165
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000166static int
167netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
168{
169 int size = sizeof(struct nx_host_sds_ring) * count;
170
171 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
172
173 return (recv_ctx->sds_rings == NULL);
174}
175
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000176static void
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000177netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
178{
179 if (recv_ctx->sds_rings != NULL)
180 kfree(recv_ctx->sds_rings);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000181
182 recv_ctx->sds_rings = NULL;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000183}
184
185static int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000186netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
187{
188 int ring;
189 struct nx_host_sds_ring *sds_ring;
190 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
191
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000192 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
Dhananjay Phadkef17443f2009-07-26 20:07:43 +0000193 return -ENOMEM;
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000194
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000195 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
196 sds_ring = &recv_ctx->sds_rings[ring];
197 netif_napi_add(netdev, &sds_ring->napi,
198 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
199 }
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000200
201 return 0;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000202}
203
204static void
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000205netxen_napi_del(struct netxen_adapter *adapter)
206{
207 int ring;
208 struct nx_host_sds_ring *sds_ring;
209 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
210
211 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
212 sds_ring = &recv_ctx->sds_rings[ring];
213 netif_napi_del(&sds_ring->napi);
214 }
215
216 netxen_free_sds_rings(&adapter->recv_ctx);
217}
218
219static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000220netxen_napi_enable(struct netxen_adapter *adapter)
221{
222 int ring;
223 struct nx_host_sds_ring *sds_ring;
224 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
225
226 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
227 sds_ring = &recv_ctx->sds_rings[ring];
228 napi_enable(&sds_ring->napi);
229 netxen_nic_enable_int(sds_ring);
230 }
231}
232
233static void
234netxen_napi_disable(struct netxen_adapter *adapter)
235{
236 int ring;
237 struct nx_host_sds_ring *sds_ring;
238 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
239
240 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
241 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000242 netxen_nic_disable_int(sds_ring);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000243 napi_synchronize(&sds_ring->napi);
244 napi_disable(&sds_ring->napi);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000245 }
246}
247
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000248static int nx_set_dma_mask(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700249{
250 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000251 uint64_t mask, cmask;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700252
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000253 adapter->pci_using_dac = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700254
Yang Hongyang6afd1422009-04-06 19:01:15 -0700255 mask = DMA_BIT_MASK(32);
Yang Hongyang6afd1422009-04-06 19:01:15 -0700256 cmask = DMA_BIT_MASK(32);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000257
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000258 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000259#ifndef CONFIG_IA64
Yang Hongyang6afd1422009-04-06 19:01:15 -0700260 mask = DMA_BIT_MASK(35);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000261#endif
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000262 } else {
263 mask = DMA_BIT_MASK(39);
264 cmask = mask;
265 }
266
Dhananjay Phadke29566402008-07-21 19:44:04 -0700267 if (pci_set_dma_mask(pdev, mask) == 0 &&
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000268 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700269 adapter->pci_using_dac = 1;
270 return 0;
271 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700272
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000273 return -EIO;
274}
275
276/* Update addressable range if firmware supports it */
277static int
278nx_update_dma_mask(struct netxen_adapter *adapter)
279{
280 int change, shift, err;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000281 uint64_t mask, old_mask, old_cmask;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000282 struct pci_dev *pdev = adapter->pdev;
283
284 change = 0;
285
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000286 shift = NXRD32(adapter, CRB_DMA_SHIFT);
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000287 if (shift > 32)
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000288 return 0;
289
290 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
291 change = 1;
292 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
293 change = 1;
294
295 if (change) {
296 old_mask = pdev->dma_mask;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000297 old_cmask = pdev->dev.coherent_dma_mask;
298
Dhananjay Phadkef60f7852009-08-04 10:39:03 +0000299 mask = DMA_BIT_MASK(32+shift);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000300
301 err = pci_set_dma_mask(pdev, mask);
302 if (err)
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000303 goto err_out;
304
305 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
306
307 err = pci_set_consistent_dma_mask(pdev, mask);
308 if (err)
309 goto err_out;
310 }
311 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700312 }
313
Dhananjay Phadke29566402008-07-21 19:44:04 -0700314 return 0;
Dhananjay Phadke50c643e2009-08-01 21:36:16 +0000315
316err_out:
317 pci_set_dma_mask(pdev, old_mask);
318 pci_set_consistent_dma_mask(pdev, old_cmask);
319 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700320}
321
Dhananjay Phadke24767ab2009-07-27 11:08:00 -0700322static void
323netxen_check_options(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700324{
Dhananjay Phadke24767ab2009-07-27 11:08:00 -0700325 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
326 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
327 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
328 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
329 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
330 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
331 }
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000332
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000333 adapter->msix_supported = 0;
334 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700335 adapter->msix_supported = !!use_msi_x;
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000336 adapter->rss_supported = !!use_msi_x;
337 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
338 switch (adapter->ahw.board_type) {
339 case NETXEN_BRDTYPE_P2_SB31_10G:
340 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000341 adapter->msix_supported = !!use_msi_x;
342 adapter->rss_supported = !!use_msi_x;
343 break;
344 default:
345 break;
346 }
347 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700348
Dhananjay Phadke24767ab2009-07-27 11:08:00 -0700349 adapter->num_txd = MAX_CMD_DESCRIPTORS;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700350
Dhananjay Phadke24767ab2009-07-27 11:08:00 -0700351 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
352 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
353 adapter->max_rds_rings = 3;
354 } else {
355 adapter->num_lro_rxd = 0;
356 adapter->max_rds_rings = 2;
357 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700358}
359
360static int
361netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
362{
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800363 u32 val, timeout;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700364
365 if (first_boot == 0x55555555) {
366 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000367 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800368
369 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
370 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700371
372 /* PCI bus master workaround */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000373 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700374 if (!(first_boot & 0x4)) {
375 first_boot |= 0x4;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000376 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
377 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700378 }
379
380 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000381 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700382 if (first_boot != 0x80000f) {
383 /* clear the register for future unloads/loads */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000384 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800385 return -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700386 }
387
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800388 /* Start P2 boot loader */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000389 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
390 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800391 timeout = 0;
392 do {
393 msleep(1);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000394 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800395
396 if (++timeout > 5000)
397 return -EIO;
398
399 } while (val == NETXEN_BDINFO_MAGIC);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700400 }
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800401 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700402}
403
404static void netxen_set_port_mode(struct netxen_adapter *adapter)
405{
406 u32 val, data;
407
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000408 val = adapter->ahw.board_type;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700409 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
410 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
411 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
412 data = NETXEN_PORT_MODE_802_3_AP;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000413 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700414 } else if (port_mode == NETXEN_PORT_MODE_XG) {
415 data = NETXEN_PORT_MODE_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000416 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700417 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
418 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000419 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700420 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
421 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000422 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700423 } else {
424 data = NETXEN_PORT_MODE_AUTO_NEG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000425 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700426 }
427
428 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
429 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
430 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
431 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
432 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
433 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000434 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700435 }
436}
437
Dhananjay Phadke29566402008-07-21 19:44:04 -0700438static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
439{
440 u32 control;
441 int pos;
442
443 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
444 if (pos) {
445 pci_read_config_dword(pdev, pos, &control);
446 if (enable)
447 control |= PCI_MSIX_FLAGS_ENABLE;
448 else
449 control = 0;
450 pci_write_config_dword(pdev, pos, control);
451 }
452}
453
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000454static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700455{
456 int i;
457
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000458 for (i = 0; i < count; i++)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700459 adapter->msix_entries[i].entry = i;
460}
461
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700462static int
463netxen_read_mac_addr(struct netxen_adapter *adapter)
464{
465 int i;
466 unsigned char *p;
467 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700468 struct net_device *netdev = adapter->netdev;
469 struct pci_dev *pdev = adapter->pdev;
470
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700471 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
472 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
473 return -EIO;
474 } else {
475 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
476 return -EIO;
477 }
478
479 p = (unsigned char *)&mac_addr;
480 for (i = 0; i < 6; i++)
481 netdev->dev_addr[i] = *(p + 5 - i);
482
483 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
484
485 /* set station address */
486
Johannes Berge1749612008-10-27 15:59:26 -0700487 if (!is_valid_ether_addr(netdev->perm_addr))
488 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700489
490 return 0;
491}
492
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000493int netxen_nic_set_mac(struct net_device *netdev, void *p)
494{
495 struct netxen_adapter *adapter = netdev_priv(netdev);
496 struct sockaddr *addr = p;
497
498 if (!is_valid_ether_addr(addr->sa_data))
499 return -EINVAL;
500
501 if (netif_running(netdev)) {
502 netif_device_detach(netdev);
503 netxen_napi_disable(adapter);
504 }
505
506 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
507 adapter->macaddr_set(adapter, addr->sa_data);
508
509 if (netif_running(netdev)) {
510 netif_device_attach(netdev);
511 netxen_napi_enable(adapter);
512 }
513 return 0;
514}
515
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800516static void netxen_set_multicast_list(struct net_device *dev)
517{
518 struct netxen_adapter *adapter = netdev_priv(dev);
519
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000520 adapter->set_multi(dev);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800521}
522
523static const struct net_device_ops netxen_netdev_ops = {
524 .ndo_open = netxen_nic_open,
525 .ndo_stop = netxen_nic_close,
526 .ndo_start_xmit = netxen_nic_xmit_frame,
527 .ndo_get_stats = netxen_nic_get_stats,
528 .ndo_validate_addr = eth_validate_addr,
529 .ndo_set_multicast_list = netxen_set_multicast_list,
530 .ndo_set_mac_address = netxen_nic_set_mac,
531 .ndo_change_mtu = netxen_nic_change_mtu,
532 .ndo_tx_timeout = netxen_tx_timeout,
533#ifdef CONFIG_NET_POLL_CONTROLLER
534 .ndo_poll_controller = netxen_nic_poll_controller,
535#endif
536};
537
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800538static void
539netxen_setup_intr(struct netxen_adapter *adapter)
540{
541 struct netxen_legacy_intr_set *legacy_intrp;
542 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000543 int err, num_msix;
544
545 if (adapter->rss_supported) {
546 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
547 MSIX_ENTRIES_PER_ADAPTER : 2;
548 } else
549 num_msix = 1;
550
551 adapter->max_sds_rings = 1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800552
553 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800554
555 if (adapter->ahw.revision_id >= NX_P3_B0)
556 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
557 else
558 legacy_intrp = &legacy_intr[0];
559 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
560 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
561 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
562 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
563
564 netxen_set_msix_bit(pdev, 0);
565
566 if (adapter->msix_supported) {
567
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000568 netxen_init_msix_entries(adapter, num_msix);
569 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
570 if (err == 0) {
571 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
572 netxen_set_msix_bit(pdev, 1);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800573
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000574 if (adapter->rss_supported)
575 adapter->max_sds_rings = num_msix;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800576
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000577 dev_info(&pdev->dev, "using msi-x interrupts\n");
578 return;
579 }
580
581 if (err > 0)
582 pci_disable_msix(pdev);
583
584 /* fall through for msi */
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800585 }
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000586
587 if (use_msi && !pci_enable_msi(pdev)) {
588 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
589 adapter->msi_tgt_status =
590 msi_tgt_status[adapter->ahw.pci_func];
591 dev_info(&pdev->dev, "using msi interrupts\n");
Dhananjay Phadkef67f3402009-05-18 21:46:40 -0700592 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000593 return;
594 }
595
596 dev_info(&pdev->dev, "using legacy interrupts\n");
597 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800598}
599
600static void
601netxen_teardown_intr(struct netxen_adapter *adapter)
602{
603 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
604 pci_disable_msix(adapter->pdev);
605 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
606 pci_disable_msi(adapter->pdev);
607}
608
609static void
610netxen_cleanup_pci_map(struct netxen_adapter *adapter)
611{
612 if (adapter->ahw.db_base != NULL)
613 iounmap(adapter->ahw.db_base);
614 if (adapter->ahw.pci_base0 != NULL)
615 iounmap(adapter->ahw.pci_base0);
616 if (adapter->ahw.pci_base1 != NULL)
617 iounmap(adapter->ahw.pci_base1);
618 if (adapter->ahw.pci_base2 != NULL)
619 iounmap(adapter->ahw.pci_base2);
620}
621
622static int
623netxen_setup_pci_map(struct netxen_adapter *adapter)
624{
625 void __iomem *mem_ptr0 = NULL;
626 void __iomem *mem_ptr1 = NULL;
627 void __iomem *mem_ptr2 = NULL;
628 void __iomem *db_ptr = NULL;
629
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800630 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
631
632 struct pci_dev *pdev = adapter->pdev;
633 int pci_func = adapter->ahw.pci_func;
634
635 int err = 0;
636
637 /*
638 * Set the CRB window to invalid. If any register in window 0 is
639 * accessed it should set the window to 0 and then reset it to 1.
640 */
641 adapter->curr_window = 255;
642 adapter->ahw.qdr_sn_window = -1;
643 adapter->ahw.ddr_mn_window = -1;
644
645 /* remap phys address */
646 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
647 mem_len = pci_resource_len(pdev, 0);
648 pci_len0 = 0;
649
650 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
651 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
652 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
653 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800654 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
655 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
656 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
657
658 /* 128 Meg of memory */
659 if (mem_len == NETXEN_PCI_128MB_SIZE) {
660 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
661 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
662 SECOND_PAGE_GROUP_SIZE);
663 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
664 THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800665 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
666 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
667 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
668 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800669 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
670 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
671 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
672 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
673 adapter->pci_write_immediate =
674 netxen_nic_pci_write_immediate_2M;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800675 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
676 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
677 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
678
679 mem_ptr0 = pci_ioremap_bar(pdev, 0);
680 if (mem_ptr0 == NULL) {
681 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
682 return -EIO;
683 }
684 pci_len0 = mem_len;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800685
686 adapter->ahw.ddr_mn_window = 0;
687 adapter->ahw.qdr_sn_window = 0;
688
689 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
690 (pci_func * 0x20);
691 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
692 if (pci_func < 4)
693 adapter->ahw.ms_win_crb += (pci_func * 0x20);
694 else
695 adapter->ahw.ms_win_crb +=
696 0xA0 + ((pci_func - 4) * 0x10);
697 } else {
698 return -EIO;
699 }
700
701 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
702
703 adapter->ahw.pci_base0 = mem_ptr0;
704 adapter->ahw.pci_len0 = pci_len0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800705 adapter->ahw.pci_base1 = mem_ptr1;
706 adapter->ahw.pci_base2 = mem_ptr2;
707
708 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
709 goto skip_doorbell;
710
711 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
712 db_len = pci_resource_len(pdev, 4);
713
714 if (db_len == 0) {
715 printk(KERN_ERR "%s: doorbell is disabled\n",
716 netxen_nic_driver_name);
717 err = -EIO;
718 goto err_out;
719 }
720
721 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
722 if (!db_ptr) {
723 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
724 netxen_nic_driver_name);
725 err = -EIO;
726 goto err_out;
727 }
728
729skip_doorbell:
730 adapter->ahw.db_base = db_ptr;
731 adapter->ahw.db_len = db_len;
732 return 0;
733
734err_out:
735 netxen_cleanup_pci_map(adapter);
736 return err;
737}
738
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800739static int
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000740netxen_start_firmware(struct netxen_adapter *adapter)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800741{
742 int val, err, first_boot;
743 struct pci_dev *pdev = adapter->pdev;
744
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000745 if (!netxen_can_start_firmware(adapter))
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000746 goto wait_init;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000747
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000748 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800749
750 err = netxen_check_hw_init(adapter, first_boot);
751 if (err) {
752 dev_err(&pdev->dev, "error in init HW init sequence\n");
753 return err;
754 }
755
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000756 netxen_request_firmware(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000757
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000758 err = netxen_need_fw_reset(adapter);
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000759 if (err < 0)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000760 goto err_out;
Dhananjay Phadke1dbc84a2009-08-23 08:35:10 +0000761 if (err == 0)
762 goto wait_init;
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000763
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800764 if (first_boot != 0x55555555) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000765 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800766 netxen_pinit_from_rom(adapter, 0);
767 msleep(1);
768 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000769
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000770 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000771 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
772 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
773
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000774 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
775 netxen_set_port_mode(adapter);
776
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000777 err = netxen_load_firmware(adapter);
778 if (err)
779 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800780
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000781 netxen_release_firmware(adapter);
782
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800783 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
784
785 /* Initialize multicast addr pool owners */
786 val = 0x7654;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000787 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800788 val |= 0x0f000000;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000789 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800790
791 }
792
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000793 err = netxen_init_dummy_dma(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800794 if (err)
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000795 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800796
797 /*
798 * Tell the hardware our version number.
799 */
800 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
801 | ((_NETXEN_NIC_LINUX_MINOR << 8))
802 | (_NETXEN_NIC_LINUX_SUBVERSION);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000803 NXWR32(adapter, CRB_DRIVER_VERSION, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800804
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000805 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
806
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000807wait_init:
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800808 /* Handshake with the card before we register the devices. */
809 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
810 if (err) {
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000811 netxen_free_dummy_dma(adapter);
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000812 goto err_out;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800813 }
814
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000815 nx_update_dma_mask(adapter);
816
817 netxen_nic_get_firmware_info(adapter);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000818 netxen_check_options(adapter);
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000819
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800820 return 0;
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000821
822err_out:
823 netxen_release_firmware(adapter);
824 return err;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800825}
826
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000827static int
828netxen_nic_request_irq(struct netxen_adapter *adapter)
829{
830 irq_handler_t handler;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000831 struct nx_host_sds_ring *sds_ring;
832 int err, ring;
833
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000834 unsigned long flags = IRQF_SAMPLE_RANDOM;
835 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000836 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000837
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000838 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
839 handler = netxen_msix_intr;
840 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
841 handler = netxen_msi_intr;
842 else {
843 flags |= IRQF_SHARED;
844 handler = netxen_intr;
845 }
846 adapter->irq = netdev->irq;
847
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000848 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
849 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000850 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000851 err = request_irq(sds_ring->irq, handler,
852 flags, sds_ring->name, sds_ring);
853 if (err)
854 return err;
855 }
856
857 return 0;
858}
859
860static void
861netxen_nic_free_irq(struct netxen_adapter *adapter)
862{
863 int ring;
864 struct nx_host_sds_ring *sds_ring;
865
866 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
867
868 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
869 sds_ring = &recv_ctx->sds_rings[ring];
870 free_irq(sds_ring->irq, sds_ring);
871 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000872}
873
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000874static void
875netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
876{
877 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
878 adapter->coal.normal.data.rx_time_us =
879 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
880 adapter->coal.normal.data.rx_packets =
881 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
882 adapter->coal.normal.data.tx_time_us =
883 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
884 adapter->coal.normal.data.tx_packets =
885 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
886}
887
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000888static int
889netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
890{
891 int err;
892
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000893 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
894 return -EIO;
895
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000896 err = adapter->init_port(adapter, adapter->physical_port);
897 if (err) {
898 printk(KERN_ERR "%s: Failed to initialize port %d\n",
899 netxen_nic_driver_name, adapter->portnum);
900 return err;
901 }
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000902 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
903 adapter->macaddr_set(adapter, netdev->dev_addr);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000904
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000905 adapter->set_multi(netdev);
906 adapter->set_mtu(adapter, netdev->mtu);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000907
908 adapter->ahw.linkup = 0;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000909
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000910 if (adapter->max_sds_rings > 1)
911 netxen_config_rss(adapter, 1);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000912
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000913 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
914 netxen_config_intr_coalesce(adapter);
915
Narender Kumar1bb482f2009-08-23 08:35:09 +0000916 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
917 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
918
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000919 netxen_napi_enable(adapter);
920
Dhananjay Phadke5103c9f2009-04-28 15:29:09 +0000921 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000922 netxen_linkevent_request(adapter, 1);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000923 else
924 netxen_nic_set_link_parameters(adapter);
925
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000926 set_bit(__NX_DEV_UP, &adapter->state);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000927 return 0;
928}
929
930static void
931netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
932{
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +0000933 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
934 return;
935
Dhananjay Phadke6a581e92009-09-05 17:43:08 +0000936 clear_bit(__NX_DEV_UP, &adapter->state);
937
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000938 spin_lock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000939 netif_carrier_off(netdev);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000940 netif_tx_disable(netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000941
942 if (adapter->stop_port)
943 adapter->stop_port(adapter);
944
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +0000945 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
946 netxen_p3_free_mac_list(adapter);
947
Amit Kumar Salecha22b57942009-08-23 08:35:12 +0000948 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
949
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000950 netxen_napi_disable(adapter);
951
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000952 netxen_release_tx_buffers(adapter);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000953 spin_unlock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000954}
955
956
957static int
958netxen_nic_attach(struct netxen_adapter *adapter)
959{
960 struct net_device *netdev = adapter->netdev;
961 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000962 int err, ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000963 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000964 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000965
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -0700966 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
967 return 0;
968
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000969 err = netxen_init_firmware(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +0000970 if (err)
971 return err;
972
973 err = netxen_napi_add(adapter, netdev);
974 if (err)
975 return err;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000976
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000977 err = netxen_alloc_sw_resources(adapter);
978 if (err) {
979 printk(KERN_ERR "%s: Error in setting sw resources\n",
980 netdev->name);
981 return err;
982 }
983
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000984 err = netxen_alloc_hw_resources(adapter);
985 if (err) {
986 printk(KERN_ERR "%s: Error in setting hw resources\n",
987 netdev->name);
988 goto err_out_free_sw;
989 }
990
Dhananjay Phadke4f96b982009-07-26 20:07:42 +0000991 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000992 tx_ring = adapter->tx_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000993 tx_ring->crb_cmd_producer = crb_cmd_producer[adapter->portnum];
994 tx_ring->crb_cmd_consumer = crb_cmd_consumer[adapter->portnum];
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000995
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000996 tx_ring->producer = 0;
997 tx_ring->sw_consumer = 0;
998
999 netxen_nic_update_cmd_producer(adapter, tx_ring);
1000 netxen_nic_update_cmd_consumer(adapter, tx_ring);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001001 }
1002
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001003 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1004 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1005 netxen_post_rx_buffers(adapter, ring, rds_ring);
1006 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001007
1008 err = netxen_nic_request_irq(adapter);
1009 if (err) {
1010 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1011 netdev->name);
1012 goto err_out_free_rxbuf;
1013 }
1014
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +00001015 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1016 netxen_nic_init_coalesce_defaults(adapter);
1017
Narender Kumarfa3ce352009-08-24 19:23:28 +00001018 netxen_create_sysfs_entries(adapter);
1019
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001020 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1021 return 0;
1022
1023err_out_free_rxbuf:
1024 netxen_release_rx_buffers(adapter);
1025 netxen_free_hw_resources(adapter);
1026err_out_free_sw:
1027 netxen_free_sw_resources(adapter);
1028 return err;
1029}
1030
1031static void
1032netxen_nic_detach(struct netxen_adapter *adapter)
1033{
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001034 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1035 return;
1036
Narender Kumarfa3ce352009-08-24 19:23:28 +00001037 netxen_remove_sysfs_entries(adapter);
1038
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001039 netxen_free_hw_resources(adapter);
Dhananjay Phadkecf981ff2009-07-17 15:27:06 +00001040 netxen_release_rx_buffers(adapter);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +00001041 netxen_nic_free_irq(adapter);
Dhananjay Phadke232e7d62009-08-13 07:03:01 +00001042 netxen_napi_del(adapter);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001043 netxen_free_sw_resources(adapter);
1044
1045 adapter->is_up = 0;
1046}
1047
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001048int
1049netxen_nic_reset_context(struct netxen_adapter *adapter)
1050{
1051 int err = 0;
1052 struct net_device *netdev = adapter->netdev;
1053
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001054 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1055 return -EBUSY;
1056
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001057 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1058
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001059 netif_device_detach(netdev);
1060
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001061 if (netif_running(netdev))
1062 netxen_nic_down(adapter, netdev);
1063
1064 netxen_nic_detach(adapter);
1065
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001066 if (netif_running(netdev)) {
1067 err = netxen_nic_attach(adapter);
1068 if (!err)
1069 err = netxen_nic_up(adapter, netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001070
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001071 if (err)
1072 goto done;
1073 }
1074
1075 netif_device_attach(netdev);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001076 }
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001077
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001078done:
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001079 clear_bit(__NX_RESETTING, &adapter->state);
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001080 return err;
1081}
1082
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001083static int
1084netxen_setup_netdev(struct netxen_adapter *adapter,
1085 struct net_device *netdev)
1086{
1087 int err = 0;
1088 struct pci_dev *pdev = adapter->pdev;
1089
1090 adapter->rx_csum = 1;
1091 adapter->mc_enabled = 0;
1092 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1093 adapter->max_mc_count = 38;
1094 else
1095 adapter->max_mc_count = 16;
1096
1097 netdev->netdev_ops = &netxen_netdev_ops;
1098 netdev->watchdog_timeo = 2*HZ;
1099
1100 netxen_nic_change_mtu(netdev, netdev->mtu);
1101
1102 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1103
1104 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1105 netdev->features |= (NETIF_F_GRO);
1106 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1107
1108 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1109 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1110 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1111 }
1112
1113 if (adapter->pci_using_dac) {
1114 netdev->features |= NETIF_F_HIGHDMA;
1115 netdev->vlan_features |= NETIF_F_HIGHDMA;
1116 }
1117
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001118 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1119 netdev->features |= (NETIF_F_HW_VLAN_TX);
1120
Narender Kumar1bb482f2009-08-23 08:35:09 +00001121 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1122 netdev->features |= NETIF_F_LRO;
1123
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001124 netdev->irq = adapter->msix_entries[0].vector;
1125
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001126 INIT_WORK(&adapter->tx_timeout_task, netxen_reset_task);
1127
1128 if (netxen_read_mac_addr(adapter))
1129 dev_warn(&pdev->dev, "failed to read mac addr\n");
1130
1131 netif_carrier_off(netdev);
1132 netif_stop_queue(netdev);
1133
1134 err = register_netdev(netdev);
1135 if (err) {
1136 dev_err(&pdev->dev, "failed to register net device\n");
1137 return err;
1138 }
1139
1140 return 0;
1141}
1142
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001143static int __devinit
1144netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1145{
1146 struct net_device *netdev = NULL;
1147 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001148 int i = 0, err;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001149 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001150 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001151
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001152 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001153 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001154 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001155 return -ENODEV;
1156 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001157
Dhananjay Phadke11d89d62008-08-08 00:08:45 -07001158 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
1159 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
1160 "will not be enabled.\n",
1161 NX_P3_A0, NX_P3_B1);
1162 return -ENODEV;
1163 }
1164
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001165 if ((err = pci_enable_device(pdev)))
1166 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001167
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001168 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1169 err = -ENODEV;
1170 goto err_out_disable_pdev;
1171 }
1172
1173 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1174 goto err_out_disable_pdev;
1175
1176 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001177
1178 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1179 if(!netdev) {
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001180 dev_err(&pdev->dev, "failed to allocate net_device\n");
1181 err = -ENOMEM;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001182 goto err_out_free_res;
1183 }
1184
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001185 SET_NETDEV_DEV(netdev, &pdev->dev);
1186
Wang Chen4cf16532008-11-12 23:38:14 -08001187 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001188 adapter->netdev = netdev;
1189 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001190 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001191
1192 revision_id = pdev->revision;
1193 adapter->ahw.revision_id = revision_id;
1194
Dhananjay Phadke50c643e2009-08-01 21:36:16 +00001195 err = nx_set_dma_mask(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001196 if (err)
1197 goto err_out_free_netdev;
1198
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001199 rwlock_init(&adapter->adapter_lock);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001200 spin_lock_init(&adapter->tx_clean_lock);
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001201 INIT_LIST_HEAD(&adapter->mac_list);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001202
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001203 err = netxen_setup_pci_map(adapter);
1204 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001205 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001206
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301207 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001208 adapter->portnum = pci_func_id;
1209
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001210 err = netxen_nic_get_board_info(adapter);
1211 if (err) {
1212 dev_err(&pdev->dev, "Error getting board config info.\n");
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001213 goto err_out_iounmap;
1214 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001215
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001216 netxen_initialize_adapter_ops(adapter);
1217
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001218 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001219 switch (adapter->ahw.board_type) {
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001220 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1221 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1222 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001223 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001224 break;
1225 default:
1226 break;
1227 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001228
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001229 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001230 if (err)
1231 goto err_out_iounmap;
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001232
Dhananjay Phadke29566402008-07-21 19:44:04 -07001233 /*
1234 * See if the firmware gave us a virtual-physical port mapping.
1235 */
1236 adapter->physical_port = adapter->portnum;
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001237 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001238 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001239 if (i != 0x55555555)
1240 adapter->physical_port = i;
1241 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001242
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001243 netxen_nic_clear_stats(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001244
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001245 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001246
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001247 err = netxen_setup_netdev(adapter, netdev);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001248 if (err)
Dhananjay Phadke29566402008-07-21 19:44:04 -07001249 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001250
1251 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001252
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001253 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1254
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001255 switch (adapter->ahw.port_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001256 case NETXEN_NIC_GBE:
1257 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1258 adapter->netdev->name);
1259 break;
1260 case NETXEN_NIC_XGBE:
1261 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1262 adapter->netdev->name);
1263 break;
1264 }
1265
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001266 return 0;
1267
Dhananjay Phadke29566402008-07-21 19:44:04 -07001268err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001269 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001270
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001271 netxen_free_dummy_dma(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001272
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001273 nx_decr_dev_ref_cnt(adapter);
1274
Dhananjay Phadke29566402008-07-21 19:44:04 -07001275err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001276 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001277
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001278err_out_free_netdev:
1279 free_netdev(netdev);
1280
1281err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001282 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001283
1284err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001285 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001286 pci_disable_device(pdev);
1287 return err;
1288}
1289
1290static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1291{
1292 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001293 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001294
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001295 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001296 if (adapter == NULL)
1297 return;
1298
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001299 netdev = adapter->netdev;
1300
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001301 netxen_cancel_fw_work(adapter);
1302
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301303 unregister_netdev(netdev);
1304
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001305 cancel_work_sync(&adapter->tx_timeout_task);
1306
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001307 netxen_nic_detach(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001308
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001309 nx_decr_dev_ref_cnt(adapter);
1310
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001311 if (adapter->portnum == 0)
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001312 netxen_free_dummy_dma(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301313
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001314 clear_bit(__NX_RESETTING, &adapter->state);
1315
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001316 netxen_teardown_intr(adapter);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301317
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001318 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001319
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001320 netxen_release_firmware(adapter);
1321
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001322 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301323 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001324 pci_set_drvdata(pdev, NULL);
1325
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001326 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001327}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001328static int __netxen_nic_shutdown(struct pci_dev *pdev)
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001329{
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001330 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1331 struct net_device *netdev = adapter->netdev;
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001332 int retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001333
1334 netif_device_detach(netdev);
1335
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001336 netxen_cancel_fw_work(adapter);
1337
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001338 if (netif_running(netdev))
1339 netxen_nic_down(adapter, netdev);
1340
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001341 cancel_work_sync(&adapter->tx_timeout_task);
1342
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001343 netxen_nic_detach(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001344
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001345 if (adapter->portnum == 0)
1346 netxen_free_dummy_dma(adapter);
1347
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001348 nx_decr_dev_ref_cnt(adapter);
1349
1350 clear_bit(__NX_RESETTING, &adapter->state);
1351
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001352 retval = pci_save_state(pdev);
1353 if (retval)
1354 return retval;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001355
1356 if (netxen_nic_wol_supported(adapter)) {
1357 pci_enable_wake(pdev, PCI_D3cold, 1);
1358 pci_enable_wake(pdev, PCI_D3hot, 1);
1359 }
1360
1361 pci_disable_device(pdev);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001362
1363 return 0;
1364}
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00001365static void netxen_nic_shutdown(struct pci_dev *pdev)
1366{
1367 if (__netxen_nic_shutdown(pdev))
1368 return;
1369}
1370#ifdef CONFIG_PM
1371static int
1372netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1373{
1374 int retval;
1375
1376 retval = __netxen_nic_shutdown(pdev);
1377 if (retval)
1378 return retval;
1379
1380 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1381 return 0;
1382}
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001383
1384static int
1385netxen_nic_resume(struct pci_dev *pdev)
1386{
1387 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1388 struct net_device *netdev = adapter->netdev;
1389 int err;
1390
1391 pci_set_power_state(pdev, PCI_D0);
1392 pci_restore_state(pdev);
1393
1394 err = pci_enable_device(pdev);
1395 if (err)
1396 return err;
1397
1398 adapter->curr_window = 255;
1399
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001400 err = netxen_start_firmware(adapter);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001401 if (err) {
1402 dev_err(&pdev->dev, "failed to start firmware\n");
1403 return err;
1404 }
1405
1406 if (netif_running(netdev)) {
1407 err = netxen_nic_attach(adapter);
1408 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001409 goto err_out;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001410
1411 err = netxen_nic_up(adapter, netdev);
1412 if (err)
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001413 goto err_out_detach;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001414
1415 netif_device_attach(netdev);
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00001416
1417 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001418 }
1419
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001420 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1421
1422err_out_detach:
1423 netxen_nic_detach(adapter);
1424err_out:
1425 nx_decr_dev_ref_cnt(adapter);
1426 return err;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001427}
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001428#endif
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001429
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001430static int netxen_nic_open(struct net_device *netdev)
1431{
Wang Chen4cf16532008-11-12 23:38:14 -08001432 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001433 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001434
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001435 if (adapter->driver_mismatch)
1436 return -EIO;
1437
Amit Kumar Salecha7042cd82009-07-27 11:15:54 -07001438 err = netxen_nic_attach(adapter);
1439 if (err)
1440 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001441
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001442 err = netxen_nic_up(adapter, netdev);
1443 if (err)
1444 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001445
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001446 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001447
1448 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001449
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001450err_out:
1451 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001452 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001453}
1454
1455/*
1456 * netxen_nic_close - Disables a network interface entry point
1457 */
1458static int netxen_nic_close(struct net_device *netdev)
1459{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001460 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001461
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001462 netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001463 return 0;
1464}
1465
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001466static void
1467netxen_tso_check(struct net_device *netdev,
1468 struct nx_host_tx_ring *tx_ring,
1469 struct cmd_desc_type0 *first_desc,
1470 struct sk_buff *skb)
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001471{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001472 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001473 __be16 protocol = skb->protocol;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001474 u16 flags = 0, vid = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001475 u32 producer;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001476 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001477 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001478 struct vlan_ethhdr *vh;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001479
Harvey Harrison09640e62009-02-01 00:45:17 -08001480 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001481
1482 vh = (struct vlan_ethhdr *)skb->data;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001483 protocol = vh->h_vlan_encapsulated_proto;
1484 flags = FLAGS_VLAN_TAGGED;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001485
1486 } else if (vlan_tx_tag_present(skb)) {
1487
1488 flags = FLAGS_VLAN_OOB;
1489 vid = vlan_tx_tag_get(skb);
1490 netxen_set_tx_vlan_tci(first_desc, vid);
1491 vlan_oob = 1;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001492 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001493
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001494 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1495 skb_shinfo(skb)->gso_size > 0) {
1496
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001497 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1498
1499 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1500 first_desc->total_hdr_length = hdr_len;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001501 if (vlan_oob) {
1502 first_desc->total_hdr_length += VLAN_HLEN;
1503 first_desc->tcp_hdr_offset = VLAN_HLEN;
1504 first_desc->ip_hdr_offset = VLAN_HLEN;
1505 /* Only in case of TSO on vlan device */
1506 flags |= FLAGS_VLAN_TAGGED;
1507 }
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001508
Harvey Harrison09640e62009-02-01 00:45:17 -08001509 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001510 TX_TCP_LSO6 : TX_TCP_LSO;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001511 tso = 1;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001512
1513 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001514 u8 l4proto;
1515
Harvey Harrison09640e62009-02-01 00:45:17 -08001516 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001517 l4proto = ip_hdr(skb)->protocol;
1518
1519 if (l4proto == IPPROTO_TCP)
1520 opcode = TX_TCP_PKT;
1521 else if(l4proto == IPPROTO_UDP)
1522 opcode = TX_UDP_PKT;
Harvey Harrison09640e62009-02-01 00:45:17 -08001523 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001524 l4proto = ipv6_hdr(skb)->nexthdr;
1525
1526 if (l4proto == IPPROTO_TCP)
1527 opcode = TX_TCPV6_PKT;
1528 else if(l4proto == IPPROTO_UDP)
1529 opcode = TX_UDPV6_PKT;
1530 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001531 }
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001532
1533 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1534 first_desc->ip_hdr_offset += skb_network_offset(skb);
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001535 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1536
1537 if (!tso)
1538 return;
1539
1540 /* For LSO, we need to copy the MAC/IP/TCP headers into
1541 * the descriptor ring
1542 */
1543 producer = tx_ring->producer;
1544 copied = 0;
1545 offset = 2;
1546
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001547 if (vlan_oob) {
1548 /* Create a TSO vlan header template for firmware */
1549
1550 hwdesc = &tx_ring->desc_head[producer];
1551 tx_ring->cmd_buf_arr[producer].skb = NULL;
1552
1553 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1554 hdr_len + VLAN_HLEN);
1555
1556 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1557 skb_copy_from_linear_data(skb, vh, 12);
1558 vh->h_vlan_proto = htons(ETH_P_8021Q);
1559 vh->h_vlan_TCI = htons(vid);
1560 skb_copy_from_linear_data_offset(skb, 12,
1561 (char *)vh + 16, copy_len - 16);
1562
Dhananjay Phadke545eb372009-08-05 07:34:07 +00001563 copied = copy_len - VLAN_HLEN;
Dhananjay Phadke028afe72009-07-26 20:07:45 +00001564 offset = 0;
1565
1566 producer = get_next_index(producer, tx_ring->num_desc);
1567 }
1568
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001569 while (copied < hdr_len) {
1570
1571 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1572 (hdr_len - copied));
1573
1574 hwdesc = &tx_ring->desc_head[producer];
1575 tx_ring->cmd_buf_arr[producer].skb = NULL;
1576
1577 skb_copy_from_linear_data_offset(skb, copied,
1578 (char *)hwdesc + offset, copy_len);
1579
1580 copied += copy_len;
1581 offset = 0;
1582
1583 producer = get_next_index(producer, tx_ring->num_desc);
1584 }
1585
1586 tx_ring->producer = producer;
1587 barrier();
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001588}
1589
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001590static int
1591netxen_map_tx_skb(struct pci_dev *pdev,
1592 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001593{
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001594 struct netxen_skb_frag *nf;
1595 struct skb_frag_struct *frag;
1596 int i, nr_frags;
1597 dma_addr_t map;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001598
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001599 nr_frags = skb_shinfo(skb)->nr_frags;
1600 nf = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001601
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001602 map = pci_map_single(pdev, skb->data,
1603 skb_headlen(skb), PCI_DMA_TODEVICE);
1604 if (pci_dma_mapping_error(pdev, map))
1605 goto out_err;
1606
1607 nf->dma = map;
1608 nf->length = skb_headlen(skb);
1609
1610 for (i = 0; i < nr_frags; i++) {
1611 frag = &skb_shinfo(skb)->frags[i];
1612 nf = &pbuf->frag_array[i+1];
1613
1614 map = pci_map_page(pdev, frag->page, frag->page_offset,
1615 frag->size, PCI_DMA_TODEVICE);
1616 if (pci_dma_mapping_error(pdev, map))
1617 goto unwind;
1618
1619 nf->dma = map;
1620 nf->length = frag->size;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001621 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001622
1623 return 0;
1624
1625unwind:
Dhananjay Phadkecf503e82009-09-03 13:10:55 +00001626 while (--i >= 0) {
1627 nf = &pbuf->frag_array[i+1];
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001628 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1629 }
1630
1631 nf = &pbuf->frag_array[0];
1632 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1633
1634out_err:
1635 return -ENOMEM;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001636}
1637
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001638static inline void
1639netxen_clear_cmddesc(u64 *desc)
1640{
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001641 desc[0] = 0ULL;
1642 desc[2] = 0ULL;
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001643}
1644
Stephen Hemminger613573252009-08-31 19:50:58 +00001645static netdev_tx_t
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001646netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001647{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001648 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001649 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001650 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001651 struct netxen_skb_frag *buffrag;
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001652 struct cmd_desc_type0 *hwdesc, *first_desc;
1653 struct pci_dev *pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001654 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001655
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001656 u32 producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001657 int frag_count, no_of_desc;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001658 u32 num_txd = tx_ring->num_desc;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001659
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001660 frag_count = skb_shinfo(skb)->nr_frags + 1;
1661
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001662 /* 4 fragments per cmd des */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001663 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001664
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001665 if (unlikely(no_of_desc + 2) > netxen_tx_avail(tx_ring)) {
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001666 netif_stop_queue(netdev);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001667 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001668 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001669
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001670 producer = tx_ring->producer;
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001671 pbuf = &tx_ring->cmd_buf_arr[producer];
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001672
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001673 pdev = adapter->pdev;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001674
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001675 if (netxen_map_tx_skb(pdev, skb, pbuf))
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001676 goto drop_packet;
1677
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001678 pbuf->skb = skb;
1679 pbuf->frag_count = frag_count;
1680
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001681 first_desc = hwdesc = &tx_ring->desc_head[producer];
1682 netxen_clear_cmddesc((u64 *)hwdesc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001683
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001684 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1685 netxen_set_tx_port(first_desc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001686
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001687 for (i = 0; i < frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001688
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001689 k = i % 4;
1690
1691 if ((k == 0) && (i > 0)) {
1692 /* move to next desc.*/
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001693 producer = get_next_index(producer, num_txd);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001694 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001695 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001696 tx_ring->cmd_buf_arr[producer].skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001697 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001698
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001699 buffrag = &pbuf->frag_array[i];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001700
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001701 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001702 switch (k) {
1703 case 0:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001704 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001705 break;
1706 case 1:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001707 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001708 break;
1709 case 2:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001710 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001711 break;
1712 case 3:
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001713 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001714 break;
1715 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001716 }
Dhananjay Phadkece644ed2009-08-23 08:35:11 +00001717
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001718 tx_ring->producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001719
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001720 netxen_tso_check(netdev, tx_ring, first_desc, skb);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001721
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001722 netxen_nic_update_cmd_producer(adapter, tx_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001723
Dhananjay Phadke1bcfd792009-07-26 20:07:40 +00001724 adapter->stats.txbytes += skb->len;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001725 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001726
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001727 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001728
1729drop_packet:
1730 adapter->stats.txdropped++;
1731 dev_kfree_skb_any(skb);
1732 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001733}
1734
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001735static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1736{
1737 struct net_device *netdev = adapter->netdev;
1738 uint32_t temp, temp_state, temp_val;
1739 int rv = 0;
1740
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001741 temp = NXRD32(adapter, CRB_TEMP_STATE);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001742
1743 temp_state = nx_get_temp_state(temp);
1744 temp_val = nx_get_temp_val(temp);
1745
1746 if (temp_state == NX_TEMP_PANIC) {
1747 printk(KERN_ALERT
1748 "%s: Device temperature %d degrees C exceeds"
1749 " maximum allowed. Hardware has been shut down.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001750 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001751 rv = 1;
1752 } else if (temp_state == NX_TEMP_WARN) {
1753 if (adapter->temp == NX_TEMP_NORMAL) {
1754 printk(KERN_ALERT
1755 "%s: Device temperature %d degrees C "
1756 "exceeds operating range."
1757 " Immediate action needed.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001758 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001759 }
1760 } else {
1761 if (adapter->temp == NX_TEMP_WARN) {
1762 printk(KERN_INFO
1763 "%s: Device temperature is now %d degrees C"
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001764 " in normal range.\n", netdev->name,
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001765 temp_val);
1766 }
1767 }
1768 adapter->temp = temp_state;
1769 return rv;
1770}
1771
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001772void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001773{
1774 struct net_device *netdev = adapter->netdev;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001775
1776 if (adapter->ahw.linkup && !linkup) {
1777 printk(KERN_INFO "%s: %s NIC Link is down\n",
1778 netxen_nic_driver_name, netdev->name);
1779 adapter->ahw.linkup = 0;
1780 if (netif_running(netdev)) {
1781 netif_carrier_off(netdev);
1782 netif_stop_queue(netdev);
1783 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001784 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001785 } else if (!adapter->ahw.linkup && linkup) {
1786 printk(KERN_INFO "%s: %s NIC Link is up\n",
1787 netxen_nic_driver_name, netdev->name);
1788 adapter->ahw.linkup = 1;
1789 if (netif_running(netdev)) {
1790 netif_carrier_on(netdev);
1791 netif_wake_queue(netdev);
1792 }
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001793 adapter->link_changed = !adapter->has_link_events;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001794 }
1795}
1796
1797static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1798{
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001799 u32 val, port, linkup;
1800
1801 port = adapter->physical_port;
1802
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001803 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001804 val = NXRD32(adapter, CRB_XG_STATE_P3);
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001805 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1806 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001807 } else {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001808 val = NXRD32(adapter, CRB_XG_STATE);
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001809 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001810 linkup = (val >> port) & 1;
1811 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001812 val = (val >> port*8) & 0xff;
1813 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001814 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001815 }
1816
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001817 netxen_advert_link_change(adapter, linkup);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001818}
1819
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001820static void netxen_tx_timeout(struct net_device *netdev)
1821{
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001822 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001823
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001824 if (test_bit(__NX_RESETTING, &adapter->state))
1825 return;
1826
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001827 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechae424fa92009-08-13 07:03:00 +00001828 schedule_work(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001829}
1830
Dhananjay Phadkef17443f2009-07-26 20:07:43 +00001831static void netxen_reset_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001832{
Jeff Garzik47906542007-11-23 21:23:36 -05001833 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001834 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001835
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001836 if (!netif_running(adapter->netdev))
1837 return;
1838
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001839 if (test_bit(__NX_RESETTING, &adapter->state))
1840 return;
1841
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001842 netxen_napi_disable(adapter);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001843
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001844 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001845
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001846 netxen_napi_enable(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001847 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001848}
1849
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001850struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1851{
1852 struct netxen_adapter *adapter = netdev_priv(netdev);
1853 struct net_device_stats *stats = &adapter->net_stats;
1854
1855 memset(stats, 0, sizeof(*stats));
1856
Narender Kumar1bb482f2009-08-23 08:35:09 +00001857 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
Dhananjay Phadkee98e3352009-04-07 22:50:38 +00001858 stats->tx_packets = adapter->stats.xmitfinished;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001859 stats->rx_bytes = adapter->stats.rxbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001860 stats->tx_bytes = adapter->stats.txbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001861 stats->rx_dropped = adapter->stats.rxdropped;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001862 stats->tx_dropped = adapter->stats.txdropped;
1863
1864 return stats;
1865}
1866
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001867static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001868{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001869 struct nx_host_sds_ring *sds_ring = data;
1870 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001871 u32 status = 0;
1872
1873 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1874
1875 if (!(status & adapter->legacy_intr.int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001876 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301877
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001878 if (adapter->ahw.revision_id >= NX_P3_B1) {
1879 /* check interrupt state machine, to be sure */
1880 status = adapter->pci_read_immediate(adapter,
1881 ISR_INT_STATE_REG);
1882 if (!ISR_LEGACY_INT_TRIGGERED(status))
1883 return IRQ_NONE;
1884
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001885 } else {
1886 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001887
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001888 our_int = NXRD32(adapter, CRB_INT_VECTOR);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001889
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001890 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001891 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001892 return IRQ_NONE;
1893
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001894 /* claim interrupt */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001895 NXWR32(adapter, CRB_INT_VECTOR, (our_int & 0xffffffff));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301896 }
1897
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001898 /* clear interrupt */
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001899 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001900 netxen_nic_disable_int(sds_ring);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001901
1902 adapter->pci_write_immediate(adapter,
1903 adapter->legacy_intr.tgt_status_reg,
1904 0xffffffff);
1905 /* read twice to ensure write is flushed */
1906 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1907 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1908
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001909 napi_schedule(&sds_ring->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001910
1911 return IRQ_HANDLED;
1912}
1913
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001914static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001915{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001916 struct nx_host_sds_ring *sds_ring = data;
1917 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001918
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001919 /* clear interrupt */
1920 adapter->pci_write_immediate(adapter,
Dhananjay Phadke7a2469c2009-05-08 22:02:27 +00001921 adapter->msi_tgt_status, 0xffffffff);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001922
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001923 napi_schedule(&sds_ring->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001924 return IRQ_HANDLED;
1925}
1926
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001927static irqreturn_t netxen_msix_intr(int irq, void *data)
1928{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001929 struct nx_host_sds_ring *sds_ring = data;
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001930
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001931 napi_schedule(&sds_ring->napi);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001932 return IRQ_HANDLED;
1933}
1934
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001935static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001936{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001937 struct nx_host_sds_ring *sds_ring =
1938 container_of(napi, struct nx_host_sds_ring, napi);
1939
1940 struct netxen_adapter *adapter = sds_ring->adapter;
1941
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001942 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001943 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001944
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001945 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001946
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001947 work_done = netxen_process_rcv_ring(sds_ring, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001948
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001949 if ((work_done < budget) && tx_complete) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001950 napi_complete(&sds_ring->napi);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001951 if (netif_running(adapter->netdev))
1952 netxen_nic_enable_int(sds_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001953 }
1954
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001955 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001956}
1957
1958#ifdef CONFIG_NET_POLL_CONTROLLER
1959static void netxen_nic_poll_controller(struct net_device *netdev)
1960{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001961 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001962 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001963 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001964 enable_irq(adapter->irq);
1965}
1966#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001967
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00001968static int
1969nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
1970{
1971 int count;
1972 if (netxen_api_lock(adapter))
1973 return -EIO;
1974
1975 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1976
1977 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
1978
1979 netxen_api_unlock(adapter);
1980 return count;
1981}
1982
1983static int
1984nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
1985{
1986 int count;
1987 if (netxen_api_lock(adapter))
1988 return -EIO;
1989
1990 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1991 WARN_ON(count == 0);
1992
1993 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
1994
1995 if (count == 0)
1996 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
1997
1998 netxen_api_unlock(adapter);
1999 return count;
2000}
2001
2002static int
2003netxen_can_start_firmware(struct netxen_adapter *adapter)
2004{
2005 int count;
2006 int can_start = 0;
2007
2008 if (netxen_api_lock(adapter))
2009 return 0;
2010
2011 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2012
2013 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2014 count = 0;
2015
2016 if (count == 0) {
2017 can_start = 1;
2018 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2019 }
2020
2021 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2022
2023 netxen_api_unlock(adapter);
2024
2025 return can_start;
2026}
2027
2028static void
2029netxen_schedule_work(struct netxen_adapter *adapter,
2030 work_func_t func, int delay)
2031{
2032 INIT_DELAYED_WORK(&adapter->fw_work, func);
2033 schedule_delayed_work(&adapter->fw_work, delay);
2034}
2035
2036static void
2037netxen_cancel_fw_work(struct netxen_adapter *adapter)
2038{
2039 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2040 msleep(10);
2041
2042 cancel_delayed_work_sync(&adapter->fw_work);
2043}
2044
2045static void
2046netxen_attach_work(struct work_struct *work)
2047{
2048 struct netxen_adapter *adapter = container_of(work,
2049 struct netxen_adapter, fw_work.work);
2050 struct net_device *netdev = adapter->netdev;
2051 int err = 0;
2052
2053 if (netif_running(netdev)) {
2054 err = netxen_nic_attach(adapter);
2055 if (err)
2056 goto done;
2057
2058 err = netxen_nic_up(adapter, netdev);
2059 if (err) {
2060 netxen_nic_detach(adapter);
2061 goto done;
2062 }
2063
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002064 netxen_config_indev_addr(netdev, NETDEV_UP);
Dhananjay Phadke6a581e92009-09-05 17:43:08 +00002065 }
2066
2067 netif_device_attach(netdev);
2068
2069done:
2070 adapter->fw_fail_cnt = 0;
2071 clear_bit(__NX_RESETTING, &adapter->state);
2072 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2073}
2074
2075static void
2076netxen_fwinit_work(struct work_struct *work)
2077{
2078 struct netxen_adapter *adapter = container_of(work,
2079 struct netxen_adapter, fw_work.work);
2080 int dev_state;
2081
2082 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2083
2084 switch (dev_state) {
2085 case NX_DEV_COLD:
2086 case NX_DEV_READY:
2087 netxen_start_firmware(adapter);
2088 netxen_schedule_work(adapter, netxen_attach_work, 0);
2089 return;
2090
2091 case NX_DEV_INITALIZING:
2092 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2093 netxen_schedule_work(adapter,
2094 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2095 return;
2096 }
2097 break;
2098
2099 case NX_DEV_FAILED:
2100 default:
2101 break;
2102 }
2103
2104 nx_incr_dev_ref_cnt(adapter);
2105 clear_bit(__NX_RESETTING, &adapter->state);
2106}
2107
2108static void
2109netxen_detach_work(struct work_struct *work)
2110{
2111 struct netxen_adapter *adapter = container_of(work,
2112 struct netxen_adapter, fw_work.work);
2113 struct net_device *netdev = adapter->netdev;
2114 int ref_cnt, delay;
2115 u32 status;
2116
2117 netif_device_detach(netdev);
2118
2119 if (netif_running(netdev))
2120 netxen_nic_down(adapter, netdev);
2121
2122 netxen_nic_detach(adapter);
2123
2124 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2125
2126 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2127
2128 if (status & NX_RCODE_FATAL_ERROR)
2129 return;
2130
2131 if (adapter->temp == NX_TEMP_PANIC)
2132 return;
2133
2134 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2135
2136 adapter->fw_wait_cnt = 0;
2137 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2138}
2139
2140static int
2141netxen_check_health(struct netxen_adapter *adapter)
2142{
2143 u32 state, heartbit;
2144 struct net_device *netdev = adapter->netdev;
2145
2146 if (netxen_nic_check_temp(adapter))
2147 goto detach;
2148
2149 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2150 if (state == NX_DEV_NEED_RESET)
2151 goto detach;
2152
2153 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2154 return 0;
2155
2156 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2157 if (heartbit != adapter->heartbit) {
2158 adapter->heartbit = heartbit;
2159 adapter->fw_fail_cnt = 0;
2160 return 0;
2161 }
2162
2163 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2164 return 0;
2165
2166 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2167
2168 dev_info(&netdev->dev, "firmware hang detected\n");
2169
2170detach:
2171 if (!test_and_set_bit(__NX_RESETTING, &adapter->state))
2172 netxen_schedule_work(adapter, netxen_detach_work, 0);
2173 return 1;
2174}
2175
2176static void
2177netxen_fw_poll_work(struct work_struct *work)
2178{
2179 struct netxen_adapter *adapter = container_of(work,
2180 struct netxen_adapter, fw_work.work);
2181
2182 if (test_bit(__NX_RESETTING, &adapter->state))
2183 goto reschedule;
2184
2185 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2186 if (!adapter->has_link_events) {
2187
2188 netxen_nic_handle_phy_intr(adapter);
2189
2190 if (adapter->link_changed)
2191 netxen_nic_set_link_parameters(adapter);
2192 }
2193 }
2194
2195 if (netxen_check_health(adapter))
2196 return;
2197
2198reschedule:
2199 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2200}
2201
Narender Kumarfa3ce352009-08-24 19:23:28 +00002202static ssize_t
2203netxen_store_bridged_mode(struct device *dev,
2204 struct device_attribute *attr, const char *buf, size_t len)
2205{
2206 struct net_device *net = to_net_dev(dev);
2207 struct netxen_adapter *adapter = netdev_priv(net);
2208 unsigned long new;
2209 int ret = -EINVAL;
2210
2211 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2212 goto err_out;
2213
2214 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2215 goto err_out;
2216
2217 if (strict_strtoul(buf, 2, &new))
2218 goto err_out;
2219
2220 if (!netxen_config_bridged_mode(adapter, !!new))
2221 ret = len;
2222
2223err_out:
2224 return ret;
2225}
2226
2227static ssize_t
2228netxen_show_bridged_mode(struct device *dev,
2229 struct device_attribute *attr, char *buf)
2230{
2231 struct net_device *net = to_net_dev(dev);
2232 struct netxen_adapter *adapter;
2233 int bridged_mode = 0;
2234
2235 adapter = netdev_priv(net);
2236
2237 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2238 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2239
2240 return sprintf(buf, "%d\n", bridged_mode);
2241}
2242
2243static struct device_attribute dev_attr_bridged_mode = {
2244 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2245 .show = netxen_show_bridged_mode,
2246 .store = netxen_store_bridged_mode,
2247};
2248
2249static void
2250netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2251{
2252 struct net_device *netdev = adapter->netdev;
2253 struct device *dev = &netdev->dev;
2254
2255 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2256 /* bridged_mode control */
2257 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2258 dev_warn(&netdev->dev,
2259 "failed to create bridged_mode sysfs entry\n");
2260 }
2261 }
2262}
2263
2264static void
2265netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2266{
2267 struct net_device *netdev = adapter->netdev;
2268 struct device *dev = &netdev->dev;
2269
2270 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2271 device_remove_file(dev, &dev_attr_bridged_mode);
2272}
2273
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002274#ifdef CONFIG_INET
2275
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002276#define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2277
2278static int
2279netxen_destip_supported(struct netxen_adapter *adapter)
2280{
2281 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2282 return 0;
2283
2284 if (adapter->ahw.cut_through)
2285 return 0;
2286
2287 return 1;
2288}
2289
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002290static void
2291netxen_config_indev_addr(struct net_device *dev, unsigned long event)
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002292{
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002293 struct in_device *indev;
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002294 struct netxen_adapter *adapter = netdev_priv(dev);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002295
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002296 if (netxen_destip_supported(adapter))
2297 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002298
2299 indev = in_dev_get(dev);
2300 if (!indev)
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002301 return;
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002302
2303 for_ifa(indev) {
2304 switch (event) {
2305 case NETDEV_UP:
2306 netxen_config_ipaddr(adapter,
2307 ifa->ifa_address, NX_IP_UP);
2308 break;
2309 case NETDEV_DOWN:
2310 netxen_config_ipaddr(adapter,
2311 ifa->ifa_address, NX_IP_DOWN);
2312 break;
2313 default:
2314 break;
2315 }
2316 } endfor_ifa(indev);
2317
2318 in_dev_put(indev);
Amit Kumar Salechad0725e42009-09-05 17:43:09 +00002319 return;
2320}
2321
2322static int netxen_netdev_event(struct notifier_block *this,
2323 unsigned long event, void *ptr)
2324{
2325 struct netxen_adapter *adapter;
2326 struct net_device *dev = (struct net_device *)ptr;
2327
2328recheck:
2329 if (dev == NULL)
2330 goto done;
2331
2332 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2333 dev = vlan_dev_real_dev(dev);
2334 goto recheck;
2335 }
2336
2337 if (!is_netxen_netdev(dev))
2338 goto done;
2339
2340 adapter = netdev_priv(dev);
2341
2342 if (!adapter)
2343 goto done;
2344
2345 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2346 goto done;
2347
2348 netxen_config_indev_addr(dev, event);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002349done:
2350 return NOTIFY_DONE;
2351}
2352
2353static int
2354netxen_inetaddr_event(struct notifier_block *this,
2355 unsigned long event, void *ptr)
2356{
2357 struct netxen_adapter *adapter;
2358 struct net_device *dev;
2359
2360 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2361
2362 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2363
2364recheck:
2365 if (dev == NULL || !netif_running(dev))
2366 goto done;
2367
2368 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2369 dev = vlan_dev_real_dev(dev);
2370 goto recheck;
2371 }
2372
2373 if (!is_netxen_netdev(dev))
2374 goto done;
2375
2376 adapter = netdev_priv(dev);
2377
2378 if (!adapter || !netxen_destip_supported(adapter))
2379 goto done;
2380
2381 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2382 goto done;
2383
2384 switch (event) {
2385 case NETDEV_UP:
2386 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2387 break;
2388 case NETDEV_DOWN:
2389 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2390 break;
2391 default:
2392 break;
2393 }
2394
2395done:
2396 return NOTIFY_DONE;
2397}
2398
2399static struct notifier_block netxen_netdev_cb = {
2400 .notifier_call = netxen_netdev_event,
2401};
2402
2403static struct notifier_block netxen_inetaddr_cb = {
2404 .notifier_call = netxen_inetaddr_event,
2405};
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002406#endif
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002407
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002408static struct pci_driver netxen_driver = {
2409 .name = netxen_nic_driver_name,
2410 .id_table = netxen_pci_tbl,
2411 .probe = netxen_nic_probe,
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002412 .remove = __devexit_p(netxen_nic_remove),
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002413#ifdef CONFIG_PM
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00002414 .suspend = netxen_nic_suspend,
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002415 .resume = netxen_nic_resume,
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00002416#endif
Amit Kumar Salecha22b57942009-08-23 08:35:12 +00002417 .shutdown = netxen_nic_shutdown
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002418};
2419
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002420static int __init netxen_init_module(void)
2421{
Dhananjay Phadkeff4fbd42009-03-13 14:52:06 +00002422 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2423
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002424#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002425 register_netdevice_notifier(&netxen_netdev_cb);
2426 register_inetaddr_notifier(&netxen_inetaddr_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002427#endif
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002428
Amit S. Kale184231b2007-01-22 06:52:53 -08002429 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002430}
2431
2432module_init(netxen_init_module);
2433
2434static void __exit netxen_exit_module(void)
2435{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002436 pci_unregister_driver(&netxen_driver);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002437
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002438#ifdef CONFIG_INET
Dhananjay Phadke6598b162009-07-26 20:07:37 +00002439 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2440 unregister_netdevice_notifier(&netxen_netdev_cb);
Dhananjay Phadke044824d2009-07-28 09:10:03 +00002441#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04002442}
2443
2444module_exit(netxen_exit_module);