LCOV - code coverage report
Current view: top level - lib/nvme - nvme_rdma.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 682 1556 43.8 %
Date: 2024-12-01 18:55:50 Functions: 47 93 50.5 %

          Line data    Source code
       1             : /*   SPDX-License-Identifier: BSD-3-Clause
       2             :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3             :  *   Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved.
       4             :  *   Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5             :  */
       6             : 
       7             : /*
       8             :  * NVMe over RDMA transport
       9             :  */
      10             : 
      11             : #include "spdk/stdinc.h"
      12             : 
      13             : #include "spdk/assert.h"
      14             : #include "spdk/dma.h"
      15             : #include "spdk/log.h"
      16             : #include "spdk/trace.h"
      17             : #include "spdk/queue.h"
      18             : #include "spdk/nvme.h"
      19             : #include "spdk/nvmf_spec.h"
      20             : #include "spdk/string.h"
      21             : #include "spdk/endian.h"
      22             : #include "spdk/likely.h"
      23             : #include "spdk/config.h"
      24             : 
      25             : #include "nvme_internal.h"
      26             : #include "spdk_internal/rdma_provider.h"
      27             : #include "spdk_internal/rdma_utils.h"
      28             : 
      29             : #define NVME_RDMA_TIME_OUT_IN_MS 2000
      30             : #define NVME_RDMA_RW_BUFFER_SIZE 131072
      31             : 
      32             : /*
      33             :  * NVME RDMA qpair Resource Defaults
      34             :  */
      35             : #define NVME_RDMA_DEFAULT_TX_SGE                2
      36             : #define NVME_RDMA_DEFAULT_RX_SGE                1
      37             : 
      38             : /* Max number of NVMe-oF SGL descriptors supported by the host */
      39             : #define NVME_RDMA_MAX_SGL_DESCRIPTORS           16
      40             : 
      41             : /* number of STAILQ entries for holding pending RDMA CM events. */
      42             : #define NVME_RDMA_NUM_CM_EVENTS                 256
      43             : 
      44             : /* The default size for a shared rdma completion queue. */
      45             : #define DEFAULT_NVME_RDMA_CQ_SIZE               4096
      46             : 
      47             : /*
      48             :  * In the special case of a stale connection we don't expose a mechanism
      49             :  * for the user to retry the connection so we need to handle it internally.
      50             :  */
      51             : #define NVME_RDMA_STALE_CONN_RETRY_MAX          5
      52             : #define NVME_RDMA_STALE_CONN_RETRY_DELAY_US     10000
      53             : 
      54             : /*
      55             :  * Maximum value of transport_retry_count used by RDMA controller
      56             :  */
      57             : #define NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT       7
      58             : 
      59             : /*
      60             :  * Maximum value of transport_ack_timeout used by RDMA controller
      61             :  */
      62             : #define NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT       31
      63             : 
      64             : /*
      65             :  * Number of microseconds to wait until the lingering qpair becomes quiet.
      66             :  */
      67             : #define NVME_RDMA_DISCONNECTED_QPAIR_TIMEOUT_US 1000000ull
      68             : 
      69             : /*
      70             :  * The max length of keyed SGL data block (3 bytes)
      71             :  */
      72             : #define NVME_RDMA_MAX_KEYED_SGL_LENGTH ((1u << 24u) - 1)
      73             : 
      74             : #define WC_PER_QPAIR(queue_depth)       (queue_depth * 2)
      75             : 
      76             : #define NVME_RDMA_POLL_GROUP_CHECK_QPN(_rqpair, qpn)                            \
      77             :         ((_rqpair)->rdma_qp && (_rqpair)->rdma_qp->qp->qp_num == (qpn))     \
      78             : 
      79             : enum nvme_rdma_wr_type {
      80             :         RDMA_WR_TYPE_RECV,
      81             :         RDMA_WR_TYPE_SEND,
      82             : };
      83             : 
      84             : struct nvme_rdma_wr {
      85             :         /* Using this instead of the enum allows this struct to only occupy one byte. */
      86             :         uint8_t type;
      87             : };
      88             : 
      89             : struct spdk_nvmf_cmd {
      90             :         struct spdk_nvme_cmd cmd;
      91             :         struct spdk_nvme_sgl_descriptor sgl[NVME_RDMA_MAX_SGL_DESCRIPTORS];
      92             : };
      93             : 
      94             : struct spdk_nvme_rdma_hooks g_nvme_hooks = {};
      95             : 
      96             : /* STAILQ wrapper for cm events. */
      97             : struct nvme_rdma_cm_event_entry {
      98             :         struct rdma_cm_event                    *evt;
      99             :         STAILQ_ENTRY(nvme_rdma_cm_event_entry)  link;
     100             : };
     101             : 
     102             : /* NVMe RDMA transport extensions for spdk_nvme_ctrlr */
     103             : struct nvme_rdma_ctrlr {
     104             :         struct spdk_nvme_ctrlr                  ctrlr;
     105             : 
     106             :         uint16_t                                max_sge;
     107             : 
     108             :         struct rdma_event_channel               *cm_channel;
     109             : 
     110             :         STAILQ_HEAD(, nvme_rdma_cm_event_entry) pending_cm_events;
     111             : 
     112             :         STAILQ_HEAD(, nvme_rdma_cm_event_entry) free_cm_events;
     113             : 
     114             :         struct nvme_rdma_cm_event_entry         *cm_events;
     115             : };
     116             : 
     117             : struct nvme_rdma_poller_stats {
     118             :         uint64_t polls;
     119             :         uint64_t idle_polls;
     120             :         uint64_t queued_requests;
     121             :         uint64_t completions;
     122             :         struct spdk_rdma_provider_qp_stats rdma_stats;
     123             : };
     124             : 
     125             : struct nvme_rdma_poll_group;
     126             : struct nvme_rdma_rsps;
     127             : 
     128             : struct nvme_rdma_poller {
     129             :         struct ibv_context              *device;
     130             :         struct ibv_cq                   *cq;
     131             :         struct spdk_rdma_provider_srq   *srq;
     132             :         struct nvme_rdma_rsps           *rsps;
     133             :         struct ibv_pd                   *pd;
     134             :         struct spdk_rdma_utils_mem_map  *mr_map;
     135             :         uint32_t                        refcnt;
     136             :         int                             required_num_wc;
     137             :         int                             current_num_wc;
     138             :         struct nvme_rdma_poller_stats   stats;
     139             :         struct nvme_rdma_poll_group     *group;
     140             :         STAILQ_ENTRY(nvme_rdma_poller)  link;
     141             : };
     142             : 
     143             : struct nvme_rdma_qpair;
     144             : 
     145             : struct nvme_rdma_poll_group {
     146             :         struct spdk_nvme_transport_poll_group           group;
     147             :         STAILQ_HEAD(, nvme_rdma_poller)                 pollers;
     148             :         uint32_t                                        num_pollers;
     149             :         TAILQ_HEAD(, nvme_rdma_qpair)                   connecting_qpairs;
     150             :         TAILQ_HEAD(, nvme_rdma_qpair)                   active_qpairs;
     151             : };
     152             : 
     153             : enum nvme_rdma_qpair_state {
     154             :         NVME_RDMA_QPAIR_STATE_INVALID = 0,
     155             :         NVME_RDMA_QPAIR_STATE_STALE_CONN,
     156             :         NVME_RDMA_QPAIR_STATE_INITIALIZING,
     157             :         NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_SEND,
     158             :         NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_POLL,
     159             :         NVME_RDMA_QPAIR_STATE_AUTHENTICATING,
     160             :         NVME_RDMA_QPAIR_STATE_RUNNING,
     161             :         NVME_RDMA_QPAIR_STATE_EXITING,
     162             :         NVME_RDMA_QPAIR_STATE_LINGERING,
     163             :         NVME_RDMA_QPAIR_STATE_EXITED,
     164             : };
     165             : 
     166             : typedef int (*nvme_rdma_cm_event_cb)(struct nvme_rdma_qpair *rqpair, int ret);
     167             : 
     168             : struct nvme_rdma_rsp_opts {
     169             :         uint16_t                                num_entries;
     170             :         struct nvme_rdma_qpair                  *rqpair;
     171             :         struct spdk_rdma_provider_srq           *srq;
     172             :         struct spdk_rdma_utils_mem_map          *mr_map;
     173             : };
     174             : 
     175             : struct nvme_rdma_rsps {
     176             :         /* Parallel arrays of response buffers + response SGLs of size num_entries */
     177             :         struct ibv_sge                          *rsp_sgls;
     178             :         struct spdk_nvme_rdma_rsp               *rsps;
     179             : 
     180             :         struct ibv_recv_wr                      *rsp_recv_wrs;
     181             : 
     182             :         /* Count of outstanding recv objects */
     183             :         uint16_t                                current_num_recvs;
     184             : 
     185             :         uint16_t                                num_entries;
     186             : };
     187             : 
     188             : /* NVMe RDMA qpair extensions for spdk_nvme_qpair */
     189             : struct nvme_rdma_qpair {
     190             :         struct spdk_nvme_qpair                  qpair;
     191             : 
     192             :         struct spdk_rdma_provider_qp            *rdma_qp;
     193             :         struct rdma_cm_id                       *cm_id;
     194             :         struct ibv_cq                           *cq;
     195             :         struct spdk_rdma_provider_srq           *srq;
     196             : 
     197             :         struct  spdk_nvme_rdma_req              *rdma_reqs;
     198             : 
     199             :         uint32_t                                max_send_sge;
     200             : 
     201             :         uint16_t                                num_entries;
     202             : 
     203             :         bool                                    delay_cmd_submit;
     204             : 
     205             :         uint32_t                                num_completions;
     206             :         uint32_t                                num_outstanding_reqs;
     207             : 
     208             :         struct nvme_rdma_rsps                   *rsps;
     209             : 
     210             :         /*
     211             :          * Array of num_entries NVMe commands registered as RDMA message buffers.
     212             :          * Indexed by rdma_req->id.
     213             :          */
     214             :         struct spdk_nvmf_cmd                    *cmds;
     215             : 
     216             :         struct spdk_rdma_utils_mem_map          *mr_map;
     217             : 
     218             :         TAILQ_HEAD(, spdk_nvme_rdma_req)        free_reqs;
     219             :         TAILQ_HEAD(, spdk_nvme_rdma_req)        outstanding_reqs;
     220             : 
     221             :         /* Count of outstanding send objects */
     222             :         uint16_t                                current_num_sends;
     223             : 
     224             :         TAILQ_ENTRY(nvme_rdma_qpair)            link_active;
     225             : 
     226             :         /* Placed at the end of the struct since it is not used frequently */
     227             :         struct rdma_cm_event                    *evt;
     228             :         struct nvme_rdma_poller                 *poller;
     229             : 
     230             :         uint64_t                                evt_timeout_ticks;
     231             :         nvme_rdma_cm_event_cb                   evt_cb;
     232             :         enum rdma_cm_event_type                 expected_evt_type;
     233             : 
     234             :         enum nvme_rdma_qpair_state              state;
     235             : 
     236             :         bool                                    in_connect_poll;
     237             : 
     238             :         uint8_t                                 stale_conn_retry_count;
     239             :         bool                                    need_destroy;
     240             : 
     241             :         TAILQ_ENTRY(nvme_rdma_qpair)            link_connecting;
     242             : };
     243             : 
     244             : enum NVME_RDMA_COMPLETION_FLAGS {
     245             :         NVME_RDMA_SEND_COMPLETED = 1u << 0,
     246             :         NVME_RDMA_RECV_COMPLETED = 1u << 1,
     247             : };
     248             : 
     249             : struct spdk_nvme_rdma_req {
     250             :         uint16_t                                id;
     251             :         uint16_t                                completion_flags: 2;
     252             :         uint16_t                                reserved: 14;
     253             :         /* if completion of RDMA_RECV received before RDMA_SEND, we will complete nvme request
     254             :          * during processing of RDMA_SEND. To complete the request we must know the response
     255             :          * received in RDMA_RECV, so store it in this field */
     256             :         struct spdk_nvme_rdma_rsp               *rdma_rsp;
     257             : 
     258             :         struct nvme_rdma_wr                     rdma_wr;
     259             : 
     260             :         struct ibv_send_wr                      send_wr;
     261             : 
     262             :         struct nvme_request                     *req;
     263             : 
     264             :         struct ibv_sge                          send_sgl[NVME_RDMA_DEFAULT_TX_SGE];
     265             : 
     266             :         TAILQ_ENTRY(spdk_nvme_rdma_req)         link;
     267             : };
     268             : 
     269             : struct spdk_nvme_rdma_rsp {
     270             :         struct spdk_nvme_cpl    cpl;
     271             :         struct nvme_rdma_qpair  *rqpair;
     272             :         struct ibv_recv_wr      *recv_wr;
     273             :         struct nvme_rdma_wr     rdma_wr;
     274             : };
     275             : 
     276             : struct nvme_rdma_memory_translation_ctx {
     277             :         void *addr;
     278             :         size_t length;
     279             :         uint32_t lkey;
     280             :         uint32_t rkey;
     281             : };
     282             : 
     283             : static const char *rdma_cm_event_str[] = {
     284             :         "RDMA_CM_EVENT_ADDR_RESOLVED",
     285             :         "RDMA_CM_EVENT_ADDR_ERROR",
     286             :         "RDMA_CM_EVENT_ROUTE_RESOLVED",
     287             :         "RDMA_CM_EVENT_ROUTE_ERROR",
     288             :         "RDMA_CM_EVENT_CONNECT_REQUEST",
     289             :         "RDMA_CM_EVENT_CONNECT_RESPONSE",
     290             :         "RDMA_CM_EVENT_CONNECT_ERROR",
     291             :         "RDMA_CM_EVENT_UNREACHABLE",
     292             :         "RDMA_CM_EVENT_REJECTED",
     293             :         "RDMA_CM_EVENT_ESTABLISHED",
     294             :         "RDMA_CM_EVENT_DISCONNECTED",
     295             :         "RDMA_CM_EVENT_DEVICE_REMOVAL",
     296             :         "RDMA_CM_EVENT_MULTICAST_JOIN",
     297             :         "RDMA_CM_EVENT_MULTICAST_ERROR",
     298             :         "RDMA_CM_EVENT_ADDR_CHANGE",
     299             :         "RDMA_CM_EVENT_TIMEWAIT_EXIT"
     300             : };
     301             : 
     302             : static struct nvme_rdma_poller *nvme_rdma_poll_group_get_poller(struct nvme_rdma_poll_group *group,
     303             :                 struct ibv_context *device);
     304             : static void nvme_rdma_poll_group_put_poller(struct nvme_rdma_poll_group *group,
     305             :                 struct nvme_rdma_poller *poller);
     306             : 
     307             : static int nvme_rdma_ctrlr_delete_io_qpair(struct spdk_nvme_ctrlr *ctrlr,
     308             :                 struct spdk_nvme_qpair *qpair);
     309             : 
     310             : static inline struct nvme_rdma_qpair *
     311          18 : nvme_rdma_qpair(struct spdk_nvme_qpair *qpair)
     312             : {
     313          18 :         assert(qpair->trtype == SPDK_NVME_TRANSPORT_RDMA);
     314          18 :         return SPDK_CONTAINEROF(qpair, struct nvme_rdma_qpair, qpair);
     315             : }
     316             : 
     317             : static inline struct nvme_rdma_poll_group *
     318           8 : nvme_rdma_poll_group(struct spdk_nvme_transport_poll_group *group)
     319             : {
     320           8 :         return (SPDK_CONTAINEROF(group, struct nvme_rdma_poll_group, group));
     321             : }
     322             : 
     323             : static inline struct nvme_rdma_ctrlr *
     324           8 : nvme_rdma_ctrlr(struct spdk_nvme_ctrlr *ctrlr)
     325             : {
     326           8 :         assert(ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_RDMA);
     327           8 :         return SPDK_CONTAINEROF(ctrlr, struct nvme_rdma_ctrlr, ctrlr);
     328             : }
     329             : 
     330             : static inline struct spdk_nvme_rdma_req *
     331           3 : nvme_rdma_req_get(struct nvme_rdma_qpair *rqpair)
     332             : {
     333             :         struct spdk_nvme_rdma_req *rdma_req;
     334             : 
     335           3 :         rdma_req = TAILQ_FIRST(&rqpair->free_reqs);
     336           3 :         if (spdk_likely(rdma_req)) {
     337           2 :                 TAILQ_REMOVE(&rqpair->free_reqs, rdma_req, link);
     338             :         }
     339             : 
     340           3 :         return rdma_req;
     341             : }
     342             : 
     343             : static inline void
     344           1 : nvme_rdma_req_put(struct nvme_rdma_qpair *rqpair, struct spdk_nvme_rdma_req *rdma_req)
     345             : {
     346           1 :         rdma_req->completion_flags = 0;
     347           1 :         rdma_req->req = NULL;
     348           1 :         rdma_req->rdma_rsp = NULL;
     349           1 :         TAILQ_INSERT_HEAD(&rqpair->free_reqs, rdma_req, link);
     350           1 : }
     351             : 
     352             : static void
     353           0 : nvme_rdma_req_complete(struct spdk_nvme_rdma_req *rdma_req,
     354             :                        struct spdk_nvme_cpl *rsp,
     355             :                        bool print_on_error)
     356             : {
     357           0 :         struct nvme_request *req = rdma_req->req;
     358             :         struct nvme_rdma_qpair *rqpair;
     359             :         struct spdk_nvme_qpair *qpair;
     360             :         bool error, print_error;
     361             : 
     362           0 :         assert(req != NULL);
     363             : 
     364           0 :         qpair = req->qpair;
     365           0 :         rqpair = nvme_rdma_qpair(qpair);
     366             : 
     367           0 :         error = spdk_nvme_cpl_is_error(rsp);
     368           0 :         print_error = error && print_on_error && !qpair->ctrlr->opts.disable_error_logging;
     369             : 
     370           0 :         if (print_error) {
     371           0 :                 spdk_nvme_qpair_print_command(qpair, &req->cmd);
     372             :         }
     373             : 
     374           0 :         if (print_error || SPDK_DEBUGLOG_FLAG_ENABLED("nvme")) {
     375           0 :                 spdk_nvme_qpair_print_completion(qpair, rsp);
     376             :         }
     377             : 
     378           0 :         assert(rqpair->num_outstanding_reqs > 0);
     379           0 :         rqpair->num_outstanding_reqs--;
     380             : 
     381           0 :         TAILQ_REMOVE(&rqpair->outstanding_reqs, rdma_req, link);
     382             : 
     383           0 :         nvme_complete_request(req->cb_fn, req->cb_arg, qpair, req, rsp);
     384           0 :         nvme_rdma_req_put(rqpair, rdma_req);
     385           0 : }
     386             : 
     387             : static const char *
     388           4 : nvme_rdma_cm_event_str_get(uint32_t event)
     389             : {
     390           4 :         if (event < SPDK_COUNTOF(rdma_cm_event_str)) {
     391           4 :                 return rdma_cm_event_str[event];
     392             :         } else {
     393           0 :                 return "Undefined";
     394             :         }
     395             : }
     396             : 
     397             : 
     398             : static int
     399          12 : nvme_rdma_qpair_process_cm_event(struct nvme_rdma_qpair *rqpair)
     400             : {
     401          12 :         struct rdma_cm_event                            *event = rqpair->evt;
     402             :         struct spdk_nvmf_rdma_accept_private_data       *accept_data;
     403          12 :         int                                             rc = 0;
     404             : 
     405          12 :         if (event) {
     406          12 :                 switch (event->event) {
     407           1 :                 case RDMA_CM_EVENT_ADDR_RESOLVED:
     408             :                 case RDMA_CM_EVENT_ADDR_ERROR:
     409             :                 case RDMA_CM_EVENT_ROUTE_RESOLVED:
     410             :                 case RDMA_CM_EVENT_ROUTE_ERROR:
     411           1 :                         break;
     412           1 :                 case RDMA_CM_EVENT_CONNECT_REQUEST:
     413           1 :                         break;
     414           1 :                 case RDMA_CM_EVENT_CONNECT_ERROR:
     415           1 :                         break;
     416           1 :                 case RDMA_CM_EVENT_UNREACHABLE:
     417             :                 case RDMA_CM_EVENT_REJECTED:
     418           1 :                         break;
     419           2 :                 case RDMA_CM_EVENT_CONNECT_RESPONSE:
     420           2 :                         rc = spdk_rdma_provider_qp_complete_connect(rqpair->rdma_qp);
     421             :                 /* fall through */
     422           2 :                 case RDMA_CM_EVENT_ESTABLISHED:
     423           2 :                         accept_data = (struct spdk_nvmf_rdma_accept_private_data *)event->param.conn.private_data;
     424           2 :                         if (accept_data == NULL) {
     425           1 :                                 rc = -1;
     426             :                         } else {
     427           1 :                                 SPDK_DEBUGLOG(nvme, "Requested queue depth %d. Target receive queue depth %d.\n",
     428             :                                               rqpair->num_entries + 1, accept_data->crqsize);
     429             :                         }
     430           2 :                         break;
     431           1 :                 case RDMA_CM_EVENT_DISCONNECTED:
     432           1 :                         rqpair->qpair.transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_REMOTE;
     433           1 :                         break;
     434           1 :                 case RDMA_CM_EVENT_DEVICE_REMOVAL:
     435           1 :                         rqpair->qpair.transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_LOCAL;
     436           1 :                         rqpair->need_destroy = true;
     437           1 :                         break;
     438           1 :                 case RDMA_CM_EVENT_MULTICAST_JOIN:
     439             :                 case RDMA_CM_EVENT_MULTICAST_ERROR:
     440           1 :                         break;
     441           1 :                 case RDMA_CM_EVENT_ADDR_CHANGE:
     442           1 :                         rqpair->qpair.transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_LOCAL;
     443           1 :                         break;
     444           1 :                 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
     445           1 :                         break;
     446           1 :                 default:
     447           1 :                         SPDK_ERRLOG("Unexpected Acceptor Event [%d]\n", event->event);
     448           1 :                         break;
     449             :                 }
     450          12 :                 rqpair->evt = NULL;
     451          12 :                 rdma_ack_cm_event(event);
     452             :         }
     453             : 
     454          12 :         return rc;
     455             : }
     456             : 
     457             : /*
     458             :  * This function must be called under the nvme controller's lock
     459             :  * because it touches global controller variables. The lock is taken
     460             :  * by the generic transport code before invoking a few of the functions
     461             :  * in this file: nvme_rdma_ctrlr_connect_qpair, nvme_rdma_ctrlr_delete_io_qpair,
     462             :  * and conditionally nvme_rdma_qpair_process_completions when it is calling
     463             :  * completions on the admin qpair. When adding a new call to this function, please
     464             :  * verify that it is in a situation where it falls under the lock.
     465             :  */
     466             : static int
     467           0 : nvme_rdma_poll_events(struct nvme_rdma_ctrlr *rctrlr)
     468             : {
     469             :         struct nvme_rdma_cm_event_entry *entry, *tmp;
     470             :         struct nvme_rdma_qpair          *event_qpair;
     471           0 :         struct rdma_cm_event            *event;
     472           0 :         struct rdma_event_channel       *channel = rctrlr->cm_channel;
     473             : 
     474           0 :         STAILQ_FOREACH_SAFE(entry, &rctrlr->pending_cm_events, link, tmp) {
     475           0 :                 event_qpair = entry->evt->id->context;
     476           0 :                 if (event_qpair->evt == NULL) {
     477           0 :                         event_qpair->evt = entry->evt;
     478           0 :                         STAILQ_REMOVE(&rctrlr->pending_cm_events, entry, nvme_rdma_cm_event_entry, link);
     479           0 :                         STAILQ_INSERT_HEAD(&rctrlr->free_cm_events, entry, link);
     480             :                 }
     481             :         }
     482             : 
     483           0 :         while (rdma_get_cm_event(channel, &event) == 0) {
     484           0 :                 event_qpair = event->id->context;
     485           0 :                 if (event_qpair->evt == NULL) {
     486           0 :                         event_qpair->evt = event;
     487             :                 } else {
     488           0 :                         assert(rctrlr == nvme_rdma_ctrlr(event_qpair->qpair.ctrlr));
     489           0 :                         entry = STAILQ_FIRST(&rctrlr->free_cm_events);
     490           0 :                         if (entry == NULL) {
     491           0 :                                 rdma_ack_cm_event(event);
     492           0 :                                 return -ENOMEM;
     493             :                         }
     494           0 :                         STAILQ_REMOVE(&rctrlr->free_cm_events, entry, nvme_rdma_cm_event_entry, link);
     495           0 :                         entry->evt = event;
     496           0 :                         STAILQ_INSERT_TAIL(&rctrlr->pending_cm_events, entry, link);
     497             :                 }
     498             :         }
     499             : 
     500             :         /* rdma_get_cm_event() returns -1 on error. If an error occurs, errno
     501             :          * will be set to indicate the failure reason. So return negated errno here.
     502             :          */
     503           0 :         return -errno;
     504             : }
     505             : 
     506             : static int
     507           4 : nvme_rdma_validate_cm_event(enum rdma_cm_event_type expected_evt_type,
     508             :                             struct rdma_cm_event *reaped_evt)
     509             : {
     510           4 :         int rc = -EBADMSG;
     511             : 
     512           4 :         if (expected_evt_type == reaped_evt->event) {
     513           1 :                 return 0;
     514             :         }
     515             : 
     516           3 :         switch (expected_evt_type) {
     517           2 :         case RDMA_CM_EVENT_ESTABLISHED:
     518             :                 /*
     519             :                  * There is an enum ib_cm_rej_reason in the kernel headers that sets 10 as
     520             :                  * IB_CM_REJ_STALE_CONN. I can't find the corresponding userspace but we get
     521             :                  * the same values here.
     522             :                  */
     523           2 :                 if (reaped_evt->event == RDMA_CM_EVENT_REJECTED && reaped_evt->status == 10) {
     524           1 :                         rc = -ESTALE;
     525           1 :                 } else if (reaped_evt->event == RDMA_CM_EVENT_CONNECT_RESPONSE) {
     526             :                         /*
     527             :                          *  If we are using a qpair which is not created using rdma cm API
     528             :                          *  then we will receive RDMA_CM_EVENT_CONNECT_RESPONSE instead of
     529             :                          *  RDMA_CM_EVENT_ESTABLISHED.
     530             :                          */
     531           1 :                         return 0;
     532             :                 }
     533           1 :                 break;
     534           1 :         default:
     535           1 :                 break;
     536             :         }
     537             : 
     538           2 :         SPDK_ERRLOG("Expected %s but received %s (%d) from CM event channel (status = %d)\n",
     539             :                     nvme_rdma_cm_event_str_get(expected_evt_type),
     540             :                     nvme_rdma_cm_event_str_get(reaped_evt->event), reaped_evt->event,
     541             :                     reaped_evt->status);
     542           2 :         return rc;
     543             : }
     544             : 
     545             : static int
     546           0 : nvme_rdma_process_event_start(struct nvme_rdma_qpair *rqpair,
     547             :                               enum rdma_cm_event_type evt,
     548             :                               nvme_rdma_cm_event_cb evt_cb)
     549             : {
     550             :         int     rc;
     551             : 
     552           0 :         assert(evt_cb != NULL);
     553             : 
     554           0 :         if (rqpair->evt != NULL) {
     555           0 :                 rc = nvme_rdma_qpair_process_cm_event(rqpair);
     556           0 :                 if (rc) {
     557           0 :                         return rc;
     558             :                 }
     559             :         }
     560             : 
     561           0 :         rqpair->expected_evt_type = evt;
     562           0 :         rqpair->evt_cb = evt_cb;
     563           0 :         rqpair->evt_timeout_ticks = (g_spdk_nvme_transport_opts.rdma_cm_event_timeout_ms * 1000 *
     564           0 :                                      spdk_get_ticks_hz()) / SPDK_SEC_TO_USEC + spdk_get_ticks();
     565             : 
     566           0 :         return 0;
     567             : }
     568             : 
     569             : static int
     570           0 : nvme_rdma_process_event_poll(struct nvme_rdma_qpair *rqpair)
     571             : {
     572             :         struct nvme_rdma_ctrlr  *rctrlr;
     573           0 :         int     rc = 0, rc2;
     574             : 
     575           0 :         rctrlr = nvme_rdma_ctrlr(rqpair->qpair.ctrlr);
     576           0 :         assert(rctrlr != NULL);
     577             : 
     578           0 :         if (!rqpair->evt && spdk_get_ticks() < rqpair->evt_timeout_ticks) {
     579           0 :                 rc = nvme_rdma_poll_events(rctrlr);
     580           0 :                 if (rc == -EAGAIN || rc == -EWOULDBLOCK) {
     581           0 :                         return rc;
     582             :                 }
     583             :         }
     584             : 
     585           0 :         if (rqpair->evt == NULL) {
     586           0 :                 rc = -EADDRNOTAVAIL;
     587           0 :                 goto exit;
     588             :         }
     589             : 
     590           0 :         rc = nvme_rdma_validate_cm_event(rqpair->expected_evt_type, rqpair->evt);
     591             : 
     592           0 :         rc2 = nvme_rdma_qpair_process_cm_event(rqpair);
     593             :         /* bad message takes precedence over the other error codes from processing the event. */
     594           0 :         rc = rc == 0 ? rc2 : rc;
     595             : 
     596           0 : exit:
     597           0 :         assert(rqpair->evt_cb != NULL);
     598           0 :         return rqpair->evt_cb(rqpair, rc);
     599             : }
     600             : 
     601             : static int
     602           3 : nvme_rdma_resize_cq(struct nvme_rdma_qpair *rqpair, struct nvme_rdma_poller *poller)
     603             : {
     604             :         int     current_num_wc, required_num_wc;
     605             :         int     max_cq_size;
     606             : 
     607           3 :         required_num_wc = poller->required_num_wc + WC_PER_QPAIR(rqpair->num_entries);
     608           3 :         current_num_wc = poller->current_num_wc;
     609           3 :         if (current_num_wc < required_num_wc) {
     610           2 :                 current_num_wc = spdk_max(current_num_wc * 2, required_num_wc);
     611             :         }
     612             : 
     613           3 :         max_cq_size = g_spdk_nvme_transport_opts.rdma_max_cq_size;
     614           3 :         if (max_cq_size != 0 && current_num_wc > max_cq_size) {
     615           0 :                 current_num_wc = max_cq_size;
     616             :         }
     617             : 
     618           3 :         if (poller->current_num_wc != current_num_wc) {
     619           2 :                 SPDK_DEBUGLOG(nvme, "Resize RDMA CQ from %d to %d\n", poller->current_num_wc,
     620             :                               current_num_wc);
     621           2 :                 if (ibv_resize_cq(poller->cq, current_num_wc)) {
     622           1 :                         SPDK_ERRLOG("RDMA CQ resize failed: errno %d: %s\n", errno, spdk_strerror(errno));
     623           1 :                         return -1;
     624             :                 }
     625             : 
     626           1 :                 poller->current_num_wc = current_num_wc;
     627             :         }
     628             : 
     629           2 :         poller->required_num_wc = required_num_wc;
     630           2 :         return 0;
     631             : }
     632             : 
     633             : static int
     634           5 : nvme_rdma_qpair_set_poller(struct spdk_nvme_qpair *qpair)
     635             : {
     636           5 :         struct nvme_rdma_qpair          *rqpair = nvme_rdma_qpair(qpair);
     637           5 :         struct nvme_rdma_poll_group     *group = nvme_rdma_poll_group(qpair->poll_group);
     638             :         struct nvme_rdma_poller         *poller;
     639             : 
     640           5 :         assert(rqpair->cq == NULL);
     641             : 
     642           5 :         poller = nvme_rdma_poll_group_get_poller(group, rqpair->cm_id->verbs);
     643           5 :         if (!poller) {
     644           2 :                 SPDK_ERRLOG("Unable to find a cq for qpair %p on poll group %p\n", qpair, qpair->poll_group);
     645           2 :                 return -EINVAL;
     646             :         }
     647             : 
     648           3 :         if (!poller->srq) {
     649           3 :                 if (nvme_rdma_resize_cq(rqpair, poller)) {
     650           1 :                         nvme_rdma_poll_group_put_poller(group, poller);
     651           1 :                         return -EPROTO;
     652             :                 }
     653             :         }
     654             : 
     655           2 :         rqpair->cq = poller->cq;
     656           2 :         rqpair->srq = poller->srq;
     657           2 :         if (rqpair->srq) {
     658           0 :                 rqpair->rsps = poller->rsps;
     659             :         }
     660           2 :         rqpair->poller = poller;
     661           2 :         return 0;
     662             : }
     663             : 
     664             : static int
     665           1 : nvme_rdma_qpair_init(struct nvme_rdma_qpair *rqpair)
     666             : {
     667             :         int                     rc;
     668           1 :         struct spdk_rdma_provider_qp_init_attr  attr = {};
     669           1 :         struct ibv_device_attr  dev_attr;
     670             :         struct nvme_rdma_ctrlr  *rctrlr;
     671             :         uint32_t num_cqe, max_num_cqe;
     672             : 
     673           1 :         rc = ibv_query_device(rqpair->cm_id->verbs, &dev_attr);
     674           1 :         if (rc != 0) {
     675           0 :                 SPDK_ERRLOG("Failed to query RDMA device attributes.\n");
     676           0 :                 return -1;
     677             :         }
     678             : 
     679           1 :         if (rqpair->qpair.poll_group) {
     680           0 :                 assert(!rqpair->cq);
     681           0 :                 rc = nvme_rdma_qpair_set_poller(&rqpair->qpair);
     682           0 :                 if (rc) {
     683           0 :                         SPDK_ERRLOG("Unable to activate the rdmaqpair.\n");
     684           0 :                         return -1;
     685             :                 }
     686           0 :                 assert(rqpair->cq);
     687             :         } else {
     688           1 :                 num_cqe = rqpair->num_entries * 2;
     689           1 :                 max_num_cqe = g_spdk_nvme_transport_opts.rdma_max_cq_size;
     690           1 :                 if (max_num_cqe != 0 && num_cqe > max_num_cqe) {
     691           0 :                         num_cqe = max_num_cqe;
     692             :                 }
     693           1 :                 rqpair->cq = ibv_create_cq(rqpair->cm_id->verbs, num_cqe, rqpair, NULL, 0);
     694           1 :                 if (!rqpair->cq) {
     695           0 :                         SPDK_ERRLOG("Unable to create completion queue: errno %d: %s\n", errno, spdk_strerror(errno));
     696           0 :                         return -1;
     697             :                 }
     698             :         }
     699             : 
     700           1 :         rctrlr = nvme_rdma_ctrlr(rqpair->qpair.ctrlr);
     701           1 :         if (g_nvme_hooks.get_ibv_pd) {
     702           0 :                 attr.pd = g_nvme_hooks.get_ibv_pd(&rctrlr->ctrlr.trid, rqpair->cm_id->verbs);
     703             :         } else {
     704           1 :                 attr.pd = spdk_rdma_utils_get_pd(rqpair->cm_id->verbs);
     705             :         }
     706             : 
     707           1 :         attr.stats =            rqpair->poller ? &rqpair->poller->stats.rdma_stats : NULL;
     708           1 :         attr.send_cq            = rqpair->cq;
     709           1 :         attr.recv_cq            = rqpair->cq;
     710           1 :         attr.cap.max_send_wr    = rqpair->num_entries; /* SEND operations */
     711           1 :         if (rqpair->srq) {
     712           0 :                 attr.srq        = rqpair->srq->srq;
     713             :         } else {
     714           1 :                 attr.cap.max_recv_wr = rqpair->num_entries; /* RECV operations */
     715             :         }
     716           1 :         attr.cap.max_send_sge   = spdk_min(NVME_RDMA_DEFAULT_TX_SGE, dev_attr.max_sge);
     717           1 :         attr.cap.max_recv_sge   = spdk_min(NVME_RDMA_DEFAULT_RX_SGE, dev_attr.max_sge);
     718             : 
     719           1 :         rqpair->rdma_qp = spdk_rdma_provider_qp_create(rqpair->cm_id, &attr);
     720             : 
     721           1 :         if (!rqpair->rdma_qp) {
     722           0 :                 return -1;
     723             :         }
     724             : 
     725             :         /* ibv_create_qp will change the values in attr.cap. Make sure we store the proper value. */
     726           1 :         rqpair->max_send_sge = spdk_min(NVME_RDMA_DEFAULT_TX_SGE, attr.cap.max_send_sge);
     727           1 :         rqpair->current_num_sends = 0;
     728             : 
     729           1 :         rqpair->cm_id->context = rqpair;
     730             : 
     731           1 :         return 0;
     732             : }
     733             : 
     734             : static void
     735           0 : nvme_rdma_reset_failed_sends(struct nvme_rdma_qpair *rqpair,
     736             :                              struct ibv_send_wr *bad_send_wr, int rc)
     737             : {
     738           0 :         SPDK_ERRLOG("Failed to post WRs on send queue, errno %d (%s), bad_wr %p\n",
     739             :                     rc, spdk_strerror(rc), bad_send_wr);
     740           0 :         while (bad_send_wr != NULL) {
     741           0 :                 assert(rqpair->current_num_sends > 0);
     742           0 :                 rqpair->current_num_sends--;
     743           0 :                 bad_send_wr = bad_send_wr->next;
     744             :         }
     745           0 : }
     746             : 
     747             : static void
     748           0 : nvme_rdma_reset_failed_recvs(struct nvme_rdma_rsps *rsps,
     749             :                              struct ibv_recv_wr *bad_recv_wr, int rc)
     750             : {
     751           0 :         SPDK_ERRLOG("Failed to post WRs on receive queue, errno %d (%s), bad_wr %p\n",
     752             :                     rc, spdk_strerror(rc), bad_recv_wr);
     753           0 :         while (bad_recv_wr != NULL) {
     754           0 :                 assert(rsps->current_num_recvs > 0);
     755           0 :                 rsps->current_num_recvs--;
     756           0 :                 bad_recv_wr = bad_recv_wr->next;
     757             :         }
     758           0 : }
     759             : 
     760             : static inline int
     761           1 : nvme_rdma_qpair_submit_sends(struct nvme_rdma_qpair *rqpair)
     762             : {
     763           1 :         struct ibv_send_wr *bad_send_wr = NULL;
     764             :         int rc;
     765             : 
     766           1 :         rc = spdk_rdma_provider_qp_flush_send_wrs(rqpair->rdma_qp, &bad_send_wr);
     767             : 
     768           1 :         if (spdk_unlikely(rc)) {
     769           0 :                 nvme_rdma_reset_failed_sends(rqpair, bad_send_wr, rc);
     770             :         }
     771             : 
     772           1 :         return rc;
     773             : }
     774             : 
     775             : static inline int
     776           0 : nvme_rdma_qpair_submit_recvs(struct nvme_rdma_qpair *rqpair)
     777             : {
     778           0 :         struct ibv_recv_wr *bad_recv_wr;
     779           0 :         int rc = 0;
     780             : 
     781           0 :         rc = spdk_rdma_provider_qp_flush_recv_wrs(rqpair->rdma_qp, &bad_recv_wr);
     782           0 :         if (spdk_unlikely(rc)) {
     783           0 :                 nvme_rdma_reset_failed_recvs(rqpair->rsps, bad_recv_wr, rc);
     784             :         }
     785             : 
     786           0 :         return rc;
     787             : }
     788             : 
     789             : static inline int
     790           0 : nvme_rdma_poller_submit_recvs(struct nvme_rdma_poller *poller)
     791             : {
     792           0 :         struct ibv_recv_wr *bad_recv_wr;
     793             :         int rc;
     794             : 
     795           0 :         rc = spdk_rdma_provider_srq_flush_recv_wrs(poller->srq, &bad_recv_wr);
     796           0 :         if (spdk_unlikely(rc)) {
     797           0 :                 nvme_rdma_reset_failed_recvs(poller->rsps, bad_recv_wr, rc);
     798             :         }
     799             : 
     800           0 :         return rc;
     801             : }
     802             : 
     803             : #define nvme_rdma_trace_ibv_sge(sg_list) \
     804             :         if (sg_list) { \
     805             :                 SPDK_DEBUGLOG(nvme, "local addr %p length 0x%x lkey 0x%x\n", \
     806             :                               (void *)(sg_list)->addr, (sg_list)->length, (sg_list)->lkey); \
     807             :         }
     808             : 
     809             : static void
     810           3 : nvme_rdma_free_rsps(struct nvme_rdma_rsps *rsps)
     811             : {
     812           3 :         if (!rsps) {
     813           1 :                 return;
     814             :         }
     815             : 
     816           2 :         spdk_free(rsps->rsps);
     817           2 :         spdk_free(rsps->rsp_sgls);
     818           2 :         spdk_free(rsps->rsp_recv_wrs);
     819           2 :         spdk_free(rsps);
     820             : }
     821             : 
     822             : static struct nvme_rdma_rsps *
     823           2 : nvme_rdma_create_rsps(struct nvme_rdma_rsp_opts *opts)
     824             : {
     825             :         struct nvme_rdma_rsps *rsps;
     826           2 :         struct spdk_rdma_utils_memory_translation translation;
     827             :         uint16_t i;
     828             :         int rc;
     829             : 
     830           2 :         rsps = spdk_zmalloc(sizeof(*rsps), 0, NULL, SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
     831           2 :         if (!rsps) {
     832           0 :                 SPDK_ERRLOG("Failed to allocate rsps object\n");
     833           0 :                 return NULL;
     834             :         }
     835             : 
     836           2 :         rsps->rsp_sgls = spdk_zmalloc(opts->num_entries * sizeof(*rsps->rsp_sgls), 0, NULL,
     837             :                                       SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
     838           2 :         if (!rsps->rsp_sgls) {
     839           1 :                 SPDK_ERRLOG("Failed to allocate rsp_sgls\n");
     840           1 :                 goto fail;
     841             :         }
     842             : 
     843           1 :         rsps->rsp_recv_wrs = spdk_zmalloc(opts->num_entries * sizeof(*rsps->rsp_recv_wrs), 0, NULL,
     844             :                                           SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
     845           1 :         if (!rsps->rsp_recv_wrs) {
     846           0 :                 SPDK_ERRLOG("Failed to allocate rsp_recv_wrs\n");
     847           0 :                 goto fail;
     848             :         }
     849             : 
     850           1 :         rsps->rsps = spdk_zmalloc(opts->num_entries * sizeof(*rsps->rsps), 0, NULL,
     851             :                                   SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
     852           1 :         if (!rsps->rsps) {
     853           0 :                 SPDK_ERRLOG("can not allocate rdma rsps\n");
     854           0 :                 goto fail;
     855             :         }
     856             : 
     857           2 :         for (i = 0; i < opts->num_entries; i++) {
     858           1 :                 struct ibv_sge *rsp_sgl = &rsps->rsp_sgls[i];
     859           1 :                 struct spdk_nvme_rdma_rsp *rsp = &rsps->rsps[i];
     860           1 :                 struct ibv_recv_wr *recv_wr = &rsps->rsp_recv_wrs[i];
     861             : 
     862           1 :                 rsp->rqpair = opts->rqpair;
     863           1 :                 rsp->rdma_wr.type = RDMA_WR_TYPE_RECV;
     864           1 :                 rsp->recv_wr = recv_wr;
     865           1 :                 rsp_sgl->addr = (uint64_t)rsp;
     866           1 :                 rsp_sgl->length = sizeof(struct spdk_nvme_cpl);
     867           1 :                 rc = spdk_rdma_utils_get_translation(opts->mr_map, rsp, sizeof(*rsp), &translation);
     868           1 :                 if (rc) {
     869           0 :                         goto fail;
     870             :                 }
     871           1 :                 rsp_sgl->lkey = spdk_rdma_utils_memory_translation_get_lkey(&translation);
     872             : 
     873           1 :                 recv_wr->wr_id = (uint64_t)&rsp->rdma_wr;
     874           1 :                 recv_wr->next = NULL;
     875           1 :                 recv_wr->sg_list = rsp_sgl;
     876           1 :                 recv_wr->num_sge = 1;
     877             : 
     878           1 :                 nvme_rdma_trace_ibv_sge(recv_wr->sg_list);
     879             : 
     880           1 :                 if (opts->rqpair) {
     881           1 :                         spdk_rdma_provider_qp_queue_recv_wrs(opts->rqpair->rdma_qp, recv_wr);
     882             :                 } else {
     883           0 :                         spdk_rdma_provider_srq_queue_recv_wrs(opts->srq, recv_wr);
     884             :                 }
     885             :         }
     886             : 
     887           1 :         rsps->num_entries = opts->num_entries;
     888           1 :         rsps->current_num_recvs = opts->num_entries;
     889             : 
     890           1 :         return rsps;
     891           1 : fail:
     892           1 :         nvme_rdma_free_rsps(rsps);
     893           1 :         return NULL;
     894             : }
     895             : 
     896             : static void
     897           3 : nvme_rdma_free_reqs(struct nvme_rdma_qpair *rqpair)
     898             : {
     899           3 :         if (!rqpair->rdma_reqs) {
     900           2 :                 return;
     901             :         }
     902             : 
     903           1 :         spdk_free(rqpair->cmds);
     904           1 :         rqpair->cmds = NULL;
     905             : 
     906           1 :         spdk_free(rqpair->rdma_reqs);
     907           1 :         rqpair->rdma_reqs = NULL;
     908             : }
     909             : 
     910             : static int
     911           4 : nvme_rdma_create_reqs(struct nvme_rdma_qpair *rqpair)
     912             : {
     913           4 :         struct spdk_rdma_utils_memory_translation translation;
     914             :         uint16_t i;
     915             :         int rc;
     916             : 
     917           4 :         assert(!rqpair->rdma_reqs);
     918           4 :         rqpair->rdma_reqs = spdk_zmalloc(rqpair->num_entries * sizeof(struct spdk_nvme_rdma_req), 0, NULL,
     919             :                                          SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
     920           4 :         if (rqpair->rdma_reqs == NULL) {
     921           1 :                 SPDK_ERRLOG("Failed to allocate rdma_reqs\n");
     922           1 :                 goto fail;
     923             :         }
     924             : 
     925           3 :         assert(!rqpair->cmds);
     926           3 :         rqpair->cmds = spdk_zmalloc(rqpair->num_entries * sizeof(*rqpair->cmds), 0, NULL,
     927             :                                     SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
     928           3 :         if (!rqpair->cmds) {
     929           0 :                 SPDK_ERRLOG("Failed to allocate RDMA cmds\n");
     930           0 :                 goto fail;
     931             :         }
     932             : 
     933           3 :         TAILQ_INIT(&rqpair->free_reqs);
     934           3 :         TAILQ_INIT(&rqpair->outstanding_reqs);
     935          10 :         for (i = 0; i < rqpair->num_entries; i++) {
     936             :                 struct spdk_nvme_rdma_req       *rdma_req;
     937             :                 struct spdk_nvmf_cmd            *cmd;
     938             : 
     939           7 :                 rdma_req = &rqpair->rdma_reqs[i];
     940           7 :                 rdma_req->rdma_wr.type = RDMA_WR_TYPE_SEND;
     941           7 :                 cmd = &rqpair->cmds[i];
     942             : 
     943           7 :                 rdma_req->id = i;
     944             : 
     945           7 :                 rc = spdk_rdma_utils_get_translation(rqpair->mr_map, cmd, sizeof(*cmd), &translation);
     946           7 :                 if (rc) {
     947           0 :                         goto fail;
     948             :                 }
     949           7 :                 rdma_req->send_sgl[0].lkey = spdk_rdma_utils_memory_translation_get_lkey(&translation);
     950             : 
     951             :                 /* The first RDMA sgl element will always point
     952             :                  * at this data structure. Depending on whether
     953             :                  * an NVMe-oF SGL is required, the length of
     954             :                  * this element may change. */
     955           7 :                 rdma_req->send_sgl[0].addr = (uint64_t)cmd;
     956           7 :                 rdma_req->send_wr.wr_id = (uint64_t)&rdma_req->rdma_wr;
     957           7 :                 rdma_req->send_wr.next = NULL;
     958           7 :                 rdma_req->send_wr.opcode = IBV_WR_SEND;
     959           7 :                 rdma_req->send_wr.send_flags = IBV_SEND_SIGNALED;
     960           7 :                 rdma_req->send_wr.sg_list = rdma_req->send_sgl;
     961           7 :                 rdma_req->send_wr.imm_data = 0;
     962             : 
     963           7 :                 TAILQ_INSERT_TAIL(&rqpair->free_reqs, rdma_req, link);
     964             :         }
     965             : 
     966           3 :         return 0;
     967           1 : fail:
     968           1 :         nvme_rdma_free_reqs(rqpair);
     969           1 :         return -ENOMEM;
     970             : }
     971             : 
     972             : static int nvme_rdma_connect(struct nvme_rdma_qpair *rqpair);
     973             : 
     974             : static int
     975           0 : nvme_rdma_route_resolved(struct nvme_rdma_qpair *rqpair, int ret)
     976             : {
     977           0 :         if (ret) {
     978           0 :                 SPDK_ERRLOG("RDMA route resolution error\n");
     979           0 :                 return -1;
     980             :         }
     981             : 
     982           0 :         ret = nvme_rdma_qpair_init(rqpair);
     983           0 :         if (ret < 0) {
     984           0 :                 SPDK_ERRLOG("nvme_rdma_qpair_init() failed\n");
     985           0 :                 return -1;
     986             :         }
     987             : 
     988           0 :         return nvme_rdma_connect(rqpair);
     989             : }
     990             : 
     991             : static int
     992           0 : nvme_rdma_addr_resolved(struct nvme_rdma_qpair *rqpair, int ret)
     993             : {
     994           0 :         if (ret) {
     995           0 :                 SPDK_ERRLOG("RDMA address resolution error\n");
     996           0 :                 return -1;
     997             :         }
     998             : 
     999           0 :         if (rqpair->qpair.ctrlr->opts.transport_ack_timeout != SPDK_NVME_TRANSPORT_ACK_TIMEOUT_DISABLED) {
    1000             : #ifdef SPDK_CONFIG_RDMA_SET_ACK_TIMEOUT
    1001           0 :                 uint8_t timeout = rqpair->qpair.ctrlr->opts.transport_ack_timeout;
    1002           0 :                 ret = rdma_set_option(rqpair->cm_id, RDMA_OPTION_ID,
    1003             :                                       RDMA_OPTION_ID_ACK_TIMEOUT,
    1004             :                                       &timeout, sizeof(timeout));
    1005           0 :                 if (ret) {
    1006           0 :                         SPDK_NOTICELOG("Can't apply RDMA_OPTION_ID_ACK_TIMEOUT %d, ret %d\n", timeout, ret);
    1007             :                 }
    1008             : #else
    1009             :                 SPDK_DEBUGLOG(nvme, "transport_ack_timeout is not supported\n");
    1010             : #endif
    1011             :         }
    1012             : 
    1013           0 :         if (rqpair->qpair.ctrlr->opts.transport_tos != SPDK_NVME_TRANSPORT_TOS_DISABLED) {
    1014             : #ifdef SPDK_CONFIG_RDMA_SET_TOS
    1015           0 :                 uint8_t tos = rqpair->qpair.ctrlr->opts.transport_tos;
    1016           0 :                 ret = rdma_set_option(rqpair->cm_id, RDMA_OPTION_ID, RDMA_OPTION_ID_TOS, &tos, sizeof(tos));
    1017           0 :                 if (ret) {
    1018           0 :                         SPDK_NOTICELOG("Can't apply RDMA_OPTION_ID_TOS %u, ret %d\n", tos, ret);
    1019             :                 }
    1020             : #else
    1021             :                 SPDK_DEBUGLOG(nvme, "transport_tos is not supported\n");
    1022             : #endif
    1023             :         }
    1024             : 
    1025           0 :         ret = rdma_resolve_route(rqpair->cm_id, NVME_RDMA_TIME_OUT_IN_MS);
    1026           0 :         if (ret) {
    1027           0 :                 SPDK_ERRLOG("rdma_resolve_route\n");
    1028           0 :                 return ret;
    1029             :         }
    1030             : 
    1031           0 :         return nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_ROUTE_RESOLVED,
    1032             :                                              nvme_rdma_route_resolved);
    1033             : }
    1034             : 
    1035             : static int
    1036           0 : nvme_rdma_resolve_addr(struct nvme_rdma_qpair *rqpair,
    1037             :                        struct sockaddr *src_addr,
    1038             :                        struct sockaddr *dst_addr)
    1039             : {
    1040             :         int ret;
    1041             : 
    1042           0 :         if (src_addr) {
    1043           0 :                 int reuse = 1;
    1044             : 
    1045           0 :                 ret = rdma_set_option(rqpair->cm_id, RDMA_OPTION_ID, RDMA_OPTION_ID_REUSEADDR,
    1046             :                                       &reuse, sizeof(reuse));
    1047           0 :                 if (ret) {
    1048           0 :                         SPDK_NOTICELOG("Can't apply RDMA_OPTION_ID_REUSEADDR %d, ret %d\n",
    1049             :                                        reuse, ret);
    1050             :                         /* It is likely that rdma_resolve_addr() returns -EADDRINUSE, but
    1051             :                          * we may missing something. We rely on rdma_resolve_addr().
    1052             :                          */
    1053             :                 }
    1054             :         }
    1055             : 
    1056           0 :         ret = rdma_resolve_addr(rqpair->cm_id, src_addr, dst_addr,
    1057             :                                 NVME_RDMA_TIME_OUT_IN_MS);
    1058           0 :         if (ret) {
    1059           0 :                 SPDK_ERRLOG("rdma_resolve_addr, %d\n", errno);
    1060           0 :                 return ret;
    1061             :         }
    1062             : 
    1063           0 :         return nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_ADDR_RESOLVED,
    1064             :                                              nvme_rdma_addr_resolved);
    1065             : }
    1066             : 
    1067             : static int nvme_rdma_stale_conn_retry(struct nvme_rdma_qpair *rqpair);
    1068             : 
    1069             : static int
    1070           0 : nvme_rdma_connect_established(struct nvme_rdma_qpair *rqpair, int ret)
    1071             : {
    1072           0 :         struct nvme_rdma_rsp_opts opts = {};
    1073             : 
    1074           0 :         if (ret == -ESTALE) {
    1075           0 :                 return nvme_rdma_stale_conn_retry(rqpair);
    1076           0 :         } else if (ret) {
    1077           0 :                 SPDK_ERRLOG("RDMA connect error %d\n", ret);
    1078           0 :                 return ret;
    1079             :         }
    1080             : 
    1081           0 :         assert(!rqpair->mr_map);
    1082           0 :         rqpair->mr_map = spdk_rdma_utils_create_mem_map(rqpair->rdma_qp->qp->pd, &g_nvme_hooks,
    1083             :                          IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
    1084           0 :         if (!rqpair->mr_map) {
    1085           0 :                 SPDK_ERRLOG("Unable to register RDMA memory translation map\n");
    1086           0 :                 return -1;
    1087             :         }
    1088             : 
    1089           0 :         ret = nvme_rdma_create_reqs(rqpair);
    1090           0 :         SPDK_DEBUGLOG(nvme, "rc =%d\n", ret);
    1091           0 :         if (ret) {
    1092           0 :                 SPDK_ERRLOG("Unable to create rqpair RDMA requests\n");
    1093           0 :                 return -1;
    1094             :         }
    1095           0 :         SPDK_DEBUGLOG(nvme, "RDMA requests created\n");
    1096             : 
    1097           0 :         if (!rqpair->srq) {
    1098           0 :                 opts.num_entries = rqpair->num_entries;
    1099           0 :                 opts.rqpair = rqpair;
    1100           0 :                 opts.srq = NULL;
    1101           0 :                 opts.mr_map = rqpair->mr_map;
    1102             : 
    1103           0 :                 assert(!rqpair->rsps);
    1104           0 :                 rqpair->rsps = nvme_rdma_create_rsps(&opts);
    1105           0 :                 if (!rqpair->rsps) {
    1106           0 :                         SPDK_ERRLOG("Unable to create rqpair RDMA responses\n");
    1107           0 :                         return -1;
    1108             :                 }
    1109           0 :                 SPDK_DEBUGLOG(nvme, "RDMA responses created\n");
    1110             : 
    1111           0 :                 ret = nvme_rdma_qpair_submit_recvs(rqpair);
    1112           0 :                 SPDK_DEBUGLOG(nvme, "rc =%d\n", ret);
    1113           0 :                 if (ret) {
    1114           0 :                         SPDK_ERRLOG("Unable to submit rqpair RDMA responses\n");
    1115           0 :                         return -1;
    1116             :                 }
    1117           0 :                 SPDK_DEBUGLOG(nvme, "RDMA responses submitted\n");
    1118             :         }
    1119             : 
    1120           0 :         rqpair->state = NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_SEND;
    1121             : 
    1122           0 :         return 0;
    1123             : }
    1124             : 
    1125             : static int
    1126           0 : nvme_rdma_connect(struct nvme_rdma_qpair *rqpair)
    1127             : {
    1128           0 :         struct rdma_conn_param                          param = {};
    1129           0 :         struct spdk_nvmf_rdma_request_private_data      request_data = {};
    1130           0 :         struct ibv_device_attr                          attr;
    1131             :         int                                             ret;
    1132             :         struct spdk_nvme_ctrlr                          *ctrlr;
    1133             : 
    1134           0 :         ret = ibv_query_device(rqpair->cm_id->verbs, &attr);
    1135           0 :         if (ret != 0) {
    1136           0 :                 SPDK_ERRLOG("Failed to query RDMA device attributes.\n");
    1137           0 :                 return ret;
    1138             :         }
    1139             : 
    1140           0 :         param.responder_resources = attr.max_qp_rd_atom;
    1141             : 
    1142           0 :         ctrlr = rqpair->qpair.ctrlr;
    1143           0 :         if (!ctrlr) {
    1144           0 :                 return -1;
    1145             :         }
    1146             : 
    1147           0 :         request_data.qid = rqpair->qpair.id;
    1148           0 :         request_data.hrqsize = rqpair->num_entries + 1;
    1149           0 :         request_data.hsqsize = rqpair->num_entries;
    1150           0 :         request_data.cntlid = ctrlr->cntlid;
    1151             : 
    1152           0 :         param.private_data = &request_data;
    1153           0 :         param.private_data_len = sizeof(request_data);
    1154           0 :         param.retry_count = ctrlr->opts.transport_retry_count;
    1155           0 :         param.rnr_retry_count = 7;
    1156             : 
    1157             :         /* Fields below are ignored by rdma cm if qpair has been
    1158             :          * created using rdma cm API. */
    1159           0 :         param.srq = 0;
    1160           0 :         param.qp_num = rqpair->rdma_qp->qp->qp_num;
    1161             : 
    1162           0 :         ret = rdma_connect(rqpair->cm_id, &param);
    1163           0 :         if (ret) {
    1164           0 :                 SPDK_ERRLOG("nvme rdma connect error\n");
    1165           0 :                 return ret;
    1166             :         }
    1167             : 
    1168           0 :         ctrlr->numa.id_valid = 1;
    1169           0 :         ctrlr->numa.id = spdk_rdma_cm_id_get_numa_id(rqpair->cm_id);
    1170             : 
    1171           0 :         return nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_ESTABLISHED,
    1172             :                                              nvme_rdma_connect_established);
    1173             : }
    1174             : 
    1175             : static int
    1176           0 : nvme_rdma_ctrlr_connect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
    1177             : {
    1178           0 :         struct sockaddr_storage dst_addr;
    1179           0 :         struct sockaddr_storage src_addr;
    1180             :         bool src_addr_specified;
    1181           0 :         long int port, src_port = 0;
    1182             :         int rc;
    1183             :         struct nvme_rdma_ctrlr *rctrlr;
    1184             :         struct nvme_rdma_qpair *rqpair;
    1185             :         struct nvme_rdma_poll_group *group;
    1186             :         int family;
    1187             : 
    1188           0 :         rqpair = nvme_rdma_qpair(qpair);
    1189           0 :         rctrlr = nvme_rdma_ctrlr(ctrlr);
    1190           0 :         assert(rctrlr != NULL);
    1191             : 
    1192           0 :         switch (ctrlr->trid.adrfam) {
    1193           0 :         case SPDK_NVMF_ADRFAM_IPV4:
    1194           0 :                 family = AF_INET;
    1195           0 :                 break;
    1196           0 :         case SPDK_NVMF_ADRFAM_IPV6:
    1197           0 :                 family = AF_INET6;
    1198           0 :                 break;
    1199           0 :         default:
    1200           0 :                 SPDK_ERRLOG("Unhandled ADRFAM %d\n", ctrlr->trid.adrfam);
    1201           0 :                 return -1;
    1202             :         }
    1203             : 
    1204           0 :         SPDK_DEBUGLOG(nvme, "adrfam %d ai_family %d\n", ctrlr->trid.adrfam, family);
    1205             : 
    1206           0 :         memset(&dst_addr, 0, sizeof(dst_addr));
    1207             : 
    1208           0 :         SPDK_DEBUGLOG(nvme, "trsvcid is %s\n", ctrlr->trid.trsvcid);
    1209           0 :         rc = nvme_parse_addr(&dst_addr, family, ctrlr->trid.traddr, ctrlr->trid.trsvcid, &port);
    1210           0 :         if (rc != 0) {
    1211           0 :                 SPDK_ERRLOG("dst_addr nvme_parse_addr() failed\n");
    1212           0 :                 return -1;
    1213             :         }
    1214             : 
    1215           0 :         if (ctrlr->opts.src_addr[0] || ctrlr->opts.src_svcid[0]) {
    1216           0 :                 memset(&src_addr, 0, sizeof(src_addr));
    1217           0 :                 rc = nvme_parse_addr(&src_addr, family,
    1218           0 :                                      ctrlr->opts.src_addr[0] ? ctrlr->opts.src_addr : NULL,
    1219           0 :                                      ctrlr->opts.src_svcid[0] ? ctrlr->opts.src_svcid : NULL,
    1220             :                                      &src_port);
    1221           0 :                 if (rc != 0) {
    1222           0 :                         SPDK_ERRLOG("src_addr nvme_parse_addr() failed\n");
    1223           0 :                         return -1;
    1224             :                 }
    1225           0 :                 src_addr_specified = true;
    1226             :         } else {
    1227           0 :                 src_addr_specified = false;
    1228             :         }
    1229             : 
    1230           0 :         rc = rdma_create_id(rctrlr->cm_channel, &rqpair->cm_id, rqpair, RDMA_PS_TCP);
    1231           0 :         if (rc < 0) {
    1232           0 :                 SPDK_ERRLOG("rdma_create_id() failed\n");
    1233           0 :                 return -1;
    1234             :         }
    1235             : 
    1236           0 :         rc = nvme_rdma_resolve_addr(rqpair,
    1237             :                                     src_addr_specified ? (struct sockaddr *)&src_addr : NULL,
    1238             :                                     (struct sockaddr *)&dst_addr);
    1239           0 :         if (rc < 0) {
    1240           0 :                 SPDK_ERRLOG("nvme_rdma_resolve_addr() failed\n");
    1241           0 :                 return -1;
    1242             :         }
    1243             : 
    1244           0 :         rqpair->state = NVME_RDMA_QPAIR_STATE_INITIALIZING;
    1245             : 
    1246           0 :         if (qpair->poll_group != NULL) {
    1247           0 :                 group = nvme_rdma_poll_group(qpair->poll_group);
    1248           0 :                 TAILQ_INSERT_TAIL(&group->connecting_qpairs, rqpair, link_connecting);
    1249             :         }
    1250             : 
    1251           0 :         return 0;
    1252             : }
    1253             : 
    1254             : static int
    1255           0 : nvme_rdma_stale_conn_reconnect(struct nvme_rdma_qpair *rqpair)
    1256             : {
    1257           0 :         struct spdk_nvme_qpair *qpair = &rqpair->qpair;
    1258             : 
    1259           0 :         if (spdk_get_ticks() < rqpair->evt_timeout_ticks) {
    1260           0 :                 return -EAGAIN;
    1261             :         }
    1262             : 
    1263           0 :         return nvme_rdma_ctrlr_connect_qpair(qpair->ctrlr, qpair);
    1264             : }
    1265             : 
    1266             : static int
    1267           0 : nvme_rdma_ctrlr_connect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr,
    1268             :                                    struct spdk_nvme_qpair *qpair)
    1269             : {
    1270           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    1271             :         int rc;
    1272             : 
    1273           0 :         if (rqpair->in_connect_poll) {
    1274           0 :                 return -EAGAIN;
    1275             :         }
    1276             : 
    1277           0 :         rqpair->in_connect_poll = true;
    1278             : 
    1279           0 :         switch (rqpair->state) {
    1280           0 :         case NVME_RDMA_QPAIR_STATE_INVALID:
    1281           0 :                 rc = -EAGAIN;
    1282           0 :                 break;
    1283             : 
    1284           0 :         case NVME_RDMA_QPAIR_STATE_INITIALIZING:
    1285             :         case NVME_RDMA_QPAIR_STATE_EXITING:
    1286           0 :                 if (!nvme_qpair_is_admin_queue(qpair)) {
    1287           0 :                         nvme_ctrlr_lock(ctrlr);
    1288             :                 }
    1289             : 
    1290           0 :                 rc = nvme_rdma_process_event_poll(rqpair);
    1291             : 
    1292           0 :                 if (!nvme_qpair_is_admin_queue(qpair)) {
    1293           0 :                         nvme_ctrlr_unlock(ctrlr);
    1294             :                 }
    1295             : 
    1296           0 :                 if (rc == 0) {
    1297           0 :                         rc = -EAGAIN;
    1298             :                 }
    1299           0 :                 rqpair->in_connect_poll = false;
    1300             : 
    1301           0 :                 return rc;
    1302             : 
    1303           0 :         case NVME_RDMA_QPAIR_STATE_STALE_CONN:
    1304           0 :                 rc = nvme_rdma_stale_conn_reconnect(rqpair);
    1305           0 :                 if (rc == 0) {
    1306           0 :                         rc = -EAGAIN;
    1307             :                 }
    1308           0 :                 break;
    1309           0 :         case NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_SEND:
    1310           0 :                 rc = nvme_fabric_qpair_connect_async(qpair, rqpair->num_entries + 1);
    1311           0 :                 if (rc == 0) {
    1312           0 :                         rqpair->state = NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_POLL;
    1313           0 :                         rc = -EAGAIN;
    1314             :                 } else {
    1315           0 :                         SPDK_ERRLOG("Failed to send an NVMe-oF Fabric CONNECT command\n");
    1316             :                 }
    1317           0 :                 break;
    1318           0 :         case NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_POLL:
    1319           0 :                 rc = nvme_fabric_qpair_connect_poll(qpair);
    1320           0 :                 if (rc == 0) {
    1321           0 :                         if (nvme_fabric_qpair_auth_required(qpair)) {
    1322           0 :                                 rc = nvme_fabric_qpair_authenticate_async(qpair);
    1323           0 :                                 if (rc == 0) {
    1324           0 :                                         rqpair->state = NVME_RDMA_QPAIR_STATE_AUTHENTICATING;
    1325           0 :                                         rc = -EAGAIN;
    1326             :                                 }
    1327             :                         } else {
    1328           0 :                                 rqpair->state = NVME_RDMA_QPAIR_STATE_RUNNING;
    1329           0 :                                 nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
    1330             :                         }
    1331           0 :                 } else if (rc != -EAGAIN) {
    1332           0 :                         SPDK_ERRLOG("Failed to poll NVMe-oF Fabric CONNECT command\n");
    1333             :                 }
    1334           0 :                 break;
    1335           0 :         case NVME_RDMA_QPAIR_STATE_AUTHENTICATING:
    1336           0 :                 rc = nvme_fabric_qpair_authenticate_poll(qpair);
    1337           0 :                 if (rc == 0) {
    1338           0 :                         rqpair->state = NVME_RDMA_QPAIR_STATE_RUNNING;
    1339           0 :                         nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
    1340             :                 }
    1341           0 :                 break;
    1342           0 :         case NVME_RDMA_QPAIR_STATE_RUNNING:
    1343           0 :                 rc = 0;
    1344           0 :                 break;
    1345           0 :         default:
    1346           0 :                 assert(false);
    1347             :                 rc = -EINVAL;
    1348             :                 break;
    1349             :         }
    1350             : 
    1351           0 :         rqpair->in_connect_poll = false;
    1352             : 
    1353           0 :         return rc;
    1354             : }
    1355             : 
    1356             : static inline int
    1357          28 : nvme_rdma_get_memory_translation(struct nvme_request *req, struct nvme_rdma_qpair *rqpair,
    1358             :                                  struct nvme_rdma_memory_translation_ctx *_ctx)
    1359             : {
    1360          28 :         struct spdk_memory_domain_translation_ctx ctx;
    1361          28 :         struct spdk_memory_domain_translation_result dma_translation = {.iov_count = 0};
    1362          28 :         struct spdk_rdma_utils_memory_translation rdma_translation;
    1363             :         int rc;
    1364             : 
    1365          28 :         assert(req);
    1366          28 :         assert(rqpair);
    1367          28 :         assert(_ctx);
    1368             : 
    1369          28 :         if (req->payload.opts && req->payload.opts->memory_domain) {
    1370           2 :                 ctx.size = sizeof(struct spdk_memory_domain_translation_ctx);
    1371           2 :                 ctx.rdma.ibv_qp = rqpair->rdma_qp->qp;
    1372           2 :                 dma_translation.size = sizeof(struct spdk_memory_domain_translation_result);
    1373             : 
    1374           6 :                 rc = spdk_memory_domain_translate_data(req->payload.opts->memory_domain,
    1375           2 :                                                        req->payload.opts->memory_domain_ctx,
    1376           2 :                                                        rqpair->rdma_qp->domain, &ctx, _ctx->addr,
    1377             :                                                        _ctx->length, &dma_translation);
    1378           2 :                 if (spdk_unlikely(rc) || dma_translation.iov_count != 1) {
    1379           1 :                         SPDK_ERRLOG("DMA memory translation failed, rc %d, iov count %u\n", rc, dma_translation.iov_count);
    1380           1 :                         return rc;
    1381             :                 }
    1382             : 
    1383           1 :                 _ctx->lkey = dma_translation.rdma.lkey;
    1384           1 :                 _ctx->rkey = dma_translation.rdma.rkey;
    1385           1 :                 _ctx->addr = dma_translation.iov.iov_base;
    1386           1 :                 _ctx->length = dma_translation.iov.iov_len;
    1387             :         } else {
    1388          26 :                 rc = spdk_rdma_utils_get_translation(rqpair->mr_map, _ctx->addr, _ctx->length, &rdma_translation);
    1389          26 :                 if (spdk_unlikely(rc)) {
    1390           2 :                         SPDK_ERRLOG("RDMA memory translation failed, rc %d\n", rc);
    1391           2 :                         return rc;
    1392             :                 }
    1393          24 :                 if (rdma_translation.translation_type == SPDK_RDMA_UTILS_TRANSLATION_MR) {
    1394          24 :                         _ctx->lkey = rdma_translation.mr_or_key.mr->lkey;
    1395          24 :                         _ctx->rkey = rdma_translation.mr_or_key.mr->rkey;
    1396             :                 } else {
    1397           0 :                         _ctx->lkey = _ctx->rkey = (uint32_t)rdma_translation.mr_or_key.key;
    1398             :                 }
    1399             :         }
    1400             : 
    1401          25 :         return 0;
    1402             : }
    1403             : 
    1404             : 
    1405             : /*
    1406             :  * Build SGL describing empty payload.
    1407             :  */
    1408             : static int
    1409           2 : nvme_rdma_build_null_request(struct spdk_nvme_rdma_req *rdma_req)
    1410             : {
    1411           2 :         struct nvme_request *req = rdma_req->req;
    1412             : 
    1413           2 :         req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
    1414             : 
    1415             :         /* The first element of this SGL is pointing at an
    1416             :          * spdk_nvmf_cmd object. For this particular command,
    1417             :          * we only need the first 64 bytes corresponding to
    1418             :          * the NVMe command. */
    1419           2 :         rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
    1420             : 
    1421             :         /* The RDMA SGL needs one element describing the NVMe command. */
    1422           2 :         rdma_req->send_wr.num_sge = 1;
    1423             : 
    1424           2 :         req->cmd.dptr.sgl1.keyed.type = SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK;
    1425           2 :         req->cmd.dptr.sgl1.keyed.subtype = SPDK_NVME_SGL_SUBTYPE_ADDRESS;
    1426           2 :         req->cmd.dptr.sgl1.keyed.length = 0;
    1427           2 :         req->cmd.dptr.sgl1.keyed.key = 0;
    1428           2 :         req->cmd.dptr.sgl1.address = 0;
    1429             : 
    1430           2 :         return 0;
    1431             : }
    1432             : 
    1433             : static inline void
    1434           3 : nvme_rdma_configure_contig_inline_request(struct spdk_nvme_rdma_req *rdma_req,
    1435             :                 struct nvme_request *req, struct nvme_rdma_memory_translation_ctx *ctx)
    1436             : {
    1437           3 :         rdma_req->send_sgl[1].lkey = ctx->lkey;
    1438             : 
    1439             :         /* The first element of this SGL is pointing at an
    1440             :          * spdk_nvmf_cmd object. For this particular command,
    1441             :          * we only need the first 64 bytes corresponding to
    1442             :          * the NVMe command. */
    1443           3 :         rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
    1444             : 
    1445           3 :         rdma_req->send_sgl[1].addr = (uint64_t)ctx->addr;
    1446           3 :         rdma_req->send_sgl[1].length = (uint32_t)ctx->length;
    1447             : 
    1448             :         /* The RDMA SGL contains two elements. The first describes
    1449             :          * the NVMe command and the second describes the data
    1450             :          * payload. */
    1451           3 :         rdma_req->send_wr.num_sge = 2;
    1452             : 
    1453           3 :         req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
    1454           3 :         req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
    1455           3 :         req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
    1456           3 :         req->cmd.dptr.sgl1.unkeyed.length = (uint32_t)ctx->length;
    1457             :         /* Inline only supported for icdoff == 0 currently.  This function will
    1458             :          * not get called for controllers with other values. */
    1459           3 :         req->cmd.dptr.sgl1.address = (uint64_t)0;
    1460           3 : }
    1461             : 
    1462             : /*
    1463             :  * Build inline SGL describing contiguous payload buffer.
    1464             :  */
    1465             : static inline int
    1466           3 : nvme_rdma_build_contig_inline_request(struct nvme_rdma_qpair *rqpair,
    1467             :                                       struct spdk_nvme_rdma_req *rdma_req)
    1468             : {
    1469           3 :         struct nvme_request *req = rdma_req->req;
    1470           3 :         struct nvme_rdma_memory_translation_ctx ctx = {
    1471           3 :                 .addr = (uint8_t *)req->payload.contig_or_cb_arg + req->payload_offset,
    1472           3 :                 .length = req->payload_size
    1473             :         };
    1474             :         int rc;
    1475             : 
    1476           3 :         assert(ctx.length != 0);
    1477           3 :         assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
    1478             : 
    1479           3 :         rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
    1480           3 :         if (spdk_unlikely(rc)) {
    1481           0 :                 return -1;
    1482             :         }
    1483             : 
    1484           3 :         nvme_rdma_configure_contig_inline_request(rdma_req, req, &ctx);
    1485             : 
    1486           3 :         return 0;
    1487             : }
    1488             : 
    1489             : static inline void
    1490           2 : nvme_rdma_configure_contig_request(struct spdk_nvme_rdma_req *rdma_req, struct nvme_request *req,
    1491             :                                    struct nvme_rdma_memory_translation_ctx *ctx)
    1492             : {
    1493           2 :         req->cmd.dptr.sgl1.keyed.key = ctx->rkey;
    1494             : 
    1495             :         /* The first element of this SGL is pointing at an
    1496             :          * spdk_nvmf_cmd object. For this particular command,
    1497             :          * we only need the first 64 bytes corresponding to
    1498             :          * the NVMe command. */
    1499           2 :         rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
    1500             : 
    1501             :         /* The RDMA SGL needs one element describing the NVMe command. */
    1502           2 :         rdma_req->send_wr.num_sge = 1;
    1503             : 
    1504           2 :         req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
    1505           2 :         req->cmd.dptr.sgl1.keyed.type = SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK;
    1506           2 :         req->cmd.dptr.sgl1.keyed.subtype = SPDK_NVME_SGL_SUBTYPE_ADDRESS;
    1507           2 :         req->cmd.dptr.sgl1.keyed.length = (uint32_t)ctx->length;
    1508           2 :         req->cmd.dptr.sgl1.address = (uint64_t)ctx->addr;
    1509           2 : }
    1510             : 
    1511             : /*
    1512             :  * Build SGL describing contiguous payload buffer.
    1513             :  */
    1514             : static inline int
    1515           3 : nvme_rdma_build_contig_request(struct nvme_rdma_qpair *rqpair,
    1516             :                                struct spdk_nvme_rdma_req *rdma_req)
    1517             : {
    1518           3 :         struct nvme_request *req = rdma_req->req;
    1519           3 :         struct nvme_rdma_memory_translation_ctx ctx = {
    1520           3 :                 .addr = (uint8_t *)req->payload.contig_or_cb_arg + req->payload_offset,
    1521           3 :                 .length = req->payload_size
    1522             :         };
    1523             :         int rc;
    1524             : 
    1525           3 :         assert(req->payload_size != 0);
    1526           3 :         assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
    1527             : 
    1528           3 :         if (spdk_unlikely(req->payload_size > NVME_RDMA_MAX_KEYED_SGL_LENGTH)) {
    1529           1 :                 SPDK_ERRLOG("SGL length %u exceeds max keyed SGL block size %u\n",
    1530             :                             req->payload_size, NVME_RDMA_MAX_KEYED_SGL_LENGTH);
    1531           1 :                 return -1;
    1532             :         }
    1533             : 
    1534           2 :         rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
    1535           2 :         if (spdk_unlikely(rc)) {
    1536           0 :                 return -1;
    1537             :         }
    1538             : 
    1539           2 :         nvme_rdma_configure_contig_request(rdma_req, req, &ctx);
    1540             : 
    1541           2 :         return 0;
    1542             : }
    1543             : 
    1544             : /*
    1545             :  * Build SGL describing scattered payload buffer.
    1546             :  */
    1547             : static inline int
    1548           7 : nvme_rdma_build_sgl_request(struct nvme_rdma_qpair *rqpair,
    1549             :                             struct spdk_nvme_rdma_req *rdma_req)
    1550             : {
    1551           7 :         struct nvme_request *req = rdma_req->req;
    1552           7 :         struct spdk_nvmf_cmd *cmd = &rqpair->cmds[rdma_req->id];
    1553           7 :         struct nvme_rdma_memory_translation_ctx ctx;
    1554             :         uint32_t remaining_size;
    1555           7 :         uint32_t sge_length;
    1556             :         int rc, max_num_sgl, num_sgl_desc;
    1557             : 
    1558           7 :         assert(req->payload_size != 0);
    1559           7 :         assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
    1560           7 :         assert(req->payload.reset_sgl_fn != NULL);
    1561           7 :         assert(req->payload.next_sge_fn != NULL);
    1562           7 :         req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
    1563             : 
    1564           7 :         max_num_sgl = req->qpair->ctrlr->max_sges;
    1565             : 
    1566           7 :         remaining_size = req->payload_size;
    1567           7 :         num_sgl_desc = 0;
    1568             :         do {
    1569          18 :                 rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &ctx.addr, &sge_length);
    1570          18 :                 if (spdk_unlikely(rc)) {
    1571           1 :                         return -1;
    1572             :                 }
    1573             : 
    1574          17 :                 sge_length = spdk_min(remaining_size, sge_length);
    1575             : 
    1576          17 :                 if (spdk_unlikely(sge_length > NVME_RDMA_MAX_KEYED_SGL_LENGTH)) {
    1577           1 :                         SPDK_ERRLOG("SGL length %u exceeds max keyed SGL block size %u\n",
    1578             :                                     sge_length, NVME_RDMA_MAX_KEYED_SGL_LENGTH);
    1579           1 :                         return -1;
    1580             :                 }
    1581          16 :                 ctx.length = sge_length;
    1582          16 :                 rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
    1583          16 :                 if (spdk_unlikely(rc)) {
    1584           1 :                         return -1;
    1585             :                 }
    1586             : 
    1587          15 :                 cmd->sgl[num_sgl_desc].keyed.key = ctx.rkey;
    1588          15 :                 cmd->sgl[num_sgl_desc].keyed.type = SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK;
    1589          15 :                 cmd->sgl[num_sgl_desc].keyed.subtype = SPDK_NVME_SGL_SUBTYPE_ADDRESS;
    1590          15 :                 cmd->sgl[num_sgl_desc].keyed.length = (uint32_t)ctx.length;
    1591          15 :                 cmd->sgl[num_sgl_desc].address = (uint64_t)ctx.addr;
    1592             : 
    1593          15 :                 remaining_size -= ctx.length;
    1594          15 :                 num_sgl_desc++;
    1595          15 :         } while (remaining_size > 0 && num_sgl_desc < max_num_sgl);
    1596             : 
    1597             : 
    1598             :         /* Should be impossible if we did our sgl checks properly up the stack, but do a sanity check here. */
    1599           4 :         if (spdk_unlikely(remaining_size > 0)) {
    1600           0 :                 return -1;
    1601             :         }
    1602             : 
    1603           4 :         req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
    1604             : 
    1605             :         /* The RDMA SGL needs one element describing some portion
    1606             :          * of the spdk_nvmf_cmd structure. */
    1607           4 :         rdma_req->send_wr.num_sge = 1;
    1608             : 
    1609             :         /*
    1610             :          * If only one SGL descriptor is required, it can be embedded directly in the command
    1611             :          * as a data block descriptor.
    1612             :          */
    1613           4 :         if (num_sgl_desc == 1) {
    1614             :                 /* The first element of this SGL is pointing at an
    1615             :                  * spdk_nvmf_cmd object. For this particular command,
    1616             :                  * we only need the first 64 bytes corresponding to
    1617             :                  * the NVMe command. */
    1618           2 :                 rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
    1619             : 
    1620           2 :                 req->cmd.dptr.sgl1.keyed.type = cmd->sgl[0].keyed.type;
    1621           2 :                 req->cmd.dptr.sgl1.keyed.subtype = cmd->sgl[0].keyed.subtype;
    1622           2 :                 req->cmd.dptr.sgl1.keyed.length = cmd->sgl[0].keyed.length;
    1623           2 :                 req->cmd.dptr.sgl1.keyed.key = cmd->sgl[0].keyed.key;
    1624           2 :                 req->cmd.dptr.sgl1.address = cmd->sgl[0].address;
    1625             :         } else {
    1626             :                 /*
    1627             :                  * Otherwise, The SGL descriptor embedded in the command must point to the list of
    1628             :                  * SGL descriptors used to describe the operation. In that case it is a last segment descriptor.
    1629             :                  */
    1630           2 :                 uint32_t descriptors_size = sizeof(struct spdk_nvme_sgl_descriptor) * num_sgl_desc;
    1631             : 
    1632           2 :                 if (spdk_unlikely(descriptors_size > rqpair->qpair.ctrlr->ioccsz_bytes)) {
    1633           1 :                         SPDK_ERRLOG("Size of SGL descriptors (%u) exceeds ICD (%u)\n",
    1634             :                                     descriptors_size, rqpair->qpair.ctrlr->ioccsz_bytes);
    1635           1 :                         return -1;
    1636             :                 }
    1637           1 :                 rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd) + descriptors_size;
    1638             : 
    1639           1 :                 req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_LAST_SEGMENT;
    1640           1 :                 req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
    1641           1 :                 req->cmd.dptr.sgl1.unkeyed.length = descriptors_size;
    1642           1 :                 req->cmd.dptr.sgl1.address = (uint64_t)0;
    1643             :         }
    1644             : 
    1645           3 :         return 0;
    1646             : }
    1647             : 
    1648             : /*
    1649             :  * Build inline SGL describing sgl payload buffer.
    1650             :  */
    1651             : static inline int
    1652           3 : nvme_rdma_build_sgl_inline_request(struct nvme_rdma_qpair *rqpair,
    1653             :                                    struct spdk_nvme_rdma_req *rdma_req)
    1654             : {
    1655           3 :         struct nvme_request *req = rdma_req->req;
    1656           3 :         struct nvme_rdma_memory_translation_ctx ctx;
    1657           3 :         uint32_t length;
    1658             :         int rc;
    1659             : 
    1660           3 :         assert(req->payload_size != 0);
    1661           3 :         assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
    1662           3 :         assert(req->payload.reset_sgl_fn != NULL);
    1663           3 :         assert(req->payload.next_sge_fn != NULL);
    1664           3 :         req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
    1665             : 
    1666           3 :         rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &ctx.addr, &length);
    1667           3 :         if (spdk_unlikely(rc)) {
    1668           0 :                 return -1;
    1669             :         }
    1670             : 
    1671           3 :         if (length < req->payload_size) {
    1672           0 :                 SPDK_DEBUGLOG(nvme, "Inline SGL request split so sending separately.\n");
    1673           0 :                 return nvme_rdma_build_sgl_request(rqpair, rdma_req);
    1674             :         }
    1675             : 
    1676           3 :         if (length > req->payload_size) {
    1677           0 :                 length = req->payload_size;
    1678             :         }
    1679             : 
    1680           3 :         ctx.length = length;
    1681           3 :         rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
    1682           3 :         if (spdk_unlikely(rc)) {
    1683           0 :                 return -1;
    1684             :         }
    1685             : 
    1686           3 :         rdma_req->send_sgl[1].addr = (uint64_t)ctx.addr;
    1687           3 :         rdma_req->send_sgl[1].length = (uint32_t)ctx.length;
    1688           3 :         rdma_req->send_sgl[1].lkey = ctx.lkey;
    1689             : 
    1690           3 :         rdma_req->send_wr.num_sge = 2;
    1691             : 
    1692             :         /* The first element of this SGL is pointing at an
    1693             :          * spdk_nvmf_cmd object. For this particular command,
    1694             :          * we only need the first 64 bytes corresponding to
    1695             :          * the NVMe command. */
    1696           3 :         rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
    1697             : 
    1698           3 :         req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
    1699           3 :         req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
    1700           3 :         req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
    1701           3 :         req->cmd.dptr.sgl1.unkeyed.length = (uint32_t)ctx.length;
    1702             :         /* Inline only supported for icdoff == 0 currently.  This function will
    1703             :          * not get called for controllers with other values. */
    1704           3 :         req->cmd.dptr.sgl1.address = (uint64_t)0;
    1705             : 
    1706           3 :         return 0;
    1707             : }
    1708             : 
    1709             : static inline int
    1710           6 : nvme_rdma_req_init(struct nvme_rdma_qpair *rqpair, struct spdk_nvme_rdma_req *rdma_req)
    1711             : {
    1712           6 :         struct nvme_request *req = rdma_req->req;
    1713           6 :         struct spdk_nvme_ctrlr *ctrlr = rqpair->qpair.ctrlr;
    1714             :         enum nvme_payload_type payload_type;
    1715             :         bool icd_supported;
    1716           6 :         int rc = -1;
    1717             : 
    1718           6 :         payload_type = nvme_payload_type(&req->payload);
    1719             :         /*
    1720             :          * Check if icdoff is non zero, to avoid interop conflicts with
    1721             :          * targets with non-zero icdoff.  Both SPDK and the Linux kernel
    1722             :          * targets use icdoff = 0.  For targets with non-zero icdoff, we
    1723             :          * will currently just not use inline data for now.
    1724             :          */
    1725           6 :         icd_supported = spdk_nvme_opc_get_data_transfer(req->cmd.opc) == SPDK_NVME_DATA_HOST_TO_CONTROLLER
    1726           6 :                         && req->payload_size <= ctrlr->ioccsz_bytes && ctrlr->icdoff == 0;
    1727             : 
    1728           6 :         if (spdk_unlikely(req->payload_size == 0)) {
    1729           2 :                 rc = nvme_rdma_build_null_request(rdma_req);
    1730           4 :         } else if (payload_type == NVME_PAYLOAD_TYPE_CONTIG) {
    1731           2 :                 if (icd_supported) {
    1732           1 :                         rc = nvme_rdma_build_contig_inline_request(rqpair, rdma_req);
    1733             :                 } else {
    1734           1 :                         rc = nvme_rdma_build_contig_request(rqpair, rdma_req);
    1735             :                 }
    1736           2 :         } else if (payload_type == NVME_PAYLOAD_TYPE_SGL) {
    1737           2 :                 if (icd_supported) {
    1738           1 :                         rc = nvme_rdma_build_sgl_inline_request(rqpair, rdma_req);
    1739             :                 } else {
    1740           1 :                         rc = nvme_rdma_build_sgl_request(rqpair, rdma_req);
    1741             :                 }
    1742             :         }
    1743             : 
    1744           6 :         if (spdk_unlikely(rc)) {
    1745           0 :                 return rc;
    1746             :         }
    1747             : 
    1748           6 :         memcpy(&rqpair->cmds[rdma_req->id], &req->cmd, sizeof(req->cmd));
    1749           6 :         return 0;
    1750             : }
    1751             : 
    1752             : static struct spdk_nvme_qpair *
    1753           5 : nvme_rdma_ctrlr_create_qpair(struct spdk_nvme_ctrlr *ctrlr,
    1754             :                              uint16_t qid, uint32_t qsize,
    1755             :                              enum spdk_nvme_qprio qprio,
    1756             :                              uint32_t num_requests,
    1757             :                              bool delay_cmd_submit,
    1758             :                              bool async)
    1759             : {
    1760             :         struct nvme_rdma_qpair *rqpair;
    1761             :         struct spdk_nvme_qpair *qpair;
    1762             :         int rc;
    1763             : 
    1764           5 :         if (qsize < SPDK_NVME_QUEUE_MIN_ENTRIES) {
    1765           2 :                 SPDK_ERRLOG("Failed to create qpair with size %u. Minimum queue size is %d.\n",
    1766             :                             qsize, SPDK_NVME_QUEUE_MIN_ENTRIES);
    1767           2 :                 return NULL;
    1768             :         }
    1769             : 
    1770           3 :         rqpair = spdk_zmalloc(sizeof(struct nvme_rdma_qpair), 0, NULL, SPDK_ENV_NUMA_ID_ANY,
    1771             :                               SPDK_MALLOC_DMA);
    1772           3 :         if (!rqpair) {
    1773           0 :                 SPDK_ERRLOG("failed to get create rqpair\n");
    1774           0 :                 return NULL;
    1775             :         }
    1776             : 
    1777             :         /* Set num_entries one less than queue size. According to NVMe
    1778             :          * and NVMe-oF specs we can not submit queue size requests,
    1779             :          * one slot shall always remain empty.
    1780             :          */
    1781           3 :         rqpair->num_entries = qsize - 1;
    1782           3 :         rqpair->delay_cmd_submit = delay_cmd_submit;
    1783           3 :         rqpair->state = NVME_RDMA_QPAIR_STATE_INVALID;
    1784           3 :         qpair = &rqpair->qpair;
    1785           3 :         rc = nvme_qpair_init(qpair, qid, ctrlr, qprio, num_requests, async);
    1786           3 :         if (rc != 0) {
    1787           0 :                 spdk_free(rqpair);
    1788           0 :                 return NULL;
    1789             :         }
    1790             : 
    1791           3 :         return qpair;
    1792             : }
    1793             : 
    1794             : static void
    1795           1 : nvme_rdma_qpair_destroy(struct nvme_rdma_qpair *rqpair)
    1796             : {
    1797           1 :         struct spdk_nvme_qpair *qpair = &rqpair->qpair;
    1798             :         struct nvme_rdma_ctrlr *rctrlr;
    1799             :         struct nvme_rdma_cm_event_entry *entry, *tmp;
    1800             : 
    1801           1 :         spdk_rdma_utils_free_mem_map(&rqpair->mr_map);
    1802             : 
    1803           1 :         if (rqpair->evt) {
    1804           0 :                 rdma_ack_cm_event(rqpair->evt);
    1805           0 :                 rqpair->evt = NULL;
    1806             :         }
    1807             : 
    1808             :         /*
    1809             :          * This works because we have the controller lock both in
    1810             :          * this function and in the function where we add new events.
    1811             :          */
    1812           1 :         if (qpair->ctrlr != NULL) {
    1813           1 :                 rctrlr = nvme_rdma_ctrlr(qpair->ctrlr);
    1814           1 :                 STAILQ_FOREACH_SAFE(entry, &rctrlr->pending_cm_events, link, tmp) {
    1815           0 :                         if (entry->evt->id->context == rqpair) {
    1816           0 :                                 STAILQ_REMOVE(&rctrlr->pending_cm_events, entry, nvme_rdma_cm_event_entry, link);
    1817           0 :                                 rdma_ack_cm_event(entry->evt);
    1818           0 :                                 STAILQ_INSERT_HEAD(&rctrlr->free_cm_events, entry, link);
    1819             :                         }
    1820             :                 }
    1821             :         }
    1822             : 
    1823           1 :         if (rqpair->cm_id) {
    1824           0 :                 if (rqpair->rdma_qp) {
    1825           0 :                         spdk_rdma_utils_put_pd(rqpair->rdma_qp->qp->pd);
    1826           0 :                         spdk_rdma_provider_qp_destroy(rqpair->rdma_qp);
    1827           0 :                         rqpair->rdma_qp = NULL;
    1828             :                 }
    1829             :         }
    1830             : 
    1831           1 :         if (rqpair->poller) {
    1832             :                 struct nvme_rdma_poll_group     *group;
    1833             : 
    1834           0 :                 assert(qpair->poll_group);
    1835           0 :                 group = nvme_rdma_poll_group(qpair->poll_group);
    1836             : 
    1837           0 :                 nvme_rdma_poll_group_put_poller(group, rqpair->poller);
    1838             : 
    1839           0 :                 rqpair->poller = NULL;
    1840           0 :                 rqpair->cq = NULL;
    1841           0 :                 if (rqpair->srq) {
    1842           0 :                         rqpair->srq = NULL;
    1843           0 :                         rqpair->rsps = NULL;
    1844             :                 }
    1845           1 :         } else if (rqpair->cq) {
    1846           0 :                 ibv_destroy_cq(rqpair->cq);
    1847           0 :                 rqpair->cq = NULL;
    1848             :         }
    1849             : 
    1850           1 :         nvme_rdma_free_reqs(rqpair);
    1851           1 :         nvme_rdma_free_rsps(rqpair->rsps);
    1852           1 :         rqpair->rsps = NULL;
    1853             : 
    1854             :         /* destroy cm_id last so cma device will not be freed before we destroy the cq. */
    1855           1 :         if (rqpair->cm_id) {
    1856           0 :                 rdma_destroy_id(rqpair->cm_id);
    1857           0 :                 rqpair->cm_id = NULL;
    1858             :         }
    1859           1 : }
    1860             : 
    1861             : static void nvme_rdma_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr);
    1862             : 
    1863             : static int
    1864           1 : nvme_rdma_qpair_disconnected(struct nvme_rdma_qpair *rqpair, int ret)
    1865             : {
    1866           1 :         if (ret) {
    1867           0 :                 SPDK_DEBUGLOG(nvme, "Target did not respond to qpair disconnect.\n");
    1868           0 :                 goto quiet;
    1869             :         }
    1870             : 
    1871           1 :         if (rqpair->poller == NULL) {
    1872             :                 /* If poller is not used, cq is not shared.
    1873             :                  * So complete disconnecting qpair immediately.
    1874             :                  */
    1875           1 :                 goto quiet;
    1876             :         }
    1877             : 
    1878           0 :         if (rqpair->rsps == NULL) {
    1879           0 :                 goto quiet;
    1880             :         }
    1881             : 
    1882           0 :         if (rqpair->need_destroy ||
    1883           0 :             (rqpair->current_num_sends != 0 ||
    1884           0 :              (!rqpair->srq && rqpair->rsps->current_num_recvs != 0))) {
    1885           0 :                 rqpair->state = NVME_RDMA_QPAIR_STATE_LINGERING;
    1886           0 :                 rqpair->evt_timeout_ticks = (NVME_RDMA_DISCONNECTED_QPAIR_TIMEOUT_US * spdk_get_ticks_hz()) /
    1887           0 :                                             SPDK_SEC_TO_USEC + spdk_get_ticks();
    1888             : 
    1889           0 :                 return -EAGAIN;
    1890             :         }
    1891             : 
    1892           0 : quiet:
    1893           1 :         rqpair->state = NVME_RDMA_QPAIR_STATE_EXITED;
    1894             : 
    1895           1 :         nvme_rdma_qpair_abort_reqs(&rqpair->qpair, rqpair->qpair.abort_dnr);
    1896           1 :         nvme_rdma_qpair_destroy(rqpair);
    1897           1 :         nvme_transport_ctrlr_disconnect_qpair_done(&rqpair->qpair);
    1898             : 
    1899           1 :         return 0;
    1900             : }
    1901             : 
    1902             : static int
    1903           0 : nvme_rdma_qpair_wait_until_quiet(struct nvme_rdma_qpair *rqpair)
    1904             : {
    1905           0 :         struct spdk_nvme_qpair *qpair = &rqpair->qpair;
    1906           0 :         struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
    1907             : 
    1908           0 :         if (spdk_get_ticks() < rqpair->evt_timeout_ticks &&
    1909           0 :             (rqpair->current_num_sends != 0 ||
    1910           0 :              (!rqpair->srq && rqpair->rsps->current_num_recvs != 0))) {
    1911           0 :                 return -EAGAIN;
    1912             :         }
    1913             : 
    1914           0 :         rqpair->state = NVME_RDMA_QPAIR_STATE_EXITED;
    1915           0 :         nvme_rdma_qpair_abort_reqs(qpair, qpair->abort_dnr);
    1916           0 :         if (!nvme_qpair_is_admin_queue(qpair)) {
    1917           0 :                 nvme_robust_mutex_lock(&ctrlr->ctrlr_lock);
    1918             :         }
    1919           0 :         nvme_rdma_qpair_destroy(rqpair);
    1920           0 :         if (!nvme_qpair_is_admin_queue(qpair)) {
    1921           0 :                 nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
    1922             :         }
    1923           0 :         nvme_transport_ctrlr_disconnect_qpair_done(&rqpair->qpair);
    1924             : 
    1925           0 :         return 0;
    1926             : }
    1927             : 
    1928             : static void
    1929           0 : _nvme_rdma_ctrlr_disconnect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair,
    1930             :                                   nvme_rdma_cm_event_cb disconnected_qpair_cb)
    1931             : {
    1932           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    1933             :         int rc;
    1934             : 
    1935           0 :         assert(disconnected_qpair_cb != NULL);
    1936             : 
    1937           0 :         rqpair->state = NVME_RDMA_QPAIR_STATE_EXITING;
    1938             : 
    1939           0 :         if (rqpair->cm_id) {
    1940           0 :                 if (rqpair->rdma_qp) {
    1941           0 :                         rc = spdk_rdma_provider_qp_disconnect(rqpair->rdma_qp);
    1942           0 :                         if ((qpair->ctrlr != NULL) && (rc == 0)) {
    1943           0 :                                 rc = nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_DISCONNECTED,
    1944             :                                                                    disconnected_qpair_cb);
    1945           0 :                                 if (rc == 0) {
    1946           0 :                                         return;
    1947             :                                 }
    1948             :                         }
    1949             :                 }
    1950             :         }
    1951             : 
    1952           0 :         disconnected_qpair_cb(rqpair, 0);
    1953             : }
    1954             : 
    1955             : static int
    1956           0 : nvme_rdma_ctrlr_disconnect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
    1957             : {
    1958           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    1959             :         int rc;
    1960             : 
    1961           0 :         switch (rqpair->state) {
    1962           0 :         case NVME_RDMA_QPAIR_STATE_EXITING:
    1963           0 :                 if (!nvme_qpair_is_admin_queue(qpair)) {
    1964           0 :                         nvme_ctrlr_lock(ctrlr);
    1965             :                 }
    1966             : 
    1967           0 :                 rc = nvme_rdma_process_event_poll(rqpair);
    1968             : 
    1969           0 :                 if (!nvme_qpair_is_admin_queue(qpair)) {
    1970           0 :                         nvme_ctrlr_unlock(ctrlr);
    1971             :                 }
    1972           0 :                 break;
    1973             : 
    1974           0 :         case NVME_RDMA_QPAIR_STATE_LINGERING:
    1975           0 :                 rc = nvme_rdma_qpair_wait_until_quiet(rqpair);
    1976           0 :                 break;
    1977           0 :         case NVME_RDMA_QPAIR_STATE_EXITED:
    1978           0 :                 rc = 0;
    1979           0 :                 break;
    1980             : 
    1981           0 :         default:
    1982           0 :                 assert(false);
    1983             :                 rc = -EAGAIN;
    1984             :                 break;
    1985             :         }
    1986             : 
    1987           0 :         return rc;
    1988             : }
    1989             : 
    1990             : static void
    1991           0 : nvme_rdma_ctrlr_disconnect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
    1992             : {
    1993             :         int rc;
    1994             : 
    1995           0 :         _nvme_rdma_ctrlr_disconnect_qpair(ctrlr, qpair, nvme_rdma_qpair_disconnected);
    1996             : 
    1997             :         /* If the async mode is disabled, poll the qpair until it is actually disconnected.
    1998             :          * It is ensured that poll_group_process_completions() calls disconnected_qpair_cb
    1999             :          * for any disconnected qpair. Hence, we do not have to check if the qpair is in
    2000             :          * a poll group or not.
    2001             :          * At the same time, if the qpair is being destroyed, i.e. this function is called by
    2002             :          * spdk_nvme_ctrlr_free_io_qpair then we need to wait until qpair is disconnected, otherwise
    2003             :          * we may leak some resources.
    2004             :          */
    2005           0 :         if (qpair->async && !qpair->destroy_in_progress) {
    2006           0 :                 return;
    2007             :         }
    2008             : 
    2009             :         while (1) {
    2010           0 :                 rc = nvme_rdma_ctrlr_disconnect_qpair_poll(ctrlr, qpair);
    2011           0 :                 if (rc != -EAGAIN) {
    2012           0 :                         break;
    2013             :                 }
    2014             :         }
    2015             : }
    2016             : 
    2017             : static int
    2018           0 : nvme_rdma_stale_conn_disconnected(struct nvme_rdma_qpair *rqpair, int ret)
    2019             : {
    2020           0 :         struct spdk_nvme_qpair *qpair = &rqpair->qpair;
    2021             : 
    2022           0 :         if (ret) {
    2023           0 :                 SPDK_DEBUGLOG(nvme, "Target did not respond to qpair disconnect.\n");
    2024             :         }
    2025             : 
    2026           0 :         nvme_rdma_qpair_destroy(rqpair);
    2027             : 
    2028           0 :         qpair->last_transport_failure_reason = qpair->transport_failure_reason;
    2029           0 :         qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_NONE;
    2030             : 
    2031           0 :         rqpair->state = NVME_RDMA_QPAIR_STATE_STALE_CONN;
    2032           0 :         rqpair->evt_timeout_ticks = (NVME_RDMA_STALE_CONN_RETRY_DELAY_US * spdk_get_ticks_hz()) /
    2033           0 :                                     SPDK_SEC_TO_USEC + spdk_get_ticks();
    2034             : 
    2035           0 :         return 0;
    2036             : }
    2037             : 
    2038             : static int
    2039           0 : nvme_rdma_stale_conn_retry(struct nvme_rdma_qpair *rqpair)
    2040             : {
    2041           0 :         struct spdk_nvme_qpair *qpair = &rqpair->qpair;
    2042             : 
    2043           0 :         if (rqpair->stale_conn_retry_count >= NVME_RDMA_STALE_CONN_RETRY_MAX) {
    2044           0 :                 SPDK_ERRLOG("Retry failed %d times, give up stale connection to qpair (cntlid:%u, qid:%u).\n",
    2045             :                             NVME_RDMA_STALE_CONN_RETRY_MAX, qpair->ctrlr->cntlid, qpair->id);
    2046           0 :                 return -ESTALE;
    2047             :         }
    2048             : 
    2049           0 :         rqpair->stale_conn_retry_count++;
    2050             : 
    2051           0 :         SPDK_NOTICELOG("%d times, retry stale connection to qpair (cntlid:%u, qid:%u).\n",
    2052             :                        rqpair->stale_conn_retry_count, qpair->ctrlr->cntlid, qpair->id);
    2053             : 
    2054           0 :         _nvme_rdma_ctrlr_disconnect_qpair(qpair->ctrlr, qpair, nvme_rdma_stale_conn_disconnected);
    2055             : 
    2056           0 :         return 0;
    2057             : }
    2058             : 
    2059             : static int
    2060           1 : nvme_rdma_ctrlr_delete_io_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
    2061             : {
    2062             :         struct nvme_rdma_qpair *rqpair;
    2063             : 
    2064           1 :         assert(qpair != NULL);
    2065           1 :         rqpair = nvme_rdma_qpair(qpair);
    2066             : 
    2067           1 :         if (rqpair->state != NVME_RDMA_QPAIR_STATE_EXITED) {
    2068             :                 int rc __attribute__((unused));
    2069             : 
    2070             :                 /* qpair was removed from the poll group while the disconnect is not finished.
    2071             :                  * Destroy rdma resources forcefully. */
    2072           1 :                 rc = nvme_rdma_qpair_disconnected(rqpair, 0);
    2073           1 :                 assert(rc == 0);
    2074             :         }
    2075             : 
    2076           1 :         nvme_rdma_qpair_abort_reqs(qpair, qpair->abort_dnr);
    2077           1 :         nvme_qpair_deinit(qpair);
    2078             : 
    2079           1 :         spdk_free(rqpair);
    2080             : 
    2081           1 :         return 0;
    2082             : }
    2083             : 
    2084             : static struct spdk_nvme_qpair *
    2085           0 : nvme_rdma_ctrlr_create_io_qpair(struct spdk_nvme_ctrlr *ctrlr, uint16_t qid,
    2086             :                                 const struct spdk_nvme_io_qpair_opts *opts)
    2087             : {
    2088           0 :         return nvme_rdma_ctrlr_create_qpair(ctrlr, qid, opts->io_queue_size, opts->qprio,
    2089           0 :                                             opts->io_queue_requests,
    2090           0 :                                             opts->delay_cmd_submit,
    2091           0 :                                             opts->async_mode);
    2092             : }
    2093             : 
    2094             : static int
    2095           0 : nvme_rdma_ctrlr_enable(struct spdk_nvme_ctrlr *ctrlr)
    2096             : {
    2097             :         /* do nothing here */
    2098           0 :         return 0;
    2099             : }
    2100             : 
    2101             : static int nvme_rdma_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr);
    2102             : 
    2103             : /* We have to use the typedef in the function declaration to appease astyle. */
    2104             : typedef struct spdk_nvme_ctrlr spdk_nvme_ctrlr_t;
    2105             : 
    2106             : static spdk_nvme_ctrlr_t *
    2107           1 : nvme_rdma_ctrlr_construct(const struct spdk_nvme_transport_id *trid,
    2108             :                           const struct spdk_nvme_ctrlr_opts *opts,
    2109             :                           void *devhandle)
    2110             : {
    2111             :         struct nvme_rdma_ctrlr *rctrlr;
    2112             :         struct ibv_context **contexts;
    2113           1 :         struct ibv_device_attr dev_attr;
    2114             :         int i, flag, rc;
    2115             : 
    2116           1 :         rctrlr = spdk_zmalloc(sizeof(struct nvme_rdma_ctrlr), 0, NULL, SPDK_ENV_NUMA_ID_ANY,
    2117             :                               SPDK_MALLOC_DMA);
    2118           1 :         if (rctrlr == NULL) {
    2119           0 :                 SPDK_ERRLOG("could not allocate ctrlr\n");
    2120           0 :                 return NULL;
    2121             :         }
    2122             : 
    2123           1 :         rctrlr->ctrlr.opts = *opts;
    2124           1 :         rctrlr->ctrlr.trid = *trid;
    2125             : 
    2126           1 :         if (opts->transport_retry_count > NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT) {
    2127           1 :                 SPDK_NOTICELOG("transport_retry_count exceeds max value %d, use max value\n",
    2128             :                                NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT);
    2129           1 :                 rctrlr->ctrlr.opts.transport_retry_count = NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT;
    2130             :         }
    2131             : 
    2132           1 :         if (opts->transport_ack_timeout > NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT) {
    2133           1 :                 SPDK_NOTICELOG("transport_ack_timeout exceeds max value %d, use max value\n",
    2134             :                                NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT);
    2135           1 :                 rctrlr->ctrlr.opts.transport_ack_timeout = NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT;
    2136             :         }
    2137             : 
    2138           1 :         contexts = rdma_get_devices(NULL);
    2139           1 :         if (contexts == NULL) {
    2140           0 :                 SPDK_ERRLOG("rdma_get_devices() failed: %s (%d)\n", spdk_strerror(errno), errno);
    2141           0 :                 spdk_free(rctrlr);
    2142           0 :                 return NULL;
    2143             :         }
    2144             : 
    2145           1 :         i = 0;
    2146           1 :         rctrlr->max_sge = NVME_RDMA_MAX_SGL_DESCRIPTORS;
    2147             : 
    2148           3 :         while (contexts[i] != NULL) {
    2149           2 :                 rc = ibv_query_device(contexts[i], &dev_attr);
    2150           2 :                 if (rc < 0) {
    2151           0 :                         SPDK_ERRLOG("Failed to query RDMA device attributes.\n");
    2152           0 :                         rdma_free_devices(contexts);
    2153           0 :                         spdk_free(rctrlr);
    2154           0 :                         return NULL;
    2155             :                 }
    2156           2 :                 rctrlr->max_sge = spdk_min(rctrlr->max_sge, (uint16_t)dev_attr.max_sge);
    2157           2 :                 i++;
    2158             :         }
    2159             : 
    2160           1 :         rdma_free_devices(contexts);
    2161             : 
    2162           1 :         rc = nvme_ctrlr_construct(&rctrlr->ctrlr);
    2163           1 :         if (rc != 0) {
    2164           0 :                 spdk_free(rctrlr);
    2165           0 :                 return NULL;
    2166             :         }
    2167             : 
    2168           1 :         STAILQ_INIT(&rctrlr->pending_cm_events);
    2169           1 :         STAILQ_INIT(&rctrlr->free_cm_events);
    2170           1 :         rctrlr->cm_events = spdk_zmalloc(NVME_RDMA_NUM_CM_EVENTS * sizeof(*rctrlr->cm_events), 0, NULL,
    2171             :                                          SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
    2172           1 :         if (rctrlr->cm_events == NULL) {
    2173           0 :                 SPDK_ERRLOG("unable to allocate buffers to hold CM events.\n");
    2174           0 :                 goto destruct_ctrlr;
    2175             :         }
    2176             : 
    2177         257 :         for (i = 0; i < NVME_RDMA_NUM_CM_EVENTS; i++) {
    2178         256 :                 STAILQ_INSERT_TAIL(&rctrlr->free_cm_events, &rctrlr->cm_events[i], link);
    2179             :         }
    2180             : 
    2181           1 :         rctrlr->cm_channel = rdma_create_event_channel();
    2182           1 :         if (rctrlr->cm_channel == NULL) {
    2183           0 :                 SPDK_ERRLOG("rdma_create_event_channel() failed\n");
    2184           0 :                 goto destruct_ctrlr;
    2185             :         }
    2186             : 
    2187           1 :         flag = fcntl(rctrlr->cm_channel->fd, F_GETFL);
    2188           1 :         if (fcntl(rctrlr->cm_channel->fd, F_SETFL, flag | O_NONBLOCK) < 0) {
    2189           0 :                 SPDK_ERRLOG("Cannot set event channel to non blocking\n");
    2190           0 :                 goto destruct_ctrlr;
    2191             :         }
    2192             : 
    2193           1 :         rctrlr->ctrlr.adminq = nvme_rdma_ctrlr_create_qpair(&rctrlr->ctrlr, 0,
    2194           1 :                                rctrlr->ctrlr.opts.admin_queue_size, 0,
    2195           1 :                                rctrlr->ctrlr.opts.admin_queue_size, false, true);
    2196           1 :         if (!rctrlr->ctrlr.adminq) {
    2197           0 :                 SPDK_ERRLOG("failed to create admin qpair\n");
    2198           0 :                 goto destruct_ctrlr;
    2199             :         }
    2200             : 
    2201           1 :         if (nvme_ctrlr_add_process(&rctrlr->ctrlr, 0) != 0) {
    2202           0 :                 SPDK_ERRLOG("nvme_ctrlr_add_process() failed\n");
    2203           0 :                 goto destruct_ctrlr;
    2204             :         }
    2205             : 
    2206           1 :         SPDK_DEBUGLOG(nvme, "successfully initialized the nvmf ctrlr\n");
    2207           1 :         return &rctrlr->ctrlr;
    2208             : 
    2209           0 : destruct_ctrlr:
    2210           0 :         nvme_ctrlr_destruct(&rctrlr->ctrlr);
    2211           0 :         return NULL;
    2212             : }
    2213             : 
    2214             : static int
    2215           1 : nvme_rdma_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr)
    2216             : {
    2217           1 :         struct nvme_rdma_ctrlr *rctrlr = nvme_rdma_ctrlr(ctrlr);
    2218             :         struct nvme_rdma_cm_event_entry *entry;
    2219             : 
    2220           1 :         if (ctrlr->adminq) {
    2221           1 :                 nvme_rdma_ctrlr_delete_io_qpair(ctrlr, ctrlr->adminq);
    2222             :         }
    2223             : 
    2224           1 :         STAILQ_FOREACH(entry, &rctrlr->pending_cm_events, link) {
    2225           0 :                 rdma_ack_cm_event(entry->evt);
    2226             :         }
    2227             : 
    2228           1 :         STAILQ_INIT(&rctrlr->free_cm_events);
    2229           1 :         STAILQ_INIT(&rctrlr->pending_cm_events);
    2230           1 :         spdk_free(rctrlr->cm_events);
    2231             : 
    2232           1 :         if (rctrlr->cm_channel) {
    2233           1 :                 rdma_destroy_event_channel(rctrlr->cm_channel);
    2234           1 :                 rctrlr->cm_channel = NULL;
    2235             :         }
    2236             : 
    2237           1 :         nvme_ctrlr_destruct_finish(ctrlr);
    2238             : 
    2239           1 :         spdk_free(rctrlr);
    2240             : 
    2241           1 :         return 0;
    2242             : }
    2243             : 
    2244             : static inline int
    2245           1 : _nvme_rdma_qpair_submit_request(struct nvme_rdma_qpair *rqpair,
    2246             :                                 struct spdk_nvme_rdma_req *rdma_req)
    2247             : {
    2248           1 :         struct spdk_nvme_qpair *qpair = &rqpair->qpair;
    2249             :         struct ibv_send_wr *wr;
    2250             :         struct nvme_rdma_poll_group *group;
    2251             : 
    2252           1 :         if (!rqpair->link_active.tqe_prev && qpair->poll_group) {
    2253           0 :                 group = nvme_rdma_poll_group(qpair->poll_group);
    2254           0 :                 TAILQ_INSERT_TAIL(&group->active_qpairs, rqpair, link_active);
    2255             :         }
    2256           1 :         assert(rqpair->current_num_sends < rqpair->num_entries);
    2257           1 :         rqpair->current_num_sends++;
    2258             : 
    2259           1 :         wr = &rdma_req->send_wr;
    2260           1 :         wr->next = NULL;
    2261           1 :         nvme_rdma_trace_ibv_sge(wr->sg_list);
    2262             : 
    2263           1 :         spdk_rdma_provider_qp_queue_send_wrs(rqpair->rdma_qp, wr);
    2264             : 
    2265           1 :         if (!rqpair->delay_cmd_submit) {
    2266           1 :                 return nvme_rdma_qpair_submit_sends(rqpair);
    2267             :         }
    2268             : 
    2269           0 :         return 0;
    2270             : }
    2271             : 
    2272             : static int
    2273           2 : nvme_rdma_qpair_submit_request(struct spdk_nvme_qpair *qpair,
    2274             :                                struct nvme_request *req)
    2275             : {
    2276             :         struct nvme_rdma_qpair *rqpair;
    2277             :         struct spdk_nvme_rdma_req *rdma_req;
    2278             :         int rc;
    2279             : 
    2280           2 :         rqpair = nvme_rdma_qpair(qpair);
    2281           2 :         assert(rqpair != NULL);
    2282           2 :         assert(req != NULL);
    2283             : 
    2284           2 :         rdma_req = nvme_rdma_req_get(rqpair);
    2285           2 :         if (spdk_unlikely(!rdma_req)) {
    2286           1 :                 if (rqpair->poller) {
    2287           1 :                         rqpair->poller->stats.queued_requests++;
    2288             :                 }
    2289             :                 /* Inform the upper layer to try again later. */
    2290           1 :                 return -EAGAIN;
    2291             :         }
    2292             : 
    2293           1 :         assert(rdma_req->req == NULL);
    2294           1 :         rdma_req->req = req;
    2295           1 :         req->cmd.cid = rdma_req->id;
    2296           1 :         rc = nvme_rdma_req_init(rqpair, rdma_req);
    2297           1 :         if (spdk_unlikely(rc)) {
    2298           0 :                 SPDK_ERRLOG("nvme_rdma_req_init() failed\n");
    2299           0 :                 nvme_rdma_req_put(rqpair, rdma_req);
    2300           0 :                 return -1;
    2301             :         }
    2302             : 
    2303           1 :         TAILQ_INSERT_TAIL(&rqpair->outstanding_reqs, rdma_req, link);
    2304           1 :         rqpair->num_outstanding_reqs++;
    2305             : 
    2306           1 :         return _nvme_rdma_qpair_submit_request(rqpair, rdma_req);
    2307             : }
    2308             : 
    2309             : static int
    2310           0 : nvme_rdma_qpair_reset(struct spdk_nvme_qpair *qpair)
    2311             : {
    2312             :         /* Currently, doing nothing here */
    2313           0 :         return 0;
    2314             : }
    2315             : 
    2316             : static void
    2317           2 : nvme_rdma_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr)
    2318             : {
    2319             :         struct spdk_nvme_rdma_req *rdma_req, *tmp;
    2320           2 :         struct spdk_nvme_cpl cpl;
    2321           2 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    2322             : 
    2323           2 :         cpl.sqid = qpair->id;
    2324           2 :         cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
    2325           2 :         cpl.status.sct = SPDK_NVME_SCT_GENERIC;
    2326           2 :         cpl.status.dnr = dnr;
    2327             : 
    2328             :         /*
    2329             :          * We cannot abort requests at the RDMA layer without
    2330             :          * unregistering them. If we do, we can still get error
    2331             :          * free completions on the shared completion queue.
    2332             :          */
    2333           2 :         if (nvme_qpair_get_state(qpair) > NVME_QPAIR_DISCONNECTING &&
    2334           0 :             nvme_qpair_get_state(qpair) != NVME_QPAIR_DESTROYING) {
    2335           0 :                 nvme_ctrlr_disconnect_qpair(qpair);
    2336             :         }
    2337             : 
    2338           2 :         TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
    2339           0 :                 nvme_rdma_req_complete(rdma_req, &cpl, true);
    2340             :         }
    2341           2 : }
    2342             : 
    2343             : static void
    2344           0 : nvme_rdma_qpair_check_timeout(struct spdk_nvme_qpair *qpair)
    2345             : {
    2346             :         uint64_t t02;
    2347             :         struct spdk_nvme_rdma_req *rdma_req, *tmp;
    2348           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    2349           0 :         struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
    2350             :         struct spdk_nvme_ctrlr_process *active_proc;
    2351             : 
    2352             :         /* Don't check timeouts during controller initialization. */
    2353           0 :         if (ctrlr->state != NVME_CTRLR_STATE_READY) {
    2354           0 :                 return;
    2355             :         }
    2356             : 
    2357           0 :         if (nvme_qpair_is_admin_queue(qpair)) {
    2358           0 :                 active_proc = nvme_ctrlr_get_current_process(ctrlr);
    2359             :         } else {
    2360           0 :                 active_proc = qpair->active_proc;
    2361             :         }
    2362             : 
    2363             :         /* Only check timeouts if the current process has a timeout callback. */
    2364           0 :         if (active_proc == NULL || active_proc->timeout_cb_fn == NULL) {
    2365           0 :                 return;
    2366             :         }
    2367             : 
    2368           0 :         t02 = spdk_get_ticks();
    2369           0 :         TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
    2370           0 :                 assert(rdma_req->req != NULL);
    2371             : 
    2372           0 :                 if (nvme_request_check_timeout(rdma_req->req, rdma_req->id, active_proc, t02)) {
    2373             :                         /*
    2374             :                          * The requests are in order, so as soon as one has not timed out,
    2375             :                          * stop iterating.
    2376             :                          */
    2377           0 :                         break;
    2378             :                 }
    2379             :         }
    2380             : }
    2381             : 
    2382             : static inline void
    2383           0 : nvme_rdma_request_ready(struct nvme_rdma_qpair *rqpair, struct spdk_nvme_rdma_req *rdma_req)
    2384             : {
    2385           0 :         struct spdk_nvme_rdma_rsp *rdma_rsp = rdma_req->rdma_rsp;
    2386           0 :         struct ibv_recv_wr *recv_wr = rdma_rsp->recv_wr;
    2387             : 
    2388           0 :         nvme_rdma_req_complete(rdma_req, &rdma_rsp->cpl, true);
    2389             : 
    2390           0 :         assert(rqpair->rsps->current_num_recvs < rqpair->rsps->num_entries);
    2391           0 :         rqpair->rsps->current_num_recvs++;
    2392             : 
    2393           0 :         recv_wr->next = NULL;
    2394           0 :         nvme_rdma_trace_ibv_sge(recv_wr->sg_list);
    2395             : 
    2396           0 :         if (!rqpair->srq) {
    2397           0 :                 spdk_rdma_provider_qp_queue_recv_wrs(rqpair->rdma_qp, recv_wr);
    2398             :         } else {
    2399           0 :                 spdk_rdma_provider_srq_queue_recv_wrs(rqpair->srq, recv_wr);
    2400             :         }
    2401           0 : }
    2402             : 
    2403             : #define MAX_COMPLETIONS_PER_POLL 128
    2404             : 
    2405             : static void
    2406           0 : nvme_rdma_fail_qpair(struct spdk_nvme_qpair *qpair, int failure_reason)
    2407             : {
    2408           0 :         if (failure_reason == IBV_WC_RETRY_EXC_ERR) {
    2409           0 :                 qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_REMOTE;
    2410           0 :         } else if (qpair->transport_failure_reason == SPDK_NVME_QPAIR_FAILURE_NONE) {
    2411           0 :                 qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_UNKNOWN;
    2412             :         }
    2413             : 
    2414           0 :         nvme_ctrlr_disconnect_qpair(qpair);
    2415           0 : }
    2416             : 
    2417             : static struct nvme_rdma_qpair *
    2418           4 : get_rdma_qpair_from_wc(struct nvme_rdma_poll_group *group, struct ibv_wc *wc)
    2419             : {
    2420             :         struct spdk_nvme_qpair *qpair;
    2421             :         struct nvme_rdma_qpair *rqpair;
    2422             : 
    2423           5 :         STAILQ_FOREACH(qpair, &group->group.connected_qpairs, poll_group_stailq) {
    2424           2 :                 rqpair = nvme_rdma_qpair(qpair);
    2425           2 :                 if (NVME_RDMA_POLL_GROUP_CHECK_QPN(rqpair, wc->qp_num)) {
    2426           1 :                         return rqpair;
    2427             :                 }
    2428             :         }
    2429             : 
    2430           4 :         STAILQ_FOREACH(qpair, &group->group.disconnected_qpairs, poll_group_stailq) {
    2431           2 :                 rqpair = nvme_rdma_qpair(qpair);
    2432           2 :                 if (NVME_RDMA_POLL_GROUP_CHECK_QPN(rqpair, wc->qp_num)) {
    2433           1 :                         return rqpair;
    2434             :                 }
    2435             :         }
    2436             : 
    2437           2 :         return NULL;
    2438             : }
    2439             : 
    2440             : static inline void
    2441           0 : nvme_rdma_log_wc_status(struct nvme_rdma_qpair *rqpair, struct ibv_wc *wc)
    2442             : {
    2443           0 :         struct nvme_rdma_wr *rdma_wr = (struct nvme_rdma_wr *)wc->wr_id;
    2444             : 
    2445           0 :         if (wc->status == IBV_WC_WR_FLUSH_ERR) {
    2446             :                 /* If qpair is in ERR state, we will receive completions for all posted and not completed
    2447             :                  * Work Requests with IBV_WC_WR_FLUSH_ERR status. Don't log an error in that case */
    2448           0 :                 SPDK_DEBUGLOG(nvme, "WC error, qid %u, qp state %d, request 0x%lu type %d, status: (%d): %s\n",
    2449             :                               rqpair->qpair.id, rqpair->qpair.state, wc->wr_id, rdma_wr->type, wc->status,
    2450             :                               ibv_wc_status_str(wc->status));
    2451             :         } else {
    2452           0 :                 SPDK_ERRLOG("WC error, qid %u, qp state %d, request 0x%lu type %d, status: (%d): %s\n",
    2453             :                             rqpair->qpair.id, rqpair->qpair.state, wc->wr_id, rdma_wr->type, wc->status,
    2454             :                             ibv_wc_status_str(wc->status));
    2455             :         }
    2456           0 : }
    2457             : 
    2458             : static inline int
    2459           0 : nvme_rdma_process_recv_completion(struct nvme_rdma_poller *poller, struct ibv_wc *wc,
    2460             :                                   struct nvme_rdma_wr *rdma_wr)
    2461             : {
    2462             :         struct nvme_rdma_qpair          *rqpair;
    2463             :         struct spdk_nvme_rdma_req       *rdma_req;
    2464             :         struct spdk_nvme_rdma_rsp       *rdma_rsp;
    2465             : 
    2466           0 :         rdma_rsp = SPDK_CONTAINEROF(rdma_wr, struct spdk_nvme_rdma_rsp, rdma_wr);
    2467             : 
    2468           0 :         if (poller && poller->srq) {
    2469           0 :                 rqpair = get_rdma_qpair_from_wc(poller->group, wc);
    2470           0 :                 if (spdk_unlikely(!rqpair)) {
    2471             :                         /* Since we do not handle the LAST_WQE_REACHED event, we do not know when
    2472             :                          * a Receive Queue in a QP, that is associated with an SRQ, is flushed.
    2473             :                          * We may get a WC for a already destroyed QP.
    2474             :                          *
    2475             :                          * However, for the SRQ, this is not any error. Hence, just re-post the
    2476             :                          * receive request to the SRQ to reuse for other QPs, and return 0.
    2477             :                          */
    2478           0 :                         spdk_rdma_provider_srq_queue_recv_wrs(poller->srq, rdma_rsp->recv_wr);
    2479           0 :                         return 0;
    2480             :                 }
    2481             :         } else {
    2482           0 :                 rqpair = rdma_rsp->rqpair;
    2483           0 :                 if (spdk_unlikely(!rqpair)) {
    2484             :                         /* TODO: Fix forceful QP destroy when it is not async mode.
    2485             :                          * CQ itself did not cause any error. Hence, return 0 for now.
    2486             :                          */
    2487           0 :                         SPDK_WARNLOG("QP might be already destroyed.\n");
    2488           0 :                         return 0;
    2489             :                 }
    2490             :         }
    2491             : 
    2492             : 
    2493           0 :         assert(rqpair->rsps->current_num_recvs > 0);
    2494           0 :         rqpair->rsps->current_num_recvs--;
    2495             : 
    2496           0 :         if (spdk_unlikely(wc->status)) {
    2497           0 :                 nvme_rdma_log_wc_status(rqpair, wc);
    2498           0 :                 goto err_wc;
    2499             :         }
    2500             : 
    2501           0 :         SPDK_DEBUGLOG(nvme, "CQ recv completion\n");
    2502             : 
    2503           0 :         if (spdk_unlikely(wc->byte_len < sizeof(struct spdk_nvme_cpl))) {
    2504           0 :                 SPDK_ERRLOG("recv length %u less than expected response size\n", wc->byte_len);
    2505           0 :                 goto err_wc;
    2506             :         }
    2507           0 :         rdma_req = &rqpair->rdma_reqs[rdma_rsp->cpl.cid];
    2508           0 :         rdma_req->completion_flags |= NVME_RDMA_RECV_COMPLETED;
    2509           0 :         rdma_req->rdma_rsp = rdma_rsp;
    2510             : 
    2511           0 :         if ((rdma_req->completion_flags & NVME_RDMA_SEND_COMPLETED) == 0) {
    2512           0 :                 return 0;
    2513             :         }
    2514             : 
    2515           0 :         rqpair->num_completions++;
    2516             : 
    2517           0 :         nvme_rdma_request_ready(rqpair, rdma_req);
    2518             : 
    2519           0 :         if (!rqpair->delay_cmd_submit) {
    2520           0 :                 if (spdk_unlikely(nvme_rdma_qpair_submit_recvs(rqpair))) {
    2521           0 :                         SPDK_ERRLOG("Unable to re-post rx descriptor\n");
    2522           0 :                         nvme_rdma_fail_qpair(&rqpair->qpair, 0);
    2523           0 :                         return -ENXIO;
    2524             :                 }
    2525             :         }
    2526             : 
    2527           0 :         return 1;
    2528             : 
    2529           0 : err_wc:
    2530           0 :         nvme_rdma_fail_qpair(&rqpair->qpair, 0);
    2531           0 :         if (poller && poller->srq) {
    2532           0 :                 spdk_rdma_provider_srq_queue_recv_wrs(poller->srq, rdma_rsp->recv_wr);
    2533             :         }
    2534           0 :         return -ENXIO;
    2535             : }
    2536             : 
    2537             : static inline int
    2538           0 : nvme_rdma_process_send_completion(struct nvme_rdma_poller *poller,
    2539             :                                   struct nvme_rdma_qpair *rdma_qpair,
    2540             :                                   struct ibv_wc *wc, struct nvme_rdma_wr *rdma_wr)
    2541             : {
    2542             :         struct nvme_rdma_qpair          *rqpair;
    2543             :         struct spdk_nvme_rdma_req       *rdma_req;
    2544             : 
    2545           0 :         rdma_req = SPDK_CONTAINEROF(rdma_wr, struct spdk_nvme_rdma_req, rdma_wr);
    2546           0 :         rqpair = rdma_req->req ? nvme_rdma_qpair(rdma_req->req->qpair) : NULL;
    2547           0 :         if (spdk_unlikely(!rqpair)) {
    2548           0 :                 rqpair = rdma_qpair != NULL ? rdma_qpair : get_rdma_qpair_from_wc(poller->group, wc);
    2549             :         }
    2550             : 
    2551             :         /* If we are flushing I/O */
    2552           0 :         if (spdk_unlikely(wc->status)) {
    2553           0 :                 if (!rqpair) {
    2554             :                         /* When poll_group is used, several qpairs share the same CQ and it is possible to
    2555             :                          * receive a completion with error (e.g. IBV_WC_WR_FLUSH_ERR) for already disconnected qpair
    2556             :                          * That happens due to qpair is destroyed while there are submitted but not completed send/receive
    2557             :                          * Work Requests */
    2558           0 :                         assert(poller);
    2559           0 :                         return 0;
    2560             :                 }
    2561           0 :                 assert(rqpair->current_num_sends > 0);
    2562           0 :                 rqpair->current_num_sends--;
    2563           0 :                 nvme_rdma_log_wc_status(rqpair, wc);
    2564           0 :                 nvme_rdma_fail_qpair(&rqpair->qpair, 0);
    2565           0 :                 if (rdma_req->rdma_rsp && poller && poller->srq) {
    2566           0 :                         spdk_rdma_provider_srq_queue_recv_wrs(poller->srq, rdma_req->rdma_rsp->recv_wr);
    2567             :                 }
    2568           0 :                 return -ENXIO;
    2569             :         }
    2570             : 
    2571             :         /* We do not support Soft Roce anymore. Other than Soft Roce's bug, we should not
    2572             :          * receive a completion without error status after qpair is disconnected/destroyed.
    2573             :          */
    2574           0 :         if (spdk_unlikely(rdma_req->req == NULL)) {
    2575             :                 /*
    2576             :                  * Some infiniband drivers do not guarantee the previous assumption after we
    2577             :                  * received a RDMA_CM_EVENT_DEVICE_REMOVAL event.
    2578             :                  */
    2579           0 :                 SPDK_ERRLOG("Received malformed completion: request 0x%"PRIx64" type %d\n", wc->wr_id,
    2580             :                             rdma_wr->type);
    2581           0 :                 if (!rqpair || !rqpair->need_destroy) {
    2582           0 :                         assert(0);
    2583             :                 }
    2584           0 :                 return -ENXIO;
    2585             :         }
    2586             : 
    2587           0 :         rdma_req->completion_flags |= NVME_RDMA_SEND_COMPLETED;
    2588           0 :         assert(rqpair->current_num_sends > 0);
    2589           0 :         rqpair->current_num_sends--;
    2590             : 
    2591           0 :         if ((rdma_req->completion_flags & NVME_RDMA_RECV_COMPLETED) == 0) {
    2592           0 :                 return 0;
    2593             :         }
    2594             : 
    2595           0 :         rqpair->num_completions++;
    2596             : 
    2597           0 :         nvme_rdma_request_ready(rqpair, rdma_req);
    2598             : 
    2599           0 :         if (!rqpair->delay_cmd_submit) {
    2600           0 :                 if (spdk_unlikely(nvme_rdma_qpair_submit_recvs(rqpair))) {
    2601           0 :                         SPDK_ERRLOG("Unable to re-post rx descriptor\n");
    2602           0 :                         nvme_rdma_fail_qpair(&rqpair->qpair, 0);
    2603           0 :                         return -ENXIO;
    2604             :                 }
    2605             :         }
    2606             : 
    2607           0 :         return 1;
    2608             : }
    2609             : 
    2610             : static inline int
    2611           0 : nvme_rdma_cq_process_completions(struct ibv_cq *cq, uint32_t batch_size,
    2612             :                                  struct nvme_rdma_poller *poller,
    2613             :                                  struct nvme_rdma_qpair *rdma_qpair,
    2614             :                                  uint64_t *rdma_completions)
    2615             : {
    2616           0 :         struct ibv_wc                   wc[MAX_COMPLETIONS_PER_POLL];
    2617             :         struct nvme_rdma_wr             *rdma_wr;
    2618           0 :         uint32_t                        reaped = 0;
    2619           0 :         int                             completion_rc = 0;
    2620             :         int                             rc, _rc, i;
    2621             : 
    2622           0 :         rc = ibv_poll_cq(cq, batch_size, wc);
    2623           0 :         if (spdk_unlikely(rc < 0)) {
    2624           0 :                 SPDK_ERRLOG("Error polling CQ! (%d): %s\n",
    2625             :                             errno, spdk_strerror(errno));
    2626           0 :                 return -ECANCELED;
    2627           0 :         } else if (rc == 0) {
    2628           0 :                 return 0;
    2629             :         }
    2630             : 
    2631           0 :         for (i = 0; i < rc; i++) {
    2632           0 :                 rdma_wr = (struct nvme_rdma_wr *)wc[i].wr_id;
    2633           0 :                 switch (rdma_wr->type) {
    2634           0 :                 case RDMA_WR_TYPE_RECV:
    2635           0 :                         _rc = nvme_rdma_process_recv_completion(poller, &wc[i], rdma_wr);
    2636           0 :                         break;
    2637             : 
    2638           0 :                 case RDMA_WR_TYPE_SEND:
    2639           0 :                         _rc = nvme_rdma_process_send_completion(poller, rdma_qpair, &wc[i], rdma_wr);
    2640           0 :                         break;
    2641             : 
    2642           0 :                 default:
    2643           0 :                         SPDK_ERRLOG("Received an unexpected opcode on the CQ: %d\n", rdma_wr->type);
    2644           0 :                         return -ECANCELED;
    2645             :                 }
    2646           0 :                 if (spdk_likely(_rc >= 0)) {
    2647           0 :                         reaped += _rc;
    2648             :                 } else {
    2649           0 :                         completion_rc = _rc;
    2650             :                 }
    2651             :         }
    2652             : 
    2653           0 :         *rdma_completions += rc;
    2654             : 
    2655           0 :         if (spdk_unlikely(completion_rc)) {
    2656           0 :                 return completion_rc;
    2657             :         }
    2658             : 
    2659           0 :         return reaped;
    2660             : }
    2661             : 
    2662             : static void
    2663           0 : dummy_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
    2664             : {
    2665             : 
    2666           0 : }
    2667             : 
    2668             : static int
    2669           0 : nvme_rdma_qpair_process_completions(struct spdk_nvme_qpair *qpair,
    2670             :                                     uint32_t max_completions)
    2671             : {
    2672           0 :         struct nvme_rdma_qpair          *rqpair = nvme_rdma_qpair(qpair);
    2673           0 :         struct nvme_rdma_ctrlr          *rctrlr = nvme_rdma_ctrlr(qpair->ctrlr);
    2674           0 :         int                             rc = 0, batch_size;
    2675             :         struct ibv_cq                   *cq;
    2676           0 :         uint64_t                        rdma_completions = 0;
    2677             : 
    2678             :         /*
    2679             :          * This is used during the connection phase. It's possible that we are still reaping error completions
    2680             :          * from other qpairs so we need to call the poll group function. Also, it's more correct since the cq
    2681             :          * is shared.
    2682             :          */
    2683           0 :         if (qpair->poll_group != NULL) {
    2684           0 :                 return spdk_nvme_poll_group_process_completions(qpair->poll_group->group, max_completions,
    2685             :                                 dummy_disconnected_qpair_cb);
    2686             :         }
    2687             : 
    2688           0 :         if (max_completions == 0) {
    2689           0 :                 max_completions = rqpair->num_entries;
    2690             :         } else {
    2691           0 :                 max_completions = spdk_min(max_completions, rqpair->num_entries);
    2692             :         }
    2693             : 
    2694           0 :         switch (nvme_qpair_get_state(qpair)) {
    2695           0 :         case NVME_QPAIR_CONNECTING:
    2696           0 :                 rc = nvme_rdma_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
    2697           0 :                 if (rc == 0) {
    2698             :                         /* Once the connection is completed, we can submit queued requests */
    2699           0 :                         nvme_qpair_resubmit_requests(qpair, rqpair->num_entries);
    2700           0 :                 } else if (rc != -EAGAIN) {
    2701           0 :                         SPDK_ERRLOG("Failed to connect rqpair=%p\n", rqpair);
    2702           0 :                         goto failed;
    2703           0 :                 } else if (rqpair->state <= NVME_RDMA_QPAIR_STATE_INITIALIZING) {
    2704           0 :                         return 0;
    2705             :                 }
    2706           0 :                 break;
    2707             : 
    2708           0 :         case NVME_QPAIR_DISCONNECTING:
    2709           0 :                 nvme_rdma_ctrlr_disconnect_qpair_poll(qpair->ctrlr, qpair);
    2710           0 :                 return -ENXIO;
    2711             : 
    2712           0 :         default:
    2713           0 :                 if (nvme_qpair_is_admin_queue(qpair)) {
    2714           0 :                         nvme_rdma_poll_events(rctrlr);
    2715             :                 }
    2716           0 :                 nvme_rdma_qpair_process_cm_event(rqpair);
    2717           0 :                 break;
    2718             :         }
    2719             : 
    2720           0 :         if (spdk_unlikely(qpair->transport_failure_reason != SPDK_NVME_QPAIR_FAILURE_NONE)) {
    2721           0 :                 goto failed;
    2722             :         }
    2723             : 
    2724           0 :         cq = rqpair->cq;
    2725             : 
    2726           0 :         rqpair->num_completions = 0;
    2727             :         do {
    2728           0 :                 batch_size = spdk_min((max_completions - rqpair->num_completions), MAX_COMPLETIONS_PER_POLL);
    2729           0 :                 rc = nvme_rdma_cq_process_completions(cq, batch_size, NULL, rqpair, &rdma_completions);
    2730             : 
    2731           0 :                 if (rc == 0) {
    2732           0 :                         break;
    2733             :                         /* Handle the case where we fail to poll the cq. */
    2734           0 :                 } else if (rc == -ECANCELED) {
    2735           0 :                         goto failed;
    2736           0 :                 } else if (rc == -ENXIO) {
    2737           0 :                         return rc;
    2738             :                 }
    2739           0 :         } while (rqpair->num_completions < max_completions);
    2740             : 
    2741           0 :         if (spdk_unlikely(nvme_rdma_qpair_submit_sends(rqpair) ||
    2742             :                           nvme_rdma_qpair_submit_recvs(rqpair))) {
    2743           0 :                 goto failed;
    2744             :         }
    2745             : 
    2746           0 :         if (spdk_unlikely(qpair->ctrlr->timeout_enabled)) {
    2747           0 :                 nvme_rdma_qpair_check_timeout(qpair);
    2748             :         }
    2749             : 
    2750           0 :         return rqpair->num_completions;
    2751             : 
    2752           0 : failed:
    2753           0 :         nvme_rdma_fail_qpair(qpair, 0);
    2754           0 :         return -ENXIO;
    2755             : }
    2756             : 
    2757             : static uint32_t
    2758           0 : nvme_rdma_ctrlr_get_max_xfer_size(struct spdk_nvme_ctrlr *ctrlr)
    2759             : {
    2760             :         /* max_mr_size by ibv_query_device indicates the largest value that we can
    2761             :          * set for a registered memory region.  It is independent from the actual
    2762             :          * I/O size and is very likely to be larger than 2 MiB which is the
    2763             :          * granularity we currently register memory regions.  Hence return
    2764             :          * UINT32_MAX here and let the generic layer use the controller data to
    2765             :          * moderate this value.
    2766             :          */
    2767           0 :         return UINT32_MAX;
    2768             : }
    2769             : 
    2770             : static uint16_t
    2771           5 : nvme_rdma_ctrlr_get_max_sges(struct spdk_nvme_ctrlr *ctrlr)
    2772             : {
    2773           5 :         struct nvme_rdma_ctrlr *rctrlr = nvme_rdma_ctrlr(ctrlr);
    2774           5 :         uint32_t max_sge = rctrlr->max_sge;
    2775           5 :         uint32_t max_in_capsule_sge = (ctrlr->cdata.nvmf_specific.ioccsz * 16 -
    2776           5 :                                        sizeof(struct spdk_nvme_cmd)) /
    2777             :                                       sizeof(struct spdk_nvme_sgl_descriptor);
    2778             : 
    2779             :         /* Max SGE is limited by capsule size */
    2780           5 :         max_sge = spdk_min(max_sge, max_in_capsule_sge);
    2781             :         /* Max SGE may be limited by MSDBD */
    2782           5 :         if (ctrlr->cdata.nvmf_specific.msdbd != 0) {
    2783           5 :                 max_sge = spdk_min(max_sge, ctrlr->cdata.nvmf_specific.msdbd);
    2784             :         }
    2785             : 
    2786             :         /* Max SGE can't be less than 1 */
    2787           5 :         max_sge = spdk_max(1, max_sge);
    2788           5 :         return max_sge;
    2789             : }
    2790             : 
    2791             : static int
    2792           0 : nvme_rdma_qpair_iterate_requests(struct spdk_nvme_qpair *qpair,
    2793             :                                  int (*iter_fn)(struct nvme_request *req, void *arg),
    2794             :                                  void *arg)
    2795             : {
    2796           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    2797             :         struct spdk_nvme_rdma_req *rdma_req, *tmp;
    2798             :         int rc;
    2799             : 
    2800           0 :         assert(iter_fn != NULL);
    2801             : 
    2802           0 :         TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
    2803           0 :                 assert(rdma_req->req != NULL);
    2804             : 
    2805           0 :                 rc = iter_fn(rdma_req->req, arg);
    2806           0 :                 if (rc != 0) {
    2807           0 :                         return rc;
    2808             :                 }
    2809             :         }
    2810             : 
    2811           0 :         return 0;
    2812             : }
    2813             : 
    2814             : static int
    2815           0 : nvme_rdma_qpair_authenticate(struct spdk_nvme_qpair *qpair)
    2816             : {
    2817           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    2818             :         int rc;
    2819             : 
    2820             :         /* If the qpair is still connecting, it'll be forced to authenticate later on */
    2821           0 :         if (rqpair->state < NVME_RDMA_QPAIR_STATE_RUNNING) {
    2822           0 :                 return 0;
    2823           0 :         } else if (rqpair->state != NVME_RDMA_QPAIR_STATE_RUNNING) {
    2824           0 :                 return -ENOTCONN;
    2825             :         }
    2826             : 
    2827           0 :         rc = nvme_fabric_qpair_authenticate_async(qpair);
    2828           0 :         if (rc == 0) {
    2829           0 :                 nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTING);
    2830           0 :                 rqpair->state = NVME_RDMA_QPAIR_STATE_AUTHENTICATING;
    2831             :         }
    2832             : 
    2833           0 :         return rc;
    2834             : }
    2835             : 
    2836             : static void
    2837           0 : nvme_rdma_admin_qpair_abort_aers(struct spdk_nvme_qpair *qpair)
    2838             : {
    2839             :         struct spdk_nvme_rdma_req *rdma_req, *tmp;
    2840           0 :         struct spdk_nvme_cpl cpl;
    2841           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    2842             : 
    2843           0 :         cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
    2844           0 :         cpl.status.sct = SPDK_NVME_SCT_GENERIC;
    2845             : 
    2846           0 :         TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
    2847           0 :                 assert(rdma_req->req != NULL);
    2848             : 
    2849           0 :                 if (rdma_req->req->cmd.opc != SPDK_NVME_OPC_ASYNC_EVENT_REQUEST) {
    2850           0 :                         continue;
    2851             :                 }
    2852             : 
    2853           0 :                 nvme_rdma_req_complete(rdma_req, &cpl, false);
    2854             :         }
    2855           0 : }
    2856             : 
    2857             : static void
    2858           9 : nvme_rdma_poller_destroy(struct nvme_rdma_poller *poller)
    2859             : {
    2860           9 :         if (poller->cq) {
    2861           7 :                 ibv_destroy_cq(poller->cq);
    2862             :         }
    2863           9 :         if (poller->rsps) {
    2864           0 :                 nvme_rdma_free_rsps(poller->rsps);
    2865             :         }
    2866           9 :         if (poller->srq) {
    2867           0 :                 spdk_rdma_provider_srq_destroy(poller->srq);
    2868             :         }
    2869           9 :         if (poller->mr_map) {
    2870           0 :                 spdk_rdma_utils_free_mem_map(&poller->mr_map);
    2871             :         }
    2872           9 :         if (poller->pd) {
    2873           0 :                 spdk_rdma_utils_put_pd(poller->pd);
    2874             :         }
    2875           9 :         free(poller);
    2876           9 : }
    2877             : 
    2878             : static struct nvme_rdma_poller *
    2879           9 : nvme_rdma_poller_create(struct nvme_rdma_poll_group *group, struct ibv_context *ctx)
    2880             : {
    2881             :         struct nvme_rdma_poller *poller;
    2882           9 :         struct ibv_device_attr dev_attr;
    2883           9 :         struct spdk_rdma_provider_srq_init_attr srq_init_attr = {};
    2884           9 :         struct nvme_rdma_rsp_opts opts;
    2885             :         int num_cqe, max_num_cqe;
    2886             :         int rc;
    2887             : 
    2888           9 :         poller = calloc(1, sizeof(*poller));
    2889           9 :         if (poller == NULL) {
    2890           0 :                 SPDK_ERRLOG("Unable to allocate poller.\n");
    2891           0 :                 return NULL;
    2892             :         }
    2893             : 
    2894           9 :         poller->group = group;
    2895           9 :         poller->device = ctx;
    2896             : 
    2897           9 :         if (g_spdk_nvme_transport_opts.rdma_srq_size != 0) {
    2898           0 :                 rc = ibv_query_device(ctx, &dev_attr);
    2899           0 :                 if (rc) {
    2900           0 :                         SPDK_ERRLOG("Unable to query RDMA device.\n");
    2901           0 :                         goto fail;
    2902             :                 }
    2903             : 
    2904           0 :                 poller->pd = spdk_rdma_utils_get_pd(ctx);
    2905           0 :                 if (poller->pd == NULL) {
    2906           0 :                         SPDK_ERRLOG("Unable to get PD.\n");
    2907           0 :                         goto fail;
    2908             :                 }
    2909             : 
    2910           0 :                 poller->mr_map = spdk_rdma_utils_create_mem_map(poller->pd, &g_nvme_hooks,
    2911             :                                  IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
    2912           0 :                 if (poller->mr_map == NULL) {
    2913           0 :                         SPDK_ERRLOG("Unable to create memory map.\n");
    2914           0 :                         goto fail;
    2915             :                 }
    2916             : 
    2917           0 :                 srq_init_attr.stats = &poller->stats.rdma_stats.recv;
    2918           0 :                 srq_init_attr.pd = poller->pd;
    2919           0 :                 srq_init_attr.srq_init_attr.attr.max_wr = spdk_min((uint32_t)dev_attr.max_srq_wr,
    2920             :                                 g_spdk_nvme_transport_opts.rdma_srq_size);
    2921           0 :                 srq_init_attr.srq_init_attr.attr.max_sge = spdk_min(dev_attr.max_sge,
    2922             :                                 NVME_RDMA_DEFAULT_RX_SGE);
    2923             : 
    2924           0 :                 poller->srq = spdk_rdma_provider_srq_create(&srq_init_attr);
    2925           0 :                 if (poller->srq == NULL) {
    2926           0 :                         SPDK_ERRLOG("Unable to create SRQ.\n");
    2927           0 :                         goto fail;
    2928             :                 }
    2929             : 
    2930           0 :                 opts.num_entries = g_spdk_nvme_transport_opts.rdma_srq_size;
    2931           0 :                 opts.rqpair = NULL;
    2932           0 :                 opts.srq = poller->srq;
    2933           0 :                 opts.mr_map = poller->mr_map;
    2934             : 
    2935           0 :                 poller->rsps = nvme_rdma_create_rsps(&opts);
    2936           0 :                 if (poller->rsps == NULL) {
    2937           0 :                         SPDK_ERRLOG("Unable to create poller RDMA responses.\n");
    2938           0 :                         goto fail;
    2939             :                 }
    2940             : 
    2941           0 :                 rc = nvme_rdma_poller_submit_recvs(poller);
    2942           0 :                 if (rc) {
    2943           0 :                         SPDK_ERRLOG("Unable to submit poller RDMA responses.\n");
    2944           0 :                         goto fail;
    2945             :                 }
    2946             : 
    2947             :                 /*
    2948             :                  * When using an srq, fix the size of the completion queue at startup.
    2949             :                  * The initiator sends only send and recv WRs. Hence, the multiplier is 2.
    2950             :                  * (The target sends also data WRs. Hence, the multiplier is 3.)
    2951             :                  */
    2952           0 :                 num_cqe = g_spdk_nvme_transport_opts.rdma_srq_size * 2;
    2953             :         } else {
    2954           9 :                 num_cqe = DEFAULT_NVME_RDMA_CQ_SIZE;
    2955             :         }
    2956             : 
    2957           9 :         max_num_cqe = g_spdk_nvme_transport_opts.rdma_max_cq_size;
    2958           9 :         if (max_num_cqe != 0 && num_cqe > max_num_cqe) {
    2959           0 :                 num_cqe = max_num_cqe;
    2960             :         }
    2961             : 
    2962           9 :         poller->cq = ibv_create_cq(poller->device, num_cqe, group, NULL, 0);
    2963             : 
    2964           9 :         if (poller->cq == NULL) {
    2965           2 :                 SPDK_ERRLOG("Unable to create CQ, errno %d.\n", errno);
    2966           2 :                 goto fail;
    2967             :         }
    2968             : 
    2969           7 :         STAILQ_INSERT_HEAD(&group->pollers, poller, link);
    2970           7 :         group->num_pollers++;
    2971           7 :         poller->current_num_wc = num_cqe;
    2972           7 :         poller->required_num_wc = 0;
    2973           7 :         return poller;
    2974             : 
    2975           2 : fail:
    2976           2 :         nvme_rdma_poller_destroy(poller);
    2977           2 :         return NULL;
    2978             : }
    2979             : 
    2980             : static void
    2981           3 : nvme_rdma_poll_group_free_pollers(struct nvme_rdma_poll_group *group)
    2982             : {
    2983             :         struct nvme_rdma_poller *poller, *tmp_poller;
    2984             : 
    2985           5 :         STAILQ_FOREACH_SAFE(poller, &group->pollers, link, tmp_poller) {
    2986           2 :                 assert(poller->refcnt == 0);
    2987           2 :                 if (poller->refcnt) {
    2988           0 :                         SPDK_WARNLOG("Destroying poller with non-zero ref count: poller %p, refcnt %d\n",
    2989             :                                      poller, poller->refcnt);
    2990             :                 }
    2991             : 
    2992           2 :                 STAILQ_REMOVE(&group->pollers, poller, nvme_rdma_poller, link);
    2993           2 :                 nvme_rdma_poller_destroy(poller);
    2994             :         }
    2995           3 : }
    2996             : 
    2997             : static struct nvme_rdma_poller *
    2998           8 : nvme_rdma_poll_group_get_poller(struct nvme_rdma_poll_group *group, struct ibv_context *device)
    2999             : {
    3000           8 :         struct nvme_rdma_poller *poller = NULL;
    3001             : 
    3002          10 :         STAILQ_FOREACH(poller, &group->pollers, link) {
    3003           3 :                 if (poller->device == device) {
    3004           1 :                         break;
    3005             :                 }
    3006             :         }
    3007             : 
    3008           8 :         if (!poller) {
    3009           7 :                 poller = nvme_rdma_poller_create(group, device);
    3010           7 :                 if (!poller) {
    3011           2 :                         SPDK_ERRLOG("Failed to create a poller for device %p\n", device);
    3012           2 :                         return NULL;
    3013             :                 }
    3014             :         }
    3015             : 
    3016           6 :         poller->refcnt++;
    3017           6 :         return poller;
    3018             : }
    3019             : 
    3020             : static void
    3021           6 : nvme_rdma_poll_group_put_poller(struct nvme_rdma_poll_group *group, struct nvme_rdma_poller *poller)
    3022             : {
    3023           6 :         assert(poller->refcnt > 0);
    3024           6 :         if (--poller->refcnt == 0) {
    3025           5 :                 STAILQ_REMOVE(&group->pollers, poller, nvme_rdma_poller, link);
    3026           5 :                 group->num_pollers--;
    3027           5 :                 nvme_rdma_poller_destroy(poller);
    3028             :         }
    3029           6 : }
    3030             : 
    3031             : static struct spdk_nvme_transport_poll_group *
    3032           1 : nvme_rdma_poll_group_create(void)
    3033             : {
    3034             :         struct nvme_rdma_poll_group     *group;
    3035             : 
    3036           1 :         group = calloc(1, sizeof(*group));
    3037           1 :         if (group == NULL) {
    3038           0 :                 SPDK_ERRLOG("Unable to allocate poll group.\n");
    3039           0 :                 return NULL;
    3040             :         }
    3041             : 
    3042           1 :         STAILQ_INIT(&group->pollers);
    3043           1 :         TAILQ_INIT(&group->connecting_qpairs);
    3044           1 :         TAILQ_INIT(&group->active_qpairs);
    3045           1 :         return &group->group;
    3046             : }
    3047             : 
    3048             : static int
    3049           0 : nvme_rdma_poll_group_connect_qpair(struct spdk_nvme_qpair *qpair)
    3050             : {
    3051           0 :         return 0;
    3052             : }
    3053             : 
    3054             : static int
    3055           0 : nvme_rdma_poll_group_disconnect_qpair(struct spdk_nvme_qpair *qpair)
    3056             : {
    3057           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    3058           0 :         struct nvme_rdma_poll_group *group = nvme_rdma_poll_group(qpair->poll_group);
    3059             : 
    3060           0 :         if (rqpair->link_connecting.tqe_prev) {
    3061           0 :                 TAILQ_REMOVE(&group->connecting_qpairs, rqpair, link_connecting);
    3062             :                 /* We use prev pointer to check if qpair is in connecting list or not .
    3063             :                  * TAILQ_REMOVE doesn't do it. So, we do it manually.
    3064             :                  */
    3065           0 :                 rqpair->link_connecting.tqe_prev = NULL;
    3066             :         }
    3067             : 
    3068           0 :         return 0;
    3069             : }
    3070             : 
    3071             : static int
    3072           0 : nvme_rdma_poll_group_add(struct spdk_nvme_transport_poll_group *tgroup,
    3073             :                          struct spdk_nvme_qpair *qpair)
    3074             : {
    3075           0 :         return 0;
    3076             : }
    3077             : 
    3078             : static int
    3079           0 : nvme_rdma_poll_group_remove(struct spdk_nvme_transport_poll_group *tgroup,
    3080             :                             struct spdk_nvme_qpair *qpair)
    3081             : {
    3082           0 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
    3083           0 :         struct nvme_rdma_poll_group *group = nvme_rdma_poll_group(qpair->poll_group);
    3084             : 
    3085           0 :         if (rqpair->link_active.tqe_prev) {
    3086           0 :                 TAILQ_REMOVE(&group->active_qpairs, rqpair, link_active);
    3087           0 :                 rqpair->link_active.tqe_prev = NULL;
    3088             :         }
    3089             : 
    3090           0 :         return 0;
    3091             : }
    3092             : 
    3093             : static inline void
    3094           0 : nvme_rdma_qpair_process_submits(struct nvme_rdma_poll_group *group,
    3095             :                                 struct nvme_rdma_qpair *rqpair)
    3096             : {
    3097           0 :         struct spdk_nvme_qpair  *qpair = &rqpair->qpair;
    3098             : 
    3099           0 :         assert(rqpair->link_active.tqe_prev != NULL);
    3100             : 
    3101           0 :         if (spdk_unlikely(rqpair->state <= NVME_RDMA_QPAIR_STATE_INITIALIZING ||
    3102             :                           rqpair->state >= NVME_RDMA_QPAIR_STATE_EXITING)) {
    3103           0 :                 return;
    3104             :         }
    3105             : 
    3106           0 :         if (spdk_unlikely(qpair->ctrlr->timeout_enabled)) {
    3107           0 :                 nvme_rdma_qpair_check_timeout(qpair);
    3108             :         }
    3109             : 
    3110           0 :         nvme_rdma_qpair_submit_sends(rqpair);
    3111           0 :         if (!rqpair->srq) {
    3112           0 :                 nvme_rdma_qpair_submit_recvs(rqpair);
    3113             :         }
    3114           0 :         if (rqpair->num_completions > 0) {
    3115           0 :                 nvme_qpair_resubmit_requests(qpair, rqpair->num_completions);
    3116           0 :                 rqpair->num_completions = 0;
    3117             :         }
    3118             : 
    3119           0 :         if (rqpair->num_outstanding_reqs == 0 && STAILQ_EMPTY(&qpair->queued_req)) {
    3120           0 :                 TAILQ_REMOVE(&group->active_qpairs, rqpair, link_active);
    3121             :                 /* We use prev pointer to check if qpair is in active list or not.
    3122             :                  * TAILQ_REMOVE doesn't do it. So, we do it manually.
    3123             :                  */
    3124           0 :                 rqpair->link_active.tqe_prev = NULL;
    3125             :         }
    3126             : }
    3127             : 
    3128             : static int64_t
    3129           0 : nvme_rdma_poll_group_process_completions(struct spdk_nvme_transport_poll_group *tgroup,
    3130             :                 uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
    3131             : {
    3132             :         struct spdk_nvme_qpair                  *qpair, *tmp_qpair;
    3133             :         struct nvme_rdma_qpair                  *rqpair, *tmp_rqpair;
    3134             :         struct nvme_rdma_poll_group             *group;
    3135             :         struct nvme_rdma_poller                 *poller;
    3136           0 :         int                                     batch_size, rc, rc2 = 0;
    3137           0 :         int64_t                                 total_completions = 0;
    3138           0 :         uint64_t                                completions_allowed = 0;
    3139           0 :         uint64_t                                completions_per_poller = 0;
    3140           0 :         uint64_t                                poller_completions = 0;
    3141           0 :         uint64_t                                rdma_completions;
    3142             : 
    3143           0 :         if (completions_per_qpair == 0) {
    3144           0 :                 completions_per_qpair = MAX_COMPLETIONS_PER_POLL;
    3145             :         }
    3146             : 
    3147           0 :         group = nvme_rdma_poll_group(tgroup);
    3148             : 
    3149           0 :         STAILQ_FOREACH_SAFE(qpair, &tgroup->disconnected_qpairs, poll_group_stailq, tmp_qpair) {
    3150           0 :                 rc = nvme_rdma_ctrlr_disconnect_qpair_poll(qpair->ctrlr, qpair);
    3151           0 :                 if (rc == 0) {
    3152           0 :                         disconnected_qpair_cb(qpair, tgroup->group->ctx);
    3153             :                 }
    3154             :         }
    3155             : 
    3156           0 :         TAILQ_FOREACH_SAFE(rqpair, &group->connecting_qpairs, link_connecting, tmp_rqpair) {
    3157           0 :                 qpair = &rqpair->qpair;
    3158             : 
    3159           0 :                 rc = nvme_rdma_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
    3160           0 :                 if (rc == 0 || rc != -EAGAIN) {
    3161           0 :                         TAILQ_REMOVE(&group->connecting_qpairs, rqpair, link_connecting);
    3162             :                         /* We use prev pointer to check if qpair is in connecting list or not.
    3163             :                          * TAILQ_REMOVE does not do it. So, we do it manually.
    3164             :                          */
    3165           0 :                         rqpair->link_connecting.tqe_prev = NULL;
    3166             : 
    3167           0 :                         if (rc == 0) {
    3168             :                                 /* Once the connection is completed, we can submit queued requests */
    3169           0 :                                 nvme_qpair_resubmit_requests(qpair, rqpair->num_entries);
    3170           0 :                         } else if (rc != -EAGAIN) {
    3171           0 :                                 SPDK_ERRLOG("Failed to connect rqpair=%p\n", rqpair);
    3172           0 :                                 nvme_rdma_fail_qpair(qpair, 0);
    3173             :                         }
    3174             :                 }
    3175             :         }
    3176             : 
    3177           0 :         STAILQ_FOREACH_SAFE(qpair, &tgroup->connected_qpairs, poll_group_stailq, tmp_qpair) {
    3178           0 :                 rqpair = nvme_rdma_qpair(qpair);
    3179             : 
    3180           0 :                 if (spdk_likely(nvme_qpair_get_state(qpair) != NVME_QPAIR_CONNECTING)) {
    3181           0 :                         nvme_rdma_qpair_process_cm_event(rqpair);
    3182             :                 }
    3183             : 
    3184           0 :                 if (spdk_unlikely(qpair->transport_failure_reason != SPDK_NVME_QPAIR_FAILURE_NONE)) {
    3185           0 :                         rc2 = -ENXIO;
    3186           0 :                         nvme_rdma_fail_qpair(qpair, 0);
    3187             :                 }
    3188             :         }
    3189             : 
    3190           0 :         completions_allowed = completions_per_qpair * tgroup->num_connected_qpairs;
    3191           0 :         if (spdk_likely(group->num_pollers)) {
    3192           0 :                 completions_per_poller = spdk_max(completions_allowed / group->num_pollers, 1);
    3193             :         }
    3194             : 
    3195           0 :         STAILQ_FOREACH(poller, &group->pollers, link) {
    3196           0 :                 poller_completions = 0;
    3197           0 :                 rdma_completions = 0;
    3198             :                 do {
    3199           0 :                         poller->stats.polls++;
    3200           0 :                         batch_size = spdk_min((completions_per_poller - poller_completions), MAX_COMPLETIONS_PER_POLL);
    3201           0 :                         rc = nvme_rdma_cq_process_completions(poller->cq, batch_size, poller, NULL, &rdma_completions);
    3202           0 :                         if (rc <= 0) {
    3203           0 :                                 if (rc == -ECANCELED) {
    3204           0 :                                         return -EIO;
    3205           0 :                                 } else if (rc == 0) {
    3206           0 :                                         poller->stats.idle_polls++;
    3207             :                                 }
    3208           0 :                                 break;
    3209             :                         }
    3210             : 
    3211           0 :                         poller_completions += rc;
    3212           0 :                 } while (poller_completions < completions_per_poller);
    3213           0 :                 total_completions += poller_completions;
    3214           0 :                 poller->stats.completions += rdma_completions;
    3215           0 :                 if (poller->srq) {
    3216           0 :                         nvme_rdma_poller_submit_recvs(poller);
    3217             :                 }
    3218             :         }
    3219             : 
    3220           0 :         TAILQ_FOREACH_SAFE(rqpair, &group->active_qpairs, link_active, tmp_rqpair) {
    3221           0 :                 nvme_rdma_qpair_process_submits(group, rqpair);
    3222             :         }
    3223             : 
    3224           0 :         return rc2 != 0 ? rc2 : total_completions;
    3225             : }
    3226             : 
    3227             : /*
    3228             :  * Handle disconnected qpairs when interrupt support gets added.
    3229             :  */
    3230             : static void
    3231           0 : nvme_rdma_poll_group_check_disconnected_qpairs(struct spdk_nvme_transport_poll_group *tgroup,
    3232             :                 spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
    3233             : {
    3234           0 : }
    3235             : 
    3236             : static int
    3237           1 : nvme_rdma_poll_group_destroy(struct spdk_nvme_transport_poll_group *tgroup)
    3238             : {
    3239           1 :         struct nvme_rdma_poll_group     *group = nvme_rdma_poll_group(tgroup);
    3240             : 
    3241           1 :         if (!STAILQ_EMPTY(&tgroup->connected_qpairs) || !STAILQ_EMPTY(&tgroup->disconnected_qpairs)) {
    3242           0 :                 return -EBUSY;
    3243             :         }
    3244             : 
    3245           1 :         nvme_rdma_poll_group_free_pollers(group);
    3246           1 :         free(group);
    3247             : 
    3248           1 :         return 0;
    3249             : }
    3250             : 
    3251             : static int
    3252           3 : nvme_rdma_poll_group_get_stats(struct spdk_nvme_transport_poll_group *tgroup,
    3253             :                                struct spdk_nvme_transport_poll_group_stat **_stats)
    3254             : {
    3255             :         struct nvme_rdma_poll_group *group;
    3256             :         struct spdk_nvme_transport_poll_group_stat *stats;
    3257             :         struct spdk_nvme_rdma_device_stat *device_stat;
    3258             :         struct nvme_rdma_poller *poller;
    3259           3 :         uint32_t i = 0;
    3260             : 
    3261           3 :         if (tgroup == NULL || _stats == NULL) {
    3262           2 :                 SPDK_ERRLOG("Invalid stats or group pointer\n");
    3263           2 :                 return -EINVAL;
    3264             :         }
    3265             : 
    3266           1 :         group = nvme_rdma_poll_group(tgroup);
    3267           1 :         stats = calloc(1, sizeof(*stats));
    3268           1 :         if (!stats) {
    3269           0 :                 SPDK_ERRLOG("Can't allocate memory for RDMA stats\n");
    3270           0 :                 return -ENOMEM;
    3271             :         }
    3272           1 :         stats->trtype = SPDK_NVME_TRANSPORT_RDMA;
    3273           1 :         stats->rdma.num_devices = group->num_pollers;
    3274             : 
    3275           1 :         if (stats->rdma.num_devices == 0) {
    3276           0 :                 *_stats = stats;
    3277           0 :                 return 0;
    3278             :         }
    3279             : 
    3280           1 :         stats->rdma.device_stats = calloc(stats->rdma.num_devices, sizeof(*stats->rdma.device_stats));
    3281           1 :         if (!stats->rdma.device_stats) {
    3282           0 :                 SPDK_ERRLOG("Can't allocate memory for RDMA device stats\n");
    3283           0 :                 free(stats);
    3284           0 :                 return -ENOMEM;
    3285             :         }
    3286             : 
    3287           3 :         STAILQ_FOREACH(poller, &group->pollers, link) {
    3288           2 :                 device_stat = &stats->rdma.device_stats[i];
    3289           2 :                 device_stat->name = poller->device->device->name;
    3290           2 :                 device_stat->polls = poller->stats.polls;
    3291           2 :                 device_stat->idle_polls = poller->stats.idle_polls;
    3292           2 :                 device_stat->completions = poller->stats.completions;
    3293           2 :                 device_stat->queued_requests = poller->stats.queued_requests;
    3294           2 :                 device_stat->total_send_wrs = poller->stats.rdma_stats.send.num_submitted_wrs;
    3295           2 :                 device_stat->send_doorbell_updates = poller->stats.rdma_stats.send.doorbell_updates;
    3296           2 :                 device_stat->total_recv_wrs = poller->stats.rdma_stats.recv.num_submitted_wrs;
    3297           2 :                 device_stat->recv_doorbell_updates = poller->stats.rdma_stats.recv.doorbell_updates;
    3298           2 :                 i++;
    3299             :         }
    3300             : 
    3301           1 :         *_stats = stats;
    3302             : 
    3303           1 :         return 0;
    3304             : }
    3305             : 
    3306             : static void
    3307           1 : nvme_rdma_poll_group_free_stats(struct spdk_nvme_transport_poll_group *tgroup,
    3308             :                                 struct spdk_nvme_transport_poll_group_stat *stats)
    3309             : {
    3310           1 :         if (stats) {
    3311           1 :                 free(stats->rdma.device_stats);
    3312             :         }
    3313           1 :         free(stats);
    3314           1 : }
    3315             : 
    3316             : static int
    3317           4 : nvme_rdma_ctrlr_get_memory_domains(const struct spdk_nvme_ctrlr *ctrlr,
    3318             :                                    struct spdk_memory_domain **domains, int array_size)
    3319             : {
    3320           4 :         struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(ctrlr->adminq);
    3321             : 
    3322           4 :         if (domains && array_size > 0) {
    3323           1 :                 domains[0] = rqpair->rdma_qp->domain;
    3324             :         }
    3325             : 
    3326           4 :         return 1;
    3327             : }
    3328             : 
    3329             : void
    3330           0 : spdk_nvme_rdma_init_hooks(struct spdk_nvme_rdma_hooks *hooks)
    3331             : {
    3332           0 :         g_nvme_hooks = *hooks;
    3333           0 : }
    3334             : 
    3335             : const struct spdk_nvme_transport_ops rdma_ops = {
    3336             :         .name = "RDMA",
    3337             :         .type = SPDK_NVME_TRANSPORT_RDMA,
    3338             :         .ctrlr_construct = nvme_rdma_ctrlr_construct,
    3339             :         .ctrlr_scan = nvme_fabric_ctrlr_scan,
    3340             :         .ctrlr_destruct = nvme_rdma_ctrlr_destruct,
    3341             :         .ctrlr_enable = nvme_rdma_ctrlr_enable,
    3342             : 
    3343             :         .ctrlr_set_reg_4 = nvme_fabric_ctrlr_set_reg_4,
    3344             :         .ctrlr_set_reg_8 = nvme_fabric_ctrlr_set_reg_8,
    3345             :         .ctrlr_get_reg_4 = nvme_fabric_ctrlr_get_reg_4,
    3346             :         .ctrlr_get_reg_8 = nvme_fabric_ctrlr_get_reg_8,
    3347             :         .ctrlr_set_reg_4_async = nvme_fabric_ctrlr_set_reg_4_async,
    3348             :         .ctrlr_set_reg_8_async = nvme_fabric_ctrlr_set_reg_8_async,
    3349             :         .ctrlr_get_reg_4_async = nvme_fabric_ctrlr_get_reg_4_async,
    3350             :         .ctrlr_get_reg_8_async = nvme_fabric_ctrlr_get_reg_8_async,
    3351             : 
    3352             :         .ctrlr_get_max_xfer_size = nvme_rdma_ctrlr_get_max_xfer_size,
    3353             :         .ctrlr_get_max_sges = nvme_rdma_ctrlr_get_max_sges,
    3354             : 
    3355             :         .ctrlr_create_io_qpair = nvme_rdma_ctrlr_create_io_qpair,
    3356             :         .ctrlr_delete_io_qpair = nvme_rdma_ctrlr_delete_io_qpair,
    3357             :         .ctrlr_connect_qpair = nvme_rdma_ctrlr_connect_qpair,
    3358             :         .ctrlr_disconnect_qpair = nvme_rdma_ctrlr_disconnect_qpair,
    3359             : 
    3360             :         .ctrlr_get_memory_domains = nvme_rdma_ctrlr_get_memory_domains,
    3361             : 
    3362             :         .qpair_abort_reqs = nvme_rdma_qpair_abort_reqs,
    3363             :         .qpair_reset = nvme_rdma_qpair_reset,
    3364             :         .qpair_submit_request = nvme_rdma_qpair_submit_request,
    3365             :         .qpair_process_completions = nvme_rdma_qpair_process_completions,
    3366             :         .qpair_iterate_requests = nvme_rdma_qpair_iterate_requests,
    3367             :         .qpair_authenticate = nvme_rdma_qpair_authenticate,
    3368             :         .admin_qpair_abort_aers = nvme_rdma_admin_qpair_abort_aers,
    3369             : 
    3370             :         .poll_group_create = nvme_rdma_poll_group_create,
    3371             :         .poll_group_connect_qpair = nvme_rdma_poll_group_connect_qpair,
    3372             :         .poll_group_disconnect_qpair = nvme_rdma_poll_group_disconnect_qpair,
    3373             :         .poll_group_add = nvme_rdma_poll_group_add,
    3374             :         .poll_group_remove = nvme_rdma_poll_group_remove,
    3375             :         .poll_group_process_completions = nvme_rdma_poll_group_process_completions,
    3376             :         .poll_group_check_disconnected_qpairs = nvme_rdma_poll_group_check_disconnected_qpairs,
    3377             :         .poll_group_destroy = nvme_rdma_poll_group_destroy,
    3378             :         .poll_group_get_stats = nvme_rdma_poll_group_get_stats,
    3379             :         .poll_group_free_stats = nvme_rdma_poll_group_free_stats,
    3380             : };
    3381             : 
    3382           1 : SPDK_NVME_TRANSPORT_REGISTER(rdma, &rdma_ops);

Generated by: LCOV version 1.15