blob: fb976cbf9c71d7cc61a1c234bfaaededb59c4896 [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>
Dhananjay Phadke6598b162009-07-26 20:07:37 +000042#include <linux/inetdevice.h>
Jeff Garzik1494a812006-11-07 05:12:16 -050043
Amit S. Kale3d396eb2006-10-21 15:33:03 -040044MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45MODULE_LICENSE("GPL");
46MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
47
dhananjay@netxen.comceded322007-07-19 14:41:09 +053048char netxen_nic_driver_name[] = "netxen_nic";
Amit S. Kale3d396eb2006-10-21 15:33:03 -040049static char netxen_nic_driver_string[] = "NetXen Network Driver version "
Amit S. Kalecb8011a2006-11-29 09:00:10 -080050 NETXEN_NIC_LINUX_VERSIONID;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040051
Dhananjay Phadke29566402008-07-21 19:44:04 -070052static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
53
54/* Default to restricted 1G auto-neg mode */
55static int wol_port_mode = 5;
56
57static int use_msi = 1;
58
59static int use_msi_x = 1;
60
Amit S. Kale3d396eb2006-10-21 15:33:03 -040061/* Local functions to NetXen NIC driver */
62static int __devinit netxen_nic_probe(struct pci_dev *pdev,
Dhananjay Phadke29566402008-07-21 19:44:04 -070063 const struct pci_device_id *ent);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040064static void __devexit netxen_nic_remove(struct pci_dev *pdev);
65static int netxen_nic_open(struct net_device *netdev);
66static int netxen_nic_close(struct net_device *netdev);
67static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
68static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000069static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040070static void netxen_watchdog(unsigned long);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070071static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040072#ifdef CONFIG_NET_POLL_CONTROLLER
73static void netxen_nic_poll_controller(struct net_device *netdev);
74#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050075static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070076static irqreturn_t netxen_msi_intr(int irq, void *data);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -080077static irqreturn_t netxen_msix_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040078
79/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010080#define ENTRY(device) \
Dhananjay Phadke040dec32008-09-12 06:55:14 -070081 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
Al Viro70081ac2008-06-23 02:04:50 +010082 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
83
Amit S. Kale3d396eb2006-10-21 15:33:03 -040084static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Dhananjay Phadke040dec32008-09-12 06:55:14 -070085 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
86 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
87 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
88 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
91 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
92 ENTRY(PCI_DEVICE_ID_NX3031),
Amit S. Kale3d396eb2006-10-21 15:33:03 -040093 {0,}
94};
95
96MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
97
Adrian Bunkb15551302008-03-31 02:22:18 +030098static struct workqueue_struct *netxen_workq;
99#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
100#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
101
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800102static void netxen_watchdog(unsigned long);
103
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700104static uint32_t crb_cmd_producer[4] = {
105 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
106 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
107};
108
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700109void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700110netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000111 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700112{
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000113 NXWR32(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
114
115 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
116 netif_stop_queue(adapter->netdev);
117 smp_mb();
118 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700119}
120
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700121static uint32_t crb_cmd_consumer[4] = {
122 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
123 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
124};
125
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700126static inline void
127netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000128 struct nx_host_tx_ring *tx_ring)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700129{
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000130 NXWR32(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700131}
132
Dhananjay Phadke29566402008-07-21 19:44:04 -0700133static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700134 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700135 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
136 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
137 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700138};
139
Dhananjay Phadke29566402008-07-21 19:44:04 -0700140static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
141
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000142static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700143{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000144 struct netxen_adapter *adapter = sds_ring->adapter;
145
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000146 NXWR32(adapter, sds_ring->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700147}
148
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000149static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700150{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000151 struct netxen_adapter *adapter = sds_ring->adapter;
152
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000153 NXWR32(adapter, sds_ring->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700154
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700155 if (!NETXEN_IS_MSI_FAMILY(adapter))
156 adapter->pci_write_immediate(adapter,
157 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700158}
159
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000160static int
161netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
162{
163 int size = sizeof(struct nx_host_sds_ring) * count;
164
165 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
166
167 return (recv_ctx->sds_rings == NULL);
168}
169
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000170static void
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000171netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
172{
173 if (recv_ctx->sds_rings != NULL)
174 kfree(recv_ctx->sds_rings);
175}
176
177static int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000178netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
179{
180 int ring;
181 struct nx_host_sds_ring *sds_ring;
182 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
183
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000184 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
185 return 1;
186
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000187 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
188 sds_ring = &recv_ctx->sds_rings[ring];
189 netif_napi_add(netdev, &sds_ring->napi,
190 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
191 }
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +0000192
193 return 0;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000194}
195
196static void
197netxen_napi_enable(struct netxen_adapter *adapter)
198{
199 int ring;
200 struct nx_host_sds_ring *sds_ring;
201 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
202
203 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
204 sds_ring = &recv_ctx->sds_rings[ring];
205 napi_enable(&sds_ring->napi);
206 netxen_nic_enable_int(sds_ring);
207 }
208}
209
210static void
211netxen_napi_disable(struct netxen_adapter *adapter)
212{
213 int ring;
214 struct nx_host_sds_ring *sds_ring;
215 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
216
217 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
218 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000219 netxen_nic_disable_int(sds_ring);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000220 napi_synchronize(&sds_ring->napi);
221 napi_disable(&sds_ring->napi);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000222 }
223}
224
Dhananjay Phadke29566402008-07-21 19:44:04 -0700225static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
226{
227 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000228 uint64_t mask, cmask;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700229
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000230 adapter->pci_using_dac = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700231
Yang Hongyang6afd1422009-04-06 19:01:15 -0700232 mask = DMA_BIT_MASK(32);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700233 /*
234 * Consistent DMA mask is set to 32 bit because it cannot be set to
235 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
236 * come off this pool.
237 */
Yang Hongyang6afd1422009-04-06 19:01:15 -0700238 cmask = DMA_BIT_MASK(32);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000239
240#ifndef CONFIG_IA64
241 if (revision_id >= NX_P3_B0)
Yang Hongyang6afd1422009-04-06 19:01:15 -0700242 mask = DMA_BIT_MASK(39);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000243 else if (revision_id == NX_P2_C1)
Yang Hongyang6afd1422009-04-06 19:01:15 -0700244 mask = DMA_BIT_MASK(35);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000245#endif
Dhananjay Phadke29566402008-07-21 19:44:04 -0700246 if (pci_set_dma_mask(pdev, mask) == 0 &&
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000247 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700248 adapter->pci_using_dac = 1;
249 return 0;
250 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700251
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000252 return -EIO;
253}
254
255/* Update addressable range if firmware supports it */
256static int
257nx_update_dma_mask(struct netxen_adapter *adapter)
258{
259 int change, shift, err;
260 uint64_t mask, old_mask;
261 struct pci_dev *pdev = adapter->pdev;
262
263 change = 0;
264
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000265 shift = NXRD32(adapter, CRB_DMA_SHIFT);
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000266 if (shift >= 32)
267 return 0;
268
269 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
270 change = 1;
271 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
272 change = 1;
273
274 if (change) {
275 old_mask = pdev->dma_mask;
276 mask = (1ULL<<(32+shift)) - 1;
277
278 err = pci_set_dma_mask(pdev, mask);
279 if (err)
280 return pci_set_dma_mask(pdev, old_mask);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700281 }
282
Dhananjay Phadke29566402008-07-21 19:44:04 -0700283 return 0;
284}
285
286static void netxen_check_options(struct netxen_adapter *adapter)
287{
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000288 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
289 adapter->num_rxd = MAX_RCV_DESCRIPTORS_10G;
290 else if (adapter->ahw.port_type == NETXEN_NIC_GBE)
291 adapter->num_rxd = MAX_RCV_DESCRIPTORS_1G;
292
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000293 adapter->msix_supported = 0;
294 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700295 adapter->msix_supported = !!use_msi_x;
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000296 adapter->rss_supported = !!use_msi_x;
297 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
298 switch (adapter->ahw.board_type) {
299 case NETXEN_BRDTYPE_P2_SB31_10G:
300 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
Dhananjay Phadkef6d21f42009-04-07 22:50:46 +0000301 adapter->msix_supported = !!use_msi_x;
302 adapter->rss_supported = !!use_msi_x;
303 break;
304 default:
305 break;
306 }
307 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700308
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000309 adapter->num_txd = MAX_CMD_DESCRIPTORS_HOST;
310 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS;
311 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700312
Dhananjay Phadke29566402008-07-21 19:44:04 -0700313 return;
314}
315
316static int
317netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
318{
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800319 u32 val, timeout;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700320
321 if (first_boot == 0x55555555) {
322 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000323 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800324
325 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
326 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700327
328 /* PCI bus master workaround */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000329 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700330 if (!(first_boot & 0x4)) {
331 first_boot |= 0x4;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000332 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
333 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700334 }
335
336 /* This is the first boot after power up */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000337 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700338 if (first_boot != 0x80000f) {
339 /* clear the register for future unloads/loads */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000340 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800341 return -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700342 }
343
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800344 /* Start P2 boot loader */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000345 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
346 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800347 timeout = 0;
348 do {
349 msleep(1);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000350 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800351
352 if (++timeout > 5000)
353 return -EIO;
354
355 } while (val == NETXEN_BDINFO_MAGIC);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700356 }
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800357 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700358}
359
360static void netxen_set_port_mode(struct netxen_adapter *adapter)
361{
362 u32 val, data;
363
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000364 val = adapter->ahw.board_type;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700365 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
366 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
367 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
368 data = NETXEN_PORT_MODE_802_3_AP;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000369 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700370 } else if (port_mode == NETXEN_PORT_MODE_XG) {
371 data = NETXEN_PORT_MODE_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000372 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700373 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
374 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000375 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700376 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
377 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000378 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700379 } else {
380 data = NETXEN_PORT_MODE_AUTO_NEG;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000381 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700382 }
383
384 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
385 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
386 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
387 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
388 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
389 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000390 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700391 }
392}
393
Dhananjay Phadke29566402008-07-21 19:44:04 -0700394static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
395{
396 u32 control;
397 int pos;
398
399 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
400 if (pos) {
401 pci_read_config_dword(pdev, pos, &control);
402 if (enable)
403 control |= PCI_MSIX_FLAGS_ENABLE;
404 else
405 control = 0;
406 pci_write_config_dword(pdev, pos, control);
407 }
408}
409
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000410static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700411{
412 int i;
413
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000414 for (i = 0; i < count; i++)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700415 adapter->msix_entries[i].entry = i;
416}
417
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700418static int
419netxen_read_mac_addr(struct netxen_adapter *adapter)
420{
421 int i;
422 unsigned char *p;
423 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700424 struct net_device *netdev = adapter->netdev;
425 struct pci_dev *pdev = adapter->pdev;
426
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700427 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
428 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
429 return -EIO;
430 } else {
431 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
432 return -EIO;
433 }
434
435 p = (unsigned char *)&mac_addr;
436 for (i = 0; i < 6; i++)
437 netdev->dev_addr[i] = *(p + 5 - i);
438
439 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
440
441 /* set station address */
442
Johannes Berge1749612008-10-27 15:59:26 -0700443 if (!is_valid_ether_addr(netdev->perm_addr))
444 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700445
446 return 0;
447}
448
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000449int netxen_nic_set_mac(struct net_device *netdev, void *p)
450{
451 struct netxen_adapter *adapter = netdev_priv(netdev);
452 struct sockaddr *addr = p;
453
454 if (!is_valid_ether_addr(addr->sa_data))
455 return -EINVAL;
456
457 if (netif_running(netdev)) {
458 netif_device_detach(netdev);
459 netxen_napi_disable(adapter);
460 }
461
462 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
463 adapter->macaddr_set(adapter, addr->sa_data);
464
465 if (netif_running(netdev)) {
466 netif_device_attach(netdev);
467 netxen_napi_enable(adapter);
468 }
469 return 0;
470}
471
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800472static void netxen_set_multicast_list(struct net_device *dev)
473{
474 struct netxen_adapter *adapter = netdev_priv(dev);
475
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000476 adapter->set_multi(dev);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800477}
478
479static const struct net_device_ops netxen_netdev_ops = {
480 .ndo_open = netxen_nic_open,
481 .ndo_stop = netxen_nic_close,
482 .ndo_start_xmit = netxen_nic_xmit_frame,
483 .ndo_get_stats = netxen_nic_get_stats,
484 .ndo_validate_addr = eth_validate_addr,
485 .ndo_set_multicast_list = netxen_set_multicast_list,
486 .ndo_set_mac_address = netxen_nic_set_mac,
487 .ndo_change_mtu = netxen_nic_change_mtu,
488 .ndo_tx_timeout = netxen_tx_timeout,
489#ifdef CONFIG_NET_POLL_CONTROLLER
490 .ndo_poll_controller = netxen_nic_poll_controller,
491#endif
492};
493
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800494static void
495netxen_setup_intr(struct netxen_adapter *adapter)
496{
497 struct netxen_legacy_intr_set *legacy_intrp;
498 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000499 int err, num_msix;
500
501 if (adapter->rss_supported) {
502 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
503 MSIX_ENTRIES_PER_ADAPTER : 2;
504 } else
505 num_msix = 1;
506
507 adapter->max_sds_rings = 1;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800508
509 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800510
511 if (adapter->ahw.revision_id >= NX_P3_B0)
512 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
513 else
514 legacy_intrp = &legacy_intr[0];
515 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
516 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
517 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
518 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
519
520 netxen_set_msix_bit(pdev, 0);
521
522 if (adapter->msix_supported) {
523
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000524 netxen_init_msix_entries(adapter, num_msix);
525 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
526 if (err == 0) {
527 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
528 netxen_set_msix_bit(pdev, 1);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800529
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000530 if (adapter->rss_supported)
531 adapter->max_sds_rings = num_msix;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800532
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000533 dev_info(&pdev->dev, "using msi-x interrupts\n");
534 return;
535 }
536
537 if (err > 0)
538 pci_disable_msix(pdev);
539
540 /* fall through for msi */
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800541 }
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000542
543 if (use_msi && !pci_enable_msi(pdev)) {
544 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
545 adapter->msi_tgt_status =
546 msi_tgt_status[adapter->ahw.pci_func];
547 dev_info(&pdev->dev, "using msi interrupts\n");
Dhananjay Phadkef67f3402009-05-18 21:46:40 -0700548 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000549 return;
550 }
551
552 dev_info(&pdev->dev, "using legacy interrupts\n");
553 adapter->msix_entries[0].vector = pdev->irq;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800554}
555
556static void
557netxen_teardown_intr(struct netxen_adapter *adapter)
558{
559 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
560 pci_disable_msix(adapter->pdev);
561 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
562 pci_disable_msi(adapter->pdev);
563}
564
565static void
566netxen_cleanup_pci_map(struct netxen_adapter *adapter)
567{
568 if (adapter->ahw.db_base != NULL)
569 iounmap(adapter->ahw.db_base);
570 if (adapter->ahw.pci_base0 != NULL)
571 iounmap(adapter->ahw.pci_base0);
572 if (adapter->ahw.pci_base1 != NULL)
573 iounmap(adapter->ahw.pci_base1);
574 if (adapter->ahw.pci_base2 != NULL)
575 iounmap(adapter->ahw.pci_base2);
576}
577
578static int
579netxen_setup_pci_map(struct netxen_adapter *adapter)
580{
581 void __iomem *mem_ptr0 = NULL;
582 void __iomem *mem_ptr1 = NULL;
583 void __iomem *mem_ptr2 = NULL;
584 void __iomem *db_ptr = NULL;
585
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800586 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
587
588 struct pci_dev *pdev = adapter->pdev;
589 int pci_func = adapter->ahw.pci_func;
590
591 int err = 0;
592
593 /*
594 * Set the CRB window to invalid. If any register in window 0 is
595 * accessed it should set the window to 0 and then reset it to 1.
596 */
597 adapter->curr_window = 255;
598 adapter->ahw.qdr_sn_window = -1;
599 adapter->ahw.ddr_mn_window = -1;
600
601 /* remap phys address */
602 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
603 mem_len = pci_resource_len(pdev, 0);
604 pci_len0 = 0;
605
606 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
607 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
608 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
609 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800610 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
611 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
612 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
613
614 /* 128 Meg of memory */
615 if (mem_len == NETXEN_PCI_128MB_SIZE) {
616 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
617 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
618 SECOND_PAGE_GROUP_SIZE);
619 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
620 THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800621 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
622 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
623 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
624 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800625 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
626 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
627 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
628 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
629 adapter->pci_write_immediate =
630 netxen_nic_pci_write_immediate_2M;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800631 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
632 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
633 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
634
635 mem_ptr0 = pci_ioremap_bar(pdev, 0);
636 if (mem_ptr0 == NULL) {
637 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
638 return -EIO;
639 }
640 pci_len0 = mem_len;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800641
642 adapter->ahw.ddr_mn_window = 0;
643 adapter->ahw.qdr_sn_window = 0;
644
645 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
646 (pci_func * 0x20);
647 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
648 if (pci_func < 4)
649 adapter->ahw.ms_win_crb += (pci_func * 0x20);
650 else
651 adapter->ahw.ms_win_crb +=
652 0xA0 + ((pci_func - 4) * 0x10);
653 } else {
654 return -EIO;
655 }
656
657 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
658
659 adapter->ahw.pci_base0 = mem_ptr0;
660 adapter->ahw.pci_len0 = pci_len0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800661 adapter->ahw.pci_base1 = mem_ptr1;
662 adapter->ahw.pci_base2 = mem_ptr2;
663
664 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
665 goto skip_doorbell;
666
667 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
668 db_len = pci_resource_len(pdev, 4);
669
670 if (db_len == 0) {
671 printk(KERN_ERR "%s: doorbell is disabled\n",
672 netxen_nic_driver_name);
673 err = -EIO;
674 goto err_out;
675 }
676
677 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
678 if (!db_ptr) {
679 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
680 netxen_nic_driver_name);
681 err = -EIO;
682 goto err_out;
683 }
684
685skip_doorbell:
686 adapter->ahw.db_base = db_ptr;
687 adapter->ahw.db_len = db_len;
688 return 0;
689
690err_out:
691 netxen_cleanup_pci_map(adapter);
692 return err;
693}
694
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800695static int
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000696netxen_start_firmware(struct netxen_adapter *adapter, int request_fw)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800697{
698 int val, err, first_boot;
699 struct pci_dev *pdev = adapter->pdev;
700
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000701 int first_driver = 0;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000702
703 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
704 first_driver = (adapter->portnum == 0);
705 else
706 first_driver = (adapter->ahw.pci_func == 0);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000707
708 if (!first_driver)
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000709 goto wait_init;
Dhananjay Phadke0b72e652009-03-13 14:52:02 +0000710
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000711 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800712
713 err = netxen_check_hw_init(adapter, first_boot);
714 if (err) {
715 dev_err(&pdev->dev, "error in init HW init sequence\n");
716 return err;
717 }
718
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000719 if (request_fw)
720 netxen_request_firmware(adapter);
721
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000722 err = netxen_need_fw_reset(adapter);
723 if (err <= 0)
724 return err;
725
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800726 if (first_boot != 0x55555555) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000727 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800728 netxen_pinit_from_rom(adapter, 0);
729 msleep(1);
730 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000731
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000732 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000733 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
734 netxen_set_port_mode(adapter);
735
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800736 netxen_load_firmware(adapter);
737
738 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
739
740 /* Initialize multicast addr pool owners */
741 val = 0x7654;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000742 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800743 val |= 0x0f000000;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000744 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800745
746 }
747
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000748 err = netxen_init_dummy_dma(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800749 if (err)
750 return err;
751
752 /*
753 * Tell the hardware our version number.
754 */
755 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
756 | ((_NETXEN_NIC_LINUX_MINOR << 8))
757 | (_NETXEN_NIC_LINUX_SUBVERSION);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000758 NXWR32(adapter, CRB_DRIVER_VERSION, val);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800759
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000760wait_init:
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800761 /* Handshake with the card before we register the devices. */
762 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
763 if (err) {
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000764 netxen_free_dummy_dma(adapter);
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800765 return err;
766 }
767
Dhananjay Phadke68b3cae2009-07-26 20:07:36 +0000768 nx_update_dma_mask(adapter);
769
770 netxen_nic_get_firmware_info(adapter);
771
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800772 return 0;
773}
774
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000775static int
776netxen_nic_request_irq(struct netxen_adapter *adapter)
777{
778 irq_handler_t handler;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000779 struct nx_host_sds_ring *sds_ring;
780 int err, ring;
781
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000782 unsigned long flags = IRQF_SAMPLE_RANDOM;
783 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000784 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000785
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000786 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
787 handler = netxen_msix_intr;
788 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
789 handler = netxen_msi_intr;
790 else {
791 flags |= IRQF_SHARED;
792 handler = netxen_intr;
793 }
794 adapter->irq = netdev->irq;
795
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000796 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
797 sds_ring = &recv_ctx->sds_rings[ring];
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000798 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000799 err = request_irq(sds_ring->irq, handler,
800 flags, sds_ring->name, sds_ring);
801 if (err)
802 return err;
803 }
804
805 return 0;
806}
807
808static void
809netxen_nic_free_irq(struct netxen_adapter *adapter)
810{
811 int ring;
812 struct nx_host_sds_ring *sds_ring;
813
814 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
815
816 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
817 sds_ring = &recv_ctx->sds_rings[ring];
818 free_irq(sds_ring->irq, sds_ring);
819 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000820}
821
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000822static void
823netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
824{
825 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
826 adapter->coal.normal.data.rx_time_us =
827 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
828 adapter->coal.normal.data.rx_packets =
829 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
830 adapter->coal.normal.data.tx_time_us =
831 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
832 adapter->coal.normal.data.tx_packets =
833 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
834}
835
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000836static int
837netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
838{
839 int err;
840
841 err = adapter->init_port(adapter, adapter->physical_port);
842 if (err) {
843 printk(KERN_ERR "%s: Failed to initialize port %d\n",
844 netxen_nic_driver_name, adapter->portnum);
845 return err;
846 }
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000847 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
848 adapter->macaddr_set(adapter, netdev->dev_addr);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000849
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000850 adapter->set_multi(netdev);
851 adapter->set_mtu(adapter, netdev->mtu);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000852
853 adapter->ahw.linkup = 0;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000854
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000855 if (adapter->max_sds_rings > 1)
856 netxen_config_rss(adapter, 1);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000857
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000858 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
859 netxen_config_intr_coalesce(adapter);
860
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000861 netxen_napi_enable(adapter);
862
Dhananjay Phadke5103c9f2009-04-28 15:29:09 +0000863 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000864 netxen_linkevent_request(adapter, 1);
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000865 else
866 netxen_nic_set_link_parameters(adapter);
867
868 mod_timer(&adapter->watchdog_timer, jiffies);
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +0000869
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000870 return 0;
871}
872
873static void
874netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
875{
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000876 spin_lock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000877 netif_carrier_off(netdev);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000878 netif_tx_disable(netdev);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000879
880 if (adapter->stop_port)
881 adapter->stop_port(adapter);
882
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +0000883 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
884 netxen_p3_free_mac_list(adapter);
885
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000886 netxen_napi_disable(adapter);
887
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000888 netxen_release_tx_buffers(adapter);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000889 spin_unlock(&adapter->tx_clean_lock);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000890
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000891 del_timer_sync(&adapter->watchdog_timer);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000892 FLUSH_SCHEDULED_WORK();
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000893}
894
895
896static int
897netxen_nic_attach(struct netxen_adapter *adapter)
898{
899 struct net_device *netdev = adapter->netdev;
900 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000901 int err, ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000902 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000903 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000904
905 err = netxen_init_firmware(adapter);
906 if (err != 0) {
907 printk(KERN_ERR "Failed to init firmware\n");
908 return -EIO;
909 }
910
911 if (adapter->fw_major < 4)
912 adapter->max_rds_rings = 3;
913 else
914 adapter->max_rds_rings = 2;
915
916 err = netxen_alloc_sw_resources(adapter);
917 if (err) {
918 printk(KERN_ERR "%s: Error in setting sw resources\n",
919 netdev->name);
920 return err;
921 }
922
923 netxen_nic_clear_stats(adapter);
924
925 err = netxen_alloc_hw_resources(adapter);
926 if (err) {
927 printk(KERN_ERR "%s: Error in setting hw resources\n",
928 netdev->name);
929 goto err_out_free_sw;
930 }
931
932 if (adapter->fw_major < 4) {
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000933 tx_ring = adapter->tx_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000934 tx_ring->crb_cmd_producer = crb_cmd_producer[adapter->portnum];
935 tx_ring->crb_cmd_consumer = crb_cmd_consumer[adapter->portnum];
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000936
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +0000937 tx_ring->producer = 0;
938 tx_ring->sw_consumer = 0;
939
940 netxen_nic_update_cmd_producer(adapter, tx_ring);
941 netxen_nic_update_cmd_consumer(adapter, tx_ring);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000942 }
943
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000944 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
945 rds_ring = &adapter->recv_ctx.rds_rings[ring];
946 netxen_post_rx_buffers(adapter, ring, rds_ring);
947 }
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000948
949 err = netxen_nic_request_irq(adapter);
950 if (err) {
951 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
952 netdev->name);
953 goto err_out_free_rxbuf;
954 }
955
Dhananjay Phadkeca2ef332009-07-26 20:07:38 +0000956 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
957 netxen_nic_init_coalesce_defaults(adapter);
958
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000959 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
960 return 0;
961
962err_out_free_rxbuf:
963 netxen_release_rx_buffers(adapter);
964 netxen_free_hw_resources(adapter);
965err_out_free_sw:
966 netxen_free_sw_resources(adapter);
967 return err;
968}
969
970static void
971netxen_nic_detach(struct netxen_adapter *adapter)
972{
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000973 netxen_free_hw_resources(adapter);
Dhananjay Phadkecf981ff2009-07-17 15:27:06 +0000974 netxen_release_rx_buffers(adapter);
Dhananjay Phadkebe339ae2009-05-08 22:02:28 +0000975 netxen_nic_free_irq(adapter);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000976 netxen_free_sw_resources(adapter);
977
978 adapter->is_up = 0;
979}
980
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400981static int __devinit
982netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
983{
984 struct net_device *netdev = NULL;
985 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700986 int i = 0, err;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700987 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700988 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400989
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700990 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700991 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700992 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800993 return -ENODEV;
994 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700995
Dhananjay Phadke11d89d62008-08-08 00:08:45 -0700996 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
997 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
998 "will not be enabled.\n",
999 NX_P3_A0, NX_P3_B1);
1000 return -ENODEV;
1001 }
1002
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001003 if ((err = pci_enable_device(pdev)))
1004 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001005
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001006 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1007 err = -ENODEV;
1008 goto err_out_disable_pdev;
1009 }
1010
1011 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1012 goto err_out_disable_pdev;
1013
1014 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001015
1016 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1017 if(!netdev) {
1018 printk(KERN_ERR"%s: Failed to allocate memory for the "
1019 "device block.Check system memory resource"
1020 " usage.\n", netxen_nic_driver_name);
1021 goto err_out_free_res;
1022 }
1023
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001024 SET_NETDEV_DEV(netdev, &pdev->dev);
1025
Wang Chen4cf16532008-11-12 23:38:14 -08001026 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001027 adapter->netdev = netdev;
1028 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001029 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001030
1031 revision_id = pdev->revision;
1032 adapter->ahw.revision_id = revision_id;
1033
1034 err = nx_set_dma_mask(adapter, revision_id);
1035 if (err)
1036 goto err_out_free_netdev;
1037
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001038 rwlock_init(&adapter->adapter_lock);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001039 spin_lock_init(&adapter->tx_clean_lock);
Dhananjay Phadke5cf4d322009-05-05 19:05:07 +00001040 INIT_LIST_HEAD(&adapter->mac_list);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001041
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001042 err = netxen_setup_pci_map(adapter);
1043 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001044 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001045
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +05301046 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001047 adapter->portnum = pci_func_id;
Dhananjay Phadke200eef22007-09-03 10:33:35 +05301048 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -07001049 adapter->mc_enabled = 0;
Dhananjay Phadke83821a02008-08-01 03:14:58 -07001050 if (NX_IS_REVISION_P3(revision_id))
Dhananjay Phadke29566402008-07-21 19:44:04 -07001051 adapter->max_mc_count = 38;
Dhananjay Phadke83821a02008-08-01 03:14:58 -07001052 else
Dhananjay Phadke29566402008-07-21 19:44:04 -07001053 adapter->max_mc_count = 16;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001054
Stephen Hemminger1abd2662008-11-21 17:32:15 -08001055 netdev->netdev_ops = &netxen_netdev_ops;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001056 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001057
1058 netxen_nic_change_mtu(netdev, netdev->mtu);
1059
1060 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Stephen Hemminger1abd2662008-11-21 17:32:15 -08001061
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001062 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001063 netdev->features |= (NETIF_F_GRO);
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001064 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1065
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001066 if (NX_IS_REVISION_P3(revision_id)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001067 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1068 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001069 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001070
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001071 if (adapter->pci_using_dac) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001072 netdev->features |= NETIF_F_HIGHDMA;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001073 netdev->vlan_features |= NETIF_F_HIGHDMA;
1074 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001075
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001076 if (netxen_nic_get_board_info(adapter) != 0) {
1077 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -07001078 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001079 err = -EIO;
1080 goto err_out_iounmap;
1081 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001082
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001083 netxen_initialize_adapter_ops(adapter);
1084
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001085 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001086 switch (adapter->ahw.board_type) {
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001087 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1088 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1089 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001090 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -07001091 break;
1092 default:
1093 break;
1094 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -07001095
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001096 err = netxen_start_firmware(adapter, 1);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001097 if (err)
1098 goto err_out_iounmap;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001099 /*
1100 * See if the firmware gave us a virtual-physical port mapping.
1101 */
1102 adapter->physical_port = adapter->portnum;
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001103 if (adapter->fw_major < 4) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001104 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001105 if (i != 0x55555555)
1106 adapter->physical_port = i;
1107 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001108
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001109 netxen_check_options(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001110
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001111 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001112
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001113 netdev->irq = adapter->msix_entries[0].vector;
1114
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +00001115 if (netxen_napi_add(adapter, netdev))
1116 goto err_out_disable_msi;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001117
Dhananjay Phadke29566402008-07-21 19:44:04 -07001118 init_timer(&adapter->watchdog_timer);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001119 adapter->watchdog_timer.function = &netxen_watchdog;
1120 adapter->watchdog_timer.data = (unsigned long)adapter;
1121 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1122 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1123
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001124 err = netxen_read_mac_addr(adapter);
1125 if (err)
1126 dev_warn(&pdev->dev, "failed to read mac addr\n");
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001127
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001128 netif_carrier_off(netdev);
1129 netif_stop_queue(netdev);
1130
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001131 if ((err = register_netdev(netdev))) {
1132 printk(KERN_ERR "%s: register_netdev failed port #%d"
1133 " aborting\n", netxen_nic_driver_name,
1134 adapter->portnum);
1135 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001136 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001137 }
1138
1139 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001140
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001141 switch (adapter->ahw.port_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001142 case NETXEN_NIC_GBE:
1143 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1144 adapter->netdev->name);
1145 break;
1146 case NETXEN_NIC_XGBE:
1147 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1148 adapter->netdev->name);
1149 break;
1150 }
1151
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001152 return 0;
1153
Dhananjay Phadke29566402008-07-21 19:44:04 -07001154err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001155 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001156
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001157 netxen_free_dummy_dma(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001158
Dhananjay Phadke29566402008-07-21 19:44:04 -07001159err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001160 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001161
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001162err_out_free_netdev:
1163 free_netdev(netdev);
1164
1165err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001166 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001167
1168err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001169 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001170 pci_disable_device(pdev);
1171 return err;
1172}
1173
1174static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1175{
1176 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001177 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001178
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001179 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001180 if (adapter == NULL)
1181 return;
1182
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001183 netdev = adapter->netdev;
1184
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301185 unregister_netdev(netdev);
1186
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301187 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001188 netxen_nic_detach(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301189 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001190
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001191 if (adapter->portnum == 0)
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001192 netxen_free_dummy_dma(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301193
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001194 netxen_teardown_intr(adapter);
Dhananjay Phadke71dcddb2009-04-07 22:50:43 +00001195 netxen_free_sds_rings(&adapter->recv_ctx);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301196
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001197 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001198
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001199 netxen_release_firmware(adapter);
1200
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001201 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301202 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001203 pci_set_drvdata(pdev, NULL);
1204
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001205 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001206}
1207
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001208#ifdef CONFIG_PM
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001209static int
1210netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1211{
1212
1213 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1214 struct net_device *netdev = adapter->netdev;
1215
1216 netif_device_detach(netdev);
1217
1218 if (netif_running(netdev))
1219 netxen_nic_down(adapter, netdev);
1220
1221 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1222 netxen_nic_detach(adapter);
1223
1224 pci_save_state(pdev);
1225
1226 if (netxen_nic_wol_supported(adapter)) {
1227 pci_enable_wake(pdev, PCI_D3cold, 1);
1228 pci_enable_wake(pdev, PCI_D3hot, 1);
1229 }
1230
1231 pci_disable_device(pdev);
1232 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1233
1234 return 0;
1235}
1236
1237static int
1238netxen_nic_resume(struct pci_dev *pdev)
1239{
1240 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1241 struct net_device *netdev = adapter->netdev;
1242 int err;
1243
1244 pci_set_power_state(pdev, PCI_D0);
1245 pci_restore_state(pdev);
1246
1247 err = pci_enable_device(pdev);
1248 if (err)
1249 return err;
1250
1251 adapter->curr_window = 255;
1252
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001253 err = netxen_start_firmware(adapter, 0);
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001254 if (err) {
1255 dev_err(&pdev->dev, "failed to start firmware\n");
1256 return err;
1257 }
1258
1259 if (netif_running(netdev)) {
1260 err = netxen_nic_attach(adapter);
1261 if (err)
1262 return err;
1263
1264 err = netxen_nic_up(adapter, netdev);
1265 if (err)
1266 return err;
1267
1268 netif_device_attach(netdev);
1269 }
1270
1271 return 0;
1272}
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001273#endif
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001274
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001275static int netxen_nic_open(struct net_device *netdev)
1276{
Wang Chen4cf16532008-11-12 23:38:14 -08001277 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001278 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001279
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001280 if (adapter->driver_mismatch)
1281 return -EIO;
1282
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001283 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001284 err = netxen_nic_attach(adapter);
1285 if (err)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001286 return err;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001287 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001288
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001289 err = netxen_nic_up(adapter, netdev);
1290 if (err)
1291 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001292
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001293 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001294
1295 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001296
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001297err_out:
1298 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001299 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001300}
1301
1302/*
1303 * netxen_nic_close - Disables a network interface entry point
1304 */
1305static int netxen_nic_close(struct net_device *netdev)
1306{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001307 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001308
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001309 netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001310 return 0;
1311}
1312
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001313static bool netxen_tso_check(struct net_device *netdev,
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001314 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1315{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001316 bool tso = false;
1317 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001318 __be16 protocol = skb->protocol;
1319 u16 flags = 0;
1320
Harvey Harrison09640e62009-02-01 00:45:17 -08001321 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001322 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1323 protocol = vh->h_vlan_encapsulated_proto;
1324 flags = FLAGS_VLAN_TAGGED;
1325 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001326
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001327 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1328 skb_shinfo(skb)->gso_size > 0) {
1329
1330 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1331 desc->total_hdr_length =
1332 skb_transport_offset(skb) + tcp_hdrlen(skb);
1333
Harvey Harrison09640e62009-02-01 00:45:17 -08001334 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001335 TX_TCP_LSO6 : TX_TCP_LSO;
1336 tso = true;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001337
1338 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001339 u8 l4proto;
1340
Harvey Harrison09640e62009-02-01 00:45:17 -08001341 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001342 l4proto = ip_hdr(skb)->protocol;
1343
1344 if (l4proto == IPPROTO_TCP)
1345 opcode = TX_TCP_PKT;
1346 else if(l4proto == IPPROTO_UDP)
1347 opcode = TX_UDP_PKT;
Harvey Harrison09640e62009-02-01 00:45:17 -08001348 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001349 l4proto = ipv6_hdr(skb)->nexthdr;
1350
1351 if (l4proto == IPPROTO_TCP)
1352 opcode = TX_TCPV6_PKT;
1353 else if(l4proto == IPPROTO_UDP)
1354 opcode = TX_UDPV6_PKT;
1355 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001356 }
1357 desc->tcp_hdr_offset = skb_transport_offset(skb);
1358 desc->ip_hdr_offset = skb_network_offset(skb);
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001359 netxen_set_tx_flags_opcode(desc, flags, opcode);
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001360 return tso;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001361}
1362
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001363static void
1364netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1365 struct netxen_cmd_buffer *pbuf, int last)
1366{
1367 int k;
1368 struct netxen_skb_frag *buffrag;
1369
1370 buffrag = &pbuf->frag_array[0];
1371 pci_unmap_single(pdev, buffrag->dma,
1372 buffrag->length, PCI_DMA_TODEVICE);
1373
1374 for (k = 1; k < last; k++) {
1375 buffrag = &pbuf->frag_array[k];
1376 pci_unmap_page(pdev, buffrag->dma,
1377 buffrag->length, PCI_DMA_TODEVICE);
1378 }
1379}
1380
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001381static inline void
1382netxen_clear_cmddesc(u64 *desc)
1383{
1384 int i;
1385 for (i = 0; i < 8; i++)
1386 desc[i] = 0ULL;
1387}
1388
1389static int
1390netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001391{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001392 struct netxen_adapter *adapter = netdev_priv(netdev);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001393 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001394 unsigned int first_seg_len = skb->len - skb->data_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001395 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001396 struct netxen_skb_frag *buffrag;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001397 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001398 struct pci_dev *pdev = adapter->pdev;
1399 dma_addr_t temp_dma;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001400 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001401
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001402 u32 producer;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001403 int frag_count, no_of_desc;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001404 u32 num_txd = tx_ring->num_desc;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001405 bool is_tso = false;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001406
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001407 frag_count = skb_shinfo(skb)->nr_frags + 1;
1408
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001409 /* 4 fragments per cmd des */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001410 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001411
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001412 if (unlikely(no_of_desc + 2) > netxen_tx_avail(tx_ring)) {
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001413 netif_stop_queue(netdev);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001414 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001415 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001416
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001417 producer = tx_ring->producer;
1418
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001419 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001420 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001421 pbuf = &tx_ring->cmd_buf_arr[producer];
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001422
1423 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1424
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001425 pbuf->skb = skb;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001426 pbuf->frag_count = frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001427 buffrag = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001428 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001429 PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001430 if (pci_dma_mapping_error(pdev, temp_dma))
1431 goto drop_packet;
1432
1433 buffrag->dma = temp_dma;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001434 buffrag->length = first_seg_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001435 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1436 netxen_set_tx_port(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001437
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001438 hwdesc->buffer_length[0] = cpu_to_le16(first_seg_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001439 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1440
1441 for (i = 1, k = 1; i < frag_count; i++, k++) {
1442 struct skb_frag_struct *frag;
1443 int len, temp_len;
1444 unsigned long offset;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001445
1446 /* move to next desc. if there is a need */
1447 if ((i & 0x3) == 0) {
1448 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001449 producer = get_next_index(producer, num_txd);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001450 hwdesc = &tx_ring->desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001451 netxen_clear_cmddesc((u64 *)hwdesc);
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001452 pbuf = &tx_ring->cmd_buf_arr[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001453 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001454 }
1455 frag = &skb_shinfo(skb)->frags[i - 1];
1456 len = frag->size;
1457 offset = frag->page_offset;
1458
1459 temp_len = len;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001460 temp_dma = pci_map_page(pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001461 len, PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001462 if (pci_dma_mapping_error(pdev, temp_dma)) {
1463 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1464 goto drop_packet;
1465 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001466
1467 buffrag++;
1468 buffrag->dma = temp_dma;
1469 buffrag->length = temp_len;
1470
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001471 hwdesc->buffer_length[k] = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001472 switch (k) {
1473 case 0:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001474 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1475 break;
1476 case 1:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001477 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1478 break;
1479 case 2:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001480 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1481 break;
1482 case 3:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001483 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1484 break;
1485 }
1486 frag++;
1487 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001488 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001489
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001490 /* For LSO, we need to copy the MAC/IP/TCP headers into
1491 * the descriptor ring
1492 */
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001493 if (is_tso) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001494 int hdr_len, first_hdr_len, more_hdr;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001495 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001496 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1497 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001498 more_hdr = 1;
1499 } else {
1500 first_hdr_len = hdr_len;
1501 more_hdr = 0;
1502 }
1503 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001504 hwdesc = &tx_ring->desc_head[producer];
1505 pbuf = &tx_ring->cmd_buf_arr[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001506 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001507
1508 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001509 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001510 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001511 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001512
1513 if (more_hdr) {
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001514 hwdesc = &tx_ring->desc_head[producer];
1515 pbuf = &tx_ring->cmd_buf_arr[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001516 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001517 /* copy the next 64 bytes - should be enough except
1518 * for pathological case
1519 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001520 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1521 hwdesc,
1522 (hdr_len -
1523 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001524 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001525 }
1526 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001527
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001528 tx_ring->producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001529 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001530
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001531 netxen_nic_update_cmd_producer(adapter, tx_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001532
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001533 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001534
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001535 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001536
1537drop_packet:
1538 adapter->stats.txdropped++;
1539 dev_kfree_skb_any(skb);
1540 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001541}
1542
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001543static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1544{
1545 struct net_device *netdev = adapter->netdev;
1546 uint32_t temp, temp_state, temp_val;
1547 int rv = 0;
1548
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001549 temp = NXRD32(adapter, CRB_TEMP_STATE);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001550
1551 temp_state = nx_get_temp_state(temp);
1552 temp_val = nx_get_temp_val(temp);
1553
1554 if (temp_state == NX_TEMP_PANIC) {
1555 printk(KERN_ALERT
1556 "%s: Device temperature %d degrees C exceeds"
1557 " maximum allowed. Hardware has been shut down.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001558 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001559
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001560 netif_device_detach(netdev);
1561 netxen_nic_down(adapter, netdev);
1562 netxen_nic_detach(adapter);
1563
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001564 rv = 1;
1565 } else if (temp_state == NX_TEMP_WARN) {
1566 if (adapter->temp == NX_TEMP_NORMAL) {
1567 printk(KERN_ALERT
1568 "%s: Device temperature %d degrees C "
1569 "exceeds operating range."
1570 " Immediate action needed.\n",
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001571 netdev->name, temp_val);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001572 }
1573 } else {
1574 if (adapter->temp == NX_TEMP_WARN) {
1575 printk(KERN_INFO
1576 "%s: Device temperature is now %d degrees C"
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001577 " in normal range.\n", netdev->name,
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001578 temp_val);
1579 }
1580 }
1581 adapter->temp = temp_state;
1582 return rv;
1583}
1584
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001585void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001586{
1587 struct net_device *netdev = adapter->netdev;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001588
1589 if (adapter->ahw.linkup && !linkup) {
1590 printk(KERN_INFO "%s: %s NIC Link is down\n",
1591 netxen_nic_driver_name, netdev->name);
1592 adapter->ahw.linkup = 0;
1593 if (netif_running(netdev)) {
1594 netif_carrier_off(netdev);
1595 netif_stop_queue(netdev);
1596 }
1597
1598 if (!adapter->has_link_events)
1599 netxen_nic_set_link_parameters(adapter);
1600
1601 } else if (!adapter->ahw.linkup && linkup) {
1602 printk(KERN_INFO "%s: %s NIC Link is up\n",
1603 netxen_nic_driver_name, netdev->name);
1604 adapter->ahw.linkup = 1;
1605 if (netif_running(netdev)) {
1606 netif_carrier_on(netdev);
1607 netif_wake_queue(netdev);
1608 }
1609
1610 if (!adapter->has_link_events)
1611 netxen_nic_set_link_parameters(adapter);
1612 }
1613}
1614
1615static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1616{
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001617 u32 val, port, linkup;
1618
1619 port = adapter->physical_port;
1620
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001621 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001622 val = NXRD32(adapter, CRB_XG_STATE_P3);
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001623 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1624 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001625 } else {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001626 val = NXRD32(adapter, CRB_XG_STATE);
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001627 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001628 linkup = (val >> port) & 1;
1629 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001630 val = (val >> port*8) & 0xff;
1631 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001632 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001633 }
1634
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001635 netxen_advert_link_change(adapter, linkup);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001636}
1637
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001638static void netxen_watchdog(unsigned long v)
1639{
1640 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001641
1642 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001643}
1644
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001645void netxen_watchdog_task(struct work_struct *work)
1646{
1647 struct netxen_adapter *adapter =
1648 container_of(work, struct netxen_adapter, watchdog_task);
1649
Dhananjay Phadkee4135c22009-07-17 15:27:08 +00001650 if (netxen_nic_check_temp(adapter))
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001651 return;
1652
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001653 if (!adapter->has_link_events)
1654 netxen_nic_handle_phy_intr(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001655
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001656 if (netif_running(adapter->netdev))
1657 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001658}
1659
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001660static void netxen_tx_timeout(struct net_device *netdev)
1661{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001662 struct netxen_adapter *adapter = (struct netxen_adapter *)
1663 netdev_priv(netdev);
1664 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001665}
1666
David Howells6d5aefb2006-12-05 19:36:26 +00001667static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001668{
Jeff Garzik47906542007-11-23 21:23:36 -05001669 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001670 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001671
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001672 if (!netif_running(adapter->netdev))
1673 return;
1674
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001675 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001676 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001677
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001678 netxen_napi_disable(adapter);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001679
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001680 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001681
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001682 netxen_napi_enable(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001683 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001684}
1685
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001686struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1687{
1688 struct netxen_adapter *adapter = netdev_priv(netdev);
1689 struct net_device_stats *stats = &adapter->net_stats;
1690
1691 memset(stats, 0, sizeof(*stats));
1692
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001693 stats->rx_packets = adapter->stats.no_rcv;
Dhananjay Phadkee98e3352009-04-07 22:50:38 +00001694 stats->tx_packets = adapter->stats.xmitfinished;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001695 stats->rx_bytes = adapter->stats.rxbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001696 stats->tx_bytes = adapter->stats.txbytes;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001697 stats->rx_dropped = adapter->stats.rxdropped;
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001698 stats->tx_dropped = adapter->stats.txdropped;
1699
1700 return stats;
1701}
1702
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001703static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001704{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001705 struct nx_host_sds_ring *sds_ring = data;
1706 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001707 u32 status = 0;
1708
1709 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1710
1711 if (!(status & adapter->legacy_intr.int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001712 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301713
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001714 if (adapter->ahw.revision_id >= NX_P3_B1) {
1715 /* check interrupt state machine, to be sure */
1716 status = adapter->pci_read_immediate(adapter,
1717 ISR_INT_STATE_REG);
1718 if (!ISR_LEGACY_INT_TRIGGERED(status))
1719 return IRQ_NONE;
1720
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001721 } else {
1722 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001723
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001724 our_int = NXRD32(adapter, CRB_INT_VECTOR);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001725
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001726 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001727 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001728 return IRQ_NONE;
1729
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001730 /* claim interrupt */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001731 NXWR32(adapter, CRB_INT_VECTOR, (our_int & 0xffffffff));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301732 }
1733
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001734 /* clear interrupt */
1735 if (adapter->fw_major < 4)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001736 netxen_nic_disable_int(sds_ring);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001737
1738 adapter->pci_write_immediate(adapter,
1739 adapter->legacy_intr.tgt_status_reg,
1740 0xffffffff);
1741 /* read twice to ensure write is flushed */
1742 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1743 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1744
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001745 napi_schedule(&sds_ring->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001746
1747 return IRQ_HANDLED;
1748}
1749
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001750static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001751{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001752 struct nx_host_sds_ring *sds_ring = data;
1753 struct netxen_adapter *adapter = sds_ring->adapter;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001754
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001755 /* clear interrupt */
1756 adapter->pci_write_immediate(adapter,
Dhananjay Phadke7a2469c2009-05-08 22:02:27 +00001757 adapter->msi_tgt_status, 0xffffffff);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001758
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001759 napi_schedule(&sds_ring->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001760 return IRQ_HANDLED;
1761}
1762
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001763static irqreturn_t netxen_msix_intr(int irq, void *data)
1764{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001765 struct nx_host_sds_ring *sds_ring = data;
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001766
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001767 napi_schedule(&sds_ring->napi);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001768 return IRQ_HANDLED;
1769}
1770
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001771static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001772{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001773 struct nx_host_sds_ring *sds_ring =
1774 container_of(napi, struct nx_host_sds_ring, napi);
1775
1776 struct netxen_adapter *adapter = sds_ring->adapter;
1777
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001778 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001779 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001780
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001781 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001782
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001783 work_done = netxen_process_rcv_ring(sds_ring, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001784
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001785 if ((work_done < budget) && tx_complete) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001786 napi_complete(&sds_ring->napi);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001787 if (netif_running(adapter->netdev))
1788 netxen_nic_enable_int(sds_ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001789 }
1790
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001791 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001792}
1793
1794#ifdef CONFIG_NET_POLL_CONTROLLER
1795static void netxen_nic_poll_controller(struct net_device *netdev)
1796{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001797 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001798 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001799 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001800 enable_irq(adapter->irq);
1801}
1802#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001803
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001804#define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
1805
1806static int
1807netxen_destip_supported(struct netxen_adapter *adapter)
1808{
1809 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1810 return 0;
1811
1812 if (adapter->ahw.cut_through)
1813 return 0;
1814
1815 return 1;
1816}
1817
1818static int netxen_netdev_event(struct notifier_block *this,
1819 unsigned long event, void *ptr)
1820{
1821 struct netxen_adapter *adapter;
1822 struct net_device *dev = (struct net_device *)ptr;
1823 struct in_device *indev;
1824
1825recheck:
1826 if (dev == NULL)
1827 goto done;
1828
1829 if (dev->priv_flags & IFF_802_1Q_VLAN) {
1830 dev = vlan_dev_real_dev(dev);
1831 goto recheck;
1832 }
1833
1834 if (!is_netxen_netdev(dev))
1835 goto done;
1836
1837 adapter = netdev_priv(dev);
1838
1839 if (!adapter || !netxen_destip_supported(adapter))
1840 goto done;
1841
1842 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1843 goto done;
1844
1845 indev = in_dev_get(dev);
1846 if (!indev)
1847 goto done;
1848
1849 for_ifa(indev) {
1850 switch (event) {
1851 case NETDEV_UP:
1852 netxen_config_ipaddr(adapter,
1853 ifa->ifa_address, NX_IP_UP);
1854 break;
1855 case NETDEV_DOWN:
1856 netxen_config_ipaddr(adapter,
1857 ifa->ifa_address, NX_IP_DOWN);
1858 break;
1859 default:
1860 break;
1861 }
1862 } endfor_ifa(indev);
1863
1864 in_dev_put(indev);
1865done:
1866 return NOTIFY_DONE;
1867}
1868
1869static int
1870netxen_inetaddr_event(struct notifier_block *this,
1871 unsigned long event, void *ptr)
1872{
1873 struct netxen_adapter *adapter;
1874 struct net_device *dev;
1875
1876 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1877
1878 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
1879
1880recheck:
1881 if (dev == NULL || !netif_running(dev))
1882 goto done;
1883
1884 if (dev->priv_flags & IFF_802_1Q_VLAN) {
1885 dev = vlan_dev_real_dev(dev);
1886 goto recheck;
1887 }
1888
1889 if (!is_netxen_netdev(dev))
1890 goto done;
1891
1892 adapter = netdev_priv(dev);
1893
1894 if (!adapter || !netxen_destip_supported(adapter))
1895 goto done;
1896
1897 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1898 goto done;
1899
1900 switch (event) {
1901 case NETDEV_UP:
1902 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
1903 break;
1904 case NETDEV_DOWN:
1905 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
1906 break;
1907 default:
1908 break;
1909 }
1910
1911done:
1912 return NOTIFY_DONE;
1913}
1914
1915static struct notifier_block netxen_netdev_cb = {
1916 .notifier_call = netxen_netdev_event,
1917};
1918
1919static struct notifier_block netxen_inetaddr_cb = {
1920 .notifier_call = netxen_inetaddr_event,
1921};
1922
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001923static struct pci_driver netxen_driver = {
1924 .name = netxen_nic_driver_name,
1925 .id_table = netxen_pci_tbl,
1926 .probe = netxen_nic_probe,
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001927 .remove = __devexit_p(netxen_nic_remove),
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001928#ifdef CONFIG_PM
Dhananjay Phadke0b72e652009-03-13 14:52:02 +00001929 .suspend = netxen_nic_suspend,
1930 .resume = netxen_nic_resume
Dhananjay Phadke01542cd2009-06-22 20:26:20 +00001931#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001932};
1933
1934/* Driver Registration on NetXen card */
1935
1936static int __init netxen_init_module(void)
1937{
Dhananjay Phadkeff4fbd42009-03-13 14:52:06 +00001938 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
1939
Al Viro79ea13c2008-01-24 02:06:46 -08001940 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001941 return -ENOMEM;
1942
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001943 register_netdevice_notifier(&netxen_netdev_cb);
1944 register_inetaddr_notifier(&netxen_inetaddr_cb);
1945
Amit S. Kale184231b2007-01-22 06:52:53 -08001946 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001947}
1948
1949module_init(netxen_init_module);
1950
1951static void __exit netxen_exit_module(void)
1952{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001953 pci_unregister_driver(&netxen_driver);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001954
1955 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
1956 unregister_netdevice_notifier(&netxen_netdev_cb);
Amit S. Kale9de06612007-02-21 06:37:06 -08001957 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001958}
1959
1960module_exit(netxen_exit_module);