blob: 141bc4389fec9a1eb31467292898b85029540712 [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
Dhananjay Phadke5d242f12009-02-25 15:57:56 +00002 * Copyright (C) 2003 - 2009 NetXen, Inc.
Dhananjay Phadke13af7a62009-09-11 11:28:15 +00003 * Copyright (C) 2009 - QLogic Corporation.
Amit S. Kale3d396eb2006-10-21 15:33:03 -04004 * All rights reserved.
Amit S. Kale80922fb2006-12-04 09:18:00 -08005 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -04006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Amit S. Kale80922fb2006-12-04 09:18:00 -080010 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040011 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Amit S. Kale80922fb2006-12-04 09:18:00 -080015 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
Amit S. Kale80922fb2006-12-04 09:18:00 -080020 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040021 * The full GNU General Public License is included in this distribution
Amit Kumar Salecha4d21fef2010-01-14 01:53:23 +000022 * in the file called "COPYING".
Amit S. Kale80922fb2006-12-04 09:18:00 -080023 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040024 */
25
26#include <linux/netdevice.h>
27#include <linux/delay.h>
28#include "netxen_nic.h"
29#include "netxen_nic_hw.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040030
31struct crb_addr_pair {
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -080032 u32 addr;
33 u32 data;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040034};
35
36#define NETXEN_MAX_CRB_XFORM 60
37static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -080038#define NETXEN_ADDR_ERROR (0xffffffff)
Amit S. Kale3d396eb2006-10-21 15:33:03 -040039
40#define crb_addr_transform(name) \
41 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
42 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
43
Amit S. Kalecb8011a2006-11-29 09:00:10 -080044#define NETXEN_NIC_XDMA_RESET 0x8000ff
45
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +000046static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +000047netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
48 struct nx_host_rds_ring *rds_ring);
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +000049static int netxen_p3_has_mn(struct netxen_adapter *adapter);
Adrian Bunk993fb902007-11-05 18:07:31 +010050
Amit S. Kale3d396eb2006-10-21 15:33:03 -040051static void crb_addr_transform_setup(void)
52{
53 crb_addr_transform(XDMA);
54 crb_addr_transform(TIMR);
55 crb_addr_transform(SRE);
56 crb_addr_transform(SQN3);
57 crb_addr_transform(SQN2);
58 crb_addr_transform(SQN1);
59 crb_addr_transform(SQN0);
60 crb_addr_transform(SQS3);
61 crb_addr_transform(SQS2);
62 crb_addr_transform(SQS1);
63 crb_addr_transform(SQS0);
64 crb_addr_transform(RPMX7);
65 crb_addr_transform(RPMX6);
66 crb_addr_transform(RPMX5);
67 crb_addr_transform(RPMX4);
68 crb_addr_transform(RPMX3);
69 crb_addr_transform(RPMX2);
70 crb_addr_transform(RPMX1);
71 crb_addr_transform(RPMX0);
72 crb_addr_transform(ROMUSB);
73 crb_addr_transform(SN);
74 crb_addr_transform(QMN);
75 crb_addr_transform(QMS);
76 crb_addr_transform(PGNI);
77 crb_addr_transform(PGND);
78 crb_addr_transform(PGN3);
79 crb_addr_transform(PGN2);
80 crb_addr_transform(PGN1);
81 crb_addr_transform(PGN0);
82 crb_addr_transform(PGSI);
83 crb_addr_transform(PGSD);
84 crb_addr_transform(PGS3);
85 crb_addr_transform(PGS2);
86 crb_addr_transform(PGS1);
87 crb_addr_transform(PGS0);
88 crb_addr_transform(PS);
89 crb_addr_transform(PH);
90 crb_addr_transform(NIU);
91 crb_addr_transform(I2Q);
92 crb_addr_transform(EG);
93 crb_addr_transform(MN);
94 crb_addr_transform(MS);
95 crb_addr_transform(CAS2);
96 crb_addr_transform(CAS1);
97 crb_addr_transform(CAS0);
98 crb_addr_transform(CAM);
99 crb_addr_transform(C2C1);
100 crb_addr_transform(C2C0);
Amit S. Kale1fcca1a2007-02-05 07:35:26 -0800101 crb_addr_transform(SMB);
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -0700102 crb_addr_transform(OCM0);
103 crb_addr_transform(I2C0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400104}
105
Dhananjay Phadke29566402008-07-21 19:44:04 -0700106void netxen_release_rx_buffers(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400107{
Dhananjay Phadke29566402008-07-21 19:44:04 -0700108 struct netxen_recv_context *recv_ctx;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700109 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700110 struct netxen_rx_buffer *rx_buf;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000111 int i, ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400112
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000113 recv_ctx = &adapter->recv_ctx;
114 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
115 rds_ring = &recv_ctx->rds_rings[ring];
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000116 for (i = 0; i < rds_ring->num_desc; ++i) {
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000117 rx_buf = &(rds_ring->rx_buf_arr[i]);
118 if (rx_buf->state == NETXEN_BUFFER_FREE)
119 continue;
120 pci_unmap_single(adapter->pdev,
121 rx_buf->dma,
122 rds_ring->dma_size,
123 PCI_DMA_FROMDEVICE);
124 if (rx_buf->skb != NULL)
125 dev_kfree_skb_any(rx_buf->skb);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700126 }
127 }
128}
129
130void netxen_release_tx_buffers(struct netxen_adapter *adapter)
131{
132 struct netxen_cmd_buffer *cmd_buf;
133 struct netxen_skb_frag *buffrag;
134 int i, j;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000135 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700136
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000137 cmd_buf = tx_ring->cmd_buf_arr;
138 for (i = 0; i < tx_ring->num_desc; i++) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700139 buffrag = cmd_buf->frag_array;
140 if (buffrag->dma) {
141 pci_unmap_single(adapter->pdev, buffrag->dma,
142 buffrag->length, PCI_DMA_TODEVICE);
143 buffrag->dma = 0ULL;
144 }
145 for (j = 0; j < cmd_buf->frag_count; j++) {
146 buffrag++;
147 if (buffrag->dma) {
148 pci_unmap_page(adapter->pdev, buffrag->dma,
149 buffrag->length,
150 PCI_DMA_TODEVICE);
151 buffrag->dma = 0ULL;
152 }
153 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700154 if (cmd_buf->skb) {
155 dev_kfree_skb_any(cmd_buf->skb);
156 cmd_buf->skb = NULL;
157 }
158 cmd_buf++;
159 }
160}
161
162void netxen_free_sw_resources(struct netxen_adapter *adapter)
163{
164 struct netxen_recv_context *recv_ctx;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700165 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000166 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000167 int ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700168
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000169 recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000170
171 if (recv_ctx->rds_rings == NULL)
172 goto skip_rds;
173
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000174 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
175 rds_ring = &recv_ctx->rds_rings[ring];
Figo.zhangf2333a02009-06-11 02:49:10 -0700176 vfree(rds_ring->rx_buf_arr);
177 rds_ring->rx_buf_arr = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700178 }
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000179 kfree(recv_ctx->rds_rings);
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000180
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000181skip_rds:
182 if (adapter->tx_ring == NULL)
183 return;
184
185 tx_ring = adapter->tx_ring;
Figo.zhangf2333a02009-06-11 02:49:10 -0700186 vfree(tx_ring->cmd_buf_arr);
Amit Kumar Salecha011f4ea2010-01-07 22:10:14 +0000187 kfree(tx_ring);
188 adapter->tx_ring = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700189}
190
191int netxen_alloc_sw_resources(struct netxen_adapter *adapter)
192{
193 struct netxen_recv_context *recv_ctx;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700194 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000195 struct nx_host_sds_ring *sds_ring;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000196 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700197 struct netxen_rx_buffer *rx_buf;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000198 int ring, i, size;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700199
200 struct netxen_cmd_buffer *cmd_buf_arr;
201 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000202 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700203
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000204 size = sizeof(struct nx_host_tx_ring);
205 tx_ring = kzalloc(size, GFP_KERNEL);
206 if (tx_ring == NULL) {
207 dev_err(&pdev->dev, "%s: failed to allocate tx ring struct\n",
208 netdev->name);
209 return -ENOMEM;
210 }
211 adapter->tx_ring = tx_ring;
212
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000213 tx_ring->num_desc = adapter->num_txd;
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +0000214 tx_ring->txq = netdev_get_tx_queue(netdev, 0);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000215
216 cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700217 if (cmd_buf_arr == NULL) {
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000218 dev_err(&pdev->dev, "%s: failed to allocate cmd buffer ring\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700219 netdev->name);
220 return -ENOMEM;
221 }
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000222 memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
223 tx_ring->cmd_buf_arr = cmd_buf_arr;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700224
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000225 recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000226
227 size = adapter->max_rds_rings * sizeof (struct nx_host_rds_ring);
228 rds_ring = kzalloc(size, GFP_KERNEL);
229 if (rds_ring == NULL) {
230 dev_err(&pdev->dev, "%s: failed to allocate rds ring struct\n",
231 netdev->name);
232 return -ENOMEM;
233 }
234 recv_ctx->rds_rings = rds_ring;
235
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000236 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
237 rds_ring = &recv_ctx->rds_rings[ring];
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000238 switch (ring) {
239 case RCV_RING_NORMAL:
240 rds_ring->num_desc = adapter->num_rxd;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000241 if (adapter->ahw.cut_through) {
242 rds_ring->dma_size =
243 NX_CT_DEFAULT_RX_BUF_LEN;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700244 rds_ring->skb_size =
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000245 NX_CT_DEFAULT_RX_BUF_LEN;
246 } else {
Dhananjay Phadke9b08beb2009-07-26 20:07:44 +0000247 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
248 rds_ring->dma_size =
249 NX_P3_RX_BUF_MAX_LEN;
250 else
251 rds_ring->dma_size =
252 NX_P2_RX_BUF_MAX_LEN;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000253 rds_ring->skb_size =
Dhananjay Phadke9b08beb2009-07-26 20:07:44 +0000254 rds_ring->dma_size + NET_IP_ALIGN;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000255 }
256 break;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700257
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000258 case RCV_RING_JUMBO:
259 rds_ring->num_desc = adapter->num_jumbo_rxd;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000260 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
261 rds_ring->dma_size =
262 NX_P3_RX_JUMBO_BUF_MAX_LEN;
263 else
264 rds_ring->dma_size =
265 NX_P2_RX_JUMBO_BUF_MAX_LEN;
Dhananjay Phadkebc75e5b2009-09-03 13:10:53 +0000266
267 if (adapter->capabilities & NX_CAP0_HW_LRO)
268 rds_ring->dma_size += NX_LRO_BUFFER_EXTRA;
269
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000270 rds_ring->skb_size =
271 rds_ring->dma_size + NET_IP_ALIGN;
272 break;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700273
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000274 case RCV_RING_LRO:
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000275 rds_ring->num_desc = adapter->num_lro_rxd;
Dhananjay Phadke9b08beb2009-07-26 20:07:44 +0000276 rds_ring->dma_size = NX_RX_LRO_BUFFER_LENGTH;
277 rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000278 break;
279
280 }
281 rds_ring->rx_buf_arr = (struct netxen_rx_buffer *)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000282 vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000283 if (rds_ring->rx_buf_arr == NULL) {
284 printk(KERN_ERR "%s: Failed to allocate "
285 "rx buffer ring %d\n",
286 netdev->name, ring);
287 /* free whatever was already allocated */
288 goto err_out;
289 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000290 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000291 INIT_LIST_HEAD(&rds_ring->free_list);
292 /*
293 * Now go through all of them, set reference handles
294 * and put them in the queues.
295 */
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000296 rx_buf = rds_ring->rx_buf_arr;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000297 for (i = 0; i < rds_ring->num_desc; i++) {
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000298 list_add_tail(&rx_buf->list,
299 &rds_ring->free_list);
300 rx_buf->ref_handle = i;
301 rx_buf->state = NETXEN_BUFFER_FREE;
302 rx_buf++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400303 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000304 spin_lock_init(&rds_ring->lock);
305 }
306
307 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
308 sds_ring = &recv_ctx->sds_rings[ring];
309 sds_ring->irq = adapter->msix_entries[ring].vector;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000310 sds_ring->adapter = adapter;
311 sds_ring->num_desc = adapter->num_rxd;
312
313 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
314 INIT_LIST_HEAD(&sds_ring->free_list[i]);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400315 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700316
317 return 0;
318
319err_out:
320 netxen_free_sw_resources(adapter);
321 return -ENOMEM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400322}
323
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400324/*
325 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
326 * address to external PCI CRB address.
327 */
Adrian Bunk993fb902007-11-05 18:07:31 +0100328static u32 netxen_decode_crb_addr(u32 addr)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400329{
330 int i;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800331 u32 base_addr, offset, pci_base;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400332
333 crb_addr_transform_setup();
334
335 pci_base = NETXEN_ADDR_ERROR;
336 base_addr = addr & 0xfff00000;
337 offset = addr & 0x000fffff;
338
339 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
340 if (crb_addr_xform[i] == base_addr) {
341 pci_base = i << 20;
342 break;
343 }
344 }
345 if (pci_base == NETXEN_ADDR_ERROR)
346 return pci_base;
347 else
348 return (pci_base + offset);
349}
350
Dhananjay Phadkec9517e52009-08-24 19:23:26 +0000351#define NETXEN_MAX_ROM_WAIT_USEC 100
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400352
Adrian Bunk993fb902007-11-05 18:07:31 +0100353static int netxen_wait_rom_done(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400354{
355 long timeout = 0;
356 long done = 0;
357
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800358 cond_resched();
359
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400360 while (done == 0) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000361 done = NXRD32(adapter, NETXEN_ROMUSB_GLB_STATUS);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400362 done &= 2;
Dhananjay Phadkec9517e52009-08-24 19:23:26 +0000363 if (++timeout >= NETXEN_MAX_ROM_WAIT_USEC) {
364 dev_err(&adapter->pdev->dev,
365 "Timeout reached waiting for rom done");
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400366 return -EIO;
367 }
Dhananjay Phadkec9517e52009-08-24 19:23:26 +0000368 udelay(1);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400369 }
370 return 0;
371}
372
Adrian Bunk993fb902007-11-05 18:07:31 +0100373static int do_rom_fast_read(struct netxen_adapter *adapter,
374 int addr, int *valp)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400375{
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000376 NXWR32(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
377 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
378 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
379 NXWR32(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400380 if (netxen_wait_rom_done(adapter)) {
381 printk("Error waiting for rom done\n");
382 return -EIO;
383 }
384 /* reset abyte_cnt and dummy_byte_cnt */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000385 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800386 udelay(10);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000387 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400388
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000389 *valp = NXRD32(adapter, NETXEN_ROMUSB_ROM_RDATA);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400390 return 0;
391}
392
Adrian Bunk993fb902007-11-05 18:07:31 +0100393static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
394 u8 *bytes, size_t size)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800395{
396 int addridx;
397 int ret = 0;
398
399 for (addridx = addr; addridx < (addr + size); addridx += 4) {
Al Virof305f782007-12-22 19:44:00 +0000400 int v;
401 ret = do_rom_fast_read(adapter, addridx, &v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800402 if (ret != 0)
403 break;
Al Virof305f782007-12-22 19:44:00 +0000404 *(__le32 *)bytes = cpu_to_le32(v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800405 bytes += 4;
406 }
407
408 return ret;
409}
410
411int
Jeff Garzik47906542007-11-23 21:23:36 -0500412netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800413 u8 *bytes, size_t size)
414{
415 int ret;
416
Dhananjay Phadkec9517e52009-08-24 19:23:26 +0000417 ret = netxen_rom_lock(adapter);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800418 if (ret < 0)
419 return ret;
420
421 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
422
423 netxen_rom_unlock(adapter);
424 return ret;
425}
426
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400427int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
428{
429 int ret;
430
Dhananjay Phadkec9517e52009-08-24 19:23:26 +0000431 if (netxen_rom_lock(adapter) != 0)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400432 return -EIO;
433
434 ret = do_rom_fast_read(adapter, addr, valp);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800435 netxen_rom_unlock(adapter);
436 return ret;
437}
438
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400439#define NETXEN_BOARDTYPE 0x4008
440#define NETXEN_BOARDNUM 0x400c
441#define NETXEN_CHIPNUM 0x4010
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400442
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000443int netxen_pinit_from_rom(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400444{
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700445 int addr, val;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800446 int i, n, init_delay = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400447 struct crb_addr_pair *buf;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800448 unsigned offset;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800449 u32 off;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400450
451 /* resetall */
Dhananjay Phadkec9517e52009-08-24 19:23:26 +0000452 netxen_rom_lock(adapter);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000453 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0xffffffff);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800454 netxen_rom_unlock(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400455
Dhananjay Phadke29566402008-07-21 19:44:04 -0700456 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
457 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800458 (n != 0xcafecafe) ||
Dhananjay Phadke29566402008-07-21 19:44:04 -0700459 netxen_rom_fast_read(adapter, 4, &n) != 0) {
460 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
461 "n: %08x\n", netxen_nic_driver_name, n);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400462 return -EIO;
463 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700464 offset = n & 0xffffU;
465 n = (n >> 16) & 0xffffU;
466 } else {
467 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
468 !(n & 0x80000000)) {
469 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
470 "n: %08x\n", netxen_nic_driver_name, n);
471 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400472 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700473 offset = 1;
474 n &= ~0x80000000;
475 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400476
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000477 if (n >= 1024) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700478 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
479 " initialized.\n", __func__, n);
480 return -EIO;
481 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400482
Dhananjay Phadke29566402008-07-21 19:44:04 -0700483 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
484 if (buf == NULL) {
485 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
486 netxen_nic_driver_name);
487 return -ENOMEM;
488 }
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000489
Dhananjay Phadke29566402008-07-21 19:44:04 -0700490 for (i = 0; i < n; i++) {
491 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
Daniel Marjamäki584dbe92009-01-29 08:55:56 +0000492 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
493 kfree(buf);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700494 return -EIO;
Daniel Marjamäki584dbe92009-01-29 08:55:56 +0000495 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400496
Dhananjay Phadke29566402008-07-21 19:44:04 -0700497 buf[i].addr = addr;
498 buf[i].data = val;
499
Dhananjay Phadke29566402008-07-21 19:44:04 -0700500 }
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000501
Dhananjay Phadke29566402008-07-21 19:44:04 -0700502 for (i = 0; i < n; i++) {
503
504 off = netxen_decode_crb_addr(buf[i].addr);
505 if (off == NETXEN_ADDR_ERROR) {
506 printk(KERN_ERR"CRB init value out of range %x\n",
Amit S. Kale1fcca1a2007-02-05 07:35:26 -0800507 buf[i].addr);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700508 continue;
509 }
510 off += NETXEN_PCI_CRBSPACE;
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000511
512 if (off & 1)
513 continue;
514
Dhananjay Phadke29566402008-07-21 19:44:04 -0700515 /* skipping cold reboot MAGIC */
516 if (off == NETXEN_CAM_RAM(0x1fc))
517 continue;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400518
Dhananjay Phadke29566402008-07-21 19:44:04 -0700519 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
Dhananjay Phadke8bee0a92009-10-21 19:39:01 +0000520 if (off == (NETXEN_CRB_I2C0 + 0x1c))
521 continue;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700522 /* do not reset PCI */
523 if (off == (ROMUSB_GLB + 0xbc))
524 continue;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800525 if (off == (ROMUSB_GLB + 0xa8))
526 continue;
527 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
528 continue;
529 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
530 continue;
531 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
532 continue;
Amit Kumar Salechae7473f12009-11-29 21:54:55 -0800533 if ((off & 0x0ff00000) == NETXEN_CRB_DDR_NET)
534 continue;
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +0000535 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18) &&
536 !NX_IS_REVISION_P3P(adapter->ahw.revision_id))
Dhananjay Phadke29566402008-07-21 19:44:04 -0700537 buf[i].data = 0x1020;
538 /* skip the function enable register */
539 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
540 continue;
541 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
542 continue;
543 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
544 continue;
545 }
546
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800547 init_delay = 1;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700548 /* After writing this register, HW needs time for CRB */
549 /* to quiet down (else crb_window returns 0xffffffff) */
550 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800551 init_delay = 1000;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700552 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400553 /* hold xdma in reset also */
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800554 buf[i].data = NETXEN_NIC_XDMA_RESET;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800555 buf[i].data = 0x8000ff;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400556 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400557 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400558
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000559 NXWR32(adapter, off, buf[i].data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400560
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800561 msleep(init_delay);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400562 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700563 kfree(buf);
564
565 /* disable_peg_cache_all */
566
567 /* unreset_net_cache */
568 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000569 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
570 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700571 }
572
573 /* p2dn replyCount */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000574 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700575 /* disable_peg_cache 0 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000576 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700577 /* disable_peg_cache 1 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000578 NXWR32(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700579
580 /* peg_clr_all */
581
582 /* peg_clr 0 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000583 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
584 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700585 /* peg_clr 1 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000586 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
587 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700588 /* peg_clr 2 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000589 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
590 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700591 /* peg_clr 3 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000592 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
593 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400594 return 0;
595}
596
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000597static struct uni_table_desc *nx_get_table_desc(const u8 *unirom, int section)
598{
599 uint32_t i;
600 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
601 __le32 entries = cpu_to_le32(directory->num_entries);
602
603 for (i = 0; i < entries; i++) {
604
605 __le32 offs = cpu_to_le32(directory->findex) +
606 (i * cpu_to_le32(directory->entry_size));
607 __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
608
609 if (tab_type == section)
610 return (struct uni_table_desc *) &unirom[offs];
611 }
612
613 return NULL;
614}
615
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +0000616#define QLCNIC_FILEHEADER_SIZE (14 * 4)
617
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000618static int
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +0000619netxen_nic_validate_header(struct netxen_adapter *adapter)
620 {
621 const u8 *unirom = adapter->fw->data;
622 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
623 u32 fw_file_size = adapter->fw->size;
624 u32 tab_size;
625 __le32 entries;
626 __le32 entry_size;
627
628 if (fw_file_size < QLCNIC_FILEHEADER_SIZE)
629 return -EINVAL;
630
631 entries = cpu_to_le32(directory->num_entries);
632 entry_size = cpu_to_le32(directory->entry_size);
633 tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
634
635 if (fw_file_size < tab_size)
636 return -EINVAL;
637
638 return 0;
639}
640
641static int
642netxen_nic_validate_bootld(struct netxen_adapter *adapter)
643{
644 struct uni_table_desc *tab_desc;
645 struct uni_data_desc *descr;
646 const u8 *unirom = adapter->fw->data;
647 __le32 idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
648 NX_UNI_BOOTLD_IDX_OFF));
649 u32 offs;
650 u32 tab_size;
651 u32 data_size;
652
653 tab_desc = nx_get_table_desc(unirom, NX_UNI_DIR_SECT_BOOTLD);
654
655 if (!tab_desc)
656 return -EINVAL;
657
658 tab_size = cpu_to_le32(tab_desc->findex) +
659 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
660
661 if (adapter->fw->size < tab_size)
662 return -EINVAL;
663
664 offs = cpu_to_le32(tab_desc->findex) +
665 (cpu_to_le32(tab_desc->entry_size) * (idx));
666 descr = (struct uni_data_desc *)&unirom[offs];
667
668 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
669
670 if (adapter->fw->size < data_size)
671 return -EINVAL;
672
673 return 0;
674}
675
676static int
677netxen_nic_validate_fw(struct netxen_adapter *adapter)
678{
679 struct uni_table_desc *tab_desc;
680 struct uni_data_desc *descr;
681 const u8 *unirom = adapter->fw->data;
682 __le32 idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
683 NX_UNI_FIRMWARE_IDX_OFF));
684 u32 offs;
685 u32 tab_size;
686 u32 data_size;
687
688 tab_desc = nx_get_table_desc(unirom, NX_UNI_DIR_SECT_FW);
689
690 if (!tab_desc)
691 return -EINVAL;
692
693 tab_size = cpu_to_le32(tab_desc->findex) +
694 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
695
696 if (adapter->fw->size < tab_size)
697 return -EINVAL;
698
699 offs = cpu_to_le32(tab_desc->findex) +
700 (cpu_to_le32(tab_desc->entry_size) * (idx));
701 descr = (struct uni_data_desc *)&unirom[offs];
702 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
703
704 if (adapter->fw->size < data_size)
705 return -EINVAL;
706
707 return 0;
708}
709
710
711static int
712netxen_nic_validate_product_offs(struct netxen_adapter *adapter)
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000713{
714 struct uni_table_desc *ptab_descr;
715 const u8 *unirom = adapter->fw->data;
Dhananjay Phadke634d7df2009-12-05 12:23:56 +0000716 int mn_present = (NX_IS_REVISION_P2(adapter->ahw.revision_id)) ?
717 1 : netxen_p3_has_mn(adapter);
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +0000718 __le32 entries;
719 __le32 entry_size;
720 u32 tab_size;
721 u32 i;
Dhananjay Phadke634d7df2009-12-05 12:23:56 +0000722
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000723 ptab_descr = nx_get_table_desc(unirom, NX_UNI_DIR_SECT_PRODUCT_TBL);
724 if (ptab_descr == NULL)
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +0000725 return -EINVAL;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000726
727 entries = cpu_to_le32(ptab_descr->num_entries);
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +0000728 entry_size = cpu_to_le32(ptab_descr->entry_size);
729 tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
730
731 if (adapter->fw->size < tab_size)
732 return -EINVAL;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000733
Dhananjay Phadke634d7df2009-12-05 12:23:56 +0000734nomn:
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000735 for (i = 0; i < entries; i++) {
736
737 __le32 flags, file_chiprev, offs;
738 u8 chiprev = adapter->ahw.revision_id;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000739 uint32_t flagbit;
740
741 offs = cpu_to_le32(ptab_descr->findex) +
742 (i * cpu_to_le32(ptab_descr->entry_size));
743 flags = cpu_to_le32(*((int *)&unirom[offs] + NX_UNI_FLAGS_OFF));
744 file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
745 NX_UNI_CHIP_REV_OFF));
746
747 flagbit = mn_present ? 1 : 2;
748
749 if ((chiprev == file_chiprev) &&
750 ((1ULL << flagbit) & flags)) {
751 adapter->file_prd_off = offs;
752 return 0;
753 }
754 }
755
Dhananjay Phadke634d7df2009-12-05 12:23:56 +0000756 if (mn_present && NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
757 mn_present = 0;
758 goto nomn;
759 }
760
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +0000761 return -EINVAL;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000762}
763
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +0000764static int
765netxen_nic_validate_unified_romimage(struct netxen_adapter *adapter)
766{
767 if (netxen_nic_validate_header(adapter)) {
768 dev_err(&adapter->pdev->dev,
769 "unified image: header validation failed\n");
770 return -EINVAL;
771 }
772
773 if (netxen_nic_validate_product_offs(adapter)) {
774 dev_err(&adapter->pdev->dev,
775 "unified image: product validation failed\n");
776 return -EINVAL;
777 }
778
779 if (netxen_nic_validate_bootld(adapter)) {
780 dev_err(&adapter->pdev->dev,
781 "unified image: bootld validation failed\n");
782 return -EINVAL;
783 }
784
785 if (netxen_nic_validate_fw(adapter)) {
786 dev_err(&adapter->pdev->dev,
787 "unified image: firmware validation failed\n");
788 return -EINVAL;
789 }
790
791 return 0;
792}
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000793
794static struct uni_data_desc *nx_get_data_desc(struct netxen_adapter *adapter,
795 u32 section, u32 idx_offset)
796{
797 const u8 *unirom = adapter->fw->data;
798 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
799 idx_offset));
800 struct uni_table_desc *tab_desc;
801 __le32 offs;
802
803 tab_desc = nx_get_table_desc(unirom, section);
804
805 if (tab_desc == NULL)
806 return NULL;
807
808 offs = cpu_to_le32(tab_desc->findex) +
809 (cpu_to_le32(tab_desc->entry_size) * idx);
810
811 return (struct uni_data_desc *)&unirom[offs];
812}
813
814static u8 *
815nx_get_bootld_offs(struct netxen_adapter *adapter)
816{
817 u32 offs = NETXEN_BOOTLD_START;
818
819 if (adapter->fw_type == NX_UNIFIED_ROMIMAGE)
820 offs = cpu_to_le32((nx_get_data_desc(adapter,
821 NX_UNI_DIR_SECT_BOOTLD,
822 NX_UNI_BOOTLD_IDX_OFF))->findex);
823
824 return (u8 *)&adapter->fw->data[offs];
825}
826
827static u8 *
828nx_get_fw_offs(struct netxen_adapter *adapter)
829{
830 u32 offs = NETXEN_IMAGE_START;
831
832 if (adapter->fw_type == NX_UNIFIED_ROMIMAGE)
833 offs = cpu_to_le32((nx_get_data_desc(adapter,
834 NX_UNI_DIR_SECT_FW,
835 NX_UNI_FIRMWARE_IDX_OFF))->findex);
836
837 return (u8 *)&adapter->fw->data[offs];
838}
839
840static __le32
841nx_get_fw_size(struct netxen_adapter *adapter)
842{
843 if (adapter->fw_type == NX_UNIFIED_ROMIMAGE)
844 return cpu_to_le32((nx_get_data_desc(adapter,
845 NX_UNI_DIR_SECT_FW,
846 NX_UNI_FIRMWARE_IDX_OFF))->size);
847 else
848 return cpu_to_le32(
849 *(u32 *)&adapter->fw->data[NX_FW_SIZE_OFFSET]);
850}
851
852static __le32
853nx_get_fw_version(struct netxen_adapter *adapter)
854{
855 struct uni_data_desc *fw_data_desc;
856 const struct firmware *fw = adapter->fw;
857 __le32 major, minor, sub;
858 const u8 *ver_str;
859 int i, ret = 0;
860
861 if (adapter->fw_type == NX_UNIFIED_ROMIMAGE) {
862
863 fw_data_desc = nx_get_data_desc(adapter,
864 NX_UNI_DIR_SECT_FW, NX_UNI_FIRMWARE_IDX_OFF);
865 ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
866 cpu_to_le32(fw_data_desc->size) - 17;
867
868 for (i = 0; i < 12; i++) {
869 if (!strncmp(&ver_str[i], "REV=", 4)) {
870 ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
871 &major, &minor, &sub);
872 break;
873 }
874 }
875
876 if (ret != 3)
877 return 0;
878
879 return major + (minor << 8) + (sub << 16);
880
881 } else
882 return cpu_to_le32(*(u32 *)&fw->data[NX_FW_VERSION_OFFSET]);
883}
884
885static __le32
886nx_get_bios_version(struct netxen_adapter *adapter)
887{
888 const struct firmware *fw = adapter->fw;
889 __le32 bios_ver, prd_off = adapter->file_prd_off;
890
891 if (adapter->fw_type == NX_UNIFIED_ROMIMAGE) {
892 bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
893 + NX_UNI_BIOS_VERSION_OFF));
894 return (bios_ver << 24) + ((bios_ver >> 8) & 0xff00) +
895 (bios_ver >> 24);
896 } else
897 return cpu_to_le32(*(u32 *)&fw->data[NX_BIOS_VERSION_OFFSET]);
898
899}
900
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000901int
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000902netxen_need_fw_reset(struct netxen_adapter *adapter)
903{
904 u32 count, old_count;
905 u32 val, version, major, minor, build;
906 int i, timeout;
907 u8 fw_type;
908
909 /* NX2031 firmware doesn't support heartbit */
910 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
911 return 1;
912
Amit Kumar Salecha6a808c62009-12-08 20:40:55 +0000913 if (adapter->need_fw_reset)
914 return 1;
915
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000916 /* last attempt had failed */
917 if (NXRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED)
918 return 1;
919
Amit Kumar Salecha581e8ae2010-01-07 22:10:15 +0000920 old_count = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000921
922 for (i = 0; i < 10; i++) {
923
924 timeout = msleep_interruptible(200);
925 if (timeout) {
926 NXWR32(adapter, CRB_CMDPEG_STATE,
927 PHAN_INITIALIZE_FAILED);
928 return -EINTR;
929 }
930
931 count = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
932 if (count != old_count)
933 break;
934 }
935
936 /* firmware is dead */
937 if (count == old_count)
938 return 1;
939
940 /* check if we have got newer or different file firmware */
941 if (adapter->fw) {
942
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000943 val = nx_get_fw_version(adapter);
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000944
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000945 version = NETXEN_DECODE_VERSION(val);
946
947 major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
948 minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
949 build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
950
951 if (version > NETXEN_VERSION_CODE(major, minor, build))
952 return 1;
953
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000954 if (version == NETXEN_VERSION_CODE(major, minor, build) &&
955 adapter->fw_type != NX_UNIFIED_ROMIMAGE) {
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000956
957 val = NXRD32(adapter, NETXEN_MIU_MN_CONTROL);
958 fw_type = (val & 0x4) ?
959 NX_P3_CT_ROMIMAGE : NX_P3_MN_ROMIMAGE;
960
961 if (adapter->fw_type != fw_type)
962 return 1;
963 }
964 }
965
966 return 0;
967}
968
969static char *fw_name[] = {
Dhananjay Phadke7e8e5d92009-10-24 16:04:02 +0000970 NX_P2_MN_ROMIMAGE_NAME,
971 NX_P3_CT_ROMIMAGE_NAME,
972 NX_P3_MN_ROMIMAGE_NAME,
973 NX_UNIFIED_ROMIMAGE_NAME,
974 NX_FLASH_ROMIMAGE_NAME,
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000975};
976
977int
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000978netxen_load_firmware(struct netxen_adapter *adapter)
979{
980 u64 *ptr64;
981 u32 i, flashaddr, size;
982 const struct firmware *fw = adapter->fw;
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000983 struct pci_dev *pdev = adapter->pdev;
984
985 dev_info(&pdev->dev, "loading firmware from %s\n",
986 fw_name[adapter->fw_type]);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000987
988 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
989 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 1);
990
991 if (fw) {
992 __le64 data;
993
994 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 8;
995
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +0000996 ptr64 = (u64 *)nx_get_bootld_offs(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000997 flashaddr = NETXEN_BOOTLD_START;
998
999 for (i = 0; i < size; i++) {
1000 data = cpu_to_le64(ptr64[i]);
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001001
1002 if (adapter->pci_mem_write(adapter, flashaddr, data))
Amit Kumar Salecha1f5e0552009-10-13 05:31:41 +00001003 return -EIO;
1004
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001005 flashaddr += 8;
1006 }
1007
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001008 size = (__force u32)nx_get_fw_size(adapter) / 8;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001009
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001010 ptr64 = (u64 *)nx_get_fw_offs(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001011 flashaddr = NETXEN_IMAGE_START;
1012
1013 for (i = 0; i < size; i++) {
1014 data = cpu_to_le64(ptr64[i]);
1015
1016 if (adapter->pci_mem_write(adapter,
Amit Kumar Salecha1f5e0552009-10-13 05:31:41 +00001017 flashaddr, data))
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001018 return -EIO;
1019
1020 flashaddr += 8;
1021 }
1022 } else {
Amit Kumar Salechaf78c0852009-09-11 11:28:11 +00001023 u64 data;
1024 u32 hi, lo;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001025
Amit Kumar Salechaf78c0852009-09-11 11:28:11 +00001026 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 8;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001027 flashaddr = NETXEN_BOOTLD_START;
1028
1029 for (i = 0; i < size; i++) {
1030 if (netxen_rom_fast_read(adapter,
Amit Kumar Salecha1f5e0552009-10-13 05:31:41 +00001031 flashaddr, (int *)&lo) != 0)
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001032 return -EIO;
Amit Kumar Salechaf78c0852009-09-11 11:28:11 +00001033 if (netxen_rom_fast_read(adapter,
Amit Kumar Salecha1f5e0552009-10-13 05:31:41 +00001034 flashaddr + 4, (int *)&hi) != 0)
Amit Kumar Salechaf78c0852009-09-11 11:28:11 +00001035 return -EIO;
1036
1037 /* hi, lo are already in host endian byteorder */
1038 data = (((u64)hi << 32) | lo);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001039
1040 if (adapter->pci_mem_write(adapter,
Amit Kumar Salecha1f5e0552009-10-13 05:31:41 +00001041 flashaddr, data))
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001042 return -EIO;
1043
Amit Kumar Salechaf78c0852009-09-11 11:28:11 +00001044 flashaddr += 8;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001045 }
1046 }
1047 msleep(1);
1048
Amit Kumar Salecha0be367b2009-10-16 15:50:08 +00001049 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
1050 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0x18, 0x1020);
1051 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0x80001e);
1052 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001053 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0x80001d);
1054 else {
1055 NXWR32(adapter, NETXEN_ROMUSB_GLB_CHIP_CLK_CTRL, 0x3fff);
1056 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 0);
1057 }
1058
1059 return 0;
1060}
1061
1062static int
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001063netxen_validate_firmware(struct netxen_adapter *adapter)
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001064{
1065 __le32 val;
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +00001066 u32 ver, min_ver, bios;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001067 struct pci_dev *pdev = adapter->pdev;
1068 const struct firmware *fw = adapter->fw;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001069 u8 fw_type = adapter->fw_type;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001070
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001071 if (fw_type == NX_UNIFIED_ROMIMAGE) {
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +00001072 if (netxen_nic_validate_unified_romimage(adapter))
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001073 return -EINVAL;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001074 } else {
1075 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_MAGIC_OFFSET]);
1076 if ((__force u32)val != NETXEN_BDINFO_MAGIC)
1077 return -EINVAL;
1078
Rajesh K Borundia10c0f2a2010-03-29 02:43:43 +00001079 if (fw->size < NX_FW_MIN_SIZE)
1080 return -EINVAL;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001081 }
1082
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001083 val = nx_get_fw_version(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001084
1085 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1086 min_ver = NETXEN_VERSION_CODE(4, 0, 216);
1087 else
1088 min_ver = NETXEN_VERSION_CODE(3, 4, 216);
1089
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +00001090 ver = NETXEN_DECODE_VERSION(val);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001091
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +00001092 if ((_major(ver) > _NETXEN_NIC_LINUX_MAJOR) || (ver < min_ver)) {
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001093 dev_err(&pdev->dev,
1094 "%s: firmware version %d.%d.%d unsupported\n",
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001095 fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001096 return -EINVAL;
1097 }
1098
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001099 val = nx_get_bios_version(adapter);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001100 netxen_rom_fast_read(adapter, NX_BIOS_VERSION_OFFSET, (int *)&bios);
1101 if ((__force u32)val != bios) {
1102 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001103 fw_name[fw_type]);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001104 return -EINVAL;
1105 }
1106
1107 /* check if flashed firmware is newer */
1108 if (netxen_rom_fast_read(adapter,
1109 NX_FW_VERSION_OFFSET, (int *)&val))
1110 return -EIO;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +00001111 val = NETXEN_DECODE_VERSION(val);
1112 if (val > ver) {
1113 dev_info(&pdev->dev, "%s: firmware is older than flash\n",
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001114 fw_name[fw_type]);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001115 return -EINVAL;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +00001116 }
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001117
1118 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
1119 return 0;
1120}
1121
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001122static void
1123nx_get_next_fwtype(struct netxen_adapter *adapter)
1124{
1125 u8 fw_type;
1126
1127 switch (adapter->fw_type) {
1128 case NX_UNKNOWN_ROMIMAGE:
1129 fw_type = NX_UNIFIED_ROMIMAGE;
1130 break;
1131
1132 case NX_UNIFIED_ROMIMAGE:
1133 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id))
1134 fw_type = NX_FLASH_ROMIMAGE;
1135 else if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1136 fw_type = NX_P2_MN_ROMIMAGE;
1137 else if (netxen_p3_has_mn(adapter))
1138 fw_type = NX_P3_MN_ROMIMAGE;
1139 else
1140 fw_type = NX_P3_CT_ROMIMAGE;
1141 break;
1142
1143 case NX_P3_MN_ROMIMAGE:
1144 fw_type = NX_P3_CT_ROMIMAGE;
1145 break;
1146
1147 case NX_P2_MN_ROMIMAGE:
1148 case NX_P3_CT_ROMIMAGE:
1149 default:
1150 fw_type = NX_FLASH_ROMIMAGE;
1151 break;
1152 }
1153
1154 adapter->fw_type = fw_type;
1155}
1156
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001157static int
1158netxen_p3_has_mn(struct netxen_adapter *adapter)
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001159{
1160 u32 capability, flashed_ver;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001161 capability = 0;
1162
Dhananjay Phadke634d7df2009-12-05 12:23:56 +00001163 /* NX2031 always had MN */
1164 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1165 return 1;
1166
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001167 netxen_rom_fast_read(adapter,
1168 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +00001169 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
1170
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001171 if (flashed_ver >= NETXEN_VERSION_CODE(4, 0, 220)) {
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001172
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001173 capability = NXRD32(adapter, NX_PEG_TUNE_CAPABILITY);
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001174 if (capability & NX_PEG_TUNE_MN_PRESENT)
1175 return 1;
1176 }
1177 return 0;
1178}
1179
1180void netxen_request_firmware(struct netxen_adapter *adapter)
1181{
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001182 struct pci_dev *pdev = adapter->pdev;
1183 int rc = 0;
1184
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001185 adapter->fw_type = NX_UNKNOWN_ROMIMAGE;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001186
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001187next:
1188 nx_get_next_fwtype(adapter);
Dhananjay Phadke7cecdca12009-10-16 15:50:10 +00001189
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001190 if (adapter->fw_type == NX_FLASH_ROMIMAGE) {
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001191 adapter->fw = NULL;
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001192 } else {
1193 rc = request_firmware(&adapter->fw,
1194 fw_name[adapter->fw_type], &pdev->dev);
1195 if (rc != 0)
1196 goto next;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001197
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001198 rc = netxen_validate_firmware(adapter);
1199 if (rc != 0) {
1200 release_firmware(adapter->fw);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001201 msleep(1);
Amit Kumar Salechaf50330f2009-10-24 16:03:58 +00001202 goto next;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001203 }
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001204 }
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001205}
1206
1207
1208void
1209netxen_release_firmware(struct netxen_adapter *adapter)
1210{
1211 if (adapter->fw)
1212 release_firmware(adapter->fw);
Dhananjay Phadkedb4cfd82009-09-05 17:43:07 +00001213 adapter->fw = NULL;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +00001214}
1215
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001216int netxen_init_dummy_dma(struct netxen_adapter *adapter)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001217{
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001218 u64 addr;
1219 u32 hi, lo;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001220
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001221 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
1222 return 0;
1223
1224 adapter->dummy_dma.addr = pci_alloc_consistent(adapter->pdev,
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001225 NETXEN_HOST_DUMMY_DMA_SIZE,
1226 &adapter->dummy_dma.phys_addr);
1227 if (adapter->dummy_dma.addr == NULL) {
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001228 dev_err(&adapter->pdev->dev,
1229 "ERROR: Could not allocate dummy DMA memory\n");
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001230 return -ENOMEM;
1231 }
1232
1233 addr = (uint64_t) adapter->dummy_dma.phys_addr;
1234 hi = (addr >> 32) & 0xffffffff;
1235 lo = addr & 0xffffffff;
1236
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001237 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
1238 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001239
1240 return 0;
1241}
1242
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001243/*
1244 * NetXen DMA watchdog control:
1245 *
1246 * Bit 0 : enabled => R/O: 1 watchdog active, 0 inactive
1247 * Bit 1 : disable_request => 1 req disable dma watchdog
1248 * Bit 2 : enable_request => 1 req enable dma watchdog
1249 * Bit 3-31 : unused
1250 */
1251void netxen_free_dummy_dma(struct netxen_adapter *adapter)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001252{
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -07001253 int i = 100;
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001254 u32 ctrl;
1255
1256 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
1257 return;
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001258
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -07001259 if (!adapter->dummy_dma.addr)
1260 return;
1261
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001262 ctrl = NXRD32(adapter, NETXEN_DMA_WATCHDOG_CTRL);
1263 if ((ctrl & 0x1) != 0) {
1264 NXWR32(adapter, NETXEN_DMA_WATCHDOG_CTRL, (ctrl | 0x2));
1265
1266 while ((ctrl & 0x1) != 0) {
1267
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001268 msleep(50);
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001269
1270 ctrl = NXRD32(adapter, NETXEN_DMA_WATCHDOG_CTRL);
1271
1272 if (--i == 0)
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001273 break;
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001274 };
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -07001275 }
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001276
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -07001277 if (i) {
1278 pci_free_consistent(adapter->pdev,
1279 NETXEN_HOST_DUMMY_DMA_SIZE,
1280 adapter->dummy_dma.addr,
1281 adapter->dummy_dma.phys_addr);
1282 adapter->dummy_dma.addr = NULL;
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001283 } else
1284 dev_err(&adapter->pdev->dev, "dma_watchdog_shutdown failed\n");
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001285}
1286
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301287int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001288{
1289 u32 val = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001290 int retries = 60;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001291
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001292 if (pegtune_val)
1293 return 0;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301294
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001295 do {
1296 val = NXRD32(adapter, CRB_CMDPEG_STATE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301297
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001298 switch (val) {
1299 case PHAN_INITIALIZE_COMPLETE:
1300 case PHAN_INITIALIZE_ACK:
1301 return 0;
1302 case PHAN_INITIALIZE_FAILED:
1303 goto out_err;
1304 default:
1305 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001306 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301307
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001308 msleep(500);
1309
1310 } while (--retries);
1311
1312 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
1313
1314out_err:
1315 dev_warn(&adapter->pdev->dev, "firmware init failed\n");
1316 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001317}
1318
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001319static int
1320netxen_receive_peg_ready(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -07001321{
1322 u32 val = 0;
1323 int retries = 2000;
1324
1325 do {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001326 val = NXRD32(adapter, CRB_RCVPEG_STATE);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001327
1328 if (val == PHAN_PEG_RCV_INITIALIZED)
1329 return 0;
1330
1331 msleep(10);
1332
1333 } while (--retries);
1334
1335 if (!retries) {
1336 printk(KERN_ERR "Receive Peg initialization not "
1337 "complete, state: 0x%x.\n", val);
1338 return -EIO;
1339 }
1340
1341 return 0;
1342}
1343
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001344int netxen_init_firmware(struct netxen_adapter *adapter)
1345{
1346 int err;
1347
1348 err = netxen_receive_peg_ready(adapter);
1349 if (err)
1350 return err;
1351
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001352 NXWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
1353 NXWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
1354 NXWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
1355 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001356
1357 return err;
1358}
1359
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001360static void
1361netxen_handle_linkevent(struct netxen_adapter *adapter, nx_fw_msg_t *msg)
1362{
1363 u32 cable_OUI;
1364 u16 cable_len;
1365 u16 link_speed;
1366 u8 link_status, module, duplex, autoneg;
1367 struct net_device *netdev = adapter->netdev;
1368
1369 adapter->has_link_events = 1;
1370
1371 cable_OUI = msg->body[1] & 0xffffffff;
1372 cable_len = (msg->body[1] >> 32) & 0xffff;
1373 link_speed = (msg->body[1] >> 48) & 0xffff;
1374
1375 link_status = msg->body[2] & 0xff;
1376 duplex = (msg->body[2] >> 16) & 0xff;
1377 autoneg = (msg->body[2] >> 24) & 0xff;
1378
1379 module = (msg->body[2] >> 8) & 0xff;
1380 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) {
1381 printk(KERN_INFO "%s: unsupported cable: OUI 0x%x, length %d\n",
1382 netdev->name, cable_OUI, cable_len);
1383 } else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) {
1384 printk(KERN_INFO "%s: unsupported cable length %d\n",
1385 netdev->name, cable_len);
1386 }
1387
1388 netxen_advert_link_change(adapter, link_status);
1389
1390 /* update link parameters */
1391 if (duplex == LINKEVENT_FULL_DUPLEX)
1392 adapter->link_duplex = DUPLEX_FULL;
1393 else
1394 adapter->link_duplex = DUPLEX_HALF;
1395 adapter->module_type = module;
1396 adapter->link_autoneg = autoneg;
1397 adapter->link_speed = link_speed;
1398}
1399
1400static void
1401netxen_handle_fw_message(int desc_cnt, int index,
1402 struct nx_host_sds_ring *sds_ring)
1403{
1404 nx_fw_msg_t msg;
1405 struct status_desc *desc;
1406 int i = 0, opcode;
1407
1408 while (desc_cnt > 0 && i < 8) {
1409 desc = &sds_ring->desc_head[index];
1410 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1411 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1412
1413 index = get_next_index(index, sds_ring->num_desc);
1414 desc_cnt--;
1415 }
1416
1417 opcode = netxen_get_nic_msg_opcode(msg.body[0]);
1418 switch (opcode) {
1419 case NX_NIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1420 netxen_handle_linkevent(sds_ring->adapter, &msg);
1421 break;
1422 default:
1423 break;
1424 }
1425}
1426
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001427static int
1428netxen_alloc_rx_skb(struct netxen_adapter *adapter,
1429 struct nx_host_rds_ring *rds_ring,
1430 struct netxen_rx_buffer *buffer)
1431{
1432 struct sk_buff *skb;
1433 dma_addr_t dma;
1434 struct pci_dev *pdev = adapter->pdev;
1435
1436 buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1437 if (!buffer->skb)
1438 return 1;
1439
1440 skb = buffer->skb;
1441
1442 if (!adapter->ahw.cut_through)
1443 skb_reserve(skb, 2);
1444
1445 dma = pci_map_single(pdev, skb->data,
1446 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1447
1448 if (pci_dma_mapping_error(pdev, dma)) {
1449 dev_kfree_skb_any(skb);
1450 buffer->skb = NULL;
1451 return 1;
1452 }
1453
1454 buffer->skb = skb;
1455 buffer->dma = dma;
1456 buffer->state = NETXEN_BUFFER_BUSY;
1457
1458 return 0;
1459}
1460
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001461static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1462 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1463{
1464 struct netxen_rx_buffer *buffer;
1465 struct sk_buff *skb;
1466
1467 buffer = &rds_ring->rx_buf_arr[index];
1468
1469 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1470 PCI_DMA_FROMDEVICE);
1471
1472 skb = buffer->skb;
1473 if (!skb)
1474 goto no_skb;
1475
1476 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1477 adapter->stats.csummed++;
1478 skb->ip_summed = CHECKSUM_UNNECESSARY;
1479 } else
1480 skb->ip_summed = CHECKSUM_NONE;
1481
1482 skb->dev = adapter->netdev;
1483
1484 buffer->skb = NULL;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001485no_skb:
1486 buffer->state = NETXEN_BUFFER_FREE;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001487 return skb;
1488}
1489
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001490static struct netxen_rx_buffer *
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001491netxen_process_rcv(struct netxen_adapter *adapter,
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001492 struct nx_host_sds_ring *sds_ring,
1493 int ring, u64 sts_data0)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001494{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001495 struct net_device *netdev = adapter->netdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001496 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001497 struct netxen_rx_buffer *buffer;
1498 struct sk_buff *skb;
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001499 struct nx_host_rds_ring *rds_ring;
1500 int index, length, cksum, pkt_offset;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001501
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001502 if (unlikely(ring >= adapter->max_rds_rings))
1503 return NULL;
1504
1505 rds_ring = &recv_ctx->rds_rings[ring];
1506
1507 index = netxen_get_sts_refhandle(sts_data0);
1508 if (unlikely(index >= rds_ring->num_desc))
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001509 return NULL;
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001510
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001511 buffer = &rds_ring->rx_buf_arr[index];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001512
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001513 length = netxen_get_sts_totallength(sts_data0);
1514 cksum = netxen_get_sts_status(sts_data0);
1515 pkt_offset = netxen_get_sts_pkt_offset(sts_data0);
1516
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001517 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1518 if (!skb)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001519 return buffer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001520
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001521 if (length > rds_ring->skb_size)
1522 skb_put(skb, rds_ring->skb_size);
1523 else
1524 skb_put(skb, length);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001525
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001526
1527 if (pkt_offset)
1528 skb_pull(skb, pkt_offset);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001529
Dhananjay Phadkebc75e5b2009-09-03 13:10:53 +00001530 skb->truesize = skb->len + sizeof(struct sk_buff);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001531 skb->protocol = eth_type_trans(skb, netdev);
1532
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001533 napi_gro_receive(&sds_ring->napi, skb);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001534
Narender Kumar1bb482f2009-08-23 08:35:09 +00001535 adapter->stats.rx_pkts++;
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001536 adapter->stats.rxbytes += length;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001537
1538 return buffer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001539}
1540
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001541#define TCP_HDR_SIZE 20
1542#define TCP_TS_OPTION_SIZE 12
1543#define TCP_TS_HDR_SIZE (TCP_HDR_SIZE + TCP_TS_OPTION_SIZE)
1544
1545static struct netxen_rx_buffer *
1546netxen_process_lro(struct netxen_adapter *adapter,
1547 struct nx_host_sds_ring *sds_ring,
1548 int ring, u64 sts_data0, u64 sts_data1)
1549{
1550 struct net_device *netdev = adapter->netdev;
1551 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1552 struct netxen_rx_buffer *buffer;
1553 struct sk_buff *skb;
1554 struct nx_host_rds_ring *rds_ring;
1555 struct iphdr *iph;
1556 struct tcphdr *th;
1557 bool push, timestamp;
1558 int l2_hdr_offset, l4_hdr_offset;
1559 int index;
1560 u16 lro_length, length, data_offset;
1561 u32 seq_number;
1562
1563 if (unlikely(ring > adapter->max_rds_rings))
1564 return NULL;
1565
1566 rds_ring = &recv_ctx->rds_rings[ring];
1567
1568 index = netxen_get_lro_sts_refhandle(sts_data0);
1569 if (unlikely(index > rds_ring->num_desc))
1570 return NULL;
1571
1572 buffer = &rds_ring->rx_buf_arr[index];
1573
1574 timestamp = netxen_get_lro_sts_timestamp(sts_data0);
1575 lro_length = netxen_get_lro_sts_length(sts_data0);
1576 l2_hdr_offset = netxen_get_lro_sts_l2_hdr_offset(sts_data0);
1577 l4_hdr_offset = netxen_get_lro_sts_l4_hdr_offset(sts_data0);
1578 push = netxen_get_lro_sts_push_flag(sts_data0);
1579 seq_number = netxen_get_lro_sts_seq_number(sts_data1);
1580
1581 skb = netxen_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1582 if (!skb)
1583 return buffer;
1584
1585 if (timestamp)
1586 data_offset = l4_hdr_offset + TCP_TS_HDR_SIZE;
1587 else
1588 data_offset = l4_hdr_offset + TCP_HDR_SIZE;
1589
1590 skb_put(skb, lro_length + data_offset);
1591
Dhananjay Phadkebc75e5b2009-09-03 13:10:53 +00001592 skb->truesize = skb->len + sizeof(struct sk_buff) + skb_headroom(skb);
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001593
1594 skb_pull(skb, l2_hdr_offset);
1595 skb->protocol = eth_type_trans(skb, netdev);
1596
1597 iph = (struct iphdr *)skb->data;
1598 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1599
1600 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1601 iph->tot_len = htons(length);
1602 iph->check = 0;
1603 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1604 th->psh = push;
1605 th->seq = htonl(seq_number);
1606
Narender Kumar1bb482f2009-08-23 08:35:09 +00001607 length = skb->len;
1608
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001609 netif_receive_skb(skb);
1610
Narender Kumar1bb482f2009-08-23 08:35:09 +00001611 adapter->stats.lro_pkts++;
1612 adapter->stats.rxbytes += length;
1613
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001614 return buffer;
1615}
1616
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001617#define netxen_merge_rx_buffers(list, head) \
1618 do { list_splice_tail_init(list, head); } while (0);
1619
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001620int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001621netxen_process_rcv_ring(struct nx_host_sds_ring *sds_ring, int max)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001622{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001623 struct netxen_adapter *adapter = sds_ring->adapter;
1624
1625 struct list_head *cur;
1626
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001627 struct status_desc *desc;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001628 struct netxen_rx_buffer *rxbuf;
1629
1630 u32 consumer = sds_ring->consumer;
1631
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001632 int count = 0;
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001633 u64 sts_data0, sts_data1;
1634 int opcode, ring = 0, desc_cnt;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001635
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001636 while (count < max) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001637 desc = &sds_ring->desc_head[consumer];
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001638 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001639
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001640 if (!(sts_data0 & STATUS_OWNER_HOST))
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001641 break;
1642
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001643 desc_cnt = netxen_get_sts_desc_cnt(sts_data0);
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001644
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001645 opcode = netxen_get_sts_opcode(sts_data0);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001646
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001647 switch (opcode) {
1648 case NETXEN_NIC_RXPKT_DESC:
1649 case NETXEN_OLD_RXPKT_DESC:
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001650 case NETXEN_NIC_SYN_OFFLOAD:
Dhananjay Phadkec1c00ab2009-08-05 07:34:09 +00001651 ring = netxen_get_sts_type(sts_data0);
1652 rxbuf = netxen_process_rcv(adapter, sds_ring,
1653 ring, sts_data0);
1654 break;
1655 case NETXEN_NIC_LRO_DESC:
1656 ring = netxen_get_lro_sts_type(sts_data0);
1657 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1658 rxbuf = netxen_process_lro(adapter, sds_ring,
1659 ring, sts_data0, sts_data1);
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001660 break;
1661 case NETXEN_NIC_RESPONSE_DESC:
1662 netxen_handle_fw_message(desc_cnt, consumer, sds_ring);
1663 default:
1664 goto skip;
1665 }
1666
1667 WARN_ON(desc_cnt > 1);
1668
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001669 if (rxbuf)
1670 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1671
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001672skip:
1673 for (; desc_cnt > 0; desc_cnt--) {
1674 desc = &sds_ring->desc_head[consumer];
1675 desc->status_desc_data[0] =
1676 cpu_to_le64(STATUS_OWNER_PHANTOM);
1677 consumer = get_next_index(consumer, sds_ring->num_desc);
1678 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001679 count++;
1680 }
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001681
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001682 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1683 struct nx_host_rds_ring *rds_ring =
1684 &adapter->recv_ctx.rds_rings[ring];
1685
1686 if (!list_empty(&sds_ring->free_list[ring])) {
1687 list_for_each(cur, &sds_ring->free_list[ring]) {
1688 rxbuf = list_entry(cur,
1689 struct netxen_rx_buffer, list);
1690 netxen_alloc_rx_skb(adapter, rds_ring, rxbuf);
1691 }
1692 spin_lock(&rds_ring->lock);
1693 netxen_merge_rx_buffers(&sds_ring->free_list[ring],
1694 &rds_ring->free_list);
1695 spin_unlock(&rds_ring->lock);
1696 }
1697
1698 netxen_post_rx_buffers_nodb(adapter, rds_ring);
1699 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001700
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001701 if (count) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001702 sds_ring->consumer = consumer;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001703 NXWRIO(adapter, sds_ring->crb_sts_consumer, consumer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001704 }
1705
1706 return count;
1707}
1708
1709/* Process Command status ring */
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001710int netxen_process_cmd_ring(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001711{
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001712 u32 sw_consumer, hw_consumer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001713 int count = 0, i;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001714 struct netxen_cmd_buffer *buffer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001715 struct pci_dev *pdev = adapter->pdev;
1716 struct net_device *netdev = adapter->netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001717 struct netxen_skb_frag *frag;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001718 int done = 0;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001719 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001720
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001721 if (!spin_trylock(&adapter->tx_clean_lock))
1722 return 1;
1723
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001724 sw_consumer = tx_ring->sw_consumer;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001725 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001726
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001727 while (sw_consumer != hw_consumer) {
1728 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001729 if (buffer->skb) {
1730 frag = &buffer->frag_array[0];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001731 pci_unmap_single(pdev, frag->dma, frag->length,
1732 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301733 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001734 for (i = 1; i < buffer->frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001735 frag++; /* Get the next frag */
1736 pci_unmap_page(pdev, frag->dma, frag->length,
1737 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301738 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001739 }
1740
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001741 adapter->stats.xmitfinished++;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001742 dev_kfree_skb_any(buffer->skb);
1743 buffer->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001744 }
1745
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001746 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001747 if (++count >= MAX_STATUS_HANDLE)
1748 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001749 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001750
Dhananjay Phadke22527862009-05-05 19:05:06 +00001751 if (count && netif_running(netdev)) {
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001752 tx_ring->sw_consumer = sw_consumer;
1753
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001754 smp_mb();
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001755
Dhananjay Phadke22527862009-05-05 19:05:06 +00001756 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001757 __netif_tx_lock(tx_ring->txq, smp_processor_id());
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001758 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH) {
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001759 netif_wake_queue(netdev);
Amit Kumar Salecha74c520d2009-09-11 11:28:14 +00001760 adapter->tx_timeo_cnt = 0;
1761 }
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001762 __netif_tx_unlock(tx_ring->txq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001763 }
1764 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001765 /*
1766 * If everything is freed up to consumer then check if the ring is full
1767 * If the ring is full then check if more needs to be freed and
1768 * schedule the call back again.
1769 *
1770 * This happens when there are 2 CPUs. One could be freeing and the
1771 * other filling it. If the ring is full when we get out of here and
1772 * the card has already interrupted the host then the host can miss the
1773 * interrupt.
1774 *
1775 * There is still a possible race condition and the host could miss an
1776 * interrupt. The card has to take care of this.
1777 */
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001778 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1779 done = (sw_consumer == hw_consumer);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001780 spin_unlock(&adapter->tx_clean_lock);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001781
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001782 return (done);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001783}
1784
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001785void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001786netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ringid,
1787 struct nx_host_rds_ring *rds_ring)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001788{
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001789 struct rcv_desc *pdesc;
1790 struct netxen_rx_buffer *buffer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001791 int producer, count = 0;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001792 netxen_ctx_msg msg = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001793 struct list_head *head;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001794
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001795 producer = rds_ring->producer;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001796
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001797 spin_lock(&rds_ring->lock);
1798 head = &rds_ring->free_list;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001799 while (!list_empty(head)) {
1800
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001801 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001802
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001803 if (!buffer->skb) {
1804 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1805 break;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001806 }
1807
1808 count++;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001809 list_del(&buffer->list);
1810
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001811 /* make a rcv descriptor */
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001812 pdesc = &rds_ring->desc_head[producer];
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001813 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001814 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001815 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001816
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001817 producer = get_next_index(producer, rds_ring->num_desc);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001818 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001819 spin_unlock(&rds_ring->lock);
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001820
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001821 if (count) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001822 rds_ring->producer = producer;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001823 NXWRIO(adapter, rds_ring->crb_rcv_producer,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001824 (producer-1) & (rds_ring->num_desc-1));
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001825
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001826 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001827 /*
1828 * Write a doorbell msg to tell phanmon of change in
1829 * receive ring producer
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001830 * Only for firmware version < 4.0.0
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001831 */
1832 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1833 netxen_set_msg_privid(msg);
1834 netxen_set_msg_count(msg,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001835 ((producer - 1) &
1836 (rds_ring->num_desc - 1)));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001837 netxen_set_msg_ctxid(msg, adapter->portnum);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001838 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
Dhananjay Phadkef03b0eb2009-10-13 05:31:44 +00001839 NXWRIO(adapter, DB_NORMALIZE(adapter,
1840 NETXEN_RCV_PRODUCER_OFFSET), msg);
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001841 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001842 }
1843}
1844
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001845static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001846netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1847 struct nx_host_rds_ring *rds_ring)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001848{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001849 struct rcv_desc *pdesc;
1850 struct netxen_rx_buffer *buffer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001851 int producer, count = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001852 struct list_head *head;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001853
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001854 producer = rds_ring->producer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001855 if (!spin_trylock(&rds_ring->lock))
1856 return;
1857
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001858 head = &rds_ring->free_list;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001859 while (!list_empty(head)) {
1860
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001861 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001862
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001863 if (!buffer->skb) {
1864 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1865 break;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001866 }
1867
1868 count++;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001869 list_del(&buffer->list);
1870
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001871 /* make a rcv descriptor */
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001872 pdesc = &rds_ring->desc_head[producer];
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001873 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001874 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001875 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001876
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001877 producer = get_next_index(producer, rds_ring->num_desc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001878 }
1879
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001880 if (count) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001881 rds_ring->producer = producer;
Amit Kumar Salecha195c5f92009-09-05 17:43:10 +00001882 NXWRIO(adapter, rds_ring->crb_rcv_producer,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001883 (producer - 1) & (rds_ring->num_desc - 1));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001884 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001885 spin_unlock(&rds_ring->lock);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001886}
1887
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001888void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1889{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001890 memset(&adapter->stats, 0, sizeof(adapter->stats));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001891 return;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001892}
1893