1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/types.h>
34 #include <asm/byteorder.h>
35 #include <linux/io.h>
36 #include <linux/delay.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/list.h>
41 #include <linux/pci.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include "qed.h"
46 #include "qed_cxt.h"
47 #include "qed_dev_api.h"
48 #include "qed_hsi.h"
49 #include "qed_hw.h"
50 #include "qed_int.h"
51 #include "qed_iscsi.h"
52 #include "qed_mcp.h"
53 #include "qed_ooo.h"
54 #include "qed_reg_addr.h"
55 #include "qed_sp.h"
56 #include "qed_sriov.h"
57 #include "qed_rdma.h"
58 
59 /***************************************************************************
60 * Structures & Definitions
61 ***************************************************************************/
62 
63 #define SPQ_HIGH_PRI_RESERVE_DEFAULT    (1)
64 
65 #define SPQ_BLOCK_DELAY_MAX_ITER        (10)
66 #define SPQ_BLOCK_DELAY_US              (10)
67 #define SPQ_BLOCK_SLEEP_MAX_ITER        (1000)
68 #define SPQ_BLOCK_SLEEP_MS              (5)
69 
70 /***************************************************************************
71 * Blocking Imp. (BLOCK/EBLOCK mode)
72 ***************************************************************************/
73 static void qed_spq_blocking_cb(struct qed_hwfn *p_hwfn,
74 				void *cookie,
75 				union event_ring_data *data, u8 fw_return_code)
76 {
77 	struct qed_spq_comp_done *comp_done;
78 
79 	comp_done = (struct qed_spq_comp_done *)cookie;
80 
81 	comp_done->fw_return_code = fw_return_code;
82 
83 	/* Make sure completion done is visible on waiting thread */
84 	smp_store_release(&comp_done->done, 0x1);
85 }
86 
87 static int __qed_spq_block(struct qed_hwfn *p_hwfn,
88 			   struct qed_spq_entry *p_ent,
89 			   u8 *p_fw_ret, bool sleep_between_iter)
90 {
91 	struct qed_spq_comp_done *comp_done;
92 	u32 iter_cnt;
93 
94 	comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
95 	iter_cnt = sleep_between_iter ? SPQ_BLOCK_SLEEP_MAX_ITER
96 				      : SPQ_BLOCK_DELAY_MAX_ITER;
97 
98 	while (iter_cnt--) {
99 		/* Validate we receive completion update */
100 		if (smp_load_acquire(&comp_done->done) == 1) { /* ^^^ */
101 			if (p_fw_ret)
102 				*p_fw_ret = comp_done->fw_return_code;
103 			return 0;
104 		}
105 
106 		if (sleep_between_iter)
107 			msleep(SPQ_BLOCK_SLEEP_MS);
108 		else
109 			udelay(SPQ_BLOCK_DELAY_US);
110 	}
111 
112 	return -EBUSY;
113 }
114 
115 static int qed_spq_block(struct qed_hwfn *p_hwfn,
116 			 struct qed_spq_entry *p_ent,
117 			 u8 *p_fw_ret, bool skip_quick_poll)
118 {
119 	struct qed_spq_comp_done *comp_done;
120 	struct qed_ptt *p_ptt;
121 	int rc;
122 
123 	/* A relatively short polling period w/o sleeping, to allow the FW to
124 	 * complete the ramrod and thus possibly to avoid the following sleeps.
125 	 */
126 	if (!skip_quick_poll) {
127 		rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, false);
128 		if (!rc)
129 			return 0;
130 	}
131 
132 	/* Move to polling with a sleeping period between iterations */
133 	rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
134 	if (!rc)
135 		return 0;
136 
137 	p_ptt = qed_ptt_acquire(p_hwfn);
138 	if (!p_ptt) {
139 		DP_NOTICE(p_hwfn, "ptt, failed to acquire\n");
140 		return -EAGAIN;
141 	}
142 
143 	DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
144 	rc = qed_mcp_drain(p_hwfn, p_ptt);
145 	qed_ptt_release(p_hwfn, p_ptt);
146 	if (rc) {
147 		DP_NOTICE(p_hwfn, "MCP drain failed\n");
148 		goto err;
149 	}
150 
151 	/* Retry after drain */
152 	rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
153 	if (!rc)
154 		return 0;
155 
156 	comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
157 	if (comp_done->done == 1) {
158 		if (p_fw_ret)
159 			*p_fw_ret = comp_done->fw_return_code;
160 		return 0;
161 	}
162 err:
163 	DP_NOTICE(p_hwfn,
164 		  "Ramrod is stuck [CID %08x cmd %02x protocol %02x echo %04x]\n",
165 		  le32_to_cpu(p_ent->elem.hdr.cid),
166 		  p_ent->elem.hdr.cmd_id,
167 		  p_ent->elem.hdr.protocol_id,
168 		  le16_to_cpu(p_ent->elem.hdr.echo));
169 
170 	return -EBUSY;
171 }
172 
173 /***************************************************************************
174 * SPQ entries inner API
175 ***************************************************************************/
176 static int qed_spq_fill_entry(struct qed_hwfn *p_hwfn,
177 			      struct qed_spq_entry *p_ent)
178 {
179 	p_ent->flags = 0;
180 
181 	switch (p_ent->comp_mode) {
182 	case QED_SPQ_MODE_EBLOCK:
183 	case QED_SPQ_MODE_BLOCK:
184 		p_ent->comp_cb.function = qed_spq_blocking_cb;
185 		break;
186 	case QED_SPQ_MODE_CB:
187 		break;
188 	default:
189 		DP_NOTICE(p_hwfn, "Unknown SPQE completion mode %d\n",
190 			  p_ent->comp_mode);
191 		return -EINVAL;
192 	}
193 
194 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
195 		   "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x] Data pointer: [%08x:%08x] Completion Mode: %s\n",
196 		   p_ent->elem.hdr.cid,
197 		   p_ent->elem.hdr.cmd_id,
198 		   p_ent->elem.hdr.protocol_id,
199 		   p_ent->elem.data_ptr.hi,
200 		   p_ent->elem.data_ptr.lo,
201 		   D_TRINE(p_ent->comp_mode, QED_SPQ_MODE_EBLOCK,
202 			   QED_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
203 			   "MODE_CB"));
204 
205 	return 0;
206 }
207 
208 /***************************************************************************
209 * HSI access
210 ***************************************************************************/
211 static void qed_spq_hw_initialize(struct qed_hwfn *p_hwfn,
212 				  struct qed_spq *p_spq)
213 {
214 	struct e4_core_conn_context *p_cxt;
215 	struct qed_cxt_info cxt_info;
216 	u16 physical_q;
217 	int rc;
218 
219 	cxt_info.iid = p_spq->cid;
220 
221 	rc = qed_cxt_get_cid_info(p_hwfn, &cxt_info);
222 
223 	if (rc < 0) {
224 		DP_NOTICE(p_hwfn, "Cannot find context info for cid=%d\n",
225 			  p_spq->cid);
226 		return;
227 	}
228 
229 	p_cxt = cxt_info.p_cxt;
230 
231 	SET_FIELD(p_cxt->xstorm_ag_context.flags10,
232 		  E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
233 	SET_FIELD(p_cxt->xstorm_ag_context.flags1,
234 		  E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
235 	SET_FIELD(p_cxt->xstorm_ag_context.flags9,
236 		  E4_XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
237 
238 	/* QM physical queue */
239 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
240 	p_cxt->xstorm_ag_context.physical_q0 = cpu_to_le16(physical_q);
241 
242 	p_cxt->xstorm_st_context.spq_base_lo =
243 		DMA_LO_LE(p_spq->chain.p_phys_addr);
244 	p_cxt->xstorm_st_context.spq_base_hi =
245 		DMA_HI_LE(p_spq->chain.p_phys_addr);
246 
247 	DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
248 		       p_hwfn->p_consq->chain.p_phys_addr);
249 }
250 
251 static int qed_spq_hw_post(struct qed_hwfn *p_hwfn,
252 			   struct qed_spq *p_spq, struct qed_spq_entry *p_ent)
253 {
254 	struct qed_chain *p_chain = &p_hwfn->p_spq->chain;
255 	struct core_db_data *p_db_data = &p_spq->db_data;
256 	u16 echo = qed_chain_get_prod_idx(p_chain);
257 	struct slow_path_element	*elem;
258 
259 	p_ent->elem.hdr.echo	= cpu_to_le16(echo);
260 	elem = qed_chain_produce(p_chain);
261 	if (!elem) {
262 		DP_NOTICE(p_hwfn, "Failed to produce from SPQ chain\n");
263 		return -EINVAL;
264 	}
265 
266 	*elem = p_ent->elem; /* struct assignment */
267 
268 	/* send a doorbell on the slow hwfn session */
269 	p_db_data->spq_prod = cpu_to_le16(qed_chain_get_prod_idx(p_chain));
270 
271 	/* make sure the SPQE is updated before the doorbell */
272 	wmb();
273 
274 	DOORBELL(p_hwfn, p_spq->db_addr_offset, *(u32 *)p_db_data);
275 
276 	/* make sure doorbell is rang */
277 	wmb();
278 
279 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
280 		   "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
281 		   p_spq->db_addr_offset,
282 		   p_spq->cid,
283 		   p_db_data->params,
284 		   p_db_data->agg_flags, qed_chain_get_prod_idx(p_chain));
285 
286 	return 0;
287 }
288 
289 /***************************************************************************
290 * Asynchronous events
291 ***************************************************************************/
292 static int
293 qed_async_event_completion(struct qed_hwfn *p_hwfn,
294 			   struct event_ring_entry *p_eqe)
295 {
296 	qed_spq_async_comp_cb cb;
297 
298 	if (!p_hwfn->p_spq || (p_eqe->protocol_id >= MAX_PROTOCOL_TYPE))
299 		return -EINVAL;
300 
301 	cb = p_hwfn->p_spq->async_comp_cb[p_eqe->protocol_id];
302 	if (cb) {
303 		return cb(p_hwfn, p_eqe->opcode, p_eqe->echo,
304 			  &p_eqe->data, p_eqe->fw_return_code);
305 	} else {
306 		DP_NOTICE(p_hwfn,
307 			  "Unknown Async completion for protocol: %d\n",
308 			  p_eqe->protocol_id);
309 		return -EINVAL;
310 	}
311 }
312 
313 int
314 qed_spq_register_async_cb(struct qed_hwfn *p_hwfn,
315 			  enum protocol_type protocol_id,
316 			  qed_spq_async_comp_cb cb)
317 {
318 	if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
319 		return -EINVAL;
320 
321 	p_hwfn->p_spq->async_comp_cb[protocol_id] = cb;
322 	return 0;
323 }
324 
325 void
326 qed_spq_unregister_async_cb(struct qed_hwfn *p_hwfn,
327 			    enum protocol_type protocol_id)
328 {
329 	if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
330 		return;
331 
332 	p_hwfn->p_spq->async_comp_cb[protocol_id] = NULL;
333 }
334 
335 /***************************************************************************
336 * EQ API
337 ***************************************************************************/
338 void qed_eq_prod_update(struct qed_hwfn *p_hwfn, u16 prod)
339 {
340 	u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
341 		   USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
342 
343 	REG_WR16(p_hwfn, addr, prod);
344 
345 	/* keep prod updates ordered */
346 	mmiowb();
347 }
348 
349 int qed_eq_completion(struct qed_hwfn *p_hwfn, void *cookie)
350 {
351 	struct qed_eq *p_eq = cookie;
352 	struct qed_chain *p_chain = &p_eq->chain;
353 	int rc = 0;
354 
355 	/* take a snapshot of the FW consumer */
356 	u16 fw_cons_idx = le16_to_cpu(*p_eq->p_fw_cons);
357 
358 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
359 
360 	/* Need to guarantee the fw_cons index we use points to a usuable
361 	 * element (to comply with our chain), so our macros would comply
362 	 */
363 	if ((fw_cons_idx & qed_chain_get_usable_per_page(p_chain)) ==
364 	    qed_chain_get_usable_per_page(p_chain))
365 		fw_cons_idx += qed_chain_get_unusable_per_page(p_chain);
366 
367 	/* Complete current segment of eq entries */
368 	while (fw_cons_idx != qed_chain_get_cons_idx(p_chain)) {
369 		struct event_ring_entry *p_eqe = qed_chain_consume(p_chain);
370 
371 		if (!p_eqe) {
372 			rc = -EINVAL;
373 			break;
374 		}
375 
376 		DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
377 			   "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
378 			   p_eqe->opcode,
379 			   p_eqe->protocol_id,
380 			   p_eqe->reserved0,
381 			   le16_to_cpu(p_eqe->echo),
382 			   p_eqe->fw_return_code,
383 			   p_eqe->flags);
384 
385 		if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
386 			if (qed_async_event_completion(p_hwfn, p_eqe))
387 				rc = -EINVAL;
388 		} else if (qed_spq_completion(p_hwfn,
389 					      p_eqe->echo,
390 					      p_eqe->fw_return_code,
391 					      &p_eqe->data)) {
392 			rc = -EINVAL;
393 		}
394 
395 		qed_chain_recycle_consumed(p_chain);
396 	}
397 
398 	qed_eq_prod_update(p_hwfn, qed_chain_get_prod_idx(p_chain));
399 
400 	return rc;
401 }
402 
403 int qed_eq_alloc(struct qed_hwfn *p_hwfn, u16 num_elem)
404 {
405 	struct qed_eq *p_eq;
406 
407 	/* Allocate EQ struct */
408 	p_eq = kzalloc(sizeof(*p_eq), GFP_KERNEL);
409 	if (!p_eq)
410 		return -ENOMEM;
411 
412 	/* Allocate and initialize EQ chain*/
413 	if (qed_chain_alloc(p_hwfn->cdev,
414 			    QED_CHAIN_USE_TO_PRODUCE,
415 			    QED_CHAIN_MODE_PBL,
416 			    QED_CHAIN_CNT_TYPE_U16,
417 			    num_elem,
418 			    sizeof(union event_ring_element),
419 			    &p_eq->chain, NULL))
420 		goto eq_allocate_fail;
421 
422 	/* register EQ completion on the SP SB */
423 	qed_int_register_cb(p_hwfn, qed_eq_completion,
424 			    p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
425 
426 	p_hwfn->p_eq = p_eq;
427 	return 0;
428 
429 eq_allocate_fail:
430 	kfree(p_eq);
431 	return -ENOMEM;
432 }
433 
434 void qed_eq_setup(struct qed_hwfn *p_hwfn)
435 {
436 	qed_chain_reset(&p_hwfn->p_eq->chain);
437 }
438 
439 void qed_eq_free(struct qed_hwfn *p_hwfn)
440 {
441 	if (!p_hwfn->p_eq)
442 		return;
443 
444 	qed_chain_free(p_hwfn->cdev, &p_hwfn->p_eq->chain);
445 
446 	kfree(p_hwfn->p_eq);
447 	p_hwfn->p_eq = NULL;
448 }
449 
450 /***************************************************************************
451 * CQE API - manipulate EQ functionality
452 ***************************************************************************/
453 static int qed_cqe_completion(struct qed_hwfn *p_hwfn,
454 			      struct eth_slow_path_rx_cqe *cqe,
455 			      enum protocol_type protocol)
456 {
457 	if (IS_VF(p_hwfn->cdev))
458 		return 0;
459 
460 	/* @@@tmp - it's possible we'll eventually want to handle some
461 	 * actual commands that can arrive here, but for now this is only
462 	 * used to complete the ramrod using the echo value on the cqe
463 	 */
464 	return qed_spq_completion(p_hwfn, cqe->echo, 0, NULL);
465 }
466 
467 int qed_eth_cqe_completion(struct qed_hwfn *p_hwfn,
468 			   struct eth_slow_path_rx_cqe *cqe)
469 {
470 	int rc;
471 
472 	rc = qed_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
473 	if (rc)
474 		DP_NOTICE(p_hwfn,
475 			  "Failed to handle RXQ CQE [cmd 0x%02x]\n",
476 			  cqe->ramrod_cmd_id);
477 
478 	return rc;
479 }
480 
481 /***************************************************************************
482 * Slow hwfn Queue (spq)
483 ***************************************************************************/
484 void qed_spq_setup(struct qed_hwfn *p_hwfn)
485 {
486 	struct qed_spq *p_spq = p_hwfn->p_spq;
487 	struct qed_spq_entry *p_virt = NULL;
488 	struct core_db_data *p_db_data;
489 	void __iomem *db_addr;
490 	dma_addr_t p_phys = 0;
491 	u32 i, capacity;
492 	int rc;
493 
494 	INIT_LIST_HEAD(&p_spq->pending);
495 	INIT_LIST_HEAD(&p_spq->completion_pending);
496 	INIT_LIST_HEAD(&p_spq->free_pool);
497 	INIT_LIST_HEAD(&p_spq->unlimited_pending);
498 	spin_lock_init(&p_spq->lock);
499 
500 	/* SPQ empty pool */
501 	p_phys	= p_spq->p_phys + offsetof(struct qed_spq_entry, ramrod);
502 	p_virt	= p_spq->p_virt;
503 
504 	capacity = qed_chain_get_capacity(&p_spq->chain);
505 	for (i = 0; i < capacity; i++) {
506 		DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
507 
508 		list_add_tail(&p_virt->list, &p_spq->free_pool);
509 
510 		p_virt++;
511 		p_phys += sizeof(struct qed_spq_entry);
512 	}
513 
514 	/* Statistics */
515 	p_spq->normal_count		= 0;
516 	p_spq->comp_count		= 0;
517 	p_spq->comp_sent_count		= 0;
518 	p_spq->unlimited_pending_count	= 0;
519 
520 	bitmap_zero(p_spq->p_comp_bitmap, SPQ_RING_SIZE);
521 	p_spq->comp_bitmap_idx = 0;
522 
523 	/* SPQ cid, cannot fail */
524 	qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
525 	qed_spq_hw_initialize(p_hwfn, p_spq);
526 
527 	/* reset the chain itself */
528 	qed_chain_reset(&p_spq->chain);
529 
530 	/* Initialize the address/data of the SPQ doorbell */
531 	p_spq->db_addr_offset = qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY);
532 	p_db_data = &p_spq->db_data;
533 	memset(p_db_data, 0, sizeof(*p_db_data));
534 	SET_FIELD(p_db_data->params, CORE_DB_DATA_DEST, DB_DEST_XCM);
535 	SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_MAX);
536 	SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_VAL_SEL,
537 		  DQ_XCM_CORE_SPQ_PROD_CMD);
538 	p_db_data->agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
539 
540 	/* Register the SPQ doorbell with the doorbell recovery mechanism */
541 	db_addr = (void __iomem *)((u8 __iomem *)p_hwfn->doorbells +
542 				   p_spq->db_addr_offset);
543 	rc = qed_db_recovery_add(p_hwfn->cdev, db_addr, &p_spq->db_data,
544 				 DB_REC_WIDTH_32B, DB_REC_KERNEL);
545 	if (rc)
546 		DP_INFO(p_hwfn,
547 			"Failed to register the SPQ doorbell with the doorbell recovery mechanism\n");
548 }
549 
550 int qed_spq_alloc(struct qed_hwfn *p_hwfn)
551 {
552 	struct qed_spq_entry *p_virt = NULL;
553 	struct qed_spq *p_spq = NULL;
554 	dma_addr_t p_phys = 0;
555 	u32 capacity;
556 
557 	/* SPQ struct */
558 	p_spq = kzalloc(sizeof(struct qed_spq), GFP_KERNEL);
559 	if (!p_spq)
560 		return -ENOMEM;
561 
562 	/* SPQ ring  */
563 	if (qed_chain_alloc(p_hwfn->cdev,
564 			    QED_CHAIN_USE_TO_PRODUCE,
565 			    QED_CHAIN_MODE_SINGLE,
566 			    QED_CHAIN_CNT_TYPE_U16,
567 			    0,   /* N/A when the mode is SINGLE */
568 			    sizeof(struct slow_path_element),
569 			    &p_spq->chain, NULL))
570 		goto spq_allocate_fail;
571 
572 	/* allocate and fill the SPQ elements (incl. ramrod data list) */
573 	capacity = qed_chain_get_capacity(&p_spq->chain);
574 	p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
575 				    capacity * sizeof(struct qed_spq_entry),
576 				    &p_phys, GFP_KERNEL);
577 	if (!p_virt)
578 		goto spq_allocate_fail;
579 
580 	p_spq->p_virt = p_virt;
581 	p_spq->p_phys = p_phys;
582 	p_hwfn->p_spq = p_spq;
583 
584 	return 0;
585 
586 spq_allocate_fail:
587 	qed_chain_free(p_hwfn->cdev, &p_spq->chain);
588 	kfree(p_spq);
589 	return -ENOMEM;
590 }
591 
592 void qed_spq_free(struct qed_hwfn *p_hwfn)
593 {
594 	struct qed_spq *p_spq = p_hwfn->p_spq;
595 	void __iomem *db_addr;
596 	u32 capacity;
597 
598 	if (!p_spq)
599 		return;
600 
601 	/* Delete the SPQ doorbell from the doorbell recovery mechanism */
602 	db_addr = (void __iomem *)((u8 __iomem *)p_hwfn->doorbells +
603 				   p_spq->db_addr_offset);
604 	qed_db_recovery_del(p_hwfn->cdev, db_addr, &p_spq->db_data);
605 
606 	if (p_spq->p_virt) {
607 		capacity = qed_chain_get_capacity(&p_spq->chain);
608 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
609 				  capacity *
610 				  sizeof(struct qed_spq_entry),
611 				  p_spq->p_virt, p_spq->p_phys);
612 	}
613 
614 	qed_chain_free(p_hwfn->cdev, &p_spq->chain);
615 	kfree(p_spq);
616 	p_hwfn->p_spq = NULL;
617 }
618 
619 int qed_spq_get_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry **pp_ent)
620 {
621 	struct qed_spq *p_spq = p_hwfn->p_spq;
622 	struct qed_spq_entry *p_ent = NULL;
623 	int rc = 0;
624 
625 	spin_lock_bh(&p_spq->lock);
626 
627 	if (list_empty(&p_spq->free_pool)) {
628 		p_ent = kzalloc(sizeof(*p_ent), GFP_ATOMIC);
629 		if (!p_ent) {
630 			DP_NOTICE(p_hwfn,
631 				  "Failed to allocate an SPQ entry for a pending ramrod\n");
632 			rc = -ENOMEM;
633 			goto out_unlock;
634 		}
635 		p_ent->queue = &p_spq->unlimited_pending;
636 	} else {
637 		p_ent = list_first_entry(&p_spq->free_pool,
638 					 struct qed_spq_entry, list);
639 		list_del(&p_ent->list);
640 		p_ent->queue = &p_spq->pending;
641 	}
642 
643 	*pp_ent = p_ent;
644 
645 out_unlock:
646 	spin_unlock_bh(&p_spq->lock);
647 	return rc;
648 }
649 
650 /* Locked variant; Should be called while the SPQ lock is taken */
651 static void __qed_spq_return_entry(struct qed_hwfn *p_hwfn,
652 				   struct qed_spq_entry *p_ent)
653 {
654 	list_add_tail(&p_ent->list, &p_hwfn->p_spq->free_pool);
655 }
656 
657 void qed_spq_return_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry *p_ent)
658 {
659 	spin_lock_bh(&p_hwfn->p_spq->lock);
660 	__qed_spq_return_entry(p_hwfn, p_ent);
661 	spin_unlock_bh(&p_hwfn->p_spq->lock);
662 }
663 
664 /**
665  * @brief qed_spq_add_entry - adds a new entry to the pending
666  *        list. Should be used while lock is being held.
667  *
668  * Addes an entry to the pending list is there is room (en empty
669  * element is available in the free_pool), or else places the
670  * entry in the unlimited_pending pool.
671  *
672  * @param p_hwfn
673  * @param p_ent
674  * @param priority
675  *
676  * @return int
677  */
678 static int qed_spq_add_entry(struct qed_hwfn *p_hwfn,
679 			     struct qed_spq_entry *p_ent,
680 			     enum spq_priority priority)
681 {
682 	struct qed_spq *p_spq = p_hwfn->p_spq;
683 
684 	if (p_ent->queue == &p_spq->unlimited_pending) {
685 
686 		if (list_empty(&p_spq->free_pool)) {
687 			list_add_tail(&p_ent->list, &p_spq->unlimited_pending);
688 			p_spq->unlimited_pending_count++;
689 
690 			return 0;
691 		} else {
692 			struct qed_spq_entry *p_en2;
693 
694 			p_en2 = list_first_entry(&p_spq->free_pool,
695 						 struct qed_spq_entry, list);
696 			list_del(&p_en2->list);
697 
698 			/* Copy the ring element physical pointer to the new
699 			 * entry, since we are about to override the entire ring
700 			 * entry and don't want to lose the pointer.
701 			 */
702 			p_ent->elem.data_ptr = p_en2->elem.data_ptr;
703 
704 			*p_en2 = *p_ent;
705 
706 			/* EBLOCK responsible to free the allocated p_ent */
707 			if (p_ent->comp_mode != QED_SPQ_MODE_EBLOCK)
708 				kfree(p_ent);
709 			else
710 				p_ent->post_ent = p_en2;
711 
712 			p_ent = p_en2;
713 		}
714 	}
715 
716 	/* entry is to be placed in 'pending' queue */
717 	switch (priority) {
718 	case QED_SPQ_PRIORITY_NORMAL:
719 		list_add_tail(&p_ent->list, &p_spq->pending);
720 		p_spq->normal_count++;
721 		break;
722 	case QED_SPQ_PRIORITY_HIGH:
723 		list_add(&p_ent->list, &p_spq->pending);
724 		p_spq->high_count++;
725 		break;
726 	default:
727 		return -EINVAL;
728 	}
729 
730 	return 0;
731 }
732 
733 /***************************************************************************
734 * Accessor
735 ***************************************************************************/
736 u32 qed_spq_get_cid(struct qed_hwfn *p_hwfn)
737 {
738 	if (!p_hwfn->p_spq)
739 		return 0xffffffff;      /* illegal */
740 	return p_hwfn->p_spq->cid;
741 }
742 
743 /***************************************************************************
744 * Posting new Ramrods
745 ***************************************************************************/
746 static int qed_spq_post_list(struct qed_hwfn *p_hwfn,
747 			     struct list_head *head, u32 keep_reserve)
748 {
749 	struct qed_spq *p_spq = p_hwfn->p_spq;
750 	int rc;
751 
752 	while (qed_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
753 	       !list_empty(head)) {
754 		struct qed_spq_entry *p_ent =
755 			list_first_entry(head, struct qed_spq_entry, list);
756 		list_move_tail(&p_ent->list, &p_spq->completion_pending);
757 		p_spq->comp_sent_count++;
758 
759 		rc = qed_spq_hw_post(p_hwfn, p_spq, p_ent);
760 		if (rc) {
761 			list_del(&p_ent->list);
762 			__qed_spq_return_entry(p_hwfn, p_ent);
763 			return rc;
764 		}
765 	}
766 
767 	return 0;
768 }
769 
770 static int qed_spq_pend_post(struct qed_hwfn *p_hwfn)
771 {
772 	struct qed_spq *p_spq = p_hwfn->p_spq;
773 	struct qed_spq_entry *p_ent = NULL;
774 
775 	while (!list_empty(&p_spq->free_pool)) {
776 		if (list_empty(&p_spq->unlimited_pending))
777 			break;
778 
779 		p_ent = list_first_entry(&p_spq->unlimited_pending,
780 					 struct qed_spq_entry, list);
781 		if (!p_ent)
782 			return -EINVAL;
783 
784 		list_del(&p_ent->list);
785 
786 		qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
787 	}
788 
789 	return qed_spq_post_list(p_hwfn, &p_spq->pending,
790 				 SPQ_HIGH_PRI_RESERVE_DEFAULT);
791 }
792 
793 /* Avoid overriding of SPQ entries when getting out-of-order completions, by
794  * marking the completions in a bitmap and increasing the chain consumer only
795  * for the first successive completed entries.
796  */
797 static void qed_spq_comp_bmap_update(struct qed_hwfn *p_hwfn, __le16 echo)
798 {
799 	u16 pos = le16_to_cpu(echo) % SPQ_RING_SIZE;
800 	struct qed_spq *p_spq = p_hwfn->p_spq;
801 
802 	__set_bit(pos, p_spq->p_comp_bitmap);
803 	while (test_bit(p_spq->comp_bitmap_idx,
804 			p_spq->p_comp_bitmap)) {
805 		__clear_bit(p_spq->comp_bitmap_idx,
806 			    p_spq->p_comp_bitmap);
807 		p_spq->comp_bitmap_idx++;
808 		qed_chain_return_produced(&p_spq->chain);
809 	}
810 }
811 
812 int qed_spq_post(struct qed_hwfn *p_hwfn,
813 		 struct qed_spq_entry *p_ent, u8 *fw_return_code)
814 {
815 	int rc = 0;
816 	struct qed_spq *p_spq = p_hwfn ? p_hwfn->p_spq : NULL;
817 	bool b_ret_ent = true;
818 	bool eblock;
819 
820 	if (!p_hwfn)
821 		return -EINVAL;
822 
823 	if (!p_ent) {
824 		DP_NOTICE(p_hwfn, "Got a NULL pointer\n");
825 		return -EINVAL;
826 	}
827 
828 	/* Complete the entry */
829 	rc = qed_spq_fill_entry(p_hwfn, p_ent);
830 
831 	spin_lock_bh(&p_spq->lock);
832 
833 	/* Check return value after LOCK is taken for cleaner error flow */
834 	if (rc)
835 		goto spq_post_fail;
836 
837 	/* Check if entry is in block mode before qed_spq_add_entry,
838 	 * which might kfree p_ent.
839 	 */
840 	eblock = (p_ent->comp_mode == QED_SPQ_MODE_EBLOCK);
841 
842 	/* Add the request to the pending queue */
843 	rc = qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
844 	if (rc)
845 		goto spq_post_fail;
846 
847 	rc = qed_spq_pend_post(p_hwfn);
848 	if (rc) {
849 		/* Since it's possible that pending failed for a different
850 		 * entry [although unlikely], the failed entry was already
851 		 * dealt with; No need to return it here.
852 		 */
853 		b_ret_ent = false;
854 		goto spq_post_fail;
855 	}
856 
857 	spin_unlock_bh(&p_spq->lock);
858 
859 	if (eblock) {
860 		/* For entries in QED BLOCK mode, the completion code cannot
861 		 * perform the necessary cleanup - if it did, we couldn't
862 		 * access p_ent here to see whether it's successful or not.
863 		 * Thus, after gaining the answer perform the cleanup here.
864 		 */
865 		rc = qed_spq_block(p_hwfn, p_ent, fw_return_code,
866 				   p_ent->queue == &p_spq->unlimited_pending);
867 
868 		if (p_ent->queue == &p_spq->unlimited_pending) {
869 			struct qed_spq_entry *p_post_ent = p_ent->post_ent;
870 
871 			kfree(p_ent);
872 
873 			/* Return the entry which was actually posted */
874 			p_ent = p_post_ent;
875 		}
876 
877 		if (rc)
878 			goto spq_post_fail2;
879 
880 		/* return to pool */
881 		qed_spq_return_entry(p_hwfn, p_ent);
882 	}
883 	return rc;
884 
885 spq_post_fail2:
886 	spin_lock_bh(&p_spq->lock);
887 	list_del(&p_ent->list);
888 	qed_spq_comp_bmap_update(p_hwfn, p_ent->elem.hdr.echo);
889 
890 spq_post_fail:
891 	/* return to the free pool */
892 	if (b_ret_ent)
893 		__qed_spq_return_entry(p_hwfn, p_ent);
894 	spin_unlock_bh(&p_spq->lock);
895 
896 	return rc;
897 }
898 
899 int qed_spq_completion(struct qed_hwfn *p_hwfn,
900 		       __le16 echo,
901 		       u8 fw_return_code,
902 		       union event_ring_data *p_data)
903 {
904 	struct qed_spq		*p_spq;
905 	struct qed_spq_entry	*p_ent = NULL;
906 	struct qed_spq_entry	*tmp;
907 	struct qed_spq_entry	*found = NULL;
908 	int			rc;
909 
910 	if (!p_hwfn)
911 		return -EINVAL;
912 
913 	p_spq = p_hwfn->p_spq;
914 	if (!p_spq)
915 		return -EINVAL;
916 
917 	spin_lock_bh(&p_spq->lock);
918 	list_for_each_entry_safe(p_ent, tmp, &p_spq->completion_pending, list) {
919 		if (p_ent->elem.hdr.echo == echo) {
920 			list_del(&p_ent->list);
921 			qed_spq_comp_bmap_update(p_hwfn, echo);
922 			p_spq->comp_count++;
923 			found = p_ent;
924 			break;
925 		}
926 
927 		/* This is relatively uncommon - depends on scenarios
928 		 * which have mutliple per-PF sent ramrods.
929 		 */
930 		DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
931 			   "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
932 			   le16_to_cpu(echo),
933 			   le16_to_cpu(p_ent->elem.hdr.echo));
934 	}
935 
936 	/* Release lock before callback, as callback may post
937 	 * an additional ramrod.
938 	 */
939 	spin_unlock_bh(&p_spq->lock);
940 
941 	if (!found) {
942 		DP_NOTICE(p_hwfn,
943 			  "Failed to find an entry this EQE [echo %04x] completes\n",
944 			  le16_to_cpu(echo));
945 		return -EEXIST;
946 	}
947 
948 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
949 		   "Complete EQE [echo %04x]: func %p cookie %p)\n",
950 		   le16_to_cpu(echo),
951 		   p_ent->comp_cb.function, p_ent->comp_cb.cookie);
952 	if (found->comp_cb.function)
953 		found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
954 					fw_return_code);
955 	else
956 		DP_VERBOSE(p_hwfn,
957 			   QED_MSG_SPQ,
958 			   "Got a completion without a callback function\n");
959 
960 	if (found->comp_mode != QED_SPQ_MODE_EBLOCK)
961 		/* EBLOCK  is responsible for returning its own entry into the
962 		 * free list.
963 		 */
964 		qed_spq_return_entry(p_hwfn, found);
965 
966 	/* Attempt to post pending requests */
967 	spin_lock_bh(&p_spq->lock);
968 	rc = qed_spq_pend_post(p_hwfn);
969 	spin_unlock_bh(&p_spq->lock);
970 
971 	return rc;
972 }
973 
974 int qed_consq_alloc(struct qed_hwfn *p_hwfn)
975 {
976 	struct qed_consq *p_consq;
977 
978 	/* Allocate ConsQ struct */
979 	p_consq = kzalloc(sizeof(*p_consq), GFP_KERNEL);
980 	if (!p_consq)
981 		return -ENOMEM;
982 
983 	/* Allocate and initialize EQ chain*/
984 	if (qed_chain_alloc(p_hwfn->cdev,
985 			    QED_CHAIN_USE_TO_PRODUCE,
986 			    QED_CHAIN_MODE_PBL,
987 			    QED_CHAIN_CNT_TYPE_U16,
988 			    QED_CHAIN_PAGE_SIZE / 0x80,
989 			    0x80, &p_consq->chain, NULL))
990 		goto consq_allocate_fail;
991 
992 	p_hwfn->p_consq = p_consq;
993 	return 0;
994 
995 consq_allocate_fail:
996 	kfree(p_consq);
997 	return -ENOMEM;
998 }
999 
1000 void qed_consq_setup(struct qed_hwfn *p_hwfn)
1001 {
1002 	qed_chain_reset(&p_hwfn->p_consq->chain);
1003 }
1004 
1005 void qed_consq_free(struct qed_hwfn *p_hwfn)
1006 {
1007 	if (!p_hwfn->p_consq)
1008 		return;
1009 
1010 	qed_chain_free(p_hwfn->cdev, &p_hwfn->p_consq->chain);
1011 
1012 	kfree(p_hwfn->p_consq);
1013 	p_hwfn->p_consq = NULL;
1014 }
1015