blob: 5b8bbcf904d5de9251da03942865c86f09787a28 [file] [log] [blame]
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001/*
Sritej Velaga40839129f2010-12-02 20:41:56 +00002 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2010 QLogic Corporation
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004 *
Sritej Velaga40839129f2010-12-02 20:41:56 +00005 * See LICENSE.qlcnic for copyright and licensing details.
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00006 */
7
8#include <linux/netdevice.h>
9#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +000011#include <linux/if_vlan.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000012#include "qlcnic.h"
13
14struct crb_addr_pair {
15 u32 addr;
16 u32 data;
17};
18
19#define QLCNIC_MAX_CRB_XFORM 60
20static unsigned int crb_addr_xform[QLCNIC_MAX_CRB_XFORM];
21
22#define crb_addr_transform(name) \
23 (crb_addr_xform[QLCNIC_HW_PX_MAP_CRB_##name] = \
24 QLCNIC_HW_CRB_HUB_AGT_ADR_##name << 20)
25
26#define QLCNIC_ADDR_ERROR (0xffffffff)
27
28static void
29qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
30 struct qlcnic_host_rds_ring *rds_ring);
31
Sony Chacko4e708122010-08-31 17:17:44 +000032static int
33qlcnic_check_fw_hearbeat(struct qlcnic_adapter *adapter);
34
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000035static void crb_addr_transform_setup(void)
36{
37 crb_addr_transform(XDMA);
38 crb_addr_transform(TIMR);
39 crb_addr_transform(SRE);
40 crb_addr_transform(SQN3);
41 crb_addr_transform(SQN2);
42 crb_addr_transform(SQN1);
43 crb_addr_transform(SQN0);
44 crb_addr_transform(SQS3);
45 crb_addr_transform(SQS2);
46 crb_addr_transform(SQS1);
47 crb_addr_transform(SQS0);
48 crb_addr_transform(RPMX7);
49 crb_addr_transform(RPMX6);
50 crb_addr_transform(RPMX5);
51 crb_addr_transform(RPMX4);
52 crb_addr_transform(RPMX3);
53 crb_addr_transform(RPMX2);
54 crb_addr_transform(RPMX1);
55 crb_addr_transform(RPMX0);
56 crb_addr_transform(ROMUSB);
57 crb_addr_transform(SN);
58 crb_addr_transform(QMN);
59 crb_addr_transform(QMS);
60 crb_addr_transform(PGNI);
61 crb_addr_transform(PGND);
62 crb_addr_transform(PGN3);
63 crb_addr_transform(PGN2);
64 crb_addr_transform(PGN1);
65 crb_addr_transform(PGN0);
66 crb_addr_transform(PGSI);
67 crb_addr_transform(PGSD);
68 crb_addr_transform(PGS3);
69 crb_addr_transform(PGS2);
70 crb_addr_transform(PGS1);
71 crb_addr_transform(PGS0);
72 crb_addr_transform(PS);
73 crb_addr_transform(PH);
74 crb_addr_transform(NIU);
75 crb_addr_transform(I2Q);
76 crb_addr_transform(EG);
77 crb_addr_transform(MN);
78 crb_addr_transform(MS);
79 crb_addr_transform(CAS2);
80 crb_addr_transform(CAS1);
81 crb_addr_transform(CAS0);
82 crb_addr_transform(CAM);
83 crb_addr_transform(C2C1);
84 crb_addr_transform(C2C0);
85 crb_addr_transform(SMB);
86 crb_addr_transform(OCM0);
87 crb_addr_transform(I2C0);
88}
89
90void qlcnic_release_rx_buffers(struct qlcnic_adapter *adapter)
91{
92 struct qlcnic_recv_context *recv_ctx;
93 struct qlcnic_host_rds_ring *rds_ring;
94 struct qlcnic_rx_buffer *rx_buf;
95 int i, ring;
96
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +000097 recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000098 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
99 rds_ring = &recv_ctx->rds_rings[ring];
100 for (i = 0; i < rds_ring->num_desc; ++i) {
101 rx_buf = &(rds_ring->rx_buf_arr[i]);
Amit Kumar Salecha96659822010-06-22 03:18:58 +0000102 if (rx_buf->skb == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000103 continue;
Amit Kumar Salecha96659822010-06-22 03:18:58 +0000104
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000105 pci_unmap_single(adapter->pdev,
106 rx_buf->dma,
107 rds_ring->dma_size,
108 PCI_DMA_FROMDEVICE);
Amit Kumar Salecha96659822010-06-22 03:18:58 +0000109
110 dev_kfree_skb_any(rx_buf->skb);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000111 }
112 }
113}
114
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000115void qlcnic_reset_rx_buffers_list(struct qlcnic_adapter *adapter)
116{
117 struct qlcnic_recv_context *recv_ctx;
118 struct qlcnic_host_rds_ring *rds_ring;
119 struct qlcnic_rx_buffer *rx_buf;
120 int i, ring;
121
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000122 recv_ctx = adapter->recv_ctx;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000123 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
124 rds_ring = &recv_ctx->rds_rings[ring];
125
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000126 INIT_LIST_HEAD(&rds_ring->free_list);
127
128 rx_buf = rds_ring->rx_buf_arr;
129 for (i = 0; i < rds_ring->num_desc; i++) {
130 list_add_tail(&rx_buf->list,
131 &rds_ring->free_list);
132 rx_buf++;
133 }
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000134 }
135}
136
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000137void qlcnic_release_tx_buffers(struct qlcnic_adapter *adapter)
138{
139 struct qlcnic_cmd_buffer *cmd_buf;
140 struct qlcnic_skb_frag *buffrag;
141 int i, j;
142 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
143
144 cmd_buf = tx_ring->cmd_buf_arr;
145 for (i = 0; i < tx_ring->num_desc; i++) {
146 buffrag = cmd_buf->frag_array;
147 if (buffrag->dma) {
148 pci_unmap_single(adapter->pdev, buffrag->dma,
149 buffrag->length, PCI_DMA_TODEVICE);
150 buffrag->dma = 0ULL;
151 }
152 for (j = 0; j < cmd_buf->frag_count; j++) {
153 buffrag++;
154 if (buffrag->dma) {
155 pci_unmap_page(adapter->pdev, buffrag->dma,
156 buffrag->length,
157 PCI_DMA_TODEVICE);
158 buffrag->dma = 0ULL;
159 }
160 }
161 if (cmd_buf->skb) {
162 dev_kfree_skb_any(cmd_buf->skb);
163 cmd_buf->skb = NULL;
164 }
165 cmd_buf++;
166 }
167}
168
169void qlcnic_free_sw_resources(struct qlcnic_adapter *adapter)
170{
171 struct qlcnic_recv_context *recv_ctx;
172 struct qlcnic_host_rds_ring *rds_ring;
173 struct qlcnic_host_tx_ring *tx_ring;
174 int ring;
175
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000176 recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000177
178 if (recv_ctx->rds_rings == NULL)
179 goto skip_rds;
180
181 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
182 rds_ring = &recv_ctx->rds_rings[ring];
183 vfree(rds_ring->rx_buf_arr);
184 rds_ring->rx_buf_arr = NULL;
185 }
186 kfree(recv_ctx->rds_rings);
187
188skip_rds:
189 if (adapter->tx_ring == NULL)
190 return;
191
192 tx_ring = adapter->tx_ring;
193 vfree(tx_ring->cmd_buf_arr);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +0000194 tx_ring->cmd_buf_arr = NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000195 kfree(adapter->tx_ring);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +0000196 adapter->tx_ring = NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000197}
198
199int qlcnic_alloc_sw_resources(struct qlcnic_adapter *adapter)
200{
201 struct qlcnic_recv_context *recv_ctx;
202 struct qlcnic_host_rds_ring *rds_ring;
203 struct qlcnic_host_sds_ring *sds_ring;
204 struct qlcnic_host_tx_ring *tx_ring;
205 struct qlcnic_rx_buffer *rx_buf;
206 int ring, i, size;
207
208 struct qlcnic_cmd_buffer *cmd_buf_arr;
209 struct net_device *netdev = adapter->netdev;
210
211 size = sizeof(struct qlcnic_host_tx_ring);
212 tx_ring = kzalloc(size, GFP_KERNEL);
213 if (tx_ring == NULL) {
214 dev_err(&netdev->dev, "failed to allocate tx ring struct\n");
215 return -ENOMEM;
216 }
217 adapter->tx_ring = tx_ring;
218
219 tx_ring->num_desc = adapter->num_txd;
220 tx_ring->txq = netdev_get_tx_queue(netdev, 0);
221
Eric Dumazet89bf67f2010-11-22 00:15:06 +0000222 cmd_buf_arr = vzalloc(TX_BUFF_RINGSIZE(tx_ring));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000223 if (cmd_buf_arr == NULL) {
224 dev_err(&netdev->dev, "failed to allocate cmd buffer ring\n");
Anirban Chakrabortyaadd8182010-05-17 01:22:08 +0000225 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000226 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000227 tx_ring->cmd_buf_arr = cmd_buf_arr;
228
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000229 recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000230
231 size = adapter->max_rds_rings * sizeof(struct qlcnic_host_rds_ring);
232 rds_ring = kzalloc(size, GFP_KERNEL);
233 if (rds_ring == NULL) {
234 dev_err(&netdev->dev, "failed to allocate rds ring struct\n");
Anirban Chakrabortyaadd8182010-05-17 01:22:08 +0000235 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000236 }
237 recv_ctx->rds_rings = rds_ring;
238
239 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
240 rds_ring = &recv_ctx->rds_rings[ring];
241 switch (ring) {
242 case RCV_RING_NORMAL:
243 rds_ring->num_desc = adapter->num_rxd;
Sritej Velagaff1b1bf82010-10-07 23:46:10 +0000244 rds_ring->dma_size = QLCNIC_P3P_RX_BUF_MAX_LEN;
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000245 rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000246 break;
247
248 case RCV_RING_JUMBO:
249 rds_ring->num_desc = adapter->num_jumbo_rxd;
250 rds_ring->dma_size =
Sritej Velagaff1b1bf82010-10-07 23:46:10 +0000251 QLCNIC_P3P_RX_JUMBO_BUF_MAX_LEN;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000252
253 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
254 rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA;
255
256 rds_ring->skb_size =
257 rds_ring->dma_size + NET_IP_ALIGN;
258 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000259 }
Joe Perchesf3167462010-11-27 23:05:44 +0000260 rds_ring->rx_buf_arr = vzalloc(RCV_BUFF_RINGSIZE(rds_ring));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000261 if (rds_ring->rx_buf_arr == NULL) {
262 dev_err(&netdev->dev, "Failed to allocate "
263 "rx buffer ring %d\n", ring);
264 goto err_out;
265 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000266 INIT_LIST_HEAD(&rds_ring->free_list);
267 /*
268 * Now go through all of them, set reference handles
269 * and put them in the queues.
270 */
271 rx_buf = rds_ring->rx_buf_arr;
272 for (i = 0; i < rds_ring->num_desc; i++) {
273 list_add_tail(&rx_buf->list,
274 &rds_ring->free_list);
275 rx_buf->ref_handle = i;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000276 rx_buf++;
277 }
278 spin_lock_init(&rds_ring->lock);
279 }
280
281 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
282 sds_ring = &recv_ctx->sds_rings[ring];
283 sds_ring->irq = adapter->msix_entries[ring].vector;
284 sds_ring->adapter = adapter;
285 sds_ring->num_desc = adapter->num_rxd;
286
287 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
288 INIT_LIST_HEAD(&sds_ring->free_list[i]);
289 }
290
291 return 0;
292
293err_out:
294 qlcnic_free_sw_resources(adapter);
295 return -ENOMEM;
296}
297
298/*
299 * Utility to translate from internal Phantom CRB address
300 * to external PCI CRB address.
301 */
302static u32 qlcnic_decode_crb_addr(u32 addr)
303{
304 int i;
305 u32 base_addr, offset, pci_base;
306
307 crb_addr_transform_setup();
308
309 pci_base = QLCNIC_ADDR_ERROR;
310 base_addr = addr & 0xfff00000;
311 offset = addr & 0x000fffff;
312
313 for (i = 0; i < QLCNIC_MAX_CRB_XFORM; i++) {
314 if (crb_addr_xform[i] == base_addr) {
315 pci_base = i << 20;
316 break;
317 }
318 }
319 if (pci_base == QLCNIC_ADDR_ERROR)
320 return pci_base;
321 else
322 return pci_base + offset;
323}
324
325#define QLCNIC_MAX_ROM_WAIT_USEC 100
326
327static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter)
328{
329 long timeout = 0;
330 long done = 0;
331
332 cond_resched();
333
334 while (done == 0) {
335 done = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_STATUS);
336 done &= 2;
337 if (++timeout >= QLCNIC_MAX_ROM_WAIT_USEC) {
338 dev_err(&adapter->pdev->dev,
339 "Timeout reached waiting for rom done");
340 return -EIO;
341 }
342 udelay(1);
343 }
344 return 0;
345}
346
347static int do_rom_fast_read(struct qlcnic_adapter *adapter,
Anirban Chakraborty18f2f612011-05-12 12:48:33 +0000348 u32 addr, u32 *valp)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000349{
350 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ADDRESS, addr);
351 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
352 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 3);
353 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_INSTR_OPCODE, 0xb);
354 if (qlcnic_wait_rom_done(adapter)) {
355 dev_err(&adapter->pdev->dev, "Error waiting for rom done\n");
356 return -EIO;
357 }
358 /* reset abyte_cnt and dummy_byte_cnt */
359 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 0);
360 udelay(10);
361 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
362
363 *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA);
364 return 0;
365}
366
367static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
368 u8 *bytes, size_t size)
369{
370 int addridx;
371 int ret = 0;
372
373 for (addridx = addr; addridx < (addr + size); addridx += 4) {
374 int v;
375 ret = do_rom_fast_read(adapter, addridx, &v);
376 if (ret != 0)
377 break;
378 *(__le32 *)bytes = cpu_to_le32(v);
379 bytes += 4;
380 }
381
382 return ret;
383}
384
385int
386qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
387 u8 *bytes, size_t size)
388{
389 int ret;
390
391 ret = qlcnic_rom_lock(adapter);
392 if (ret < 0)
393 return ret;
394
395 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
396
397 qlcnic_rom_unlock(adapter);
398 return ret;
399}
400
Anirban Chakraborty18f2f612011-05-12 12:48:33 +0000401int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, u32 addr, u32 *valp)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000402{
403 int ret;
404
405 if (qlcnic_rom_lock(adapter) != 0)
406 return -EIO;
407
408 ret = do_rom_fast_read(adapter, addr, valp);
409 qlcnic_rom_unlock(adapter);
410 return ret;
411}
412
413int qlcnic_pinit_from_rom(struct qlcnic_adapter *adapter)
414{
415 int addr, val;
416 int i, n, init_delay;
417 struct crb_addr_pair *buf;
418 unsigned offset;
419 u32 off;
420 struct pci_dev *pdev = adapter->pdev;
421
Sony Chackod4066832010-08-19 05:08:31 +0000422 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
423 QLCWR32(adapter, CRB_RCVPEG_STATE, 0);
424
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000425 qlcnic_rom_lock(adapter);
Sucheta Chakraborty469c2212010-06-17 02:56:38 +0000426 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xfeffffff);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000427 qlcnic_rom_unlock(adapter);
428
Sony Chackod4066832010-08-19 05:08:31 +0000429 /* Init HW CRB block */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000430 if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) ||
431 qlcnic_rom_fast_read(adapter, 4, &n) != 0) {
432 dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n);
433 return -EIO;
434 }
435 offset = n & 0xffffU;
436 n = (n >> 16) & 0xffffU;
437
438 if (n >= 1024) {
439 dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n");
440 return -EIO;
441 }
442
443 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
444 if (buf == NULL) {
445 dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n");
446 return -ENOMEM;
447 }
448
449 for (i = 0; i < n; i++) {
450 if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
451 qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
452 kfree(buf);
453 return -EIO;
454 }
455
456 buf[i].addr = addr;
457 buf[i].data = val;
458 }
459
460 for (i = 0; i < n; i++) {
461
462 off = qlcnic_decode_crb_addr(buf[i].addr);
463 if (off == QLCNIC_ADDR_ERROR) {
464 dev_err(&pdev->dev, "CRB init value out of range %x\n",
465 buf[i].addr);
466 continue;
467 }
468 off += QLCNIC_PCI_CRBSPACE;
469
470 if (off & 1)
471 continue;
472
473 /* skipping cold reboot MAGIC */
474 if (off == QLCNIC_CAM_RAM(0x1fc))
475 continue;
476 if (off == (QLCNIC_CRB_I2C0 + 0x1c))
477 continue;
478 if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */
479 continue;
480 if (off == (ROMUSB_GLB + 0xa8))
481 continue;
482 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
483 continue;
484 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
485 continue;
486 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
487 continue;
488 if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET)
489 continue;
490 /* skip the function enable register */
491 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION))
492 continue;
493 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION2))
494 continue;
495 if ((off & 0x0ff00000) == QLCNIC_CRB_SMB)
496 continue;
497
498 init_delay = 1;
499 /* After writing this register, HW needs time for CRB */
500 /* to quiet down (else crb_window returns 0xffffffff) */
501 if (off == QLCNIC_ROMUSB_GLB_SW_RESET)
502 init_delay = 1000;
503
504 QLCWR32(adapter, off, buf[i].data);
505
506 msleep(init_delay);
507 }
508 kfree(buf);
509
Sony Chackod4066832010-08-19 05:08:31 +0000510 /* Initialize protocol process engine */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000511 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000512 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8);
513 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000514 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0);
515 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0);
516 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0);
517 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0);
518 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0);
519 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0);
520 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0);
521 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0);
Sony Chackod4066832010-08-19 05:08:31 +0000522 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x8, 0);
523 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0xc, 0);
524 msleep(1);
525 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
526 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000527 return 0;
528}
529
Sony Chacko4e708122010-08-31 17:17:44 +0000530static int qlcnic_cmd_peg_ready(struct qlcnic_adapter *adapter)
531{
532 u32 val;
533 int retries = QLCNIC_CMDPEG_CHECK_RETRY_COUNT;
534
535 do {
536 val = QLCRD32(adapter, CRB_CMDPEG_STATE);
537
538 switch (val) {
539 case PHAN_INITIALIZE_COMPLETE:
540 case PHAN_INITIALIZE_ACK:
541 return 0;
542 case PHAN_INITIALIZE_FAILED:
543 goto out_err;
544 default:
545 break;
546 }
547
548 msleep(QLCNIC_CMDPEG_CHECK_DELAY);
549
550 } while (--retries);
551
552 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
553
554out_err:
555 dev_err(&adapter->pdev->dev, "Command Peg initialization not "
556 "complete, state: 0x%x.\n", val);
557 return -EIO;
558}
559
560static int
561qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter)
562{
563 u32 val;
564 int retries = QLCNIC_RCVPEG_CHECK_RETRY_COUNT;
565
566 do {
567 val = QLCRD32(adapter, CRB_RCVPEG_STATE);
568
569 if (val == PHAN_PEG_RCV_INITIALIZED)
570 return 0;
571
572 msleep(QLCNIC_RCVPEG_CHECK_DELAY);
573
574 } while (--retries);
575
576 if (!retries) {
577 dev_err(&adapter->pdev->dev, "Receive Peg initialization not "
578 "complete, state: 0x%x.\n", val);
579 return -EIO;
580 }
581
582 return 0;
583}
584
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +0000585int
Sony Chackod4066832010-08-19 05:08:31 +0000586qlcnic_check_fw_status(struct qlcnic_adapter *adapter)
587{
Sony Chacko4e708122010-08-31 17:17:44 +0000588 int err;
Sony Chackod4066832010-08-19 05:08:31 +0000589
Sony Chacko4e708122010-08-31 17:17:44 +0000590 err = qlcnic_cmd_peg_ready(adapter);
591 if (err)
592 return err;
Sony Chackod4066832010-08-19 05:08:31 +0000593
Sony Chacko4e708122010-08-31 17:17:44 +0000594 err = qlcnic_receive_peg_ready(adapter);
595 if (err)
596 return err;
597
598 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
599
600 return err;
Sony Chackod4066832010-08-19 05:08:31 +0000601}
602
603int
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000604qlcnic_setup_idc_param(struct qlcnic_adapter *adapter) {
605
606 int timeo;
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +0000607 u32 val;
608
Anirban Chakraborty45918e22010-06-29 07:52:12 +0000609 val = QLCRD32(adapter, QLCNIC_CRB_DEV_PARTITION_INFO);
610 val = QLC_DEV_GET_DRV(val, adapter->portnum);
611 if ((val & 0x3) != QLCNIC_TYPE_NIC) {
612 dev_err(&adapter->pdev->dev,
613 "Not an Ethernet NIC func=%u\n", val);
614 return -EIO;
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +0000615 }
Anirban Chakraborty45918e22010-06-29 07:52:12 +0000616 adapter->physical_port = (val >> 2);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000617 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DEV_INIT_TIMEOUT, &timeo))
Sony Chacko251b0362010-08-19 05:08:24 +0000618 timeo = QLCNIC_INIT_TIMEOUT_SECS;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000619
620 adapter->dev_init_timeo = timeo;
621
622 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DRV_RESET_TIMEOUT, &timeo))
Sony Chacko251b0362010-08-19 05:08:24 +0000623 timeo = QLCNIC_RESET_TIMEOUT_SECS;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000624
625 adapter->reset_ack_timeo = timeo;
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +0000626
627 return 0;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000628}
629
amit salecha0e5f20b2011-01-10 00:15:21 +0000630static int qlcnic_get_flt_entry(struct qlcnic_adapter *adapter, u8 region,
631 struct qlcnic_flt_entry *region_entry)
632{
633 struct qlcnic_flt_header flt_hdr;
634 struct qlcnic_flt_entry *flt_entry;
635 int i = 0, ret;
636 u32 entry_size;
637
638 memset(region_entry, 0, sizeof(struct qlcnic_flt_entry));
639 ret = qlcnic_rom_fast_read_words(adapter, QLCNIC_FLT_LOCATION,
640 (u8 *)&flt_hdr,
641 sizeof(struct qlcnic_flt_header));
642 if (ret) {
643 dev_warn(&adapter->pdev->dev,
644 "error reading flash layout header\n");
645 return -EIO;
646 }
647
648 entry_size = flt_hdr.len - sizeof(struct qlcnic_flt_header);
649 flt_entry = (struct qlcnic_flt_entry *)vzalloc(entry_size);
650 if (flt_entry == NULL) {
651 dev_warn(&adapter->pdev->dev, "error allocating memory\n");
652 return -EIO;
653 }
654
655 ret = qlcnic_rom_fast_read_words(adapter, QLCNIC_FLT_LOCATION +
656 sizeof(struct qlcnic_flt_header),
657 (u8 *)flt_entry, entry_size);
658 if (ret) {
659 dev_warn(&adapter->pdev->dev,
660 "error reading flash layout entries\n");
661 goto err_out;
662 }
663
664 while (i < (entry_size/sizeof(struct qlcnic_flt_entry))) {
665 if (flt_entry[i].region == region)
666 break;
667 i++;
668 }
669 if (i >= (entry_size/sizeof(struct qlcnic_flt_entry))) {
670 dev_warn(&adapter->pdev->dev,
671 "region=%x not found in %d regions\n", region, i);
672 ret = -EIO;
673 goto err_out;
674 }
675 memcpy(region_entry, &flt_entry[i], sizeof(struct qlcnic_flt_entry));
676
677err_out:
678 vfree(flt_entry);
679 return ret;
680}
681
schacko8f891382010-06-17 02:56:40 +0000682int
683qlcnic_check_flash_fw_ver(struct qlcnic_adapter *adapter)
684{
amit salecha0e5f20b2011-01-10 00:15:21 +0000685 struct qlcnic_flt_entry fw_entry;
schacko8f891382010-06-17 02:56:40 +0000686 u32 ver = -1, min_ver;
amit salecha0e5f20b2011-01-10 00:15:21 +0000687 int ret;
schacko8f891382010-06-17 02:56:40 +0000688
amit salecha0e5f20b2011-01-10 00:15:21 +0000689 ret = qlcnic_get_flt_entry(adapter, QLCNIC_FW_IMAGE_REGION, &fw_entry);
690 if (!ret)
691 /* 0-4:-signature, 4-8:-fw version */
692 qlcnic_rom_fast_read(adapter, fw_entry.start_addr + 4,
693 (int *)&ver);
694 else
695 qlcnic_rom_fast_read(adapter, QLCNIC_FW_VERSION_OFFSET,
696 (int *)&ver);
schacko8f891382010-06-17 02:56:40 +0000697
698 ver = QLCNIC_DECODE_VERSION(ver);
699 min_ver = QLCNIC_MIN_FW_VERSION;
700
701 if (ver < min_ver) {
702 dev_err(&adapter->pdev->dev,
703 "firmware version %d.%d.%d unsupported."
704 "Min supported version %d.%d.%d\n",
705 _major(ver), _minor(ver), _build(ver),
706 _major(min_ver), _minor(min_ver), _build(min_ver));
707 return -EINVAL;
708 }
709
710 return 0;
711}
712
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000713static int
714qlcnic_has_mn(struct qlcnic_adapter *adapter)
715{
schacko8f891382010-06-17 02:56:40 +0000716 u32 capability;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000717 capability = 0;
718
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000719 capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY);
720 if (capability & QLCNIC_PEG_TUNE_MN_PRESENT)
721 return 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000722
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000723 return 0;
724}
725
726static
727struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section)
728{
729 u32 i;
730 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
731 __le32 entries = cpu_to_le32(directory->num_entries);
732
733 for (i = 0; i < entries; i++) {
734
735 __le32 offs = cpu_to_le32(directory->findex) +
736 (i * cpu_to_le32(directory->entry_size));
737 __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
738
739 if (tab_type == section)
740 return (struct uni_table_desc *) &unirom[offs];
741 }
742
743 return NULL;
744}
745
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000746#define FILEHEADER_SIZE (14 * 4)
747
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000748static int
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000749qlcnic_validate_header(struct qlcnic_adapter *adapter)
750{
751 const u8 *unirom = adapter->fw->data;
752 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
753 __le32 fw_file_size = adapter->fw->size;
754 __le32 entries;
755 __le32 entry_size;
756 __le32 tab_size;
757
758 if (fw_file_size < FILEHEADER_SIZE)
759 return -EINVAL;
760
761 entries = cpu_to_le32(directory->num_entries);
762 entry_size = cpu_to_le32(directory->entry_size);
763 tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
764
765 if (fw_file_size < tab_size)
766 return -EINVAL;
767
768 return 0;
769}
770
771static int
772qlcnic_validate_bootld(struct qlcnic_adapter *adapter)
773{
774 struct uni_table_desc *tab_desc;
775 struct uni_data_desc *descr;
776 const u8 *unirom = adapter->fw->data;
777 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
778 QLCNIC_UNI_BOOTLD_IDX_OFF));
779 __le32 offs;
780 __le32 tab_size;
781 __le32 data_size;
782
783 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD);
784
785 if (!tab_desc)
786 return -EINVAL;
787
788 tab_size = cpu_to_le32(tab_desc->findex) +
Amit Kumar Salecha22dfaa82010-04-01 19:01:35 +0000789 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000790
791 if (adapter->fw->size < tab_size)
792 return -EINVAL;
793
794 offs = cpu_to_le32(tab_desc->findex) +
795 (cpu_to_le32(tab_desc->entry_size) * (idx));
796 descr = (struct uni_data_desc *)&unirom[offs];
797
Amit Kumar Salecha22dfaa82010-04-01 19:01:35 +0000798 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000799
800 if (adapter->fw->size < data_size)
801 return -EINVAL;
802
803 return 0;
804}
805
806static int
807qlcnic_validate_fw(struct qlcnic_adapter *adapter)
808{
809 struct uni_table_desc *tab_desc;
810 struct uni_data_desc *descr;
811 const u8 *unirom = adapter->fw->data;
812 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
813 QLCNIC_UNI_FIRMWARE_IDX_OFF));
814 __le32 offs;
815 __le32 tab_size;
816 __le32 data_size;
817
818 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW);
819
820 if (!tab_desc)
821 return -EINVAL;
822
823 tab_size = cpu_to_le32(tab_desc->findex) +
Amit Kumar Salecha22dfaa82010-04-01 19:01:35 +0000824 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000825
826 if (adapter->fw->size < tab_size)
827 return -EINVAL;
828
829 offs = cpu_to_le32(tab_desc->findex) +
830 (cpu_to_le32(tab_desc->entry_size) * (idx));
831 descr = (struct uni_data_desc *)&unirom[offs];
Amit Kumar Salecha22dfaa82010-04-01 19:01:35 +0000832 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000833
834 if (adapter->fw->size < data_size)
835 return -EINVAL;
836
837 return 0;
838}
839
840static int
841qlcnic_validate_product_offs(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000842{
843 struct uni_table_desc *ptab_descr;
844 const u8 *unirom = adapter->fw->data;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000845 int mn_present = qlcnic_has_mn(adapter);
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000846 __le32 entries;
847 __le32 entry_size;
848 __le32 tab_size;
849 u32 i;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000850
851 ptab_descr = qlcnic_get_table_desc(unirom,
852 QLCNIC_UNI_DIR_SECT_PRODUCT_TBL);
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000853 if (!ptab_descr)
854 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000855
856 entries = cpu_to_le32(ptab_descr->num_entries);
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000857 entry_size = cpu_to_le32(ptab_descr->entry_size);
858 tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
859
860 if (adapter->fw->size < tab_size)
861 return -EINVAL;
862
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000863nomn:
864 for (i = 0; i < entries; i++) {
865
866 __le32 flags, file_chiprev, offs;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000867 u8 chiprev = adapter->ahw->revision_id;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000868 u32 flagbit;
869
870 offs = cpu_to_le32(ptab_descr->findex) +
871 (i * cpu_to_le32(ptab_descr->entry_size));
872 flags = cpu_to_le32(*((int *)&unirom[offs] +
873 QLCNIC_UNI_FLAGS_OFF));
874 file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
875 QLCNIC_UNI_CHIP_REV_OFF));
876
877 flagbit = mn_present ? 1 : 2;
878
879 if ((chiprev == file_chiprev) &&
880 ((1ULL << flagbit) & flags)) {
881 adapter->file_prd_off = offs;
882 return 0;
883 }
884 }
885 if (mn_present) {
886 mn_present = 0;
887 goto nomn;
888 }
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +0000889 return -EINVAL;
890}
891
892static int
893qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter)
894{
895 if (qlcnic_validate_header(adapter)) {
896 dev_err(&adapter->pdev->dev,
897 "unified image: header validation failed\n");
898 return -EINVAL;
899 }
900
901 if (qlcnic_validate_product_offs(adapter)) {
902 dev_err(&adapter->pdev->dev,
903 "unified image: product validation failed\n");
904 return -EINVAL;
905 }
906
907 if (qlcnic_validate_bootld(adapter)) {
908 dev_err(&adapter->pdev->dev,
909 "unified image: bootld validation failed\n");
910 return -EINVAL;
911 }
912
913 if (qlcnic_validate_fw(adapter)) {
914 dev_err(&adapter->pdev->dev,
915 "unified image: firmware validation failed\n");
916 return -EINVAL;
917 }
918
919 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000920}
921
922static
923struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter,
924 u32 section, u32 idx_offset)
925{
926 const u8 *unirom = adapter->fw->data;
927 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
928 idx_offset));
929 struct uni_table_desc *tab_desc;
930 __le32 offs;
931
932 tab_desc = qlcnic_get_table_desc(unirom, section);
933
934 if (tab_desc == NULL)
935 return NULL;
936
937 offs = cpu_to_le32(tab_desc->findex) +
938 (cpu_to_le32(tab_desc->entry_size) * idx);
939
940 return (struct uni_data_desc *)&unirom[offs];
941}
942
943static u8 *
944qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter)
945{
946 u32 offs = QLCNIC_BOOTLD_START;
947
948 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
949 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
950 QLCNIC_UNI_DIR_SECT_BOOTLD,
951 QLCNIC_UNI_BOOTLD_IDX_OFF))->findex);
952
953 return (u8 *)&adapter->fw->data[offs];
954}
955
956static u8 *
957qlcnic_get_fw_offs(struct qlcnic_adapter *adapter)
958{
959 u32 offs = QLCNIC_IMAGE_START;
960
961 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
962 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
963 QLCNIC_UNI_DIR_SECT_FW,
964 QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex);
965
966 return (u8 *)&adapter->fw->data[offs];
967}
968
969static __le32
970qlcnic_get_fw_size(struct qlcnic_adapter *adapter)
971{
972 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
973 return cpu_to_le32((qlcnic_get_data_desc(adapter,
974 QLCNIC_UNI_DIR_SECT_FW,
975 QLCNIC_UNI_FIRMWARE_IDX_OFF))->size);
976 else
977 return cpu_to_le32(
978 *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]);
979}
980
981static __le32
982qlcnic_get_fw_version(struct qlcnic_adapter *adapter)
983{
984 struct uni_data_desc *fw_data_desc;
985 const struct firmware *fw = adapter->fw;
986 __le32 major, minor, sub;
987 const u8 *ver_str;
988 int i, ret;
989
990 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
991 return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]);
992
993 fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW,
994 QLCNIC_UNI_FIRMWARE_IDX_OFF);
995 ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
996 cpu_to_le32(fw_data_desc->size) - 17;
997
998 for (i = 0; i < 12; i++) {
999 if (!strncmp(&ver_str[i], "REV=", 4)) {
1000 ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
1001 &major, &minor, &sub);
1002 if (ret != 3)
1003 return 0;
1004 else
1005 return major + (minor << 8) + (sub << 16);
1006 }
1007 }
1008
1009 return 0;
1010}
1011
1012static __le32
1013qlcnic_get_bios_version(struct qlcnic_adapter *adapter)
1014{
1015 const struct firmware *fw = adapter->fw;
1016 __le32 bios_ver, prd_off = adapter->file_prd_off;
1017
1018 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
1019 return cpu_to_le32(
1020 *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]);
1021
1022 bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
1023 + QLCNIC_UNI_BIOS_VERSION_OFF));
1024
Amit Kumar Salechaaddd5ab2010-03-08 00:14:49 +00001025 return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001026}
1027
Sucheta Chakraborty091754a2010-08-19 05:08:32 +00001028static void qlcnic_rom_lock_recovery(struct qlcnic_adapter *adapter)
1029{
1030 if (qlcnic_pcie_sem_lock(adapter, 2, QLCNIC_ROM_LOCK_ID))
1031 dev_info(&adapter->pdev->dev, "Resetting rom_lock\n");
1032
1033 qlcnic_pcie_sem_unlock(adapter, 2);
1034}
1035
Sony Chacko4e708122010-08-31 17:17:44 +00001036static int
1037qlcnic_check_fw_hearbeat(struct qlcnic_adapter *adapter)
1038{
1039 u32 heartbeat, ret = -EIO;
1040 int retries = QLCNIC_HEARTBEAT_CHECK_RETRY_COUNT;
1041
1042 adapter->heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1043
1044 do {
1045 msleep(QLCNIC_HEARTBEAT_PERIOD_MSECS);
1046 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1047 if (heartbeat != adapter->heartbeat) {
1048 ret = QLCNIC_RCODE_SUCCESS;
1049 break;
1050 }
1051 } while (--retries);
1052
1053 return ret;
1054}
1055
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001056int
1057qlcnic_need_fw_reset(struct qlcnic_adapter *adapter)
1058{
Sony Chacko4e708122010-08-31 17:17:44 +00001059 if (qlcnic_check_fw_hearbeat(adapter)) {
Sucheta Chakraborty091754a2010-08-19 05:08:32 +00001060 qlcnic_rom_lock_recovery(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001061 return 1;
Sucheta Chakraborty091754a2010-08-19 05:08:32 +00001062 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001063
Sucheta Chakraborty091754a2010-08-19 05:08:32 +00001064 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001065 return 1;
1066
Amit Kumar Salecha97f29d82010-09-16 19:14:42 +00001067 if (adapter->fw)
1068 return 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001069
1070 return 0;
1071}
1072
1073static const char *fw_name[] = {
1074 QLCNIC_UNIFIED_ROMIMAGE_NAME,
1075 QLCNIC_FLASH_ROMIMAGE_NAME,
1076};
1077
1078int
1079qlcnic_load_firmware(struct qlcnic_adapter *adapter)
1080{
1081 u64 *ptr64;
1082 u32 i, flashaddr, size;
1083 const struct firmware *fw = adapter->fw;
1084 struct pci_dev *pdev = adapter->pdev;
1085
1086 dev_info(&pdev->dev, "loading firmware from %s\n",
1087 fw_name[adapter->fw_type]);
1088
1089 if (fw) {
1090 __le64 data;
1091
1092 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1093
1094 ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter);
1095 flashaddr = QLCNIC_BOOTLD_START;
1096
1097 for (i = 0; i < size; i++) {
1098 data = cpu_to_le64(ptr64[i]);
1099
1100 if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data))
1101 return -EIO;
1102
1103 flashaddr += 8;
1104 }
1105
1106 size = (__force u32)qlcnic_get_fw_size(adapter) / 8;
1107
1108 ptr64 = (u64 *)qlcnic_get_fw_offs(adapter);
1109 flashaddr = QLCNIC_IMAGE_START;
1110
1111 for (i = 0; i < size; i++) {
1112 data = cpu_to_le64(ptr64[i]);
1113
1114 if (qlcnic_pci_mem_write_2M(adapter,
1115 flashaddr, data))
1116 return -EIO;
1117
1118 flashaddr += 8;
1119 }
Amit Kumar Salecha0bc92b52010-04-01 19:01:28 +00001120
1121 size = (__force u32)qlcnic_get_fw_size(adapter) % 8;
1122 if (size) {
1123 data = cpu_to_le64(ptr64[i]);
1124
1125 if (qlcnic_pci_mem_write_2M(adapter,
1126 flashaddr, data))
1127 return -EIO;
1128 }
1129
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001130 } else {
1131 u64 data;
1132 u32 hi, lo;
Sritej Velagaf8d54812011-04-01 14:28:26 +00001133 int ret;
1134 struct qlcnic_flt_entry bootld_entry;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001135
Sritej Velagaf8d54812011-04-01 14:28:26 +00001136 ret = qlcnic_get_flt_entry(adapter, QLCNIC_BOOTLD_REGION,
1137 &bootld_entry);
1138 if (!ret) {
1139 size = bootld_entry.size / 8;
1140 flashaddr = bootld_entry.start_addr;
1141 } else {
1142 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1143 flashaddr = QLCNIC_BOOTLD_START;
1144 dev_info(&pdev->dev,
1145 "using legacy method to get flash fw region");
1146 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001147
1148 for (i = 0; i < size; i++) {
1149 if (qlcnic_rom_fast_read(adapter,
1150 flashaddr, (int *)&lo) != 0)
1151 return -EIO;
1152 if (qlcnic_rom_fast_read(adapter,
1153 flashaddr + 4, (int *)&hi) != 0)
1154 return -EIO;
1155
1156 data = (((u64)hi << 32) | lo);
1157
1158 if (qlcnic_pci_mem_write_2M(adapter,
1159 flashaddr, data))
1160 return -EIO;
1161
1162 flashaddr += 8;
1163 }
1164 }
1165 msleep(1);
1166
1167 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020);
1168 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e);
1169 return 0;
1170}
1171
1172static int
1173qlcnic_validate_firmware(struct qlcnic_adapter *adapter)
1174{
1175 __le32 val;
schacko8f891382010-06-17 02:56:40 +00001176 u32 ver, bios, min_size;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001177 struct pci_dev *pdev = adapter->pdev;
1178 const struct firmware *fw = adapter->fw;
1179 u8 fw_type = adapter->fw_type;
1180
1181 if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) {
Sucheta Chakrabortyb7eff102010-03-08 00:14:48 +00001182 if (qlcnic_validate_unified_romimage(adapter))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001183 return -EINVAL;
1184
1185 min_size = QLCNIC_UNI_FW_MIN_SIZE;
1186 } else {
1187 val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]);
1188 if ((__force u32)val != QLCNIC_BDINFO_MAGIC)
1189 return -EINVAL;
1190
1191 min_size = QLCNIC_FW_MIN_SIZE;
1192 }
1193
1194 if (fw->size < min_size)
1195 return -EINVAL;
1196
1197 val = qlcnic_get_fw_version(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001198 ver = QLCNIC_DECODE_VERSION(val);
1199
schacko8f891382010-06-17 02:56:40 +00001200 if (ver < QLCNIC_MIN_FW_VERSION) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001201 dev_err(&pdev->dev,
1202 "%s: firmware version %d.%d.%d unsupported\n",
1203 fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
1204 return -EINVAL;
1205 }
1206
1207 val = qlcnic_get_bios_version(adapter);
1208 qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios);
1209 if ((__force u32)val != bios) {
1210 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
1211 fw_name[fw_type]);
1212 return -EINVAL;
1213 }
1214
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001215 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
1216 return 0;
1217}
1218
1219static void
1220qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter)
1221{
1222 u8 fw_type;
1223
1224 switch (adapter->fw_type) {
1225 case QLCNIC_UNKNOWN_ROMIMAGE:
1226 fw_type = QLCNIC_UNIFIED_ROMIMAGE;
1227 break;
1228
1229 case QLCNIC_UNIFIED_ROMIMAGE:
1230 default:
1231 fw_type = QLCNIC_FLASH_ROMIMAGE;
1232 break;
1233 }
1234
1235 adapter->fw_type = fw_type;
1236}
1237
1238
1239
1240void qlcnic_request_firmware(struct qlcnic_adapter *adapter)
1241{
1242 struct pci_dev *pdev = adapter->pdev;
1243 int rc;
1244
1245 adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE;
1246
1247next:
1248 qlcnic_get_next_fwtype(adapter);
1249
1250 if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) {
1251 adapter->fw = NULL;
1252 } else {
1253 rc = request_firmware(&adapter->fw,
1254 fw_name[adapter->fw_type], &pdev->dev);
1255 if (rc != 0)
1256 goto next;
1257
1258 rc = qlcnic_validate_firmware(adapter);
1259 if (rc != 0) {
1260 release_firmware(adapter->fw);
1261 msleep(1);
1262 goto next;
1263 }
1264 }
1265}
1266
1267
1268void
1269qlcnic_release_firmware(struct qlcnic_adapter *adapter)
1270{
1271 if (adapter->fw)
1272 release_firmware(adapter->fw);
1273 adapter->fw = NULL;
1274}
1275
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001276static void
1277qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1278 struct qlcnic_fw_msg *msg)
1279{
1280 u32 cable_OUI;
1281 u16 cable_len;
1282 u16 link_speed;
1283 u8 link_status, module, duplex, autoneg;
1284 struct net_device *netdev = adapter->netdev;
1285
1286 adapter->has_link_events = 1;
1287
1288 cable_OUI = msg->body[1] & 0xffffffff;
1289 cable_len = (msg->body[1] >> 32) & 0xffff;
1290 link_speed = (msg->body[1] >> 48) & 0xffff;
1291
1292 link_status = msg->body[2] & 0xff;
1293 duplex = (msg->body[2] >> 16) & 0xff;
1294 autoneg = (msg->body[2] >> 24) & 0xff;
1295
1296 module = (msg->body[2] >> 8) & 0xff;
1297 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
1298 dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, "
1299 "length %d\n", cable_OUI, cable_len);
1300 else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
1301 dev_info(&netdev->dev, "unsupported cable length %d\n",
1302 cable_len);
1303
1304 qlcnic_advert_link_change(adapter, link_status);
1305
1306 if (duplex == LINKEVENT_FULL_DUPLEX)
1307 adapter->link_duplex = DUPLEX_FULL;
1308 else
1309 adapter->link_duplex = DUPLEX_HALF;
1310
1311 adapter->module_type = module;
1312 adapter->link_autoneg = autoneg;
1313 adapter->link_speed = link_speed;
1314}
1315
1316static void
1317qlcnic_handle_fw_message(int desc_cnt, int index,
1318 struct qlcnic_host_sds_ring *sds_ring)
1319{
1320 struct qlcnic_fw_msg msg;
1321 struct status_desc *desc;
1322 int i = 0, opcode;
1323
1324 while (desc_cnt > 0 && i < 8) {
1325 desc = &sds_ring->desc_head[index];
1326 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1327 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1328
1329 index = get_next_index(index, sds_ring->num_desc);
1330 desc_cnt--;
1331 }
1332
1333 opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
1334 switch (opcode) {
1335 case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1336 qlcnic_handle_linkevent(sds_ring->adapter, &msg);
1337 break;
1338 default:
1339 break;
1340 }
1341}
1342
1343static int
1344qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
1345 struct qlcnic_host_rds_ring *rds_ring,
1346 struct qlcnic_rx_buffer *buffer)
1347{
1348 struct sk_buff *skb;
1349 dma_addr_t dma;
1350 struct pci_dev *pdev = adapter->pdev;
1351
Amit Kumar Salecha96659822010-06-22 03:18:58 +00001352 skb = dev_alloc_skb(rds_ring->skb_size);
1353 if (!skb) {
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001354 adapter->stats.skb_alloc_failure++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001355 return -ENOMEM;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001356 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001357
Eric Dumazet04746ff2010-09-17 22:58:08 -07001358 skb_reserve(skb, NET_IP_ALIGN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001359
1360 dma = pci_map_single(pdev, skb->data,
1361 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1362
1363 if (pci_dma_mapping_error(pdev, dma)) {
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001364 adapter->stats.rx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001365 dev_kfree_skb_any(skb);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001366 return -ENOMEM;
1367 }
1368
1369 buffer->skb = skb;
1370 buffer->dma = dma;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001371
1372 return 0;
1373}
1374
1375static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1376 struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum)
1377{
1378 struct qlcnic_rx_buffer *buffer;
1379 struct sk_buff *skb;
1380
1381 buffer = &rds_ring->rx_buf_arr[index];
1382
Amit Kumar Salecha96659822010-06-22 03:18:58 +00001383 if (unlikely(buffer->skb == NULL)) {
1384 WARN_ON(1);
1385 return NULL;
1386 }
1387
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001388 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1389 PCI_DMA_FROMDEVICE);
1390
1391 skb = buffer->skb;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001392
Michał Mirosław135d84a2011-04-19 03:03:57 +00001393 if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
1394 (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001395 adapter->stats.csummed++;
1396 skb->ip_summed = CHECKSUM_UNNECESSARY;
1397 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001398 skb_checksum_none_assert(skb);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001399 }
1400
1401 skb->dev = adapter->netdev;
1402
1403 buffer->skb = NULL;
Amit Kumar Salecha96659822010-06-22 03:18:58 +00001404
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001405 return skb;
1406}
1407
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001408static inline int
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001409qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter, struct sk_buff *skb,
1410 u16 *vlan_tag)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001411{
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001412 struct ethhdr *eth_hdr;
1413
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001414 if (!__vlan_get_tag(skb, vlan_tag)) {
1415 eth_hdr = (struct ethhdr *) skb->data;
1416 memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
1417 skb_pull(skb, VLAN_HLEN);
1418 }
1419 if (!adapter->pvid)
1420 return 0;
1421
1422 if (*vlan_tag == adapter->pvid) {
1423 /* Outer vlan tag. Packet should follow non-vlan path */
1424 *vlan_tag = 0xffff;
1425 return 0;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001426 }
1427 if (adapter->flags & QLCNIC_TAGGING_ENABLED)
1428 return 0;
1429
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001430 return -EINVAL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001431}
1432
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001433static struct qlcnic_rx_buffer *
1434qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1435 struct qlcnic_host_sds_ring *sds_ring,
1436 int ring, u64 sts_data0)
1437{
1438 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001439 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001440 struct qlcnic_rx_buffer *buffer;
1441 struct sk_buff *skb;
1442 struct qlcnic_host_rds_ring *rds_ring;
1443 int index, length, cksum, pkt_offset;
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001444 u16 vid = 0xffff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001445
1446 if (unlikely(ring >= adapter->max_rds_rings))
1447 return NULL;
1448
1449 rds_ring = &recv_ctx->rds_rings[ring];
1450
1451 index = qlcnic_get_sts_refhandle(sts_data0);
1452 if (unlikely(index >= rds_ring->num_desc))
1453 return NULL;
1454
1455 buffer = &rds_ring->rx_buf_arr[index];
1456
1457 length = qlcnic_get_sts_totallength(sts_data0);
1458 cksum = qlcnic_get_sts_status(sts_data0);
1459 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1460
1461 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1462 if (!skb)
1463 return buffer;
1464
1465 if (length > rds_ring->skb_size)
1466 skb_put(skb, rds_ring->skb_size);
1467 else
1468 skb_put(skb, length);
1469
1470 if (pkt_offset)
1471 skb_pull(skb, pkt_offset);
1472
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001473 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1474 adapter->stats.rxdropped++;
1475 dev_kfree_skb(skb);
1476 return buffer;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001477 }
1478
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001479 skb->protocol = eth_type_trans(skb, netdev);
1480
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00001481 if (vid != 0xffff)
1482 __vlan_hwaccel_put_tag(skb, vid);
1483
1484 napi_gro_receive(&sds_ring->napi, skb);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001485
1486 adapter->stats.rx_pkts++;
1487 adapter->stats.rxbytes += length;
1488
1489 return buffer;
1490}
1491
1492#define QLC_TCP_HDR_SIZE 20
1493#define QLC_TCP_TS_OPTION_SIZE 12
1494#define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1495
1496static struct qlcnic_rx_buffer *
1497qlcnic_process_lro(struct qlcnic_adapter *adapter,
1498 struct qlcnic_host_sds_ring *sds_ring,
1499 int ring, u64 sts_data0, u64 sts_data1)
1500{
1501 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001502 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001503 struct qlcnic_rx_buffer *buffer;
1504 struct sk_buff *skb;
1505 struct qlcnic_host_rds_ring *rds_ring;
1506 struct iphdr *iph;
1507 struct tcphdr *th;
1508 bool push, timestamp;
1509 int l2_hdr_offset, l4_hdr_offset;
1510 int index;
1511 u16 lro_length, length, data_offset;
1512 u32 seq_number;
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001513 u16 vid = 0xffff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001514
1515 if (unlikely(ring > adapter->max_rds_rings))
1516 return NULL;
1517
1518 rds_ring = &recv_ctx->rds_rings[ring];
1519
1520 index = qlcnic_get_lro_sts_refhandle(sts_data0);
1521 if (unlikely(index > rds_ring->num_desc))
1522 return NULL;
1523
1524 buffer = &rds_ring->rx_buf_arr[index];
1525
1526 timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1527 lro_length = qlcnic_get_lro_sts_length(sts_data0);
1528 l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1529 l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1530 push = qlcnic_get_lro_sts_push_flag(sts_data0);
1531 seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1532
1533 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1534 if (!skb)
1535 return buffer;
1536
1537 if (timestamp)
1538 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1539 else
1540 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1541
1542 skb_put(skb, lro_length + data_offset);
1543
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001544 skb_pull(skb, l2_hdr_offset);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001545
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001546 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1547 adapter->stats.rxdropped++;
1548 dev_kfree_skb(skb);
1549 return buffer;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001550 }
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001551
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001552 skb->protocol = eth_type_trans(skb, netdev);
1553
1554 iph = (struct iphdr *)skb->data;
1555 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1556
1557 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1558 iph->tot_len = htons(length);
1559 iph->check = 0;
1560 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1561 th->psh = push;
1562 th->seq = htonl(seq_number);
1563
1564 length = skb->len;
1565
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00001566 if (vid != 0xffff)
1567 __vlan_hwaccel_put_tag(skb, vid);
1568 netif_receive_skb(skb);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001569
1570 adapter->stats.lro_pkts++;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001571 adapter->stats.lrobytes += length;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001572
1573 return buffer;
1574}
1575
1576int
1577qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1578{
1579 struct qlcnic_adapter *adapter = sds_ring->adapter;
1580 struct list_head *cur;
1581 struct status_desc *desc;
1582 struct qlcnic_rx_buffer *rxbuf;
1583 u64 sts_data0, sts_data1;
1584
1585 int count = 0;
1586 int opcode, ring, desc_cnt;
1587 u32 consumer = sds_ring->consumer;
1588
1589 while (count < max) {
1590 desc = &sds_ring->desc_head[consumer];
1591 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1592
1593 if (!(sts_data0 & STATUS_OWNER_HOST))
1594 break;
1595
1596 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1597 opcode = qlcnic_get_sts_opcode(sts_data0);
1598
1599 switch (opcode) {
1600 case QLCNIC_RXPKT_DESC:
1601 case QLCNIC_OLD_RXPKT_DESC:
1602 case QLCNIC_SYN_OFFLOAD:
1603 ring = qlcnic_get_sts_type(sts_data0);
1604 rxbuf = qlcnic_process_rcv(adapter, sds_ring,
1605 ring, sts_data0);
1606 break;
1607 case QLCNIC_LRO_DESC:
1608 ring = qlcnic_get_lro_sts_type(sts_data0);
1609 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1610 rxbuf = qlcnic_process_lro(adapter, sds_ring,
1611 ring, sts_data0, sts_data1);
1612 break;
1613 case QLCNIC_RESPONSE_DESC:
1614 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1615 default:
1616 goto skip;
1617 }
1618
1619 WARN_ON(desc_cnt > 1);
1620
Amit Kumar Salecha96659822010-06-22 03:18:58 +00001621 if (likely(rxbuf))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001622 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001623 else
1624 adapter->stats.null_rxbuf++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001625
1626skip:
1627 for (; desc_cnt > 0; desc_cnt--) {
1628 desc = &sds_ring->desc_head[consumer];
1629 desc->status_desc_data[0] =
1630 cpu_to_le64(STATUS_OWNER_PHANTOM);
1631 consumer = get_next_index(consumer, sds_ring->num_desc);
1632 }
1633 count++;
1634 }
1635
1636 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1637 struct qlcnic_host_rds_ring *rds_ring =
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001638 &adapter->recv_ctx->rds_rings[ring];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001639
1640 if (!list_empty(&sds_ring->free_list[ring])) {
1641 list_for_each(cur, &sds_ring->free_list[ring]) {
1642 rxbuf = list_entry(cur,
1643 struct qlcnic_rx_buffer, list);
1644 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1645 }
1646 spin_lock(&rds_ring->lock);
1647 list_splice_tail_init(&sds_ring->free_list[ring],
1648 &rds_ring->free_list);
1649 spin_unlock(&rds_ring->lock);
1650 }
1651
1652 qlcnic_post_rx_buffers_nodb(adapter, rds_ring);
1653 }
1654
1655 if (count) {
1656 sds_ring->consumer = consumer;
1657 writel(consumer, sds_ring->crb_sts_consumer);
1658 }
1659
1660 return count;
1661}
1662
1663void
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001664qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001665 struct qlcnic_host_rds_ring *rds_ring)
1666{
1667 struct rcv_desc *pdesc;
1668 struct qlcnic_rx_buffer *buffer;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001669 int count = 0;
1670 u32 producer;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001671 struct list_head *head;
1672
1673 producer = rds_ring->producer;
1674
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001675 head = &rds_ring->free_list;
1676 while (!list_empty(head)) {
1677
1678 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1679
1680 if (!buffer->skb) {
1681 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1682 break;
1683 }
1684
1685 count++;
1686 list_del(&buffer->list);
1687
1688 /* make a rcv descriptor */
1689 pdesc = &rds_ring->desc_head[producer];
1690 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1691 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1692 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1693
1694 producer = get_next_index(producer, rds_ring->num_desc);
1695 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001696
1697 if (count) {
1698 rds_ring->producer = producer;
1699 writel((producer-1) & (rds_ring->num_desc-1),
1700 rds_ring->crb_rcv_producer);
1701 }
1702}
1703
1704static void
1705qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
1706 struct qlcnic_host_rds_ring *rds_ring)
1707{
1708 struct rcv_desc *pdesc;
1709 struct qlcnic_rx_buffer *buffer;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001710 int count = 0;
1711 uint32_t producer;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001712 struct list_head *head;
1713
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001714 if (!spin_trylock(&rds_ring->lock))
1715 return;
1716
Amit Kumar Salechaed6f1352010-04-22 02:51:40 +00001717 producer = rds_ring->producer;
1718
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001719 head = &rds_ring->free_list;
1720 while (!list_empty(head)) {
1721
1722 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1723
1724 if (!buffer->skb) {
1725 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1726 break;
1727 }
1728
1729 count++;
1730 list_del(&buffer->list);
1731
1732 /* make a rcv descriptor */
1733 pdesc = &rds_ring->desc_head[producer];
1734 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1735 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1736 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1737
1738 producer = get_next_index(producer, rds_ring->num_desc);
1739 }
1740
1741 if (count) {
1742 rds_ring->producer = producer;
1743 writel((producer - 1) & (rds_ring->num_desc - 1),
1744 rds_ring->crb_rcv_producer);
1745 }
1746 spin_unlock(&rds_ring->lock);
1747}
1748
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001749void
1750qlcnic_fetch_mac(struct qlcnic_adapter *adapter, u32 off1, u32 off2,
1751 u8 alt_mac, u8 *mac)
1752{
1753 u32 mac_low, mac_high;
1754 int i;
1755
1756 mac_low = QLCRD32(adapter, off1);
1757 mac_high = QLCRD32(adapter, off2);
1758
1759 if (alt_mac) {
1760 mac_low |= (mac_low >> 16) | (mac_high << 16);
1761 mac_high >>= 16;
1762 }
1763
1764 for (i = 0; i < 2; i++)
1765 mac[i] = (u8)(mac_high >> ((1 - i) * 8));
1766 for (i = 2; i < 6; i++)
1767 mac[i] = (u8)(mac_low >> ((5 - i) * 8));
1768}