Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[linux/fpc-iii.git] / drivers / net / qlcnic / qlcnic_init.c
blob71a4e664ad76970d920c669f3fa8366f92ca62fa
1 /*
2 * Copyright (C) 2009 - QLogic Corporation.
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
20 * The full GNU General Public License is included in this distribution
21 * in the file called "COPYING".
25 #include <linux/netdevice.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include "qlcnic.h"
30 struct crb_addr_pair {
31 u32 addr;
32 u32 data;
35 #define QLCNIC_MAX_CRB_XFORM 60
36 static unsigned int crb_addr_xform[QLCNIC_MAX_CRB_XFORM];
38 #define crb_addr_transform(name) \
39 (crb_addr_xform[QLCNIC_HW_PX_MAP_CRB_##name] = \
40 QLCNIC_HW_CRB_HUB_AGT_ADR_##name << 20)
42 #define QLCNIC_ADDR_ERROR (0xffffffff)
44 static void
45 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
46 struct qlcnic_host_rds_ring *rds_ring);
48 static void crb_addr_transform_setup(void)
50 crb_addr_transform(XDMA);
51 crb_addr_transform(TIMR);
52 crb_addr_transform(SRE);
53 crb_addr_transform(SQN3);
54 crb_addr_transform(SQN2);
55 crb_addr_transform(SQN1);
56 crb_addr_transform(SQN0);
57 crb_addr_transform(SQS3);
58 crb_addr_transform(SQS2);
59 crb_addr_transform(SQS1);
60 crb_addr_transform(SQS0);
61 crb_addr_transform(RPMX7);
62 crb_addr_transform(RPMX6);
63 crb_addr_transform(RPMX5);
64 crb_addr_transform(RPMX4);
65 crb_addr_transform(RPMX3);
66 crb_addr_transform(RPMX2);
67 crb_addr_transform(RPMX1);
68 crb_addr_transform(RPMX0);
69 crb_addr_transform(ROMUSB);
70 crb_addr_transform(SN);
71 crb_addr_transform(QMN);
72 crb_addr_transform(QMS);
73 crb_addr_transform(PGNI);
74 crb_addr_transform(PGND);
75 crb_addr_transform(PGN3);
76 crb_addr_transform(PGN2);
77 crb_addr_transform(PGN1);
78 crb_addr_transform(PGN0);
79 crb_addr_transform(PGSI);
80 crb_addr_transform(PGSD);
81 crb_addr_transform(PGS3);
82 crb_addr_transform(PGS2);
83 crb_addr_transform(PGS1);
84 crb_addr_transform(PGS0);
85 crb_addr_transform(PS);
86 crb_addr_transform(PH);
87 crb_addr_transform(NIU);
88 crb_addr_transform(I2Q);
89 crb_addr_transform(EG);
90 crb_addr_transform(MN);
91 crb_addr_transform(MS);
92 crb_addr_transform(CAS2);
93 crb_addr_transform(CAS1);
94 crb_addr_transform(CAS0);
95 crb_addr_transform(CAM);
96 crb_addr_transform(C2C1);
97 crb_addr_transform(C2C0);
98 crb_addr_transform(SMB);
99 crb_addr_transform(OCM0);
100 crb_addr_transform(I2C0);
103 void qlcnic_release_rx_buffers(struct qlcnic_adapter *adapter)
105 struct qlcnic_recv_context *recv_ctx;
106 struct qlcnic_host_rds_ring *rds_ring;
107 struct qlcnic_rx_buffer *rx_buf;
108 int i, ring;
110 recv_ctx = &adapter->recv_ctx;
111 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
112 rds_ring = &recv_ctx->rds_rings[ring];
113 for (i = 0; i < rds_ring->num_desc; ++i) {
114 rx_buf = &(rds_ring->rx_buf_arr[i]);
115 if (rx_buf->state == QLCNIC_BUFFER_FREE)
116 continue;
117 pci_unmap_single(adapter->pdev,
118 rx_buf->dma,
119 rds_ring->dma_size,
120 PCI_DMA_FROMDEVICE);
121 if (rx_buf->skb != NULL)
122 dev_kfree_skb_any(rx_buf->skb);
127 void qlcnic_release_tx_buffers(struct qlcnic_adapter *adapter)
129 struct qlcnic_cmd_buffer *cmd_buf;
130 struct qlcnic_skb_frag *buffrag;
131 int i, j;
132 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
134 cmd_buf = tx_ring->cmd_buf_arr;
135 for (i = 0; i < tx_ring->num_desc; i++) {
136 buffrag = cmd_buf->frag_array;
137 if (buffrag->dma) {
138 pci_unmap_single(adapter->pdev, buffrag->dma,
139 buffrag->length, PCI_DMA_TODEVICE);
140 buffrag->dma = 0ULL;
142 for (j = 0; j < cmd_buf->frag_count; j++) {
143 buffrag++;
144 if (buffrag->dma) {
145 pci_unmap_page(adapter->pdev, buffrag->dma,
146 buffrag->length,
147 PCI_DMA_TODEVICE);
148 buffrag->dma = 0ULL;
151 if (cmd_buf->skb) {
152 dev_kfree_skb_any(cmd_buf->skb);
153 cmd_buf->skb = NULL;
155 cmd_buf++;
159 void qlcnic_free_sw_resources(struct qlcnic_adapter *adapter)
161 struct qlcnic_recv_context *recv_ctx;
162 struct qlcnic_host_rds_ring *rds_ring;
163 struct qlcnic_host_tx_ring *tx_ring;
164 int ring;
166 recv_ctx = &adapter->recv_ctx;
168 if (recv_ctx->rds_rings == NULL)
169 goto skip_rds;
171 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
172 rds_ring = &recv_ctx->rds_rings[ring];
173 vfree(rds_ring->rx_buf_arr);
174 rds_ring->rx_buf_arr = NULL;
176 kfree(recv_ctx->rds_rings);
178 skip_rds:
179 if (adapter->tx_ring == NULL)
180 return;
182 tx_ring = adapter->tx_ring;
183 vfree(tx_ring->cmd_buf_arr);
184 kfree(adapter->tx_ring);
187 int qlcnic_alloc_sw_resources(struct qlcnic_adapter *adapter)
189 struct qlcnic_recv_context *recv_ctx;
190 struct qlcnic_host_rds_ring *rds_ring;
191 struct qlcnic_host_sds_ring *sds_ring;
192 struct qlcnic_host_tx_ring *tx_ring;
193 struct qlcnic_rx_buffer *rx_buf;
194 int ring, i, size;
196 struct qlcnic_cmd_buffer *cmd_buf_arr;
197 struct net_device *netdev = adapter->netdev;
199 size = sizeof(struct qlcnic_host_tx_ring);
200 tx_ring = kzalloc(size, GFP_KERNEL);
201 if (tx_ring == NULL) {
202 dev_err(&netdev->dev, "failed to allocate tx ring struct\n");
203 return -ENOMEM;
205 adapter->tx_ring = tx_ring;
207 tx_ring->num_desc = adapter->num_txd;
208 tx_ring->txq = netdev_get_tx_queue(netdev, 0);
210 cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring));
211 if (cmd_buf_arr == NULL) {
212 dev_err(&netdev->dev, "failed to allocate cmd buffer ring\n");
213 goto err_out;
215 memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
216 tx_ring->cmd_buf_arr = cmd_buf_arr;
218 recv_ctx = &adapter->recv_ctx;
220 size = adapter->max_rds_rings * sizeof(struct qlcnic_host_rds_ring);
221 rds_ring = kzalloc(size, GFP_KERNEL);
222 if (rds_ring == NULL) {
223 dev_err(&netdev->dev, "failed to allocate rds ring struct\n");
224 goto err_out;
226 recv_ctx->rds_rings = rds_ring;
228 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
229 rds_ring = &recv_ctx->rds_rings[ring];
230 switch (ring) {
231 case RCV_RING_NORMAL:
232 rds_ring->num_desc = adapter->num_rxd;
233 rds_ring->dma_size = QLCNIC_P3_RX_BUF_MAX_LEN;
234 rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
235 break;
237 case RCV_RING_JUMBO:
238 rds_ring->num_desc = adapter->num_jumbo_rxd;
239 rds_ring->dma_size =
240 QLCNIC_P3_RX_JUMBO_BUF_MAX_LEN;
242 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
243 rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA;
245 rds_ring->skb_size =
246 rds_ring->dma_size + NET_IP_ALIGN;
247 break;
249 rds_ring->rx_buf_arr = (struct qlcnic_rx_buffer *)
250 vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
251 if (rds_ring->rx_buf_arr == NULL) {
252 dev_err(&netdev->dev, "Failed to allocate "
253 "rx buffer ring %d\n", ring);
254 goto err_out;
256 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
257 INIT_LIST_HEAD(&rds_ring->free_list);
259 * Now go through all of them, set reference handles
260 * and put them in the queues.
262 rx_buf = rds_ring->rx_buf_arr;
263 for (i = 0; i < rds_ring->num_desc; i++) {
264 list_add_tail(&rx_buf->list,
265 &rds_ring->free_list);
266 rx_buf->ref_handle = i;
267 rx_buf->state = QLCNIC_BUFFER_FREE;
268 rx_buf++;
270 spin_lock_init(&rds_ring->lock);
273 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
274 sds_ring = &recv_ctx->sds_rings[ring];
275 sds_ring->irq = adapter->msix_entries[ring].vector;
276 sds_ring->adapter = adapter;
277 sds_ring->num_desc = adapter->num_rxd;
279 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
280 INIT_LIST_HEAD(&sds_ring->free_list[i]);
283 return 0;
285 err_out:
286 qlcnic_free_sw_resources(adapter);
287 return -ENOMEM;
291 * Utility to translate from internal Phantom CRB address
292 * to external PCI CRB address.
294 static u32 qlcnic_decode_crb_addr(u32 addr)
296 int i;
297 u32 base_addr, offset, pci_base;
299 crb_addr_transform_setup();
301 pci_base = QLCNIC_ADDR_ERROR;
302 base_addr = addr & 0xfff00000;
303 offset = addr & 0x000fffff;
305 for (i = 0; i < QLCNIC_MAX_CRB_XFORM; i++) {
306 if (crb_addr_xform[i] == base_addr) {
307 pci_base = i << 20;
308 break;
311 if (pci_base == QLCNIC_ADDR_ERROR)
312 return pci_base;
313 else
314 return pci_base + offset;
317 #define QLCNIC_MAX_ROM_WAIT_USEC 100
319 static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter)
321 long timeout = 0;
322 long done = 0;
324 cond_resched();
326 while (done == 0) {
327 done = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_STATUS);
328 done &= 2;
329 if (++timeout >= QLCNIC_MAX_ROM_WAIT_USEC) {
330 dev_err(&adapter->pdev->dev,
331 "Timeout reached waiting for rom done");
332 return -EIO;
334 udelay(1);
336 return 0;
339 static int do_rom_fast_read(struct qlcnic_adapter *adapter,
340 int addr, int *valp)
342 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ADDRESS, addr);
343 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
344 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 3);
345 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_INSTR_OPCODE, 0xb);
346 if (qlcnic_wait_rom_done(adapter)) {
347 dev_err(&adapter->pdev->dev, "Error waiting for rom done\n");
348 return -EIO;
350 /* reset abyte_cnt and dummy_byte_cnt */
351 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 0);
352 udelay(10);
353 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
355 *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA);
356 return 0;
359 static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
360 u8 *bytes, size_t size)
362 int addridx;
363 int ret = 0;
365 for (addridx = addr; addridx < (addr + size); addridx += 4) {
366 int v;
367 ret = do_rom_fast_read(adapter, addridx, &v);
368 if (ret != 0)
369 break;
370 *(__le32 *)bytes = cpu_to_le32(v);
371 bytes += 4;
374 return ret;
378 qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
379 u8 *bytes, size_t size)
381 int ret;
383 ret = qlcnic_rom_lock(adapter);
384 if (ret < 0)
385 return ret;
387 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
389 qlcnic_rom_unlock(adapter);
390 return ret;
393 int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, int addr, int *valp)
395 int ret;
397 if (qlcnic_rom_lock(adapter) != 0)
398 return -EIO;
400 ret = do_rom_fast_read(adapter, addr, valp);
401 qlcnic_rom_unlock(adapter);
402 return ret;
405 int qlcnic_pinit_from_rom(struct qlcnic_adapter *adapter)
407 int addr, val;
408 int i, n, init_delay;
409 struct crb_addr_pair *buf;
410 unsigned offset;
411 u32 off;
412 struct pci_dev *pdev = adapter->pdev;
414 /* resetall */
415 qlcnic_rom_lock(adapter);
416 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xffffffff);
417 qlcnic_rom_unlock(adapter);
419 if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) ||
420 qlcnic_rom_fast_read(adapter, 4, &n) != 0) {
421 dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n);
422 return -EIO;
424 offset = n & 0xffffU;
425 n = (n >> 16) & 0xffffU;
427 if (n >= 1024) {
428 dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n");
429 return -EIO;
432 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
433 if (buf == NULL) {
434 dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n");
435 return -ENOMEM;
438 for (i = 0; i < n; i++) {
439 if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
440 qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
441 kfree(buf);
442 return -EIO;
445 buf[i].addr = addr;
446 buf[i].data = val;
449 for (i = 0; i < n; i++) {
451 off = qlcnic_decode_crb_addr(buf[i].addr);
452 if (off == QLCNIC_ADDR_ERROR) {
453 dev_err(&pdev->dev, "CRB init value out of range %x\n",
454 buf[i].addr);
455 continue;
457 off += QLCNIC_PCI_CRBSPACE;
459 if (off & 1)
460 continue;
462 /* skipping cold reboot MAGIC */
463 if (off == QLCNIC_CAM_RAM(0x1fc))
464 continue;
465 if (off == (QLCNIC_CRB_I2C0 + 0x1c))
466 continue;
467 if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */
468 continue;
469 if (off == (ROMUSB_GLB + 0xa8))
470 continue;
471 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
472 continue;
473 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
474 continue;
475 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
476 continue;
477 if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET)
478 continue;
479 /* skip the function enable register */
480 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION))
481 continue;
482 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION2))
483 continue;
484 if ((off & 0x0ff00000) == QLCNIC_CRB_SMB)
485 continue;
487 init_delay = 1;
488 /* After writing this register, HW needs time for CRB */
489 /* to quiet down (else crb_window returns 0xffffffff) */
490 if (off == QLCNIC_ROMUSB_GLB_SW_RESET)
491 init_delay = 1000;
493 QLCWR32(adapter, off, buf[i].data);
495 msleep(init_delay);
497 kfree(buf);
499 /* p2dn replyCount */
500 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e);
501 /* disable_peg_cache 0 & 1*/
502 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8);
503 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8);
505 /* peg_clr_all */
506 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0);
507 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0);
508 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0);
509 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0);
510 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0);
511 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0);
512 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0);
513 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0);
514 return 0;
518 qlcnic_setup_idc_param(struct qlcnic_adapter *adapter) {
520 int timeo;
521 u32 val;
523 val = QLCRD32(adapter, QLCNIC_CRB_DEV_PARTITION_INFO);
524 val = (val >> (adapter->portnum * 4)) & 0xf;
526 if ((val & 0x3) != 1) {
527 dev_err(&adapter->pdev->dev, "Not an Ethernet NIC func=%u\n",
528 val);
529 return -EIO;
532 adapter->physical_port = (val >> 2);
534 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DEV_INIT_TIMEOUT, &timeo))
535 timeo = 30;
537 adapter->dev_init_timeo = timeo;
539 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DRV_RESET_TIMEOUT, &timeo))
540 timeo = 10;
542 adapter->reset_ack_timeo = timeo;
544 return 0;
547 static int
548 qlcnic_has_mn(struct qlcnic_adapter *adapter)
550 u32 capability, flashed_ver;
551 capability = 0;
553 qlcnic_rom_fast_read(adapter,
554 QLCNIC_FW_VERSION_OFFSET, (int *)&flashed_ver);
555 flashed_ver = QLCNIC_DECODE_VERSION(flashed_ver);
557 capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY);
558 if (capability & QLCNIC_PEG_TUNE_MN_PRESENT)
559 return 1;
561 return 0;
564 static
565 struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section)
567 u32 i;
568 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
569 __le32 entries = cpu_to_le32(directory->num_entries);
571 for (i = 0; i < entries; i++) {
573 __le32 offs = cpu_to_le32(directory->findex) +
574 (i * cpu_to_le32(directory->entry_size));
575 __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
577 if (tab_type == section)
578 return (struct uni_table_desc *) &unirom[offs];
581 return NULL;
584 #define FILEHEADER_SIZE (14 * 4)
586 static int
587 qlcnic_validate_header(struct qlcnic_adapter *adapter)
589 const u8 *unirom = adapter->fw->data;
590 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
591 __le32 fw_file_size = adapter->fw->size;
592 __le32 entries;
593 __le32 entry_size;
594 __le32 tab_size;
596 if (fw_file_size < FILEHEADER_SIZE)
597 return -EINVAL;
599 entries = cpu_to_le32(directory->num_entries);
600 entry_size = cpu_to_le32(directory->entry_size);
601 tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
603 if (fw_file_size < tab_size)
604 return -EINVAL;
606 return 0;
609 static int
610 qlcnic_validate_bootld(struct qlcnic_adapter *adapter)
612 struct uni_table_desc *tab_desc;
613 struct uni_data_desc *descr;
614 const u8 *unirom = adapter->fw->data;
615 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
616 QLCNIC_UNI_BOOTLD_IDX_OFF));
617 __le32 offs;
618 __le32 tab_size;
619 __le32 data_size;
621 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD);
623 if (!tab_desc)
624 return -EINVAL;
626 tab_size = cpu_to_le32(tab_desc->findex) +
627 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
629 if (adapter->fw->size < tab_size)
630 return -EINVAL;
632 offs = cpu_to_le32(tab_desc->findex) +
633 (cpu_to_le32(tab_desc->entry_size) * (idx));
634 descr = (struct uni_data_desc *)&unirom[offs];
636 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
638 if (adapter->fw->size < data_size)
639 return -EINVAL;
641 return 0;
644 static int
645 qlcnic_validate_fw(struct qlcnic_adapter *adapter)
647 struct uni_table_desc *tab_desc;
648 struct uni_data_desc *descr;
649 const u8 *unirom = adapter->fw->data;
650 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
651 QLCNIC_UNI_FIRMWARE_IDX_OFF));
652 __le32 offs;
653 __le32 tab_size;
654 __le32 data_size;
656 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW);
658 if (!tab_desc)
659 return -EINVAL;
661 tab_size = cpu_to_le32(tab_desc->findex) +
662 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
664 if (adapter->fw->size < tab_size)
665 return -EINVAL;
667 offs = cpu_to_le32(tab_desc->findex) +
668 (cpu_to_le32(tab_desc->entry_size) * (idx));
669 descr = (struct uni_data_desc *)&unirom[offs];
670 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
672 if (adapter->fw->size < data_size)
673 return -EINVAL;
675 return 0;
678 static int
679 qlcnic_validate_product_offs(struct qlcnic_adapter *adapter)
681 struct uni_table_desc *ptab_descr;
682 const u8 *unirom = adapter->fw->data;
683 int mn_present = qlcnic_has_mn(adapter);
684 __le32 entries;
685 __le32 entry_size;
686 __le32 tab_size;
687 u32 i;
689 ptab_descr = qlcnic_get_table_desc(unirom,
690 QLCNIC_UNI_DIR_SECT_PRODUCT_TBL);
691 if (!ptab_descr)
692 return -EINVAL;
694 entries = cpu_to_le32(ptab_descr->num_entries);
695 entry_size = cpu_to_le32(ptab_descr->entry_size);
696 tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
698 if (adapter->fw->size < tab_size)
699 return -EINVAL;
701 nomn:
702 for (i = 0; i < entries; i++) {
704 __le32 flags, file_chiprev, offs;
705 u8 chiprev = adapter->ahw.revision_id;
706 u32 flagbit;
708 offs = cpu_to_le32(ptab_descr->findex) +
709 (i * cpu_to_le32(ptab_descr->entry_size));
710 flags = cpu_to_le32(*((int *)&unirom[offs] +
711 QLCNIC_UNI_FLAGS_OFF));
712 file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
713 QLCNIC_UNI_CHIP_REV_OFF));
715 flagbit = mn_present ? 1 : 2;
717 if ((chiprev == file_chiprev) &&
718 ((1ULL << flagbit) & flags)) {
719 adapter->file_prd_off = offs;
720 return 0;
723 if (mn_present) {
724 mn_present = 0;
725 goto nomn;
727 return -EINVAL;
730 static int
731 qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter)
733 if (qlcnic_validate_header(adapter)) {
734 dev_err(&adapter->pdev->dev,
735 "unified image: header validation failed\n");
736 return -EINVAL;
739 if (qlcnic_validate_product_offs(adapter)) {
740 dev_err(&adapter->pdev->dev,
741 "unified image: product validation failed\n");
742 return -EINVAL;
745 if (qlcnic_validate_bootld(adapter)) {
746 dev_err(&adapter->pdev->dev,
747 "unified image: bootld validation failed\n");
748 return -EINVAL;
751 if (qlcnic_validate_fw(adapter)) {
752 dev_err(&adapter->pdev->dev,
753 "unified image: firmware validation failed\n");
754 return -EINVAL;
757 return 0;
760 static
761 struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter,
762 u32 section, u32 idx_offset)
764 const u8 *unirom = adapter->fw->data;
765 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
766 idx_offset));
767 struct uni_table_desc *tab_desc;
768 __le32 offs;
770 tab_desc = qlcnic_get_table_desc(unirom, section);
772 if (tab_desc == NULL)
773 return NULL;
775 offs = cpu_to_le32(tab_desc->findex) +
776 (cpu_to_le32(tab_desc->entry_size) * idx);
778 return (struct uni_data_desc *)&unirom[offs];
781 static u8 *
782 qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter)
784 u32 offs = QLCNIC_BOOTLD_START;
786 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
787 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
788 QLCNIC_UNI_DIR_SECT_BOOTLD,
789 QLCNIC_UNI_BOOTLD_IDX_OFF))->findex);
791 return (u8 *)&adapter->fw->data[offs];
794 static u8 *
795 qlcnic_get_fw_offs(struct qlcnic_adapter *adapter)
797 u32 offs = QLCNIC_IMAGE_START;
799 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
800 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
801 QLCNIC_UNI_DIR_SECT_FW,
802 QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex);
804 return (u8 *)&adapter->fw->data[offs];
807 static __le32
808 qlcnic_get_fw_size(struct qlcnic_adapter *adapter)
810 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
811 return cpu_to_le32((qlcnic_get_data_desc(adapter,
812 QLCNIC_UNI_DIR_SECT_FW,
813 QLCNIC_UNI_FIRMWARE_IDX_OFF))->size);
814 else
815 return cpu_to_le32(
816 *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]);
819 static __le32
820 qlcnic_get_fw_version(struct qlcnic_adapter *adapter)
822 struct uni_data_desc *fw_data_desc;
823 const struct firmware *fw = adapter->fw;
824 __le32 major, minor, sub;
825 const u8 *ver_str;
826 int i, ret;
828 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
829 return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]);
831 fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW,
832 QLCNIC_UNI_FIRMWARE_IDX_OFF);
833 ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
834 cpu_to_le32(fw_data_desc->size) - 17;
836 for (i = 0; i < 12; i++) {
837 if (!strncmp(&ver_str[i], "REV=", 4)) {
838 ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
839 &major, &minor, &sub);
840 if (ret != 3)
841 return 0;
842 else
843 return major + (minor << 8) + (sub << 16);
847 return 0;
850 static __le32
851 qlcnic_get_bios_version(struct qlcnic_adapter *adapter)
853 const struct firmware *fw = adapter->fw;
854 __le32 bios_ver, prd_off = adapter->file_prd_off;
856 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
857 return cpu_to_le32(
858 *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]);
860 bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
861 + QLCNIC_UNI_BIOS_VERSION_OFF));
863 return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24);
867 qlcnic_need_fw_reset(struct qlcnic_adapter *adapter)
869 u32 count, old_count;
870 u32 val, version, major, minor, build;
871 int i, timeout;
873 if (adapter->need_fw_reset)
874 return 1;
876 /* last attempt had failed */
877 if (QLCRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED)
878 return 1;
880 old_count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
882 for (i = 0; i < 10; i++) {
884 timeout = msleep_interruptible(200);
885 if (timeout) {
886 QLCWR32(adapter, CRB_CMDPEG_STATE,
887 PHAN_INITIALIZE_FAILED);
888 return -EINTR;
891 count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
892 if (count != old_count)
893 break;
896 /* firmware is dead */
897 if (count == old_count)
898 return 1;
900 /* check if we have got newer or different file firmware */
901 if (adapter->fw) {
903 val = qlcnic_get_fw_version(adapter);
905 version = QLCNIC_DECODE_VERSION(val);
907 major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
908 minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
909 build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
911 if (version > QLCNIC_VERSION_CODE(major, minor, build))
912 return 1;
915 return 0;
918 static const char *fw_name[] = {
919 QLCNIC_UNIFIED_ROMIMAGE_NAME,
920 QLCNIC_FLASH_ROMIMAGE_NAME,
924 qlcnic_load_firmware(struct qlcnic_adapter *adapter)
926 u64 *ptr64;
927 u32 i, flashaddr, size;
928 const struct firmware *fw = adapter->fw;
929 struct pci_dev *pdev = adapter->pdev;
931 dev_info(&pdev->dev, "loading firmware from %s\n",
932 fw_name[adapter->fw_type]);
934 if (fw) {
935 __le64 data;
937 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
939 ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter);
940 flashaddr = QLCNIC_BOOTLD_START;
942 for (i = 0; i < size; i++) {
943 data = cpu_to_le64(ptr64[i]);
945 if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data))
946 return -EIO;
948 flashaddr += 8;
951 size = (__force u32)qlcnic_get_fw_size(adapter) / 8;
953 ptr64 = (u64 *)qlcnic_get_fw_offs(adapter);
954 flashaddr = QLCNIC_IMAGE_START;
956 for (i = 0; i < size; i++) {
957 data = cpu_to_le64(ptr64[i]);
959 if (qlcnic_pci_mem_write_2M(adapter,
960 flashaddr, data))
961 return -EIO;
963 flashaddr += 8;
966 size = (__force u32)qlcnic_get_fw_size(adapter) % 8;
967 if (size) {
968 data = cpu_to_le64(ptr64[i]);
970 if (qlcnic_pci_mem_write_2M(adapter,
971 flashaddr, data))
972 return -EIO;
975 } else {
976 u64 data;
977 u32 hi, lo;
979 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
980 flashaddr = QLCNIC_BOOTLD_START;
982 for (i = 0; i < size; i++) {
983 if (qlcnic_rom_fast_read(adapter,
984 flashaddr, (int *)&lo) != 0)
985 return -EIO;
986 if (qlcnic_rom_fast_read(adapter,
987 flashaddr + 4, (int *)&hi) != 0)
988 return -EIO;
990 data = (((u64)hi << 32) | lo);
992 if (qlcnic_pci_mem_write_2M(adapter,
993 flashaddr, data))
994 return -EIO;
996 flashaddr += 8;
999 msleep(1);
1001 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020);
1002 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e);
1003 return 0;
1006 static int
1007 qlcnic_validate_firmware(struct qlcnic_adapter *adapter)
1009 __le32 val;
1010 u32 ver, min_ver, bios, min_size;
1011 struct pci_dev *pdev = adapter->pdev;
1012 const struct firmware *fw = adapter->fw;
1013 u8 fw_type = adapter->fw_type;
1015 if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) {
1016 if (qlcnic_validate_unified_romimage(adapter))
1017 return -EINVAL;
1019 min_size = QLCNIC_UNI_FW_MIN_SIZE;
1020 } else {
1021 val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]);
1022 if ((__force u32)val != QLCNIC_BDINFO_MAGIC)
1023 return -EINVAL;
1025 min_size = QLCNIC_FW_MIN_SIZE;
1028 if (fw->size < min_size)
1029 return -EINVAL;
1031 val = qlcnic_get_fw_version(adapter);
1033 min_ver = QLCNIC_VERSION_CODE(4, 0, 216);
1035 ver = QLCNIC_DECODE_VERSION(val);
1037 if ((_major(ver) > _QLCNIC_LINUX_MAJOR) || (ver < min_ver)) {
1038 dev_err(&pdev->dev,
1039 "%s: firmware version %d.%d.%d unsupported\n",
1040 fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
1041 return -EINVAL;
1044 val = qlcnic_get_bios_version(adapter);
1045 qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios);
1046 if ((__force u32)val != bios) {
1047 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
1048 fw_name[fw_type]);
1049 return -EINVAL;
1052 /* check if flashed firmware is newer */
1053 if (qlcnic_rom_fast_read(adapter,
1054 QLCNIC_FW_VERSION_OFFSET, (int *)&val))
1055 return -EIO;
1057 val = QLCNIC_DECODE_VERSION(val);
1058 if (val > ver) {
1059 dev_info(&pdev->dev, "%s: firmware is older than flash\n",
1060 fw_name[fw_type]);
1061 return -EINVAL;
1064 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
1065 return 0;
1068 static void
1069 qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter)
1071 u8 fw_type;
1073 switch (adapter->fw_type) {
1074 case QLCNIC_UNKNOWN_ROMIMAGE:
1075 fw_type = QLCNIC_UNIFIED_ROMIMAGE;
1076 break;
1078 case QLCNIC_UNIFIED_ROMIMAGE:
1079 default:
1080 fw_type = QLCNIC_FLASH_ROMIMAGE;
1081 break;
1084 adapter->fw_type = fw_type;
1089 void qlcnic_request_firmware(struct qlcnic_adapter *adapter)
1091 struct pci_dev *pdev = adapter->pdev;
1092 int rc;
1094 adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE;
1096 next:
1097 qlcnic_get_next_fwtype(adapter);
1099 if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) {
1100 adapter->fw = NULL;
1101 } else {
1102 rc = request_firmware(&adapter->fw,
1103 fw_name[adapter->fw_type], &pdev->dev);
1104 if (rc != 0)
1105 goto next;
1107 rc = qlcnic_validate_firmware(adapter);
1108 if (rc != 0) {
1109 release_firmware(adapter->fw);
1110 msleep(1);
1111 goto next;
1117 void
1118 qlcnic_release_firmware(struct qlcnic_adapter *adapter)
1120 if (adapter->fw)
1121 release_firmware(adapter->fw);
1122 adapter->fw = NULL;
1125 int qlcnic_phantom_init(struct qlcnic_adapter *adapter)
1127 u32 val;
1128 int retries = 60;
1130 do {
1131 val = QLCRD32(adapter, CRB_CMDPEG_STATE);
1133 switch (val) {
1134 case PHAN_INITIALIZE_COMPLETE:
1135 case PHAN_INITIALIZE_ACK:
1136 return 0;
1137 case PHAN_INITIALIZE_FAILED:
1138 goto out_err;
1139 default:
1140 break;
1143 msleep(500);
1145 } while (--retries);
1147 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
1149 out_err:
1150 dev_err(&adapter->pdev->dev, "firmware init failed\n");
1151 return -EIO;
1154 static int
1155 qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter)
1157 u32 val;
1158 int retries = 2000;
1160 do {
1161 val = QLCRD32(adapter, CRB_RCVPEG_STATE);
1163 if (val == PHAN_PEG_RCV_INITIALIZED)
1164 return 0;
1166 msleep(10);
1168 } while (--retries);
1170 if (!retries) {
1171 dev_err(&adapter->pdev->dev, "Receive Peg initialization not "
1172 "complete, state: 0x%x.\n", val);
1173 return -EIO;
1176 return 0;
1179 int qlcnic_init_firmware(struct qlcnic_adapter *adapter)
1181 int err;
1183 err = qlcnic_receive_peg_ready(adapter);
1184 if (err)
1185 return err;
1187 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
1189 return err;
1192 static void
1193 qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1194 struct qlcnic_fw_msg *msg)
1196 u32 cable_OUI;
1197 u16 cable_len;
1198 u16 link_speed;
1199 u8 link_status, module, duplex, autoneg;
1200 struct net_device *netdev = adapter->netdev;
1202 adapter->has_link_events = 1;
1204 cable_OUI = msg->body[1] & 0xffffffff;
1205 cable_len = (msg->body[1] >> 32) & 0xffff;
1206 link_speed = (msg->body[1] >> 48) & 0xffff;
1208 link_status = msg->body[2] & 0xff;
1209 duplex = (msg->body[2] >> 16) & 0xff;
1210 autoneg = (msg->body[2] >> 24) & 0xff;
1212 module = (msg->body[2] >> 8) & 0xff;
1213 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
1214 dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, "
1215 "length %d\n", cable_OUI, cable_len);
1216 else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
1217 dev_info(&netdev->dev, "unsupported cable length %d\n",
1218 cable_len);
1220 qlcnic_advert_link_change(adapter, link_status);
1222 if (duplex == LINKEVENT_FULL_DUPLEX)
1223 adapter->link_duplex = DUPLEX_FULL;
1224 else
1225 adapter->link_duplex = DUPLEX_HALF;
1227 adapter->module_type = module;
1228 adapter->link_autoneg = autoneg;
1229 adapter->link_speed = link_speed;
1232 static void
1233 qlcnic_handle_fw_message(int desc_cnt, int index,
1234 struct qlcnic_host_sds_ring *sds_ring)
1236 struct qlcnic_fw_msg msg;
1237 struct status_desc *desc;
1238 int i = 0, opcode;
1240 while (desc_cnt > 0 && i < 8) {
1241 desc = &sds_ring->desc_head[index];
1242 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1243 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1245 index = get_next_index(index, sds_ring->num_desc);
1246 desc_cnt--;
1249 opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
1250 switch (opcode) {
1251 case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1252 qlcnic_handle_linkevent(sds_ring->adapter, &msg);
1253 break;
1254 default:
1255 break;
1259 static int
1260 qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
1261 struct qlcnic_host_rds_ring *rds_ring,
1262 struct qlcnic_rx_buffer *buffer)
1264 struct sk_buff *skb;
1265 dma_addr_t dma;
1266 struct pci_dev *pdev = adapter->pdev;
1268 buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1269 if (!buffer->skb) {
1270 adapter->stats.skb_alloc_failure++;
1271 return -ENOMEM;
1274 skb = buffer->skb;
1276 skb_reserve(skb, 2);
1278 dma = pci_map_single(pdev, skb->data,
1279 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1281 if (pci_dma_mapping_error(pdev, dma)) {
1282 adapter->stats.rx_dma_map_error++;
1283 dev_kfree_skb_any(skb);
1284 buffer->skb = NULL;
1285 return -ENOMEM;
1288 buffer->skb = skb;
1289 buffer->dma = dma;
1290 buffer->state = QLCNIC_BUFFER_BUSY;
1292 return 0;
1295 static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1296 struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum)
1298 struct qlcnic_rx_buffer *buffer;
1299 struct sk_buff *skb;
1301 buffer = &rds_ring->rx_buf_arr[index];
1303 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1304 PCI_DMA_FROMDEVICE);
1306 skb = buffer->skb;
1307 if (!skb) {
1308 adapter->stats.null_skb++;
1309 goto no_skb;
1312 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1313 adapter->stats.csummed++;
1314 skb->ip_summed = CHECKSUM_UNNECESSARY;
1315 } else {
1316 skb->ip_summed = CHECKSUM_NONE;
1319 skb->dev = adapter->netdev;
1321 buffer->skb = NULL;
1322 no_skb:
1323 buffer->state = QLCNIC_BUFFER_FREE;
1324 return skb;
1327 static struct qlcnic_rx_buffer *
1328 qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1329 struct qlcnic_host_sds_ring *sds_ring,
1330 int ring, u64 sts_data0)
1332 struct net_device *netdev = adapter->netdev;
1333 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1334 struct qlcnic_rx_buffer *buffer;
1335 struct sk_buff *skb;
1336 struct qlcnic_host_rds_ring *rds_ring;
1337 int index, length, cksum, pkt_offset;
1339 if (unlikely(ring >= adapter->max_rds_rings))
1340 return NULL;
1342 rds_ring = &recv_ctx->rds_rings[ring];
1344 index = qlcnic_get_sts_refhandle(sts_data0);
1345 if (unlikely(index >= rds_ring->num_desc))
1346 return NULL;
1348 buffer = &rds_ring->rx_buf_arr[index];
1350 length = qlcnic_get_sts_totallength(sts_data0);
1351 cksum = qlcnic_get_sts_status(sts_data0);
1352 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1354 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1355 if (!skb)
1356 return buffer;
1358 if (length > rds_ring->skb_size)
1359 skb_put(skb, rds_ring->skb_size);
1360 else
1361 skb_put(skb, length);
1363 if (pkt_offset)
1364 skb_pull(skb, pkt_offset);
1366 skb->truesize = skb->len + sizeof(struct sk_buff);
1367 skb->protocol = eth_type_trans(skb, netdev);
1369 napi_gro_receive(&sds_ring->napi, skb);
1371 adapter->stats.rx_pkts++;
1372 adapter->stats.rxbytes += length;
1374 return buffer;
1377 #define QLC_TCP_HDR_SIZE 20
1378 #define QLC_TCP_TS_OPTION_SIZE 12
1379 #define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1381 static struct qlcnic_rx_buffer *
1382 qlcnic_process_lro(struct qlcnic_adapter *adapter,
1383 struct qlcnic_host_sds_ring *sds_ring,
1384 int ring, u64 sts_data0, u64 sts_data1)
1386 struct net_device *netdev = adapter->netdev;
1387 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1388 struct qlcnic_rx_buffer *buffer;
1389 struct sk_buff *skb;
1390 struct qlcnic_host_rds_ring *rds_ring;
1391 struct iphdr *iph;
1392 struct tcphdr *th;
1393 bool push, timestamp;
1394 int l2_hdr_offset, l4_hdr_offset;
1395 int index;
1396 u16 lro_length, length, data_offset;
1397 u32 seq_number;
1399 if (unlikely(ring > adapter->max_rds_rings))
1400 return NULL;
1402 rds_ring = &recv_ctx->rds_rings[ring];
1404 index = qlcnic_get_lro_sts_refhandle(sts_data0);
1405 if (unlikely(index > rds_ring->num_desc))
1406 return NULL;
1408 buffer = &rds_ring->rx_buf_arr[index];
1410 timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1411 lro_length = qlcnic_get_lro_sts_length(sts_data0);
1412 l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1413 l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1414 push = qlcnic_get_lro_sts_push_flag(sts_data0);
1415 seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1417 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1418 if (!skb)
1419 return buffer;
1421 if (timestamp)
1422 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1423 else
1424 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1426 skb_put(skb, lro_length + data_offset);
1428 skb->truesize = skb->len + sizeof(struct sk_buff) + skb_headroom(skb);
1430 skb_pull(skb, l2_hdr_offset);
1431 skb->protocol = eth_type_trans(skb, netdev);
1433 iph = (struct iphdr *)skb->data;
1434 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1436 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1437 iph->tot_len = htons(length);
1438 iph->check = 0;
1439 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1440 th->psh = push;
1441 th->seq = htonl(seq_number);
1443 length = skb->len;
1445 netif_receive_skb(skb);
1447 adapter->stats.lro_pkts++;
1448 adapter->stats.lrobytes += length;
1450 return buffer;
1454 qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1456 struct qlcnic_adapter *adapter = sds_ring->adapter;
1457 struct list_head *cur;
1458 struct status_desc *desc;
1459 struct qlcnic_rx_buffer *rxbuf;
1460 u64 sts_data0, sts_data1;
1462 int count = 0;
1463 int opcode, ring, desc_cnt;
1464 u32 consumer = sds_ring->consumer;
1466 while (count < max) {
1467 desc = &sds_ring->desc_head[consumer];
1468 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1470 if (!(sts_data0 & STATUS_OWNER_HOST))
1471 break;
1473 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1474 opcode = qlcnic_get_sts_opcode(sts_data0);
1476 switch (opcode) {
1477 case QLCNIC_RXPKT_DESC:
1478 case QLCNIC_OLD_RXPKT_DESC:
1479 case QLCNIC_SYN_OFFLOAD:
1480 ring = qlcnic_get_sts_type(sts_data0);
1481 rxbuf = qlcnic_process_rcv(adapter, sds_ring,
1482 ring, sts_data0);
1483 break;
1484 case QLCNIC_LRO_DESC:
1485 ring = qlcnic_get_lro_sts_type(sts_data0);
1486 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1487 rxbuf = qlcnic_process_lro(adapter, sds_ring,
1488 ring, sts_data0, sts_data1);
1489 break;
1490 case QLCNIC_RESPONSE_DESC:
1491 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1492 default:
1493 goto skip;
1496 WARN_ON(desc_cnt > 1);
1498 if (rxbuf)
1499 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1500 else
1501 adapter->stats.null_rxbuf++;
1503 skip:
1504 for (; desc_cnt > 0; desc_cnt--) {
1505 desc = &sds_ring->desc_head[consumer];
1506 desc->status_desc_data[0] =
1507 cpu_to_le64(STATUS_OWNER_PHANTOM);
1508 consumer = get_next_index(consumer, sds_ring->num_desc);
1510 count++;
1513 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1514 struct qlcnic_host_rds_ring *rds_ring =
1515 &adapter->recv_ctx.rds_rings[ring];
1517 if (!list_empty(&sds_ring->free_list[ring])) {
1518 list_for_each(cur, &sds_ring->free_list[ring]) {
1519 rxbuf = list_entry(cur,
1520 struct qlcnic_rx_buffer, list);
1521 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1523 spin_lock(&rds_ring->lock);
1524 list_splice_tail_init(&sds_ring->free_list[ring],
1525 &rds_ring->free_list);
1526 spin_unlock(&rds_ring->lock);
1529 qlcnic_post_rx_buffers_nodb(adapter, rds_ring);
1532 if (count) {
1533 sds_ring->consumer = consumer;
1534 writel(consumer, sds_ring->crb_sts_consumer);
1537 return count;
1540 void
1541 qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter, u32 ringid,
1542 struct qlcnic_host_rds_ring *rds_ring)
1544 struct rcv_desc *pdesc;
1545 struct qlcnic_rx_buffer *buffer;
1546 int producer, count = 0;
1547 struct list_head *head;
1549 spin_lock(&rds_ring->lock);
1551 producer = rds_ring->producer;
1553 head = &rds_ring->free_list;
1554 while (!list_empty(head)) {
1556 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1558 if (!buffer->skb) {
1559 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1560 break;
1563 count++;
1564 list_del(&buffer->list);
1566 /* make a rcv descriptor */
1567 pdesc = &rds_ring->desc_head[producer];
1568 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1569 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1570 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1572 producer = get_next_index(producer, rds_ring->num_desc);
1575 if (count) {
1576 rds_ring->producer = producer;
1577 writel((producer-1) & (rds_ring->num_desc-1),
1578 rds_ring->crb_rcv_producer);
1580 spin_unlock(&rds_ring->lock);
1583 static void
1584 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
1585 struct qlcnic_host_rds_ring *rds_ring)
1587 struct rcv_desc *pdesc;
1588 struct qlcnic_rx_buffer *buffer;
1589 int producer, count = 0;
1590 struct list_head *head;
1592 if (!spin_trylock(&rds_ring->lock))
1593 return;
1595 producer = rds_ring->producer;
1597 head = &rds_ring->free_list;
1598 while (!list_empty(head)) {
1600 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1602 if (!buffer->skb) {
1603 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1604 break;
1607 count++;
1608 list_del(&buffer->list);
1610 /* make a rcv descriptor */
1611 pdesc = &rds_ring->desc_head[producer];
1612 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1613 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1614 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1616 producer = get_next_index(producer, rds_ring->num_desc);
1619 if (count) {
1620 rds_ring->producer = producer;
1621 writel((producer - 1) & (rds_ring->num_desc - 1),
1622 rds_ring->crb_rcv_producer);
1624 spin_unlock(&rds_ring->lock);
1627 static struct qlcnic_rx_buffer *
1628 qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter,
1629 struct qlcnic_host_sds_ring *sds_ring,
1630 int ring, u64 sts_data0)
1632 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1633 struct qlcnic_rx_buffer *buffer;
1634 struct sk_buff *skb;
1635 struct qlcnic_host_rds_ring *rds_ring;
1636 int index, length, cksum, pkt_offset;
1638 if (unlikely(ring >= adapter->max_rds_rings))
1639 return NULL;
1641 rds_ring = &recv_ctx->rds_rings[ring];
1643 index = qlcnic_get_sts_refhandle(sts_data0);
1644 if (unlikely(index >= rds_ring->num_desc))
1645 return NULL;
1647 buffer = &rds_ring->rx_buf_arr[index];
1649 length = qlcnic_get_sts_totallength(sts_data0);
1650 cksum = qlcnic_get_sts_status(sts_data0);
1651 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1653 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1654 if (!skb)
1655 return buffer;
1657 skb_put(skb, rds_ring->skb_size);
1659 if (pkt_offset)
1660 skb_pull(skb, pkt_offset);
1662 skb->truesize = skb->len + sizeof(struct sk_buff);
1664 if (!qlcnic_check_loopback_buff(skb->data))
1665 adapter->diag_cnt++;
1667 dev_kfree_skb_any(skb);
1668 adapter->stats.rx_pkts++;
1669 adapter->stats.rxbytes += length;
1671 return buffer;
1674 void
1675 qlcnic_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1677 struct qlcnic_adapter *adapter = sds_ring->adapter;
1678 struct status_desc *desc;
1679 struct qlcnic_rx_buffer *rxbuf;
1680 u64 sts_data0;
1682 int opcode, ring, desc_cnt;
1683 u32 consumer = sds_ring->consumer;
1685 desc = &sds_ring->desc_head[consumer];
1686 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1688 if (!(sts_data0 & STATUS_OWNER_HOST))
1689 return;
1691 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1692 opcode = qlcnic_get_sts_opcode(sts_data0);
1694 ring = qlcnic_get_sts_type(sts_data0);
1695 rxbuf = qlcnic_process_rcv_diag(adapter, sds_ring,
1696 ring, sts_data0);
1698 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1699 consumer = get_next_index(consumer, sds_ring->num_desc);
1701 sds_ring->consumer = consumer;
1702 writel(consumer, sds_ring->crb_sts_consumer);