qlcnic: additional driver statistics
[linux-2.6/libata-dev.git] / drivers / net / qlcnic / qlcnic_init.c
blob9ef9f58f84dd7c35e6bbf4e1d2a13806973853c7
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 return -ENOMEM;
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 return -ENOMEM;
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 if (adapter->ahw.cut_through) {
234 rds_ring->dma_size =
235 QLCNIC_CT_DEFAULT_RX_BUF_LEN;
236 rds_ring->skb_size =
237 QLCNIC_CT_DEFAULT_RX_BUF_LEN;
238 } else {
239 rds_ring->dma_size =
240 QLCNIC_P3_RX_BUF_MAX_LEN;
241 rds_ring->skb_size =
242 rds_ring->dma_size + NET_IP_ALIGN;
244 break;
246 case RCV_RING_JUMBO:
247 rds_ring->num_desc = adapter->num_jumbo_rxd;
248 rds_ring->dma_size =
249 QLCNIC_P3_RX_JUMBO_BUF_MAX_LEN;
251 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
252 rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA;
254 rds_ring->skb_size =
255 rds_ring->dma_size + NET_IP_ALIGN;
256 break;
258 case RCV_RING_LRO:
259 rds_ring->num_desc = adapter->num_lro_rxd;
260 rds_ring->dma_size = QLCNIC_RX_LRO_BUFFER_LENGTH;
261 rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
262 break;
265 rds_ring->rx_buf_arr = (struct qlcnic_rx_buffer *)
266 vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
267 if (rds_ring->rx_buf_arr == NULL) {
268 dev_err(&netdev->dev, "Failed to allocate "
269 "rx buffer ring %d\n", ring);
270 goto err_out;
272 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
273 INIT_LIST_HEAD(&rds_ring->free_list);
275 * Now go through all of them, set reference handles
276 * and put them in the queues.
278 rx_buf = rds_ring->rx_buf_arr;
279 for (i = 0; i < rds_ring->num_desc; i++) {
280 list_add_tail(&rx_buf->list,
281 &rds_ring->free_list);
282 rx_buf->ref_handle = i;
283 rx_buf->state = QLCNIC_BUFFER_FREE;
284 rx_buf++;
286 spin_lock_init(&rds_ring->lock);
289 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
290 sds_ring = &recv_ctx->sds_rings[ring];
291 sds_ring->irq = adapter->msix_entries[ring].vector;
292 sds_ring->adapter = adapter;
293 sds_ring->num_desc = adapter->num_rxd;
295 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
296 INIT_LIST_HEAD(&sds_ring->free_list[i]);
299 return 0;
301 err_out:
302 qlcnic_free_sw_resources(adapter);
303 return -ENOMEM;
307 * Utility to translate from internal Phantom CRB address
308 * to external PCI CRB address.
310 static u32 qlcnic_decode_crb_addr(u32 addr)
312 int i;
313 u32 base_addr, offset, pci_base;
315 crb_addr_transform_setup();
317 pci_base = QLCNIC_ADDR_ERROR;
318 base_addr = addr & 0xfff00000;
319 offset = addr & 0x000fffff;
321 for (i = 0; i < QLCNIC_MAX_CRB_XFORM; i++) {
322 if (crb_addr_xform[i] == base_addr) {
323 pci_base = i << 20;
324 break;
327 if (pci_base == QLCNIC_ADDR_ERROR)
328 return pci_base;
329 else
330 return pci_base + offset;
333 #define QLCNIC_MAX_ROM_WAIT_USEC 100
335 static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter)
337 long timeout = 0;
338 long done = 0;
340 cond_resched();
342 while (done == 0) {
343 done = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_STATUS);
344 done &= 2;
345 if (++timeout >= QLCNIC_MAX_ROM_WAIT_USEC) {
346 dev_err(&adapter->pdev->dev,
347 "Timeout reached waiting for rom done");
348 return -EIO;
350 udelay(1);
352 return 0;
355 static int do_rom_fast_read(struct qlcnic_adapter *adapter,
356 int addr, int *valp)
358 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ADDRESS, addr);
359 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
360 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 3);
361 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_INSTR_OPCODE, 0xb);
362 if (qlcnic_wait_rom_done(adapter)) {
363 dev_err(&adapter->pdev->dev, "Error waiting for rom done\n");
364 return -EIO;
366 /* reset abyte_cnt and dummy_byte_cnt */
367 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 0);
368 udelay(10);
369 QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
371 *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA);
372 return 0;
375 static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
376 u8 *bytes, size_t size)
378 int addridx;
379 int ret = 0;
381 for (addridx = addr; addridx < (addr + size); addridx += 4) {
382 int v;
383 ret = do_rom_fast_read(adapter, addridx, &v);
384 if (ret != 0)
385 break;
386 *(__le32 *)bytes = cpu_to_le32(v);
387 bytes += 4;
390 return ret;
394 qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
395 u8 *bytes, size_t size)
397 int ret;
399 ret = qlcnic_rom_lock(adapter);
400 if (ret < 0)
401 return ret;
403 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
405 qlcnic_rom_unlock(adapter);
406 return ret;
409 int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, int addr, int *valp)
411 int ret;
413 if (qlcnic_rom_lock(adapter) != 0)
414 return -EIO;
416 ret = do_rom_fast_read(adapter, addr, valp);
417 qlcnic_rom_unlock(adapter);
418 return ret;
421 int qlcnic_pinit_from_rom(struct qlcnic_adapter *adapter)
423 int addr, val;
424 int i, n, init_delay;
425 struct crb_addr_pair *buf;
426 unsigned offset;
427 u32 off;
428 struct pci_dev *pdev = adapter->pdev;
430 /* resetall */
431 qlcnic_rom_lock(adapter);
432 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xffffffff);
433 qlcnic_rom_unlock(adapter);
435 if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) ||
436 qlcnic_rom_fast_read(adapter, 4, &n) != 0) {
437 dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n);
438 return -EIO;
440 offset = n & 0xffffU;
441 n = (n >> 16) & 0xffffU;
443 if (n >= 1024) {
444 dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n");
445 return -EIO;
448 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
449 if (buf == NULL) {
450 dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n");
451 return -ENOMEM;
454 for (i = 0; i < n; i++) {
455 if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
456 qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
457 kfree(buf);
458 return -EIO;
461 buf[i].addr = addr;
462 buf[i].data = val;
465 for (i = 0; i < n; i++) {
467 off = qlcnic_decode_crb_addr(buf[i].addr);
468 if (off == QLCNIC_ADDR_ERROR) {
469 dev_err(&pdev->dev, "CRB init value out of range %x\n",
470 buf[i].addr);
471 continue;
473 off += QLCNIC_PCI_CRBSPACE;
475 if (off & 1)
476 continue;
478 /* skipping cold reboot MAGIC */
479 if (off == QLCNIC_CAM_RAM(0x1fc))
480 continue;
481 if (off == (QLCNIC_CRB_I2C0 + 0x1c))
482 continue;
483 if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */
484 continue;
485 if (off == (ROMUSB_GLB + 0xa8))
486 continue;
487 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
488 continue;
489 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
490 continue;
491 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
492 continue;
493 if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET)
494 continue;
495 /* skip the function enable register */
496 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION))
497 continue;
498 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION2))
499 continue;
500 if ((off & 0x0ff00000) == QLCNIC_CRB_SMB)
501 continue;
503 init_delay = 1;
504 /* After writing this register, HW needs time for CRB */
505 /* to quiet down (else crb_window returns 0xffffffff) */
506 if (off == QLCNIC_ROMUSB_GLB_SW_RESET)
507 init_delay = 1000;
509 QLCWR32(adapter, off, buf[i].data);
511 msleep(init_delay);
513 kfree(buf);
515 /* p2dn replyCount */
516 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e);
517 /* disable_peg_cache 0 & 1*/
518 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8);
519 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8);
521 /* peg_clr_all */
522 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0);
523 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0);
524 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0);
525 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0);
526 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0);
527 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0);
528 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0);
529 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0);
530 return 0;
533 void
534 qlcnic_setup_idc_param(struct qlcnic_adapter *adapter) {
536 int timeo;
538 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DEV_INIT_TIMEOUT, &timeo))
539 timeo = 30;
541 adapter->dev_init_timeo = timeo;
543 if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DRV_RESET_TIMEOUT, &timeo))
544 timeo = 10;
546 adapter->reset_ack_timeo = timeo;
549 static int
550 qlcnic_has_mn(struct qlcnic_adapter *adapter)
552 u32 capability, flashed_ver;
553 capability = 0;
555 qlcnic_rom_fast_read(adapter,
556 QLCNIC_FW_VERSION_OFFSET, (int *)&flashed_ver);
557 flashed_ver = QLCNIC_DECODE_VERSION(flashed_ver);
559 if (flashed_ver >= QLCNIC_VERSION_CODE(4, 0, 220)) {
561 capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY);
562 if (capability & QLCNIC_PEG_TUNE_MN_PRESENT)
563 return 1;
565 return 0;
568 static
569 struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section)
571 u32 i;
572 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
573 __le32 entries = cpu_to_le32(directory->num_entries);
575 for (i = 0; i < entries; i++) {
577 __le32 offs = cpu_to_le32(directory->findex) +
578 (i * cpu_to_le32(directory->entry_size));
579 __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
581 if (tab_type == section)
582 return (struct uni_table_desc *) &unirom[offs];
585 return NULL;
588 #define FILEHEADER_SIZE (14 * 4)
590 static int
591 qlcnic_validate_header(struct qlcnic_adapter *adapter)
593 const u8 *unirom = adapter->fw->data;
594 struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
595 __le32 fw_file_size = adapter->fw->size;
596 __le32 entries;
597 __le32 entry_size;
598 __le32 tab_size;
600 if (fw_file_size < FILEHEADER_SIZE)
601 return -EINVAL;
603 entries = cpu_to_le32(directory->num_entries);
604 entry_size = cpu_to_le32(directory->entry_size);
605 tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
607 if (fw_file_size < tab_size)
608 return -EINVAL;
610 return 0;
613 static int
614 qlcnic_validate_bootld(struct qlcnic_adapter *adapter)
616 struct uni_table_desc *tab_desc;
617 struct uni_data_desc *descr;
618 const u8 *unirom = adapter->fw->data;
619 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
620 QLCNIC_UNI_BOOTLD_IDX_OFF));
621 __le32 offs;
622 __le32 tab_size;
623 __le32 data_size;
625 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD);
627 if (!tab_desc)
628 return -EINVAL;
630 tab_size = cpu_to_le32(tab_desc->findex) +
631 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
633 if (adapter->fw->size < tab_size)
634 return -EINVAL;
636 offs = cpu_to_le32(tab_desc->findex) +
637 (cpu_to_le32(tab_desc->entry_size) * (idx));
638 descr = (struct uni_data_desc *)&unirom[offs];
640 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
642 if (adapter->fw->size < data_size)
643 return -EINVAL;
645 return 0;
648 static int
649 qlcnic_validate_fw(struct qlcnic_adapter *adapter)
651 struct uni_table_desc *tab_desc;
652 struct uni_data_desc *descr;
653 const u8 *unirom = adapter->fw->data;
654 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
655 QLCNIC_UNI_FIRMWARE_IDX_OFF));
656 __le32 offs;
657 __le32 tab_size;
658 __le32 data_size;
660 tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW);
662 if (!tab_desc)
663 return -EINVAL;
665 tab_size = cpu_to_le32(tab_desc->findex) +
666 (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
668 if (adapter->fw->size < tab_size)
669 return -EINVAL;
671 offs = cpu_to_le32(tab_desc->findex) +
672 (cpu_to_le32(tab_desc->entry_size) * (idx));
673 descr = (struct uni_data_desc *)&unirom[offs];
674 data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
676 if (adapter->fw->size < data_size)
677 return -EINVAL;
679 return 0;
682 static int
683 qlcnic_validate_product_offs(struct qlcnic_adapter *adapter)
685 struct uni_table_desc *ptab_descr;
686 const u8 *unirom = adapter->fw->data;
687 int mn_present = qlcnic_has_mn(adapter);
688 __le32 entries;
689 __le32 entry_size;
690 __le32 tab_size;
691 u32 i;
693 ptab_descr = qlcnic_get_table_desc(unirom,
694 QLCNIC_UNI_DIR_SECT_PRODUCT_TBL);
695 if (!ptab_descr)
696 return -EINVAL;
698 entries = cpu_to_le32(ptab_descr->num_entries);
699 entry_size = cpu_to_le32(ptab_descr->entry_size);
700 tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
702 if (adapter->fw->size < tab_size)
703 return -EINVAL;
705 nomn:
706 for (i = 0; i < entries; i++) {
708 __le32 flags, file_chiprev, offs;
709 u8 chiprev = adapter->ahw.revision_id;
710 u32 flagbit;
712 offs = cpu_to_le32(ptab_descr->findex) +
713 (i * cpu_to_le32(ptab_descr->entry_size));
714 flags = cpu_to_le32(*((int *)&unirom[offs] +
715 QLCNIC_UNI_FLAGS_OFF));
716 file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
717 QLCNIC_UNI_CHIP_REV_OFF));
719 flagbit = mn_present ? 1 : 2;
721 if ((chiprev == file_chiprev) &&
722 ((1ULL << flagbit) & flags)) {
723 adapter->file_prd_off = offs;
724 return 0;
727 if (mn_present) {
728 mn_present = 0;
729 goto nomn;
731 return -EINVAL;
734 static int
735 qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter)
737 if (qlcnic_validate_header(adapter)) {
738 dev_err(&adapter->pdev->dev,
739 "unified image: header validation failed\n");
740 return -EINVAL;
743 if (qlcnic_validate_product_offs(adapter)) {
744 dev_err(&adapter->pdev->dev,
745 "unified image: product validation failed\n");
746 return -EINVAL;
749 if (qlcnic_validate_bootld(adapter)) {
750 dev_err(&adapter->pdev->dev,
751 "unified image: bootld validation failed\n");
752 return -EINVAL;
755 if (qlcnic_validate_fw(adapter)) {
756 dev_err(&adapter->pdev->dev,
757 "unified image: firmware validation failed\n");
758 return -EINVAL;
761 return 0;
764 static
765 struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter,
766 u32 section, u32 idx_offset)
768 const u8 *unirom = adapter->fw->data;
769 int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
770 idx_offset));
771 struct uni_table_desc *tab_desc;
772 __le32 offs;
774 tab_desc = qlcnic_get_table_desc(unirom, section);
776 if (tab_desc == NULL)
777 return NULL;
779 offs = cpu_to_le32(tab_desc->findex) +
780 (cpu_to_le32(tab_desc->entry_size) * idx);
782 return (struct uni_data_desc *)&unirom[offs];
785 static u8 *
786 qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter)
788 u32 offs = QLCNIC_BOOTLD_START;
790 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
791 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
792 QLCNIC_UNI_DIR_SECT_BOOTLD,
793 QLCNIC_UNI_BOOTLD_IDX_OFF))->findex);
795 return (u8 *)&adapter->fw->data[offs];
798 static u8 *
799 qlcnic_get_fw_offs(struct qlcnic_adapter *adapter)
801 u32 offs = QLCNIC_IMAGE_START;
803 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
804 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
805 QLCNIC_UNI_DIR_SECT_FW,
806 QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex);
808 return (u8 *)&adapter->fw->data[offs];
811 static __le32
812 qlcnic_get_fw_size(struct qlcnic_adapter *adapter)
814 if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
815 return cpu_to_le32((qlcnic_get_data_desc(adapter,
816 QLCNIC_UNI_DIR_SECT_FW,
817 QLCNIC_UNI_FIRMWARE_IDX_OFF))->size);
818 else
819 return cpu_to_le32(
820 *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]);
823 static __le32
824 qlcnic_get_fw_version(struct qlcnic_adapter *adapter)
826 struct uni_data_desc *fw_data_desc;
827 const struct firmware *fw = adapter->fw;
828 __le32 major, minor, sub;
829 const u8 *ver_str;
830 int i, ret;
832 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
833 return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]);
835 fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW,
836 QLCNIC_UNI_FIRMWARE_IDX_OFF);
837 ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
838 cpu_to_le32(fw_data_desc->size) - 17;
840 for (i = 0; i < 12; i++) {
841 if (!strncmp(&ver_str[i], "REV=", 4)) {
842 ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
843 &major, &minor, &sub);
844 if (ret != 3)
845 return 0;
846 else
847 return major + (minor << 8) + (sub << 16);
851 return 0;
854 static __le32
855 qlcnic_get_bios_version(struct qlcnic_adapter *adapter)
857 const struct firmware *fw = adapter->fw;
858 __le32 bios_ver, prd_off = adapter->file_prd_off;
860 if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
861 return cpu_to_le32(
862 *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]);
864 bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
865 + QLCNIC_UNI_BIOS_VERSION_OFF));
867 return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24);
871 qlcnic_need_fw_reset(struct qlcnic_adapter *adapter)
873 u32 count, old_count;
874 u32 val, version, major, minor, build;
875 int i, timeout;
877 if (adapter->need_fw_reset)
878 return 1;
880 /* last attempt had failed */
881 if (QLCRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED)
882 return 1;
884 old_count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
886 for (i = 0; i < 10; i++) {
888 timeout = msleep_interruptible(200);
889 if (timeout) {
890 QLCWR32(adapter, CRB_CMDPEG_STATE,
891 PHAN_INITIALIZE_FAILED);
892 return -EINTR;
895 count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
896 if (count != old_count)
897 break;
900 /* firmware is dead */
901 if (count == old_count)
902 return 1;
904 /* check if we have got newer or different file firmware */
905 if (adapter->fw) {
907 val = qlcnic_get_fw_version(adapter);
909 version = QLCNIC_DECODE_VERSION(val);
911 major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
912 minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
913 build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
915 if (version > QLCNIC_VERSION_CODE(major, minor, build))
916 return 1;
919 return 0;
922 static const char *fw_name[] = {
923 QLCNIC_UNIFIED_ROMIMAGE_NAME,
924 QLCNIC_FLASH_ROMIMAGE_NAME,
928 qlcnic_load_firmware(struct qlcnic_adapter *adapter)
930 u64 *ptr64;
931 u32 i, flashaddr, size;
932 const struct firmware *fw = adapter->fw;
933 struct pci_dev *pdev = adapter->pdev;
935 dev_info(&pdev->dev, "loading firmware from %s\n",
936 fw_name[adapter->fw_type]);
938 if (fw) {
939 __le64 data;
941 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
943 ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter);
944 flashaddr = QLCNIC_BOOTLD_START;
946 for (i = 0; i < size; i++) {
947 data = cpu_to_le64(ptr64[i]);
949 if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data))
950 return -EIO;
952 flashaddr += 8;
955 size = (__force u32)qlcnic_get_fw_size(adapter) / 8;
957 ptr64 = (u64 *)qlcnic_get_fw_offs(adapter);
958 flashaddr = QLCNIC_IMAGE_START;
960 for (i = 0; i < size; i++) {
961 data = cpu_to_le64(ptr64[i]);
963 if (qlcnic_pci_mem_write_2M(adapter,
964 flashaddr, data))
965 return -EIO;
967 flashaddr += 8;
970 size = (__force u32)qlcnic_get_fw_size(adapter) % 8;
971 if (size) {
972 data = cpu_to_le64(ptr64[i]);
974 if (qlcnic_pci_mem_write_2M(adapter,
975 flashaddr, data))
976 return -EIO;
979 } else {
980 u64 data;
981 u32 hi, lo;
983 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
984 flashaddr = QLCNIC_BOOTLD_START;
986 for (i = 0; i < size; i++) {
987 if (qlcnic_rom_fast_read(adapter,
988 flashaddr, (int *)&lo) != 0)
989 return -EIO;
990 if (qlcnic_rom_fast_read(adapter,
991 flashaddr + 4, (int *)&hi) != 0)
992 return -EIO;
994 data = (((u64)hi << 32) | lo);
996 if (qlcnic_pci_mem_write_2M(adapter,
997 flashaddr, data))
998 return -EIO;
1000 flashaddr += 8;
1003 msleep(1);
1005 QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020);
1006 QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e);
1007 return 0;
1010 static int
1011 qlcnic_validate_firmware(struct qlcnic_adapter *adapter)
1013 __le32 val;
1014 u32 ver, min_ver, bios, min_size;
1015 struct pci_dev *pdev = adapter->pdev;
1016 const struct firmware *fw = adapter->fw;
1017 u8 fw_type = adapter->fw_type;
1019 if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) {
1020 if (qlcnic_validate_unified_romimage(adapter))
1021 return -EINVAL;
1023 min_size = QLCNIC_UNI_FW_MIN_SIZE;
1024 } else {
1025 val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]);
1026 if ((__force u32)val != QLCNIC_BDINFO_MAGIC)
1027 return -EINVAL;
1029 min_size = QLCNIC_FW_MIN_SIZE;
1032 if (fw->size < min_size)
1033 return -EINVAL;
1035 val = qlcnic_get_fw_version(adapter);
1037 min_ver = QLCNIC_VERSION_CODE(4, 0, 216);
1039 ver = QLCNIC_DECODE_VERSION(val);
1041 if ((_major(ver) > _QLCNIC_LINUX_MAJOR) || (ver < min_ver)) {
1042 dev_err(&pdev->dev,
1043 "%s: firmware version %d.%d.%d unsupported\n",
1044 fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
1045 return -EINVAL;
1048 val = qlcnic_get_bios_version(adapter);
1049 qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios);
1050 if ((__force u32)val != bios) {
1051 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
1052 fw_name[fw_type]);
1053 return -EINVAL;
1056 /* check if flashed firmware is newer */
1057 if (qlcnic_rom_fast_read(adapter,
1058 QLCNIC_FW_VERSION_OFFSET, (int *)&val))
1059 return -EIO;
1061 val = QLCNIC_DECODE_VERSION(val);
1062 if (val > ver) {
1063 dev_info(&pdev->dev, "%s: firmware is older than flash\n",
1064 fw_name[fw_type]);
1065 return -EINVAL;
1068 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
1069 return 0;
1072 static void
1073 qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter)
1075 u8 fw_type;
1077 switch (adapter->fw_type) {
1078 case QLCNIC_UNKNOWN_ROMIMAGE:
1079 fw_type = QLCNIC_UNIFIED_ROMIMAGE;
1080 break;
1082 case QLCNIC_UNIFIED_ROMIMAGE:
1083 default:
1084 fw_type = QLCNIC_FLASH_ROMIMAGE;
1085 break;
1088 adapter->fw_type = fw_type;
1093 void qlcnic_request_firmware(struct qlcnic_adapter *adapter)
1095 struct pci_dev *pdev = adapter->pdev;
1096 int rc;
1098 adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE;
1100 next:
1101 qlcnic_get_next_fwtype(adapter);
1103 if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) {
1104 adapter->fw = NULL;
1105 } else {
1106 rc = request_firmware(&adapter->fw,
1107 fw_name[adapter->fw_type], &pdev->dev);
1108 if (rc != 0)
1109 goto next;
1111 rc = qlcnic_validate_firmware(adapter);
1112 if (rc != 0) {
1113 release_firmware(adapter->fw);
1114 msleep(1);
1115 goto next;
1121 void
1122 qlcnic_release_firmware(struct qlcnic_adapter *adapter)
1124 if (adapter->fw)
1125 release_firmware(adapter->fw);
1126 adapter->fw = NULL;
1129 int qlcnic_phantom_init(struct qlcnic_adapter *adapter)
1131 u32 val;
1132 int retries = 60;
1134 do {
1135 val = QLCRD32(adapter, CRB_CMDPEG_STATE);
1137 switch (val) {
1138 case PHAN_INITIALIZE_COMPLETE:
1139 case PHAN_INITIALIZE_ACK:
1140 return 0;
1141 case PHAN_INITIALIZE_FAILED:
1142 goto out_err;
1143 default:
1144 break;
1147 msleep(500);
1149 } while (--retries);
1151 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
1153 out_err:
1154 dev_err(&adapter->pdev->dev, "firmware init failed\n");
1155 return -EIO;
1158 static int
1159 qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter)
1161 u32 val;
1162 int retries = 2000;
1164 do {
1165 val = QLCRD32(adapter, CRB_RCVPEG_STATE);
1167 if (val == PHAN_PEG_RCV_INITIALIZED)
1168 return 0;
1170 msleep(10);
1172 } while (--retries);
1174 if (!retries) {
1175 dev_err(&adapter->pdev->dev, "Receive Peg initialization not "
1176 "complete, state: 0x%x.\n", val);
1177 return -EIO;
1180 return 0;
1183 int qlcnic_init_firmware(struct qlcnic_adapter *adapter)
1185 int err;
1187 err = qlcnic_receive_peg_ready(adapter);
1188 if (err)
1189 return err;
1191 QLCWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
1192 QLCWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
1193 QLCWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
1194 QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
1196 return err;
1199 static void
1200 qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1201 struct qlcnic_fw_msg *msg)
1203 u32 cable_OUI;
1204 u16 cable_len;
1205 u16 link_speed;
1206 u8 link_status, module, duplex, autoneg;
1207 struct net_device *netdev = adapter->netdev;
1209 adapter->has_link_events = 1;
1211 cable_OUI = msg->body[1] & 0xffffffff;
1212 cable_len = (msg->body[1] >> 32) & 0xffff;
1213 link_speed = (msg->body[1] >> 48) & 0xffff;
1215 link_status = msg->body[2] & 0xff;
1216 duplex = (msg->body[2] >> 16) & 0xff;
1217 autoneg = (msg->body[2] >> 24) & 0xff;
1219 module = (msg->body[2] >> 8) & 0xff;
1220 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
1221 dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, "
1222 "length %d\n", cable_OUI, cable_len);
1223 else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
1224 dev_info(&netdev->dev, "unsupported cable length %d\n",
1225 cable_len);
1227 qlcnic_advert_link_change(adapter, link_status);
1229 if (duplex == LINKEVENT_FULL_DUPLEX)
1230 adapter->link_duplex = DUPLEX_FULL;
1231 else
1232 adapter->link_duplex = DUPLEX_HALF;
1234 adapter->module_type = module;
1235 adapter->link_autoneg = autoneg;
1236 adapter->link_speed = link_speed;
1239 static void
1240 qlcnic_handle_fw_message(int desc_cnt, int index,
1241 struct qlcnic_host_sds_ring *sds_ring)
1243 struct qlcnic_fw_msg msg;
1244 struct status_desc *desc;
1245 int i = 0, opcode;
1247 while (desc_cnt > 0 && i < 8) {
1248 desc = &sds_ring->desc_head[index];
1249 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1250 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1252 index = get_next_index(index, sds_ring->num_desc);
1253 desc_cnt--;
1256 opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
1257 switch (opcode) {
1258 case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1259 qlcnic_handle_linkevent(sds_ring->adapter, &msg);
1260 break;
1261 default:
1262 break;
1266 static int
1267 qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
1268 struct qlcnic_host_rds_ring *rds_ring,
1269 struct qlcnic_rx_buffer *buffer)
1271 struct sk_buff *skb;
1272 dma_addr_t dma;
1273 struct pci_dev *pdev = adapter->pdev;
1275 buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1276 if (!buffer->skb) {
1277 adapter->stats.skb_alloc_failure++;
1278 return -ENOMEM;
1281 skb = buffer->skb;
1283 if (!adapter->ahw.cut_through)
1284 skb_reserve(skb, 2);
1286 dma = pci_map_single(pdev, skb->data,
1287 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1289 if (pci_dma_mapping_error(pdev, dma)) {
1290 adapter->stats.rx_dma_map_error++;
1291 dev_kfree_skb_any(skb);
1292 buffer->skb = NULL;
1293 return -ENOMEM;
1296 buffer->skb = skb;
1297 buffer->dma = dma;
1298 buffer->state = QLCNIC_BUFFER_BUSY;
1300 return 0;
1303 static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1304 struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum)
1306 struct qlcnic_rx_buffer *buffer;
1307 struct sk_buff *skb;
1309 buffer = &rds_ring->rx_buf_arr[index];
1311 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1312 PCI_DMA_FROMDEVICE);
1314 skb = buffer->skb;
1315 if (!skb) {
1316 adapter->stats.null_skb++;
1317 goto no_skb;
1320 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1321 adapter->stats.csummed++;
1322 skb->ip_summed = CHECKSUM_UNNECESSARY;
1323 } else {
1324 skb->ip_summed = CHECKSUM_NONE;
1327 skb->dev = adapter->netdev;
1329 buffer->skb = NULL;
1330 no_skb:
1331 buffer->state = QLCNIC_BUFFER_FREE;
1332 return skb;
1335 static struct qlcnic_rx_buffer *
1336 qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1337 struct qlcnic_host_sds_ring *sds_ring,
1338 int ring, u64 sts_data0)
1340 struct net_device *netdev = adapter->netdev;
1341 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1342 struct qlcnic_rx_buffer *buffer;
1343 struct sk_buff *skb;
1344 struct qlcnic_host_rds_ring *rds_ring;
1345 int index, length, cksum, pkt_offset;
1347 if (unlikely(ring >= adapter->max_rds_rings))
1348 return NULL;
1350 rds_ring = &recv_ctx->rds_rings[ring];
1352 index = qlcnic_get_sts_refhandle(sts_data0);
1353 if (unlikely(index >= rds_ring->num_desc))
1354 return NULL;
1356 buffer = &rds_ring->rx_buf_arr[index];
1358 length = qlcnic_get_sts_totallength(sts_data0);
1359 cksum = qlcnic_get_sts_status(sts_data0);
1360 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1362 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1363 if (!skb)
1364 return buffer;
1366 if (length > rds_ring->skb_size)
1367 skb_put(skb, rds_ring->skb_size);
1368 else
1369 skb_put(skb, length);
1371 if (pkt_offset)
1372 skb_pull(skb, pkt_offset);
1374 skb->truesize = skb->len + sizeof(struct sk_buff);
1375 skb->protocol = eth_type_trans(skb, netdev);
1377 napi_gro_receive(&sds_ring->napi, skb);
1379 adapter->stats.rx_pkts++;
1380 adapter->stats.rxbytes += length;
1382 return buffer;
1385 #define QLC_TCP_HDR_SIZE 20
1386 #define QLC_TCP_TS_OPTION_SIZE 12
1387 #define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1389 static struct qlcnic_rx_buffer *
1390 qlcnic_process_lro(struct qlcnic_adapter *adapter,
1391 struct qlcnic_host_sds_ring *sds_ring,
1392 int ring, u64 sts_data0, u64 sts_data1)
1394 struct net_device *netdev = adapter->netdev;
1395 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1396 struct qlcnic_rx_buffer *buffer;
1397 struct sk_buff *skb;
1398 struct qlcnic_host_rds_ring *rds_ring;
1399 struct iphdr *iph;
1400 struct tcphdr *th;
1401 bool push, timestamp;
1402 int l2_hdr_offset, l4_hdr_offset;
1403 int index;
1404 u16 lro_length, length, data_offset;
1405 u32 seq_number;
1407 if (unlikely(ring > adapter->max_rds_rings))
1408 return NULL;
1410 rds_ring = &recv_ctx->rds_rings[ring];
1412 index = qlcnic_get_lro_sts_refhandle(sts_data0);
1413 if (unlikely(index > rds_ring->num_desc))
1414 return NULL;
1416 buffer = &rds_ring->rx_buf_arr[index];
1418 timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1419 lro_length = qlcnic_get_lro_sts_length(sts_data0);
1420 l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1421 l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1422 push = qlcnic_get_lro_sts_push_flag(sts_data0);
1423 seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1425 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1426 if (!skb)
1427 return buffer;
1429 if (timestamp)
1430 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1431 else
1432 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1434 skb_put(skb, lro_length + data_offset);
1436 skb->truesize = skb->len + sizeof(struct sk_buff) + skb_headroom(skb);
1438 skb_pull(skb, l2_hdr_offset);
1439 skb->protocol = eth_type_trans(skb, netdev);
1441 iph = (struct iphdr *)skb->data;
1442 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1444 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1445 iph->tot_len = htons(length);
1446 iph->check = 0;
1447 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1448 th->psh = push;
1449 th->seq = htonl(seq_number);
1451 length = skb->len;
1453 netif_receive_skb(skb);
1455 adapter->stats.lro_pkts++;
1456 adapter->stats.lrobytes += length;
1458 return buffer;
1462 qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1464 struct qlcnic_adapter *adapter = sds_ring->adapter;
1465 struct list_head *cur;
1466 struct status_desc *desc;
1467 struct qlcnic_rx_buffer *rxbuf;
1468 u64 sts_data0, sts_data1;
1470 int count = 0;
1471 int opcode, ring, desc_cnt;
1472 u32 consumer = sds_ring->consumer;
1474 while (count < max) {
1475 desc = &sds_ring->desc_head[consumer];
1476 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1478 if (!(sts_data0 & STATUS_OWNER_HOST))
1479 break;
1481 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1482 opcode = qlcnic_get_sts_opcode(sts_data0);
1484 switch (opcode) {
1485 case QLCNIC_RXPKT_DESC:
1486 case QLCNIC_OLD_RXPKT_DESC:
1487 case QLCNIC_SYN_OFFLOAD:
1488 ring = qlcnic_get_sts_type(sts_data0);
1489 rxbuf = qlcnic_process_rcv(adapter, sds_ring,
1490 ring, sts_data0);
1491 break;
1492 case QLCNIC_LRO_DESC:
1493 ring = qlcnic_get_lro_sts_type(sts_data0);
1494 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1495 rxbuf = qlcnic_process_lro(adapter, sds_ring,
1496 ring, sts_data0, sts_data1);
1497 break;
1498 case QLCNIC_RESPONSE_DESC:
1499 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1500 default:
1501 goto skip;
1504 WARN_ON(desc_cnt > 1);
1506 if (rxbuf)
1507 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1508 else
1509 adapter->stats.null_rxbuf++;
1511 skip:
1512 for (; desc_cnt > 0; desc_cnt--) {
1513 desc = &sds_ring->desc_head[consumer];
1514 desc->status_desc_data[0] =
1515 cpu_to_le64(STATUS_OWNER_PHANTOM);
1516 consumer = get_next_index(consumer, sds_ring->num_desc);
1518 count++;
1521 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1522 struct qlcnic_host_rds_ring *rds_ring =
1523 &adapter->recv_ctx.rds_rings[ring];
1525 if (!list_empty(&sds_ring->free_list[ring])) {
1526 list_for_each(cur, &sds_ring->free_list[ring]) {
1527 rxbuf = list_entry(cur,
1528 struct qlcnic_rx_buffer, list);
1529 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1531 spin_lock(&rds_ring->lock);
1532 list_splice_tail_init(&sds_ring->free_list[ring],
1533 &rds_ring->free_list);
1534 spin_unlock(&rds_ring->lock);
1537 qlcnic_post_rx_buffers_nodb(adapter, rds_ring);
1540 if (count) {
1541 sds_ring->consumer = consumer;
1542 writel(consumer, sds_ring->crb_sts_consumer);
1545 return count;
1548 void
1549 qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter, u32 ringid,
1550 struct qlcnic_host_rds_ring *rds_ring)
1552 struct rcv_desc *pdesc;
1553 struct qlcnic_rx_buffer *buffer;
1554 int producer, count = 0;
1555 struct list_head *head;
1557 producer = rds_ring->producer;
1559 spin_lock(&rds_ring->lock);
1560 head = &rds_ring->free_list;
1561 while (!list_empty(head)) {
1563 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1565 if (!buffer->skb) {
1566 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1567 break;
1570 count++;
1571 list_del(&buffer->list);
1573 /* make a rcv descriptor */
1574 pdesc = &rds_ring->desc_head[producer];
1575 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1576 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1577 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1579 producer = get_next_index(producer, rds_ring->num_desc);
1581 spin_unlock(&rds_ring->lock);
1583 if (count) {
1584 rds_ring->producer = producer;
1585 writel((producer-1) & (rds_ring->num_desc-1),
1586 rds_ring->crb_rcv_producer);
1590 static void
1591 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
1592 struct qlcnic_host_rds_ring *rds_ring)
1594 struct rcv_desc *pdesc;
1595 struct qlcnic_rx_buffer *buffer;
1596 int producer, count = 0;
1597 struct list_head *head;
1599 producer = rds_ring->producer;
1600 if (!spin_trylock(&rds_ring->lock))
1601 return;
1603 head = &rds_ring->free_list;
1604 while (!list_empty(head)) {
1606 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1608 if (!buffer->skb) {
1609 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1610 break;
1613 count++;
1614 list_del(&buffer->list);
1616 /* make a rcv descriptor */
1617 pdesc = &rds_ring->desc_head[producer];
1618 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1619 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1620 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1622 producer = get_next_index(producer, rds_ring->num_desc);
1625 if (count) {
1626 rds_ring->producer = producer;
1627 writel((producer - 1) & (rds_ring->num_desc - 1),
1628 rds_ring->crb_rcv_producer);
1630 spin_unlock(&rds_ring->lock);
1633 static struct qlcnic_rx_buffer *
1634 qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter,
1635 struct qlcnic_host_sds_ring *sds_ring,
1636 int ring, u64 sts_data0)
1638 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1639 struct qlcnic_rx_buffer *buffer;
1640 struct sk_buff *skb;
1641 struct qlcnic_host_rds_ring *rds_ring;
1642 int index, length, cksum, pkt_offset;
1644 if (unlikely(ring >= adapter->max_rds_rings))
1645 return NULL;
1647 rds_ring = &recv_ctx->rds_rings[ring];
1649 index = qlcnic_get_sts_refhandle(sts_data0);
1650 if (unlikely(index >= rds_ring->num_desc))
1651 return NULL;
1653 buffer = &rds_ring->rx_buf_arr[index];
1655 length = qlcnic_get_sts_totallength(sts_data0);
1656 cksum = qlcnic_get_sts_status(sts_data0);
1657 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1659 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1660 if (!skb)
1661 return buffer;
1663 skb_put(skb, rds_ring->skb_size);
1665 if (pkt_offset)
1666 skb_pull(skb, pkt_offset);
1668 skb->truesize = skb->len + sizeof(struct sk_buff);
1670 if (!qlcnic_check_loopback_buff(skb->data))
1671 adapter->diag_cnt++;
1673 dev_kfree_skb_any(skb);
1674 adapter->stats.rx_pkts++;
1675 adapter->stats.rxbytes += length;
1677 return buffer;
1680 void
1681 qlcnic_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1683 struct qlcnic_adapter *adapter = sds_ring->adapter;
1684 struct status_desc *desc;
1685 struct qlcnic_rx_buffer *rxbuf;
1686 u64 sts_data0;
1688 int opcode, ring, desc_cnt;
1689 u32 consumer = sds_ring->consumer;
1691 desc = &sds_ring->desc_head[consumer];
1692 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1694 if (!(sts_data0 & STATUS_OWNER_HOST))
1695 return;
1697 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1698 opcode = qlcnic_get_sts_opcode(sts_data0);
1700 ring = qlcnic_get_sts_type(sts_data0);
1701 rxbuf = qlcnic_process_rcv_diag(adapter, sds_ring,
1702 ring, sts_data0);
1704 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1705 consumer = get_next_index(consumer, sds_ring->num_desc);
1707 sds_ring->consumer = consumer;
1708 writel(consumer, sds_ring->crb_sts_consumer);