1 /* QLogic qed NIC Driver
2 * Copyright (c) 2015-2017 QLogic Corporation
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/types.h>
34 #include <asm/byteorder.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>
47 #include "qed_dev_api.h"
51 #include "qed_iscsi.h"
54 #include "qed_reg_addr.h"
56 #include "qed_sriov.h"
59 /***************************************************************************
60 * Structures & Definitions
61 ***************************************************************************/
63 #define SPQ_HIGH_PRI_RESERVE_DEFAULT (1)
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)
70 /***************************************************************************
71 * Blocking Imp. (BLOCK/EBLOCK mode)
72 ***************************************************************************/
73 static void qed_spq_blocking_cb(struct qed_hwfn *p_hwfn,
75 union event_ring_data *data, u8 fw_return_code)
77 struct qed_spq_comp_done *comp_done;
79 comp_done = (struct qed_spq_comp_done *)cookie;
81 comp_done->fw_return_code = fw_return_code;
83 /* Make sure completion done is visible on waiting thread */
84 smp_store_release(&comp_done->done, 0x1);
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)
91 struct qed_spq_comp_done *comp_done;
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;
99 /* Validate we receive completion update */
100 if (READ_ONCE(comp_done->done) == 1) {
101 /* Read updated FW return value */
102 smp_read_barrier_depends();
104 *p_fw_ret = comp_done->fw_return_code;
108 if (sleep_between_iter)
109 msleep(SPQ_BLOCK_SLEEP_MS);
111 udelay(SPQ_BLOCK_DELAY_US);
117 static int qed_spq_block(struct qed_hwfn *p_hwfn,
118 struct qed_spq_entry *p_ent,
119 u8 *p_fw_ret, bool skip_quick_poll)
121 struct qed_spq_comp_done *comp_done;
122 struct qed_ptt *p_ptt;
125 /* A relatively short polling period w/o sleeping, to allow the FW to
126 * complete the ramrod and thus possibly to avoid the following sleeps.
128 if (!skip_quick_poll) {
129 rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, false);
134 /* Move to polling with a sleeping period between iterations */
135 rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
139 p_ptt = qed_ptt_acquire(p_hwfn);
141 DP_NOTICE(p_hwfn, "ptt, failed to acquire\n");
145 DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
146 rc = qed_mcp_drain(p_hwfn, p_ptt);
147 qed_ptt_release(p_hwfn, p_ptt);
149 DP_NOTICE(p_hwfn, "MCP drain failed\n");
153 /* Retry after drain */
154 rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
158 comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
159 if (comp_done->done == 1) {
161 *p_fw_ret = comp_done->fw_return_code;
166 "Ramrod is stuck [CID %08x cmd %02x protocol %02x echo %04x]\n",
167 le32_to_cpu(p_ent->elem.hdr.cid),
168 p_ent->elem.hdr.cmd_id,
169 p_ent->elem.hdr.protocol_id,
170 le16_to_cpu(p_ent->elem.hdr.echo));
175 /***************************************************************************
176 * SPQ entries inner API
177 ***************************************************************************/
178 static int qed_spq_fill_entry(struct qed_hwfn *p_hwfn,
179 struct qed_spq_entry *p_ent)
183 switch (p_ent->comp_mode) {
184 case QED_SPQ_MODE_EBLOCK:
185 case QED_SPQ_MODE_BLOCK:
186 p_ent->comp_cb.function = qed_spq_blocking_cb;
188 case QED_SPQ_MODE_CB:
191 DP_NOTICE(p_hwfn, "Unknown SPQE completion mode %d\n",
196 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
197 "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x] Data pointer: [%08x:%08x] Completion Mode: %s\n",
199 p_ent->elem.hdr.cmd_id,
200 p_ent->elem.hdr.protocol_id,
201 p_ent->elem.data_ptr.hi,
202 p_ent->elem.data_ptr.lo,
203 D_TRINE(p_ent->comp_mode, QED_SPQ_MODE_EBLOCK,
204 QED_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
210 /***************************************************************************
212 ***************************************************************************/
213 static void qed_spq_hw_initialize(struct qed_hwfn *p_hwfn,
214 struct qed_spq *p_spq)
216 struct core_conn_context *p_cxt;
217 struct qed_cxt_info cxt_info;
221 cxt_info.iid = p_spq->cid;
223 rc = qed_cxt_get_cid_info(p_hwfn, &cxt_info);
226 DP_NOTICE(p_hwfn, "Cannot find context info for cid=%d\n",
231 p_cxt = cxt_info.p_cxt;
233 SET_FIELD(p_cxt->xstorm_ag_context.flags10,
234 XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
235 SET_FIELD(p_cxt->xstorm_ag_context.flags1,
236 XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
237 SET_FIELD(p_cxt->xstorm_ag_context.flags9,
238 XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
240 /* QM physical queue */
241 physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
242 p_cxt->xstorm_ag_context.physical_q0 = cpu_to_le16(physical_q);
244 p_cxt->xstorm_st_context.spq_base_lo =
245 DMA_LO_LE(p_spq->chain.p_phys_addr);
246 p_cxt->xstorm_st_context.spq_base_hi =
247 DMA_HI_LE(p_spq->chain.p_phys_addr);
249 DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
250 p_hwfn->p_consq->chain.p_phys_addr);
253 static int qed_spq_hw_post(struct qed_hwfn *p_hwfn,
254 struct qed_spq *p_spq, struct qed_spq_entry *p_ent)
256 struct qed_chain *p_chain = &p_hwfn->p_spq->chain;
257 u16 echo = qed_chain_get_prod_idx(p_chain);
258 struct slow_path_element *elem;
259 struct core_db_data db;
261 p_ent->elem.hdr.echo = cpu_to_le16(echo);
262 elem = qed_chain_produce(p_chain);
264 DP_NOTICE(p_hwfn, "Failed to produce from SPQ chain\n");
268 *elem = p_ent->elem; /* struct assignment */
270 /* send a doorbell on the slow hwfn session */
271 memset(&db, 0, sizeof(db));
272 SET_FIELD(db.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
273 SET_FIELD(db.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
274 SET_FIELD(db.params, CORE_DB_DATA_AGG_VAL_SEL,
275 DQ_XCM_CORE_SPQ_PROD_CMD);
276 db.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
277 db.spq_prod = cpu_to_le16(qed_chain_get_prod_idx(p_chain));
279 /* make sure the SPQE is updated before the doorbell */
282 DOORBELL(p_hwfn, qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY), *(u32 *)&db);
284 /* make sure doorbell is rang */
287 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
288 "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
289 qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY),
290 p_spq->cid, db.params, db.agg_flags,
291 qed_chain_get_prod_idx(p_chain));
296 /***************************************************************************
297 * Asynchronous events
298 ***************************************************************************/
300 qed_async_event_completion(struct qed_hwfn *p_hwfn,
301 struct event_ring_entry *p_eqe)
303 qed_spq_async_comp_cb cb;
305 if (!p_hwfn->p_spq || (p_eqe->protocol_id >= MAX_PROTOCOL_TYPE))
308 cb = p_hwfn->p_spq->async_comp_cb[p_eqe->protocol_id];
310 return cb(p_hwfn, p_eqe->opcode, p_eqe->echo,
311 &p_eqe->data, p_eqe->fw_return_code);
314 "Unknown Async completion for protocol: %d\n",
321 qed_spq_register_async_cb(struct qed_hwfn *p_hwfn,
322 enum protocol_type protocol_id,
323 qed_spq_async_comp_cb cb)
325 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
328 p_hwfn->p_spq->async_comp_cb[protocol_id] = cb;
333 qed_spq_unregister_async_cb(struct qed_hwfn *p_hwfn,
334 enum protocol_type protocol_id)
336 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
339 p_hwfn->p_spq->async_comp_cb[protocol_id] = NULL;
342 /***************************************************************************
344 ***************************************************************************/
345 void qed_eq_prod_update(struct qed_hwfn *p_hwfn, u16 prod)
347 u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
348 USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
350 REG_WR16(p_hwfn, addr, prod);
352 /* keep prod updates ordered */
356 int qed_eq_completion(struct qed_hwfn *p_hwfn, void *cookie)
358 struct qed_eq *p_eq = cookie;
359 struct qed_chain *p_chain = &p_eq->chain;
362 /* take a snapshot of the FW consumer */
363 u16 fw_cons_idx = le16_to_cpu(*p_eq->p_fw_cons);
365 DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
367 /* Need to guarantee the fw_cons index we use points to a usuable
368 * element (to comply with our chain), so our macros would comply
370 if ((fw_cons_idx & qed_chain_get_usable_per_page(p_chain)) ==
371 qed_chain_get_usable_per_page(p_chain))
372 fw_cons_idx += qed_chain_get_unusable_per_page(p_chain);
374 /* Complete current segment of eq entries */
375 while (fw_cons_idx != qed_chain_get_cons_idx(p_chain)) {
376 struct event_ring_entry *p_eqe = qed_chain_consume(p_chain);
383 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
384 "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
388 le16_to_cpu(p_eqe->echo),
389 p_eqe->fw_return_code,
392 if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
393 if (qed_async_event_completion(p_hwfn, p_eqe))
395 } else if (qed_spq_completion(p_hwfn,
397 p_eqe->fw_return_code,
402 qed_chain_recycle_consumed(p_chain);
405 qed_eq_prod_update(p_hwfn, qed_chain_get_prod_idx(p_chain));
407 /* Attempt to post pending requests */
408 spin_lock_bh(&p_hwfn->p_spq->lock);
409 rc = qed_spq_pend_post(p_hwfn);
410 spin_unlock_bh(&p_hwfn->p_spq->lock);
415 int qed_eq_alloc(struct qed_hwfn *p_hwfn, u16 num_elem)
419 /* Allocate EQ struct */
420 p_eq = kzalloc(sizeof(*p_eq), GFP_KERNEL);
424 /* Allocate and initialize EQ chain*/
425 if (qed_chain_alloc(p_hwfn->cdev,
426 QED_CHAIN_USE_TO_PRODUCE,
428 QED_CHAIN_CNT_TYPE_U16,
430 sizeof(union event_ring_element),
432 goto eq_allocate_fail;
434 /* register EQ completion on the SP SB */
435 qed_int_register_cb(p_hwfn, qed_eq_completion,
436 p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
446 void qed_eq_setup(struct qed_hwfn *p_hwfn)
448 qed_chain_reset(&p_hwfn->p_eq->chain);
451 void qed_eq_free(struct qed_hwfn *p_hwfn)
456 qed_chain_free(p_hwfn->cdev, &p_hwfn->p_eq->chain);
462 /***************************************************************************
463 * CQE API - manipulate EQ functionality
464 ***************************************************************************/
465 static int qed_cqe_completion(struct qed_hwfn *p_hwfn,
466 struct eth_slow_path_rx_cqe *cqe,
467 enum protocol_type protocol)
469 if (IS_VF(p_hwfn->cdev))
472 /* @@@tmp - it's possible we'll eventually want to handle some
473 * actual commands that can arrive here, but for now this is only
474 * used to complete the ramrod using the echo value on the cqe
476 return qed_spq_completion(p_hwfn, cqe->echo, 0, NULL);
479 int qed_eth_cqe_completion(struct qed_hwfn *p_hwfn,
480 struct eth_slow_path_rx_cqe *cqe)
484 rc = qed_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
487 "Failed to handle RXQ CQE [cmd 0x%02x]\n",
493 /***************************************************************************
494 * Slow hwfn Queue (spq)
495 ***************************************************************************/
496 void qed_spq_setup(struct qed_hwfn *p_hwfn)
498 struct qed_spq *p_spq = p_hwfn->p_spq;
499 struct qed_spq_entry *p_virt = NULL;
500 dma_addr_t p_phys = 0;
503 INIT_LIST_HEAD(&p_spq->pending);
504 INIT_LIST_HEAD(&p_spq->completion_pending);
505 INIT_LIST_HEAD(&p_spq->free_pool);
506 INIT_LIST_HEAD(&p_spq->unlimited_pending);
507 spin_lock_init(&p_spq->lock);
510 p_phys = p_spq->p_phys + offsetof(struct qed_spq_entry, ramrod);
511 p_virt = p_spq->p_virt;
513 capacity = qed_chain_get_capacity(&p_spq->chain);
514 for (i = 0; i < capacity; i++) {
515 DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
517 list_add_tail(&p_virt->list, &p_spq->free_pool);
520 p_phys += sizeof(struct qed_spq_entry);
524 p_spq->normal_count = 0;
525 p_spq->comp_count = 0;
526 p_spq->comp_sent_count = 0;
527 p_spq->unlimited_pending_count = 0;
529 bitmap_zero(p_spq->p_comp_bitmap, SPQ_RING_SIZE);
530 p_spq->comp_bitmap_idx = 0;
532 /* SPQ cid, cannot fail */
533 qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
534 qed_spq_hw_initialize(p_hwfn, p_spq);
536 /* reset the chain itself */
537 qed_chain_reset(&p_spq->chain);
540 int qed_spq_alloc(struct qed_hwfn *p_hwfn)
542 struct qed_spq_entry *p_virt = NULL;
543 struct qed_spq *p_spq = NULL;
544 dma_addr_t p_phys = 0;
548 p_spq = kzalloc(sizeof(struct qed_spq), GFP_KERNEL);
553 if (qed_chain_alloc(p_hwfn->cdev,
554 QED_CHAIN_USE_TO_PRODUCE,
555 QED_CHAIN_MODE_SINGLE,
556 QED_CHAIN_CNT_TYPE_U16,
557 0, /* N/A when the mode is SINGLE */
558 sizeof(struct slow_path_element),
559 &p_spq->chain, NULL))
560 goto spq_allocate_fail;
562 /* allocate and fill the SPQ elements (incl. ramrod data list) */
563 capacity = qed_chain_get_capacity(&p_spq->chain);
564 p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
565 capacity * sizeof(struct qed_spq_entry),
566 &p_phys, GFP_KERNEL);
568 goto spq_allocate_fail;
570 p_spq->p_virt = p_virt;
571 p_spq->p_phys = p_phys;
572 p_hwfn->p_spq = p_spq;
577 qed_chain_free(p_hwfn->cdev, &p_spq->chain);
582 void qed_spq_free(struct qed_hwfn *p_hwfn)
584 struct qed_spq *p_spq = p_hwfn->p_spq;
591 capacity = qed_chain_get_capacity(&p_spq->chain);
592 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
594 sizeof(struct qed_spq_entry),
595 p_spq->p_virt, p_spq->p_phys);
598 qed_chain_free(p_hwfn->cdev, &p_spq->chain);
600 p_hwfn->p_spq = NULL;
603 int qed_spq_get_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry **pp_ent)
605 struct qed_spq *p_spq = p_hwfn->p_spq;
606 struct qed_spq_entry *p_ent = NULL;
609 spin_lock_bh(&p_spq->lock);
611 if (list_empty(&p_spq->free_pool)) {
612 p_ent = kzalloc(sizeof(*p_ent), GFP_ATOMIC);
615 "Failed to allocate an SPQ entry for a pending ramrod\n");
619 p_ent->queue = &p_spq->unlimited_pending;
621 p_ent = list_first_entry(&p_spq->free_pool,
622 struct qed_spq_entry, list);
623 list_del(&p_ent->list);
624 p_ent->queue = &p_spq->pending;
630 spin_unlock_bh(&p_spq->lock);
634 /* Locked variant; Should be called while the SPQ lock is taken */
635 static void __qed_spq_return_entry(struct qed_hwfn *p_hwfn,
636 struct qed_spq_entry *p_ent)
638 list_add_tail(&p_ent->list, &p_hwfn->p_spq->free_pool);
641 void qed_spq_return_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry *p_ent)
643 spin_lock_bh(&p_hwfn->p_spq->lock);
644 __qed_spq_return_entry(p_hwfn, p_ent);
645 spin_unlock_bh(&p_hwfn->p_spq->lock);
649 * @brief qed_spq_add_entry - adds a new entry to the pending
650 * list. Should be used while lock is being held.
652 * Addes an entry to the pending list is there is room (en empty
653 * element is available in the free_pool), or else places the
654 * entry in the unlimited_pending pool.
662 static int qed_spq_add_entry(struct qed_hwfn *p_hwfn,
663 struct qed_spq_entry *p_ent,
664 enum spq_priority priority)
666 struct qed_spq *p_spq = p_hwfn->p_spq;
668 if (p_ent->queue == &p_spq->unlimited_pending) {
670 if (list_empty(&p_spq->free_pool)) {
671 list_add_tail(&p_ent->list, &p_spq->unlimited_pending);
672 p_spq->unlimited_pending_count++;
676 struct qed_spq_entry *p_en2;
678 p_en2 = list_first_entry(&p_spq->free_pool,
679 struct qed_spq_entry, list);
680 list_del(&p_en2->list);
682 /* Copy the ring element physical pointer to the new
683 * entry, since we are about to override the entire ring
684 * entry and don't want to lose the pointer.
686 p_ent->elem.data_ptr = p_en2->elem.data_ptr;
690 /* EBLOCK responsible to free the allocated p_ent */
691 if (p_ent->comp_mode != QED_SPQ_MODE_EBLOCK)
694 p_ent->post_ent = p_en2;
700 /* entry is to be placed in 'pending' queue */
702 case QED_SPQ_PRIORITY_NORMAL:
703 list_add_tail(&p_ent->list, &p_spq->pending);
704 p_spq->normal_count++;
706 case QED_SPQ_PRIORITY_HIGH:
707 list_add(&p_ent->list, &p_spq->pending);
717 /***************************************************************************
719 ***************************************************************************/
720 u32 qed_spq_get_cid(struct qed_hwfn *p_hwfn)
723 return 0xffffffff; /* illegal */
724 return p_hwfn->p_spq->cid;
727 /***************************************************************************
728 * Posting new Ramrods
729 ***************************************************************************/
730 static int qed_spq_post_list(struct qed_hwfn *p_hwfn,
731 struct list_head *head, u32 keep_reserve)
733 struct qed_spq *p_spq = p_hwfn->p_spq;
736 while (qed_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
738 struct qed_spq_entry *p_ent =
739 list_first_entry(head, struct qed_spq_entry, list);
740 list_del(&p_ent->list);
741 list_add_tail(&p_ent->list, &p_spq->completion_pending);
742 p_spq->comp_sent_count++;
744 rc = qed_spq_hw_post(p_hwfn, p_spq, p_ent);
746 list_del(&p_ent->list);
747 __qed_spq_return_entry(p_hwfn, p_ent);
755 int qed_spq_pend_post(struct qed_hwfn *p_hwfn)
757 struct qed_spq *p_spq = p_hwfn->p_spq;
758 struct qed_spq_entry *p_ent = NULL;
760 while (!list_empty(&p_spq->free_pool)) {
761 if (list_empty(&p_spq->unlimited_pending))
764 p_ent = list_first_entry(&p_spq->unlimited_pending,
765 struct qed_spq_entry, list);
769 list_del(&p_ent->list);
771 qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
774 return qed_spq_post_list(p_hwfn, &p_spq->pending,
775 SPQ_HIGH_PRI_RESERVE_DEFAULT);
778 /* Avoid overriding of SPQ entries when getting out-of-order completions, by
779 * marking the completions in a bitmap and increasing the chain consumer only
780 * for the first successive completed entries.
782 static void qed_spq_comp_bmap_update(struct qed_hwfn *p_hwfn, __le16 echo)
784 u16 pos = le16_to_cpu(echo) % SPQ_RING_SIZE;
785 struct qed_spq *p_spq = p_hwfn->p_spq;
787 __set_bit(pos, p_spq->p_comp_bitmap);
788 while (test_bit(p_spq->comp_bitmap_idx,
789 p_spq->p_comp_bitmap)) {
790 __clear_bit(p_spq->comp_bitmap_idx,
791 p_spq->p_comp_bitmap);
792 p_spq->comp_bitmap_idx++;
793 qed_chain_return_produced(&p_spq->chain);
797 int qed_spq_post(struct qed_hwfn *p_hwfn,
798 struct qed_spq_entry *p_ent, u8 *fw_return_code)
801 struct qed_spq *p_spq = p_hwfn ? p_hwfn->p_spq : NULL;
802 bool b_ret_ent = true;
808 DP_NOTICE(p_hwfn, "Got a NULL pointer\n");
812 /* Complete the entry */
813 rc = qed_spq_fill_entry(p_hwfn, p_ent);
815 spin_lock_bh(&p_spq->lock);
817 /* Check return value after LOCK is taken for cleaner error flow */
821 /* Add the request to the pending queue */
822 rc = qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
826 rc = qed_spq_pend_post(p_hwfn);
828 /* Since it's possible that pending failed for a different
829 * entry [although unlikely], the failed entry was already
830 * dealt with; No need to return it here.
836 spin_unlock_bh(&p_spq->lock);
838 if (p_ent->comp_mode == QED_SPQ_MODE_EBLOCK) {
839 /* For entries in QED BLOCK mode, the completion code cannot
840 * perform the necessary cleanup - if it did, we couldn't
841 * access p_ent here to see whether it's successful or not.
842 * Thus, after gaining the answer perform the cleanup here.
844 rc = qed_spq_block(p_hwfn, p_ent, fw_return_code,
845 p_ent->queue == &p_spq->unlimited_pending);
847 if (p_ent->queue == &p_spq->unlimited_pending) {
848 struct qed_spq_entry *p_post_ent = p_ent->post_ent;
852 /* Return the entry which was actually posted */
860 qed_spq_return_entry(p_hwfn, p_ent);
865 spin_lock_bh(&p_spq->lock);
866 list_del(&p_ent->list);
867 qed_spq_comp_bmap_update(p_hwfn, p_ent->elem.hdr.echo);
870 /* return to the free pool */
872 __qed_spq_return_entry(p_hwfn, p_ent);
873 spin_unlock_bh(&p_spq->lock);
878 int qed_spq_completion(struct qed_hwfn *p_hwfn,
881 union event_ring_data *p_data)
883 struct qed_spq *p_spq;
884 struct qed_spq_entry *p_ent = NULL;
885 struct qed_spq_entry *tmp;
886 struct qed_spq_entry *found = NULL;
891 p_spq = p_hwfn->p_spq;
895 spin_lock_bh(&p_spq->lock);
896 list_for_each_entry_safe(p_ent, tmp, &p_spq->completion_pending, list) {
897 if (p_ent->elem.hdr.echo == echo) {
898 list_del(&p_ent->list);
899 qed_spq_comp_bmap_update(p_hwfn, echo);
905 /* This is relatively uncommon - depends on scenarios
906 * which have mutliple per-PF sent ramrods.
908 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
909 "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
911 le16_to_cpu(p_ent->elem.hdr.echo));
914 /* Release lock before callback, as callback may post
915 * an additional ramrod.
917 spin_unlock_bh(&p_spq->lock);
921 "Failed to find an entry this EQE [echo %04x] completes\n",
926 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
927 "Complete EQE [echo %04x]: func %p cookie %p)\n",
929 p_ent->comp_cb.function, p_ent->comp_cb.cookie);
930 if (found->comp_cb.function)
931 found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
936 "Got a completion without a callback function\n");
938 if (found->comp_mode != QED_SPQ_MODE_EBLOCK)
939 /* EBLOCK is responsible for returning its own entry into the
942 qed_spq_return_entry(p_hwfn, found);
947 int qed_consq_alloc(struct qed_hwfn *p_hwfn)
949 struct qed_consq *p_consq;
951 /* Allocate ConsQ struct */
952 p_consq = kzalloc(sizeof(*p_consq), GFP_KERNEL);
956 /* Allocate and initialize EQ chain*/
957 if (qed_chain_alloc(p_hwfn->cdev,
958 QED_CHAIN_USE_TO_PRODUCE,
960 QED_CHAIN_CNT_TYPE_U16,
961 QED_CHAIN_PAGE_SIZE / 0x80,
962 0x80, &p_consq->chain, NULL))
963 goto consq_allocate_fail;
965 p_hwfn->p_consq = p_consq;
973 void qed_consq_setup(struct qed_hwfn *p_hwfn)
975 qed_chain_reset(&p_hwfn->p_consq->chain);
978 void qed_consq_free(struct qed_hwfn *p_hwfn)
980 if (!p_hwfn->p_consq)
983 qed_chain_free(p_hwfn->cdev, &p_hwfn->p_consq->chain);
985 kfree(p_hwfn->p_consq);
986 p_hwfn->p_consq = NULL;