1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /* QLogic qed NIC Driver
3 * Copyright (c) 2015-2017 QLogic Corporation
4 * Copyright (c) 2019-2020 Marvell International Ltd.
7 #include <linux/types.h>
8 #include <asm/byteorder.h>
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/pci.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/string.h>
21 #include "qed_dev_api.h"
23 #include "qed_iro_hsi.h"
26 #include "qed_iscsi.h"
29 #include "qed_reg_addr.h"
31 #include "qed_sriov.h"
34 /***************************************************************************
35 * Structures & Definitions
36 ***************************************************************************/
38 #define SPQ_HIGH_PRI_RESERVE_DEFAULT (1)
40 #define SPQ_BLOCK_DELAY_MAX_ITER (10)
41 #define SPQ_BLOCK_DELAY_US (10)
42 #define SPQ_BLOCK_SLEEP_MAX_ITER (1000)
43 #define SPQ_BLOCK_SLEEP_MS (5)
45 /***************************************************************************
46 * Blocking Imp. (BLOCK/EBLOCK mode)
47 ***************************************************************************/
48 static void qed_spq_blocking_cb(struct qed_hwfn
*p_hwfn
,
50 union event_ring_data
*data
, u8 fw_return_code
)
52 struct qed_spq_comp_done
*comp_done
;
54 comp_done
= (struct qed_spq_comp_done
*)cookie
;
56 comp_done
->fw_return_code
= fw_return_code
;
58 /* Make sure completion done is visible on waiting thread */
59 smp_store_release(&comp_done
->done
, 0x1);
62 static int __qed_spq_block(struct qed_hwfn
*p_hwfn
,
63 struct qed_spq_entry
*p_ent
,
64 u8
*p_fw_ret
, bool sleep_between_iter
)
66 struct qed_spq_comp_done
*comp_done
;
69 comp_done
= (struct qed_spq_comp_done
*)p_ent
->comp_cb
.cookie
;
70 iter_cnt
= sleep_between_iter
? SPQ_BLOCK_SLEEP_MAX_ITER
71 : SPQ_BLOCK_DELAY_MAX_ITER
;
74 /* Validate we receive completion update */
75 if (smp_load_acquire(&comp_done
->done
) == 1) { /* ^^^ */
77 *p_fw_ret
= comp_done
->fw_return_code
;
81 if (sleep_between_iter
)
82 msleep(SPQ_BLOCK_SLEEP_MS
);
84 udelay(SPQ_BLOCK_DELAY_US
);
90 static int qed_spq_block(struct qed_hwfn
*p_hwfn
,
91 struct qed_spq_entry
*p_ent
,
92 u8
*p_fw_ret
, bool skip_quick_poll
)
94 struct qed_spq_comp_done
*comp_done
;
95 struct qed_ptt
*p_ptt
;
98 /* A relatively short polling period w/o sleeping, to allow the FW to
99 * complete the ramrod and thus possibly to avoid the following sleeps.
101 if (!skip_quick_poll
) {
102 rc
= __qed_spq_block(p_hwfn
, p_ent
, p_fw_ret
, false);
107 /* Move to polling with a sleeping period between iterations */
108 rc
= __qed_spq_block(p_hwfn
, p_ent
, p_fw_ret
, true);
112 p_ptt
= qed_ptt_acquire(p_hwfn
);
114 DP_NOTICE(p_hwfn
, "ptt, failed to acquire\n");
118 DP_INFO(p_hwfn
, "Ramrod is stuck, requesting MCP drain\n");
119 rc
= qed_mcp_drain(p_hwfn
, p_ptt
);
120 qed_ptt_release(p_hwfn
, p_ptt
);
122 DP_NOTICE(p_hwfn
, "MCP drain failed\n");
126 /* Retry after drain */
127 rc
= __qed_spq_block(p_hwfn
, p_ent
, p_fw_ret
, true);
131 comp_done
= (struct qed_spq_comp_done
*)p_ent
->comp_cb
.cookie
;
132 if (comp_done
->done
== 1) {
134 *p_fw_ret
= comp_done
->fw_return_code
;
138 p_ptt
= qed_ptt_acquire(p_hwfn
);
141 qed_hw_err_notify(p_hwfn
, p_ptt
, QED_HW_ERR_RAMROD_FAIL
,
142 "Ramrod is stuck [CID %08x %s:%02x %s:%02x echo %04x]\n",
143 le32_to_cpu(p_ent
->elem
.hdr
.cid
),
144 qed_get_ramrod_cmd_id_str(p_ent
->elem
.hdr
.protocol_id
,
145 p_ent
->elem
.hdr
.cmd_id
),
146 p_ent
->elem
.hdr
.cmd_id
,
147 qed_get_protocol_type_str(p_ent
->elem
.hdr
.protocol_id
),
148 p_ent
->elem
.hdr
.protocol_id
,
149 le16_to_cpu(p_ent
->elem
.hdr
.echo
));
150 qed_ptt_release(p_hwfn
, p_ptt
);
155 /***************************************************************************
156 * SPQ entries inner API
157 ***************************************************************************/
158 static int qed_spq_fill_entry(struct qed_hwfn
*p_hwfn
,
159 struct qed_spq_entry
*p_ent
)
163 switch (p_ent
->comp_mode
) {
164 case QED_SPQ_MODE_EBLOCK
:
165 case QED_SPQ_MODE_BLOCK
:
166 p_ent
->comp_cb
.function
= qed_spq_blocking_cb
;
168 case QED_SPQ_MODE_CB
:
171 DP_NOTICE(p_hwfn
, "Unknown SPQE completion mode %d\n",
178 "Ramrod hdr: [CID 0x%08x %s:0x%02x %s:0x%02x] Data ptr: [%08x:%08x] Cmpltion Mode: %s\n",
180 qed_get_ramrod_cmd_id_str(p_ent
->elem
.hdr
.protocol_id
,
181 p_ent
->elem
.hdr
.cmd_id
),
182 p_ent
->elem
.hdr
.cmd_id
,
183 qed_get_protocol_type_str(p_ent
->elem
.hdr
.protocol_id
),
184 p_ent
->elem
.hdr
.protocol_id
,
185 p_ent
->elem
.data_ptr
.hi
, p_ent
->elem
.data_ptr
.lo
,
186 D_TRINE(p_ent
->comp_mode
, QED_SPQ_MODE_EBLOCK
,
187 QED_SPQ_MODE_BLOCK
, "MODE_EBLOCK", "MODE_BLOCK",
193 /***************************************************************************
195 ***************************************************************************/
196 static void qed_spq_hw_initialize(struct qed_hwfn
*p_hwfn
,
197 struct qed_spq
*p_spq
)
199 struct core_conn_context
*p_cxt
;
200 struct qed_cxt_info cxt_info
;
204 cxt_info
.iid
= p_spq
->cid
;
206 rc
= qed_cxt_get_cid_info(p_hwfn
, &cxt_info
);
209 DP_NOTICE(p_hwfn
, "Cannot find context info for cid=%d\n",
214 p_cxt
= cxt_info
.p_cxt
;
216 SET_FIELD(p_cxt
->xstorm_ag_context
.flags10
,
217 XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN
, 1);
218 SET_FIELD(p_cxt
->xstorm_ag_context
.flags1
,
219 XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE
, 1);
220 SET_FIELD(p_cxt
->xstorm_ag_context
.flags9
,
221 XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN
, 1);
223 /* QM physical queue */
224 physical_q
= qed_get_cm_pq_idx(p_hwfn
, PQ_FLAGS_LB
);
225 p_cxt
->xstorm_ag_context
.physical_q0
= cpu_to_le16(physical_q
);
227 p_cxt
->xstorm_st_context
.spq_base_addr
.lo
=
228 DMA_LO_LE(p_spq
->chain
.p_phys_addr
);
229 p_cxt
->xstorm_st_context
.spq_base_addr
.hi
=
230 DMA_HI_LE(p_spq
->chain
.p_phys_addr
);
233 static int qed_spq_hw_post(struct qed_hwfn
*p_hwfn
,
234 struct qed_spq
*p_spq
, struct qed_spq_entry
*p_ent
)
236 struct qed_chain
*p_chain
= &p_hwfn
->p_spq
->chain
;
237 struct core_db_data
*p_db_data
= &p_spq
->db_data
;
238 u16 echo
= qed_chain_get_prod_idx(p_chain
);
239 struct slow_path_element
*elem
;
241 p_ent
->elem
.hdr
.echo
= cpu_to_le16(echo
);
242 elem
= qed_chain_produce(p_chain
);
244 DP_NOTICE(p_hwfn
, "Failed to produce from SPQ chain\n");
248 *elem
= p_ent
->elem
; /* struct assignment */
250 /* send a doorbell on the slow hwfn session */
251 p_db_data
->spq_prod
= cpu_to_le16(qed_chain_get_prod_idx(p_chain
));
253 /* make sure the SPQE is updated before the doorbell */
256 DOORBELL(p_hwfn
, p_spq
->db_addr_offset
, *(u32
*)p_db_data
);
258 /* make sure doorbell is rang */
261 DP_VERBOSE(p_hwfn
, QED_MSG_SPQ
,
262 "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
263 p_spq
->db_addr_offset
,
266 p_db_data
->agg_flags
, qed_chain_get_prod_idx(p_chain
));
271 /***************************************************************************
272 * Asynchronous events
273 ***************************************************************************/
275 qed_async_event_completion(struct qed_hwfn
*p_hwfn
,
276 struct event_ring_entry
*p_eqe
)
278 qed_spq_async_comp_cb cb
;
283 if (p_eqe
->protocol_id
>= MAX_PROTOCOL_TYPE
) {
284 DP_ERR(p_hwfn
, "Wrong protocol: %s:%d\n",
285 qed_get_protocol_type_str(p_eqe
->protocol_id
),
291 cb
= p_hwfn
->p_spq
->async_comp_cb
[p_eqe
->protocol_id
];
293 return cb(p_hwfn
, p_eqe
->opcode
, p_eqe
->echo
,
294 &p_eqe
->data
, p_eqe
->fw_return_code
);
297 "Unknown Async completion for %s:%d\n",
298 qed_get_protocol_type_str(p_eqe
->protocol_id
),
306 qed_spq_register_async_cb(struct qed_hwfn
*p_hwfn
,
307 enum protocol_type protocol_id
,
308 qed_spq_async_comp_cb cb
)
310 if (!p_hwfn
->p_spq
|| (protocol_id
>= MAX_PROTOCOL_TYPE
))
313 p_hwfn
->p_spq
->async_comp_cb
[protocol_id
] = cb
;
318 qed_spq_unregister_async_cb(struct qed_hwfn
*p_hwfn
,
319 enum protocol_type protocol_id
)
321 if (!p_hwfn
->p_spq
|| (protocol_id
>= MAX_PROTOCOL_TYPE
))
324 p_hwfn
->p_spq
->async_comp_cb
[protocol_id
] = NULL
;
327 /***************************************************************************
329 ***************************************************************************/
330 void qed_eq_prod_update(struct qed_hwfn
*p_hwfn
, u16 prod
)
332 u32 addr
= GET_GTT_REG_ADDR(GTT_BAR0_MAP_REG_USDM_RAM
,
333 USTORM_EQE_CONS
, p_hwfn
->rel_pf_id
);
335 REG_WR16(p_hwfn
, addr
, prod
);
338 int qed_eq_completion(struct qed_hwfn
*p_hwfn
, void *cookie
)
340 struct qed_eq
*p_eq
= cookie
;
341 struct qed_chain
*p_chain
= &p_eq
->chain
;
344 /* take a snapshot of the FW consumer */
345 u16 fw_cons_idx
= le16_to_cpu(*p_eq
->p_fw_cons
);
347 DP_VERBOSE(p_hwfn
, QED_MSG_SPQ
, "fw_cons_idx %x\n", fw_cons_idx
);
349 /* Need to guarantee the fw_cons index we use points to a usuable
350 * element (to comply with our chain), so our macros would comply
352 if ((fw_cons_idx
& qed_chain_get_usable_per_page(p_chain
)) ==
353 qed_chain_get_usable_per_page(p_chain
))
354 fw_cons_idx
+= qed_chain_get_unusable_per_page(p_chain
);
356 /* Complete current segment of eq entries */
357 while (fw_cons_idx
!= qed_chain_get_cons_idx(p_chain
)) {
358 struct event_ring_entry
*p_eqe
= qed_chain_consume(p_chain
);
365 DP_VERBOSE(p_hwfn
, QED_MSG_SPQ
,
366 "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
370 le16_to_cpu(p_eqe
->echo
),
371 p_eqe
->fw_return_code
,
374 if (GET_FIELD(p_eqe
->flags
, EVENT_RING_ENTRY_ASYNC
)) {
375 if (qed_async_event_completion(p_hwfn
, p_eqe
))
377 } else if (qed_spq_completion(p_hwfn
,
379 p_eqe
->fw_return_code
,
384 qed_chain_recycle_consumed(p_chain
);
387 qed_eq_prod_update(p_hwfn
, qed_chain_get_prod_idx(p_chain
));
389 /* Attempt to post pending requests */
390 spin_lock_bh(&p_hwfn
->p_spq
->lock
);
391 rc
= qed_spq_pend_post(p_hwfn
);
392 spin_unlock_bh(&p_hwfn
->p_spq
->lock
);
397 int qed_eq_alloc(struct qed_hwfn
*p_hwfn
, u16 num_elem
)
399 struct qed_chain_init_params params
= {
400 .mode
= QED_CHAIN_MODE_PBL
,
401 .intended_use
= QED_CHAIN_USE_TO_PRODUCE
,
402 .cnt_type
= QED_CHAIN_CNT_TYPE_U16
,
403 .num_elems
= num_elem
,
404 .elem_size
= sizeof(union event_ring_element
),
409 /* Allocate EQ struct */
410 p_eq
= kzalloc(sizeof(*p_eq
), GFP_KERNEL
);
414 ret
= qed_chain_alloc(p_hwfn
->cdev
, &p_eq
->chain
, ¶ms
);
416 DP_NOTICE(p_hwfn
, "Failed to allocate EQ chain\n");
417 goto eq_allocate_fail
;
420 /* register EQ completion on the SP SB */
421 qed_int_register_cb(p_hwfn
, qed_eq_completion
,
422 p_eq
, &p_eq
->eq_sb_index
, &p_eq
->p_fw_cons
);
433 void qed_eq_setup(struct qed_hwfn
*p_hwfn
)
435 qed_chain_reset(&p_hwfn
->p_eq
->chain
);
438 void qed_eq_free(struct qed_hwfn
*p_hwfn
)
443 qed_chain_free(p_hwfn
->cdev
, &p_hwfn
->p_eq
->chain
);
449 /***************************************************************************
450 * CQE API - manipulate EQ functionality
451 ***************************************************************************/
452 static int qed_cqe_completion(struct qed_hwfn
*p_hwfn
,
453 struct eth_slow_path_rx_cqe
*cqe
,
454 enum protocol_type protocol
)
456 if (IS_VF(p_hwfn
->cdev
))
459 /* @@@tmp - it's possible we'll eventually want to handle some
460 * actual commands that can arrive here, but for now this is only
461 * used to complete the ramrod using the echo value on the cqe
463 return qed_spq_completion(p_hwfn
, cqe
->echo
, 0, NULL
);
466 int qed_eth_cqe_completion(struct qed_hwfn
*p_hwfn
,
467 struct eth_slow_path_rx_cqe
*cqe
)
471 rc
= qed_cqe_completion(p_hwfn
, cqe
, PROTOCOLID_ETH
);
474 "Failed to handle RXQ CQE [cmd 0x%02x]\n",
480 /***************************************************************************
481 * Slow hwfn Queue (spq)
482 ***************************************************************************/
483 void qed_spq_setup(struct qed_hwfn
*p_hwfn
)
485 struct qed_spq
*p_spq
= p_hwfn
->p_spq
;
486 struct qed_spq_entry
*p_virt
= NULL
;
487 struct core_db_data
*p_db_data
;
488 void __iomem
*db_addr
;
489 dma_addr_t p_phys
= 0;
493 INIT_LIST_HEAD(&p_spq
->pending
);
494 INIT_LIST_HEAD(&p_spq
->completion_pending
);
495 INIT_LIST_HEAD(&p_spq
->free_pool
);
496 INIT_LIST_HEAD(&p_spq
->unlimited_pending
);
497 spin_lock_init(&p_spq
->lock
);
500 p_phys
= p_spq
->p_phys
+ offsetof(struct qed_spq_entry
, ramrod
);
501 p_virt
= p_spq
->p_virt
;
503 capacity
= qed_chain_get_capacity(&p_spq
->chain
);
504 for (i
= 0; i
< capacity
; i
++) {
505 DMA_REGPAIR_LE(p_virt
->elem
.data_ptr
, p_phys
);
507 list_add_tail(&p_virt
->list
, &p_spq
->free_pool
);
510 p_phys
+= sizeof(struct qed_spq_entry
);
514 p_spq
->normal_count
= 0;
515 p_spq
->comp_count
= 0;
516 p_spq
->comp_sent_count
= 0;
517 p_spq
->unlimited_pending_count
= 0;
519 bitmap_zero(p_spq
->p_comp_bitmap
, SPQ_RING_SIZE
);
520 p_spq
->comp_bitmap_idx
= 0;
522 /* SPQ cid, cannot fail */
523 qed_cxt_acquire_cid(p_hwfn
, PROTOCOLID_CORE
, &p_spq
->cid
);
524 qed_spq_hw_initialize(p_hwfn
, p_spq
);
526 /* reset the chain itself */
527 qed_chain_reset(&p_spq
->chain
);
529 /* Initialize the address/data of the SPQ doorbell */
530 p_spq
->db_addr_offset
= qed_db_addr(p_spq
->cid
, DQ_DEMS_LEGACY
);
531 p_db_data
= &p_spq
->db_data
;
532 memset(p_db_data
, 0, sizeof(*p_db_data
));
533 SET_FIELD(p_db_data
->params
, CORE_DB_DATA_DEST
, DB_DEST_XCM
);
534 SET_FIELD(p_db_data
->params
, CORE_DB_DATA_AGG_CMD
, DB_AGG_CMD_MAX
);
535 SET_FIELD(p_db_data
->params
, CORE_DB_DATA_AGG_VAL_SEL
,
536 DQ_XCM_CORE_SPQ_PROD_CMD
);
537 p_db_data
->agg_flags
= DQ_XCM_CORE_DQ_CF_CMD
;
539 /* Register the SPQ doorbell with the doorbell recovery mechanism */
540 db_addr
= (void __iomem
*)((u8 __iomem
*)p_hwfn
->doorbells
+
541 p_spq
->db_addr_offset
);
542 rc
= qed_db_recovery_add(p_hwfn
->cdev
, db_addr
, &p_spq
->db_data
,
543 DB_REC_WIDTH_32B
, DB_REC_KERNEL
);
546 "Failed to register the SPQ doorbell with the doorbell recovery mechanism\n");
549 int qed_spq_alloc(struct qed_hwfn
*p_hwfn
)
551 struct qed_chain_init_params params
= {
552 .mode
= QED_CHAIN_MODE_SINGLE
,
553 .intended_use
= QED_CHAIN_USE_TO_PRODUCE
,
554 .cnt_type
= QED_CHAIN_CNT_TYPE_U16
,
555 .elem_size
= sizeof(struct slow_path_element
),
557 struct qed_dev
*cdev
= p_hwfn
->cdev
;
558 struct qed_spq_entry
*p_virt
= NULL
;
559 struct qed_spq
*p_spq
= NULL
;
560 dma_addr_t p_phys
= 0;
565 p_spq
= kzalloc(sizeof(*p_spq
), GFP_KERNEL
);
570 ret
= qed_chain_alloc(cdev
, &p_spq
->chain
, ¶ms
);
572 DP_NOTICE(p_hwfn
, "Failed to allocate SPQ chain\n");
573 goto spq_chain_alloc_fail
;
576 /* allocate and fill the SPQ elements (incl. ramrod data list) */
577 capacity
= qed_chain_get_capacity(&p_spq
->chain
);
580 p_virt
= dma_alloc_coherent(&cdev
->pdev
->dev
,
581 capacity
* sizeof(struct qed_spq_entry
),
582 &p_phys
, GFP_KERNEL
);
586 p_spq
->p_virt
= p_virt
;
587 p_spq
->p_phys
= p_phys
;
588 p_hwfn
->p_spq
= p_spq
;
593 qed_chain_free(cdev
, &p_spq
->chain
);
594 spq_chain_alloc_fail
:
600 void qed_spq_free(struct qed_hwfn
*p_hwfn
)
602 struct qed_spq
*p_spq
= p_hwfn
->p_spq
;
603 void __iomem
*db_addr
;
609 /* Delete the SPQ doorbell from the doorbell recovery mechanism */
610 db_addr
= (void __iomem
*)((u8 __iomem
*)p_hwfn
->doorbells
+
611 p_spq
->db_addr_offset
);
612 qed_db_recovery_del(p_hwfn
->cdev
, db_addr
, &p_spq
->db_data
);
615 capacity
= qed_chain_get_capacity(&p_spq
->chain
);
616 dma_free_coherent(&p_hwfn
->cdev
->pdev
->dev
,
618 sizeof(struct qed_spq_entry
),
619 p_spq
->p_virt
, p_spq
->p_phys
);
622 qed_chain_free(p_hwfn
->cdev
, &p_spq
->chain
);
624 p_hwfn
->p_spq
= NULL
;
627 int qed_spq_get_entry(struct qed_hwfn
*p_hwfn
, struct qed_spq_entry
**pp_ent
)
629 struct qed_spq
*p_spq
= p_hwfn
->p_spq
;
630 struct qed_spq_entry
*p_ent
= NULL
;
633 spin_lock_bh(&p_spq
->lock
);
635 if (list_empty(&p_spq
->free_pool
)) {
636 p_ent
= kzalloc(sizeof(*p_ent
), GFP_ATOMIC
);
639 "Failed to allocate an SPQ entry for a pending ramrod\n");
643 p_ent
->queue
= &p_spq
->unlimited_pending
;
645 p_ent
= list_first_entry(&p_spq
->free_pool
,
646 struct qed_spq_entry
, list
);
647 list_del(&p_ent
->list
);
648 p_ent
->queue
= &p_spq
->pending
;
654 spin_unlock_bh(&p_spq
->lock
);
658 /* Locked variant; Should be called while the SPQ lock is taken */
659 static void __qed_spq_return_entry(struct qed_hwfn
*p_hwfn
,
660 struct qed_spq_entry
*p_ent
)
662 list_add_tail(&p_ent
->list
, &p_hwfn
->p_spq
->free_pool
);
665 void qed_spq_return_entry(struct qed_hwfn
*p_hwfn
, struct qed_spq_entry
*p_ent
)
667 spin_lock_bh(&p_hwfn
->p_spq
->lock
);
668 __qed_spq_return_entry(p_hwfn
, p_ent
);
669 spin_unlock_bh(&p_hwfn
->p_spq
->lock
);
673 * qed_spq_add_entry() - Add a new entry to the pending list.
674 * Should be used while lock is being held.
676 * @p_hwfn: HW device data.
677 * @p_ent: An entry to add.
678 * @priority: Desired priority.
680 * Adds an entry to the pending list is there is room (an empty
681 * element is available in the free_pool), or else places the
682 * entry in the unlimited_pending pool.
684 * Return: zero on success, -EINVAL on invalid @priority.
686 static int qed_spq_add_entry(struct qed_hwfn
*p_hwfn
,
687 struct qed_spq_entry
*p_ent
,
688 enum spq_priority priority
)
690 struct qed_spq
*p_spq
= p_hwfn
->p_spq
;
692 if (p_ent
->queue
== &p_spq
->unlimited_pending
) {
693 if (list_empty(&p_spq
->free_pool
)) {
694 list_add_tail(&p_ent
->list
, &p_spq
->unlimited_pending
);
695 p_spq
->unlimited_pending_count
++;
699 struct qed_spq_entry
*p_en2
;
701 p_en2
= list_first_entry(&p_spq
->free_pool
,
702 struct qed_spq_entry
, list
);
703 list_del(&p_en2
->list
);
705 /* Copy the ring element physical pointer to the new
706 * entry, since we are about to override the entire ring
707 * entry and don't want to lose the pointer.
709 p_ent
->elem
.data_ptr
= p_en2
->elem
.data_ptr
;
713 /* EBLOCK responsible to free the allocated p_ent */
714 if (p_ent
->comp_mode
!= QED_SPQ_MODE_EBLOCK
)
717 p_ent
->post_ent
= p_en2
;
723 /* entry is to be placed in 'pending' queue */
725 case QED_SPQ_PRIORITY_NORMAL
:
726 list_add_tail(&p_ent
->list
, &p_spq
->pending
);
727 p_spq
->normal_count
++;
729 case QED_SPQ_PRIORITY_HIGH
:
730 list_add(&p_ent
->list
, &p_spq
->pending
);
740 /***************************************************************************
742 ***************************************************************************/
743 u32
qed_spq_get_cid(struct qed_hwfn
*p_hwfn
)
746 return 0xffffffff; /* illegal */
747 return p_hwfn
->p_spq
->cid
;
750 /***************************************************************************
751 * Posting new Ramrods
752 ***************************************************************************/
753 static int qed_spq_post_list(struct qed_hwfn
*p_hwfn
,
754 struct list_head
*head
, u32 keep_reserve
)
756 struct qed_spq
*p_spq
= p_hwfn
->p_spq
;
759 while (qed_chain_get_elem_left(&p_spq
->chain
) > keep_reserve
&&
761 struct qed_spq_entry
*p_ent
=
762 list_first_entry(head
, struct qed_spq_entry
, list
);
763 list_move_tail(&p_ent
->list
, &p_spq
->completion_pending
);
764 p_spq
->comp_sent_count
++;
766 rc
= qed_spq_hw_post(p_hwfn
, p_spq
, p_ent
);
768 list_del(&p_ent
->list
);
769 __qed_spq_return_entry(p_hwfn
, p_ent
);
777 int qed_spq_pend_post(struct qed_hwfn
*p_hwfn
)
779 struct qed_spq
*p_spq
= p_hwfn
->p_spq
;
780 struct qed_spq_entry
*p_ent
= NULL
;
782 while (!list_empty(&p_spq
->free_pool
)) {
783 if (list_empty(&p_spq
->unlimited_pending
))
786 p_ent
= list_first_entry(&p_spq
->unlimited_pending
,
787 struct qed_spq_entry
, list
);
791 list_del(&p_ent
->list
);
793 qed_spq_add_entry(p_hwfn
, p_ent
, p_ent
->priority
);
796 return qed_spq_post_list(p_hwfn
, &p_spq
->pending
,
797 SPQ_HIGH_PRI_RESERVE_DEFAULT
);
800 static void qed_spq_recov_set_ret_code(struct qed_spq_entry
*p_ent
,
806 if (p_ent
->elem
.hdr
.protocol_id
== PROTOCOLID_ROCE
||
807 p_ent
->elem
.hdr
.protocol_id
== PROTOCOLID_IWARP
)
808 *fw_return_code
= RDMA_RETURN_OK
;
811 /* Avoid overriding of SPQ entries when getting out-of-order completions, by
812 * marking the completions in a bitmap and increasing the chain consumer only
813 * for the first successive completed entries.
815 static void qed_spq_comp_bmap_update(struct qed_hwfn
*p_hwfn
, __le16 echo
)
817 u16 pos
= le16_to_cpu(echo
) % SPQ_RING_SIZE
;
818 struct qed_spq
*p_spq
= p_hwfn
->p_spq
;
820 __set_bit(pos
, p_spq
->p_comp_bitmap
);
821 while (test_bit(p_spq
->comp_bitmap_idx
,
822 p_spq
->p_comp_bitmap
)) {
823 __clear_bit(p_spq
->comp_bitmap_idx
,
824 p_spq
->p_comp_bitmap
);
825 p_spq
->comp_bitmap_idx
++;
826 qed_chain_return_produced(&p_spq
->chain
);
830 int qed_spq_post(struct qed_hwfn
*p_hwfn
,
831 struct qed_spq_entry
*p_ent
, u8
*fw_return_code
)
834 struct qed_spq
*p_spq
= p_hwfn
? p_hwfn
->p_spq
: NULL
;
835 bool b_ret_ent
= true;
842 DP_NOTICE(p_hwfn
, "Got a NULL pointer\n");
846 if (p_hwfn
->cdev
->recov_in_prog
) {
849 "Recovery is in progress. Skip spq post [%s:%02x %s:%02x]\n",
850 qed_get_ramrod_cmd_id_str(p_ent
->elem
.hdr
.protocol_id
,
851 p_ent
->elem
.hdr
.cmd_id
),
852 p_ent
->elem
.hdr
.cmd_id
,
853 qed_get_protocol_type_str(p_ent
->elem
.hdr
.protocol_id
),
854 p_ent
->elem
.hdr
.protocol_id
);
856 /* Let the flow complete w/o any error handling */
857 qed_spq_recov_set_ret_code(p_ent
, fw_return_code
);
861 /* Complete the entry */
862 rc
= qed_spq_fill_entry(p_hwfn
, p_ent
);
864 spin_lock_bh(&p_spq
->lock
);
866 /* Check return value after LOCK is taken for cleaner error flow */
870 /* Check if entry is in block mode before qed_spq_add_entry,
871 * which might kfree p_ent.
873 eblock
= (p_ent
->comp_mode
== QED_SPQ_MODE_EBLOCK
);
875 /* Add the request to the pending queue */
876 rc
= qed_spq_add_entry(p_hwfn
, p_ent
, p_ent
->priority
);
880 rc
= qed_spq_pend_post(p_hwfn
);
882 /* Since it's possible that pending failed for a different
883 * entry [although unlikely], the failed entry was already
884 * dealt with; No need to return it here.
890 spin_unlock_bh(&p_spq
->lock
);
893 /* For entries in QED BLOCK mode, the completion code cannot
894 * perform the necessary cleanup - if it did, we couldn't
895 * access p_ent here to see whether it's successful or not.
896 * Thus, after gaining the answer perform the cleanup here.
898 rc
= qed_spq_block(p_hwfn
, p_ent
, fw_return_code
,
899 p_ent
->queue
== &p_spq
->unlimited_pending
);
901 if (p_ent
->queue
== &p_spq
->unlimited_pending
) {
902 struct qed_spq_entry
*p_post_ent
= p_ent
->post_ent
;
906 /* Return the entry which was actually posted */
914 qed_spq_return_entry(p_hwfn
, p_ent
);
919 spin_lock_bh(&p_spq
->lock
);
920 list_del(&p_ent
->list
);
921 qed_spq_comp_bmap_update(p_hwfn
, p_ent
->elem
.hdr
.echo
);
924 /* return to the free pool */
926 __qed_spq_return_entry(p_hwfn
, p_ent
);
927 spin_unlock_bh(&p_spq
->lock
);
932 int qed_spq_completion(struct qed_hwfn
*p_hwfn
,
935 union event_ring_data
*p_data
)
937 struct qed_spq
*p_spq
;
938 struct qed_spq_entry
*p_ent
= NULL
;
939 struct qed_spq_entry
*tmp
;
940 struct qed_spq_entry
*found
= NULL
;
945 p_spq
= p_hwfn
->p_spq
;
949 spin_lock_bh(&p_spq
->lock
);
950 list_for_each_entry_safe(p_ent
, tmp
, &p_spq
->completion_pending
, list
) {
951 if (p_ent
->elem
.hdr
.echo
== echo
) {
952 list_del(&p_ent
->list
);
953 qed_spq_comp_bmap_update(p_hwfn
, echo
);
959 /* This is relatively uncommon - depends on scenarios
960 * which have mutliple per-PF sent ramrods.
962 DP_VERBOSE(p_hwfn
, QED_MSG_SPQ
,
963 "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
965 le16_to_cpu(p_ent
->elem
.hdr
.echo
));
968 /* Release lock before callback, as callback may post
969 * an additional ramrod.
971 spin_unlock_bh(&p_spq
->lock
);
975 "Failed to find an entry this EQE [echo %04x] completes\n",
980 DP_VERBOSE(p_hwfn
, QED_MSG_SPQ
,
981 "Complete EQE [echo %04x]: func %p cookie %p)\n",
983 p_ent
->comp_cb
.function
, p_ent
->comp_cb
.cookie
);
984 if (found
->comp_cb
.function
)
985 found
->comp_cb
.function(p_hwfn
, found
->comp_cb
.cookie
, p_data
,
990 "Got a completion without a callback function\n");
992 if (found
->comp_mode
!= QED_SPQ_MODE_EBLOCK
)
993 /* EBLOCK is responsible for returning its own entry into the
996 qed_spq_return_entry(p_hwfn
, found
);
1001 #define QED_SPQ_CONSQ_ELEM_SIZE 0x80
1003 int qed_consq_alloc(struct qed_hwfn
*p_hwfn
)
1005 struct qed_chain_init_params params
= {
1006 .mode
= QED_CHAIN_MODE_PBL
,
1007 .intended_use
= QED_CHAIN_USE_TO_PRODUCE
,
1008 .cnt_type
= QED_CHAIN_CNT_TYPE_U16
,
1009 .num_elems
= QED_CHAIN_PAGE_SIZE
/ QED_SPQ_CONSQ_ELEM_SIZE
,
1010 .elem_size
= QED_SPQ_CONSQ_ELEM_SIZE
,
1012 struct qed_consq
*p_consq
;
1015 /* Allocate ConsQ struct */
1016 p_consq
= kzalloc(sizeof(*p_consq
), GFP_KERNEL
);
1020 /* Allocate and initialize ConsQ chain */
1021 ret
= qed_chain_alloc(p_hwfn
->cdev
, &p_consq
->chain
, ¶ms
);
1023 DP_NOTICE(p_hwfn
, "Failed to allocate ConsQ chain");
1024 goto consq_alloc_fail
;
1027 p_hwfn
->p_consq
= p_consq
;
1037 void qed_consq_setup(struct qed_hwfn
*p_hwfn
)
1039 qed_chain_reset(&p_hwfn
->p_consq
->chain
);
1042 void qed_consq_free(struct qed_hwfn
*p_hwfn
)
1044 if (!p_hwfn
->p_consq
)
1047 qed_chain_free(p_hwfn
->cdev
, &p_hwfn
->p_consq
->chain
);
1049 kfree(p_hwfn
->p_consq
);
1050 p_hwfn
->p_consq
= NULL
;