blob: c61c1d187a9e3a00e1c50628a333e75ea24458a4 [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#include "netxen_nic_phan_reg.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040037
Jeff Garzik1494a812006-11-07 05:12:16 -050038#include <linux/dma-mapping.h>
Dhananjay Phadke391587c2009-01-14 20:48:11 -080039#include <linux/if_vlan.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030040#include <net/ip.h>
Dhananjay Phadke00977782009-01-16 11:03:25 -080041#include <linux/ipv6.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);
66static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
67static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000068static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040069static void netxen_watchdog(unsigned long);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070070static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040071#ifdef CONFIG_NET_POLL_CONTROLLER
72static void netxen_nic_poll_controller(struct net_device *netdev);
73#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050074static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070075static irqreturn_t netxen_msi_intr(int irq, void *data);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -080076static irqreturn_t netxen_msix_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040077
78/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010079#define ENTRY(device) \
Dhananjay Phadke040dec32008-09-12 06:55:14 -070080 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
Al Viro70081ac2008-06-23 02:04:50 +010081 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
82
Amit S. Kale3d396eb2006-10-21 15:33:03 -040083static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Dhananjay Phadke040dec32008-09-12 06:55:14 -070084 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
85 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
86 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
87 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
88 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
91 ENTRY(PCI_DEVICE_ID_NX3031),
Amit S. Kale3d396eb2006-10-21 15:33:03 -040092 {0,}
93};
94
95MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
96
Adrian Bunkb15551302008-03-31 02:22:18 +030097static struct workqueue_struct *netxen_workq;
98#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
99#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
100
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800101static void netxen_watchdog(unsigned long);
102
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700103static uint32_t crb_cmd_producer[4] = {
104 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
105 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
106};
107
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700108void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700109netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000110 struct nx_host_tx_ring *tx_ring, u32 producer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700111{
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000112 NXWR32(adapter, tx_ring->crb_cmd_producer, producer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700113}
114
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700115static uint32_t crb_cmd_consumer[4] = {
116 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
117 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
118};
119
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700120static inline void
121netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000122 struct nx_host_tx_ring *tx_ring, u32 consumer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700123{
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000124 NXWR32(adapter, tx_ring->crb_cmd_consumer, consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700125}
126
Dhananjay Phadke29566402008-07-21 19:44:04 -0700127static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700128 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700129 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
130 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
131 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700132};
133
Dhananjay Phadke29566402008-07-21 19:44:04 -0700134static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
135
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000136static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700137{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000138 struct netxen_adapter *adapter = sds_ring->adapter;
139
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000140 NXWR32(adapter, sds_ring->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700141}
142
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000143static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700144{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000145 struct netxen_adapter *adapter = sds_ring->adapter;
146
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000147 NXWR32(adapter, sds_ring->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700148
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700149 if (!NETXEN_IS_MSI_FAMILY(adapter))
150 adapter->pci_write_immediate(adapter,
151 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700152}
153
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000154static int
155netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
156{
157 int size = sizeof(struct nx_host_sds_ring) * count;
158
159 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
160
161 return (recv_ctx->sds_rings == NULL);
162}
163
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000164static void
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000165netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
166{
167 if (recv_ctx->sds_rings != NULL)
168 kfree(recv_ctx->sds_rings);
169}
170
171static int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000172netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
173{
174 int ring;
175 struct nx_host_sds_ring *sds_ring;
176 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
177
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000178 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
179 return 1;
180
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000181 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
182 sds_ring = &recv_ctx->sds_rings[ring];
183 netif_napi_add(netdev, &sds_ring->napi,
184 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
185 }
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000186
187 return 0;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000188}
189
190static void
191netxen_napi_enable(struct netxen_adapter *adapter)
192{
193 int ring;
194 struct nx_host_sds_ring *sds_ring;
195 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
196
197 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
198 sds_ring = &recv_ctx->sds_rings[ring];
199 napi_enable(&sds_ring->napi);
200 netxen_nic_enable_int(sds_ring);
201 }
202}
203
204static void
205netxen_napi_disable(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];
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000213 napi_disable(&sds_ring->napi);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000214 netxen_nic_disable_int(sds_ring);
215 synchronize_irq(sds_ring->irq);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000216 }
217}
218
Dhananjay Phadke29566402008-07-21 19:44:04 -0700219static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
220{
221 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000222 uint64_t mask, cmask;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700223
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000224 adapter->pci_using_dac = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700225
Yang Hongyang6afd1422009-04-06 19:01:15 -0700226 mask = DMA_BIT_MASK(32);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700227 /*
228 * Consistent DMA mask is set to 32 bit because it cannot be set to
229 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
230 * come off this pool.
231 */
Yang Hongyang6afd1422009-04-06 19:01:15 -0700232 cmask = DMA_BIT_MASK(32);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000233
234#ifndef CONFIG_IA64
235 if (revision_id >= NX_P3_B0)
Yang Hongyang6afd1422009-04-06 19:01:15 -0700236 mask = DMA_BIT_MASK(39);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000237 else if (revision_id == NX_P2_C1)
Yang Hongyang6afd1422009-04-06 19:01:15 -0700238 mask = DMA_BIT_MASK(35);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000239#endif
Dhananjay Phadke29566402008-07-21 19:44:04 -0700240 if (pci_set_dma_mask(pdev, mask) == 0 &&
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000241 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700242 adapter->pci_using_dac = 1;
243 return 0;
244 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700245
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000246 return -EIO;
247}
248
249/* Update addressable range if firmware supports it */
250static int
251nx_update_dma_mask(struct netxen_adapter *adapter)
252{
253 int change, shift, err;
254 uint64_t mask, old_mask;
255 struct pci_dev *pdev = adapter->pdev;
256
257 change = 0;
258
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000259 shift = NXRD32(adapter, CRB_DMA_SHIFT);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000260 if (shift >= 32)
261 return 0;
262
263 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
264 change = 1;
265 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
266 change = 1;
267
268 if (change) {
269 old_mask = pdev->dma_mask;
270 mask = (1ULL<<(32+shift)) - 1;
271
272 err = pci_set_dma_mask(pdev, mask);
273 if (err)
274 return pci_set_dma_mask(pdev, old_mask);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700275 }
276
Dhananjay Phadke29566402008-07-21 19:44:04 -0700277 return 0;
278}
279
280static void netxen_check_options(struct netxen_adapter *adapter)
281{
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000282 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
283 adapter->num_rxd = MAX_RCV_DESCRIPTORS_10G;
284 else if (adapter->ahw.port_type == NETXEN_NIC_GBE)
285 adapter->num_rxd = MAX_RCV_DESCRIPTORS_1G;
286
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000287 adapter->msix_supported = 0;
288 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700289 adapter->msix_supported = !!use_msi_x;
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000290 adapter->rss_supported = !!use_msi_x;
291 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
292 switch (adapter->ahw.board_type) {
293 case NETXEN_BRDTYPE_P2_SB31_10G:
294 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
295 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
296 adapter->msix_supported = !!use_msi_x;
297 adapter->rss_supported = !!use_msi_x;
298 break;
299 default:
300 break;
301 }
302 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700303
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000304 adapter->num_txd = MAX_CMD_DESCRIPTORS_HOST;
305 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS;
306 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700307
Dhananjay Phadke29566402008-07-21 19:44:04 -0700308 return;
309}
310
311static int
312netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
313{
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800314 u32 val, timeout;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700315
316 if (first_boot == 0x55555555) {
317 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000318 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800319
320 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
321 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700322
323 /* PCI bus master workaround */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000324 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700325 if (!(first_boot & 0x4)) {
326 first_boot |= 0x4;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000327 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
328 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700329 }
330
331 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000332 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700333 if (first_boot != 0x80000f) {
334 /* clear the register for future unloads/loads */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000335 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800336 return -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700337 }
338
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800339 /* Start P2 boot loader */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000340 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
341 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800342 timeout = 0;
343 do {
344 msleep(1);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000345 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800346
347 if (++timeout > 5000)
348 return -EIO;
349
350 } while (val == NETXEN_BDINFO_MAGIC);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700351 }
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800352 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700353}
354
355static void netxen_set_port_mode(struct netxen_adapter *adapter)
356{
357 u32 val, data;
358
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000359 val = adapter->ahw.board_type;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700360 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
361 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
362 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
363 data = NETXEN_PORT_MODE_802_3_AP;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000364 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700365 } else if (port_mode == NETXEN_PORT_MODE_XG) {
366 data = NETXEN_PORT_MODE_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000367 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700368 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
369 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000370 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700371 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
372 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000373 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700374 } else {
375 data = NETXEN_PORT_MODE_AUTO_NEG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000376 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700377 }
378
379 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
380 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
381 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
382 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
383 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
384 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000385 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700386 }
387}
388
Dhananjay Phadke29566402008-07-21 19:44:04 -0700389static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
390{
391 u32 control;
392 int pos;
393
394 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
395 if (pos) {
396 pci_read_config_dword(pdev, pos, &control);
397 if (enable)
398 control |= PCI_MSIX_FLAGS_ENABLE;
399 else
400 control = 0;
401 pci_write_config_dword(pdev, pos, control);
402 }
403}
404
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000405static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700406{
407 int i;
408
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000409 for (i = 0; i < count; i++)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700410 adapter->msix_entries[i].entry = i;
411}
412
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700413static int
414netxen_read_mac_addr(struct netxen_adapter *adapter)
415{
416 int i;
417 unsigned char *p;
418 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700419 struct net_device *netdev = adapter->netdev;
420 struct pci_dev *pdev = adapter->pdev;
421
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700422 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
423 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
424 return -EIO;
425 } else {
426 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
427 return -EIO;
428 }
429
430 p = (unsigned char *)&mac_addr;
431 for (i = 0; i < 6; i++)
432 netdev->dev_addr[i] = *(p + 5 - i);
433
434 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
435
436 /* set station address */
437
Johannes Berge1749612008-10-27 15:59:26 -0700438 if (!is_valid_ether_addr(netdev->perm_addr))
439 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700440
441 return 0;
442}
443
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000444int netxen_nic_set_mac(struct net_device *netdev, void *p)
445{
446 struct netxen_adapter *adapter = netdev_priv(netdev);
447 struct sockaddr *addr = p;
448
449 if (!is_valid_ether_addr(addr->sa_data))
450 return -EINVAL;
451
452 if (netif_running(netdev)) {
453 netif_device_detach(netdev);
454 netxen_napi_disable(adapter);
455 }
456
457 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
458 adapter->macaddr_set(adapter, addr->sa_data);
459
460 if (netif_running(netdev)) {
461 netif_device_attach(netdev);
462 netxen_napi_enable(adapter);
463 }
464 return 0;
465}
466
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800467static void netxen_set_multicast_list(struct net_device *dev)
468{
469 struct netxen_adapter *adapter = netdev_priv(dev);
470
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000471 adapter->set_multi(dev);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800472}
473
474static const struct net_device_ops netxen_netdev_ops = {
475 .ndo_open = netxen_nic_open,
476 .ndo_stop = netxen_nic_close,
477 .ndo_start_xmit = netxen_nic_xmit_frame,
478 .ndo_get_stats = netxen_nic_get_stats,
479 .ndo_validate_addr = eth_validate_addr,
480 .ndo_set_multicast_list = netxen_set_multicast_list,
481 .ndo_set_mac_address = netxen_nic_set_mac,
482 .ndo_change_mtu = netxen_nic_change_mtu,
483 .ndo_tx_timeout = netxen_tx_timeout,
484#ifdef CONFIG_NET_POLL_CONTROLLER
485 .ndo_poll_controller = netxen_nic_poll_controller,
486#endif
487};
488
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800489static void
490netxen_setup_intr(struct netxen_adapter *adapter)
491{
492 struct netxen_legacy_intr_set *legacy_intrp;
493 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000494 int err, num_msix;
495
496 if (adapter->rss_supported) {
497 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
498 MSIX_ENTRIES_PER_ADAPTER : 2;
499 } else
500 num_msix = 1;
501
502 adapter->max_sds_rings = 1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800503
504 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800505
506 if (adapter->ahw.revision_id >= NX_P3_B0)
507 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
508 else
509 legacy_intrp = &legacy_intr[0];
510 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
511 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
512 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
513 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
514
515 netxen_set_msix_bit(pdev, 0);
516
517 if (adapter->msix_supported) {
518
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000519 netxen_init_msix_entries(adapter, num_msix);
520 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
521 if (err == 0) {
522 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
523 netxen_set_msix_bit(pdev, 1);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800524
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000525 if (adapter->rss_supported)
526 adapter->max_sds_rings = num_msix;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800527
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000528 dev_info(&pdev->dev, "using msi-x interrupts\n");
529 return;
530 }
531
532 if (err > 0)
533 pci_disable_msix(pdev);
534
535 /* fall through for msi */
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800536 }
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000537
538 if (use_msi && !pci_enable_msi(pdev)) {
539 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
540 adapter->msi_tgt_status =
541 msi_tgt_status[adapter->ahw.pci_func];
542 dev_info(&pdev->dev, "using msi interrupts\n");
543 return;
544 }
545
546 dev_info(&pdev->dev, "using legacy interrupts\n");
547 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800548}
549
550static void
551netxen_teardown_intr(struct netxen_adapter *adapter)
552{
553 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
554 pci_disable_msix(adapter->pdev);
555 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
556 pci_disable_msi(adapter->pdev);
557}
558
559static void
560netxen_cleanup_pci_map(struct netxen_adapter *adapter)
561{
562 if (adapter->ahw.db_base != NULL)
563 iounmap(adapter->ahw.db_base);
564 if (adapter->ahw.pci_base0 != NULL)
565 iounmap(adapter->ahw.pci_base0);
566 if (adapter->ahw.pci_base1 != NULL)
567 iounmap(adapter->ahw.pci_base1);
568 if (adapter->ahw.pci_base2 != NULL)
569 iounmap(adapter->ahw.pci_base2);
570}
571
572static int
573netxen_setup_pci_map(struct netxen_adapter *adapter)
574{
575 void __iomem *mem_ptr0 = NULL;
576 void __iomem *mem_ptr1 = NULL;
577 void __iomem *mem_ptr2 = NULL;
578 void __iomem *db_ptr = NULL;
579
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800580 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
581
582 struct pci_dev *pdev = adapter->pdev;
583 int pci_func = adapter->ahw.pci_func;
584
585 int err = 0;
586
587 /*
588 * Set the CRB window to invalid. If any register in window 0 is
589 * accessed it should set the window to 0 and then reset it to 1.
590 */
591 adapter->curr_window = 255;
592 adapter->ahw.qdr_sn_window = -1;
593 adapter->ahw.ddr_mn_window = -1;
594
595 /* remap phys address */
596 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
597 mem_len = pci_resource_len(pdev, 0);
598 pci_len0 = 0;
599
600 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
601 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
602 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
603 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800604 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
605 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
606 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
607
608 /* 128 Meg of memory */
609 if (mem_len == NETXEN_PCI_128MB_SIZE) {
610 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
611 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
612 SECOND_PAGE_GROUP_SIZE);
613 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
614 THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800615 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
616 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
617 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
618 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800619 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
620 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
621 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
622 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
623 adapter->pci_write_immediate =
624 netxen_nic_pci_write_immediate_2M;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800625 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
626 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
627 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
628
629 mem_ptr0 = pci_ioremap_bar(pdev, 0);
630 if (mem_ptr0 == NULL) {
631 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
632 return -EIO;
633 }
634 pci_len0 = mem_len;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800635
636 adapter->ahw.ddr_mn_window = 0;
637 adapter->ahw.qdr_sn_window = 0;
638
639 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
640 (pci_func * 0x20);
641 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
642 if (pci_func < 4)
643 adapter->ahw.ms_win_crb += (pci_func * 0x20);
644 else
645 adapter->ahw.ms_win_crb +=
646 0xA0 + ((pci_func - 4) * 0x10);
647 } else {
648 return -EIO;
649 }
650
651 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
652
653 adapter->ahw.pci_base0 = mem_ptr0;
654 adapter->ahw.pci_len0 = pci_len0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800655 adapter->ahw.pci_base1 = mem_ptr1;
656 adapter->ahw.pci_base2 = mem_ptr2;
657
658 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
659 goto skip_doorbell;
660
661 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
662 db_len = pci_resource_len(pdev, 4);
663
664 if (db_len == 0) {
665 printk(KERN_ERR "%s: doorbell is disabled\n",
666 netxen_nic_driver_name);
667 err = -EIO;
668 goto err_out;
669 }
670
671 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
672 if (!db_ptr) {
673 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
674 netxen_nic_driver_name);
675 err = -EIO;
676 goto err_out;
677 }
678
679skip_doorbell:
680 adapter->ahw.db_base = db_ptr;
681 adapter->ahw.db_len = db_len;
682 return 0;
683
684err_out:
685 netxen_cleanup_pci_map(adapter);
686 return err;
687}
688
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800689static int
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000690netxen_start_firmware(struct netxen_adapter *adapter, int request_fw)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800691{
692 int val, err, first_boot;
693 struct pci_dev *pdev = adapter->pdev;
694
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000695 int first_driver = 0;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000696
697 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
698 first_driver = (adapter->portnum == 0);
699 else
700 first_driver = (adapter->ahw.pci_func == 0);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000701
702 if (!first_driver)
703 return 0;
704
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000705 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800706
707 err = netxen_check_hw_init(adapter, first_boot);
708 if (err) {
709 dev_err(&pdev->dev, "error in init HW init sequence\n");
710 return err;
711 }
712
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000713 if (request_fw)
714 netxen_request_firmware(adapter);
715
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800716 if (first_boot != 0x55555555) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000717 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800718 netxen_pinit_from_rom(adapter, 0);
719 msleep(1);
720 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000721
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000722 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000723 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
724 netxen_set_port_mode(adapter);
725
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800726 netxen_load_firmware(adapter);
727
728 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
729
730 /* Initialize multicast addr pool owners */
731 val = 0x7654;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000732 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800733 val |= 0x0f000000;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000734 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800735
736 }
737
738 err = netxen_initialize_adapter_offload(adapter);
739 if (err)
740 return err;
741
742 /*
743 * Tell the hardware our version number.
744 */
745 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
746 | ((_NETXEN_NIC_LINUX_MINOR << 8))
747 | (_NETXEN_NIC_LINUX_SUBVERSION);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000748 NXWR32(adapter, CRB_DRIVER_VERSION, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800749
750 /* Handshake with the card before we register the devices. */
751 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
752 if (err) {
753 netxen_free_adapter_offload(adapter);
754 return err;
755 }
756
757 return 0;
758}
759
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000760static int
761netxen_nic_request_irq(struct netxen_adapter *adapter)
762{
763 irq_handler_t handler;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000764 struct nx_host_sds_ring *sds_ring;
765 int err, ring;
766
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000767 unsigned long flags = IRQF_SAMPLE_RANDOM;
768 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000769 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000770
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000771 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
772 handler = netxen_msix_intr;
773 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
774 handler = netxen_msi_intr;
775 else {
776 flags |= IRQF_SHARED;
777 handler = netxen_intr;
778 }
779 adapter->irq = netdev->irq;
780
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000781 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
782 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000783 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000784 err = request_irq(sds_ring->irq, handler,
785 flags, sds_ring->name, sds_ring);
786 if (err)
787 return err;
788 }
789
790 return 0;
791}
792
793static void
794netxen_nic_free_irq(struct netxen_adapter *adapter)
795{
796 int ring;
797 struct nx_host_sds_ring *sds_ring;
798
799 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
800
801 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
802 sds_ring = &recv_ctx->sds_rings[ring];
803 free_irq(sds_ring->irq, sds_ring);
804 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000805}
806
807static int
808netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
809{
810 int err;
811
812 err = adapter->init_port(adapter, adapter->physical_port);
813 if (err) {
814 printk(KERN_ERR "%s: Failed to initialize port %d\n",
815 netxen_nic_driver_name, adapter->portnum);
816 return err;
817 }
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000818 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
819 adapter->macaddr_set(adapter, netdev->dev_addr);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000820
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000821 adapter->set_multi(netdev);
822 adapter->set_mtu(adapter, netdev->mtu);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000823
824 adapter->ahw.linkup = 0;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000825
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000826 netxen_napi_enable(adapter);
827
828 if (adapter->max_sds_rings > 1)
829 netxen_config_rss(adapter, 1);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000830
Dhananjay Phadke5103c9f2009-04-28 15:29:09 +0000831 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000832 netxen_linkevent_request(adapter, 1);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000833 else
834 netxen_nic_set_link_parameters(adapter);
835
836 mod_timer(&adapter->watchdog_timer, jiffies);
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000837
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000838 return 0;
839}
840
841static void
842netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
843{
844 netif_carrier_off(netdev);
845 netif_stop_queue(netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000846
847 if (adapter->stop_port)
848 adapter->stop_port(adapter);
849
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +0000850 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
851 netxen_p3_free_mac_list(adapter);
852
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000853 netxen_napi_disable(adapter);
854
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000855 netxen_release_tx_buffers(adapter);
856
857 FLUSH_SCHEDULED_WORK();
858 del_timer_sync(&adapter->watchdog_timer);
859}
860
861
862static int
863netxen_nic_attach(struct netxen_adapter *adapter)
864{
865 struct net_device *netdev = adapter->netdev;
866 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000867 int err, ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000868 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000869 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000870
871 err = netxen_init_firmware(adapter);
872 if (err != 0) {
873 printk(KERN_ERR "Failed to init firmware\n");
874 return -EIO;
875 }
876
877 if (adapter->fw_major < 4)
878 adapter->max_rds_rings = 3;
879 else
880 adapter->max_rds_rings = 2;
881
882 err = netxen_alloc_sw_resources(adapter);
883 if (err) {
884 printk(KERN_ERR "%s: Error in setting sw resources\n",
885 netdev->name);
886 return err;
887 }
888
889 netxen_nic_clear_stats(adapter);
890
891 err = netxen_alloc_hw_resources(adapter);
892 if (err) {
893 printk(KERN_ERR "%s: Error in setting hw resources\n",
894 netdev->name);
895 goto err_out_free_sw;
896 }
897
898 if (adapter->fw_major < 4) {
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000899 tx_ring = adapter->tx_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000900 tx_ring->crb_cmd_producer = crb_cmd_producer[adapter->portnum];
901 tx_ring->crb_cmd_consumer = crb_cmd_consumer[adapter->portnum];
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000902
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000903 netxen_nic_update_cmd_producer(adapter, tx_ring, 0);
904 netxen_nic_update_cmd_consumer(adapter, tx_ring, 0);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000905 }
906
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000907 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
908 rds_ring = &adapter->recv_ctx.rds_rings[ring];
909 netxen_post_rx_buffers(adapter, ring, rds_ring);
910 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000911
912 err = netxen_nic_request_irq(adapter);
913 if (err) {
914 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
915 netdev->name);
916 goto err_out_free_rxbuf;
917 }
918
919 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
920 return 0;
921
922err_out_free_rxbuf:
923 netxen_release_rx_buffers(adapter);
924 netxen_free_hw_resources(adapter);
925err_out_free_sw:
926 netxen_free_sw_resources(adapter);
927 return err;
928}
929
930static void
931netxen_nic_detach(struct netxen_adapter *adapter)
932{
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000933 netxen_release_rx_buffers(adapter);
934 netxen_free_hw_resources(adapter);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000935 netxen_nic_free_irq(adapter);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000936 netxen_free_sw_resources(adapter);
937
938 adapter->is_up = 0;
939}
940
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400941static int __devinit
942netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
943{
944 struct net_device *netdev = NULL;
945 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700946 int i = 0, err;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700947 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700948 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400949
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700950 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700951 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700952 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800953 return -ENODEV;
954 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700955
Dhananjay Phadke11d89d62008-08-08 00:08:45 -0700956 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
957 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
958 "will not be enabled.\n",
959 NX_P3_A0, NX_P3_B1);
960 return -ENODEV;
961 }
962
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400963 if ((err = pci_enable_device(pdev)))
964 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700965
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400966 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
967 err = -ENODEV;
968 goto err_out_disable_pdev;
969 }
970
971 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
972 goto err_out_disable_pdev;
973
974 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700975
976 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
977 if(!netdev) {
978 printk(KERN_ERR"%s: Failed to allocate memory for the "
979 "device block.Check system memory resource"
980 " usage.\n", netxen_nic_driver_name);
981 goto err_out_free_res;
982 }
983
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700984 SET_NETDEV_DEV(netdev, &pdev->dev);
985
Wang Chen4cf16532008-11-12 23:38:14 -0800986 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700987 adapter->netdev = netdev;
988 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700989 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700990
991 revision_id = pdev->revision;
992 adapter->ahw.revision_id = revision_id;
993
994 err = nx_set_dma_mask(adapter, revision_id);
995 if (err)
996 goto err_out_free_netdev;
997
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700998 rwlock_init(&adapter->adapter_lock);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000999 spin_lock_init(&adapter->tx_clean_lock);
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001000 INIT_LIST_HEAD(&adapter->mac_list);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001001
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001002 err = netxen_setup_pci_map(adapter);
1003 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001004 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001005
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301006 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001007 adapter->portnum = pci_func_id;
Dhananjay Phadke200eef22007-09-03 10:33:35 +05301008 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -07001009 adapter->mc_enabled = 0;
Dhananjay Phadke83821a02008-08-01 03:14:58 -07001010 if (NX_IS_REVISION_P3(revision_id))
Dhananjay Phadke29566402008-07-21 19:44:04 -07001011 adapter->max_mc_count = 38;
Dhananjay Phadke83821a02008-08-01 03:14:58 -07001012 else
Dhananjay Phadke29566402008-07-21 19:44:04 -07001013 adapter->max_mc_count = 16;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001014
Stephen Hemminger1abd2662008-11-21 17:32:15 -08001015 netdev->netdev_ops = &netxen_netdev_ops;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001016 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001017
1018 netxen_nic_change_mtu(netdev, netdev->mtu);
1019
1020 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Stephen Hemminger1abd2662008-11-21 17:32:15 -08001021
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001022 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001023 netdev->features |= (NETIF_F_GRO);
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001024 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1025
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001026 if (NX_IS_REVISION_P3(revision_id)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001027 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1028 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001029 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001030
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001031 if (adapter->pci_using_dac) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001032 netdev->features |= NETIF_F_HIGHDMA;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001033 netdev->vlan_features |= NETIF_F_HIGHDMA;
1034 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001035
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001036 if (netxen_nic_get_board_info(adapter) != 0) {
1037 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -07001038 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001039 err = -EIO;
1040 goto err_out_iounmap;
1041 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001042
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001043 netxen_initialize_adapter_ops(adapter);
1044
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001045 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001046 switch (adapter->ahw.board_type) {
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001047 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1048 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1049 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001050 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001051 break;
1052 default:
1053 break;
1054 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001055
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001056 err = netxen_start_firmware(adapter, 1);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001057 if (err)
1058 goto err_out_iounmap;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001059
Dhananjay Phadke1f434f62009-03-02 16:02:16 +00001060 nx_update_dma_mask(adapter);
1061
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001062 netxen_nic_get_firmware_info(adapter);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001063
Dhananjay Phadke29566402008-07-21 19:44:04 -07001064 /*
1065 * See if the firmware gave us a virtual-physical port mapping.
1066 */
1067 adapter->physical_port = adapter->portnum;
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001068 if (adapter->fw_major < 4) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001069 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001070 if (i != 0x55555555)
1071 adapter->physical_port = i;
1072 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001073
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001074 netxen_check_options(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001075
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001076 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001077
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001078 netdev->irq = adapter->msix_entries[0].vector;
1079
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +00001080 if (netxen_napi_add(adapter, netdev))
1081 goto err_out_disable_msi;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001082
Dhananjay Phadke29566402008-07-21 19:44:04 -07001083 init_timer(&adapter->watchdog_timer);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001084 adapter->watchdog_timer.function = &netxen_watchdog;
1085 adapter->watchdog_timer.data = (unsigned long)adapter;
1086 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1087 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1088
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001089 err = netxen_read_mac_addr(adapter);
1090 if (err)
1091 dev_warn(&pdev->dev, "failed to read mac addr\n");
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001092
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001093 netif_carrier_off(netdev);
1094 netif_stop_queue(netdev);
1095
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001096 if ((err = register_netdev(netdev))) {
1097 printk(KERN_ERR "%s: register_netdev failed port #%d"
1098 " aborting\n", netxen_nic_driver_name,
1099 adapter->portnum);
1100 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001101 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001102 }
1103
1104 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001105
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001106 switch (adapter->ahw.port_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001107 case NETXEN_NIC_GBE:
1108 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1109 adapter->netdev->name);
1110 break;
1111 case NETXEN_NIC_XGBE:
1112 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1113 adapter->netdev->name);
1114 break;
1115 }
1116
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001117 return 0;
1118
Dhananjay Phadke29566402008-07-21 19:44:04 -07001119err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001120 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001121
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001122 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001123
Dhananjay Phadke29566402008-07-21 19:44:04 -07001124err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001125 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001126
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001127err_out_free_netdev:
1128 free_netdev(netdev);
1129
1130err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001131 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001132
1133err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001134 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001135 pci_disable_device(pdev);
1136 return err;
1137}
1138
1139static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1140{
1141 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001142 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001143
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001144 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001145 if (adapter == NULL)
1146 return;
1147
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001148 netdev = adapter->netdev;
1149
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301150 unregister_netdev(netdev);
1151
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301152 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001153 netxen_nic_detach(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301154 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001155
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001156 if (adapter->portnum == 0)
1157 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301158
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001159 netxen_teardown_intr(adapter);
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +00001160 netxen_free_sds_rings(&adapter->recv_ctx);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301161
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001162 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001163
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001164 netxen_release_firmware(adapter);
1165
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001166 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301167 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001168 pci_set_drvdata(pdev, NULL);
1169
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001170 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001171}
1172
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001173static int
1174netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1175{
1176
1177 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1178 struct net_device *netdev = adapter->netdev;
1179
1180 netif_device_detach(netdev);
1181
1182 if (netif_running(netdev))
1183 netxen_nic_down(adapter, netdev);
1184
1185 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1186 netxen_nic_detach(adapter);
1187
1188 pci_save_state(pdev);
1189
1190 if (netxen_nic_wol_supported(adapter)) {
1191 pci_enable_wake(pdev, PCI_D3cold, 1);
1192 pci_enable_wake(pdev, PCI_D3hot, 1);
1193 }
1194
1195 pci_disable_device(pdev);
1196 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1197
1198 return 0;
1199}
1200
1201static int
1202netxen_nic_resume(struct pci_dev *pdev)
1203{
1204 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1205 struct net_device *netdev = adapter->netdev;
1206 int err;
1207
1208 pci_set_power_state(pdev, PCI_D0);
1209 pci_restore_state(pdev);
1210
1211 err = pci_enable_device(pdev);
1212 if (err)
1213 return err;
1214
1215 adapter->curr_window = 255;
1216
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001217 err = netxen_start_firmware(adapter, 0);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001218 if (err) {
1219 dev_err(&pdev->dev, "failed to start firmware\n");
1220 return err;
1221 }
1222
1223 if (netif_running(netdev)) {
1224 err = netxen_nic_attach(adapter);
1225 if (err)
1226 return err;
1227
1228 err = netxen_nic_up(adapter, netdev);
1229 if (err)
1230 return err;
1231
1232 netif_device_attach(netdev);
1233 }
1234
1235 return 0;
1236}
1237
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001238static int netxen_nic_open(struct net_device *netdev)
1239{
Wang Chen4cf16532008-11-12 23:38:14 -08001240 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001241 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001242
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001243 if (adapter->driver_mismatch)
1244 return -EIO;
1245
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001246 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001247 err = netxen_nic_attach(adapter);
1248 if (err)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001249 return err;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001250 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001251
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001252 err = netxen_nic_up(adapter, netdev);
1253 if (err)
1254 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001255
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001256 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001257
1258 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001259
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001260err_out:
1261 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001262 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001263}
1264
1265/*
1266 * netxen_nic_close - Disables a network interface entry point
1267 */
1268static int netxen_nic_close(struct net_device *netdev)
1269{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001270 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001271
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001272 netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001273 return 0;
1274}
1275
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001276static bool netxen_tso_check(struct net_device *netdev,
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001277 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1278{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001279 bool tso = false;
1280 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001281 __be16 protocol = skb->protocol;
1282 u16 flags = 0;
1283
Harvey Harrison09640e62009-02-01 00:45:17 -08001284 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001285 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1286 protocol = vh->h_vlan_encapsulated_proto;
1287 flags = FLAGS_VLAN_TAGGED;
1288 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001289
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001290 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1291 skb_shinfo(skb)->gso_size > 0) {
1292
1293 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1294 desc->total_hdr_length =
1295 skb_transport_offset(skb) + tcp_hdrlen(skb);
1296
Harvey Harrison09640e62009-02-01 00:45:17 -08001297 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001298 TX_TCP_LSO6 : TX_TCP_LSO;
1299 tso = true;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001300
1301 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001302 u8 l4proto;
1303
Harvey Harrison09640e62009-02-01 00:45:17 -08001304 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001305 l4proto = ip_hdr(skb)->protocol;
1306
1307 if (l4proto == IPPROTO_TCP)
1308 opcode = TX_TCP_PKT;
1309 else if(l4proto == IPPROTO_UDP)
1310 opcode = TX_UDP_PKT;
Harvey Harrison09640e62009-02-01 00:45:17 -08001311 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001312 l4proto = ipv6_hdr(skb)->nexthdr;
1313
1314 if (l4proto == IPPROTO_TCP)
1315 opcode = TX_TCPV6_PKT;
1316 else if(l4proto == IPPROTO_UDP)
1317 opcode = TX_UDPV6_PKT;
1318 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001319 }
1320 desc->tcp_hdr_offset = skb_transport_offset(skb);
1321 desc->ip_hdr_offset = skb_network_offset(skb);
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001322 netxen_set_tx_flags_opcode(desc, flags, opcode);
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001323 return tso;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001324}
1325
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001326static void
1327netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1328 struct netxen_cmd_buffer *pbuf, int last)
1329{
1330 int k;
1331 struct netxen_skb_frag *buffrag;
1332
1333 buffrag = &pbuf->frag_array[0];
1334 pci_unmap_single(pdev, buffrag->dma,
1335 buffrag->length, PCI_DMA_TODEVICE);
1336
1337 for (k = 1; k < last; k++) {
1338 buffrag = &pbuf->frag_array[k];
1339 pci_unmap_page(pdev, buffrag->dma,
1340 buffrag->length, PCI_DMA_TODEVICE);
1341 }
1342}
1343
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001344static inline void
1345netxen_clear_cmddesc(u64 *desc)
1346{
1347 int i;
1348 for (i = 0; i < 8; i++)
1349 desc[i] = 0ULL;
1350}
1351
1352static int
1353netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001354{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001355 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001356 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001357 unsigned int first_seg_len = skb->len - skb->data_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001358 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001359 struct netxen_skb_frag *buffrag;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001360 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001361 struct pci_dev *pdev = adapter->pdev;
1362 dma_addr_t temp_dma;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001363 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001364
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001365 u32 producer, consumer;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001366 int frag_count, no_of_desc;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001367 u32 num_txd = tx_ring->num_desc;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001368 bool is_tso = false;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001369
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001370 frag_count = skb_shinfo(skb)->nr_frags + 1;
1371
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001372 /* 4 fragments per cmd des */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001373 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001374
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001375 producer = tx_ring->producer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001376 smp_mb();
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001377 consumer = tx_ring->sw_consumer;
Dhananjay Phadke22527862009-05-05 19:05:06 +00001378 if ((no_of_desc+2) >= find_diff_among(producer, consumer, num_txd)) {
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001379 netif_stop_queue(netdev);
1380 smp_mb();
1381 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001382 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001383
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001384 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001385 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001386 pbuf = &tx_ring->cmd_buf_arr[producer];
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001387
1388 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1389
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001390 pbuf->skb = skb;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001391 pbuf->frag_count = frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001392 buffrag = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001393 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001394 PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001395 if (pci_dma_mapping_error(pdev, temp_dma))
1396 goto drop_packet;
1397
1398 buffrag->dma = temp_dma;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001399 buffrag->length = first_seg_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001400 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1401 netxen_set_tx_port(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001402
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001403 hwdesc->buffer_length[0] = cpu_to_le16(first_seg_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001404 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1405
1406 for (i = 1, k = 1; i < frag_count; i++, k++) {
1407 struct skb_frag_struct *frag;
1408 int len, temp_len;
1409 unsigned long offset;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001410
1411 /* move to next desc. if there is a need */
1412 if ((i & 0x3) == 0) {
1413 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001414 producer = get_next_index(producer, num_txd);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001415 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001416 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001417 pbuf = &tx_ring->cmd_buf_arr[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001418 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001419 }
1420 frag = &skb_shinfo(skb)->frags[i - 1];
1421 len = frag->size;
1422 offset = frag->page_offset;
1423
1424 temp_len = len;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001425 temp_dma = pci_map_page(pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001426 len, PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001427 if (pci_dma_mapping_error(pdev, temp_dma)) {
1428 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1429 goto drop_packet;
1430 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001431
1432 buffrag++;
1433 buffrag->dma = temp_dma;
1434 buffrag->length = temp_len;
1435
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001436 hwdesc->buffer_length[k] = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001437 switch (k) {
1438 case 0:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001439 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1440 break;
1441 case 1:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001442 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1443 break;
1444 case 2:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001445 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1446 break;
1447 case 3:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001448 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1449 break;
1450 }
1451 frag++;
1452 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001453 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001454
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001455 /* For LSO, we need to copy the MAC/IP/TCP headers into
1456 * the descriptor ring
1457 */
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001458 if (is_tso) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001459 int hdr_len, first_hdr_len, more_hdr;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001460 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001461 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1462 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001463 more_hdr = 1;
1464 } else {
1465 first_hdr_len = hdr_len;
1466 more_hdr = 0;
1467 }
1468 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001469 hwdesc = &tx_ring->desc_head[producer];
1470 pbuf = &tx_ring->cmd_buf_arr[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001471 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001472
1473 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001474 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001475 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001476 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001477
1478 if (more_hdr) {
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001479 hwdesc = &tx_ring->desc_head[producer];
1480 pbuf = &tx_ring->cmd_buf_arr[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001481 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001482 /* copy the next 64 bytes - should be enough except
1483 * for pathological case
1484 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001485 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1486 hwdesc,
1487 (hdr_len -
1488 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001489 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001490 }
1491 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001492
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001493 tx_ring->producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001494 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001495
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001496 netxen_nic_update_cmd_producer(adapter, tx_ring, producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001497
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001498 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001499 netdev->trans_start = jiffies;
1500
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001501 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001502
1503drop_packet:
1504 adapter->stats.txdropped++;
1505 dev_kfree_skb_any(skb);
1506 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001507}
1508
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001509static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1510{
1511 struct net_device *netdev = adapter->netdev;
1512 uint32_t temp, temp_state, temp_val;
1513 int rv = 0;
1514
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001515 temp = NXRD32(adapter, CRB_TEMP_STATE);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001516
1517 temp_state = nx_get_temp_state(temp);
1518 temp_val = nx_get_temp_val(temp);
1519
1520 if (temp_state == NX_TEMP_PANIC) {
1521 printk(KERN_ALERT
1522 "%s: Device temperature %d degrees C exceeds"
1523 " maximum allowed. Hardware has been shut down.\n",
1524 netxen_nic_driver_name, temp_val);
1525
1526 netif_carrier_off(netdev);
1527 netif_stop_queue(netdev);
1528 rv = 1;
1529 } else if (temp_state == NX_TEMP_WARN) {
1530 if (adapter->temp == NX_TEMP_NORMAL) {
1531 printk(KERN_ALERT
1532 "%s: Device temperature %d degrees C "
1533 "exceeds operating range."
1534 " Immediate action needed.\n",
1535 netxen_nic_driver_name, temp_val);
1536 }
1537 } else {
1538 if (adapter->temp == NX_TEMP_WARN) {
1539 printk(KERN_INFO
1540 "%s: Device temperature is now %d degrees C"
1541 " in normal range.\n", netxen_nic_driver_name,
1542 temp_val);
1543 }
1544 }
1545 adapter->temp = temp_state;
1546 return rv;
1547}
1548
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001549void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001550{
1551 struct net_device *netdev = adapter->netdev;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001552
1553 if (adapter->ahw.linkup && !linkup) {
1554 printk(KERN_INFO "%s: %s NIC Link is down\n",
1555 netxen_nic_driver_name, netdev->name);
1556 adapter->ahw.linkup = 0;
1557 if (netif_running(netdev)) {
1558 netif_carrier_off(netdev);
1559 netif_stop_queue(netdev);
1560 }
1561
1562 if (!adapter->has_link_events)
1563 netxen_nic_set_link_parameters(adapter);
1564
1565 } else if (!adapter->ahw.linkup && linkup) {
1566 printk(KERN_INFO "%s: %s NIC Link is up\n",
1567 netxen_nic_driver_name, netdev->name);
1568 adapter->ahw.linkup = 1;
1569 if (netif_running(netdev)) {
1570 netif_carrier_on(netdev);
1571 netif_wake_queue(netdev);
1572 }
1573
1574 if (!adapter->has_link_events)
1575 netxen_nic_set_link_parameters(adapter);
1576 }
1577}
1578
1579static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1580{
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001581 u32 val, port, linkup;
1582
1583 port = adapter->physical_port;
1584
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001585 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001586 val = NXRD32(adapter, CRB_XG_STATE_P3);
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001587 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1588 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001589 } else {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001590 val = NXRD32(adapter, CRB_XG_STATE);
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001591 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001592 linkup = (val >> port) & 1;
1593 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001594 val = (val >> port*8) & 0xff;
1595 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001596 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001597 }
1598
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001599 netxen_advert_link_change(adapter, linkup);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001600}
1601
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001602static void netxen_watchdog(unsigned long v)
1603{
1604 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001605
1606 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001607}
1608
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001609void netxen_watchdog_task(struct work_struct *work)
1610{
1611 struct netxen_adapter *adapter =
1612 container_of(work, struct netxen_adapter, watchdog_task);
1613
1614 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1615 return;
1616
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001617 if (!adapter->has_link_events)
1618 netxen_nic_handle_phy_intr(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001619
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001620 if (netif_running(adapter->netdev))
1621 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001622}
1623
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001624static void netxen_tx_timeout(struct net_device *netdev)
1625{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001626 struct netxen_adapter *adapter = (struct netxen_adapter *)
1627 netdev_priv(netdev);
1628 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001629}
1630
David Howells6d5aefb2006-12-05 19:36:26 +00001631static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001632{
Jeff Garzik47906542007-11-23 21:23:36 -05001633 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001634 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001635
1636 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001637 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001638
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001639 netxen_napi_disable(adapter);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001640
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001641 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001642
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001643 netxen_napi_enable(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001644 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001645}
1646
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001647struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1648{
1649 struct netxen_adapter *adapter = netdev_priv(netdev);
1650 struct net_device_stats *stats = &adapter->net_stats;
1651
1652 memset(stats, 0, sizeof(*stats));
1653
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001654 stats->rx_packets = adapter->stats.no_rcv;
Dhananjay Phadkee98e3352009-04-07 22:50:38 +00001655 stats->tx_packets = adapter->stats.xmitfinished;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001656 stats->rx_bytes = adapter->stats.rxbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001657 stats->tx_bytes = adapter->stats.txbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001658 stats->rx_dropped = adapter->stats.rxdropped;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001659 stats->tx_dropped = adapter->stats.txdropped;
1660
1661 return stats;
1662}
1663
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001664static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001665{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001666 struct nx_host_sds_ring *sds_ring = data;
1667 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001668 u32 status = 0;
1669
1670 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1671
1672 if (!(status & adapter->legacy_intr.int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001673 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301674
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001675 if (adapter->ahw.revision_id >= NX_P3_B1) {
1676 /* check interrupt state machine, to be sure */
1677 status = adapter->pci_read_immediate(adapter,
1678 ISR_INT_STATE_REG);
1679 if (!ISR_LEGACY_INT_TRIGGERED(status))
1680 return IRQ_NONE;
1681
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001682 } else {
1683 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001684
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001685 our_int = NXRD32(adapter, CRB_INT_VECTOR);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001686
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001687 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001688 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001689 return IRQ_NONE;
1690
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001691 /* claim interrupt */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001692 NXWR32(adapter, CRB_INT_VECTOR, (our_int & 0xffffffff));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301693 }
1694
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001695 /* clear interrupt */
1696 if (adapter->fw_major < 4)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001697 netxen_nic_disable_int(sds_ring);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001698
1699 adapter->pci_write_immediate(adapter,
1700 adapter->legacy_intr.tgt_status_reg,
1701 0xffffffff);
1702 /* read twice to ensure write is flushed */
1703 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1704 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1705
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001706 napi_schedule(&sds_ring->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001707
1708 return IRQ_HANDLED;
1709}
1710
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001711static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001712{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001713 struct nx_host_sds_ring *sds_ring = data;
1714 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001715
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001716 /* clear interrupt */
1717 adapter->pci_write_immediate(adapter,
Dhananjay Phadke7a2469c2009-05-08 22:02:27 +00001718 adapter->msi_tgt_status, 0xffffffff);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001719
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001720 napi_schedule(&sds_ring->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001721 return IRQ_HANDLED;
1722}
1723
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001724static irqreturn_t netxen_msix_intr(int irq, void *data)
1725{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001726 struct nx_host_sds_ring *sds_ring = data;
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001727
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001728 napi_schedule(&sds_ring->napi);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001729 return IRQ_HANDLED;
1730}
1731
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001732static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001733{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001734 struct nx_host_sds_ring *sds_ring =
1735 container_of(napi, struct nx_host_sds_ring, napi);
1736
1737 struct netxen_adapter *adapter = sds_ring->adapter;
1738
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001739 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001740 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001741
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001742 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001743
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001744 work_done = netxen_process_rcv_ring(sds_ring, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001745
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001746 if ((work_done < budget) && tx_complete) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001747 napi_complete(&sds_ring->napi);
1748 netxen_nic_enable_int(sds_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001749 }
1750
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001751 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001752}
1753
1754#ifdef CONFIG_NET_POLL_CONTROLLER
1755static void netxen_nic_poll_controller(struct net_device *netdev)
1756{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001757 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001758 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001759 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001760 enable_irq(adapter->irq);
1761}
1762#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001763
1764static struct pci_driver netxen_driver = {
1765 .name = netxen_nic_driver_name,
1766 .id_table = netxen_pci_tbl,
1767 .probe = netxen_nic_probe,
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001768 .remove = __devexit_p(netxen_nic_remove),
1769 .suspend = netxen_nic_suspend,
1770 .resume = netxen_nic_resume
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001771};
1772
1773/* Driver Registration on NetXen card */
1774
1775static int __init netxen_init_module(void)
1776{
Dhananjay Phadkeff4fbd42009-03-13 14:52:06 +00001777 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
1778
Al Viro79ea13c2008-01-24 02:06:46 -08001779 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001780 return -ENOMEM;
1781
Amit S. Kale184231b2007-01-22 06:52:53 -08001782 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001783}
1784
1785module_init(netxen_init_module);
1786
1787static void __exit netxen_exit_module(void)
1788{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001789 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001790 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001791}
1792
1793module_exit(netxen_exit_module);