xref: /dpdk/drivers/net/qede/base/ecore_spq.c (revision df2b82ca)
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8 
9 #include "bcm_osal.h"
10 #include "reg_addr.h"
11 #include "ecore_gtt_reg_addr.h"
12 #include "ecore_hsi_common.h"
13 #include "ecore.h"
14 #include "ecore_sp_api.h"
15 #include "ecore_spq.h"
16 #include "ecore_iro.h"
17 #include "ecore_init_fw_funcs.h"
18 #include "ecore_cxt.h"
19 #include "ecore_int.h"
20 #include "ecore_dev_api.h"
21 #include "ecore_mcp.h"
22 #include "ecore_hw.h"
23 #include "ecore_sriov.h"
24 
25 /***************************************************************************
26  * Structures & Definitions
27  ***************************************************************************/
28 
29 #define SPQ_HIGH_PRI_RESERVE_DEFAULT	(1)
30 
31 #define SPQ_BLOCK_DELAY_MAX_ITER	(10)
32 #define SPQ_BLOCK_DELAY_US		(10)
33 #define SPQ_BLOCK_SLEEP_MAX_ITER	(1000)
34 #define SPQ_BLOCK_SLEEP_MS		(5)
35 
36 /***************************************************************************
37  * Blocking Imp. (BLOCK/EBLOCK mode)
38  ***************************************************************************/
39 static void ecore_spq_blocking_cb(struct ecore_hwfn *p_hwfn,
40 				  void *cookie,
41 				  union event_ring_data *data,
42 				  u8 fw_return_code)
43 {
44 	struct ecore_spq_comp_done *comp_done;
45 
46 	comp_done = (struct ecore_spq_comp_done *)cookie;
47 
48 	comp_done->done = 0x1;
49 	comp_done->fw_return_code = fw_return_code;
50 
51 	/* make update visible to waiting thread */
52 	OSAL_SMP_WMB(p_hwfn->p_dev);
53 }
54 
55 static enum _ecore_status_t __ecore_spq_block(struct ecore_hwfn *p_hwfn,
56 					      struct ecore_spq_entry *p_ent,
57 					      u8 *p_fw_ret,
58 					      bool sleep_between_iter)
59 {
60 	struct ecore_spq_comp_done *comp_done;
61 	u32 iter_cnt;
62 
63 	comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie;
64 	iter_cnt = sleep_between_iter ? SPQ_BLOCK_SLEEP_MAX_ITER
65 				      : SPQ_BLOCK_DELAY_MAX_ITER;
66 
67 	while (iter_cnt--) {
68 		OSAL_POLL_MODE_DPC(p_hwfn);
69 		OSAL_SMP_RMB(p_hwfn->p_dev);
70 		if (comp_done->done == 1) {
71 			if (p_fw_ret)
72 				*p_fw_ret = comp_done->fw_return_code;
73 			return ECORE_SUCCESS;
74 		}
75 
76 		if (sleep_between_iter)
77 			OSAL_MSLEEP(SPQ_BLOCK_SLEEP_MS);
78 		else
79 			OSAL_UDELAY(SPQ_BLOCK_DELAY_US);
80 	}
81 
82 	return ECORE_TIMEOUT;
83 }
84 
85 static enum _ecore_status_t ecore_spq_block(struct ecore_hwfn *p_hwfn,
86 					    struct ecore_spq_entry *p_ent,
87 					    u8 *p_fw_ret, bool skip_quick_poll)
88 {
89 	struct ecore_spq_comp_done *comp_done;
90 	enum _ecore_status_t rc;
91 
92 	/* A relatively short polling period w/o sleeping, to allow the FW to
93 	 * complete the ramrod and thus possibly to avoid the following sleeps.
94 	 */
95 	if (!skip_quick_poll) {
96 		rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, false);
97 		if (rc == ECORE_SUCCESS)
98 			return ECORE_SUCCESS;
99 	}
100 
101 	/* Move to polling with a sleeping period between iterations */
102 	rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true);
103 	if (rc == ECORE_SUCCESS)
104 		return ECORE_SUCCESS;
105 
106 	DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
107 	rc = ecore_mcp_drain(p_hwfn, p_hwfn->p_main_ptt);
108 	if (rc != ECORE_SUCCESS) {
109 		DP_NOTICE(p_hwfn, true, "MCP drain failed\n");
110 		goto err;
111 	}
112 
113 	/* Retry after drain */
114 	rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true);
115 	if (rc == ECORE_SUCCESS)
116 		return ECORE_SUCCESS;
117 
118 	comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie;
119 	if (comp_done->done == 1) {
120 		if (p_fw_ret)
121 			*p_fw_ret = comp_done->fw_return_code;
122 		return ECORE_SUCCESS;
123 	}
124 err:
125 	DP_NOTICE(p_hwfn, true,
126 		  "Ramrod is stuck [CID %08x cmd %02x proto %02x echo %04x]\n",
127 		  OSAL_LE32_TO_CPU(p_ent->elem.hdr.cid),
128 		  p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id,
129 		  OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo));
130 
131 	ecore_hw_err_notify(p_hwfn, ECORE_HW_ERR_RAMROD_FAIL);
132 
133 	return ECORE_BUSY;
134 }
135 
136 /***************************************************************************
137  * SPQ entries inner API
138  ***************************************************************************/
139 static enum _ecore_status_t
140 ecore_spq_fill_entry(struct ecore_hwfn *p_hwfn, struct ecore_spq_entry *p_ent)
141 {
142 	p_ent->flags = 0;
143 
144 	switch (p_ent->comp_mode) {
145 	case ECORE_SPQ_MODE_EBLOCK:
146 	case ECORE_SPQ_MODE_BLOCK:
147 		p_ent->comp_cb.function = ecore_spq_blocking_cb;
148 		break;
149 	case ECORE_SPQ_MODE_CB:
150 		break;
151 	default:
152 		DP_NOTICE(p_hwfn, true, "Unknown SPQE completion mode %d\n",
153 			  p_ent->comp_mode);
154 		return ECORE_INVAL;
155 	}
156 
157 	DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
158 		   "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x]"
159 		   " Data pointer: [%08x:%08x] Completion Mode: %s\n",
160 		   p_ent->elem.hdr.cid, p_ent->elem.hdr.cmd_id,
161 		   p_ent->elem.hdr.protocol_id,
162 		   p_ent->elem.data_ptr.hi, p_ent->elem.data_ptr.lo,
163 		   D_TRINE(p_ent->comp_mode, ECORE_SPQ_MODE_EBLOCK,
164 			   ECORE_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
165 			   "MODE_CB"));
166 
167 	return ECORE_SUCCESS;
168 }
169 
170 /***************************************************************************
171  * HSI access
172  ***************************************************************************/
173 static void ecore_spq_hw_initialize(struct ecore_hwfn *p_hwfn,
174 				    struct ecore_spq *p_spq)
175 {
176 	u16 pq;
177 	struct ecore_cxt_info cxt_info;
178 	struct core_conn_context *p_cxt;
179 	union ecore_qm_pq_params pq_params;
180 	enum _ecore_status_t rc;
181 
182 	cxt_info.iid = p_spq->cid;
183 
184 	rc = ecore_cxt_get_cid_info(p_hwfn, &cxt_info);
185 
186 	if (rc < 0) {
187 		DP_NOTICE(p_hwfn, true, "Cannot find context info for cid=%d\n",
188 			  p_spq->cid);
189 		return;
190 	}
191 
192 	p_cxt = cxt_info.p_cxt;
193 
194 	SET_FIELD(p_cxt->xstorm_ag_context.flags10,
195 		  XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
196 	SET_FIELD(p_cxt->xstorm_ag_context.flags1,
197 		  XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
198 	/* SET_FIELD(p_cxt->xstorm_ag_context.flags10,
199 	 *           XSTORM_CORE_CONN_AG_CTX_SLOW_PATH_EN, 1);
200 	 */
201 	SET_FIELD(p_cxt->xstorm_ag_context.flags9,
202 		  XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
203 
204 	/* CDU validation - FIXME currently disabled */
205 
206 	/* QM physical queue */
207 	OSAL_MEMSET(&pq_params, 0, sizeof(pq_params));
208 	pq_params.core.tc = LB_TC;
209 	pq = ecore_get_qm_pq(p_hwfn, PROTOCOLID_CORE, &pq_params);
210 	p_cxt->xstorm_ag_context.physical_q0 = OSAL_CPU_TO_LE16(pq);
211 
212 	p_cxt->xstorm_st_context.spq_base_lo =
213 	    DMA_LO_LE(p_spq->chain.p_phys_addr);
214 	p_cxt->xstorm_st_context.spq_base_hi =
215 	    DMA_HI_LE(p_spq->chain.p_phys_addr);
216 
217 	DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
218 		       p_hwfn->p_consq->chain.p_phys_addr);
219 }
220 
221 static enum _ecore_status_t ecore_spq_hw_post(struct ecore_hwfn *p_hwfn,
222 					      struct ecore_spq *p_spq,
223 					      struct ecore_spq_entry *p_ent)
224 {
225 	struct ecore_chain *p_chain = &p_hwfn->p_spq->chain;
226 	u16 echo = ecore_chain_get_prod_idx(p_chain);
227 	struct slow_path_element *elem;
228 	struct core_db_data db;
229 
230 	p_ent->elem.hdr.echo = OSAL_CPU_TO_LE16(echo);
231 	elem = ecore_chain_produce(p_chain);
232 	if (!elem) {
233 		DP_NOTICE(p_hwfn, true, "Failed to produce from SPQ chain\n");
234 		return ECORE_INVAL;
235 	}
236 
237 	*elem = p_ent->elem;	/* struct assignment */
238 
239 	/* send a doorbell on the slow hwfn session */
240 	OSAL_MEMSET(&db, 0, sizeof(db));
241 	SET_FIELD(db.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
242 	SET_FIELD(db.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
243 	SET_FIELD(db.params, CORE_DB_DATA_AGG_VAL_SEL,
244 		  DQ_XCM_CORE_SPQ_PROD_CMD);
245 	db.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
246 	db.spq_prod = OSAL_CPU_TO_LE16(ecore_chain_get_prod_idx(p_chain));
247 
248 	/* make sure the SPQE is updated before the doorbell */
249 	OSAL_WMB(p_hwfn->p_dev);
250 
251 	DOORBELL(p_hwfn, DB_ADDR(p_spq->cid, DQ_DEMS_LEGACY), *(u32 *)&db);
252 
253 	/* make sure doorbell is rang */
254 	OSAL_WMB(p_hwfn->p_dev);
255 
256 	DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
257 		   "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x"
258 		   " agg_params: %02x, prod: %04x\n",
259 		   DB_ADDR(p_spq->cid, DQ_DEMS_LEGACY), p_spq->cid, db.params,
260 		   db.agg_flags, ecore_chain_get_prod_idx(p_chain));
261 
262 	return ECORE_SUCCESS;
263 }
264 
265 /***************************************************************************
266  * Asynchronous events
267  ***************************************************************************/
268 
269 static enum _ecore_status_t
270 ecore_async_event_completion(struct ecore_hwfn *p_hwfn,
271 			     struct event_ring_entry *p_eqe)
272 {
273 	switch (p_eqe->protocol_id) {
274 	case PROTOCOLID_COMMON:
275 		return ecore_sriov_eqe_event(p_hwfn,
276 					     p_eqe->opcode,
277 					     p_eqe->echo, &p_eqe->data);
278 	default:
279 		DP_NOTICE(p_hwfn,
280 			  true, "Unknown Async completion for protocol: %d\n",
281 			  p_eqe->protocol_id);
282 		return ECORE_INVAL;
283 	}
284 }
285 
286 /***************************************************************************
287  * EQ API
288  ***************************************************************************/
289 void ecore_eq_prod_update(struct ecore_hwfn *p_hwfn, u16 prod)
290 {
291 	u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
292 	    USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
293 
294 	REG_WR16(p_hwfn, addr, prod);
295 
296 	/* keep prod updates ordered */
297 	OSAL_MMIOWB(p_hwfn->p_dev);
298 }
299 
300 enum _ecore_status_t ecore_eq_completion(struct ecore_hwfn *p_hwfn,
301 					 void *cookie)
302 {
303 	struct ecore_eq *p_eq = cookie;
304 	struct ecore_chain *p_chain = &p_eq->chain;
305 	enum _ecore_status_t rc = 0;
306 
307 	/* take a snapshot of the FW consumer */
308 	u16 fw_cons_idx = OSAL_LE16_TO_CPU(*p_eq->p_fw_cons);
309 
310 	DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
311 
312 	/* Need to guarantee the fw_cons index we use points to a usuable
313 	 * element (to comply with our chain), so our macros would comply
314 	 */
315 	if ((fw_cons_idx & ecore_chain_get_usable_per_page(p_chain)) ==
316 	    ecore_chain_get_usable_per_page(p_chain)) {
317 		fw_cons_idx += ecore_chain_get_unusable_per_page(p_chain);
318 	}
319 
320 	/* Complete current segment of eq entries */
321 	while (fw_cons_idx != ecore_chain_get_cons_idx(p_chain)) {
322 		struct event_ring_entry *p_eqe = ecore_chain_consume(p_chain);
323 		if (!p_eqe) {
324 			rc = ECORE_INVAL;
325 			break;
326 		}
327 
328 		DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
329 			   "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
330 			   p_eqe->opcode,	     /* Event Opcode */
331 			   p_eqe->protocol_id,	/* Event Protocol ID */
332 			   p_eqe->reserved0,	/* Reserved */
333 			   /* Echo value from ramrod data on the host */
334 			   OSAL_LE16_TO_CPU(p_eqe->echo),
335 			   p_eqe->fw_return_code,    /* FW return code for SP
336 						      * ramrods
337 						      */
338 			   p_eqe->flags);
339 
340 		if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
341 			if (ecore_async_event_completion(p_hwfn, p_eqe))
342 				rc = ECORE_INVAL;
343 		} else if (ecore_spq_completion(p_hwfn,
344 						p_eqe->echo,
345 						p_eqe->fw_return_code,
346 						&p_eqe->data)) {
347 			rc = ECORE_INVAL;
348 		}
349 
350 		ecore_chain_recycle_consumed(p_chain);
351 	}
352 
353 	ecore_eq_prod_update(p_hwfn, ecore_chain_get_prod_idx(p_chain));
354 
355 	return rc;
356 }
357 
358 struct ecore_eq *ecore_eq_alloc(struct ecore_hwfn *p_hwfn, u16 num_elem)
359 {
360 	struct ecore_eq *p_eq;
361 
362 	/* Allocate EQ struct */
363 	p_eq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_eq));
364 	if (!p_eq) {
365 		DP_NOTICE(p_hwfn, true,
366 			  "Failed to allocate `struct ecore_eq'\n");
367 		return OSAL_NULL;
368 	}
369 
370 	/* Allocate and initialize EQ chain*/
371 	if (ecore_chain_alloc(p_hwfn->p_dev,
372 			      ECORE_CHAIN_USE_TO_PRODUCE,
373 			      ECORE_CHAIN_MODE_PBL,
374 			      ECORE_CHAIN_CNT_TYPE_U16,
375 			      num_elem,
376 			      sizeof(union event_ring_element),
377 			      &p_eq->chain, OSAL_NULL)) {
378 		DP_NOTICE(p_hwfn, true, "Failed to allocate eq chain\n");
379 		goto eq_allocate_fail;
380 	}
381 
382 	/* register EQ completion on the SP SB */
383 	ecore_int_register_cb(p_hwfn, ecore_eq_completion,
384 			      p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
385 
386 	return p_eq;
387 
388 eq_allocate_fail:
389 	ecore_eq_free(p_hwfn, p_eq);
390 	return OSAL_NULL;
391 }
392 
393 void ecore_eq_setup(struct ecore_hwfn *p_hwfn, struct ecore_eq *p_eq)
394 {
395 	ecore_chain_reset(&p_eq->chain);
396 }
397 
398 void ecore_eq_free(struct ecore_hwfn *p_hwfn, struct ecore_eq *p_eq)
399 {
400 	if (!p_eq)
401 		return;
402 	ecore_chain_free(p_hwfn->p_dev, &p_eq->chain);
403 	OSAL_FREE(p_hwfn->p_dev, p_eq);
404 }
405 
406 /***************************************************************************
407 * CQE API - manipulate EQ functionality
408 ***************************************************************************/
409 static enum _ecore_status_t ecore_cqe_completion(struct ecore_hwfn *p_hwfn,
410 						 struct eth_slow_path_rx_cqe
411 						 *cqe,
412 						 enum protocol_type protocol)
413 {
414 	if (IS_VF(p_hwfn->p_dev))
415 		return OSAL_VF_CQE_COMPLETION(p_hwfn, cqe, protocol);
416 
417 	/* @@@tmp - it's possible we'll eventually want to handle some
418 	 * actual commands that can arrive here, but for now this is only
419 	 * used to complete the ramrod using the echo value on the cqe
420 	 */
421 	return ecore_spq_completion(p_hwfn, cqe->echo, 0, OSAL_NULL);
422 }
423 
424 enum _ecore_status_t ecore_eth_cqe_completion(struct ecore_hwfn *p_hwfn,
425 					      struct eth_slow_path_rx_cqe *cqe)
426 {
427 	enum _ecore_status_t rc;
428 
429 	rc = ecore_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
430 	if (rc) {
431 		DP_NOTICE(p_hwfn, true,
432 			  "Failed to handle RXQ CQE [cmd 0x%02x]\n",
433 			  cqe->ramrod_cmd_id);
434 	}
435 
436 	return rc;
437 }
438 
439 /***************************************************************************
440  * Slow hwfn Queue (spq)
441  ***************************************************************************/
442 void ecore_spq_setup(struct ecore_hwfn *p_hwfn)
443 {
444 	struct ecore_spq *p_spq = p_hwfn->p_spq;
445 	struct ecore_spq_entry *p_virt = OSAL_NULL;
446 	dma_addr_t p_phys = 0;
447 	u32 i, capacity;
448 
449 	OSAL_LIST_INIT(&p_spq->pending);
450 	OSAL_LIST_INIT(&p_spq->completion_pending);
451 	OSAL_LIST_INIT(&p_spq->free_pool);
452 	OSAL_LIST_INIT(&p_spq->unlimited_pending);
453 	OSAL_SPIN_LOCK_INIT(&p_spq->lock);
454 
455 	/* SPQ empty pool */
456 	p_phys = p_spq->p_phys + OFFSETOF(struct ecore_spq_entry, ramrod);
457 	p_virt = p_spq->p_virt;
458 
459 	capacity = ecore_chain_get_capacity(&p_spq->chain);
460 	for (i = 0; i < capacity; i++) {
461 		DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
462 
463 		OSAL_LIST_PUSH_TAIL(&p_virt->list, &p_spq->free_pool);
464 
465 		p_virt++;
466 		p_phys += sizeof(struct ecore_spq_entry);
467 	}
468 
469 	/* Statistics */
470 	p_spq->normal_count = 0;
471 	p_spq->comp_count = 0;
472 	p_spq->comp_sent_count = 0;
473 	p_spq->unlimited_pending_count = 0;
474 
475 	OSAL_MEM_ZERO(p_spq->p_comp_bitmap,
476 		      SPQ_COMP_BMAP_SIZE * sizeof(unsigned long));
477 	p_spq->comp_bitmap_idx = 0;
478 
479 	/* SPQ cid, cannot fail */
480 	ecore_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
481 	ecore_spq_hw_initialize(p_hwfn, p_spq);
482 
483 	/* reset the chain itself */
484 	ecore_chain_reset(&p_spq->chain);
485 }
486 
487 enum _ecore_status_t ecore_spq_alloc(struct ecore_hwfn *p_hwfn)
488 {
489 	struct ecore_spq_entry *p_virt = OSAL_NULL;
490 	struct ecore_spq *p_spq = OSAL_NULL;
491 	dma_addr_t p_phys = 0;
492 	u32 capacity;
493 
494 	/* SPQ struct */
495 	p_spq =
496 	    OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(struct ecore_spq));
497 	if (!p_spq) {
498 		DP_NOTICE(p_hwfn, true,
499 			  "Failed to allocate `struct ecore_spq'\n");
500 		return ECORE_NOMEM;
501 	}
502 
503 	/* SPQ ring  */
504 	if (ecore_chain_alloc(p_hwfn->p_dev,
505 			      ECORE_CHAIN_USE_TO_PRODUCE,
506 			      ECORE_CHAIN_MODE_SINGLE,
507 			      ECORE_CHAIN_CNT_TYPE_U16,
508 			      0, /* N/A when the mode is SINGLE */
509 			      sizeof(struct slow_path_element),
510 			      &p_spq->chain, OSAL_NULL)) {
511 		DP_NOTICE(p_hwfn, true, "Failed to allocate spq chain\n");
512 		goto spq_allocate_fail;
513 	}
514 
515 	/* allocate and fill the SPQ elements (incl. ramrod data list) */
516 	capacity = ecore_chain_get_capacity(&p_spq->chain);
517 	p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, &p_phys,
518 					 capacity *
519 					 sizeof(struct ecore_spq_entry));
520 	if (!p_virt)
521 		goto spq_allocate_fail;
522 
523 	p_spq->p_virt = p_virt;
524 	p_spq->p_phys = p_phys;
525 
526 	OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_spq->lock);
527 
528 	p_hwfn->p_spq = p_spq;
529 	return ECORE_SUCCESS;
530 
531 spq_allocate_fail:
532 	ecore_chain_free(p_hwfn->p_dev, &p_spq->chain);
533 	OSAL_FREE(p_hwfn->p_dev, p_spq);
534 	return ECORE_NOMEM;
535 }
536 
537 void ecore_spq_free(struct ecore_hwfn *p_hwfn)
538 {
539 	struct ecore_spq *p_spq = p_hwfn->p_spq;
540 	u32 capacity;
541 
542 	if (!p_spq)
543 		return;
544 
545 	if (p_spq->p_virt) {
546 		capacity = ecore_chain_get_capacity(&p_spq->chain);
547 		OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
548 				       p_spq->p_virt,
549 				       p_spq->p_phys,
550 				       capacity *
551 				       sizeof(struct ecore_spq_entry));
552 	}
553 
554 	ecore_chain_free(p_hwfn->p_dev, &p_spq->chain);
555 	OSAL_SPIN_LOCK_DEALLOC(&p_spq->lock);
556 	OSAL_FREE(p_hwfn->p_dev, p_spq);
557 }
558 
559 enum _ecore_status_t
560 ecore_spq_get_entry(struct ecore_hwfn *p_hwfn, struct ecore_spq_entry **pp_ent)
561 {
562 	struct ecore_spq *p_spq = p_hwfn->p_spq;
563 	struct ecore_spq_entry *p_ent = OSAL_NULL;
564 	enum _ecore_status_t rc = ECORE_SUCCESS;
565 
566 	OSAL_SPIN_LOCK(&p_spq->lock);
567 
568 	if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
569 		p_ent = OSAL_ZALLOC(p_hwfn->p_dev, GFP_ATOMIC, sizeof(*p_ent));
570 		if (!p_ent) {
571 			DP_NOTICE(p_hwfn, true,
572 				 "Failed to allocate an SPQ entry for a pending"
573 				 " ramrod\n");
574 			rc = ECORE_NOMEM;
575 			goto out_unlock;
576 		}
577 		p_ent->queue = &p_spq->unlimited_pending;
578 	} else {
579 		p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool,
580 					      struct ecore_spq_entry, list);
581 		OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->free_pool);
582 		p_ent->queue = &p_spq->pending;
583 	}
584 
585 	*pp_ent = p_ent;
586 
587 out_unlock:
588 	OSAL_SPIN_UNLOCK(&p_spq->lock);
589 	return rc;
590 }
591 
592 /* Locked variant; Should be called while the SPQ lock is taken */
593 static void __ecore_spq_return_entry(struct ecore_hwfn *p_hwfn,
594 				     struct ecore_spq_entry *p_ent)
595 {
596 	OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_hwfn->p_spq->free_pool);
597 }
598 
599 void ecore_spq_return_entry(struct ecore_hwfn *p_hwfn,
600 			    struct ecore_spq_entry *p_ent)
601 {
602 	OSAL_SPIN_LOCK(&p_hwfn->p_spq->lock);
603 	__ecore_spq_return_entry(p_hwfn, p_ent);
604 	OSAL_SPIN_UNLOCK(&p_hwfn->p_spq->lock);
605 }
606 
607 /**
608  * @brief ecore_spq_add_entry - adds a new entry to the pending
609  *        list. Should be used while lock is being held.
610  *
611  * Addes an entry to the pending list is there is room (en empty
612  * element is available in the free_pool), or else places the
613  * entry in the unlimited_pending pool.
614  *
615  * @param p_hwfn
616  * @param p_ent
617  * @param priority
618  *
619  * @return enum _ecore_status_t
620  */
621 static enum _ecore_status_t
622 ecore_spq_add_entry(struct ecore_hwfn *p_hwfn,
623 		    struct ecore_spq_entry *p_ent, enum spq_priority priority)
624 {
625 	struct ecore_spq *p_spq = p_hwfn->p_spq;
626 
627 	if (p_ent->queue == &p_spq->unlimited_pending) {
628 		if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
629 			OSAL_LIST_PUSH_TAIL(&p_ent->list,
630 					    &p_spq->unlimited_pending);
631 			p_spq->unlimited_pending_count++;
632 
633 			return ECORE_SUCCESS;
634 
635 		} else {
636 			struct ecore_spq_entry *p_en2;
637 
638 			p_en2 = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool,
639 						     struct ecore_spq_entry,
640 						     list);
641 			OSAL_LIST_REMOVE_ENTRY(&p_en2->list, &p_spq->free_pool);
642 
643 			/* Copy the ring element physical pointer to the new
644 			 * entry, since we are about to override the entire ring
645 			 * entry and don't want to lose the pointer.
646 			 */
647 			p_ent->elem.data_ptr = p_en2->elem.data_ptr;
648 
649 			*p_en2 = *p_ent;
650 
651 			/* EBLOCK responsible to free the allocated p_ent */
652 			if (p_ent->comp_mode != ECORE_SPQ_MODE_EBLOCK)
653 				OSAL_FREE(p_hwfn->p_dev, p_ent);
654 
655 			p_ent = p_en2;
656 		}
657 	}
658 
659 	/* entry is to be placed in 'pending' queue */
660 	switch (priority) {
661 	case ECORE_SPQ_PRIORITY_NORMAL:
662 		OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_spq->pending);
663 		p_spq->normal_count++;
664 		break;
665 	case ECORE_SPQ_PRIORITY_HIGH:
666 		OSAL_LIST_PUSH_HEAD(&p_ent->list, &p_spq->pending);
667 		p_spq->high_count++;
668 		break;
669 	default:
670 		return ECORE_INVAL;
671 	}
672 
673 	return ECORE_SUCCESS;
674 }
675 
676 /***************************************************************************
677  * Accessor
678  ***************************************************************************/
679 
680 u32 ecore_spq_get_cid(struct ecore_hwfn *p_hwfn)
681 {
682 	if (!p_hwfn->p_spq)
683 		return 0xffffffff;	/* illegal */
684 	return p_hwfn->p_spq->cid;
685 }
686 
687 /***************************************************************************
688  * Posting new Ramrods
689  ***************************************************************************/
690 
691 static enum _ecore_status_t ecore_spq_post_list(struct ecore_hwfn *p_hwfn,
692 						osal_list_t *head,
693 						u32 keep_reserve)
694 {
695 	struct ecore_spq *p_spq = p_hwfn->p_spq;
696 	enum _ecore_status_t rc;
697 
698 	/* TODO - implementation might be wasteful; will always keep room
699 	 * for an additional high priority ramrod (even if one is already
700 	 * pending FW)
701 	 */
702 	while (ecore_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
703 	       !OSAL_LIST_IS_EMPTY(head)) {
704 		struct ecore_spq_entry *p_ent =
705 		    OSAL_LIST_FIRST_ENTRY(head, struct ecore_spq_entry, list);
706 		if (p_ent != OSAL_NULL) {
707 #if defined(_NTDDK_)
708 #pragma warning(suppress : 6011 28182)
709 #endif
710 			OSAL_LIST_REMOVE_ENTRY(&p_ent->list, head);
711 			OSAL_LIST_PUSH_TAIL(&p_ent->list,
712 					    &p_spq->completion_pending);
713 			p_spq->comp_sent_count++;
714 
715 			rc = ecore_spq_hw_post(p_hwfn, p_spq, p_ent);
716 			if (rc) {
717 				OSAL_LIST_REMOVE_ENTRY(&p_ent->list,
718 						    &p_spq->completion_pending);
719 				__ecore_spq_return_entry(p_hwfn, p_ent);
720 				return rc;
721 			}
722 		}
723 	}
724 
725 	return ECORE_SUCCESS;
726 }
727 
728 static enum _ecore_status_t ecore_spq_pend_post(struct ecore_hwfn *p_hwfn)
729 {
730 	struct ecore_spq *p_spq = p_hwfn->p_spq;
731 	struct ecore_spq_entry *p_ent = OSAL_NULL;
732 
733 	while (!OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
734 		if (OSAL_LIST_IS_EMPTY(&p_spq->unlimited_pending))
735 			break;
736 
737 		p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->unlimited_pending,
738 					      struct ecore_spq_entry, list);
739 		if (!p_ent)
740 			return ECORE_INVAL;
741 
742 #if defined(_NTDDK_)
743 #pragma warning(suppress : 6011)
744 #endif
745 		OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->unlimited_pending);
746 
747 		ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
748 	}
749 
750 	return ecore_spq_post_list(p_hwfn,
751 				 &p_spq->pending, SPQ_HIGH_PRI_RESERVE_DEFAULT);
752 }
753 
754 enum _ecore_status_t ecore_spq_post(struct ecore_hwfn *p_hwfn,
755 				    struct ecore_spq_entry *p_ent,
756 				    u8 *fw_return_code)
757 {
758 	enum _ecore_status_t rc = ECORE_SUCCESS;
759 	struct ecore_spq *p_spq = p_hwfn ? p_hwfn->p_spq : OSAL_NULL;
760 	bool b_ret_ent = true;
761 
762 	if (!p_hwfn)
763 		return ECORE_INVAL;
764 
765 	if (!p_ent) {
766 		DP_NOTICE(p_hwfn, true, "Got a NULL pointer\n");
767 		return ECORE_INVAL;
768 	}
769 
770 	if (p_hwfn->p_dev->recov_in_prog) {
771 		DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
772 			   "Recovery is in progress -> skip spq post"
773 			   " [cmd %02x protocol %02x]\n",
774 			   p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id);
775 		/* Return success to let the flows to be completed successfully
776 		 * w/o any error handling.
777 		 */
778 		return ECORE_SUCCESS;
779 	}
780 
781 	OSAL_SPIN_LOCK(&p_spq->lock);
782 
783 	/* Complete the entry */
784 	rc = ecore_spq_fill_entry(p_hwfn, p_ent);
785 
786 	/* Check return value after LOCK is taken for cleaner error flow */
787 	if (rc)
788 		goto spq_post_fail;
789 
790 	/* Add the request to the pending queue */
791 	rc = ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
792 	if (rc)
793 		goto spq_post_fail;
794 
795 	rc = ecore_spq_pend_post(p_hwfn);
796 	if (rc) {
797 		/* Since it's possible that pending failed for a different
798 		 * entry [although unlikely], the failed entry was already
799 		 * dealt with; No need to return it here.
800 		 */
801 		b_ret_ent = false;
802 		goto spq_post_fail;
803 	}
804 
805 	OSAL_SPIN_UNLOCK(&p_spq->lock);
806 
807 	if (p_ent->comp_mode == ECORE_SPQ_MODE_EBLOCK) {
808 		/* For entries in ECORE BLOCK mode, the completion code cannot
809 		 * perform the necessary cleanup - if it did, we couldn't
810 		 * access p_ent here to see whether it's successful or not.
811 		 * Thus, after gaining the answer perform the cleanup here.
812 		 */
813 		rc = ecore_spq_block(p_hwfn, p_ent, fw_return_code,
814 				     p_ent->queue == &p_spq->unlimited_pending);
815 
816 		if (p_ent->queue == &p_spq->unlimited_pending) {
817 			/* This is an allocated p_ent which does not need to
818 			 * return to pool.
819 			 */
820 			OSAL_FREE(p_hwfn->p_dev, p_ent);
821 
822 			/* TBD: handle error flow and remove p_ent from
823 			 * completion pending
824 			 */
825 			return rc;
826 		}
827 
828 		if (rc)
829 			goto spq_post_fail2;
830 
831 		/* return to pool */
832 		ecore_spq_return_entry(p_hwfn, p_ent);
833 	}
834 	return rc;
835 
836 spq_post_fail2:
837 	OSAL_SPIN_LOCK(&p_spq->lock);
838 	OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->completion_pending);
839 	ecore_chain_return_produced(&p_spq->chain);
840 
841 spq_post_fail:
842 	/* return to the free pool */
843 	if (b_ret_ent)
844 		__ecore_spq_return_entry(p_hwfn, p_ent);
845 	OSAL_SPIN_UNLOCK(&p_spq->lock);
846 
847 	return rc;
848 }
849 
850 enum _ecore_status_t ecore_spq_completion(struct ecore_hwfn *p_hwfn,
851 					  __le16 echo,
852 					  u8 fw_return_code,
853 					  union event_ring_data *p_data)
854 {
855 	struct ecore_spq *p_spq;
856 	struct ecore_spq_entry *p_ent = OSAL_NULL;
857 	struct ecore_spq_entry *tmp;
858 	struct ecore_spq_entry *found = OSAL_NULL;
859 	enum _ecore_status_t rc;
860 
861 	if (!p_hwfn)
862 		return ECORE_INVAL;
863 
864 	p_spq = p_hwfn->p_spq;
865 	if (!p_spq)
866 		return ECORE_INVAL;
867 
868 	OSAL_SPIN_LOCK(&p_spq->lock);
869 	OSAL_LIST_FOR_EACH_ENTRY_SAFE(p_ent,
870 				      tmp,
871 				      &p_spq->completion_pending,
872 				      list, struct ecore_spq_entry) {
873 		if (p_ent->elem.hdr.echo == echo) {
874 			OSAL_LIST_REMOVE_ENTRY(&p_ent->list,
875 					       &p_spq->completion_pending);
876 
877 			/* Avoid overriding of SPQ entries when getting
878 			 * out-of-order completions, by marking the completions
879 			 * in a bitmap and increasing the chain consumer only
880 			 * for the first successive completed entries.
881 			 */
882 			SPQ_COMP_BMAP_SET_BIT(p_spq, echo);
883 			while (SPQ_COMP_BMAP_TEST_BIT(p_spq,
884 						      p_spq->comp_bitmap_idx)) {
885 				SPQ_COMP_BMAP_CLEAR_BIT(p_spq,
886 							p_spq->comp_bitmap_idx);
887 				p_spq->comp_bitmap_idx++;
888 				ecore_chain_return_produced(&p_spq->chain);
889 			}
890 
891 			p_spq->comp_count++;
892 			found = p_ent;
893 			break;
894 		}
895 
896 		/* This is debug and should be relatively uncommon - depends
897 		 * on scenarios which have mutliple per-PF sent ramrods.
898 		 */
899 		DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
900 			   "Got completion for echo %04x - doesn't match"
901 			   " echo %04x in completion pending list\n",
902 			   OSAL_LE16_TO_CPU(echo),
903 			   OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo));
904 	}
905 
906 	/* Release lock before callback, as callback may post
907 	 * an additional ramrod.
908 	 */
909 	OSAL_SPIN_UNLOCK(&p_spq->lock);
910 
911 	if (!found) {
912 		DP_NOTICE(p_hwfn, true,
913 			  "Failed to find an entry this"
914 			  " EQE [echo %04x] completes\n",
915 			  OSAL_LE16_TO_CPU(echo));
916 		return ECORE_EXISTS;
917 	}
918 
919 	DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
920 		   "Complete EQE [echo %04x]: func %p cookie %p)\n",
921 		   OSAL_LE16_TO_CPU(echo),
922 		   p_ent->comp_cb.function, p_ent->comp_cb.cookie);
923 	if (found->comp_cb.function)
924 		found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
925 					fw_return_code);
926 	else
927 		DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
928 			   "Got a completion without a callback function\n");
929 
930 	if ((found->comp_mode != ECORE_SPQ_MODE_EBLOCK) ||
931 	    (found->queue == &p_spq->unlimited_pending))
932 		/* EBLOCK  is responsible for returning its own entry into the
933 		 * free list, unless it originally added the entry into the
934 		 * unlimited pending list.
935 		 */
936 		ecore_spq_return_entry(p_hwfn, found);
937 
938 	/* Attempt to post pending requests */
939 	OSAL_SPIN_LOCK(&p_spq->lock);
940 	rc = ecore_spq_pend_post(p_hwfn);
941 	OSAL_SPIN_UNLOCK(&p_spq->lock);
942 
943 	return rc;
944 }
945 
946 struct ecore_consq *ecore_consq_alloc(struct ecore_hwfn *p_hwfn)
947 {
948 	struct ecore_consq *p_consq;
949 
950 	/* Allocate ConsQ struct */
951 	p_consq =
952 	    OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_consq));
953 	if (!p_consq) {
954 		DP_NOTICE(p_hwfn, true,
955 			  "Failed to allocate `struct ecore_consq'\n");
956 		return OSAL_NULL;
957 	}
958 
959 	/* Allocate and initialize EQ chain */
960 	if (ecore_chain_alloc(p_hwfn->p_dev,
961 			      ECORE_CHAIN_USE_TO_PRODUCE,
962 			      ECORE_CHAIN_MODE_PBL,
963 			      ECORE_CHAIN_CNT_TYPE_U16,
964 			      ECORE_CHAIN_PAGE_SIZE / 0x80,
965 			      0x80,
966 			      &p_consq->chain, OSAL_NULL)) {
967 		DP_NOTICE(p_hwfn, true, "Failed to allocate consq chain");
968 		goto consq_allocate_fail;
969 	}
970 
971 	return p_consq;
972 
973 consq_allocate_fail:
974 	ecore_consq_free(p_hwfn, p_consq);
975 	return OSAL_NULL;
976 }
977 
978 void ecore_consq_setup(struct ecore_hwfn *p_hwfn, struct ecore_consq *p_consq)
979 {
980 	ecore_chain_reset(&p_consq->chain);
981 }
982 
983 void ecore_consq_free(struct ecore_hwfn *p_hwfn, struct ecore_consq *p_consq)
984 {
985 	if (!p_consq)
986 		return;
987 	ecore_chain_free(p_hwfn->p_dev, &p_consq->chain);
988 	OSAL_FREE(p_hwfn->p_dev, p_consq);
989 }
990