Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/genesis-2.6
[linux-3.10.git] / drivers / net / qlcnic / qlcnic_init.c
1 /*
2  * Copyright (C) 2009 - QLogic Corporation.
3  * All rights reserved.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * The full GNU General Public License is included in this distribution
21  * in the file called "COPYING".
22  *
23  */
24
25 #include <linux/netdevice.h>
26 #include <linux/delay.h>
27 #include "qlcnic.h"
28
29 struct crb_addr_pair {
30         u32 addr;
31         u32 data;
32 };
33
34 #define QLCNIC_MAX_CRB_XFORM 60
35 static unsigned int crb_addr_xform[QLCNIC_MAX_CRB_XFORM];
36
37 #define crb_addr_transform(name) \
38         (crb_addr_xform[QLCNIC_HW_PX_MAP_CRB_##name] = \
39         QLCNIC_HW_CRB_HUB_AGT_ADR_##name << 20)
40
41 #define QLCNIC_ADDR_ERROR (0xffffffff)
42
43 static void
44 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
45                 struct qlcnic_host_rds_ring *rds_ring);
46
47 static void crb_addr_transform_setup(void)
48 {
49         crb_addr_transform(XDMA);
50         crb_addr_transform(TIMR);
51         crb_addr_transform(SRE);
52         crb_addr_transform(SQN3);
53         crb_addr_transform(SQN2);
54         crb_addr_transform(SQN1);
55         crb_addr_transform(SQN0);
56         crb_addr_transform(SQS3);
57         crb_addr_transform(SQS2);
58         crb_addr_transform(SQS1);
59         crb_addr_transform(SQS0);
60         crb_addr_transform(RPMX7);
61         crb_addr_transform(RPMX6);
62         crb_addr_transform(RPMX5);
63         crb_addr_transform(RPMX4);
64         crb_addr_transform(RPMX3);
65         crb_addr_transform(RPMX2);
66         crb_addr_transform(RPMX1);
67         crb_addr_transform(RPMX0);
68         crb_addr_transform(ROMUSB);
69         crb_addr_transform(SN);
70         crb_addr_transform(QMN);
71         crb_addr_transform(QMS);
72         crb_addr_transform(PGNI);
73         crb_addr_transform(PGND);
74         crb_addr_transform(PGN3);
75         crb_addr_transform(PGN2);
76         crb_addr_transform(PGN1);
77         crb_addr_transform(PGN0);
78         crb_addr_transform(PGSI);
79         crb_addr_transform(PGSD);
80         crb_addr_transform(PGS3);
81         crb_addr_transform(PGS2);
82         crb_addr_transform(PGS1);
83         crb_addr_transform(PGS0);
84         crb_addr_transform(PS);
85         crb_addr_transform(PH);
86         crb_addr_transform(NIU);
87         crb_addr_transform(I2Q);
88         crb_addr_transform(EG);
89         crb_addr_transform(MN);
90         crb_addr_transform(MS);
91         crb_addr_transform(CAS2);
92         crb_addr_transform(CAS1);
93         crb_addr_transform(CAS0);
94         crb_addr_transform(CAM);
95         crb_addr_transform(C2C1);
96         crb_addr_transform(C2C0);
97         crb_addr_transform(SMB);
98         crb_addr_transform(OCM0);
99         crb_addr_transform(I2C0);
100 }
101
102 void qlcnic_release_rx_buffers(struct qlcnic_adapter *adapter)
103 {
104         struct qlcnic_recv_context *recv_ctx;
105         struct qlcnic_host_rds_ring *rds_ring;
106         struct qlcnic_rx_buffer *rx_buf;
107         int i, ring;
108
109         recv_ctx = &adapter->recv_ctx;
110         for (ring = 0; ring < adapter->max_rds_rings; ring++) {
111                 rds_ring = &recv_ctx->rds_rings[ring];
112                 for (i = 0; i < rds_ring->num_desc; ++i) {
113                         rx_buf = &(rds_ring->rx_buf_arr[i]);
114                         if (rx_buf->state == QLCNIC_BUFFER_FREE)
115                                 continue;
116                         pci_unmap_single(adapter->pdev,
117                                         rx_buf->dma,
118                                         rds_ring->dma_size,
119                                         PCI_DMA_FROMDEVICE);
120                         if (rx_buf->skb != NULL)
121                                 dev_kfree_skb_any(rx_buf->skb);
122                 }
123         }
124 }
125
126 void qlcnic_release_tx_buffers(struct qlcnic_adapter *adapter)
127 {
128         struct qlcnic_cmd_buffer *cmd_buf;
129         struct qlcnic_skb_frag *buffrag;
130         int i, j;
131         struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
132
133         cmd_buf = tx_ring->cmd_buf_arr;
134         for (i = 0; i < tx_ring->num_desc; i++) {
135                 buffrag = cmd_buf->frag_array;
136                 if (buffrag->dma) {
137                         pci_unmap_single(adapter->pdev, buffrag->dma,
138                                          buffrag->length, PCI_DMA_TODEVICE);
139                         buffrag->dma = 0ULL;
140                 }
141                 for (j = 0; j < cmd_buf->frag_count; j++) {
142                         buffrag++;
143                         if (buffrag->dma) {
144                                 pci_unmap_page(adapter->pdev, buffrag->dma,
145                                                buffrag->length,
146                                                PCI_DMA_TODEVICE);
147                                 buffrag->dma = 0ULL;
148                         }
149                 }
150                 if (cmd_buf->skb) {
151                         dev_kfree_skb_any(cmd_buf->skb);
152                         cmd_buf->skb = NULL;
153                 }
154                 cmd_buf++;
155         }
156 }
157
158 void qlcnic_free_sw_resources(struct qlcnic_adapter *adapter)
159 {
160         struct qlcnic_recv_context *recv_ctx;
161         struct qlcnic_host_rds_ring *rds_ring;
162         struct qlcnic_host_tx_ring *tx_ring;
163         int ring;
164
165         recv_ctx = &adapter->recv_ctx;
166
167         if (recv_ctx->rds_rings == NULL)
168                 goto skip_rds;
169
170         for (ring = 0; ring < adapter->max_rds_rings; ring++) {
171                 rds_ring = &recv_ctx->rds_rings[ring];
172                 vfree(rds_ring->rx_buf_arr);
173                 rds_ring->rx_buf_arr = NULL;
174         }
175         kfree(recv_ctx->rds_rings);
176
177 skip_rds:
178         if (adapter->tx_ring == NULL)
179                 return;
180
181         tx_ring = adapter->tx_ring;
182         vfree(tx_ring->cmd_buf_arr);
183         kfree(adapter->tx_ring);
184 }
185
186 int qlcnic_alloc_sw_resources(struct qlcnic_adapter *adapter)
187 {
188         struct qlcnic_recv_context *recv_ctx;
189         struct qlcnic_host_rds_ring *rds_ring;
190         struct qlcnic_host_sds_ring *sds_ring;
191         struct qlcnic_host_tx_ring *tx_ring;
192         struct qlcnic_rx_buffer *rx_buf;
193         int ring, i, size;
194
195         struct qlcnic_cmd_buffer *cmd_buf_arr;
196         struct net_device *netdev = adapter->netdev;
197
198         size = sizeof(struct qlcnic_host_tx_ring);
199         tx_ring = kzalloc(size, GFP_KERNEL);
200         if (tx_ring == NULL) {
201                 dev_err(&netdev->dev, "failed to allocate tx ring struct\n");
202                 return -ENOMEM;
203         }
204         adapter->tx_ring = tx_ring;
205
206         tx_ring->num_desc = adapter->num_txd;
207         tx_ring->txq = netdev_get_tx_queue(netdev, 0);
208
209         cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring));
210         if (cmd_buf_arr == NULL) {
211                 dev_err(&netdev->dev, "failed to allocate cmd buffer ring\n");
212                 return -ENOMEM;
213         }
214         memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
215         tx_ring->cmd_buf_arr = cmd_buf_arr;
216
217         recv_ctx = &adapter->recv_ctx;
218
219         size = adapter->max_rds_rings * sizeof(struct qlcnic_host_rds_ring);
220         rds_ring = kzalloc(size, GFP_KERNEL);
221         if (rds_ring == NULL) {
222                 dev_err(&netdev->dev, "failed to allocate rds ring struct\n");
223                 return -ENOMEM;
224         }
225         recv_ctx->rds_rings = rds_ring;
226
227         for (ring = 0; ring < adapter->max_rds_rings; ring++) {
228                 rds_ring = &recv_ctx->rds_rings[ring];
229                 switch (ring) {
230                 case RCV_RING_NORMAL:
231                         rds_ring->num_desc = adapter->num_rxd;
232                         if (adapter->ahw.cut_through) {
233                                 rds_ring->dma_size =
234                                         QLCNIC_CT_DEFAULT_RX_BUF_LEN;
235                                 rds_ring->skb_size =
236                                         QLCNIC_CT_DEFAULT_RX_BUF_LEN;
237                         } else {
238                                 rds_ring->dma_size =
239                                         QLCNIC_P3_RX_BUF_MAX_LEN;
240                                 rds_ring->skb_size =
241                                         rds_ring->dma_size + NET_IP_ALIGN;
242                         }
243                         break;
244
245                 case RCV_RING_JUMBO:
246                         rds_ring->num_desc = adapter->num_jumbo_rxd;
247                         rds_ring->dma_size =
248                                 QLCNIC_P3_RX_JUMBO_BUF_MAX_LEN;
249
250                         if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
251                                 rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA;
252
253                         rds_ring->skb_size =
254                                 rds_ring->dma_size + NET_IP_ALIGN;
255                         break;
256
257                 case RCV_RING_LRO:
258                         rds_ring->num_desc = adapter->num_lro_rxd;
259                         rds_ring->dma_size = QLCNIC_RX_LRO_BUFFER_LENGTH;
260                         rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
261                         break;
262
263                 }
264                 rds_ring->rx_buf_arr = (struct qlcnic_rx_buffer *)
265                         vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
266                 if (rds_ring->rx_buf_arr == NULL) {
267                         dev_err(&netdev->dev, "Failed to allocate "
268                                 "rx buffer ring %d\n", ring);
269                         goto err_out;
270                 }
271                 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
272                 INIT_LIST_HEAD(&rds_ring->free_list);
273                 /*
274                  * Now go through all of them, set reference handles
275                  * and put them in the queues.
276                  */
277                 rx_buf = rds_ring->rx_buf_arr;
278                 for (i = 0; i < rds_ring->num_desc; i++) {
279                         list_add_tail(&rx_buf->list,
280                                         &rds_ring->free_list);
281                         rx_buf->ref_handle = i;
282                         rx_buf->state = QLCNIC_BUFFER_FREE;
283                         rx_buf++;
284                 }
285                 spin_lock_init(&rds_ring->lock);
286         }
287
288         for (ring = 0; ring < adapter->max_sds_rings; ring++) {
289                 sds_ring = &recv_ctx->sds_rings[ring];
290                 sds_ring->irq = adapter->msix_entries[ring].vector;
291                 sds_ring->adapter = adapter;
292                 sds_ring->num_desc = adapter->num_rxd;
293
294                 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
295                         INIT_LIST_HEAD(&sds_ring->free_list[i]);
296         }
297
298         return 0;
299
300 err_out:
301         qlcnic_free_sw_resources(adapter);
302         return -ENOMEM;
303 }
304
305 /*
306  * Utility to translate from internal Phantom CRB address
307  * to external PCI CRB address.
308  */
309 static u32 qlcnic_decode_crb_addr(u32 addr)
310 {
311         int i;
312         u32 base_addr, offset, pci_base;
313
314         crb_addr_transform_setup();
315
316         pci_base = QLCNIC_ADDR_ERROR;
317         base_addr = addr & 0xfff00000;
318         offset = addr & 0x000fffff;
319
320         for (i = 0; i < QLCNIC_MAX_CRB_XFORM; i++) {
321                 if (crb_addr_xform[i] == base_addr) {
322                         pci_base = i << 20;
323                         break;
324                 }
325         }
326         if (pci_base == QLCNIC_ADDR_ERROR)
327                 return pci_base;
328         else
329                 return pci_base + offset;
330 }
331
332 #define QLCNIC_MAX_ROM_WAIT_USEC        100
333
334 static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter)
335 {
336         long timeout = 0;
337         long done = 0;
338
339         cond_resched();
340
341         while (done == 0) {
342                 done = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_STATUS);
343                 done &= 2;
344                 if (++timeout >= QLCNIC_MAX_ROM_WAIT_USEC) {
345                         dev_err(&adapter->pdev->dev,
346                                 "Timeout reached  waiting for rom done");
347                         return -EIO;
348                 }
349                 udelay(1);
350         }
351         return 0;
352 }
353
354 static int do_rom_fast_read(struct qlcnic_adapter *adapter,
355                             int addr, int *valp)
356 {
357         QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ADDRESS, addr);
358         QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
359         QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 3);
360         QLCWR32(adapter, QLCNIC_ROMUSB_ROM_INSTR_OPCODE, 0xb);
361         if (qlcnic_wait_rom_done(adapter)) {
362                 dev_err(&adapter->pdev->dev, "Error waiting for rom done\n");
363                 return -EIO;
364         }
365         /* reset abyte_cnt and dummy_byte_cnt */
366         QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 0);
367         udelay(10);
368         QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
369
370         *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA);
371         return 0;
372 }
373
374 static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
375                                   u8 *bytes, size_t size)
376 {
377         int addridx;
378         int ret = 0;
379
380         for (addridx = addr; addridx < (addr + size); addridx += 4) {
381                 int v;
382                 ret = do_rom_fast_read(adapter, addridx, &v);
383                 if (ret != 0)
384                         break;
385                 *(__le32 *)bytes = cpu_to_le32(v);
386                 bytes += 4;
387         }
388
389         return ret;
390 }
391
392 int
393 qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
394                                 u8 *bytes, size_t size)
395 {
396         int ret;
397
398         ret = qlcnic_rom_lock(adapter);
399         if (ret < 0)
400                 return ret;
401
402         ret = do_rom_fast_read_words(adapter, addr, bytes, size);
403
404         qlcnic_rom_unlock(adapter);
405         return ret;
406 }
407
408 int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, int addr, int *valp)
409 {
410         int ret;
411
412         if (qlcnic_rom_lock(adapter) != 0)
413                 return -EIO;
414
415         ret = do_rom_fast_read(adapter, addr, valp);
416         qlcnic_rom_unlock(adapter);
417         return ret;
418 }
419
420 int qlcnic_pinit_from_rom(struct qlcnic_adapter *adapter)
421 {
422         int addr, val;
423         int i, n, init_delay;
424         struct crb_addr_pair *buf;
425         unsigned offset;
426         u32 off;
427         struct pci_dev *pdev = adapter->pdev;
428
429         /* resetall */
430         qlcnic_rom_lock(adapter);
431         QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xffffffff);
432         qlcnic_rom_unlock(adapter);
433
434         if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) ||
435                         qlcnic_rom_fast_read(adapter, 4, &n) != 0) {
436                 dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n);
437                 return -EIO;
438         }
439         offset = n & 0xffffU;
440         n = (n >> 16) & 0xffffU;
441
442         if (n >= 1024) {
443                 dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n");
444                 return -EIO;
445         }
446
447         buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
448         if (buf == NULL) {
449                 dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n");
450                 return -ENOMEM;
451         }
452
453         for (i = 0; i < n; i++) {
454                 if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
455                 qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
456                         kfree(buf);
457                         return -EIO;
458                 }
459
460                 buf[i].addr = addr;
461                 buf[i].data = val;
462         }
463
464         for (i = 0; i < n; i++) {
465
466                 off = qlcnic_decode_crb_addr(buf[i].addr);
467                 if (off == QLCNIC_ADDR_ERROR) {
468                         dev_err(&pdev->dev, "CRB init value out of range %x\n",
469                                         buf[i].addr);
470                         continue;
471                 }
472                 off += QLCNIC_PCI_CRBSPACE;
473
474                 if (off & 1)
475                         continue;
476
477                 /* skipping cold reboot MAGIC */
478                 if (off == QLCNIC_CAM_RAM(0x1fc))
479                         continue;
480                 if (off == (QLCNIC_CRB_I2C0 + 0x1c))
481                         continue;
482                 if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */
483                         continue;
484                 if (off == (ROMUSB_GLB + 0xa8))
485                         continue;
486                 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
487                         continue;
488                 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
489                         continue;
490                 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
491                         continue;
492                 if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET)
493                         continue;
494                 /* skip the function enable register */
495                 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION))
496                         continue;
497                 if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION2))
498                         continue;
499                 if ((off & 0x0ff00000) == QLCNIC_CRB_SMB)
500                         continue;
501
502                 init_delay = 1;
503                 /* After writing this register, HW needs time for CRB */
504                 /* to quiet down (else crb_window returns 0xffffffff) */
505                 if (off == QLCNIC_ROMUSB_GLB_SW_RESET)
506                         init_delay = 1000;
507
508                 QLCWR32(adapter, off, buf[i].data);
509
510                 msleep(init_delay);
511         }
512         kfree(buf);
513
514         /* p2dn replyCount */
515         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e);
516         /* disable_peg_cache 0 & 1*/
517         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8);
518         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8);
519
520         /* peg_clr_all */
521         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0);
522         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0);
523         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0);
524         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0);
525         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0);
526         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0);
527         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0);
528         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0);
529         return 0;
530 }
531
532 static int
533 qlcnic_has_mn(struct qlcnic_adapter *adapter)
534 {
535         u32 capability, flashed_ver;
536         capability = 0;
537
538         qlcnic_rom_fast_read(adapter,
539                         QLCNIC_FW_VERSION_OFFSET, (int *)&flashed_ver);
540         flashed_ver = QLCNIC_DECODE_VERSION(flashed_ver);
541
542         if (flashed_ver >= QLCNIC_VERSION_CODE(4, 0, 220)) {
543
544                 capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY);
545                 if (capability & QLCNIC_PEG_TUNE_MN_PRESENT)
546                         return 1;
547         }
548         return 0;
549 }
550
551 static
552 struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section)
553 {
554         u32 i;
555         struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
556         __le32 entries = cpu_to_le32(directory->num_entries);
557
558         for (i = 0; i < entries; i++) {
559
560                 __le32 offs = cpu_to_le32(directory->findex) +
561                                 (i * cpu_to_le32(directory->entry_size));
562                 __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
563
564                 if (tab_type == section)
565                         return (struct uni_table_desc *) &unirom[offs];
566         }
567
568         return NULL;
569 }
570
571 #define FILEHEADER_SIZE (14 * 4)
572
573 static int
574 qlcnic_validate_header(struct qlcnic_adapter *adapter)
575 {
576         const u8 *unirom = adapter->fw->data;
577         struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
578         __le32 fw_file_size = adapter->fw->size;
579         __le32 entries;
580         __le32 entry_size;
581         __le32 tab_size;
582
583         if (fw_file_size < FILEHEADER_SIZE)
584                 return -EINVAL;
585
586         entries = cpu_to_le32(directory->num_entries);
587         entry_size = cpu_to_le32(directory->entry_size);
588         tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
589
590         if (fw_file_size < tab_size)
591                 return -EINVAL;
592
593         return 0;
594 }
595
596 static int
597 qlcnic_validate_bootld(struct qlcnic_adapter *adapter)
598 {
599         struct uni_table_desc *tab_desc;
600         struct uni_data_desc *descr;
601         const u8 *unirom = adapter->fw->data;
602         int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
603                                 QLCNIC_UNI_BOOTLD_IDX_OFF));
604         __le32 offs;
605         __le32 tab_size;
606         __le32 data_size;
607
608         tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD);
609
610         if (!tab_desc)
611                 return -EINVAL;
612
613         tab_size = cpu_to_le32(tab_desc->findex) +
614                         (cpu_to_le32(tab_desc->entry_size * (idx + 1)));
615
616         if (adapter->fw->size < tab_size)
617                 return -EINVAL;
618
619         offs = cpu_to_le32(tab_desc->findex) +
620                 (cpu_to_le32(tab_desc->entry_size) * (idx));
621         descr = (struct uni_data_desc *)&unirom[offs];
622
623         data_size = descr->findex + cpu_to_le32(descr->size);
624
625         if (adapter->fw->size < data_size)
626                 return -EINVAL;
627
628         return 0;
629 }
630
631 static int
632 qlcnic_validate_fw(struct qlcnic_adapter *adapter)
633 {
634         struct uni_table_desc *tab_desc;
635         struct uni_data_desc *descr;
636         const u8 *unirom = adapter->fw->data;
637         int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
638                                 QLCNIC_UNI_FIRMWARE_IDX_OFF));
639         __le32 offs;
640         __le32 tab_size;
641         __le32 data_size;
642
643         tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW);
644
645         if (!tab_desc)
646                 return -EINVAL;
647
648         tab_size = cpu_to_le32(tab_desc->findex) +
649                         (cpu_to_le32(tab_desc->entry_size * (idx + 1)));
650
651         if (adapter->fw->size < tab_size)
652                 return -EINVAL;
653
654         offs = cpu_to_le32(tab_desc->findex) +
655                 (cpu_to_le32(tab_desc->entry_size) * (idx));
656         descr = (struct uni_data_desc *)&unirom[offs];
657         data_size = descr->findex + cpu_to_le32(descr->size);
658
659         if (adapter->fw->size < data_size)
660                 return -EINVAL;
661
662         return 0;
663 }
664
665 static int
666 qlcnic_validate_product_offs(struct qlcnic_adapter *adapter)
667 {
668         struct uni_table_desc *ptab_descr;
669         const u8 *unirom = adapter->fw->data;
670         int mn_present = qlcnic_has_mn(adapter);
671         __le32 entries;
672         __le32 entry_size;
673         __le32 tab_size;
674         u32 i;
675
676         ptab_descr = qlcnic_get_table_desc(unirom,
677                                 QLCNIC_UNI_DIR_SECT_PRODUCT_TBL);
678         if (!ptab_descr)
679                 return -EINVAL;
680
681         entries = cpu_to_le32(ptab_descr->num_entries);
682         entry_size = cpu_to_le32(ptab_descr->entry_size);
683         tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
684
685         if (adapter->fw->size < tab_size)
686                 return -EINVAL;
687
688 nomn:
689         for (i = 0; i < entries; i++) {
690
691                 __le32 flags, file_chiprev, offs;
692                 u8 chiprev = adapter->ahw.revision_id;
693                 u32 flagbit;
694
695                 offs = cpu_to_le32(ptab_descr->findex) +
696                                 (i * cpu_to_le32(ptab_descr->entry_size));
697                 flags = cpu_to_le32(*((int *)&unirom[offs] +
698                                                 QLCNIC_UNI_FLAGS_OFF));
699                 file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
700                                                 QLCNIC_UNI_CHIP_REV_OFF));
701
702                 flagbit = mn_present ? 1 : 2;
703
704                 if ((chiprev == file_chiprev) &&
705                                         ((1ULL << flagbit) & flags)) {
706                         adapter->file_prd_off = offs;
707                         return 0;
708                 }
709         }
710         if (mn_present) {
711                 mn_present = 0;
712                 goto nomn;
713         }
714         return -EINVAL;
715 }
716
717 static int
718 qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter)
719 {
720         if (qlcnic_validate_header(adapter)) {
721                 dev_err(&adapter->pdev->dev,
722                                 "unified image: header validation failed\n");
723                 return -EINVAL;
724         }
725
726         if (qlcnic_validate_product_offs(adapter)) {
727                 dev_err(&adapter->pdev->dev,
728                                 "unified image: product validation failed\n");
729                 return -EINVAL;
730         }
731
732         if (qlcnic_validate_bootld(adapter)) {
733                 dev_err(&adapter->pdev->dev,
734                                 "unified image: bootld validation failed\n");
735                 return -EINVAL;
736         }
737
738         if (qlcnic_validate_fw(adapter)) {
739                 dev_err(&adapter->pdev->dev,
740                                 "unified image: firmware validation failed\n");
741                 return -EINVAL;
742         }
743
744         return 0;
745 }
746
747 static
748 struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter,
749                         u32 section, u32 idx_offset)
750 {
751         const u8 *unirom = adapter->fw->data;
752         int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
753                                                                 idx_offset));
754         struct uni_table_desc *tab_desc;
755         __le32 offs;
756
757         tab_desc = qlcnic_get_table_desc(unirom, section);
758
759         if (tab_desc == NULL)
760                 return NULL;
761
762         offs = cpu_to_le32(tab_desc->findex) +
763                         (cpu_to_le32(tab_desc->entry_size) * idx);
764
765         return (struct uni_data_desc *)&unirom[offs];
766 }
767
768 static u8 *
769 qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter)
770 {
771         u32 offs = QLCNIC_BOOTLD_START;
772
773         if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
774                 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
775                                         QLCNIC_UNI_DIR_SECT_BOOTLD,
776                                         QLCNIC_UNI_BOOTLD_IDX_OFF))->findex);
777
778         return (u8 *)&adapter->fw->data[offs];
779 }
780
781 static u8 *
782 qlcnic_get_fw_offs(struct qlcnic_adapter *adapter)
783 {
784         u32 offs = QLCNIC_IMAGE_START;
785
786         if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
787                 offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
788                                         QLCNIC_UNI_DIR_SECT_FW,
789                                         QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex);
790
791         return (u8 *)&adapter->fw->data[offs];
792 }
793
794 static __le32
795 qlcnic_get_fw_size(struct qlcnic_adapter *adapter)
796 {
797         if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
798                 return cpu_to_le32((qlcnic_get_data_desc(adapter,
799                                         QLCNIC_UNI_DIR_SECT_FW,
800                                         QLCNIC_UNI_FIRMWARE_IDX_OFF))->size);
801         else
802                 return cpu_to_le32(
803                         *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]);
804 }
805
806 static __le32
807 qlcnic_get_fw_version(struct qlcnic_adapter *adapter)
808 {
809         struct uni_data_desc *fw_data_desc;
810         const struct firmware *fw = adapter->fw;
811         __le32 major, minor, sub;
812         const u8 *ver_str;
813         int i, ret;
814
815         if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
816                 return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]);
817
818         fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW,
819                         QLCNIC_UNI_FIRMWARE_IDX_OFF);
820         ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
821                 cpu_to_le32(fw_data_desc->size) - 17;
822
823         for (i = 0; i < 12; i++) {
824                 if (!strncmp(&ver_str[i], "REV=", 4)) {
825                         ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
826                                         &major, &minor, &sub);
827                         if (ret != 3)
828                                 return 0;
829                         else
830                                 return major + (minor << 8) + (sub << 16);
831                 }
832         }
833
834         return 0;
835 }
836
837 static __le32
838 qlcnic_get_bios_version(struct qlcnic_adapter *adapter)
839 {
840         const struct firmware *fw = adapter->fw;
841         __le32 bios_ver, prd_off = adapter->file_prd_off;
842
843         if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
844                 return cpu_to_le32(
845                         *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]);
846
847         bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
848                                 + QLCNIC_UNI_BIOS_VERSION_OFF));
849
850         return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24);
851 }
852
853 int
854 qlcnic_need_fw_reset(struct qlcnic_adapter *adapter)
855 {
856         u32 count, old_count;
857         u32 val, version, major, minor, build;
858         int i, timeout;
859
860         if (adapter->need_fw_reset)
861                 return 1;
862
863         /* last attempt had failed */
864         if (QLCRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED)
865                 return 1;
866
867         old_count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
868
869         for (i = 0; i < 10; i++) {
870
871                 timeout = msleep_interruptible(200);
872                 if (timeout) {
873                         QLCWR32(adapter, CRB_CMDPEG_STATE,
874                                         PHAN_INITIALIZE_FAILED);
875                         return -EINTR;
876                 }
877
878                 count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
879                 if (count != old_count)
880                         break;
881         }
882
883         /* firmware is dead */
884         if (count == old_count)
885                 return 1;
886
887         /* check if we have got newer or different file firmware */
888         if (adapter->fw) {
889
890                 val = qlcnic_get_fw_version(adapter);
891
892                 version = QLCNIC_DECODE_VERSION(val);
893
894                 major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
895                 minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
896                 build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
897
898                 if (version > QLCNIC_VERSION_CODE(major, minor, build))
899                         return 1;
900         }
901
902         return 0;
903 }
904
905 static const char *fw_name[] = {
906         QLCNIC_UNIFIED_ROMIMAGE_NAME,
907         QLCNIC_FLASH_ROMIMAGE_NAME,
908 };
909
910 int
911 qlcnic_load_firmware(struct qlcnic_adapter *adapter)
912 {
913         u64 *ptr64;
914         u32 i, flashaddr, size;
915         const struct firmware *fw = adapter->fw;
916         struct pci_dev *pdev = adapter->pdev;
917
918         dev_info(&pdev->dev, "loading firmware from %s\n",
919                         fw_name[adapter->fw_type]);
920
921         if (fw) {
922                 __le64 data;
923
924                 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
925
926                 ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter);
927                 flashaddr = QLCNIC_BOOTLD_START;
928
929                 for (i = 0; i < size; i++) {
930                         data = cpu_to_le64(ptr64[i]);
931
932                         if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data))
933                                 return -EIO;
934
935                         flashaddr += 8;
936                 }
937
938                 size = (__force u32)qlcnic_get_fw_size(adapter) / 8;
939
940                 ptr64 = (u64 *)qlcnic_get_fw_offs(adapter);
941                 flashaddr = QLCNIC_IMAGE_START;
942
943                 for (i = 0; i < size; i++) {
944                         data = cpu_to_le64(ptr64[i]);
945
946                         if (qlcnic_pci_mem_write_2M(adapter,
947                                                 flashaddr, data))
948                                 return -EIO;
949
950                         flashaddr += 8;
951                 }
952         } else {
953                 u64 data;
954                 u32 hi, lo;
955
956                 size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
957                 flashaddr = QLCNIC_BOOTLD_START;
958
959                 for (i = 0; i < size; i++) {
960                         if (qlcnic_rom_fast_read(adapter,
961                                         flashaddr, (int *)&lo) != 0)
962                                 return -EIO;
963                         if (qlcnic_rom_fast_read(adapter,
964                                         flashaddr + 4, (int *)&hi) != 0)
965                                 return -EIO;
966
967                         data = (((u64)hi << 32) | lo);
968
969                         if (qlcnic_pci_mem_write_2M(adapter,
970                                                 flashaddr, data))
971                                 return -EIO;
972
973                         flashaddr += 8;
974                 }
975         }
976         msleep(1);
977
978         QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020);
979         QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e);
980         return 0;
981 }
982
983 static int
984 qlcnic_validate_firmware(struct qlcnic_adapter *adapter)
985 {
986         __le32 val;
987         u32 ver, min_ver, bios, min_size;
988         struct pci_dev *pdev = adapter->pdev;
989         const struct firmware *fw = adapter->fw;
990         u8 fw_type = adapter->fw_type;
991
992         if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) {
993                 if (qlcnic_validate_unified_romimage(adapter))
994                         return -EINVAL;
995
996                 min_size = QLCNIC_UNI_FW_MIN_SIZE;
997         } else {
998                 val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]);
999                 if ((__force u32)val != QLCNIC_BDINFO_MAGIC)
1000                         return -EINVAL;
1001
1002                 min_size = QLCNIC_FW_MIN_SIZE;
1003         }
1004
1005         if (fw->size < min_size)
1006                 return -EINVAL;
1007
1008         val = qlcnic_get_fw_version(adapter);
1009
1010         min_ver = QLCNIC_VERSION_CODE(4, 0, 216);
1011
1012         ver = QLCNIC_DECODE_VERSION(val);
1013
1014         if ((_major(ver) > _QLCNIC_LINUX_MAJOR) || (ver < min_ver)) {
1015                 dev_err(&pdev->dev,
1016                                 "%s: firmware version %d.%d.%d unsupported\n",
1017                 fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
1018                 return -EINVAL;
1019         }
1020
1021         val = qlcnic_get_bios_version(adapter);
1022         qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios);
1023         if ((__force u32)val != bios) {
1024                 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
1025                                 fw_name[fw_type]);
1026                 return -EINVAL;
1027         }
1028
1029         /* check if flashed firmware is newer */
1030         if (qlcnic_rom_fast_read(adapter,
1031                         QLCNIC_FW_VERSION_OFFSET, (int *)&val))
1032                 return -EIO;
1033
1034         val = QLCNIC_DECODE_VERSION(val);
1035         if (val > ver) {
1036                 dev_info(&pdev->dev, "%s: firmware is older than flash\n",
1037                                 fw_name[fw_type]);
1038                 return -EINVAL;
1039         }
1040
1041         QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
1042         return 0;
1043 }
1044
1045 static void
1046 qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter)
1047 {
1048         u8 fw_type;
1049
1050         switch (adapter->fw_type) {
1051         case QLCNIC_UNKNOWN_ROMIMAGE:
1052                 fw_type = QLCNIC_UNIFIED_ROMIMAGE;
1053                 break;
1054
1055         case QLCNIC_UNIFIED_ROMIMAGE:
1056         default:
1057                 fw_type = QLCNIC_FLASH_ROMIMAGE;
1058                 break;
1059         }
1060
1061         adapter->fw_type = fw_type;
1062 }
1063
1064
1065
1066 void qlcnic_request_firmware(struct qlcnic_adapter *adapter)
1067 {
1068         struct pci_dev *pdev = adapter->pdev;
1069         int rc;
1070
1071         adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE;
1072
1073 next:
1074         qlcnic_get_next_fwtype(adapter);
1075
1076         if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) {
1077                 adapter->fw = NULL;
1078         } else {
1079                 rc = request_firmware(&adapter->fw,
1080                                 fw_name[adapter->fw_type], &pdev->dev);
1081                 if (rc != 0)
1082                         goto next;
1083
1084                 rc = qlcnic_validate_firmware(adapter);
1085                 if (rc != 0) {
1086                         release_firmware(adapter->fw);
1087                         msleep(1);
1088                         goto next;
1089                 }
1090         }
1091 }
1092
1093
1094 void
1095 qlcnic_release_firmware(struct qlcnic_adapter *adapter)
1096 {
1097         if (adapter->fw)
1098                 release_firmware(adapter->fw);
1099         adapter->fw = NULL;
1100 }
1101
1102 int qlcnic_phantom_init(struct qlcnic_adapter *adapter)
1103 {
1104         u32 val;
1105         int retries = 60;
1106
1107         do {
1108                 val = QLCRD32(adapter, CRB_CMDPEG_STATE);
1109
1110                 switch (val) {
1111                 case PHAN_INITIALIZE_COMPLETE:
1112                 case PHAN_INITIALIZE_ACK:
1113                         return 0;
1114                 case PHAN_INITIALIZE_FAILED:
1115                         goto out_err;
1116                 default:
1117                         break;
1118                 }
1119
1120                 msleep(500);
1121
1122         } while (--retries);
1123
1124         QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
1125
1126 out_err:
1127         dev_err(&adapter->pdev->dev, "firmware init failed\n");
1128         return -EIO;
1129 }
1130
1131 static int
1132 qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter)
1133 {
1134         u32 val;
1135         int retries = 2000;
1136
1137         do {
1138                 val = QLCRD32(adapter, CRB_RCVPEG_STATE);
1139
1140                 if (val == PHAN_PEG_RCV_INITIALIZED)
1141                         return 0;
1142
1143                 msleep(10);
1144
1145         } while (--retries);
1146
1147         if (!retries) {
1148                 dev_err(&adapter->pdev->dev, "Receive Peg initialization not "
1149                               "complete, state: 0x%x.\n", val);
1150                 return -EIO;
1151         }
1152
1153         return 0;
1154 }
1155
1156 int qlcnic_init_firmware(struct qlcnic_adapter *adapter)
1157 {
1158         int err;
1159
1160         err = qlcnic_receive_peg_ready(adapter);
1161         if (err)
1162                 return err;
1163
1164         QLCWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
1165         QLCWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
1166         QLCWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
1167         QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
1168
1169         return err;
1170 }
1171
1172 static void
1173 qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1174                                 struct qlcnic_fw_msg *msg)
1175 {
1176         u32 cable_OUI;
1177         u16 cable_len;
1178         u16 link_speed;
1179         u8  link_status, module, duplex, autoneg;
1180         struct net_device *netdev = adapter->netdev;
1181
1182         adapter->has_link_events = 1;
1183
1184         cable_OUI = msg->body[1] & 0xffffffff;
1185         cable_len = (msg->body[1] >> 32) & 0xffff;
1186         link_speed = (msg->body[1] >> 48) & 0xffff;
1187
1188         link_status = msg->body[2] & 0xff;
1189         duplex = (msg->body[2] >> 16) & 0xff;
1190         autoneg = (msg->body[2] >> 24) & 0xff;
1191
1192         module = (msg->body[2] >> 8) & 0xff;
1193         if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
1194                 dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, "
1195                                 "length %d\n", cable_OUI, cable_len);
1196         else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
1197                 dev_info(&netdev->dev, "unsupported cable length %d\n",
1198                                 cable_len);
1199
1200         qlcnic_advert_link_change(adapter, link_status);
1201
1202         if (duplex == LINKEVENT_FULL_DUPLEX)
1203                 adapter->link_duplex = DUPLEX_FULL;
1204         else
1205                 adapter->link_duplex = DUPLEX_HALF;
1206
1207         adapter->module_type = module;
1208         adapter->link_autoneg = autoneg;
1209         adapter->link_speed = link_speed;
1210 }
1211
1212 static void
1213 qlcnic_handle_fw_message(int desc_cnt, int index,
1214                 struct qlcnic_host_sds_ring *sds_ring)
1215 {
1216         struct qlcnic_fw_msg msg;
1217         struct status_desc *desc;
1218         int i = 0, opcode;
1219
1220         while (desc_cnt > 0 && i < 8) {
1221                 desc = &sds_ring->desc_head[index];
1222                 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1223                 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1224
1225                 index = get_next_index(index, sds_ring->num_desc);
1226                 desc_cnt--;
1227         }
1228
1229         opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
1230         switch (opcode) {
1231         case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1232                 qlcnic_handle_linkevent(sds_ring->adapter, &msg);
1233                 break;
1234         default:
1235                 break;
1236         }
1237 }
1238
1239 static int
1240 qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
1241                 struct qlcnic_host_rds_ring *rds_ring,
1242                 struct qlcnic_rx_buffer *buffer)
1243 {
1244         struct sk_buff *skb;
1245         dma_addr_t dma;
1246         struct pci_dev *pdev = adapter->pdev;
1247
1248         buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1249         if (!buffer->skb) {
1250                 adapter->stats.skb_alloc_failure++;
1251                 return -ENOMEM;
1252         }
1253
1254         skb = buffer->skb;
1255
1256         if (!adapter->ahw.cut_through)
1257                 skb_reserve(skb, 2);
1258
1259         dma = pci_map_single(pdev, skb->data,
1260                         rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1261
1262         if (pci_dma_mapping_error(pdev, dma)) {
1263                 dev_kfree_skb_any(skb);
1264                 buffer->skb = NULL;
1265                 return -ENOMEM;
1266         }
1267
1268         buffer->skb = skb;
1269         buffer->dma = dma;
1270         buffer->state = QLCNIC_BUFFER_BUSY;
1271
1272         return 0;
1273 }
1274
1275 static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1276                 struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum)
1277 {
1278         struct qlcnic_rx_buffer *buffer;
1279         struct sk_buff *skb;
1280
1281         buffer = &rds_ring->rx_buf_arr[index];
1282
1283         pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1284                         PCI_DMA_FROMDEVICE);
1285
1286         skb = buffer->skb;
1287         if (!skb)
1288                 goto no_skb;
1289
1290         if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1291                 adapter->stats.csummed++;
1292                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1293         } else {
1294                 skb->ip_summed = CHECKSUM_NONE;
1295         }
1296
1297         skb->dev = adapter->netdev;
1298
1299         buffer->skb = NULL;
1300 no_skb:
1301         buffer->state = QLCNIC_BUFFER_FREE;
1302         return skb;
1303 }
1304
1305 static struct qlcnic_rx_buffer *
1306 qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1307                 struct qlcnic_host_sds_ring *sds_ring,
1308                 int ring, u64 sts_data0)
1309 {
1310         struct net_device *netdev = adapter->netdev;
1311         struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1312         struct qlcnic_rx_buffer *buffer;
1313         struct sk_buff *skb;
1314         struct qlcnic_host_rds_ring *rds_ring;
1315         int index, length, cksum, pkt_offset;
1316
1317         if (unlikely(ring >= adapter->max_rds_rings))
1318                 return NULL;
1319
1320         rds_ring = &recv_ctx->rds_rings[ring];
1321
1322         index = qlcnic_get_sts_refhandle(sts_data0);
1323         if (unlikely(index >= rds_ring->num_desc))
1324                 return NULL;
1325
1326         buffer = &rds_ring->rx_buf_arr[index];
1327
1328         length = qlcnic_get_sts_totallength(sts_data0);
1329         cksum  = qlcnic_get_sts_status(sts_data0);
1330         pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1331
1332         skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1333         if (!skb)
1334                 return buffer;
1335
1336         if (length > rds_ring->skb_size)
1337                 skb_put(skb, rds_ring->skb_size);
1338         else
1339                 skb_put(skb, length);
1340
1341         if (pkt_offset)
1342                 skb_pull(skb, pkt_offset);
1343
1344         skb->truesize = skb->len + sizeof(struct sk_buff);
1345         skb->protocol = eth_type_trans(skb, netdev);
1346
1347         napi_gro_receive(&sds_ring->napi, skb);
1348
1349         adapter->stats.rx_pkts++;
1350         adapter->stats.rxbytes += length;
1351
1352         return buffer;
1353 }
1354
1355 #define QLC_TCP_HDR_SIZE            20
1356 #define QLC_TCP_TS_OPTION_SIZE      12
1357 #define QLC_TCP_TS_HDR_SIZE         (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1358
1359 static struct qlcnic_rx_buffer *
1360 qlcnic_process_lro(struct qlcnic_adapter *adapter,
1361                 struct qlcnic_host_sds_ring *sds_ring,
1362                 int ring, u64 sts_data0, u64 sts_data1)
1363 {
1364         struct net_device *netdev = adapter->netdev;
1365         struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1366         struct qlcnic_rx_buffer *buffer;
1367         struct sk_buff *skb;
1368         struct qlcnic_host_rds_ring *rds_ring;
1369         struct iphdr *iph;
1370         struct tcphdr *th;
1371         bool push, timestamp;
1372         int l2_hdr_offset, l4_hdr_offset;
1373         int index;
1374         u16 lro_length, length, data_offset;
1375         u32 seq_number;
1376
1377         if (unlikely(ring > adapter->max_rds_rings))
1378                 return NULL;
1379
1380         rds_ring = &recv_ctx->rds_rings[ring];
1381
1382         index = qlcnic_get_lro_sts_refhandle(sts_data0);
1383         if (unlikely(index > rds_ring->num_desc))
1384                 return NULL;
1385
1386         buffer = &rds_ring->rx_buf_arr[index];
1387
1388         timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1389         lro_length = qlcnic_get_lro_sts_length(sts_data0);
1390         l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1391         l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1392         push = qlcnic_get_lro_sts_push_flag(sts_data0);
1393         seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1394
1395         skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1396         if (!skb)
1397                 return buffer;
1398
1399         if (timestamp)
1400                 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1401         else
1402                 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1403
1404         skb_put(skb, lro_length + data_offset);
1405
1406         skb->truesize = skb->len + sizeof(struct sk_buff) + skb_headroom(skb);
1407
1408         skb_pull(skb, l2_hdr_offset);
1409         skb->protocol = eth_type_trans(skb, netdev);
1410
1411         iph = (struct iphdr *)skb->data;
1412         th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1413
1414         length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1415         iph->tot_len = htons(length);
1416         iph->check = 0;
1417         iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1418         th->psh = push;
1419         th->seq = htonl(seq_number);
1420
1421         length = skb->len;
1422
1423         netif_receive_skb(skb);
1424
1425         adapter->stats.lro_pkts++;
1426         adapter->stats.lrobytes += length;
1427
1428         return buffer;
1429 }
1430
1431 int
1432 qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1433 {
1434         struct qlcnic_adapter *adapter = sds_ring->adapter;
1435         struct list_head *cur;
1436         struct status_desc *desc;
1437         struct qlcnic_rx_buffer *rxbuf;
1438         u64 sts_data0, sts_data1;
1439
1440         int count = 0;
1441         int opcode, ring, desc_cnt;
1442         u32 consumer = sds_ring->consumer;
1443
1444         while (count < max) {
1445                 desc = &sds_ring->desc_head[consumer];
1446                 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1447
1448                 if (!(sts_data0 & STATUS_OWNER_HOST))
1449                         break;
1450
1451                 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1452                 opcode = qlcnic_get_sts_opcode(sts_data0);
1453
1454                 switch (opcode) {
1455                 case QLCNIC_RXPKT_DESC:
1456                 case QLCNIC_OLD_RXPKT_DESC:
1457                 case QLCNIC_SYN_OFFLOAD:
1458                         ring = qlcnic_get_sts_type(sts_data0);
1459                         rxbuf = qlcnic_process_rcv(adapter, sds_ring,
1460                                         ring, sts_data0);
1461                         break;
1462                 case QLCNIC_LRO_DESC:
1463                         ring = qlcnic_get_lro_sts_type(sts_data0);
1464                         sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1465                         rxbuf = qlcnic_process_lro(adapter, sds_ring,
1466                                         ring, sts_data0, sts_data1);
1467                         break;
1468                 case QLCNIC_RESPONSE_DESC:
1469                         qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1470                 default:
1471                         goto skip;
1472                 }
1473
1474                 WARN_ON(desc_cnt > 1);
1475
1476                 if (rxbuf)
1477                         list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1478
1479 skip:
1480                 for (; desc_cnt > 0; desc_cnt--) {
1481                         desc = &sds_ring->desc_head[consumer];
1482                         desc->status_desc_data[0] =
1483                                 cpu_to_le64(STATUS_OWNER_PHANTOM);
1484                         consumer = get_next_index(consumer, sds_ring->num_desc);
1485                 }
1486                 count++;
1487         }
1488
1489         for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1490                 struct qlcnic_host_rds_ring *rds_ring =
1491                         &adapter->recv_ctx.rds_rings[ring];
1492
1493                 if (!list_empty(&sds_ring->free_list[ring])) {
1494                         list_for_each(cur, &sds_ring->free_list[ring]) {
1495                                 rxbuf = list_entry(cur,
1496                                                 struct qlcnic_rx_buffer, list);
1497                                 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1498                         }
1499                         spin_lock(&rds_ring->lock);
1500                         list_splice_tail_init(&sds_ring->free_list[ring],
1501                                                 &rds_ring->free_list);
1502                         spin_unlock(&rds_ring->lock);
1503                 }
1504
1505                 qlcnic_post_rx_buffers_nodb(adapter, rds_ring);
1506         }
1507
1508         if (count) {
1509                 sds_ring->consumer = consumer;
1510                 writel(consumer, sds_ring->crb_sts_consumer);
1511         }
1512
1513         return count;
1514 }
1515
1516 void
1517 qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter, u32 ringid,
1518         struct qlcnic_host_rds_ring *rds_ring)
1519 {
1520         struct rcv_desc *pdesc;
1521         struct qlcnic_rx_buffer *buffer;
1522         int producer, count = 0;
1523         struct list_head *head;
1524
1525         producer = rds_ring->producer;
1526
1527         spin_lock(&rds_ring->lock);
1528         head = &rds_ring->free_list;
1529         while (!list_empty(head)) {
1530
1531                 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1532
1533                 if (!buffer->skb) {
1534                         if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1535                                 break;
1536                 }
1537
1538                 count++;
1539                 list_del(&buffer->list);
1540
1541                 /* make a rcv descriptor  */
1542                 pdesc = &rds_ring->desc_head[producer];
1543                 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1544                 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1545                 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1546
1547                 producer = get_next_index(producer, rds_ring->num_desc);
1548         }
1549         spin_unlock(&rds_ring->lock);
1550
1551         if (count) {
1552                 rds_ring->producer = producer;
1553                 writel((producer-1) & (rds_ring->num_desc-1),
1554                                 rds_ring->crb_rcv_producer);
1555         }
1556 }
1557
1558 static void
1559 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
1560                 struct qlcnic_host_rds_ring *rds_ring)
1561 {
1562         struct rcv_desc *pdesc;
1563         struct qlcnic_rx_buffer *buffer;
1564         int producer, count = 0;
1565         struct list_head *head;
1566
1567         producer = rds_ring->producer;
1568         if (!spin_trylock(&rds_ring->lock))
1569                 return;
1570
1571         head = &rds_ring->free_list;
1572         while (!list_empty(head)) {
1573
1574                 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1575
1576                 if (!buffer->skb) {
1577                         if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1578                                 break;
1579                 }
1580
1581                 count++;
1582                 list_del(&buffer->list);
1583
1584                 /* make a rcv descriptor  */
1585                 pdesc = &rds_ring->desc_head[producer];
1586                 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1587                 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1588                 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1589
1590                 producer = get_next_index(producer, rds_ring->num_desc);
1591         }
1592
1593         if (count) {
1594                 rds_ring->producer = producer;
1595                 writel((producer - 1) & (rds_ring->num_desc - 1),
1596                                 rds_ring->crb_rcv_producer);
1597         }
1598         spin_unlock(&rds_ring->lock);
1599 }
1600
1601 static struct qlcnic_rx_buffer *
1602 qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter,
1603                 struct qlcnic_host_sds_ring *sds_ring,
1604                 int ring, u64 sts_data0)
1605 {
1606         struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1607         struct qlcnic_rx_buffer *buffer;
1608         struct sk_buff *skb;
1609         struct qlcnic_host_rds_ring *rds_ring;
1610         int index, length, cksum, pkt_offset;
1611
1612         if (unlikely(ring >= adapter->max_rds_rings))
1613                 return NULL;
1614
1615         rds_ring = &recv_ctx->rds_rings[ring];
1616
1617         index = qlcnic_get_sts_refhandle(sts_data0);
1618         if (unlikely(index >= rds_ring->num_desc))
1619                 return NULL;
1620
1621         buffer = &rds_ring->rx_buf_arr[index];
1622
1623         length = qlcnic_get_sts_totallength(sts_data0);
1624         cksum  = qlcnic_get_sts_status(sts_data0);
1625         pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1626
1627         skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1628         if (!skb)
1629                 return buffer;
1630
1631         skb_put(skb, rds_ring->skb_size);
1632
1633         if (pkt_offset)
1634                 skb_pull(skb, pkt_offset);
1635
1636         skb->truesize = skb->len + sizeof(struct sk_buff);
1637
1638         if (!qlcnic_check_loopback_buff(skb->data))
1639                 adapter->diag_cnt++;
1640
1641         dev_kfree_skb_any(skb);
1642         adapter->stats.rx_pkts++;
1643         adapter->stats.rxbytes += length;
1644
1645         return buffer;
1646 }
1647
1648 void
1649 qlcnic_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1650 {
1651         struct qlcnic_adapter *adapter = sds_ring->adapter;
1652         struct status_desc *desc;
1653         struct qlcnic_rx_buffer *rxbuf;
1654         u64 sts_data0;
1655
1656         int opcode, ring, desc_cnt;
1657         u32 consumer = sds_ring->consumer;
1658
1659         desc = &sds_ring->desc_head[consumer];
1660         sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1661
1662         if (!(sts_data0 & STATUS_OWNER_HOST))
1663                 return;
1664
1665         desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1666         opcode = qlcnic_get_sts_opcode(sts_data0);
1667
1668         ring = qlcnic_get_sts_type(sts_data0);
1669         rxbuf = qlcnic_process_rcv_diag(adapter, sds_ring,
1670                                         ring, sts_data0);
1671
1672         desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1673         consumer = get_next_index(consumer, sds_ring->num_desc);
1674
1675         sds_ring->consumer = consumer;
1676         writel(consumer, sds_ring->crb_sts_consumer);
1677 }