LCOV - code coverage report
Current view: top level - lib/nvme - nvme_rdma.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 673 1553 43.3 %
Date: 2024-11-05 10:06:02 Functions: 44 90 48.9 %

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

Generated by: LCOV version 1.15