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 (smp_load_acquire(&comp_done->done) == 1) { /* ^^^ */
102 *p_fw_ret = comp_done->fw_return_code;
106 if (sleep_between_iter)
107 msleep(SPQ_BLOCK_SLEEP_MS);
109 udelay(SPQ_BLOCK_DELAY_US);
115 static int qed_spq_block(struct qed_hwfn *p_hwfn,
116 struct qed_spq_entry *p_ent,
117 u8 *p_fw_ret, bool skip_quick_poll)
119 struct qed_spq_comp_done *comp_done;
120 struct qed_ptt *p_ptt;
123 /* A relatively short polling period w/o sleeping, to allow the FW to
124 * complete the ramrod and thus possibly to avoid the following sleeps.
126 if (!skip_quick_poll) {
127 rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, false);
132 /* Move to polling with a sleeping period between iterations */
133 rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
137 p_ptt = qed_ptt_acquire(p_hwfn);
139 DP_NOTICE(p_hwfn, "ptt, failed to acquire\n");
143 DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
144 rc = qed_mcp_drain(p_hwfn, p_ptt);
145 qed_ptt_release(p_hwfn, p_ptt);
147 DP_NOTICE(p_hwfn, "MCP drain failed\n");
151 /* Retry after drain */
152 rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
156 comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
157 if (comp_done->done == 1) {
159 *p_fw_ret = comp_done->fw_return_code;
164 "Ramrod is stuck [CID %08x cmd %02x protocol %02x echo %04x]\n",
165 le32_to_cpu(p_ent->elem.hdr.cid),
166 p_ent->elem.hdr.cmd_id,
167 p_ent->elem.hdr.protocol_id,
168 le16_to_cpu(p_ent->elem.hdr.echo));
173 /***************************************************************************
174 * SPQ entries inner API
175 ***************************************************************************/
176 static int qed_spq_fill_entry(struct qed_hwfn *p_hwfn,
177 struct qed_spq_entry *p_ent)
181 switch (p_ent->comp_mode) {
182 case QED_SPQ_MODE_EBLOCK:
183 case QED_SPQ_MODE_BLOCK:
184 p_ent->comp_cb.function = qed_spq_blocking_cb;
186 case QED_SPQ_MODE_CB:
189 DP_NOTICE(p_hwfn, "Unknown SPQE completion mode %d\n",
194 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
195 "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x] Data pointer: [%08x:%08x] Completion Mode: %s\n",
197 p_ent->elem.hdr.cmd_id,
198 p_ent->elem.hdr.protocol_id,
199 p_ent->elem.data_ptr.hi,
200 p_ent->elem.data_ptr.lo,
201 D_TRINE(p_ent->comp_mode, QED_SPQ_MODE_EBLOCK,
202 QED_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
208 /***************************************************************************
210 ***************************************************************************/
211 static void qed_spq_hw_initialize(struct qed_hwfn *p_hwfn,
212 struct qed_spq *p_spq)
214 struct e4_core_conn_context *p_cxt;
215 struct qed_cxt_info cxt_info;
219 cxt_info.iid = p_spq->cid;
221 rc = qed_cxt_get_cid_info(p_hwfn, &cxt_info);
224 DP_NOTICE(p_hwfn, "Cannot find context info for cid=%d\n",
229 p_cxt = cxt_info.p_cxt;
231 SET_FIELD(p_cxt->xstorm_ag_context.flags10,
232 E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
233 SET_FIELD(p_cxt->xstorm_ag_context.flags1,
234 E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
235 SET_FIELD(p_cxt->xstorm_ag_context.flags9,
236 E4_XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
238 /* QM physical queue */
239 physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
240 p_cxt->xstorm_ag_context.physical_q0 = cpu_to_le16(physical_q);
242 p_cxt->xstorm_st_context.spq_base_lo =
243 DMA_LO_LE(p_spq->chain.p_phys_addr);
244 p_cxt->xstorm_st_context.spq_base_hi =
245 DMA_HI_LE(p_spq->chain.p_phys_addr);
247 DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
248 p_hwfn->p_consq->chain.p_phys_addr);
251 static int qed_spq_hw_post(struct qed_hwfn *p_hwfn,
252 struct qed_spq *p_spq, struct qed_spq_entry *p_ent)
254 struct qed_chain *p_chain = &p_hwfn->p_spq->chain;
255 u16 echo = qed_chain_get_prod_idx(p_chain);
256 struct slow_path_element *elem;
257 struct core_db_data db;
259 p_ent->elem.hdr.echo = cpu_to_le16(echo);
260 elem = qed_chain_produce(p_chain);
262 DP_NOTICE(p_hwfn, "Failed to produce from SPQ chain\n");
266 *elem = p_ent->elem; /* struct assignment */
268 /* send a doorbell on the slow hwfn session */
269 memset(&db, 0, sizeof(db));
270 SET_FIELD(db.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
271 SET_FIELD(db.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
272 SET_FIELD(db.params, CORE_DB_DATA_AGG_VAL_SEL,
273 DQ_XCM_CORE_SPQ_PROD_CMD);
274 db.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
275 db.spq_prod = cpu_to_le16(qed_chain_get_prod_idx(p_chain));
277 /* make sure the SPQE is updated before the doorbell */
280 DOORBELL(p_hwfn, qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY), *(u32 *)&db);
282 /* make sure doorbell is rang */
285 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
286 "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
287 qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY),
288 p_spq->cid, db.params, db.agg_flags,
289 qed_chain_get_prod_idx(p_chain));
294 /***************************************************************************
295 * Asynchronous events
296 ***************************************************************************/
298 qed_async_event_completion(struct qed_hwfn *p_hwfn,
299 struct event_ring_entry *p_eqe)
301 qed_spq_async_comp_cb cb;
303 if (!p_hwfn->p_spq || (p_eqe->protocol_id >= MAX_PROTOCOL_TYPE))
306 cb = p_hwfn->p_spq->async_comp_cb[p_eqe->protocol_id];
308 return cb(p_hwfn, p_eqe->opcode, p_eqe->echo,
309 &p_eqe->data, p_eqe->fw_return_code);
312 "Unknown Async completion for protocol: %d\n",
319 qed_spq_register_async_cb(struct qed_hwfn *p_hwfn,
320 enum protocol_type protocol_id,
321 qed_spq_async_comp_cb cb)
323 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
326 p_hwfn->p_spq->async_comp_cb[protocol_id] = cb;
331 qed_spq_unregister_async_cb(struct qed_hwfn *p_hwfn,
332 enum protocol_type protocol_id)
334 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
337 p_hwfn->p_spq->async_comp_cb[protocol_id] = NULL;
340 /***************************************************************************
342 ***************************************************************************/
343 void qed_eq_prod_update(struct qed_hwfn *p_hwfn, u16 prod)
345 u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
346 USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
348 REG_WR16(p_hwfn, addr, prod);
350 /* keep prod updates ordered */
354 int qed_eq_completion(struct qed_hwfn *p_hwfn, void *cookie)
356 struct qed_eq *p_eq = cookie;
357 struct qed_chain *p_chain = &p_eq->chain;
360 /* take a snapshot of the FW consumer */
361 u16 fw_cons_idx = le16_to_cpu(*p_eq->p_fw_cons);
363 DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
365 /* Need to guarantee the fw_cons index we use points to a usuable
366 * element (to comply with our chain), so our macros would comply
368 if ((fw_cons_idx & qed_chain_get_usable_per_page(p_chain)) ==
369 qed_chain_get_usable_per_page(p_chain))
370 fw_cons_idx += qed_chain_get_unusable_per_page(p_chain);
372 /* Complete current segment of eq entries */
373 while (fw_cons_idx != qed_chain_get_cons_idx(p_chain)) {
374 struct event_ring_entry *p_eqe = qed_chain_consume(p_chain);
381 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
382 "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
386 le16_to_cpu(p_eqe->echo),
387 p_eqe->fw_return_code,
390 if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
391 if (qed_async_event_completion(p_hwfn, p_eqe))
393 } else if (qed_spq_completion(p_hwfn,
395 p_eqe->fw_return_code,
400 qed_chain_recycle_consumed(p_chain);
403 qed_eq_prod_update(p_hwfn, qed_chain_get_prod_idx(p_chain));
405 /* Attempt to post pending requests */
406 spin_lock_bh(&p_hwfn->p_spq->lock);
407 rc = qed_spq_pend_post(p_hwfn);
408 spin_unlock_bh(&p_hwfn->p_spq->lock);
413 int qed_eq_alloc(struct qed_hwfn *p_hwfn, u16 num_elem)
417 /* Allocate EQ struct */
418 p_eq = kzalloc(sizeof(*p_eq), GFP_KERNEL);
422 /* Allocate and initialize EQ chain*/
423 if (qed_chain_alloc(p_hwfn->cdev,
424 QED_CHAIN_USE_TO_PRODUCE,
426 QED_CHAIN_CNT_TYPE_U16,
428 sizeof(union event_ring_element),
430 goto eq_allocate_fail;
432 /* register EQ completion on the SP SB */
433 qed_int_register_cb(p_hwfn, qed_eq_completion,
434 p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
444 void qed_eq_setup(struct qed_hwfn *p_hwfn)
446 qed_chain_reset(&p_hwfn->p_eq->chain);
449 void qed_eq_free(struct qed_hwfn *p_hwfn)
454 qed_chain_free(p_hwfn->cdev, &p_hwfn->p_eq->chain);
460 /***************************************************************************
461 * CQE API - manipulate EQ functionality
462 ***************************************************************************/
463 static int qed_cqe_completion(struct qed_hwfn *p_hwfn,
464 struct eth_slow_path_rx_cqe *cqe,
465 enum protocol_type protocol)
467 if (IS_VF(p_hwfn->cdev))
470 /* @@@tmp - it's possible we'll eventually want to handle some
471 * actual commands that can arrive here, but for now this is only
472 * used to complete the ramrod using the echo value on the cqe
474 return qed_spq_completion(p_hwfn, cqe->echo, 0, NULL);
477 int qed_eth_cqe_completion(struct qed_hwfn *p_hwfn,
478 struct eth_slow_path_rx_cqe *cqe)
482 rc = qed_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
485 "Failed to handle RXQ CQE [cmd 0x%02x]\n",
491 /***************************************************************************
492 * Slow hwfn Queue (spq)
493 ***************************************************************************/
494 void qed_spq_setup(struct qed_hwfn *p_hwfn)
496 struct qed_spq *p_spq = p_hwfn->p_spq;
497 struct qed_spq_entry *p_virt = NULL;
498 dma_addr_t p_phys = 0;
501 INIT_LIST_HEAD(&p_spq->pending);
502 INIT_LIST_HEAD(&p_spq->completion_pending);
503 INIT_LIST_HEAD(&p_spq->free_pool);
504 INIT_LIST_HEAD(&p_spq->unlimited_pending);
505 spin_lock_init(&p_spq->lock);
508 p_phys = p_spq->p_phys + offsetof(struct qed_spq_entry, ramrod);
509 p_virt = p_spq->p_virt;
511 capacity = qed_chain_get_capacity(&p_spq->chain);
512 for (i = 0; i < capacity; i++) {
513 DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
515 list_add_tail(&p_virt->list, &p_spq->free_pool);
518 p_phys += sizeof(struct qed_spq_entry);
522 p_spq->normal_count = 0;
523 p_spq->comp_count = 0;
524 p_spq->comp_sent_count = 0;
525 p_spq->unlimited_pending_count = 0;
527 bitmap_zero(p_spq->p_comp_bitmap, SPQ_RING_SIZE);
528 p_spq->comp_bitmap_idx = 0;
530 /* SPQ cid, cannot fail */
531 qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
532 qed_spq_hw_initialize(p_hwfn, p_spq);
534 /* reset the chain itself */
535 qed_chain_reset(&p_spq->chain);
538 int qed_spq_alloc(struct qed_hwfn *p_hwfn)
540 struct qed_spq_entry *p_virt = NULL;
541 struct qed_spq *p_spq = NULL;
542 dma_addr_t p_phys = 0;
546 p_spq = kzalloc(sizeof(struct qed_spq), GFP_KERNEL);
551 if (qed_chain_alloc(p_hwfn->cdev,
552 QED_CHAIN_USE_TO_PRODUCE,
553 QED_CHAIN_MODE_SINGLE,
554 QED_CHAIN_CNT_TYPE_U16,
555 0, /* N/A when the mode is SINGLE */
556 sizeof(struct slow_path_element),
557 &p_spq->chain, NULL))
558 goto spq_allocate_fail;
560 /* allocate and fill the SPQ elements (incl. ramrod data list) */
561 capacity = qed_chain_get_capacity(&p_spq->chain);
562 p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
563 capacity * sizeof(struct qed_spq_entry),
564 &p_phys, GFP_KERNEL);
566 goto spq_allocate_fail;
568 p_spq->p_virt = p_virt;
569 p_spq->p_phys = p_phys;
570 p_hwfn->p_spq = p_spq;
575 qed_chain_free(p_hwfn->cdev, &p_spq->chain);
580 void qed_spq_free(struct qed_hwfn *p_hwfn)
582 struct qed_spq *p_spq = p_hwfn->p_spq;
589 capacity = qed_chain_get_capacity(&p_spq->chain);
590 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
592 sizeof(struct qed_spq_entry),
593 p_spq->p_virt, p_spq->p_phys);
596 qed_chain_free(p_hwfn->cdev, &p_spq->chain);
598 p_hwfn->p_spq = NULL;
601 int qed_spq_get_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry **pp_ent)
603 struct qed_spq *p_spq = p_hwfn->p_spq;
604 struct qed_spq_entry *p_ent = NULL;
607 spin_lock_bh(&p_spq->lock);
609 if (list_empty(&p_spq->free_pool)) {
610 p_ent = kzalloc(sizeof(*p_ent), GFP_ATOMIC);
613 "Failed to allocate an SPQ entry for a pending ramrod\n");
617 p_ent->queue = &p_spq->unlimited_pending;
619 p_ent = list_first_entry(&p_spq->free_pool,
620 struct qed_spq_entry, list);
621 list_del(&p_ent->list);
622 p_ent->queue = &p_spq->pending;
628 spin_unlock_bh(&p_spq->lock);
632 /* Locked variant; Should be called while the SPQ lock is taken */
633 static void __qed_spq_return_entry(struct qed_hwfn *p_hwfn,
634 struct qed_spq_entry *p_ent)
636 list_add_tail(&p_ent->list, &p_hwfn->p_spq->free_pool);
639 void qed_spq_return_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry *p_ent)
641 spin_lock_bh(&p_hwfn->p_spq->lock);
642 __qed_spq_return_entry(p_hwfn, p_ent);
643 spin_unlock_bh(&p_hwfn->p_spq->lock);
647 * @brief qed_spq_add_entry - adds a new entry to the pending
648 * list. Should be used while lock is being held.
650 * Addes an entry to the pending list is there is room (en empty
651 * element is available in the free_pool), or else places the
652 * entry in the unlimited_pending pool.
660 static int qed_spq_add_entry(struct qed_hwfn *p_hwfn,
661 struct qed_spq_entry *p_ent,
662 enum spq_priority priority)
664 struct qed_spq *p_spq = p_hwfn->p_spq;
666 if (p_ent->queue == &p_spq->unlimited_pending) {
668 if (list_empty(&p_spq->free_pool)) {
669 list_add_tail(&p_ent->list, &p_spq->unlimited_pending);
670 p_spq->unlimited_pending_count++;
674 struct qed_spq_entry *p_en2;
676 p_en2 = list_first_entry(&p_spq->free_pool,
677 struct qed_spq_entry, list);
678 list_del(&p_en2->list);
680 /* Copy the ring element physical pointer to the new
681 * entry, since we are about to override the entire ring
682 * entry and don't want to lose the pointer.
684 p_ent->elem.data_ptr = p_en2->elem.data_ptr;
688 /* EBLOCK responsible to free the allocated p_ent */
689 if (p_ent->comp_mode != QED_SPQ_MODE_EBLOCK)
692 p_ent->post_ent = p_en2;
698 /* entry is to be placed in 'pending' queue */
700 case QED_SPQ_PRIORITY_NORMAL:
701 list_add_tail(&p_ent->list, &p_spq->pending);
702 p_spq->normal_count++;
704 case QED_SPQ_PRIORITY_HIGH:
705 list_add(&p_ent->list, &p_spq->pending);
715 /***************************************************************************
717 ***************************************************************************/
718 u32 qed_spq_get_cid(struct qed_hwfn *p_hwfn)
721 return 0xffffffff; /* illegal */
722 return p_hwfn->p_spq->cid;
725 /***************************************************************************
726 * Posting new Ramrods
727 ***************************************************************************/
728 static int qed_spq_post_list(struct qed_hwfn *p_hwfn,
729 struct list_head *head, u32 keep_reserve)
731 struct qed_spq *p_spq = p_hwfn->p_spq;
734 while (qed_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
736 struct qed_spq_entry *p_ent =
737 list_first_entry(head, struct qed_spq_entry, list);
738 list_del(&p_ent->list);
739 list_add_tail(&p_ent->list, &p_spq->completion_pending);
740 p_spq->comp_sent_count++;
742 rc = qed_spq_hw_post(p_hwfn, p_spq, p_ent);
744 list_del(&p_ent->list);
745 __qed_spq_return_entry(p_hwfn, p_ent);
753 int qed_spq_pend_post(struct qed_hwfn *p_hwfn)
755 struct qed_spq *p_spq = p_hwfn->p_spq;
756 struct qed_spq_entry *p_ent = NULL;
758 while (!list_empty(&p_spq->free_pool)) {
759 if (list_empty(&p_spq->unlimited_pending))
762 p_ent = list_first_entry(&p_spq->unlimited_pending,
763 struct qed_spq_entry, list);
767 list_del(&p_ent->list);
769 qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
772 return qed_spq_post_list(p_hwfn, &p_spq->pending,
773 SPQ_HIGH_PRI_RESERVE_DEFAULT);
776 /* Avoid overriding of SPQ entries when getting out-of-order completions, by
777 * marking the completions in a bitmap and increasing the chain consumer only
778 * for the first successive completed entries.
780 static void qed_spq_comp_bmap_update(struct qed_hwfn *p_hwfn, __le16 echo)
782 u16 pos = le16_to_cpu(echo) % SPQ_RING_SIZE;
783 struct qed_spq *p_spq = p_hwfn->p_spq;
785 __set_bit(pos, p_spq->p_comp_bitmap);
786 while (test_bit(p_spq->comp_bitmap_idx,
787 p_spq->p_comp_bitmap)) {
788 __clear_bit(p_spq->comp_bitmap_idx,
789 p_spq->p_comp_bitmap);
790 p_spq->comp_bitmap_idx++;
791 qed_chain_return_produced(&p_spq->chain);
795 int qed_spq_post(struct qed_hwfn *p_hwfn,
796 struct qed_spq_entry *p_ent, u8 *fw_return_code)
799 struct qed_spq *p_spq = p_hwfn ? p_hwfn->p_spq : NULL;
800 bool b_ret_ent = true;
807 DP_NOTICE(p_hwfn, "Got a NULL pointer\n");
811 /* Complete the entry */
812 rc = qed_spq_fill_entry(p_hwfn, p_ent);
814 spin_lock_bh(&p_spq->lock);
816 /* Check return value after LOCK is taken for cleaner error flow */
820 /* Check if entry is in block mode before qed_spq_add_entry,
821 * which might kfree p_ent.
823 eblock = (p_ent->comp_mode == QED_SPQ_MODE_EBLOCK);
825 /* Add the request to the pending queue */
826 rc = qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
830 rc = qed_spq_pend_post(p_hwfn);
832 /* Since it's possible that pending failed for a different
833 * entry [although unlikely], the failed entry was already
834 * dealt with; No need to return it here.
840 spin_unlock_bh(&p_spq->lock);
843 /* For entries in QED BLOCK mode, the completion code cannot
844 * perform the necessary cleanup - if it did, we couldn't
845 * access p_ent here to see whether it's successful or not.
846 * Thus, after gaining the answer perform the cleanup here.
848 rc = qed_spq_block(p_hwfn, p_ent, fw_return_code,
849 p_ent->queue == &p_spq->unlimited_pending);
851 if (p_ent->queue == &p_spq->unlimited_pending) {
852 struct qed_spq_entry *p_post_ent = p_ent->post_ent;
856 /* Return the entry which was actually posted */
864 qed_spq_return_entry(p_hwfn, p_ent);
869 spin_lock_bh(&p_spq->lock);
870 list_del(&p_ent->list);
871 qed_spq_comp_bmap_update(p_hwfn, p_ent->elem.hdr.echo);
874 /* return to the free pool */
876 __qed_spq_return_entry(p_hwfn, p_ent);
877 spin_unlock_bh(&p_spq->lock);
882 int qed_spq_completion(struct qed_hwfn *p_hwfn,
885 union event_ring_data *p_data)
887 struct qed_spq *p_spq;
888 struct qed_spq_entry *p_ent = NULL;
889 struct qed_spq_entry *tmp;
890 struct qed_spq_entry *found = NULL;
895 p_spq = p_hwfn->p_spq;
899 spin_lock_bh(&p_spq->lock);
900 list_for_each_entry_safe(p_ent, tmp, &p_spq->completion_pending, list) {
901 if (p_ent->elem.hdr.echo == echo) {
902 list_del(&p_ent->list);
903 qed_spq_comp_bmap_update(p_hwfn, echo);
909 /* This is relatively uncommon - depends on scenarios
910 * which have mutliple per-PF sent ramrods.
912 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
913 "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
915 le16_to_cpu(p_ent->elem.hdr.echo));
918 /* Release lock before callback, as callback may post
919 * an additional ramrod.
921 spin_unlock_bh(&p_spq->lock);
925 "Failed to find an entry this EQE [echo %04x] completes\n",
930 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
931 "Complete EQE [echo %04x]: func %p cookie %p)\n",
933 p_ent->comp_cb.function, p_ent->comp_cb.cookie);
934 if (found->comp_cb.function)
935 found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
940 "Got a completion without a callback function\n");
942 if (found->comp_mode != QED_SPQ_MODE_EBLOCK)
943 /* EBLOCK is responsible for returning its own entry into the
946 qed_spq_return_entry(p_hwfn, found);
951 int qed_consq_alloc(struct qed_hwfn *p_hwfn)
953 struct qed_consq *p_consq;
955 /* Allocate ConsQ struct */
956 p_consq = kzalloc(sizeof(*p_consq), GFP_KERNEL);
960 /* Allocate and initialize EQ chain*/
961 if (qed_chain_alloc(p_hwfn->cdev,
962 QED_CHAIN_USE_TO_PRODUCE,
964 QED_CHAIN_CNT_TYPE_U16,
965 QED_CHAIN_PAGE_SIZE / 0x80,
966 0x80, &p_consq->chain, NULL))
967 goto consq_allocate_fail;
969 p_hwfn->p_consq = p_consq;
977 void qed_consq_setup(struct qed_hwfn *p_hwfn)
979 qed_chain_reset(&p_hwfn->p_consq->chain);
982 void qed_consq_free(struct qed_hwfn *p_hwfn)
984 if (!p_hwfn->p_consq)
987 qed_chain_free(p_hwfn->cdev, &p_hwfn->p_consq->chain);
989 kfree(p_hwfn->p_consq);
990 p_hwfn->p_consq = NULL;