OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / net / ethernet / qlogic / qlcnic / qlcnic_init.c
blob38669583840ce0a9e9f3ad31cd0d0d02a7b95bd6
1 /*
2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2010 QLogic Corporation
5 * See LICENSE.qlcnic for copyright and licensing details.
6 */
8 #include <linux/netdevice.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/if_vlan.h>
12 #include "qlcnic.h"
14 struct crb_addr_pair {
15 u32 addr;
16 u32 data;
19 #define QLCNIC_MAX_CRB_XFORM 60
20 static unsigned int crb_addr_xform[QLCNIC_MAX_CRB_XFORM];
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)
26 #define QLCNIC_ADDR_ERROR (0xffffffff)
28 static void
29 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
30 struct qlcnic_host_rds_ring *rds_ring);
32 static int
33 qlcnic_check_fw_hearbeat(struct qlcnic_adapter *adapter);
35 static void crb_addr_transform_setup(void)
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);
90 void qlcnic_release_rx_buffers(struct qlcnic_adapter *adapter)
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;
97 recv_ctx = adapter->recv_ctx;
98 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]);
102 if (rx_buf->skb == NULL)
103 continue;
105 pci_unmap_single(adapter->pdev,
106 rx_buf->dma,
107 rds_ring->dma_size,
108 PCI_DMA_FROMDEVICE);
110 dev_kfree_skb_any(rx_buf->skb);
115 void qlcnic_reset_rx_buffers_list(struct qlcnic_adapter *adapter)
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;
122 recv_ctx = adapter->recv_ctx;
123 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
124 rds_ring = &recv_ctx->rds_rings[ring];
126 INIT_LIST_HEAD(&rds_ring->free_list);
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++;
137 void qlcnic_release_tx_buffers(struct qlcnic_adapter *adapter)
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;
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;
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;
161 if (cmd_buf->skb) {
162 dev_kfree_skb_any(cmd_buf->skb);
163 cmd_buf->skb = NULL;
165 cmd_buf++;
169 void qlcnic_free_sw_resources(struct qlcnic_adapter *adapter)
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;
176 recv_ctx = adapter->recv_ctx;
178 if (recv_ctx->rds_rings == NULL)
179 goto skip_rds;
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;
186 kfree(recv_ctx->rds_rings);
188 skip_rds:
189 if (adapter->tx_ring == NULL)
190 return;
192 tx_ring = adapter->tx_ring;
193 vfree(tx_ring->cmd_buf_arr);
194 tx_ring->cmd_buf_arr = NULL;
195 kfree(adapter->tx_ring);
196 adapter->tx_ring = NULL;
199 int qlcnic_alloc_sw_resources(struct qlcnic_adapter *adapter)
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;
208 struct qlcnic_cmd_buffer *cmd_buf_arr;
209 struct net_device *netdev = adapter->netdev;
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;
217 adapter->tx_ring = tx_ring;
219 tx_ring->num_desc = adapter->num_txd;
220 tx_ring->txq = netdev_get_tx_queue(netdev, 0);
222 cmd_buf_arr = vzalloc(TX_BUFF_RINGSIZE(tx_ring));
223 if (cmd_buf_arr == NULL) {
224 dev_err(&netdev->dev, "failed to allocate cmd buffer ring\n");
225 goto err_out;
227 tx_ring->cmd_buf_arr = cmd_buf_arr;
229 recv_ctx = adapter->recv_ctx;
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");
235 goto err_out;
237 recv_ctx->rds_rings = rds_ring;
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;
244 rds_ring->dma_size = QLCNIC_P3P_RX_BUF_MAX_LEN;
245 rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
246 break;
248 case RCV_RING_JUMBO:
249 rds_ring->num_desc = adapter->num_jumbo_rxd;
250 rds_ring->dma_size =
251 QLCNIC_P3P_RX_JUMBO_BUF_MAX_LEN;
253 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
254 rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA;
256 rds_ring->skb_size =
257 rds_ring->dma_size + NET_IP_ALIGN;
258 break;
260 rds_ring->rx_buf_arr = vzalloc(RCV_BUFF_RINGSIZE(rds_ring));
261 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;
266 INIT_LIST_HEAD(&rds_ring->free_list);
268 * Now go through all of them, set reference handles
269 * and put them in the queues.
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;
276 rx_buf++;
278 spin_lock_init(&rds_ring->lock);
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;
287 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
288 INIT_LIST_HEAD(&sds_ring->free_list[i]);
291 return 0;
293 err_out:
294 qlcnic_free_sw_resources(adapter);
295 return -ENOMEM;
299 * Utility to translate from internal Phantom CRB address
300 * to external PCI CRB address.
302 static u32 qlcnic_decode_crb_addr(u32 addr)
304 int i;
305 u32 base_addr, offset, pci_base;
307 crb_addr_transform_setup();
309 pci_base = QLCNIC_ADDR_ERROR;
310 base_addr = addr & 0xfff00000;
311 offset = addr & 0x000fffff;
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;
319 if (pci_base == QLCNIC_ADDR_ERROR)
320 return pci_base;
321 else
322 return pci_base + offset;
325 #define QLCNIC_MAX_ROM_WAIT_USEC 100
327 static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter)
329 long timeout = 0;
330 long done = 0;
332 cond_resched();
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;
342 udelay(1);
344 return 0;
347 static int do_rom_fast_read(struct qlcnic_adapter *adapter,
348 u32 addr, u32 *valp)
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;
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);
363 *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA);
364 return 0;
367 static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
368 u8 *bytes, size_t size)
370 int addridx;
371 int ret = 0;
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;
382 return ret;
386 qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
387 u8 *bytes, size_t size)
389 int ret;
391 ret = qlcnic_rom_lock(adapter);
392 if (ret < 0)
393 return ret;
395 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
397 qlcnic_rom_unlock(adapter);
398 return ret;
401 int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, u32 addr, u32 *valp)
403 int ret;
405 if (qlcnic_rom_lock(adapter) != 0)
406 return -EIO;
408 ret = do_rom_fast_read(adapter, addr, valp);
409 qlcnic_rom_unlock(adapter);
410 return ret;
413 int qlcnic_pinit_from_rom(struct qlcnic_adapter *adapter)
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;
422 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
423 QLCWR32(adapter, CRB_RCVPEG_STATE, 0);
425 /* Halt all the indiviual PEGs and other blocks */
426 /* disable all I2Q */
427 QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x10, 0x0);
428 QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x14, 0x0);
429 QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x18, 0x0);
430 QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x1c, 0x0);
431 QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x20, 0x0);
432 QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x24, 0x0);
434 /* disable all niu interrupts */
435 QLCWR32(adapter, QLCNIC_CRB_NIU + 0x40, 0xff);
436 /* disable xge rx/tx */
437 QLCWR32(adapter, QLCNIC_CRB_NIU + 0x70000, 0x00);
438 /* disable xg1 rx/tx */
439 QLCWR32(adapter, QLCNIC_CRB_NIU + 0x80000, 0x00);
440 /* disable sideband mac */
441 QLCWR32(adapter, QLCNIC_CRB_NIU + 0x90000, 0x00);
442 /* disable ap0 mac */
443 QLCWR32(adapter, QLCNIC_CRB_NIU + 0xa0000, 0x00);
444 /* disable ap1 mac */
445 QLCWR32(adapter, QLCNIC_CRB_NIU + 0xb0000, 0x00);
447 /* halt sre */
448 val = QLCRD32(adapter, QLCNIC_CRB_SRE + 0x1000);
449 QLCWR32(adapter, QLCNIC_CRB_SRE + 0x1000, val & (~(0x1)));
451 /* halt epg */
452 QLCWR32(adapter, QLCNIC_CRB_EPG + 0x1300, 0x1);
454 /* halt timers */
455 QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x0, 0x0);
456 QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x8, 0x0);
457 QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x10, 0x0);
458 QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x18, 0x0);
459 QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x100, 0x0);
460 QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x200, 0x0);
461 /* halt pegs */
462 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c, 1);
463 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c, 1);
464 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c, 1);
465 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c, 1);
466 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c, 1);
467 msleep(20);
469 qlcnic_rom_unlock(adapter);
470 /* big hammer don't reset CAM block on reset */
471 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xfeffffff);
473 /* Init HW CRB block */
474 if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) ||
475 qlcnic_rom_fast_read(adapter, 4, &n) != 0) {
476 dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n);
477 return -EIO;
479 offset = n & 0xffffU;
480 n = (n >> 16) & 0xffffU;
482 if (n >= 1024) {
483 dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n");
484 return -EIO;
487 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
488 if (buf == NULL) {
489 dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n");
490 return -ENOMEM;
493 for (i = 0; i < n; i++) {
494 if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
495 qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
496 kfree(buf);
497 return -EIO;
500 buf[i].addr = addr;
501 buf[i].data = val;
504 for (i = 0; i < n; i++) {
506 off = qlcnic_decode_crb_addr(buf[i].addr);
507 if (off == QLCNIC_ADDR_ERROR) {
508 dev_err(&pdev->dev, "CRB init value out of range %x\n",
509 buf[i].addr);
510 continue;
512 off += QLCNIC_PCI_CRBSPACE;
514 if (off & 1)
515 continue;
517 /* skipping cold reboot MAGIC */
518 if (off == QLCNIC_CAM_RAM(0x1fc))
519 continue;
520 if (off == (QLCNIC_CRB_I2C0 + 0x1c))
521 continue;
522 if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */
523 continue;
524 if (off == (ROMUSB_GLB + 0xa8))
525 continue;
526 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
527 continue;
528 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
529 continue;
530 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
531 continue;
532 if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET)
533 continue;
534 /* skip the function enable register */
535 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION))
536 continue;
537 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION2))
538 continue;
539 if ((off & 0x0ff00000) == QLCNIC_CRB_SMB)
540 continue;
542 init_delay = 1;
543 /* After writing this register, HW needs time for CRB */
544 /* to quiet down (else crb_window returns 0xffffffff) */
545 if (off == QLCNIC_ROMUSB_GLB_SW_RESET)
546 init_delay = 1000;
548 QLCWR32(adapter, off, buf[i].data);
550 msleep(init_delay);
552 kfree(buf);
554 /* Initialize protocol process engine */
555 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e);
556 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8);
557 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8);
558 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0);
559 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0);
560 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0);
561 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0);
562 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0);
563 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0);
564 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0);
565 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0);
566 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x8, 0);
567 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0xc, 0);
568 msleep(1);
570 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
571 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
573 return 0;
576 static int qlcnic_cmd_peg_ready(struct qlcnic_adapter *adapter)
578 u32 val;
579 int retries = QLCNIC_CMDPEG_CHECK_RETRY_COUNT;
581 do {
582 val = QLCRD32(adapter, CRB_CMDPEG_STATE);
584 switch (val) {
585 case PHAN_INITIALIZE_COMPLETE:
586 case PHAN_INITIALIZE_ACK:
587 return 0;
588 case PHAN_INITIALIZE_FAILED:
589 goto out_err;
590 default:
591 break;
594 msleep(QLCNIC_CMDPEG_CHECK_DELAY);
596 } while (--retries);
598 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
600 out_err:
601 dev_err(&adapter->pdev->dev, "Command Peg initialization not "
602 "complete, state: 0x%x.\n", val);
603 return -EIO;
606 static int
607 qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter)
609 u32 val;
610 int retries = QLCNIC_RCVPEG_CHECK_RETRY_COUNT;
612 do {
613 val = QLCRD32(adapter, CRB_RCVPEG_STATE);
615 if (val == PHAN_PEG_RCV_INITIALIZED)
616 return 0;
618 msleep(QLCNIC_RCVPEG_CHECK_DELAY);
620 } while (--retries);
622 if (!retries) {
623 dev_err(&adapter->pdev->dev, "Receive Peg initialization not "
624 "complete, state: 0x%x.\n", val);
625 return -EIO;
628 return 0;
632 qlcnic_check_fw_status(struct qlcnic_adapter *adapter)
634 int err;
636 err = qlcnic_cmd_peg_ready(adapter);
637 if (err)
638 return err;
640 err = qlcnic_receive_peg_ready(adapter);
641 if (err)
642 return err;
644 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
646 return err;
650 qlcnic_setup_idc_param(struct qlcnic_adapter *adapter) {
652 int timeo;
653 u32 val;
655 val = QLCRD32(adapter, QLCNIC_CRB_DEV_PARTITION_INFO);
656 val = QLC_DEV_GET_DRV(val, adapter->portnum);
657 if ((val & 0x3) != QLCNIC_TYPE_NIC) {
658 dev_err(&adapter->pdev->dev,
659 "Not an Ethernet NIC func=%u\n", val);
660 return -EIO;
662 adapter->physical_port = (val >> 2);
663 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DEV_INIT_TIMEOUT, &timeo))
664 timeo = QLCNIC_INIT_TIMEOUT_SECS;
666 adapter->dev_init_timeo = timeo;
668 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DRV_RESET_TIMEOUT, &timeo))
669 timeo = QLCNIC_RESET_TIMEOUT_SECS;
671 adapter->reset_ack_timeo = timeo;
673 return 0;
676 static int qlcnic_get_flt_entry(struct qlcnic_adapter *adapter, u8 region,
677 struct qlcnic_flt_entry *region_entry)
679 struct qlcnic_flt_header flt_hdr;
680 struct qlcnic_flt_entry *flt_entry;
681 int i = 0, ret;
682 u32 entry_size;
684 memset(region_entry, 0, sizeof(struct qlcnic_flt_entry));
685 ret = qlcnic_rom_fast_read_words(adapter, QLCNIC_FLT_LOCATION,
686 (u8 *)&flt_hdr,
687 sizeof(struct qlcnic_flt_header));
688 if (ret) {
689 dev_warn(&adapter->pdev->dev,
690 "error reading flash layout header\n");
691 return -EIO;
694 entry_size = flt_hdr.len - sizeof(struct qlcnic_flt_header);
695 flt_entry = (struct qlcnic_flt_entry *)vzalloc(entry_size);
696 if (flt_entry == NULL) {
697 dev_warn(&adapter->pdev->dev, "error allocating memory\n");
698 return -EIO;
701 ret = qlcnic_rom_fast_read_words(adapter, QLCNIC_FLT_LOCATION +
702 sizeof(struct qlcnic_flt_header),
703 (u8 *)flt_entry, entry_size);
704 if (ret) {
705 dev_warn(&adapter->pdev->dev,
706 "error reading flash layout entries\n");
707 goto err_out;
710 while (i < (entry_size/sizeof(struct qlcnic_flt_entry))) {
711 if (flt_entry[i].region == region)
712 break;
713 i++;
715 if (i >= (entry_size/sizeof(struct qlcnic_flt_entry))) {
716 dev_warn(&adapter->pdev->dev,
717 "region=%x not found in %d regions\n", region, i);
718 ret = -EIO;
719 goto err_out;
721 memcpy(region_entry, &flt_entry[i], sizeof(struct qlcnic_flt_entry));
723 err_out:
724 vfree(flt_entry);
725 return ret;
729 qlcnic_check_flash_fw_ver(struct qlcnic_adapter *adapter)
731 struct qlcnic_flt_entry fw_entry;
732 u32 ver = -1, min_ver;
733 int ret;
735 if (adapter->ahw->revision_id == QLCNIC_P3P_C0)
736 ret = qlcnic_get_flt_entry(adapter, QLCNIC_C0_FW_IMAGE_REGION,
737 &fw_entry);
738 else
739 ret = qlcnic_get_flt_entry(adapter, QLCNIC_B0_FW_IMAGE_REGION,
740 &fw_entry);
742 if (!ret)
743 /* 0-4:-signature, 4-8:-fw version */
744 qlcnic_rom_fast_read(adapter, fw_entry.start_addr + 4,
745 (int *)&ver);
746 else
747 qlcnic_rom_fast_read(adapter, QLCNIC_FW_VERSION_OFFSET,
748 (int *)&ver);
750 ver = QLCNIC_DECODE_VERSION(ver);
751 min_ver = QLCNIC_MIN_FW_VERSION;
753 if (ver < min_ver) {
754 dev_err(&adapter->pdev->dev,
755 "firmware version %d.%d.%d unsupported."
756 "Min supported version %d.%d.%d\n",
757 _major(ver), _minor(ver), _build(ver),
758 _major(min_ver), _minor(min_ver), _build(min_ver));
759 return -EINVAL;
762 return 0;
765 static int
766 qlcnic_has_mn(struct qlcnic_adapter *adapter)
768 u32 capability;
769 capability = 0;
771 capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY);
772 if (capability & QLCNIC_PEG_TUNE_MN_PRESENT)
773 return 1;
775 return 0;
778 static
779 struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section)
781 u32 i;
782 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
783 __le32 entries = cpu_to_le32(directory->num_entries);
785 for (i = 0; i < entries; i++) {
787 __le32 offs = cpu_to_le32(directory->findex) +
788 (i * cpu_to_le32(directory->entry_size));
789 __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
791 if (tab_type == section)
792 return (struct uni_table_desc *) &unirom[offs];
795 return NULL;
798 #define FILEHEADER_SIZE (14 * 4)
800 static int
801 qlcnic_validate_header(struct qlcnic_adapter *adapter)
803 const u8 *unirom = adapter->fw->data;
804 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
805 __le32 fw_file_size = adapter->fw->size;
806 __le32 entries;
807 __le32 entry_size;
808 __le32 tab_size;
810 if (fw_file_size < FILEHEADER_SIZE)
811 return -EINVAL;
813 entries = cpu_to_le32(directory->num_entries);
814 entry_size = cpu_to_le32(directory->entry_size);
815 tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
817 if (fw_file_size < tab_size)
818 return -EINVAL;
820 return 0;
823 static int
824 qlcnic_validate_bootld(struct qlcnic_adapter *adapter)
826 struct uni_table_desc *tab_desc;
827 struct uni_data_desc *descr;
828 const u8 *unirom = adapter->fw->data;
829 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
830 QLCNIC_UNI_BOOTLD_IDX_OFF));
831 __le32 offs;
832 __le32 tab_size;
833 __le32 data_size;
835 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD);
837 if (!tab_desc)
838 return -EINVAL;
840 tab_size = cpu_to_le32(tab_desc->findex) +
841 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
843 if (adapter->fw->size < tab_size)
844 return -EINVAL;
846 offs = cpu_to_le32(tab_desc->findex) +
847 (cpu_to_le32(tab_desc->entry_size) * (idx));
848 descr = (struct uni_data_desc *)&unirom[offs];
850 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
852 if (adapter->fw->size < data_size)
853 return -EINVAL;
855 return 0;
858 static int
859 qlcnic_validate_fw(struct qlcnic_adapter *adapter)
861 struct uni_table_desc *tab_desc;
862 struct uni_data_desc *descr;
863 const u8 *unirom = adapter->fw->data;
864 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
865 QLCNIC_UNI_FIRMWARE_IDX_OFF));
866 __le32 offs;
867 __le32 tab_size;
868 __le32 data_size;
870 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW);
872 if (!tab_desc)
873 return -EINVAL;
875 tab_size = cpu_to_le32(tab_desc->findex) +
876 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
878 if (adapter->fw->size < tab_size)
879 return -EINVAL;
881 offs = cpu_to_le32(tab_desc->findex) +
882 (cpu_to_le32(tab_desc->entry_size) * (idx));
883 descr = (struct uni_data_desc *)&unirom[offs];
884 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
886 if (adapter->fw->size < data_size)
887 return -EINVAL;
889 return 0;
892 static int
893 qlcnic_validate_product_offs(struct qlcnic_adapter *adapter)
895 struct uni_table_desc *ptab_descr;
896 const u8 *unirom = adapter->fw->data;
897 int mn_present = qlcnic_has_mn(adapter);
898 __le32 entries;
899 __le32 entry_size;
900 __le32 tab_size;
901 u32 i;
903 ptab_descr = qlcnic_get_table_desc(unirom,
904 QLCNIC_UNI_DIR_SECT_PRODUCT_TBL);
905 if (!ptab_descr)
906 return -EINVAL;
908 entries = cpu_to_le32(ptab_descr->num_entries);
909 entry_size = cpu_to_le32(ptab_descr->entry_size);
910 tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
912 if (adapter->fw->size < tab_size)
913 return -EINVAL;
915 nomn:
916 for (i = 0; i < entries; i++) {
918 __le32 flags, file_chiprev, offs;
919 u8 chiprev = adapter->ahw->revision_id;
920 u32 flagbit;
922 offs = cpu_to_le32(ptab_descr->findex) +
923 (i * cpu_to_le32(ptab_descr->entry_size));
924 flags = cpu_to_le32(*((int *)&unirom[offs] +
925 QLCNIC_UNI_FLAGS_OFF));
926 file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
927 QLCNIC_UNI_CHIP_REV_OFF));
929 flagbit = mn_present ? 1 : 2;
931 if ((chiprev == file_chiprev) &&
932 ((1ULL << flagbit) & flags)) {
933 adapter->file_prd_off = offs;
934 return 0;
937 if (mn_present) {
938 mn_present = 0;
939 goto nomn;
941 return -EINVAL;
944 static int
945 qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter)
947 if (qlcnic_validate_header(adapter)) {
948 dev_err(&adapter->pdev->dev,
949 "unified image: header validation failed\n");
950 return -EINVAL;
953 if (qlcnic_validate_product_offs(adapter)) {
954 dev_err(&adapter->pdev->dev,
955 "unified image: product validation failed\n");
956 return -EINVAL;
959 if (qlcnic_validate_bootld(adapter)) {
960 dev_err(&adapter->pdev->dev,
961 "unified image: bootld validation failed\n");
962 return -EINVAL;
965 if (qlcnic_validate_fw(adapter)) {
966 dev_err(&adapter->pdev->dev,
967 "unified image: firmware validation failed\n");
968 return -EINVAL;
971 return 0;
974 static
975 struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter,
976 u32 section, u32 idx_offset)
978 const u8 *unirom = adapter->fw->data;
979 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
980 idx_offset));
981 struct uni_table_desc *tab_desc;
982 __le32 offs;
984 tab_desc = qlcnic_get_table_desc(unirom, section);
986 if (tab_desc == NULL)
987 return NULL;
989 offs = cpu_to_le32(tab_desc->findex) +
990 (cpu_to_le32(tab_desc->entry_size) * idx);
992 return (struct uni_data_desc *)&unirom[offs];
995 static u8 *
996 qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter)
998 u32 offs = QLCNIC_BOOTLD_START;
1000 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
1001 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
1002 QLCNIC_UNI_DIR_SECT_BOOTLD,
1003 QLCNIC_UNI_BOOTLD_IDX_OFF))->findex);
1005 return (u8 *)&adapter->fw->data[offs];
1008 static u8 *
1009 qlcnic_get_fw_offs(struct qlcnic_adapter *adapter)
1011 u32 offs = QLCNIC_IMAGE_START;
1013 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
1014 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
1015 QLCNIC_UNI_DIR_SECT_FW,
1016 QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex);
1018 return (u8 *)&adapter->fw->data[offs];
1021 static __le32
1022 qlcnic_get_fw_size(struct qlcnic_adapter *adapter)
1024 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
1025 return cpu_to_le32((qlcnic_get_data_desc(adapter,
1026 QLCNIC_UNI_DIR_SECT_FW,
1027 QLCNIC_UNI_FIRMWARE_IDX_OFF))->size);
1028 else
1029 return cpu_to_le32(
1030 *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]);
1033 static __le32
1034 qlcnic_get_fw_version(struct qlcnic_adapter *adapter)
1036 struct uni_data_desc *fw_data_desc;
1037 const struct firmware *fw = adapter->fw;
1038 __le32 major, minor, sub;
1039 const u8 *ver_str;
1040 int i, ret;
1042 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
1043 return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]);
1045 fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW,
1046 QLCNIC_UNI_FIRMWARE_IDX_OFF);
1047 ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
1048 cpu_to_le32(fw_data_desc->size) - 17;
1050 for (i = 0; i < 12; i++) {
1051 if (!strncmp(&ver_str[i], "REV=", 4)) {
1052 ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
1053 &major, &minor, &sub);
1054 if (ret != 3)
1055 return 0;
1056 else
1057 return major + (minor << 8) + (sub << 16);
1061 return 0;
1064 static __le32
1065 qlcnic_get_bios_version(struct qlcnic_adapter *adapter)
1067 const struct firmware *fw = adapter->fw;
1068 __le32 bios_ver, prd_off = adapter->file_prd_off;
1070 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
1071 return cpu_to_le32(
1072 *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]);
1074 bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
1075 + QLCNIC_UNI_BIOS_VERSION_OFF));
1077 return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24);
1080 static void qlcnic_rom_lock_recovery(struct qlcnic_adapter *adapter)
1082 if (qlcnic_pcie_sem_lock(adapter, 2, QLCNIC_ROM_LOCK_ID))
1083 dev_info(&adapter->pdev->dev, "Resetting rom_lock\n");
1085 qlcnic_pcie_sem_unlock(adapter, 2);
1088 static int
1089 qlcnic_check_fw_hearbeat(struct qlcnic_adapter *adapter)
1091 u32 heartbeat, ret = -EIO;
1092 int retries = QLCNIC_HEARTBEAT_CHECK_RETRY_COUNT;
1094 adapter->heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1096 do {
1097 msleep(QLCNIC_HEARTBEAT_PERIOD_MSECS);
1098 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1099 if (heartbeat != adapter->heartbeat) {
1100 ret = QLCNIC_RCODE_SUCCESS;
1101 break;
1103 } while (--retries);
1105 return ret;
1109 qlcnic_need_fw_reset(struct qlcnic_adapter *adapter)
1111 if ((adapter->flags & QLCNIC_FW_HANG) ||
1112 qlcnic_check_fw_hearbeat(adapter)) {
1113 qlcnic_rom_lock_recovery(adapter);
1114 return 1;
1117 if (adapter->need_fw_reset)
1118 return 1;
1120 if (adapter->fw)
1121 return 1;
1123 return 0;
1126 static const char *fw_name[] = {
1127 QLCNIC_UNIFIED_ROMIMAGE_NAME,
1128 QLCNIC_FLASH_ROMIMAGE_NAME,
1132 qlcnic_load_firmware(struct qlcnic_adapter *adapter)
1134 u64 *ptr64;
1135 u32 i, flashaddr, size;
1136 const struct firmware *fw = adapter->fw;
1137 struct pci_dev *pdev = adapter->pdev;
1139 dev_info(&pdev->dev, "loading firmware from %s\n",
1140 fw_name[adapter->fw_type]);
1142 if (fw) {
1143 __le64 data;
1145 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1147 ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter);
1148 flashaddr = QLCNIC_BOOTLD_START;
1150 for (i = 0; i < size; i++) {
1151 data = cpu_to_le64(ptr64[i]);
1153 if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data))
1154 return -EIO;
1156 flashaddr += 8;
1159 size = (__force u32)qlcnic_get_fw_size(adapter) / 8;
1161 ptr64 = (u64 *)qlcnic_get_fw_offs(adapter);
1162 flashaddr = QLCNIC_IMAGE_START;
1164 for (i = 0; i < size; i++) {
1165 data = cpu_to_le64(ptr64[i]);
1167 if (qlcnic_pci_mem_write_2M(adapter,
1168 flashaddr, data))
1169 return -EIO;
1171 flashaddr += 8;
1174 size = (__force u32)qlcnic_get_fw_size(adapter) % 8;
1175 if (size) {
1176 data = cpu_to_le64(ptr64[i]);
1178 if (qlcnic_pci_mem_write_2M(adapter,
1179 flashaddr, data))
1180 return -EIO;
1183 } else {
1184 u64 data;
1185 u32 hi, lo;
1186 int ret;
1187 struct qlcnic_flt_entry bootld_entry;
1189 ret = qlcnic_get_flt_entry(adapter, QLCNIC_BOOTLD_REGION,
1190 &bootld_entry);
1191 if (!ret) {
1192 size = bootld_entry.size / 8;
1193 flashaddr = bootld_entry.start_addr;
1194 } else {
1195 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1196 flashaddr = QLCNIC_BOOTLD_START;
1197 dev_info(&pdev->dev,
1198 "using legacy method to get flash fw region");
1201 for (i = 0; i < size; i++) {
1202 if (qlcnic_rom_fast_read(adapter,
1203 flashaddr, (int *)&lo) != 0)
1204 return -EIO;
1205 if (qlcnic_rom_fast_read(adapter,
1206 flashaddr + 4, (int *)&hi) != 0)
1207 return -EIO;
1209 data = (((u64)hi << 32) | lo);
1211 if (qlcnic_pci_mem_write_2M(adapter,
1212 flashaddr, data))
1213 return -EIO;
1215 flashaddr += 8;
1218 msleep(1);
1220 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020);
1221 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e);
1222 return 0;
1225 static int
1226 qlcnic_validate_firmware(struct qlcnic_adapter *adapter)
1228 __le32 val;
1229 u32 ver, bios, min_size;
1230 struct pci_dev *pdev = adapter->pdev;
1231 const struct firmware *fw = adapter->fw;
1232 u8 fw_type = adapter->fw_type;
1234 if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) {
1235 if (qlcnic_validate_unified_romimage(adapter))
1236 return -EINVAL;
1238 min_size = QLCNIC_UNI_FW_MIN_SIZE;
1239 } else {
1240 val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]);
1241 if ((__force u32)val != QLCNIC_BDINFO_MAGIC)
1242 return -EINVAL;
1244 min_size = QLCNIC_FW_MIN_SIZE;
1247 if (fw->size < min_size)
1248 return -EINVAL;
1250 val = qlcnic_get_fw_version(adapter);
1251 ver = QLCNIC_DECODE_VERSION(val);
1253 if (ver < QLCNIC_MIN_FW_VERSION) {
1254 dev_err(&pdev->dev,
1255 "%s: firmware version %d.%d.%d unsupported\n",
1256 fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
1257 return -EINVAL;
1260 val = qlcnic_get_bios_version(adapter);
1261 qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios);
1262 if ((__force u32)val != bios) {
1263 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
1264 fw_name[fw_type]);
1265 return -EINVAL;
1268 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
1269 return 0;
1272 static void
1273 qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter)
1275 u8 fw_type;
1277 switch (adapter->fw_type) {
1278 case QLCNIC_UNKNOWN_ROMIMAGE:
1279 fw_type = QLCNIC_UNIFIED_ROMIMAGE;
1280 break;
1282 case QLCNIC_UNIFIED_ROMIMAGE:
1283 default:
1284 fw_type = QLCNIC_FLASH_ROMIMAGE;
1285 break;
1288 adapter->fw_type = fw_type;
1293 void qlcnic_request_firmware(struct qlcnic_adapter *adapter)
1295 struct pci_dev *pdev = adapter->pdev;
1296 int rc;
1298 adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE;
1300 next:
1301 qlcnic_get_next_fwtype(adapter);
1303 if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) {
1304 adapter->fw = NULL;
1305 } else {
1306 rc = request_firmware(&adapter->fw,
1307 fw_name[adapter->fw_type], &pdev->dev);
1308 if (rc != 0)
1309 goto next;
1311 rc = qlcnic_validate_firmware(adapter);
1312 if (rc != 0) {
1313 release_firmware(adapter->fw);
1314 msleep(1);
1315 goto next;
1321 void
1322 qlcnic_release_firmware(struct qlcnic_adapter *adapter)
1324 if (adapter->fw)
1325 release_firmware(adapter->fw);
1326 adapter->fw = NULL;
1329 static void
1330 qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1331 struct qlcnic_fw_msg *msg)
1333 u32 cable_OUI;
1334 u16 cable_len;
1335 u16 link_speed;
1336 u8 link_status, module, duplex, autoneg;
1337 u8 lb_status = 0;
1338 struct net_device *netdev = adapter->netdev;
1340 adapter->has_link_events = 1;
1342 cable_OUI = msg->body[1] & 0xffffffff;
1343 cable_len = (msg->body[1] >> 32) & 0xffff;
1344 link_speed = (msg->body[1] >> 48) & 0xffff;
1346 link_status = msg->body[2] & 0xff;
1347 duplex = (msg->body[2] >> 16) & 0xff;
1348 autoneg = (msg->body[2] >> 24) & 0xff;
1349 lb_status = (msg->body[2] >> 32) & 0x3;
1351 module = (msg->body[2] >> 8) & 0xff;
1352 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
1353 dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, "
1354 "length %d\n", cable_OUI, cable_len);
1355 else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
1356 dev_info(&netdev->dev, "unsupported cable length %d\n",
1357 cable_len);
1359 if (!link_status && (lb_status == QLCNIC_ILB_MODE ||
1360 lb_status == QLCNIC_ELB_MODE))
1361 adapter->ahw->loopback_state |= QLCNIC_LINKEVENT;
1363 qlcnic_advert_link_change(adapter, link_status);
1365 if (duplex == LINKEVENT_FULL_DUPLEX)
1366 adapter->link_duplex = DUPLEX_FULL;
1367 else
1368 adapter->link_duplex = DUPLEX_HALF;
1370 adapter->module_type = module;
1371 adapter->link_autoneg = autoneg;
1372 adapter->link_speed = link_speed;
1375 static void
1376 qlcnic_handle_fw_message(int desc_cnt, int index,
1377 struct qlcnic_host_sds_ring *sds_ring)
1379 struct qlcnic_fw_msg msg;
1380 struct status_desc *desc;
1381 struct qlcnic_adapter *adapter;
1382 struct device *dev;
1383 int i = 0, opcode, ret;
1385 while (desc_cnt > 0 && i < 8) {
1386 desc = &sds_ring->desc_head[index];
1387 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1388 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1390 index = get_next_index(index, sds_ring->num_desc);
1391 desc_cnt--;
1394 adapter = sds_ring->adapter;
1395 dev = &adapter->pdev->dev;
1396 opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
1398 switch (opcode) {
1399 case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1400 qlcnic_handle_linkevent(adapter, &msg);
1401 break;
1402 case QLCNIC_C2H_OPCODE_CONFIG_LOOPBACK:
1403 ret = (u32)(msg.body[1]);
1404 switch (ret) {
1405 case 0:
1406 adapter->ahw->loopback_state |= QLCNIC_LB_RESPONSE;
1407 break;
1408 case 1:
1409 dev_info(dev, "loopback already in progress\n");
1410 adapter->diag_cnt = -QLCNIC_TEST_IN_PROGRESS;
1411 break;
1412 case 2:
1413 dev_info(dev, "loopback cable is not connected\n");
1414 adapter->diag_cnt = -QLCNIC_LB_CABLE_NOT_CONN;
1415 break;
1416 default:
1417 dev_info(dev, "loopback configure request failed,"
1418 " ret %x\n", ret);
1419 adapter->diag_cnt = -QLCNIC_UNDEFINED_ERROR;
1420 break;
1422 break;
1423 default:
1424 break;
1428 static int
1429 qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
1430 struct qlcnic_host_rds_ring *rds_ring,
1431 struct qlcnic_rx_buffer *buffer)
1433 struct sk_buff *skb;
1434 dma_addr_t dma;
1435 struct pci_dev *pdev = adapter->pdev;
1437 skb = dev_alloc_skb(rds_ring->skb_size);
1438 if (!skb) {
1439 adapter->stats.skb_alloc_failure++;
1440 return -ENOMEM;
1443 skb_reserve(skb, NET_IP_ALIGN);
1445 dma = pci_map_single(pdev, skb->data,
1446 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1448 if (pci_dma_mapping_error(pdev, dma)) {
1449 adapter->stats.rx_dma_map_error++;
1450 dev_kfree_skb_any(skb);
1451 return -ENOMEM;
1454 buffer->skb = skb;
1455 buffer->dma = dma;
1457 return 0;
1460 static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1461 struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum)
1463 struct qlcnic_rx_buffer *buffer;
1464 struct sk_buff *skb;
1466 buffer = &rds_ring->rx_buf_arr[index];
1468 if (unlikely(buffer->skb == NULL)) {
1469 WARN_ON(1);
1470 return NULL;
1473 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1474 PCI_DMA_FROMDEVICE);
1476 skb = buffer->skb;
1478 if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
1479 (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
1480 adapter->stats.csummed++;
1481 skb->ip_summed = CHECKSUM_UNNECESSARY;
1482 } else {
1483 skb_checksum_none_assert(skb);
1486 skb->dev = adapter->netdev;
1488 buffer->skb = NULL;
1490 return skb;
1493 static inline int
1494 qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter, struct sk_buff *skb,
1495 u16 *vlan_tag)
1497 struct ethhdr *eth_hdr;
1499 if (!__vlan_get_tag(skb, vlan_tag)) {
1500 eth_hdr = (struct ethhdr *) skb->data;
1501 memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
1502 skb_pull(skb, VLAN_HLEN);
1504 if (!adapter->pvid)
1505 return 0;
1507 if (*vlan_tag == adapter->pvid) {
1508 /* Outer vlan tag. Packet should follow non-vlan path */
1509 *vlan_tag = 0xffff;
1510 return 0;
1512 if (adapter->flags & QLCNIC_TAGGING_ENABLED)
1513 return 0;
1515 return -EINVAL;
1518 static struct qlcnic_rx_buffer *
1519 qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1520 struct qlcnic_host_sds_ring *sds_ring,
1521 int ring, u64 sts_data0)
1523 struct net_device *netdev = adapter->netdev;
1524 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1525 struct qlcnic_rx_buffer *buffer;
1526 struct sk_buff *skb;
1527 struct qlcnic_host_rds_ring *rds_ring;
1528 int index, length, cksum, pkt_offset;
1529 u16 vid = 0xffff;
1531 if (unlikely(ring >= adapter->max_rds_rings))
1532 return NULL;
1534 rds_ring = &recv_ctx->rds_rings[ring];
1536 index = qlcnic_get_sts_refhandle(sts_data0);
1537 if (unlikely(index >= rds_ring->num_desc))
1538 return NULL;
1540 buffer = &rds_ring->rx_buf_arr[index];
1542 length = qlcnic_get_sts_totallength(sts_data0);
1543 cksum = qlcnic_get_sts_status(sts_data0);
1544 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1546 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1547 if (!skb)
1548 return buffer;
1550 if (length > rds_ring->skb_size)
1551 skb_put(skb, rds_ring->skb_size);
1552 else
1553 skb_put(skb, length);
1555 if (pkt_offset)
1556 skb_pull(skb, pkt_offset);
1558 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1559 adapter->stats.rxdropped++;
1560 dev_kfree_skb(skb);
1561 return buffer;
1564 skb->protocol = eth_type_trans(skb, netdev);
1566 if (vid != 0xffff)
1567 __vlan_hwaccel_put_tag(skb, vid);
1569 napi_gro_receive(&sds_ring->napi, skb);
1571 adapter->stats.rx_pkts++;
1572 adapter->stats.rxbytes += length;
1574 return buffer;
1577 #define QLC_TCP_HDR_SIZE 20
1578 #define QLC_TCP_TS_OPTION_SIZE 12
1579 #define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1581 static struct qlcnic_rx_buffer *
1582 qlcnic_process_lro(struct qlcnic_adapter *adapter,
1583 struct qlcnic_host_sds_ring *sds_ring,
1584 int ring, u64 sts_data0, u64 sts_data1)
1586 struct net_device *netdev = adapter->netdev;
1587 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1588 struct qlcnic_rx_buffer *buffer;
1589 struct sk_buff *skb;
1590 struct qlcnic_host_rds_ring *rds_ring;
1591 struct iphdr *iph;
1592 struct tcphdr *th;
1593 bool push, timestamp;
1594 int l2_hdr_offset, l4_hdr_offset;
1595 int index;
1596 u16 lro_length, length, data_offset;
1597 u32 seq_number;
1598 u16 vid = 0xffff;
1600 if (unlikely(ring > adapter->max_rds_rings))
1601 return NULL;
1603 rds_ring = &recv_ctx->rds_rings[ring];
1605 index = qlcnic_get_lro_sts_refhandle(sts_data0);
1606 if (unlikely(index > rds_ring->num_desc))
1607 return NULL;
1609 buffer = &rds_ring->rx_buf_arr[index];
1611 timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1612 lro_length = qlcnic_get_lro_sts_length(sts_data0);
1613 l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1614 l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1615 push = qlcnic_get_lro_sts_push_flag(sts_data0);
1616 seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1618 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1619 if (!skb)
1620 return buffer;
1622 if (timestamp)
1623 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1624 else
1625 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1627 skb_put(skb, lro_length + data_offset);
1629 skb_pull(skb, l2_hdr_offset);
1631 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1632 adapter->stats.rxdropped++;
1633 dev_kfree_skb(skb);
1634 return buffer;
1637 skb->protocol = eth_type_trans(skb, netdev);
1639 iph = (struct iphdr *)skb->data;
1640 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1642 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1643 iph->tot_len = htons(length);
1644 iph->check = 0;
1645 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1646 th->psh = push;
1647 th->seq = htonl(seq_number);
1649 length = skb->len;
1651 if (vid != 0xffff)
1652 __vlan_hwaccel_put_tag(skb, vid);
1653 netif_receive_skb(skb);
1655 adapter->stats.lro_pkts++;
1656 adapter->stats.lrobytes += length;
1658 return buffer;
1662 qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1664 struct qlcnic_adapter *adapter = sds_ring->adapter;
1665 struct list_head *cur;
1666 struct status_desc *desc;
1667 struct qlcnic_rx_buffer *rxbuf;
1668 u64 sts_data0, sts_data1;
1670 int count = 0;
1671 int opcode, ring, desc_cnt;
1672 u32 consumer = sds_ring->consumer;
1674 while (count < max) {
1675 desc = &sds_ring->desc_head[consumer];
1676 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1678 if (!(sts_data0 & STATUS_OWNER_HOST))
1679 break;
1681 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1682 opcode = qlcnic_get_sts_opcode(sts_data0);
1684 switch (opcode) {
1685 case QLCNIC_RXPKT_DESC:
1686 case QLCNIC_OLD_RXPKT_DESC:
1687 case QLCNIC_SYN_OFFLOAD:
1688 ring = qlcnic_get_sts_type(sts_data0);
1689 rxbuf = qlcnic_process_rcv(adapter, sds_ring,
1690 ring, sts_data0);
1691 break;
1692 case QLCNIC_LRO_DESC:
1693 ring = qlcnic_get_lro_sts_type(sts_data0);
1694 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1695 rxbuf = qlcnic_process_lro(adapter, sds_ring,
1696 ring, sts_data0, sts_data1);
1697 break;
1698 case QLCNIC_RESPONSE_DESC:
1699 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1700 default:
1701 goto skip;
1704 WARN_ON(desc_cnt > 1);
1706 if (likely(rxbuf))
1707 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1708 else
1709 adapter->stats.null_rxbuf++;
1711 skip:
1712 for (; desc_cnt > 0; desc_cnt--) {
1713 desc = &sds_ring->desc_head[consumer];
1714 desc->status_desc_data[0] =
1715 cpu_to_le64(STATUS_OWNER_PHANTOM);
1716 consumer = get_next_index(consumer, sds_ring->num_desc);
1718 count++;
1721 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1722 struct qlcnic_host_rds_ring *rds_ring =
1723 &adapter->recv_ctx->rds_rings[ring];
1725 if (!list_empty(&sds_ring->free_list[ring])) {
1726 list_for_each(cur, &sds_ring->free_list[ring]) {
1727 rxbuf = list_entry(cur,
1728 struct qlcnic_rx_buffer, list);
1729 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1731 spin_lock(&rds_ring->lock);
1732 list_splice_tail_init(&sds_ring->free_list[ring],
1733 &rds_ring->free_list);
1734 spin_unlock(&rds_ring->lock);
1737 qlcnic_post_rx_buffers_nodb(adapter, rds_ring);
1740 if (count) {
1741 sds_ring->consumer = consumer;
1742 writel(consumer, sds_ring->crb_sts_consumer);
1745 return count;
1748 void
1749 qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter,
1750 struct qlcnic_host_rds_ring *rds_ring)
1752 struct rcv_desc *pdesc;
1753 struct qlcnic_rx_buffer *buffer;
1754 int count = 0;
1755 u32 producer;
1756 struct list_head *head;
1758 producer = rds_ring->producer;
1760 head = &rds_ring->free_list;
1761 while (!list_empty(head)) {
1763 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1765 if (!buffer->skb) {
1766 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1767 break;
1770 count++;
1771 list_del(&buffer->list);
1773 /* make a rcv descriptor */
1774 pdesc = &rds_ring->desc_head[producer];
1775 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1776 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1777 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1779 producer = get_next_index(producer, rds_ring->num_desc);
1782 if (count) {
1783 rds_ring->producer = producer;
1784 writel((producer-1) & (rds_ring->num_desc-1),
1785 rds_ring->crb_rcv_producer);
1789 static void
1790 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
1791 struct qlcnic_host_rds_ring *rds_ring)
1793 struct rcv_desc *pdesc;
1794 struct qlcnic_rx_buffer *buffer;
1795 int count = 0;
1796 uint32_t producer;
1797 struct list_head *head;
1799 if (!spin_trylock(&rds_ring->lock))
1800 return;
1802 producer = rds_ring->producer;
1804 head = &rds_ring->free_list;
1805 while (!list_empty(head)) {
1807 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1809 if (!buffer->skb) {
1810 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1811 break;
1814 count++;
1815 list_del(&buffer->list);
1817 /* make a rcv descriptor */
1818 pdesc = &rds_ring->desc_head[producer];
1819 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1820 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1821 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1823 producer = get_next_index(producer, rds_ring->num_desc);
1826 if (count) {
1827 rds_ring->producer = producer;
1828 writel((producer - 1) & (rds_ring->num_desc - 1),
1829 rds_ring->crb_rcv_producer);
1831 spin_unlock(&rds_ring->lock);
1834 static void dump_skb(struct sk_buff *skb, struct qlcnic_adapter *adapter)
1836 int i;
1837 unsigned char *data = skb->data;
1839 printk(KERN_INFO "\n");
1840 for (i = 0; i < skb->len; i++) {
1841 QLCDB(adapter, DRV, "%02x ", data[i]);
1842 if ((i & 0x0f) == 8)
1843 printk(KERN_INFO "\n");
1847 void qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter,
1848 struct qlcnic_host_sds_ring *sds_ring,
1849 int ring, u64 sts_data0)
1851 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1852 struct sk_buff *skb;
1853 struct qlcnic_host_rds_ring *rds_ring;
1854 int index, length, cksum, pkt_offset;
1856 if (unlikely(ring >= adapter->max_rds_rings))
1857 return;
1859 rds_ring = &recv_ctx->rds_rings[ring];
1861 index = qlcnic_get_sts_refhandle(sts_data0);
1862 length = qlcnic_get_sts_totallength(sts_data0);
1863 if (unlikely(index >= rds_ring->num_desc))
1864 return;
1866 cksum = qlcnic_get_sts_status(sts_data0);
1867 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1869 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1870 if (!skb)
1871 return;
1873 if (length > rds_ring->skb_size)
1874 skb_put(skb, rds_ring->skb_size);
1875 else
1876 skb_put(skb, length);
1878 if (pkt_offset)
1879 skb_pull(skb, pkt_offset);
1881 if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
1882 adapter->diag_cnt++;
1883 else
1884 dump_skb(skb, adapter);
1886 dev_kfree_skb_any(skb);
1887 adapter->stats.rx_pkts++;
1888 adapter->stats.rxbytes += length;
1890 return;
1893 void
1894 qlcnic_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1896 struct qlcnic_adapter *adapter = sds_ring->adapter;
1897 struct status_desc *desc;
1898 u64 sts_data0;
1899 int ring, opcode, desc_cnt;
1901 u32 consumer = sds_ring->consumer;
1903 desc = &sds_ring->desc_head[consumer];
1904 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1906 if (!(sts_data0 & STATUS_OWNER_HOST))
1907 return;
1909 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1910 opcode = qlcnic_get_sts_opcode(sts_data0);
1911 switch (opcode) {
1912 case QLCNIC_RESPONSE_DESC:
1913 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1914 break;
1915 default:
1916 ring = qlcnic_get_sts_type(sts_data0);
1917 qlcnic_process_rcv_diag(adapter, sds_ring, ring, sts_data0);
1918 break;
1921 for (; desc_cnt > 0; desc_cnt--) {
1922 desc = &sds_ring->desc_head[consumer];
1923 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1924 consumer = get_next_index(consumer, sds_ring->num_desc);
1927 sds_ring->consumer = consumer;
1928 writel(consumer, sds_ring->crb_sts_consumer);
1931 void
1932 qlcnic_fetch_mac(struct qlcnic_adapter *adapter, u32 off1, u32 off2,
1933 u8 alt_mac, u8 *mac)
1935 u32 mac_low, mac_high;
1936 int i;
1938 mac_low = off1;
1939 mac_high = off2;
1941 if (alt_mac) {
1942 mac_low |= (mac_low >> 16) | (mac_high << 16);
1943 mac_high >>= 16;
1946 for (i = 0; i < 2; i++)
1947 mac[i] = (u8)(mac_high >> ((1 - i) * 8));
1948 for (i = 2; i < 6; i++)
1949 mac[i] = (u8)(mac_low >> ((5 - i) * 8));