2 * Copyright (c) 2017-2018 Cavium, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include "ecore_gtt_reg_addr.h"
36 #include "ecore_hsi_common.h"
38 #include "ecore_sp_api.h"
39 #include "ecore_spq.h"
40 #include "ecore_iro.h"
41 #include "ecore_init_fw_funcs.h"
42 #include "ecore_cxt.h"
43 #include "ecore_int.h"
44 #include "ecore_dev_api.h"
45 #include "ecore_mcp.h"
46 #ifdef CONFIG_ECORE_RDMA
47 #include "ecore_rdma.h"
50 #include "ecore_sriov.h"
51 #ifdef CONFIG_ECORE_ISCSI
52 #include "ecore_iscsi.h"
53 #include "ecore_ooo.h"
58 #pragma warning(disable : 28167)
59 #pragma warning(disable : 28123)
62 /***************************************************************************
63 * Structures & Definitions
64 ***************************************************************************/
66 #define SPQ_HIGH_PRI_RESERVE_DEFAULT (1)
68 #define SPQ_BLOCK_DELAY_MAX_ITER (10)
69 #define SPQ_BLOCK_DELAY_US (10)
70 #define SPQ_BLOCK_SLEEP_MAX_ITER (200)
71 #define SPQ_BLOCK_SLEEP_MS (5)
74 /***************************************************************************
76 ***************************************************************************/
77 static void ecore_iscsi_eq_dump(struct ecore_hwfn *p_hwfn,
78 struct event_ring_entry *p_eqe)
80 if (p_eqe->opcode >= MAX_ISCSI_EQE_OPCODE) {
81 DP_NOTICE(p_hwfn, false, "Unknown iSCSI EQ: %x\n",
85 switch (p_eqe->opcode) {
86 case ISCSI_EVENT_TYPE_INIT_FUNC:
87 case ISCSI_EVENT_TYPE_DESTROY_FUNC:
90 case ISCSI_EVENT_TYPE_OFFLOAD_CONN:
91 case ISCSI_EVENT_TYPE_TERMINATE_CONN:
92 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
93 "iSCSI EQE: Port %x, Op %x, echo %x, FWret %x, CID %x, ConnID %x, ERR %x\n",
94 p_hwfn->port_id, p_eqe->opcode,
95 OSAL_LE16_TO_CPU(p_eqe->echo),
96 p_eqe->fw_return_code,
97 OSAL_LE16_TO_CPU(p_eqe->data.iscsi_info.icid),
98 OSAL_LE16_TO_CPU(p_eqe->data.iscsi_info.conn_id),
99 p_eqe->data.iscsi_info.error_code);
101 case ISCSI_EVENT_TYPE_UPDATE_CONN:
102 case ISCSI_EVENT_TYPE_CLEAR_SQ:
103 case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
104 case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
105 case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
106 case ISCSI_EVENT_TYPE_ASYN_CLOSE_RCVD:
107 case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
108 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
109 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
110 case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
111 case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
112 case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
113 case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
121 /***************************************************************************
122 * Blocking Imp. (BLOCK/EBLOCK mode)
123 ***************************************************************************/
124 static void ecore_spq_blocking_cb(struct ecore_hwfn *p_hwfn, void *cookie,
125 union event_ring_data OSAL_UNUSED *data,
128 struct ecore_spq_comp_done *comp_done;
130 comp_done = (struct ecore_spq_comp_done *)cookie;
132 comp_done->done = 0x1;
133 comp_done->fw_return_code = fw_return_code;
135 /* make update visible to waiting thread */
136 OSAL_SMP_WMB(p_hwfn->p_dev);
139 static enum _ecore_status_t __ecore_spq_block(struct ecore_hwfn *p_hwfn,
140 struct ecore_spq_entry *p_ent,
142 bool sleep_between_iter)
144 struct ecore_spq_comp_done *comp_done;
147 comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie;
148 iter_cnt = sleep_between_iter ? SPQ_BLOCK_SLEEP_MAX_ITER
149 : SPQ_BLOCK_DELAY_MAX_ITER;
151 if (CHIP_REV_IS_EMUL(p_hwfn->p_dev) && sleep_between_iter)
156 OSAL_POLL_MODE_DPC(p_hwfn);
157 OSAL_SMP_RMB(p_hwfn->p_dev);
158 if (comp_done->done == 1) {
160 *p_fw_ret = comp_done->fw_return_code;
161 return ECORE_SUCCESS;
164 if (sleep_between_iter) {
165 OSAL_MSLEEP(SPQ_BLOCK_SLEEP_MS);
167 OSAL_UDELAY(SPQ_BLOCK_DELAY_US);
171 return ECORE_TIMEOUT;
174 static enum _ecore_status_t ecore_spq_block(struct ecore_hwfn *p_hwfn,
175 struct ecore_spq_entry *p_ent,
176 u8 *p_fw_ret, bool skip_quick_poll)
178 struct ecore_spq_comp_done *comp_done;
179 struct ecore_ptt *p_ptt;
180 enum _ecore_status_t rc;
182 /* A relatively short polling period w/o sleeping, to allow the FW to
183 * complete the ramrod and thus possibly to avoid the following sleeps.
185 if (!skip_quick_poll) {
186 rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, false);
187 if (rc == ECORE_SUCCESS)
188 return ECORE_SUCCESS;
191 /* Move to polling with a sleeping period between iterations */
192 rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true);
193 if (rc == ECORE_SUCCESS)
194 return ECORE_SUCCESS;
196 p_ptt = ecore_ptt_acquire(p_hwfn);
200 DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
201 rc = ecore_mcp_drain(p_hwfn, p_ptt);
202 ecore_ptt_release(p_hwfn, p_ptt);
203 if (rc != ECORE_SUCCESS) {
204 DP_NOTICE(p_hwfn, true, "MCP drain failed\n");
208 /* Retry after drain */
209 rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true);
210 if (rc == ECORE_SUCCESS)
211 return ECORE_SUCCESS;
213 comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie;
214 if (comp_done->done == 1) {
216 *p_fw_ret = comp_done->fw_return_code;
217 return ECORE_SUCCESS;
220 DP_NOTICE(p_hwfn, true,
221 "Ramrod is stuck [CID %08x cmd %02x protocol %02x echo %04x]\n",
222 OSAL_LE32_TO_CPU(p_ent->elem.hdr.cid),
223 p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id,
224 OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo));
226 ecore_hw_err_notify(p_hwfn, ECORE_HW_ERR_RAMROD_FAIL);
231 /***************************************************************************
232 * SPQ entries inner API
233 ***************************************************************************/
234 static enum _ecore_status_t ecore_spq_fill_entry(struct ecore_hwfn *p_hwfn,
235 struct ecore_spq_entry *p_ent)
239 switch (p_ent->comp_mode) {
240 case ECORE_SPQ_MODE_EBLOCK:
241 case ECORE_SPQ_MODE_BLOCK:
242 p_ent->comp_cb.function = ecore_spq_blocking_cb;
244 case ECORE_SPQ_MODE_CB:
247 DP_NOTICE(p_hwfn, true, "Unknown SPQE completion mode %d\n",
252 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
253 "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x] Data pointer: [%08x:%08x] Completion Mode: %s\n",
254 p_ent->elem.hdr.cid, p_ent->elem.hdr.cmd_id,
255 p_ent->elem.hdr.protocol_id,
256 p_ent->elem.data_ptr.hi, p_ent->elem.data_ptr.lo,
257 D_TRINE(p_ent->comp_mode, ECORE_SPQ_MODE_EBLOCK,
258 ECORE_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
261 return ECORE_SUCCESS;
264 /***************************************************************************
266 ***************************************************************************/
267 static void ecore_spq_hw_initialize(struct ecore_hwfn *p_hwfn,
268 struct ecore_spq *p_spq)
270 struct e4_core_conn_context *p_cxt;
271 struct ecore_cxt_info cxt_info;
273 enum _ecore_status_t rc;
275 cxt_info.iid = p_spq->cid;
277 rc = ecore_cxt_get_cid_info(p_hwfn, &cxt_info);
280 DP_NOTICE(p_hwfn, true, "Cannot find context info for cid=%d\n",
285 p_cxt = cxt_info.p_cxt;
287 /* @@@TBD we zero the context until we have ilt_reset implemented. */
288 OSAL_MEM_ZERO(p_cxt, sizeof(*p_cxt));
290 if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev)) {
291 SET_FIELD(p_cxt->xstorm_ag_context.flags10,
292 E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
293 SET_FIELD(p_cxt->xstorm_ag_context.flags1,
294 E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
295 /*SET_FIELD(p_cxt->xstorm_ag_context.flags10,
296 E4_XSTORM_CORE_CONN_AG_CTX_SLOW_PATH_EN, 1);*/
297 SET_FIELD(p_cxt->xstorm_ag_context.flags9,
298 E4_XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
300 ECORE_E5_MISSING_CODE;
303 /* CDU validation - FIXME currently disabled */
305 /* QM physical queue */
306 physical_q = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
307 p_cxt->xstorm_ag_context.physical_q0 = OSAL_CPU_TO_LE16(physical_q);
309 p_cxt->xstorm_st_context.spq_base_lo =
310 DMA_LO_LE(p_spq->chain.p_phys_addr);
311 p_cxt->xstorm_st_context.spq_base_hi =
312 DMA_HI_LE(p_spq->chain.p_phys_addr);
314 DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
315 p_hwfn->p_consq->chain.p_phys_addr);
318 static enum _ecore_status_t ecore_spq_hw_post(struct ecore_hwfn *p_hwfn,
319 struct ecore_spq *p_spq,
320 struct ecore_spq_entry *p_ent)
322 struct ecore_chain *p_chain = &p_hwfn->p_spq->chain;
323 struct core_db_data *p_db_data = &p_spq->db_data;
324 u16 echo = ecore_chain_get_prod_idx(p_chain);
325 struct slow_path_element *elem;
327 p_ent->elem.hdr.echo = OSAL_CPU_TO_LE16(echo);
328 elem = ecore_chain_produce(p_chain);
330 DP_NOTICE(p_hwfn, true, "Failed to produce from SPQ chain\n");
334 *elem = p_ent->elem; /* Struct assignment */
336 p_db_data->spq_prod =
337 OSAL_CPU_TO_LE16(ecore_chain_get_prod_idx(p_chain));
339 /* Make sure the SPQE is updated before the doorbell */
340 OSAL_WMB(p_hwfn->p_dev);
342 DOORBELL(p_hwfn, p_spq->db_addr_offset, *(u32 *)p_db_data);
344 /* Make sure doorbell was rung */
345 OSAL_WMB(p_hwfn->p_dev);
347 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
348 "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
349 p_spq->db_addr_offset, p_spq->cid, p_db_data->params,
350 p_db_data->agg_flags, ecore_chain_get_prod_idx(p_chain));
352 return ECORE_SUCCESS;
355 /***************************************************************************
356 * Asynchronous events
357 ***************************************************************************/
359 static enum _ecore_status_t
360 ecore_async_event_completion(struct ecore_hwfn *p_hwfn,
361 struct event_ring_entry *p_eqe)
363 ecore_spq_async_comp_cb cb;
365 if (!p_hwfn->p_spq || (p_eqe->protocol_id >= MAX_PROTOCOL_TYPE)) {
369 cb = p_hwfn->p_spq->async_comp_cb[p_eqe->protocol_id];
371 return cb(p_hwfn, p_eqe->opcode, p_eqe->echo,
372 &p_eqe->data, p_eqe->fw_return_code);
375 true, "Unknown Async completion for protocol: %d\n",
382 ecore_spq_register_async_cb(struct ecore_hwfn *p_hwfn,
383 enum protocol_type protocol_id,
384 ecore_spq_async_comp_cb cb)
386 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE)) {
390 p_hwfn->p_spq->async_comp_cb[protocol_id] = cb;
391 return ECORE_SUCCESS;
395 ecore_spq_unregister_async_cb(struct ecore_hwfn *p_hwfn,
396 enum protocol_type protocol_id)
398 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE)) {
402 p_hwfn->p_spq->async_comp_cb[protocol_id] = OSAL_NULL;
405 /***************************************************************************
407 ***************************************************************************/
408 void ecore_eq_prod_update(struct ecore_hwfn *p_hwfn,
411 u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
412 USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
414 REG_WR16(p_hwfn, addr, prod);
416 /* keep prod updates ordered */
417 OSAL_MMIOWB(p_hwfn->p_dev);
420 enum _ecore_status_t ecore_eq_completion(struct ecore_hwfn *p_hwfn,
424 struct ecore_eq *p_eq = cookie;
425 struct ecore_chain *p_chain = &p_eq->chain;
426 enum _ecore_status_t rc = 0;
428 /* take a snapshot of the FW consumer */
429 u16 fw_cons_idx = OSAL_LE16_TO_CPU(*p_eq->p_fw_cons);
431 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
433 /* Need to guarantee the fw_cons index we use points to a usuable
434 * element (to comply with our chain), so our macros would comply
436 if ((fw_cons_idx & ecore_chain_get_usable_per_page(p_chain)) ==
437 ecore_chain_get_usable_per_page(p_chain)) {
438 fw_cons_idx += ecore_chain_get_unusable_per_page(p_chain);
441 /* Complete current segment of eq entries */
442 while (fw_cons_idx != ecore_chain_get_cons_idx(p_chain)) {
443 struct event_ring_entry *p_eqe = ecore_chain_consume(p_chain);
451 "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
452 p_eqe->opcode, /* Event Opcode */
453 p_eqe->protocol_id, /* Event Protocol ID */
454 p_eqe->reserved0, /* Reserved */
455 OSAL_LE16_TO_CPU(p_eqe->echo),/* Echo value from
456 ramrod data on the host
458 p_eqe->fw_return_code, /* FW return code for SP
463 if (p_eqe->protocol_id == PROTOCOLID_ISCSI)
464 ecore_iscsi_eq_dump(p_hwfn, p_eqe);
467 if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
468 if (ecore_async_event_completion(p_hwfn, p_eqe))
470 } else if (ecore_spq_completion(p_hwfn,
472 p_eqe->fw_return_code,
477 ecore_chain_recycle_consumed(p_chain);
480 ecore_eq_prod_update(p_hwfn, ecore_chain_get_prod_idx(p_chain));
482 /* Attempt to post pending requests */
483 OSAL_SPIN_LOCK(&p_hwfn->p_spq->lock);
484 rc = ecore_spq_pend_post(p_hwfn);
485 OSAL_SPIN_UNLOCK(&p_hwfn->p_spq->lock);
490 enum _ecore_status_t ecore_eq_alloc(struct ecore_hwfn *p_hwfn, u16 num_elem)
492 struct ecore_eq *p_eq;
494 /* Allocate EQ struct */
495 p_eq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_eq));
497 DP_NOTICE(p_hwfn, false,
498 "Failed to allocate `struct ecore_eq'\n");
502 /* Allocate and initialize EQ chain*/
503 if (ecore_chain_alloc(p_hwfn->p_dev,
504 ECORE_CHAIN_USE_TO_PRODUCE,
505 ECORE_CHAIN_MODE_PBL,
506 ECORE_CHAIN_CNT_TYPE_U16,
508 sizeof(union event_ring_element),
509 &p_eq->chain, OSAL_NULL) != ECORE_SUCCESS) {
510 DP_NOTICE(p_hwfn, false, "Failed to allocate eq chain\n");
511 goto eq_allocate_fail;
514 /* register EQ completion on the SP SB */
515 ecore_int_register_cb(p_hwfn, ecore_eq_completion,
516 p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
519 return ECORE_SUCCESS;
522 OSAL_FREE(p_hwfn->p_dev, p_eq);
526 void ecore_eq_setup(struct ecore_hwfn *p_hwfn)
528 ecore_chain_reset(&p_hwfn->p_eq->chain);
531 void ecore_eq_free(struct ecore_hwfn *p_hwfn)
536 ecore_chain_free(p_hwfn->p_dev, &p_hwfn->p_eq->chain);
538 OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_eq);
539 p_hwfn->p_eq = OSAL_NULL;
542 /***************************************************************************
543 * CQE API - manipulate EQ functionallity
544 ***************************************************************************/
545 static enum _ecore_status_t ecore_cqe_completion(struct ecore_hwfn *p_hwfn,
546 struct eth_slow_path_rx_cqe *cqe,
547 enum protocol_type protocol)
549 if (IS_VF(p_hwfn->p_dev))
550 return OSAL_VF_CQE_COMPLETION(p_hwfn, cqe, protocol);
552 /* @@@tmp - it's possible we'll eventually want to handle some
553 * actual commands that can arrive here, but for now this is only
554 * used to complete the ramrod using the echo value on the cqe
556 return ecore_spq_completion(p_hwfn, cqe->echo, 0, OSAL_NULL);
559 enum _ecore_status_t ecore_eth_cqe_completion(struct ecore_hwfn *p_hwfn,
560 struct eth_slow_path_rx_cqe *cqe)
562 enum _ecore_status_t rc;
564 rc = ecore_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
566 DP_NOTICE(p_hwfn, true,
567 "Failed to handle RXQ CQE [cmd 0x%02x]\n",
574 /***************************************************************************
575 * Slow hwfn Queue (spq)
576 ***************************************************************************/
577 void ecore_spq_setup(struct ecore_hwfn *p_hwfn)
579 struct ecore_spq *p_spq = p_hwfn->p_spq;
580 struct ecore_spq_entry *p_virt = OSAL_NULL;
581 struct core_db_data *p_db_data;
582 void OSAL_IOMEM *db_addr;
583 dma_addr_t p_phys = 0;
585 enum _ecore_status_t rc;
587 OSAL_LIST_INIT(&p_spq->pending);
588 OSAL_LIST_INIT(&p_spq->completion_pending);
589 OSAL_LIST_INIT(&p_spq->free_pool);
590 OSAL_LIST_INIT(&p_spq->unlimited_pending);
591 OSAL_SPIN_LOCK_INIT(&p_spq->lock);
594 p_phys = p_spq->p_phys + OFFSETOF(struct ecore_spq_entry, ramrod);
595 p_virt = p_spq->p_virt;
597 capacity = ecore_chain_get_capacity(&p_spq->chain);
598 for (i = 0; i < capacity; i++) {
599 DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
601 OSAL_LIST_PUSH_TAIL(&p_virt->list, &p_spq->free_pool);
604 p_phys += sizeof(struct ecore_spq_entry);
608 p_spq->normal_count = 0;
609 p_spq->comp_count = 0;
610 p_spq->comp_sent_count = 0;
611 p_spq->unlimited_pending_count = 0;
613 OSAL_MEM_ZERO(p_spq->p_comp_bitmap,
614 SPQ_COMP_BMAP_SIZE * sizeof(unsigned long));
615 p_spq->comp_bitmap_idx = 0;
617 /* SPQ cid, cannot fail */
618 ecore_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
619 ecore_spq_hw_initialize(p_hwfn, p_spq);
621 /* reset the chain itself */
622 ecore_chain_reset(&p_spq->chain);
624 /* Initialize the address/data of the SPQ doorbell */
625 p_spq->db_addr_offset = DB_ADDR(p_spq->cid, DQ_DEMS_LEGACY);
626 p_db_data = &p_spq->db_data;
627 OSAL_MEM_ZERO(p_db_data, sizeof(*p_db_data));
628 SET_FIELD(p_db_data->params, CORE_DB_DATA_DEST, DB_DEST_XCM);
629 SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_MAX);
630 SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_VAL_SEL,
631 DQ_XCM_CORE_SPQ_PROD_CMD);
632 p_db_data->agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
634 /* Register the SPQ doorbell with the doorbell recovery mechanism */
635 db_addr = (void *)((u8 *)p_hwfn->doorbells + p_spq->db_addr_offset);
636 rc = ecore_db_recovery_add(p_hwfn->p_dev, db_addr, &p_spq->db_data,
637 DB_REC_WIDTH_32B, DB_REC_KERNEL);
638 if (rc != ECORE_SUCCESS)
640 "Failed to register the SPQ doorbell with the doorbell recovery mechanism\n");
643 enum _ecore_status_t ecore_spq_alloc(struct ecore_hwfn *p_hwfn)
645 struct ecore_spq_entry *p_virt = OSAL_NULL;
646 struct ecore_spq *p_spq = OSAL_NULL;
647 dma_addr_t p_phys = 0;
652 OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(struct ecore_spq));
654 DP_NOTICE(p_hwfn, false, "Failed to allocate `struct ecore_spq'\n");
659 if (ecore_chain_alloc(p_hwfn->p_dev,
660 ECORE_CHAIN_USE_TO_PRODUCE,
661 ECORE_CHAIN_MODE_SINGLE,
662 ECORE_CHAIN_CNT_TYPE_U16,
663 0, /* N/A when the mode is SINGLE */
664 sizeof(struct slow_path_element),
665 &p_spq->chain, OSAL_NULL)) {
666 DP_NOTICE(p_hwfn, false, "Failed to allocate spq chain\n");
667 goto spq_allocate_fail;
670 /* allocate and fill the SPQ elements (incl. ramrod data list) */
671 capacity = ecore_chain_get_capacity(&p_spq->chain);
672 p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, &p_phys,
674 sizeof(struct ecore_spq_entry));
676 goto spq_allocate_fail;
679 p_spq->p_virt = p_virt;
680 p_spq->p_phys = p_phys;
682 #ifdef CONFIG_ECORE_LOCK_ALLOC
683 if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_spq->lock))
684 goto spq_allocate_fail;
687 p_hwfn->p_spq = p_spq;
688 return ECORE_SUCCESS;
691 ecore_chain_free(p_hwfn->p_dev, &p_spq->chain);
692 OSAL_FREE(p_hwfn->p_dev, p_spq);
696 void ecore_spq_free(struct ecore_hwfn *p_hwfn)
698 struct ecore_spq *p_spq = p_hwfn->p_spq;
699 void OSAL_IOMEM *db_addr;
705 /* Delete the SPQ doorbell from the doorbell recovery mechanism */
706 db_addr = (void *)((u8 *)p_hwfn->doorbells + p_spq->db_addr_offset);
707 ecore_db_recovery_del(p_hwfn->p_dev, db_addr, &p_spq->db_data);
710 capacity = ecore_chain_get_capacity(&p_spq->chain);
711 OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
715 sizeof(struct ecore_spq_entry));
718 ecore_chain_free(p_hwfn->p_dev, &p_spq->chain);
719 #ifdef CONFIG_ECORE_LOCK_ALLOC
720 OSAL_SPIN_LOCK_DEALLOC(&p_spq->lock);
723 OSAL_FREE(p_hwfn->p_dev, p_spq);
724 p_hwfn->p_spq = OSAL_NULL;
727 enum _ecore_status_t ecore_spq_get_entry(struct ecore_hwfn *p_hwfn,
728 struct ecore_spq_entry **pp_ent)
730 struct ecore_spq *p_spq = p_hwfn->p_spq;
731 struct ecore_spq_entry *p_ent = OSAL_NULL;
732 enum _ecore_status_t rc = ECORE_SUCCESS;
734 OSAL_SPIN_LOCK(&p_spq->lock);
736 if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
737 p_ent = OSAL_ZALLOC(p_hwfn->p_dev, GFP_ATOMIC, sizeof(*p_ent));
739 DP_NOTICE(p_hwfn, false, "Failed to allocate an SPQ entry for a pending ramrod\n");
743 p_ent->queue = &p_spq->unlimited_pending;
745 p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool,
746 struct ecore_spq_entry,
748 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->free_pool);
749 p_ent->queue = &p_spq->pending;
755 OSAL_SPIN_UNLOCK(&p_spq->lock);
759 /* Locked variant; Should be called while the SPQ lock is taken */
760 static void __ecore_spq_return_entry(struct ecore_hwfn *p_hwfn,
761 struct ecore_spq_entry *p_ent)
763 OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_hwfn->p_spq->free_pool);
766 void ecore_spq_return_entry(struct ecore_hwfn *p_hwfn,
767 struct ecore_spq_entry *p_ent)
769 OSAL_SPIN_LOCK(&p_hwfn->p_spq->lock);
770 __ecore_spq_return_entry(p_hwfn, p_ent);
771 OSAL_SPIN_UNLOCK(&p_hwfn->p_spq->lock);
775 * @brief ecore_spq_add_entry - adds a new entry to the pending
776 * list. Should be used while lock is being held.
778 * Addes an entry to the pending list is there is room (en empty
779 * element is avaliable in the free_pool), or else places the
780 * entry in the unlimited_pending pool.
786 * @return enum _ecore_status_t
788 static enum _ecore_status_t ecore_spq_add_entry(struct ecore_hwfn *p_hwfn,
789 struct ecore_spq_entry *p_ent,
790 enum spq_priority priority)
792 struct ecore_spq *p_spq = p_hwfn->p_spq;
794 if (p_ent->queue == &p_spq->unlimited_pending) {
795 if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
796 OSAL_LIST_PUSH_TAIL(&p_ent->list,
797 &p_spq->unlimited_pending);
798 p_spq->unlimited_pending_count++;
800 return ECORE_SUCCESS;
803 struct ecore_spq_entry *p_en2;
805 p_en2 = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool,
806 struct ecore_spq_entry,
808 OSAL_LIST_REMOVE_ENTRY(&p_en2->list, &p_spq->free_pool);
810 /* Copy the ring element physical pointer to the new
811 * entry, since we are about to override the entire ring
812 * entry and don't want to lose the pointer.
814 p_ent->elem.data_ptr = p_en2->elem.data_ptr;
818 /* EBLOCK responsible to free the allocated p_ent */
819 if (p_ent->comp_mode != ECORE_SPQ_MODE_EBLOCK)
820 OSAL_FREE(p_hwfn->p_dev, p_ent);
826 /* entry is to be placed in 'pending' queue */
828 case ECORE_SPQ_PRIORITY_NORMAL:
829 OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_spq->pending);
830 p_spq->normal_count++;
832 case ECORE_SPQ_PRIORITY_HIGH:
833 OSAL_LIST_PUSH_HEAD(&p_ent->list, &p_spq->pending);
840 return ECORE_SUCCESS;
843 /***************************************************************************
845 ***************************************************************************/
847 u32 ecore_spq_get_cid(struct ecore_hwfn *p_hwfn)
849 if (!p_hwfn->p_spq) {
850 return 0xffffffff; /* illegal */
852 return p_hwfn->p_spq->cid;
855 /***************************************************************************
856 * Posting new Ramrods
857 ***************************************************************************/
859 static enum _ecore_status_t ecore_spq_post_list(struct ecore_hwfn *p_hwfn,
863 struct ecore_spq *p_spq = p_hwfn->p_spq;
864 enum _ecore_status_t rc;
866 /* TODO - implementation might be wasteful; will always keep room
867 * for an additional high priority ramrod (even if one is already
870 while (ecore_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
871 !OSAL_LIST_IS_EMPTY(head)) {
872 struct ecore_spq_entry *p_ent =
873 OSAL_LIST_FIRST_ENTRY(head, struct ecore_spq_entry, list);
874 if (p_ent != OSAL_NULL) {
876 #pragma warning(suppress : 6011 28182)
878 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, head);
879 OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_spq->completion_pending);
880 p_spq->comp_sent_count++;
882 rc = ecore_spq_hw_post(p_hwfn, p_spq, p_ent);
884 OSAL_LIST_REMOVE_ENTRY(&p_ent->list,
885 &p_spq->completion_pending);
886 __ecore_spq_return_entry(p_hwfn, p_ent);
892 return ECORE_SUCCESS;
895 enum _ecore_status_t ecore_spq_pend_post(struct ecore_hwfn *p_hwfn)
897 struct ecore_spq *p_spq = p_hwfn->p_spq;
898 struct ecore_spq_entry *p_ent = OSAL_NULL;
900 while (!OSAL_LIST_IS_EMPTY(&p_spq->free_pool))
902 if (OSAL_LIST_IS_EMPTY(&p_spq->unlimited_pending))
905 p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->unlimited_pending,
906 struct ecore_spq_entry,
912 #pragma warning(suppress : 6011)
914 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->unlimited_pending);
916 ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
919 return ecore_spq_post_list(p_hwfn, &p_spq->pending,
920 SPQ_HIGH_PRI_RESERVE_DEFAULT);
923 enum _ecore_status_t ecore_spq_post(struct ecore_hwfn *p_hwfn,
924 struct ecore_spq_entry *p_ent,
927 enum _ecore_status_t rc = ECORE_SUCCESS;
928 struct ecore_spq *p_spq = p_hwfn ? p_hwfn->p_spq : OSAL_NULL;
929 bool b_ret_ent = true;
935 DP_NOTICE(p_hwfn, true, "Got a NULL pointer\n");
939 if (p_hwfn->p_dev->recov_in_prog) {
940 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
941 "Recovery is in progress -> skip spq post [cmd %02x protocol %02x]\n",
942 p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id);
943 /* Return success to let the flows to be completed successfully
944 * w/o any error handling.
946 return ECORE_SUCCESS;
949 OSAL_SPIN_LOCK(&p_spq->lock);
951 /* Complete the entry */
952 rc = ecore_spq_fill_entry(p_hwfn, p_ent);
954 /* Check return value after LOCK is taken for cleaner error flow */
958 /* Add the request to the pending queue */
959 rc = ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
963 rc = ecore_spq_pend_post(p_hwfn);
965 /* Since it's possible that pending failed for a different
966 * entry [although unlikely], the failed entry was already
967 * dealt with; No need to return it here.
973 OSAL_SPIN_UNLOCK(&p_spq->lock);
975 if (p_ent->comp_mode == ECORE_SPQ_MODE_EBLOCK) {
976 /* For entries in ECORE BLOCK mode, the completion code cannot
977 * perform the necessary cleanup - if it did, we couldn't
978 * access p_ent here to see whether it's successful or not.
979 * Thus, after gaining the answer perform the cleanup here.
981 rc = ecore_spq_block(p_hwfn, p_ent, fw_return_code,
982 p_ent->queue == &p_spq->unlimited_pending);
984 if (p_ent->queue == &p_spq->unlimited_pending) {
985 /* This is an allocated p_ent which does not need to
988 OSAL_FREE(p_hwfn->p_dev, p_ent);
990 /* TBD: handle error flow and remove p_ent from
1000 ecore_spq_return_entry(p_hwfn, p_ent);
1005 OSAL_SPIN_LOCK(&p_spq->lock);
1006 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->completion_pending);
1007 ecore_chain_return_produced(&p_spq->chain);
1010 /* return to the free pool */
1012 __ecore_spq_return_entry(p_hwfn, p_ent);
1013 OSAL_SPIN_UNLOCK(&p_spq->lock);
1018 enum _ecore_status_t ecore_spq_completion(struct ecore_hwfn *p_hwfn,
1021 union event_ring_data *p_data)
1023 struct ecore_spq *p_spq;
1024 struct ecore_spq_entry *p_ent = OSAL_NULL;
1025 struct ecore_spq_entry *tmp;
1026 struct ecore_spq_entry *found = OSAL_NULL;
1032 p_spq = p_hwfn->p_spq;
1037 OSAL_SPIN_LOCK(&p_spq->lock);
1038 OSAL_LIST_FOR_EACH_ENTRY_SAFE(p_ent,
1040 &p_spq->completion_pending,
1042 struct ecore_spq_entry) {
1043 if (p_ent->elem.hdr.echo == echo) {
1044 OSAL_LIST_REMOVE_ENTRY(&p_ent->list,
1045 &p_spq->completion_pending);
1047 /* Avoid overriding of SPQ entries when getting
1048 * out-of-order completions, by marking the completions
1049 * in a bitmap and increasing the chain consumer only
1050 * for the first successive completed entries.
1052 SPQ_COMP_BMAP_SET_BIT(p_spq, echo);
1053 while (SPQ_COMP_BMAP_TEST_BIT(p_spq,
1054 p_spq->comp_bitmap_idx)) {
1055 SPQ_COMP_BMAP_CLEAR_BIT(p_spq,
1056 p_spq->comp_bitmap_idx);
1057 p_spq->comp_bitmap_idx++;
1058 ecore_chain_return_produced(&p_spq->chain);
1061 p_spq->comp_count++;
1066 /* This is debug and should be relatively uncommon - depends
1067 * on scenarios which have mutliple per-PF sent ramrods.
1069 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
1070 "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
1071 OSAL_LE16_TO_CPU(echo),
1072 OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo));
1075 /* Release lock before callback, as callback may post
1076 * an additional ramrod.
1078 OSAL_SPIN_UNLOCK(&p_spq->lock);
1081 DP_NOTICE(p_hwfn, true,
1082 "Failed to find an entry this EQE [echo %04x] completes\n",
1083 OSAL_LE16_TO_CPU(echo));
1084 return ECORE_EXISTS;
1087 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
1088 "Complete EQE [echo %04x]: func %p cookie %p)\n",
1089 OSAL_LE16_TO_CPU(echo),
1090 p_ent->comp_cb.function, p_ent->comp_cb.cookie);
1091 if (found->comp_cb.function)
1092 found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
1095 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "Got a completion without a callback function\n");
1097 if ((found->comp_mode != ECORE_SPQ_MODE_EBLOCK) ||
1098 (found->queue == &p_spq->unlimited_pending))
1099 /* EBLOCK is responsible for returning its own entry into the
1100 * free list, unless it originally added the entry into the
1101 * unlimited pending list.
1103 ecore_spq_return_entry(p_hwfn, found);
1105 return ECORE_SUCCESS;
1108 enum _ecore_status_t ecore_consq_alloc(struct ecore_hwfn *p_hwfn)
1110 struct ecore_consq *p_consq;
1112 /* Allocate ConsQ struct */
1113 p_consq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_consq));
1115 DP_NOTICE(p_hwfn, false,
1116 "Failed to allocate `struct ecore_consq'\n");
1120 /* Allocate and initialize EQ chain*/
1121 if (ecore_chain_alloc(p_hwfn->p_dev,
1122 ECORE_CHAIN_USE_TO_PRODUCE,
1123 ECORE_CHAIN_MODE_PBL,
1124 ECORE_CHAIN_CNT_TYPE_U16,
1125 ECORE_CHAIN_PAGE_SIZE/0x80,
1127 &p_consq->chain, OSAL_NULL) != ECORE_SUCCESS) {
1128 DP_NOTICE(p_hwfn, false, "Failed to allocate consq chain");
1129 goto consq_allocate_fail;
1132 p_hwfn->p_consq = p_consq;
1133 return ECORE_SUCCESS;
1135 consq_allocate_fail:
1136 OSAL_FREE(p_hwfn->p_dev, p_consq);
1140 void ecore_consq_setup(struct ecore_hwfn *p_hwfn)
1142 ecore_chain_reset(&p_hwfn->p_consq->chain);
1145 void ecore_consq_free(struct ecore_hwfn *p_hwfn)
1147 if (!p_hwfn->p_consq)
1150 ecore_chain_free(p_hwfn->p_dev, &p_hwfn->p_consq->chain);
1152 OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_consq);
1153 p_hwfn->p_consq = OSAL_NULL;
1157 #pragma warning(pop)