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