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 struct core_db_data *p_db_data = &p_spq->db_data;
256 u16 echo = qed_chain_get_prod_idx(p_chain);
257 struct slow_path_element *elem;
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 p_db_data->spq_prod = cpu_to_le16(qed_chain_get_prod_idx(p_chain));
271 /* make sure the SPQE is updated before the doorbell */
274 DOORBELL(p_hwfn, p_spq->db_addr_offset, *(u32 *)p_db_data);
276 /* make sure doorbell is rang */
279 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
280 "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
281 p_spq->db_addr_offset,
284 p_db_data->agg_flags, qed_chain_get_prod_idx(p_chain));
289 /***************************************************************************
290 * Asynchronous events
291 ***************************************************************************/
293 qed_async_event_completion(struct qed_hwfn *p_hwfn,
294 struct event_ring_entry *p_eqe)
296 qed_spq_async_comp_cb cb;
298 if (!p_hwfn->p_spq || (p_eqe->protocol_id >= MAX_PROTOCOL_TYPE))
301 cb = p_hwfn->p_spq->async_comp_cb[p_eqe->protocol_id];
303 return cb(p_hwfn, p_eqe->opcode, p_eqe->echo,
304 &p_eqe->data, p_eqe->fw_return_code);
307 "Unknown Async completion for protocol: %d\n",
314 qed_spq_register_async_cb(struct qed_hwfn *p_hwfn,
315 enum protocol_type protocol_id,
316 qed_spq_async_comp_cb cb)
318 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
321 p_hwfn->p_spq->async_comp_cb[protocol_id] = cb;
326 qed_spq_unregister_async_cb(struct qed_hwfn *p_hwfn,
327 enum protocol_type protocol_id)
329 if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
332 p_hwfn->p_spq->async_comp_cb[protocol_id] = NULL;
335 /***************************************************************************
337 ***************************************************************************/
338 void qed_eq_prod_update(struct qed_hwfn *p_hwfn, u16 prod)
340 u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
341 USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
343 REG_WR16(p_hwfn, addr, prod);
345 /* keep prod updates ordered */
349 int qed_eq_completion(struct qed_hwfn *p_hwfn, void *cookie)
351 struct qed_eq *p_eq = cookie;
352 struct qed_chain *p_chain = &p_eq->chain;
355 /* take a snapshot of the FW consumer */
356 u16 fw_cons_idx = le16_to_cpu(*p_eq->p_fw_cons);
358 DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
360 /* Need to guarantee the fw_cons index we use points to a usuable
361 * element (to comply with our chain), so our macros would comply
363 if ((fw_cons_idx & qed_chain_get_usable_per_page(p_chain)) ==
364 qed_chain_get_usable_per_page(p_chain))
365 fw_cons_idx += qed_chain_get_unusable_per_page(p_chain);
367 /* Complete current segment of eq entries */
368 while (fw_cons_idx != qed_chain_get_cons_idx(p_chain)) {
369 struct event_ring_entry *p_eqe = qed_chain_consume(p_chain);
376 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
377 "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
381 le16_to_cpu(p_eqe->echo),
382 p_eqe->fw_return_code,
385 if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
386 if (qed_async_event_completion(p_hwfn, p_eqe))
388 } else if (qed_spq_completion(p_hwfn,
390 p_eqe->fw_return_code,
395 qed_chain_recycle_consumed(p_chain);
398 qed_eq_prod_update(p_hwfn, qed_chain_get_prod_idx(p_chain));
400 /* Attempt to post pending requests */
401 spin_lock_bh(&p_hwfn->p_spq->lock);
402 rc = qed_spq_pend_post(p_hwfn);
403 spin_unlock_bh(&p_hwfn->p_spq->lock);
408 int qed_eq_alloc(struct qed_hwfn *p_hwfn, u16 num_elem)
412 /* Allocate EQ struct */
413 p_eq = kzalloc(sizeof(*p_eq), GFP_KERNEL);
417 /* Allocate and initialize EQ chain*/
418 if (qed_chain_alloc(p_hwfn->cdev,
419 QED_CHAIN_USE_TO_PRODUCE,
421 QED_CHAIN_CNT_TYPE_U16,
423 sizeof(union event_ring_element),
425 goto eq_allocate_fail;
427 /* register EQ completion on the SP SB */
428 qed_int_register_cb(p_hwfn, qed_eq_completion,
429 p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
439 void qed_eq_setup(struct qed_hwfn *p_hwfn)
441 qed_chain_reset(&p_hwfn->p_eq->chain);
444 void qed_eq_free(struct qed_hwfn *p_hwfn)
449 qed_chain_free(p_hwfn->cdev, &p_hwfn->p_eq->chain);
455 /***************************************************************************
456 * CQE API - manipulate EQ functionality
457 ***************************************************************************/
458 static int qed_cqe_completion(struct qed_hwfn *p_hwfn,
459 struct eth_slow_path_rx_cqe *cqe,
460 enum protocol_type protocol)
462 if (IS_VF(p_hwfn->cdev))
465 /* @@@tmp - it's possible we'll eventually want to handle some
466 * actual commands that can arrive here, but for now this is only
467 * used to complete the ramrod using the echo value on the cqe
469 return qed_spq_completion(p_hwfn, cqe->echo, 0, NULL);
472 int qed_eth_cqe_completion(struct qed_hwfn *p_hwfn,
473 struct eth_slow_path_rx_cqe *cqe)
477 rc = qed_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
480 "Failed to handle RXQ CQE [cmd 0x%02x]\n",
486 /***************************************************************************
487 * Slow hwfn Queue (spq)
488 ***************************************************************************/
489 void qed_spq_setup(struct qed_hwfn *p_hwfn)
491 struct qed_spq *p_spq = p_hwfn->p_spq;
492 struct qed_spq_entry *p_virt = NULL;
493 struct core_db_data *p_db_data;
494 void __iomem *db_addr;
495 dma_addr_t p_phys = 0;
499 INIT_LIST_HEAD(&p_spq->pending);
500 INIT_LIST_HEAD(&p_spq->completion_pending);
501 INIT_LIST_HEAD(&p_spq->free_pool);
502 INIT_LIST_HEAD(&p_spq->unlimited_pending);
503 spin_lock_init(&p_spq->lock);
506 p_phys = p_spq->p_phys + offsetof(struct qed_spq_entry, ramrod);
507 p_virt = p_spq->p_virt;
509 capacity = qed_chain_get_capacity(&p_spq->chain);
510 for (i = 0; i < capacity; i++) {
511 DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
513 list_add_tail(&p_virt->list, &p_spq->free_pool);
516 p_phys += sizeof(struct qed_spq_entry);
520 p_spq->normal_count = 0;
521 p_spq->comp_count = 0;
522 p_spq->comp_sent_count = 0;
523 p_spq->unlimited_pending_count = 0;
525 bitmap_zero(p_spq->p_comp_bitmap, SPQ_RING_SIZE);
526 p_spq->comp_bitmap_idx = 0;
528 /* SPQ cid, cannot fail */
529 qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
530 qed_spq_hw_initialize(p_hwfn, p_spq);
532 /* reset the chain itself */
533 qed_chain_reset(&p_spq->chain);
535 /* Initialize the address/data of the SPQ doorbell */
536 p_spq->db_addr_offset = qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY);
537 p_db_data = &p_spq->db_data;
538 memset(p_db_data, 0, sizeof(*p_db_data));
539 SET_FIELD(p_db_data->params, CORE_DB_DATA_DEST, DB_DEST_XCM);
540 SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_MAX);
541 SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_VAL_SEL,
542 DQ_XCM_CORE_SPQ_PROD_CMD);
543 p_db_data->agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
545 /* Register the SPQ doorbell with the doorbell recovery mechanism */
546 db_addr = (void __iomem *)((u8 __iomem *)p_hwfn->doorbells +
547 p_spq->db_addr_offset);
548 rc = qed_db_recovery_add(p_hwfn->cdev, db_addr, &p_spq->db_data,
549 DB_REC_WIDTH_32B, DB_REC_KERNEL);
552 "Failed to register the SPQ doorbell with the doorbell recovery mechanism\n");
555 int qed_spq_alloc(struct qed_hwfn *p_hwfn)
557 struct qed_spq_entry *p_virt = NULL;
558 struct qed_spq *p_spq = NULL;
559 dma_addr_t p_phys = 0;
563 p_spq = kzalloc(sizeof(struct qed_spq), GFP_KERNEL);
568 if (qed_chain_alloc(p_hwfn->cdev,
569 QED_CHAIN_USE_TO_PRODUCE,
570 QED_CHAIN_MODE_SINGLE,
571 QED_CHAIN_CNT_TYPE_U16,
572 0, /* N/A when the mode is SINGLE */
573 sizeof(struct slow_path_element),
574 &p_spq->chain, NULL))
575 goto spq_allocate_fail;
577 /* allocate and fill the SPQ elements (incl. ramrod data list) */
578 capacity = qed_chain_get_capacity(&p_spq->chain);
579 p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
580 capacity * sizeof(struct qed_spq_entry),
581 &p_phys, GFP_KERNEL);
583 goto spq_allocate_fail;
585 p_spq->p_virt = p_virt;
586 p_spq->p_phys = p_phys;
587 p_hwfn->p_spq = p_spq;
592 qed_chain_free(p_hwfn->cdev, &p_spq->chain);
597 void qed_spq_free(struct qed_hwfn *p_hwfn)
599 struct qed_spq *p_spq = p_hwfn->p_spq;
600 void __iomem *db_addr;
606 /* Delete the SPQ doorbell from the doorbell recovery mechanism */
607 db_addr = (void __iomem *)((u8 __iomem *)p_hwfn->doorbells +
608 p_spq->db_addr_offset);
609 qed_db_recovery_del(p_hwfn->cdev, db_addr, &p_spq->db_data);
612 capacity = qed_chain_get_capacity(&p_spq->chain);
613 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
615 sizeof(struct qed_spq_entry),
616 p_spq->p_virt, p_spq->p_phys);
619 qed_chain_free(p_hwfn->cdev, &p_spq->chain);
621 p_hwfn->p_spq = NULL;
624 int qed_spq_get_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry **pp_ent)
626 struct qed_spq *p_spq = p_hwfn->p_spq;
627 struct qed_spq_entry *p_ent = NULL;
630 spin_lock_bh(&p_spq->lock);
632 if (list_empty(&p_spq->free_pool)) {
633 p_ent = kzalloc(sizeof(*p_ent), GFP_ATOMIC);
636 "Failed to allocate an SPQ entry for a pending ramrod\n");
640 p_ent->queue = &p_spq->unlimited_pending;
642 p_ent = list_first_entry(&p_spq->free_pool,
643 struct qed_spq_entry, list);
644 list_del(&p_ent->list);
645 p_ent->queue = &p_spq->pending;
651 spin_unlock_bh(&p_spq->lock);
655 /* Locked variant; Should be called while the SPQ lock is taken */
656 static void __qed_spq_return_entry(struct qed_hwfn *p_hwfn,
657 struct qed_spq_entry *p_ent)
659 list_add_tail(&p_ent->list, &p_hwfn->p_spq->free_pool);
662 void qed_spq_return_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry *p_ent)
664 spin_lock_bh(&p_hwfn->p_spq->lock);
665 __qed_spq_return_entry(p_hwfn, p_ent);
666 spin_unlock_bh(&p_hwfn->p_spq->lock);
670 * @brief qed_spq_add_entry - adds a new entry to the pending
671 * list. Should be used while lock is being held.
673 * Addes an entry to the pending list is there is room (en empty
674 * element is available in the free_pool), or else places the
675 * entry in the unlimited_pending pool.
683 static int qed_spq_add_entry(struct qed_hwfn *p_hwfn,
684 struct qed_spq_entry *p_ent,
685 enum spq_priority priority)
687 struct qed_spq *p_spq = p_hwfn->p_spq;
689 if (p_ent->queue == &p_spq->unlimited_pending) {
691 if (list_empty(&p_spq->free_pool)) {
692 list_add_tail(&p_ent->list, &p_spq->unlimited_pending);
693 p_spq->unlimited_pending_count++;
697 struct qed_spq_entry *p_en2;
699 p_en2 = list_first_entry(&p_spq->free_pool,
700 struct qed_spq_entry, list);
701 list_del(&p_en2->list);
703 /* Copy the ring element physical pointer to the new
704 * entry, since we are about to override the entire ring
705 * entry and don't want to lose the pointer.
707 p_ent->elem.data_ptr = p_en2->elem.data_ptr;
711 /* EBLOCK responsible to free the allocated p_ent */
712 if (p_ent->comp_mode != QED_SPQ_MODE_EBLOCK)
715 p_ent->post_ent = p_en2;
721 /* entry is to be placed in 'pending' queue */
723 case QED_SPQ_PRIORITY_NORMAL:
724 list_add_tail(&p_ent->list, &p_spq->pending);
725 p_spq->normal_count++;
727 case QED_SPQ_PRIORITY_HIGH:
728 list_add(&p_ent->list, &p_spq->pending);
738 /***************************************************************************
740 ***************************************************************************/
741 u32 qed_spq_get_cid(struct qed_hwfn *p_hwfn)
744 return 0xffffffff; /* illegal */
745 return p_hwfn->p_spq->cid;
748 /***************************************************************************
749 * Posting new Ramrods
750 ***************************************************************************/
751 static int qed_spq_post_list(struct qed_hwfn *p_hwfn,
752 struct list_head *head, u32 keep_reserve)
754 struct qed_spq *p_spq = p_hwfn->p_spq;
757 while (qed_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
759 struct qed_spq_entry *p_ent =
760 list_first_entry(head, struct qed_spq_entry, list);
761 list_move_tail(&p_ent->list, &p_spq->completion_pending);
762 p_spq->comp_sent_count++;
764 rc = qed_spq_hw_post(p_hwfn, p_spq, p_ent);
766 list_del(&p_ent->list);
767 __qed_spq_return_entry(p_hwfn, p_ent);
775 int qed_spq_pend_post(struct qed_hwfn *p_hwfn)
777 struct qed_spq *p_spq = p_hwfn->p_spq;
778 struct qed_spq_entry *p_ent = NULL;
780 while (!list_empty(&p_spq->free_pool)) {
781 if (list_empty(&p_spq->unlimited_pending))
784 p_ent = list_first_entry(&p_spq->unlimited_pending,
785 struct qed_spq_entry, list);
789 list_del(&p_ent->list);
791 qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
794 return qed_spq_post_list(p_hwfn, &p_spq->pending,
795 SPQ_HIGH_PRI_RESERVE_DEFAULT);
798 static void qed_spq_recov_set_ret_code(struct qed_spq_entry *p_ent,
804 if (p_ent->elem.hdr.protocol_id == PROTOCOLID_ROCE ||
805 p_ent->elem.hdr.protocol_id == PROTOCOLID_IWARP)
806 *fw_return_code = RDMA_RETURN_OK;
809 /* Avoid overriding of SPQ entries when getting out-of-order completions, by
810 * marking the completions in a bitmap and increasing the chain consumer only
811 * for the first successive completed entries.
813 static void qed_spq_comp_bmap_update(struct qed_hwfn *p_hwfn, __le16 echo)
815 u16 pos = le16_to_cpu(echo) % SPQ_RING_SIZE;
816 struct qed_spq *p_spq = p_hwfn->p_spq;
818 __set_bit(pos, p_spq->p_comp_bitmap);
819 while (test_bit(p_spq->comp_bitmap_idx,
820 p_spq->p_comp_bitmap)) {
821 __clear_bit(p_spq->comp_bitmap_idx,
822 p_spq->p_comp_bitmap);
823 p_spq->comp_bitmap_idx++;
824 qed_chain_return_produced(&p_spq->chain);
828 int qed_spq_post(struct qed_hwfn *p_hwfn,
829 struct qed_spq_entry *p_ent, u8 *fw_return_code)
832 struct qed_spq *p_spq = p_hwfn ? p_hwfn->p_spq : NULL;
833 bool b_ret_ent = true;
840 DP_NOTICE(p_hwfn, "Got a NULL pointer\n");
844 if (p_hwfn->cdev->recov_in_prog) {
847 "Recovery is in progress. Skip spq post [cmd %02x protocol %02x]\n",
848 p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id);
850 /* Let the flow complete w/o any error handling */
851 qed_spq_recov_set_ret_code(p_ent, fw_return_code);
855 /* Complete the entry */
856 rc = qed_spq_fill_entry(p_hwfn, p_ent);
858 spin_lock_bh(&p_spq->lock);
860 /* Check return value after LOCK is taken for cleaner error flow */
864 /* Check if entry is in block mode before qed_spq_add_entry,
865 * which might kfree p_ent.
867 eblock = (p_ent->comp_mode == QED_SPQ_MODE_EBLOCK);
869 /* Add the request to the pending queue */
870 rc = qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
874 rc = qed_spq_pend_post(p_hwfn);
876 /* Since it's possible that pending failed for a different
877 * entry [although unlikely], the failed entry was already
878 * dealt with; No need to return it here.
884 spin_unlock_bh(&p_spq->lock);
887 /* For entries in QED BLOCK mode, the completion code cannot
888 * perform the necessary cleanup - if it did, we couldn't
889 * access p_ent here to see whether it's successful or not.
890 * Thus, after gaining the answer perform the cleanup here.
892 rc = qed_spq_block(p_hwfn, p_ent, fw_return_code,
893 p_ent->queue == &p_spq->unlimited_pending);
895 if (p_ent->queue == &p_spq->unlimited_pending) {
896 struct qed_spq_entry *p_post_ent = p_ent->post_ent;
900 /* Return the entry which was actually posted */
908 qed_spq_return_entry(p_hwfn, p_ent);
913 spin_lock_bh(&p_spq->lock);
914 list_del(&p_ent->list);
915 qed_spq_comp_bmap_update(p_hwfn, p_ent->elem.hdr.echo);
918 /* return to the free pool */
920 __qed_spq_return_entry(p_hwfn, p_ent);
921 spin_unlock_bh(&p_spq->lock);
926 int qed_spq_completion(struct qed_hwfn *p_hwfn,
929 union event_ring_data *p_data)
931 struct qed_spq *p_spq;
932 struct qed_spq_entry *p_ent = NULL;
933 struct qed_spq_entry *tmp;
934 struct qed_spq_entry *found = NULL;
939 p_spq = p_hwfn->p_spq;
943 spin_lock_bh(&p_spq->lock);
944 list_for_each_entry_safe(p_ent, tmp, &p_spq->completion_pending, list) {
945 if (p_ent->elem.hdr.echo == echo) {
946 list_del(&p_ent->list);
947 qed_spq_comp_bmap_update(p_hwfn, echo);
953 /* This is relatively uncommon - depends on scenarios
954 * which have mutliple per-PF sent ramrods.
956 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
957 "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
959 le16_to_cpu(p_ent->elem.hdr.echo));
962 /* Release lock before callback, as callback may post
963 * an additional ramrod.
965 spin_unlock_bh(&p_spq->lock);
969 "Failed to find an entry this EQE [echo %04x] completes\n",
974 DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
975 "Complete EQE [echo %04x]: func %p cookie %p)\n",
977 p_ent->comp_cb.function, p_ent->comp_cb.cookie);
978 if (found->comp_cb.function)
979 found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
984 "Got a completion without a callback function\n");
986 if (found->comp_mode != QED_SPQ_MODE_EBLOCK)
987 /* EBLOCK is responsible for returning its own entry into the
990 qed_spq_return_entry(p_hwfn, found);
995 int qed_consq_alloc(struct qed_hwfn *p_hwfn)
997 struct qed_consq *p_consq;
999 /* Allocate ConsQ struct */
1000 p_consq = kzalloc(sizeof(*p_consq), GFP_KERNEL);
1004 /* Allocate and initialize EQ chain*/
1005 if (qed_chain_alloc(p_hwfn->cdev,
1006 QED_CHAIN_USE_TO_PRODUCE,
1008 QED_CHAIN_CNT_TYPE_U16,
1009 QED_CHAIN_PAGE_SIZE / 0x80,
1010 0x80, &p_consq->chain, NULL))
1011 goto consq_allocate_fail;
1013 p_hwfn->p_consq = p_consq;
1016 consq_allocate_fail:
1021 void qed_consq_setup(struct qed_hwfn *p_hwfn)
1023 qed_chain_reset(&p_hwfn->p_consq->chain);
1026 void qed_consq_free(struct qed_hwfn *p_hwfn)
1028 if (!p_hwfn->p_consq)
1031 qed_chain_free(p_hwfn->cdev, &p_hwfn->p_consq->chain);
1033 kfree(p_hwfn->p_consq);
1034 p_hwfn->p_consq = NULL;