Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
0002 /* QLogic qed NIC Driver
0003  * Copyright (c) 2015-2017  QLogic Corporation
0004  * Copyright (c) 2019-2020 Marvell International Ltd.
0005  */
0006 
0007 #include <linux/types.h>
0008 #include <asm/byteorder.h>
0009 #include <linux/io.h>
0010 #include <linux/delay.h>
0011 #include <linux/dma-mapping.h>
0012 #include <linux/errno.h>
0013 #include <linux/kernel.h>
0014 #include <linux/list.h>
0015 #include <linux/pci.h>
0016 #include <linux/slab.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/string.h>
0019 #include "qed.h"
0020 #include "qed_cxt.h"
0021 #include "qed_dev_api.h"
0022 #include "qed_hsi.h"
0023 #include "qed_iro_hsi.h"
0024 #include "qed_hw.h"
0025 #include "qed_int.h"
0026 #include "qed_iscsi.h"
0027 #include "qed_mcp.h"
0028 #include "qed_ooo.h"
0029 #include "qed_reg_addr.h"
0030 #include "qed_sp.h"
0031 #include "qed_sriov.h"
0032 #include "qed_rdma.h"
0033 
0034 /***************************************************************************
0035  * Structures & Definitions
0036  ***************************************************************************/
0037 
0038 #define SPQ_HIGH_PRI_RESERVE_DEFAULT    (1)
0039 
0040 #define SPQ_BLOCK_DELAY_MAX_ITER        (10)
0041 #define SPQ_BLOCK_DELAY_US              (10)
0042 #define SPQ_BLOCK_SLEEP_MAX_ITER        (1000)
0043 #define SPQ_BLOCK_SLEEP_MS              (5)
0044 
0045 /***************************************************************************
0046  * Blocking Imp. (BLOCK/EBLOCK mode)
0047  ***************************************************************************/
0048 static void qed_spq_blocking_cb(struct qed_hwfn *p_hwfn,
0049                 void *cookie,
0050                 union event_ring_data *data, u8 fw_return_code)
0051 {
0052     struct qed_spq_comp_done *comp_done;
0053 
0054     comp_done = (struct qed_spq_comp_done *)cookie;
0055 
0056     comp_done->fw_return_code = fw_return_code;
0057 
0058     /* Make sure completion done is visible on waiting thread */
0059     smp_store_release(&comp_done->done, 0x1);
0060 }
0061 
0062 static int __qed_spq_block(struct qed_hwfn *p_hwfn,
0063                struct qed_spq_entry *p_ent,
0064                u8 *p_fw_ret, bool sleep_between_iter)
0065 {
0066     struct qed_spq_comp_done *comp_done;
0067     u32 iter_cnt;
0068 
0069     comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
0070     iter_cnt = sleep_between_iter ? SPQ_BLOCK_SLEEP_MAX_ITER
0071                       : SPQ_BLOCK_DELAY_MAX_ITER;
0072 
0073     while (iter_cnt--) {
0074         /* Validate we receive completion update */
0075         if (smp_load_acquire(&comp_done->done) == 1) { /* ^^^ */
0076             if (p_fw_ret)
0077                 *p_fw_ret = comp_done->fw_return_code;
0078             return 0;
0079         }
0080 
0081         if (sleep_between_iter)
0082             msleep(SPQ_BLOCK_SLEEP_MS);
0083         else
0084             udelay(SPQ_BLOCK_DELAY_US);
0085     }
0086 
0087     return -EBUSY;
0088 }
0089 
0090 static int qed_spq_block(struct qed_hwfn *p_hwfn,
0091              struct qed_spq_entry *p_ent,
0092              u8 *p_fw_ret, bool skip_quick_poll)
0093 {
0094     struct qed_spq_comp_done *comp_done;
0095     struct qed_ptt *p_ptt;
0096     int rc;
0097 
0098     /* A relatively short polling period w/o sleeping, to allow the FW to
0099      * complete the ramrod and thus possibly to avoid the following sleeps.
0100      */
0101     if (!skip_quick_poll) {
0102         rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, false);
0103         if (!rc)
0104             return 0;
0105     }
0106 
0107     /* Move to polling with a sleeping period between iterations */
0108     rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
0109     if (!rc)
0110         return 0;
0111 
0112     p_ptt = qed_ptt_acquire(p_hwfn);
0113     if (!p_ptt) {
0114         DP_NOTICE(p_hwfn, "ptt, failed to acquire\n");
0115         return -EAGAIN;
0116     }
0117 
0118     DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
0119     rc = qed_mcp_drain(p_hwfn, p_ptt);
0120     qed_ptt_release(p_hwfn, p_ptt);
0121     if (rc) {
0122         DP_NOTICE(p_hwfn, "MCP drain failed\n");
0123         goto err;
0124     }
0125 
0126     /* Retry after drain */
0127     rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
0128     if (!rc)
0129         return 0;
0130 
0131     comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
0132     if (comp_done->done == 1) {
0133         if (p_fw_ret)
0134             *p_fw_ret = comp_done->fw_return_code;
0135         return 0;
0136     }
0137 err:
0138     p_ptt = qed_ptt_acquire(p_hwfn);
0139     if (!p_ptt)
0140         return -EBUSY;
0141     qed_hw_err_notify(p_hwfn, p_ptt, QED_HW_ERR_RAMROD_FAIL,
0142               "Ramrod is stuck [CID %08x %s:%02x %s:%02x echo %04x]\n",
0143               le32_to_cpu(p_ent->elem.hdr.cid),
0144               qed_get_ramrod_cmd_id_str(p_ent->elem.hdr.protocol_id,
0145                             p_ent->elem.hdr.cmd_id),
0146               p_ent->elem.hdr.cmd_id,
0147               qed_get_protocol_type_str(p_ent->elem.hdr.protocol_id),
0148                             p_ent->elem.hdr.protocol_id,
0149               le16_to_cpu(p_ent->elem.hdr.echo));
0150     qed_ptt_release(p_hwfn, p_ptt);
0151 
0152     return -EBUSY;
0153 }
0154 
0155 /***************************************************************************
0156  * SPQ entries inner API
0157  ***************************************************************************/
0158 static int qed_spq_fill_entry(struct qed_hwfn *p_hwfn,
0159                   struct qed_spq_entry *p_ent)
0160 {
0161     p_ent->flags = 0;
0162 
0163     switch (p_ent->comp_mode) {
0164     case QED_SPQ_MODE_EBLOCK:
0165     case QED_SPQ_MODE_BLOCK:
0166         p_ent->comp_cb.function = qed_spq_blocking_cb;
0167         break;
0168     case QED_SPQ_MODE_CB:
0169         break;
0170     default:
0171         DP_NOTICE(p_hwfn, "Unknown SPQE completion mode %d\n",
0172               p_ent->comp_mode);
0173         return -EINVAL;
0174     }
0175 
0176     DP_VERBOSE(p_hwfn,
0177            QED_MSG_SPQ,
0178            "Ramrod hdr: [CID 0x%08x %s:0x%02x %s:0x%02x] Data ptr: [%08x:%08x] Cmpltion Mode: %s\n",
0179            p_ent->elem.hdr.cid,
0180            qed_get_ramrod_cmd_id_str(p_ent->elem.hdr.protocol_id,
0181                          p_ent->elem.hdr.cmd_id),
0182            p_ent->elem.hdr.cmd_id,
0183            qed_get_protocol_type_str(p_ent->elem.hdr.protocol_id),
0184                          p_ent->elem.hdr.protocol_id,
0185            p_ent->elem.data_ptr.hi, p_ent->elem.data_ptr.lo,
0186            D_TRINE(p_ent->comp_mode, QED_SPQ_MODE_EBLOCK,
0187                QED_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
0188                "MODE_CB"));
0189 
0190     return 0;
0191 }
0192 
0193 /***************************************************************************
0194  * HSI access
0195  ***************************************************************************/
0196 static void qed_spq_hw_initialize(struct qed_hwfn *p_hwfn,
0197                   struct qed_spq *p_spq)
0198 {
0199     struct core_conn_context *p_cxt;
0200     struct qed_cxt_info cxt_info;
0201     u16 physical_q;
0202     int rc;
0203 
0204     cxt_info.iid = p_spq->cid;
0205 
0206     rc = qed_cxt_get_cid_info(p_hwfn, &cxt_info);
0207 
0208     if (rc < 0) {
0209         DP_NOTICE(p_hwfn, "Cannot find context info for cid=%d\n",
0210               p_spq->cid);
0211         return;
0212     }
0213 
0214     p_cxt = cxt_info.p_cxt;
0215 
0216     SET_FIELD(p_cxt->xstorm_ag_context.flags10,
0217           XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
0218     SET_FIELD(p_cxt->xstorm_ag_context.flags1,
0219           XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
0220     SET_FIELD(p_cxt->xstorm_ag_context.flags9,
0221           XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
0222 
0223     /* QM physical queue */
0224     physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
0225     p_cxt->xstorm_ag_context.physical_q0 = cpu_to_le16(physical_q);
0226 
0227     p_cxt->xstorm_st_context.spq_base_addr.lo =
0228         DMA_LO_LE(p_spq->chain.p_phys_addr);
0229     p_cxt->xstorm_st_context.spq_base_addr.hi =
0230         DMA_HI_LE(p_spq->chain.p_phys_addr);
0231 }
0232 
0233 static int qed_spq_hw_post(struct qed_hwfn *p_hwfn,
0234                struct qed_spq *p_spq, struct qed_spq_entry *p_ent)
0235 {
0236     struct qed_chain *p_chain = &p_hwfn->p_spq->chain;
0237     struct core_db_data *p_db_data = &p_spq->db_data;
0238     u16 echo = qed_chain_get_prod_idx(p_chain);
0239     struct slow_path_element    *elem;
0240 
0241     p_ent->elem.hdr.echo    = cpu_to_le16(echo);
0242     elem = qed_chain_produce(p_chain);
0243     if (!elem) {
0244         DP_NOTICE(p_hwfn, "Failed to produce from SPQ chain\n");
0245         return -EINVAL;
0246     }
0247 
0248     *elem = p_ent->elem; /* struct assignment */
0249 
0250     /* send a doorbell on the slow hwfn session */
0251     p_db_data->spq_prod = cpu_to_le16(qed_chain_get_prod_idx(p_chain));
0252 
0253     /* make sure the SPQE is updated before the doorbell */
0254     wmb();
0255 
0256     DOORBELL(p_hwfn, p_spq->db_addr_offset, *(u32 *)p_db_data);
0257 
0258     /* make sure doorbell is rang */
0259     wmb();
0260 
0261     DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
0262            "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
0263            p_spq->db_addr_offset,
0264            p_spq->cid,
0265            p_db_data->params,
0266            p_db_data->agg_flags, qed_chain_get_prod_idx(p_chain));
0267 
0268     return 0;
0269 }
0270 
0271 /***************************************************************************
0272  * Asynchronous events
0273  ***************************************************************************/
0274 static int
0275 qed_async_event_completion(struct qed_hwfn *p_hwfn,
0276                struct event_ring_entry *p_eqe)
0277 {
0278     qed_spq_async_comp_cb cb;
0279 
0280     if (!p_hwfn->p_spq)
0281         return -EINVAL;
0282 
0283     if (p_eqe->protocol_id >= MAX_PROTOCOL_TYPE) {
0284         DP_ERR(p_hwfn, "Wrong protocol: %s:%d\n",
0285                qed_get_protocol_type_str(p_eqe->protocol_id),
0286                p_eqe->protocol_id);
0287 
0288         return -EINVAL;
0289     }
0290 
0291     cb = p_hwfn->p_spq->async_comp_cb[p_eqe->protocol_id];
0292     if (cb) {
0293         return cb(p_hwfn, p_eqe->opcode, p_eqe->echo,
0294               &p_eqe->data, p_eqe->fw_return_code);
0295     } else {
0296         DP_NOTICE(p_hwfn,
0297               "Unknown Async completion for %s:%d\n",
0298               qed_get_protocol_type_str(p_eqe->protocol_id),
0299               p_eqe->protocol_id);
0300 
0301         return -EINVAL;
0302     }
0303 }
0304 
0305 int
0306 qed_spq_register_async_cb(struct qed_hwfn *p_hwfn,
0307               enum protocol_type protocol_id,
0308               qed_spq_async_comp_cb cb)
0309 {
0310     if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
0311         return -EINVAL;
0312 
0313     p_hwfn->p_spq->async_comp_cb[protocol_id] = cb;
0314     return 0;
0315 }
0316 
0317 void
0318 qed_spq_unregister_async_cb(struct qed_hwfn *p_hwfn,
0319                 enum protocol_type protocol_id)
0320 {
0321     if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
0322         return;
0323 
0324     p_hwfn->p_spq->async_comp_cb[protocol_id] = NULL;
0325 }
0326 
0327 /***************************************************************************
0328  * EQ API
0329  ***************************************************************************/
0330 void qed_eq_prod_update(struct qed_hwfn *p_hwfn, u16 prod)
0331 {
0332     u32 addr = GET_GTT_REG_ADDR(GTT_BAR0_MAP_REG_USDM_RAM,
0333                     USTORM_EQE_CONS, p_hwfn->rel_pf_id);
0334 
0335     REG_WR16(p_hwfn, addr, prod);
0336 }
0337 
0338 int qed_eq_completion(struct qed_hwfn *p_hwfn, void *cookie)
0339 {
0340     struct qed_eq *p_eq = cookie;
0341     struct qed_chain *p_chain = &p_eq->chain;
0342     int rc = 0;
0343 
0344     /* take a snapshot of the FW consumer */
0345     u16 fw_cons_idx = le16_to_cpu(*p_eq->p_fw_cons);
0346 
0347     DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
0348 
0349     /* Need to guarantee the fw_cons index we use points to a usuable
0350      * element (to comply with our chain), so our macros would comply
0351      */
0352     if ((fw_cons_idx & qed_chain_get_usable_per_page(p_chain)) ==
0353         qed_chain_get_usable_per_page(p_chain))
0354         fw_cons_idx += qed_chain_get_unusable_per_page(p_chain);
0355 
0356     /* Complete current segment of eq entries */
0357     while (fw_cons_idx != qed_chain_get_cons_idx(p_chain)) {
0358         struct event_ring_entry *p_eqe = qed_chain_consume(p_chain);
0359 
0360         if (!p_eqe) {
0361             rc = -EINVAL;
0362             break;
0363         }
0364 
0365         DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
0366                "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
0367                p_eqe->opcode,
0368                p_eqe->protocol_id,
0369                p_eqe->reserved0,
0370                le16_to_cpu(p_eqe->echo),
0371                p_eqe->fw_return_code,
0372                p_eqe->flags);
0373 
0374         if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
0375             if (qed_async_event_completion(p_hwfn, p_eqe))
0376                 rc = -EINVAL;
0377         } else if (qed_spq_completion(p_hwfn,
0378                           p_eqe->echo,
0379                           p_eqe->fw_return_code,
0380                           &p_eqe->data)) {
0381             rc = -EINVAL;
0382         }
0383 
0384         qed_chain_recycle_consumed(p_chain);
0385     }
0386 
0387     qed_eq_prod_update(p_hwfn, qed_chain_get_prod_idx(p_chain));
0388 
0389     /* Attempt to post pending requests */
0390     spin_lock_bh(&p_hwfn->p_spq->lock);
0391     rc = qed_spq_pend_post(p_hwfn);
0392     spin_unlock_bh(&p_hwfn->p_spq->lock);
0393 
0394     return rc;
0395 }
0396 
0397 int qed_eq_alloc(struct qed_hwfn *p_hwfn, u16 num_elem)
0398 {
0399     struct qed_chain_init_params params = {
0400         .mode       = QED_CHAIN_MODE_PBL,
0401         .intended_use   = QED_CHAIN_USE_TO_PRODUCE,
0402         .cnt_type   = QED_CHAIN_CNT_TYPE_U16,
0403         .num_elems  = num_elem,
0404         .elem_size  = sizeof(union event_ring_element),
0405     };
0406     struct qed_eq *p_eq;
0407     int ret;
0408 
0409     /* Allocate EQ struct */
0410     p_eq = kzalloc(sizeof(*p_eq), GFP_KERNEL);
0411     if (!p_eq)
0412         return -ENOMEM;
0413 
0414     ret = qed_chain_alloc(p_hwfn->cdev, &p_eq->chain, &params);
0415     if (ret) {
0416         DP_NOTICE(p_hwfn, "Failed to allocate EQ chain\n");
0417         goto eq_allocate_fail;
0418     }
0419 
0420     /* register EQ completion on the SP SB */
0421     qed_int_register_cb(p_hwfn, qed_eq_completion,
0422                 p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
0423 
0424     p_hwfn->p_eq = p_eq;
0425     return 0;
0426 
0427 eq_allocate_fail:
0428     kfree(p_eq);
0429 
0430     return ret;
0431 }
0432 
0433 void qed_eq_setup(struct qed_hwfn *p_hwfn)
0434 {
0435     qed_chain_reset(&p_hwfn->p_eq->chain);
0436 }
0437 
0438 void qed_eq_free(struct qed_hwfn *p_hwfn)
0439 {
0440     if (!p_hwfn->p_eq)
0441         return;
0442 
0443     qed_chain_free(p_hwfn->cdev, &p_hwfn->p_eq->chain);
0444 
0445     kfree(p_hwfn->p_eq);
0446     p_hwfn->p_eq = NULL;
0447 }
0448 
0449 /***************************************************************************
0450  * CQE API - manipulate EQ functionality
0451  ***************************************************************************/
0452 static int qed_cqe_completion(struct qed_hwfn *p_hwfn,
0453                   struct eth_slow_path_rx_cqe *cqe,
0454                   enum protocol_type protocol)
0455 {
0456     if (IS_VF(p_hwfn->cdev))
0457         return 0;
0458 
0459     /* @@@tmp - it's possible we'll eventually want to handle some
0460      * actual commands that can arrive here, but for now this is only
0461      * used to complete the ramrod using the echo value on the cqe
0462      */
0463     return qed_spq_completion(p_hwfn, cqe->echo, 0, NULL);
0464 }
0465 
0466 int qed_eth_cqe_completion(struct qed_hwfn *p_hwfn,
0467                struct eth_slow_path_rx_cqe *cqe)
0468 {
0469     int rc;
0470 
0471     rc = qed_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
0472     if (rc)
0473         DP_NOTICE(p_hwfn,
0474               "Failed to handle RXQ CQE [cmd 0x%02x]\n",
0475               cqe->ramrod_cmd_id);
0476 
0477     return rc;
0478 }
0479 
0480 /***************************************************************************
0481  * Slow hwfn Queue (spq)
0482  ***************************************************************************/
0483 void qed_spq_setup(struct qed_hwfn *p_hwfn)
0484 {
0485     struct qed_spq *p_spq = p_hwfn->p_spq;
0486     struct qed_spq_entry *p_virt = NULL;
0487     struct core_db_data *p_db_data;
0488     void __iomem *db_addr;
0489     dma_addr_t p_phys = 0;
0490     u32 i, capacity;
0491     int rc;
0492 
0493     INIT_LIST_HEAD(&p_spq->pending);
0494     INIT_LIST_HEAD(&p_spq->completion_pending);
0495     INIT_LIST_HEAD(&p_spq->free_pool);
0496     INIT_LIST_HEAD(&p_spq->unlimited_pending);
0497     spin_lock_init(&p_spq->lock);
0498 
0499     /* SPQ empty pool */
0500     p_phys  = p_spq->p_phys + offsetof(struct qed_spq_entry, ramrod);
0501     p_virt  = p_spq->p_virt;
0502 
0503     capacity = qed_chain_get_capacity(&p_spq->chain);
0504     for (i = 0; i < capacity; i++) {
0505         DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
0506 
0507         list_add_tail(&p_virt->list, &p_spq->free_pool);
0508 
0509         p_virt++;
0510         p_phys += sizeof(struct qed_spq_entry);
0511     }
0512 
0513     /* Statistics */
0514     p_spq->normal_count     = 0;
0515     p_spq->comp_count       = 0;
0516     p_spq->comp_sent_count      = 0;
0517     p_spq->unlimited_pending_count  = 0;
0518 
0519     bitmap_zero(p_spq->p_comp_bitmap, SPQ_RING_SIZE);
0520     p_spq->comp_bitmap_idx = 0;
0521 
0522     /* SPQ cid, cannot fail */
0523     qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
0524     qed_spq_hw_initialize(p_hwfn, p_spq);
0525 
0526     /* reset the chain itself */
0527     qed_chain_reset(&p_spq->chain);
0528 
0529     /* Initialize the address/data of the SPQ doorbell */
0530     p_spq->db_addr_offset = qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY);
0531     p_db_data = &p_spq->db_data;
0532     memset(p_db_data, 0, sizeof(*p_db_data));
0533     SET_FIELD(p_db_data->params, CORE_DB_DATA_DEST, DB_DEST_XCM);
0534     SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_MAX);
0535     SET_FIELD(p_db_data->params, CORE_DB_DATA_AGG_VAL_SEL,
0536           DQ_XCM_CORE_SPQ_PROD_CMD);
0537     p_db_data->agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
0538 
0539     /* Register the SPQ doorbell with the doorbell recovery mechanism */
0540     db_addr = (void __iomem *)((u8 __iomem *)p_hwfn->doorbells +
0541                    p_spq->db_addr_offset);
0542     rc = qed_db_recovery_add(p_hwfn->cdev, db_addr, &p_spq->db_data,
0543                  DB_REC_WIDTH_32B, DB_REC_KERNEL);
0544     if (rc)
0545         DP_INFO(p_hwfn,
0546             "Failed to register the SPQ doorbell with the doorbell recovery mechanism\n");
0547 }
0548 
0549 int qed_spq_alloc(struct qed_hwfn *p_hwfn)
0550 {
0551     struct qed_chain_init_params params = {
0552         .mode       = QED_CHAIN_MODE_SINGLE,
0553         .intended_use   = QED_CHAIN_USE_TO_PRODUCE,
0554         .cnt_type   = QED_CHAIN_CNT_TYPE_U16,
0555         .elem_size  = sizeof(struct slow_path_element),
0556     };
0557     struct qed_dev *cdev = p_hwfn->cdev;
0558     struct qed_spq_entry *p_virt = NULL;
0559     struct qed_spq *p_spq = NULL;
0560     dma_addr_t p_phys = 0;
0561     u32 capacity;
0562     int ret;
0563 
0564     /* SPQ struct */
0565     p_spq = kzalloc(sizeof(*p_spq), GFP_KERNEL);
0566     if (!p_spq)
0567         return -ENOMEM;
0568 
0569     /* SPQ ring */
0570     ret = qed_chain_alloc(cdev, &p_spq->chain, &params);
0571     if (ret) {
0572         DP_NOTICE(p_hwfn, "Failed to allocate SPQ chain\n");
0573         goto spq_chain_alloc_fail;
0574     }
0575 
0576     /* allocate and fill the SPQ elements (incl. ramrod data list) */
0577     capacity = qed_chain_get_capacity(&p_spq->chain);
0578     ret = -ENOMEM;
0579 
0580     p_virt = dma_alloc_coherent(&cdev->pdev->dev,
0581                     capacity * sizeof(struct qed_spq_entry),
0582                     &p_phys, GFP_KERNEL);
0583     if (!p_virt)
0584         goto spq_alloc_fail;
0585 
0586     p_spq->p_virt = p_virt;
0587     p_spq->p_phys = p_phys;
0588     p_hwfn->p_spq = p_spq;
0589 
0590     return 0;
0591 
0592 spq_alloc_fail:
0593     qed_chain_free(cdev, &p_spq->chain);
0594 spq_chain_alloc_fail:
0595     kfree(p_spq);
0596 
0597     return ret;
0598 }
0599 
0600 void qed_spq_free(struct qed_hwfn *p_hwfn)
0601 {
0602     struct qed_spq *p_spq = p_hwfn->p_spq;
0603     void __iomem *db_addr;
0604     u32 capacity;
0605 
0606     if (!p_spq)
0607         return;
0608 
0609     /* Delete the SPQ doorbell from the doorbell recovery mechanism */
0610     db_addr = (void __iomem *)((u8 __iomem *)p_hwfn->doorbells +
0611                    p_spq->db_addr_offset);
0612     qed_db_recovery_del(p_hwfn->cdev, db_addr, &p_spq->db_data);
0613 
0614     if (p_spq->p_virt) {
0615         capacity = qed_chain_get_capacity(&p_spq->chain);
0616         dma_free_coherent(&p_hwfn->cdev->pdev->dev,
0617                   capacity *
0618                   sizeof(struct qed_spq_entry),
0619                   p_spq->p_virt, p_spq->p_phys);
0620     }
0621 
0622     qed_chain_free(p_hwfn->cdev, &p_spq->chain);
0623     kfree(p_spq);
0624     p_hwfn->p_spq = NULL;
0625 }
0626 
0627 int qed_spq_get_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry **pp_ent)
0628 {
0629     struct qed_spq *p_spq = p_hwfn->p_spq;
0630     struct qed_spq_entry *p_ent = NULL;
0631     int rc = 0;
0632 
0633     spin_lock_bh(&p_spq->lock);
0634 
0635     if (list_empty(&p_spq->free_pool)) {
0636         p_ent = kzalloc(sizeof(*p_ent), GFP_ATOMIC);
0637         if (!p_ent) {
0638             DP_NOTICE(p_hwfn,
0639                   "Failed to allocate an SPQ entry for a pending ramrod\n");
0640             rc = -ENOMEM;
0641             goto out_unlock;
0642         }
0643         p_ent->queue = &p_spq->unlimited_pending;
0644     } else {
0645         p_ent = list_first_entry(&p_spq->free_pool,
0646                      struct qed_spq_entry, list);
0647         list_del(&p_ent->list);
0648         p_ent->queue = &p_spq->pending;
0649     }
0650 
0651     *pp_ent = p_ent;
0652 
0653 out_unlock:
0654     spin_unlock_bh(&p_spq->lock);
0655     return rc;
0656 }
0657 
0658 /* Locked variant; Should be called while the SPQ lock is taken */
0659 static void __qed_spq_return_entry(struct qed_hwfn *p_hwfn,
0660                    struct qed_spq_entry *p_ent)
0661 {
0662     list_add_tail(&p_ent->list, &p_hwfn->p_spq->free_pool);
0663 }
0664 
0665 void qed_spq_return_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry *p_ent)
0666 {
0667     spin_lock_bh(&p_hwfn->p_spq->lock);
0668     __qed_spq_return_entry(p_hwfn, p_ent);
0669     spin_unlock_bh(&p_hwfn->p_spq->lock);
0670 }
0671 
0672 /**
0673  * qed_spq_add_entry() - Add a new entry to the pending list.
0674  *                       Should be used while lock is being held.
0675  *
0676  * @p_hwfn: HW device data.
0677  * @p_ent: An entry to add.
0678  * @priority: Desired priority.
0679  *
0680  * Adds an entry to the pending list is there is room (an empty
0681  * element is available in the free_pool), or else places the
0682  * entry in the unlimited_pending pool.
0683  *
0684  * Return: zero on success, -EINVAL on invalid @priority.
0685  */
0686 static int qed_spq_add_entry(struct qed_hwfn *p_hwfn,
0687                  struct qed_spq_entry *p_ent,
0688                  enum spq_priority priority)
0689 {
0690     struct qed_spq *p_spq = p_hwfn->p_spq;
0691 
0692     if (p_ent->queue == &p_spq->unlimited_pending) {
0693         if (list_empty(&p_spq->free_pool)) {
0694             list_add_tail(&p_ent->list, &p_spq->unlimited_pending);
0695             p_spq->unlimited_pending_count++;
0696 
0697             return 0;
0698         } else {
0699             struct qed_spq_entry *p_en2;
0700 
0701             p_en2 = list_first_entry(&p_spq->free_pool,
0702                          struct qed_spq_entry, list);
0703             list_del(&p_en2->list);
0704 
0705             /* Copy the ring element physical pointer to the new
0706              * entry, since we are about to override the entire ring
0707              * entry and don't want to lose the pointer.
0708              */
0709             p_ent->elem.data_ptr = p_en2->elem.data_ptr;
0710 
0711             *p_en2 = *p_ent;
0712 
0713             /* EBLOCK responsible to free the allocated p_ent */
0714             if (p_ent->comp_mode != QED_SPQ_MODE_EBLOCK)
0715                 kfree(p_ent);
0716             else
0717                 p_ent->post_ent = p_en2;
0718 
0719             p_ent = p_en2;
0720         }
0721     }
0722 
0723     /* entry is to be placed in 'pending' queue */
0724     switch (priority) {
0725     case QED_SPQ_PRIORITY_NORMAL:
0726         list_add_tail(&p_ent->list, &p_spq->pending);
0727         p_spq->normal_count++;
0728         break;
0729     case QED_SPQ_PRIORITY_HIGH:
0730         list_add(&p_ent->list, &p_spq->pending);
0731         p_spq->high_count++;
0732         break;
0733     default:
0734         return -EINVAL;
0735     }
0736 
0737     return 0;
0738 }
0739 
0740 /***************************************************************************
0741  * Accessor
0742  ***************************************************************************/
0743 u32 qed_spq_get_cid(struct qed_hwfn *p_hwfn)
0744 {
0745     if (!p_hwfn->p_spq)
0746         return 0xffffffff;      /* illegal */
0747     return p_hwfn->p_spq->cid;
0748 }
0749 
0750 /***************************************************************************
0751  * Posting new Ramrods
0752  ***************************************************************************/
0753 static int qed_spq_post_list(struct qed_hwfn *p_hwfn,
0754                  struct list_head *head, u32 keep_reserve)
0755 {
0756     struct qed_spq *p_spq = p_hwfn->p_spq;
0757     int rc;
0758 
0759     while (qed_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
0760            !list_empty(head)) {
0761         struct qed_spq_entry *p_ent =
0762             list_first_entry(head, struct qed_spq_entry, list);
0763         list_move_tail(&p_ent->list, &p_spq->completion_pending);
0764         p_spq->comp_sent_count++;
0765 
0766         rc = qed_spq_hw_post(p_hwfn, p_spq, p_ent);
0767         if (rc) {
0768             list_del(&p_ent->list);
0769             __qed_spq_return_entry(p_hwfn, p_ent);
0770             return rc;
0771         }
0772     }
0773 
0774     return 0;
0775 }
0776 
0777 int qed_spq_pend_post(struct qed_hwfn *p_hwfn)
0778 {
0779     struct qed_spq *p_spq = p_hwfn->p_spq;
0780     struct qed_spq_entry *p_ent = NULL;
0781 
0782     while (!list_empty(&p_spq->free_pool)) {
0783         if (list_empty(&p_spq->unlimited_pending))
0784             break;
0785 
0786         p_ent = list_first_entry(&p_spq->unlimited_pending,
0787                      struct qed_spq_entry, list);
0788         if (!p_ent)
0789             return -EINVAL;
0790 
0791         list_del(&p_ent->list);
0792 
0793         qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
0794     }
0795 
0796     return qed_spq_post_list(p_hwfn, &p_spq->pending,
0797                  SPQ_HIGH_PRI_RESERVE_DEFAULT);
0798 }
0799 
0800 static void qed_spq_recov_set_ret_code(struct qed_spq_entry *p_ent,
0801                        u8 *fw_return_code)
0802 {
0803     if (!fw_return_code)
0804         return;
0805 
0806     if (p_ent->elem.hdr.protocol_id == PROTOCOLID_ROCE ||
0807         p_ent->elem.hdr.protocol_id == PROTOCOLID_IWARP)
0808         *fw_return_code = RDMA_RETURN_OK;
0809 }
0810 
0811 /* Avoid overriding of SPQ entries when getting out-of-order completions, by
0812  * marking the completions in a bitmap and increasing the chain consumer only
0813  * for the first successive completed entries.
0814  */
0815 static void qed_spq_comp_bmap_update(struct qed_hwfn *p_hwfn, __le16 echo)
0816 {
0817     u16 pos = le16_to_cpu(echo) % SPQ_RING_SIZE;
0818     struct qed_spq *p_spq = p_hwfn->p_spq;
0819 
0820     __set_bit(pos, p_spq->p_comp_bitmap);
0821     while (test_bit(p_spq->comp_bitmap_idx,
0822             p_spq->p_comp_bitmap)) {
0823         __clear_bit(p_spq->comp_bitmap_idx,
0824                 p_spq->p_comp_bitmap);
0825         p_spq->comp_bitmap_idx++;
0826         qed_chain_return_produced(&p_spq->chain);
0827     }
0828 }
0829 
0830 int qed_spq_post(struct qed_hwfn *p_hwfn,
0831          struct qed_spq_entry *p_ent, u8 *fw_return_code)
0832 {
0833     int rc = 0;
0834     struct qed_spq *p_spq = p_hwfn ? p_hwfn->p_spq : NULL;
0835     bool b_ret_ent = true;
0836     bool eblock;
0837 
0838     if (!p_hwfn)
0839         return -EINVAL;
0840 
0841     if (!p_ent) {
0842         DP_NOTICE(p_hwfn, "Got a NULL pointer\n");
0843         return -EINVAL;
0844     }
0845 
0846     if (p_hwfn->cdev->recov_in_prog) {
0847         DP_VERBOSE(p_hwfn,
0848                QED_MSG_SPQ,
0849                "Recovery is in progress. Skip spq post [%s:%02x %s:%02x]\n",
0850                qed_get_ramrod_cmd_id_str(p_ent->elem.hdr.protocol_id,
0851                              p_ent->elem.hdr.cmd_id),
0852                p_ent->elem.hdr.cmd_id,
0853                qed_get_protocol_type_str(p_ent->elem.hdr.protocol_id),
0854                p_ent->elem.hdr.protocol_id);
0855 
0856         /* Let the flow complete w/o any error handling */
0857         qed_spq_recov_set_ret_code(p_ent, fw_return_code);
0858         return 0;
0859     }
0860 
0861     /* Complete the entry */
0862     rc = qed_spq_fill_entry(p_hwfn, p_ent);
0863 
0864     spin_lock_bh(&p_spq->lock);
0865 
0866     /* Check return value after LOCK is taken for cleaner error flow */
0867     if (rc)
0868         goto spq_post_fail;
0869 
0870     /* Check if entry is in block mode before qed_spq_add_entry,
0871      * which might kfree p_ent.
0872      */
0873     eblock = (p_ent->comp_mode == QED_SPQ_MODE_EBLOCK);
0874 
0875     /* Add the request to the pending queue */
0876     rc = qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
0877     if (rc)
0878         goto spq_post_fail;
0879 
0880     rc = qed_spq_pend_post(p_hwfn);
0881     if (rc) {
0882         /* Since it's possible that pending failed for a different
0883          * entry [although unlikely], the failed entry was already
0884          * dealt with; No need to return it here.
0885          */
0886         b_ret_ent = false;
0887         goto spq_post_fail;
0888     }
0889 
0890     spin_unlock_bh(&p_spq->lock);
0891 
0892     if (eblock) {
0893         /* For entries in QED BLOCK mode, the completion code cannot
0894          * perform the necessary cleanup - if it did, we couldn't
0895          * access p_ent here to see whether it's successful or not.
0896          * Thus, after gaining the answer perform the cleanup here.
0897          */
0898         rc = qed_spq_block(p_hwfn, p_ent, fw_return_code,
0899                    p_ent->queue == &p_spq->unlimited_pending);
0900 
0901         if (p_ent->queue == &p_spq->unlimited_pending) {
0902             struct qed_spq_entry *p_post_ent = p_ent->post_ent;
0903 
0904             kfree(p_ent);
0905 
0906             /* Return the entry which was actually posted */
0907             p_ent = p_post_ent;
0908         }
0909 
0910         if (rc)
0911             goto spq_post_fail2;
0912 
0913         /* return to pool */
0914         qed_spq_return_entry(p_hwfn, p_ent);
0915     }
0916     return rc;
0917 
0918 spq_post_fail2:
0919     spin_lock_bh(&p_spq->lock);
0920     list_del(&p_ent->list);
0921     qed_spq_comp_bmap_update(p_hwfn, p_ent->elem.hdr.echo);
0922 
0923 spq_post_fail:
0924     /* return to the free pool */
0925     if (b_ret_ent)
0926         __qed_spq_return_entry(p_hwfn, p_ent);
0927     spin_unlock_bh(&p_spq->lock);
0928 
0929     return rc;
0930 }
0931 
0932 int qed_spq_completion(struct qed_hwfn *p_hwfn,
0933                __le16 echo,
0934                u8 fw_return_code,
0935                union event_ring_data *p_data)
0936 {
0937     struct qed_spq      *p_spq;
0938     struct qed_spq_entry    *p_ent = NULL;
0939     struct qed_spq_entry    *tmp;
0940     struct qed_spq_entry    *found = NULL;
0941 
0942     if (!p_hwfn)
0943         return -EINVAL;
0944 
0945     p_spq = p_hwfn->p_spq;
0946     if (!p_spq)
0947         return -EINVAL;
0948 
0949     spin_lock_bh(&p_spq->lock);
0950     list_for_each_entry_safe(p_ent, tmp, &p_spq->completion_pending, list) {
0951         if (p_ent->elem.hdr.echo == echo) {
0952             list_del(&p_ent->list);
0953             qed_spq_comp_bmap_update(p_hwfn, echo);
0954             p_spq->comp_count++;
0955             found = p_ent;
0956             break;
0957         }
0958 
0959         /* This is relatively uncommon - depends on scenarios
0960          * which have mutliple per-PF sent ramrods.
0961          */
0962         DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
0963                "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
0964                le16_to_cpu(echo),
0965                le16_to_cpu(p_ent->elem.hdr.echo));
0966     }
0967 
0968     /* Release lock before callback, as callback may post
0969      * an additional ramrod.
0970      */
0971     spin_unlock_bh(&p_spq->lock);
0972 
0973     if (!found) {
0974         DP_NOTICE(p_hwfn,
0975               "Failed to find an entry this EQE [echo %04x] completes\n",
0976               le16_to_cpu(echo));
0977         return -EEXIST;
0978     }
0979 
0980     DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
0981            "Complete EQE [echo %04x]: func %p cookie %p)\n",
0982            le16_to_cpu(echo),
0983            p_ent->comp_cb.function, p_ent->comp_cb.cookie);
0984     if (found->comp_cb.function)
0985         found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
0986                     fw_return_code);
0987     else
0988         DP_VERBOSE(p_hwfn,
0989                QED_MSG_SPQ,
0990                "Got a completion without a callback function\n");
0991 
0992     if (found->comp_mode != QED_SPQ_MODE_EBLOCK)
0993         /* EBLOCK  is responsible for returning its own entry into the
0994          * free list.
0995          */
0996         qed_spq_return_entry(p_hwfn, found);
0997 
0998     return 0;
0999 }
1000 
1001 #define QED_SPQ_CONSQ_ELEM_SIZE     0x80
1002 
1003 int qed_consq_alloc(struct qed_hwfn *p_hwfn)
1004 {
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,
1011     };
1012     struct qed_consq *p_consq;
1013     int ret;
1014 
1015     /* Allocate ConsQ struct */
1016     p_consq = kzalloc(sizeof(*p_consq), GFP_KERNEL);
1017     if (!p_consq)
1018         return -ENOMEM;
1019 
1020     /* Allocate and initialize ConsQ chain */
1021     ret = qed_chain_alloc(p_hwfn->cdev, &p_consq->chain, &params);
1022     if (ret) {
1023         DP_NOTICE(p_hwfn, "Failed to allocate ConsQ chain");
1024         goto consq_alloc_fail;
1025     }
1026 
1027     p_hwfn->p_consq = p_consq;
1028 
1029     return 0;
1030 
1031 consq_alloc_fail:
1032     kfree(p_consq);
1033 
1034     return ret;
1035 }
1036 
1037 void qed_consq_setup(struct qed_hwfn *p_hwfn)
1038 {
1039     qed_chain_reset(&p_hwfn->p_consq->chain);
1040 }
1041 
1042 void qed_consq_free(struct qed_hwfn *p_hwfn)
1043 {
1044     if (!p_hwfn->p_consq)
1045         return;
1046 
1047     qed_chain_free(p_hwfn->cdev, &p_hwfn->p_consq->chain);
1048 
1049     kfree(p_hwfn->p_consq);
1050     p_hwfn->p_consq = NULL;
1051 }