Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / net / ethernet / qlogic / qlcnic / qlcnic_init.c
blob312c1c37889d719585d9e1eb9c2ab211f65a81d7
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 qlcnic_rom_lock(adapter);
426 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xfeffffff);
427 qlcnic_rom_unlock(adapter);
429 /* Init HW CRB block */
430 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;
435 offset = n & 0xffffU;
436 n = (n >> 16) & 0xffffU;
438 if (n >= 1024) {
439 dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n");
440 return -EIO;
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;
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;
456 buf[i].addr = addr;
457 buf[i].data = val;
460 for (i = 0; i < n; i++) {
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;
468 off += QLCNIC_PCI_CRBSPACE;
470 if (off & 1)
471 continue;
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;
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;
504 QLCWR32(adapter, off, buf[i].data);
506 msleep(init_delay);
508 kfree(buf);
510 /* Initialize protocol process engine */
511 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e);
512 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8);
513 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8);
514 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);
522 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);
527 return 0;
530 static int qlcnic_cmd_peg_ready(struct qlcnic_adapter *adapter)
532 u32 val;
533 int retries = QLCNIC_CMDPEG_CHECK_RETRY_COUNT;
535 do {
536 val = QLCRD32(adapter, CRB_CMDPEG_STATE);
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;
548 msleep(QLCNIC_CMDPEG_CHECK_DELAY);
550 } while (--retries);
552 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
554 out_err:
555 dev_err(&adapter->pdev->dev, "Command Peg initialization not "
556 "complete, state: 0x%x.\n", val);
557 return -EIO;
560 static int
561 qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter)
563 u32 val;
564 int retries = QLCNIC_RCVPEG_CHECK_RETRY_COUNT;
566 do {
567 val = QLCRD32(adapter, CRB_RCVPEG_STATE);
569 if (val == PHAN_PEG_RCV_INITIALIZED)
570 return 0;
572 msleep(QLCNIC_RCVPEG_CHECK_DELAY);
574 } while (--retries);
576 if (!retries) {
577 dev_err(&adapter->pdev->dev, "Receive Peg initialization not "
578 "complete, state: 0x%x.\n", val);
579 return -EIO;
582 return 0;
586 qlcnic_check_fw_status(struct qlcnic_adapter *adapter)
588 int err;
590 err = qlcnic_cmd_peg_ready(adapter);
591 if (err)
592 return err;
594 err = qlcnic_receive_peg_ready(adapter);
595 if (err)
596 return err;
598 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
600 return err;
604 qlcnic_setup_idc_param(struct qlcnic_adapter *adapter) {
606 int timeo;
607 u32 val;
609 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;
616 adapter->physical_port = (val >> 2);
617 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DEV_INIT_TIMEOUT, &timeo))
618 timeo = QLCNIC_INIT_TIMEOUT_SECS;
620 adapter->dev_init_timeo = timeo;
622 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DRV_RESET_TIMEOUT, &timeo))
623 timeo = QLCNIC_RESET_TIMEOUT_SECS;
625 adapter->reset_ack_timeo = timeo;
627 return 0;
630 static int qlcnic_get_flt_entry(struct qlcnic_adapter *adapter, u8 region,
631 struct qlcnic_flt_entry *region_entry)
633 struct qlcnic_flt_header flt_hdr;
634 struct qlcnic_flt_entry *flt_entry;
635 int i = 0, ret;
636 u32 entry_size;
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;
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;
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;
664 while (i < (entry_size/sizeof(struct qlcnic_flt_entry))) {
665 if (flt_entry[i].region == region)
666 break;
667 i++;
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;
675 memcpy(region_entry, &flt_entry[i], sizeof(struct qlcnic_flt_entry));
677 err_out:
678 vfree(flt_entry);
679 return ret;
683 qlcnic_check_flash_fw_ver(struct qlcnic_adapter *adapter)
685 struct qlcnic_flt_entry fw_entry;
686 u32 ver = -1, min_ver;
687 int ret;
689 if (adapter->ahw->revision_id == QLCNIC_P3P_C0)
690 ret = qlcnic_get_flt_entry(adapter, QLCNIC_C0_FW_IMAGE_REGION,
691 &fw_entry);
692 else
693 ret = qlcnic_get_flt_entry(adapter, QLCNIC_B0_FW_IMAGE_REGION,
694 &fw_entry);
696 if (!ret)
697 /* 0-4:-signature, 4-8:-fw version */
698 qlcnic_rom_fast_read(adapter, fw_entry.start_addr + 4,
699 (int *)&ver);
700 else
701 qlcnic_rom_fast_read(adapter, QLCNIC_FW_VERSION_OFFSET,
702 (int *)&ver);
704 ver = QLCNIC_DECODE_VERSION(ver);
705 min_ver = QLCNIC_MIN_FW_VERSION;
707 if (ver < min_ver) {
708 dev_err(&adapter->pdev->dev,
709 "firmware version %d.%d.%d unsupported."
710 "Min supported version %d.%d.%d\n",
711 _major(ver), _minor(ver), _build(ver),
712 _major(min_ver), _minor(min_ver), _build(min_ver));
713 return -EINVAL;
716 return 0;
719 static int
720 qlcnic_has_mn(struct qlcnic_adapter *adapter)
722 u32 capability;
723 capability = 0;
725 capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY);
726 if (capability & QLCNIC_PEG_TUNE_MN_PRESENT)
727 return 1;
729 return 0;
732 static
733 struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section)
735 u32 i;
736 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
737 __le32 entries = cpu_to_le32(directory->num_entries);
739 for (i = 0; i < entries; i++) {
741 __le32 offs = cpu_to_le32(directory->findex) +
742 (i * cpu_to_le32(directory->entry_size));
743 __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
745 if (tab_type == section)
746 return (struct uni_table_desc *) &unirom[offs];
749 return NULL;
752 #define FILEHEADER_SIZE (14 * 4)
754 static int
755 qlcnic_validate_header(struct qlcnic_adapter *adapter)
757 const u8 *unirom = adapter->fw->data;
758 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
759 __le32 fw_file_size = adapter->fw->size;
760 __le32 entries;
761 __le32 entry_size;
762 __le32 tab_size;
764 if (fw_file_size < FILEHEADER_SIZE)
765 return -EINVAL;
767 entries = cpu_to_le32(directory->num_entries);
768 entry_size = cpu_to_le32(directory->entry_size);
769 tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
771 if (fw_file_size < tab_size)
772 return -EINVAL;
774 return 0;
777 static int
778 qlcnic_validate_bootld(struct qlcnic_adapter *adapter)
780 struct uni_table_desc *tab_desc;
781 struct uni_data_desc *descr;
782 const u8 *unirom = adapter->fw->data;
783 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
784 QLCNIC_UNI_BOOTLD_IDX_OFF));
785 __le32 offs;
786 __le32 tab_size;
787 __le32 data_size;
789 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD);
791 if (!tab_desc)
792 return -EINVAL;
794 tab_size = cpu_to_le32(tab_desc->findex) +
795 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
797 if (adapter->fw->size < tab_size)
798 return -EINVAL;
800 offs = cpu_to_le32(tab_desc->findex) +
801 (cpu_to_le32(tab_desc->entry_size) * (idx));
802 descr = (struct uni_data_desc *)&unirom[offs];
804 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
806 if (adapter->fw->size < data_size)
807 return -EINVAL;
809 return 0;
812 static int
813 qlcnic_validate_fw(struct qlcnic_adapter *adapter)
815 struct uni_table_desc *tab_desc;
816 struct uni_data_desc *descr;
817 const u8 *unirom = adapter->fw->data;
818 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
819 QLCNIC_UNI_FIRMWARE_IDX_OFF));
820 __le32 offs;
821 __le32 tab_size;
822 __le32 data_size;
824 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW);
826 if (!tab_desc)
827 return -EINVAL;
829 tab_size = cpu_to_le32(tab_desc->findex) +
830 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
832 if (adapter->fw->size < tab_size)
833 return -EINVAL;
835 offs = cpu_to_le32(tab_desc->findex) +
836 (cpu_to_le32(tab_desc->entry_size) * (idx));
837 descr = (struct uni_data_desc *)&unirom[offs];
838 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
840 if (adapter->fw->size < data_size)
841 return -EINVAL;
843 return 0;
846 static int
847 qlcnic_validate_product_offs(struct qlcnic_adapter *adapter)
849 struct uni_table_desc *ptab_descr;
850 const u8 *unirom = adapter->fw->data;
851 int mn_present = qlcnic_has_mn(adapter);
852 __le32 entries;
853 __le32 entry_size;
854 __le32 tab_size;
855 u32 i;
857 ptab_descr = qlcnic_get_table_desc(unirom,
858 QLCNIC_UNI_DIR_SECT_PRODUCT_TBL);
859 if (!ptab_descr)
860 return -EINVAL;
862 entries = cpu_to_le32(ptab_descr->num_entries);
863 entry_size = cpu_to_le32(ptab_descr->entry_size);
864 tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
866 if (adapter->fw->size < tab_size)
867 return -EINVAL;
869 nomn:
870 for (i = 0; i < entries; i++) {
872 __le32 flags, file_chiprev, offs;
873 u8 chiprev = adapter->ahw->revision_id;
874 u32 flagbit;
876 offs = cpu_to_le32(ptab_descr->findex) +
877 (i * cpu_to_le32(ptab_descr->entry_size));
878 flags = cpu_to_le32(*((int *)&unirom[offs] +
879 QLCNIC_UNI_FLAGS_OFF));
880 file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
881 QLCNIC_UNI_CHIP_REV_OFF));
883 flagbit = mn_present ? 1 : 2;
885 if ((chiprev == file_chiprev) &&
886 ((1ULL << flagbit) & flags)) {
887 adapter->file_prd_off = offs;
888 return 0;
891 if (mn_present) {
892 mn_present = 0;
893 goto nomn;
895 return -EINVAL;
898 static int
899 qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter)
901 if (qlcnic_validate_header(adapter)) {
902 dev_err(&adapter->pdev->dev,
903 "unified image: header validation failed\n");
904 return -EINVAL;
907 if (qlcnic_validate_product_offs(adapter)) {
908 dev_err(&adapter->pdev->dev,
909 "unified image: product validation failed\n");
910 return -EINVAL;
913 if (qlcnic_validate_bootld(adapter)) {
914 dev_err(&adapter->pdev->dev,
915 "unified image: bootld validation failed\n");
916 return -EINVAL;
919 if (qlcnic_validate_fw(adapter)) {
920 dev_err(&adapter->pdev->dev,
921 "unified image: firmware validation failed\n");
922 return -EINVAL;
925 return 0;
928 static
929 struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter,
930 u32 section, u32 idx_offset)
932 const u8 *unirom = adapter->fw->data;
933 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
934 idx_offset));
935 struct uni_table_desc *tab_desc;
936 __le32 offs;
938 tab_desc = qlcnic_get_table_desc(unirom, section);
940 if (tab_desc == NULL)
941 return NULL;
943 offs = cpu_to_le32(tab_desc->findex) +
944 (cpu_to_le32(tab_desc->entry_size) * idx);
946 return (struct uni_data_desc *)&unirom[offs];
949 static u8 *
950 qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter)
952 u32 offs = QLCNIC_BOOTLD_START;
954 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
955 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
956 QLCNIC_UNI_DIR_SECT_BOOTLD,
957 QLCNIC_UNI_BOOTLD_IDX_OFF))->findex);
959 return (u8 *)&adapter->fw->data[offs];
962 static u8 *
963 qlcnic_get_fw_offs(struct qlcnic_adapter *adapter)
965 u32 offs = QLCNIC_IMAGE_START;
967 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
968 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
969 QLCNIC_UNI_DIR_SECT_FW,
970 QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex);
972 return (u8 *)&adapter->fw->data[offs];
975 static __le32
976 qlcnic_get_fw_size(struct qlcnic_adapter *adapter)
978 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
979 return cpu_to_le32((qlcnic_get_data_desc(adapter,
980 QLCNIC_UNI_DIR_SECT_FW,
981 QLCNIC_UNI_FIRMWARE_IDX_OFF))->size);
982 else
983 return cpu_to_le32(
984 *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]);
987 static __le32
988 qlcnic_get_fw_version(struct qlcnic_adapter *adapter)
990 struct uni_data_desc *fw_data_desc;
991 const struct firmware *fw = adapter->fw;
992 __le32 major, minor, sub;
993 const u8 *ver_str;
994 int i, ret;
996 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
997 return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]);
999 fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW,
1000 QLCNIC_UNI_FIRMWARE_IDX_OFF);
1001 ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
1002 cpu_to_le32(fw_data_desc->size) - 17;
1004 for (i = 0; i < 12; i++) {
1005 if (!strncmp(&ver_str[i], "REV=", 4)) {
1006 ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
1007 &major, &minor, &sub);
1008 if (ret != 3)
1009 return 0;
1010 else
1011 return major + (minor << 8) + (sub << 16);
1015 return 0;
1018 static __le32
1019 qlcnic_get_bios_version(struct qlcnic_adapter *adapter)
1021 const struct firmware *fw = adapter->fw;
1022 __le32 bios_ver, prd_off = adapter->file_prd_off;
1024 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
1025 return cpu_to_le32(
1026 *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]);
1028 bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
1029 + QLCNIC_UNI_BIOS_VERSION_OFF));
1031 return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24);
1034 static void qlcnic_rom_lock_recovery(struct qlcnic_adapter *adapter)
1036 if (qlcnic_pcie_sem_lock(adapter, 2, QLCNIC_ROM_LOCK_ID))
1037 dev_info(&adapter->pdev->dev, "Resetting rom_lock\n");
1039 qlcnic_pcie_sem_unlock(adapter, 2);
1042 static int
1043 qlcnic_check_fw_hearbeat(struct qlcnic_adapter *adapter)
1045 u32 heartbeat, ret = -EIO;
1046 int retries = QLCNIC_HEARTBEAT_CHECK_RETRY_COUNT;
1048 adapter->heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1050 do {
1051 msleep(QLCNIC_HEARTBEAT_PERIOD_MSECS);
1052 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1053 if (heartbeat != adapter->heartbeat) {
1054 ret = QLCNIC_RCODE_SUCCESS;
1055 break;
1057 } while (--retries);
1059 return ret;
1063 qlcnic_need_fw_reset(struct qlcnic_adapter *adapter)
1065 if ((adapter->flags & QLCNIC_FW_HANG) ||
1066 qlcnic_check_fw_hearbeat(adapter)) {
1067 qlcnic_rom_lock_recovery(adapter);
1068 return 1;
1071 if (adapter->need_fw_reset)
1072 return 1;
1074 if (adapter->fw)
1075 return 1;
1077 return 0;
1080 static const char *fw_name[] = {
1081 QLCNIC_UNIFIED_ROMIMAGE_NAME,
1082 QLCNIC_FLASH_ROMIMAGE_NAME,
1086 qlcnic_load_firmware(struct qlcnic_adapter *adapter)
1088 u64 *ptr64;
1089 u32 i, flashaddr, size;
1090 const struct firmware *fw = adapter->fw;
1091 struct pci_dev *pdev = adapter->pdev;
1093 dev_info(&pdev->dev, "loading firmware from %s\n",
1094 fw_name[adapter->fw_type]);
1096 if (fw) {
1097 __le64 data;
1099 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1101 ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter);
1102 flashaddr = QLCNIC_BOOTLD_START;
1104 for (i = 0; i < size; i++) {
1105 data = cpu_to_le64(ptr64[i]);
1107 if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data))
1108 return -EIO;
1110 flashaddr += 8;
1113 size = (__force u32)qlcnic_get_fw_size(adapter) / 8;
1115 ptr64 = (u64 *)qlcnic_get_fw_offs(adapter);
1116 flashaddr = QLCNIC_IMAGE_START;
1118 for (i = 0; i < size; i++) {
1119 data = cpu_to_le64(ptr64[i]);
1121 if (qlcnic_pci_mem_write_2M(adapter,
1122 flashaddr, data))
1123 return -EIO;
1125 flashaddr += 8;
1128 size = (__force u32)qlcnic_get_fw_size(adapter) % 8;
1129 if (size) {
1130 data = cpu_to_le64(ptr64[i]);
1132 if (qlcnic_pci_mem_write_2M(adapter,
1133 flashaddr, data))
1134 return -EIO;
1137 } else {
1138 u64 data;
1139 u32 hi, lo;
1140 int ret;
1141 struct qlcnic_flt_entry bootld_entry;
1143 ret = qlcnic_get_flt_entry(adapter, QLCNIC_BOOTLD_REGION,
1144 &bootld_entry);
1145 if (!ret) {
1146 size = bootld_entry.size / 8;
1147 flashaddr = bootld_entry.start_addr;
1148 } else {
1149 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1150 flashaddr = QLCNIC_BOOTLD_START;
1151 dev_info(&pdev->dev,
1152 "using legacy method to get flash fw region");
1155 for (i = 0; i < size; i++) {
1156 if (qlcnic_rom_fast_read(adapter,
1157 flashaddr, (int *)&lo) != 0)
1158 return -EIO;
1159 if (qlcnic_rom_fast_read(adapter,
1160 flashaddr + 4, (int *)&hi) != 0)
1161 return -EIO;
1163 data = (((u64)hi << 32) | lo);
1165 if (qlcnic_pci_mem_write_2M(adapter,
1166 flashaddr, data))
1167 return -EIO;
1169 flashaddr += 8;
1172 msleep(1);
1174 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020);
1175 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e);
1176 return 0;
1179 static int
1180 qlcnic_validate_firmware(struct qlcnic_adapter *adapter)
1182 __le32 val;
1183 u32 ver, bios, min_size;
1184 struct pci_dev *pdev = adapter->pdev;
1185 const struct firmware *fw = adapter->fw;
1186 u8 fw_type = adapter->fw_type;
1188 if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) {
1189 if (qlcnic_validate_unified_romimage(adapter))
1190 return -EINVAL;
1192 min_size = QLCNIC_UNI_FW_MIN_SIZE;
1193 } else {
1194 val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]);
1195 if ((__force u32)val != QLCNIC_BDINFO_MAGIC)
1196 return -EINVAL;
1198 min_size = QLCNIC_FW_MIN_SIZE;
1201 if (fw->size < min_size)
1202 return -EINVAL;
1204 val = qlcnic_get_fw_version(adapter);
1205 ver = QLCNIC_DECODE_VERSION(val);
1207 if (ver < QLCNIC_MIN_FW_VERSION) {
1208 dev_err(&pdev->dev,
1209 "%s: firmware version %d.%d.%d unsupported\n",
1210 fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
1211 return -EINVAL;
1214 val = qlcnic_get_bios_version(adapter);
1215 qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios);
1216 if ((__force u32)val != bios) {
1217 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
1218 fw_name[fw_type]);
1219 return -EINVAL;
1222 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
1223 return 0;
1226 static void
1227 qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter)
1229 u8 fw_type;
1231 switch (adapter->fw_type) {
1232 case QLCNIC_UNKNOWN_ROMIMAGE:
1233 fw_type = QLCNIC_UNIFIED_ROMIMAGE;
1234 break;
1236 case QLCNIC_UNIFIED_ROMIMAGE:
1237 default:
1238 fw_type = QLCNIC_FLASH_ROMIMAGE;
1239 break;
1242 adapter->fw_type = fw_type;
1247 void qlcnic_request_firmware(struct qlcnic_adapter *adapter)
1249 struct pci_dev *pdev = adapter->pdev;
1250 int rc;
1252 adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE;
1254 next:
1255 qlcnic_get_next_fwtype(adapter);
1257 if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) {
1258 adapter->fw = NULL;
1259 } else {
1260 rc = request_firmware(&adapter->fw,
1261 fw_name[adapter->fw_type], &pdev->dev);
1262 if (rc != 0)
1263 goto next;
1265 rc = qlcnic_validate_firmware(adapter);
1266 if (rc != 0) {
1267 release_firmware(adapter->fw);
1268 msleep(1);
1269 goto next;
1275 void
1276 qlcnic_release_firmware(struct qlcnic_adapter *adapter)
1278 if (adapter->fw)
1279 release_firmware(adapter->fw);
1280 adapter->fw = NULL;
1283 static void
1284 qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1285 struct qlcnic_fw_msg *msg)
1287 u32 cable_OUI;
1288 u16 cable_len;
1289 u16 link_speed;
1290 u8 link_status, module, duplex, autoneg;
1291 u8 lb_status = 0;
1292 struct net_device *netdev = adapter->netdev;
1294 adapter->has_link_events = 1;
1296 cable_OUI = msg->body[1] & 0xffffffff;
1297 cable_len = (msg->body[1] >> 32) & 0xffff;
1298 link_speed = (msg->body[1] >> 48) & 0xffff;
1300 link_status = msg->body[2] & 0xff;
1301 duplex = (msg->body[2] >> 16) & 0xff;
1302 autoneg = (msg->body[2] >> 24) & 0xff;
1303 lb_status = (msg->body[2] >> 32) & 0x3;
1305 module = (msg->body[2] >> 8) & 0xff;
1306 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
1307 dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, "
1308 "length %d\n", cable_OUI, cable_len);
1309 else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
1310 dev_info(&netdev->dev, "unsupported cable length %d\n",
1311 cable_len);
1313 if (!link_status && (lb_status == QLCNIC_ILB_MODE ||
1314 lb_status == QLCNIC_ELB_MODE))
1315 adapter->ahw->loopback_state |= QLCNIC_LINKEVENT;
1317 qlcnic_advert_link_change(adapter, link_status);
1319 if (duplex == LINKEVENT_FULL_DUPLEX)
1320 adapter->link_duplex = DUPLEX_FULL;
1321 else
1322 adapter->link_duplex = DUPLEX_HALF;
1324 adapter->module_type = module;
1325 adapter->link_autoneg = autoneg;
1326 adapter->link_speed = link_speed;
1329 static void
1330 qlcnic_handle_fw_message(int desc_cnt, int index,
1331 struct qlcnic_host_sds_ring *sds_ring)
1333 struct qlcnic_fw_msg msg;
1334 struct status_desc *desc;
1335 struct qlcnic_adapter *adapter;
1336 struct device *dev;
1337 int i = 0, opcode, ret;
1339 while (desc_cnt > 0 && i < 8) {
1340 desc = &sds_ring->desc_head[index];
1341 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1342 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1344 index = get_next_index(index, sds_ring->num_desc);
1345 desc_cnt--;
1348 adapter = sds_ring->adapter;
1349 dev = &adapter->pdev->dev;
1350 opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
1352 switch (opcode) {
1353 case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1354 qlcnic_handle_linkevent(adapter, &msg);
1355 break;
1356 case QLCNIC_C2H_OPCODE_CONFIG_LOOPBACK:
1357 ret = (u32)(msg.body[1]);
1358 switch (ret) {
1359 case 0:
1360 adapter->ahw->loopback_state |= QLCNIC_LB_RESPONSE;
1361 break;
1362 case 1:
1363 dev_info(dev, "loopback already in progress\n");
1364 adapter->diag_cnt = -QLCNIC_TEST_IN_PROGRESS;
1365 break;
1366 case 2:
1367 dev_info(dev, "loopback cable is not connected\n");
1368 adapter->diag_cnt = -QLCNIC_LB_CABLE_NOT_CONN;
1369 break;
1370 default:
1371 dev_info(dev, "loopback configure request failed,"
1372 " ret %x\n", ret);
1373 adapter->diag_cnt = -QLCNIC_UNDEFINED_ERROR;
1374 break;
1376 break;
1377 default:
1378 break;
1382 static int
1383 qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
1384 struct qlcnic_host_rds_ring *rds_ring,
1385 struct qlcnic_rx_buffer *buffer)
1387 struct sk_buff *skb;
1388 dma_addr_t dma;
1389 struct pci_dev *pdev = adapter->pdev;
1391 skb = dev_alloc_skb(rds_ring->skb_size);
1392 if (!skb) {
1393 adapter->stats.skb_alloc_failure++;
1394 return -ENOMEM;
1397 skb_reserve(skb, NET_IP_ALIGN);
1399 dma = pci_map_single(pdev, skb->data,
1400 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1402 if (pci_dma_mapping_error(pdev, dma)) {
1403 adapter->stats.rx_dma_map_error++;
1404 dev_kfree_skb_any(skb);
1405 return -ENOMEM;
1408 buffer->skb = skb;
1409 buffer->dma = dma;
1411 return 0;
1414 static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1415 struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum)
1417 struct qlcnic_rx_buffer *buffer;
1418 struct sk_buff *skb;
1420 buffer = &rds_ring->rx_buf_arr[index];
1422 if (unlikely(buffer->skb == NULL)) {
1423 WARN_ON(1);
1424 return NULL;
1427 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1428 PCI_DMA_FROMDEVICE);
1430 skb = buffer->skb;
1432 if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
1433 (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
1434 adapter->stats.csummed++;
1435 skb->ip_summed = CHECKSUM_UNNECESSARY;
1436 } else {
1437 skb_checksum_none_assert(skb);
1440 skb->dev = adapter->netdev;
1442 buffer->skb = NULL;
1444 return skb;
1447 static inline int
1448 qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter, struct sk_buff *skb,
1449 u16 *vlan_tag)
1451 struct ethhdr *eth_hdr;
1453 if (!__vlan_get_tag(skb, vlan_tag)) {
1454 eth_hdr = (struct ethhdr *) skb->data;
1455 memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
1456 skb_pull(skb, VLAN_HLEN);
1458 if (!adapter->pvid)
1459 return 0;
1461 if (*vlan_tag == adapter->pvid) {
1462 /* Outer vlan tag. Packet should follow non-vlan path */
1463 *vlan_tag = 0xffff;
1464 return 0;
1466 if (adapter->flags & QLCNIC_TAGGING_ENABLED)
1467 return 0;
1469 return -EINVAL;
1472 static struct qlcnic_rx_buffer *
1473 qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1474 struct qlcnic_host_sds_ring *sds_ring,
1475 int ring, u64 sts_data0)
1477 struct net_device *netdev = adapter->netdev;
1478 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1479 struct qlcnic_rx_buffer *buffer;
1480 struct sk_buff *skb;
1481 struct qlcnic_host_rds_ring *rds_ring;
1482 int index, length, cksum, pkt_offset;
1483 u16 vid = 0xffff;
1485 if (unlikely(ring >= adapter->max_rds_rings))
1486 return NULL;
1488 rds_ring = &recv_ctx->rds_rings[ring];
1490 index = qlcnic_get_sts_refhandle(sts_data0);
1491 if (unlikely(index >= rds_ring->num_desc))
1492 return NULL;
1494 buffer = &rds_ring->rx_buf_arr[index];
1496 length = qlcnic_get_sts_totallength(sts_data0);
1497 cksum = qlcnic_get_sts_status(sts_data0);
1498 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1500 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1501 if (!skb)
1502 return buffer;
1504 if (length > rds_ring->skb_size)
1505 skb_put(skb, rds_ring->skb_size);
1506 else
1507 skb_put(skb, length);
1509 if (pkt_offset)
1510 skb_pull(skb, pkt_offset);
1512 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1513 adapter->stats.rxdropped++;
1514 dev_kfree_skb(skb);
1515 return buffer;
1518 skb->protocol = eth_type_trans(skb, netdev);
1520 if (vid != 0xffff)
1521 __vlan_hwaccel_put_tag(skb, vid);
1523 napi_gro_receive(&sds_ring->napi, skb);
1525 adapter->stats.rx_pkts++;
1526 adapter->stats.rxbytes += length;
1528 return buffer;
1531 #define QLC_TCP_HDR_SIZE 20
1532 #define QLC_TCP_TS_OPTION_SIZE 12
1533 #define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1535 static struct qlcnic_rx_buffer *
1536 qlcnic_process_lro(struct qlcnic_adapter *adapter,
1537 struct qlcnic_host_sds_ring *sds_ring,
1538 int ring, u64 sts_data0, u64 sts_data1)
1540 struct net_device *netdev = adapter->netdev;
1541 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1542 struct qlcnic_rx_buffer *buffer;
1543 struct sk_buff *skb;
1544 struct qlcnic_host_rds_ring *rds_ring;
1545 struct iphdr *iph;
1546 struct tcphdr *th;
1547 bool push, timestamp;
1548 int l2_hdr_offset, l4_hdr_offset;
1549 int index;
1550 u16 lro_length, length, data_offset;
1551 u32 seq_number;
1552 u16 vid = 0xffff;
1554 if (unlikely(ring > adapter->max_rds_rings))
1555 return NULL;
1557 rds_ring = &recv_ctx->rds_rings[ring];
1559 index = qlcnic_get_lro_sts_refhandle(sts_data0);
1560 if (unlikely(index > rds_ring->num_desc))
1561 return NULL;
1563 buffer = &rds_ring->rx_buf_arr[index];
1565 timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1566 lro_length = qlcnic_get_lro_sts_length(sts_data0);
1567 l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1568 l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1569 push = qlcnic_get_lro_sts_push_flag(sts_data0);
1570 seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1572 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1573 if (!skb)
1574 return buffer;
1576 if (timestamp)
1577 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1578 else
1579 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1581 skb_put(skb, lro_length + data_offset);
1583 skb_pull(skb, l2_hdr_offset);
1585 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1586 adapter->stats.rxdropped++;
1587 dev_kfree_skb(skb);
1588 return buffer;
1591 skb->protocol = eth_type_trans(skb, netdev);
1593 iph = (struct iphdr *)skb->data;
1594 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1596 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1597 iph->tot_len = htons(length);
1598 iph->check = 0;
1599 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1600 th->psh = push;
1601 th->seq = htonl(seq_number);
1603 length = skb->len;
1605 if (vid != 0xffff)
1606 __vlan_hwaccel_put_tag(skb, vid);
1607 netif_receive_skb(skb);
1609 adapter->stats.lro_pkts++;
1610 adapter->stats.lrobytes += length;
1612 return buffer;
1616 qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1618 struct qlcnic_adapter *adapter = sds_ring->adapter;
1619 struct list_head *cur;
1620 struct status_desc *desc;
1621 struct qlcnic_rx_buffer *rxbuf;
1622 u64 sts_data0, sts_data1;
1624 int count = 0;
1625 int opcode, ring, desc_cnt;
1626 u32 consumer = sds_ring->consumer;
1628 while (count < max) {
1629 desc = &sds_ring->desc_head[consumer];
1630 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1632 if (!(sts_data0 & STATUS_OWNER_HOST))
1633 break;
1635 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1636 opcode = qlcnic_get_sts_opcode(sts_data0);
1638 switch (opcode) {
1639 case QLCNIC_RXPKT_DESC:
1640 case QLCNIC_OLD_RXPKT_DESC:
1641 case QLCNIC_SYN_OFFLOAD:
1642 ring = qlcnic_get_sts_type(sts_data0);
1643 rxbuf = qlcnic_process_rcv(adapter, sds_ring,
1644 ring, sts_data0);
1645 break;
1646 case QLCNIC_LRO_DESC:
1647 ring = qlcnic_get_lro_sts_type(sts_data0);
1648 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1649 rxbuf = qlcnic_process_lro(adapter, sds_ring,
1650 ring, sts_data0, sts_data1);
1651 break;
1652 case QLCNIC_RESPONSE_DESC:
1653 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1654 default:
1655 goto skip;
1658 WARN_ON(desc_cnt > 1);
1660 if (likely(rxbuf))
1661 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1662 else
1663 adapter->stats.null_rxbuf++;
1665 skip:
1666 for (; desc_cnt > 0; desc_cnt--) {
1667 desc = &sds_ring->desc_head[consumer];
1668 desc->status_desc_data[0] =
1669 cpu_to_le64(STATUS_OWNER_PHANTOM);
1670 consumer = get_next_index(consumer, sds_ring->num_desc);
1672 count++;
1675 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1676 struct qlcnic_host_rds_ring *rds_ring =
1677 &adapter->recv_ctx->rds_rings[ring];
1679 if (!list_empty(&sds_ring->free_list[ring])) {
1680 list_for_each(cur, &sds_ring->free_list[ring]) {
1681 rxbuf = list_entry(cur,
1682 struct qlcnic_rx_buffer, list);
1683 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1685 spin_lock(&rds_ring->lock);
1686 list_splice_tail_init(&sds_ring->free_list[ring],
1687 &rds_ring->free_list);
1688 spin_unlock(&rds_ring->lock);
1691 qlcnic_post_rx_buffers_nodb(adapter, rds_ring);
1694 if (count) {
1695 sds_ring->consumer = consumer;
1696 writel(consumer, sds_ring->crb_sts_consumer);
1699 return count;
1702 void
1703 qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter,
1704 struct qlcnic_host_rds_ring *rds_ring)
1706 struct rcv_desc *pdesc;
1707 struct qlcnic_rx_buffer *buffer;
1708 int count = 0;
1709 u32 producer;
1710 struct list_head *head;
1712 producer = rds_ring->producer;
1714 head = &rds_ring->free_list;
1715 while (!list_empty(head)) {
1717 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1719 if (!buffer->skb) {
1720 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1721 break;
1724 count++;
1725 list_del(&buffer->list);
1727 /* make a rcv descriptor */
1728 pdesc = &rds_ring->desc_head[producer];
1729 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1730 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1731 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1733 producer = get_next_index(producer, rds_ring->num_desc);
1736 if (count) {
1737 rds_ring->producer = producer;
1738 writel((producer-1) & (rds_ring->num_desc-1),
1739 rds_ring->crb_rcv_producer);
1743 static void
1744 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
1745 struct qlcnic_host_rds_ring *rds_ring)
1747 struct rcv_desc *pdesc;
1748 struct qlcnic_rx_buffer *buffer;
1749 int count = 0;
1750 uint32_t producer;
1751 struct list_head *head;
1753 if (!spin_trylock(&rds_ring->lock))
1754 return;
1756 producer = rds_ring->producer;
1758 head = &rds_ring->free_list;
1759 while (!list_empty(head)) {
1761 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1763 if (!buffer->skb) {
1764 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1765 break;
1768 count++;
1769 list_del(&buffer->list);
1771 /* make a rcv descriptor */
1772 pdesc = &rds_ring->desc_head[producer];
1773 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1774 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1775 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1777 producer = get_next_index(producer, rds_ring->num_desc);
1780 if (count) {
1781 rds_ring->producer = producer;
1782 writel((producer - 1) & (rds_ring->num_desc - 1),
1783 rds_ring->crb_rcv_producer);
1785 spin_unlock(&rds_ring->lock);
1788 static void dump_skb(struct sk_buff *skb, struct qlcnic_adapter *adapter)
1790 int i;
1791 unsigned char *data = skb->data;
1793 printk(KERN_INFO "\n");
1794 for (i = 0; i < skb->len; i++) {
1795 QLCDB(adapter, DRV, "%02x ", data[i]);
1796 if ((i & 0x0f) == 8)
1797 printk(KERN_INFO "\n");
1801 void qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter,
1802 struct qlcnic_host_sds_ring *sds_ring,
1803 int ring, u64 sts_data0)
1805 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1806 struct sk_buff *skb;
1807 struct qlcnic_host_rds_ring *rds_ring;
1808 int index, length, cksum, pkt_offset;
1810 if (unlikely(ring >= adapter->max_rds_rings))
1811 return;
1813 rds_ring = &recv_ctx->rds_rings[ring];
1815 index = qlcnic_get_sts_refhandle(sts_data0);
1816 length = qlcnic_get_sts_totallength(sts_data0);
1817 if (unlikely(index >= rds_ring->num_desc))
1818 return;
1820 cksum = qlcnic_get_sts_status(sts_data0);
1821 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1823 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1824 if (!skb)
1825 return;
1827 if (length > rds_ring->skb_size)
1828 skb_put(skb, rds_ring->skb_size);
1829 else
1830 skb_put(skb, length);
1832 if (pkt_offset)
1833 skb_pull(skb, pkt_offset);
1835 if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
1836 adapter->diag_cnt++;
1837 else
1838 dump_skb(skb, adapter);
1840 dev_kfree_skb_any(skb);
1841 adapter->stats.rx_pkts++;
1842 adapter->stats.rxbytes += length;
1844 return;
1847 void
1848 qlcnic_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1850 struct qlcnic_adapter *adapter = sds_ring->adapter;
1851 struct status_desc *desc;
1852 u64 sts_data0;
1853 int ring, opcode, desc_cnt;
1855 u32 consumer = sds_ring->consumer;
1857 desc = &sds_ring->desc_head[consumer];
1858 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1860 if (!(sts_data0 & STATUS_OWNER_HOST))
1861 return;
1863 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1864 opcode = qlcnic_get_sts_opcode(sts_data0);
1865 switch (opcode) {
1866 case QLCNIC_RESPONSE_DESC:
1867 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1868 break;
1869 default:
1870 ring = qlcnic_get_sts_type(sts_data0);
1871 qlcnic_process_rcv_diag(adapter, sds_ring, ring, sts_data0);
1872 break;
1875 for (; desc_cnt > 0; desc_cnt--) {
1876 desc = &sds_ring->desc_head[consumer];
1877 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1878 consumer = get_next_index(consumer, sds_ring->num_desc);
1881 sds_ring->consumer = consumer;
1882 writel(consumer, sds_ring->crb_sts_consumer);
1885 void
1886 qlcnic_fetch_mac(struct qlcnic_adapter *adapter, u32 off1, u32 off2,
1887 u8 alt_mac, u8 *mac)
1889 u32 mac_low, mac_high;
1890 int i;
1892 mac_low = off1;
1893 mac_high = off2;
1895 if (alt_mac) {
1896 mac_low |= (mac_low >> 16) | (mac_high << 16);
1897 mac_high >>= 16;
1900 for (i = 0; i < 2; i++)
1901 mac[i] = (u8)(mac_high >> ((1 - i) * 8));
1902 for (i = 2; i < 6; i++)
1903 mac[i] = (u8)(mac_low >> ((5 - i) * 8));